CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/FWCore/Sources/src/ProducerSourceBase.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/EventPrincipal.h"
00011 #include "FWCore/Framework/interface/Event.h"
00012 #include "FWCore/Sources/interface/ProducerSourceBase.h"
00013 
00014 namespace edm {
00015   //used for defaults
00016   static unsigned long long const kNanoSecPerSec = 1000000000ULL;
00017   static unsigned long long const kAveEventPerSec = 200ULL;
00018   
00019   ProducerSourceBase::ProducerSourceBase(ParameterSet const& pset,
00020                                        InputSourceDescription const& desc, bool realData) :
00021       InputSource(pset, desc),
00022       numberEventsInRun_(pset.getUntrackedParameter<unsigned int>("numberEventsInRun", remainingEvents())),
00023       numberEventsInLumi_(pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock", remainingEvents())),
00024       presentTime_(pset.getUntrackedParameter<unsigned long long>("firstTime", 1ULL)),  //time in ns
00025       origTime_(presentTime_),
00026       timeBetweenEvents_(pset.getUntrackedParameter<unsigned long long>("timeBetweenEvents", kNanoSecPerSec/kAveEventPerSec)),
00027       eventCreationDelay_(pset.getUntrackedParameter<unsigned int>("eventCreationDelay", 0)),
00028       numberEventsInThisRun_(0),
00029       numberEventsInThisLumi_(0),
00030       zerothEvent_(pset.getUntrackedParameter<unsigned int>("firstEvent", 1) - 1),
00031       eventID_(pset.getUntrackedParameter<unsigned int>("firstRun", 1), pset.getUntrackedParameter<unsigned int>("firstLuminosityBlock", 1), zerothEvent_),
00032       origEventID_(eventID_),
00033       isRealData_(realData),
00034       eType_(EventAuxiliary::Undefined) {
00035 
00036     setTimestamp(Timestamp(presentTime_));
00037     // We need to map this string to the EventAuxiliary::ExperimentType enumeration
00038     // std::string eType = pset.getUntrackedParameter<std::string>("experimentType", std::string("Any"))),
00039   }
00040 
00041   ProducerSourceBase::~ProducerSourceBase() {
00042   }
00043 
00044   boost::shared_ptr<RunAuxiliary>
00045   ProducerSourceBase::readRunAuxiliary_() {
00046     Timestamp ts = Timestamp(presentTime_);
00047     resetNewRun();
00048     return boost::shared_ptr<RunAuxiliary>(new RunAuxiliary(eventID_.run(), ts, Timestamp::invalidTimestamp()));
00049   }
00050 
00051   boost::shared_ptr<LuminosityBlockAuxiliary>
00052   ProducerSourceBase::readLuminosityBlockAuxiliary_() {
00053     if (processingMode() == Runs) return boost::shared_ptr<LuminosityBlockAuxiliary>();
00054     Timestamp ts = Timestamp(presentTime_);
00055     resetNewLumi();
00056     return boost::shared_ptr<LuminosityBlockAuxiliary>(new LuminosityBlockAuxiliary(eventID_.run(), eventID_.luminosityBlock(), ts, Timestamp::invalidTimestamp()));
00057   }
00058 
00059   EventPrincipal *
00060   ProducerSourceBase::readEvent_(EventPrincipal& eventPrincipal) {
00061     assert(eventCached() || processingMode() != RunsLumisAndEvents);
00062     EventSourceSentry sentry(*this);
00063     EventAuxiliary aux(eventID_, processGUID(), Timestamp(presentTime_), isRealData_, eType_);
00064     eventPrincipal.fillEventPrincipal(aux);
00065     Event e(eventPrincipal, moduleDescription());
00066     produce(e);
00067     e.commit_();
00068     resetEventCached();
00069     return &eventPrincipal;
00070   }
00071 
00072   void
00073   ProducerSourceBase::skip(int offset) {
00074     EventID oldEventID = eventID_;
00075     for(; offset < 0; ++offset) {
00076       retreatToPrevious(eventID_, presentTime_);
00077     }
00078     for(; offset > 0; --offset) {
00079       advanceToNext(eventID_, presentTime_);
00080     }
00081     if(eventID_.run() != oldEventID.run()) {
00082       // New Run
00083       setNewRun();
00084       setNewLumi();
00085     }
00086     if (eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
00087       // New Lumi
00088       setNewLumi();
00089     }
00090   }
00091 
00092   void
00093   ProducerSourceBase::beginJob() {
00094     // initialize cannot be called from the constructor, because it is a virtual function
00095     // that needs to be invoked from a derived class if the derived class overrides it.
00096     initialize(eventID_, presentTime_, timeBetweenEvents_);
00097   }
00098 
00099   void
00100   ProducerSourceBase::beginRun(Run&) {
00101   }
00102 
00103   void
00104   ProducerSourceBase::endRun(Run&) {
00105   }
00106 
00107   void
00108   ProducerSourceBase::beginLuminosityBlock(LuminosityBlock&) {
00109   }
00110 
00111   void
00112   ProducerSourceBase::endLuminosityBlock(LuminosityBlock&) {
00113   }
00114 
00115   void
00116   ProducerSourceBase::initialize(EventID&, TimeValue_t&, TimeValue_t&) {
00117   }
00118 
00119   void
00120   ProducerSourceBase::rewind_() {
00121     presentTime_ = origTime_;
00122     eventID_ = origEventID_;
00123     numberEventsInThisRun_ = 0;
00124     numberEventsInThisLumi_ = 0;
00125     setNewRun();
00126     setNewLumi();
00127   }
00128     
00129   InputSource::ItemType 
00130   ProducerSourceBase::getNextItemType() {
00131     if(state() == IsInvalid) {
00132       return noFiles() ? IsStop : IsFile;
00133     }
00134     if (newRun()) {
00135       return IsRun;
00136     }
00137     if (newLumi()) {
00138       return IsLumi;
00139     }
00140     if(eventCached()) {
00141       return IsEvent;
00142     }
00143     EventID oldEventID = eventID_;
00144     advanceToNext(eventID_, presentTime_);
00145     if (eventCreationDelay_ > 0) {usleep(eventCreationDelay_);}
00146     size_t index = fileIndex();
00147     bool another = setRunAndEventInfo(eventID_, presentTime_);
00148     if(!another) {
00149       return IsStop;
00150     }
00151     bool newFile = (fileIndex() > index);
00152     setEventCached();
00153     if(eventID_.run() != oldEventID.run()) {
00154       // New Run
00155       setNewRun();
00156       setNewLumi();
00157       return newFile ? IsFile : IsRun;
00158     }
00159     if (processingMode() == Runs) {
00160       return newFile ? IsFile : IsRun;
00161     }
00162     if (processingMode() == RunsAndLumis) {
00163       return newFile ? IsFile : IsLumi;
00164     }
00165     // Same Run
00166     if (eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
00167       // New Lumi
00168       setNewLumi();
00169       return newFile ? IsFile : IsLumi;
00170     }
00171     return newFile ? IsFile : IsEvent;
00172   }
00173 
00174   void 
00175   ProducerSourceBase::advanceToNext(EventID& eventID, TimeValue_t& time)  {
00176     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ < numberEventsInRun_) {
00177       // same run
00178       ++numberEventsInThisRun_;
00179       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ < numberEventsInLumi_)) {
00180         // new lumi
00181         eventID = eventID.next(eventID.luminosityBlock() + 1);
00182         numberEventsInThisLumi_ = 1;
00183       } else {
00184         eventID = eventID.next(eventID.luminosityBlock());
00185         ++numberEventsInThisLumi_;
00186       }
00187     } else {
00188       // new run
00189       eventID = eventID.nextRunFirstEvent(origEventID_.luminosityBlock());
00190       numberEventsInThisLumi_ = 1;
00191       numberEventsInThisRun_ = 1;
00192     }
00193     time += timeBetweenEvents_;
00194   }
00195 
00196   void 
00197   ProducerSourceBase::retreatToPrevious(EventID& eventID, TimeValue_t& time)  {
00198     if (numberEventsInRun_ < 1 || numberEventsInThisRun_ > 0) {
00199       // same run
00200       --numberEventsInThisRun_;
00201       eventID = eventID.previous(eventID.luminosityBlock());
00202       if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ > 0)) {
00203         // new lumi
00204         eventID = eventID.previous(eventID.luminosityBlock() - 1);
00205         numberEventsInThisLumi_ = numberEventsInLumi_;
00206       } else {
00207         --numberEventsInThisLumi_;
00208       }
00209     } else {
00210       // new run
00211       eventID = eventID.previousRunLastEvent(origEventID_.luminosityBlock() + numberEventsInRun_/numberEventsInLumi_);
00212       eventID = EventID(numberEventsInRun_, eventID.luminosityBlock(), eventID.run());
00213       numberEventsInThisLumi_ = numberEventsInLumi_;
00214       numberEventsInThisRun_ = numberEventsInRun_;
00215     }
00216     time -= timeBetweenEvents_;
00217   }
00218 
00219   bool
00220   ProducerSourceBase::noFiles() const {
00221     return false;
00222   }
00223   
00224   size_t
00225   ProducerSourceBase::fileIndex() const {
00226     return 0UL;
00227   }
00228   
00229   void
00230   ProducerSourceBase::fillDescription(ParameterSetDescription& desc) {
00231     desc.addOptionalUntracked<unsigned int>("numberEventsInRun")->setComment("Number of events to generate in each run.");
00232     desc.addOptionalUntracked<unsigned int>("numberEventsInLuminosityBlock")->setComment("Number of events to generate in each lumi.");
00233     desc.addUntracked<unsigned long long>("firstTime", 1)->setComment("Time before first event (ns) (for timestamp).");
00234     desc.addUntracked<unsigned long long>("timeBetweenEvents", kNanoSecPerSec/kAveEventPerSec)->setComment("Time between consecutive events (ns) (for timestamp).");
00235     desc.addUntracked<unsigned int>("eventCreationDelay", 0)->setComment("Real time delay between generation of consecutive events (ms).");
00236     desc.addUntracked<unsigned int>("firstEvent", 1)->setComment("Event number of first event to generate.");
00237     desc.addUntracked<unsigned int>("firstLuminosityBlock", 1)->setComment("Luminosity block number of first lumi to generate.");
00238     desc.addUntracked<unsigned int>("firstRun", 1)->setComment("Run number of first run to generate.");
00239     InputSource::fillDescription(desc);
00240   }
00241 }
00242