CbmRoot
Loading...
Searching...
No Matches
CbmMCInput.cxx
Go to the documentation of this file.
1/* Copyright (C) 2018-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer] */
4
10#include "CbmMCInput.h"
11
12#include "FairMCEventHeader.h"
13#include <Logger.h>
14
15#include "TFile.h"
16#include "TList.h"
17#include "TObjString.h"
18#include "TRandom.h"
19
20#include <cassert>
21
22
23// ----- Default constructor ---------------------------------------------
25// ---------------------------------------------------------------------------
26
27
28// ----- Constructor -----------------------------------------------------
30 : TObject()
31 , fChain(chain)
32 , fMode(mode)
33 , fBranches()
34 , fLastUsedEntry(-1)
35 , fNofUsedEntries(0)
36{
37}
38// ---------------------------------------------------------------------------
39
40
41// ----- Destructor ------------------------------------------------------
43// ---------------------------------------------------------------------------
44
45
46// ----- Get branch list -------------------------------------------------
47std::set<TString>& CbmMCInput::GetBranchList()
48{
49
50 // At first call, read branch list from file
51 if (fBranches.empty()) ReadBranches();
52
53 return fBranches;
54}
55// ---------------------------------------------------------------------------
56
57
58// ----- Get next entry from chain ---------------------------------------
60{
61
62 assert(fChain);
63
64 // Determine entry number to be retrieved
65 Int_t entry = -1;
66 if (fMode == ECbmTreeAccess::kRandom) { entry = gRandom->Integer(GetNofEntries()); } //? Random entry number
67 else {
68 entry = fLastUsedEntry + 1;
69 if (entry >= GetNofEntries()) entry = (fMode == ECbmTreeAccess::kRepeat ? 0 : -1);
70 } //? Sequential entry number
71
72 // Stop run when entryId is -1. This happens when mode is kRegular and
73 // the end of the chain is reached.
74 if (entry < 0) return -1;
75
76 // Get entry from chain
77 assert(entry >= 0 && entry < GetNofEntries()); // Just to make sure...
78 Int_t nBytes = fChain->GetEntry(entry);
79 if (nBytes <= 0) LOG(warn) << "InputChain: " << nBytes << " Bytes read from tree!";
80 fLastUsedEntry = entry;
82
83 return entry;
84}
85// ---------------------------------------------------------------------------
86
87
88// ----- Get list of data branches from file -----------------------------
90{
91
92 fBranches.clear();
93 TList* listFile = fChain->GetFile()->Get<TList>("BranchList");
94 assert(listFile);
95 TObjString* branchName = nullptr;
96 for (Int_t entry = 0; entry < listFile->GetEntries(); entry++) {
97 branchName = dynamic_cast<TObjString*>(listFile->At(entry));
98 assert(branchName);
99 fBranches.insert(branchName->GetString());
100 } //# Entries in branch list
101
102 return fBranches.size();
103}
104// ---------------------------------------------------------------------------
105
106
ClassImp(CbmConverterManager)
ECbmTreeAccess
Mode to read entries from a ROOT TTree.
Definition CbmDefs.h:152
An MC (transport) input to digitisation in CBM.
Definition CbmMCInput.h:33
UInt_t fNofUsedEntries
Definition CbmMCInput.h:100
ECbmTreeAccess fMode
Input chain.
Definition CbmMCInput.h:97
TChain * fChain
Definition CbmMCInput.h:96
UInt_t fLastUsedEntry
Definition CbmMCInput.h:99
std::set< TString > fBranches
Definition CbmMCInput.h:98
std::set< TString > & GetBranchList()
List of branches @value Reference to branch list.
Int_t GetNextEntry()
Get the next unused entry from the chain @value Id of tree entry.
CbmMCInput()
Default constructor.
virtual ~CbmMCInput()
Destructor.
Long64_t GetNofEntries() const
Number of entries @value Number of entries in this input chain.
Definition CbmMCInput.h:86
UInt_t ReadBranches()
Read list of branches from file @value Number of branches.