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 (m_event.header->UTCTime != lastSec) {
52  lastSec = m_event.header->UTCTime;
53  m_rti.chain->GetEntryWithIndex(m_event.header->UTCTime);
54  }
55 
56  return m_rtiInfo;
57 }
58 
60  static unsigned int lastRun = 0;
61 
62  if (m_event.header->Run != lastRun) {
63  lastRun = m_event.header->Run;
64  m_file.chain->GetEntryWithIndex(m_event.header->Run);
65  }
66 
67  return m_fileInfo;
68 }
69 
71  static const std::string &fnName = "SingleTreeChain::GetRTITree";
72  auto logger = getLogger(fnName);
73 
74  switch (m_accessMode) {
75  case AccessMode::Read:
76  return m_rti.chain;
77  case AccessMode::Write:
78  logger->error("Not supported in Write mode");
79  return nullptr;
80  default:
81  throw std::runtime_error("SingleTreeChain::GetRTITree - Unrecognized access mode");
82  }
83 }
84 
85 
87  static const std::string &fnName = "SingleTreeChain::GetFileInfoTree";
88  auto logger = getLogger(fnName);
89 
90  switch (m_accessMode) {
91  case AccessMode::Read:
92  return m_file.chain;
93  case AccessMode::Write:
94  logger->error("Not supported in Write mode");
95  return nullptr;
96  default:
97  throw std::runtime_error("SingleTreeChain::GetFileInfoTree - Unrecognized access mode");
98  }
99 }
100 
101 void SingleTreeChain::SetDirectory(TDirectory *directory) {
102  m_data.tree->SetDirectory(directory);
103  m_rti.tree->SetDirectory(directory);
104  m_file.tree->SetDirectory(directory);
105 }
106 
107 int SingleTreeChain::Add(const std::string& filePath) {
108  switch (m_accessMode) {
109  case AccessMode::Read:
110  m_file.chain->Add(filePath.c_str());
111  m_rti.chain->Add(filePath.c_str());
112 
113  return m_data.chain->Add(filePath.c_str());
114  case AccessMode::Write:
115  return -1;
116  default:
117  throw std::runtime_error("SingleTreeChain::Add - Unrecognized access mode");
118  }
119 }
120 
122  switch (m_accessMode) {
123  case AccessMode::Read:
124  return -1;
125  case AccessMode::Write:
126  return m_data.tree->Fill();
127  default:
128  throw std::runtime_error("SingleTreeChain::Fill - Unrecognized access mode");
129  }
130 }
131 
133  switch (m_accessMode) {
134  case AccessMode::Read:
135  return -1;
136  case AccessMode::Write:
137  return m_rti.tree->Fill();
138  default:
139  throw std::runtime_error("SingleTreeChain::FillRTI - Unrecognized access mode");
140  }
141 }
142 
144  switch (m_accessMode) {
145  case AccessMode::Read:
146  return -1;
147  case AccessMode::Write:
148  return m_file.tree->Fill();
149  default:
150  throw std::runtime_error("SingleTreeChain::FillFileInfo - Unrecognized access mode");
151  }
152 }
153 
154 unsigned long int SingleTreeChain::GetEntries() {
155  switch (m_accessMode) {
156  case AccessMode::Read:
157  return m_data.chain->GetEntries();
158  case AccessMode::Write:
159  return m_data.tree->GetEntries();
160  default:
161  throw std::runtime_error("SingleTreeChain::GetEntries - Unrecognized access mode");
162  }
163 }
164 
166  switch (m_accessMode) {
167  case AccessMode::Read:
168  m_file.chain->Print();
169  m_rti.chain->Print();
170  return m_data.chain->Print();
171  case AccessMode::Write:
172  m_file.tree->Print();
173  m_rti.tree->Print();
174  return m_data.tree->Print();
175  default:
176  throw std::runtime_error("SingleTreeChain::Print - Unrecognized access mode");
177  }
178 }
179 
181  switch (m_accessMode) {
182  case AccessMode::Read:
183  m_file.chain->Write();
184  m_rti.chain->Write();
185  return m_data.chain->Write();
186  case AccessMode::Write:
187  m_file.tree->BuildIndex("FileInfo.Run");
188  m_file.tree->Write();
189 
190  m_rti.tree->BuildIndex("RTIInfo.UTCTime");
191  m_rti.tree->Write();
192 
193  m_data.tree->BuildIndex("HeaderData.Run", "HeaderData.EventNo");
194  return m_data.tree->Write();
195  default:
196  throw std::runtime_error("SingleTreeChain::Write - Unrecognized access mode");
197  }
198 }
199 
201  switch (m_accessMode) {
202  case AccessMode::Read:
203  if (m_data.chain->GetBranch("MCTruthBaseData")) {
204  m_event.SetMC(true);
205  }
206 
208  m_file.chain->SetBranchAddress("FileInfo", &m_fileInfoPtr);
209  if (m_file.chain->GetBranch("MCFileInfo"))
210  m_file.chain->SetBranchAddress("MCFileInfo", &m_fileInfoMCPtr);
211  m_rti.chain->SetBranchAddress("RTIInfo", &m_rtiInfoPtr);
212 
213  m_file.chain->BuildIndex("FileInfo.Run");
214  m_rti.chain->BuildIndex("RTIInfo.UTCTime");
215  break;
216  case AccessMode::Write:
217  m_event.BranchAll(this->m_data.tree);
218  m_file.tree->Branch("FileInfo", &m_fileInfo);
219  if (isMC)
220  m_file.tree->Branch("MCFileInfo", &m_fileInfoMC);
221  m_rti.tree->Branch("RTIInfo", &m_rtiInfo);
222  break;
223  default:
224  throw std::runtime_error("SingleTreeChain::SetupBranches - Unrecognized access mode");
225  }
226 }
227 
228 static const std::unordered_map<std::string, std::function<void(Event &)>> evDisablers = {
229  {"EventSummary", [](Event &event) { event.evSummary.DisableIO(); }},
230  {"DAQ", [](Event &event) { event.daq.DisableIO(); }},
231  {"TofBase", [](Event &event) { event.tofBase.DisableIO(); }},
232  {"TofPlus", [](Event &event) { event.tofPlus.DisableIO(); }},
233  {"TofBaseStandalone", [](Event &event) { event.tofBaseSt.DisableIO(); }},
234  {"TofPlusStandalone", [](Event &event) { event.tofPlusSt.DisableIO(); }},
235  {"EcalBase", [](Event &event) { event.ecalBase.DisableIO(); }},
236  {"EcalPlus", [](Event &event) { event.ecalPlus.DisableIO(); }},
237  {"TtrTrackBase", [](Event &event) { event.trTrackBase.DisableIO(); }},
238  {"TtrTrackPlus", [](Event &event) { event.trTrackPlus.DisableIO(); }},
239  {"TrdKBase", [](Event &event) { event.trdKBase.DisableIO(); }},
240  {"TrdKBaseStandalone", [](Event &event) { event.trdKBaseSt.DisableIO(); }},
241  {"RichBase", [](Event &event) { event.richBase.DisableIO(); }},
242  {"RichPlus", [](Event &event) { event.richPlus.DisableIO(); }},
243  {"UnbExtHitBase", [](Event &event) { event.extHitBase.DisableIO(); }},
244  {"MCTruthBase", [](Event &event) { event.mcTruthBase.DisableIO(); }},
245  {"MCTruthPlus", [](Event &event) { event.mcTruthPlus.DisableIO(); }},
246 };
247 
249  const std::string &exclBranches) {
250  static const std::string &fnName = "SingleTreeChain::CreateSkimTree";
251  auto logger = getLogger(fnName);
252 
253  auto outFile = std::make_unique<TFile>(filename.c_str(), "recreate");
254  auto newChain = std::make_unique<SingleTreeChain>(SingleTreeChain::AccessMode::Write);
255 
256  if (!exclBranches.empty()) {
257  for (const std::string &container : TokenizeString(exclBranches, ';')) {
258  if (evDisablers.find(container) != std::end(evDisablers)) {
259  logger->info("Disabling container {}", container);
260  evDisablers.at(container)(newChain->m_event);
261  } else {
262  logger->error("Unknown container {}", container);
263  }
264  }
265  }
266 
267  // link existing branches to new tree branches
268  newChain->m_event.MirrorBranches(newChain->m_data.tree, m_event);
269  newChain->m_rti.tree->Branch("RTIInfo", &m_rtiInfo);
270  newChain->m_file.tree->Branch("FileInfo", &m_fileInfo);
271  if (m_file.tree->GetBranch("MCFileInfo"))
272  newChain->m_file.tree->Branch("MCFileInfo", &m_fileInfoMC);
273 
274  return {std::move(outFile), std::move(newChain), this};
275 }
276 
277 void SingleTreeChain::SetEntryList(TEntryList *entryList, Option_t *option) {
278  static const std::string &fnName = "SingleTreeChain::SetEntryList";
279  auto logger = getLogger(fnName);
280 
281  switch (m_accessMode) {
282  case AccessMode::Write:
283  logger->error("TEntryList not supported in Write mode");
284  break;
285  case AccessMode::Read:
286  m_data.chain->SetEntryList(entryList, option);
287  break;
288  default:
289  break;
290  }
291 }
292 
293 } // 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.
SingleTreeChain::EventItr end(SingleTreeChain &chain)
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.
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