CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootTree.cc
Go to the documentation of this file.
1 #include "RootTree.h"
2 #include "RootDelayedReader.h"
8 #include "InputFile.h"
9 #include "TTree.h"
10 #include "TTreeIndex.h"
11 #include "TTreeCache.h"
12 
13 #include <iostream>
14 
15 namespace edm {
16  namespace {
17  TBranch* getAuxiliaryBranch(TTree* tree, BranchType const& branchType) {
18  TBranch* branch = tree->GetBranch(BranchTypeToAuxiliaryBranchName(branchType).c_str());
19  if (branch == 0) {
20  branch = tree->GetBranch(BranchTypeToAuxBranchName(branchType).c_str());
21  }
22  return branch;
23  }
24  TBranch* getProductProvenanceBranch(TTree* tree, BranchType const& branchType) {
25  TBranch* branch = tree->GetBranch(BranchTypeToBranchEntryInfoBranchName(branchType).c_str());
26  return branch;
27  }
28  TBranch* getStatusBranch(TTree* tree, BranchType const& branchType) { // backward compatibility
29  TBranch* branch = tree->GetBranch(BranchTypeToProductStatusBranchName(branchType).c_str()); // backward compatibility
30  return branch; // backward compatibility
31  } // backward compatibility
32  }
33  RootTree::RootTree(boost::shared_ptr<InputFile> filePtr,
34  BranchType const& branchType,
35  unsigned int maxVirtualSize,
36  unsigned int cacheSize,
37  unsigned int learningEntries) :
38  filePtr_(filePtr),
39  tree_(dynamic_cast<TTree*>(filePtr_.get() != 0 ? filePtr->Get(BranchTypeToProductTreeName(branchType).c_str()) : 0)),
40  metaTree_(dynamic_cast<TTree*>(filePtr_.get() != 0 ? filePtr->Get(BranchTypeToMetaDataTreeName(branchType).c_str()) : 0)),
41  branchType_(branchType),
42  auxBranch_(tree_ ? getAuxiliaryBranch(tree_, branchType_) : 0),
43  branchEntryInfoBranch_(metaTree_ ? getProductProvenanceBranch(metaTree_, branchType_) : getProductProvenanceBranch(tree_, branchType_)),
44  treeCache_(),
45  rawTreeCache_(),
46  entries_(tree_ ? tree_->GetEntries() : 0),
47  entryNumber_(-1),
48  branchNames_(),
49  branches_(new BranchMap),
50  trainNow_(false),
51  switchOverEntry_(-1),
52  learningEntries_(learningEntries),
53  cacheSize_(cacheSize),
54  productStatuses_(), // backward compatibility
55  pProductStatuses_(&productStatuses_), // backward compatibility
56  infoTree_(dynamic_cast<TTree*>(filePtr_.get() != 0 ? filePtr->Get(BranchTypeToInfoTreeName(branchType).c_str()) : 0)), // backward compatibility
57  statusBranch_(infoTree_ ? getStatusBranch(infoTree_, branchType_) : 0) { // backward compatibility
58  setTreeMaxVirtualSize(maxVirtualSize);
59  setCacheSize(cacheSize);
60  }
61 
63  }
64 
65  bool
67  if (metaTree_ == 0 || metaTree_->GetNbranches() == 0) {
68  return tree_ != 0 && auxBranch_ != 0;
69  }
70  if (tree_ != 0 && auxBranch_ != 0 && metaTree_ != 0) { // backward compatibility
71  if (branchEntryInfoBranch_ != 0 || statusBranch_ != 0) return true; // backward compatibility
72  return (entries_ == metaTree_->GetEntries() && tree_->GetNbranches() <= metaTree_->GetNbranches() + 1); // backward compatibility
73  } // backward compatibility
74  return false;
75  }
76 
77  void
79  assert(isValid());
80  prod.init();
81  if(tree_->GetBranch(prod.branchName().c_str()) == 0){
82  prod.setDropped();
83  }
84  }
85 
86  void
88  BranchDescription const& prod,
89  std::string const& oldBranchName) {
90  assert(isValid());
91  prod.init();
92  //use the translated branch name
93  TBranch* branch = tree_->GetBranch(oldBranchName.c_str());
95  info.productBranch_ = 0;
96  if (prod.present()) {
97  info.productBranch_ = branch;
98  //we want the new branch name for the JobReport
99  branchNames_.push_back(prod.branchName());
100  }
101  TTree* provTree = (metaTree_ != 0 ? metaTree_ : tree_);
102  info.provenanceBranch_ = provTree->GetBranch(oldBranchName.c_str());
103  branches_->insert(std::make_pair(key, info));
104  }
105 
106  void
107  RootTree::dropBranch(std::string const& oldBranchName) {
108  //use the translated branch name
109  TBranch* branch = tree_->GetBranch(oldBranchName.c_str());
110  if (branch != 0) {
111  TObjArray* leaves = tree_->GetListOfLeaves();
112  int entries = leaves->GetEntries();
113  for (int i = 0; i < entries; ++i) {
114  TLeaf* leaf = (TLeaf*)(*leaves)[i];
115  if (leaf == 0) continue;
116  TBranch* br = leaf->GetBranch();
117  if (br == 0) continue;
118  if (br->GetMother() == branch) {
119  leaves->Remove(leaf);
120  }
121  }
122  leaves->Compress();
123  tree_->GetListOfBranches()->Remove(branch);
124  tree_->GetListOfBranches()->Compress();
125  delete branch;
126  }
127  }
128 
129  roottree::BranchMap const&
130  RootTree::branches() const {return *branches_;}
131 
132  boost::shared_ptr<DelayedReader>
133  RootTree::makeDelayedReader(FileFormatVersion const& fileFormatVersion, boost::shared_ptr<RootFile> rootFilePtr) const {
134  boost::shared_ptr<DelayedReader>
135  store(new RootDelayedReader(entryNumber_, branches_, *this, fileFormatVersion, rootFilePtr));
136  return store;
137  }
138 
139  void
140  RootTree::setCacheSize(unsigned int cacheSize) {
141  cacheSize_ = cacheSize;
142  tree_->SetCacheSize(static_cast<Long64_t>(cacheSize));
143  treeCache_.reset(dynamic_cast<TTreeCache*>(filePtr_->GetCacheRead()));
144  filePtr_->SetCacheRead(0);
145  rawTreeCache_.reset();
146  }
147 
148  void
149  RootTree::setTreeMaxVirtualSize(int treeMaxVirtualSize) {
150  if (treeMaxVirtualSize >= 0) tree_->SetMaxVirtualSize(static_cast<Long64_t>(treeMaxVirtualSize));
151  }
152 
153  void
155  filePtr_->SetCacheRead(treeCache_.get());
156  entryNumber_ = theEntryNumber;
157  tree_->LoadTree(entryNumber_);
158  filePtr_->SetCacheRead(0);
159  if(treeCache_ && trainNow_ && entryNumber_ >= 0) {
160  startTraining();
161  trainNow_ = false;
162  }
163  if (treeCache_ && treeCache_->IsLearning() && switchOverEntry_ >= 0 && entryNumber_ >= switchOverEntry_) {
164  stopTraining();
165  }
166  }
167 
168  void
169  RootTree::getEntry(TBranch* branch, EntryNumber entryNumber) const {
170  if (!treeCache_) {
171  filePtr_->SetCacheRead(0);
172  roottree::getEntry(branch, entryNumber);
173  } else if (treeCache_->IsLearning() && rawTreeCache_) {
174  treeCache_->AddBranch(branch, kTRUE);
175  filePtr_->SetCacheRead(rawTreeCache_.get());
176  roottree::getEntry(branch, entryNumber);
177  filePtr_->SetCacheRead(0);
178  } else {
179  filePtr_->SetCacheRead(treeCache_.get());
180  roottree::getEntry(branch, entryNumber);
181  filePtr_->SetCacheRead(0);
182  }
183  }
184 
185  void
187  if (cacheSize_ == 0) {
188  return;
189  }
190  assert(treeCache_ && treeCache_->GetOwner() == tree_);
191  assert(branchType_ == InEvent);
192  assert(!rawTreeCache_);
193  treeCache_->SetLearnEntries(learningEntries_);
194  tree_->SetCacheSize(static_cast<Long64_t>(cacheSize_));
195  rawTreeCache_.reset(dynamic_cast<TTreeCache *>(filePtr_->GetCacheRead()));
196  filePtr_->SetCacheRead(0);
197  rawTreeCache_->SetLearnEntries(0);
199  rawTreeCache_->StartLearningPhase();
201  rawTreeCache_->AddBranch("*", kTRUE);
202  rawTreeCache_->StopLearningPhase();
203  treeCache_->StartLearningPhase();
204  treeCache_->SetEntryRange(switchOverEntry_, tree_->GetEntries());
205  treeCache_->AddBranch(poolNames::branchListIndexesBranchName().c_str(), kTRUE);
206  treeCache_->AddBranch(BranchTypeToAuxiliaryBranchName(branchType_).c_str(), kTRUE);
207  }
208 
209  void
211  filePtr_->SetCacheRead(treeCache_.get());
212  treeCache_->StopLearningPhase();
213  rawTreeCache_.reset();
214  }
215 
216  void
218  // The TFile is about to be closed, and destructed.
219  // Just to play it safe, zero all pointers to quantities that are owned by the TFile.
221  tree_ = metaTree_ = infoTree_ = 0;
222  // We own the treeCache_.
223  // We make sure the treeCache_ is detached from the file,
224  // so that ROOT does not also delete it.
225  filePtr_->SetCacheRead(0);
226  // We give up our shared ownership of the TFile itself.
227  filePtr_.reset();
228  }
229 
230  void
231  RootTree::trainCache(char const* branchNames) {
232  if (cacheSize_ == 0) {
233  return;
234  }
235  tree_->LoadTree(0);
236  assert(treeCache_);
237  filePtr_->SetCacheRead(treeCache_.get());
238  assert(treeCache_->GetOwner() == tree_);
239  treeCache_->StartLearningPhase();
240  treeCache_->SetEntryRange(0, tree_->GetEntries());
241  treeCache_->AddBranch(branchNames, kTRUE);
242  treeCache_->StopLearningPhase();
243  // We own the treeCache_.
244  // We make sure the treeCache_ is detached from the file,
245  // so that ROOT does not also delete it.
246  filePtr_->SetCacheRead(0);
247  }
248 
249  namespace roottree {
250  Int_t
251  getEntry(TBranch* branch, EntryNumber entryNumber) {
252  Int_t n = 0;
253  try {
254  n = branch->GetEntry(entryNumber);
255  }
256  catch(cms::Exception const& e) {
257  throw Exception(errors::FileReadError) << e.explainSelf() << "\n";
258  }
259  return n;
260  }
261 
262  Int_t
263  getEntry(TTree* tree, EntryNumber entryNumber) {
264  Int_t n = 0;
265  try {
266  n = tree->GetEntry(entryNumber);
267  }
268  catch(cms::Exception const& e) {
269  throw Exception(errors::FileReadError) << e.explainSelf() << "\n";
270  }
271  return n;
272  }
273 
274  std::auto_ptr<TTreeCache>
275  trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames) {
276  tree->LoadTree(0);
277  tree->SetCacheSize(cacheSize);
278  std::auto_ptr<TTreeCache> treeCache(dynamic_cast<TTreeCache*>(file.GetCacheRead()));
279  if (0 != treeCache.get()) {
280  treeCache->StartLearningPhase();
281  treeCache->SetEntryRange(0, tree->GetEntries());
282  treeCache->AddBranch(branchNames, kTRUE);
283  treeCache->StopLearningPhase();
284  }
285  // We own the treeCache_.
286  // We make sure the treeCache_ is detached from the file,
287  // so that ROOT does not also delete it.
288  file.SetCacheRead(0);
289  return treeCache;
290  }
291  }
292 }
EntryNumber entryNumber_
Definition: RootTree.h:150
TBranch * statusBranch_
Definition: RootTree.h:162
Int_t getEntry(TBranch *branch, EntryNumber entryNumber)
Definition: RootTree.cc:251
boost::shared_ptr< TTreeCache > treeCache_
Definition: RootTree.h:147
int i
Definition: DBlmapReader.cc:9
std::string const & BranchTypeToMetaDataTreeName(BranchType const &branchType)
Definition: BranchType.cc:100
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:108
unsigned int learningEntries_
Definition: RootTree.h:155
void addBranch(BranchKey const &key, BranchDescription const &prod, std::string const &oldBranchName)
Definition: RootTree.cc:87
void dropBranch(std::string const &oldBranchName)
Definition: RootTree.cc:107
std::vector< std::string > branchNames_
Definition: RootTree.h:151
std::string & branchName() const
virtual std::string explainSelf() const
Definition: Exception.cc:56
TTree * tree_
Definition: RootTree.h:139
bool trainNow_
Definition: RootTree.h:153
TFileCacheRead * GetCacheRead() const
Definition: InputFile.h:37
roottree::BranchMap BranchMap
Definition: RootTree.h:65
EntryNumber const & entries() const
Definition: RootTree.h:87
TTree * metaTree_
Definition: RootTree.h:140
void stopTraining()
Definition: RootTree.cc:210
TBranch * branchEntryInfoBranch_
Definition: RootTree.h:143
EntryNumber entries_
Definition: RootTree.h:149
boost::shared_ptr< TTreeCache > rawTreeCache_
Definition: RootTree.h:148
void trainCache(char const *branchNames)
Definition: RootTree.cc:231
void setTreeMaxVirtualSize(int treeMaxVirtualSize)
Definition: RootTree.cc:149
std::map< BranchKey const, BranchInfo > BranchMap
Definition: RootTree.h:57
BranchType
Definition: BranchType.h:11
std::auto_ptr< TTreeCache > trainCache(TTree *tree, InputFile &file, unsigned int cacheSize, char const *branchNames)
Definition: RootTree.cc:275
RootTree(boost::shared_ptr< InputFile > filePtr, BranchType const &branchType, unsigned int maxVirtualSize, unsigned int cacheSize, unsigned int learningEntries)
Definition: RootTree.cc:33
tuple leaf
Definition: Node.py:62
std::string const & BranchTypeToBranchEntryInfoBranchName(BranchType const &branchType)
Definition: BranchType.cc:120
std::string const & BranchTypeToProductTreeName(BranchType const &branchType)
Definition: BranchType.cc:96
TBranch * auxBranch_
Definition: RootTree.h:142
std::string const & BranchTypeToProductStatusBranchName(BranchType const &branchType)
Definition: BranchType.cc:116
std::string const & BranchTypeToInfoTreeName(BranchType const &branchType)
Definition: BranchType.cc:104
TBranch * provenanceBranch_
Definition: RootTree.h:53
boost::shared_ptr< DelayedReader > makeDelayedReader(FileFormatVersion const &fileFormatVersion, boost::shared_ptr< RootFile > rootFilePtr=boost::shared_ptr< RootFile >()) const
Definition: RootTree.cc:133
void close()
Definition: RootTree.cc:217
void getEntry(TBranch *branch, EntryNumber entry) const
Definition: RootTree.cc:169
void startTraining()
Definition: RootTree.cc:186
boost::shared_ptr< BranchMap > branches_
Definition: RootTree.h:152
void setPresence(BranchDescription const &prod)
Definition: RootTree.cc:78
bool isValid() const
Definition: RootTree.cc:66
TBranch * productBranch_
Definition: RootTree.h:52
void setCacheSize(unsigned int cacheSize)
Definition: RootTree.cc:140
list key
Definition: combine.py:13
unsigned int cacheSize_
Definition: RootTree.h:156
Long64_t EntryNumber
Definition: RootTree.h:43
std::string const & BranchTypeToAuxBranchName(BranchType const &branchType)
Definition: BranchType.cc:112
BranchMap const & branches() const
Definition: RootTree.cc:130
boost::shared_ptr< InputFile > filePtr_
Definition: RootTree.h:135
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:231
void SetCacheRead(TFileCacheRead *tfcr)
Definition: InputFile.h:38
EntryNumber switchOverEntry_
Definition: RootTree.h:154
T get(const Candidate &c)
Definition: component.h:56
BranchType branchType_
Definition: RootTree.h:141
TTree * infoTree_
Definition: RootTree.h:161
void setEntryNumber(EntryNumber theEntryNumber)
Definition: RootTree.cc:154
roottree::EntryNumber EntryNumber
Definition: RootTree.h:66