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