CMS 3D CMS Logo

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