CMS 3D CMS Logo

PileUp.h
Go to the documentation of this file.
1 #ifndef Mixing_Base_PileUp_h
2 #define Mixing_Base_PileUp_h
3 
4 #include <memory>
5 #include <string>
6 #include <vector>
7 #include <optional>
16 
17 #include "TH1F.h"
18 
19 namespace CLHEP {
20  class RandPoissonQ;
21  class RandPoisson;
22  class HepRandomEngine;
23 } // namespace CLHEP
24 
25 class MixingModuleConfig;
26 class MixingRcd;
28 
29 namespace edm {
30  class SecondaryEventProvider;
31  class StreamID;
32  class ProcessContext;
33 
34  struct PileUpConfig {
35  PileUpConfig(std::string sourcename, double averageNumber, std::unique_ptr<TH1F>& histo, const bool playback)
39  std::shared_ptr<TH1F> histo_;
40  const bool playback_;
41  };
42 
43  class PileUp {
44  public:
45  explicit PileUp(ParameterSet const& pset,
46  const std::shared_ptr<PileUpConfig>& config,
48  const bool mixingConfigFromDB);
49  ~PileUp();
50 
51  template <typename T>
52  void readPileUp(edm::EventID const& signal,
53  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
54  T eventOperator,
55  int const NumPU,
56  StreamID const&);
57 
58  template <typename T>
59  void playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin,
60  std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end,
61  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
62  T eventOperator);
63 
64  template <typename T>
65  void playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin,
66  std::vector<edm::EventID>::const_iterator end,
67  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
68  T eventOperator);
69 
70  double averageNumber() const { return averageNumber_; }
71  bool poisson() const { return poisson_; }
72  bool doPileUp(int BX) {
73  if (Source_type_ != "cosmics") {
74  return none_ ? false : averageNumber_ > 0.;
75  } else {
76  return (BX >= minBunch_cosmics_ && BX <= maxBunch_cosmics_);
77  }
78  }
79  void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
80  input_->dropUnwantedBranches(wantedBranches);
81  }
84  void endStream();
85 
86  void beginRun(const edm::Run& run, const edm::EventSetup& setup);
88 
89  void endRun(const edm::Run& run, const edm::EventSetup& setup);
91 
93 
94  void reload(const edm::EventSetup& setup);
95 
96  void CalculatePileup(int MinBunch,
97  int MaxBunch,
98  std::vector<int>& PileupSelection,
99  std::vector<float>& TrueNumInteractions,
100  StreamID const&);
101 
102  //template<typename T>
103  // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
104  // std::vector<edm::SecondaryEventIDAndFileInfo> &ids, T& eventOperator);
105 
106  const unsigned int& input() const { return inputType_; }
107  void input(unsigned int s) { inputType_ = s; }
108 
109  private:
110  std::unique_ptr<CLHEP::RandPoissonQ> const& poissonDistribution(StreamID const& streamID);
111  std::unique_ptr<CLHEP::RandPoisson> const& poissonDistr_OOT(StreamID const& streamID);
112  CLHEP::HepRandomEngine* randomEngine(StreamID const& streamID);
114  void setRandomEngine(LuminosityBlock const&);
115 
116  unsigned int inputType_;
120  int const intAverage_;
121  std::shared_ptr<TH1F> histo_;
124  bool poisson_;
125  bool fixed_;
126  bool none_;
130 
131  bool PU_Study_;
133 
136 
139 
142  std::shared_ptr<ProductRegistry> productRegistry_;
143  std::unique_ptr<VectorInputSource> const input_;
144  std::shared_ptr<ProcessConfiguration> processConfiguration_;
145  std::shared_ptr<ProcessContext> processContext_;
146  std::shared_ptr<StreamContext> streamContext_;
147  std::unique_ptr<EventPrincipal> eventPrincipal_;
148  std::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
149  std::shared_ptr<RunPrincipal> runPrincipal_;
151  std::optional<ServiceToken> serviceToken_;
152  std::unique_ptr<SecondaryEventProvider> provider_;
153  std::unique_ptr<CLHEP::RandPoissonQ> PoissonDistribution_;
154  std::unique_ptr<CLHEP::RandPoisson> PoissonDistr_OOT_;
155  CLHEP::HepRandomEngine* randomEngine_;
156 
157  //TH1F *h1f;
158  //TH1F *hprobFunction;
159  //TFile *probFileHisto;
160 
161  //playback info
162  bool playback_;
163 
164  // sequential reading
166  };
167 
168  template <typename T>
170  private:
171  std::vector<edm::SecondaryEventIDAndFileInfo>& ids_;
174 
175  public:
176  RecordEventID(std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T& eventOperator)
177  : ids_(ids), eventOperator_(eventOperator), eventCount(1) {}
178  bool operator()(EventPrincipal const& eventPrincipal, size_t fileNameHash) {
179  bool used = eventOperator_(eventPrincipal, eventCount);
180  if (used) {
181  ++eventCount;
182  ids_.emplace_back(eventPrincipal.id(), fileNameHash);
183  }
184  return used;
185  }
186  };
187 
198  template <typename T>
199  void PileUp::readPileUp(edm::EventID const& signal,
200  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
201  T eventOperator,
202  int const pileEventCnt,
203  StreamID const& streamID) {
204  // One reason PileUp is responsible for recording event IDs is
205  // that it is the one that knows how many events will be read.
206  ids.reserve(pileEventCnt);
207  RecordEventID<T> recorder(ids, eventOperator);
208  int read = 0;
209  CLHEP::HepRandomEngine* engine = (sequential_ ? nullptr : randomEngine(streamID));
210  read = input_->loopOverEvents(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder, engine, &signal);
211  if (read != pileEventCnt)
212  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt
213  << " requested.";
214  }
215 
216  template <typename T>
217  void PileUp::playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin,
218  std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end,
219  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
220  T eventOperator) {
221  //TrueNumInteractions.push_back( end - begin ) ;
222  RecordEventID<T> recorder(ids, eventOperator);
223  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
224  }
225 
226  template <typename T>
227  void PileUp::playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin,
228  std::vector<edm::EventID>::const_iterator end,
229  std::vector<edm::SecondaryEventIDAndFileInfo>& ids,
230  T eventOperator) {
231  //TrueNumInteractions.push_back( end - begin ) ;
232  RecordEventID<T> recorder(ids, eventOperator);
233  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
234  }
235 
236 } // namespace edm
237 
238 #endif
bool manage_OOT_
Definition: PileUp.h:127
size_t fileNameHash_
Definition: PileUp.h:141
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:152
std::shared_ptr< ProcessConfiguration > processConfiguration_
Definition: PileUp.h:144
double averageNumber() const
Definition: PileUp.h:70
unsigned int inputType_
Definition: PileUp.h:116
bool operator()(EventPrincipal const &eventPrincipal, size_t fileNameHash)
Definition: PileUp.h:178
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:39
void playPileUp(std::vector< edm::SecondaryEventIDAndFileInfo >::const_iterator begin, std::vector< edm::SecondaryEventIDAndFileInfo >::const_iterator end, std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T eventOperator)
Definition: PileUp.h:217
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Definition: PileUp.h:79
void beginStream(edm::StreamID)
Definition: PileUp.cc:209
std::optional< ServiceToken > serviceToken_
Definition: PileUp.h:151
bool fixed_
Definition: PileUp.h:125
Definition: config.py:1
PileUp(ParameterSet const &pset, const std::shared_ptr< PileUpConfig > &config, edm::ConsumesCollector iC, const bool mixingConfigFromDB)
Definition: PileUp.cc:69
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:359
int const intAverage_
Definition: PileUp.h:120
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:121
bool probFunctionDistribution_
Definition: PileUp.h:123
int maxBunch_cosmics_
Definition: PileUp.h:138
std::string Study_type_
Definition: PileUp.h:132
void playOldFormatPileUp(std::vector< edm::EventID >::const_iterator begin, std::vector< edm::EventID >::const_iterator end, std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T eventOperator)
Definition: PileUp.h:227
const bool playback_
Definition: PileUp.h:40
RecordEventID(std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T &eventOperator)
Definition: PileUp.h:176
bool histoDistribution_
Definition: PileUp.h:122
bool poisson_
Definition: PileUp.h:124
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:252
bool playback_
Definition: PileUp.h:162
PileupRandomNumberGenerator * randomGenerator_
Definition: PileUp.h:150
std::string sourcename_
Definition: PileUp.h:37
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:143
bool poisson_OOT_
Definition: PileUp.h:128
void input(unsigned int s)
Definition: PileUp.h:107
int intFixed_ITPU_
Definition: PileUp.h:135
void beginJob(eventsetup::ESRecordsToProxyIndices const &)
Definition: PileUp.cc:201
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:227
bool PU_Study_
Definition: PileUp.h:131
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
Definition: PileUp.cc:390
bool doPileUp(int BX)
Definition: PileUp.h:72
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:235
bool fixed_OOT_
Definition: PileUp.h:129
int minBunch_cosmics_
Definition: PileUp.h:137
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
Definition: PileUp.cc:367
std::unique_ptr< CLHEP::RandPoisson > PoissonDistr_OOT_
Definition: PileUp.h:154
CLHEP::HepRandomEngine * randomEngine_
Definition: PileUp.h:155
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:259
const unsigned int & input() const
Definition: PileUp.h:106
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:148
std::string type_
Definition: PileUp.h:117
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:246
std::vector< edm::SecondaryEventIDAndFileInfo > & ids_
Definition: PileUp.h:171
void setRandomEngine(StreamID)
Definition: PileUp.cc:383
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:142
bool poisson() const
Definition: PileUp.h:71
bool sequential_
Definition: PileUp.h:165
std::string Source_type_
Definition: PileUp.h:118
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:269
HLT enums.
bool none_
Definition: PileUp.h:126
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:375
std::unique_ptr< CLHEP::RandPoissonQ > PoissonDistribution_
Definition: PileUp.h:153
void endStream()
Definition: PileUp.cc:218
int intFixed_OOT_
Definition: PileUp.h:134
std::shared_ptr< ProcessContext > processContext_
Definition: PileUp.h:145
double averageNumber_
Definition: PileUp.h:38
Log< level::Warning, false > LogWarning
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:147
EventID const & id() const
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:149
long double T
std::shared_ptr< StreamContext > streamContext_
Definition: PileUp.h:146
double averageNumber_
Definition: PileUp.h:119
Definition: Run.h:45
void readPileUp(edm::EventID const &signal, std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T eventOperator, int const NumPU, StreamID const &)
Definition: PileUp.h:199
edm::ESGetToken< MixingModuleConfig, MixingRcd > configToken_
Definition: PileUp.h:140
PileUpConfig(std::string sourcename, double averageNumber, std::unique_ptr< TH1F > &histo, const bool playback)
Definition: PileUp.h:35