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