CMS 3D CMS Logo

DataMixingEMWorker.cc
Go to the documentation of this file.
1 // File: DataMixingEMWorker.cc
2 // Description: see DataMixingEMWorker.h
3 // Author: Mike Hildreth, University of Notre Dame
4 //
5 //--------------------------------------------
6 
7 #include <map>
8 #include <memory>
11 //
12 //
13 #include "DataMixingEMWorker.h"
14 
15 
16 using namespace std;
17 
18 namespace edm
19 {
20 
21  // Virtual constructor
22 
23  DataMixingEMWorker::DataMixingEMWorker() { }
24 
25  // Constructor
26  DataMixingEMWorker::DataMixingEMWorker(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, retrieve
35 
36  EBProducerSig_ = ps.getParameter<edm::InputTag>("EBProducerSig");
37  EEProducerSig_ = ps.getParameter<edm::InputTag>("EEProducerSig");
38  ESProducerSig_ = ps.getParameter<edm::InputTag>("ESProducerSig");
39 
43 
44 
45  EBrechitCollectionSig_ = ps.getParameter<edm::InputTag>("EBrechitCollectionSig");
46  EErechitCollectionSig_ = ps.getParameter<edm::InputTag>("EErechitCollectionSig");
47  ESrechitCollectionSig_ = ps.getParameter<edm::InputTag>("ESrechitCollectionSig");
48 
49  EBPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("EBPileRecHitInputTag");
50  EEPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("EEPileRecHitInputTag");
51  ESPileRecHitInputTag_ = ps.getParameter<edm::InputTag>("ESPileRecHitInputTag");
52 
56 
57 
58  EBRecHitCollectionDM_ = ps.getParameter<std::string>("EBRecHitCollectionDM");
59  EERecHitCollectionDM_ = ps.getParameter<std::string>("EERecHitCollectionDM");
60  ESRecHitCollectionDM_ = ps.getParameter<std::string>("ESRecHitCollectionDM");
61 
62  }
63 
64 
65  // Virtual destructor needed.
67  }
68 
70  // fill in maps of hits
71 
72  LogInfo("DataMixingEMWorker")<<"===============> adding MC signals for "<<e.id();
73 
74  // EB first
75 
77 
78  const EBRecHitCollection* EBRecHits = nullptr;
79 
80  if(e.getByToken(EBRecHitToken_, pEBRecHits) ){
81  EBRecHits = pEBRecHits.product(); // get a ptr to the product
82  LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
83  }
84 
85 
86  if (EBRecHits)
87  {
88  // loop over rechits, storing them in a map so we can add pileup later
89  for(EBRecHitCollection::const_iterator it = EBRecHits->begin();
90  it != EBRecHits->end(); ++it) {
91 
92  EBRecHitStorage_.insert(EBRecHitMap::value_type( ( it->id() ), *it ));
93 
94  LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: "
95  << it->id().rawId() << "\n"
96  << " rechit energy: " << it->energy();
97 
98  }
99  }
100 
101  // EE next
102 
103  Handle< EERecHitCollection > pEERecHits;
104 
105  const EERecHitCollection* EERecHits = nullptr;
106 
107 
108  if(e.getByToken(EERecHitToken_, pEERecHits) ){
109  EERecHits = pEERecHits.product(); // get a ptr to the product
110  LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
111  }
112 
113 
114  if (EERecHits)
115  {
116  // loop over rechits, storing them in a map so we can add pileup later
117  for(EERecHitCollection::const_iterator it = EERecHits->begin();
118  it != EERecHits->end(); ++it) {
119 
120  EERecHitStorage_.insert(EERecHitMap::value_type( ( it->id() ), *it ));
121 #ifdef DEBUG
122  LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: "
123  << it->id().rawId() << "\n"
124  << " rechit energy: " << it->energy();
125 #endif
126 
127  }
128  }
129  // ES next
130 
131  Handle< ESRecHitCollection > pESRecHits;
132 
133  const ESRecHitCollection* ESRecHits = nullptr;
134 
135 
136  if(e.getByToken( ESRecHitToken_, pESRecHits) ){
137  ESRecHits = pESRecHits.product(); // get a ptr to the product
138 #ifdef DEBUG
139  LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
140 #endif
141  }
142 
143 
144  if (ESRecHits)
145  {
146  // loop over rechits, storing them in a map so we can add pileup later
147  for(ESRecHitCollection::const_iterator it = ESRecHits->begin();
148  it != ESRecHits->end(); ++it) {
149 
150  ESRecHitStorage_.insert(ESRecHitMap::value_type( ( it->id() ), *it ));
151 
152 #ifdef DEBUG
153  LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: "
154  << it->id().rawId() << "\n"
155  << " rechit energy: " << it->energy();
156 #endif
157 
158  }
159  }
160 
161  } // end of addEMSignals
162 
163  void DataMixingEMWorker::addEMPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,
164  ModuleCallingContext const* mcc) {
165 
166  LogInfo("DataMixingEMWorker") <<"\n===============> adding pileups from event "<<ep->id()<<" for bunchcrossing "<<bcr;
167 
168  // fill in maps of hits; same code as addSignals, except now applied to the pileup events
169 
170  // EB first
171 
172  std::shared_ptr<Wrapper<EBRecHitCollection> const> EBRecHitsPTR =
173  getProductByTag<EBRecHitCollection>(*ep, EBPileRecHitInputTag_, mcc);
174 
175  if(EBRecHitsPTR ) {
176 
177  const EBRecHitCollection* EBRecHits = const_cast< EBRecHitCollection * >(EBRecHitsPTR->product());
178 
179  LogDebug("DataMixingEMWorker") << "total # EB rechits: " << EBRecHits->size();
180 
181  // loop over digis, adding these to the existing maps
182  for(EBRecHitCollection::const_iterator it = EBRecHits->begin();
183  it != EBRecHits->end(); ++it) {
184 
185  EBRecHitStorage_.insert(EBRecHitMap::value_type( (it->id()), *it ));
186 
187 #ifdef DEBUG
188  LogDebug("DataMixingEMWorker") << "processed EBRecHit with rawId: "
189  << it->id().rawId() << "\n"
190  << " rechit energy: " << it->energy();
191 #endif
192  }
193  }
194 
195  // EE Next
196 
197  std::shared_ptr<Wrapper<EERecHitCollection> const> EERecHitsPTR =
198  getProductByTag<EERecHitCollection>(*ep, EEPileRecHitInputTag_, mcc);
199 
200  if(EERecHitsPTR ) {
201 
202  const EERecHitCollection* EERecHits = const_cast< EERecHitCollection * >(EERecHitsPTR->product());
203 
204  LogDebug("DataMixingEMWorker") << "total # EE rechits: " << EERecHits->size();
205 
206  // loop over digis, adding these to the existing maps
207  for(EERecHitCollection::const_iterator it = EERecHits->begin();
208  it != EERecHits->end(); ++it) {
209 
210  EERecHitStorage_.insert(EERecHitMap::value_type( (it->id()), *it ));
211 
212 #ifdef DEBUG
213  LogDebug("DataMixingEMWorker") << "processed EERecHit with rawId: "
214  << it->id().rawId() << "\n"
215  << " rechit energy: " << it->energy();
216 #endif
217  }
218  }
219 
220  // ES Next
221 
222  std::shared_ptr<Wrapper<ESRecHitCollection> const> ESRecHitsPTR =
223  getProductByTag<ESRecHitCollection>(*ep, ESPileRecHitInputTag_, mcc);
224 
225  if(ESRecHitsPTR ) {
226 
227  const ESRecHitCollection* ESRecHits = const_cast< ESRecHitCollection * >(ESRecHitsPTR->product());
228 
229  LogDebug("DataMixingEMWorker") << "total # ES rechits: " << ESRecHits->size();
230 
231  // loop over digis, adding these to the existing maps
232  for(ESRecHitCollection::const_iterator it = ESRecHits->begin();
233  it != ESRecHits->end(); ++it) {
234 
235  ESRecHitStorage_.insert(ESRecHitMap::value_type( (it->id()), *it ));
236 
237 #ifdef DEBUG
238  LogDebug("DataMixingEMWorker") << "processed ESRecHit with rawId: "
239  << it->id().rawId() << "\n"
240  << " rechit energy: " << it->energy();
241 #endif
242  }
243  }
244 
245 
246  }
247 
249 
250  // collection of rechits to put in the event
251  std::unique_ptr< EBRecHitCollection > EBrechits( new EBRecHitCollection );
252  std::unique_ptr< EERecHitCollection > EErechits( new EERecHitCollection );
253  std::unique_ptr< ESRecHitCollection > ESrechits( new ESRecHitCollection );
254 
255  // loop over the maps we have, re-making individual hits or digis if necessary.
256  DetId formerID = 0;
257  DetId currentID;
258  float ESum = 0.;
259  float EBTime = 0.;
260 
261  // EB first...
262 
263  EBRecHitMap::const_iterator iEBchk;
264 
265  for(EBRecHitMap::const_iterator iEB = EBRecHitStorage_.begin();
266  iEB != EBRecHitStorage_.end(); ++iEB) {
267 
268  currentID = iEB->first;
269 
270  if (currentID == formerID) { // we have to add these rechits together
271 
272  ESum+=(iEB->second).energy();
273  }
274  else {
275  if(formerID>0) {
276  // cutoff for ESum?
277  EcalRecHit aHit(formerID, ESum, EBTime);
278  EBrechits->push_back( aHit );
279  }
280  //save pointers for next iteration
281  formerID = currentID;
282  ESum = (iEB->second).energy();
283  EBTime = (iEB->second).time(); // take time of first hit in sequence - is this ok?
284  }
285 
286  iEBchk = iEB;
287  if((++iEBchk) == EBRecHitStorage_.end()) { //make sure not to lose the last one
288  EcalRecHit aHit(formerID, ESum, EBTime);
289  EBrechits->push_back( aHit );
290  }
291  }
292 
293  // EE next...
294 
295  // loop over the maps we have, re-making individual hits or digis if necessary.
296  formerID = 0;
297  ESum = 0.;
298  float EETime = 0.;
299 
300  EERecHitMap::const_iterator iEEchk;
301 
302  for(EERecHitMap::const_iterator iEE = EERecHitStorage_.begin();
303  iEE != EERecHitStorage_.end(); ++iEE) {
304 
305  currentID = iEE->first;
306 
307  if (currentID == formerID) { // we have to add these rechits together
308 
309  ESum+=(iEE->second).energy();
310  }
311  else {
312  if(formerID>0) {
313  // cutoff for ESum?
314  EcalRecHit aHit(formerID, ESum, EETime);
315  EErechits->push_back( aHit );
316  }
317  //save pointers for next iteration
318  formerID = currentID;
319  ESum = (iEE->second).energy();
320  EETime = (iEE->second).time(); // take time of first hit in sequence - is this ok?
321  }
322 
323  iEEchk = iEE;
324  if((++iEEchk) == EERecHitStorage_.end()) { //make sure not to lose the last one
325  EcalRecHit aHit(formerID, ESum, EETime);
326  EErechits->push_back( aHit );
327  }
328  }
329 
330  // ES next...
331 
332  // loop over the maps we have, re-making individual hits or digis if necessary.
333  formerID = 0;
334  ESum = 0.;
335  float ESTime = 0.;
336 
337  ESRecHitMap::const_iterator iESchk;
338 
339  for(ESRecHitMap::const_iterator iES = ESRecHitStorage_.begin();
340  iES != ESRecHitStorage_.end(); ++iES) {
341 
342  currentID = iES->first;
343 
344  if (currentID == formerID) { // we have to add these rechits together
345 
346  ESum+=(iES->second).energy();
347  }
348  else {
349  if(formerID>0) {
350  // cutoff for ESum?
351  EcalRecHit aHit(formerID, ESum, ESTime);
352  ESrechits->push_back( aHit );
353  }
354  //save pointers for next iteration
355  formerID = currentID;
356  ESum = (iES->second).energy();
357  ESTime = (iES->second).time(); // take time of first hit in sequence - is this ok?
358  }
359 
360  iESchk = iES;
361  if((++iESchk) == ESRecHitStorage_.end()) { //make sure not to lose the last one
362  EcalRecHit aHit(formerID, ESum, ESTime);
363  ESrechits->push_back( aHit );
364  }
365  }
366 
367  // done merging
368 
369  // put the collection of reconstructed hits in the event
370  LogInfo("DataMixingEMWorker") << "total # EB Merged rechits: " << EBrechits->size() ;
371  LogInfo("DataMixingEMWorker") << "total # EE Merged rechits: " << EErechits->size() ;
372  LogInfo("DataMixingEMWorker") << "total # ES Merged rechits: " << ESrechits->size() ;
373 
374  e.put(std::move(EBrechits), EBRecHitCollectionDM_ );
375  e.put(std::move(EErechits), EERecHitCollectionDM_ );
376  e.put(std::move(ESrechits), ESRecHitCollectionDM_ );
377 
378  // clear local storage after this event
379 
380  EBRecHitStorage_.clear();
381  EERecHitStorage_.clear();
382  ESRecHitStorage_.clear();
383 
384  }
385 
386 } //edm
387 
388 // LocalWords: ESProducerSig
#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 putEM(edm::Event &e)
void addEMSignals(const edm::Event &e)
edm::EDGetTokenT< EERecHitCollection > EEPileRecHitToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
EventID const & id() const
std::vector< EcalRecHit >::const_iterator const_iterator
edm::InputTag EBrechitCollectionSig_
edm::InputTag ESPileRecHitInputTag_
edm::EDGetTokenT< EERecHitCollection > EERecHitToken_
edm::InputTag EErechitCollectionSig_
edm::EDGetTokenT< EBRecHitCollection > EBRecHitToken_
edm::EDGetTokenT< ESRecHitCollection > ESPileRecHitToken_
const_iterator end() const
Definition: DetId.h:18
edm::EDGetTokenT< ESRecHitCollection > ESRecHitToken_
edm::InputTag ESrechitCollectionSig_
T const * product() const
Definition: Handle.h:81
void addEMPileups(const int bcr, const edm::EventPrincipal *, unsigned int EventId, ModuleCallingContext const *)
edm::InputTag EBPileRecHitInputTag_
edm::InputTag EEPileRecHitInputTag_
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
size_type size() const
edm::EDGetTokenT< EBRecHitCollection > EBPileRecHitToken_
def move(src, dest)
Definition: eostools.py:511
const_iterator begin() const