4 #include "TTreeIndex.h"
7 #include <unordered_map>
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");
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");
24 throw std::runtime_error(
"NAIAChain::GetEntries - Unrecognized access mode");
29 static int currentTree = -1;
34 spdlog::trace(
"Event {} -> CHANGING TREE", iEv);
49 static unsigned int lastSec = 0;
52 throw(std::runtime_error(
"GetEventRTIInfo called on a MC chain"));
63 static unsigned int lastRun = 0;
74 static unsigned int lastRun = 0;
77 throw(std::runtime_error(
"GetEventMCFileInfo called on a non-MC chain"));
88 static const std::string &fnName =
"NAIAChain::GetRTITree";
95 logger->error(
"Not supported in Write mode");
98 throw std::runtime_error(
"NAIAChain::GetRTITree - Unrecognized access mode");
103 static const std::string &fnName =
"NAIAChain::GetFileInfoTree";
110 logger->error(
"Not supported in Write mode");
113 throw std::runtime_error(
"NAIAChain::GetFileInfoTree - Unrecognized access mode");
137 throw std::runtime_error(
"NAIAChain::Add - Unrecognized access mode");
148 throw std::runtime_error(
"NAIAChain::Fill - Unrecognized access mode");
159 throw std::runtime_error(
"NAIAChain::FillRTI - Unrecognized access mode");
170 throw std::runtime_error(
"NAIAChain::FillFileInfo - Unrecognized access mode");
181 throw std::runtime_error(
"NAIAChain::GetEntries - Unrecognized access mode");
196 throw std::runtime_error(
"NAIAChain::Print - Unrecognized access mode");
210 m_rti.
tree->BuildIndex(
"RTIInfo.UTCTime");
213 m_data.
tree->BuildIndex(
"HeaderData.Run",
"HeaderData.EventNo");
216 throw std::runtime_error(
"NAIAChain::Write - Unrecognized access mode");
240 if (create_MC_branches)
245 throw std::runtime_error(
"NAIAChain::SetupBranches - Unrecognized access mode");
249 static const std::unordered_map<std::string, std::function<void(Event &)>>
evDisablers = {
250 {
"EventSummary", [](
Event &event) {
event.evSummary.DisableIO(); }},
251 {
"DAQ", [](
Event &event) {
event.daq.DisableIO(); }},
252 {
"TofBase", [](
Event &event) {
event.tofBase.DisableIO(); }},
253 {
"TofPlus", [](
Event &event) {
event.tofPlus.DisableIO(); }},
254 {
"TofBaseStandalone", [](
Event &event) {
event.tofBaseSt.DisableIO(); }},
255 {
"TofPlusStandalone", [](
Event &event) {
event.tofPlusSt.DisableIO(); }},
256 {
"EcalBase", [](
Event &event) {
event.ecalBase.DisableIO(); }},
257 {
"EcalPlus", [](
Event &event) {
event.ecalPlus.DisableIO(); }},
258 {
"TrTrackBase", [](
Event &event) {
event.trTrackBase.DisableIO(); }},
259 {
"TrTrackPlus", [](
Event &event) {
event.trTrackPlus.DisableIO(); }},
260 {
"SecondTrTrackBase", [](
Event &event) {
event.secondTrTrackBase.DisableIO(); }},
261 {
"TrTrackBaseStandalone", [](
Event &event) {
event.trTrackBaseSt.DisableIO(); }},
262 {
"TrdKBase", [](
Event &event) {
event.trdKBase.DisableIO(); }},
263 {
"TrdKBaseStandalone", [](
Event &event) {
event.trdKBaseSt.DisableIO(); }},
264 {
"RichBase", [](
Event &event) {
event.richBase.DisableIO(); }},
265 {
"RichPlus", [](
Event &event) {
event.richPlus.DisableIO(); }},
266 {
"UnbExtHitBase", [](
Event &event) {
event.extHitBase.DisableIO(); }},
267 {
"MCTruthBase", [](
Event &event) {
event.mcTruthBase.DisableIO(); }},
268 {
"MCTruthPlus", [](
Event &event) {
event.mcTruthPlus.DisableIO(); }},
272 static const std::string &fnName =
"NAIAChain::CreateSkimTree";
275 auto outFile = std::make_unique<TFile>(filename.c_str(),
"recreate");
278 if (!exclBranches.empty()) {
279 for (
const std::string &container :
TokenizeString(exclBranches,
';')) {
281 logger->info(
"Disabling container {}", container);
284 logger->error(
"Unknown container {}", container);
290 newChain->m_event.MirrorBranches(newChain->m_data.tree,
m_event);
291 newChain->m_rti.tree->Branch(
"RTIInfo", &
m_rtiInfo);
292 newChain->m_file.tree->Branch(
"FileInfo", &
m_fileInfo);
294 newChain->m_file.tree->Branch(
"MCFileInfo", &
m_fileInfoMC);
296 return {std::move(outFile), std::move(newChain),
this};
300 static const std::string &fnName =
"NAIAChain::SetEntryList";
305 logger->error(
"TEntryList not supported in Write mode");
316 static const std::string &fnName =
"NAIAChain::CheckVersion";
321 auto file = std::make_unique<TFile>(filePath.c_str(),
"read");
324 logger->error(
"NAIA version header not found in file {}. This file will be skipped.", filePath);
328 if (file_version->major != this_version.major) {
329 logger->error(
"Major version mismatch in file {} ({} on file vs {} in this library). This file will be skipped.",
330 filePath, file_version->major, this_version.major);
334 if (file_version->minor != this_version.minor) {
335 logger->warn(
"Minor version mismatch in file {} ({} on file vs {} in this library). This should be fine, however "
336 "please report any issue to the NAIA development team.",
337 filePath, file_version->minor, this_version.minor);
void SetupBranches(bool create_MC_branches=false)
Set all branch addresses for reading operations, or create all branches for writing operation...
Helper class to ease skimming operations.
const RTIInfo & GetEventRTIInfo()
Get the RTIInfo object associated with this event.
AccessMode
Simple enum to express whether we are in read or write mode.
NAIAChain class description.
void SetEntryList(TEntryList *entryList, Option_t *option)
Set an entry list for this tree.
unsigned long int GetEntries()
Get the total number of events.
Event & GetEventWithIndex(unsigned int run, unsigned int eventno)
Get the Event object using the underlying index.
NAIAChain(AccessMode mode=AccessMode::Read)
Construct a new Single Tree Chain object.
void SetDirectory(TDirectory *directory)
Set the TDirectory for the trees.
Container class for versioning info.
void BranchAll(TTree *tree)
Forwards this tree to all containers so that each one can create its own branch.
void SetAllBranchAddress(TTree *tree)
Forwards this tree to all containers so that each one can create its own branch address for reading o...
MCFileInfo * m_fileInfoMCPtr
needed for SetBranchAddress
TChain * GetRTITree()
Get the RTIInfo TTree object.
int FillFileInfo()
Fill the FileInfo data.
TChain * GetFileInfoTree()
Get the FileInfo TTree object.
static const std::unordered_map< std::string, std::function< void(Event &)> > evDisablers
const MCFileInfo & GetEventMCFileInfo()
Get the MCFileInfo object associated with this event.
RTIInfo * m_rtiInfoPtr
needed for SetBranchAddress
SkimTreeHandle< NAIAChain > CreateSkimTree(const std::string &filename, const std::string &exclBranches)
Create a new SkimTree handle object and setup all internal branches.
void Print()
Print all the chains.
int Add(const std::string &filePath)
Add a file to the chain.
void SetEventNumber(unsigned long long iEv)
Set the Event Number for all containers. The corresponding entry will be loaded upon the first read r...
int Fill()
Fill the event data.
Container class for processed File information.
int FillRTI()
Fill the RTI data.
Container class for additional MC File information.
const FileInfo & GetEventFileInfo()
Get the FileInfo object associated with this event.
std::vector< std::string > TokenizeString(const std::string &input, const char separator)
Utility function that splits a string according to the provided separator.
Event & GetEvent(unsigned long long iEv)
Get the Event object.
static bool CheckVersion(const std::string &filePath)
bool IsMC() const
Check if this file is a MC file.
NAIAChain::EventItr end(NAIAChain &chain)
void SetMC(bool isMC)
Set wether this is a MC event or not.
auto getLogger(const std::string &fnName)
Create a new logger with a given function name.
int Write()
Write the trees to disk.
Container class for RTI info.