CbmRoot
CbmMCInputSet.cxx
Go to the documentation of this file.
1 
6 #include "CbmMCInputSet.h"
7 
8 #include "FairLogger.h"
9 #include "FairRootManager.h"
10 #include <cassert>
11 
12 
13 // ----- Default constructor ---------------------------------------------
15 // ---------------------------------------------------------------------------
16 
17 
18 // ----- Constructor -----------------------------------------------------
20  : TObject()
21  , fRate(rate)
22  , fInputs()
23  , fInputHandle()
24  , fBranches()
25  , fDeltaDist(nullptr) {
26 
27  if (rate > 0.) {
28  Double_t mean = 1.e9 / rate; // mean time between events
29  fDeltaDist = new TF1("DeltaDist", "exp(-x/[0])/[0]", 0., 10. * mean);
30  fDeltaDist->SetParameter(0, mean);
31  }
32  fInputHandle = fInputs.begin();
33 }
34 // ---------------------------------------------------------------------------
35 
36 
37 // ----- Destructor ------------------------------------------------------
39  if (fDeltaDist) delete fDeltaDist;
40  for (auto const& entry : fInputs)
41  if (entry.second) delete entry.second;
42 }
43 // ---------------------------------------------------------------------------
44 
45 
46 // ----- Set the branch address of an input branch -----------------------
47 Bool_t CbmMCInputSet::ActivateObject(TObject** object, const char* branchName) {
48 
49  // The branch address has to be set for each input chain
50  for (auto const& mapEntry : fInputs) {
51  CbmMCInput* input = mapEntry.second;
52  assert(input);
53  input->GetChain()->SetBranchStatus(branchName, 1);
54  input->GetChain()->SetBranchAddress(branchName, object);
55  }
56 
57  return kTRUE;
58 }
59 // ---------------------------------------------------------------------------
60 
61 
62 // ----- Add an input to the set -----------------------------------------
63 void CbmMCInputSet::AddInput(UInt_t inputId,
64  TChain* chain,
65  ECbmTreeAccess mode) {
66 
67  // Catch invalid chain pointer.
68  if (!chain) {
69  LOG(fatal) << "MCInputSet: invalid chain for input ID " << inputId << "!";
70  return;
71  } //? No valid input chain
72 
73  // Catch input ID already being used.
74  if (fInputs.find(inputId) != fInputs.end()) {
75  LOG(fatal) << "MCInputSet: input ID " << inputId << " is already defined!";
76  return;
77  } //? Input ID already used
78 
79  // Create CbmMCInput object
80  CbmMCInput* input = new CbmMCInput(chain, mode);
81 
82  // The first input defines the reference branch list.
83  if (fInputs.empty()) {
84  fBranches = input->GetBranchList();
85  } //? First input
86 
87  // Check compatibility of the input branch list with the reference list.
88  else {
89  if (!CheckBranchList(input)) {
90  LOG(fatal) << "MCInputSet: Incompatible branch list!";
91  return;
92  } //? Branch list not compatible
93  } //? Not first input
94 
95  // Register input and set input handle
96  fInputs[inputId] = input;
97  fInputHandle = fInputs.begin();
98 }
99 // ---------------------------------------------------------------------------
100 
101 
102 // ----- Check the branch list of an input -------------------------------
104 
105  assert(input);
106  Bool_t success = kTRUE;
107  for (auto const& entry : fBranches) {
108  auto it = input->GetBranchList().find(entry);
109  if (it == input->GetBranchList().end()) {
110  LOG(debug) << "MCInputSet: Required branch " << entry
111  << " not present in input!";
112  success = kFALSE;
113  break;
114  } //? Global branch not in input
115  } //# Global branches
116 
117  if (!success) {
118  std::stringstream ss;
119  ss << "MCInputSet: Reference branch list is ";
120  for (auto const& entry : fBranches)
121  ss << entry << " ";
122  LOG(info) << ss.str();
123  std::stringstream ss1;
124  ss1 << "MCInputSet: Input branch list is ";
125  for (auto const& entry : input->GetBranchList())
126  ss1 << entry << " ";
127  LOG(info) << ss1.str();
128  } //? Branches not compatible
129 
130  return success;
131 }
132 // ---------------------------------------------------------------------------
133 
134 
135 // ----- Time difference to next event -----------------------------------
137  if (!fDeltaDist) return 0.;
138  return fDeltaDist->GetRandom();
139 }
140 // ---------------------------------------------------------------------------
141 
142 
143 // ----- Maximal number of events to be read from the input --------------
145 
146  Int_t minimum = -1;
147 
148  for (auto const& entry : fInputs) {
149  Int_t test = entry.second->GetMaxNofEvents();
150  LOG(info) << "MCInputSet: Max. number of events for input " << entry.first
151  << " is " << test;
152  if (test >= 0 && (minimum == -1 || test < minimum)) minimum = test;
153  } //# Inputs
154 
155  minimum *= fInputs.size();
156  LOG(info) << "MCInputSet: Maximal number of events is " << minimum;
157 
158  return minimum;
159 }
160 // ---------------------------------------------------------------------------
161 
162 
163 // ----- Get next entry from chain ---------------------------------------
164 std::tuple<Bool_t, UInt_t, Int_t> CbmMCInputSet::GetNextEntry() {
165 
166  // Flag for having reached the last input
167  Bool_t allInputsUsed = kFALSE;
168 
169  // The input handle points to the input to be used
170  Int_t entry = fInputHandle->second->GetNextEntry();
171  Int_t inputId = fInputHandle->first;
172 
173  // Increment input handle. If end of set reached, signal that and
174  // reset the handle to the begin.
175  fInputHandle++;
176  if (fInputHandle == fInputs.end()) {
177  allInputsUsed = kTRUE;
178  fInputHandle = fInputs.begin();
179  }
180 
181  return std::make_tuple(allInputsUsed, inputId, entry);
182 }
183 // ---------------------------------------------------------------------------
184 
185 
186 // ----- Register input chains to FairRootManager ------------------------
188  for (auto const& mapEntry : fInputs) {
189  FairRootManager::Instance()->SetInChain(mapEntry.second->GetChain(),
190  mapEntry.first);
191  }
192 }
193 // ---------------------------------------------------------------------------
194 
195 
CbmMCInputSet::AddInput
void AddInput(UInt_t inputId, TChain *chain, ECbmTreeAccess mode=ECbmTreeAccess::kRegular)
Add an input to the set.
Definition: CbmMCInputSet.cxx:63
CbmMCInputSet::~CbmMCInputSet
virtual ~CbmMCInputSet()
Destructor.
Definition: CbmMCInputSet.cxx:38
CbmMCInputSet::GetNextEntry
std::tuple< Bool_t, UInt_t, Int_t > GetNextEntry()
Get the next entry from the inputs @value Status tuple.
Definition: CbmMCInputSet.cxx:164
CbmMCInputSet::GetMaxNofEvents
Int_t GetMaxNofEvents() const
Maximal number of events to be read from the input set @value Maximal number of events.
Definition: CbmMCInputSet.cxx:144
CbmMCInputSet::fInputs
std::map< UInt_t, CbmMCInput * > fInputs
Definition: CbmMCInputSet.h:143
CbmMCInputSet::fDeltaDist
TF1 * fDeltaDist
Definition: CbmMCInputSet.h:146
CbmMCInputSet::CbmMCInputSet
CbmMCInputSet()
Default constructor.
Definition: CbmMCInputSet.cxx:14
CbmMCInput::GetBranchList
std::set< TString > & GetBranchList()
List of branches @value Reference to branch list.
Definition: CbmMCInput.cxx:39
ECbmTreeAccess
ECbmTreeAccess
Mode to read entries from a ROOT TTree.
Definition: CbmDefs.h:130
CbmMCInputSet::fBranches
std::set< TString > fBranches
Definition: CbmMCInputSet.h:145
CbmMCInputSet.h
CbmMCInputSet::ActivateObject
virtual Bool_t ActivateObject(TObject **object, const char *branchName)
Activate and connect all input chains.
Definition: CbmMCInputSet.cxx:47
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMCInputSet::fInputHandle
std::map< UInt_t, CbmMCInput * >::iterator fInputHandle
Definition: CbmMCInputSet.h:144
CbmMCInput
An MC (transport) input to digitisation in CBM.
Definition: CbmMCInput.h:27
CbmMCInputSet::GetDeltaT
Double_t GetDeltaT()
Time difference to next event @value Time difference to next event [ns].
Definition: CbmMCInputSet.cxx:136
CbmMCInputSet::CheckBranchList
Bool_t CheckBranchList(CbmMCInput *input)
Compare an input branch list with the reference branch list.
Definition: CbmMCInputSet.cxx:103
CbmMCInputSet
A MC transport input to digitisation in CBM.
Definition: CbmMCInputSet.h:34
CbmMCInputSet::RegisterChains
void RegisterChains()
register all input chains to the FairRootManager
Definition: CbmMCInputSet.cxx:187
CbmMCInput::GetChain
TChain * GetChain() const
Pointer to chain @value Pointer to TChain object.
Definition: CbmMCInput.h:54