All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileMuons_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file FileMuons_module.cc
3 /// \brief Generator for muons from a file.
4 ///
5 /// Module designed to produce a set list of particles for a MC event
6 ///
7 /// \author echurch@fnal.gov
8 ////////////////////////////////////////////////////////////////////////
9 // C++ includes.
10 #include <cmath>
11 #include <fstream>
12 #include <iostream>
13 #include <memory>
14 #include <stdio.h>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 // Framework includes
20 #include "art/Framework/Core/EDProducer.h"
21 #include "art/Framework/Principal/Event.h"
22 #include "art/Framework/Principal/Run.h"
23 #include "art/Framework/Core/ModuleMacros.h"
24 #include "art/Framework/Services/Registry/ServiceHandle.h"
25 #include "messagefacility/MessageLogger/MessageLogger.h"
26 #include "fhiclcpp/ParameterSet.h"
27 
28 // nusimdata includes
29 #include "nusimdata/SimulationBase/MCTruth.h"
30 #include "nusimdata/SimulationBase/MCParticle.h"
31 
32 // lar includes
35 
36 #include "TVector3.h"
37 #include "TDatabasePDG.h"
38 #include "TFile.h"
39 #include "TTree.h"
40 
41 
42 namespace evgen {
43 
44  /// module to produce single or multiple specified particles in the detector
45  class FileMuons : public art::EDProducer {
46 
47  public:
48  explicit FileMuons(fhicl::ParameterSet const& pset);
49 
50  // This is called for each event.
51  void produce(art::Event& evt);
52  void beginJob();
53  void beginRun(art::Run& run);
54  void endJob();
55 
56  private:
57 
58  void ReadEvents(simb::MCTruth &mct);
59 
60  int fEventNumberOffset; // Where in file to start.
61  std::vector<int> fPDG;
62  std::vector<double> fXYZ_Off;
63  std::string fFileName;
64  std::string fMuonsFileType;
65  std::string fTreeName;
66  std::vector<std::string> fBranchNames;
67 
68  std::ifstream *fMuonFile;
69  TFile *fMuonFileR;
70  TTree *TNtuple;
71  unsigned int countFile;
72 
73  Float_t xtmp, ytmp, ztmp;
74  Float_t pxtmp, pytmp, pztmp;
75  Float_t charge;
76  Float_t E;
77  Float_t costheta;
78  Float_t phi;
79  Float_t xdet;
80  Float_t ydet;
81  Float_t zdet;
82 
83  TBranch *b_x; //!
84  TBranch *b_y; //!
85  TBranch *b_z; //!
86  TBranch *b_E; //!
87  TBranch *b_costheta; //!
88  TBranch *b_phi; //!
89  TBranch *b_xdet; //!
90  TBranch *b_ydet; //!
91  TBranch *b_zdet; //!
92  TBranch *b_px; //!
93  TBranch *b_py; //!
94  TBranch *b_pz; //!
95  TBranch *b_charge; //!
96 
97 
98  };
99 } // namespace
100 
101 
102 
103 namespace evgen{
104 
105  //____________________________________________________________________________
106  FileMuons::FileMuons(fhicl::ParameterSet const& pset)
107  : EDProducer{pset}
108  , fEventNumberOffset(pset.get< int >("EventNumberOffset"))
109  , fPDG (pset.get< std::vector<int> >("PDG") )
110  , fXYZ_Off (pset.get< std::vector<double> >("InitialXYZOffsets"))
111  , fFileName (pset.get< std::string >("FileName") )
112  , fMuonsFileType (pset.get< std::string >("MuonsFileType") )
113  , fTreeName (pset.get< std::string >("TreeName") )
114  , fBranchNames (pset.get< std::vector<std::string> >("BranchNames") )
115  {
116 
117  produces< std::vector<simb::MCTruth> >();
119 
120  }
121 
122  //____________________________________________________________________________
124  {
126  mf::LogInfo("FileMuons : starting at event ") << countFile <<std::endl;
127 
128  if (fMuonsFileType.compare("source")==0)
129  {
130  std::cout << "FileMuons: Not yet equipped to walk through muons with TFS mojo."<< std::endl;
131  }
132  else if (fMuonsFileType.compare("root")==0)
133  {
134  std::cout << "FileMuons: You have chosen to read muons from Root File " << fFileName << std::endl;
135  }
136  else if (fMuonsFileType.compare("text")==0)
137  {
138  std::cout << "FileMuons: You have chosen to read muons from " << fFileName << "." << std::endl;
139  }
140  else
141  {
142  std::cout << "FileMuons: You must specify one of source/text/root file to read for muons."<< std::endl;
143 
144  }
145 
146  if (fMuonsFileType.compare("text")==0)
147  {
148  fMuonFile = new std::ifstream(fFileName.c_str());
149  long begin = fMuonFile->tellg();
150  fMuonFile->seekg (0, std::ios::end);
151  long end = fMuonFile->tellg();
152  std::cout << "FileMuons: " << fFileName << " size is: " << (end-begin) << " bytes.\n";
153  fMuonFile->seekg (0, std::ios::beg);
154 
155  for (unsigned int header=0; header<3 && fMuonFile->good(); ++header)
156  {
157  std::string line;
158  getline(*fMuonFile,line);
159  }
160  if (!fMuonFile->good())
161  {
162  std::cout << "FileMuons: Problem reading muon file header."<< std::endl;
163  }
164  } // fMuonsFileType is a text file.
165  else if (fMuonsFileType.compare("root")==0)
166  {
167  fMuonFileR = new TFile(fFileName.c_str(),"READ");
168  TNtuple = (TTree*)(fMuonFileR->Get(fTreeName.c_str()));
169 
170  TNtuple->SetBranchAddress("x", &xtmp, &b_x);
171  TNtuple->SetBranchAddress("y", &ytmp, &b_y);
172  TNtuple->SetBranchAddress("z", &ztmp, &b_z);
173  TNtuple->SetBranchAddress("E", &E, &b_E);
174  TNtuple->SetBranchAddress("costheta", &costheta, &b_costheta);
175  TNtuple->SetBranchAddress("phi", &phi, &b_phi);
176  TNtuple->SetBranchAddress("xdet", &xdet, &b_xdet);
177  TNtuple->SetBranchAddress("ydet", &ydet, &b_ydet);
178  TNtuple->SetBranchAddress("zdet", &zdet, &b_zdet);
179  TNtuple->SetBranchAddress("px", &pxtmp, &b_px);
180  TNtuple->SetBranchAddress("py", &pytmp, &b_py);
181  TNtuple->SetBranchAddress("pz", &pztmp, &b_pz);
182  TNtuple->SetBranchAddress("charge", &charge, &b_charge);
183 
184  } // fMuonsFileType is a root file.
185 
186  }
187 
188  //____________________________________________________________________________
190  {
191  if (fMuonsFileType.compare("text")==0)
192  fMuonFile->close();
193  if (fMuonsFileType.compare("root")==0)
194  fMuonFileR->Close();
195  }
196 
197  //____________________________________________________________________________
198  void FileMuons::beginRun(art::Run& run)
199  {
200  art::ServiceHandle<geo::Geometry const> geo;
201  run.put(std::make_unique<sumdata::RunData>(geo->DetectorName()));
202  }
203 
204  //____________________________________________________________________________
205  void FileMuons::produce(art::Event& evt)
206  {
207 
208  ///unique_ptr allows ownership to be transferred to the art::Event after the put statement
209  std::unique_ptr< std::vector<simb::MCTruth> > truthcol(new std::vector<simb::MCTruth>);
210 
211  simb::MCTruth truth;
212  truth.SetOrigin(simb::kSingleParticle);
213  ReadEvents(truth);
214 
215 // std::cout << "put mctruth into the vector" << std::endl;
216  truthcol->push_back(truth);
217 
218 // std::cout << "add vector to the event " << truthcol->size() << std::endl;
219  evt.put(std::move(truthcol));
220 
221  return;
222  }
223 
224  //____________________________________________________________________________
225  void FileMuons::ReadEvents(simb::MCTruth &mct)
226  {
227 
228 // std::cout << "size of particle vector is " << fPDG.size() << std::endl;
229 
230  ///every event will have one of each particle species in the fPDG array
231  for (unsigned int i=0; i<fPDG.size(); ++i) {
232 
233  // Choose momentum
234  //double p = 0.0;
235  double m(0.108);
236 
237  TVector3 x;
238  TVector3 p;
239  Double_t q = 0.;
240  Int_t pdgLocal;
241 
242  if (fMuonsFileType.compare("text")==0)
243  {
244 
245  std::string line;
246  getline(*fMuonFile,line);
247  if (!fMuonFile->good())
248  {
249  std::cout << "FileMuons: Problem reading muon file line ...."<< countFile << ". Perhaps you've exhausted the events in " << fFileName << std::endl; exit(0);
250  }
251  else
252  {
253  // std::cout << "FileMuons: getline() gives "<< line << " for event " << countFile << std::endl;
254  }
255  countFile++;
256 
257  MF_LOG_DEBUG("FileMuons: countFile is ") << countFile <<std::endl;
258  char * cstr, *ptok;
259 
260  // Split this line into tokens
261  cstr = new char [line.size()+1];
262  strcpy (cstr, line.c_str());
263  // cstr now contains a c-string copy of str
264  ptok=strtok (cstr,"*");
265  unsigned int fieldCount = 0;
266  unsigned int posIndex = 0;
267  unsigned int pIndex = 0;
268  while (ptok!=NULL)
269  {
270  // std::cout << ptok << std::endl;
271  ptok=strtok(NULL,"*");
272  if (fieldCount==9 || fieldCount==10 || fieldCount==11)
273  {
274  p[pIndex] = atof(ptok); pIndex++;
275  // std::cout << ptok << std::endl;
276  }
277  if (fieldCount==6 || fieldCount==7 || fieldCount==8)
278  {
279  x[posIndex] = atof(ptok);
280  // make the z axis point up for x, as with p
281  if (posIndex==2) {x[posIndex] = -1.0*x[posIndex];}
282  posIndex++;
283  }
284  if (fieldCount==12)
285  {
286  q = atof(ptok);
287  }
288  fieldCount++;
289  }
290 
291  delete[] cstr;
292 
293  }
294  else if (fMuonsFileType.compare("root")==0) // from root file
295  {
296  /*
297  // Don't use this yet. Keep the specific branch-by-branch identification.
298  for (unsigned int ii=0;ii<fBranchNames.size();ii++)
299  {
300  TNtuple->SetBranchAddress(fBranchNames[ii], x+ii);
301  }
302  */
303  // TNtuple->ResetBranchAddresses();
304  TNtuple->GetEntry(countFile);
305  //TNtuple->Show(countFile);
306 
307  x.SetXYZ(xdet,ydet,-zdet); // as with txt file, make z point up.
308  // Watch for units change to mm in Modern JdJ files!!
309  // This is for pre Spring-2012 JdJ Ntuples.
310  p.SetXYZ(pxtmp,pytmp,pztmp);
311  q = charge;
312 
313  countFile++;
314 
315  } // End read.
316 
317  static TDatabasePDG pdgt;
318  pdgLocal = -q*fPDG[i];
319 
320  TParticlePDG* pdgp = pdgt.GetParticle(pdgLocal);
321  if (pdgp) m = pdgp->Mass();
322 
323 
324  // std::cout << "set the position "<<std::endl;
325  // This gives coordinates at the center of the 300mx300m plate that is 3m above top of
326  // cavern. Got these by histogramming deJong's xdet,ydet,zdet.
327  const double cryoGap = 15.0;
328  x[0] -= fXYZ_Off[0];
329  x[1] -= fXYZ_Off[1];
330  x[2] -= fXYZ_Off[2]; // 3 for plate height above top of cryostat.
331  // Now, must rotate to TPC coordinates. Let's orient TPC axis along z axis,
332  // Cosmics, mostly going along deJong's +z axis must be going along TPC -y axis.
333  x.RotateX(-M_PI/2);
334  p.RotateX(-M_PI/2);
335  //add vector of the position of the center of the point between Cryostats
336  // level with top. (To which I've added 3m - in above code - in height.)
337  // This is referenced from origin at center-right of first cryostat.
338  art::ServiceHandle<geo::Geometry const> geom;
339  TVector3 off3(geom->CryostatHalfWidth()*0.01,geom->CryostatHalfHeight()*0.01,geom->CryostatLength()*0.01+cryoGap*0.01/2.0) ;
340  x += off3;
341 
342  TLorentzVector pos(x[0]*100.0, x[1]*100.0, x[2]*100.0, 0.0);
343  TLorentzVector pvec(p[0]*1000.0,p[1]*1000.0,p[2]*1000.0,std::sqrt(p.Mag2()*1000.0*1000.0+m*m));
344  std::cout << "x[m] and p [TeV] are " << std::endl;
345  x.Print();
346  p.Print();
347 
348  int trackid = -1*(i+1); // set track id to -i as these are all primary particles and have id <= 0
349  std::string primary("primary");
350  simb::MCParticle part(trackid, pdgLocal, primary);
351  part.AddTrajectoryPoint(pos, pvec);
352 
353  // std::cout << "add the particle to the primary" << std::endl;
354 
355  mct.Add(part);
356 
357  }//end loop over particles
358 
359  return;
360  }
361 
362  DEFINE_ART_MODULE(FileMuons)
363 
364 }//end namespace evgen
365 ////////////////////////////////////////////////////////////////////////
std::vector< double > fXYZ_Off
process_name opflash particleana ie x
pdgs p
Definition: selectors.fcl:22
std::string fMuonsFileType
void ReadEvents(simb::MCTruth &mct)
void beginRun(art::Run &run)
produces< sumdata::RunData, art::InRun >()
std::ifstream * fMuonFile
tuple m
now if test mode generate materials, CRT shell, world, gdml header else just generate CRT shell for u...
std::vector< std::string > fBranchNames
createEngine fPDG
unsigned int countFile
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
std::string fFileName
module to produce single or multiple specified particles in the detector
FileMuons(fhicl::ParameterSet const &pset)
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
TCEvent evt
Definition: DataStructs.cxx:8
std::vector< int > fPDG
art framework interface to geometry description
BEGIN_PROLOG could also be cout
std::string fTreeName
void produce(art::Event &evt)