CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BranchMapReader.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : BranchMapReader
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Dan Riley
10 // Created: Tue May 20 10:31:32 EDT 2008
11 //
12 
13 // system include files
14 
15 // user include files
17 
28 
29 #include "TBranch.h"
30 #include "TFile.h"
31 #include "TTree.h"
32 
33 namespace fwlite {
34  namespace internal {
35 
37 
38  BMRStrategy::BMRStrategy(TFile* file, int fileVersion)
39  : currentFile_(file), eventTree_(0), luminosityBlockTree_(0), runTree_(0),
40  eventEntry_(-1), luminosityBlockEntry_(-1), runEntry_(-1), fileVersion_(fileVersion) {
41  // do in derived obects
42  // updateFile(file);
43  }
44 
46  }
47 
48  class Strategy : public BMRStrategy {
49  public:
50  typedef std::map<edm::BranchID, edm::BranchDescription> bidToDesc;
51 
52  Strategy(TFile* file, int fileVersion);
53  virtual ~Strategy();
54  virtual bool updateFile(TFile* file);
55  virtual bool updateEvent(Long_t eventEntry) { eventEntry_ = eventEntry; return true; }
56  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) {
57  luminosityBlockEntry_ = luminosityBlockEntry;
58  return true;
59  }
60  virtual bool updateRun(Long_t runEntry) {
61  runEntry_ = runEntry;
62  return true;
63  }
64  virtual bool updateMap() { return true; }
67  virtual std::vector<edm::BranchDescription> const& getBranchDescriptions();
68 
69  TBranch* getBranchRegistry(edm::ProductRegistry** pReg);
70 
72  std::vector<edm::BranchDescription> bDesc_;
74  };
75 
76  Strategy::Strategy(TFile* file, int fileVersion)
77  : BMRStrategy(file, fileVersion), mapperFilled_(false) {
78  // do in derived obects
79  // updateFile(file);
80  }
81 
83  // probably need to clean up something here...
84  }
85 
86  bool Strategy::updateFile(TFile* file) {
88  eventTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
89  luminosityBlockTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::luminosityBlockTreeName().c_str()));
90  runTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::runTreeName().c_str()));
91  fileUUID_ = currentFile_->GetUUID();
92  branchDescriptionMap_.clear();
93  bDesc_.clear();
94  return 0 != eventTree_;
95  }
96 
98  TBranch* bReg(0);
99 
100  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
101  if(0 != metaDataTree) {
102  bReg = metaDataTree->GetBranch(edm::poolNames::productDescriptionBranchName().c_str());
103  bReg->SetAddress(ppReg);
104  bReg->GetEntry(0);
105  (*ppReg)->setFrozen(false);
106  }
107  return bReg;
108  }
109 
110  std::vector<edm::BranchDescription> const&
112  if(bDesc_.empty()) {
113  for(bidToDesc::const_iterator i = branchDescriptionMap_.begin(); i != branchDescriptionMap_.end(); ++i) {
114  bDesc_.push_back(i->second);
115  }
116  }
117  return bDesc_;
118  }
119 
122  throw edm::Exception(edm::errors::UnimplementedFeature) << "Unsupported EDM file version";
123  }
124 
125  edm::BranchDescription const &
127  edm::BranchID bid = productToBranchID(pid);
128  bidToDesc::const_iterator bdi = branchDescriptionMap_.find(bid);
129  if(branchDescriptionMap_.end() == bdi) {
131  }
132  return bdi->second;
133  }
134 
136  public:
137  BranchMapReaderStrategyV1(TFile* file, int fileVersion);
138  virtual bool updateFile(TFile* file);
139  virtual bool updateMap();
141  private:
143  };
144 
146  : Strategy(file, fileVersion) {
147  updateFile(file);
148  }
149 
151  if(Strategy::updateFile(file)) {
152  mapperFilled_ = false;
153  return true;
154  }
155  return false;
156  }
157 
159  if(mapperFilled_) {
160  return true;
161  }
162 
163  branchDescriptionMap_.clear();
164  bDesc_.clear();
165 
167  edm::ProductRegistry* pReg = &reg;
168  TBranch* br = getBranchRegistry(&pReg);
169 
170  if(0 != br) {
171  edm::ProductRegistry::ProductList const& prodList = reg.productList();
172 
173  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
174  if(edm::InEvent == it->second.branchType()) {
175  // call to regenerate branchName
176  it->second.init();
177  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
178  }
179  }
180  mapperFilled_ = true;
181  }
182  return 0 != br;
183  }
184 
185  // v7 has differences in product status that are not implemented in BranchMapReader yet
187  public:
188  BranchMapReaderStrategyV7(TFile* file, int fileVersion);
190  private:
192  };
193 
195  : BranchMapReaderStrategyV1(file, fileVersion) {
196  updateFile(file);
197  }
198 
200  public:
201  BranchMapReaderStrategyV8(TFile* file, int fileVersion);
202  virtual bool updateFile(TFile* file);
203  virtual bool updateEvent(Long_t eventEntry);
204  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
205  virtual bool updateRun(Long_t runEntry);
206  virtual bool updateMap();
208  private:
213  };
214 
216  : Strategy(file, fileVersion),
217  eventEntryInfoVector_(), pEventEntryInfoVector_(&eventEntryInfoVector_) {
218  updateFile(file);
219  }
220 
222  // std::cout << "v8 updateevent " << newevent << std::endl;
223  if(newevent != eventEntry_) {
224  eventEntry_ = newevent;
225  mapperFilled_ = false;
226  }
227  return true;
228  }
229 
231  if(newLumi != luminosityBlockEntry_) {
232  luminosityBlockEntry_ = newLumi;
233  mapperFilled_ = false;
234  }
235  return true;
236  }
237 
239  if(newRun != runEntry_) {
240  runEntry_ = newRun;
241  mapperFilled_ = false;
242  }
243  return true;
244  }
245 
247  Strategy::updateFile(file);
248  mapperFilled_ = false;
249  entryInfoBranch_ = 0;
250  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventMetaDataTreeName().c_str()));
251  if(0 != metaDataTree) {
252  entryInfoBranch_ = metaDataTree->GetBranch(BranchTypeToBranchEntryInfoBranchName(edm::InEvent).c_str());
253 // std::cout << "entryInfoBranch for " << BranchTypeToBranchEntryInfoBranchName(edm::InEvent) << " " << entryInfoBranch_ << std::endl;
254  } else {
255  return false;
256  }
259 
260  branchDescriptionMap_.clear();
261  bDesc_.clear();
262 
264  edm::ProductRegistry* pReg = &reg;
265  TBranch *br = getBranchRegistry(&pReg);
266 
267  if(0 != br) {
268  edm::ProductRegistry::ProductList const& prodList = reg.productList();
269 
270  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
271  if(edm::InEvent == it->second.branchType()) {
272  // call to regenerate branchName
273  it->second.init();
274  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
275  }
276  }
277  }
278  return 0 != br;
279  }
280 
282  if(mapperFilled_) {
283  return true;
284  }
285 
286  assert(entryInfoBranch_);
287 
288  entryInfoBranch_->GetEntry(eventEntry_);
289 
290  for(std::vector<edm::EventEntryInfo>::const_iterator it = pEventEntryInfoVector_->begin(),
291  itEnd = pEventEntryInfoVector_->end();
292  it != itEnd; ++it) {
293 // eventInfoMap_.insert(*it);
294  }
295  mapperFilled_ = true;
296  return true;
297  }
298 
300  public:
301  BranchMapReaderStrategyV11(TFile* file, int fileVersion);
302  virtual bool updateFile(TFile* file);
303  virtual bool updateEvent(Long_t eventEntry);
304  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
305  virtual bool updateRun(Long_t runEntry);
306  virtual bool updateMap();
309  private:
310  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
314  };
315 
317  : Strategy(file, fileVersion), eventHistoryTree_(0), pHistory_(&history_) {
318  updateFile(file);
319  }
320 
322 // std::cout << "v11 updateevent " << newevent << std::endl;
323  if(newevent != eventEntry_) {
324  eventEntry_ = newevent;
325  mapperFilled_ = false;
326  }
327  return true;
328  }
329 
331  if(newlumi != luminosityBlockEntry_) {
332  luminosityBlockEntry_ = newlumi;
333  mapperFilled_ = false;
334  }
335  return true;
336  }
337 
339  if(newRun != runEntry_) {
340  runEntry_ = newRun;
341  mapperFilled_ = false;
342  }
343  return true;
344  }
345 
347  Strategy::updateFile(file);
348  mapperFilled_ = false;
349  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
350 
351  if(0 == metaDataTree) {
353  <<"No "<<edm::poolNames::metaDataTreeName()<<" TTree in file";
354  }
356  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
357  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
358  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
359  b->SetAddress(&branchIDListsPtr);
360  b->GetEntry(0);
361 // std::cout << "--> " << branchIDLists_->size() << std::endl;
362  } else {
364  << "FindBranch of branchIDList failed";
365  return false;
366  }
367 
368  eventHistoryTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventHistoryTreeName().c_str()));
369 
370  branchDescriptionMap_.clear();
371  bDesc_.clear();
372 
374  edm::ProductRegistry* pReg = &reg;
375  TBranch *br = getBranchRegistry(&pReg);
376 
377  if(0 != br) {
378  edm::ProductRegistry::ProductList const& prodList = reg.productList();
379 
380  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
381  if(edm::InEvent == it->second.branchType()) {
382  // call to regenerate branchName
383  it->second.init();
384  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
385 // std::cout << "v11 updatefile " << it->second.branchID() << std::endl;
386  }
387  }
388  }
389  return 0 != br;
390  }
391 
393  if(!mapperFilled_) {
394  TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
395  if(!eventHistoryBranch) {
397  << "Failed to find history branch in event history tree";
398  return false;
399  }
400  // yes, SetAddress really does need to be called every time...
401  eventHistoryBranch->SetAddress(&pHistory_);
402  eventHistoryTree_->GetEntry(eventEntry_);
403  mapperFilled_ = true;
404  }
405  return true;
406  }
407 
410  updateMap();
412  }
413 
415  public:
416  BranchMapReaderStrategyV17(TFile* file, int fileVersion);
417  virtual bool updateFile(TFile* file);
418  virtual bool updateEvent(Long_t eventEntry);
419  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
420  virtual bool updateRun(Long_t runEntry);
421  virtual bool updateMap();
424  private:
425  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
426  TTree* eventsTree_;
429  };
430 
432  : Strategy(file, fileVersion), eventsTree_(0), pBranchListIndexes_(&branchListIndexes_) {
433  updateFile(file);
434  }
435 
437 // std::cout << "v11 updateevent " << newevent << std::endl;
438  if(newevent != eventEntry_) {
439  eventEntry_ = newevent;
440  mapperFilled_ = false;
441  }
442  return true;
443  }
444 
446  if(newlumi != luminosityBlockEntry_) {
447  luminosityBlockEntry_ = newlumi;
448  mapperFilled_ = false;
449  }
450  return true;
451  }
452 
454  if(newRun != runEntry_) {
455  runEntry_ = newRun;
456  mapperFilled_ = false;
457  }
458  return true;
459  }
460 
462  Strategy::updateFile(file);
463  mapperFilled_ = false;
464  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
465  if(0==metaDataTree) {
467  }
469  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
470  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
471  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
472  b->SetAddress(&branchIDListsPtr);
473  b->GetEntry(0);
474 // std::cout << "--> " << branchIDLists_->size() << std::endl;
475  } else {
477  << "FindBranch of branchIDList failed";
478  return false;
479  }
480 
481  eventsTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
482 
483  branchDescriptionMap_.clear();
484  bDesc_.clear();
485 
487  edm::ProductRegistry* pReg = &reg;
488  TBranch *br = getBranchRegistry(&pReg);
489 
490  if(0 != br) {
491  edm::ProductRegistry::ProductList const& prodList = reg.productList();
492 
493  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
494  if(edm::InEvent == it->second.branchType()) {
495  // call to regenerate branchName
496  it->second.init();
497  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
498 // std::cout << "v11 updatefile " << it->second.branchID() << std::endl;
499  }
500  }
501  }
502  return 0 != br;
503  }
504 
506  if(!mapperFilled_) {
507  TBranch* branchListIndexesBranch = eventsTree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
508  if(!branchListIndexesBranch) {
510  << "Failed to find branch list indexes branch in event tree";
511  return false;
512  }
513  // yes, SetAddress really does need to be called every time...
514  branchListIndexesBranch->SetAddress(&pBranchListIndexes_);
515  branchListIndexesBranch->GetEntry(eventEntry_);
516  mapperFilled_ = true;
517  }
518  return true;
519  }
520 
523  updateMap();
525  }
526  }
527 
528 //
529 // constants, enums and typedefs
530 //
531 
532 //
533 // static data member definitions
534 //
535 
536 //
537 // constructors and destructor
538 //
539 
541  fileVersion_(-1) {
542  if(0==file) {
543  throw cms::Exception("NoFile")<<"The TFile pointer is null";
544  }
545  strategy_ = newStrategy(file, getFileVersion(file));
546 }
547 
548 //
549 // member functions
550 //
551 
553  TTree* metaDataTree = dynamic_cast<TTree*>(file->Get(edm::poolNames::metaDataTreeName().c_str()));
554  if(0==metaDataTree) {
555  return 0;
556  }
557 
560  TBranch* bVer = metaDataTree->GetBranch(edm::poolNames::fileFormatVersionBranchName().c_str());
561  bVer->SetAddress(&pV);
562  bVer->GetEntry(0);
563  fileVersion_ = v.value();
564  return v.value();
565 }
566 
567 bool BranchMapReader::updateEvent(Long_t newevent) {
568  return strategy_->updateEvent(newevent);
569 }
570 
572  return strategy_->updateLuminosityBlock(newlumi);
573 }
574 
575 bool BranchMapReader::updateRun(Long_t newRun) {
576  return strategy_->updateRun(newRun);
577 }
578 
580  if(0 == strategy_.get()) {
581  strategy_ = newStrategy(file, getFileVersion(file));
582  return true;
583  }
584 
585  TFile* currentFile(strategy_->currentFile_);
586  bool isNew(file != currentFile);
587 
588  if(!isNew) {
589  //could still have a new TFile which just happens to share the same memory address as the previous file
590  //will assume that if the Event tree's address and UUID are the same as before then we do not have
591  // to treat this like a new file
592  TTree* eventTreeTemp = dynamic_cast<TTree*>(currentFile->Get(edm::poolNames::eventTreeName().c_str()));
593  isNew = eventTreeTemp != strategy_->eventTree_ || strategy_->fileUUID_ != currentFile->GetUUID();
594  }
595  if(isNew) {
596  int fileVersion = getFileVersion(file);
597  if(fileVersion != strategy_->fileVersion_) {
598  strategy_ = newStrategy(file, fileVersion);
599  } else {
600  strategy_->updateFile(file);
601  }
602  }
603  return isNew;
604 }
605 
608  return strategy_->productToBranch(pid);
609 }
610 
611 std::vector<edm::BranchDescription> const&
613  return strategy_->getBranchDescriptions();
614 }
615 
616 
617 std::auto_ptr<internal::BMRStrategy>
618 BranchMapReader::newStrategy(TFile* file, int fileVersion) {
619  std::auto_ptr<internal::BMRStrategy> s;
620 
621  if(fileVersion >= 17) {
622  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV17(file, fileVersion));
623  } else if(fileVersion >= 11) {
624  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV11(file, fileVersion));
625  } else if(fileVersion >= 8) {
626  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV8(file, fileVersion));
627  } else if(fileVersion >= 7) {
628  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV7(file, fileVersion));
629  } else {
630  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV1(file, fileVersion));
631  }
632  return s;
633 }
634 }
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< internal::BMRStrategy > newStrategy(TFile *file, int fileVersion)
std::auto_ptr< internal::BMRStrategy > strategy_
std::auto_ptr< edm::BranchIDLists > branchIDLists_
BMRStrategy(TFile *file, int fileVersion)
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
virtual edm::BranchListIndexes const & branchListIndexes() const
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
bool updateEvent(Long_t eventEntry)
virtual std::vector< edm::BranchDescription > const & getBranchDescriptions()
std::map< BranchKey, BranchDescription > ProductList
virtual edm::BranchListIndexes const & branchListIndexes() const
bool updateFile(TFile *file)
virtual bool updateEvent(Long_t eventEntry)
virtual edm::BranchID productToBranchID(edm::ProductID const &pid)
std::string const & fileFormatVersionBranchName()
Definition: BranchType.cc:212
virtual bool updateEvent(Long_t eventEntry)
BranchMapReaderStrategyV7(TFile *file, int fileVersion)
std::vector< EventEntryInfo > EventEntryInfoVector
std::string const & luminosityBlockTreeName()
Definition: BranchType.cc:265
std::vector< edm::BranchDescription > bDesc_
bool updateLuminosityBlock(Long_t luminosityBlockEntry)
ProductList const & productList() const
std::vector< BranchListIndex > BranchListIndexes
edm::EventEntryInfoVector eventEntryInfoVector_
virtual bool updateEvent(Long_t eventEntry)
BranchID productIDToBranchID(ProductID const &pid, BranchIDLists const &lists, BranchListIndexes const &indexes)
std::auto_ptr< edm::BranchIDLists > branchIDLists_
Strategy(TFile *file, int fileVersion)
std::string const & eventHistoryBranchName()
Definition: BranchType.cc:232
tuple br
Definition: scaleCards.py:54
virtual edm::BranchID productToBranchID(edm::ProductID const &pid)
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
TBranch * getBranchRegistry(edm::ProductRegistry **pReg)
const edm::BranchDescription & productToBranch(const edm::ProductID &pid)
virtual edm::BranchListIndexes const & branchListIndexes() const
std::string const & BranchTypeToBranchEntryInfoBranchName(BranchType const &branchType)
Definition: BranchType.cc:126
std::string const & metaDataTreeName()
Definition: BranchType.cc:167
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
Container::value_type value_type
std::string const & runTreeName()
Definition: BranchType.cc:268
BranchListIndexes const & branchListIndexes() const
Definition: History.h:50
virtual bool updateEvent(Long_t eventEntry)
BranchMapReaderStrategyV1(TFile *file, int fileVersion)
virtual edm::BranchID productToBranchID(edm::ProductID const &pid)
std::string const & eventMetaDataTreeName()
Definition: BranchType.cc:258
static const edm::BranchDescription kDefaultBranchDescription
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
double b
Definition: hdecay.h:120
virtual bool updateFile(TFile *file)
std::string const & productDescriptionBranchName()
Definition: BranchType.cc:172
virtual edm::BranchDescription const & productToBranch(edm::ProductID const &pid)
BranchMapReaderStrategyV8(TFile *file, int fileVersion)
BranchMapReaderStrategyV17(TFile *file, int fileVersion)
author Stefano ARGIRO author Bill Tanenbaum
virtual edm::BranchListIndexes const & branchListIndexes() const
BranchMapReaderStrategyV11(TFile *file, int fileVersion)
virtual bool updateRun(Long_t runEntry)
virtual edm::BranchListIndexes const & branchListIndexes() const
std::string const & eventTreeName()
Definition: BranchType.cc:254
bool updateRun(Long_t runEntry)
std::string const & branchIDListBranchName()
Definition: BranchType.cc:207
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:241
edm::EventEntryInfoVector * pEventEntryInfoVector_
std::map< edm::BranchID, edm::BranchDescription > bidToDesc
std::string const & eventHistoryTreeName()
Definition: BranchType.cc:262
const std::vector< edm::BranchDescription > & getBranchDescriptions()
mathSSE::Vec4< T > v
virtual bool updateRun(Long_t runEntry)