GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gEvDump.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \program gevdump
5 
6 \brief A GENIE utility printing-out GHEP event trees.
7 
8  *** Synopsis :
9 
10  gevdump -f filename
11  [-n n1[,n2]]
12  [--event-record-print-level]
13 
14  [] denotes an optional argument
15 
16  -f
17  Specifies a GENIE GHEP/ROOT event file.
18  -n
19  Specifies range of events to print-out (default: all)
20  --event-record-print-level
21  Allows users to set the level of information shown when the event
22  record is printed in the screen. See GHepRecord::Print().
23 
24  Examples:
25 
26  1. Print out all events from /data/sample.ghep.root
27  % gevdump -f /data/sample.ghep.root
28 
29  2. Print out the first 500 events from /data/sample.ghep.root
30  % gevdump -f /data/sample.ghep.root -n 0,499
31 
32  3. Print out the event 178 from /data/sample.ghep.root
33  shell$ gevdump -f /data/sample.ghep.root -n 178
34 
35 \author Costas Andreopoulos <c.andreopoulos \at cern.ch>
36  University of Liverpool
37 
38 \created September 02, 2005
39 
40 \cpright Copyright (c) 2003-2024, The GENIE Collaboration
41  For the full text of the license visit http://copyright.genie-mc.org
42 
43 */
44 //____________________________________________________________________________
45 
46 #include <string>
47 
48 #include <TFile.h>
49 #include <TTree.h>
50 #include <TLeaf.h>
51 
52 #include "Framework/Conventions/GBuild.h"
60 #include "Framework/Utils/RunOpt.h"
61 
62 #ifdef __GENIE_FLUX_DRIVERS_ENABLED__
64 #include "Tools/Flux/GNuMIFlux.h"
65 #endif
66 
67 using std::string;
68 using namespace genie;
69 
70 void GetCommandLineArgs (int argc, char ** argv);
71 void PrintSyntax (void);
72 void GetEventRange (Long64_t nev, Long64_t & n1, Long64_t & n2);
73 
74 Long64_t gOptNEvtL;
75 Long64_t gOptNEvtH;
77 
78 //___________________________________________________________________
79 int main(int argc, char ** argv)
80 {
81  GetCommandLineArgs (argc, argv);
82 
83  // set print level
84  GHepRecord::SetPrintLevel(RunOpt::Instance()->EventRecordPrintLevel());
85 
86  // Add a dummy value to Dark Matter to allow reading DarkMatter files
87  PDGLibrary::Instance()->AddDarkMatter( 1.0, 0.5 );
88 
89  //
90  // open the ROOT file and get the TTree & its header
91  //
92 
93  TFile file(gOptInpFilename.c_str(),"READ");
94 
95  TTree * ghep_tree =
96  dynamic_cast <TTree *> (file.Get("gtree"));
97  if(!ghep_tree) {
98  LOG("gevdump", pFATAL)
99  << "No GHEP event tree in input file: " << gOptInpFilename;
100  gAbortingInErr=true;
101  exit(1);
102  }
103  Long64_t nev = ghep_tree->GetEntries();
104  LOG("gevdump", pFATAL)
105  << "Input GHEP event tree has " << nev
106  << ((nev==1) ? " entry." : " entries.");
107 
108  NtpMCTreeHeader * thdr =
109  dynamic_cast <NtpMCTreeHeader *> ( file.Get("header") );
110  LOG("gevdump", pNOTICE)
111  << "Input tree header: " << *thdr;
112 
113  //
114  // set branch addresses
115  //
116 
117  // main event record branch, always present
118  NtpMCEventRecord * mcrec = 0;
119  ghep_tree->SetBranchAddress("gmcrec", &mcrec);
120 
121  // if the event file was created by GENIE's gevpick `cherry-picking' app
122  // (see $GENIE/src/stdapp/gEvPick.cxx) then there will be additional branches
123  // holding the original event filename and event number (in that file)
124  // for each `cherry-picked' event.
125  bool have_gevpick_branches = false;
126  TObjString* orig_filename = 0;
127  Long64_t orig_evtnum;
128  TBranch * brOrigFilename = ghep_tree->GetBranch("orig_filename");
129  TBranch * brOrigEvtNum = ghep_tree->GetBranch("orig_evtnum");
130  if(brOrigFilename!=0 && brOrigEvtNum!=0) {
131  have_gevpick_branches = true;
132  brOrigFilename->SetAddress(&orig_filename);
133  brOrigEvtNum ->SetAddress(&orig_evtnum);
134  }
135 
136  // if the event file was created by one of GENIE's specialized event generation
137  // then there may be additional branches holding flux pass-through
138  // info (flux neutrino parent info for each generated event).
139 #ifdef __GENIE_FLUX_DRIVERS_ENABLED__
140  flux::GJPARCNuFluxPassThroughInfo * jparc_flux_info = 0;
141  flux::GNuMIFluxPassThroughInfo * gnumi_flux_info = 0;
142  TBranch * brFluxInfo = ghep_tree->GetBranch("flux");
143  if(brFluxInfo) {
144  TObjArray * leafarr = brFluxInfo->GetListOfLeaves();
145  TIter iter(leafarr);
146  TLeaf * leaf = 0;
147  while((leaf = (TLeaf*)iter.Next())) {
148  string ltypename = leaf->GetTypeName();
149  if(ltypename == "genie::flux::GJPARCNuFluxPassThroughInfo") {
150  brFluxInfo->SetAddress(&jparc_flux_info);
151  LOG("gevdump", pNOTICE)
152  << "Found JPARC neutrino flux pass-though info";
153  }
154  else
155  if(ltypename == "genie::flux::GNuMIFluxPassThroughInfo") {
156  brFluxInfo->SetAddress(&gnumi_flux_info);
157  LOG("gevdump", pNOTICE)
158  << "Found NuMI neutrino flux pass-though info";
159  }
160  }//leaf
161  }//flux branch
162 #endif
163 
164 
165  //
166  // event loop
167  //
168 
169  Long64_t n1,n2;
170  GetEventRange(nev,n1,n2);
171  for(Long64_t i = n1; i <= n2; i++) {
172  ghep_tree->GetEntry(i);
173 
174  // retrieve GHEP event record abd print it out.
175  NtpMCRecHeader rec_header = mcrec->hdr;
176  EventRecord & event = *(mcrec->event);
177  LOG("gevdump", pNOTICE)
178  << " ** Event: " << rec_header.ievent
179  << event;
180 
181  // print info from additional tree branches that might be present
182  // if the event file was created by GENIE's gevpick app.
183  if(have_gevpick_branches) {
184  LOG("gevdump", pNOTICE)
185  << "\n Above event was originally event: " << orig_evtnum
186  << "\n in event file: " << orig_filename->GetString().Data()
187  << "\n\n";
188  }
189 
190  // print info from additional JPARC or NuMI flux pass-through branches
191  // that might be present of the event file was created by GENIE's
192  // specialized event generation applications for T2K or NuMI-expts.
193 #ifdef __GENIE_FLUX_DRIVERS_ENABLED__
194  if(jparc_flux_info) {
195  LOG("gevdump", pNOTICE)
196  << "Associated JPARC flux pass-through info for above event:"
197  << *jparc_flux_info;
198  }
199  if(gnumi_flux_info) {
200  LOG("gevdump", pNOTICE)
201  << "Associated NuMI flux pass-through info for above event:"
202  << *gnumi_flux_info;
203  }
204 #endif
205 
206  mcrec->Clear();
207  }
208 
209  // clean-up
210 
211  file.Close();
212 
213  LOG("gevdump", pNOTICE) << "Done!";
214  return 0;
215 }
216 //___________________________________________________________________
217 void GetEventRange(Long64_t nev, Long64_t & n1, Long64_t & n2)
218 {
219  if(gOptNEvtL == -1 && gOptNEvtH == -1) {
220  // read all events
221  n1=0;
222  n2=nev-1;
223  }
224  else {
225  // read a range of events
226  n1 = TMath::Max((Long64_t)0, gOptNEvtL);
227  n2 = TMath::Min(nev-1, gOptNEvtH);
228  if(n2-n1 <0) {
229  LOG("gevdump", pFATAL) << "Invalid event range";
230  PrintSyntax();
231  gAbortingInErr = true;
232  exit(1);
233  }
234  }
235 }
236 //___________________________________________________________________
237 void GetCommandLineArgs(int argc, char ** argv)
238 {
239  LOG("gevdump", pINFO) << "*** Parsing command line arguments";
240 
241  // Common run options.
243 
244  // Parse run options for this app
245 
246  CmdLnArgParser parser(argc,argv);
247 
248  // get GENIE event sample
249  if ( parser.OptionExists('f') ) {
250  LOG("gevdump", pINFO) << "Reading event sample filename";
251  gOptInpFilename = parser.ArgAsString('f');
252  } else {
253  LOG("gevdump", pFATAL)
254  << "Unspecified input filename - Exiting";
255  PrintSyntax();
256  gAbortingInErr = true;
257  exit(1);
258  }
259 
260  // number of events:
261  if ( parser.OptionExists('n') ) {
262  LOG("gevdump", pINFO) << "Reading number of events to analyze";
263  string nev = parser.ArgAsString('n');
264  if (nev.find(",") != string::npos) {
265  vector<long> vecn = parser.ArgAsLongTokens('n',",");
266  if(vecn.size()!=2) {
267  LOG("gevdump", pFATAL) << "Invalid syntax";
268  PrintSyntax();
269  gAbortingInErr = true;
270  exit(1);
271  }
272  // read a range of events
273  gOptNEvtL = vecn[0];
274  gOptNEvtH = vecn[1];
275  } else {
276  // read single event
277  gOptNEvtL = parser.ArgAsLong('n');
279  }
280  } else {
281  LOG("gevdump", pINFO)
282  << "Unspecified number of events to analyze - Use all";
283  gOptNEvtL = -1;
284  gOptNEvtH = -1;
285  }
286 
287 
288 }
289 //_________________________________________________________________________________
290 void PrintSyntax(void)
291 {
292  LOG("gevdump", pNOTICE)
293  << "\n\n" << "Syntax:" << "\n"
294  << " gevdump -f sample.root [-n n1[,n2]] [--event-record-print-level]\n";
295 }
296 //_________________________________________________________________________________
static void SetPrintLevel(int print_level)
Definition: GHepRecord.cxx:956
NtpMCRecHeader hdr
record header
Definition: NtpMCRecordI.h:38
void AddDarkMatter(double mass, double med_ratio)
Definition: PDGLibrary.cxx:153
MINOS-style ntuple record. Each such ntuple record holds a generated EventRecord object. Ntuples of this type are intended for feeding GENIE events into other applications (for example the GEANT4 based MC generation framework of an experiment) if no direct interface exists.
void ReadFromCommandLine(int argc, char **argv)
Definition: RunOpt.cxx:99
#define pFATAL
Definition: Messenger.h:56
int main(int argc, char **argv)
Definition: gAtmoEvGen.cxx:327
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
unsigned int ievent
Event number.
Long64_t gOptNEvtL
Definition: gEvDump.cxx:74
MINOS-style Ntuple Class to hold an output MC Tree Header.
#define pINFO
Definition: Messenger.h:62
void GetEventRange(Long64_t nev, Long64_t &n1, Long64_t &n2)
Definition: gEvDump.cxx:217
string gOptInpFilename
Definition: gEvDump.cxx:76
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:37
static PDGLibrary * Instance(void)
Definition: PDGLibrary.cxx:68
static RunOpt * Instance(void)
Definition: RunOpt.cxx:54
Long64_t gOptNEvtH
Definition: gEvDump.cxx:75
MINOS-style Ntuple Class to hold an MC Event Record Header.
Command line argument parser.
#define pNOTICE
Definition: Messenger.h:61
void GetCommandLineArgs(int argc, char **argv)
Definition: gAtmoEvGen.cxx:563
void Clear(Option_t *opt="")
bool gAbortingInErr
Definition: Messenger.cxx:34
void PrintSyntax(void)
EventRecord * event
event