1 #ifndef RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH 2 #define RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH 11 #include "Math/SVector.h" 12 #include "Math/SMatrix.h" 27 inline float fast_expf(
float x) {
return unsafe_expf<6>(x); }
28 inline float fast_logf(
float x) {
return unsafe_logf<7>(x); }
56 const double *pedestals,
57 const double *pedestalRMSes,
58 const double *gainRatios,
59 std::vector<double> &timeFitParameters,
60 std::vector<double> &litudeFitParameters,
61 std::pair<double, double> &timeFitLimits);
66 const double *pedestals,
const double *pedestalRMSes,
67 const double *gainRatios);
68 void computeTime(std::vector<double> &timeFitParameters,
69 std::pair<double, double> &timeFitLimits,
70 std::vector<double> &litudeFitParameters);
71 void computeAmplitude(std::vector<double> &litudeFitParameters);
73 bool fixMGPAslew(
const C &dataFrame);
75 double computeAmplitudeImpl(std::vector<double> &litudeFitParameters,
97 const double *pedestals,
98 const double *pedestalRMSes,
99 const double *gainRatios) {
100 sampleMask_ = sampleMask;
101 theDetId_ =
DetId(dataFrame.id().rawId());
103 calculatedRechit_.timeMax = 5;
104 calculatedRechit_.amplitudeMax = 0;
105 calculatedRechit_.timeError = -999;
114 if (dataFrame.sample(0).gainId() == 1 &&
115 sampleMask_.useSample(0, theDetId_)) {
116 pedestal_ += double(dataFrame.sample(0).adc());
119 if (num_ != 0 && dataFrame.sample(1).gainId() == 1 &&
120 sampleMask_.useSample(1, theDetId_) &&
121 std::abs(dataFrame.sample(1).adc() - dataFrame.sample(0).adc()) <
122 3 * pedestalRMSes[0]) {
123 pedestal_ += double(dataFrame.sample(1).adc());
129 pedestal_ = pedestals[0];
136 double ampMaxValue = -1000;
144 for (
int iSample = 0; iSample < C::MAXSAMPLES; iSample++) {
146 GainId = dataFrame.sample(iSample).gainId();
151 if (!sampleMask_.useSample(iSample, theDetId_)) {
155 }
else if (GainId == 1) {
156 sample = double(dataFrame.sample(iSample).adc() - pedestal_);
157 sampleError = pedestalRMSes[0];
158 }
else if (GainId == 2 || GainId == 3) {
160 (double(dataFrame.sample(iSample).adc() - pedestals[GainId - 1])) *
161 gainRatios[GainId - 1];
162 sampleError = pedestalRMSes[GainId - 1] * gainRatios[GainId - 1];
170 useless_[iSample] = (sampleError <= 0) | bad;
171 amplitudes_[iSample] =
sample;
173 amplitudeErrors_[iSample] = useless_[iSample] ? 1
e+9 : sampleError;
174 amplitudeIE2_[iSample] =
177 : 1 / (amplitudeErrors_[iSample] * amplitudeErrors_[iSample]);
178 if (sampleError > 0) {
179 if (ampMaxValue < sample) {
181 ampMaxError_ = sampleError;
203 for (
int iSample = 1; iSample < C::MAXSAMPLES; iSample++) {
206 if (!sampleMask_.useSample(iSample, theDetId_))
continue;
208 GainIdPrev = dataFrame.sample(iSample - 1).gainId();
209 GainIdNext = dataFrame.sample(iSample).gainId();
210 if (GainIdPrev >= 1 && GainIdPrev <= 3 && GainIdNext >= 1 &&
211 GainIdNext <= 3 && GainIdPrev < GainIdNext) {
212 amplitudes_[iSample - 1] = 0;
213 amplitudeErrors_[iSample - 1] = 1
e+9;
214 amplitudeIE2_[iSample - 1] = 0;
215 useless_[iSample - 1] =
true;
225 std::vector<double> &timeFitParameters,
226 std::pair<double, double> &timeFitLimits,
227 std::vector<double> &litudeFitParameters) {
233 double ampMaxAlphaBeta = 0;
234 double tMaxAlphaBeta = 5;
235 double tMaxErrorAlphaBeta = 999;
237 double tMaxErrorRatio = 999;
248 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
249 if (useless_[
i])
continue;
250 double inverr2 = amplitudeIE2_[
i];
253 sumA += amplitudes_[
i] * inverr2;
254 sumAA += amplitudes_[
i] * (amplitudes_[
i] * inverr2);
257 NullChi2 = (sumAA - sumA * sumA / sum1) / sum0;
269 double alphabeta = amplitudeFitParameters[0] * amplitudeFitParameters[1];
270 double invalphabeta = 1. / alphabeta;
271 double alpha = amplitudeFitParameters[0];
272 double beta = amplitudeFitParameters[1];
274 Ratio ratios_[C::MAXSAMPLES * (C::MAXSAMPLES - 1) / 2];
275 unsigned int ratios_size = 0;
277 double Rlim[amplitudes_.size()];
278 for (
unsigned int k = 1;
k != amplitudes_.size(); ++
k)
281 double relErr2[amplitudes_.size()];
282 double invampl[amplitudes_.size()];
283 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
284 invampl[
i] = (useless_[
i]) ? 0 : 1. / amplitudes_[
i];
285 relErr2[
i] = (useless_[
i]) ? 0 : (amplitudeErrors_[
i] * invampl[
i]) *
286 (amplitudeErrors_[
i] * invampl[
i]);
289 for (
unsigned int i = 0;
i < amplitudes_.size() - 1;
i++) {
290 if (useless_[
i])
continue;
291 for (
unsigned int j = i + 1; j < amplitudes_.size(); j++) {
292 if (useless_[j])
continue;
294 if (amplitudes_[i] > 1 && amplitudes_[j] > 1) {
297 double Rtmp = amplitudes_[
i] / amplitudes_[j];
302 double err1 = Rtmp * Rtmp * (relErr2[
i] + relErr2[j]);
310 double err2 = amplitudeErrors_[j] * (amplitudes_[
i] - amplitudes_[j]) *
311 (invampl[j] * invampl[j]);
316 double err3 = (0.289 * 0.289) * (invampl[j] * invampl[j]);
318 double totalError =
std::sqrt(err1 + err2 + err3);
321 if (totalError < 1.0 && Rtmp > 0.001 && Rtmp < Rlim[j - i]) {
322 Ratio currentRatio = {
i, (j -
i), Rtmp, totalError };
323 ratios_[ratios_size++] = currentRatio;
330 if (0 == ratios_size)
return;
333 Tmax timesAB_[C::MAXSAMPLES * (C::MAXSAMPLES - 1) / 2];
334 unsigned int timesAB_size = 0;
339 for (
unsigned int i = 0;
i < ratios_size;
i++) {
341 double stepOverBeta = double(ratios_[
i].
step) /
beta;
344 double Rmin = ratios_[
i].value - ratios_[
i].error;
345 if (Rmin < 0.001) Rmin = 0.001;
347 double Rmax = ratios_[
i].value + ratios_[
i].error;
348 double RLimit = Rlim[ratios_[
i].step];
349 if (Rmax > RLimit) Rmax = RLimit;
363 double tmax = 0.5 * (time1 + time2);
364 double tmaxerr = 0.5 *
std::sqrt((time1 - time2) * (time1 - time2));
369 int itmin =
std::max(-1,
int(std::floor(tmax - alphabeta)));
370 double loffset = (double(itmin) -
tmax) * invalphabeta;
371 for (
unsigned int it = itmin + 1; it < amplitudes_.size(); it++) {
372 loffset += invalphabeta;
373 if (useless_[it])
continue;
374 double inverr2 = amplitudeIE2_[it];
376 double term1 = 1.0 + loffset;
382 sumAf += amplitudes_[it] * (f * inverr2);
383 sumff += f * (f * inverr2);
389 chi2 = sumAA - sumAf * (sumAf / sumff);
390 amp = (sumAf / sumff);
396 if (chi2 > 0 && tmaxerr > 0 && tmax > 0) {
399 timesAB_[timesAB_size++] = currentTmax;
404 if (0 == timesAB_size)
return;
407 double chi2min = 1.0e+9;
410 for (
unsigned int i = 0;
i < timesAB_size;
i++) {
411 if (timesAB_[
i].
chi2 < chi2min) {
412 chi2min = timesAB_[
i].
chi2;
420 double chi2Limit = chi2min + 1.0;
423 for (
unsigned int i = 0;
i < timesAB_size;
i++) {
424 if (timesAB_[
i].
chi2 < chi2Limit) {
425 double inverseSigmaSquared =
426 1.0 / (timesAB_[
i].error * timesAB_[
i].error);
427 time_wgt += inverseSigmaSquared;
428 time_max += timesAB_[
i].value * inverseSigmaSquared;
432 tMaxAlphaBeta = time_max / time_wgt;
433 tMaxErrorAlphaBeta = 1.0 /
sqrt(time_wgt);
438 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
439 if (useless_[
i])
continue;
440 double inverr2 = amplitudeIE2_[
i];
441 double offset = (double(i) - tMaxAlphaBeta) * invalphabeta;
442 double term1 = 1.0 +
offset;
445 sumAf += amplitudes_[
i] * (f * inverr2);
446 sumff += f * (f * inverr2);
451 ampMaxAlphaBeta = sumAf / sumff;
452 double chi2AlphaBeta = (sumAA - sumAf * sumAf / sumff) / sum0;
453 if (chi2AlphaBeta > NullChi2) {
473 if (ampMaxAlphaBeta / ampMaxError_ > 5.0) {
481 for (
unsigned int i = 0;
i < ratios_size;
i++) {
483 if (ratios_[
i].
step == 1 && ratios_[
i].
value >= timeFitLimits.first &&
484 ratios_[
i].value <= timeFitLimits.second) {
486 double time_max_i = ratios_[
i].index;
490 double u = timeFitParameters[timeFitParameters.size() - 1];
491 for (
int k = timeFitParameters.size() - 2;
k >= 0;
k--) {
492 u = u * ratios_[
i].value + timeFitParameters[
k];
496 double du = (timeFitParameters.size() - 1) *
497 timeFitParameters[timeFitParameters.size() - 1];
498 for (
int k = timeFitParameters.size() - 2;
k >= 1;
k--) {
499 du = du * ratios_[
i].value +
k * timeFitParameters[
k];
503 double errorsquared = ratios_[
i].error * ratios_[
i].error * du * du;
504 if (errorsquared > 0) {
506 time_max += (time_max_i - u) / errorsquared;
507 time_wgt += 1.0 / errorsquared;
519 tMaxRatio = time_max / time_wgt;
520 tMaxErrorRatio = 1.0 /
sqrt(time_wgt);
524 if (ampMaxAlphaBeta / ampMaxError_ > 10.0) {
528 calculatedRechit_.timeError = tMaxErrorRatio;
533 calculatedRechit_.timeMax =
534 (tMaxAlphaBeta * (10.0 - ampMaxAlphaBeta / ampMaxError_) +
535 tMaxRatio * (ampMaxAlphaBeta / ampMaxError_ - 5.0)) / 5.0;
536 calculatedRechit_.timeError =
537 (tMaxErrorAlphaBeta * (10.0 - ampMaxAlphaBeta / ampMaxError_) +
538 tMaxErrorRatio * (ampMaxAlphaBeta / ampMaxError_ - 5.0)) / 5.0;
545 calculatedRechit_.timeMax = tMaxAlphaBeta;
546 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
553 calculatedRechit_.timeMax = tMaxAlphaBeta;
554 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
561 std::vector<double> &litudeFitParameters) {
563 calculatedRechit_.amplitudeMax =
564 computeAmplitudeImpl(amplitudeFitParameters, 1., 1.);
570 std::vector<double> &litudeFitParameters,
double corr4,
double corr6) {
577 double alpha = amplitudeFitParameters[0];
578 double beta = amplitudeFitParameters[1];
582 double pedestalLimit = calculatedRechit_.timeMax - (alpha *
beta) - 1.0;
588 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
589 if (useless_[
i])
continue;
590 double inverr2 = amplitudeIE2_[
i];
592 double termOne = 1 + (i - calculatedRechit_.timeMax) / (alpha * beta);
595 (i - calculatedRechit_.timeMax) / beta);
599 if ((i < pedestalLimit) ||
600 (f > 0.6 * corr6 && i <= calculatedRechit_.timeMax) ||
601 (f > 0.4 * corr4 && i >= calculatedRechit_.timeMax)) {
603 sumA += amplitudes_[
i] * inverr2;
605 sumAF += (f * inverr2) * amplitudes_[i];
606 sumFF += f * (f * inverr2);
610 double amplitudeMax = 0;
612 double denom = sumFF * sum1 - sumF * sumF;
614 amplitudeMax = (sumAF * sum1 - sumA * sumF) / denom;
623 const double *pedestals,
const double *pedestalRMSes,
624 const double *gainRatios, std::vector<double> &timeFitParameters,
625 std::vector<double> &litudeFitParameters,
626 std::pair<double, double> &timeFitLimits) {
628 init(dataFrame, sampleMask, pedestals, pedestalRMSes, gainRatios);
629 computeTime(timeFitParameters, timeFitLimits, amplitudeFitParameters);
630 computeAmplitude(amplitudeFitParameters);
655 pedestal_, calculatedRechit_.timeMax - 5,
656 calculatedRechit_.timeError);
void computeAmplitude(std::vector< double > &litudeFitParameters)
std::array< double, C::MAXSAMPLES > amplitudeErrors_
CalculatedRecHit getCalculatedRecHit()
void computeTime(std::vector< double > &timeFitParameters, std::pair< double, double > &timeFitLimits, std::vector< double > &litudeFitParameters)
CalculatedRecHit calculatedRechit_
EcalSampleMask sampleMask_
Abs< T >::type abs(const T &t)
EcalUncalibratedRecHit makeRecHit(const C &dataFrame, const EcalSampleMask &sampleMask, const double *pedestals, const double *pedestalRMSes, const double *gainRatios, std::vector< double > &timeFitParameters, std::vector< double > &litudeFitParameters, std::pair< double, double > &timeFitLimits)
std::array< double, C::MAXSAMPLES > amplitudeIE2_
std::array< double, C::MAXSAMPLES > amplitudes_
double computeAmplitudeImpl(std::vector< double > &litudeFitParameters, double, double)
static const double tmax[3]
bool fixMGPAslew(const C &dataFrame)
std::array< bool, C::MAXSAMPLES > useless_
const double Rmax[kNumberCalorimeter]
void init(const C &dataFrame, const EcalSampleMask &sampleMask, const double *pedestals, const double *pedestalRMSes, const double *gainRatios)
const double Rmin[kNumberCalorimeter]