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 
133 
134 
135  template<typename T>
137  {
138  private:
139  std::vector<edm::SecondaryEventIDAndFileInfo>& ids_;
142  public:
143  RecordEventID(std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T& eventOperator)
144  : ids_(ids), eventOperator_(eventOperator), eventCount(0) {
145  }
146  void operator()(EventPrincipal const& eventPrincipal, size_t fileNameHash) {
147  ids_.emplace_back(eventPrincipal.id(), fileNameHash);
148  eventOperator_(eventPrincipal, ++eventCount);
149  }
150  };
151 
162  template<typename T>
163  void
164  PileUp::readPileUp(edm::EventID const& signal, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator,
165  int const pileEventCnt, StreamID const& streamID) {
166 
167  // One reason PileUp is responsible for recording event IDs is
168  // that it is the one that knows how many events will be read.
169  ids.reserve(pileEventCnt);
170  RecordEventID<T> recorder(ids,eventOperator);
171  int read = 0;
172  CLHEP::HepRandomEngine* engine = (sequential_ ? nullptr : randomEngine(streamID));
173  read = input_->loopOverEvents(*eventPrincipal_, fileNameHash_, pileEventCnt, recorder, engine, &signal);
174  if (read != pileEventCnt)
175  edm::LogWarning("PileUp") << "Could not read enough pileup events: only " << read << " out of " << pileEventCnt << " requested.";
176  }
177 
178  template<typename T>
179  void
180  PileUp::playPileUp(std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator begin, std::vector<edm::SecondaryEventIDAndFileInfo>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
181  //TrueNumInteractions.push_back( end - begin ) ;
182  RecordEventID<T> recorder(ids, eventOperator);
183  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
184  }
185 
186  template<typename T>
187  void
188  PileUp::playOldFormatPileUp(std::vector<edm::EventID>::const_iterator begin, std::vector<edm::EventID>::const_iterator end, std::vector<edm::SecondaryEventIDAndFileInfo>& ids, T eventOperator) {
189  //TrueNumInteractions.push_back( end - begin ) ;
190  RecordEventID<T> recorder(ids, eventOperator);
191  input_->loopSpecified(*eventPrincipal_, fileNameHash_, begin, end, recorder);
192  }
193 
194 }
195 
196 
197 #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:146
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:180
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:194
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:64
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:188
RecordEventID(std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T &eventOperator)
Definition: PileUp.h:143
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:222
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:187
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:201
std::unique_ptr< CLHEP::RandPoissonQ > const & poissonDistribution(StreamID const &streamID)
Definition: PileUp.cc:335
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:381
bool doPileUp(int BX)
Definition: PileUp.h:46
void beginLuminosityBlock(const edm::LuminosityBlock &lumi, const edm::EventSetup &setup)
Definition: PileUp.cc:208
bool fixed_OOT_
Definition: PileUp.h:98
int minBunch_cosmics_
Definition: PileUp.h:107
void setupPileUpEvent(const edm::EventSetup &setup)
Definition: PileUp.cc:228
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:217
std::vector< edm::SecondaryEventIDAndFileInfo > & ids_
Definition: PileUp.h:139
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:350
void reload(const edm::EventSetup &setup)
Definition: PileUp.cc:237
#define begin
Definition: vmac.h:30
bool none_
Definition: PileUp.h:95
TH1F * hprobFunction
Definition: PileUp.h:123
CLHEP::HepRandomEngine * randomEngine(StreamID const &streamID)
Definition: PileUp.cc:365
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:43
void readPileUp(edm::EventID const &signal, std::vector< edm::SecondaryEventIDAndFileInfo > &ids, T eventOperator, int const NumPU, StreamID const &)
Definition: PileUp.h:164