CMS 3D CMS Logo

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  class ProcessContext;
30 
31  struct PileUpConfig {
32  PileUpConfig(std::string sourcename, double averageNumber, std::unique_ptr<TH1F>& histo, const bool playback)
33  : sourcename_(sourcename), averageNumber_(averageNumber), histo_(histo.release()), playback_(playback) {}
36  std::shared_ptr<TH1F> histo_;
37  const bool playback_;
38  };
39 
40  class PileUp {
41  public:
42  explicit PileUp(ParameterSet const& pset, const std::shared_ptr<PileUpConfig>& config);
43  ~PileUp();
44 
45  template<typename T>
46  void readPileUp(edm::EventID const& signal, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator, int const NumPU, StreamID const&);
47 
48  template<typename T>
49  void playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator);
50 
51  template<typename T>
52  void playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator);
53 
54  double averageNumber() const {return averageNumber_;}
55  bool poisson() const {return poisson_;}
56  bool doPileUp( int BX ) {
57  if(Source_type_ != "cosmics") {
58  return none_ ? false : averageNumber_>0.;
59  }
60  else {
61  return ( BX >= minBunch_cosmics_ && BX <= maxBunch_cosmics_);
62  }
63  }
64  void dropUnwantedBranches(std::vector<std::string> const& wantedBranches) {
65  input_->dropUnwantedBranches(wantedBranches);
66  }
67  void beginStream(edm::StreamID);
68  void endStream();
69 
70  void beginRun(const edm::Run& run, const edm::EventSetup& setup);
71  void beginLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& setup);
72 
73  void endRun(const edm::Run& run, const edm::EventSetup& setup);
74  void endLuminosityBlock(const edm::LuminosityBlock& lumi, const edm::EventSetup& setup);
75 
76  void setupPileUpEvent(const edm::EventSetup& setup);
77 
78  void reload(const edm::EventSetup & setup);
79 
80  void CalculatePileup(int MinBunch, int MaxBunch, std::vector<int>& PileupSelection, std::vector<float>& TrueNumInteractions, StreamID const&);
81 
82  //template<typename T>
83  // void recordEventForPlayback(EventPrincipal const& eventPrincipal,
84  // std::vector<edm::SecondaryEventIDAndFileInfo> &ids, T& eventOperator);
85 
86  const unsigned int & input()const{return inputType_;}
87  void input(unsigned int s){inputType_=s;}
88 
89  private:
90 
91  std::unique_ptr<CLHEP::RandPoissonQ> const& poissonDistribution(StreamID const& streamID);
92  std::unique_ptr<CLHEP::RandPoisson> const& poissonDistr_OOT(StreamID const& streamID);
93  CLHEP::HepRandomEngine* randomEngine(StreamID const& streamID);
94 
95  unsigned int inputType_;
99  int const intAverage_;
100  std::shared_ptr<TH1F> histo_;
103  bool poisson_;
104  bool fixed_;
105  bool none_;
109 
110  bool PU_Study_;
112 
113 
116 
119 
121  std::shared_ptr<ProductRegistry> productRegistry_;
122  std::unique_ptr<VectorInputSource> const input_;
123  std::shared_ptr<ProcessConfiguration> processConfiguration_;
124  std::shared_ptr<ProcessContext> processContext_;
125  std::shared_ptr<StreamContext> streamContext_;
126  std::unique_ptr<EventPrincipal> eventPrincipal_;
127  std::shared_ptr<LuminosityBlockPrincipal> lumiPrincipal_;
128  std::shared_ptr<RunPrincipal> runPrincipal_;
129  std::unique_ptr<SecondaryEventProvider> provider_;
130  std::unique_ptr<CLHEP::RandPoissonQ> PoissonDistribution_;
131  std::unique_ptr<CLHEP::RandPoisson> PoissonDistr_OOT_;
132  CLHEP::HepRandomEngine* randomEngine_;
133 
134  //TH1F *h1f;
135  //TH1F *hprobFunction;
136  //TFile *probFileHisto;
137 
138  //playback info
139  bool playback_;
140 
141  // sequential reading
143  };
144 
145 
146 
147  template<typename T>
149  {
150  private:
151  std::vector<edm::SecondaryEventIDAndFileInfo>& ids_;
154  public:
155  RecordEventID(std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T& eventOperator)
156  : ids_(ids), eventOperator_(eventOperator), eventCount(1) {
157  }
158  bool operator()(EventPrincipal const& eventPrincipal, size_t fileNameHash) {
159  bool used = eventOperator_(eventPrincipal, eventCount);
160  if(used) {
161  ++eventCount;
162  ids_.emplace_back(eventPrincipal.id(), fileNameHash);
163  }
164  return used;
165  }
166  };
167 
178  template<typename T>
179  void
180  PileUp::readPileUp(edm::EventID const& signal, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator,
181  int const pileEventCnt, StreamID const& streamID) {
182 
183  // One reason PileUp is responsible for recording event IDs is
184  // that it is the one that knows how many events will be read.
185  ids.reserve(pileEventCnt);
186  RecordEventID<T> recorder(ids,eventOperator);
187  int read = 0;
188  CLHEP::HepRandomEngine* engine = (sequential_ ? nullptr : randomEngine(streamID));
189  read = input_->loopOverEvents(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder, engine, &signal);
190  if (read != pileEventCnt)
191  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
192  }
193 
194  template<typename T>
195  void
196  PileUp::playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
197  //TrueNumInteractions.push_back( end - begin ) ;
198  RecordEventID<T> recorder(ids, eventOperator);
199  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
200  }
201 
202  template<typename T>
203  void
204  PileUp::playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
205  //TrueNumInteractions.push_back( end - begin ) ;
206  RecordEventID<T> recorder(ids, eventOperator);
207  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
208  }
209 
210 }
211 
212 
213 #endif
CLHEP::HepRandomEngine * randomEngine
Definition: Dummies.cc:7
bool manage_OOT_
Definition: PileUp.h:106
const unsigned int & input() const
Definition: PileUp.h:86
size_t fileNameHash_
Definition: PileUp.h:120
bool poisson() const
Definition: PileUp.h:55
std::unique_ptr< SecondaryEventProvider > provider_
Definition: PileUp.h:129
std::shared_ptr< ProcessConfiguration > processConfiguration_
Definition: PileUp.h:123
unsigned int inputType_
Definition: PileUp.h:95
bool operator()(EventPrincipal const &eventPrincipal, size_t fileNameHash)
Definition: PileUp.h:158
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:36
void dropUnwantedBranches(std::vector< std::string > const &wantedBranches)
Definition: PileUp.h:64
EventID const & id() const
def setup(process, global_tag, zero_tesla=False)
Definition: GeneralSetup.py:2
bool fixed_
Definition: PileUp.h:104
Definition: config.py:1
int const intAverage_
Definition: PileUp.h:99
std::shared_ptr< TH1F > histo_
Definition: PileUp.h:100
bool probFunctionDistribution_
Definition: PileUp.h:102
int maxBunch_cosmics_
Definition: PileUp.h:118
std::string Study_type_
Definition: PileUp.h:111
const bool playback_
Definition: PileUp.h:37
RecordEventID(std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T &eventOperator)
Definition: PileUp.h:155
bool histoDistribution_
Definition: PileUp.h:101
bool poisson_
Definition: PileUp.h:103
double averageNumber() const
Definition: PileUp.h:54
bool playback_
Definition: PileUp.h:139
std::string sourcename_
Definition: PileUp.h:34
std::unique_ptr< VectorInputSource > const input_
Definition: PileUp.h:122
bool poisson_OOT_
Definition: PileUp.h:107
void input(unsigned int s)
Definition: PileUp.h:87
int intFixed_ITPU_
Definition: PileUp.h:115
#define end
Definition: vmac.h:39
bool PU_Study_
Definition: PileUp.h:110
bool doPileUp(int BX)
Definition: PileUp.h:56
bool fixed_OOT_
Definition: PileUp.h:108
int minBunch_cosmics_
Definition: PileUp.h:117
std::unique_ptr< CLHEP::RandPoisson > PoissonDistr_OOT_
Definition: PileUp.h:131
CLHEP::HepRandomEngine * randomEngine_
Definition: PileUp.h:132
std::shared_ptr< LuminosityBlockPrincipal > lumiPrincipal_
Definition: PileUp.h:127
std::string type_
Definition: PileUp.h:96
std::vector< edm::SecondaryEventIDAndFileInfo > & ids_
Definition: PileUp.h:151
std::shared_ptr< ProductRegistry > productRegistry_
Definition: PileUp.h:121
bool sequential_
Definition: PileUp.h:142
std::string Source_type_
Definition: PileUp.h:97
#define begin
Definition: vmac.h:32
HLT enums.
bool none_
Definition: PileUp.h:105
std::unique_ptr< CLHEP::RandPoissonQ > PoissonDistribution_
Definition: PileUp.h:130
int intFixed_OOT_
Definition: PileUp.h:114
std::shared_ptr< ProcessContext > processContext_
Definition: PileUp.h:124
double averageNumber_
Definition: PileUp.h:35
std::unique_ptr< EventPrincipal > eventPrincipal_
Definition: PileUp.h:126
std::shared_ptr< RunPrincipal > runPrincipal_
Definition: PileUp.h:128
long double T
std::shared_ptr< StreamContext > streamContext_
Definition: PileUp.h:125
double averageNumber_
Definition: PileUp.h:98
Definition: Run.h:45
PileUpConfig(std::string sourcename, double averageNumber, std::unique_ptr< TH1F > &histo, const bool playback)
Definition: PileUp.h:32