CMS 3D CMS Logo

EcalTBWeightUncalibRecHitProducer.cc
Go to the documentation of this file.
1 
15 
16 #include <iostream>
17 #include <iomanip>
18 #include <cmath>
19 
21 
23 
26 
29 
33 
38 
42 
43 #include "CLHEP/Matrix/Matrix.h"
44 #include "CLHEP/Matrix/SymMatrix.h"
45 #include <vector>
46 
47 #define DEBUG
49 testbeamEEShape(EEShape(false)), // Shapes have been updated in 2018 such as to be able to fetch shape from the DB if EBShape(true)//EEShape(true) are used
50 testbeamEBShape(EBShape(false)) // use false as argument if you would rather prefer to use Phase I hardcoded shapes (18.05.2018 K. Theofilatos)
51 {
52 
53  EBdigiCollection_ = ps.getParameter<edm::InputTag>("EBdigiCollection");
54  EEdigiCollection_ = ps.getParameter<edm::InputTag>("EEdigiCollection");
55  tdcRecInfoCollection_ = ps.getParameter<edm::InputTag>("tdcRecInfoCollection");
56  EBhitCollection_ = ps.getParameter<std::string>("EBhitCollection");
57  EEhitCollection_ = ps.getParameter<std::string>("EEhitCollection");
58  nbTimeBin_ = ps.getParameter<int>("nbTimeBin");
59  use2004OffsetConvention_ = ps.getUntrackedParameter< bool >("use2004OffsetConvention",false);
60  produces< EBUncalibratedRecHitCollection >(EBhitCollection_);
61  produces< EEUncalibratedRecHitCollection >(EEhitCollection_);
62 }
63 
65 }
66 
67 void
69 {
70 
71  using namespace edm;
72 
74  const EBDigiCollection* EBdigis =nullptr;
75  if (!EBdigiCollection_.label().empty() || !EBdigiCollection_.instance().empty())
76  {
77  // evt.getByLabel( digiProducer_, EBdigiCollection_, pEBDigis);
78  evt.getByLabel( EBdigiCollection_, pEBDigis);
79  if (!pEBDigis.isValid()) {
80  edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product " << EBdigiCollection_ ;
81  } else {
82  EBdigis = pEBDigis.product(); // get a ptr to the produc
83 #ifdef DEBUG
84  LogDebug("EcalUncalibRecHitInfo") << "total # EBdigis: " << EBdigis->size() ;
85 #endif
86  }
87  }
88 
90  const EEDigiCollection* EEdigis =nullptr;
91 
92  if (!EEdigiCollection_.label().empty() || !EEdigiCollection_.instance().empty())
93  {
94  // evt.getByLabel( digiProducer_, EEdigiCollection_, pEEDigis);
95  evt.getByLabel( EEdigiCollection_, pEEDigis);
96  if (!pEEDigis.isValid()) {
97  edm::LogError("EcalUncalibRecHitError") << "Error! can't get the product " << EEdigiCollection_ ;
98  } else {
99  EEdigis = pEEDigis.product(); // get a ptr to the produc
100 #ifdef DEBUG
101  LogDebug("EcalUncalibRecHitInfo") << "total # EEdigis: " << EEdigis->size() ;
102  // std::cout << "total # EEdigis: " << EEdigis->size() << std::endl;
103 #endif
104  }
105  }
106 
107 
108  if (!EBdigis && !EEdigis)
109  return;
110 
112  const EcalTBTDCRecInfo* recTDC =nullptr;
113 
114  // evt.getByLabel( digiProducer_, EBdigiCollection_, pEBDigis);
115  evt.getByLabel( tdcRecInfoCollection_, pRecTDC);
116  if (pRecTDC.isValid()) {
117  recTDC = pRecTDC.product(); // get a ptr to the product
118  }
119 
120  // fetch map of groups of xtals
122  es.get<EcalWeightXtalGroupsRcd>().get(pGrp);
123  const EcalWeightXtalGroups* grp = pGrp.product();
124  if (!grp)
125  return;
126 
127  const EcalXtalGroupsMap& grpMap = grp->getMap();
128 
129 
130  // Gain Ratios
132  es.get<EcalGainRatiosRcd>().get(pRatio);
133  const EcalGainRatioMap& gainMap = pRatio.product()->getMap(); // map of gain ratios
134 
135 
136  // fetch TB weights
137 #ifdef DEBUG
138  LogDebug("EcalUncalibRecHitDebug") <<"Fetching EcalTBWeights from DB " ;
139  // std::cout <<"Fetching EcalTBWeights from DB " ;
140 #endif
142  es.get<EcalTBWeightsRcd>().get(pWgts);
143  const EcalTBWeights* wgts = pWgts.product();
144 
145  if (!wgts)
146  return;
147 
148 #ifdef DEBUG
149  LogDebug("EcalUncalibRecHitDebug") << "EcalTBWeightMap.size(): " << std::setprecision(3) << wgts->getMap().size() ;
150  // std::cout << "EcalTBWeightMap.size(): " << std::setprecision(3) << wgts->getMap().size() ;
151 #endif
152 
153  // fetch the pedestals from the cond DB via EventSetup
154 #ifdef DEBUG
155  LogDebug("EcalUncalibRecHitDebug") << "fetching pedestals....";
156  // std::cout << "fetching pedestals....";
157 #endif
159  es.get<EcalPedestalsRcd>().get( pedHandle );
160  const EcalPedestalsMap& pedMap = pedHandle.product()->getMap(); // map of pedestals
161 #ifdef DEBUG
162  LogDebug("EcalUncalibRecHitDebug") << "done." ;
163  // std::cout << "done." ;
164 #endif
165  // collection of reco'ed ampltudes to put in the event
166 
167  auto EBuncalibRechits = std::make_unique<EBUncalibratedRecHitCollection>();
168  auto EEuncalibRechits = std::make_unique<EEUncalibratedRecHitCollection>();
169 
170  EcalPedestalsMapIterator pedIter; // pedestal iterator
171 
172  EcalGainRatioMap::const_iterator gainIter; // gain iterator
173 
174  EcalXtalGroupsMap::const_iterator git; // group iterator
175 
176  EcalTBWeights::EcalTBWeightMap::const_iterator wit; //weights iterator
177  // loop over EB digis
178  //Getting the TDC bin
179  EcalTBWeights::EcalTDCId tdcid(int(nbTimeBin_/2)+1);
180 
181  if (recTDC)
182  if (recTDC->offset() == -999.)
183  {
184  edm::LogError("EcalUncalibRecHitError") << "TDC bin completely out of range. Returning" ;
185  return;
186  }
187 
188  if (EBdigis)
189  {
190  for(unsigned int idig = 0; idig < EBdigis->size(); ++idig) {
191 
192  EBDataFrame itdg = (*EBdigis)[idig];
193 
194 
195  // counter_++; // verbosity counter
196 
197  // find pedestals for this channel
198 #ifdef DEBUG
199  LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EBDetId(itdg.id()) ;
200 #endif
201  pedIter = pedMap.find(itdg.id().rawId());
202  if( pedIter == pedMap.end() ) {
203  edm::LogError("EcalUncalibRecHitError") << "error!! could not find pedestals for channel: " << EBDetId(itdg.id())
204  << "\n no uncalib rechit will be made for this digi!"
205  ;
206  continue;
207  }
208  const EcalPedestals::Item& aped = (*pedIter);
209  double pedVec[3];
210  double pedRMSVec[3];
211  pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
212  pedRMSVec[0]=aped.rms_x12;pedRMSVec[1]=aped.rms_x6;pedRMSVec[2]=aped.rms_x1;
213 
214  // find gain ratios
215 #ifdef DEBUG
216  LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EBDetId(itdg.id()) ;
217 #endif
218  gainIter = gainMap.find(itdg.id().rawId());
219  if( gainIter == gainMap.end() ) {
220  edm::LogError("EcalUncalibRecHitError") << "error!! could not find gain ratios for channel: " << EBDetId(itdg.id())
221  << "\n no uncalib rechit will be made for this digi!"
222  ;
223  continue;
224  }
225  const EcalMGPAGainRatio& aGain = (*gainIter);
226  double gainRatios[3];
227  gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
228 
229 
230 
231  // lookup group ID for this channel
232  git = grpMap.find( itdg.id().rawId() );
233  if( git == grpMap.end() ) {
234  edm::LogError("EcalUncalibRecHitError") << "No group id found for this crystal. something wrong with EcalWeightXtalGroups in your DB?"
235  << "\n no uncalib rechit will be made for digi with id: " << EBDetId(itdg.id())
236  ;
237  continue;
238  }
239  const EcalXtalGroupId& gid = (*git);
240 
241 
242  //GAIN SWITCHING DETECTION ///////////////////////////////////////////////////////////////////////////////////////////////////
243  double sampleGainRef = 1;
244  int sampleSwitch = 999;
245  for (int sample = 0; sample < itdg.size(); ++sample)
246  {
247  double gainSample = itdg.sample(sample).gainId();
248  if(gainSample != sampleGainRef) {sampleGainRef = gainSample; sampleSwitch = sample;}
249  }//loop sample
251 
252  if (recTDC)
253  {
254  int tdcBin=0;
255  if (recTDC->offset() <= 0.)
256  tdcBin = 1;
257  if (recTDC->offset() >= 1.)
258  tdcBin = nbTimeBin_;
259  else
260  tdcBin = int(recTDC->offset()*float(nbTimeBin_))+1;
261 
262  if (tdcBin < 1 || tdcBin > nbTimeBin_ )
263  {
264  edm::LogError("EcalUncalibRecHitError") << "TDC bin out of range " << tdcBin << " offset " << recTDC->offset();
265  continue;
266  }
267 
268  // In case gain switching happens at the sample 4 (5th sample)
269  // (sample 5 (6th sample) in 2004 TDC convention) an extra
270  // set of weights has to be used. This set of weights is assigned to
271  // TDC values going from 25 and up.
272  if (use2004OffsetConvention_ && sampleSwitch == 5)
273  tdcid=EcalTBWeights::EcalTDCId(tdcBin+25);
274  else if (!use2004OffsetConvention_ && sampleSwitch == 4)
275  tdcid=EcalTBWeights::EcalTDCId(tdcBin+25);
276  else
277  tdcid=EcalTBWeights::EcalTDCId(tdcBin);
278  }//check TDC
279 
280  // now lookup the correct weights in the map
281  wit = wgts->getMap().find( std::make_pair(gid,tdcid) );
282  if( wit == wgts->getMap().end() ) { // no weights found for this group ID
283  edm::LogError("EcalUncalibRecHitError") << "No weights found for EcalGroupId: " << gid.id() << " and EcalTDCId: " << tdcid
284  << "\n skipping digi with id: " << EBDetId(itdg.id())
285  ;
286  continue;
287  }
288  const EcalWeightSet& wset = wit->second; // this is the EcalWeightSet
289 
290 
291  // EcalWeightMatrix is vec<vec:double>>
292 
293 #ifdef DEBUG
294  LogDebug("EcalUncalibRecHitDebug") << "accessing matrices of weights...";
295 #endif
298  //const EcalWeightSet::EcalChi2WeightMatrix& mat3 = wset.getChi2WeightsBeforeGainSwitch();
299  //const EcalWeightSet::EcalChi2WeightMatrix& mat4 = wset.getChi2WeightsAfterGainSwitch();
301  weights[0]=&mat1;
302  weights[1]=&mat2;
303  // weights.push_back(clmat1);
304  // weights.push_back(clmat2);
305  // LogDebug("EcalUncalibRecHitDebug") << "weights before switch:\n" << clmat1 ;
306  // LogDebug("EcalUncalibRecHitDebug") << "weights after switch:\n" << clmat2 ;
307 
308 
309  // build CLHEP chi2 matrices
310  //const EcalWeightSet::EcalChi2WeightMatrix* chi2mat[2];
311  // chi2mat[0]=&mat3;
312  // chi2mat[1]=&mat4;
313 
315  EBalgo_.makeRecHit(itdg, pedVec, pedRMSVec, gainRatios, weights, testbeamEBShape);
316  //EBalgo_.makeRecHit(itdg, pedVec, gainRatios, weights, chi2mat);
317  EBuncalibRechits->push_back( aHit );
318 #ifdef DEBUG
319  if(aHit.amplitude()>0.) {
320  LogDebug("EcalUncalibRecHitDebug") << "processed EBDataFrame with id: "
321  << EBDetId(itdg.id()) << "\n"
322  << "uncalib rechit amplitude: " << aHit.amplitude()
323  ;
324  }
325 #endif
326  }
327  }
328  // put the collection of reconstructed hits in the event
329  evt.put(std::move(EBuncalibRechits), EBhitCollection_);
330 
331 
332  if (EEdigis)
333  {
334  for(unsigned int idig = 0; idig < EEdigis->size(); ++idig) {
335 
336  EEDataFrame itdg = (*EEdigis)[idig];
337 
338  // counter_++; // verbosity counter
339 
340  // find pedestals for this channel
341 #ifdef DEBUG
342  LogDebug("EcalUncalibRecHitDebug") << "looking up pedestal for crystal: " << EEDetId(itdg.id()) ;
343  // std::cout << "looking up pedestal for crystal: " << EEDetId(itdg.id()) ;
344 #endif
345  pedIter = pedMap.find(itdg.id().rawId());
346  if( pedIter == pedMap.end() ) {
347  edm::LogError("EcalUncalibRecHitError") << "error!! could not find pedestals for channel: " << EEDetId(itdg.id())
348  << "\n no uncalib rechit will be made for this digi!"
349  ;
350  continue;
351  }
352  const EcalPedestals::Item& aped = (*pedIter);
353  double pedVec[3];
354  double pedRMSVec[3];
355  pedVec[0]=aped.mean_x12;pedVec[1]=aped.mean_x6;pedVec[2]=aped.mean_x1;
356  pedRMSVec[0]=aped.rms_x12;pedRMSVec[1]=aped.rms_x6;pedRMSVec[2]=aped.rms_x1;
357 
358  // find gain ratios
359 #ifdef DEBUG
360  LogDebug("EcalUncalibRecHitDebug") << "looking up gainRatios for crystal: " << EEDetId(itdg.id()) ;
361  // std::cout << "looking up gainRatios for crystal: " << EEDetId(itdg.id()) ;
362 #endif
363  gainIter = gainMap.find(itdg.id().rawId());
364  if( gainIter == gainMap.end() ) {
365  edm::LogError("EcalUncalibRecHitError") << "error!! could not find gain ratios for channel: " << EEDetId(itdg.id())
366  << "\n no uncalib rechit will be made for this digi!"
367  ;
368  continue;
369  }
370  const EcalMGPAGainRatio& aGain = (*gainIter);
371  double gainRatios[3];
372  gainRatios[0]=1.;gainRatios[1]=aGain.gain12Over6();gainRatios[2]=aGain.gain6Over1()*aGain.gain12Over6();
373 
374 
375 
376  // lookup group ID for this channel
377  git = grpMap.find( itdg.id().rawId() );
378  if( git == grpMap.end() ) {
379  edm::LogError("EcalUncalibRecHitError") << "No group id found for this crystal. something wrong with EcalWeightXtalGroups in your DB?"
380  << "\n no uncalib rechit will be made for digi with id: " << EEDetId(itdg.id())
381  ;
382  continue;
383  }
384  const EcalXtalGroupId& gid = (*git);
385 
386 
387  //GAIN SWITCHING DETECTION ///////////////////////////////////////////////////////////////////////////////////////////////////
388  double sampleGainRef = 1;
389  int sampleSwitch = 999;
390  for (int sample = 0; sample < itdg.size(); ++sample)
391  {
392  double gainSample = itdg.sample(sample).gainId();
393  if(gainSample != sampleGainRef) {sampleGainRef = gainSample; sampleSwitch = sample;}
394  }//loop sample
396 
397  if (recTDC)
398  {
399  int tdcBin=0;
400  if (recTDC->offset() <= 0.)
401  tdcBin = 1;
402  if (recTDC->offset() >= 1.)
403  tdcBin = nbTimeBin_;
404  else
405  tdcBin = int(recTDC->offset()*float(nbTimeBin_))+1;
406 
407  if (tdcBin < 1 || tdcBin > nbTimeBin_ )
408  {
409  edm::LogError("EcalUncalibRecHitError") << "TDC bin out of range " << tdcBin << " offset " << recTDC->offset();
410  continue;
411  }
412 
413  // In case gain switching happens at the sample 4 (5th sample)
414  // (sample 5 (6th sample) in 2004 TDC convention) an extra
415  // set of weights has to be used. This set of weights is assigned to
416  // TDC values going from 25 and up.
417  if (use2004OffsetConvention_ && sampleSwitch == 5)
418  tdcid=EcalTBWeights::EcalTDCId(tdcBin+25);
419  else if (!use2004OffsetConvention_ && sampleSwitch == 4)
420  tdcid=EcalTBWeights::EcalTDCId(tdcBin+25);
421  else
422  tdcid=EcalTBWeights::EcalTDCId(tdcBin);
423  }//check TDC
424 
425  // now lookup the correct weights in the map
426  wit = wgts->getMap().find( std::make_pair(gid,tdcid) );
427  if( wit == wgts->getMap().end() ) { // no weights found for this group ID
428  edm::LogError("EcalUncalibRecHitError") << "No weights found for EcalGroupId: " << gid.id() << " and EcalTDCId: " << tdcid
429  << "\n skipping digi with id: " << EEDetId(itdg.id())
430  ;
431  continue;
432  }
433  const EcalWeightSet& wset = wit->second; // this is the EcalWeightSet
434 
435 
436  // EcalWeightMatrix is vec<vec:double>>
437 
438 #ifdef DEBUG
439  LogDebug("EcalUncalibRecHitDebug") << "accessing matrices of weights...";
440  // std::cout << "accessing matrices of weights...";
441 #endif
444  //const EcalWeightSet::EcalChi2WeightMatrix& mat3 = wset.getChi2WeightsBeforeGainSwitch();
445  //const EcalWeightSet::EcalChi2WeightMatrix& mat4 = wset.getChi2WeightsAfterGainSwitch();
447  weights[0]=&mat1;
448  weights[1]=&mat2;
449  // weights.push_back(clmat1);
450  // weights.push_back(clmat2);
451  // LogDebug("EcalUncalibRecHitDebug") << "weights before switch:\n" << clmat1 ;
452  // LogDebug("EcalUncalibRecHitDebug") << "weights after switch:\n" << clmat2 ;
453 
454 
455  // build CLHEP chi2 matrices
456  //const EcalWeightSet::EcalChi2WeightMatrix* chi2mat[2];
457  //chi2mat[0]=&mat3;
458  //chi2mat[1]=&mat4;
459 
461  EEalgo_.makeRecHit(itdg, pedVec, pedRMSVec, gainRatios, weights, testbeamEEShape);
462  //EEalgo_.makeRecHit(itdg, pedVec, gainRatios, weights, chi2mat);
463  EEuncalibRechits->push_back( aHit );
464 #ifdef DEBUG
465  if(aHit.amplitude()>0.) {
466  LogDebug("EcalUncalibRecHitDebug") << "processed EEDataFrame with id: "
467  << EEDetId(itdg.id()) << "\n"
468  << "uncalib rechit amplitude: " << aHit.amplitude()
469 
470 // std::cout << "processed EEDataFrame with id: "
471 // << EEDetId(itdg.id()) << "\n"
472 // << "uncalib rechit amplitude: " << aHit.amplitude() << std::endl;
473  ;
474  }
475 #endif
476  }
477  }
478  // put the collection of reconstructed hits in the event
479  evt.put(std::move(EEuncalibRechits), EEhitCollection_);
480 }
481 
482 // HepMatrix
483 // EcalTBWeightUncalibRecHitProducer::makeMatrixFromVectors(const std::vector< std::vector<EcalWeight> >& vecvec) {
484 // int nrow = vecvec.size();
485 // int ncol = (vecvec[0]).size();
486 // HepMatrix clmat(nrow,ncol);
487 // //LogDebug("EcalUncalibRecHitDebug") << "created HepMatrix(" << nrow << "," << ncol << ")" ;
488 // for(int irow=0;irow<nrow;++irow) {
489 // for(int icol=0;icol<ncol;++icol) {
490 // clmat[irow][icol] = ((vecvec[irow])[icol]).value();
491 // }
492 // }
493 // return clmat;
494 // }
495 
496 // HepMatrix
497 // EcalTBWeightUncalibRecHitProducer::makeDummySymMatrix(int size)
498 // {
499 // HepMatrix clmat(10,10);
500 // //LogDebug("EcalUncalibRecHitDebug") << "created HepMatrix(" << nrow << "," << ncol << ")" ;
501 // for(int irow=0; irow<size; ++irow) {
502 // for(int icol=0 ; icol<size; ++icol) {
503 // clmat[irow][icol] = irow+icol;
504 // }
505 // }
506 // return clmat;
507 // }
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
key_type id() const
Definition: EBDataFrame.h:31
const self & getMap() const
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
EcalMGPASample sample(int i) const
Definition: EcalDataFrame.h:29
int gainId() const
get the gainId (2 bits)
int size() const
Definition: EcalDataFrame.h:26
const unsigned int id() const
EcalPedestalsMap::const_iterator EcalPedestalsMapIterator
Definition: EcalPedestals.h:52
EcalTBWeightUncalibRecHitProducer(const edm::ParameterSet &ps)
virtual EcalUncalibratedRecHit makeRecHit(const C &dataFrame, const double *pedestals, const double *pedestalsRMS, const double *gainRatios, const EcalWeightSet::EcalWeightMatrix **weights, const EcalShapeBase &testbeamPulseShape)
Compute parameters.
float gain6Over1() const
bool isValid() const
Definition: HandleBase.h:74
EcalWeightMatrix & getWeightsAfterGainSwitch()
Definition: EcalWeightSet.h:30
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
key_type id() const
Definition: EEDataFrame.h:28
Definition: EBShape.h:6
EcalUncalibRecHitRecWeightsAlgo< EEDataFrame > EEalgo_
EcalWeightMatrix & getWeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:29
T const * product() const
Definition: Handle.h:74
std::vector< Item >::const_iterator const_iterator
float gain12Over6() const
std::string const & label() const
Definition: InputTag.h:36
HLT enums.
void produce(edm::Event &evt, const edm::EventSetup &es) override
T get() const
Definition: EventSetup.h:71
const_iterator find(uint32_t rawId) const
const EcalTBWeightMap & getMap() const
Definition: EcalTBWeights.h:31
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:22
const_iterator end() const
EcalUncalibRecHitRecWeightsAlgo< EBDataFrame > EBalgo_
T const * product() const
Definition: ESHandle.h:86
std::string const & instance() const
Definition: InputTag.h:37
def move(src, dest)
Definition: eostools.py:511
float offset() const
Definition: EEShape.h:6