26 #include <TObjArray.h>
27 #include <TObjString.h>
47 , fCheckInputBranches()
50 , fListFolder(new TObjArray(16))
54 , fSourceIdentifier(0)
56 , IsInitialized(kFALSE)
61 , fEventTimeInMCHeader(kTRUE)
62 , fEvtHeaderIsNew(kFALSE)
77 , fActualSignalIdentifier(0)
79 , fSignalChainList(nullptr)
80 , fBackgroundChain(nullptr)
82 , fRunIdFromBG(kFALSE)
83 , fRunIdFromSG(kFALSE)
84 , fRunIdFromSG_identifier(0)
86 if (fRootFile->IsZombie()) {
87 LOG(fatal) <<
"Error opening the Input file";
89 LOG(info) <<
"FairMixedSource created------------";
102 , fCheckInputBranches()
105 , fListFolder(new TObjArray(16))
109 , fSourceIdentifier(0)
111 , IsInitialized(kFALSE)
116 , fEventTimeInMCHeader(kTRUE)
117 , fEvtHeaderIsNew(kFALSE)
130 , fSBRatiobyN(kFALSE)
131 , fSBRatiobyT(kFALSE)
132 , fActualSignalIdentifier(0)
134 , fSignalChainList(nullptr)
135 , fBackgroundChain(nullptr)
137 , fRunIdFromBG(kFALSE)
138 , fRunIdFromSG(kFALSE)
139 , fRunIdFromSG_identifier(0)
141 fRootFile = TFile::Open(RootFileName->Data());
142 if (fRootFile->IsZombie()) {
143 LOG(fatal) <<
"Error opening the Input file";
146 LOG(info) <<
"FairMixedSource created------------";
157 , fCheckInputBranches()
160 , fListFolder(new TObjArray(16))
164 , fSourceIdentifier(0)
166 , IsInitialized(kFALSE)
171 , fEventTimeInMCHeader(kTRUE)
172 , fEvtHeaderIsNew(kFALSE)
185 , fSBRatiobyN(kFALSE)
186 , fSBRatiobyT(kFALSE)
187 , fActualSignalIdentifier(0)
189 , fSignalChainList(nullptr)
190 , fBackgroundChain(nullptr)
192 , fRunIdFromBG(kFALSE)
193 , fRunIdFromSG(kFALSE)
194 , fRunIdFromSG_identifier(0)
196 fRootFile = TFile::Open(RootFileName.Data());
216 LOG(info) <<
"fSBRatiobyN = " << (fSBRatiobyN ?
"true" :
"false")
217 <<
" / fSBRatiobyT = " << (fSBRatiobyT ?
"true" :
"false");
221 LOG(info) <<
"FairMixedSource already initialized";
224 if (!fBackgroundChain) {
226 LOG(info) <<
"FairMixedSource::Init() chain created";
229 fBackgroundChain->Add(fRootFile->GetName());
230 LOG(info) <<
"*********** CHAIN HAS " << fBackgroundChain->GetEntries() <<
" entries";
236 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmroot"));
238 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmout"));
259 TList* list =
dynamic_cast<TList*
>(fRootFile->Get(
"BranchList"));
261 LOG(fatal) <<
"No Branch list in input file";
262 TString chainName = fInputTitle;
264 fInputLevel.push_back(chainName);
265 fCheckInputBranches[chainName] =
new std::list<TString>;
268 LOG(info) <<
"Enteries in the list " << list->GetEntries();
269 for (Int_t i = 0; i < list->GetEntries(); i++) {
270 Obj =
dynamic_cast<TObjString*
>(list->At(i));
272 ObjName = Obj->GetString();
273 LOG(info) <<
"Branch name " << ObjName.Data();
274 fCheckInputBranches[chainName]->push_back(ObjName.Data());
281 gROOT->GetListOfBrowsables()->Add(fCbmroot);
282 fListFolder->Add(fCbmroot);
292 for (
auto fileName : fInputChainList) {
299 TFile* inputFile = TFile::Open(fileName);
300 if (inputFile->IsZombie()) {
301 LOG(fatal) <<
"Error opening the file " << fileName.Data()
302 <<
" which should be added to the input chain or as friend chain";
308 LOG(fatal) <<
"Branch structure of the input file " << fRootFile->GetName() <<
" and the file to be added "
316 fBackgroundChain->Add(fileName);
322 fNoOfEntries = fBackgroundChain->GetEntries();
325 for (Int_t i = 0; i < fListFolder->GetEntriesFast(); i++) {
326 TFolder* fold =
static_cast<TFolder*
>(fListFolder->At(i));
327 fEvtHeader =
static_cast<FairEventHeader*
>(fold->FindObjectAny(
"EventHeader."));
328 fMCHeader =
static_cast<FairMCEventHeader*
>(fold->FindObjectAny(
"MCEventHeader."));
330 ActivateObject(reinterpret_cast<TObject**>(&fEvtHeader),
"EventHeader.");
333 ActivateObject(reinterpret_cast<TObject**>(&fMCHeader),
"MCEventHeader.");
338 LOG(info) <<
"Entries in this Source " << fNoOfEntries <<
" ------------";
342 Bool_t FairMixedSource::SpecifyRunId()
344 LOG(debug) <<
"---FairMixedSource::SpecifyRunId --- ";
345 LOG(debug) <<
"---FairMixedSource::SpecifyRunId fRunIdFromBG = " << fRunIdFromBG;
346 LOG(debug) <<
"---FairMixedSource::SpecifyRunId fRunIdFromSG = " << fRunIdFromSG;
347 LOG(debug) <<
"---FairMixedSource::SpecifyRunId fRunIdFromSG_identifier = " << fRunIdFromSG_identifier;
349 if (fRunIdFromBG == fRunIdFromSG) {
351 <<
"Please specify where to take the from: Signal or Bcckgrand by calling UseRunIdFromBG or UseRunIdFromSG";
356 fBackgroundChain->GetEntry(0);
357 fBackgroundChain->Dump();
358 LOG(debug) <<
"---Get RunId from background chain --- ";
360 if (fRunIdFromSG_identifier <= fNoOfSignals) {
361 TChain* Sig_chain = fRootManager->
GetSignalChainNo(fRunIdFromSG_identifier);
362 Sig_chain->GetEntry(0);
363 LOG(debug) <<
"---Get RunId from Signal chain --- ";
365 LOG(fatal) <<
"--- Invalid signal identifier, Max number of signals : " << fNoOfSignals;
381 Double_t SBratio = gRandom->Uniform(0, 1);
382 Bool_t GetASignal = kFALSE;
384 if (fSBRatiobyN || fSBRatiobyT) {
386 for (
const auto& mi : fSignalBGN) {
388 LOG(debug) <<
"---Check signal no. " << mi.first <<
" SBratio " << SBratio <<
" : ratio " << ratio;
389 if (SBratio <= ratio) {
390 TChain* chain = fSignalTypeList[mi.first];
391 UInt_t entry = fCurrentEntry[mi.first];
392 chain->GetEntry(entry);
397 fCurrentEntry[mi.first] = entry + 1;
398 LOG(debug) <<
"---Get entry No. " << entry <<
" from signal chain number --- " << mi.first <<
" --- ";
403 UInt_t entry = fCurrentEntry[0];
404 fBackgroundChain->GetEntry(entry);
408 fCurrentEntry[0] = entry + 1;
409 LOG(debug) <<
"---Get entry from background chain --- ";
415 LOG(debug) <<
"--Event number --- " << fCurrentEntryNo <<
" with time ----" <<
GetEventTime();
430 LOG(debug) <<
"FairMixedSource::FillEventHeader() Event " << fCurrentEntryNo <<
" at " << feh->
GetEventTime()
440 std::set<TString> branches;
441 for (
auto li : (*fCheckInputBranches[inputLevel])) {
455 set<TString>::iterator iter1;
456 TList* list =
dynamic_cast<TList*
>(fileHandle->Get(
"BranchList"));
459 for (Int_t i = 0; i < list->GetEntries(); i++) {
460 Obj =
dynamic_cast<TObjString*
>(list->At(i));
461 iter1 = branches.find(Obj->GetString().Data());
462 if (iter1 != branches.end()) {
463 branches.erase(iter1);
475 if (branches.size() != 0) {
484 TFile* SignalInFile = TFile::Open(name.Data());
485 if (SignalInFile->IsZombie()) {
486 LOG(fatal) <<
"Error opening the Signal file";
489 if (fSignalTypeList[identifier] == 0) {
491 fSignalTypeList[identifier] = chain;
493 fCurrentEntry[identifier] = 0;
495 fActualSignalIdentifier = identifier;
496 chain->AddFile(name.Data());
498 TChain* CurrentChain = fSignalTypeList[identifier];
499 CurrentChain->AddFile(name.Data());
509 if (i << fNoOfSignals) {
510 return fSignalTypeList[i];
512 LOG(info) <<
"Error signal identifier " << i <<
" does not exist ";
519 fCurrentEntry[0] = 0;
521 LOG(info) <<
"No background file defined.";
523 fRootFile = TFile::Open(name);
524 if (fRootFile->IsZombie()) {
525 LOG(fatal) <<
"Error opening the Background file " << name.Data();
532 LOG(info) <<
"No background file defined.";
534 TFile* BGFile = TFile::Open(name);
535 if (BGFile->IsZombie()) {
536 LOG(fatal) <<
"Error opening the Background file " << name.Data();
538 if (fBackgroundChain != 0) {
539 fBackgroundChain->AddFile(name.Data());
542 LOG(fatal) <<
"Use SetBackGroundFile first, then add files to background";
553 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmroot"));
555 fCbmroot =
dynamic_cast<TFolder*
>(fRootFile->Get(
"cbmout"));
577 TList* list =
dynamic_cast<TList*
>(fRootFile->Get(
"BranchList"));
578 TString chainName =
"BGInChain";
579 fInputLevel.push_back(chainName);
580 fCheckInputBranches[chainName] =
new std::list<TString>;
583 for (Int_t i = 0; i < list->GetEntries(); i++) {
584 Obj =
dynamic_cast<TObjString*
>(list->At(i));
585 fCheckInputBranches[chainName]->push_back(Obj->GetString().Data());
590 gROOT->GetListOfBrowsables()->Add(fCbmroot);
591 fListFolder->Add(fCbmroot);
597 for (
const auto& mi : fSignalTypeList) {
598 TChain* currentChain = mi.second;
601 TFile* ChainFirstFile = currentChain->GetFile();
606 LOG(fatal) <<
"Branch structure of the signal chain is different than the back ground one";
614 fBackgroundChain->SetBranchStatus(BrName, 1);
615 fBackgroundChain->SetBranchAddress(BrName, obj);
618 for (
const auto& mi : fSignalTypeList) {
619 TChain* currentChain = mi.second;
620 LOG(debug2) <<
"Set the Branch address for signal file number " << no++ <<
" and branch " << BrName;
621 currentChain->SetBranchStatus(BrName, 1);
622 currentChain->SetBranchAddress(BrName, obj);
631 Bool_t BG_Object = ActivateObjectAnyImpl(fBackgroundChain, obj, info, BrName);
633 for (
const auto& mi : fSignalTypeList) {
634 TChain* currentChain = mi.second;
635 if (!(ActivateObjectAnyImpl(currentChain, obj, info, BrName)))
644 Int_t totEnt = fBackgroundChain->GetEntries();
645 LOG(info) <<
"The number of entries in background chain is " << totEnt;
647 fBackgroundChain->GetEntry(i);
654 LOG(fatal) <<
"Signal rate already set by TIME!!";
656 Double_t value = 1.0 / (background + 1.0);
657 LOG(debug) <<
"-- Signal id = " << Signalid <<
" with weight = " << value;
658 if (background != 0) {
659 fSignalBGN[Signalid] = value;
661 LOG(fatal) <<
"Background cannot be Zero when setting the signal rate!!";
669 LOG(fatal) <<
"Signal rate already set by NUMBER!!";
671 if (fEventTimeInMCHeader) {
672 LOG(fatal) <<
"You have to Set the Event mean time before using SetSignalRateTime!";
674 if (fEventMeanTime == 0) {
675 LOG(fatal) <<
"Event mean time cannot be zero when using signal rate with time ";
678 Double_t value = fEventMeanTime / background;
679 if (background != 0) {
680 fSignalBGN[Signalid] = value;
682 LOG(fatal) <<
"Background cannot be Zero when setting the signal rate!!";
688 Int_t MaxEventNo = 0;
693 Int_t MaxBG = fBackgroundChain->GetEntries();
696 for (
const auto& mi : fSignalBGN) {
697 TChain* chain = fSignalTypeList[mi.first];
698 MaxS = chain->GetEntries();
699 LOG(info) <<
"Signal chain No " << mi.first <<
" has : " << MaxS <<
" entries ";
701 if (floor(MaxS / ratio) > MaxBG) {
702 localMax = MaxBG +
static_cast<Int_t
>(floor(MaxBG * ratio));
703 LOG(warn) <<
"No of Event in Background chain is not enough for all signals in chain " << mi.first;
705 localMax =
static_cast<Int_t
>(floor(MaxS / ratio));
706 LOG(warn) <<
"No of Event in signal chain " << mi.first
707 <<
" is not enough, the maximum event number will be reduced to : " << localMax;
709 if (MaxEventNo == 0 || MaxEventNo > localMax) {
710 MaxEventNo = localMax;
713 LOG(info) <<
"Maximum No of Event will be set to : " << MaxEventNo;
719 fEventMeanTime = mean;
728 fTimeProb =
new TF1(
"TimeProb",
"(1/[0])*exp(-x/[0])", 0., mean * 10);
729 fTimeProb->SetParameter(0, mean);
730 fTimeProb->GetRandom();
731 fEventTimeInMCHeader = kFALSE;
738 fEventMeanTime = (fEventTimeMin + fEventTimeMax) / 2;
739 fEventTimeInMCHeader = kFALSE;
744 fBeamTime = beamTime;
750 LOG(debug) <<
"Set event time for Entry = " << fTimeforEntryNo <<
" , where the current entry is "
751 << fCurrentEntryNo <<
" and eventTime is " << fEventTime;
757 }
while (fmod(fEventTime, fBeamTime + fGapTime) > fBeamTime);
759 LOG(debug) <<
"New time = " << fEventTime;
760 fTimeforEntryNo = fCurrentEntryNo;
765 Double_t deltaTime = 0;
766 if (fTimeProb != 0) {
767 deltaTime = fTimeProb->GetRandom();
768 LOG(debug) <<
"Time set via sampling method : " << deltaTime;
770 deltaTime = gRandom->Uniform(fEventTimeMin, fEventTimeMax);
771 LOG(debug) <<
"Time set via Uniform Random : " << deltaTime;
778 LOG(debug) <<
"-- Get Event Time --";
779 if (!fEvtHeaderIsNew && fEvtHeader != 0) {
781 if (!(EvtTime < 0)) {
786 if (fEventTimeInMCHeader && !fMCHeader) {
787 LOG(debug) <<
"No MCEventHeader, time is set to 0";
789 }
else if (fEventTimeInMCHeader && fMCHeader) {
790 fEventTime = fMCHeader->
GetT();
791 LOG(debug) <<
"Get event time from MCEventHeader : " << fEventTime <<
" ns";
795 if (fTimeforEntryNo != fCurrentEntryNo) {
798 LOG(debug) <<
"Calculate event time from user input : " << fEventTime <<
" ns";
806 if (fEvtHeader == 0) {
820 if (fEvtHeader == 0) {
827 chain->FindBranch(BrName)->GetEntry(Entry);
834 fRunIdFromBG = kTRUE;
842 fRunIdFromSG = kTRUE;
843 fRunIdFromSG_identifier = identifier;
static char * GetTreeName()
void FillEventHeader(FairEventHeader *feh)
list of container factories
Bool_t OpenBackgroundChain()
FairMixedSource(TFile *f, const char *Title="InputRootFile", UInt_t identifier=0)
void ReadBKEvent(UInt_t i=0)
virtual Bool_t ActivateObjectAny(void **, const std::type_info &, const char *)
void UseRunIdfromSG(UInt_t identifier=1)
void AddBackgroundFile(TString name)
virtual Int_t CheckMaxEventNo(Int_t EvtEnd=0)
Int_t AddBranchToList(const char *name)
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
void SetEventTimeInterval(Double_t min, Double_t max)
virtual ~FairMixedSource()
TChain * GetSignalChainNo(UInt_t i)
virtual Bool_t ActivateObject(TObject **obj, const char *BrName)
void SetInChain(TChain *tempChain, Int_t ident=-1)
void ReadBranchEvent(const char *BrName)
TChain * GetSignalChainNo(UInt_t i)
void AddSignalFile(TString name, UInt_t identifier)
void SetBackgroundFile(TString name)
void SetListOfFolders(TObjArray *ta)
void BGWindowWidthNo(UInt_t background, UInt_t Signalid)
static char * GetFolderName()
void SetSignalFile(TString name, UInt_t identifier)
void BGWindowWidthTime(Double_t background, UInt_t Signalid)
void SetBeamTime(Double_t beamTime, Double_t gapTime)
void SetEventMeanTime(Double_t mean)
Double_t GetDeltaEventTime()
Int_t ReadEvent(UInt_t i=0)
Bool_t CompareBranchList(TFile *fileHandle, TString inputLevel)