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