File indexing completed on 2024-04-06 12:04:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <iostream>
0021
0022
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
0035 #include "DataFormats/Provenance/interface/LuminosityBlockAux.h"
0036
0037
0038
0039
0040 namespace fwlite {
0041
0042
0043
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
0060 fileVersion_ = branchMap_->getFileVersion(iFile);
0061
0062
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
0076
0077
0078
0079
0080
0081
0082
0083
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
0101 fileVersion_ = branchMap_->getFileVersion();
0102
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
0116
0117
0118
0119
0120
0121
0122
0123 }
0124 branchMap_->updateLuminosityBlock(0);
0125
0126
0127
0128
0129 }
0130
0131 LuminosityBlock::~LuminosityBlock() {
0132 for (auto const& label : labels_) {
0133 delete[] label;
0134 }
0135 delete pOldAux_;
0136 }
0137
0138
0139
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
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
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;
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
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
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
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 }
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 }