CMS 3D CMS Logo

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