CMS 3D CMS Logo

IDGeneratorSourceBase.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 ----------------------------------------------------------------------*/
3 
4 #include <cerrno>
5 
18 
20 
21 namespace {
22  void checkFirstLumiForRuns(std::vector<edm::LuminosityBlockID> const& iFirstLumis) {
23  if (iFirstLumis.empty())
24  return;
25 
26  auto previous = iFirstLumis[0].luminosityBlock();
27  for (auto it = iFirstLumis.begin() + 1; it != iFirstLumis.end(); ++it) {
28  if (not(it->luminosityBlock() > previous)) {
30  << "Incorrect ordering of LuminosityBlock numbers in parameter 'firstLuminosityBlockForEachRun'";
31  }
32  }
33  }
34 } // namespace
35 namespace edm {
36  //used for defaults
37  static unsigned long long constexpr kNanoSecPerSec = 1000000000ULL;
38  static unsigned long long constexpr kAveEventPerSec = 200ULL;
39 
40  template <typename BASE>
43  bool realData)
44  : BASE(pset, desc),
45  firstLumiForRuns_(
46  pset.getUntrackedParameter<std::vector<edm::LuminosityBlockID>>("firstLuminosityBlockForEachRun")),
47  numberEventsInRun_(pset.getUntrackedParameter<unsigned int>("numberEventsInRun", BASE::remainingEvents())),
48  numberEventsInLumi_(
49  pset.getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock", BASE::remainingEvents())),
50  presentTime_(pset.getUntrackedParameter<unsigned long long>("firstTime", 1ULL)), //time in ns
51  origTime_(presentTime_),
52  timeBetweenEvents_(
53  pset.getUntrackedParameter<unsigned long long>("timeBetweenEvents", kNanoSecPerSec / kAveEventPerSec)),
54  eventCreationDelay_(pset.getUntrackedParameter<unsigned int>("eventCreationDelay", 0)),
55  numberEventsInThisRun_(0),
56  numberEventsInThisLumi_(0),
57  zerothEvent_(pset.existsAs<unsigned int>("firstEvent", false)
58  ? pset.getUntrackedParameter<unsigned int>("firstEvent", 1) - 1
59  : pset.getUntrackedParameter<unsigned long long>("firstEvent", 1) - 1),
60  eventID_(pset.getUntrackedParameter<unsigned int>("firstRun", 1),
61  pset.getUntrackedParameter<unsigned int>("firstLuminosityBlock", 1),
62  zerothEvent_),
63  origEventID_(eventID_),
64  isRealData_(realData),
65  eType_(EventAuxiliary::Undefined) {
66  BASE::setTimestamp(Timestamp(presentTime_));
67  checkFirstLumiForRuns(firstLumiForRuns_);
68  if (not firstLumiForRuns_.empty()) {
69  numberEventsInRun_ = -1;
70  eventID_ = EventID(runForLumi(eventID_.luminosityBlock()), eventID_.luminosityBlock(), zerothEvent_);
71  }
72  }
73 
74  template <typename BASE>
76 
77  template <typename BASE>
78  std::shared_ptr<RunAuxiliary> IDGeneratorSourceBase<BASE>::readRunAuxiliary_() {
79  Timestamp ts = Timestamp(presentTime_);
80  BASE::resetNewRun();
81  return std::make_shared<RunAuxiliary>(eventID_.run(), ts, Timestamp::invalidTimestamp());
82  }
83 
84  template <typename BASE>
85  std::shared_ptr<LuminosityBlockAuxiliary> IDGeneratorSourceBase<BASE>::readLuminosityBlockAuxiliary_() {
86  if (BASE::processingMode() == BASE::Runs)
87  return std::shared_ptr<LuminosityBlockAuxiliary>();
88  Timestamp ts = Timestamp(presentTime_);
89  BASE::resetNewLumi();
90  return std::make_shared<LuminosityBlockAuxiliary>(
91  eventID_.run(), eventID_.luminosityBlock(), ts, Timestamp::invalidTimestamp());
92  }
93 
94  template <typename BASE>
96  EventID oldEventID = eventID_;
97  for (; offset < 0; ++offset) {
98  retreatToPrevious(eventID_, presentTime_);
99  }
100  for (; offset > 0; --offset) {
101  advanceToNext(eventID_, presentTime_);
102  }
103  if (eventID_.run() != oldEventID.run()) {
104  // New Run
105  BASE::setNewRun();
106  BASE::setNewLumi();
107  }
108  if (eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
109  // New Lumi
110  BASE::setNewLumi();
111  }
112  }
113 
114  template <typename BASE>
116  BASE::beginJob();
117  // Initialize cannot be called from the constructor, because it is a virtual function
118  // that needs to be invoked from a derived class if the derived class overrides it.
119  initialize(eventID_, presentTime_, timeBetweenEvents_);
120  }
121 
122  template <typename BASE>
124 
125  template <typename BASE>
127  presentTime_ = origTime_;
128  eventID_ = origEventID_;
129  numberEventsInThisRun_ = 0;
130  numberEventsInThisLumi_ = 0;
131  BASE::setNewRun();
132  BASE::setNewLumi();
133  }
134 
135  template <typename BASE>
137  if (BASE::state() == BASE::IsInvalid) {
138  return noFiles() ? BASE::IsStop : BASE::IsFile;
139  }
140  if (BASE::newRun()) {
141  return BASE::IsRun;
142  }
143  if (BASE::newLumi()) {
144  return BASE::IsLumi;
145  }
146  if (BASE::eventCached()) {
147  return BASE::IsEvent;
148  }
149  EventID oldEventID = eventID_;
150  advanceToNext(eventID_, presentTime_);
151  if (eventCreationDelay_ > 0) {
152  usleep(eventCreationDelay_);
153  }
154  size_t index = fileIndex();
155  bool another = setRunAndEventInfo(eventID_, presentTime_, eType_);
156  if (!another) {
157  return BASE::IsStop;
158  }
159  bool newFile = (fileIndex() > index);
160  BASE::setEventCached();
161  if (BASE::newRun() || eventID_.run() != oldEventID.run()) {
162  // New Run
163  BASE::setNewRun();
164  BASE::setNewLumi();
165  return newFile ? BASE::IsFile : BASE::IsRun;
166  }
167  // Same Run
168  if (BASE::newLumi() || eventID_.luminosityBlock() != oldEventID.luminosityBlock()) {
169  // New Lumi
170  BASE::setNewLumi();
171  return newFile ? BASE::IsFile : BASE::IsLumi;
172  }
173  return newFile ? BASE::IsFile : BASE::IsEvent;
174  }
175 
176  template <typename BASE>
178  if (numberEventsInRun_ < 1 || numberEventsInThisRun_ < numberEventsInRun_) {
179  // same run
180  ++numberEventsInThisRun_;
181  if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ < numberEventsInLumi_)) {
182  // new lumi
183  eventID = eventID.next(eventID.luminosityBlock() + 1);
184  numberEventsInThisLumi_ = 1;
185  if (not firstLumiForRuns_.empty()) {
186  auto run = runForLumi(eventID.luminosityBlock());
187  if (run != eventID.run()) {
188  numberEventsInThisRun_ = 1;
189  eventID = eventID.nextRunFirstEvent(eventID_.luminosityBlock());
190 
191  eventID = EventID(run, eventID.luminosityBlock(), eventID.event());
192  }
193  }
194  } else {
195  eventID = eventID.next(eventID.luminosityBlock());
196  ++numberEventsInThisLumi_;
197  }
198  } else {
199  // new run
200  eventID = eventID.nextRunFirstEvent(origEventID_.luminosityBlock());
201  numberEventsInThisLumi_ = 1;
202  numberEventsInThisRun_ = 1;
203  }
204  time += timeBetweenEvents_;
205  }
206 
207  template <typename BASE>
209  if (numberEventsInRun_ < 1 || numberEventsInThisRun_ > 0) {
210  // same run
211  --numberEventsInThisRun_;
212  eventID = eventID.previous(eventID.luminosityBlock());
213  if (!(numberEventsInLumi_ < 1 || numberEventsInThisLumi_ > 0)) {
214  // new lumi
215  eventID = eventID.previous(eventID.luminosityBlock() - 1);
216  numberEventsInThisLumi_ = numberEventsInLumi_;
217 
218  if (not firstLumiForRuns_.empty()) {
219  auto run = runForLumi(eventID.luminosityBlock());
220  if (run != eventID.run()) {
221  eventID = eventID.previousRunLastEvent(eventID_.luminosityBlock());
222 
223  eventID = EventID(run, eventID.luminosityBlock(), eventID.event());
224  }
225  }
226  } else {
227  --numberEventsInThisLumi_;
228  }
229  } else {
230  // new run
231  assert(numberEventsInLumi_ != 0);
232  eventID = eventID.previousRunLastEvent(origEventID_.luminosityBlock() + numberEventsInRun_ / numberEventsInLumi_);
233  eventID = EventID(numberEventsInRun_, eventID.luminosityBlock(), eventID.run());
234  numberEventsInThisLumi_ = numberEventsInLumi_;
235  numberEventsInThisRun_ = numberEventsInRun_;
236  }
237  time -= timeBetweenEvents_;
238  }
239 
240  template <typename BASE>
242  auto it = std::find_if(firstLumiForRuns_.rbegin(), firstLumiForRuns_.rend(), [iLumi](auto const& iV) {
243  return iV.luminosityBlock() <= iLumi;
244  });
245  if (it == firstLumiForRuns_.rend()) {
246  //use first since we are off the end
247  return firstLumiForRuns_[0].run();
248  }
249  return it->run();
250  }
251 
252  template <typename BASE>
254  return false;
255  }
256 
257  template <typename BASE>
259  return 0UL;
260  }
261 
262  template <typename BASE>
264  desc.addOptionalUntracked<unsigned int>("numberEventsInRun")
265  ->setComment("Number of events to generate in each run.");
266  desc.addOptionalUntracked<unsigned int>("numberEventsInLuminosityBlock")
267  ->setComment("Number of events to generate in each lumi.");
268  desc.addUntracked<unsigned long long>("firstTime", 1)->setComment("Time before first event (ns) (for timestamp).");
269  desc.addUntracked<unsigned long long>("timeBetweenEvents", kNanoSecPerSec / kAveEventPerSec)
270  ->setComment("Time between consecutive events (ns) (for timestamp).");
271  desc.addUntracked<unsigned int>("eventCreationDelay", 0)
272  ->setComment("Real time delay between generation of consecutive events (ms).");
273 
274  desc.addNode(edm::ParameterDescription<unsigned int>("firstEvent", 1U, false) xor
275  edm::ParameterDescription<unsigned long long>("firstEvent", 1ULL, false))
276  ->setComment(
277  "'firstEvent' is an XOR group because it can have type uint32 or uint64, default:1\n"
278  "Event number of first event to generate.");
279 
280  desc.addUntracked<unsigned int>("firstLuminosityBlock", 1)
281  ->setComment("Luminosity block number of first lumi to generate.");
282  desc.addUntracked<unsigned int>("firstRun", 1)->setComment("Run number of first run to generate.");
283  desc.addUntracked<std::vector<edm::LuminosityBlockID>>("firstLuminosityBlockForEachRun", {})
284  ->setComment(
285  "When the source makes a new LuminosityBlock, this list is checked to see what Run number should be used. "
286  "The LuminosityBlock numbers are required to be in ascending order.");
287  BASE::fillDescription(desc);
288  }
289 
291  template class IDGeneratorSourceBase<InputSource>;
292 } // namespace edm
static unsigned long long constexpr kAveEventPerSec
EventID next(LuminosityBlockNumber_t const &lumi) const
Definition: EventID.h:43
static AlgebraicMatrix initialize()
IDGeneratorSourceBase(ParameterSet const &pset, InputSourceDescription const &desc, bool realData)
assert(be >=bs)
unsigned int LuminosityBlockNumber_t
void beginJob()
Definition: Breakpoints.cc:14
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:39
EventID previous(LuminosityBlockNumber_t const &lumi) const
Definition: EventID.h:58
unsigned long long TimeValue_t
Definition: Timestamp.h:28
RunNumber_t run() const
Definition: EventID.h:38
EventID nextRunFirstEvent(LuminosityBlockNumber_t const &lumi) const
Definition: EventID.h:50
EventID previousRunLastEvent(LuminosityBlockNumber_t const &lumi) const
Definition: EventID.h:51
static unsigned long long constexpr kNanoSecPerSec
HLT enums.
unsigned int RunNumber_t
EventNumber_t event() const
Definition: EventID.h:40