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 #include "TFile.h"
16 #include "TTree.h"
17 #include "TBranch.h"
18 
19 // user include files
31 
32 namespace fwlite {
33  namespace internal {
34 
35  BMRStrategy::BMRStrategy(TFile* file, int fileVersion)
36  : currentFile_(file), eventTree_(0), luminosityBlockTree_(0), runTree_(0),
37  eventEntry_(-1), luminosityBlockEntry_(-1), runEntry_(-1), fileVersion_(fileVersion) {
38  // do in derived obects
39  // updateFile(file);
40  }
41 
43  }
44 
45  class Strategy : public BMRStrategy {
46  public:
47  typedef std::map<edm::BranchID, edm::BranchDescription> bidToDesc;
48 
49  Strategy(TFile* file, int fileVersion);
50  virtual ~Strategy();
51  virtual bool updateFile(TFile* file);
52  virtual bool updateEvent(Long_t eventEntry) { eventEntry_ = eventEntry; return true; }
53  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry) {
54  luminosityBlockEntry_ = luminosityBlockEntry;
55  return true;
56  }
57  virtual bool updateRun(Long_t runEntry) {
58  runEntry_ = runEntry;
59  return true;
60  }
61  virtual bool updateMap() { return true; }
64  virtual const std::vector<edm::BranchDescription>& getBranchDescriptions();
65 
66  TBranch* getBranchRegistry(edm::ProductRegistry** pReg);
67 
69  std::vector<edm::BranchDescription> bDesc_;
71  };
72 
73  Strategy::Strategy(TFile* file, int fileVersion)
74  : BMRStrategy(file, fileVersion), mapperFilled_(false) {
75  // do in derived obects
76  // updateFile(file);
77  }
78 
80  // probably need to clean up something here...
81  }
82 
83  bool Strategy::updateFile(TFile* file) {
85  eventTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
86  luminosityBlockTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::luminosityBlockTreeName().c_str()));
87  runTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::runTreeName().c_str()));
88  fileUUID_ = currentFile_->GetUUID();
89  branchDescriptionMap_.clear();
90  bDesc_.clear();
91  return 0 != eventTree_;
92  }
93 
95  TBranch* bReg(0);
96 
97  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
98  if (0 != metaDataTree) {
99  bReg = metaDataTree->GetBranch(edm::poolNames::productDescriptionBranchName().c_str());
100  bReg->SetAddress(ppReg);
101  bReg->GetEntry(0);
102  (*ppReg)->setFrozen(false);
103  }
104  return bReg;
105  }
106 
107  const std::vector<edm::BranchDescription>&
109  if (bDesc_.empty()) {
110  for (bidToDesc::const_iterator i = branchDescriptionMap_.begin(); i != branchDescriptionMap_.end(); ++i) {
111  bDesc_.push_back(i->second);
112  }
113  }
114  return bDesc_;
115  }
116 
119  throw edm::Exception(edm::errors::UnimplementedFeature) << "Unsupported EDM file version";
120  }
121 
124  edm::BranchID bid = productToBranchID(pid);
125  bidToDesc::const_iterator bdi = branchDescriptionMap_.find(bid);
126  if (branchDescriptionMap_.end() == bdi) {
127  return edm::BranchDescription();
128  }
129  return bdi->second;
130  }
131 
133  public:
134  BranchMapReaderStrategyV1(TFile* file, int fileVersion);
135  virtual bool updateFile(TFile* file);
136  virtual bool updateMap();
137  private:
138  };
139 
141  : Strategy(file, fileVersion) {
142  updateFile(file);
143  }
144 
146  if (Strategy::updateFile(file)) {
147  mapperFilled_ = false;
148  return true;
149  }
150  return false;
151  }
152 
154  if (mapperFilled_) {
155  return true;
156  }
157 
158  branchDescriptionMap_.clear();
159  bDesc_.clear();
160 
162  edm::ProductRegistry* pReg = &reg;
163  TBranch* br = getBranchRegistry(&pReg);
164 
165  if (0 != br) {
166  const edm::ProductRegistry::ProductList& prodList = reg.productList();
167 
168  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
169  if (edm::InEvent == it->second.branchType()) {
170  // call to regenerate branchName
171  it->second.init();
172  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
173  }
174  }
175  mapperFilled_ = true;
176  }
177  return 0 != br;
178  }
179 
180  // v7 has differences in product status that are not implemented in BranchMapReader yet
182  public:
183  BranchMapReaderStrategyV7(TFile* file, int fileVersion);
184  };
185 
187  : BranchMapReaderStrategyV1(file, fileVersion) {
188  updateFile(file);
189  }
190 
192  public:
193  BranchMapReaderStrategyV8(TFile* file, int fileVersion);
194  virtual bool updateFile(TFile* file);
195  virtual bool updateEvent(Long_t eventEntry);
196  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
197  virtual bool updateRun(Long_t runEntry);
198  virtual bool updateMap();
199  private:
203  };
204 
206  : Strategy(file, fileVersion),
207  eventEntryInfoVector_(), pEventEntryInfoVector_(&eventEntryInfoVector_) {
208  updateFile(file);
209  }
210 
212  // std::cout << "v8 updateevent " << newevent << std::endl;
213  if (newevent != eventEntry_) {
214  eventEntry_ = newevent;
215  mapperFilled_ = false;
216  }
217  return true;
218  }
219 
221  if (newLumi != luminosityBlockEntry_) {
222  luminosityBlockEntry_ = newLumi;
223  mapperFilled_ = false;
224  }
225  return true;
226  }
227 
229  if (newRun != runEntry_) {
230  runEntry_ = newRun;
231  mapperFilled_ = false;
232  }
233  return true;
234  }
235 
237  Strategy::updateFile(file);
238  mapperFilled_ = false;
239  entryInfoBranch_ = 0;
240  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventMetaDataTreeName().c_str()));
241  if (0 != metaDataTree) {
242  entryInfoBranch_ = metaDataTree->GetBranch(BranchTypeToBranchEntryInfoBranchName(edm::InEvent).c_str());
243 // std::cout << "entryInfoBranch for " << BranchTypeToBranchEntryInfoBranchName(edm::InEvent) << " " << entryInfoBranch_ << std::endl;
244  } else {
245  return false;
246  }
249 
250  branchDescriptionMap_.clear();
251  bDesc_.clear();
252 
254  edm::ProductRegistry* pReg = &reg;
255  TBranch *br = getBranchRegistry(&pReg);
256 
257  if (0 != br) {
258  const edm::ProductRegistry::ProductList& prodList = reg.productList();
259 
260  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
261  if (edm::InEvent == it->second.branchType()) {
262  // call to regenerate branchName
263  it->second.init();
264  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
265  }
266  }
267  }
268  return 0 != br;
269  }
270 
272  if (mapperFilled_) {
273  return true;
274  }
275 
276  assert (entryInfoBranch_);
277 
278  entryInfoBranch_->GetEntry(eventEntry_);
279 
280  for (std::vector<edm::EventEntryInfo>::const_iterator it = pEventEntryInfoVector_->begin(),
281  itEnd = pEventEntryInfoVector_->end();
282  it != itEnd; ++it) {
283 // eventInfoMap_.insert(*it);
284  }
285  mapperFilled_ = true;
286  return true;
287  }
288 
290  public:
291  BranchMapReaderStrategyV11(TFile* file, int fileVersion);
292  virtual bool updateFile(TFile* file);
293  virtual bool updateEvent(Long_t eventEntry);
294  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
295  virtual bool updateRun(Long_t runEntry);
296  virtual bool updateMap();
298  private:
299  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
303  };
304 
306  : Strategy(file, fileVersion), eventHistoryTree_(0), pHistory_(&history_) {
307  updateFile(file);
308  }
309 
311 // std::cout << "v11 updateevent " << newevent << std::endl;
312  if (newevent != eventEntry_) {
313  eventEntry_ = newevent;
314  mapperFilled_ = false;
315  }
316  return true;
317  }
318 
320  if (newlumi != luminosityBlockEntry_) {
321  luminosityBlockEntry_ = newlumi;
322  mapperFilled_ = false;
323  }
324  return true;
325  }
326 
328  if (newRun != runEntry_) {
329  runEntry_ = newRun;
330  mapperFilled_ = false;
331  }
332  return true;
333  }
334 
336  Strategy::updateFile(file);
337  mapperFilled_ = false;
338  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
339 
340  if(0 == metaDataTree) {
342  <<"No "<<edm::poolNames::metaDataTreeName()<<" TTree in file";
343  }
345  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
346  if (metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
347  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
348  b->SetAddress(&branchIDListsPtr);
349  b->GetEntry(0);
350 // std::cout << "--> " << branchIDLists_->size() << std::endl;
351  } else {
353  << "FindBranch of branchIDList failed";
354  return false;
355  }
356 
357  eventHistoryTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventHistoryTreeName().c_str()));
358 
359  branchDescriptionMap_.clear();
360  bDesc_.clear();
361 
363  edm::ProductRegistry* pReg = &reg;
364  TBranch *br = getBranchRegistry(&pReg);
365 
366  if (0 != br) {
367  const edm::ProductRegistry::ProductList& prodList = reg.productList();
368 
369  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
370  if (edm::InEvent == it->second.branchType()) {
371  // call to regenerate branchName
372  it->second.init();
373  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
374 // std::cout << "v11 updatefile " << it->second.branchID() << std::endl;
375  }
376  }
377  }
378  return 0 != br;
379  }
380 
382  if (!mapperFilled_) {
383  TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
384  if (!eventHistoryBranch) {
386  << "Failed to find history branch in event history tree";
387  return false;
388  }
389  // yes, SetAddress really does need to be called every time...
390  eventHistoryBranch->SetAddress(&pHistory_);
391  eventHistoryTree_->GetEntry(eventEntry_);
392  mapperFilled_ = true;
393  }
394  return true;
395  }
396 
399  updateMap();
401  }
402 
404  public:
405  BranchMapReaderStrategyV17(TFile* file, int fileVersion);
406  virtual bool updateFile(TFile* file);
407  virtual bool updateEvent(Long_t eventEntry);
408  virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry);
409  virtual bool updateRun(Long_t runEntry);
410  virtual bool updateMap();
412  private:
413  std::auto_ptr<edm::BranchIDLists> branchIDLists_;
414  TTree* eventsTree_;
417  };
418 
420  : Strategy(file, fileVersion), eventsTree_(0), pBranchListIndexes_(&branchListIndexes_) {
421  updateFile(file);
422  }
423 
425 // std::cout << "v11 updateevent " << newevent << std::endl;
426  if (newevent != eventEntry_) {
427  eventEntry_ = newevent;
428  mapperFilled_ = false;
429  }
430  return true;
431  }
432 
434  if (newlumi != luminosityBlockEntry_) {
435  luminosityBlockEntry_ = newlumi;
436  mapperFilled_ = false;
437  }
438  return true;
439  }
440 
442  if (newRun != runEntry_) {
443  runEntry_ = newRun;
444  mapperFilled_ = false;
445  }
446  return true;
447  }
448 
450  Strategy::updateFile(file);
451  mapperFilled_ = false;
452  TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()));
453  if(0==metaDataTree) {
455  }
457  edm::BranchIDLists* branchIDListsPtr = branchIDLists_.get();
458  if (metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
459  TBranch* b = metaDataTree->GetBranch(edm::poolNames::branchIDListBranchName().c_str());
460  b->SetAddress(&branchIDListsPtr);
461  b->GetEntry(0);
462 // std::cout << "--> " << branchIDLists_->size() << std::endl;
463  } else {
465  << "FindBranch of branchIDList failed";
466  return false;
467  }
468 
469  eventsTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
470 
471  branchDescriptionMap_.clear();
472  bDesc_.clear();
473 
475  edm::ProductRegistry* pReg = &reg;
476  TBranch *br = getBranchRegistry(&pReg);
477 
478  if (0 != br) {
479  const edm::ProductRegistry::ProductList& prodList = reg.productList();
480 
481  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
482  if (edm::InEvent == it->second.branchType()) {
483  // call to regenerate branchName
484  it->second.init();
485  branchDescriptionMap_.insert(bidToDesc::value_type(it->second.branchID(), it->second));
486 // std::cout << "v11 updatefile " << it->second.branchID() << std::endl;
487  }
488  }
489  }
490  return 0 != br;
491  }
492 
494  if (!mapperFilled_) {
495  TBranch* branchListIndexesBranch = eventsTree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
496  if (!branchListIndexesBranch) {
498  << "Failed to find history branch in event history tree";
499  return false;
500  }
501  // yes, SetAddress really does need to be called every time...
502  branchListIndexesBranch->SetAddress(&pBranchListIndexes_);
503  branchListIndexesBranch->GetEntry(eventEntry_);
504  mapperFilled_ = true;
505  }
506  return true;
507  }
508 
511  updateMap();
513  }
514  }
515 
516 //
517 // constants, enums and typedefs
518 //
519 
520 //
521 // static data member definitions
522 //
523 
524 //
525 // constructors and destructor
526 //
527 
529  fileVersion_(-1) {
530  if (0==file) {
531  throw cms::Exception("NoFile")<<"The TFile pointer is null";
532  }
533  strategy_ = newStrategy(file, getFileVersion(file));
534 }
535 
536 //
537 // member functions
538 //
539 
541  TTree* metaDataTree = dynamic_cast<TTree*>(file->Get(edm::poolNames::metaDataTreeName().c_str()));
542  if (0==metaDataTree) {
543  return 0;
544  }
545 
548  TBranch* bVer = metaDataTree->GetBranch(edm::poolNames::fileFormatVersionBranchName().c_str());
549  bVer->SetAddress(&pV);
550  bVer->GetEntry(0);
551  fileVersion_ = v.value();
552  return v.value();
553 }
554 
555 bool BranchMapReader::updateEvent(Long_t newevent) {
556  return strategy_->updateEvent(newevent);
557 }
558 
560  return strategy_->updateLuminosityBlock(newlumi);
561 }
562 
563 bool BranchMapReader::updateRun(Long_t newRun) {
564  return strategy_->updateRun(newRun);
565 }
566 
568  if (0 == strategy_.get()) {
569  strategy_ = newStrategy(file, getFileVersion(file));
570  return true;
571  }
572 
573  TFile* currentFile(strategy_->currentFile_);
574  bool isNew(file != currentFile);
575 
576  if (!isNew) {
577  //could still have a new TFile which just happens to share the same memory address as the previous file
578  //will assume that if the Event tree's address and UUID are the same as before then we do not have
579  // to treat this like a new file
580  TTree* eventTreeTemp = dynamic_cast<TTree*>(currentFile->Get(edm::poolNames::eventTreeName().c_str()));
581  isNew = eventTreeTemp != strategy_->eventTree_ || strategy_->fileUUID_ != currentFile->GetUUID();
582  }
583  if (isNew) {
584  int fileVersion = getFileVersion(file);
585  if (fileVersion != strategy_->fileVersion_) {
586  strategy_ = newStrategy(file, fileVersion);
587  } else {
588  strategy_->updateFile(file);
589  }
590  }
591  return isNew;
592 }
593 
596  return strategy_->productToBranch(pid);
597 }
598 
599 const std::vector<edm::BranchDescription>&
601  return strategy_->getBranchDescriptions();
602 }
603 
604 
605 std::auto_ptr<internal::BMRStrategy>
606 BranchMapReader::newStrategy(TFile* file, int fileVersion) {
607  std::auto_ptr<internal::BMRStrategy> s;
608 
609  if (fileVersion >= 17) {
610  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV17(file, fileVersion));
611  } else if (fileVersion >= 11) {
612  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV11(file, fileVersion));
613  } else if (fileVersion >= 8) {
614  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV8(file, fileVersion));
615  } else if (fileVersion >= 7) {
616  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV7(file, fileVersion));
617  } else {
618  s = std::auto_ptr<internal::BMRStrategy>(new internal::BranchMapReaderStrategyV1(file, fileVersion));
619  }
620  return s;
621 }
622 }
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< internal::BMRStrategy > newStrategy(TFile *file, int fileVersion)
std::auto_ptr< internal::BMRStrategy > strategy_
virtual const std::vector< edm::BranchDescription > & getBranchDescriptions()
std::auto_ptr< edm::BranchIDLists > branchIDLists_
BMRStrategy(TFile *file, int fileVersion)
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
list file
Definition: dbtoweb.py:253
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
bool updateEvent(Long_t eventEntry)
std::map< BranchKey, BranchDescription > ProductList
bool updateFile(TFile *file)
virtual bool updateEvent(Long_t eventEntry)
std::string const & fileFormatVersionBranchName()
Definition: BranchType.cc:202
const edm::BranchDescription productToBranch(const edm::ProductID &pid)
virtual bool updateEvent(Long_t eventEntry)
Container::value_type value_type
BranchMapReaderStrategyV7(TFile *file, int fileVersion)
std::vector< EventEntryInfo > EventEntryInfoVector
std::string const & luminosityBlockTreeName()
Definition: BranchType.cc:255
virtual const edm::BranchDescription productToBranch(const edm::ProductID &pid)
std::vector< edm::BranchDescription > bDesc_
bool updateLuminosityBlock(Long_t luminosityBlockEntry)
ProductList const & productList() const
virtual edm::BranchID productToBranchID(const edm::ProductID &pid)
const std::vector< edm::BranchDescription > & getBranchDescriptions()
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)
virtual edm::BranchID productToBranchID(const edm::ProductID &pid)
std::string const & eventHistoryBranchName()
Definition: BranchType.cc:222
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
TBranch * getBranchRegistry(edm::ProductRegistry **pReg)
std::string const & BranchTypeToBranchEntryInfoBranchName(BranchType const &branchType)
Definition: BranchType.cc:120
std::string const & metaDataTreeName()
Definition: BranchType.cc:157
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
std::string const & runTreeName()
Definition: BranchType.cc:258
BranchListIndexes const & branchListIndexes() const
Definition: History.h:50
virtual bool updateEvent(Long_t eventEntry)
BranchMapReaderStrategyV1(TFile *file, int fileVersion)
std::string const & eventMetaDataTreeName()
Definition: BranchType.cc:248
virtual bool updateLuminosityBlock(Long_t luminosityBlockEntry)
double b
Definition: hdecay.h:120
virtual bool updateFile(TFile *file)
std::string const & productDescriptionBranchName()
Definition: BranchType.cc:162
virtual edm::BranchID productToBranchID(const edm::ProductID &pid)
BranchMapReaderStrategyV8(TFile *file, int fileVersion)
BranchMapReaderStrategyV17(TFile *file, int fileVersion)
author Stefano ARGIRO author Bill Tanenbaum
BranchMapReaderStrategyV11(TFile *file, int fileVersion)
virtual bool updateRun(Long_t runEntry)
std::string const & eventTreeName()
Definition: BranchType.cc:244
bool updateRun(Long_t runEntry)
std::string const & branchIDListBranchName()
Definition: BranchType.cc:197
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:231
edm::EventEntryInfoVector * pEventEntryInfoVector_
std::map< edm::BranchID, edm::BranchDescription > bidToDesc
string s
Definition: asciidump.py:422
std::string const & eventHistoryTreeName()
Definition: BranchType.cc:252
mathSSE::Vec4< T > v
virtual bool updateRun(Long_t runEntry)