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>
65 ,
fRtdb(FairRuntimeDb::instance())
93 R3BLOG(fatal,
"Singleton instance already exists.");
100 R3BLOG(fatal,
"Error opening the Input file");
102 R3BLOG(debug,
"R3BFileSource created------------");
119 ,
fRtdb(FairRuntimeDb::instance())
147 R3BLOG(fatal,
"Singleton instance already exists.");
152 fRootFile = TFile::Open(RootFileName->Data());
155 R3BLOG(fatal,
"Error opening the Input file");
157 R3BLOG(debug,
"R3BFileSource created------------");
174 ,
fRtdb(FairRuntimeDb::instance())
202 R3BLOG(fatal,
"Singleton instance already exists.");
207 fRootFile = TFile::Open(RootFileName.Data());
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);
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");
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());
299 FairRootManager::Instance()->AddBranchToList(ObjName.Data());
304 gROOT->GetListOfBrowsables()->Add(
fFolderIn);
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";
337 LOG(fatal) <<
"Branch structure of the input file " <<
fRootFile->GetName()
338 <<
" and the file to be added " << fileName.Data() <<
" are different.";
356 for (Int_t i = 0; i <
fListFolder->GetEntriesFast(); i++)
358 TFolder* fold =
static_cast<TFolder*
>(
fListFolder->At(i));
360 fMCHeader =
static_cast<FairMCEventHeader*
>(fold->FindObjectAny(
"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);
389 R3BLOG(warn,
"Input file for RunIds was not found, it is Ok!");
400 R3BLOG(info,
"Reading RunId file");
404 while (
fInputFile >> hex >> rid >> expRun >> 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());
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++)
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",
529 std::list<TString> friendList;
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;
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);
584 chain->AddFile(fileName, 1234567890, FairRootManager::GetTreeName());
595 TChain* chain =
static_cast<TChain*
>(mi.second);
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();
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;
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]);
658 if (!inputLevel.Contains(
"InputChain"))
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);
757 TList* list =
dynamic_cast<TList*
>(f->Get(
"BranchList"));
758 TString chainName = inputLevel;
764 for (Int_t i = 0; i < list->GetEntries(); i++)
766 Obj =
dynamic_cast<TObjString*
>(list->At(i));
768 std::cout << Obj->GetString().Data() << std::endl;
769 FairRootManager::Instance()->AddBranchToList(Obj->GetString().Data());
773 TChain* chain =
new TChain(inputLevel, folderName);
784 std::set<TString> branches;
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);
857 LOG(fatal) <<
"Error opening the Input file";
859 LOG(info) <<
"R3BFileSource set------------";
864 Int_t MaxEventNo = 0;
871 MaxEventNo =
fInChain->GetEntries();
879 fTimeProb =
new TF1(
"TimeProb",
"(1/[0])*exp(-x/[0])", 0., mean * 10);
904 LOG(debug) <<
"Set event time for Entry = " <<
fTimeforEntryNo <<
" , where the current entry is "
923 Double_t deltaTime = 0;
927 LOG(debug) <<
"Time set via sampling method : " << deltaTime;
932 LOG(debug) <<
"Time set via Uniform Random : " << deltaTime;
939 LOG(debug) <<
"-- Get Event Time --";
942 Double_t EvtTime =
fEvtHeader->GetEventTime();
951 LOG(debug) <<
"No MCEventHeader, time is set to 0";
957 LOG(debug) <<
"Get event time from MCEventHeader : " <<
fEventTime <<
" ns";
967 LOG(debug) <<
"Calculate event time from user input : " <<
fEventTime <<
" ns";
999 fInTree->FindBranch(BrName)->GetEntry(Entry);
1005 fInChain->FindBranch(BrName)->GetEntry(Entry);
1023 feh->SetRunId(fRunId);
1024 feh->SetInputFileId(0);
#define R3BLOG(severity, x)
std::map< TString, TChain * > fFriendTypeList
FairMCEventHeader * fMCHeader
MC Event header.
std::ifstream fInputFile
input file with runids
FairRuntimeDb * fRtdb
RuntimeDb.
void FillEventHeader(FairEventHeader *feh) override
std::list< TString > fInputLevel
Bool_t fEvtHeaderIsNew
This flag is true if the event header was created in this session otherwise it is false which means t...
static R3BFileSource * Instance()
static instance
Bool_t fEventTimeInMCHeader
This is true if the event time used, came from simulation.
std::list< TString > fFriendFileList
List of all files added with AddFriend.
FairFileHeader * fFileHeader
File Header.
std::map< TString, std::list< TString > * > fCheckInputBranches
Int_t ReadEvent(UInt_t i=0) override
Double_t fGapTime
Time without particles in beam (gap) (ns)
R3BEventHeader * fEvtHeader
Event Header.
TFolder * fFolderOut
folder structure of output
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...
TFile * fRootFile
ROOT file.
void SetEventTimeInterval(Double_t min, Double_t max)
Set the min and max limit for event time in ns.
TString fInputTitle
Title of input source, could be input, background or signal.
TObjArray * fListFolder
list of folders from all input (and friends) files
Int_t CheckMaxEventNo(Int_t EvtEnd=0) override
Check the maximum event number we can run to.
UInt_t fCurrentEntryNo
for internal use, to return the same event time for the same entry
UInt_t fNoOfEntries
No of Entries in this source.
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)
TF1 * fTimeProb
used to generate random numbers for event time;
Double_t fEventTimeMax
max time for one Event (ns)
void AddFile(TString FileName)
Add ROOT file to input, the file will be chained to already added files.
std::list< TString > fInputChainList
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
Bool_t fCheckFileLayout
True if the file layout should be checked when adding files to a chain.
Int_t fCurrentEntryNr
Current Entry number.
std::vector< uint64_t > fTimestamp
static TMCThreadLocal R3BFileSource * fSourceInstance
Double_t fEventTimeMin
min time for one event (ns)
Double_t fEventMeanTime
EventMean time used (P(t)=1/fEventMeanTime*Exp(-t/fEventMeanTime)
Double_t fEventTime
Time of event since th start (ns)
std::map< TString, std::multimap< TString, TArrayI > > fRunIdInfoAll
void CreateNewFriendChain(TString inputFile, TString inputLevel)
TFolder * fFolderIn
folder structure of input
UInt_t fTimeforEntryNo
for internal use, to return the same event time for the same entry
TTree * fInTree
Input Tree.
void ReadBranchEvent(const char *BrName) override
Read the tree entry on one branch.
Int_t GetRunid(uint64_t ts)
GetRunid method to obtain the runid as function of timestamps.
Double_t fBeamTime
Time of particles in beam (ns)
TChain * fInChain
Input Chain.
Bool_t CompareBranchList(TFile *fileHandle, TString inputLevel)
void SetEventMeanTime(Double_t mean)
Set the mean time for the event in ns.
Bool_t IsInitialized
Initialization flag, true if initialized.
std::vector< UInt_t > fRunid