CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DataFormats/FWLite/src/LuminosityBlock.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     DataFormats/FWLite
00004 // Class  :     LuminosityBlock
00005 //
00014 //
00015 // Original Author:  Eric Vaandering
00016 //         Created:  Wed Jan  13 15:01:20 EDT 2007
00017 //
00018 
00019 // system include files
00020 #include <iostream>
00021 
00022 // user include files
00023 #include "DataFormats/FWLite/interface/LuminosityBlock.h"
00024 #include "TFile.h"
00025 #include "TTree.h"
00026 #include "FWCore/Utilities/interface/Exception.h"
00027 #include "DataFormats/Provenance/interface/BranchType.h"
00028 #include "DataFormats/Provenance/interface/ProcessHistoryID.h"
00029 
00030 #include "FWCore/Utilities/interface/EDMException.h"
00031 #include "DataFormats/FWLite/interface/LumiHistoryGetter.h"
00032 #include "DataFormats/FWLite/interface/RunFactory.h"
00033 
00034 //used for backwards compatability
00035 #include "DataFormats/Provenance/interface/LuminosityBlockAux.h"
00036 
00037 //
00038 // constants, enums and typedefs
00039 //
00040 namespace fwlite {
00041 
00042 //
00043 // constructors and destructor
00044 //
00045   LuminosityBlock::LuminosityBlock(TFile* iFile):
00046     branchMap_(new BranchMapReader(iFile)),
00047     pAux_(&aux_),
00048     pOldAux_(0),
00049     fileVersion_(-1),
00050     dataHelper_(branchMap_->getLuminosityBlockTree(),
00051                 boost::shared_ptr<HistoryGetterBase>(new LumiHistoryGetter(this)),
00052                 branchMap_)
00053   {
00054     if(0==iFile) {
00055       throw cms::Exception("NoFile")<<"The TFile pointer passed to the constructor was null";
00056     }
00057 
00058     if(0==branchMap_->getLuminosityBlockTree()) {
00059       throw cms::Exception("NoLumiTree")<<"The TFile contains no TTree named " <<edm::poolNames::luminosityBlockTreeName();
00060     }
00061     //need to know file version in order to determine how to read the basic product info
00062     fileVersion_ = branchMap_->getFileVersion(iFile);
00063 
00064     //got this logic from IOPool/Input/src/RootFile.cc
00065 
00066     TTree* luminosityBlockTree = branchMap_->getLuminosityBlockTree();
00067     if(fileVersion_ >= 3 ) {
00068       auxBranch_ = luminosityBlockTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InLumi).c_str());
00069       if(0==auxBranch_) {
00070         throw cms::Exception("NoLuminosityBlockAuxilliary")<<"The TTree "
00071         <<edm::poolNames::luminosityBlockTreeName()
00072         <<" does not contain a branch named 'LuminosityBlockAuxiliary'";
00073       }
00074       auxBranch_->SetAddress(&pAux_);
00075     } else {
00076       throw cms::Exception("OldFileVersion")<<"The FWLite Luminosity Block code des not support old file versions";
00077 //       This code commented from fwlite::Event. May be portable if needed.
00078 //       pOldAux_ = new edm::EventAux();
00079 //       auxBranch_ = luminosityBlockTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InLuminosityBlock).c_str());
00080 //       if(0==auxBranch_) {
00081 //         throw cms::Exception("NoLuminosityBlockAux")<<"The TTree "
00082 //           <<edm::poolNames::luminosityBlockTreeName()
00083 //           <<" does not contain a branch named 'LuminosityBlockAux'";
00084 //       }
00085 //       auxBranch_->SetAddress(&pOldAux_);
00086     }
00087     branchMap_->updateLuminosityBlock(0);
00088     runFactory_ =  boost::shared_ptr<RunFactory>(new RunFactory());
00089 }
00090 
00091   LuminosityBlock::LuminosityBlock(boost::shared_ptr<BranchMapReader> branchMap, boost::shared_ptr<RunFactory> runFactory):
00092     branchMap_(branchMap),
00093     pAux_(&aux_),
00094     pOldAux_(0),
00095     fileVersion_(-1),
00096     dataHelper_(branchMap_->getLuminosityBlockTree(),
00097                 boost::shared_ptr<HistoryGetterBase>(new LumiHistoryGetter(this)),
00098                 branchMap_),
00099     runFactory_(runFactory)
00100   {
00101 
00102     if(0==branchMap_->getLuminosityBlockTree()) {
00103       throw cms::Exception("NoLumiTree")<<"The TFile contains no TTree named " <<edm::poolNames::luminosityBlockTreeName();
00104     }
00105     //need to know file version in order to determine how to read the basic event info
00106     fileVersion_ = branchMap_->getFileVersion();
00107     //got this logic from IOPool/Input/src/RootFile.cc
00108 
00109     TTree* luminosityBlockTree = branchMap_->getLuminosityBlockTree();
00110     if(fileVersion_ >= 3 ) {
00111       auxBranch_ = luminosityBlockTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InLumi).c_str());
00112       if(0==auxBranch_) {
00113         throw cms::Exception("NoLuminosityBlockAuxilliary")<<"The TTree "
00114         <<edm::poolNames::luminosityBlockTreeName()
00115         <<" does not contain a branch named 'LuminosityBlockAuxiliary'";
00116       }
00117       auxBranch_->SetAddress(&pAux_);
00118     } else {
00119       throw cms::Exception("OldFileVersion")<<"The FWLite Luminosity Block code des not support old file versions";
00120 /*      pOldAux_ = new edm::EventAux();
00121       auxBranch_ = luminosityBlockTree->GetBranch(edm::BranchTypeToAuxBranchName(edm::InLuminosityBlock).c_str());
00122       if(0==auxBranch_) {
00123         throw cms::Exception("NoLuminosityBlockAux")<<"The TTree "
00124           <<edm::poolNames::luminosityBlockTreeName()
00125           <<" does not contain a branch named 'LuminosityBlockAux'";
00126       }
00127       auxBranch_->SetAddress(&pOldAux_);*/
00128     }
00129     branchMap_->updateLuminosityBlock(0);
00130 
00131 //     if(fileVersion_ >= 7 ) {
00132 //       eventHistoryTree_ = dynamic_cast<TTree*>(iFile->Get(edm::poolNames::eventHistoryTreeName().c_str()));
00133 //     }
00134 
00135 }
00136 
00137 LuminosityBlock::~LuminosityBlock()
00138 {
00139   for(std::vector<const char*>::iterator it = labels_.begin(), itEnd=labels_.end();
00140       it != itEnd;
00141       ++it) {
00142     delete [] *it;
00143   }
00144   delete pOldAux_;
00145 }
00146 
00147 //
00148 // member functions
00149 //
00150 
00151 const LuminosityBlock&
00152 LuminosityBlock::operator++()
00153 {
00154    Long_t luminosityBlockIndex = branchMap_->getLuminosityBlockEntry();
00155    if(luminosityBlockIndex < size())
00156    {
00157       branchMap_->updateLuminosityBlock(++luminosityBlockIndex);
00158    }
00159    return *this;
00160 }
00161 
00162 
00163 bool
00164 LuminosityBlock::to (edm::RunNumber_t run, edm::LuminosityBlockNumber_t luminosityBlock)
00165 {
00166    entryFinder_.fillIndex(*branchMap_);
00167    EntryFinder::EntryNumber_t entry = entryFinder_.findLumi(run, luminosityBlock);
00168    if (entry == EntryFinder::invalidEntry) {
00169       return false;
00170    }
00171    return branchMap_->updateLuminosityBlock(entry);
00172 }
00173 
00174 const LuminosityBlock&
00175 LuminosityBlock::toBegin()
00176 {
00177    branchMap_->updateLuminosityBlock(0);
00178    return *this;
00179 }
00180 
00181 //
00182 // const member functions
00183 //
00184 Long64_t
00185 LuminosityBlock::size() const
00186 {
00187   return branchMap_->getLuminosityBlockTree()->GetEntries();
00188 }
00189 
00190 bool
00191 LuminosityBlock::isValid() const
00192 {
00193   Long_t luminosityBlockIndex = branchMap_->getLuminosityBlockEntry();
00194   return luminosityBlockIndex!=-1 and luminosityBlockIndex < size();
00195 }
00196 
00197 
00198 LuminosityBlock::operator bool() const
00199 {
00200   return isValid();
00201 }
00202 
00203 bool
00204 LuminosityBlock::atEnd() const
00205 {
00206   Long_t luminosityBlockIndex = branchMap_->getLuminosityBlockEntry();
00207   return luminosityBlockIndex==-1 or luminosityBlockIndex == size();
00208 }
00209 
00210 
00211 const std::string
00212 LuminosityBlock::getBranchNameFor(const std::type_info& iInfo,
00213                   const char* iModuleLabel,
00214                   const char* iProductInstanceLabel,
00215                   const char* iProcessLabel) const
00216 {
00217     return dataHelper_.getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
00218 }
00219 
00220 
00221 bool
00222 LuminosityBlock::getByLabel(const std::type_info& iInfo,
00223                   const char* iModuleLabel,
00224                   const char* iProductInstanceLabel,
00225                   const char* iProcessLabel,
00226                   void* oData) const {
00227     if(atEnd()) {
00228         throw cms::Exception("OffEnd")<<"You have requested data past the last lumi";
00229     }
00230     Long_t lumiIndex = branchMap_->getLuminosityBlockEntry();
00231     return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, lumiIndex);
00232 }
00233 
00234 edm::LuminosityBlockAuxiliary const&
00235 LuminosityBlock::luminosityBlockAuxiliary() const
00236 {
00237    Long_t luminosityBlockIndex = branchMap_->getLuminosityBlockEntry();
00238    updateAux(luminosityBlockIndex);
00239    return aux_;
00240 }
00241 
00242 void
00243 LuminosityBlock::updateAux(Long_t luminosityBlockIndex) const
00244 {
00245   if(auxBranch_->GetEntryNumber() != luminosityBlockIndex) {
00246     auxBranch_->GetEntry(luminosityBlockIndex);
00247     //handling dealing with old version
00248     if(0 != pOldAux_) {
00249       conversion(*pOldAux_,aux_);
00250     }
00251   }
00252 }
00253 
00254 
00255 const edm::ProcessHistory&
00256 LuminosityBlock::history() const
00257 {
00258   edm::ProcessHistoryID processHistoryID;
00259 
00260   bool newFormat = false;//(fileVersion_ >= 5);
00261 
00262   Long_t lumiIndex = branchMap_->getLuminosityBlockEntry();
00263   updateAux(lumiIndex);
00264   if (!newFormat) {
00265     processHistoryID = aux_.processHistoryID();
00266   }
00267 
00268   if(historyMap_.empty() || newFormat) {
00269     procHistoryNames_.clear();
00270     TTree *meta = dynamic_cast<TTree*>(branchMap_->getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
00271     if(0==meta) {
00272       throw cms::Exception("NoMetaTree")<<"The TFile does not appear to contain a TTree named "
00273       <<edm::poolNames::metaDataTreeName();
00274     }
00275     if (historyMap_.empty()) {
00276       if (fileVersion_ < 11) {
00277         edm::ProcessHistoryMap* pPhm=&historyMap_;
00278         TBranch* b = meta->GetBranch(edm::poolNames::processHistoryMapBranchName().c_str());
00279         b->SetAddress(&pPhm);
00280         b->GetEntry(0);
00281       } else {
00282         edm::ProcessHistoryVector historyVector;
00283         edm::ProcessHistoryVector* pPhv=&historyVector;
00284         TBranch* b = meta->GetBranch(edm::poolNames::processHistoryBranchName().c_str());
00285         b->SetAddress(&pPhv);
00286         b->GetEntry(0);
00287         for (edm::ProcessHistoryVector::const_iterator i = historyVector.begin(), e = historyVector.end();
00288             i != e; ++i) {
00289           historyMap_.insert(std::make_pair(i->id(), *i));
00290         }
00291       }
00292     }
00293 //     if (newFormat) {
00294 //       if (fileVersion_ >= 7) {
00295 //         edm::History history;
00296 //         edm::History* pHistory = &history;
00297 //         TBranch* eventHistoryBranch = eventHistoryTree_->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
00298 //         if (!eventHistoryBranch)
00299 //           throw edm::Exception(edm::errors::FatalRootError)
00300 //             << "Failed to find history branch in event history tree";
00301 //         eventHistoryBranch->SetAddress(&pHistory);
00302 //         eventHistoryTree_->GetEntry(lumiIndex);
00303 //         processHistoryID = history.processHistoryID();
00304 //       } else {
00305 //         std::vector<edm::EventProcessHistoryID> *pEventProcessHistoryIDs = &eventProcessHistoryIDs_;
00306 //         TBranch* b = meta->GetBranch(edm::poolNames::eventHistoryBranchName().c_str());
00307 //         b->SetAddress(&pEventProcessHistoryIDs);
00308 //         b->GetEntry(0);
00309 //         edm::EventProcessHistoryID target(aux_.id(), edm::ProcessHistoryID());
00310 //         processHistoryID = std::lower_bound(eventProcessHistoryIDs_.begin(), eventProcessHistoryIDs_.end(), target)->processHistoryID_;
00311 //       }
00312 //     }
00313 
00314   }
00315   return historyMap_[processHistoryID];
00316 }
00317 
00318 
00319 edm::EDProduct const*
00320 LuminosityBlock::getByProductID(edm::ProductID const& iID) const {
00321   Long_t luminosityBlockIndex = branchMap_->getLuminosityBlockEntry();
00322   return dataHelper_.getByProductID(iID, luminosityBlockIndex);
00323 }
00324 
00325 
00326 //
00327 // static member functions
00328 //
00329 void
00330 LuminosityBlock::throwProductNotFoundException(const std::type_info& iType, const char* iModule, const char* iProduct, const char* iProcess)
00331 {
00332     edm::TypeID type(iType);
00333   throw edm::Exception(edm::errors::ProductNotFound)<<"A branch was found for \n  type ='"<<type.className()<<"'\n  module='"<<iModule
00334     <<"'\n  productInstance='"<<((0!=iProduct)?iProduct:"")<<"'\n  process='"<<((0!=iProcess)?iProcess:"")<<"'\n"
00335     "but no data is available for this LuminosityBlock";
00336 }
00337 
00338 
00339 namespace {
00340   struct NoDelete {
00341     void operator()(void*){}
00342   };
00343 }
00344 
00345 fwlite::Run const& LuminosityBlock::getRun() const {
00346   run_ = runFactory_->makeRun(boost::shared_ptr<BranchMapReader>(&*branchMap_,NoDelete()));
00347   edm::RunNumber_t run = luminosityBlockAuxiliary().run();
00348   run_->to(run);
00349   return *run_;
00350 }
00351 
00352 }