1 #ifndef RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
2 #define RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
14 #include "Math/SVector.h"
15 #include "Math/SMatrix.h"
45 const double *pedestals,
46 const double* pedestalRMSes,
47 const double *gainRatios,
48 std::vector < double >&timeFitParameters,
49 std::vector < double >&litudeFitParameters,
50 std::pair < double, double >&timeFitLimits);
54 void init(
const C &dataFrame,
const double * pedestals,
const double * pedestalRMSes,
const double * gainRatios );
55 void computeTime(std::vector < double >&timeFitParameters, std::pair < double, double >&timeFitLimits, std::vector< double > &litudeFitParameters);
77 calculatedRechit_.timeMax = 5;
78 calculatedRechit_.amplitudeMax = 0;
79 calculatedRechit_.timeError = -999;
81 amplitudes_.reserve(C::MAXSAMPLES);
82 amplitudeErrors_.clear();
83 amplitudeErrors_.reserve(C::MAXSAMPLES);
85 ratios_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
87 times_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
89 timesAB_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
97 if (dataFrame.sample(0).gainId() == 1) {
98 pedestal_ += double (dataFrame.sample(0).adc());
102 dataFrame.sample(1).gainId() == 1 &&
103 fabs(dataFrame.sample(1).adc()-dataFrame.sample(0).adc())<3*pedestalRMSes[0]) {
104 pedestal_ += double (dataFrame.sample(1).adc());
110 pedestal_ = pedestals[0];
117 double ampMaxValue = -1000;
125 for (
int iSample = 0; iSample < C::MAXSAMPLES; iSample++) {
126 GainId = dataFrame.sample(iSample).gainId();
129 sample = double (dataFrame.sample(iSample).adc() - pedestal_);
130 sampleError = pedestalRMSes[0];
131 }
else if (GainId == 2 || GainId == 3){
132 sample = (double (dataFrame.sample(iSample).adc() - pedestals[GainId - 1])) *gainRatios[GainId - 1];
133 sampleError = pedestalRMSes[GainId-1]*gainRatios[GainId-1];
141 amplitudes_.push_back(sample);
142 amplitudeErrors_.push_back(sampleError);
143 if(ampMaxValue < sample){
144 ampMaxValue = sample;
145 ampMaxError_ = sampleError;
149 amplitudes_.push_back(sample);
150 amplitudeErrors_.push_back(1e+9);
171 for (
int iSample = 1; iSample < C::MAXSAMPLES; iSample++) {
172 GainIdPrev = dataFrame.sample(iSample-1).gainId();
173 GainIdNext = dataFrame.sample(iSample).gainId();
174 if( GainIdPrev>=1 && GainIdPrev<=3 &&
175 GainIdNext>=1 && GainIdNext<=3 &&
176 GainIdPrev<GainIdNext ){
177 amplitudes_[iSample-1]=1e-9;
178 amplitudeErrors_[iSample-1]=1e+9;
188 std::pair < double, double >&timeFitLimits, std::vector < double >&litudeFitParameters)
195 double ampMaxAlphaBeta = 0;
196 double tMaxAlphaBeta = 5;
197 double tMaxErrorAlphaBeta = 999;
198 double tMaxRatio = 5;
199 double tMaxErrorRatio = 999;
210 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
211 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
214 sumA += amplitudes_[
i]/err2;
215 sumAA += amplitudes_[
i]*amplitudes_[
i]/err2;
218 NullChi2 = (sumAA - sumA*sumA/sum1)/sum0;
230 double alphabeta = amplitudeFitParameters[0]*amplitudeFitParameters[1];
231 double alpha = amplitudeFitParameters[0];
232 double beta = amplitudeFitParameters[1];
234 for(
unsigned int i = 0;
i < amplitudes_.size()-1;
i++){
235 for(
unsigned int j =
i+1;
j < amplitudes_.size();
j++){
237 if(amplitudes_[
i]>1 && amplitudes_[
j]>1){
240 double Rtmp = amplitudes_[
i]/amplitudes_[
j];
245 double err1 = Rtmp*Rtmp*( (amplitudeErrors_[
i]*amplitudeErrors_[
i]/(amplitudes_[
i]*amplitudes_[
i])) + (amplitudeErrors_[
j]*amplitudeErrors_[
j]/(amplitudes_[
j]*amplitudes_[
j])) );
249 if(num_>0) stat = num_;
251 double err2 = amplitudeErrors_[
j]*(amplitudes_[
i]-amplitudes_[
j])/(amplitudes_[
j]*amplitudes_[
j])/
sqrt(stat);
255 double err3 = 0.289/amplitudes_[
j];
257 double totalError =
sqrt(err1 + err2*err2 +err3*err3);
263 && Rtmp<
exp(
double(j-
i)/beta)-0.001
265 Ratio currentRatio = {
i, (j-
i), Rtmp, totalError };
266 ratios_.push_back(currentRatio);
275 if(!ratios_.size() >0)
281 for(
unsigned int i = 0;
i < ratios_.size();
i++){
283 double stepOverBeta = double(ratios_[
i].
step)/
beta;
286 double Rmin = ratios_[
i].value - ratios_[
i].error;
287 if(Rmin<0.001) Rmin=0.001;
289 double Rmax = ratios_[
i].value + ratios_[
i].error;
290 double RLimit =
exp(stepOverBeta)-0.001;
291 if( Rmax > RLimit ) Rmax = RLimit;
293 double time1 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmin))/alpha)-1.0);
294 double time2 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmax))/alpha)-1.0);
297 double tmax = 0.5 * (time1 + time2);
298 double tmaxerr = 0.5 *
sqrt( (time1 - time2)*(time1 - time2) );
303 for(
unsigned int it = 0; it < amplitudes_.size(); it++){
304 double err2 = amplitudeErrors_[it]*amplitudeErrors_[it];
305 double offset = (double(it) -
tmax)/alphabeta;
306 double term1 = 1.0 +
offset;
308 double f =
exp( alpha*(
log(1.0+offset) - offset) );
309 sumAf += amplitudes_[it]*f/err2;
317 chi2 = sumAA - sumAf*sumAf/sumff;
324 if(chi2 > 0 && tmaxerr > 0 && tmax > 0){
325 Tmax currentTmax={ ratios_[
i].
index, ratios_[
i].step,
tmax, tmaxerr, amp, chi2 };
326 timesAB_.push_back(currentTmax);
331 if( !(timesAB_.size()> 0))
335 double chi2min = 1.0e+9;
336 double timeMinimum = 5;
337 double errorMinimum = 999;
338 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
339 if( timesAB_[
i].chi2 <= chi2min ){
340 chi2min = timesAB_[
i].chi2;
341 timeMinimum = timesAB_[
i].value;
342 errorMinimum = timesAB_[
i].error;
348 double chi2Limit = chi2min + 1.0;
351 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
352 if( timesAB_[
i].chi2 < chi2Limit ){
353 double inverseSigmaSquared = 1.0/(timesAB_[
i].error*timesAB_[
i].error);
354 time_wgt += inverseSigmaSquared;
355 time_max += timesAB_[
i].value*inverseSigmaSquared;
359 tMaxAlphaBeta = time_max/time_wgt;
360 tMaxErrorAlphaBeta = 1.0/
sqrt(time_wgt);
365 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
366 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
367 double offset = (double(
i) - tMaxAlphaBeta)/alphabeta;
368 double term1 = 1.0 +
offset;
370 double f =
exp( alpha*(
log(1.0+offset) - offset) );
371 sumAf += amplitudes_[
i]*f/err2;
377 ampMaxAlphaBeta = sumAf/sumff;
378 double chi2AlphaBeta = (sumAA - sumAf*sumAf/sumff)/sum0;
379 if(chi2AlphaBeta > NullChi2){
401 if( ampMaxAlphaBeta/ampMaxError_ > 5.0 ){
410 for (
unsigned int i = 0;
i < ratios_.size();
i++) {
412 if(ratios_[
i].
step == 1
413 && ratios_[
i].
value >= timeFitLimits.first
414 && ratios_[
i].value <= timeFitLimits.second
417 double time_max_i = ratios_[
i].index;
421 double u = timeFitParameters[timeFitParameters.size() - 1];
422 for (
int k = timeFitParameters.size() - 2;
k >= 0;
k--) {
423 u = u * ratios_[
i].value + timeFitParameters[
k];
428 (timeFitParameters.size() -
429 1) * timeFitParameters[timeFitParameters.size() - 1];
430 for (
int k = timeFitParameters.size() - 2;
k >= 1;
k--) {
431 du = du * ratios_[
i].value +
k * timeFitParameters[
k];
436 double errorsquared =
437 ratios_[
i].error * ratios_[
i].error * du * du;
438 if (errorsquared > 0) {
440 time_max += (time_max_i - u) / errorsquared;
441 time_wgt += 1.0 / errorsquared;
443 { ratios_[
i].
index, 1, (time_max_i - u),
444 sqrt(errorsquared),0,1 };
445 times_.push_back(currentTmax);
454 tMaxRatio = time_max/time_wgt;
455 tMaxErrorRatio = 1.0/
sqrt(time_wgt);
459 if( ampMaxAlphaBeta/ampMaxError_ > 10.0 ){
462 calculatedRechit_.timeMax = tMaxRatio;
463 calculatedRechit_.timeError = tMaxErrorRatio;
468 calculatedRechit_.timeMax = ( tMaxAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
469 calculatedRechit_.timeError = ( tMaxErrorAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxErrorRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
476 calculatedRechit_.timeMax = tMaxAlphaBeta;
477 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
484 calculatedRechit_.timeMax = tMaxAlphaBeta;
485 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
500 double alpha = amplitudeFitParameters[0];
501 double beta = amplitudeFitParameters[1];
505 double pedestalLimit = calculatedRechit_.timeMax - (alpha *
beta) - 1.0;
511 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
512 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
514 double termOne = 1 + (
i - calculatedRechit_.timeMax) / (alpha * beta);
515 if (termOne > 1.e-5) f =
exp(alpha *
log(termOne)) *
exp(-(
i - calculatedRechit_.timeMax) / beta);
519 if ( (
i < pedestalLimit)
520 || (f > 0.6 &&
i <= calculatedRechit_.timeMax)
521 || (f > 0.4 &&
i >= calculatedRechit_.timeMax)) {
523 sumA += amplitudes_[
i]/err2;
525 sumAF += f*amplitudes_[
i]/err2;
530 calculatedRechit_.amplitudeMax = 0;
532 double denom = sumFF*sum1 - sumF*sumF;
533 if(fabs(denom)>1.0e-20){
534 calculatedRechit_.amplitudeMax = (sumAF*sum1 - sumA*sumF)/denom;
543 const double *pedestals,
544 const double *pedestalRMSes,
545 const double *gainRatios,
546 std::vector < double >&timeFitParameters,
547 std::vector < double >&litudeFitParameters,
548 std::pair < double, double >&timeFitLimits)
551 init( dataFrame, pedestals, pedestalRMSes, gainRatios );
552 computeTime( timeFitParameters, timeFitLimits, amplitudeFitParameters );
553 computeAmplitude( amplitudeFitParameters );
578 calculatedRechit_.amplitudeMax,
580 calculatedRechit_.timeMax - 5,
581 calculatedRechit_.timeError);
void computeAmplitude(std::vector< double > &litudeFitParameters)
virtual EcalUncalibratedRecHit makeRecHit(const C &dataFrame, const double *pedestals, const double *pedestalRMSes, const double *gainRatios, std::vector< double > &timeFitParameters, std::vector< double > &litudeFitParameters, std::pair< double, double > &timeFitLimits)
CalculatedRecHit getCalculatedRecHit()
void computeTime(std::vector< double > &timeFitParameters, std::pair< double, double > &timeFitLimits, std::vector< double > &litudeFitParameters)
CalculatedRecHit calculatedRechit_
Exp< T >::type exp(const T &t)
std::vector< double > amplitudeErrors_
unsigned int offset(bool)
static const double tmax[3]
std::vector< double > amplitudes_
Log< T >::type log(const T &t)
bool fixMGPAslew(const C &dataFrame)
void init(const C &dataFrame, const double *pedestals, const double *pedestalRMSes, const double *gainRatios)
std::vector< Ratio > ratios_
std::vector< Tmax > times_
const double Rmax[kNumberCalorimeter]
std::vector< Tmax > timesAB_
const double Rmin[kNumberCalorimeter]