CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/DataFormats/FWLite/src/EntryFinder.cc

Go to the documentation of this file.
00001 
00002 /*
00003 // -*- C++ -*-
00004 //
00005 // Package:     FWLite/DataFormats
00006 // Class  :     Index
00007 //
00008 
00009    Description: <one line class summary>
00010 
00011    Usage:
00012    <usage>
00013 
00014 */
00015 //
00016 // Original Author:  Bill Tanenbaum
00017 //
00018 
00019 // user include files
00020 #include "DataFormats/FWLite/interface/EntryFinder.h"
00021 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
00022 #include "FWCore/FWLite/interface/BranchMapReader.h"
00023 #include "FWCore/Utilities/interface/Exception.h"
00024 
00025 #include "TBranch.h"
00026 #include "TFile.h"
00027 #include "TTree.h"
00028 
00029 // forward declarations
00030 
00031 namespace fwlite {
00032 
00033   // This is a helper class for IndexIntoFile.
00034   class FWLiteEventFinder : public edm::IndexIntoFile::EventFinder {
00035   public:
00036     explicit FWLiteEventFinder(TBranch* auxBranch) : auxBranch_(auxBranch) {}
00037     virtual ~FWLiteEventFinder() {}
00038     virtual
00039     edm::EventNumber_t getEventNumberOfEntry(long long entry) const {
00040       void* saveAddress = auxBranch_->GetAddress();
00041       edm::EventAuxiliary eventAux;
00042       edm::EventAuxiliary *pEvAux = &eventAux;
00043       auxBranch_->SetAddress(&pEvAux);
00044       auxBranch_->GetEntry(entry);
00045       auxBranch_->SetAddress(saveAddress);
00046       return eventAux.event();
00047     }
00048 
00049   private:
00050      TBranch* auxBranch_;
00051   };
00052 
00053    EntryFinder::EntryFinder() : indexIntoFile_(), fileIndex_() {}
00054    EntryFinder::~EntryFinder() {}
00055 
00056    EntryFinder::EntryNumber_t
00057    EntryFinder::findEvent(edm::RunNumber_t const& run, edm::LuminosityBlockNumber_t const& lumi, edm::EventNumber_t const& event) const {
00058      EntryFinder::EntryNumber_t ret = invalidEntry;
00059      if (!indexIntoFile_.empty()) {
00060        edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findEventPosition(run, lumi, event);
00061        if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
00062          ret = i.entry();
00063        }
00064      } else {
00065        edm::FileIndex::const_iterator i = fileIndex_.findEventPosition(run, lumi, event);
00066        if (fileIndex_.end() != i) {
00067          ret = i->entry_;
00068        }
00069      }
00070      return ret;
00071    }
00072 
00073    EntryFinder::EntryNumber_t
00074    EntryFinder::findLumi(edm::RunNumber_t const& run, edm::LuminosityBlockNumber_t const& lumi) const {
00075      EntryFinder::EntryNumber_t ret = invalidEntry;
00076      if (!indexIntoFile_.empty()) {
00077        edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findLumiPosition(run, lumi);
00078        if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
00079          ret = i.entry();
00080        }
00081      } else {
00082        edm::FileIndex::const_iterator i = fileIndex_.findLumiPosition(run, lumi);
00083        if (fileIndex_.end() != i) {
00084          ret = i->entry_;
00085        }
00086      }
00087      return ret;
00088    }
00089 
00090    EntryFinder::EntryNumber_t
00091    EntryFinder::findRun(edm::RunNumber_t const& run) const {
00092      EntryFinder::EntryNumber_t ret = invalidEntry;
00093      if (!indexIntoFile_.empty()) {
00094        edm::IndexIntoFile::IndexIntoFileItr i = indexIntoFile_.findRunPosition(run);
00095        if (indexIntoFile_.end(edm::IndexIntoFile::numericalOrder) != i) {
00096          ret = i.entry();
00097        }
00098      } else {
00099        edm::FileIndex::const_iterator i = fileIndex_.findRunPosition(run);
00100        if (fileIndex_.end() != i) {
00101          ret = i->entry_;
00102        }
00103      }
00104      return ret;
00105    }
00106 
00107    void
00108    EntryFinder::fillIndex(BranchMapReader const& branchMap) {
00109     if (empty()) {
00110       TTree* meta = dynamic_cast<TTree*>(branchMap.getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
00111       if (0 == meta) {
00112         throw cms::Exception("NoMetaTree") << "The TFile does not contain a TTree named "
00113           << edm::poolNames::metaDataTreeName();
00114       }
00115       if (meta->FindBranch(edm::poolNames::indexIntoFileBranchName().c_str()) != 0) {
00116         edm::IndexIntoFile* indexPtr = &indexIntoFile_;
00117         TBranch* b = meta->GetBranch(edm::poolNames::indexIntoFileBranchName().c_str());
00118         b->SetAddress(&indexPtr);
00119         b->GetEntry(0);
00120         TTree* eventTree = branchMap.getEventTree();
00121         TBranch* auxBranch = eventTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
00122         if(0 == auxBranch) {
00123           throw cms::Exception("NoEventAuxilliary") << "The TTree "
00124           << edm::poolNames::eventTreeName()
00125           << " does not contain a branch named 'EventAuxiliary'";
00126         }
00127 
00128         indexIntoFile_.setNumberOfEvents(auxBranch->GetEntries());
00129         indexIntoFile_.setEventFinder(boost::shared_ptr<edm::IndexIntoFile::EventFinder>(new FWLiteEventFinder(auxBranch)));
00130 
00131       } else if (meta->FindBranch(edm::poolNames::fileIndexBranchName().c_str()) != 0) {
00132         edm::FileIndex* findexPtr = &fileIndex_;
00133         TBranch* b = meta->GetBranch(edm::poolNames::fileIndexBranchName().c_str());
00134         b->SetAddress(&findexPtr);
00135         b->GetEntry(0);
00136       } else {
00137         // TBD: fill the FileIndex for old file formats (prior to CMSSW 2_0_0)
00138         throw cms::Exception("NoIndexBranch") << "The TFile does not contain a TBranch named " <<
00139           edm::poolNames::indexIntoFileBranchName().c_str() << " or " << edm::poolNames::fileIndexBranchName().c_str();
00140       }
00141     }
00142     assert(!empty());
00143   }
00144 }