CMS 3D CMS Logo

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

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