CMS 3D CMS Logo

RootTree.h
Go to the documentation of this file.
1 #ifndef IOPool_Input_RootTree_h
2 #define IOPool_Input_RootTree_h
3 
4 /*----------------------------------------------------------------------
5 
6 RootTree.h // used by ROOT input sources
7 
8 ----------------------------------------------------------------------*/
9 
17 
18 #include "Rtypes.h"
19 #include "TBranch.h"
20 
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include <unordered_set>
25 #include <unordered_map>
26 
27 class TBranch;
28 class TClass;
29 class TTree;
30 class TTreeCache;
31 
32 namespace edm {
33  class BranchKey;
34  class RootDelayedReader;
35  class InputFile;
36  class RootTree;
37 
38  class StreamContext;
39  class ModuleCallingContext;
40 
41  namespace signalslot {
42  template <typename T>
43  class Signal;
44  }
45 
46  namespace roottree {
47  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
48  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
49  unsigned int const defaultLearningEntries = 20U;
50  unsigned int const defaultNonEventLearningEntries = 1U;
52  struct BranchInfo {
55  productBranch_(nullptr),
56  provenanceBranch_(nullptr),
57  classCache_(nullptr),
60  TBranch* productBranch_;
61  TBranch* provenanceBranch_; // For backward compatibility
62  //All access to a ROOT file is serialized
63  CMS_SA_ALLOW mutable TClass* classCache_;
65  };
66 
67  class BranchMap {
68  enum {
71  };
72 
73  public:
74  void reserve(size_t iSize) { map_.reserve(iSize); }
75  void insert(edm::BranchID const& iKey, BranchInfo const& iInfo) { map_.emplace(iKey.id(), iInfo); }
76  BranchInfo const* find(BranchID const& iKey) const {
77  auto itFound = map_.find(iKey.id());
78  if (itFound == map_.end()) {
79  return nullptr;
80  }
81  return &itFound->second;
82  }
83  BranchInfo* find(BranchID const& iKey) {
84  auto itFound = map_.find(iKey.id());
85  if (itFound == map_.end()) {
86  return nullptr;
87  }
88  return &itFound->second;
89  }
90 
91  private:
92  std::unordered_map<unsigned int, BranchInfo> map_;
93  };
94 
95  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
96  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
97  std::unique_ptr<TTreeCache> trainCache(TTree* tree,
98  InputFile& file,
99  unsigned int cacheSize,
100  char const* branchNames);
101  } // namespace roottree
102 
103  class RootTree {
104  public:
107  RootTree(std::shared_ptr<InputFile> filePtr,
108  BranchType const& branchType,
109  unsigned int nIndexes,
110  unsigned int maxVirtualSize,
111  unsigned int cacheSize,
112  unsigned int learningEntries,
113  bool enablePrefetching,
114  InputType inputType);
115  ~RootTree();
116 
117  RootTree(RootTree const&) = delete; // Disallow copying and moving
118  RootTree& operator=(RootTree const&) = delete; // Disallow copying and moving
119 
120  bool isValid() const;
121  void numberOfBranchesToAdd(size_t iSize) { branches_.reserve(iSize); }
122  void addBranch(BranchDescription const& prod, std::string const& oldBranchName);
123  void dropBranch(std::string const& oldBranchName);
124  void getEntry(TBranch* branch, EntryNumber entry) const;
125  void setPresence(BranchDescription& prod, std::string const& oldBranchName);
126 
127  bool next() { return ++entryNumber_ < entries_; }
128  bool nextWithCache();
129  bool previous() { return --entryNumber_ >= 0; }
130  bool current() const { return entryNumber_ < entries_ && entryNumber_ >= 0; }
131  bool current(EntryNumber entry) const { return entry < entries_ && entry >= 0; }
132  void rewind() { entryNumber_ = 0; }
133  void close();
134  bool skipEntries(unsigned int& offset);
135  EntryNumber const& entryNumber() const { return entryNumber_; }
136  EntryNumber const& entryNumberForIndex(unsigned int index) const;
137  EntryNumber const& entries() const { return entries_; }
138  void setEntryNumber(EntryNumber theEntryNumber);
139  void insertEntryForIndex(unsigned int index);
140  std::vector<std::string> const& branchNames() const { return branchNames_; }
143  template <typename T>
144  void fillAux(T*& pAux) {
145  auxBranch_->SetAddress(&pAux);
147  }
148  template <typename T>
149  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
150  if (metaTree_ != nullptr) {
151  // Metadata was in separate tree. Not cached.
152  branch->SetAddress(&pbuf);
154  } else {
155  fillBranchEntry<T>(branch, pbuf);
156  }
157  }
158 
159  template <typename T>
160  void fillBranchEntry(TBranch* branch, T*& pbuf) {
161  branch->SetAddress(&pbuf);
163  }
164 
165  template <typename T>
167  if (metaTree_ != nullptr) {
168  // Metadata was in separate tree. Not cached.
169  branch->SetAddress(&pbuf);
171  } else {
172  fillBranchEntry<T>(branch, entryNumber, pbuf);
173  }
174  }
175 
176  template <typename T>
177  void fillBranchEntry(TBranch* branch, EntryNumber entryNumber, T*& pbuf) {
178  branch->SetAddress(&pbuf);
180  }
181 
182  TTree const* tree() const { return tree_; }
183  TTree* tree() { return tree_; }
184  TTree const* metaTree() const { return metaTree_; }
185  BranchMap const& branches() const;
186 
187  //For backwards compatibility
188  TBranch* branchEntryInfoBranch() const { return branchEntryInfoBranch_; }
189 
190  inline TTreeCache* checkTriggerCache(TBranch* branch, EntryNumber entryNumber) const;
191  TTreeCache* checkTriggerCacheImpl(TBranch* branch, EntryNumber entryNumber) const;
192  inline TTreeCache* selectCache(TBranch* branch, EntryNumber entryNumber) const;
193  void trainCache(char const* branchNames);
194  void resetTraining() { trainNow_ = true; }
195 
196  BranchType branchType() const { return branchType_; }
197 
198  void setSignals(
199  signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* preEventReadSource,
200  signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* postEventReadSource);
201 
202  private:
203  void setCacheSize(unsigned int cacheSize);
204  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
205  void startTraining();
206  void stopTraining();
207 
208  std::shared_ptr<InputFile> filePtr_;
209  // We use bare pointers for pointers to some ROOT entities.
210  // Root owns them and uses bare pointers internally.
211  // Therefore,using smart pointers here will do no good.
212  TTree* tree_;
213  TTree* metaTree_;
215  TBranch* auxBranch_;
216  // We use a smart pointer to own the TTreeCache.
217  // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
218  // So, we make sure to it is detached before closing the TFile so there is no double delete.
219  std::shared_ptr<TTreeCache> treeCache_;
220  std::shared_ptr<TTreeCache> rawTreeCache_;
221  //All access to a ROOT file is serialized
222  CMS_SA_ALLOW mutable std::shared_ptr<TTreeCache> triggerTreeCache_;
223  CMS_SA_ALLOW mutable std::shared_ptr<TTreeCache> rawTriggerTreeCache_;
224  CMS_SA_ALLOW mutable std::unordered_set<TBranch*> trainedSet_;
225  CMS_SA_ALLOW mutable std::unordered_set<TBranch*> triggerSet_;
228  std::unique_ptr<std::vector<EntryNumber> > entryNumberForIndex_;
229  std::vector<std::string> branchNames_;
231  bool trainNow_;
235  unsigned int learningEntries_;
236  unsigned int cacheSize_;
237  unsigned long treeAutoFlush_;
238  // Enable asynchronous I/O in ROOT (done in a separate thread). Only takes
239  // effect on the primary treeCache_; all other caches have this explicitly disabled.
242  std::unique_ptr<RootDelayedReader> rootDelayedReader_;
243 
244  TBranch* branchEntryInfoBranch_; //backwards compatibility
245  // below for backward compatibility
246  TTree* infoTree_; // backward compatibility
247  TBranch* statusBranch_; // backward compatibility
248  };
249 } // namespace edm
250 #endif
edm::RootTree::triggerTreeCache_
std::shared_ptr< TTreeCache > triggerTreeCache_
Definition: RootTree.h:222
edm::RootTree::tree
TTree const * tree() const
Definition: RootTree.h:182
edm::RootTree::treeAutoFlush_
unsigned long treeAutoFlush_
Definition: RootTree.h:237
ProvenanceFwd.h
edm::roottree::BranchMap::reserve
void reserve(size_t iSize)
Definition: RootTree.h:74
edm::RootTree::addBranch
void addBranch(BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:137
edm::RootTree::trainCache
void trainCache(char const *branchNames)
Definition: RootTree.cc:461
edm::RootTree::setEntryNumber
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:202
edm::RootTree::rootDelayedReader
DelayedReader * rootDelayedReader() const
Definition: RootTree.cc:128
edm::RootTree::cacheSize_
unsigned int cacheSize_
Definition: RootTree.h:236
edm::RootTree::EntryNumber
roottree::EntryNumber EntryNumber
Definition: RootTree.h:106
edm::RootTree::fillBranchEntry
void fillBranchEntry(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:177
edm::RootTree::startTraining
void startTraining()
Definition: RootTree.cc:396
edm::roottree::trainCache
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:516
edm::roottree::defaultNonEventLearningEntries
unsigned const int defaultNonEventLearningEntries
Definition: RootTree.h:50
edm::RootTree::tree_
TTree * tree_
Definition: RootTree.h:212
edm::roottree::BranchMap::map_
std::unordered_map< unsigned int, BranchInfo > map_
Definition: RootTree.h:92
edm::RootTree::branches
BranchMap const & branches() const
Definition: RootTree.cc:177
edm::RootTree::statusBranch_
TBranch * statusBranch_
Definition: RootTree.h:247
edm::roottree::BranchMap::find
BranchInfo const * find(BranchID const &iKey) const
Definition: RootTree.h:76
MicroEventContent_cff.branch
branch
Definition: MicroEventContent_cff.py:152
edm::roottree::BranchInfo::classCache_
TClass * classCache_
Definition: RootTree.h:63
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::RootTree::auxBranch_
TBranch * auxBranch_
Definition: RootTree.h:215
mps_splice.entry
entry
Definition: mps_splice.py:68
tree
Definition: tree.py:1
edm::RootTree::trainedSet_
std::unordered_set< TBranch * > trainedSet_
Definition: RootTree.h:224
edm::RootTree::rawTriggerTreeCache_
std::shared_ptr< TTreeCache > rawTriggerTreeCache_
Definition: RootTree.h:223
edm::RootTree::fillBranchEntryMeta
void fillBranchEntryMeta(TBranch *branch, EntryNumber entryNumber, T *&pbuf)
Definition: RootTree.h:166
edm::RootTree::rawTreeCache_
std::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:220
edm::RootTree::~RootTree
~RootTree()
Definition: RootTree.cc:98
edm::RootTree::checkTriggerCacheImpl
TTreeCache * checkTriggerCacheImpl(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:266
edm::RootTree::entries_
EntryNumber entries_
Definition: RootTree.h:226
IndexIntoFile.h
edm::RootTree::setCacheSize
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:179
Signal
Definition: vlib.h:28
edm::RootTree::operator=
RootTree & operator=(RootTree const &)=delete
edm::InputFile
Definition: InputFile.h:21
edm::BranchID::id
unsigned int id() const
Definition: BranchID.h:21
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
edm::RootTree::branches_
BranchMap branches_
Definition: RootTree.h:230
edm::InputType
InputType
Definition: InputType.h:5
edm::BranchType
BranchType
Definition: BranchType.h:11
edm::roottree::BranchMap::find
BranchInfo * find(BranchID const &iKey)
Definition: RootTree.h:83
edm::roottree::BranchMap::kInfos
Definition: RootTree.h:70
InputFile
Definition: FedRawDataInputSource.h:206
edm::RootTree::numberOfBranchesToAdd
void numberOfBranchesToAdd(size_t iSize)
Definition: RootTree.h:121
edm::RootTree::resetTraining
void resetTraining()
Definition: RootTree.h:194
edm::RootTree::RootTree
RootTree(std::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int nIndexes, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries, bool enablePrefetching, InputType inputType)
Definition: RootTree.cc:29
edm::RootTree::branchEntryInfoBranch_
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:244
edm::RootTree::entryNumberForIndex_
std::unique_ptr< std::vector< EntryNumber > > entryNumberForIndex_
Definition: RootTree.h:228
edm::roottree::EntryNumber
IndexIntoFile::EntryNumber_t EntryNumber
Definition: RootTree.h:51
edm::RootTree::skipEntries
bool skipEntries(unsigned int &offset)
Definition: RootTree.cc:381
edm::RootTree::enablePrefetching_
bool enablePrefetching_
Definition: RootTree.h:240
edm::RootTree::metaTree
TTree const * metaTree() const
Definition: RootTree.h:184
edm::StreamContext
Definition: StreamContext.h:31
edm::RootTree::switchOverEntry_
EntryNumber switchOverEntry_
Definition: RootTree.h:232
edm::RootTree::selectCache
TTreeCache * selectCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:347
edm::roottree::BranchInfo::offsetToWrapperBase_
Int_t offsetToWrapperBase_
Definition: RootTree.h:64
edm::RootTree::filePtr_
std::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:208
edm::RootTree::entryNumberForIndex
EntryNumber const & entryNumberForIndex(unsigned int index) const
Definition: RootTree.cc:100
edm::BranchID
Definition: BranchID.h:14
dumpMFGeometry_cfg.prod
prod
Definition: dumpMFGeometry_cfg.py:24
edm::RootTree::fillAux
void fillAux(T *&pAux)
Definition: RootTree.h:144
edm::RootTree::branchNames_
std::vector< std::string > branchNames_
Definition: RootTree.h:229
edm::RootTree::fillBranchEntry
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:160
edm::roottree::defaultCacheSize
unsigned const int defaultCacheSize
Definition: RootTree.h:47
edm::RootTree::triggerSet_
std::unordered_set< TBranch * > triggerSet_
Definition: RootTree.h:225
edm::RootTree::infoTree_
TTree * infoTree_
Definition: RootTree.h:246
edm::RootTree::resetAndGetRootDelayedReader
DelayedReader * resetAndGetRootDelayedReader() const
Definition: RootTree.cc:123
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
InputType.h
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::RootTree::getEntry
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:365
BranchDescription.h
edm::RootTree::branchType_
BranchType branchType_
Definition: RootTree.h:214
edm::RootTree::enableTriggerCache_
bool enableTriggerCache_
Definition: RootTree.h:241
edm::RootTree::entryNumber_
EntryNumber entryNumber_
Definition: RootTree.h:227
edm::roottree::BranchInfo::provenanceBranch_
TBranch * provenanceBranch_
Definition: RootTree.h:61
thread_safety_macros.h
edm::RootTree::dropBranch
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:153
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
edm::roottree::defaultLearningEntries
unsigned const int defaultLearningEntries
Definition: RootTree.h:49
edm::roottree::BranchInfo
Definition: RootTree.h:52
edm::RootTree::checkTriggerCache
TTreeCache * checkTriggerCache(TBranch *branch, EntryNumber entryNumber) const
Definition: RootTree.cc:256
edm::RootTree::trainNow_
bool trainNow_
Definition: RootTree.h:231
edm::RootTree::branchNames
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:140
edm::RootTree::insertEntryForIndex
void insertEntryForIndex(unsigned int index)
Definition: RootTree.cc:105
edm::RootTree::previous
bool previous()
Definition: RootTree.h:129
edm::RootTree::current
bool current(EntryNumber entry) const
Definition: RootTree.h:131
edm::roottree::BranchMap
Definition: RootTree.h:67
edm::roottree::BranchInfo::branchDescription_
const BranchDescription branchDescription_
Definition: RootTree.h:59
edm::roottree::defaultNonEventCacheSize
unsigned const int defaultNonEventCacheSize
Definition: RootTree.h:48
edm::RootTree::current
bool current() const
Definition: RootTree.h:130
edm::RootTree::isValid
bool isValid() const
Definition: RootTree.cc:110
edm::RootTree::learningEntries_
unsigned int learningEntries_
Definition: RootTree.h:235
edm::RootTree::stopTraining
void stopTraining()
Definition: RootTree.cc:433
edm::RootTree::setTreeMaxVirtualSize
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:189
edm::RootTree::entryNumber
EntryNumber const & entryNumber() const
Definition: RootTree.h:135
edm::RootTree::rewind
void rewind()
Definition: RootTree.h:132
edm::RootTree::entries
EntryNumber const & entries() const
Definition: RootTree.h:137
Frameworkfwd.h
T
long double T
Definition: Basic3DVectorLD.h:48
edm::RootTree::branchEntryInfoBranch
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:188
edm::RootTree::close
void close()
Definition: RootTree.cc:440
edm::roottree::BranchMap::kKeys
Definition: RootTree.h:69
edm::roottree::BranchInfo::BranchInfo
BranchInfo(BranchDescription const &prod)
Definition: RootTree.h:53
edm::RootTree::rawTriggerSwitchOverEntry_
EntryNumber rawTriggerSwitchOverEntry_
Definition: RootTree.h:233
BranchKey.h
edm::roottree::BranchMap::insert
void insert(edm::BranchID const &iKey, BranchInfo const &iInfo)
Definition: RootTree.h:75
edm::DelayedReader
Definition: DelayedReader.h:29
edm::RootTree::performedSwitchOver_
bool performedSwitchOver_
Definition: RootTree.h:234
edm::RootTree::fillBranchEntryMeta
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:149
edm::IndexIntoFile::EntryNumber_t
long long EntryNumber_t
Definition: IndexIntoFile.h:231
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
edm::RootTree::branchType
BranchType branchType() const
Definition: RootTree.h:196
edm::BranchDescription
Definition: BranchDescription.h:32
edm::roottree::BranchInfo::productBranch_
TBranch * productBranch_
Definition: RootTree.h:60
edm::RootTree
Definition: RootTree.h:103
edm::RootTree::tree
TTree * tree()
Definition: RootTree.h:183
edm::roottree::getEntry
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:496
edm::RootTree::treeCache_
std::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:219
edm::RootTree::BranchMap
roottree::BranchMap BranchMap
Definition: RootTree.h:105
edm::RootTree::setSignals
void setSignals(signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const *preEventReadSource, signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const *postEventReadSource)
Definition: RootTree.cc:489
edm::RootTree::metaTree_
TTree * metaTree_
Definition: RootTree.h:213
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
edm::signalslot::Signal
Definition: DelayedReader.h:26
edm::RootTree::nextWithCache
bool nextWithCache()
Definition: RootTree.cc:194
edm::RootTree::rootDelayedReader_
std::unique_ptr< RootDelayedReader > rootDelayedReader_
Definition: RootTree.h:242
edm::RootTree::setPresence
void setPresence(BranchDescription &prod, std::string const &oldBranchName)
Definition: RootTree.cc:130
edm::RootTree::next
bool next()
Definition: RootTree.h:127
edm::ModuleCallingContext
Definition: ModuleCallingContext.h:29