CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/FWCore/Framework/src/ConfigurableInputSource.cc

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------
00002 ----------------------------------------------------------------------*/
00003 
00004 #include <errno.h>
00005 
00006 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
00007 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
00010 #include "FWCore/Framework/interface/ConfigurableInputSource.h"
00011 #include "FWCore/Framework/interface/EventPrincipal.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/Framework/interface/MessageReceiverForSource.h"
00014 
00015 namespace edm {
00016   //used for defaults
00017   static unsigned long long const kNanoSecPerSec = 1000000000ULL;
00018   static unsigned long long const kAveEventPerSec = 200ULL;
00019   
00020   ConfigurableInputSource::ConfigurableInputSource(ParameterSet const& pset,
00021                                        InputSourceDescription const& desc, bool realData) :
00022     InputSource(pset, desc),
00023     numberEventsInRun_(pset.getUntrackedParameter<unsigned int>("numberEventsInRun", remainingEvents())),
00024     numberEventsInLumi_(pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock", remainingEvents())),
00025     presentTime_(pset.getUntrackedParameter<unsigned long long>("firstTime", 1ULL)),  //time in ns
00026     origTime_(presentTime_),
00027     timeBetweenEvents_(pset.getUntrackedParameter<unsigned long long>("timeBetweenEvents", kNanoSecPerSec/kAveEventPerSec)),
00028     eventCreationDelay_(pset.getUntrackedParameter<unsigned int>("eventCreationDelay", 0)),
00029     numberEventsInThisRun_(0),
00030     numberEventsInThisLumi_(0),
00031     zerothEvent_(pset.getUntrackedParameter<unsigned int>("firstEvent", 1) - 1),
00032     eventID_(pset.getUntrackedParameter<unsigned int>("firstRun", 1), pset.getUntrackedParameter<unsigned int>("firstLuminosityBlock", 1), zerothEvent_),
00033     origEventID_(eventID_),
00034     newRun_(true),
00035     newLumi_(true),
00036     eventCached_(false),
00037     lumiSet_(false),
00038     eventSet_(false),
00039     isRealData_(realData),
00040     eType_(EventAuxiliary::Undefined),
00041     numberOfEventsBeforeBigSkip_(0),
00042     numberOfSequentialEvents_(0)
00043   { 
00044 
00045     setTimestamp(Timestamp(presentTime_));
00046     // We need to map this string to the EventAuxiliary::ExperimentType enumeration
00047     // std::string eType = pset.getUntrackedParameter<std::string>("experimentType", std::string("Any"))),
00048   }
00049 
00050   ConfigurableInputSource::~ConfigurableInputSource() {
00051   }
00052 
00053   boost::shared_ptr<RunAuxiliary>
00054   ConfigurableInputSource::readRunAuxiliary_() {
00055     Timestamp ts = Timestamp(presentTime_);
00056     newRun_ = false;
00057     return boost::shared_ptr<RunAuxiliary>(new RunAuxiliary(eventID_.run(), ts, Timestamp::invalidTimestamp()));
00058   }
00059 
00060   boost::shared_ptr<LuminosityBlockAuxiliary>
00061   ConfigurableInputSource::readLuminosityBlockAuxiliary_() {
00062     if (processingMode() == Runs) return boost::shared_ptr<LuminosityBlockAuxiliary>();
00063     Timestamp ts = Timestamp(presentTime_);
00064     newLumi_ = false;
00065     return boost::shared_ptr<LuminosityBlockAuxiliary>(new LuminosityBlockAuxiliary(eventID_.run(), eventID_.luminosityBlock(), ts, Timestamp::invalidTimestamp()));
00066   }
00067 
00068   EventPrincipal *
00069   ConfigurableInputSource::readEvent_() {
00070     assert(eventCached_ || processingMode() != RunsLumisAndEvents);
00071     eventCached_ = false;
00072     return eventPrincipalCache();
00073   }
00074 
00075   void
00076   ConfigurableInputSource::reallyReadEvent() {
00077     if (processingMode() != RunsLumisAndEvents) return;
00078     EventSourceSentry sentry(*this);
00079     EventAuxiliary aux(eventID_, processGUID(), Timestamp(presentTime_), isRealData_, eType_);
00080     eventPrincipalCache()->fillEventPrincipal(aux, luminosityBlockPrincipal());
00081     Event e(*eventPrincipalCache(), moduleDescription());
00082     if (!produce(e)) {
00083       eventCached_ = false;
00084       return;
00085     }
00086     e.commit_();
00087     eventCached_ = true;
00088   }
00089 
00090   void
00091   ConfigurableInputSource::skip(int offset) {
00092     for (; offset < 0; ++offset) {
00093       retreatToPrevious();
00094     }
00095     for (; offset > 0; --offset) {
00096       advanceToNext();
00097     }
00098   }
00099 
00100 
00101   void
00102   ConfigurableInputSource::setRun(RunNumber_t r) {
00103     // No need to check for invalid (zero) run number,
00104     // as this is a legitimate way of stopping the job.
00105     // Do nothing if the run is not changed.
00106     if (r != eventID_.run()) {
00107       eventID_ = EventID(r, origEventID_.luminosityBlock(), zerothEvent_);
00108       numberEventsInThisRun_ = 0;
00109       numberEventsInThisLumi_ = 0;
00110       newRun_ = newLumi_ = true;
00111     }
00112   }
00113 
00114   void
00115   ConfigurableInputSource::beginRun(Run&) {
00116   }
00117 
00118   void
00119   ConfigurableInputSource::endRun(Run&) {
00120   }
00121 
00122   void
00123   ConfigurableInputSource::beginLuminosityBlock(LuminosityBlock&) {
00124   }
00125 
00126   void
00127   ConfigurableInputSource::endLuminosityBlock(LuminosityBlock&) {
00128   }
00129 
00130   void
00131   ConfigurableInputSource::setLumi(LuminosityBlockNumber_t lb) {
00132     // Protect against invalid lumi.
00133     if (lb == LuminosityBlockNumber_t()) {
00134         lb = origEventID_.luminosityBlock();
00135     }
00136     // Do nothing if the lumi block is not changed.
00137     if (lb != eventID_.luminosityBlock()) {
00138       eventID_.setLuminosityBlockNumber(lb);
00139       numberEventsInThisLumi_ = 0;
00140       newLumi_ = true;
00141     }
00142     lumiSet_ = true;
00143   }
00144 
00145   void 
00146   ConfigurableInputSource::postForkReacquireResources(boost::shared_ptr<edm::multicore::MessageReceiverForSource> iReceiver) {
00147     receiver_ = iReceiver;
00148     receiver_->receive();
00149     numberOfEventsBeforeBigSkip_ = receiver_->numberOfConsecutiveIndices() + 1;
00150     numberOfSequentialEvents_ = receiver_->numberOfConsecutiveIndices();
00151     repeat();
00152     rewind();
00153   }
00154 
00155   void
00156   ConfigurableInputSource::rewind_() {
00157     presentTime_ = origTime_;
00158     eventID_ = origEventID_;
00159     numberEventsInThisRun_ = 0;
00160     numberEventsInThisLumi_ = 0;
00161 
00162     if(receiver_) {
00163       unsigned int numberToSkip = receiver_->numberToSkip();
00164       numberOfEventsBeforeBigSkip_ = receiver_->numberOfConsecutiveIndices() + 1;
00165       //NOTE: skip() will decrease numberOfEventsBeforeBigSkip_ and therefore we need
00166       // to be sure it is large enough so that it never goes to 0 during the skipping
00167       if(numberOfEventsBeforeBigSkip_ < numberToSkip) {
00168         numberOfEventsBeforeBigSkip_ = numberToSkip+1;
00169       }
00170       skip(numberToSkip);
00171       decreaseRemainingEventsBy(numberToSkip);
00172       numberOfEventsBeforeBigSkip_ = receiver_->numberOfConsecutiveIndices() + 1;
00173     }
00174     newRun_ = newLumi_ = true;
00175   }
00176     
00177   InputSource::ItemType 
00178   ConfigurableInputSource::getNextItemType() {
00179     if (newRun_) {
00180       if (eventID_.run() == RunNumber_t()) {
00181         eventCached_ = false;
00182         return IsStop;
00183       }
00184       return IsRun;
00185     }
00186     if (newLumi_) {
00187       return IsLumi;
00188     }
00189     if(eventCached_) {
00190       return IsEvent;
00191     }
00192     EventID oldEventID = eventID_;
00193     LuminosityBlockNumber_t oldLumi = eventID_.luminosityBlock();
00194     size_t index = fileIndex();
00195     if (!eventSet_) {
00196       lumiSet_ = false;
00197       setRunAndEventInfo();
00198       eventSet_ = true;
00199       nextEvent();
00200     }
00201     if (eventID_.run() == RunNumber_t()) {
00202       eventCached_ = false;
00203       return IsStop;
00204     }
00205     bool newFile = (fileIndex() > index);
00206     if (oldEventID.run() != eventID_.run()) {
00207       //  New Run
00208       // If the user did not explicitly set the luminosity block number,
00209       // reset it back to the beginning.
00210       if (!lumiSet_) {
00211         eventID_.setLuminosityBlockNumber(origEventID_.luminosityBlock());
00212       }
00213       newRun_ = newLumi_ = true;
00214       return newFile ? IsFile : IsRun;
00215     }
00216       // Same Run
00217     if (oldLumi != eventID_.luminosityBlock()) {
00218       // New Lumi
00219       newLumi_ = true;
00220       if (processingMode() != Runs) {
00221         return newFile ? IsFile : IsLumi;
00222       }
00223     }
00224     reallyReadEvent();
00225     if(!eventCached_) {
00226       return IsStop;
00227     }
00228     eventSet_ = false;
00229     return IsEvent;
00230   }
00231 
00232   void 
00233   ConfigurableInputSource::advanceToNext()  {
00234     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ < numberEventsInRun_) {
00235       // same run
00236       ++numberEventsInThisRun_;
00237       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ < numberEventsInLumi_)) {
00238         // new lumi
00239         eventID_ = eventID_.next(eventID_.luminosityBlock() + 1);
00240         numberEventsInThisLumi_ = 1;
00241       } else {
00242         eventID_ = eventID_.next(eventID_.luminosityBlock());
00243         ++numberEventsInThisLumi_;
00244       }
00245     } else {
00246       // new run
00247       eventID_ = eventID_.nextRunFirstEvent(origEventID_.luminosityBlock());
00248       numberEventsInThisLumi_ = 1;
00249       numberEventsInThisRun_ = 1;
00250     }
00251     presentTime_ += timeBetweenEvents_;
00252   }
00253 
00254   void 
00255   ConfigurableInputSource::retreatToPrevious()  {
00256     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ > 0) {
00257       // same run
00258       --numberEventsInThisRun_;
00259       eventID_ = eventID_.previous(eventID_.luminosityBlock());
00260       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ > 0)) {
00261         // new lumi
00262         eventID_ = eventID_.previous(eventID_.luminosityBlock() - 1);
00263         numberEventsInThisLumi_ = numberEventsInLumi_;
00264       } else {
00265         --numberEventsInThisLumi_;
00266       }
00267     } else {
00268       // new run
00269       eventID_ = eventID_.previousRunLastEvent(origEventID_.luminosityBlock() + numberEventsInRun_/numberEventsInLumi_);
00270       eventID_ = EventID(numberEventsInRun_, eventID_.luminosityBlock(), eventID_.run());
00271       numberEventsInThisLumi_ = numberEventsInLumi_;
00272       numberEventsInThisRun_ = numberEventsInRun_;
00273     }
00274     presentTime_ -= timeBetweenEvents_;
00275   }
00276   
00277   void
00278   ConfigurableInputSource::setRunAndEventInfo() {
00279     if(receiver_ && 0 == --numberOfEventsBeforeBigSkip_) {
00280       receiver_->receive();
00281       unsigned long numberOfEventsToSkip = receiver_->numberToSkip();
00282       if (numberOfEventsToSkip !=0) {
00283         skip(numberOfEventsToSkip);
00284         decreaseRemainingEventsBy(numberOfEventsToSkip);
00285       }
00286       numberOfEventsBeforeBigSkip_ = receiver_->numberOfConsecutiveIndices();
00287       //Since we decrease 'remaining events' count we need to see if we reached 0 and therefore are at the end
00288       if(0 == numberOfEventsBeforeBigSkip_ or 0==remainingEvents() or 0 == remainingLuminosityBlocks()) {
00289         //this means we are to stop
00290         eventID_ = EventID();
00291         return;
00292       }
00293     }
00294     advanceToNext();
00295     if (eventCreationDelay_ > 0) {usleep(eventCreationDelay_);}
00296   }
00297 
00298   void
00299   ConfigurableInputSource::fillDescription(ParameterSetDescription& desc) {
00300     desc.addOptionalUntracked<unsigned int>("numberEventsInRun")->setComment("Number of events to generate in each run.");
00301     desc.addOptionalUntracked<unsigned int>("numberEventsInLuminosityBlock")->setComment("Number of events to generate in each lumi.");
00302     desc.addUntracked<unsigned long long>("firstTime", 1)->setComment("Time before first event (ns) (for timestamp).");
00303     desc.addUntracked<unsigned long long>("timeBetweenEvents", kNanoSecPerSec/kAveEventPerSec)->setComment("Time between consecutive events (ns) (for timestamp).");
00304     desc.addUntracked<unsigned int>("eventCreationDelay", 0)->setComment("Real time delay between generation of consecutive events (ms).");
00305     desc.addUntracked<unsigned int>("firstEvent", 1)->setComment("Event number of first event to generate.");
00306     desc.addUntracked<unsigned int>("firstLuminosityBlock", 1)->setComment("Luminosity block number of first lumi to generate.");
00307     desc.addUntracked<unsigned int>("firstRun", 1)->setComment("Run number of first run to generate.");
00308     InputSource::fillDescription(desc);
00309   }
00310 
00311 }