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