GENIEGenerator
Loading...
Searching...
No Matches
NtpWriter.cxx
Go to the documentation of this file.
1//____________________________________________________________________________
2/*
3 Copyright (c) 2003-2025, 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
27
28#include "RVersion.h"
29
30using std::ostringstream;
31
32using namespace genie;
33
34//____________________________________________________________________________
35NtpWriter::NtpWriter(NtpMCFormat_t fmt, Long_t runnu, Long_t seed) :
36fNtpFormat(fmt),
37fRunNu(runnu),
38fRunSeed(seed),
39fOutFile(0),
40fOutTree(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//____________________________________________________________________________
56//____________________________________________________________________________
57void 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//____________________________________________________________________________
128void NtpWriter::CustomizeFilename(string filename)
129{
130 fOutFilename = filename;
131}
132//____________________________________________________________________________
134{
135 this->SetDefaultFilename(prefix);
136}
137//____________________________________________________________________________
138void NtpWriter::SetDefaultFilename(string filename_prefix)
139{
140 ostringstream fnstr;
141 fnstr << filename_prefix << "."
142 << fRunNu << "."
144 << ".root";
145
146 fOutFilename = fnstr.str();
147}
148//____________________________________________________________________________
149void 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
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
219 fNtpMCTreeHeader->runnu = fRunNu;
220 fNtpMCTreeHeader->runseed = fRunSeed;
221
222 LOG("Ntp", pINFO) << *fNtpMCTreeHeader;
223}
224//____________________________________________________________________________
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//____________________________________________________________________________
#define pNOTICE
Definition Messenger.h:61
#define pINFO
Definition Messenger.h:62
#define pERROR
Definition Messenger.h:59
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE,...
Definition Messenger.h:96
Generated Event Record. It is a GHepRecord object that can accept / be visited by EventRecordVisitorI...
Definition EventRecord.h:37
MINOS-style ntuple record. Each such ntuple record holds a generated EventRecord object....
static const char * AsString(NtpMCFormat_t fmt)
Definition NtpMCFormat.h:36
static const char * FilenameTag(NtpMCFormat_t fmt)
Definition NtpMCFormat.h:50
Stores the GENIE configuration in ROOT TFolders along with the output event tree.
Stores a snapshot of your environment in ROOT TFolder along with the output event tree.
Definition NtpMCJobEnv.h:26
TFolder * TakeSnapshot(void)
MINOS-style Ntuple Class to hold an output MC Tree Header.
void CreateGHEPEventBranch(void)
void SetDefaultFilename(string filename_prefix="gntp")
Long_t fRunNu
run nu
Definition NtpWriter.h:72
TBranch * fEventBranch
the generated event branch
Definition NtpWriter.h:77
~NtpWriter()
initialize the ntuple writer
Definition NtpWriter.cxx:52
void CustomizeFilename(string filename)
void OpenFile(string filename)
void Initialize(void)
add event
Definition NtpWriter.cxx:83
void CreateTree(void)
void Save(void)
get the even tree
void CreateEventBranch(void)
NtpMCEventRecord * fNtpMCEventRecord
Definition NtpWriter.h:78
NtpMCTreeHeader * fNtpMCTreeHeader
Definition NtpWriter.h:79
void AddEventRecord(int ievent, const EventRecord *ev_rec)
save the event tree
Definition NtpWriter.cxx:57
Long_t fRunSeed
run seed
Definition NtpWriter.h:73
void CustomizeFilenamePrefix(string prefix)
TFile * fOutFile
output file
Definition NtpWriter.h:75
TTree * fOutTree
output tree
Definition NtpWriter.h:76
void CreateTreeHeader(void)
NtpMCFormat_t fNtpFormat
enumeration of event formats
Definition NtpWriter.h:71
string fOutFilename
output filename
Definition NtpWriter.h:74
NtpWriter(NtpMCFormat_t fmt=kNFGHEP, Long_t runnu=0, Long_t runseed=-1)
Definition NtpWriter.cxx:35
TuneId * Tune(void) const
Definition RunOpt.h:46
static RunOpt * Instance(void)
Definition RunOpt.cxx:54
GENIE tune ID.
Definition TuneId.h:37
string CustomSource(void) const
Definition TuneId.h:77
bool IsCustom(void) const
Definition TuneId.h:62
string TuneDirectory(void) const
Definition TuneId.cxx:116
string Name(void) const
Definition TuneId.h:46
THE MAIN GENIE PROJECT NAMESPACE
Definition AlgCmp.h:25
@ kNFGHEP
Definition NtpMCFormat.h:30
enum genie::ENtpMCFormat NtpMCFormat_t