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>
7 //#include <boost/bind.hpp>
13 
14 #include "CLHEP/Random/RandPoissonQ.h"
15 #include "CLHEP/Random/RandFlat.h"
16 
17 #include "boost/shared_ptr.hpp"
18 
19 #include "TRandom.h"
20 #include "TFile.h"
21 #include "TH1F.h"
22 
23 class TFile;
24 class TH1F;
25 
26 namespace CLHEP {
27  class RandPoissonQ;
28  class RandPoisson;
29 }
30 
31 
32 
33 namespace edm {
34  class SecondaryEventProvider;
35 
36  class PileUp {
37  public:
38  explicit PileUp(ParameterSet const& pset, double averageNumber, TH1F* const histo, const bool playback);
39  ~PileUp();
40 
41  template<typename T>
42  void readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int NumPU );
43 
44  template<typename T>
45  void playPileUp(const std::vector<edm::EventID> &ids, T eventOperator);
46 
47  double averageNumber() const {return averageNumber_;}
48  bool poisson() const {return poisson_;}
49  bool doPileUp() {return none_ ? false : averageNumber_>0.;}
50  void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
51  input_->dropUnwantedBranches(wantedBranches);
52  }
53  void beginJob();
54  void endJob();
55 
56  void beginRun(const edm::Run& run, const edm::EventSetup& setup);
58 
59  void endRun(const edm::Run& run, const edm::EventSetup& setup);
61 
63 
64  void reload(const edm::EventSetup & setup);
65 
66  void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions);
67 
68  //template<typename T>
69  // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
70  // std::vector<edm::EventID> &ids, T& eventOperator);
71 
72  const unsigned int & input()const{return inputType_;}
73  void input(unsigned int s){inputType_=s;}
74 
75  private:
76  unsigned int inputType_;
79  int const intAverage_;
80  TH1F* histo_;
83  bool poisson_;
84  bool fixed_;
85  bool none_;
88  bool fixed_OOT_;
89 
90  bool PU_Study_;
92 
95 
96  boost::shared_ptr<ProductRegistry> productRegistry_;
97  std::unique_ptr<VectorInputSource> const input_;
98  boost::shared_ptr<ProcessConfiguration> processConfiguration_;
99  std::unique_ptr<EventPrincipal> eventPrincipal_;
100  boost::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
101  boost::shared_ptr<RunPrincipal> runPrincipal_;
102  std::unique_ptr<SecondaryEventProvider> provider_;
103  std::unique_ptr<CLHEP::RandPoissonQ> poissonDistribution_;
104  std::unique_ptr<CLHEP::RandPoisson> poissonDistr_OOT_;
105 
106 
107  TH1F *h1f;
110 
111  //playback info
112  bool playback_;
113 
114  // sequential reading
116 
117  // force reading pileup events from the same lumisection as the signal event
118  bool samelumi_;
119 
120  // read the seed for the histo and probability function cases
121  int seed_;
122  };
123 
124 
125 
126  template<typename T>
128  {
129  private:
130  std::vector<edm::EventID>& ids_;
133  public:
134  RecordEventID(std::vector<edm::EventID>& ids, T& eventOperator)
135  : ids_(ids), eventOperator_(eventOperator), eventCount( 0 ) {}
136  void operator()(EventPrincipal const& eventPrincipal) {
137  ids_.push_back(eventPrincipal.id());
138  eventOperator_(eventPrincipal, ++eventCount);
139  }
140  };
141 
142 
153  template<typename T>
154  void
155  PileUp::readPileUp(edm::EventID const & signal, std::vector<edm::EventID> &ids, T eventOperator, const int pileEventCnt) {
156 
157  // One reason PileUp is responsible for recording event IDs is
158  // that it is the one that knows how many events will be read.
159  ids.reserve(pileEventCnt);
160  RecordEventID<T> recorder(ids,eventOperator);
161  int read;
162  if (samelumi_) {
163  const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
164  if (sequential_)
165  read = input_->loopSequentialWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
166  else
167  read = input_->loopRandomWithID(*eventPrincipal_, lumi, pileEventCnt, recorder);
168  } else {
169  if (sequential_) {
170  // boost::bind creates a functor from recordEventForPlayback
171  // so that recordEventForPlayback can insert itself before
172  // the original eventOperator.
173 
174  read = input_->loopSequential(*eventPrincipal_, pileEventCnt, recorder);
175  //boost::bind(&PileUp::recordEventForPlayback<T>,
176  // boost::ref(*this), _1, boost::ref(ids),
177  // boost::ref(eventOperator))
178  // );
179 
180  } else {
181  read = input_->loopRandom(*eventPrincipal_, pileEventCnt, recorder);
182  // boost::bind(&PileUp::recordEventForPlayback<T>,
183  // boost::ref(*this), _1, boost::ref(ids),
184  // boost::ref(eventOperator))
185  // );
186  }
187  }
188  if (read != pileEventCnt)
189  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
190  }
191 
192 
193 
194  template<typename T>
195  void
196  PileUp::playPileUp(const std::vector<edm::EventID> &ids, T eventOperator) {
197  //TrueNumInteractions.push_back( ids.size() ) ;
198  input_->loopSpecified(*eventPrincipal_,ids,eventOperator);
199  }
200 
201 
202 
205  /* template<typename T>
206  void recordEventForPlayback(EventPrincipal const& eventPrincipal,
207  std::vector<edm::EventID> &ids, T& eventOperator)
208  {
209  ids.push_back(eventPrincipal.id());
210  eventOperator(eventPrincipal);
211  }
212  */
213 }
214 
215 
216 #endif
void playPileUp(const std::vector< edm::EventID > &ids, T eventOperator)
Definition: PileUp.h:196
RunNumber_t run() const
Definition: EventID.h:42
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions)
Definition: PileUp.cc:311
boost::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:101
bool manage_OOT_
Definition: PileUp.h:86
const unsigned int & input() const
Definition: PileUp.h:72
bool poisson() const
Definition: PileUp.h:48
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:102
std::vector< edm::EventID > & ids_
Definition: PileUp.h:130
TH1F * h1f
Definition: PileUp.h:107
unsigned int inputType_
Definition: PileUp.h:76
std::unique_ptr< CLHEP::RandPoisson > poissonDistr_OOT_
Definition: PileUp.h:104
tuple lumi
Definition: fjr2json.py:35
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Definition: PileUp.h:50
EventID const & id() const
void readPileUp(edm::EventID const &signal, std::vector< edm::EventID > &ids, T eventOperator, const int NumPU)
Definition: PileUp.h:155
void endJob()
Definition: PileUp.cc:165
TH1F * histo_
Definition: PileUp.h:80
bool fixed_
Definition: PileUp.h:84
PileUp(ParameterSet const &pset, double averageNumber, TH1F *const histo, const bool playback)
Definition: PileUp.cc:29
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:43
int const intAverage_
Definition: PileUp.h:79
bool probFunctionDistribution_
Definition: PileUp.h:82
std::string Study_type_
Definition: PileUp.h:91
boost::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:100
bool histoDistribution_
Definition: PileUp.h:81
bool poisson_
Definition: PileUp.h:83
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:193
double averageNumber() const
Definition: PileUp.h:47
bool playback_
Definition: PileUp.h:112
bool samelumi_
Definition: PileUp.h:118
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:97
bool poisson_OOT_
Definition: PileUp.h:87
void beginJob()
Definition: PileUp.cc:158
void input(unsigned int s)
Definition: PileUp.h:73
int intFixed_ITPU_
Definition: PileUp.h:94
RecordEventID(std::vector< edm::EventID > &ids, T &eventOperator)
Definition: PileUp.h:134
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:172
bool PU_Study_
Definition: PileUp.h:90
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:179
bool fixed_OOT_
Definition: PileUp.h:88
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:199
std::string type_
Definition: PileUp.h:77
boost::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:96
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:188
TFile * probFileHisto
Definition: PileUp.h:109
boost::shared_ptr< ProcessConfiguration > processConfiguration_
Definition: PileUp.h:98
bool sequential_
Definition: PileUp.h:115
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:208
std::unique_ptr< CLHEP::RandPoissonQ > poissonDistribution_
Definition: PileUp.h:103
int seed_
Definition: PileUp.h:121
bool none_
Definition: PileUp.h:85
TH1F * hprobFunction
Definition: PileUp.h:108
tuple playback
Definition: Playback_cff.py:20
int intFixed_OOT_
Definition: PileUp.h:93
bool doPileUp()
Definition: PileUp.h:49
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:99
long double T
void operator()(EventPrincipal const &eventPrincipal)
Definition: PileUp.h:136
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
double averageNumber_
Definition: PileUp.h:78
Definition: Run.h:41