test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DataMixingHcalDigiWorker.cc
Go to the documentation of this file.
1 // File: DataMixingHcalDigiWorker.cc
2 // Description: see DataMixingHcalDigiWorker.h
3 // Author: Mike Hildreth, University of Notre Dame
4 //
5 //--------------------------------------------
6 
7 #include <map>
8 #include <memory>
18 // calibration headers, for future reference
23 //
24 //
26 
27 using namespace std;
28 
29 namespace {
30 
31  typedef std::multimap<DetId, CaloSamples> HcalDigiMap;
32 
33  template <class DIGI>
34  void convertFc2adc (const CaloSamples& fC, const HcalDbService& conditions,
35  DIGI& digi, int capIdOffset = 0) {
36  HcalDetId id = fC.id();
37  const HcalQIECoder* channelCoder = conditions.getHcalCoder (id);
38  const HcalQIEShape* shape = conditions.getHcalShape (channelCoder);
39  HcalCoderDb coder (*channelCoder, *shape);
40  coder.fC2adc(fC, digi, capIdOffset);
41  }
42 
43  template <class DIGI>
44  void convertAdc2fC (const DIGI& digi, const HcalDbService& conditions, bool keepPedestals, CaloSamples& fC) {
45  HcalDetId id (digi.id());
46  const HcalQIECoder* channelCoder = conditions.getHcalCoder (id);
47  const HcalQIEShape* shape = conditions.getHcalShape (channelCoder);
48  HcalCoderDb coder (*channelCoder, *shape);
49  coder.adc2fC(digi, fC);
50  if (!keepPedestals) { // subtract pedestals
51  const HcalCalibrations& calib = conditions.getHcalCalibrations(id);
52  for (int i = 0; i < digi.size(); ++i) {
53  int capId (digi.sample(i).capid());
54  fC[i] -= calib.pedestal (capId);
55  }
56  }
57  }
58 
59  template <class DIGIS>
60  void convertHcalDigis (const DIGIS& digis, const HcalDbService& conditions, bool keepPedestals, HcalDigiMap& map) {
61  for (auto digi = digis.begin(); digi != digis.end(); ++digi) {
62  CaloSamples fC;
63  convertAdc2fC (*digi, conditions, keepPedestals, fC);
64  if (!keepPedestals && map.find(digi->id()) == map.end()) {
65  edm::LogWarning("DataMixingHcalDigiWorker")<<"No signal hits found for HCAL cell "<<digi->id()<<" Pedestals may be lost for mixed hit";
66  }
67  map.insert(HcalDigiMap::value_type (digi->id(), fC));
68  }
69  }
70 
71  template <class DIGIS>
72  bool convertSignalHcalDigis (const edm::Event &e, const edm::EDGetTokenT<DIGIS>& token,
73  const HcalDbService& conditions, HcalDigiMap& map) {
74  edm::Handle<DIGIS> digis;
75  if (!e.getByToken (token, digis)) return false;
76  convertHcalDigis (*digis, conditions, true, map); // keep pedestals
77  return true;
78  }
79 
80  template <class DIGIS>
81  bool convertPileupHcalDigis (const edm::EventPrincipal& ep,
82  const edm::InputTag& tag, const edm::ModuleCallingContext* mcc,
83  const HcalDbService& conditions, HcalDigiMap& map) {
84  auto digis = edm::getProductByTag<DIGIS>(ep, tag, mcc);
85  if (!digis) return false;
86  convertHcalDigis (*(digis->product()), conditions, false, map); // subtract pedestals
87  return true;
88  }
89 
90  template <class DIGIS>
91  std::auto_ptr<DIGIS> buildHcalDigis (const HcalDigiMap& map, const HcalDbService& conditions) {
92  std::auto_ptr<DIGIS> digis( new DIGIS );
93  // loop over the maps we have, re-making individual hits or digis if necessary.
94  DetId formerID = 0;
95  CaloSamples resultSample;
96 
97  for(auto hit = map.begin(); hit != map.end(); ++hit) {
98  DetId currentID = hit->first;
99  const CaloSamples& hitSample = hit->second;
100 
101  if (currentID == formerID) { // accumulating hits
102  //loop over digi samples in each CaloSample
103  unsigned int sizenew = (hitSample).size();
104  unsigned int sizeold = resultSample.size();
105  if (sizenew > sizeold) { // extend sample
106  for (unsigned int isamp = sizeold; isamp < sizenew; ++isamp) resultSample[isamp] = 0;
107  resultSample.setSize (sizenew);
108  }
109  for(unsigned int isamp = 0; isamp<sizenew; isamp++) { // add new values
110  resultSample[isamp] += hitSample[isamp]; // for debugging below
111  }
112  }
113  auto hit1 = hit;
114  bool lastEntry = (++hit1 == map.end());
115  if (currentID != formerID || lastEntry) { // store current digi
116  if (formerID>0 || lastEntry) {
117  // make new digi
118  digis->push_back(typename DIGIS::value_type(formerID));
119  convertFc2adc (resultSample, conditions, digis->back(), 0); // FR guess: simulation starts with 0
120  }
121  //reset pointers for next iteration
122  formerID = currentID;
123  resultSample = hitSample;
124  }
125  }
126  return digis;
127  }
128 
129 } // namespace {}
130 
131 namespace edm
132 {
133 
134  // Virtual constructor
135 
136  DataMixingHcalDigiWorker::DataMixingHcalDigiWorker() { }
137 
138  // Constructor
139  DataMixingHcalDigiWorker::DataMixingHcalDigiWorker(const edm::ParameterSet& ps, edm::ConsumesCollector && iC) :
140  label_(ps.getParameter<std::string>("Label"))
141 
142  {
143 
144  // get the subdetector names
145  // this->getSubdetectorNames(); //something like this may be useful to check what we are supposed to do...
146 
147  // declare the products to produce
148 
149  // Hcal
150 
151  HBHEdigiCollectionSig_ = ps.getParameter<edm::InputTag>("HBHEdigiCollectionSig");
152  HOdigiCollectionSig_ = ps.getParameter<edm::InputTag>("HOdigiCollectionSig");
153  HFdigiCollectionSig_ = ps.getParameter<edm::InputTag>("HFdigiCollectionSig");
154  ZDCdigiCollectionSig_ = ps.getParameter<edm::InputTag>("ZDCdigiCollectionSig");
155 
156  HBHEPileInputTag_ = ps.getParameter<edm::InputTag>("HBHEPileInputTag");
157  HOPileInputTag_ = ps.getParameter<edm::InputTag>("HOPileInputTag");
158  HFPileInputTag_ = ps.getParameter<edm::InputTag>("HFPileInputTag");
159  ZDCPileInputTag_ = ps.getParameter<edm::InputTag>("ZDCPileInputTag");
160 
164 
168 
169  DoZDC_ = false;
170  if(ZDCPileInputTag_.label() != "") DoZDC_ = true;
171 
172  if(DoZDC_) {
175  }
176 
177 
178  HBHEDigiCollectionDM_ = ps.getParameter<std::string>("HBHEDigiCollectionDM");
179  HODigiCollectionDM_ = ps.getParameter<std::string>("HODigiCollectionDM");
180  HFDigiCollectionDM_ = ps.getParameter<std::string>("HFDigiCollectionDM");
181  ZDCDigiCollectionDM_ = ps.getParameter<std::string>("ZDCDigiCollectionDM");
182 
183 
184  }
185 
186  // Virtual destructor needed.
188  }
189 
191  // Calibration stuff will look like this:
192  // get conditions
193  edm::ESHandle<HcalDbService> conditions;
194  ES.get<HcalDbRecord>().get(conditions);
195 
196 
197  // fill in maps of hits
198 
199  LogInfo("DataMixingHcalDigiWorker")<<"===============> adding MC signals for "<<e.id();
200  convertSignalHcalDigis<HBHEDigiCollection> (e, HBHEDigiToken_, *conditions, HBHEDigiStorage_);
201  convertSignalHcalDigis<HODigiCollection> (e, HODigiToken_, *conditions, HODigiStorage_);
202  convertSignalHcalDigis<HFDigiCollection> (e, HFDigiToken_, *conditions, HFDigiStorage_);
203 
204 
205 
206  // ZDC next
207 
208  if(DoZDC_){
209 
210  Handle< ZDCDigiCollection > pZDCDigis;
211 
212  const ZDCDigiCollection* ZDCDigis = 0;
213 
214  if( e.getByToken( ZDCDigiToken_, pZDCDigis) ) {
215  ZDCDigis = pZDCDigis.product(); // get a ptr to the product
216 #ifdef DEBUG
217  LogDebug("DataMixingHcalDigiWorker") << "total # ZDC digis: " << ZDCDigis->size();
218 #endif
219  }
220 
221 
222  if (ZDCDigis)
223  {
224  // loop over digis, storing them in a map so we can add pileup later
225  for(ZDCDigiCollection::const_iterator it = ZDCDigis->begin();
226  it != ZDCDigis->end(); ++it) {
227 
228  // calibration, for future reference: (same block for all Hcal types) ZDC is different
229  HcalZDCDetId cell = it->id();
230  // const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);
231  const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
232  const HcalQIEShape* shape = conditions->getHcalShape (channelCoder); // this one is generic
233  HcalCoderDb coder (*channelCoder, *shape);
234 
235  CaloSamples tool;
236  coder.adc2fC((*it),tool);
237 
238  ZDCDigiStorage_.insert(ZDCDigiMap::value_type( ( it->id() ), tool ));
239 
240 #ifdef DEBUG
241  // Commented out because this does not compile anymore
242  // LogDebug("DataMixingHcalDigiWorker") << "processed ZDCDigi with rawId: "
243  // << it->id() << "\n"
244  // << " digi energy: " << it->energy();
245 #endif
246 
247  }
248  }
249  }
250 
251  } // end of addHCalSignals
252 
253  void DataMixingHcalDigiWorker::addHcalPileups(const int bcr, const EventPrincipal *ep, unsigned int eventNr,const edm::EventSetup& ES,
254  ModuleCallingContext const* mcc) {
255 
256  LogDebug("DataMixingHcalDigiWorker") <<"\n===============> adding pileups from event "<<ep->id()<<" for bunchcrossing "<<bcr;
257 
258  // get conditions
259  edm::ESHandle<HcalDbService> conditions;
260  ES.get<HcalDbRecord>().get(conditions);
261 
262  convertPileupHcalDigis<HBHEDigiCollection> (*ep, HBHEPileInputTag_, mcc, *conditions, HBHEDigiStorage_);
263  convertPileupHcalDigis<HODigiCollection> (*ep, HOPileInputTag_, mcc, *conditions, HODigiStorage_);
264  convertPileupHcalDigis<HFDigiCollection> (*ep, HFPileInputTag_, mcc, *conditions, HFDigiStorage_);
265 
266 
267  // ZDC Next
268 
269  if(DoZDC_) {
270 
271 
272  std::shared_ptr<Wrapper<ZDCDigiCollection> const> ZDCDigisPTR =
273  getProductByTag<ZDCDigiCollection>(*ep, ZDCPileInputTag_, mcc);
274 
275  if(ZDCDigisPTR ) {
276 
277  const ZDCDigiCollection* ZDCDigis = const_cast< ZDCDigiCollection * >(ZDCDigisPTR->product());
278 
279  LogDebug("DataMixingHcalDigiWorker") << "total # ZDC digis: " << ZDCDigis->size();
280 
281  // loop over digis, adding these to the existing maps
282  for(ZDCDigiCollection::const_iterator it = ZDCDigis->begin();
283  it != ZDCDigis->end(); ++it) {
284 
285  // calibration, for future reference: (same block for all Hcal types) ZDC is different
286  HcalZDCDetId cell = it->id();
287  // const HcalCalibrations& calibrations=conditions->getHcalCalibrations(cell);
288  const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
289  const HcalQIEShape* shape = conditions->getHcalShape (channelCoder); // this one is generic
290  HcalCoderDb coder (*channelCoder, *shape);
291 
292  CaloSamples tool;
293  coder.adc2fC((*it),tool);
294 
295  ZDCDigiStorage_.insert(ZDCDigiMap::value_type( (it->id()), tool ));
296 
297 #ifdef DEBUG
298  // Commented out because this does not compile anymore
299  // LogDebug("DataMixingHcalDigiWorker") << "processed ZDCDigi with rawId: "
300  // << it->id() << "\n"
301  // << " digi energy: " << it->energy();
302 #endif
303  }
304  }
305  }
306 
307 
308  }
309 
311  edm::ESHandle<HcalDbService> conditions;
312  ES.get<HcalDbRecord>().get(conditions);
313 
314  // collection of digis to put in the event
315  std::auto_ptr< HBHEDigiCollection > HBHEdigis = buildHcalDigis<HBHEDigiCollection> (HBHEDigiStorage_, *conditions);
316  std::auto_ptr< HODigiCollection > HOdigis = buildHcalDigis<HODigiCollection> (HODigiStorage_, *conditions);
317  std::auto_ptr< HFDigiCollection > HFdigis = buildHcalDigis<HFDigiCollection> (HFDigiStorage_, *conditions);
318  std::auto_ptr< ZDCDigiCollection > ZDCdigis( new ZDCDigiCollection );
319 
320  // loop over the maps we have, re-making individual hits or digis if necessary.
321  DetId formerID = 0;
322  DetId currentID;
323 
324  double fC_new;
325  double fC_old;
326  double fC_sum;
327 
328 
329  // ZDC next...
330 
331  // loop over the maps we have, re-making individual hits or digis if necessary.
332  formerID = 0;
333  CaloSamples ZDC_old;
334 
335  ZDCDigiMap::const_iterator iZDCchk;
336 
337  for(ZDCDigiMap::const_iterator iZDC = ZDCDigiStorage_.begin();
338  iZDC != ZDCDigiStorage_.end(); ++iZDC) {
339 
340  currentID = iZDC->first;
341 
342  if (currentID == formerID) { // we have to add these digis together
343 
344  //loop over digi samples in each CaloSample
345  unsigned int sizenew = (iZDC->second).size();
346  unsigned int sizeold = ZDC_old.size();
347 
348  unsigned int max_samp = std::max(sizenew, sizeold);
349 
350  CaloSamples ZDC_bigger(currentID,max_samp);
351 
352  bool usenew = false;
353 
354  if(sizenew > sizeold) usenew = true;
355 
356  // samples from different events can be of different lengths - sum all
357  // that overlap.
358 
359  for(unsigned int isamp = 0; isamp<max_samp; isamp++) {
360  if(isamp < sizenew) {
361  fC_new = (iZDC->second)[isamp];
362  }
363  else { fC_new = 0;}
364 
365  if(isamp < sizeold) {
366  fC_old = ZDC_old[isamp];
367  }
368  else { fC_old = 0;}
369 
370  // add values
371  fC_sum = fC_new + fC_old;
372 
373  if(usenew) {ZDC_bigger[isamp] = fC_sum; }
374  else { ZDC_old[isamp] = fC_sum; } // overwrite old sample, adding new info
375 
376  }
377  if(usenew) ZDC_old = ZDC_bigger; // save new, larger sized sample in "old" slot
378 
379  }
380  else {
381  if(formerID>0) {
382  // make new digi
383  ZDCdigis->push_back(ZDCDataFrame(formerID));
384 
385  // set up information to convert back
386 
387  HcalZDCDetId cell = ZDC_old.id();
388  const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
389  const HcalQIEShape* shape = conditions->getHcalShape (channelCoder); // this one is generic
390  HcalCoderDb coder (*channelCoder, *shape);
391 
392  unsigned int sizeold = ZDC_old.size();
393  for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
394  coder.fC2adc(ZDC_old,(ZDCdigis->back()), 1 ); // as per simulation, capid=1
395  }
396  }
397  //save pointers for next iteration
398  formerID = currentID;
399  ZDC_old = iZDC->second;
400  }
401 
402  iZDCchk = iZDC;
403  if((++iZDCchk) == ZDCDigiStorage_.end()) { //make sure not to lose the last one
404  // make new digi
405  ZDCdigis->push_back(ZDCDataFrame(currentID));
406 
407  // set up information to convert back
408 
409  HcalZDCDetId cell = (iZDC->second).id();
410  const HcalQIECoder* channelCoder = conditions->getHcalCoder (cell);
411  const HcalQIEShape* shape = conditions->getHcalShape (channelCoder); // this one is generic
412  HcalCoderDb coder (*channelCoder, *shape);
413 
414  unsigned int sizeold = (iZDC->second).size();
415  for(unsigned int isamp = 0; isamp<sizeold; isamp++) {
416  coder.fC2adc(ZDC_old,(ZDCdigis->back()), 1 ); // as per simulation, capid=1
417  }
418 
419  }
420  }
421 
422 
423 
424  //done merging
425 
426  // put the collection of recunstructed hits in the event
427  LogInfo("DataMixingHcalDigiWorker") << "total # HBHE Merged digis: " << HBHEdigis->size() ;
428  LogInfo("DataMixingHcalDigiWorker") << "total # HO Merged digis: " << HOdigis->size() ;
429  LogInfo("DataMixingHcalDigiWorker") << "total # HF Merged digis: " << HFdigis->size() ;
430  LogInfo("DataMixingHcalDigiWorker") << "total # ZDC Merged digis: " << ZDCdigis->size() ;
431 
432 
433  // make empty collections for now:
434  std::auto_ptr<HBHEUpgradeDigiCollection> hbheupgradeResult(new HBHEUpgradeDigiCollection());
435  std::auto_ptr<HFUpgradeDigiCollection> hfupgradeResult(new HFUpgradeDigiCollection());
436 
437 
438  e.put( HBHEdigis, HBHEDigiCollectionDM_ );
439  e.put( HOdigis, HODigiCollectionDM_ );
440  e.put( HFdigis, HFDigiCollectionDM_ );
441  e.put( ZDCdigis, ZDCDigiCollectionDM_ );
442  e.put( hbheupgradeResult, "HBHEUpgradeDigiCollection" );
443  e.put( hfupgradeResult, "HFUpgradeDigiCollection" );
444 
445  // clear local storage after this event
446  HBHEDigiStorage_.clear();
447  HODigiStorage_.clear();
448  HFDigiStorage_.clear();
449  ZDCDigiStorage_.clear();
450 
451  }
452 
453 } //edm
#define LogDebug(id)
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
edm::EDGetTokenT< HBHEDigiCollection > HBHEDigiPToken_
edm::EDGetTokenT< HODigiCollection > HODigiToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
edm::EDGetTokenT< ZDCDigiCollection > ZDCDigiToken_
void setSize(unsigned int size)
Definition: CaloSamples.h:55
edm::EDGetTokenT< HODigiCollection > HODigiPToken_
EventID const & id() const
std::vector< ZDCDataFrame >::const_iterator const_iterator
double pedestal(int fCapId) const
get pedestal for capid=0..3
void addHcalPileups(const int bcr, const edm::EventPrincipal *, unsigned int EventId, const edm::EventSetup &ES, ModuleCallingContext const *)
virtual void fC2adc(const CaloSamples &clf, HBHEDataFrame &df, int fCapIdOffset) const
Definition: HcalCoderDb.cc:68
edm::EDGetTokenT< ZDCDigiCollection > ZDCDigiPToken_
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
edm::EDGetTokenT< HFDigiCollection > HFDigiPToken_
void putHcal(edm::Event &e, const edm::EventSetup &ES)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
virtual void adc2fC(const HBHEDataFrame &df, CaloSamples &lf) const
Definition: HcalCoderDb.cc:60
edm::SortedCollection< HcalUpgradeDataFrame > HFUpgradeDigiCollection
edm::SortedCollection< HcalUpgradeDataFrame > HBHEUpgradeDigiCollection
const_iterator end() const
Definition: DetId.h:18
T const * product() const
Definition: Handle.h:81
int size() const
get the size
Definition: CaloSamples.h:24
const T & get() const
Definition: EventSetup.h:56
const HcalQIECoder * getHcalCoder(const HcalGenericDetId &fId) const
edm::EDGetTokenT< HFDigiCollection > HFDigiToken_
const HcalQIEShape * getHcalShape(const HcalGenericDetId &fId) const
std::string const & label() const
Definition: InputTag.h:36
edm::EventID id() const
Definition: EventBase.h:59
size_type size() const
DetId id() const
get the (generic) id
Definition: CaloSamples.h:21
const HcalCalibrations & getHcalCalibrations(const HcalGenericDetId &fId) const
void addHcalSignals(const edm::Event &e, const edm::EventSetup &ES)
tuple size
Write out results.
const_iterator begin() const
edm::EDGetTokenT< HBHEDigiCollection > HBHEDigiToken_