EDepSimKinemPassThrough.cc
Go to the documentation of this file.
2 
3 #include <TROOT.h>
4 #include <TList.h>
5 
6 #include "EDepSimLog.hh"
7 
8 #include <memory>
9 #include <cstdlib>
10 #include <cstring>
11 
12 #define PASSTHRUDIR "DetSimPassThru"
13 
14 // void sort() {
15 // TFile f("hsimple.root");
16 // TTree *tree = (TTree*)f.Get("ntuple");
17 // Int_t nentries = (Int_t)tree->GetEntries();
18 // //Drawing variable pz with no graphics option.
19 // //variable pz stored in array fV1 (see TTree::Draw)
20 // tree->Draw("pz","","goff");
21 // Int_t *index = new Int_t[nentries];
22 // //sort array containing pz in decreasing order
23 // //The array index contains the entry numbers in decreasing order
24 // of pz
25 // TMath::Sort(nentries,tree->GetV1(),index);
26 
27 // //open new file to store the sorted Tree
28 // TFile f2("hsimple_sorted.root","recreate");
29 // //Create an empty clone of the original tree
30 // TTree *tsorted = (TTree*)tree->CloneTree(0);
31 // for (Int_t i=0;i<nentries;i++) {
32 // tree->GetEntry(index[i]);
33 // tsorted->Fill();
34 // }
35 // tsorted->Write();
36 // delete [] index;
37 // }
38 
39 
41 
43  CleanUp();
44 }
45 
47  Init();
48  EDepSimNamedDebug("PassThru",
49  "Have called the EDepSim::KinemPassThrough constructor.");
50 }
51 
53  EDepSimNamedTrace("PassThru",
54  "Get pointer to EDepSim::KinemPassThrough instance.");
57  if (!fKinemPassThrough) std::abort();
58  return fKinemPassThrough;
59 }
60 
61 bool EDepSim::KinemPassThrough::AddInputTree(const TTree * inputTreePtr,
62  const char * inputFileName,
63  const char* generatorName) {
64  EDepSimNamedDebug("PassThru",
65  "Adding a generator mc-truth input"
66  " tree to the list of input trees that can be used.");
67 
69 
71  EDepSimError("Pass-through trees not created.");
72  return false;
73  }
74 
75  if (inputTreePtr == NULL) {
76  EDepSimError("NULL input tree pointer. TTree not saved.");
77  return false;
78  }
79 
80  std::string inputTreeName(inputTreePtr->GetName());
81  // check that all input trees have the same name. In future may add
82  // functionality so that maintain two persistent trees simultaneously
83  if (!fInputTreeChain) {
84  fFirstTreeName = inputTreeName;
85  fInputTreeChain = new TChain(fFirstTreeName.c_str());
86  }
87 
88  if (fFirstTreeName != inputTreeName) {
89  EDepSimError("Input tree name not compatible: "
90  " All pass-through trees must have same name.");
91  return false;
92  }
93 
94  // check if its already been added
95  if (fInputTreeMap.find(inputTreePtr) != fInputTreeMap.end()) {
96  EDepSimError("Input tree already in chain.");
97  return false;
98  }
99 
100  // Add input tree to TChain
101  fInputTreeChain->Add(inputFileName);
102 
103  // Clone tree if this has not been done already. We check if fPersistent
104  // tree is NULL rather than looking to see if fInputTreeChain has a list
105  // of clones (as for a TChain this always returns false).
106  if (fPersistentTree == NULL) {
107  EDepSimNamedDebug("PassThru", "Clone the input TTree");
108  fPersistentTree = (TTree*) fInputTreeChain->CloneTree(0);
109  }
110 
111  // Add the input file to the file list so it can be saved in the output
112  // tree. This is used for error reporting and debugging.
113  fFileList.push_back(SetInputFileName(inputFileName));
114 
115  // fill in input tree maps
116  fInputTreeMap[inputTreePtr] = fFileList.size() - 1;
117  fFirstEntryMap[inputTreePtr] = (fInputTreeChain->GetEntries()
118  - inputTreePtr->GetEntries());
119 
120  // Copy the input file name and make sure it's 0 terminated.
121  std::strncpy(fInputFileName, fFileList.back().c_str(),
122  sizeof(fInputFileName));
123  fInputFileName[sizeof(fInputFileName)-1] = 0;
124  // Copy the generator name and make sure it's 0 terminated.
125  std::strncpy(fInputFileGenerator, generatorName,
126  sizeof(fInputFileGenerator));
127  // Copy the generator name and make sure it's 0 terminated.
128  std::strncpy(fInputFileTreeName, inputTreeName.c_str(),
129  sizeof(fInputFileTreeName));
130  fInputFileTreeName[sizeof(fInputFileTreeName)-1] = 0;
131  fInputFilePOT = inputTreePtr->GetWeight();
132  fInputFileEntries = inputTreePtr->GetEntries();
133  fInputFilesTree->Fill();
134 
135  EDepSimNamedDebug("PassThru",
136  "Have added a " << fFirstTreeName
137  << " tree from the input file "<< inputFileName);
138 
139  // Now can start copying events
140  return true;
141 }
142 
144  TFile* output = NULL;;
145  TIter files(gROOT->GetListOfFiles());
146  TObject* object;
147  while ((object = files.Next())) {
148  TFile* file = dynamic_cast<TFile*>(object);
149  if (!file) continue;
150  if (!file->IsOpen()) continue;
151  std::string fileOption(file->GetOption());
152  if (fileOption.find("CREATE") != std::string::npos) {
153  output = file;
154  continue;
155  }
156  }
157 
158  if (!output) {
159  return;
160  }
161 
162  output->cd();
163 
164  // Check if the directory exists (and possibly create it).
165  if (!output->Get(PASSTHRUDIR)) {
166  output->mkdir(PASSTHRUDIR,"DETSIM Pass-Through Information");
167  }
168 
169  // Make sure we are in the pass-thru directory.
170  output->cd(PASSTHRUDIR);
171 
172  // Create the book keeping three that connects a particular entry to the
173  // entry in the original file.
174  if (fInputKinemTree == NULL) {
175  EDepSimNamedDebug("PassThru", "Create InputKinem TTree");
176  // This adds the tree to the current output file.
177  fInputKinemTree = new TTree("InputKinem",
178  "Map kinematics with input files");
179  fInputKinemTree->Branch("inputFileNum" , &fInputFileNumber);
180  fInputKinemTree->Branch("inputEntryNum", &fOrigEntryNumber);
181  }
182 
183  if (fInputFilesTree == NULL) {
184  EDepSimNamedDebug("PassThru","Create inputFileList TTree");
185  // This adds the tree to the current output file.
186  fInputFilesTree = new TTree("InputFiles","Input file information");
187  fInputFilesTree->Branch("fileName", &fInputFileName,
188  "fileName/C");
189  fInputFilesTree->Branch("generatorName", &fInputFileGenerator,
190  "generatorName/C");
191  fInputFilesTree->Branch("treeName", &fInputFileTreeName,
192  "treeName/C");
193  fInputFilesTree->Branch("filePOT", &fInputFilePOT);
194  fInputFilesTree->Branch("fileEntries", &fInputFileEntries);
195  }
196 }
197 
198 bool
199 EDepSim::KinemPassThrough::AddEntry(const TTree* inputTree, int origEntry) {
200  if (!fPersistentTree) {
201  EDepSimNamedDebug("PassThru", "Cannot copy entry from tree "
202  "since fPersistentTree is NULL.");
203  return false;
204  }
205 
207 
208  // Search the input tree maps for a TTree pointer that matches the
209  // inputTree.
210  TreeToInt::iterator treeid_iter = fInputTreeMap.find(inputTree);
211  TreeToInt::iterator firstentry_iter = fFirstEntryMap.find(inputTree);
212  if (treeid_iter == fInputTreeMap.end()
213  || firstentry_iter == fFirstEntryMap.end()) {
214  EDepSimError("Cannot copy entry from tree not in list of input trees.");
215  return false;
216  }
217 
218  fInputFileNumber = treeid_iter->second;
219  int first_event_in_chain = firstentry_iter->second;
220 
221  // Now fill temp tree with i'th entry from first_event_in_chain of TChain
222  // of input trees.
223  if (fInputTreeChain->GetEntry(origEntry+first_event_in_chain)) {
224  fOrigEntryNumber = origEntry;
225  fPersistentTree->Fill();
226  fInputKinemTree->Fill(); // Also store book keeping info.
227 
228  EDepSimNamedTrace("PassThru",
229  "Copied entry " << origEntry
230  << " from " << fFirstTreeName
231  << " tree in file "
233 
234  return true;
235  }
236 
237  EDepSimError("Cannot copy entry " << origEntry+first_event_in_chain
238  << " from TChained Tree. Make sure entry is in input tree!");
239  return false;
240 }
241 
243  // The most recent entry number is the sum of the entry number in
244  // the current tree plus the number of entries already stored in
245  // persistent tree.
246  if (!fPersistentTree) {
247  EDepSimError("No entries in fPersistent tree.");
248  return -1;
249  }
250  return fPersistentTree->GetEntries() - 1;
251 }
252 
254  // Remove the path from the input file name.
255  std::string::size_type start_pos = name.rfind("/");
256  if (start_pos == std::string::npos) start_pos = 0; else ++start_pos;
257  std::string baseName(name,start_pos);
258 
259  return baseName;
260 }
261 
263  // Delete the output trees so that they can not be written twice (which
264  // they would be as detsim calls TFile::Write() which writes anything in
265  // memory to the TFile.
266 
267  if (fPersistentTree) {delete fPersistentTree;}
268  if (fInputFilesTree) {delete fInputFilesTree;}
269  if (fInputKinemTree) {delete fInputKinemTree;}
270  if (fInputTreeChain) {delete fInputTreeChain;}
271 
272  Init();
273 
274  return;
275 }
276 
278  fPersistentTree = NULL;
279  fInputFilesTree = NULL;
280  fInputKinemTree = NULL;
281  fInputTreeChain = NULL;
282  fFirstTreeName.clear();
283  fFileList.clear();
284  fInputFileNumber = -1;
285  fOrigEntryNumber = -1;
286  fInputFileName[0] = 0;
287  fInputFileGenerator[0] = 0;
288  fInputFileTreeName[0] = 0;
289  return;
290 }
char fInputFileTreeName[128]
Used to fill the name of the tree for this file.
static QCString name
Definition: declinfo.cpp:673
TTree * fInputKinemTree
Tree relating all events in the persistent tree to an input file.
intermediate_table::iterator iterator
std::string fFirstTreeName
Store the first tree name.
TChain * fInputTreeChain
TChain to store input trees.
TreeToInt fFirstEntryMap
for relating input tree to first entry in TChain.
static EDepSim::KinemPassThrough * fKinemPassThrough
Static pointer to singleton instance.
#define EDepSimNamedDebug(trace, outStream)
Definition: EDepSimLog.hh:634
std::vector< std::string > fFileList
Used to store list of files before they are written to tree.
std::string string
Definition: nybbler.cc:12
void CleanUp()
Clean up all of the allocated pointers.
double fInputFilePOT
Used to fill input file list tree "filePOT" field.
#define PASSTHRUDIR
std::string SetInputFileName(std::string name)
Set the name of the input file being read.
list files
Definition: languages.py:9
KinemPassThrough()
Private constructor.
char fInputFileName[1024]
Used to fill input file list tree with input file name.
bool AddEntry(const TTree *inputTreePtr, const int origEntry)
static EDepSim::KinemPassThrough * GetInstance()
#define EDepSimNamedTrace(trace, outStream)
Definition: EDepSimLog.hh:672
TreeToInt fInputTreeMap
for relating input tree pointers to the input file.
TTree * fInputFilesTree
Tree containing list of input files.
bool AddInputTree(const TTree *inputTreePtr, const char *inputFileName, const char *generatorName)
char fInputFileGenerator[128]
Used to fill the name of the generator for this file.
void Init()
Set default values for all fields.
#define EDepSimError(outStream)
Definition: EDepSimLog.hh:503
TTree * fPersistentTree
Persistent tree that stores entries from multiple temp trees.
static QCString baseName
Definition: scanner.cpp:10890