FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMixedSource.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 //
10 // FairMixedSource.cxx
11 // FAIRROOT
12 //
13 // Created by Mohammad Al-Turany on 08/02/14.
14 //
15 //
16 #include "FairMixedSource.h"
17 
18 #include "FairEventHeader.h"
19 #include "FairLogger.h"
20 #include "FairMCEventHeader.h"
21 #include "FairRootManager.h"
22 #include "FairRuntimeDb.h" // for FairRuntimeDb
23 
24 #include <TFolder.h>
25 #include <TList.h>
26 #include <TObjArray.h>
27 #include <TObjString.h>
28 #include <TObject.h>
29 #include <TROOT.h>
30 #include <TRandom.h> // for TRandom, gRandom
31 #include <TString.h>
32 #include <cmath> // floor, fmod
33 #include <map>
34 #include <set>
35 
36 using std::map;
37 using std::set;
38 
39 FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t)
40  : FairSource()
41  , fRootManager(0)
42  , fInputTitle(Title)
43  , fRootFile(f)
44  , fFriendFileList()
45  , fInputChainList()
46  , fFriendTypeList()
47  , fCheckInputBranches()
48  , fInputLevel()
49  , fRunIdInfoAll()
50  , fListFolder(new TObjArray(16))
51  , fRtdb(FairRuntimeDb::instance())
52  , fCbmout(0)
53  , fCbmroot(0)
54  , fSourceIdentifier(0)
55  , fNoOfEntries(-1)
56  , IsInitialized(kFALSE)
57  , fMCHeader(0)
58  , fEvtHeader(0)
59  , fOutHeader(0)
60  , fFileHeader(0)
61  , fEventTimeInMCHeader(kTRUE)
62  , fEvtHeaderIsNew(kFALSE)
63  , fCurrentEntryNo(0)
64  , fTimeforEntryNo(0)
65  , fNoOfBGEntries(0)
66  , fCurrentEntry()
67  , fEventTimeMin(0.)
68  , fEventTimeMax(0.)
69  , fEventTime(0.)
70  , fBeamTime(-1.)
71  , fGapTime(-1.)
72  , fEventMeanTime(0.)
73  , fTimeProb(0)
74  , fSignalBGN()
75  , fSBRatiobyN(kFALSE)
76  , fSBRatiobyT(kFALSE)
77  , fActualSignalIdentifier(0)
78  , fNoOfSignals(0)
79  , fSignalChainList(nullptr)
80  , fBackgroundChain(nullptr)
81  , fSignalTypeList()
82  , fRunIdFromBG(kFALSE)
83  , fRunIdFromSG(kFALSE)
84  , fRunIdFromSG_identifier(0)
85 {
86  if (fRootFile->IsZombie()) {
87  LOG(fatal) << "Error opening the Input file";
88  }
89  LOG(info) << "FairMixedSource created------------";
90 
91  fRootManager = FairRootManager::Instance();
92 }
93 
94 FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title, UInt_t)
95  : FairSource()
96  , fRootManager(0)
97  , fInputTitle(Title)
98  , fRootFile(0)
99  , fFriendFileList()
100  , fInputChainList()
101  , fFriendTypeList()
102  , fCheckInputBranches()
103  , fInputLevel()
104  , fRunIdInfoAll()
105  , fListFolder(new TObjArray(16))
106  , fRtdb(FairRuntimeDb::instance())
107  , fCbmout(0)
108  , fCbmroot(0)
109  , fSourceIdentifier(0)
110  , fNoOfEntries(-1)
111  , IsInitialized(kFALSE)
112  , fMCHeader(0)
113  , fEvtHeader(0)
114  , fOutHeader(0)
115  , fFileHeader(0)
116  , fEventTimeInMCHeader(kTRUE)
117  , fEvtHeaderIsNew(kFALSE)
118  , fCurrentEntryNo(0)
119  , fTimeforEntryNo(0)
120  , fNoOfBGEntries(0)
121  , fCurrentEntry()
122  , fEventTimeMin(0.)
123  , fEventTimeMax(0.)
124  , fEventTime(0.)
125  , fBeamTime(-1.)
126  , fGapTime(-1.)
127  , fEventMeanTime(0.)
128  , fTimeProb(0)
129  , fSignalBGN()
130  , fSBRatiobyN(kFALSE)
131  , fSBRatiobyT(kFALSE)
132  , fActualSignalIdentifier(0)
133  , fNoOfSignals(0)
134  , fSignalChainList(nullptr)
135  , fBackgroundChain(nullptr)
136  , fSignalTypeList()
137  , fRunIdFromBG(kFALSE)
138  , fRunIdFromSG(kFALSE)
139  , fRunIdFromSG_identifier(0)
140 {
141  fRootFile = TFile::Open(RootFileName->Data());
142  if (fRootFile->IsZombie()) {
143  LOG(fatal) << "Error opening the Input file";
144  }
145  fRootManager = FairRootManager::Instance();
146  LOG(info) << "FairMixedSource created------------";
147 }
148 
149 FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalId, const char* Title, UInt_t)
150  : FairSource()
151  , fRootManager(0)
152  , fInputTitle(Title)
153  , fRootFile(0)
154  , fFriendFileList()
155  , fInputChainList()
156  , fFriendTypeList()
157  , fCheckInputBranches()
158  , fInputLevel()
159  , fRunIdInfoAll()
160  , fListFolder(new TObjArray(16))
161  , fRtdb(FairRuntimeDb::instance())
162  , fCbmout(0)
163  , fCbmroot(0)
164  , fSourceIdentifier(0)
165  , fNoOfEntries(-1)
166  , IsInitialized(kFALSE)
167  , fMCHeader(0)
168  , fEvtHeader(0)
169  , fOutHeader(0)
170  , fFileHeader(0)
171  , fEventTimeInMCHeader(kTRUE)
172  , fEvtHeaderIsNew(kFALSE)
173  , fCurrentEntryNo(0)
174  , fTimeforEntryNo(0)
175  , fNoOfBGEntries(0)
176  , fCurrentEntry()
177  , fEventTimeMin(0.)
178  , fEventTimeMax(0.)
179  , fEventTime(0.)
180  , fBeamTime(-1.)
181  , fGapTime(-1.)
182  , fEventMeanTime(0.)
183  , fTimeProb(0)
184  , fSignalBGN()
185  , fSBRatiobyN(kFALSE)
186  , fSBRatiobyT(kFALSE)
187  , fActualSignalIdentifier(0)
188  , fNoOfSignals(0)
189  , fSignalChainList(nullptr)
190  , fBackgroundChain(nullptr)
191  , fSignalTypeList()
192  , fRunIdFromBG(kFALSE)
193  , fRunIdFromSG(kFALSE)
194  , fRunIdFromSG_identifier(0)
195 {
196  fRootFile = TFile::Open(RootFileName.Data());
197 
198  if (signalId == 0) {
199  SetBackgroundFile(RootFileName);
200  } else {
201  SetSignalFile(RootFileName, signalId);
202  }
203  fRootManager = FairRootManager::Instance();
204  // fBackgroundFile = TFile::Open(name);
205  // if (fBackgroundFile->IsZombie()) {
206  // } else {
207  // fBackgroundChain = new TChain(FairRootManager::GetTreeName(), "/cbmroot");
208  // fBackgroundChain->AddFile(name.Data());
209  // }
210 }
211 
214 {
215  fOutHeader = new FairEventHeader();
216  LOG(info) << "fSBRatiobyN = " << (fSBRatiobyN ? "true" : "false")
217  << " / fSBRatiobyT = " << (fSBRatiobyT ? "true" : "false");
218 
219  // LOG(info) << "*********** CHAIN HAS " << fBackgroundChain->GetEntries() << " entries";
220  if (IsInitialized) {
221  LOG(info) << "FairMixedSource already initialized";
222  return kTRUE;
223  }
224  if (!fBackgroundChain) {
225  fBackgroundChain = new TChain(FairRootManager::GetTreeName(), Form("/%s", FairRootManager::GetFolderName()));
226  LOG(info) << "FairMixedSource::Init() chain created";
227  }
228 
229  fBackgroundChain->Add(fRootFile->GetName());
230  LOG(info) << "*********** CHAIN HAS " << fBackgroundChain->GetEntries() << " entries";
231 
232  // Get the folder structure from file which describes the input tree.
233  // There are two different names possible, so check both.
234  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get(FairRootManager::GetFolderName()));
235  if (!fCbmroot) {
236  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmroot"));
237  if (!fCbmroot) {
238  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmout"));
239  if (!fCbmroot) {
240  fCbmroot = gROOT->GetRootFolder()->AddFolder(FairRootManager::GetFolderName(), "Main Folder");
241  } else {
242  fCbmroot->SetName(FairRootManager::GetFolderName());
243  }
244  }
245  }
246  // Get The list of branches from the input file and add it to the
247  // actual list of existing branches.
248  // Add this list of branches also to the map of input trees, which
249  // stores the information which branches belong to which input tree.
250  // There is at least one primary input tree, but there can be many
251  // additional friend trees.
252  // This information is needed to add new files to the correct friend
253  // tree. With this information it is also possible to check if the
254  // input files which are added to the input chain all have the same
255  // branch structure. Without this check it is possible to add trees
256  // with a different branch structure but the same tree name. ROOT
257  // probably only checks if the name of the tree is the same.
258 
259  TList* list = dynamic_cast<TList*>(fRootFile->Get("BranchList"));
260  if (list == 0)
261  LOG(fatal) << "No Branch list in input file";
262  TString chainName = fInputTitle;
263  TString ObjName;
264  fInputLevel.push_back(chainName);
265  fCheckInputBranches[chainName] = new std::list<TString>;
266  if (list) {
267  TObjString* Obj = 0;
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));
271  if (Obj != 0) {
272  ObjName = Obj->GetString();
273  LOG(info) << "Branch name " << ObjName.Data();
274  fCheckInputBranches[chainName]->push_back(ObjName.Data());
275 
276  FairRootManager::Instance()->AddBranchToList(ObjName.Data());
277  }
278  }
279  }
280 
281  gROOT->GetListOfBrowsables()->Add(fCbmroot);
282  fListFolder->Add(fCbmroot);
283 
284  // Store the information about the unique runids in the input file
285  // together with the filename and the number of events for each runid
286  // this information is needed later to check if inconsitencies exist
287  // between the main input chain and any of the friend chains.
288 
289  // GetRunIdInfo(fInFile->GetName(), chainName);
290 
291  // Add all additional input files to the input chain and do a consitency check
292  for (auto fileName : fInputChainList) {
293  // Store global gFile pointer for safety reasons.
294  // Set gFile to old value at the end of the routine.R
295  TFile* temp = gFile;
296 
297  // Temporarily open the input file to extract information which
298  // is needed to bring the friend trees in the correct order
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";
303  }
304 
305  // Check if the branchlist is the same as for the first input file.
306  Bool_t isOk = CompareBranchList(inputFile, chainName);
307  if (!isOk) {
308  LOG(fatal) << "Branch structure of the input file " << fRootFile->GetName() << " and the file to be added "
309  << fileName.Data();
310  return kFALSE;
311  }
312 
313  // Add the runid information for all files in the chain.
314  // GetRunIdInfo(inputFile->GetName(), chainName);
315  // Add the file to the input chain
316  fBackgroundChain->Add(fileName);
317 
318  // Close the temporarly file and restore the gFile pointer.
319  inputFile->Close();
320  gFile = temp;
321  }
322  fNoOfEntries = fBackgroundChain->GetEntries();
323  FairRootManager::Instance()->SetInChain(fBackgroundChain, 0);
324 
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."));
329  if (fEvtHeader) {
330  ActivateObject(reinterpret_cast<TObject**>(&fEvtHeader), "EventHeader.");
331  }
332  if (fMCHeader) {
333  ActivateObject(reinterpret_cast<TObject**>(&fMCHeader), "MCEventHeader.");
334  }
335  }
337 
338  LOG(info) << "Entries in this Source " << fNoOfEntries << " ------------";
339  return kTRUE;
340 }
341 
342 Bool_t FairMixedSource::SpecifyRunId()
343 {
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;
348 
349  if (fRunIdFromBG == fRunIdFromSG) {
350  LOG(fatal)
351  << "Please specify where to take the from: Signal or Bcckgrand by calling UseRunIdFromBG or UseRunIdFromSG";
352  return false;
353  }
354 
355  if (fRunIdFromBG) {
356  fBackgroundChain->GetEntry(0);
357  fBackgroundChain->Dump();
358  LOG(debug) << "---Get RunId from background chain --- ";
359  } else {
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 --- ";
364  } else {
365  LOG(fatal) << "--- Invalid signal identifier, Max number of signals : " << fNoOfSignals;
366  }
367  }
368  if (fEvtHeader) {
369  fOutHeader->SetRunId(fEvtHeader->GetRunId());
370  }
371  if (fMCHeader) {
372  fOutHeader->SetRunId(fMCHeader->GetRunID());
373  }
374  return kTRUE;
375 }
376 
378 {
379  SetEventTime();
380 
381  Double_t SBratio = gRandom->Uniform(0, 1);
382  Bool_t GetASignal = kFALSE;
383 
384  if (fSBRatiobyN || fSBRatiobyT) {
385  Double_t ratio = 0;
386  for (const auto& mi : fSignalBGN) {
387  ratio = mi.second;
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);
393  fOutHeader->SetMCEntryNumber(entry);
394  fOutHeader->SetInputFileId(mi.first);
395  fOutHeader->SetEventTime(GetEventTime());
396  GetASignal = kTRUE;
397  fCurrentEntry[mi.first] = entry + 1;
398  LOG(debug) << "---Get entry No. " << entry << " from signal chain number --- " << mi.first << " --- ";
399  break;
400  }
401  }
402  if (!GetASignal) {
403  UInt_t entry = fCurrentEntry[0];
404  fBackgroundChain->GetEntry(entry);
405  fOutHeader->SetMCEntryNumber(entry);
406  fOutHeader->SetInputFileId(0); // Background files has always 0 as Id
407  fOutHeader->SetEventTime(GetEventTime());
408  fCurrentEntry[0] = entry + 1;
409  LOG(debug) << "---Get entry from background chain --- ";
410  }
411  }
412 
413  fCurrentEntryNo = i;
414  fOutHeader->SetEventTime(GetEventTime());
415  LOG(debug) << "--Event number --- " << fCurrentEntryNo << " with time ----" << GetEventTime();
416 
417  return 0;
418 }
419 
421 
423 
425 {
426  feh->SetEventTime(fOutHeader->GetEventTime());
427  feh->SetMCEntryNumber(fOutHeader->GetMCEntryNumber());
428  feh->SetInputFileId(fOutHeader->GetInputFileId());
429  feh->SetRunId(fOutHeader->GetRunId());
430  LOG(debug) << "FairMixedSource::FillEventHeader() Event " << fCurrentEntryNo << " at " << feh->GetEventTime()
431  << " -> Run id = " << fOutHeader->GetRunId() << " event#" << feh->GetMCEntryNumber() << " from file#"
432  << fOutHeader->GetInputFileId();
433  return;
434 }
435 
436 Bool_t FairMixedSource::CompareBranchList(TFile* fileHandle, TString inputLevel)
437 {
438  // fill a set with the original branch structure
439  // This allows to use functions find and erase
440  std::set<TString> branches;
441  for (auto li : (*fCheckInputBranches[inputLevel])) {
442  branches.insert(li);
443  }
444 
445  // To do so we have to loop over the branches in the file and to compare
446  // the branches in the file with the information stored in
447  // fCheckInputBranches["InputChain"]. If both lists are equal everything
448  // is okay
449 
450  // Get The list of branches from the input file one by one and compare
451  // it to the reference list of branches which is defined for this tree.
452  // If a branch with the same name is found, this branch is removed from
453  // the list. If in the end no branch is left in the list everything is
454  // fine.
455  set<TString>::iterator iter1;
456  TList* list = dynamic_cast<TList*>(fileHandle->Get("BranchList"));
457  if (list) {
458  TObjString* Obj = 0;
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);
464  } else {
465  // Not found is an error because branch structure is
466  // different. It is impossible to add to tree with a
467  // different branch structure
468  return kFALSE;
469  }
470  }
471  }
472  // If the size of branches is !=0 after removing all branches also in the
473  // reference list, this is also a sign that both branch list are not the
474  // same
475  if (branches.size() != 0) {
476  return kFALSE;
477  }
478 
479  return kTRUE;
480 }
481 
482 void FairMixedSource::SetSignalFile(TString name, UInt_t identifier)
483 {
484  TFile* SignalInFile = TFile::Open(name.Data());
485  if (SignalInFile->IsZombie()) {
486  LOG(fatal) << "Error opening the Signal file";
487  } else {
489  if (fSignalTypeList[identifier] == 0) {
490  TChain* chain = new TChain(FairRootManager::GetTreeName(), Form("/%s", FairRootManager::GetFolderName()));
491  fSignalTypeList[identifier] = chain;
492  FairRootManager::Instance()->SetInChain(chain, identifier);
493  fCurrentEntry[identifier] = 0;
494  fNoOfSignals++;
495  fActualSignalIdentifier = identifier;
496  chain->AddFile(name.Data());
497  } else {
498  TChain* CurrentChain = fSignalTypeList[identifier];
499  CurrentChain->AddFile(name.Data());
500  // TObjArray* fileElements=CurrentChain->GetListOfFiles();
501  }
502  }
503 }
504 
505 void FairMixedSource::AddSignalFile(TString name, UInt_t identifier) { SetSignalFile(name, identifier); }
506 
508 {
509  if (i << fNoOfSignals) {
510  return fSignalTypeList[i];
511  } else {
512  LOG(info) << "Error signal identifier " << i << " does not exist ";
513  return 0;
514  }
515 }
516 
518 {
519  fCurrentEntry[0] = 0;
520  if (name.IsNull()) {
521  LOG(info) << "No background file defined.";
522  }
523  fRootFile = TFile::Open(name);
524  if (fRootFile->IsZombie()) {
525  LOG(fatal) << "Error opening the Background file " << name.Data();
526  }
527 }
528 
530 {
531  if (name.IsNull()) {
532  LOG(info) << "No background file defined.";
533  }
534  TFile* BGFile = TFile::Open(name);
535  if (BGFile->IsZombie()) {
536  LOG(fatal) << "Error opening the Background file " << name.Data();
537  } else {
538  if (fBackgroundChain != 0) {
539  fBackgroundChain->AddFile(name.Data());
540  // TObjArray* fileElements=fBackgroundChain->GetListOfFiles();
541  } else {
542  LOG(fatal) << "Use SetBackGroundFile first, then add files to background";
543  }
544  }
545 }
546 
548 {
549  // Get the folder structure from file which describes the input tree.
550  // There are two different names possible, so check both.
551  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get(FairRootManager::GetFolderName()));
552  if (!fCbmroot) {
553  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmroot"));
554  if (!fCbmroot) {
555  fCbmroot = dynamic_cast<TFolder*>(fRootFile->Get("cbmout"));
556  if (!fCbmroot) {
557  fCbmroot = gROOT->GetRootFolder()->AddFolder(FairRootManager::GetFolderName(), "Main Folder");
558  } else {
559  fCbmroot->SetName(FairRootManager::GetFolderName());
560  }
561  }
562  }
563 
564  // Get The list of branches from the input file and add it to the
565  // actual list of existing branches.
566  // Add this list of branches also to the map of input trees, which
567  // stores the information which branches belong to which input tree.
568  // There is at least one primary input tree, but there can be many
569  // additional friend trees.
570  // This information is needed to add new files to the correct friend
571  // tree. With this information it is also possible to check if the
572  // input files which are added to the input chain all have the same
573  // branch structure. Without this check it is possible to add trees
574  // with a different branch structure but the same tree name. ROOT
575  // probably only checks if the name of the tree is the same.
576 
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>;
581  if (list) {
582  TObjString* Obj = 0;
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());
586  FairRootManager::Instance()->AddBranchToList(Obj->GetString().Data());
587  }
588  }
589 
590  gROOT->GetListOfBrowsables()->Add(fCbmroot);
591  fListFolder->Add(fCbmroot);
592  return kTRUE;
593 }
594 
596 {
597  for (const auto& mi : fSignalTypeList) {
598  TChain* currentChain = mi.second;
599  // cout << "Signal chain is : " << currentChain->GetName()<< endl;
600  // currentChain->Dump();
601  TFile* ChainFirstFile = currentChain->GetFile();
602  // cout << "First file in signal chain is : " << ChainFirstFile << endl;
603  // Check if the branchlist is the same as for the first input file.
604  Bool_t isOk = CompareBranchList(ChainFirstFile, "BGInChain");
605  if (!isOk) {
606  LOG(fatal) << "Branch structure of the signal chain is different than the back ground one";
607  }
608  }
609  return kTRUE;
610 }
611 
612 Bool_t FairMixedSource::ActivateObject(TObject** obj, const char* BrName)
613 {
614  fBackgroundChain->SetBranchStatus(BrName, 1);
615  fBackgroundChain->SetBranchAddress(BrName, obj);
616 
617  Int_t no = 0;
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);
623  }
624 
625  return kTRUE;
626 }
627 
628 Bool_t FairMixedSource::ActivateObjectAny(void** obj, const std::type_info& info, const char* BrName)
629 {
630 
631  Bool_t BG_Object = ActivateObjectAnyImpl(fBackgroundChain, obj, info, BrName);
632 
633  for (const auto& mi : fSignalTypeList) {
634  TChain* currentChain = mi.second;
635  if (!(ActivateObjectAnyImpl(currentChain, obj, info, BrName)))
636  return false;
637  }
638  return BG_Object;
639 }
640 
642 {
643  if (0 == i) {
644  Int_t totEnt = fBackgroundChain->GetEntries();
645  LOG(info) << "The number of entries in background chain is " << totEnt;
646  }
647  fBackgroundChain->GetEntry(i);
648 }
649 
650 void FairMixedSource::BGWindowWidthNo(UInt_t background, UInt_t Signalid)
651 {
652  fSBRatiobyN = kTRUE;
653  if (fSBRatiobyT) {
654  LOG(fatal) << "Signal rate already set by TIME!!";
655  }
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;
660  } else {
661  LOG(fatal) << "Background cannot be Zero when setting the signal rate!!";
662  }
663 }
664 
665 void FairMixedSource::BGWindowWidthTime(Double_t background, UInt_t Signalid)
666 {
667  fSBRatiobyT = kTRUE;
668  if (fSBRatiobyN) {
669  LOG(fatal) << "Signal rate already set by NUMBER!!";
670  }
671  if (fEventTimeInMCHeader) {
672  LOG(fatal) << "You have to Set the Event mean time before using SetSignalRateTime!";
673  }
674  if (fEventMeanTime == 0) {
675  LOG(fatal) << "Event mean time cannot be zero when using signal rate with time ";
676  }
678  Double_t value = fEventMeanTime / background;
679  if (background != 0) {
680  fSignalBGN[Signalid] = value;
681  } else {
682  LOG(fatal) << "Background cannot be Zero when setting the signal rate!!";
683  }
684 }
685 
687 {
688  Int_t MaxEventNo = 0;
689  if (EvtEnd != 0) {
690  MaxEventNo = EvtEnd;
691  }
692  Int_t localMax = 0;
693  Int_t MaxBG = fBackgroundChain->GetEntries();
694  Int_t MaxS = 0;
695  Double_t ratio = 0.;
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 ";
700  ratio = mi.second;
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;
704  } else {
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;
708  }
709  if (MaxEventNo == 0 || MaxEventNo > localMax) {
710  MaxEventNo = localMax;
711  }
712  }
713  LOG(info) << "Maximum No of Event will be set to : " << MaxEventNo;
714  return MaxEventNo;
715 }
716 
718 {
719  fEventMeanTime = mean;
720  /*
721  TString form="(1/";
722  form+= mean;
723  form+=")*exp(-x/";
724  form+=mean;
725  form+=")";
726  fTimeProb= new TF1("TimeProb", form.Data(), 0., mean*10);
727 */
728  fTimeProb = new TF1("TimeProb", "(1/[0])*exp(-x/[0])", 0., mean * 10);
729  fTimeProb->SetParameter(0, mean);
730  fTimeProb->GetRandom();
731  fEventTimeInMCHeader = kFALSE;
732 }
733 
734 void FairMixedSource::SetEventTimeInterval(Double_t min, Double_t max)
735 {
736  fEventTimeMin = min;
737  fEventTimeMax = max;
738  fEventMeanTime = (fEventTimeMin + fEventTimeMax) / 2;
739  fEventTimeInMCHeader = kFALSE;
740 }
741 
742 void FairMixedSource::SetBeamTime(Double_t beamTime, Double_t gapTime)
743 {
744  fBeamTime = beamTime;
745  fGapTime = gapTime;
746 }
747 
749 {
750  LOG(debug) << "Set event time for Entry = " << fTimeforEntryNo << " , where the current entry is "
751  << fCurrentEntryNo << " and eventTime is " << fEventTime;
752  if (fBeamTime < 0) {
753  fEventTime += GetDeltaEventTime();
754  } else {
755  do {
756  fEventTime += GetDeltaEventTime();
757  } while (fmod(fEventTime, fBeamTime + fGapTime) > fBeamTime);
758  }
759  LOG(debug) << "New time = " << fEventTime;
760  fTimeforEntryNo = fCurrentEntryNo;
761 }
762 
764 {
765  Double_t deltaTime = 0;
766  if (fTimeProb != 0) {
767  deltaTime = fTimeProb->GetRandom();
768  LOG(debug) << "Time set via sampling method : " << deltaTime;
769  } else {
770  deltaTime = gRandom->Uniform(fEventTimeMin, fEventTimeMax);
771  LOG(debug) << "Time set via Uniform Random : " << deltaTime;
772  }
773  return deltaTime;
774 }
775 
777 {
778  LOG(debug) << "-- Get Event Time --";
779  if (!fEvtHeaderIsNew && fEvtHeader != 0) {
780  Double_t EvtTime = fEvtHeader->GetEventTime();
781  if (!(EvtTime < 0)) {
782  return EvtTime;
783  }
784  }
785 
786  if (fEventTimeInMCHeader && !fMCHeader) {
787  LOG(debug) << "No MCEventHeader, time is set to 0";
788  return 0;
789  } else if (fEventTimeInMCHeader && fMCHeader) {
790  fEventTime = fMCHeader->GetT();
791  LOG(debug) << "Get event time from MCEventHeader : " << fEventTime << " ns";
792  return fEventTime;
793  } else {
794 
795  if (fTimeforEntryNo != fCurrentEntryNo) {
796  SetEventTime();
797  }
798  LOG(debug) << "Calculate event time from user input : " << fEventTime << " ns";
799  return fEventTime;
800  }
801 }
802 
803 void FairMixedSource::ReadBranchEvent(const char* BrName)
804 {
806  if (fEvtHeader == 0) {
807  return;
808  } // No event header, Reading will start later
809  TChain* chain = fSignalTypeList[fEvtHeader->GetInputFileId()];
810  if (!chain) {
811  return;
812  }
813  chain->FindBranch(BrName)->GetEntry(fEvtHeader->GetMCEntryNumber());
814  return;
815 }
816 
817 void FairMixedSource::ReadBranchEvent(const char* BrName, Int_t Entry)
818 {
820  if (fEvtHeader == 0) {
821  return;
822  } // No event header, Reading will start later
823  TChain* chain = fSignalTypeList[fEvtHeader->GetInputFileId()];
824  if (!chain) {
825  return;
826  }
827  chain->FindBranch(BrName)->GetEntry(Entry);
828  return;
829 }
830 
832 {
834  fRunIdFromBG = kTRUE;
835 }
836 
837 void FairMixedSource::UseRunIdfromSG(UInt_t identifier)
838 {
842  fRunIdFromSG = kTRUE;
843  fRunIdFromSG_identifier = identifier;
844 }
static char * GetTreeName()
void SetMCEntryNumber(Int_t id)
void FillEventHeader(FairEventHeader *feh)
list of container factories
Definition: FairRuntimeDb.h:24
Bool_t OpenBackgroundChain()
FairMixedSource(TFile *f, const char *Title="InputRootFile", UInt_t identifier=0)
UInt_t GetRunID() const
void ReadBKEvent(UInt_t i=0)
Int_t GetMCEntryNumber()
virtual Bool_t ActivateObjectAny(void **, const std::type_info &, const char *)
void SetEventTime(Double_t time)
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)
Double_t GetEventTime()
void SetEventTimeInterval(Double_t min, Double_t max)
Double_t GetEventTime()
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 SetInputFileId(Int_t id)
void BGWindowWidthNo(UInt_t background, UInt_t Signalid)
Int_t GetInputFileId()
Double_t GetT() const
vertex z [cm]
static char * GetFolderName()
void SetSignalFile(TString name, UInt_t identifier)
void SetRunId(UInt_t runid)
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)