22#include "FairEventHeader.h"
23#include "FairFileHeader.h"
24#include "FairLogger.h"
25#include "FairMCEventHeader.h"
26#include "FairRootManager.h"
27#include "FairRuntimeDb.h"
29#include "TChainElement.h"
35#include <TObjString.h>
49TMCThreadLocal
R3BFileSource* R3BFileSource::fSourceInstance = 0;
59 , fCheckInputBranches()
64 , fListFolder(new TObjArray(16))
65 , fRtdb(FairRuntimeDb::instance())
68 , fSourceIdentifier(0)
70 , IsInitialized(kFALSE)
74 , fEventTimeInMCHeader(kTRUE)
75 , fEvtHeaderIsNew(kFALSE)
85 , fCheckFileLayout(kTRUE)
93 R3BLOG(fatal,
"Singleton instance already exists.");
96 fSourceInstance =
this;
98 if (fRootFile->IsZombie())
100 R3BLOG(fatal,
"Error opening the Input file");
102 R3BLOG(debug,
"R3BFileSource created------------");
113 , fCheckInputBranches()
118 , fListFolder(new TObjArray(16))
119 , fRtdb(FairRuntimeDb::instance())
122 , fSourceIdentifier(0)
124 , IsInitialized(kFALSE)
128 , fEventTimeInMCHeader(kTRUE)
129 , fEvtHeaderIsNew(kFALSE)
131 , fTimeforEntryNo(-1)
139 , fCheckFileLayout(kTRUE)
147 R3BLOG(fatal,
"Singleton instance already exists.");
150 fSourceInstance =
this;
152 fRootFile = TFile::Open(RootFileName->Data());
153 if (fRootFile->IsZombie())
155 R3BLOG(fatal,
"Error opening the Input file");
157 R3BLOG(debug,
"R3BFileSource created------------");
168 , fCheckInputBranches()
173 , fListFolder(new TObjArray(16))
174 , fRtdb(FairRuntimeDb::instance())
177 , fSourceIdentifier(0)
179 , IsInitialized(kFALSE)
183 , fEventTimeInMCHeader(kTRUE)
184 , fEvtHeaderIsNew(kFALSE)
186 , fTimeforEntryNo(-1)
194 , fCheckFileLayout(kTRUE)
202 R3BLOG(fatal,
"Singleton instance already exists.");
205 fSourceInstance =
this;
207 fRootFile = TFile::Open(RootFileName.Data());
208 if (fRootFile->IsZombie())
210 R3BLOG(fatal,
"Error opening the Input file");
212 R3BLOG(debug,
"R3BFileSource created------------");
219 LOG(debug) <<
"Enter Destructor of R3BFileSource";
222 LOG(debug) <<
"Leave Destructor of R3BFileSource";
231 R3BLOG(info,
"R3BFileSource already initialized");
236 fInChain =
new TChain(FairRootManager::GetTreeName(), Form(
"/%s", FairRootManager::GetFolderName()));
237 R3BLOG(debug,
"Chain created");
238 FairRootManager::Instance()->SetInChain(fInChain);
240 fInChain->Add(fRootFile->GetName());
244 fFolderIn =
dynamic_cast<TFolder*
>(fRootFile->Get(FairRootManager::GetFolderName()));
247 fFolderIn =
dynamic_cast<TFolder*
>(fRootFile->Get(
"r3broot"));
250 fFolderIn =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmout"));
253 fFolderIn =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmroot"));
256 fFolderIn = gROOT->GetRootFolder()->AddFolder(FairRootManager::GetFolderName(),
"Main Folder");
260 fFolderIn->SetName(FairRootManager::GetFolderName());
277 TList* list =
dynamic_cast<TList*
>(fRootFile->Get(
"BranchList"));
280 R3BLOG(fatal,
"No Branch list in input file");
282 TString chainName = fInputTitle;
284 fInputLevel.push_back(chainName);
285 fCheckInputBranches[chainName] =
new std::list<TString>;
289 R3BLOG(debug,
"Enteries in the list " << list->GetEntries());
290 for (Int_t i = 0; i < list->GetEntries(); i++)
292 Obj =
dynamic_cast<TObjString*
>(list->At(i));
295 ObjName = Obj->GetString();
296 R3BLOG(debug,
"Branch name " << ObjName.Data());
297 fCheckInputBranches[chainName]->push_back(ObjName.Data());
299 FairRootManager::Instance()->AddBranchToList(ObjName.Data());
304 gROOT->GetListOfBrowsables()->Add(fFolderIn);
305 fListFolder->Add(fFolderIn);
316 for (
auto fileName : fInputChainList)
324 TFile* inputFile = TFile::Open(fileName);
325 if (inputFile->IsZombie())
327 LOG(fatal) <<
"Error opening the file " << fileName.Data()
328 <<
" which should be added to the input chain or as friend chain";
331 if (fCheckFileLayout)
337 LOG(fatal) <<
"Branch structure of the input file " << fRootFile->GetName()
338 <<
" and the file to be added " << fileName.Data() <<
" are different.";
346 fInChain->Add(fileName);
352 fNoOfEntries = fInChain->GetEntries();
354 R3BLOG(debug,
"Entries in this Source " << fNoOfEntries);
356 for (Int_t i = 0; i < fListFolder->GetEntriesFast(); i++)
358 TFolder* fold =
static_cast<TFolder*
>(fListFolder->At(i));
359 fEvtHeader =
static_cast<R3BEventHeader*
>(fold->FindObjectAny(
"EventHeader."));
360 fMCHeader =
static_cast<FairMCEventHeader*
>(fold->FindObjectAny(
"MCEventHeader."));
363 ActivateObject(
reinterpret_cast<TObject**
>(&fEvtHeader),
"EventHeader.");
367 ActivateObject(
reinterpret_cast<TObject**
>(&fMCHeader),
"MCEventHeader.");
371 FairRootManager::Instance()->SetListOfFolders(fListFolder);
375 TList* timebasedlist =
dynamic_cast<TList*
>(fRootFile->Get(
"TimeBasedBranchList"));
376 if (timebasedlist == 0)
378 LOG(warn) <<
"No time based branch list in input file";
382 FairRootManager::Instance()->SetTimeBasedBranchNameList(timebasedlist);
386 fInputFile.open(fInputFileName.Data(), std::fstream::in);
387 if (!fInputFile.is_open())
389 R3BLOG(warn,
"Input file for RunIds was not found, it is Ok!");
393 R3BLOG(info,
"Input file for RunIds " << fInputFileName.Data() <<
" was found");
395 fInputFile.seekg(0, std::ios::beg);
398 if (fInputFile.is_open())
400 R3BLOG(info,
"Reading RunId file");
404 while (fInputFile >> hex >> rid >> expRun >> ts)
406 fRunid.push_back(rid);
407 fTimestamp.push_back(ts);
409 fInputFile.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
412 R3BLOG(info,
"End of reading RunId file");
427 fRootFile =
static_cast<TFile*
>(tempTree->GetCurrentFile());
429 IsInitialized = kFALSE;
434Int_t R3BFileSource::GetRunid(uint64_t st)
436 UInt_t fArraysize = fTimestamp.size();
437 if (fArraysize != fRunid.size())
439 R3BLOG(error,
"\033[5m\033[31m Different number of RunIds and timestamps \033[0m");
445 for (Int_t j = 0; j < fArraysize - 1; j++)
446 if (st >= fTimestamp[j] && st < fTimestamp[j + 1])
448 prevts = fTimestamp[j];
449 nextts = fTimestamp[j + 1];
450 R3BLOG(debug,
"New timestamp " << nextts <<
" for RunId " << fRunid[j]);
454 if (st >= fTimestamp[fArraysize - 1])
456 prevts = fTimestamp[fArraysize - 1];
457 nextts = fTimestamp[fArraysize - 1];
458 R3BLOG(debug,
"Prev/next timestamp " << prevts <<
"/" << nextts <<
" for runid " << fRunid[fArraysize - 1]);
459 return fRunid[fArraysize - 1];
464 R3BLOG(warn,
"\033[5m\033[33m RunId was not found, it will be 1 \033[0m");
485 printf(
"Processed: \033[32m %d \033[0m of \033[34m %d \033[0m (\033[33m %.2f \033[0m of 100), current RunId "
486 "\033[31m %d \033[0m \r",
489 100. * i / (
double)(fNoOfEntries),
494 if (nextts > 0 && prevts >= 0 && (fEvtHeader->GetTimeStamp() > nextts || fEvtHeader->GetTimeStamp() < prevts))
496 fRunId = GetRunid(fEvtHeader->GetTimeStamp());
499 if (fInChain->GetEntry(i))
529 std::list<TString> friendList;
530 for (
auto fileName : fFriendFileList)
532 if (find(friendList.begin(), friendList.end(), fileName) == friendList.end())
534 friendList.push_back(fileName);
542 Int_t friendType = 1;
544 for (
auto fileName : friendList)
552 Bool_t inputLevelFound = kFALSE;
554 for (
auto level : fInputLevel)
558 inputFile = TFile::Open(fileName);
559 if (inputFile->IsZombie())
561 LOG(fatal) <<
"Error opening the file " << level.Data()
562 <<
" which should be added to the input chain or as friend chain";
570 inputLevelFound = kTRUE;
576 if (!inputLevelFound)
578 inputLevel = Form(
"FriendTree_%i", friendType);
583 TChain* chain =
static_cast<TChain*
>(fFriendTypeList[inputLevel]);
584 chain->AddFile(fileName, 1234567890, FairRootManager::GetTreeName());
593 for (
const auto& mi : fFriendTypeList)
595 TChain* chain =
static_cast<TChain*
>(mi.second);
596 fInChain->AddFriend(chain);
609 LOG(info) <<
"The input consists out of the following trees and files: ";
610 LOG(info) <<
" - " << fInChain->GetName();
611 TObjArray* fileElements = fInChain->GetListOfFiles();
612 TIter next(fileElements);
613 TChainElement* chEl = 0;
614 while ((chEl =
static_cast<TChainElement*
>(next())))
616 LOG(info) <<
" - " << chEl->GetTitle();
619 for (
const auto& mi : fFriendTypeList)
621 TChain* chain =
static_cast<TChain*
>(mi.second);
622 LOG(info) <<
" - " << chain->GetName();
623 fileElements = chain->GetListOfFiles();
624 TIter next1(fileElements);
626 while ((chEl =
static_cast<TChainElement*
>(next1())))
628 LOG(info) <<
" - " << chEl->GetTitle();
635 std::multimap<TString, std::multimap<TString, TArrayI>>::iterator it1;
636 std::multimap<TString, TArrayI> map1;
639 it1 = fRunIdInfoAll.find(
"InputChain");
641 std::vector<Int_t> runid;
642 std::vector<Int_t> events;
643 for (
auto& mmi : map1)
645 TArrayI bla = mmi.second;
646 runid.push_back(bla[0]);
647 events.push_back(bla[1]);
655 for (
auto level : fInputLevel)
658 if (!inputLevel.Contains(
"InputChain"))
660 it1 = fRunIdInfoAll.find(inputLevel);
662 if (runid.size() != map1.size())
668 for (
auto& mmi : map1)
670 TArrayI bla = mmi.second;
671 if ((bla[0] != runid[counter]) || (bla[1] != events[counter]))
687 LOG(error) <<
"The input chain and the friend chain " << inputLevel.Data() <<
" have a different structure:";
690 LOG(error) <<
"The input chain has the following runids and event numbers:";
691 for (UInt_t i = 0; i < runid.size(); i++)
693 LOG(error) <<
" - Runid " << runid[i] <<
" with " << events[i] <<
" events";
695 LOG(error) <<
"The " << inputLevel.Data() <<
" chain has the following runids and event numbers:";
696 for (
auto& mmi : map1)
698 TArrayI bla = mmi.second;
699 LOG(error) <<
" - Runid " << bla[0] <<
" with " << bla[1] <<
" events";
705 for (
auto& mmi : map1)
707 TArrayI bla = mmi.second;
708 LOG(error) <<
"Runid Input Chain, " << inputLevel.Data() <<
" chain: " << bla[0] <<
", "
710 LOG(error) <<
"Event number Input Chain, " << inputLevel.Data() <<
" chain: " << bla[1] <<
", "
715 LOG(fatal) <<
"Event structure mismatch";
723 TFile* f = TFile::Open(inputFile);
725 TFolder* added = NULL;
726 TString folderName1 = FairRootManager::GetFolderName();
727 TString folderName = Form(
"/%s", folderName1.Data());
728 added =
dynamic_cast<TFolder*
>(f->Get(folderName1));
731 folderName =
"/r3broot";
732 folderName1 =
"r3broot";
733 added =
dynamic_cast<TFolder*
>(f->Get(
"r3broot"));
736 folderName =
"/cbmout";
737 folderName1 =
"cbmout";
738 added =
dynamic_cast<TFolder*
>(f->Get(
"cbmout"));
741 folderName =
"/cbmroot";
742 folderName1 =
"cbmroot";
743 added =
dynamic_cast<TFolder*
>(f->Get(
"cbmroot"));
746 LOG(fatal) <<
"Could not find folder r3broot, cbmout nor cbmroot.";
752 folderName1 = folderName1 +
"_" + inputLevel;
753 added->SetName(folderName1);
754 fListFolder->Add(added);
757 TList* list =
dynamic_cast<TList*
>(f->Get(
"BranchList"));
758 TString chainName = inputLevel;
759 fInputLevel.push_back(chainName);
760 fCheckInputBranches[chainName] =
new std::list<TString>;
764 for (Int_t i = 0; i < list->GetEntries(); i++)
766 Obj =
dynamic_cast<TObjString*
>(list->At(i));
767 fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
768 std::cout << Obj->GetString().Data() << std::endl;
769 FairRootManager::Instance()->AddBranchToList(Obj->GetString().Data());
773 TChain* chain =
new TChain(inputLevel, folderName);
774 fFriendTypeList[inputLevel] = chain;
784 std::set<TString> branches;
785 for (
auto li : *(fCheckInputBranches[inputLevel]))
801 TList* list =
dynamic_cast<TList*
>(fileHandle->Get(
"BranchList"));
805 for (Int_t i = 0; i < list->GetEntries(); i++)
807 Obj =
dynamic_cast<TObjString*
>(list->At(i));
808 iter1 = branches.find(Obj->GetString().Data());
809 if (iter1 != branches.end())
811 branches.erase(iter1);
825 if (branches.size() != 0)
827 LOG(info) <<
"Compare Branch List will return kFALSE. The list has " << branches.size() <<
" branches:";
828 for (
auto branchName : branches)
829 LOG(info) <<
" -> " << branchName;
840 fInTree->SetBranchStatus(BrName, 1);
841 fInTree->SetBranchAddress(BrName, obj);
845 fInChain->SetBranchStatus(BrName, 1);
846 fInChain->SetBranchAddress(BrName, obj);
854 fRootFile = TFile::Open(name.Data());
855 if (fRootFile->IsZombie())
857 LOG(fatal) <<
"Error opening the Input file";
859 LOG(info) <<
"R3BFileSource set------------";
864 Int_t MaxEventNo = 0;
871 MaxEventNo = fInChain->GetEntries();
878 fEventMeanTime = mean;
879 fTimeProb =
new TF1(
"TimeProb",
"(1/[0])*exp(-x/[0])", 0., mean * 10);
880 fTimeProb->SetParameter(0, mean);
881 fTimeProb->GetRandom();
882 fEventTimeInMCHeader = kFALSE;
889 fEventMeanTime = (fEventTimeMin + fEventTimeMax) / 2.0;
890 fEventTimeInMCHeader = kFALSE;
895 fBeamTime = beamTime;
902 if (fTimeforEntryNo == fCurrentEntryNo)
904 LOG(debug) <<
"Set event time for Entry = " << fTimeforEntryNo <<
" , where the current entry is "
905 << fCurrentEntryNo <<
" and eventTime is " << fEventTime;
915 }
while (fmod(fEventTime, fBeamTime + fGapTime) > fBeamTime);
917 LOG(debug) <<
"New time = " << fEventTime;
918 fTimeforEntryNo = fCurrentEntryNo;
923 Double_t deltaTime = 0;
926 deltaTime = fTimeProb->GetRandom();
927 LOG(debug) <<
"Time set via sampling method : " << deltaTime;
931 deltaTime = gRandom->Uniform(fEventTimeMin, fEventTimeMax);
932 LOG(debug) <<
"Time set via Uniform Random : " << deltaTime;
939 LOG(debug) <<
"-- Get Event Time --";
940 if (!fEvtHeaderIsNew && fEvtHeader != 0)
942 Double_t EvtTime = fEvtHeader->GetEventTime();
949 if (fEventTimeInMCHeader && !fMCHeader)
951 LOG(debug) <<
"No MCEventHeader, time is set to 0";
954 else if (fEventTimeInMCHeader && fMCHeader)
956 fEventTime = fMCHeader->GetT();
957 LOG(debug) <<
"Get event time from MCEventHeader : " << fEventTime <<
" ns";
963 if (fTimeforEntryNo != fCurrentEntryNo)
967 LOG(debug) <<
"Calculate event time from user input : " << fEventTime <<
" ns";
981 fInTree->FindBranch(BrName)->GetEntry(fEvtHeader->GetMCEntryNumber());
987 fInChain->FindBranch(BrName)->GetEntry(fEvtHeader->GetMCEntryNumber());
996 fCurrentEntryNo = Entry;
999 fInTree->FindBranch(BrName)->GetEntry(Entry);
1005 fInChain->FindBranch(BrName)->GetEntry(Entry);
1023 feh->SetRunId(fRunId);
1024 feh->SetInputFileId(0);
#define R3BLOG(severity, x)
void FillEventHeader(FairEventHeader *feh) override
static R3BFileSource * Instance()
static instance
Int_t ReadEvent(UInt_t i=0) override
void SetBeamTime(Double_t beamTime, Double_t gapTime)
Set the repetition time of the beam when it can interact (beamTime) and when no interaction happen (g...
void SetEventTimeInterval(Double_t min, Double_t max)
Set the min and max limit for event time in ns.
Int_t CheckMaxEventNo(Int_t EvtEnd=0) override
Check the maximum event number we can run to.
R3BFileSource(TFile *f, const char *Title="InputRootFile", UInt_t identifier=0)
Double_t GetDeltaEventTime()
void SetInTree(TTree *tempTree)
Set the input tree when running on PROOF worker.
void AddFriend(TString FileName)
Add a friend file (input) by name)
void AddFile(TString FileName)
Add ROOT file to input, the file will be chained to already added files.
Bool_t ActivateObject(TObject **obj, const char *BrName) override
Bool_t SpecifyRunId()
Read one event from source to find out which RunId to use.
void SetInputFile(TString name)
~R3BFileSource() override
void CreateNewFriendChain(TString inputFile, TString inputLevel)
void ReadBranchEvent(const char *BrName) override
Read the tree entry on one branch.
Bool_t CompareBranchList(TFile *fileHandle, TString inputLevel)
void SetEventMeanTime(Double_t mean)
Set the mean time for the event in ns.