CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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>
12 
13 #include "TRandom.h"
14 #include "TFile.h"
15 #include "TH1F.h"
16 
17 class TFile;
18 class TH1F;
19 
20 namespace CLHEP {
21  class RandPoissonQ;
22  class RandPoisson;
23  class HepRandomEngine;
24 }
25 
26 namespace edm {
27  class SecondaryEventProvider;
28  class StreamID;
29 
30  class PileUp {
31  public:
32  explicit PileUp(ParameterSet const& pset, std::string sourcename, double averageNumber, TH1F* const histo, const bool playback);
33  ~PileUp();
34 
35  template<typename T>
36  void readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int NumPU, StreamID const&);
37 
38  template<typename T>
39  void playPileUp(const std::vector<edm::EventID> &ids, T eventOperator);
40 
41  double averageNumber() const {return averageNumber_;}
42  bool poisson() const {return poisson_;}
43  bool doPileUp( int BX ) {
44  if(Source_type_ != "cosmics") {
45  return none_ ? false : averageNumber_>0.;
46  }
47  else {
48  return ( BX >= minBunch_cosmics_ && BX <= maxBunch_cosmics_);
49  }
50  }
51  void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
52  input_->dropUnwantedBranches(wantedBranches);
53  }
54  void beginJob();
55  void endJob();
56 
57  void beginRun(const edm::Run& run, const edm::EventSetup& setup);
59 
60  void endRun(const edm::Run& run, const edm::EventSetup& setup);
62 
64 
65  void reload(const edm::EventSetup & setup);
66 
67  void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions, StreamID const&);
68 
69  //template<typename T>
70  // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
71  // std::vector<edm::EventID> &ids, T& eventOperator);
72 
73  const unsigned int & input()const{return inputType_;}
74  void input(unsigned int s){inputType_=s;}
75 
76  private:
77 
78  std::unique_ptr<CLHEP::RandPoissonQ> const& poissonDistribution(StreamID const& streamID);
79  std::unique_ptr<CLHEP::RandPoisson> const& poissonDistr_OOT(StreamID const& streamID);
80  CLHEP::HepRandomEngine* randomEngine(StreamID const& streamID);
81 
82  unsigned int inputType_;
86  int const intAverage_;
87  TH1F* histo_;
90  bool poisson_;
91  bool fixed_;
92  bool none_;
95  bool fixed_OOT_;
96 
97  bool PU_Study_;
99 
100 
103 
106 
107  std::shared_ptr<ProductRegistry> productRegistry_;
108  std::unique_ptr<VectorInputSource> const input_;
109  std::shared_ptr<ProcessConfiguration> processConfiguration_;
110  std::unique_ptr<EventPrincipal> eventPrincipal_;
111  std::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
112  std::shared_ptr<RunPrincipal> runPrincipal_;
113  std::unique_ptr<SecondaryEventProvider> provider_;
114  std::vector<std::unique_ptr<CLHEP::RandPoissonQ> > vPoissonDistribution_;
115  std::vector<std::unique_ptr<CLHEP::RandPoisson> > vPoissonDistr_OOT_;
116  std::vector<CLHEP::HepRandomEngine*> randomEngines_;
117 
118  TH1F *h1f;
121 
122  //playback info
123  bool playback_;
124 
125  // sequential reading
127 
128  // force reading pileup events from the same lumisection as the signal event
129  bool samelumi_;
130 
131  // read the seed for the histo and probability function cases
132  int seed_;
133  };
134 
135 
136 
137  template<typename T>
139  {
140  private:
141  std::vector<edm::EventID>& ids_;
144  public:
145  RecordEventID(std::vector<edm::EventID>& ids, T& eventOperator)
146  : ids_(ids), eventOperator_(eventOperator), eventCount( 0 ) {}
147  void operator()(EventPrincipal const& eventPrincipal) {
148  ids_.push_back(eventPrincipal.id());
149  eventOperator_(eventPrincipal, ++eventCount);
150  }
151  };
152 
153 
154  template<typename T>
156  {
157  private:
160  public:
161  PassEventID(T& eventOperator)
162  : eventOperator_(eventOperator), eventCount( 0 ) {}
163  void operator()(EventPrincipal const& eventPrincipal) {
164  eventOperator_(eventPrincipal, ++eventCount);
165  }
166  };
167 
168 
179  template<typename T>
180  void
181  PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator,
182  const int pileEventCnt, StreamID const& streamID) {
183 
184  // One reason PileUp is responsible for recording event IDs is
185  // that it is the one that knows how many events will be read.
186  ids.reserve(pileEventCnt);
187  RecordEventID<T> recorder(ids,eventOperator);
188  int read;
189  if (samelumi_) {
190  const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
191  if (sequential_)
192  read = input_->loopSequentialWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
193  else
194  read = input_->loopRandomWithID(*eventPrincipal_, lumi, pileEventCnt, recorder, randomEngine(streamID));
195  } else {
196  if (sequential_) {
197  read = input_->loopSequential(*eventPrincipal_, pileEventCnt, recorder);
198  } else {
199  read = input_->loopRandom(*eventPrincipal_, pileEventCnt, recorder, randomEngine(streamID));
200  }
201  }
202  if (read != pileEventCnt)
203  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
204  }
205 
206 
207 
208  template<typename T>
209  void
210  PileUp::playPileUp(const std::vector<edm::EventID> &ids, T eventOperator) {
211  //TrueNumInteractions.push_back( ids.size() ) ;
212  PassEventID<T> recorder(eventOperator);
213  input_->loopSpecified(*eventPrincipal_,ids,recorder);
214  }
215 
216 
217 
220  /* template<typename T>
221  void recordEventForPlayback(EventPrincipal const& eventPrincipal,
222  std::vector<edm::EventID> &ids, T& eventOperator)
223  {
224  ids.push_back(eventPrincipal.id());
225  eventOperator(eventPrincipal);
226  }
227  */
228 }
229 
230 
231 #endif
void playPileUp(const std::vector< edm::EventID > &ids, T eventOperator)
Definition: PileUp.h:210
RunNumber_t run() const
Definition: EventID.h:39
bool manage_OOT_
Definition: PileUp.h:93
const unsigned int & input() const
Definition: PileUp.h:73
bool poisson() const
Definition: PileUp.h:42
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:113
std::vector< edm::EventID > & ids_
Definition: PileUp.h:141
TH1F * h1f
Definition: PileUp.h:118
std::shared_ptr< ProcessConfiguration > processConfiguration_
Definition: PileUp.h:109
unsigned int inputType_
Definition: PileUp.h:82
tuple lumi
Definition: fjr2json.py:35
T & eventOperator_
Definition: PileUp.h:158
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Definition: PileUp.h:51
void readPileUp(edm::EventID const &signal, std::vector< edm::EventID > &ids, T eventOperator, const int NumPU, StreamID const &)
Definition: PileUp.h:181
EventID const & id() const
void endJob()
Definition: PileUp.cc:187
PassEventID(T &eventOperator)
Definition: PileUp.h:161
TH1F * histo_
Definition: PileUp.h:87
bool fixed_
Definition: PileUp.h:91
PileUp(ParameterSet const &pset, std::string sourcename, double averageNumber, TH1F *const histo, const bool playback)
Definition: PileUp.cc:33
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
std::vector< CLHEP::HepRandomEngine * > randomEngines_
Definition: PileUp.h:116
int const intAverage_
Definition: PileUp.h:86
bool probFunctionDistribution_
Definition: PileUp.h:89
int maxBunch_cosmics_
Definition: PileUp.h:105
std::string Study_type_
Definition: PileUp.h:98
bool histoDistribution_
Definition: PileUp.h:88
bool poisson_
Definition: PileUp.h:90
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:215
double averageNumber() const
Definition: PileUp.h:41
bool playback_
Definition: PileUp.h:123
std::vector< std::unique_ptr< CLHEP::RandPoissonQ > > vPoissonDistribution_
Definition: PileUp.h:114
bool samelumi_
Definition: PileUp.h:129
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:108
bool poisson_OOT_
Definition: PileUp.h:94
void beginJob()
Definition: PileUp.cc:180
void input(unsigned int s)
Definition: PileUp.h:74
int intFixed_ITPU_
Definition: PileUp.h:102
RecordEventID(std::vector< edm::EventID > &ids, T &eventOperator)
Definition: PileUp.h:145
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:194
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:328
bool PU_Study_
Definition: PileUp.h:97
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
Definition: PileUp.cc:374
bool doPileUp(int BX)
Definition: PileUp.h:43
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:201
bool fixed_OOT_
Definition: PileUp.h:95
int minBunch_cosmics_
Definition: PileUp.h:104
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:221
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:111
std::string type_
Definition: PileUp.h:83
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:210
TFile * probFileHisto
Definition: PileUp.h:120
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:107
bool sequential_
Definition: PileUp.h:126
std::string Source_type_
Definition: PileUp.h:84
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
Definition: PileUp.cc:343
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:230
int seed_
Definition: PileUp.h:132
bool none_
Definition: PileUp.h:92
TH1F * hprobFunction
Definition: PileUp.h:119
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:358
tuple playback
Definition: Playback_cff.py:20
std::vector< std::unique_ptr< CLHEP::RandPoisson > > vPoissonDistr_OOT_
Definition: PileUp.h:115
int intFixed_OOT_
Definition: PileUp.h:101
void operator()(EventPrincipal const &eventPrincipal)
Definition: PileUp.h:163
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:110
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:112
long double T
void operator()(EventPrincipal const &eventPrincipal)
Definition: PileUp.h:147
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
double averageNumber_
Definition: PileUp.h:85
Definition: Run.h:41