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  // read the seed for the histo and probability function cases
133  int seed_;
134  };
135 
136 
137 
138  template<typename T>
140  {
141  private:
142  std::vector<edm::SecondaryEventIDAndFileInfo>& ids_;
145  public:
146  RecordEventID(std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T& eventOperator)
147  : ids_(ids), eventOperator_(eventOperator), eventCount(0) {
148  }
149  void operator()(EventPrincipal const& eventPrincipal, size_t fileNameHash) {
150  ids_.emplace_back(eventPrincipal.id(), fileNameHash);
151  eventOperator_(eventPrincipal, ++eventCount);
152  }
153  };
154 
165  template<typename T>
166  void
167  PileUp::readPileUp(edm::EventID const& signal, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator,
168  int const pileEventCnt, StreamID const& streamID) {
169 
170  // One reason PileUp is responsible for recording event IDs is
171  // that it is the one that knows how many events will be read.
172  ids.reserve(pileEventCnt);
173  RecordEventID<T> recorder(ids,eventOperator);
174  int read = 0;
175  CLHEP::HepRandomEngine* engine = (sequential_ ? nullptr : randomEngine(streamID));
176  read = input_->loopOverEvents(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder, engine, &signal);
177  if (read != pileEventCnt)
178  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
179  }
180 
181  template<typename T>
182  void
183  PileUp::playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
184  //TrueNumInteractions.push_back( end - begin ) ;
185  RecordEventID<T> recorder(ids, eventOperator);
186  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
187  }
188 
189  template<typename T>
190  void
191  PileUp::playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
192  //TrueNumInteractions.push_back( end - begin ) ;
193  RecordEventID<T> recorder(ids, eventOperator);
194  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
195  }
196 
197 }
198 
199 
200 #endif
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:149
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:183
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:180
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
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:191
RecordEventID(std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T &eventOperator)
Definition: PileUp.h:146
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:208
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
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:112
bool poisson_OOT_
Definition: PileUp.h:97
void beginJob()
Definition: PileUp.cc:173
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:187
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:321
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:367
bool doPileUp(int BX)
Definition: PileUp.h:46
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:194
bool fixed_OOT_
Definition: PileUp.h:98
int minBunch_cosmics_
Definition: PileUp.h:107
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:214
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:203
std::vector< edm::SecondaryEventIDAndFileInfo > & ids_
Definition: PileUp.h:142
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:336
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:223
#define begin
Definition: vmac.h:30
int seed_
Definition: PileUp.h:133
bool none_
Definition: PileUp.h:95
TH1F * hprobFunction
Definition: PileUp.h:123
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:351
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:167