Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:16

0001 /*----------------------------------------------------------------------
0002 ----------------------------------------------------------------------*/
0003 #include "RunHelper.h"
0004 
0005 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0006 #include "FWCore/Utilities/interface/EDMException.h"
0007 
0008 #include <cassert>
0009 
0010 namespace edm {
0011 
0012   std::unique_ptr<RunHelperBase> makeRunHelper(ParameterSet const& pset) {
0013     if (pset.exists("setRunNumber")) {
0014       RunNumber_t run = pset.getUntrackedParameter<unsigned int>("setRunNumber");
0015       if (run != 0U) {
0016         return std::make_unique<SetRunHelper>(pset);
0017       }
0018     }
0019     if (pset.exists("setRunNumberForEachLumi")) {
0020       std::vector<RunNumber_t> runs = pset.getUntrackedParameter<std::vector<unsigned int>>("setRunNumberForEachLumi");
0021       if (!runs.empty()) {
0022         return std::make_unique<SetRunForEachLumiHelper>(pset);
0023       }
0024     }
0025     if (pset.exists("firstLuminosityBlockForEachRun")) {
0026       if (not pset.getUntrackedParameter<std::vector<LuminosityBlockID>>("firstLuminosityBlockForEachRun").empty()) {
0027         return std::make_unique<FirstLuminosityBlockForEachRunHelper>(pset);
0028       }
0029     }
0030     return std::make_unique<DefaultRunHelper>();
0031   }
0032 
0033   RunHelperBase::~RunHelperBase() {}
0034 
0035   void RunHelperBase::checkLumiConsistency(LuminosityBlockNumber_t lumi, LuminosityBlockNumber_t originalLumi) const {
0036     assert(lumi == originalLumi);
0037   }
0038 
0039   void RunHelperBase::checkRunConsistency(RunNumber_t run, RunNumber_t originalRun) const {
0040     assert(run == originalRun);
0041   }
0042 
0043   DefaultRunHelper::~DefaultRunHelper() {}
0044 
0045   SetRunHelper::SetRunHelper(ParameterSet const& pset)
0046       : RunHelperBase(),
0047         setRun_(pset.getUntrackedParameter<unsigned int>("setRunNumber")),
0048         forcedRunOffset_(0),
0049         firstTime_(true) {}
0050 
0051   SetRunHelper::~SetRunHelper() {}
0052 
0053   void SetRunHelper::setForcedRunOffset(RunNumber_t firstRun) {
0054     if (firstTime_ && setRun_ != 0) {
0055       forcedRunOffset_ = setRun_ - firstRun;
0056       if (forcedRunOffset_ < 0) {
0057         throw Exception(errors::Configuration)
0058             << "The value of the 'setRunNumber' parameter must not be\n"
0059             << "less than the first run number in the first input file.\n"
0060             << "'setRunNumber' was " << setRun_ << ", while the first run was " << firstRun << ".\n";
0061       }
0062     }
0063     firstTime_ = false;
0064   }
0065 
0066   void SetRunHelper::overrideRunNumber(RunID& id) {
0067     id = RunID(id.run() + forcedRunOffset_);
0068     if (id < RunID::firstValidRun())
0069       id = RunID::firstValidRun();
0070   }
0071 
0072   void SetRunHelper::overrideRunNumber(LuminosityBlockID& id) {
0073     id = LuminosityBlockID(id.run() + forcedRunOffset_, id.luminosityBlock());
0074     if (RunID(id.run()) < RunID::firstValidRun())
0075       id = LuminosityBlockID(RunID::firstValidRun().run(), id.luminosityBlock());
0076   }
0077 
0078   void SetRunHelper::overrideRunNumber(EventID& id, bool isRealData) {
0079     if (isRealData) {
0080       throw Exception(errors::Configuration, "SetRunHelper::overrideRunNumber()")
0081           << "The 'setRunNumber' parameter of PoolSource cannot be used with real data.\n";
0082     }
0083     id = EventID(id.run() + forcedRunOffset_, id.luminosityBlock(), id.event());
0084     if (RunID(id.run()) < RunID::firstValidRun()) {
0085       id = EventID(
0086           RunID::firstValidRun().run(), LuminosityBlockID::firstValidLuminosityBlock().luminosityBlock(), id.event());
0087     }
0088   }
0089 
0090   void SetRunHelper::checkRunConsistency(RunNumber_t run, RunNumber_t originalRun) const {
0091     assert(run == originalRun + forcedRunOffset_);
0092   }
0093 
0094   SetRunForEachLumiHelper::SetRunForEachLumiHelper(ParameterSet const& pset)
0095       : RunHelperBase(),
0096         setRunNumberForEachLumi_(pset.getUntrackedParameter<std::vector<unsigned int>>("setRunNumberForEachLumi")),
0097         indexOfNextRunNumber_(0),
0098         realRunNumber_(0),
0099         fakeNewRun_(false),
0100         firstTime_(true) {}
0101 
0102   SetRunForEachLumiHelper::~SetRunForEachLumiHelper() {}
0103 
0104   InputSource::ItemType SetRunForEachLumiHelper::nextItemType(InputSource::ItemType const& previousItemType,
0105                                                               InputSource::ItemType const& newItemType,
0106                                                               RunNumber_t,
0107                                                               LuminosityBlockNumber_t,
0108                                                               EventNumber_t) {
0109     if (newItemType == InputSource::IsRun ||
0110         (newItemType == InputSource::IsLumi && previousItemType != InputSource::IsRun)) {
0111       if (firstTime_) {
0112         firstTime_ = false;
0113       } else {
0114         ++indexOfNextRunNumber_;
0115       }
0116       if (indexOfNextRunNumber_ == setRunNumberForEachLumi_.size()) {
0117         throw Exception(errors::MismatchedInputFiles, "PoolSource::getNextItemType")
0118             << " Parameter 'setRunNumberForEachLumi' has " << setRunNumberForEachLumi_.size() << " entries\n"
0119             << "but this job is processing more luminosity blocks than this.\n";
0120       }
0121       RunNumber_t run = setRunNumberForEachLumi_[indexOfNextRunNumber_];
0122       if (run == 0) {
0123         throw Exception(errors::Configuration, "PoolSource")
0124             << "'setRunNumberForEachLumi' contains an illegal run number of '0'.\n";
0125       }
0126       bool sameRunNumber = (indexOfNextRunNumber_ != 0U && run == setRunNumberForEachLumi_[indexOfNextRunNumber_ - 1]);
0127       if (!sameRunNumber) {
0128         fakeNewRun_ = (newItemType != InputSource::IsRun);
0129         return InputSource::IsRun;
0130       }
0131     }
0132     return newItemType;
0133   }
0134 
0135   RunNumber_t SetRunForEachLumiHelper::runNumberToUseForThisLumi() const {
0136     return setRunNumberForEachLumi_.at(indexOfNextRunNumber_);
0137   }
0138 
0139   void SetRunForEachLumiHelper::checkForNewRun(RunNumber_t run, LuminosityBlockNumber_t) {
0140     if (realRunNumber_ != 0 && run != realRunNumber_) {
0141       throw Exception(errors::MismatchedInputFiles, "PoolSource::checkForNewRun")
0142           << " Parameter 'setRunNumberForEachLumi' can only process a single run.\n"
0143           << "but this job is processing more than one run.\n";
0144     }
0145     realRunNumber_ = run;
0146   }
0147 
0148   void SetRunForEachLumiHelper::overrideRunNumber(RunID& id) { id = RunID(runNumberToUseForThisLumi()); }
0149 
0150   void SetRunForEachLumiHelper::overrideRunNumber(LuminosityBlockID& id) {
0151     id = LuminosityBlockID(runNumberToUseForThisLumi(), id.luminosityBlock());
0152   }
0153 
0154   void SetRunForEachLumiHelper::overrideRunNumber(EventID& id, bool isRealData) {
0155     if (isRealData) {
0156       throw Exception(errors::Configuration, "SetRunForEachLumiHelper::overrideRunNumber()")
0157           << "The 'setRunNumberForEachLumi' parameter of PoolSource cannot be used with real data.\n";
0158     }
0159     id = EventID(runNumberToUseForThisLumi(), id.luminosityBlock(), id.event());
0160   }
0161 
0162   void SetRunForEachLumiHelper::checkRunConsistency(RunNumber_t run, RunNumber_t originalRun) const {
0163     assert(run == runNumberToUseForThisLumi());
0164   }
0165 
0166   FirstLuminosityBlockForEachRunHelper::FirstLuminosityBlockForEachRunHelper(ParameterSet const& pset)
0167       : lumiToRun_(pset.getUntrackedParameter<std::vector<edm::LuminosityBlockID>>("firstLuminosityBlockForEachRun")),
0168         realRunNumber_{0},
0169         lastUsedRunNumber_{0},
0170         fakeNewRun_{false} {}
0171 
0172   InputSource::ItemType FirstLuminosityBlockForEachRunHelper::nextItemType(InputSource::ItemType const& previousItemType,
0173                                                                            InputSource::ItemType const& newItemType,
0174                                                                            RunNumber_t,
0175                                                                            LuminosityBlockNumber_t iLumi,
0176                                                                            EventNumber_t) {
0177     if (newItemType == InputSource::IsLumi && previousItemType != InputSource::IsRun) {
0178       auto run = findRunFromLumi(iLumi);
0179       if (run == 0) {
0180         throw Exception(errors::Configuration, "PoolSource")
0181             << "'firstLuminosityBlockForEachRun' contains an illegal run number of '0'.\n";
0182       }
0183       if (lastUsedRunNumber_ != run) {
0184         fakeNewRun_ = true;
0185         lastUsedRunNumber_ = run;
0186         return InputSource::IsRun;
0187       }
0188     }
0189     return newItemType;
0190   }
0191 
0192   RunNumber_t FirstLuminosityBlockForEachRunHelper::runNumberToUseForThisLumi() const { return lastUsedRunNumber_; }
0193 
0194   void FirstLuminosityBlockForEachRunHelper::checkForNewRun(RunNumber_t run, LuminosityBlockNumber_t iLumi) {
0195     if (realRunNumber_ != 0 && run != realRunNumber_) {
0196       throw Exception(errors::MismatchedInputFiles, "PoolSource::checkForNewRun")
0197           << " Parameter 'firstLuminosityBlockForEachRun' can only process a single run.\n"
0198           << "but this job is processing more than one run.\n";
0199     }
0200     lastUsedRunNumber_ = findRunFromLumi(iLumi);
0201     realRunNumber_ = run;
0202     fakeNewRun_ = false;
0203   }
0204 
0205   void FirstLuminosityBlockForEachRunHelper::overrideRunNumber(RunID& id) { id = RunID(runNumberToUseForThisLumi()); }
0206 
0207   void FirstLuminosityBlockForEachRunHelper::overrideRunNumber(LuminosityBlockID& id) {
0208     id = LuminosityBlockID(runNumberToUseForThisLumi(), id.luminosityBlock());
0209   }
0210 
0211   void FirstLuminosityBlockForEachRunHelper::overrideRunNumber(EventID& id, bool isRealData) {
0212     if (isRealData) {
0213       throw Exception(errors::Configuration, "FirstLuminosityBlockForEachRunHelper::overrideRunNumber()")
0214           << "The 'firstLuminosityBlockForEachRun' parameter of PoolSource cannot be used with real data.\n";
0215     }
0216     id = EventID(runNumberToUseForThisLumi(), id.luminosityBlock(), id.event());
0217   }
0218 
0219   void FirstLuminosityBlockForEachRunHelper::checkRunConsistency(RunNumber_t run, RunNumber_t originalRun) const {
0220     assert(run == runNumberToUseForThisLumi());
0221   }
0222 
0223   RunNumber_t FirstLuminosityBlockForEachRunHelper::findRunFromLumi(LuminosityBlockNumber_t iLumi) const {
0224     RunNumber_t run = 0;
0225     for (auto const& lumiID : lumiToRun_) {
0226       if (lumiID.luminosityBlock() > iLumi) {
0227         break;
0228       }
0229       run = lumiID.run();
0230     }
0231     if (0 == run) {
0232       throw Exception(errors::Configuration, "FirstLuminosityBlockForEachRunHelper::findRunFromLumi()")
0233           << "The 'firstLuminosityBlockForEachRun' parameter does not have a matching Run number for LuminosityBlock "
0234              "number: "
0235           << iLumi << ".\n";
0236     }
0237     return run;
0238   }
0239 
0240   void RunHelperBase::fillDescription(ParameterSetDescription& desc) {
0241     desc.addOptionalNode(ParameterDescription<unsigned int>("setRunNumber", 0U, false) xor
0242                              ParameterDescription<std::vector<unsigned int>>(
0243                                  "setRunNumberForEachLumi", std::vector<unsigned int>(), false) xor
0244                              ParameterDescription<std::vector<LuminosityBlockID>>(
0245                                  "firstLuminosityBlockForEachRun", std::vector<LuminosityBlockID>(), false),
0246                          true)
0247         ->setComment(
0248             "If 'setRun' is non-zero, change number of first run to this number. Apply same offset to all runs."
0249             "If 'setRunNumberForEachLumi' is non-empty, use these as run numbers for each lumi respectively."
0250             "If 'firstLuminosityBlockForEachRun' is non-empty, the LuminosityBlock ID is used to determine which Run "
0251             "ID to use. The entries must be ascending values of LuminosityBlock ID."
0252             "''setRun', 'setRunNumberForEachLumi' and 'firstLuminosityBlockForEachRun' are mutually exclusive and "
0253             "allowed only for simulation.");
0254   }
0255 }  // namespace edm