Go to the documentation of this file.00001
00002
00003
00004
00005
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021
00022
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
00034 #include "DataFormats/Provenance/interface/RunAux.h"
00035
00036
00037
00038
00039 namespace fwlite {
00040
00041
00042
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
00061 fileVersion_ = branchMap_->getFileVersion(iFile);
00062
00063
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
00077
00078
00079
00080
00081
00082
00083
00084
00085 }
00086 branchMap_->updateRun(0);
00087
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
00103 fileVersion_ = branchMap_->getFileVersion();
00104
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
00118
00119
00120
00121
00122
00123
00124
00125 }
00126 branchMap_->updateRun(0);
00127
00128
00129
00130
00131
00132
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
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
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
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;
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
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
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
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 }