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"));
58 throw std::runtime_error(
59 fmt::format(
"Failed to read entry for UTC second {} from RTI tree. (ec: {})",
m_event.
header->
UTCTime, rc));
67 static unsigned int lastRun = 0;
78 static unsigned int lastRun = 0;
81 throw(std::runtime_error(
"GetEventMCFileInfo called on a non-MC chain"));
92 static const std::string &fnName =
"NAIAChain::GetRTITree";
99 logger->error(
"Not supported in Write mode");
102 throw std::runtime_error(
"NAIAChain::GetRTITree - Unrecognized access mode");
107 static const std::string &fnName =
"NAIAChain::GetFileInfoTree";
114 logger->error(
"Not supported in Write mode");
117 throw std::runtime_error(
"NAIAChain::GetFileInfoTree - Unrecognized access mode");
141 throw std::runtime_error(
"NAIAChain::Add - Unrecognized access mode");
152 throw std::runtime_error(
"NAIAChain::Fill - Unrecognized access mode");
163 throw std::runtime_error(
"NAIAChain::FillRTI - Unrecognized access mode");
174 throw std::runtime_error(
"NAIAChain::FillFileInfo - Unrecognized access mode");
185 throw std::runtime_error(
"NAIAChain::GetEntries - Unrecognized access mode");
200 throw std::runtime_error(
"NAIAChain::Print - Unrecognized access mode");
215 spdlog::debug(
"Building RTIInfo index");
216 m_rti.
tree->BuildIndex(
"RTIInfo.UTCTime");
220 m_data.
tree->BuildIndex(
"HeaderData.Run",
"HeaderData.EventNo");
223 throw std::runtime_error(
"NAIAChain::Write - Unrecognized access mode");
247 if (create_MC_branches)
252 throw std::runtime_error(
"NAIAChain::SetupBranches - Unrecognized access mode");
256 static const std::unordered_map<std::string, std::function<void(Event &)>>
evDisablers = {
257 {
"EventSummary", [](
Event &event) {
event.evSummary.DisableIO(); }},
258 {
"DAQ", [](
Event &event) {
event.daq.DisableIO(); }},
259 {
"TofBase", [](
Event &event) {
event.tofBase.DisableIO(); }},
260 {
"TofPlus", [](
Event &event) {
event.tofPlus.DisableIO(); }},
261 {
"TofBaseStandalone", [](
Event &event) {
event.tofBaseSt.DisableIO(); }},
262 {
"TofPlusStandalone", [](
Event &event) {
event.tofPlusSt.DisableIO(); }},
263 {
"EcalBase", [](
Event &event) {
event.ecalBase.DisableIO(); }},
264 {
"EcalPlus", [](
Event &event) {
event.ecalPlus.DisableIO(); }},
265 {
"TrTrackBase", [](
Event &event) {
event.trTrackBase.DisableIO(); }},
266 {
"TrTrackPlus", [](
Event &event) {
event.trTrackPlus.DisableIO(); }},
267 {
"SecondTrTrackBase", [](
Event &event) {
event.secondTrTrackBase.DisableIO(); }},
268 {
"TrTrackBaseStandalone", [](
Event &event) {
event.trTrackBaseSt.DisableIO(); }},
269 {
"TrdKBase", [](
Event &event) {
event.trdKBase.DisableIO(); }},
270 {
"TrdKBaseStandalone", [](
Event &event) {
event.trdKBaseSt.DisableIO(); }},
271 {
"RichBase", [](
Event &event) {
event.richBase.DisableIO(); }},
272 {
"RichPlus", [](
Event &event) {
event.richPlus.DisableIO(); }},
273 {
"UnbExtHitBase", [](
Event &event) {
event.extHitBase.DisableIO(); }},
274 {
"MCTruthBase", [](
Event &event) {
event.mcTruthBase.DisableIO(); }},
275 {
"MCTruthPlus", [](
Event &event) {
event.mcTruthPlus.DisableIO(); }},
279 static const std::string &fnName =
"NAIAChain::CreateSkimTree";
282 auto outFile = std::make_unique<TFile>(filename.c_str(),
"recreate");
284 newChain->m_isMC =
m_isMC;
286 if (!exclBranches.empty()) {
287 for (
const std::string &container :
TokenizeString(exclBranches,
';')) {
289 logger->info(
"Disabling container {}", container);
292 logger->error(
"Unknown container {}", container);
298 newChain->m_event.MirrorBranches(newChain->m_data.tree,
m_event);
300 return {std::move(outFile), std::move(newChain),
this};
304 static const std::string &fnName =
"NAIAChain::SetEntryList";
309 logger->error(
"TEntryList not supported in Write mode");
320 static const std::string &fnName =
"NAIAChain::CheckVersion";
323 NAIA::Version this_version{};
325 auto file = std::unique_ptr<TFile>(TFile::Open(filePath.c_str()));
326 NAIA::Version *file_version = file->Get<NAIA::Version>(
"VersionHeader");
328 logger->error(
"NAIA version header not found in file {}. This file will be skipped.", filePath);
332 if (file_version->major != this_version.major) {
333 logger->error(
"Major version mismatch in file {} ({} on file vs {} in this library). This file will be skipped.",
334 filePath, file_version->major, this_version.major);
338 if (file_version->minor != this_version.minor) {
339 logger->warn(
"Minor version mismatch in file {} ({} on file vs {} in this library). This should be fine, however "
340 "please report any issue to the NAIA development team.",
341 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.
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.