CbmRoot
CbmMCInput.cxx
Go to the documentation of this file.
1 
6 #include "CbmMCInput.h"
7 
8 #include "FairLogger.h"
9 #include "FairMCEventHeader.h"
10 #include "TFile.h"
11 #include "TList.h"
12 #include "TObjString.h"
13 #include "TRandom.h"
14 #include <cassert>
15 
16 
17 // ----- Default constructor ---------------------------------------------
19 // ---------------------------------------------------------------------------
20 
21 
22 // ----- Constructor -----------------------------------------------------
24  : TObject()
25  , fChain(chain)
26  , fMode(mode)
27  , fBranches()
28  , fLastUsedEntry(-1)
29  , fNofUsedEntries(0) {}
30 // ---------------------------------------------------------------------------
31 
32 
33 // ----- Destructor ------------------------------------------------------
35 // ---------------------------------------------------------------------------
36 
37 
38 // ----- Get branch list -------------------------------------------------
39 std::set<TString>& CbmMCInput::GetBranchList() {
40 
41  // At first call, read branch list from file
42  if (fBranches.empty()) ReadBranches();
43 
44  return fBranches;
45 }
46 // ---------------------------------------------------------------------------
47 
48 
49 // ----- Get next entry from chain ---------------------------------------
51 
52  assert(fChain);
53 
54  // Determine entry number to be retrieved
55  Int_t entry = -1;
57  entry = gRandom->Integer(GetNofEntries());
58  } //? Random entry number
59  else {
60  entry = fLastUsedEntry + 1;
61  if (entry >= GetNofEntries())
62  entry = (fMode == ECbmTreeAccess::kRepeat ? 0 : -1);
63  } //? Sequential entry number
64 
65  // Stop run when entryId is -1. This happens when mode is kRegular and
66  // the end of the chain is reached.
67  if (entry < 0) return -1;
68 
69  // Get entry from chain
70  assert(entry >= 0 && entry < GetNofEntries()); // Just to make sure...
71  Int_t nBytes = fChain->GetEntry(entry);
72  if (nBytes <= 0)
73  LOG(warn) << "InputChain: " << nBytes << " Bytes read from tree!";
74  fLastUsedEntry = entry;
76 
77  return entry;
78 }
79 // ---------------------------------------------------------------------------
80 
81 
82 // ----- Get list of data branches from file -----------------------------
84 
85  fBranches.clear();
86  TList* listFile = dynamic_cast<TList*>(fChain->GetFile()->Get("BranchList"));
87  assert(listFile);
88  TObjString* branchName = nullptr;
89  for (Int_t entry = 0; entry < listFile->GetEntries(); entry++) {
90  branchName = dynamic_cast<TObjString*>(listFile->At(entry));
91  assert(branchName);
92  fBranches.insert(branchName->GetString());
93  } //# Entries in branch list
94 
95  return fBranches.size();
96 }
97 // ---------------------------------------------------------------------------
98 
99 
CbmMCInput::fMode
ECbmTreeAccess fMode
Input chain.
Definition: CbmMCInput.h:93
CbmMCInput::fNofUsedEntries
UInt_t fNofUsedEntries
Definition: CbmMCInput.h:96
CbmMCInput::~CbmMCInput
virtual ~CbmMCInput()
Destructor.
Definition: CbmMCInput.cxx:34
ECbmTreeAccess::kRegular
@ kRegular
CbmMCInput::GetNofEntries
Long64_t GetNofEntries() const
Number of entries @value Number of entries in this input chain.
Definition: CbmMCInput.h:82
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
CbmMCInput::fLastUsedEntry
UInt_t fLastUsedEntry
Definition: CbmMCInput.h:95
CbmMCInput::GetNextEntry
Int_t GetNextEntry()
Get the next unused entry from the chain @value Id of tree entry.
Definition: CbmMCInput.cxx:50
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMCInput
An MC (transport) input to digitisation in CBM.
Definition: CbmMCInput.h:27
ECbmTreeAccess::kRepeat
@ kRepeat
CbmMCInput::fChain
TChain * fChain
Definition: CbmMCInput.h:92
CbmMCInput::ReadBranches
UInt_t ReadBranches()
Read list of branches from file @value Number of branches.
Definition: CbmMCInput.cxx:83
CbmMCInput::CbmMCInput
CbmMCInput()
Default constructor.
Definition: CbmMCInput.cxx:18
CbmMCInput::fBranches
std::set< TString > fBranches
Definition: CbmMCInput.h:94
ECbmTreeAccess::kRandom
@ kRandom
CbmMCInput.h