NAIA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
SingleTreeChain.cpp
Go to the documentation of this file.
3 
4 #include "TTreeIndex.h"
5 
6 #include <functional>
7 #include <unordered_map>
8 
9 namespace NAIA {
10 
11 SingleTreeChain::SingleTreeChain(AccessMode mode) : m_accessMode{mode} {
12  switch (m_accessMode) {
13  case AccessMode::Read:
14  m_data.chain = new TChain("NAIAChain");
15  m_rti.chain = new TChain("RTI");
16  m_file.chain = new TChain("FileInfo");
17  break;
18  case AccessMode::Write:
19  m_data.tree = new TTree("NAIAChain", "AMS-Italy ntuple tree");
20  m_rti.tree = new TTree("RTI", "RTIInfo tree");
21  m_file.tree = new TTree("FileInfo", "FileInfo tree");
22  break;
23  default:
24  throw std::runtime_error("SingleTreeChain::GetEntries - Unrecognized access mode");
25  }
26 }
27 
28 Event &SingleTreeChain::GetEvent(unsigned long long iEv) {
29  static int currentTree = -1;
30 
31  // when the tree changes we need to reload the branches...
32  auto treeReadEntry = m_data.chain->LoadTree(iEv);
33  if (m_data.chain->GetTreeNumber() != currentTree) {
34  spdlog::trace("Event {} -> CHANGING TREE", iEv);
36  currentTree = m_data.chain->GetTreeNumber();
37  }
38 
39  // do stuff to propagate iEv to the Event object (containers need to know which event to load)
40  m_event.SetEventNumber(treeReadEntry);
41  return m_event;
42 }
43 
44 Event &SingleTreeChain::GetEventWithIndex(unsigned int run, unsigned int eventno) {
45  return GetEvent(m_data.chain->GetEntryNumberWithIndex(run, eventno));
46 }
47 
49  static unsigned int lastSec = 0;
50 
51  if (IsMC())
52  throw(std::runtime_error("GetEventRTIInfo called on a MC chain"));
53 
54  if (m_event.header->UTCTime != lastSec) {
55  lastSec = m_event.header->UTCTime;
56  m_rti.chain->GetEntryWithIndex(m_event.header->UTCTime);
57  }
58 
59  return m_rtiInfo;
60 }
61 
63  static unsigned int lastRun = 0;
64 
65  if (m_event.header->Run != lastRun) {
66  lastRun = m_event.header->Run;
67  m_file.chain->GetEntryWithIndex(m_event.header->Run);
68  }
69 
70  return m_fileInfo;
71 }
72 
74  static unsigned int lastRun = 0;
75 
76  if (!IsMC())
77  throw(std::runtime_error("GetEventMCFileInfo called on a non-MC chain"));
78 
79  if (m_event.header->Run != lastRun) {
80  lastRun = m_event.header->Run;
81  m_file.chain->GetEntryWithIndex(m_event.header->Run);
82  }
83 
84  return m_fileInfoMC;
85 }
86 
88  static const std::string &fnName = "SingleTreeChain::GetRTITree";
89  auto logger = getLogger(fnName);
90 
91  switch (m_accessMode) {
92  case AccessMode::Read:
93  return m_rti.chain;
94  case AccessMode::Write:
95  logger->error("Not supported in Write mode");
96  return nullptr;
97  default:
98  throw std::runtime_error("SingleTreeChain::GetRTITree - Unrecognized access mode");
99  }
100 }
101 
103  static const std::string &fnName = "SingleTreeChain::GetFileInfoTree";
104  auto logger = getLogger(fnName);
105 
106  switch (m_accessMode) {
107  case AccessMode::Read:
108  return m_file.chain;
109  case AccessMode::Write:
110  logger->error("Not supported in Write mode");
111  return nullptr;
112  default:
113  throw std::runtime_error("SingleTreeChain::GetFileInfoTree - Unrecognized access mode");
114  }
115 }
116 
117 void SingleTreeChain::SetDirectory(TDirectory *directory) {
118  m_data.tree->SetDirectory(directory);
119  m_rti.tree->SetDirectory(directory);
120  m_file.tree->SetDirectory(directory);
121 }
122 
123 int SingleTreeChain::Add(const std::string &filePath) {
124  switch (m_accessMode) {
125  case AccessMode::Read:
126  m_file.chain->Add(filePath.c_str());
127  m_rti.chain->Add(filePath.c_str());
128 
129  return m_data.chain->Add(filePath.c_str());
130  case AccessMode::Write:
131  return -1;
132  default:
133  throw std::runtime_error("SingleTreeChain::Add - Unrecognized access mode");
134  }
135 }
136 
138  switch (m_accessMode) {
139  case AccessMode::Read:
140  return -1;
141  case AccessMode::Write:
142  return m_data.tree->Fill();
143  default:
144  throw std::runtime_error("SingleTreeChain::Fill - Unrecognized access mode");
145  }
146 }
147 
149  switch (m_accessMode) {
150  case AccessMode::Read:
151  return -1;
152  case AccessMode::Write:
153  return m_rti.tree->Fill();
154  default:
155  throw std::runtime_error("SingleTreeChain::FillRTI - Unrecognized access mode");
156  }
157 }
158 
160  switch (m_accessMode) {
161  case AccessMode::Read:
162  return -1;
163  case AccessMode::Write:
164  return m_file.tree->Fill();
165  default:
166  throw std::runtime_error("SingleTreeChain::FillFileInfo - Unrecognized access mode");
167  }
168 }
169 
170 unsigned long int SingleTreeChain::GetEntries() {
171  switch (m_accessMode) {
172  case AccessMode::Read:
173  return m_data.chain->GetEntries();
174  case AccessMode::Write:
175  return m_data.tree->GetEntries();
176  default:
177  throw std::runtime_error("SingleTreeChain::GetEntries - Unrecognized access mode");
178  }
179 }
180 
182  switch (m_accessMode) {
183  case AccessMode::Read:
184  m_file.chain->Print();
185  m_rti.chain->Print();
186  return m_data.chain->Print();
187  case AccessMode::Write:
188  m_file.tree->Print();
189  m_rti.tree->Print();
190  return m_data.tree->Print();
191  default:
192  throw std::runtime_error("SingleTreeChain::Print - Unrecognized access mode");
193  }
194 }
195 
197  switch (m_accessMode) {
198  case AccessMode::Read:
199  m_file.chain->Write();
200  m_rti.chain->Write();
201  return m_data.chain->Write();
202  case AccessMode::Write:
203  m_file.tree->BuildIndex("FileInfo.Run");
204  m_file.tree->Write();
205 
206  m_rti.tree->BuildIndex("RTIInfo.UTCTime");
207  m_rti.tree->Write();
208 
209  m_data.tree->BuildIndex("HeaderData.Run", "HeaderData.EventNo");
210  return m_data.tree->Write();
211  default:
212  throw std::runtime_error("SingleTreeChain::Write - Unrecognized access mode");
213  }
214 }
215 
217  switch (m_accessMode) {
218  case AccessMode::Read:
219  if (m_data.chain->GetBranch("MCTruthBaseData")) {
220  m_event.SetMC(true);
221  }
222 
224  m_file.chain->SetBranchAddress("FileInfo", &m_fileInfoPtr);
225  if (m_file.chain->GetBranch("MCFileInfo"))
226  m_file.chain->SetBranchAddress("MCFileInfo", &m_fileInfoMCPtr);
227  m_rti.chain->SetBranchAddress("RTIInfo", &m_rtiInfoPtr);
228 
229  m_file.chain->BuildIndex("FileInfo.Run");
230  m_rti.chain->BuildIndex("RTIInfo.UTCTime");
231  break;
232  case AccessMode::Write:
233  m_event.BranchAll(this->m_data.tree);
234  m_file.tree->Branch("FileInfo", &m_fileInfo);
235  if (isMC)
236  m_file.tree->Branch("MCFileInfo", &m_fileInfoMC);
237  m_rti.tree->Branch("RTIInfo", &m_rtiInfo);
238  break;
239  default:
240  throw std::runtime_error("SingleTreeChain::SetupBranches - Unrecognized access mode");
241  }
242 }
243 
244 static const std::unordered_map<std::string, std::function<void(Event &)>> evDisablers = {
245  {"EventSummary", [](Event &event) { event.evSummary.DisableIO(); }},
246  {"DAQ", [](Event &event) { event.daq.DisableIO(); }},
247  {"TofBase", [](Event &event) { event.tofBase.DisableIO(); }},
248  {"TofPlus", [](Event &event) { event.tofPlus.DisableIO(); }},
249  {"TofBaseStandalone", [](Event &event) { event.tofBaseSt.DisableIO(); }},
250  {"TofPlusStandalone", [](Event &event) { event.tofPlusSt.DisableIO(); }},
251  {"EcalBase", [](Event &event) { event.ecalBase.DisableIO(); }},
252  {"EcalPlus", [](Event &event) { event.ecalPlus.DisableIO(); }},
253  {"TtrTrackBase", [](Event &event) { event.trTrackBase.DisableIO(); }},
254  {"TtrTrackPlus", [](Event &event) { event.trTrackPlus.DisableIO(); }},
255  {"TrdKBase", [](Event &event) { event.trdKBase.DisableIO(); }},
256  {"TrdKBaseStandalone", [](Event &event) { event.trdKBaseSt.DisableIO(); }},
257  {"RichBase", [](Event &event) { event.richBase.DisableIO(); }},
258  {"RichPlus", [](Event &event) { event.richPlus.DisableIO(); }},
259  {"UnbExtHitBase", [](Event &event) { event.extHitBase.DisableIO(); }},
260  {"MCTruthBase", [](Event &event) { event.mcTruthBase.DisableIO(); }},
261  {"MCTruthPlus", [](Event &event) { event.mcTruthPlus.DisableIO(); }},
262 };
263 
265  const std::string &exclBranches) {
266  static const std::string &fnName = "SingleTreeChain::CreateSkimTree";
267  auto logger = getLogger(fnName);
268 
269  auto outFile = std::make_unique<TFile>(filename.c_str(), "recreate");
270  auto newChain = std::make_unique<SingleTreeChain>(SingleTreeChain::AccessMode::Write);
271 
272  if (!exclBranches.empty()) {
273  for (const std::string &container : TokenizeString(exclBranches, ';')) {
274  if (evDisablers.find(container) != std::end(evDisablers)) {
275  logger->info("Disabling container {}", container);
276  evDisablers.at(container)(newChain->m_event);
277  } else {
278  logger->error("Unknown container {}", container);
279  }
280  }
281  }
282 
283  // link existing branches to new tree branches
284  newChain->m_event.MirrorBranches(newChain->m_data.tree, m_event);
285  newChain->m_rti.tree->Branch("RTIInfo", &m_rtiInfo);
286  newChain->m_file.tree->Branch("FileInfo", &m_fileInfo);
287  if (m_file.tree->GetBranch("MCFileInfo"))
288  newChain->m_file.tree->Branch("MCFileInfo", &m_fileInfoMC);
289 
290  return {std::move(outFile), std::move(newChain), this};
291 }
292 
293 void SingleTreeChain::SetEntryList(TEntryList *entryList, Option_t *option) {
294  static const std::string &fnName = "SingleTreeChain::SetEntryList";
295  auto logger = getLogger(fnName);
296 
297  switch (m_accessMode) {
298  case AccessMode::Write:
299  logger->error("TEntryList not supported in Write mode");
300  break;
301  case AccessMode::Read:
302  m_data.chain->SetEntryList(entryList, option);
303  break;
304  default:
305  break;
306  }
307 }
308 
309 } // namespace NAIA
SkimTreeHandle< SingleTreeChain > CreateSkimTree(const std::string &filename, const std::string &exclBranches)
Create a new SkimTree handle object and setup all internal branches.
Event & GetEvent(unsigned long long iEv)
Get the Event object.
Helper class to ease skimming operations.
TChain * GetRTITree()
Get the RTIInfo TTree object.
unsigned int Run
The current run.
Definition: Header.h:63
void SetDirectory(TDirectory *directory)
Set the TDirectory for the trees.
MCFileInfo * m_fileInfoMCPtr
needed for SetBranchAddress
void BranchAll(TTree *tree)
Forwards this tree to all containers so that each one can create its own branch.
Definition: Event.cpp:4
int FillRTI()
Fill the RTI data.
const FileInfo & GetEventFileInfo()
Get the FileInfo object associated with this event.
void SetAllBranchAddress(TTree *tree)
Forwards this tree to all containers so that each one can create its own branch address for reading o...
Definition: Event.cpp:36
Event & GetEventWithIndex(unsigned int run, unsigned int eventno)
Get the Event object using the underlying index.
TChain * GetFileInfoTree()
Get the FileInfo TTree object.
Event object.
Definition: Event.h:20
void SetEntryList(TEntryList *entryList, Option_t *option)
Set an entry list for this tree.
SingleTreeChain(AccessMode mode=AccessMode::Read)
Construct a new Single Tree Chain object.
static const std::unordered_map< std::string, std::function< void(Event &)> > evDisablers
int Write()
Write the trees to disk.
void SetEventNumber(unsigned long long iEv)
Set the Event Number for all containers. The corresponding entry will be loaded upon the first read r...
Definition: Event.cpp:132
int Add(const std::string &filePath)
Add a file to the chain.
Header header
Definition: Event.h:96
SingleTreeChain class description.
Container class for processed File information.
Definition: FileInfo.h:23
const RTIInfo & GetEventRTIInfo()
Get the RTIInfo object associated with this event.
bool IsMC()
Check if this file is a MC file.
SingleTreeChain::EventItr end(SingleTreeChain &chain)
Container class for additional MC File information.
Definition: FileInfo.h:54
std::vector< std::string > TokenizeString(const std::string &input, const char separator)
Utility function that splits a string according to the provided separator.
Definition: Utils.h:30
AccessMode
Simple enum to express whether we are in read or write mode.
void SetMC(bool isMC)
Set wether this is a MC event or not.
Definition: Event.h:86
RTIInfo * m_rtiInfoPtr
needed for SetBranchAddress
int FillFileInfo()
Fill the FileInfo data.
unsigned long int GetEntries()
Get the total number of events.
const MCFileInfo & GetEventMCFileInfo()
Get the MCFileInfo object associated with this event.
auto getLogger(const std::string &fnName)
Create a new logger with a given function name.
Definition: Logging.h:18
int Fill()
Fill the event data.
void SetupBranches(bool isMC=false)
Set all branch addresses for reading operations, or create all branches for writing operation...
Container class for RTI info.
Definition: RTIInfo.h:33
void Print()
Print all the chains.
unsigned int UTCTime
UTC time (in seconds) of current event.
Definition: Header.h:66