CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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 int const kNanoSecPerSec = 1000000000U;
00018   static unsigned int const kAveEventPerSec = 200U;
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 int>("firstTime", 1)),  //time in ns
00026     origTime_(presentTime_),
00027     timeBetweenEvents_(pset.getUntrackedParameter<unsigned int>("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     std::auto_ptr<EventAuxiliary> aux(new EventAuxiliary(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     if (!eventSet_) {
00195       lumiSet_ = false;
00196       setRunAndEventInfo();
00197       eventSet_ = true;
00198     }
00199     if (eventID_.run() == RunNumber_t()) {
00200       eventCached_ = false;
00201       return IsStop;
00202     }
00203     if (oldEventID.run() != eventID_.run()) {
00204       //  New Run
00205       // If the user did not explicitly set the luminosity block number,
00206       // reset it back to the beginning.
00207       if (!lumiSet_) {
00208         eventID_.setLuminosityBlockNumber(origEventID_.luminosityBlock());
00209       }
00210       newRun_ = newLumi_ = true;
00211       return IsRun;
00212     }
00213       // Same Run
00214     if (oldLumi != eventID_.luminosityBlock()) {
00215       // New Lumi
00216       newLumi_ = true;
00217       if (processingMode() != Runs) {
00218         return IsLumi;
00219       }
00220     }
00221     reallyReadEvent();
00222     if(!eventCached_) {
00223       return IsStop;
00224     }
00225     eventSet_ = false;
00226     return IsEvent;
00227   }
00228 
00229   void 
00230   ConfigurableInputSource::advanceToNext()  {
00231     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ < numberEventsInRun_) {
00232       // same run
00233       ++numberEventsInThisRun_;
00234       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ < numberEventsInLumi_)) {
00235         // new lumi
00236         eventID_ = eventID_.next(eventID_.luminosityBlock() + 1);
00237         numberEventsInThisLumi_ = 1;
00238       } else {
00239         eventID_ = eventID_.next(eventID_.luminosityBlock());
00240         ++numberEventsInThisLumi_;
00241       }
00242     } else {
00243       // new run
00244       eventID_ = eventID_.nextRunFirstEvent(origEventID_.luminosityBlock());
00245       numberEventsInThisLumi_ = 1;
00246       numberEventsInThisRun_ = 1;
00247     }
00248     presentTime_ += timeBetweenEvents_;
00249   }
00250 
00251   void 
00252   ConfigurableInputSource::retreatToPrevious()  {
00253     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ > 0) {
00254       // same run
00255       --numberEventsInThisRun_;
00256       eventID_ = eventID_.previous(eventID_.luminosityBlock());
00257       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ > 0)) {
00258         // new lumi
00259         eventID_ = eventID_.previous(eventID_.luminosityBlock() - 1);
00260         numberEventsInThisLumi_ = numberEventsInLumi_;
00261       } else {
00262         --numberEventsInThisLumi_;
00263       }
00264     } else {
00265       // new run
00266       eventID_ = eventID_.previousRunLastEvent(origEventID_.luminosityBlock() + numberEventsInRun_/numberEventsInLumi_);
00267       eventID_ = EventID(numberEventsInRun_, eventID_.luminosityBlock(), eventID_.run());
00268       numberEventsInThisLumi_ = numberEventsInLumi_;
00269       numberEventsInThisRun_ = numberEventsInRun_;
00270     }
00271     presentTime_ -= timeBetweenEvents_;
00272   }
00273   
00274   void
00275   ConfigurableInputSource::setRunAndEventInfo() {
00276     if(receiver_ && 0 == --numberOfEventsBeforeBigSkip_) {
00277       receiver_->receive();
00278       unsigned long numberOfEventsToSkip = receiver_->numberToSkip();
00279       if (numberOfEventsToSkip !=0) {
00280         skip(numberOfEventsToSkip);
00281         decreaseRemainingEventsBy(numberOfEventsToSkip);
00282       }
00283       numberOfEventsBeforeBigSkip_ = receiver_->numberOfConsecutiveIndices();
00284       //Since we decrease 'remaining events' count we need to see if we reached 0 and therefore are at the end
00285       if(0 == numberOfEventsBeforeBigSkip_ or 0==remainingEvents() or 0 == remainingLuminosityBlocks()) {
00286         //this means we are to stop
00287         eventID_ = EventID();
00288         return;
00289       }
00290     }
00291     advanceToNext();
00292     if (eventCreationDelay_ > 0) {usleep(eventCreationDelay_);}
00293   }
00294 
00295   void
00296   ConfigurableInputSource::fillDescription(ParameterSetDescription& desc) {
00297     desc.addOptionalUntracked<unsigned int>("numberEventsInRun")->setComment("Number of events to generate in each run.");
00298     desc.addOptionalUntracked<unsigned int>("numberEventsInLuminosityBlock")->setComment("Number of events to generate in each lumi.");
00299     desc.addUntracked<unsigned int>("firstTime", 1)->setComment("Time before first event (ns) (for timestamp).");
00300     desc.addUntracked<unsigned int>("timeBetweenEvents", kNanoSecPerSec/kAveEventPerSec)->setComment("Time between consecutive events (ns) (for timestamp).");
00301     desc.addUntracked<unsigned int>("eventCreationDelay", 0)->setComment("Real time delay between generation of consecutive events (ms).");
00302     desc.addUntracked<unsigned int>("firstEvent", 1)->setComment("Event number of first event to generate.");
00303     desc.addUntracked<unsigned int>("firstLuminosityBlock", 1)->setComment("Luminosity block number of first lumi to generate.");
00304     desc.addUntracked<unsigned int>("firstRun", 1)->setComment("Run number of first run to generate.");
00305     InputSource::fillDescription(desc);
00306   }
00307 
00308 }