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