1 #ifndef RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
2 #define RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
11 #include "Math/SVector.h"
12 #include "Math/SMatrix.h"
43 const double *pedestals,
44 const double* pedestalRMSes,
45 const double *gainRatios,
46 std::vector < double >&timeFitParameters,
47 std::vector < double >&litudeFitParameters,
48 std::pair < double, double >&timeFitLimits);
52 void init(
const C &dataFrame,
const EcalSampleMask &sampleMask,
const double * pedestals,
const double * pedestalRMSes,
const double * gainRatios );
53 void computeTime(std::vector < double >&timeFitParameters, std::pair < double, double >&timeFitLimits, std::vector< double > &litudeFitParameters);
77 const double * pedestals,
const double * pedestalRMSes,
const double * gainRatios )
79 sampleMask_ = sampleMask;
80 theDetId_ =
DetId(dataFrame.id().rawId());
82 calculatedRechit_.timeMax = 5;
83 calculatedRechit_.amplitudeMax = 0;
84 calculatedRechit_.timeError = -999;
86 amplitudes_.reserve(C::MAXSAMPLES);
87 amplitudeErrors_.clear();
88 amplitudeErrors_.reserve(C::MAXSAMPLES);
90 ratios_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
92 times_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
94 timesAB_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
102 if (dataFrame.sample(0).gainId() == 1 &&
103 sampleMask_.useSample(0, theDetId_ )
105 pedestal_ += double (dataFrame.sample(0).adc());
109 dataFrame.sample(1).gainId() == 1 &&
110 sampleMask_.useSample(1, theDetId_) &&
111 fabs(dataFrame.sample(1).adc()-dataFrame.sample(0).adc())<3*pedestalRMSes[0]) {
112 pedestal_ += double (dataFrame.sample(1).adc());
118 pedestal_ = pedestals[0];
125 double ampMaxValue = -1000;
133 for (
int iSample = 0; iSample < C::MAXSAMPLES; iSample++) {
137 GainId = dataFrame.sample(iSample).gainId();
142 if (!sampleMask_.useSample(iSample, theDetId_ ) ) {
146 else if (GainId == 1) {
147 sample = double (dataFrame.sample(iSample).adc() - pedestal_);
148 sampleError = pedestalRMSes[0];
150 else if (GainId == 2 || GainId == 3){
151 sample = (double (dataFrame.sample(iSample).adc() - pedestals[GainId - 1])) *gainRatios[GainId - 1];
152 sampleError = pedestalRMSes[GainId-1]*gainRatios[GainId-1];
161 amplitudes_.push_back(sample);
162 amplitudeErrors_.push_back(sampleError);
163 if(ampMaxValue < sample){
165 ampMaxError_ = sampleError;
169 amplitudes_.push_back(sample);
170 amplitudeErrors_.push_back(1
e+9);
191 for (
int iSample = 1; iSample < C::MAXSAMPLES; iSample++) {
194 if (!sampleMask_.useSample(iSample, theDetId_) )
continue;
196 GainIdPrev = dataFrame.sample(iSample-1).gainId();
197 GainIdNext = dataFrame.sample(iSample).gainId();
198 if( GainIdPrev>=1 && GainIdPrev<=3 &&
199 GainIdNext>=1 && GainIdNext<=3 &&
200 GainIdPrev<GainIdNext ){
201 amplitudes_[iSample-1]=1
e-9;
202 amplitudeErrors_[iSample-1]=1
e+9;
212 std::pair < double, double >&timeFitLimits, std::vector < double >&litudeFitParameters)
219 double ampMaxAlphaBeta = 0;
220 double tMaxAlphaBeta = 5;
221 double tMaxErrorAlphaBeta = 999;
222 double tMaxRatio = 5;
223 double tMaxErrorRatio = 999;
234 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
235 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
238 sumA += amplitudes_[
i]/err2;
239 sumAA += amplitudes_[
i]*amplitudes_[
i]/err2;
242 NullChi2 = (sumAA - sumA*sumA/sum1)/sum0;
254 double alphabeta = amplitudeFitParameters[0]*amplitudeFitParameters[1];
255 double alpha = amplitudeFitParameters[0];
256 double beta = amplitudeFitParameters[1];
258 for(
unsigned int i = 0;
i < amplitudes_.size()-1;
i++){
259 for(
unsigned int j =
i+1;
j < amplitudes_.size();
j++){
261 if(amplitudes_[
i]>1 && amplitudes_[
j]>1){
264 double Rtmp = amplitudes_[
i]/amplitudes_[
j];
269 double err1 = Rtmp*Rtmp*( (amplitudeErrors_[
i]*amplitudeErrors_[
i]/(amplitudes_[
i]*amplitudes_[
i])) + (amplitudeErrors_[
j]*amplitudeErrors_[
j]/(amplitudes_[
j]*amplitudes_[
j])) );
273 if(num_>0) stat = num_;
275 double err2 = amplitudeErrors_[
j]*(amplitudes_[
i]-amplitudes_[
j])/(amplitudes_[
j]*amplitudes_[
j])/
sqrt(stat);
279 double err3 = 0.289/amplitudes_[
j];
281 double totalError =
sqrt(err1 + err2*err2 +err3*err3);
287 && Rtmp<
exp(
double(j-
i)/beta)-0.001
289 Ratio currentRatio = {
i, (j-
i), Rtmp, totalError };
290 ratios_.push_back(currentRatio);
299 if(!ratios_.size() >0)
305 for(
unsigned int i = 0;
i < ratios_.size();
i++){
307 double stepOverBeta = double(ratios_[
i].
step)/
beta;
310 double Rmin = ratios_[
i].value - ratios_[
i].error;
311 if(Rmin<0.001) Rmin=0.001;
313 double Rmax = ratios_[
i].value + ratios_[
i].error;
314 double RLimit =
exp(stepOverBeta)-0.001;
315 if( Rmax > RLimit ) Rmax = RLimit;
317 double time1 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmin))/alpha)-1.0);
318 double time2 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmax))/alpha)-1.0);
321 double tmax = 0.5 * (time1 + time2);
322 double tmaxerr = 0.5 *
sqrt( (time1 - time2)*(time1 - time2) );
327 for(
unsigned int it = 0; it < amplitudes_.size(); it++){
328 double err2 = amplitudeErrors_[it]*amplitudeErrors_[it];
329 double offset = (double(it) -
tmax)/alphabeta;
330 double term1 = 1.0 +
offset;
332 double f =
exp( alpha*(
log(1.0+offset) - offset) );
333 sumAf += amplitudes_[it]*f/err2;
341 chi2 = sumAA - sumAf*sumAf/sumff;
348 if(chi2 > 0 && tmaxerr > 0 && tmax > 0){
349 Tmax currentTmax={ ratios_[
i].
index, ratios_[
i].step,
tmax, tmaxerr, amp, chi2 };
350 timesAB_.push_back(currentTmax);
355 if( !(timesAB_.size()> 0))
359 double chi2min = 1.0e+9;
362 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
363 if( timesAB_[
i].chi2 <= chi2min ){
364 chi2min = timesAB_[
i].chi2;
372 double chi2Limit = chi2min + 1.0;
375 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
376 if( timesAB_[
i].chi2 < chi2Limit ){
377 double inverseSigmaSquared = 1.0/(timesAB_[
i].error*timesAB_[
i].error);
378 time_wgt += inverseSigmaSquared;
379 time_max += timesAB_[
i].value*inverseSigmaSquared;
383 tMaxAlphaBeta = time_max/time_wgt;
384 tMaxErrorAlphaBeta = 1.0/
sqrt(time_wgt);
389 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
390 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
391 double offset = (double(
i) - tMaxAlphaBeta)/alphabeta;
392 double term1 = 1.0 +
offset;
394 double f =
exp( alpha*(
log(1.0+offset) - offset) );
395 sumAf += amplitudes_[
i]*f/err2;
401 ampMaxAlphaBeta = sumAf/sumff;
402 double chi2AlphaBeta = (sumAA - sumAf*sumAf/sumff)/sum0;
403 if(chi2AlphaBeta > NullChi2){
425 if( ampMaxAlphaBeta/ampMaxError_ > 5.0 ){
434 for (
unsigned int i = 0;
i < ratios_.size();
i++) {
436 if(ratios_[
i].
step == 1
437 && ratios_[
i].
value >= timeFitLimits.first
438 && ratios_[
i].value <= timeFitLimits.second
441 double time_max_i = ratios_[
i].index;
445 double u = timeFitParameters[timeFitParameters.size() - 1];
446 for (
int k = timeFitParameters.size() - 2;
k >= 0;
k--) {
447 u = u * ratios_[
i].value + timeFitParameters[
k];
452 (timeFitParameters.size() -
453 1) * timeFitParameters[timeFitParameters.size() - 1];
454 for (
int k = timeFitParameters.size() - 2;
k >= 1;
k--) {
455 du = du * ratios_[
i].value +
k * timeFitParameters[
k];
460 double errorsquared =
461 ratios_[
i].error * ratios_[
i].error * du * du;
462 if (errorsquared > 0) {
464 time_max += (time_max_i - u) / errorsquared;
465 time_wgt += 1.0 / errorsquared;
467 { ratios_[
i].
index, 1, (time_max_i - u),
468 sqrt(errorsquared),0,1 };
469 times_.push_back(currentTmax);
478 tMaxRatio = time_max/time_wgt;
479 tMaxErrorRatio = 1.0/
sqrt(time_wgt);
483 if( ampMaxAlphaBeta/ampMaxError_ > 10.0 ){
486 calculatedRechit_.timeMax = tMaxRatio;
487 calculatedRechit_.timeError = tMaxErrorRatio;
492 calculatedRechit_.timeMax = ( tMaxAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
493 calculatedRechit_.timeError = ( tMaxErrorAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxErrorRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
500 calculatedRechit_.timeMax = tMaxAlphaBeta;
501 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
508 calculatedRechit_.timeMax = tMaxAlphaBeta;
509 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
524 double alpha = amplitudeFitParameters[0];
525 double beta = amplitudeFitParameters[1];
529 double pedestalLimit = calculatedRechit_.timeMax - (alpha *
beta) - 1.0;
535 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
536 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
538 double termOne = 1 + (
i - calculatedRechit_.timeMax) / (alpha * beta);
539 if (termOne > 1.
e-5) f =
exp(alpha *
log(termOne)) *
exp(-(
i - calculatedRechit_.timeMax) / beta);
543 if ( (
i < pedestalLimit)
544 || (f > 0.6 &&
i <= calculatedRechit_.timeMax)
545 || (f > 0.4 &&
i >= calculatedRechit_.timeMax)) {
547 sumA += amplitudes_[
i]/err2;
549 sumAF += f*amplitudes_[
i]/err2;
554 calculatedRechit_.amplitudeMax = 0;
556 double denom = sumFF*sum1 - sumF*sumF;
557 if(fabs(denom)>1.0
e-20){
558 calculatedRechit_.amplitudeMax = (sumAF*sum1 - sumA*sumF)/denom;
568 const double *pedestals,
569 const double *pedestalRMSes,
570 const double *gainRatios,
571 std::vector < double >&timeFitParameters,
572 std::vector < double >&litudeFitParameters,
573 std::pair < double, double >&timeFitLimits)
576 init( dataFrame, sampleMask, pedestals, pedestalRMSes, gainRatios );
577 computeTime( timeFitParameters, timeFitLimits, amplitudeFitParameters );
578 computeAmplitude( amplitudeFitParameters );
603 calculatedRechit_.amplitudeMax,
605 calculatedRechit_.timeMax - 5,
606 calculatedRechit_.timeError);
void computeAmplitude(std::vector< double > &litudeFitParameters)
CalculatedRecHit getCalculatedRecHit()
void computeTime(std::vector< double > &timeFitParameters, std::pair< double, double > &timeFitLimits, std::vector< double > &litudeFitParameters)
CalculatedRecHit calculatedRechit_
EcalSampleMask sampleMask_
std::vector< double > amplitudeErrors_
virtual 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)
unsigned int offset(bool)
static const double tmax[3]
std::vector< double > amplitudes_
bool fixMGPAslew(const C &dataFrame)
std::vector< Ratio > ratios_
std::vector< Tmax > times_
const double Rmax[kNumberCalorimeter]
void init(const C &dataFrame, const EcalSampleMask &sampleMask, const double *pedestals, const double *pedestalRMSes, const double *gainRatios)
std::vector< Tmax > timesAB_
const double Rmin[kNumberCalorimeter]