Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:16

0001 /*----------------------------------------------------------------------
0002 ----------------------------------------------------------------------*/
0003 
0004 #include <cerrno>
0005 
0006 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0007 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0008 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0009 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0012 #include "FWCore/Framework/interface/EventPrincipal.h"
0013 #include "FWCore/Framework/interface/LuminosityBlock.h"
0014 #include "FWCore/Framework/interface/Run.h"
0015 #include "FWCore/Framework/interface/Event.h"
0016 #include "FWCore/Framework/interface/ExceptionHelpers.h"
0017 #include "FWCore/Sources/interface/IDGeneratorSourceBase.h"
0018 
0019 #include "FWCore/Sources/interface/PuttableSourceBase.h"
0020 
0021 namespace {
0022   void checkFirstLumiForRuns(std::vector<edm::LuminosityBlockID> const& iFirstLumis) {
0023     if (iFirstLumis.empty())
0024       return;
0025 
0026     auto previous = iFirstLumis[0].luminosityBlock();
0027     for (auto it = iFirstLumis.begin() + 1; it != iFirstLumis.end(); ++it) {
0028       if (not(it->luminosityBlock() > previous)) {
0029         throw edm::Exception(edm::errors::Configuration)
0030             << "Incorrect ordering of LuminosityBlock numbers in parameter 'firstLuminosityBlockForEachRun'";
0031       }
0032     }
0033   }
0034 }  // namespace
0035 namespace edm {
0036   //used for defaults
0037   static unsigned long long constexpr kNanoSecPerSec = 1000000000ULL;
0038   static unsigned long long constexpr kAveEventPerSec = 200ULL;
0039 
0040   template <typename BASE>
0041   IDGeneratorSourceBase<BASE>::IDGeneratorSourceBase(ParameterSet const& pset,
0042                                                      InputSourceDescription const& desc,
0043                                                      bool realData)
0044       : BASE(pset, desc),
0045         firstLumiForRuns_(
0046             pset.getUntrackedParameter<std::vector<edm::LuminosityBlockID>>("firstLuminosityBlockForEachRun")),
0047         numberEventsInRun_(pset.getUntrackedParameter<unsigned int>("numberEventsInRun", BASE::remainingEvents())),
0048         numberEventsInLumi_(
0049             pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock", BASE::remainingEvents())),
0050         presentTime_(pset.getUntrackedParameter<unsigned long long>("firstTime", 1ULL)),  //time in ns
0051         origTime_(presentTime_),
0052         timeBetweenEvents_(
0053             pset.getUntrackedParameter<unsigned long long>("timeBetweenEvents", kNanoSecPerSec / kAveEventPerSec)),
0054         eventCreationDelay_(pset.getUntrackedParameter<unsigned int>("eventCreationDelay", 0)),
0055         numberEventsInThisRun_(0),
0056         numberEventsInThisLumi_(0),
0057         zerothEvent_(pset.existsAs<unsigned int>("firstEvent", false)
0058                          ? pset.getUntrackedParameter<unsigned int>("firstEvent", 1) - 1
0059                          : pset.getUntrackedParameter<unsigned long long>("firstEvent", 1) - 1),
0060         eventID_(pset.getUntrackedParameter<unsigned int>("firstRun", 1),
0061                  pset.getUntrackedParameter<unsigned int>("firstLuminosityBlock", 1),
0062                  zerothEvent_),
0063         origEventID_(eventID_),
0064         isRealData_(realData),
0065         eType_(EventAuxiliary::Undefined) {
0066     BASE::setTimestamp(Timestamp(presentTime_));
0067     checkFirstLumiForRuns(firstLumiForRuns_);
0068     if (not firstLumiForRuns_.empty()) {
0069       numberEventsInRun_ = -1;
0070       eventID_ = EventID(runForLumi(eventID_.luminosityBlock()), eventID_.luminosityBlock(), zerothEvent_);
0071     }
0072   }
0073 
0074   template <typename BASE>
0075   IDGeneratorSourceBase<BASE>::~IDGeneratorSourceBase() noexcept(false) {}
0076 
0077   template <typename BASE>
0078   std::shared_ptr<RunAuxiliary> IDGeneratorSourceBase<BASE>::readRunAuxiliary_() {
0079     Timestamp ts = Timestamp(presentTime_);
0080     BASE::resetNewRun();
0081     return std::make_shared<RunAuxiliary>(eventID_.run(), ts, Timestamp::invalidTimestamp());
0082   }
0083 
0084   template <typename BASE>
0085   std::shared_ptr<LuminosityBlockAuxiliary> IDGeneratorSourceBase<BASE>::readLuminosityBlockAuxiliary_() {
0086     if (BASE::processingMode() == BASE::Runs)
0087       return std::shared_ptr<LuminosityBlockAuxiliary>();
0088     Timestamp ts = Timestamp(presentTime_);
0089     BASE::resetNewLumi();
0090     return std::make_shared<LuminosityBlockAuxiliary>(
0091         eventID_.run(), eventID_.luminosityBlock(), ts, Timestamp::invalidTimestamp());
0092   }
0093 
0094   template <typename BASE>
0095   void IDGeneratorSourceBase<BASE>::skip(int offset) {
0096     EventID oldEventID = eventID_;
0097     for (; offset < 0; ++offset) {
0098       retreatToPrevious(eventID_, presentTime_);
0099     }
0100     for (; offset > 0; --offset) {
0101       advanceToNext(eventID_, presentTime_);
0102     }
0103     if (eventID_.run() != oldEventID.run()) {
0104       // New Run
0105       BASE::setNewRun();
0106       BASE::setNewLumi();
0107     }
0108     if (eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
0109       // New Lumi
0110       BASE::setNewLumi();
0111     }
0112   }
0113 
0114   template <typename BASE>
0115   void IDGeneratorSourceBase<BASE>::beginJob() {
0116     BASE::beginJob();
0117     // Initialize cannot be called from the constructor, because it is a virtual function
0118     // that needs to be invoked from a derived class if the derived class overrides it.
0119     initialize(eventID_, presentTime_, timeBetweenEvents_);
0120   }
0121 
0122   template <typename BASE>
0123   void IDGeneratorSourceBase<BASE>::initialize(EventID&, TimeValue_t&, TimeValue_t&) {}
0124 
0125   template <typename BASE>
0126   void IDGeneratorSourceBase<BASE>::rewind_() {
0127     presentTime_ = origTime_;
0128     eventID_ = origEventID_;
0129     numberEventsInThisRun_ = 0;
0130     numberEventsInThisLumi_ = 0;
0131     BASE::setNewRun();
0132     BASE::setNewLumi();
0133   }
0134 
0135   template <typename BASE>
0136   typename BASE::ItemType IDGeneratorSourceBase<BASE>::getNextItemType() {
0137     if (BASE::state() == BASE::IsInvalid) {
0138       return noFiles() ? BASE::IsStop : BASE::IsFile;
0139     }
0140     if (BASE::newRun()) {
0141       return BASE::IsRun;
0142     }
0143     if (BASE::newLumi()) {
0144       return BASE::IsLumi;
0145     }
0146     if (BASE::eventCached()) {
0147       return BASE::IsEvent;
0148     }
0149     EventID oldEventID = eventID_;
0150     advanceToNext(eventID_, presentTime_);
0151     if (eventCreationDelay_ > 0) {
0152       usleep(eventCreationDelay_);
0153     }
0154     size_t index = fileIndex();
0155     bool another = setRunAndEventInfo(eventID_, presentTime_, eType_);
0156     if (!another) {
0157       return BASE::IsStop;
0158     }
0159     bool newFile = (fileIndex() > index);
0160     BASE::setEventCached();
0161     if (BASE::newRun() || eventID_.run() != oldEventID.run()) {
0162       // New Run
0163       BASE::setNewRun();
0164       BASE::setNewLumi();
0165       return newFile ? BASE::IsFile : BASE::IsRun;
0166     }
0167     // Same Run
0168     if (BASE::newLumi() || eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
0169       // New Lumi
0170       BASE::setNewLumi();
0171       return newFile ? BASE::IsFile : BASE::IsLumi;
0172     }
0173     return newFile ? BASE::IsFile : BASE::IsEvent;
0174   }
0175 
0176   template <typename BASE>
0177   void IDGeneratorSourceBase<BASE>::advanceToNext(EventID& eventID, TimeValue_t& time) {
0178     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ < numberEventsInRun_) {
0179       // same run
0180       ++numberEventsInThisRun_;
0181       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ < numberEventsInLumi_)) {
0182         // new lumi
0183         eventID = eventID.next(eventID.luminosityBlock() + 1);
0184         numberEventsInThisLumi_ = 1;
0185         if (not firstLumiForRuns_.empty()) {
0186           auto run = runForLumi(eventID.luminosityBlock());
0187           if (run != eventID.run()) {
0188             numberEventsInThisRun_ = 1;
0189             eventID = eventID.nextRunFirstEvent(eventID_.luminosityBlock());
0190 
0191             eventID = EventID(run, eventID.luminosityBlock(), eventID.event());
0192           }
0193         }
0194       } else {
0195         eventID = eventID.next(eventID.luminosityBlock());
0196         ++numberEventsInThisLumi_;
0197       }
0198     } else {
0199       // new run
0200       eventID = eventID.nextRunFirstEvent(origEventID_.luminosityBlock());
0201       numberEventsInThisLumi_ = 1;
0202       numberEventsInThisRun_ = 1;
0203     }
0204     time += timeBetweenEvents_;
0205   }
0206 
0207   template <typename BASE>
0208   void IDGeneratorSourceBase<BASE>::retreatToPrevious(EventID& eventID, TimeValue_t& time) {
0209     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ > 0) {
0210       // same run
0211       --numberEventsInThisRun_;
0212       eventID = eventID.previous(eventID.luminosityBlock());
0213       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ > 0)) {
0214         // new lumi
0215         eventID = eventID.previous(eventID.luminosityBlock() - 1);
0216         numberEventsInThisLumi_ = numberEventsInLumi_;
0217 
0218         if (not firstLumiForRuns_.empty()) {
0219           auto run = runForLumi(eventID.luminosityBlock());
0220           if (run != eventID.run()) {
0221             eventID = eventID.previousRunLastEvent(eventID_.luminosityBlock());
0222 
0223             eventID = EventID(run, eventID.luminosityBlock(), eventID.event());
0224           }
0225         }
0226       } else {
0227         --numberEventsInThisLumi_;
0228       }
0229     } else {
0230       // new run
0231       assert(numberEventsInLumi_ != 0);
0232       eventID = eventID.previousRunLastEvent(origEventID_.luminosityBlock() + numberEventsInRun_ / numberEventsInLumi_);
0233       eventID = EventID(numberEventsInRun_, eventID.luminosityBlock(), eventID.run());
0234       numberEventsInThisLumi_ = numberEventsInLumi_;
0235       numberEventsInThisRun_ = numberEventsInRun_;
0236     }
0237     time -= timeBetweenEvents_;
0238   }
0239 
0240   template <typename BASE>
0241   RunNumber_t IDGeneratorSourceBase<BASE>::runForLumi(LuminosityBlockNumber_t iLumi) const {
0242     auto it = std::find_if(firstLumiForRuns_.rbegin(), firstLumiForRuns_.rend(), [iLumi](auto const& iV) {
0243       return iV.luminosityBlock() <= iLumi;
0244     });
0245     if (it == firstLumiForRuns_.rend()) {
0246       //use first since we are off the end
0247       return firstLumiForRuns_[0].run();
0248     }
0249     return it->run();
0250   }
0251 
0252   template <typename BASE>
0253   bool IDGeneratorSourceBase<BASE>::noFiles() const {
0254     return false;
0255   }
0256 
0257   template <typename BASE>
0258   size_t IDGeneratorSourceBase<BASE>::fileIndex() const {
0259     return 0UL;
0260   }
0261 
0262   template <typename BASE>
0263   void IDGeneratorSourceBase<BASE>::fillDescription(ParameterSetDescription& desc) {
0264     desc.addOptionalUntracked<unsigned int>("numberEventsInRun")
0265         ->setComment("Number of events to generate in each run.");
0266     desc.addOptionalUntracked<unsigned int>("numberEventsInLuminosityBlock")
0267         ->setComment("Number of events to generate in each lumi.");
0268     desc.addUntracked<unsigned long long>("firstTime", 1)->setComment("Time before first event (ns) (for timestamp).");
0269     desc.addUntracked<unsigned long long>("timeBetweenEvents", kNanoSecPerSec / kAveEventPerSec)
0270         ->setComment("Time between consecutive events (ns) (for timestamp).");
0271     desc.addUntracked<unsigned int>("eventCreationDelay", 0)
0272         ->setComment("Real time delay between generation of consecutive events (ms).");
0273 
0274     desc.addNode(edm::ParameterDescription<unsigned int>("firstEvent", 1U, false) xor
0275                  edm::ParameterDescription<unsigned long long>("firstEvent", 1ULL, false))
0276         ->setComment(
0277             "'firstEvent' is an XOR group because it can have type uint32 or uint64, default:1\n"
0278             "Event number of first event to generate.");
0279 
0280     desc.addUntracked<unsigned int>("firstLuminosityBlock", 1)
0281         ->setComment("Luminosity block number of first lumi to generate.");
0282     desc.addUntracked<unsigned int>("firstRun", 1)->setComment("Run number of first run to generate.");
0283     desc.addUntracked<std::vector<edm::LuminosityBlockID>>("firstLuminosityBlockForEachRun", {})
0284         ->setComment(
0285             "When the source makes a new LuminosityBlock, this list is checked to see what Run number should be used. "
0286             "The LuminosityBlock numbers are required to be in ascending order.");
0287     BASE::fillDescription(desc);
0288   }
0289 
0290   template class IDGeneratorSourceBase<PuttableSourceBase>;
0291   template class IDGeneratorSourceBase<InputSource>;
0292 }  // namespace edm