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