CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MixCollectionValidation.cc
Go to the documentation of this file.
1 // system include files
3 
4 // user include files
7 
10 
12 
13 #include "TFile.h"
16 
17 #include <memory>
18 #include <utility>
19 
20 using namespace edm;
21 
22 MixCollectionValidation::MixCollectionValidation(const edm::ParameterSet& iConfig): outputFile_(iConfig.getParameter<std::string>("outputFile")),
23  minbunch_(iConfig.getParameter<int>("minBunch")),
24  maxbunch_(iConfig.getParameter<int>("maxBunch")),
25  verbose_(iConfig.getUntrackedParameter<bool>("verbose",false)),
26  dbe_(0),nbin_(maxbunch_-minbunch_+1)
27 {
28 
29  if ( outputFile_.size() != 0 ) {
30  edm::LogInfo("OutputInfo") << " Ecal SimHits Task histograms will be saved to " << outputFile_.c_str();
31  } else {
32  edm::LogInfo("OutputInfo") << " Ecal SimHits Task histograms will NOT be saved";
33  }
34 
35  // get hold of back-end interface
37  if ( dbe_ ) {
38  if ( verbose_ ) { dbe_->setVerbose(1); }
39  else { dbe_->setVerbose(0); }
40  }
41 
42  if ( dbe_ ) {
43  if ( verbose_ ) dbe_->showDirStructure();
44  }
45 
46  // get hold of back-end interface
48  // dbe_->showDirStructure();
49  dbe_->setCurrentFolder("MixingV/Mixing");
50 
51  // define the histograms according to the configuration
52 
53  ParameterSet ps=iConfig.getParameter<ParameterSet>("mixObjects");
55 
56  for (std::vector<std::string>::iterator it=names_.begin();it!= names_.end();++it)
57  {
58  ParameterSet pset=ps.getParameter<ParameterSet>((*it));
59  if (!pset.exists("type")) continue; //to allow replacement by empty pset
60  std::string object = pset.getParameter<std::string>("type");
61  std::vector<InputTag> tags=pset.getParameter<std::vector<InputTag> >("input");
62 
63  if ( object == "HepMCProduct" ) {
64 
65  std::string title = "Log10 Number of GenParticle in " + object;
66  std::string name = "NumberOf" + object;
67  nrHepMCProductH_ = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,0.,40.);
68 
70  if (HepMCProductTags_.size()) {
71  crossingFrame_Hep_Token_ = consumes<CrossingFrame<HepMCProduct> >(
73  }
74  }
75  else if ( object == "SimTrack" ) {
76 
77  std::string title = "Log10 Number of " + object;
78  std::string name = "NumberOf" + object;
79  nrSimTrackH_ = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,0.,40.);
80 
82  if (SimTrackTags_.size()) {
83  crossingFrame_SimTr_Token_ = consumes<CrossingFrame<SimTrack> >(
84  edm::InputTag("mix", SimTrackTags_[0].label()));
85  }
86  }
87  else if ( object == "SimVertex" ) {
88 
89  std::string title = "Log10 Number of " + object;
90  std::string name = "NumberOf" + object;
91  nrSimVertexH_ = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,0.,40.);
92 
94  if (SimVertexTags_.size()) {
95  crossingFrame_SimVtx_Token_ = consumes<CrossingFrame<SimVertex> >(
96  edm::InputTag("mix", SimVertexTags_[0].label()));
97  }
98  }
99  else if ( object == "PSimHit" ) {
100  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
101  for (unsigned int ii=0;ii<subdets.size();ii++) {
102 
103  std::string title = "Log10 Number of " + subdets[ii];
104  std::string name = "NumberOf" + subdets[ii];
105  SimHitNrmap_[subdets[ii]] = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,0.,40.);
106 
107  title = "Time of " + subdets[ii];
108  name = "TimeOf" + subdets[ii];
109  SimHitTimemap_[subdets[ii]] = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,-125.,375.);
110 
111  }
112 
113  PSimHitTags_ = tags;
114  for (auto const & it : PSimHitTags_)
116  edm::InputTag("mix", it.label() + it.instance())));
117  }
118  else if ( object == "PCaloHit" ) {
119  std::vector<std::string> subdets=pset.getParameter<std::vector<std::string> >("subdets");
120  for (unsigned int ii=0;ii<subdets.size();ii++) {
121 
122  std::string title = "Log10 Number of " + subdets[ii];
123  std::string name = "NumberOf" + subdets[ii];
124  CaloHitNrmap_[subdets[ii]] = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,0.,40.);
125 
126  title = "Time of " + subdets[ii];
127  name = "TimeOf" + subdets[ii];
128  CaloHitTimemap_[subdets[ii]] = dbe_->bookProfile(name,title,nbin_,minbunch_,maxbunch_+1,40,-125.,375.);
129 
130  }
131 
133  for (auto const & it : PCaloHitTags_)
135  edm::InputTag("mix", it.label() + it.instance())));
136  }
137  }
138 
139 }
140 
142 {
143 
144  // do anything here that needs to be done at desctruction time
145  // (e.g. close files, deallocate resources etc.)
146 }
147 
149 }
150 
151 
153  if (outputFile_.size() != 0 && dbe_ ) dbe_->save(outputFile_);
154 }
155 
156 //
157 // member functions
158 //
159 
160 // ------------ method called to analyze the data ------------
161 void
163 {
164 
165  using namespace edm;
166 
167  if ( HepMCProductTags_.size() > 0 ) {
168  bool gotHepMCProduct;
170  gotHepMCProduct = iEvent.getByToken(crossingFrame_Hep_Token_, crossingFrame);
171 
172  if (gotHepMCProduct){
173  std::auto_ptr<MixCollection<HepMCProduct> >
174  hepMCProduct (new MixCollection<HepMCProduct>(crossingFrame.product ()));
176 
177  fillGenParticleMulti(hitItr, hepMCProduct, nrHepMCProductH_);
178  }
179  }
180 
181  if ( SimTrackTags_.size() > 0 ) {
182  bool gotSimTrack;
183  edm::Handle<CrossingFrame<SimTrack> > crossingFrame;
184  gotSimTrack = iEvent.getByToken(crossingFrame_SimTr_Token_,crossingFrame);
185 
186  if (gotSimTrack){
187  std::auto_ptr<MixCollection<SimTrack> >
188  simTracks (new MixCollection<SimTrack>(crossingFrame.product ()));
190 
191  fillMultiplicity(hitItr, simTracks, nrSimTrackH_);
192  }
193  }
194 
195  if ( SimVertexTags_.size() > 0 ) {
196  bool gotSimVertex;
197  edm::Handle<CrossingFrame<SimVertex> > crossingFrame;
198  std::string SimVertexLabel = SimVertexTags_[0].label();
199  gotSimVertex = iEvent.getByToken(crossingFrame_SimVtx_Token_, crossingFrame);
200 
201  if (gotSimVertex){
202  std::auto_ptr<MixCollection<SimVertex> >
203  simVerteces (new MixCollection<SimVertex>(crossingFrame.product ()));
205 
206  fillMultiplicity(hitItr, simVerteces, nrSimVertexH_);
207  }
208  }
209 
210  if ( PSimHitTags_.size() > 0 ) {
211 
212  edm::Handle<CrossingFrame<PSimHit> > crossingFrame;
213 
214  for ( int i = 0; i < (int)PSimHitTags_.size(); i++ ) {
215  bool gotPSimHit;
216  gotPSimHit = iEvent.getByToken(crossingFrame_PSimHit_Tokens_[i], crossingFrame);
217 
218  if (gotPSimHit){
219  std::auto_ptr<MixCollection<PSimHit> >
220  simHits (new MixCollection<PSimHit>(crossingFrame.product ()));
221 
223 
224  fillMultiplicity(hitItr, simHits, SimHitNrmap_[PSimHitTags_[i].instance()]);
225 
226  fillSimHitTime(hitItr, simHits, SimHitTimemap_[PSimHitTags_[i].instance()]);
227  }
228  }
229  }
230 
231  if ( PCaloHitTags_.size() > 0 ) {
232 
233  edm::Handle<CrossingFrame<PCaloHit> > crossingFrame;
234 
235  for ( int i = 0; i < (int)PCaloHitTags_.size(); i++ ) {
236  bool gotPCaloHit;
237  std::string PCaloHitLabel = PCaloHitTags_[i].label()+PCaloHitTags_[i].instance();
238  gotPCaloHit = iEvent.getByToken(crossingFrame_PCaloHit_Tokens_[i], crossingFrame);
239 
240  if (gotPCaloHit){
241  std::auto_ptr<MixCollection<PCaloHit> >
242  caloHits (new MixCollection<PCaloHit>(crossingFrame.product ()));
243 
245 
246  fillMultiplicity(hitItr, caloHits, CaloHitNrmap_[PCaloHitTags_[i].instance()]);
247 
248  fillCaloHitTime(hitItr, caloHits, CaloHitTimemap_[PCaloHitTags_[i].instance()]);
249  }
250  }
251  }
252 
253 }
254 
255 template<class T1, class T2> void MixCollectionValidation::fillMultiplicity(T1 & theItr_, T2 & theColl_, MonitorElement * theProfile_) {
256 
257  std::vector<int> theMult(nbin_);
258 
259  for ( theItr_ = theColl_->begin() ; theItr_ != theColl_->end() ; ++theItr_) {
260 
261  int bunch = (*theItr_).eventId().bunchCrossing();
262  int index = bunch - minbunch_;
263  if ( index >= 0 && index < nbin_ ) { theMult[index] += 1; }
264  else { edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range"; }
265 
266  }
267 
268  for ( int i = 0; i < nbin_; i++ ) {
269  theProfile_->Fill(float(i+minbunch_+0.5),std::log10(std::max(float(0.1),float(theMult[i]))));
270  }
271 }
272 
273 
274 template<class T1, class T2> void MixCollectionValidation::fillGenParticleMulti(T1 & theItr_, T2 & theColl_, MonitorElement * theProfile_) {
275 
276  std::vector<int> theMult(nbin_);
277 
278  for ( theItr_ = theColl_->begin() ; theItr_ != theColl_->end() ; ++theItr_) {
279 
280  int bunch = theItr_.bunch();
281  int index = bunch - minbunch_;
282  if ( index >= 0 && index < nbin_ ) { theMult[index] += (*theItr_).GetEvent()->particles_size(); }
283  else { edm::LogWarning("MixCollectionValidation") << "fillMultiplicity: bunch number " << bunch << " out of range"; }
284 
285  }
286 
287  for ( int i = 0; i < nbin_; i++ ) {
288  theProfile_->Fill(float(i+minbunch_+0.5),std::log10(std::max(float(0.1),float(theMult[i]))));
289  }
290 }
291 
292 template<class T1, class T2> void MixCollectionValidation::fillSimHitTime(T1 & theItr_, T2 & theColl_, MonitorElement * theProfile_) {
293 
294  for ( theItr_ = theColl_->begin() ; theItr_ != theColl_->end() ; ++theItr_) {
295 
296  int bunch = (*theItr_).eventId().bunchCrossing();
297  float time = (*theItr_).timeOfFlight();
298  int index = bunch - minbunch_;
299  if ( index >= 0 && index < nbin_ ) { theProfile_->Fill(float(bunch+0.5),time); }
300  else { edm::LogWarning("MixCollectionValidation") << "fillSimHitTime: bunch number " << bunch << " out of range"; }
301 
302  }
303 
304 }
305 
306 template<class T1, class T2> void MixCollectionValidation::fillCaloHitTime(T1 & theItr_, T2 & theColl_, MonitorElement * theProfile_) {
307 
308  for ( theItr_ = theColl_->begin() ; theItr_ != theColl_->end() ; ++theItr_) {
309 
310  int bunch = (*theItr_).eventId().bunchCrossing();
311  float time = (*theItr_).time();
312  int index = bunch - minbunch_;
313  if ( index >= 0 && index < nbin_ ) { theProfile_->Fill(float(bunch+0.5),time); }
314  else { edm::LogWarning("MixCollectionValidation") << "fillCaloHitTime: bunch number " << bunch << " out of range"; }
315 
316  }
317 
318 }
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::map< std::string, MonitorElement * > CaloHitNrmap_
std::vector< edm::EDGetTokenT< CrossingFrame< PCaloHit > > > crossingFrame_PCaloHit_Tokens_
std::vector< edm::InputTag > HepMCProductTags_
MixCollectionValidation(const edm::ParameterSet &)
static PFTauRenderPlugin instance
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
std::vector< edm::EDGetTokenT< CrossingFrame< PSimHit > > > crossingFrame_PSimHit_Tokens_
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::map< std::string, MonitorElement * > SimHitTimemap_
int ii
Definition: cuy.py:588
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
void Fill(long long x)
int iEvent
Definition: GenABIO.cc:230
const T & max(const T &a, const T &b)
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_
std::map< std::string, MonitorElement * > SimHitNrmap_
MonitorElement * bookProfile(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, const char *option="s")
Definition: DQMStore.cc:1256
void setVerbose(unsigned level)
Definition: DQMStore.cc:619
void fillSimHitTime(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)
edm::EDGetTokenT< CrossingFrame< SimTrack > > crossingFrame_SimTr_Token_
std::vector< std::string > getParameterNames() const
DQMStore * dbe_
std::vector< edm::InputTag > PCaloHitTags_
tuple tags
Definition: o2o.py:248
std::vector< edm::InputTag > SimTrackTags_
edm::EDGetTokenT< CrossingFrame< edm::HepMCProduct > > crossingFrame_Hep_Token_
constexpr char const * subdets[11]
tuple simHits
Definition: trackerHits.py:16
T const * product() const
Definition: Handle.h:81
list object
Definition: dbtoconf.py:77
std::vector< edm::InputTag > SimVertexTags_
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, const uint32_t lumi=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE", const bool resetMEsAfterWriting=false)
Definition: DQMStore.cc:2490
std::map< std::string, MonitorElement * > CaloHitTimemap_
virtual void analyze(const edm::Event &, const edm::EventSetup &)
volatile std::atomic< bool > shutdown_flag false
void showDirStructure(void) const
Definition: DQMStore.cc:3282
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:655
void fillGenParticleMulti(T1 &theItr_, T2 &theColl_, MonitorElement *theProfile_)
std::vector< std::string > names_