CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
13 
14 #include "Rtypes.h"
15 #include "TBranch.h"
16 
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 class TBranch;
23 class TClass;
24 class TTree;
25 class TTreeCache;
26 
27 namespace edm {
28  struct BranchKey;
29  class DelayedReader;
30  class InputFile;
31  class RootTree;
32 
33  namespace roottree {
34  unsigned int const defaultCacheSize = 20U * 1024 * 1024;
35  unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
36  unsigned int const defaultLearningEntries = 20U;
37  unsigned int const defaultNonEventLearningEntries = 1U;
38  typedef Long64_t EntryNumber;
39  struct BranchInfo {
41  branchDescription_(prod),
42  productBranch_(0),
44  classCache_(0) {}
46  TBranch* productBranch_;
47  TBranch* provenanceBranch_; // For backward compatibility
48  mutable TClass* classCache_;
49  };
50  typedef std::map<BranchKey const, BranchInfo> BranchMap;
51  Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
52  Int_t getEntry(TTree* tree, EntryNumber entryNumber);
53  std::unique_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
54  }
55 
56  class RootTree {
57  public:
60  RootTree(boost::shared_ptr<InputFile> filePtr,
61  BranchType const& branchType,
62  unsigned int maxVirtualSize,
63  unsigned int cacheSize,
64  unsigned int learningEntries);
65  ~RootTree();
66 
67  RootTree(RootTree const&) = delete; // Disallow copying and moving
68  RootTree& operator=(RootTree const&) = delete; // Disallow copying and moving
69 
70  bool isValid() const;
71  void addBranch(BranchKey const& key,
72  BranchDescription const& prod,
73  std::string const& oldBranchName);
74  void dropBranch(std::string const& oldBranchName);
75  void getEntry(TBranch *branch, EntryNumber entry) const;
77  std::string const& oldBranchName);
78 
79  bool next() {return ++entryNumber_ < entries_;}
80  bool previous() {return --entryNumber_ >= 0;}
81  bool current() {return entryNumber_ < entries_ && entryNumber_ >= 0;}
82  void rewind() {entryNumber_ = 0;}
83  void close();
84  EntryNumber const& entryNumber() const {return entryNumber_;}
85  EntryNumber const& entries() const {return entries_;}
86  void setEntryNumber(EntryNumber theEntryNumber);
87  std::vector<std::string> const& branchNames() const {return branchNames_;}
89  template <typename T>
90  void fillAux(T*& pAux) {
91  auxBranch_->SetAddress(&pAux);
93  }
94  template <typename T>
95  void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
96  if (metaTree_ != 0) {
97  // Metadata was in separate tree. Not cached.
98  branch->SetAddress(&pbuf);
100  } else {
101  fillBranchEntry<T>(branch, pbuf);
102  }
103  }
104 
105  template <typename T>
106  void fillBranchEntry(TBranch* branch, T*& pbuf) {
107  branch->SetAddress(&pbuf);
108  getEntry(branch, entryNumber_);
109  }
110 
111  TTree const* tree() const {return tree_;}
112  TTree* tree() {return tree_;}
113  TTree const* metaTree() const {return metaTree_;}
114  BranchMap const& branches() const;
115 
116  //For backwards compatibility
118 
119  void trainCache(char const* branchNames);
120  void resetTraining() {trainNow_ = true;}
121 
123  private:
124  void setCacheSize(unsigned int cacheSize);
125  void setTreeMaxVirtualSize(int treeMaxVirtualSize);
126  void startTraining();
127  void stopTraining();
128 
129  boost::shared_ptr<InputFile> filePtr_;
130 // We use bare pointers for pointers to some ROOT entities.
131 // Root owns them and uses bare pointers internally.
132 // Therefore,using smart pointers here will do no good.
133  TTree* tree_;
134  TTree* metaTree_;
136  TBranch* auxBranch_;
137 // We use a smart pointer to own the TTreeCache.
138 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
139 // So, we make sure to it is detached before closing the TFile so there is no double delete.
140  boost::shared_ptr<TTreeCache> treeCache_;
141  boost::shared_ptr<TTreeCache> rawTreeCache_;
144  std::vector<std::string> branchNames_;
145  boost::shared_ptr<BranchMap> branches_;
146  bool trainNow_;
148  unsigned int learningEntries_;
149  unsigned int cacheSize_;
150  long int treeAutoFlush_;
151  std::unique_ptr<DelayedReader> rootDelayedReader_;
152 
153  TBranch* branchEntryInfoBranch_; //backwards compatibility
154  // below for backward compatibility
155  TTree* infoTree_; // backward compatibility
156  TBranch* statusBranch_; // backward compatibility
157  };
158 }
159 #endif
EntryNumber entryNumber_
Definition: RootTree.h:143
TBranch * statusBranch_
Definition: RootTree.h:156
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:273
boost::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:140
unsigned int const defaultNonEventLearningEntries
Definition: RootTree.h:37
unsigned int learningEntries_
Definition: RootTree.h:148
void addBranch(BranchKey const &key, BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:95
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:115
ConstBranchDescription branchDescription_
Definition: RootTree.h:45
std::vector< std::string > branchNames_
Definition: RootTree.h:144
std::vector< std::string > const & branchNames() const
Definition: RootTree.h:87
void setPresence(BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:86
TTree * tree_
Definition: RootTree.h:133
bool trainNow_
Definition: RootTree.h:146
roottree::BranchMap BranchMap
Definition: RootTree.h:58
EntryNumber const & entries() const
Definition: RootTree.h:85
TTree * metaTree_
Definition: RootTree.h:134
TTree * tree()
Definition: RootTree.h:112
void stopTraining()
Definition: RootTree.cc:232
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:153
EntryNumber entries_
Definition: RootTree.h:142
boost::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:141
BranchInfo(ConstBranchDescription const &prod)
Definition: RootTree.h:40
void fillBranchEntryMeta(TBranch *branch, T *&pbuf)
Definition: RootTree.h:95
void trainCache(char const *branchNames)
Definition: RootTree.cc:253
unsigned int const defaultCacheSize
Definition: RootTree.h:34
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:150
TBranch * branchEntryInfoBranch() const
Definition: RootTree.h:117
std::map< BranchKey const, BranchInfo > BranchMap
Definition: RootTree.h:50
BranchType
Definition: BranchType.h:11
bool previous()
Definition: RootTree.h:80
RootTree & operator=(RootTree const &)=delete
TTree const * metaTree() const
Definition: RootTree.h:113
TTree const * tree() const
Definition: RootTree.h:111
tuple InputFile
Open Root file and provide MEs ############.
bool next()
Definition: RootTree.h:79
RootTree(boost::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries)
Definition: RootTree.cc:27
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
BranchType branchType() const
Definition: RootTree.h:122
TBranch * auxBranch_
Definition: RootTree.h:136
long int treeAutoFlush_
Definition: RootTree.h:150
EntryNumber const & entryNumber() const
Definition: RootTree.h:84
TBranch * provenanceBranch_
Definition: RootTree.h:47
DelayedReader * rootDelayedReader() const
Definition: RootTree.cc:80
void close()
Definition: RootTree.cc:239
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:182
void fillBranchEntry(TBranch *branch, T *&pbuf)
Definition: RootTree.h:106
void startTraining()
Definition: RootTree.cc:208
boost::shared_ptr< BranchMap > branches_
Definition: RootTree.h:145
bool isValid() const
Definition: RootTree.cc:68
TBranch * productBranch_
Definition: RootTree.h:46
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:141
list key
Definition: combine.py:13
unsigned int cacheSize_
Definition: RootTree.h:149
unsigned int const defaultNonEventCacheSize
Definition: RootTree.h:35
Long64_t EntryNumber
Definition: RootTree.h:38
BranchMap const & branches() const
Definition: RootTree.cc:138
std::unique_ptr< DelayedReader > rootDelayedReader_
Definition: RootTree.h:151
boost::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:129
void resetTraining()
Definition: RootTree.h:120
void rewind()
Definition: RootTree.h:82
std::unique_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:297
long double T
unsigned int const defaultLearningEntries
Definition: RootTree.h:36
EntryNumber switchOverEntry_
Definition: RootTree.h:147
void fillAux(T *&pAux)
Definition: RootTree.h:90
BranchType branchType_
Definition: RootTree.h:135
TTree * infoTree_
Definition: RootTree.h:155
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:155
roottree::EntryNumber EntryNumber
Definition: RootTree.h:59
bool current()
Definition: RootTree.h:81