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::SecondaryEventIDAndFileInfo>& ids, T eventOperator, int const NumPU, StreamID const&);
37 
38  template<typename T>
39  void playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator);
40 
41  template<typename T>
42  void playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator);
43 
44  double averageNumber() const {return averageNumber_;}
45  bool poisson() const {return poisson_;}
46  bool doPileUp( int BX ) {
47  if(Source_type_ != "cosmics") {
48  return none_ ? false : averageNumber_>0.;
49  }
50  else {
51  return ( BX >= minBunch_cosmics_ && BX <= maxBunch_cosmics_);
52  }
53  }
54  void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
55  input_->dropUnwantedBranches(wantedBranches);
56  }
57  void beginJob();
58  void endJob();
59 
60  void beginRun(const edm::Run& run, const edm::EventSetup& setup);
62 
63  void endRun(const edm::Run& run, const edm::EventSetup& setup);
65 
67 
68  void reload(const edm::EventSetup & setup);
69 
70  void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions, StreamID const&);
71 
72  //template<typename T>
73  // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
74  // std::vector<edm::SecondaryEventIDAndFileInfo> &ids, T& eventOperator);
75 
76  const unsigned int & input()const{return inputType_;}
77  void input(unsigned int s){inputType_=s;}
78 
79  private:
80 
81  std::unique_ptr<CLHEP::RandPoissonQ> const& poissonDistribution(StreamID const& streamID);
82  std::unique_ptr<CLHEP::RandPoisson> const& poissonDistr_OOT(StreamID const& streamID);
83  CLHEP::HepRandomEngine* randomEngine(StreamID const& streamID);
84 
85  unsigned int inputType_;
89  int const intAverage_;
90  TH1F* histo_;
93  bool poisson_;
94  bool fixed_;
95  bool none_;
98  bool fixed_OOT_;
99 
100  bool PU_Study_;
102 
103 
106 
109 
111  std::shared_ptr<ProductRegistry> productRegistry_;
112  std::unique_ptr<VectorInputSource> const input_;
113  std::shared_ptr<ProcessConfiguration> processConfiguration_;
114  std::unique_ptr<EventPrincipal> eventPrincipal_;
115  std::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
116  std::shared_ptr<RunPrincipal> runPrincipal_;
117  std::unique_ptr<SecondaryEventProvider> provider_;
118  std::vector<std::unique_ptr<CLHEP::RandPoissonQ> > vPoissonDistribution_;
119  std::vector<std::unique_ptr<CLHEP::RandPoisson> > vPoissonDistr_OOT_;
120  std::vector<CLHEP::HepRandomEngine*> randomEngines_;
121 
122  TH1F *h1f;
125 
126  //playback info
127  bool playback_;
128 
129  // sequential reading
131 
132  // force reading pileup events from the same lumisection as the signal event
133  bool samelumi_;
134 
135  // read the seed for the histo and probability function cases
136  int seed_;
137  };
138 
139 
140 
141  template<typename T>
143  {
144  private:
145  std::vector<edm::SecondaryEventIDAndFileInfo>& ids_;
148  public:
149  RecordEventID(std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T& eventOperator)
150  : ids_(ids), eventOperator_(eventOperator), eventCount(0) {
151  }
152  void operator()(EventPrincipal const& eventPrincipal, size_t fileNameHash) {
153  ids_.emplace_back(eventPrincipal.id(), fileNameHash);
154  eventOperator_(eventPrincipal, ++eventCount);
155  }
156  };
157 
168  template<typename T>
169  void
170  PileUp::readPileUp(edm::EventID const& signal, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator,
171  int const pileEventCnt, StreamID const& streamID) {
172 
173  // One reason PileUp is responsible for recording event IDs is
174  // that it is the one that knows how many events will be read.
175  ids.reserve(pileEventCnt);
176  RecordEventID<T> recorder(ids,eventOperator);
177  int read;
178  if (samelumi_) {
179  const edm::LuminosityBlockID lumi(signal.run(), signal.luminosityBlock());
180  if (sequential_)
181  read = input_->loopSequentialWithID(*eventPrincipal_, fileNameHash_, lumi, pileEventCnt, recorder);
182  else
183  read = input_->loopRandomWithID(*eventPrincipal_, fileNameHash_, lumi, pileEventCnt, recorder, randomEngine(streamID));
184  } else {
185  if (sequential_) {
186  read = input_->loopSequential(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder);
187  } else {
188  read = input_->loopRandom(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder, randomEngine(streamID));
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  template<typename T>
196  void
197  PileUp::playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
198  //TrueNumInteractions.push_back( end - begin ) ;
199  RecordEventID<T> recorder(ids, eventOperator);
200  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
201  }
202 
203  template<typename T>
204  void
205  PileUp::playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
206  //TrueNumInteractions.push_back( end - begin ) ;
207  RecordEventID<T> recorder(ids, eventOperator);
208  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
209  }
210 
211 }
212 
213 
214 #endif
RunNumber_t run() const
Definition: EventID.h:39
bool manage_OOT_
Definition: PileUp.h:96
const unsigned int & input() const
Definition: PileUp.h:76
size_t fileNameHash_
Definition: PileUp.h:110
bool poisson() const
Definition: PileUp.h:45
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:117
void operator()(EventPrincipal const &eventPrincipal, size_t fileNameHash)
Definition: PileUp.h:152
TH1F * h1f
Definition: PileUp.h:122
std::shared_ptr< ProcessConfiguration > processConfiguration_
Definition: PileUp.h:113
unsigned int inputType_
Definition: PileUp.h:85
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:197
tuple lumi
Definition: fjr2json.py:35
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Definition: PileUp.h:54
EventID const & id() const
void endJob()
Definition: PileUp.cc:188
TH1F * histo_
Definition: PileUp.h:90
bool fixed_
Definition: PileUp.h:94
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:120
int const intAverage_
Definition: PileUp.h:89
bool probFunctionDistribution_
Definition: PileUp.h:92
int maxBunch_cosmics_
Definition: PileUp.h:108
std::string Study_type_
Definition: PileUp.h:101
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:205
RecordEventID(std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T &eventOperator)
Definition: PileUp.h:149
bool histoDistribution_
Definition: PileUp.h:91
bool poisson_
Definition: PileUp.h:93
void endLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:216
double averageNumber() const
Definition: PileUp.h:44
bool playback_
Definition: PileUp.h:127
std::vector< std::unique_ptr< CLHEP::RandPoissonQ > > vPoissonDistribution_
Definition: PileUp.h:118
bool samelumi_
Definition: PileUp.h:133
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:112
bool poisson_OOT_
Definition: PileUp.h:97
void beginJob()
Definition: PileUp.cc:181
void input(unsigned int s)
Definition: PileUp.h:77
int intFixed_ITPU_
Definition: PileUp.h:105
#define end
Definition: vmac.h:37
void beginRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:195
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:329
bool PU_Study_
Definition: PileUp.h:100
void CalculatePileup(int MinBunch, int MaxBunch, std::vector< int > &PileupSelection, std::vector< float > &TrueNumInteractions, StreamID const &)
Definition: PileUp.cc:375
bool doPileUp(int BX)
Definition: PileUp.h:46
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:202
bool fixed_OOT_
Definition: PileUp.h:98
int minBunch_cosmics_
Definition: PileUp.h:107
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:222
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:115
std::string type_
Definition: PileUp.h:86
void endRun(const edm::Run &run, const edm::EventSetup &setup)
Definition: PileUp.cc:211
std::vector< edm::SecondaryEventIDAndFileInfo > & ids_
Definition: PileUp.h:145
TFile * probFileHisto
Definition: PileUp.h:124
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:111
bool sequential_
Definition: PileUp.h:130
std::string Source_type_
Definition: PileUp.h:87
std::unique_ptr< CLHEP::RandPoisson > const & poissonDistr_OOT(StreamID const &streamID)
Definition: PileUp.cc:344
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:231
#define begin
Definition: vmac.h:30
int seed_
Definition: PileUp.h:136
bool none_
Definition: PileUp.h:95
TH1F * hprobFunction
Definition: PileUp.h:123
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:359
std::vector< std::unique_ptr< CLHEP::RandPoisson > > vPoissonDistr_OOT_
Definition: PileUp.h:119
int intFixed_OOT_
Definition: PileUp.h:104
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:114
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:116
long double T
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
double averageNumber_
Definition: PileUp.h:88
Definition: Run.h:41
void readPileUp(edm::EventID const &signal, std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T eventOperator, int const NumPU, StreamID const &)
Definition: PileUp.h:170