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/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
00035 #include "DataFormats/Provenance/interface/LuminosityBlockAux.h"
00036
00037
00038
00039
00040 namespace fwlite {
00041
00042
00043
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
00062 fileVersion_ = branchMap_->getFileVersion(iFile);
00063
00064
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
00078
00079
00080
00081
00082
00083
00084
00085
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
00106 fileVersion_ = branchMap_->getFileVersion();
00107
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
00121
00122
00123
00124
00125
00126
00127
00128 }
00129 branchMap_->updateLuminosityBlock(0);
00130
00131
00132
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
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
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
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;
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
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
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
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 }