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/Run.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/RunHistoryGetter.h"
0032
0033
0034 #include "DataFormats/Provenance/interface/RunAux.h"
0035
0036
0037
0038
0039 namespace fwlite {
0040
0041
0042
0043
0044 Run::Run(TFile* iFile)
0045 : branchMap_(new BranchMapReader(iFile)),
0046 pAux_(&aux_),
0047 pOldAux_(nullptr),
0048 fileVersion_(-1),
0049 dataHelper_(branchMap_->getRunTree(), std::make_shared<RunHistoryGetter>(this), branchMap_) {
0050 if (nullptr == iFile) {
0051 throw cms::Exception("NoFile") << "The TFile pointer passed to the constructor was null";
0052 }
0053
0054 if (nullptr == branchMap_->getRunTree()) {
0055 throw cms::Exception("NoRunTree") << "The TFile contains no TTree named " << edm::poolNames::runTreeName();
0056 }
0057
0058 fileVersion_ = branchMap_->getFileVersion(iFile);
0059
0060
0061
0062 TTree* runTree = branchMap_->getRunTree();
0063 if (fileVersion_ >= 3) {
0064 auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
0065 if (nullptr == auxBranch_) {
0066 throw cms::Exception("NoRunAuxilliary")
0067 << "The TTree " << edm::poolNames::runTreeName() << " does not contain a branch named 'RunAuxiliary'";
0068 }
0069 auxBranch_->SetAddress(&pAux_);
0070 } else {
0071 throw cms::Exception("OldFileVersion") << "The FWLite Run code des not support old file versions";
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 }
0082 branchMap_->updateRun(0);
0083
0084 }
0085
0086 Run::Run(std::shared_ptr<BranchMapReader> branchMap)
0087 : branchMap_(branchMap),
0088 pAux_(&aux_),
0089 pOldAux_(nullptr),
0090 fileVersion_(-1),
0091 dataHelper_(branchMap_->getRunTree(), std::make_shared<RunHistoryGetter>(this), branchMap_) {
0092 if (nullptr == branchMap_->getRunTree()) {
0093 throw cms::Exception("NoRunTree") << "The TFile contains no TTree named " << edm::poolNames::runTreeName();
0094 }
0095
0096 fileVersion_ = branchMap_->getFileVersion();
0097
0098
0099 TTree* runTree = branchMap_->getRunTree();
0100 if (fileVersion_ >= 3) {
0101 auxBranch_ = runTree->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InRun).c_str());
0102 if (nullptr == auxBranch_) {
0103 throw cms::Exception("NoRunAuxilliary")
0104 << "The TTree " << edm::poolNames::runTreeName() << " does not contain a branch named 'RunAuxiliary'";
0105 }
0106 auxBranch_->SetAddress(&pAux_);
0107 } else {
0108 throw cms::Exception("OldFileVersion") << "The FWLite Run code des not support old file versions";
0109
0110
0111
0112
0113
0114
0115
0116
0117 }
0118 branchMap_->updateRun(0);
0119
0120
0121
0122
0123
0124
0125 }
0126
0127 Run::~Run() {
0128 for (auto const& label : labels_) {
0129 delete[] label;
0130 }
0131 delete pOldAux_;
0132 }
0133
0134
0135
0136
0137
0138 const Run& Run::operator++() {
0139 Long_t runIndex = branchMap_->getRunEntry();
0140 if (runIndex < size()) {
0141 branchMap_->updateRun(++runIndex);
0142 }
0143 return *this;
0144 }
0145
0146 bool Run::to(edm::RunNumber_t run) {
0147 entryFinder_.fillIndex(*branchMap_);
0148 EntryFinder::EntryNumber_t entry = entryFinder_.findRun(run);
0149 if (entry == EntryFinder::invalidEntry) {
0150 return false;
0151 }
0152 return branchMap_->updateRun(entry);
0153 }
0154
0155 const Run& Run::toBegin() {
0156 branchMap_->updateRun(0);
0157 return *this;
0158 }
0159
0160
0161
0162
0163 Long64_t Run::size() const { return branchMap_->getRunTree()->GetEntries(); }
0164
0165 bool Run::isValid() const {
0166 Long_t runIndex = branchMap_->getRunEntry();
0167 return runIndex != -1 and runIndex < size();
0168 }
0169
0170 Run::operator bool() const { return isValid(); }
0171
0172 bool Run::atEnd() const {
0173 Long_t runIndex = branchMap_->getRunEntry();
0174 return runIndex == -1 or runIndex == size();
0175 }
0176
0177 std::string const Run::getBranchNameFor(std::type_info const& iInfo,
0178 char const* iModuleLabel,
0179 char const* iProductInstanceLabel,
0180 char const* iProcessLabel) const {
0181 return dataHelper_.getBranchNameFor(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel);
0182 }
0183
0184 bool Run::getByLabel(std::type_info const& iInfo,
0185 char const* iModuleLabel,
0186 char const* iProductInstanceLabel,
0187 char const* iProcessLabel,
0188 void* oData) const {
0189 if (atEnd()) {
0190 throw cms::Exception("OffEnd") << "You have requested data past the last run";
0191 }
0192 Long_t runIndex = branchMap_->getRunEntry();
0193 return dataHelper_.getByLabel(iInfo, iModuleLabel, iProductInstanceLabel, iProcessLabel, oData, runIndex);
0194 }
0195
0196 edm::RunAuxiliary const& Run::runAuxiliary() const {
0197 Long_t runIndex = branchMap_->getRunEntry();
0198 updateAux(runIndex);
0199 return aux_;
0200 }
0201
0202 void Run::updateAux(Long_t runIndex) const {
0203 if (auxBranch_->GetEntryNumber() != runIndex) {
0204 auxBranch_->GetEntry(runIndex);
0205
0206 if (nullptr != pOldAux_) {
0207 conversion(*pOldAux_, aux_);
0208 }
0209 }
0210 }
0211
0212 const edm::ProcessHistory& Run::history() const {
0213 edm::ProcessHistoryID processHistoryID;
0214
0215 bool newFormat = false;
0216
0217 Long_t runIndex = branchMap_->getRunEntry();
0218 updateAux(runIndex);
0219 if (!newFormat) {
0220 processHistoryID = aux_.processHistoryID();
0221 }
0222
0223 if (historyMap_.empty() || newFormat) {
0224 procHistoryNames_.clear();
0225 TTree* meta = dynamic_cast<TTree*>(branchMap_->getFile()->Get(edm::poolNames::metaDataTreeName().c_str()));
0226 if (nullptr == meta) {
0227 throw cms::Exception("NoMetaTree")
0228 << "The TFile does not appear to contain a TTree named " << edm::poolNames::metaDataTreeName();
0229 }
0230 if (historyMap_.empty()) {
0231 if (fileVersion_ < 11) {
0232 edm::ProcessHistoryMap* pPhm = &historyMap_;
0233 TBranch* b = meta->GetBranch(edm::poolNames::processHistoryMapBranchName().c_str());
0234 b->SetAddress(&pPhm);
0235 b->GetEntry(0);
0236 } else {
0237 edm::ProcessHistoryVector historyVector;
0238 edm::ProcessHistoryVector* pPhv = &historyVector;
0239 TBranch* b = meta->GetBranch(edm::poolNames::processHistoryBranchName().c_str());
0240 b->SetAddress(&pPhv);
0241 b->GetEntry(0);
0242 for (auto& history : historyVector) {
0243 historyMap_.insert(std::make_pair(history.setProcessHistoryID(), history));
0244 }
0245 }
0246 }
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267 }
0268 return historyMap_[processHistoryID];
0269 }
0270
0271 edm::WrapperBase const* Run::getByProductID(edm::ProductID const& iID) const {
0272 Long_t runIndex = branchMap_->getRunEntry();
0273 return dataHelper_.getByProductID(iID, runIndex);
0274 }
0275
0276
0277
0278
0279 void Run::throwProductNotFoundException(std::type_info const& iType,
0280 char const* iModule,
0281 char const* iProduct,
0282 char const* iProcess) {
0283 edm::TypeID type(iType);
0284 throw edm::Exception(edm::errors::ProductNotFound)
0285 << "A branch was found for \n type ='" << type.className() << "'\n module='" << iModule
0286 << "'\n productInstance='" << ((nullptr != iProduct) ? iProduct : "") << "'\n process='"
0287 << ((nullptr != iProcess) ? iProcess : "")
0288 << "'\n"
0289 "but no data is available for this Run";
0290 }
0291 }