GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NtpWriter.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <cassert>
12 #include <sstream>
13 
14 #include <TFile.h>
15 #include <TTree.h>
16 #include <TClonesArray.h>
17 #include <TFolder.h>
18 
26 #include "Framework/Utils/RunOpt.h"
27 
28 #include "RVersion.h"
29 
30 using std::ostringstream;
31 
32 using namespace genie;
33 
34 //____________________________________________________________________________
35 NtpWriter::NtpWriter(NtpMCFormat_t fmt, Long_t runnu, Long_t seed) :
36 fNtpFormat(fmt),
37 fRunNu(runnu),
38 fRunSeed(seed),
39 fOutFile(0),
40 fOutTree(0),
41 fEventBranch(0),
42 fNtpMCEventRecord(0),
43 fNtpMCTreeHeader(0)
44 {
45  LOG("Ntp", pNOTICE) << "Run number: " << runnu;
46  LOG("Ntp", pNOTICE)
47  << "Requested G/ROOT tree format: " << NtpMCFormat::AsString(fNtpFormat);
48 
49  this->SetDefaultFilename();
50 }
51 //____________________________________________________________________________
53 {
54 
55 }
56 //____________________________________________________________________________
57 void NtpWriter::AddEventRecord(int ievent, const EventRecord * ev_rec)
58 {
59  LOG("Ntp", pINFO) << "Adding event " << ievent << " to output tree";
60 
61  if(!ev_rec) {
62  LOG("Ntp", pERROR) << "NULL input EventRecord!";
63  return;
64  }
65  if(!fOutTree) {
66  LOG("Ntp", pERROR) << "No open output TTree to add the input EventRecord!";
67  return;
68  }
69 
70  switch (fNtpFormat) {
71  case kNFGHEP:
73  fNtpMCEventRecord->Fill(ievent, ev_rec);
74  fOutTree->Fill();
75  delete fNtpMCEventRecord;
77  break;
78  default:
79  break;
80  }
81 }
82 //____________________________________________________________________________
84 {
85  LOG("Ntp",pINFO) << "Initializing GENIE output MC tree";
86 
87  this->OpenFile(fOutFilename); // open ROOT file
88  this->CreateTree(); // create output tree
89 
90  //-- create the event branch
91  this->CreateEventBranch();
92 
93  //-- create the tree header
94  this->CreateTreeHeader();
95  //-- update the tune name (and associated directories) from RunOpt
96  // (keep header from RunOpt entanglement)
97  string tunename("unknown");
98  string tuneDir("unknown");
99  string customDirs("");
100  TuneId* tuneId = RunOpt::Instance()->Tune();
101  if ( ! tuneId ) {
102  LOG("Ntp", pERROR)
103  << "No TuneId is available from RunOpt";
104  } else {
105  tunename = tuneId->Name();
106  tuneDir = tuneId->TuneDirectory();
107  if ( tuneId->IsCustom() ) {
108  tunename += "*"; // flag it as possibly modified
109  customDirs = tuneId->CustomSource();
110  }
111  }
112  fNtpMCTreeHeader->tune.SetString(tunename.c_str());
113  fNtpMCTreeHeader->tuneDir.SetString(tuneDir.c_str());
114  fNtpMCTreeHeader->customDirs.SetString(customDirs.c_str());
115 
116  //-- write the tree header
117  fNtpMCTreeHeader->Write();
118 
119  //-- save GENIE configuration for this MC Job
120  NtpMCJobConfig configuration;
121  configuration.Load()->Write();
122 
123  //-- take a snapshot of the user's environment
124  NtpMCJobEnv environment;
125  environment.TakeSnapshot()->Write();
126 }
127 //____________________________________________________________________________
128 void NtpWriter::CustomizeFilename(string filename)
129 {
130  fOutFilename = filename;
131 }
132 //____________________________________________________________________________
134 {
135  this->SetDefaultFilename(prefix);
136 }
137 //____________________________________________________________________________
138 void NtpWriter::SetDefaultFilename(string filename_prefix)
139 {
140  ostringstream fnstr;
141  fnstr << filename_prefix << "."
142  << fRunNu << "."
144  << ".root";
145 
146  fOutFilename = fnstr.str();
147 }
148 //____________________________________________________________________________
149 void NtpWriter::OpenFile(string filename)
150 {
151  if(fOutFile) delete fOutFile;
152 
153  LOG("Ntp", pINFO)
154  << "Opening the output ROOT file: " << filename;
155 
156  // use "TFile::Open()" instead of "new TFile()" so that it can handle
157  // alternative URLs (e.g. xrootd, etc)
158  fOutFile = TFile::Open(filename.c_str(),"RECREATE");
159 }
160 //____________________________________________________________________________
162 {
163  if(fOutTree) delete fOutTree;
164 
165  LOG("Ntp", pINFO) << "Creating the output GENIE/ROOT tree";
166 
167  ostringstream title;
168  title << "GENIE MC Truth TTree"
169  << ", Format: " << NtpMCFormat::AsString(fNtpFormat);
170 
171  fOutTree = new TTree("gtree",title.str().c_str());
172  fOutTree->SetAutoSave(200000000); // autosave when 0.2 Gbyte written
173 }
174 //____________________________________________________________________________
176 {
177  switch (fNtpFormat) {
178  case kNFGHEP:
179  this->CreateGHEPEventBranch();
180  break;
181  default:
182  LOG("Ntp", pERROR)
183  << "Unknown TTree format. Can not create TBranches";
184  break;
185  }
186  assert(fEventBranch);
187  fEventBranch->SetAutoDelete(kFALSE);
188 }
189 //____________________________________________________________________________
191 {
192  LOG("Ntp", pINFO) << "Creating a NtpMCEventRecord TBranch";
193 
194  fNtpMCEventRecord = 0;
195  TTree::SetBranchStyle(1);
196 
197 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,0,0)
198  int split = 0;
199 #else
200  int split = 1;
201 #endif
202 
203  fEventBranch = fOutTree->Branch("gmcrec",
204  "genie::NtpMCEventRecord", &fNtpMCEventRecord, 32000, split);
205  // was split=1 ... but, at least w/ ROOT 6.06/04, this generates
206  // Warning in <TTree::Bronch>: genie::NtpMCEventRecord cannot be split, resetting splitlevel to 0
207  // which the art framework turns into a fatal error
208 }
209 //____________________________________________________________________________
211 {
212  LOG("Ntp", pINFO) << "Creating the NtpMCTreeHeader";
213 
215 
217 
221 
222  LOG("Ntp", pINFO) << *fNtpMCTreeHeader;
223 }
224 //____________________________________________________________________________
225 void NtpWriter::Save(void)
226 {
227  LOG("Ntp", pINFO) << "Saving the output tree";
228 
229  if(fOutFile) {
230 
231  fOutFile->Write();
232  fOutFile->Close();
233  delete fOutFile;
234  fOutFile = 0;
235 
236  } else {
237  LOG("Ntp", pERROR) << "No open ROOT file was found";
238  }
239 }
240 //____________________________________________________________________________
Stores the GENIE configuration in ROOT TFolders along with the output event tree. ...
TuneId * Tune(void) const
Definition: RunOpt.h:46
string Name(void) const
Definition: TuneId.h:46
void OpenFile(string filename)
Definition: NtpWriter.cxx:149
void CreateTree(void)
Definition: NtpWriter.cxx:161
#define pERROR
Definition: Messenger.h:59
Stores a snapshot of your environment in ROOT TFolder along with the output event tree...
Definition: NtpMCJobEnv.h:26
void CustomizeFilename(string filename)
Definition: NtpWriter.cxx:128
void SetDefaultFilename(string filename_prefix="gntp")
Definition: NtpWriter.cxx:138
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)
TObjString tune
GENIE Tune Name.
void CreateGHEPEventBranch(void)
Definition: NtpWriter.cxx:190
bool IsCustom(void) const
Definition: TuneId.h:62
NtpMCFormat_t fNtpFormat
enumeration of event formats
Definition: NtpWriter.h:71
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
TObjString tuneDir
directory from when tune config came
Long_t fRunSeed
run seed
Definition: NtpWriter.h:73
~NtpWriter()
initialize the ntuple writer
Definition: NtpWriter.cxx:52
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
string TuneDirectory(void) const
Definition: TuneId.cxx:116
void CreateEventBranch(void)
Definition: NtpWriter.cxx:175
string CustomSource(void) const
Definition: TuneId.h:77
MINOS-style Ntuple Class to hold an output MC Tree Header.
void Save(void)
get the even tree
Definition: NtpWriter.cxx:225
#define pINFO
Definition: Messenger.h:62
Long_t runnu
MC Job run number.
static const char * FilenameTag(NtpMCFormat_t fmt)
Definition: NtpMCFormat.h:50
static const char * AsString(NtpMCFormat_t fmt)
Definition: NtpMCFormat.h:36
void AddEventRecord(int ievent, const EventRecord *ev_rec)
save the event tree
Definition: NtpWriter.cxx:57
void CreateTreeHeader(void)
Definition: NtpWriter.cxx:210
TObjString customDirs
any custom directories
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition: EventRecord.h:37
void CustomizeFilenamePrefix(string prefix)
Definition: NtpWriter.cxx:133
NtpWriter(NtpMCFormat_t fmt=kNFGHEP, Long_t runnu=0, Long_t runseed=-1)
Definition: NtpWriter.cxx:35
void Initialize(void)
add event
Definition: NtpWriter.cxx:83
NtpMCFormat_t format
Event Record format (GENIE support multiple formats)
static RunOpt * Instance(void)
Definition: RunOpt.cxx:54
TFolder * Load(void)
TTree * fOutTree
output tree
Definition: NtpWriter.h:76
GENIE tune ID.
Definition: TuneId.h:37
enum genie::ENtpMCFormat NtpMCFormat_t
#define pNOTICE
Definition: Messenger.h:61
Long_t runseed
Random seed used in the MC run.
Long_t fRunNu
run nu
Definition: NtpWriter.h:72
NtpMCEventRecord * fNtpMCEventRecord
Definition: NtpWriter.h:78
TFolder * TakeSnapshot(void)
Definition: NtpMCJobEnv.cxx:41