CMS 3D CMS Logo

DataMixingHcalWorker.cc
Go to the documentation of this file.
1 // File: DataMixingHcalWorker.cc
2 // Description: see DataMixingHcalWorker.h
3 // Author: Mike Hildreth, University of Notre Dame
4 //
5 //--------------------------------------------
6 
7 #include <map>
8 #include <memory>
11 //
12 //
13 #include "DataMixingHcalWorker.h"
14 
15 
16 using namespace std;
17 
18 namespace edm
19 {
20 
21  // Virtual constructor
22 
23  DataMixingHcalWorker::DataMixingHcalWorker() { }
24 
25  // Constructor
26  DataMixingHcalWorker::DataMixingHcalWorker(const edm::ParameterSet& ps, edm::ConsumesCollector && iC) :
27  label_(ps.getParameter<std::string>("Label"))
28 
29  {
30 
31  // get the subdetector names
32  // this->getSubdetectorNames(); //something like this may be useful to check what we are supposed to do...
33 
34  // declare the products to produce
35 
36  // Hcal
37 
38  HBHErechitCollectionSig_ = ps.getParameter<edm::InputTag>("HBHEProducerSig");
39  HOrechitCollectionSig_ = ps.getParameter<edm::InputTag>("HOProducerSig");
40  HFrechitCollectionSig_ = ps.getParameter<edm::InputTag>("HFProducerSig");
41  ZDCrechitCollectionSig_ = ps.getParameter<edm::InputTag>("ZDCrechitCollectionSig");
42 
43  HBHEPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("HBHEPileRecHitInputTag");
44  HOPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("HOPileRecHitInputTag");
45  HFPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("HFPileRecHitInputTag");
46  ZDCPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("ZDCPileRecHitInputTag");
47 
48  HBHERecHitCollectionDM_ = ps.getParameter<std::string>("HBHERecHitCollectionDM");
49  HORecHitCollectionDM_ = ps.getParameter<std::string>("HORecHitCollectionDM");
50  HFRecHitCollectionDM_ = ps.getParameter<std::string>("HFRecHitCollectionDM");
51  ZDCRecHitCollectionDM_ = ps.getParameter<std::string>("ZDCRecHitCollectionDM");
52 
56 
60 
61 
64 
65  }
66 
67  // Virtual destructor needed.
69  }
70 
72  // fill in maps of hits
73 
74  LogInfo("DataMixingHcalWorker")<<"===============> adding MC signals for "<<e.id();
75 
76  // HBHE first
77 
78  Handle< HBHERecHitCollection > pHBHERecHits;
79 
80  const HBHERecHitCollection* HBHERecHits = nullptr;
81 
82  if( e.getByToken( HBHERecHitToken_, pHBHERecHits) ) {
83  HBHERecHits = pHBHERecHits.product(); // get a ptr to the product
84  LogDebug("DataMixingHcalWorker") << "total # HBHE rechits: " << HBHERecHits->size();
85  }
86 
87 
88  if (HBHERecHits)
89  {
90  // loop over rechits, storing them in a map so we can add pileup later
91  for(HBHERecHitCollection::const_iterator it = HBHERecHits->begin();
92  it != HBHERecHits->end(); ++it) {
93 
94  HBHERecHitStorage_.insert(HBHERecHitMap::value_type( ( it->id() ), *it ));
95 
96 #ifdef DEBUG
97  LogDebug("DataMixingHcalWorker") << "processed HBHERecHit with rawId: "
98  << it->id() << "\n"
99  << " rechit energy: " << it->energy();
100 #endif
101 
102  }
103  }
104 
105  // HO next
106 
107  Handle< HORecHitCollection > pHORecHits;
108 
109  const HORecHitCollection* HORecHits = nullptr;
110 
111  if( e.getByToken( HORecHitToken_, pHORecHits) ){
112  HORecHits = pHORecHits.product(); // get a ptr to the product
113 #ifdef DEBUG
114  LogDebug("DataMixingHcalWorker") << "total # HO rechits: " << HORecHits->size();
115 #endif
116  }
117 
118 
119  if (HORecHits)
120  {
121  // loop over rechits, storing them in a map so we can add pileup later
122  for(HORecHitCollection::const_iterator it = HORecHits->begin();
123  it != HORecHits->end(); ++it) {
124 
125  HORecHitStorage_.insert(HORecHitMap::value_type( ( it->id() ), *it ));
126 
127 #ifdef DEBUG
128  LogDebug("DataMixingHcalWorker") << "processed HORecHit with rawId: "
129  << it->id() << "\n"
130  << " rechit energy: " << it->energy();
131 #endif
132 
133  }
134  }
135 
136  // HF next
137 
138  Handle< HFRecHitCollection > pHFRecHits;
139 
140  const HFRecHitCollection* HFRecHits = nullptr;
141 
142  if( e.getByToken( HFRecHitToken_, pHFRecHits) ) {
143  HFRecHits = pHFRecHits.product(); // get a ptr to the product
144 #ifdef DEBUG
145  LogDebug("DataMixingHcalWorker") << "total # HF rechits: " << HFRecHits->size();
146 #endif
147  }
148 
149 
150  if (HFRecHits)
151  {
152  // loop over rechits, storing them in a map so we can add pileup later
153  for(HFRecHitCollection::const_iterator it = HFRecHits->begin();
154  it != HFRecHits->end(); ++it) {
155 
156  HFRecHitStorage_.insert(HFRecHitMap::value_type( ( it->id() ), *it ));
157 
158 #ifdef DEBUG
159  LogDebug("DataMixingHcalWorker") << "processed HFRecHit with rawId: "
160  << it->id() << "\n"
161  << " rechit energy: " << it->energy();
162 #endif
163 
164  }
165  }
166 
167  // ZDC next
168 
169  Handle< ZDCRecHitCollection > pZDCRecHits;
170 
171  const ZDCRecHitCollection* ZDCRecHits = nullptr;
172 
173  if( e.getByToken( ZDCRecHitToken_, pZDCRecHits) ) {
174  ZDCRecHits = pZDCRecHits.product(); // get a ptr to the product
175 #ifdef DEBUG
176  LogDebug("DataMixingHcalWorker") << "total # ZDC rechits: " << ZDCRecHits->size();
177 #endif
178  }
179 
180 
181  if (ZDCRecHits)
182  {
183  // loop over rechits, storing them in a map so we can add pileup later
184  for(ZDCRecHitCollection::const_iterator it = ZDCRecHits->begin();
185  it != ZDCRecHits->end(); ++it) {
186 
187  ZDCRecHitStorage_.insert(ZDCRecHitMap::value_type( ( it->id() ), *it ));
188 
189 #ifdef DEBUG
190  LogDebug("DataMixingHcalWorker") << "processed ZDCRecHit with rawId: "
191  << it->id() << "\n"
192  << " rechit energy: " << it->energy();
193 #endif
194 
195  }
196  }
197 
198  } // end of addEMSignals
199 
200  void DataMixingHcalWorker::addHcalPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,
201  ModuleCallingContext const* mcc) {
202 
203  LogDebug("DataMixingHcalWorker") <<"\n===============> adding pileups from event "<<ep->id()<<" for bunchcrossing "<<bcr;
204 
205  // fill in maps of hits; same code as addSignals, except now applied to the pileup events
206 
207  // HBHE first
208 
209  std::shared_ptr<Wrapper<HBHERecHitCollection> const> HBHERecHitsPTR =
210  getProductByTag<HBHERecHitCollection>(*ep, HBHEPileRecHitInputTag_, mcc);
211 
212  if(HBHERecHitsPTR ) {
213 
214  const HBHERecHitCollection* HBHERecHits = const_cast< HBHERecHitCollection * >(HBHERecHitsPTR->product());
215 
216  LogDebug("DataMixingEMWorker") << "total # HBHE rechits: " << HBHERecHits->size();
217 
218  // loop over rechits, adding these to the existing maps
219  for(HBHERecHitCollection::const_iterator it = HBHERecHits->begin();
220  it != HBHERecHits->end(); ++it) {
221 
222  HBHERecHitStorage_.insert(HBHERecHitMap::value_type( (it->id()), *it ));
223 
224 #ifdef DEBUG
225  LogDebug("DataMixingEMWorker") << "processed HBHERecHit with rawId: "
226  << it->id().rawId() << "\n"
227  << " rechit energy: " << it->energy();
228 #endif
229  }
230  }
231 
232  // HO Next
233 
234  std::shared_ptr<Wrapper<HORecHitCollection> const> HORecHitsPTR =
235  getProductByTag<HORecHitCollection>(*ep, HOPileRecHitInputTag_, mcc);
236 
237  if(HORecHitsPTR ) {
238 
239  const HORecHitCollection* HORecHits = const_cast< HORecHitCollection * >(HORecHitsPTR->product());
240 
241  LogDebug("DataMixingEMWorker") << "total # HO rechits: " << HORecHits->size();
242 
243  // loop over rechits, adding these to the existing maps
244  for(HORecHitCollection::const_iterator it = HORecHits->begin();
245  it != HORecHits->end(); ++it) {
246 
247  HORecHitStorage_.insert(HORecHitMap::value_type( (it->id()), *it ));
248 
249 #ifdef DEBUG
250  LogDebug("DataMixingEMWorker") << "processed HORecHit with rawId: "
251  << it->id().rawId() << "\n"
252  << " rechit energy: " << it->energy();
253 #endif
254  }
255  }
256 
257  // HF Next
258 
259  std::shared_ptr<Wrapper<HFRecHitCollection> const> HFRecHitsPTR =
260  getProductByTag<HFRecHitCollection>(*ep, HFPileRecHitInputTag_, mcc);
261 
262  if(HFRecHitsPTR ) {
263 
264  const HFRecHitCollection* HFRecHits = const_cast< HFRecHitCollection * >(HFRecHitsPTR->product());
265 
266  LogDebug("DataMixingEMWorker") << "total # HF rechits: " << HFRecHits->size();
267 
268  // loop over rechits, adding these to the existing maps
269  for(HFRecHitCollection::const_iterator it = HFRecHits->begin();
270  it != HFRecHits->end(); ++it) {
271 
272  HFRecHitStorage_.insert(HFRecHitMap::value_type( (it->id()), *it ));
273 
274 #ifdef DEBUG
275  LogDebug("DataMixingEMWorker") << "processed HFRecHit with rawId: "
276  << it->id().rawId() << "\n"
277  << " rechit energy: " << it->energy();
278 #endif
279  }
280  }
281 
282  // ZDC Next
283 
284  std::shared_ptr<Wrapper<ZDCRecHitCollection> const> ZDCRecHitsPTR =
285  getProductByTag<ZDCRecHitCollection>(*ep, ZDCPileRecHitInputTag_, mcc);
286 
287  if(ZDCRecHitsPTR ) {
288 
289  const ZDCRecHitCollection* ZDCRecHits = const_cast< ZDCRecHitCollection * >(ZDCRecHitsPTR->product());
290 
291  LogDebug("DataMixingEMWorker") << "total # ZDC rechits: " << ZDCRecHits->size();
292 
293  // loop over rechits, adding these to the existing maps
294  for(ZDCRecHitCollection::const_iterator it = ZDCRecHits->begin();
295  it != ZDCRecHits->end(); ++it) {
296 
297  ZDCRecHitStorage_.insert(ZDCRecHitMap::value_type( (it->id()), *it ));
298 
299 #ifdef DEBUG
300  LogDebug("DataMixingEMWorker") << "processed ZDCRecHit with rawId: "
301  << it->id().rawId() << "\n"
302  << " rechit energy: " << it->energy();
303 #endif
304  }
305  }
306 
307 
308  }
309 
311 
312  // collection of rechits to put in the event
313  std::unique_ptr< HBHERecHitCollection > HBHErechits( new HBHERecHitCollection );
314  std::unique_ptr< HORecHitCollection > HOrechits( new HORecHitCollection );
315  std::unique_ptr< HFRecHitCollection > HFrechits( new HFRecHitCollection );
316  std::unique_ptr< ZDCRecHitCollection > ZDCrechits( new ZDCRecHitCollection );
317 
318  // loop over the maps we have, re-making individual hits or rechits if necessary.
319  DetId formerID = 0;
320  DetId currentID;
321  float ESum = 0.;
322  float HBTime = 0.;
323 
324  // HB first...
325 
326  HBHERecHitMap::const_iterator iHBchk;
327 
328  for(HBHERecHitMap::const_iterator iHB = HBHERecHitStorage_.begin();
329  iHB != HBHERecHitStorage_.end(); ++iHB) {
330 
331  currentID = iHB->first;
332 
333  if (currentID == formerID) { // we have to add these rechits together
334 
335  ESum+=(iHB->second).energy();
336 
337  }
338  else {
339  if(formerID>0) {
340  // cutoff for ESum?
341  HBHERecHit aHit(formerID, ESum, HBTime);
342  HBHErechits->push_back( aHit );
343  }
344  //save pointers for next iteration
345  formerID = currentID;
346  ESum = (iHB->second).energy();
347  HBTime = (iHB->second).time(); // take time of first hit in sequence - is this ok?
348  }
349 
350  iHBchk = iHB;
351  if((++iHBchk) == HBHERecHitStorage_.end()) { //make sure not to lose the last one
352  HBHERecHit aHit(formerID, ESum, HBTime);
353  HBHErechits->push_back( aHit );
354  }
355  }
356 
357  // HO next...
358 
359  // loop over the maps we have, re-making individual hits or rechits if necessary.
360  formerID = 0;
361  ESum = 0.;
362  float HOTime = 0.;
363 
364  HORecHitMap::const_iterator iHOchk;
365 
366  for(HORecHitMap::const_iterator iHO = HORecHitStorage_.begin();
367  iHO != HORecHitStorage_.end(); ++iHO) {
368 
369  currentID = iHO->first;
370 
371  if (currentID == formerID) { // we have to add these rechits together
372 
373  ESum+=(iHO->second).energy();
374 
375  }
376  else {
377  if(formerID>0) {
378  // cutoff for ESum?
379  HORecHit aHit(formerID, ESum, HOTime);
380  HOrechits->push_back( aHit );
381  }
382  //save pointers for next iteration
383  formerID = currentID;
384  ESum = (iHO->second).energy();
385  HOTime = (iHO->second).time(); // take time of first hit in sequence - is this ok?
386  }
387 
388  iHOchk = iHO;
389  if((++iHOchk) == HORecHitStorage_.end()) { //make sure not to lose the last one
390  HORecHit aHit(formerID, ESum, HOTime);
391  HOrechits->push_back( aHit );
392  }
393  }
394 
395 
396  // HF next...
397 
398  // loop over the maps we have, re-making individual hits or rechits if necessary.
399  formerID = 0;
400  ESum = 0.;
401  float HFTime = 0.;
402 
403  HFRecHitMap::const_iterator iHFchk;
404 
405  for(HFRecHitMap::const_iterator iHF = HFRecHitStorage_.begin();
406  iHF != HFRecHitStorage_.end(); ++iHF) {
407 
408  currentID = iHF->first;
409 
410  if (currentID == formerID) { // we have to add these rechits together
411 
412  ESum+=(iHF->second).energy();
413 
414  }
415  else {
416  if(formerID>0) {
417  // cutoff for ESum?
418  HFRecHit aHit(formerID, ESum, HFTime);
419  HFrechits->push_back( aHit );
420  }
421  //save pointers for next iteration
422  formerID = currentID;
423  ESum = (iHF->second).energy();
424  HFTime = (iHF->second).time(); // take time of first hit in sequence - is this ok?
425  }
426 
427  iHFchk = iHF;
428  if((++iHFchk) == HFRecHitStorage_.end()) { //make sure not to lose the last one
429  HFRecHit aHit(formerID, ESum, HBTime);
430  HFrechits->push_back( aHit );
431  }
432  }
433 
434  // ZDC next...
435 
436  // loop over the maps we have, re-making individual hits or rechits if necessary.
437  formerID = 0;
438  ESum = 0.;
439  float ZDCTime = 0.;
440  float lowGainEnergy = 0;
441  ZDCRecHit ZOldHit;
442 
443  ZDCRecHitMap::const_iterator iZDCchk;
444 
445  for(ZDCRecHitMap::const_iterator iZDC = ZDCRecHitStorage_.begin();
446  iZDC != ZDCRecHitStorage_.end(); ++iZDC) {
447 
448  currentID = iZDC->first;
449 
450  if (currentID == formerID) { // we have to add these rechits together
451 
452  ESum+=(iZDC->second).energy();
453 
454  }
455  else {
456  if(formerID>0) {
457  // cutoff for ESum?
458  ZDCRecHit aHit(formerID, ESum, ZDCTime, lowGainEnergy);
459  ZDCrechits->push_back( aHit );
460  }
461  //save pointers for next iteration
462  formerID = currentID;
463  ESum = (iZDC->second).energy();
464  lowGainEnergy = (iZDC->second).lowGainEnergy();
465  ZDCTime = (iZDC->second).time(); // take time of first hit in sequence - is this ok?
466  }
467 
468  iZDCchk = iZDC;
469  if((++iZDCchk) == ZDCRecHitStorage_.end()) { //make sure not to lose the last one
470  ZDCRecHit aHit(formerID, ESum, HBTime, lowGainEnergy);
471  ZDCrechits->push_back( aHit );
472  }
473  }
474 
475  //done merging
476 
477  // put the collection of recunstructed hits in the event
478  LogInfo("DataMixingHcalWorker") << "total # HBHE Merged rechits: " << HBHErechits->size() ;
479  LogInfo("DataMixingHcalWorker") << "total # HO Merged rechits: " << HOrechits->size() ;
480  LogInfo("DataMixingHcalWorker") << "total # HF Merged rechits: " << HFrechits->size() ;
481  LogInfo("DataMixingHcalWorker") << "total # ZDC Merged rechits: " << ZDCrechits->size() ;
482 
483  e.put(std::move(HBHErechits), HBHERecHitCollectionDM_ );
484  e.put(std::move(HOrechits), HORecHitCollectionDM_ );
485  e.put(std::move(HFrechits), HFRecHitCollectionDM_ );
486  e.put(std::move(ZDCrechits), ZDCRecHitCollectionDM_ );
487 
488  // clear local storage after this event
489  HBHERecHitStorage_.clear();
490  HORecHitStorage_.clear();
491  HFRecHitStorage_.clear();
492  ZDCRecHitStorage_.clear();
493 
494  }
495 
496 } //edm
#define LogDebug(id)
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
void addHcalSignals(const edm::Event &e)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
void addHcalPileups(const int bcr, const edm::EventPrincipal *, unsigned int EventId, ModuleCallingContext const *)
EventID const & id() const
std::vector< HBHERecHit >::const_iterator const_iterator
edm::EDGetTokenT< ZDCRecHitCollection > ZDCRecHitToken_
edm::EDGetTokenT< ZDCRecHitCollection > ZDCRecHitPToken_
edm::EDGetTokenT< HORecHitCollection > HORecHitToken_
edm::EDGetTokenT< HBHERecHitCollection > HBHERecHitPToken_
edm::EDGetTokenT< HFRecHitCollection > HFRecHitToken_
const_iterator end() const
Definition: DetId.h:18
T const * product() const
Definition: Handle.h:81
edm::EDGetTokenT< HBHERecHitCollection > HBHERecHitToken_
edm::EDGetTokenT< HFRecHitCollection > HFRecHitPToken_
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
size_type size() const
edm::EDGetTokenT< HORecHitCollection > HORecHitPToken_
def move(src, dest)
Definition: eostools.py:511
const_iterator begin() const