NtpWriter.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2019, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5  or see $GENIE/LICENSE
6 
7  Author: Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
8  University of Liverpool & STFC Rutherford Appleton Lab
9 
10  For the class documentation see the corresponding header file.
11 
12  Important revisions after version 2.0.0 :
13  @ Feb 07, 2008 - CA
14  Made event branch a priv data member and modified code accordingly.
15  Added method to hand over the event tree so that additional user-defined
16  branches (eg flux info) may be added.
17  @ Mar 26, 2010 - CA
18  Added CustomizeFilename() and CustomizeFilenamePrefix() to allow the use
19  to customize either the entire output name or just the prefix before the
20  run number.
21 
22 */
23 //____________________________________________________________________________
24 
25 #include <cassert>
26 #include <sstream>
27 
28 #include <TFile.h>
29 #include <TTree.h>
30 #include <TClonesArray.h>
31 #include <TFolder.h>
32 
40 
41 #include "RVersion.h"
42 
43 using std::ostringstream;
44 
45 using namespace genie;
46 
47 //____________________________________________________________________________
49 fNtpFormat(fmt),
50 fRunNu(runnu),
51 fOutFile(0),
52 fOutTree(0),
53 fEventBranch(0),
54 fNtpMCEventRecord(0),
55 fNtpMCTreeHeader(0)
56 {
57  LOG("Ntp", pNOTICE) << "Run number: " << runnu;
58  LOG("Ntp", pNOTICE)
59  << "Requested G/ROOT tree format: " << NtpMCFormat::AsString(fNtpFormat);
60 
61  this->SetDefaultFilename();
62 }
63 //____________________________________________________________________________
65 {
66 
67 }
68 //____________________________________________________________________________
69 void NtpWriter::AddEventRecord(int ievent, const EventRecord * ev_rec)
70 {
71  LOG("Ntp", pINFO) << "Adding event " << ievent << " to output tree";
72 
73  if(!ev_rec) {
74  LOG("Ntp", pERROR) << "NULL input EventRecord!";
75  return;
76  }
77  if(!fOutTree) {
78  LOG("Ntp", pERROR) << "No open output TTree to add the input EventRecord!";
79  return;
80  }
81 
82  switch (fNtpFormat) {
83  case kNFGHEP:
85  fNtpMCEventRecord->Fill(ievent, ev_rec);
86  fOutTree->Fill();
87  delete fNtpMCEventRecord;
89  break;
90  default:
91  break;
92  }
93 }
94 //____________________________________________________________________________
96 {
97  LOG("Ntp",pINFO) << "Initializing GENIE output MC tree";
98 
99  this->OpenFile(fOutFilename); // open ROOT file
100  this->CreateTree(); // create output tree
101 
102  //-- create the event branch
103  this->CreateEventBranch();
104 
105  //-- create the tree header
106  this->CreateTreeHeader();
107  fNtpMCTreeHeader->Write();
108 
109  //-- save GENIE configuration for this MC Job
110  NtpMCJobConfig configuration;
111  configuration.Load()->Write();
112 
113  //-- take a snapshot of the user's environment
114  NtpMCJobEnv environment;
115  environment.TakeSnapshot()->Write();
116 }
117 //____________________________________________________________________________
119 {
121 }
122 //____________________________________________________________________________
124 {
125  this->SetDefaultFilename(prefix);
126 }
127 //____________________________________________________________________________
128 void NtpWriter::SetDefaultFilename(string filename_prefix)
129 {
130  ostringstream fnstr;
131  fnstr << filename_prefix << "."
132  << fRunNu << "."
134  << ".root";
135 
136  fOutFilename = fnstr.str();
137 }
138 //____________________________________________________________________________
140 {
141  if(fOutFile) delete fOutFile;
142 
143  LOG("Ntp", pINFO)
144  << "Opening the output ROOT file: " << filename;
145 
146  // use "TFile::Open()" instead of "new TFile()" so that it can handle
147  // alternative URLs (e.g. xrootd, etc)
148  fOutFile = TFile::Open(filename.c_str(),"RECREATE");
149 }
150 //____________________________________________________________________________
152 {
153  if(fOutTree) delete fOutTree;
154 
155  LOG("Ntp", pINFO) << "Creating the output GENIE/ROOT tree";
156 
157  ostringstream title;
158  title << "GENIE MC Truth TTree"
159  << ", Format: " << NtpMCFormat::AsString(fNtpFormat);
160 
161  fOutTree = new TTree("gtree",title.str().c_str());
162  fOutTree->SetAutoSave(200000000); // autosave when 0.2 Gbyte written
163 }
164 //____________________________________________________________________________
166 {
167  switch (fNtpFormat) {
168  case kNFGHEP:
169  this->CreateGHEPEventBranch();
170  break;
171  default:
172  LOG("Ntp", pERROR)
173  << "Unknown TTree format. Can not create TBranches";
174  break;
175  }
177  fEventBranch->SetAutoDelete(kFALSE);
178 }
179 //____________________________________________________________________________
181 {
182  LOG("Ntp", pINFO) << "Creating a NtpMCEventRecord TBranch";
183 
184  fNtpMCEventRecord = 0;
185  TTree::SetBranchStyle(1);
186 
187 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
188  int split = 0;
189 #else
190  int split = 1;
191 #endif
192 
193  fEventBranch = fOutTree->Branch("gmcrec",
194  "genie::NtpMCEventRecord", &fNtpMCEventRecord, 32000, split);
195  // was split=1 ... but, at least w/ ROOT 6.06/04, this generates
196  // Warning in <TTree::Bronch>: genie::NtpMCEventRecord cannot be split, resetting splitlevel to 0
197  // which the art framework turns into a fatal error
198 }
199 //____________________________________________________________________________
201 {
202  LOG("Ntp", pINFO) << "Creating the NtpMCTreeHeader";
203 
205 
207 
210 
211  LOG("Ntp", pINFO) << *fNtpMCTreeHeader;
212 }
213 //____________________________________________________________________________
214 void NtpWriter::Save(void)
215 {
216  LOG("Ntp", pINFO) << "Saving the output tree";
217 
218  if(fOutFile) {
219 
220  fOutFile->Write();
221  fOutFile->Close();
222  delete fOutFile;
223  fOutFile = 0;
224 
225  } else {
226  LOG("Ntp", pERROR) << "No open ROOT file was found";
227  }
228 }
229 //____________________________________________________________________________
230 
void split(double tt, double *fr)
Stores the GENIE configuration in ROOT TFolders along with the output event tree. ...
void OpenFile(string filename)
Definition: NtpWriter.cxx:139
void CreateTree(void)
Definition: NtpWriter.cxx:151
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
#define pERROR
Definition: Messenger.h:60
Stores a snapshot of your environment in ROOT TFolder along with the output event tree...
Definition: NtpMCJobEnv.h:27
void CustomizeFilename(string filename)
Definition: NtpWriter.cxx:118
void SetDefaultFilename(string filename_prefix="gntp")
Definition: NtpWriter.cxx:128
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 Fill(unsigned int ievent, const EventRecord *ev_rec)
void CreateGHEPEventBranch(void)
Definition: NtpWriter.cxx:180
string filename
Definition: shutoffs.py:106
NtpMCFormat_t fNtpFormat
enumeration of event formats
Definition: NtpWriter.h:72
string fOutFilename
output filename
Definition: NtpWriter.h:74
TFile * fOutFile
output file
Definition: NtpWriter.h:75
NtpMCTreeHeader * fNtpMCTreeHeader
Definition: NtpWriter.h:79
TBranch * fEventBranch
the generated event branch
Definition: NtpWriter.h:77
~NtpWriter()
initialize the ntuple writer
Definition: NtpWriter.cxx:64
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:97
void CreateEventBranch(void)
Definition: NtpWriter.cxx:165
MINOS-style Ntuple Class to hold an output MC Tree Header.
void Save(void)
get the even tree
Definition: NtpWriter.cxx:214
#define pINFO
Definition: Messenger.h:63
Long_t runnu
MC Job run number.
static const char * FilenameTag(NtpMCFormat_t fmt)
Definition: NtpMCFormat.h:51
static const char * AsString(NtpMCFormat_t fmt)
Definition: NtpMCFormat.h:37
void AddEventRecord(int ievent, const EventRecord *ev_rec)
save the event tree
Definition: NtpWriter.cxx:69
const XML_Char * prefix
Definition: expat.h:380
void CreateTreeHeader(void)
Definition: NtpWriter.cxx:200
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:38
void CustomizeFilenamePrefix(string prefix)
Definition: NtpWriter.cxx:123
void Initialize(void)
add event
Definition: NtpWriter.cxx:95
NtpMCFormat_t format
Event Record format (GENIE support multiple formats)
TFolder * Load(void)
TTree * fOutTree
output tree
Definition: NtpWriter.h:76
assert(nhit_max >=nhit_nbins)
enum genie::ENtpMCFormat NtpMCFormat_t
#define pNOTICE
Definition: Messenger.h:62
NtpWriter(NtpMCFormat_t fmt=kNFGHEP, Long_t runnu=0)
Definition: NtpWriter.cxx:48
Long_t fRunNu
run nu
Definition: NtpWriter.h:73
NtpMCEventRecord * fNtpMCEventRecord
Definition: NtpWriter.h:78
TFolder * TakeSnapshot(void)
Definition: NtpMCJobEnv.cxx:47