CMS 3D CMS Logo

SourceWithWaits.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Integration
4 // Class : SourceWithWaits
5 //
6 // Original Author: W. David Dagenhart
7 // Created: 12 October 2023
8 
9 // This source allows configuring both a time per lumi section
10 // and events per lumi. Calls to usleep are inserted in the
11 // getNextItemType function in the amount
12 //
13 // (time per lumi) / (events per lumi + 1)
14 //
15 // The sleeps occur before getNextItemType returns when
16 // an event is next and also when a lumi is next (excluding
17 // the first lumi). The total time sleeping that elapses per
18 // lumi is approximately equal to the configured amount.
19 // The algorithm accomplishing this is not perfect and
20 // if the events take enough time to process, then the lumis
21 // will last longer than configured amount (just because
22 // that was a lot easier to implement and good enough for
23 // the test this is used for).
24 //
25 // The time per lumi is the same for all lumis. events per lumi
26 // can be different each lumi. You can also configure a single
27 // value for lumis per run if you want multiple runs.
28 //
29 // The job will stop when the end of the vector specifying
30 // events per lumi is reached (it might end earlier if maxEvents
31 // is also configured).
32 //
33 // In some ways this source is like EmptySource. It does not produce
34 // or read anything. The initial intent is to use for tests of
35 // some issues we are facing with concurrent lumis in the online
36 // source. It emulates the relevant behavior of that source without
37 // all the associated complexity.
38 
52 
53 #include <cassert>
54 #include <memory>
55 #include <unistd.h>
56 #include <vector>
57 
58 namespace edmtest {
60  public:
62  ~SourceWithWaits() override;
64 
65  private:
67  std::shared_ptr<edm::RunAuxiliary> readRunAuxiliary_() override;
68  std::shared_ptr<edm::LuminosityBlockAuxiliary> readLuminosityBlockAuxiliary_() override;
69  void readEvent_(edm::EventPrincipal&) override;
70 
71  unsigned int timePerLumi_; // seconds
72  std::vector<unsigned int> eventsPerLumi_;
73  unsigned int lumisPerRun_;
74 
78  unsigned int currentFile_ = 0;
79  unsigned int eventInCurrentLumi_ = 0;
80  unsigned int lumiInCurrentRun_ = 0;
81  };
82 
84  : edm::InputSource(pset, desc),
85  timePerLumi_(pset.getUntrackedParameter<unsigned int>("timePerLumi")),
86  eventsPerLumi_(pset.getUntrackedParameter<std::vector<unsigned int>>("eventsPerLumi")),
87  lumisPerRun_(pset.getUntrackedParameter<unsigned int>("lumisPerRun")) {}
88 
90 
93  desc.addUntracked<unsigned int>("timePerLumi");
94  desc.addUntracked<std::vector<unsigned int>>("eventsPerLumi");
95  desc.addUntracked<unsigned int>("lumisPerRun");
96  descriptions.add("source", desc);
97  }
98 
100  constexpr unsigned int secondsToMicroseconds = 1000000;
101 
102  // First three cases are for the initial file, run, and lumi transitions
103  // Note that there will always be at exactly one file and at least
104  // one run from this test source.
105  if (currentFile_ == 0u) {
106  ++currentFile_;
108  } else if (currentRun_ == 0u) {
109  ++currentRun_;
111  } else if (currentLumi_ == 0u) {
112  ++currentLumi_;
114  // The job will stop when we hit the end of the eventsPerLumi vector
115  // unless maxEvents stopped it earlier.
116  if ((currentLumi_ - 1) >= eventsPerLumi_.size()) {
118  }
120  }
121  // Handle more events in the current lumi
123  // note the argument to usleep is microseconds, timePerLumi_ is in seconds
124  usleep(secondsToMicroseconds * timePerLumi_ / (eventsPerLumi_[currentLumi_ - 1] + 1));
126  ++currentEvent_;
128  }
129  // Next lumi
130  else if (lumiInCurrentRun_ < lumisPerRun_) {
131  usleep(secondsToMicroseconds * timePerLumi_ / (eventsPerLumi_[currentLumi_ - 1] + 1));
132  ++currentLumi_;
134  // The job will stop when we hit the end of the eventsPerLumi vector
135  // unless maxEvents stopped it earlier.
136  if ((currentLumi_ - 1) >= eventsPerLumi_.size()) {
138  }
141  }
142  // Next run
143  else {
144  // The job will stop when we hit the end of the eventsPerLumi vector
145  // unless maxEvents stopped it earlier. Don't start the run if
146  // it will end with no lumis in it.
147  if (currentLumi_ >= eventsPerLumi_.size()) {
149  }
150  ++currentRun_;
151  lumiInCurrentRun_ = 0;
153  }
154  // Should be impossible to get here
155  assert(false);
156  // return something so it will compile
158  }
159 
160  std::shared_ptr<edm::RunAuxiliary> SourceWithWaits::readRunAuxiliary_() {
162  return std::make_shared<edm::RunAuxiliary>(currentRun_, ts, edm::Timestamp::invalidTimestamp());
163  }
164 
165  std::shared_ptr<edm::LuminosityBlockAuxiliary> SourceWithWaits::readLuminosityBlockAuxiliary_() {
167  return std::make_shared<edm::LuminosityBlockAuxiliary>(
169  }
170 
172  bool isRealData = false;
175  auto history = processHistoryRegistry().getMapped(aux.processHistoryID());
176  eventPrincipal.fillEventPrincipal(aux, history);
177  }
178 
179 } // namespace edmtest
static Timestamp invalidTimestamp()
Definition: Timestamp.h:75
SourceWithWaits(edm::ParameterSet const &, edm::InputSourceDescription const &)
unsigned long long EventNumber_t
unsigned int eventInCurrentLumi_
assert(be >=bs)
unsigned int LuminosityBlockNumber_t
std::vector< unsigned int > eventsPerLumi_
static void fillDescriptions(edm::ConfigurationDescriptions &)
edm::RunNumber_t currentRun_
#define DEFINE_FWK_INPUT_SOURCE(type)
std::string const & processGUID() const
Accessor for global process identifier.
Definition: InputSource.h:197
edm::InputSource::ItemType getNextItemType() override
void readEvent_(edm::EventPrincipal &) override
std::shared_ptr< edm::RunAuxiliary > readRunAuxiliary_() override
std::shared_ptr< edm::LuminosityBlockAuxiliary > readLuminosityBlockAuxiliary_() override
ProcessHistoryRegistry const & processHistoryRegistry() const
Accessors for process history registry.
Definition: InputSource.h:139
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool getMapped(ProcessHistoryID const &key, ProcessHistory &value) const
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistory const *processHistory, DelayedReader *reader=nullptr)
edm::LuminosityBlockNumber_t currentLumi_
edm::EventNumber_t currentEvent_
HLT enums.
unsigned int RunNumber_t
Helper class to handle FWLite file input sources.