CMS 3D CMS Logo

MixCollectionValidation.cc
Go to the documentation of this file.
1 // system include files
3 
4 // user include files
7 
10 
12 
15 #include "TFile.h"
16 
17 #include <memory>
18 #include <utility>
19 
20 using namespace edm;
21 
23  : minbunch_(iConfig.getParameter<int>("minBunch")),
24  maxbunch_(iConfig.getParameter<int>("maxBunch")),
25  verbose_(iConfig.getUntrackedParameter<bool>("verbose", false)),
26  nbin_(maxbunch_ - minbunch_ + 1) {
27  // Histograms will be defined according to the configuration
28  ParameterSet mixObjextsSet_ = iConfig.getParameter<ParameterSet>("mixObjects");
29 }
30 
32  // do anything here that needs to be done at desctruction time
33  // (e.g. close files, deallocate resources etc.)
34 }
35 
37  edm::Run const &iRun,
38  edm::EventSetup const & /* iSetup */) {
39  iBooker.setCurrentFolder("MixingV/Mixing");
40 
41  std::vector<std::string> names = mixObjextsSet_.getParameterNames();
42 
43  for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
45  if (!pset.exists("type"))
46  continue; // to allow replacement by empty pset
47  std::string object = pset.getParameter<std::string>("type");
48  std::vector<InputTag> tags = pset.getParameter<std::vector<InputTag>>("input");
49 
50  if (object == "HepMCProduct") {
51  std::string title = "Log10 Number of GenParticle in " + object;
52  std::string name = "NumberOf" + object;
53  nrHepMCProductH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
54 
55  HepMCProductTags_ = tags;
56  if (!HepMCProductTags_.empty()) {
58  consumes<CrossingFrame<HepMCProduct>>(edm::InputTag("mix", HepMCProductTags_[0].label()));
59  }
60  } else if (object == "SimTrack") {
61  std::string title = "Log10 Number of " + object;
62  std::string name = "NumberOf" + object;
63  nrSimTrackH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
64 
65  SimTrackTags_ = tags;
66  if (!SimTrackTags_.empty()) {
67  crossingFrame_SimTr_Token_ = consumes<CrossingFrame<SimTrack>>(edm::InputTag("mix", SimTrackTags_[0].label()));
68  }
69  } else if (object == "SimVertex") {
70  std::string title = "Log10 Number of " + object;
71  std::string name = "NumberOf" + object;
72  nrSimVertexH_ = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
73 
74  SimVertexTags_ = tags;
75  if (!SimVertexTags_.empty()) {
77  consumes<CrossingFrame<SimVertex>>(edm::InputTag("mix", SimVertexTags_[0].label()));
78  }
79  } else if (object == "PSimHit") {
80  std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
81  for (unsigned int ii = 0; ii < subdets.size(); ii++) {
82  std::string title = "Log10 Number of " + subdets[ii];
83  std::string name = "NumberOf" + subdets[ii];
84  SimHitNrmap_[subdets[ii]] = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
85 
86  title = "Time of " + subdets[ii];
87  name = "TimeOf" + subdets[ii];
88  SimHitTimemap_[subdets[ii]] =
89  iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, -125., 375.);
90  }
91 
92  PSimHitTags_ = tags;
93  for (auto const &it : PSimHitTags_)
95  consumes<CrossingFrame<PSimHit>>(edm::InputTag("mix", it.label() + it.instance())));
96  } else if (object == "PCaloHit") {
97  std::vector<std::string> subdets = pset.getParameter<std::vector<std::string>>("subdets");
98  for (unsigned int ii = 0; ii < subdets.size(); ii++) {
99  std::string title = "Log10 Number of " + subdets[ii];
100  std::string name = "NumberOf" + subdets[ii];
101  CaloHitNrmap_[subdets[ii]] = iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, 0., 40.);
102 
103  title = "Time of " + subdets[ii];
104  name = "TimeOf" + subdets[ii];
105  CaloHitTimemap_[subdets[ii]] =
106  iBooker.bookProfile(name, title, nbin_, minbunch_, maxbunch_ + 1, 40, -125., 375.);
107  }
108 
109  PCaloHitTags_ = tags;
110  for (auto const &it : PCaloHitTags_)
112  consumes<CrossingFrame<PCaloHit>>(edm::InputTag("mix", it.label() + it.instance())));
113  }
114  }
115 }
116 
118  using namespace edm;
119 
120  if (!HepMCProductTags_.empty()) {
121  bool gotHepMCProduct;
123  gotHepMCProduct = iEvent.getByToken(crossingFrame_Hep_Token_, crossingFrame);
124 
125  if (gotHepMCProduct) {
126  std::unique_ptr<MixCollection<HepMCProduct>> hepMCProduct(
127  new MixCollection<HepMCProduct>(crossingFrame.product()));
129 
130  fillGenParticleMulti(hitItr, hepMCProduct, nrHepMCProductH_);
131  }
132  }
133 
134  if (!SimTrackTags_.empty()) {
135  bool gotSimTrack;
137  gotSimTrack = iEvent.getByToken(crossingFrame_SimTr_Token_, crossingFrame);
138 
139  if (gotSimTrack) {
140  std::unique_ptr<MixCollection<SimTrack>> simTracks(new MixCollection<SimTrack>(crossingFrame.product()));
142 
143  fillMultiplicity(hitItr, simTracks, nrSimTrackH_);
144  }
145  }
146 
147  if (!SimVertexTags_.empty()) {
148  bool gotSimVertex;
150  std::string SimVertexLabel = SimVertexTags_[0].label();
151  gotSimVertex = iEvent.getByToken(crossingFrame_SimVtx_Token_, crossingFrame);
152 
153  if (gotSimVertex) {
154  std::unique_ptr<MixCollection<SimVertex>> simVerteces(new MixCollection<SimVertex>(crossingFrame.product()));
156 
157  fillMultiplicity(hitItr, simVerteces, nrSimVertexH_);
158  }
159  }
160 
161  if (!PSimHitTags_.empty()) {
162  edm::Handle<CrossingFrame<PSimHit>> crossingFrame;
163 
164  for (int i = 0; i < (int)PSimHitTags_.size(); i++) {
165  bool gotPSimHit;
166  gotPSimHit = iEvent.getByToken(crossingFrame_PSimHit_Tokens_[i], crossingFrame);
167 
168  if (gotPSimHit) {
169  std::unique_ptr<MixCollection<PSimHit>> simHits(new MixCollection<PSimHit>(crossingFrame.product()));
170 
172 
173  fillMultiplicity(hitItr, simHits, SimHitNrmap_[PSimHitTags_[i].instance()]);
174 
175  fillSimHitTime(hitItr, simHits, SimHitTimemap_[PSimHitTags_[i].instance()]);
176  }
177  }
178  }
179 
180  if (!PCaloHitTags_.empty()) {
182 
183  for (int i = 0; i < (int)PCaloHitTags_.size(); i++) {
184  bool gotPCaloHit;
185  std::string PCaloHitLabel = PCaloHitTags_[i].label() + PCaloHitTags_[i].instance();
186  gotPCaloHit = iEvent.getByToken(crossingFrame_PCaloHit_Tokens_[i], crossingFrame);
187 
188  if (gotPCaloHit) {
189  std::unique_ptr<MixCollection<PCaloHit>> caloHits(new MixCollection<PCaloHit>(crossingFrame.product()));
190 
192 
193  fillMultiplicity(hitItr, caloHits, CaloHitNrmap_[PCaloHitTags_[i].instance()]);
194 
195  fillCaloHitTime(hitItr, caloHits, CaloHitTimemap_[PCaloHitTags_[i].instance()]);
196  }
197  }
198  }
199 }
200 
201 template <class T1, class T2>
202 void MixCollectionValidation::fillMultiplicity(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
203  std::vector<int> theMult(nbin_);
204 
205  for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
206  int bunch = (*theItr_).eventId().bunchCrossing();
207  int index = bunch - minbunch_;
208  if (index >= 0 && index < nbin_) {
209  theMult[index] += 1;
210  } else {
211  edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range";
212  }
213  }
214 
215  for (int i = 0; i < nbin_; i++) {
216  theProfile_->Fill(float(i + minbunch_ + 0.5), std::log10(std::max(float(0.1), float(theMult[i]))));
217  }
218 }
219 
220 template <class T1, class T2>
221 void MixCollectionValidation::fillGenParticleMulti(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
222  std::vector<int> theMult(nbin_);
223 
224  for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
225  int bunch = theItr_.bunch();
226  int index = bunch - minbunch_;
227  if (index >= 0 && index < nbin_) {
228  theMult[index] += (*theItr_).GetEvent()->particles_size();
229  } else {
230  edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range";
231  }
232  }
233 
234  for (int i = 0; i < nbin_; i++) {
235  theProfile_->Fill(float(i + minbunch_ + 0.5), std::log10(std::max(float(0.1), float(theMult[i]))));
236  }
237 }
238 
239 template <class T1, class T2>
240 void MixCollectionValidation::fillSimHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
241  for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
242  int bunch = (*theItr_).eventId().bunchCrossing();
243  float time = (*theItr_).timeOfFlight();
244  int index = bunch - minbunch_;
245  if (index >= 0 && index < nbin_) {
246  theProfile_->Fill(float(bunch + 0.5), time);
247  } else {
248  edm::LogWarning("MixCollectionValidation") << "fillSimHitTime: bunch number " << bunch << " out of range";
249  }
250  }
251 }
252 
253 template <class T1, class T2>
254 void MixCollectionValidation::fillCaloHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_) {
255  for (theItr_ = theColl_->begin(); theItr_ != theColl_->end(); ++theItr_) {
256  int bunch = (*theItr_).eventId().bunchCrossing();
257  float time = (*theItr_).time();
258  int index = bunch - minbunch_;
259  if (index >= 0 && index < nbin_) {
260  theProfile_->Fill(float(bunch + 0.5), time);
261  } else {
262  edm::LogWarning("MixCollectionValidation") << "fillCaloHitTime: bunch number " << bunch << " out of range";
263  }
264  }
265 }
T getParameter(std::string const &) const
std::map< std::string, MonitorElement * > SimHitNrmap_
std::vector< edm::EDGetTokenT< CrossingFrame< PSimHit > > > crossingFrame_PSimHit_Tokens_
std::map< std::string, MonitorElement * > CaloHitTimemap_
std::vector< edm::InputTag > HepMCProductTags_
MonitorElement * bookProfile(Args &&...args)
Definition: DQMStore.h:113
MixCollectionValidation(const edm::ParameterSet &)
static PFTauRenderPlugin instance
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< edm::EDGetTokenT< CrossingFrame< PCaloHit > > > crossingFrame_PCaloHit_Tokens_
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::map< std::string, MonitorElement * > CaloHitNrmap_
bool exists(std::string const &parameterName) const
checks if a parameter exists
const std::string names[nVars_]
void Fill(long long x)
char const * label
int iEvent
Definition: GenABIO.cc:224
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:268
void fillCaloHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)
edm::EDGetTokenT< CrossingFrame< SimVertex > > crossingFrame_SimVtx_Token_
void fillMultiplicity(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)
std::vector< edm::InputTag > PSimHitTags_
void fillSimHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)
std::map< std::string, MonitorElement * > SimHitTimemap_
edm::EDGetTokenT< CrossingFrame< SimTrack > > crossingFrame_SimTr_Token_
std::vector< std::string > getParameterNames() const
std::vector< edm::InputTag > PCaloHitTags_
ii
Definition: cuy.py:590
std::vector< edm::InputTag > SimTrackTags_
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > crossingFrame_Hep_Token_
T const * product() const
Definition: Handle.h:74
std::vector< edm::InputTag > SimVertexTags_
HLT enums.
static const std::string subdets[7]
Definition: TrackUtils.cc:60
Definition: Run.h:45
void analyze(const edm::Event &, const edm::EventSetup &) override
void fillGenParticleMulti(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)