1 #ifndef RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
2 #define RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRatioMethodAlgo_HH
14 #include "Math/SVector.h"
15 #include "Math/SMatrix.h"
46 const double *pedestals,
47 const double* pedestalRMSes,
48 const double *gainRatios,
49 std::vector < double >&timeFitParameters,
50 std::vector < double >&litudeFitParameters,
51 std::pair < double, double >&timeFitLimits);
55 void init(
const C &dataFrame,
const EcalSampleMask &sampleMask,
const double * pedestals,
const double * pedestalRMSes,
const double * gainRatios );
56 void computeTime(std::vector < double >&timeFitParameters, std::pair < double, double >&timeFitLimits, std::vector< double > &litudeFitParameters);
80 const double * pedestals,
const double * pedestalRMSes,
const double * gainRatios )
82 sampleMask_ = sampleMask;
83 theDetId_ =
DetId(dataFrame.id().rawId());
85 calculatedRechit_.timeMax = 5;
86 calculatedRechit_.amplitudeMax = 0;
87 calculatedRechit_.timeError = -999;
89 amplitudes_.reserve(C::MAXSAMPLES);
90 amplitudeErrors_.clear();
91 amplitudeErrors_.reserve(C::MAXSAMPLES);
93 ratios_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
95 times_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
97 timesAB_.reserve(C::MAXSAMPLES*(C::MAXSAMPLES-1)/2);
105 if (dataFrame.sample(0).gainId() == 1 &&
106 sampleMask_.useSample(0, theDetId_ )
108 pedestal_ += double (dataFrame.sample(0).adc());
112 dataFrame.sample(1).gainId() == 1 &&
113 sampleMask_.useSample(1, theDetId_) &&
114 fabs(dataFrame.sample(1).adc()-dataFrame.sample(0).adc())<3*pedestalRMSes[0]) {
115 pedestal_ += double (dataFrame.sample(1).adc());
121 pedestal_ = pedestals[0];
128 double ampMaxValue = -1000;
136 for (
int iSample = 0; iSample < C::MAXSAMPLES; iSample++) {
140 GainId = dataFrame.sample(iSample).gainId();
145 if (!sampleMask_.useSample(iSample, theDetId_ ) ) {
149 else if (GainId == 1) {
150 sample = double (dataFrame.sample(iSample).adc() - pedestal_);
151 sampleError = pedestalRMSes[0];
153 else if (GainId == 2 || GainId == 3){
154 sample = (double (dataFrame.sample(iSample).adc() - pedestals[GainId - 1])) *gainRatios[GainId - 1];
155 sampleError = pedestalRMSes[GainId-1]*gainRatios[GainId-1];
164 amplitudes_.push_back(sample);
165 amplitudeErrors_.push_back(sampleError);
166 if(ampMaxValue < sample){
168 ampMaxError_ = sampleError;
172 amplitudes_.push_back(sample);
173 amplitudeErrors_.push_back(1
e+9);
194 for (
int iSample = 1; iSample < C::MAXSAMPLES; iSample++) {
197 if (!sampleMask_.useSample(iSample, theDetId_) )
continue;
199 GainIdPrev = dataFrame.sample(iSample-1).gainId();
200 GainIdNext = dataFrame.sample(iSample).gainId();
201 if( GainIdPrev>=1 && GainIdPrev<=3 &&
202 GainIdNext>=1 && GainIdNext<=3 &&
203 GainIdPrev<GainIdNext ){
204 amplitudes_[iSample-1]=1
e-9;
205 amplitudeErrors_[iSample-1]=1
e+9;
215 std::pair < double, double >&timeFitLimits, std::vector < double >&litudeFitParameters)
222 double ampMaxAlphaBeta = 0;
223 double tMaxAlphaBeta = 5;
224 double tMaxErrorAlphaBeta = 999;
225 double tMaxRatio = 5;
226 double tMaxErrorRatio = 999;
237 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
238 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
241 sumA += amplitudes_[
i]/err2;
242 sumAA += amplitudes_[
i]*amplitudes_[
i]/err2;
245 NullChi2 = (sumAA - sumA*sumA/sum1)/sum0;
257 double alphabeta = amplitudeFitParameters[0]*amplitudeFitParameters[1];
258 double alpha = amplitudeFitParameters[0];
259 double beta = amplitudeFitParameters[1];
261 for(
unsigned int i = 0;
i < amplitudes_.size()-1;
i++){
262 for(
unsigned int j =
i+1;
j < amplitudes_.size();
j++){
264 if(amplitudes_[
i]>1 && amplitudes_[
j]>1){
267 double Rtmp = amplitudes_[
i]/amplitudes_[
j];
272 double err1 = Rtmp*Rtmp*( (amplitudeErrors_[
i]*amplitudeErrors_[
i]/(amplitudes_[
i]*amplitudes_[
i])) + (amplitudeErrors_[
j]*amplitudeErrors_[
j]/(amplitudes_[
j]*amplitudes_[
j])) );
276 if(num_>0) stat = num_;
278 double err2 = amplitudeErrors_[
j]*(amplitudes_[
i]-amplitudes_[
j])/(amplitudes_[
j]*amplitudes_[
j])/
sqrt(stat);
282 double err3 = 0.289/amplitudes_[
j];
284 double totalError =
sqrt(err1 + err2*err2 +err3*err3);
290 && Rtmp<
exp(
double(j-
i)/beta)-0.001
292 Ratio currentRatio = {
i, (j-
i), Rtmp, totalError };
293 ratios_.push_back(currentRatio);
302 if(!ratios_.size() >0)
308 for(
unsigned int i = 0;
i < ratios_.size();
i++){
310 double stepOverBeta = double(ratios_[
i].
step)/
beta;
313 double Rmin = ratios_[
i].value - ratios_[
i].error;
314 if(Rmin<0.001) Rmin=0.001;
316 double Rmax = ratios_[
i].value + ratios_[
i].error;
317 double RLimit =
exp(stepOverBeta)-0.001;
318 if( Rmax > RLimit ) Rmax = RLimit;
320 double time1 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmin))/alpha)-1.0);
321 double time2 = offset - ratios_[
i].step/(
exp((stepOverBeta-
log(Rmax))/alpha)-1.0);
324 double tmax = 0.5 * (time1 + time2);
325 double tmaxerr = 0.5 *
sqrt( (time1 - time2)*(time1 - time2) );
330 for(
unsigned int it = 0; it < amplitudes_.size(); it++){
331 double err2 = amplitudeErrors_[it]*amplitudeErrors_[it];
332 double offset = (double(it) -
tmax)/alphabeta;
333 double term1 = 1.0 +
offset;
335 double f =
exp( alpha*(
log(1.0+offset) - offset) );
336 sumAf += amplitudes_[it]*f/err2;
344 chi2 = sumAA - sumAf*sumAf/sumff;
351 if(chi2 > 0 && tmaxerr > 0 && tmax > 0){
352 Tmax currentTmax={ ratios_[
i].
index, ratios_[
i].step,
tmax, tmaxerr, amp, chi2 };
353 timesAB_.push_back(currentTmax);
358 if( !(timesAB_.size()> 0))
362 double chi2min = 1.0e+9;
365 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
366 if( timesAB_[
i].chi2 <= chi2min ){
367 chi2min = timesAB_[
i].chi2;
375 double chi2Limit = chi2min + 1.0;
378 for(
unsigned int i = 0;
i < timesAB_.size();
i++){
379 if( timesAB_[
i].chi2 < chi2Limit ){
380 double inverseSigmaSquared = 1.0/(timesAB_[
i].error*timesAB_[
i].error);
381 time_wgt += inverseSigmaSquared;
382 time_max += timesAB_[
i].value*inverseSigmaSquared;
386 tMaxAlphaBeta = time_max/time_wgt;
387 tMaxErrorAlphaBeta = 1.0/
sqrt(time_wgt);
392 for(
unsigned int i = 0;
i < amplitudes_.size();
i++){
393 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
394 double offset = (double(
i) - tMaxAlphaBeta)/alphabeta;
395 double term1 = 1.0 +
offset;
397 double f =
exp( alpha*(
log(1.0+offset) - offset) );
398 sumAf += amplitudes_[
i]*f/err2;
404 ampMaxAlphaBeta = sumAf/sumff;
405 double chi2AlphaBeta = (sumAA - sumAf*sumAf/sumff)/sum0;
406 if(chi2AlphaBeta > NullChi2){
428 if( ampMaxAlphaBeta/ampMaxError_ > 5.0 ){
437 for (
unsigned int i = 0;
i < ratios_.size();
i++) {
439 if(ratios_[
i].
step == 1
440 && ratios_[
i].
value >= timeFitLimits.first
441 && ratios_[
i].value <= timeFitLimits.second
444 double time_max_i = ratios_[
i].index;
448 double u = timeFitParameters[timeFitParameters.size() - 1];
449 for (
int k = timeFitParameters.size() - 2;
k >= 0;
k--) {
450 u = u * ratios_[
i].value + timeFitParameters[
k];
455 (timeFitParameters.size() -
456 1) * timeFitParameters[timeFitParameters.size() - 1];
457 for (
int k = timeFitParameters.size() - 2;
k >= 1;
k--) {
458 du = du * ratios_[
i].value +
k * timeFitParameters[
k];
463 double errorsquared =
464 ratios_[
i].error * ratios_[
i].error * du * du;
465 if (errorsquared > 0) {
467 time_max += (time_max_i - u) / errorsquared;
468 time_wgt += 1.0 / errorsquared;
470 { ratios_[
i].
index, 1, (time_max_i - u),
471 sqrt(errorsquared),0,1 };
472 times_.push_back(currentTmax);
481 tMaxRatio = time_max/time_wgt;
482 tMaxErrorRatio = 1.0/
sqrt(time_wgt);
486 if( ampMaxAlphaBeta/ampMaxError_ > 10.0 ){
489 calculatedRechit_.timeMax = tMaxRatio;
490 calculatedRechit_.timeError = tMaxErrorRatio;
495 calculatedRechit_.timeMax = ( tMaxAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
496 calculatedRechit_.timeError = ( tMaxErrorAlphaBeta*(10.0-ampMaxAlphaBeta/ampMaxError_) + tMaxErrorRatio*(ampMaxAlphaBeta/ampMaxError_ - 5.0) )/5.0;
503 calculatedRechit_.timeMax = tMaxAlphaBeta;
504 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
511 calculatedRechit_.timeMax = tMaxAlphaBeta;
512 calculatedRechit_.timeError = tMaxErrorAlphaBeta;
527 double alpha = amplitudeFitParameters[0];
528 double beta = amplitudeFitParameters[1];
532 double pedestalLimit = calculatedRechit_.timeMax - (alpha *
beta) - 1.0;
538 for (
unsigned int i = 0;
i < amplitudes_.size();
i++) {
539 double err2 = amplitudeErrors_[
i]*amplitudeErrors_[
i];
541 double termOne = 1 + (
i - calculatedRechit_.timeMax) / (alpha * beta);
542 if (termOne > 1.
e-5) f =
exp(alpha *
log(termOne)) *
exp(-(
i - calculatedRechit_.timeMax) / beta);
546 if ( (
i < pedestalLimit)
547 || (f > 0.6 &&
i <= calculatedRechit_.timeMax)
548 || (f > 0.4 &&
i >= calculatedRechit_.timeMax)) {
550 sumA += amplitudes_[
i]/err2;
552 sumAF += f*amplitudes_[
i]/err2;
557 calculatedRechit_.amplitudeMax = 0;
559 double denom = sumFF*sum1 - sumF*sumF;
560 if(fabs(denom)>1.0
e-20){
561 calculatedRechit_.amplitudeMax = (sumAF*sum1 - sumA*sumF)/denom;
571 const double *pedestals,
572 const double *pedestalRMSes,
573 const double *gainRatios,
574 std::vector < double >&timeFitParameters,
575 std::vector < double >&litudeFitParameters,
576 std::pair < double, double >&timeFitLimits)
579 init( dataFrame, sampleMask, pedestals, pedestalRMSes, gainRatios );
580 computeTime( timeFitParameters, timeFitLimits, amplitudeFitParameters );
581 computeAmplitude( amplitudeFitParameters );
606 calculatedRechit_.amplitudeMax,
608 calculatedRechit_.timeMax - 5,
609 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]