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