GenieOutput_module.cc
Go to the documentation of this file.
3 
8 
11 
15 
16 #include "nutools/EventGeneratorBase/GENIE/GENIE2ART.h"
17 #include "nutools/EventGeneratorBase/GENIE/MCTruthAndFriendsItr.h"
18 
19 // GENIE includes
20 #ifdef GENIE_PRE_R3
21  #include "GENIE/Messenger/Messenger.h"
22  #include "Ntuple/NtpMCFormat.h"
23  #include "Ntuple/NtpWriter.h"
24  #include "Ntuple/NtpMCEventRecord.h"
25  //#include "Ntuple/NtpMCTreeHeader.h"
26  #include "PDG/PDGLibrary.h"
27  #include "GHEP/GHepRecord.h"
28 #else
29  #include "GENIE/Framework/Messenger/Messenger.h"
30  #include "GENIE/Framework/ParticleData/PDGLibrary.h"
31  #include "GENIE/Framework/GHEP/GHepRecord.h"
32  #include "GENIE/Framework/Ntuple/NtpMCFormat.h"
33  #include "GENIE/Framework/Ntuple/NtpWriter.h"
34  #include "GENIE/Framework/Ntuple/NtpMCEventRecord.h"
35  // #include "GENIE/Framework/Ntuple/NtpMCTreeHeader.h"
36 #endif
37 
38 #include <iostream>
39 #include <iomanip>
40 #include <fstream>
41 #include <sstream>
42 
43 // Necessary because the GENIE LOG_* macros don't fully qualify Messenger
44 using genie::Messenger;
45 
46 namespace evg {
47  class GenieOutput;
48 
50  // hold/document fcl parameters
51  template<class T> using Atom = fhicl::Atom<T>;
52  template<class T> using Sequence = fhicl::Sequence<T>;
53  template<class T> using Table = fhicl::Table<T>;
55  using Name = fhicl::Name;
56 
58  Name("inputModuleLabels"),
59  Comment("list of input module labels to use, if empty use everything")
60  // { } // how to default this to empty ??
61  };
63  Name("outputGHEPFile"),
64  Comment("name of file to write in std GENIE gntp.*.ghep.root format\n"
65  "if blank don't write file, if name contains '%l' then\n"
66  "events from different input labels will go to separate outputs\n"),
67  ""
68  };
70  Name("dumpFilePattern"),
71  Comment("name of file for formatted dumps; if name contains '%l' then\n"
72  "events from different input labels will go to separate streams\n"
73  "if blank use std::cout"),
74  ""
75  };
77  Name("dumpGeniePrintLevel"),
78  Comment("print fetched genie::EventRecord -1=no, 13=max info\n"
79  "see GENIE manual for legal values"),
80  -1
81  };
83  Name("dumpMCTruth"),
84  Comment("dump the MCTruth objects (std:cout) as they're retrieved"),
85  false
86  };
88  Name("dumpGTruth"),
89  Comment("dump the GTruth objects (std:cout) as they're retrieved"),
90  false
91  };
93  Name("dumpMCFlux"),
94  Comment("dump the MCFlux objects (std:cout) as they're retrieved"),
95  false
96  };
97  }; // end-of GenieOutputParams
98 }
99 
101 
102 public:
103 
104  // Allow 'art --print-description' to work
106 
107  //explicit GenieOutput(fhicl::ParameterSet const & p);
108  explicit GenieOutput(const Parameters& params);
109 
110  // The destructor generated by the compiler is fine for classes
111  // without bare pointers or other resource use.
112  ~GenieOutput();
113 
114  // Plugins should not be copied or assigned.
115  GenieOutput(GenieOutput const &) = delete;
116  GenieOutput(GenieOutput &&) = delete;
117  GenieOutput & operator = (GenieOutput const &) = delete;
118  GenieOutput & operator = (GenieOutput &&) = delete;
119 
120  // Required functions.
121  void analyze(art::Event const & e) override;
122 
123  // Selected optional functions.
124  /*
125  void beginJob() override;
126  void beginRun(art::Run const & r) override;
127  void beginSubRun(art::SubRun const & sr) override;
128  void endJob() override;
129  void endRun(art::Run const & r) override;
130  void endSubRun(art::SubRun const & sr) override;
131  void reconfigure(fhicl::ParameterSet const & p) override;
132  void respondToCloseInputFile(art::FileBlock const & fb) override;
133  void respondToCloseOutputFiles(art::FileBlock const & fb) override;
134  void respondToOpenInputFile(art::FileBlock const & fb) override;
135  void respondToOpenOutputFiles(art::FileBlock const & fb) override;
136  */
137 
138 private:
139 
140  // private methods
141  genie::NtpWriter* FetchNtpWriter(const std::string& label);
142  std::ostream* FetchDumpStream(const std::string& label);
143 
144 
145  // member data here.
146 
148 
149  std::vector<std::string> fInputModuleLabels; ///< label(s) of existing MCTruth/GTruth/MCFlux
153 
157 
160 
161  std::map<std::string,genie::NtpWriter*> fOutputNtpWriters;
162  std::map<std::string,std::ostream*> fDumpStreams;
163 
164 };
165 
166 
167 //evg::GenieOutput::GenieOutput(fhicl::ParameterSet const & pset)
168 // : EDAnalyzer(pset)
170  : EDAnalyzer(params)
171  , fParams(params)
172  , fSeparateOutputNtpWriters(false)
173  , fSeparateDumpStreams(false)
174 {
175  // trigger early initialization of PDG database & message service
177 
178  fInputModuleLabels = fParams().inputModuleLabels();
179  fOutputGHEPFilePattern = fParams().outputGHEPFilePattern();
180  fDumpFilePattern = fParams().dumpFilePattern();
181  fDumpGeniePrintLevel = fParams().dumpGeniePrintLevel();
182 
184  ( fOutputGHEPFilePattern.find("%l") != std::string::npos );
186  ( fDumpFilePattern.find("%l") != std::string::npos );
187 
188  fDumpMCTruth = fParams().dumpMCTruth();
189  fDumpGTruth = fParams().dumpGTruth();
190  fDumpMCFlux = fParams().dumpMCFlux();
191 
192  /*
193  mf::LogInfo("GenieOutput") << "##### Dump options "
194  << fDumpMCTruth << " "
195  << fDumpGTruth << " "
196  << fDumpMCFlux << " " ;
197  */
198 
199 }
200 
202 {
203 
204  // release resources
206  fOutputNtpWriters.begin();
207  for ( ; mitro != fOutputNtpWriters.end(); ++mitro ) {
208  std::string label = mitro->first;
209  genie::NtpWriter* ntpw = mitro->second;
210  if ( ntpw ) {
211  // close out the file
212  ntpw->Save();
213  delete ntpw;
214  }
215  mitro->second = 0;
216  }
217 
219  for ( ; mitrd != fDumpStreams.end(); ++mitrd ) {
220  std::string label = mitrd->first;
221  std::ofstream* fout = dynamic_cast<std::ofstream*>(mitrd->second);
222  if ( fout ) {
223  fout->flush();
224  fout->close();
225  delete fout;
226  }
227  mitrd->second = 0;
228  }
229 }
230 
232 {
233  //std::cerr << "evg::GenieOutput::analyze " << std::endl;
234 
236 
237  bool flag = true;
238  int indx = -1;
239  while ( ( flag = mcitr.Next() ) ) {
240  ++indx;
241  std::string label = mcitr.GetLabel();
242  const simb::MCTruth* pmctruth = mcitr.GetMCTruth();
243  const simb::GTruth* pgtruth = mcitr.GetGTruth();
244  const simb::MCFlux* pmcflux = mcitr.GetMCFlux();
245 
246  static int ievt = -1; // ! bah!
247  ++ievt;
248 
249  static const simb::GTruth nullGTruth;
250  if ( ! pgtruth ) {
251  mf::LogInfo("GenieOutput") << "##### no GTruth ";
252  pgtruth = &nullGTruth;
253  }
254 
255  genie::EventRecord* grec = evgb::RetrieveGHEP(*pmctruth,*pgtruth);
256 
257  genie::NtpWriter* ntpWriter = FetchNtpWriter(label);
258  if ( ntpWriter ) {
259  // check ownership! (might need copy)
260  ntpWriter->AddEventRecord(ievt,grec);
261  }
262 
263  std::ostream* osdump = FetchDumpStream(label);
264  if ( osdump ) {
265  *osdump
266  << " ** Event: GenieOutput_module " << ievt
267  << *grec;
268  osdump->flush();
269  }
270 
271  if ( fDumpMCTruth || fDumpGTruth || fDumpMCFlux ) {
272  std::ostringstream dumpSimBaseObj;
273  dumpSimBaseObj << " after Next() " << indx << " " << flag
274  << std::endl;;
275  if ( fDumpMCTruth ) {
276  if ( pmctruth ) dumpSimBaseObj << *pmctruth << std::endl;
277  else dumpSimBaseObj << "no simb::MCTruth available" << std::endl;
278  }
279  if ( fDumpGTruth ) {
280  if ( pgtruth ) {
281  dumpSimBaseObj << *pgtruth << std::endl;
282  //dumpSimBaseObj << "sorry no operator<< exists for simb::GTruth"
283  // << " - someone should write one" << std::endl;
284  } else dumpSimBaseObj << "no simb::GTruth available" << std::endl;
285  }
286  if ( fDumpMCFlux ) {
287  if ( pmcflux ) dumpSimBaseObj << *pmcflux << std::endl;
288  else dumpSimBaseObj << "no simb::MCFlux available" << std::endl;
289  }
290  mf::LogInfo("GenieOutput") << dumpSimBaseObj.str();
291  }
292 
293  delete grec; // don't leak stuff
294  } // loop over MCTruthAndFriends
295 
296 }
297 
299 
300  if ( fOutputGHEPFilePattern == "" ) return 0;
301 
302  genie::NtpWriter* ntpwret = 0;
304  else ntpwret = fOutputNtpWriters["*"];
305 
306  if ( ntpwret ) return ntpwret; // already openned
307 
308  // nope?? okay
309 
310  std::string finalFileName = fOutputGHEPFilePattern;
312  size_t posl = finalFileName.find("%l");
313  if ( posl != std::string::npos ) {
314  finalFileName.replace(posl,2,label);
315  }
316  }
317  ntpwret = new genie::NtpWriter(genie::kNFGHEP,0);
318  ntpwret->CustomizeFilename(finalFileName);
319  ntpwret->Initialize();
320 
322  else fOutputNtpWriters["*"] = ntpwret;
323 
324  return ntpwret;
325 
326 
327 }
328 
329 
331  if (fDumpGeniePrintLevel < 0 ) return 0;
333 
334  std::ostream* osret = 0;
335  if ( fSeparateDumpStreams ) osret = fDumpStreams[label];
336  else osret = fDumpStreams["*"];
337 
338  if ( osret ) return osret; // already openned
339 
340  // nope?? okay
341 
342  if ( fDumpFilePattern == "" ||
343  fDumpFilePattern == "--" ||
344  fDumpFilePattern == "cout" ||
345  fDumpFilePattern == "std::cout" ) {
346  // standardize so we don't check all these again
347  fDumpFilePattern = "std::cout";
348  osret = &(std::cout);
349  } else {
350  std::string finalFileName = fDumpFilePattern;
351  if ( fSeparateDumpStreams ) {
352  size_t posl = finalFileName.find("%l");
353  if ( posl != std::string::npos ) {
354  finalFileName.replace(posl,2,label);
355  }
356  }
357  osret = new std::ofstream(finalFileName.c_str(),
358  std::ios_base::trunc|std::ios_base::out);
359 
360  }
361  if ( fSeparateDumpStreams ) fDumpStreams[label] = osret;
362  else fDumpStreams["*"] = osret;
363 
364  return osret;
365 
366 }
367 
368 /*
369 void evg::GenieOutput::beginJob()
370 {
371  // Implementation of optional member function here.
372 }
373 
374 void evg::GenieOutput::beginRun(art::Run const & r)
375 {
376  // Implementation of optional member function here.
377 }
378 
379 void evg::GenieOutput::beginSubRun(art::SubRun const & sr)
380 {
381  // Implementation of optional member function here.
382 }
383 
384 void evg::GenieOutput::endJob()
385 {
386  // Implementation of optional member function here.
387 }
388 
389 void evg::GenieOutput::endRun(art::Run const & r)
390 {
391  // Implementation of optional member function here.
392 }
393 
394 void evg::GenieOutput::endSubRun(art::SubRun const & sr)
395 {
396  // Implementation of optional member function here.
397 }
398 
399 void evg::GenieOutput::reconfigure(fhicl::ParameterSet const & p)
400 {
401  // Implementation of optional member function here.
402 }
403 
404 void evg::GenieOutput::respondToCloseInputFile(art::FileBlock const & fb)
405 {
406  // Implementation of optional member function here.
407 }
408 
409 void evg::GenieOutput::respondToCloseOutputFiles(art::FileBlock const & fb)
410 {
411  // Implementation of optional member function here.
412 }
413 
414 void evg::GenieOutput::respondToOpenInputFile(art::FileBlock const & fb)
415 {
416  // Implementation of optional member function here.
417 }
418 
419 void evg::GenieOutput::respondToOpenOutputFiles(art::FileBlock const & fb)
420 {
421  // Implementation of optional member function here.
422 }
423 */
424 
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:67
intermediate_table::iterator iterator
static void SetPrintLevel(int print_level)
Definition: GHepRecord.cxx:992
std::vector< std::string > fInputModuleLabels
label(s) of existing MCTruth/GTruth/MCFlux
void CustomizeFilename(string filename)
Definition: NtpWriter.cxx:141
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
genie::EventRecord * RetrieveGHEP(const simb::MCTruth &truth, const simb::GTruth &gtruth, bool useFirstTrajPosition=true)
return genie::EventRecord pointer; callee takes possession
Definition: GENIE2ART.cxx:455
std::string fDumpFilePattern
const simb::MCTruth * GetMCTruth() const
std::map< std::string, genie::NtpWriter * > fOutputNtpWriters
object containing MC flux information
std::map< std::string, std::ostream * > fDumpStreams
Atom< std::string > outputGHEPFilePattern
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:69
const simb::MCFlux * GetMCFlux() const
void Save(void)
get the even tree
Definition: NtpWriter.cxx:237
void AddEventRecord(int ievent, const EventRecord *ev_rec)
save the event tree
Definition: NtpWriter.cxx:70
A more convenient interface to the log4cpp Message Service.
Definition: Messenger.h:259
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:38
AdcCodeMitigator::Name Name
void Initialize(void)
add event
Definition: NtpWriter.cxx:96
static PDGLibrary * Instance(void)
Definition: PDGLibrary.cxx:43
#define Comment
genie::NtpWriter * FetchNtpWriter(const std::string &label)
A utility class to facilitate creating the GENIE MC Ntuple from the output GENIE GHEP event records...
Definition: NtpWriter.h:40
GenieOutput(const Parameters &params)
const simb::GTruth * GetGTruth() const
std::ostream * FetchDumpStream(const std::string &label)
void analyze(art::Event const &e) override
Atom< std::string > dumpFilePattern
TCEvent evt
Definition: DataStructs.cxx:7
Event generator information.
Definition: MCTruth.h:32
Sequence< std::string > inputModuleLabels
std::string GetLabel() const
QTextStream & endl(QTextStream &s)
std::string fOutputGHEPFilePattern