Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:11

0001 // -*- C++ -*-
0002 //
0003 // Package:     DataFormats/FWLite
0004 // Class  :     Run
0005 //
0006 /**\class Run Run.h DataFormats/FWLite/interface/Run.h
0007 
0008    Description: <one line class summary>
0009 
0010    Usage:
0011    <usage>
0012 
0013 */
0014 //
0015 // Original Author:  Eric Vaandering
0016 //         Created:  Wed Jan  13 15:01:20 EDT 2007
0017 //
0018 
0019 // system include files
0020 #include <iostream>
0021 
0022 // user include files
0023 #include "DataFormats/FWLite/interface/Run.h"
0024 #include "TFile.h"
0025 #include "TTree.h"
0026 #include "FWCore/Utilities/interface/Exception.h"
0027 #include "DataFormats/Provenance/interface/BranchType.h"
0028 #include "DataFormats/Provenance/interface/ProcessHistoryID.h"
0029 
0030 #include "FWCore/Utilities/interface/EDMException.h"
0031 #include "DataFormats/FWLite/interface/RunHistoryGetter.h"
0032 
0033 //used for backwards compatability
0034 #include "DataFormats/Provenance/interface/RunAux.h"
0035 
0036 //
0037 // constants, enums and typedefs
0038 //
0039 namespace fwlite {
0040 
0041   //
0042   // constructors and destructor
0043   //
0044   Run::Run(TFile* iFile)
0045       : branchMap_(new BranchMapReader(iFile)),
0046         pAux_(&aux_),
0047         pOldAux_(nullptr),
0048         fileVersion_(-1),
0049         dataHelper_(branchMap_->getRunTree(), std::make_shared<RunHistoryGetter>(this), branchMap_) {
0050     if (nullptr == iFile) {
0051       throw cms::Exception("NoFile") << "The TFile pointer passed to the constructor was null";
0052     }
0053 
0054     if (nullptr == branchMap_->getRunTree()) {
0055       throw cms::Exception("NoRunTree") << "The TFile contains no TTree named " << edm::poolNames::runTreeName();
0056     }
0057     //need to know file version in order to determine how to read the basic product info
0058     fileVersion_ = branchMap_->getFileVersion(iFile);
0059 
0060     //got this logic from IOPool/Input/src/RootFile.cc
0061 
0062     TTree* runTree = branchMap_->getRunTree();
0063     if (fileVersion_ >= 3) {
0064       auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
0065       if (nullptr == auxBranch_) {
0066         throw cms::Exception("NoRunAuxilliary")
0067             << "The TTree " << edm::poolNames::runTreeName() << " does not contain a branch named 'RunAuxiliary'";
0068       }
0069       auxBranch_->SetAddress(&pAux_);
0070     } else {
0071       throw cms::Exception("OldFileVersion") << "The FWLite Run code des not support old file versions";
0072       //       This code commented from fwlite::Event. May be portable if needed.
0073       //       pOldAux_ = new edm::EventAux();
0074       //       auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InRun).c_str());
0075       //       if(nullptr == auxBranch_) {
0076       //         throw cms::Exception("NoRunAux")<<"The TTree "
0077       //           <<edm::poolNames::runTreeName()
0078       //           <<" does not contain a branch named 'RunAux'";
0079       //       }
0080       //       auxBranch_->SetAddress(&pOldAux_);
0081     }
0082     branchMap_->updateRun(0);
0083     //     getter_ = std::make_shared<ProductGetter>(this);
0084   }
0085 
0086   Run::Run(std::shared_ptr<BranchMapReader> branchMap)
0087       : branchMap_(branchMap),
0088         pAux_(&aux_),
0089         pOldAux_(nullptr),
0090         fileVersion_(-1),
0091         dataHelper_(branchMap_->getRunTree(), std::make_shared<RunHistoryGetter>(this), branchMap_) {
0092     if (nullptr == branchMap_->getRunTree()) {
0093       throw cms::Exception("NoRunTree") << "The TFile contains no TTree named " << edm::poolNames::runTreeName();
0094     }
0095     //need to know file version in order to determine how to read the basic event info
0096     fileVersion_ = branchMap_->getFileVersion();
0097     //got this logic from IOPool/Input/src/RootFile.cc
0098 
0099     TTree* runTree = branchMap_->getRunTree();
0100     if (fileVersion_ >= 3) {
0101       auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
0102       if (nullptr == auxBranch_) {
0103         throw cms::Exception("NoRunAuxilliary")
0104             << "The TTree " << edm::poolNames::runTreeName() << " does not contain a branch named 'RunAuxiliary'";
0105       }
0106       auxBranch_->SetAddress(&pAux_);
0107     } else {
0108       throw cms::Exception("OldFileVersion") << "The FWLite Run code des not support old file versions";
0109       /*      pOldAux_ = new edm::EventAux();
0110       auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InRun).c_str());
0111       if(nullptr == auxBranch_) {
0112         throw cms::Exception("NoRunAux")<<"The TTree "
0113           <<edm::poolNames::runTreeName()
0114           <<" does not contain a branch named 'RunAux'";
0115       }
0116       auxBranch_->SetAddress(&pOldAux_);*/
0117     }
0118     branchMap_->updateRun(0);
0119 
0120     //     if(fileVersion_ >= 7) {
0121     //       eventHistoryTree_ = dynamic_cast<TTree*>(iFile->Get(edm::poolNames::eventHistoryTreeName().c_str()));
0122     //     }
0123 
0124     //     getter_ = std::make_shared<ProductGetter>(this);
0125   }
0126 
0127   Run::~Run() {
0128     for (auto const& label : labels_) {
0129       delete[] label;
0130     }
0131     delete pOldAux_;
0132   }
0133 
0134   //
0135   // member functions
0136   //
0137 
0138   const Run& Run::operator++() {
0139     Long_t runIndex = branchMap_->getRunEntry();
0140     if (runIndex < size()) {
0141       branchMap_->updateRun(++runIndex);
0142     }
0143     return *this;
0144   }
0145 
0146   bool Run::to(edm::RunNumber_t run) {
0147     entryFinder_.fillIndex(*branchMap_);
0148     EntryFinder::EntryNumber_t entry = entryFinder_.findRun(run);
0149     if (entry == EntryFinder::invalidEntry) {
0150       return false;
0151     }
0152     return branchMap_->updateRun(entry);
0153   }
0154 
0155   const Run& Run::toBegin() {
0156     branchMap_->updateRun(0);
0157     return *this;
0158   }
0159 
0160   //
0161   // const member functions
0162   //
0163   Long64_t Run::size() const { return branchMap_->getRunTree()->GetEntries(); }
0164 
0165   bool Run::isValid() const {
0166     Long_t runIndex = branchMap_->getRunEntry();
0167     return runIndex != -1 and runIndex < size();
0168   }
0169 
0170   Run::operator bool() const { return isValid(); }
0171 
0172   bool Run::atEnd() const {
0173     Long_t runIndex = branchMap_->getRunEntry();
0174     return runIndex == -1 or runIndex == size();
0175   }
0176 
0177   std::string const Run::getBranchNameFor(std::type_info const& iInfo,
0178                                           char const* iModuleLabel,
0179                                           char const* iProductInstanceLabel,
0180                                           char const* iProcessLabel) const {
0181     return dataHelper_.getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
0182   }
0183 
0184   bool Run::getByLabel(std::type_info const& iInfo,
0185                        char const* iModuleLabel,
0186                        char const* iProductInstanceLabel,
0187                        char const* iProcessLabel,
0188                        void* oData) const {
0189     if (atEnd()) {
0190       throw cms::Exception("OffEnd") << "You have requested data past the last run";
0191     }
0192     Long_t runIndex = branchMap_->getRunEntry();
0193     return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, runIndex);
0194   }
0195 
0196   edm::RunAuxiliary const& Run::runAuxiliary() const {
0197     Long_t runIndex = branchMap_->getRunEntry();
0198     updateAux(runIndex);
0199     return aux_;
0200   }
0201 
0202   void Run::updateAux(Long_t runIndex) const {
0203     if (auxBranch_->GetEntryNumber() != runIndex) {
0204       auxBranch_->GetEntry(runIndex);
0205       //handling dealing with old version
0206       if (nullptr != pOldAux_) {
0207         conversion(*pOldAux_, aux_);
0208       }
0209     }
0210   }
0211 
0212   const edm::ProcessHistory& Run::history() const {
0213     edm::ProcessHistoryID processHistoryID;
0214 
0215     bool newFormat = false;  //(fileVersion_ >= 5);
0216 
0217     Long_t runIndex = branchMap_->getRunEntry();
0218     updateAux(runIndex);
0219     if (!newFormat) {
0220       processHistoryID = aux_.processHistoryID();
0221     }
0222 
0223     if (historyMap_.empty() || newFormat) {
0224       procHistoryNames_.clear();
0225       TTree* meta = dynamic_cast<TTree*>(branchMap_->getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
0226       if (nullptr == meta) {
0227         throw cms::Exception("NoMetaTree")
0228             << "The TFile does not appear to contain a TTree named " << edm::poolNames::metaDataTreeName();
0229       }
0230       if (historyMap_.empty()) {
0231         if (fileVersion_ < 11) {
0232           edm::ProcessHistoryMap* pPhm = &historyMap_;
0233           TBranch* b = meta->GetBranch(edm::poolNames::processHistoryMapBranchName().c_str());
0234           b->SetAddress(&pPhm);
0235           b->GetEntry(0);
0236         } else {
0237           edm::ProcessHistoryVector historyVector;
0238           edm::ProcessHistoryVector* pPhv = &historyVector;
0239           TBranch* b = meta->GetBranch(edm::poolNames::processHistoryBranchName().c_str());
0240           b->SetAddress(&pPhv);
0241           b->GetEntry(0);
0242           for (auto& history : historyVector) {
0243             historyMap_.insert(std::make_pair(history.setProcessHistoryID(), history));
0244           }
0245         }
0246       }
0247       //     if (newFormat) {
0248       //       if (fileVersion_ >= 7) {
0249       //         edm::History history;
0250       //         edm::History* pHistory = &history;
0251       //         TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
0252       //         if (!eventHistoryBranch)
0253       //           throw edm::Exception(edm::errors::FatalRootError)
0254       //             << "Failed to find history branch in event history tree";
0255       //         eventHistoryBranch->SetAddress(&pHistory);
0256       //         eventHistoryTree_->GetEntry(runIndex);
0257       //         processHistoryID = history.processHistoryID();
0258       //       } else {
0259       //         std::vector<edm::EventProcessHistoryID> *pEventProcessHistoryIDs = &eventProcessHistoryIDs_;
0260       //         TBranch* b = meta->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
0261       //         b->SetAddress(&pEventProcessHistoryIDs);
0262       //         b->GetEntry(0);
0263       //         edm::EventProcessHistoryID target(aux_.id(), edm::ProcessHistoryID());
0264       //         processHistoryID = std::lower_bound(eventProcessHistoryIDs_.begin(), eventProcessHistoryIDs_.end(), target)->processHistoryID_;
0265       //       }
0266       //     }
0267     }
0268     return historyMap_[processHistoryID];
0269   }
0270 
0271   edm::WrapperBase const* Run::getByProductID(edm::ProductID const& iID) const {
0272     Long_t runIndex = branchMap_->getRunEntry();
0273     return dataHelper_.getByProductID(iID, runIndex);
0274   }
0275 
0276   //
0277   // static member functions
0278   //
0279   void Run::throwProductNotFoundException(std::type_info const& iType,
0280                                           char const* iModule,
0281                                           char const* iProduct,
0282                                           char const* iProcess) {
0283     edm::TypeID type(iType);
0284     throw edm::Exception(edm::errors::ProductNotFound)
0285         << "A branch was found for \n  type ='" << type.className() << "'\n  module='" << iModule
0286         << "'\n  productInstance='" << ((nullptr != iProduct) ? iProduct : "") << "'\n  process='"
0287         << ((nullptr != iProcess) ? iProcess : "")
0288         << "'\n"
0289            "but no data is available for this Run";
0290   }
0291 }  // namespace fwlite