Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:25

0001 
0002 /*
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWLite/DataFormats
0006 // Class  :     Index
0007 //
0008 
0009    Description: <one line class summary>
0010 
0011    Usage:
0012    <usage>
0013 
0014 */
0015 //
0016 // Original Author:  Bill Tanenbaum
0017 //
0018 
0019 // user include files
0020 #include "DataFormats/FWLite/interface/EntryFinder.h"
0021 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0022 #include "FWCore/FWLite/interface/BranchMapReader.h"
0023 #include "FWCore/Utilities/interface/Exception.h"
0024 
0025 #include "TBranch.h"
0026 #include "TFile.h"
0027 #include "TTree.h"
0028 
0029 // forward declarations
0030 
0031 namespace fwlite {
0032 
0033   // This is a helper class for IndexIntoFile.
0034   class FWLiteEventFinder : public edm::IndexIntoFile::EventFinder {
0035   public:
0036     explicit FWLiteEventFinder(TBranch* auxBranch) : auxBranch_(auxBranch) {}
0037     ~FWLiteEventFinder() override {}
0038 
0039     edm::EventNumber_t getEventNumberOfEntry(edm::IndexIntoFile::EntryNumber_t entry) const override {
0040       void* saveAddress = auxBranch_->GetAddress();
0041       edm::EventAuxiliary eventAux;
0042       edm::EventAuxiliary* pEvAux = &eventAux;
0043       auxBranch_->SetAddress(&pEvAux);
0044       auxBranch_->GetEntry(entry);
0045       auxBranch_->SetAddress(saveAddress);
0046       return eventAux.event();
0047     }
0048 
0049   private:
0050     TBranch* auxBranch_;
0051   };
0052 
0053   EntryFinder::EntryFinder() : indexIntoFile_(), fileIndex_() {}
0054   EntryFinder::~EntryFinder() {}
0055 
0056   EntryFinder::EntryNumber_t EntryFinder::findEvent(edm::RunNumber_t const& run,
0057                                                     edm::LuminosityBlockNumber_t const& lumi,
0058                                                     edm::EventNumber_t const& event) const {
0059     EntryFinder::EntryNumber_t ret = invalidEntry;
0060     if (!indexIntoFile_.empty()) {
0061       edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findEventPosition(run, lumi, event);
0062       if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
0063         ret = i.entry();
0064       }
0065     } else {
0066       edm::FileIndex::const_iterator i = fileIndex_.findEventPosition(run, lumi, event);
0067       if (fileIndex_.end() != i) {
0068         ret = i->entry_;
0069       }
0070     }
0071     return ret;
0072   }
0073 
0074   EntryFinder::EntryNumber_t EntryFinder::findLumi(edm::RunNumber_t const& run,
0075                                                    edm::LuminosityBlockNumber_t const& lumi) const {
0076     EntryFinder::EntryNumber_t ret = invalidEntry;
0077     if (!indexIntoFile_.empty()) {
0078       edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findLumiPosition(run, lumi);
0079       if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
0080         ret = i.entry();
0081       }
0082     } else {
0083       edm::FileIndex::const_iterator i = fileIndex_.findLumiPosition(run, lumi);
0084       if (fileIndex_.end() != i) {
0085         ret = i->entry_;
0086       }
0087     }
0088     return ret;
0089   }
0090 
0091   EntryFinder::EntryNumber_t EntryFinder::findRun(edm::RunNumber_t const& run) const {
0092     EntryFinder::EntryNumber_t ret = invalidEntry;
0093     if (!indexIntoFile_.empty()) {
0094       edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findRunPosition(run);
0095       if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
0096         ret = i.entry();
0097       }
0098     } else {
0099       edm::FileIndex::const_iterator i = fileIndex_.findRunPosition(run);
0100       if (fileIndex_.end() != i) {
0101         ret = i->entry_;
0102       }
0103     }
0104     return ret;
0105   }
0106 
0107   void EntryFinder::fillIndex(BranchMapReader& branchMap) {
0108     if (empty()) {
0109       TTree* meta = dynamic_cast<TTree*>(branchMap.getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
0110       if (nullptr == meta) {
0111         throw cms::Exception("NoMetaTree")
0112             << "The TFile does not contain a TTree named " << edm::poolNames::metaDataTreeName();
0113       }
0114       if (meta->FindBranch(edm::poolNames::indexIntoFileBranchName().c_str()) != nullptr) {
0115         edm::IndexIntoFile* indexPtr = &indexIntoFile_;
0116         TBranch* b = meta->GetBranch(edm::poolNames::indexIntoFileBranchName().c_str());
0117         b->SetAddress(&indexPtr);
0118         b->GetEntry(0);
0119         TTree* eventTree = branchMap.getEventTree();
0120         TBranch* auxBranch = eventTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
0121         if (nullptr == auxBranch) {
0122           throw cms::Exception("NoEventAuxilliary")
0123               << "The TTree " << edm::poolNames::eventTreeName() << " does not contain a branch named 'EventAuxiliary'";
0124         }
0125 
0126         indexIntoFile_.setNumberOfEvents(auxBranch->GetEntries());
0127         indexIntoFile_.setEventFinder(
0128             std::shared_ptr<edm::IndexIntoFile::EventFinder>(std::make_shared<FWLiteEventFinder>(auxBranch)));
0129 
0130       } else if (meta->FindBranch(edm::poolNames::fileIndexBranchName().c_str()) != nullptr) {
0131         edm::FileIndex* findexPtr = &fileIndex_;
0132         TBranch* b = meta->GetBranch(edm::poolNames::fileIndexBranchName().c_str());
0133         b->SetAddress(&findexPtr);
0134         b->GetEntry(0);
0135       } else {
0136         // TBD: fill the FileIndex for old file formats (prior to CMSSW 2_0_0)
0137         throw cms::Exception("NoIndexBranch")
0138             << "The TFile does not contain a TBranch named " << edm::poolNames::indexIntoFileBranchName().c_str()
0139             << " or " << edm::poolNames::fileIndexBranchName().c_str();
0140       }
0141     }
0142     assert(!empty());
0143   }
0144 }  // namespace fwlite