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