CMS 3D CMS Logo

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