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 #include <cassert>
34 
35 namespace fwlite {
36  namespace internal {
37 
39 
40  BMRStrategy::BMRStrategy(TFile* file, int fileVersion)
41  : currentFile_(file), eventTree_(0), luminosityBlockTree_(0), runTree_(0),
42  eventEntry_(-1), luminosityBlockEntry_(-1), runEntry_(-1), fileVersion_(fileVersion) {
43  // do in derived obects
44  // updateFile(file);
45  }
46 
48  }
49 
50  class Strategy : public BMRStrategy {
51  public:
52  typedef std::map<edm::BranchID, edm::BranchDescription> bidToDesc;
53 
54  Strategy(TFile* file, int fileVersion);
55  virtual ~Strategy();
56  virtual bool updateFile(TFile* file) override;
57  virtual bool updateEvent(Long_t eventEntry) override { eventEntry_ = eventEntry; return true; }
58  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override {
59  luminosityBlockEntry_ = luminosityBlockEntry;
60  return true;
61  }
62  virtual bool updateRun(Long_t runEntry) override {
63  runEntry_ = runEntry;
64  return true;
65  }
66  virtual bool updateMap() override { return true; }
67  virtual edm::BranchID productToBranchID(edm::ProductID const& pid) override;
68  virtual edm::BranchDescription const& productToBranch(edm::ProductID const& pid) override;
69  virtual std::vector<edm::BranchDescription> const& getBranchDescriptions() override;
70 
71  TBranch* getBranchRegistry(edm::ProductRegistry** pReg);
72 
74  std::vector<edm::BranchDescription> bDesc_;
76  };
77 
78  Strategy::Strategy(TFile* file, int fileVersion)
79  : BMRStrategy(file, fileVersion), mapperFilled_(false) {
80  // do in derived obects
81  // updateFile(file);
82  }
83 
85  // probably need to clean up something here...
86  }
87 
88  bool Strategy::updateFile(TFile* file) {
90  eventTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
91  luminosityBlockTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::luminosityBlockTreeName().c_str()));
92  runTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::runTreeName().c_str()));
93  fileUUID_ = currentFile_->GetUUID();
94  branchDescriptionMap_.clear();
95  bDesc_.clear();
96  return 0 != eventTree_;
97  }
98 
100  TBranch* bReg(0);
101 
102  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
103  if(0 != metaDataTree) {
104  bReg = metaDataTree->GetBranch(edm::poolNames::productDescriptionBranchName().c_str());
105  bReg->SetAddress(ppReg);
106  bReg->GetEntry(0);
107  }
108  return bReg;
109  }
110 
111  std::vector<edm::BranchDescription> const&
113  if(bDesc_.empty()) {
114  for(bidToDesc::const_iterator i = branchDescriptionMap_.begin(); i != branchDescriptionMap_.end(); ++i) {
115  bDesc_.push_back(i->second);
116  }
117  }
118  return bDesc_;
119  }
120 
123  throw edm::Exception(edm::errors::UnimplementedFeature) << "Unsupported EDM file version";
124  }
125 
126  edm::BranchDescription const &
128  edm::BranchID bid = productToBranchID(pid);
129  bidToDesc::const_iterator bdi = branchDescriptionMap_.find(bid);
130  if(branchDescriptionMap_.end() == bdi) {
132  }
133  return bdi->second;
134  }
135 
137  public:
138  BranchMapReaderStrategyV1(TFile* file, int fileVersion);
139  virtual bool updateFile(TFile* file) override;
140  virtual bool updateMap() override;
141  virtual edm::BranchListIndexes const& branchListIndexes() const override {return dummyBranchListIndexes_;}
142  private:
144  };
145 
147  : Strategy(file, fileVersion) {
148  updateFile(file);
149  }
150 
152  if(Strategy::updateFile(file)) {
153  mapperFilled_ = false;
154  return true;
155  }
156  return false;
157  }
158 
160  if(mapperFilled_) {
161  return true;
162  }
163 
164  branchDescriptionMap_.clear();
165  bDesc_.clear();
166 
168  edm::ProductRegistry* pReg = &reg;
169  TBranch* br = getBranchRegistry(&pReg);
170 
171  if(0 != br) {
173 
174  for(auto& item : prodList) {
175  edm::BranchDescription& prod = item.second;
176  if(edm::InEvent == prod.branchType()) {
177  // call to regenerate branchName
178  prod.init();
180  }
181  }
182  mapperFilled_ = true;
183  }
184  reg.setFrozen(false);
185  return 0 != br;
186  }
187 
188  // v7 has differences in product status that are not implemented in BranchMapReader yet
190  public:
191  BranchMapReaderStrategyV7(TFile* file, int fileVersion);
192  virtual edm::BranchListIndexes const& branchListIndexes() const override {return dummyBranchListIndexes_;}
193  private:
195  };
196 
198  : BranchMapReaderStrategyV1(file, fileVersion) {
199  updateFile(file);
200  }
201 
203  public:
204  BranchMapReaderStrategyV8(TFile* file, int fileVersion);
205  virtual bool updateFile(TFile* file) override;
206  virtual bool updateEvent(Long_t eventEntry) override;
207  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override;
208  virtual bool updateRun(Long_t runEntry) override;
209  virtual bool updateMap() override;
210  virtual edm::BranchListIndexes const& branchListIndexes() const override {return dummyBranchListIndexes_;}
211  private:
216  };
217 
219  : Strategy(file, fileVersion),
220  eventEntryInfoVector_(), pEventEntryInfoVector_(&eventEntryInfoVector_) {
221  updateFile(file);
222  }
223 
225  // std::cout << "v8 updateevent " << newevent << std::endl;
226  if(newevent != eventEntry_) {
227  eventEntry_ = newevent;
228  mapperFilled_ = false;
229  }
230  return true;
231  }
232 
234  if(newLumi != luminosityBlockEntry_) {
235  luminosityBlockEntry_ = newLumi;
236  mapperFilled_ = false;
237  }
238  return true;
239  }
240 
242  if(newRun != runEntry_) {
243  runEntry_ = newRun;
244  mapperFilled_ = false;
245  }
246  return true;
247  }
248 
250  Strategy::updateFile(file);
251  mapperFilled_ = false;
252  entryInfoBranch_ = 0;
253  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventMetaDataTreeName().c_str()));
254  if(0 != metaDataTree) {
255  entryInfoBranch_ = metaDataTree->GetBranch(BranchTypeToBranchEntryInfoBranchName(edm::InEvent).c_str());
256 // std::cout << "entryInfoBranch for " << BranchTypeToBranchEntryInfoBranchName(edm::InEvent) << " " << entryInfoBranch_ << std::endl;
257  } else {
258  return false;
259  }
262 
263  branchDescriptionMap_.clear();
264  bDesc_.clear();
265 
267  edm::ProductRegistry* pReg = &reg;
268  TBranch *br = getBranchRegistry(&pReg);
269 
270  if(0 != br) {
272 
273  for(auto& item : prodList) {
274  edm::BranchDescription& prod = item.second;
275  if(edm::InEvent == prod.branchType()) {
276  // call to regenerate branchName
277  prod.init();
279  }
280  }
281  }
282  reg.setFrozen(false);
283  return 0 != br;
284  }
285 
287  if(mapperFilled_) {
288  return true;
289  }
290 
291  assert(entryInfoBranch_);
292 
293  entryInfoBranch_->GetEntry(eventEntry_);
294 
295  for(std::vector<edm::EventEntryInfo>::const_iterator it = pEventEntryInfoVector_->begin(),
296  itEnd = pEventEntryInfoVector_->end();
297  it != itEnd; ++it) {
298 // eventInfoMap_.insert(*it);
299  }
300  mapperFilled_ = true;
301  return true;
302  }
303 
305  public:
306  BranchMapReaderStrategyV11(TFile* file, int fileVersion);
307  virtual bool updateFile(TFile* file) override;
308  virtual bool updateEvent(Long_t eventEntry) override;
309  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override;
310  virtual bool updateRun(Long_t runEntry) override;
311  virtual bool updateMap() override;
312  virtual edm::BranchID productToBranchID(edm::ProductID const& pid) override;
313  virtual edm::BranchListIndexes const& branchListIndexes() const override {return history_.branchListIndexes();}
314  private:
315  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
319  };
320 
322  : Strategy(file, fileVersion), eventHistoryTree_(0), pHistory_(&history_) {
323  updateFile(file);
324  }
325 
327 // std::cout << "v11 updateevent " << newevent << std::endl;
328  if(newevent != eventEntry_) {
329  eventEntry_ = newevent;
330  mapperFilled_ = false;
331  }
332  return true;
333  }
334 
336  if(newlumi != luminosityBlockEntry_) {
337  luminosityBlockEntry_ = newlumi;
338  mapperFilled_ = false;
339  }
340  return true;
341  }
342 
344  if(newRun != runEntry_) {
345  runEntry_ = newRun;
346  mapperFilled_ = false;
347  }
348  return true;
349  }
350 
352  Strategy::updateFile(file);
353  mapperFilled_ = false;
354  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
355 
356  if(0 == metaDataTree) {
358  <<"No "<<edm::poolNames::metaDataTreeName()<<" TTree in file";
359  }
361  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
362  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
363  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
364  b->SetAddress(&branchIDListsPtr);
365  b->GetEntry(0);
366 // std::cout << "--> " << branchIDLists_->size() << std::endl;
367  } else {
369  << "FindBranch of branchIDList failed";
370  return false;
371  }
372 
373  eventHistoryTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventHistoryTreeName().c_str()));
374 
375  branchDescriptionMap_.clear();
376  bDesc_.clear();
377 
379  edm::ProductRegistry* pReg = &reg;
380  TBranch *br = getBranchRegistry(&pReg);
381 
382  if(0 != br) {
384 
385  for(auto& item : prodList) {
386  edm::BranchDescription& prod = item.second;
387  if(edm::InEvent == prod.branchType()) {
388  // call to regenerate branchName
389  prod.init();
391 // std::cout << "v11 updatefile " << prod.branchID() << std::endl;
392  }
393  }
394  }
395  reg.setFrozen(false);
396  return 0 != br;
397  }
398 
400  if(!mapperFilled_) {
401  TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
402  if(!eventHistoryBranch) {
404  << "Failed to find history branch in event history tree";
405  return false;
406  }
407  // yes, SetAddress really does need to be called every time...
408  eventHistoryBranch->SetAddress(&pHistory_);
409  eventHistoryTree_->GetEntry(eventEntry_);
410  mapperFilled_ = true;
411  }
412  return true;
413  }
414 
417  updateMap();
419  }
420 
422  public:
423  BranchMapReaderStrategyV17(TFile* file, int fileVersion);
424  virtual bool updateFile(TFile* file) override;
425  virtual bool updateEvent(Long_t eventEntry) override;
426  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override;
427  virtual bool updateRun(Long_t runEntry) override;
428  virtual bool updateMap() override;
429  virtual edm::BranchID productToBranchID(edm::ProductID const& pid) override;
430  virtual edm::BranchListIndexes const& branchListIndexes() const override {return branchListIndexes_;}
431  private:
432  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
433  TTree* eventsTree_;
436  };
437 
439  : Strategy(file, fileVersion), eventsTree_(0), pBranchListIndexes_(&branchListIndexes_) {
440  updateFile(file);
441  }
442 
444 // std::cout << "v11 updateevent " << newevent << std::endl;
445  if(newevent != eventEntry_) {
446  eventEntry_ = newevent;
447  mapperFilled_ = false;
448  }
449  return true;
450  }
451 
453  if(newlumi != luminosityBlockEntry_) {
454  luminosityBlockEntry_ = newlumi;
455  mapperFilled_ = false;
456  }
457  return true;
458  }
459 
461  if(newRun != runEntry_) {
462  runEntry_ = newRun;
463  mapperFilled_ = false;
464  }
465  return true;
466  }
467 
469  Strategy::updateFile(file);
470  mapperFilled_ = false;
471  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
472  if(0==metaDataTree) {
474  }
476  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
477  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
478  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
479  b->SetAddress(&branchIDListsPtr);
480  b->GetEntry(0);
481 // std::cout << "--> " << branchIDLists_->size() << std::endl;
482  } else {
484  << "FindBranch of branchIDList failed";
485  return false;
486  }
487 
488  eventsTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
489 
490  branchDescriptionMap_.clear();
491  bDesc_.clear();
492 
494  edm::ProductRegistry* pReg = &reg;
495  TBranch *br = getBranchRegistry(&pReg);
496 
497  if(0 != br) {
499 
500  for(auto& item : prodList) {
501  edm::BranchDescription& prod = item.second;
502  if(edm::InEvent == prod.branchType()) {
503  // call to regenerate branchName
504  prod.init();
506 // std::cout << "v11 updatefile " << prod.branchID() << std::endl;
507  }
508  }
509  }
510  reg.setFrozen(false);
511  return 0 != br;
512  }
513 
515  if(!mapperFilled_) {
516  TBranch* branchListIndexesBranch = eventsTree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
517  if(!branchListIndexesBranch) {
519  << "Failed to find branch list indexes branch in event tree";
520  return false;
521  }
522  // yes, SetAddress really does need to be called every time...
523  branchListIndexesBranch->SetAddress(&pBranchListIndexes_);
524  branchListIndexesBranch->GetEntry(eventEntry_);
525  mapperFilled_ = true;
526  }
527  return true;
528  }
529 
532  updateMap();
534  }
535  }
536 
537 //
538 // constants, enums and typedefs
539 //
540 
541 //
542 // static data member definitions
543 //
544 
545 //
546 // constructors and destructor
547 //
548 
550  fileVersion_(-1) {
551  if(0==file) {
552  throw cms::Exception("NoFile")<<"The TFile pointer is null";
553  }
554  strategy_ = newStrategy(file, getFileVersion(file));
555 }
556 
557 //
558 // member functions
559 //
560 
562  TTree* metaDataTree = dynamic_cast<TTree*>(file->Get(edm::poolNames::metaDataTreeName().c_str()));
563  if(0==metaDataTree) {
564  return 0;
565  }
566 
569  TBranch* bVer = metaDataTree->GetBranch(edm::poolNames::fileFormatVersionBranchName().c_str());
570  bVer->SetAddress(&pV);
571  bVer->GetEntry(0);
572  fileVersion_ = v.value();
573  return v.value();
574 }
575 
576 bool BranchMapReader::updateEvent(Long_t newevent) {
577  return strategy_->updateEvent(newevent);
578 }
579 
581  return strategy_->updateLuminosityBlock(newlumi);
582 }
583 
584 bool BranchMapReader::updateRun(Long_t newRun) {
585  return strategy_->updateRun(newRun);
586 }
587 
589  if(0 == strategy_.get()) {
590  strategy_ = newStrategy(file, getFileVersion(file));
591  return true;
592  }
593 
594  TFile* currentFile(strategy_->currentFile_);
595  bool isNew(file != currentFile);
596 
597  if(!isNew) {
598  //could still have a new TFile which just happens to share the same memory address as the previous file
599  //will assume that if the Event tree's address and UUID are the same as before then we do not have
600  // to treat this like a new file
601  TTree* eventTreeTemp = dynamic_cast<TTree*>(currentFile->Get(edm::poolNames::eventTreeName().c_str()));
602  isNew = eventTreeTemp != strategy_->eventTree_ || strategy_->fileUUID_ != currentFile->GetUUID();
603  }
604  if(isNew) {
605  int fileVersion = getFileVersion(file);
606  if(fileVersion != strategy_->fileVersion_) {
607  strategy_ = newStrategy(file, fileVersion);
608  } else {
609  strategy_->updateFile(file);
610  }
611  }
612  return isNew;
613 }
614 
617  return strategy_->productToBranch(pid);
618 }
619 
620 std::vector<edm::BranchDescription> const&
622  return strategy_->getBranchDescriptions();
623 }
624 
625 
626 std::auto_ptr<internal::BMRStrategy>
627 BranchMapReader::newStrategy(TFile* file, int fileVersion) {
628  std::auto_ptr<internal::BMRStrategy> s;
629 
630  if(fileVersion >= 17) {
631  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV17(file, fileVersion));
632  } else if(fileVersion >= 11) {
633  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV11(file, fileVersion));
634  } else if(fileVersion >= 8) {
635  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV8(file, fileVersion));
636  } else if(fileVersion >= 7) {
637  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV7(file, fileVersion));
638  } else {
639  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV1(file, fileVersion));
640  }
641  return s;
642 }
643 }
virtual bool updateFile(TFile *file) override
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< internal::BMRStrategy > newStrategy(TFile *file, int fileVersion)
BranchType const & branchType() const
virtual bool updateFile(TFile *file) override
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
std::auto_ptr< internal::BMRStrategy > strategy_
std::auto_ptr< edm::BranchIDLists > branchIDLists_
BMRStrategy(TFile *file, int fileVersion)
bool updateEvent(Long_t eventEntry)
std::map< BranchKey, BranchDescription > ProductList
bool updateFile(TFile *file)
virtual bool updateFile(TFile *file) override
virtual bool updateRun(Long_t runEntry) override
std::string const & fileFormatVersionBranchName()
Definition: BranchType.cc:212
BranchMapReaderStrategyV7(TFile *file, int fileVersion)
std::vector< EventEntryInfo > EventEntryInfoVector
virtual edm::BranchListIndexes const & branchListIndexes() const override
virtual std::vector< edm::BranchDescription > const & getBranchDescriptions() override
std::string const & luminosityBlockTreeName()
Definition: BranchType.cc:265
std::vector< edm::BranchDescription > bDesc_
bool updateLuminosityBlock(Long_t luminosityBlockEntry)
virtual bool updateEvent(Long_t eventEntry) override
virtual bool updateFile(TFile *file) override
std::vector< BranchListIndex > BranchListIndexes
edm::EventEntryInfoVector eventEntryInfoVector_
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
virtual edm::BranchListIndexes const & branchListIndexes() const override
virtual bool updateEvent(Long_t eventEntry) override
virtual edm::BranchID productToBranchID(edm::ProductID const &pid) override
virtual bool updateRun(Long_t runEntry) override
TBranch * getBranchRegistry(edm::ProductRegistry **pReg)
const edm::BranchDescription & productToBranch(const edm::ProductID &pid)
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override
std::string const & BranchTypeToBranchEntryInfoBranchName(BranchType const &branchType)
Definition: BranchType.cc:126
BranchID const & branchID() const
std::string const & metaDataTreeName()
Definition: BranchType.cc:167
void setFrozen(bool initializeLookupInfo=true)
virtual bool updateFile(TFile *file) override
Container::value_type value_type
virtual edm::BranchListIndexes const & branchListIndexes() const override
std::string const & runTreeName()
Definition: BranchType.cc:268
BranchListIndexes const & branchListIndexes() const
Definition: History.h:50
BranchMapReaderStrategyV1(TFile *file, int fileVersion)
virtual edm::BranchListIndexes const & branchListIndexes() const override
virtual edm::BranchID productToBranchID(edm::ProductID const &pid) override
std::string const & eventMetaDataTreeName()
Definition: BranchType.cc:258
static const edm::BranchDescription kDefaultBranchDescription
virtual bool updateEvent(Long_t eventEntry) override
tuple pid
Definition: sysUtil.py:22
double b
Definition: hdecay.h:120
ProductList & productListUpdator()
std::string const & productDescriptionBranchName()
Definition: BranchType.cc:172
BranchMapReaderStrategyV8(TFile *file, int fileVersion)
BranchMapReaderStrategyV17(TFile *file, int fileVersion)
virtual bool updateEvent(Long_t eventEntry) override
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override
BranchMapReaderStrategyV11(TFile *file, int fileVersion)
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_
virtual bool updateMap() override
std::map< edm::BranchID, edm::BranchDescription > bidToDesc
volatile std::atomic< bool > shutdown_flag false
virtual edm::BranchListIndexes const & branchListIndexes() const override
std::string const & eventHistoryTreeName()
Definition: BranchType.cc:262
virtual edm::BranchID productToBranchID(edm::ProductID const &pid) override
virtual edm::BranchDescription const & productToBranch(edm::ProductID const &pid) override
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override
const std::vector< edm::BranchDescription > & getBranchDescriptions()
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) override
virtual bool updateRun(Long_t runEntry) override
virtual bool updateRun(Long_t runEntry) override