Change digitization threshold to >=3 ACD units, now cut on ADC value of digit after...
[u/mrichter/AliRoot.git] / RAW / AliCaloFastAltroFitv0.cxx
CommitLineData
40df175f 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id:$ */
17
18/* History of cvs commits:
19 * $Log$
20 */
21
22//______________________________________________________
23// Author : Aleksei Pavlinov; IHEP, Protvino, Russia
24// Feb 17, 2009
25// Implementation of fit procedure from ALICE-INT-2008-026:
26// "Time and amplitude reconstruction from sampling
27// measurements of the PHOS signal profile"
28// M.Yu.Bogolyubsky and ..
29//
30// Fit by function en*x*x*exp(-2.*x): x = (t-t0)/tau.
31// The main goal is fast estimation of amplitude and t0.
32//
33
34// --- AliRoot header files ---
35#include "AliCaloFastAltroFitv0.h"
36
37#include <TH1.h>
38#include <TF1.h>
39#include <TCanvas.h>
40#include <TGraphErrors.h>
41#include <TMath.h>
42
43#include <math.h>
44
45ClassImp(AliCaloFastAltroFitv0)
46
47//____________________________________________________________________________
48 AliCaloFastAltroFitv0::AliCaloFastAltroFitv0()
49: TNamed(),
50 fSig(0),fTau(0),fN(0),fPed(0), fAmp(0),fAmpErr(0),fT0(0),fT0Err(0),fChi2(0.),fNDF(0)
51,fNfit(0),fTfit(0),fAmpfit(0), fStdFun(0)
52{
53}
54
55//____________________________________________________________________________
56AliCaloFastAltroFitv0::AliCaloFastAltroFitv0(const char* name, const char* title,
57 const Double_t sig, const Double_t tau, const Double_t n)
58 : TNamed(name, title),
59 fSig(sig),fTau(tau),fN(n),fPed(0), fAmp(0),fAmpErr(0),fT0(0),fT0Err(0),fChi2(0.),fNDF(0)
60 ,fNfit(0),fTfit(0),fAmpfit(0), fStdFun(0)
61{
62 if(strlen(name)==0) SetName("CaloFastAltroFitv0");
63}
64
65//____________________________________________________________________________
66AliCaloFastAltroFitv0::AliCaloFastAltroFitv0(const AliCaloFastAltroFitv0 &obj)
67 : TNamed(obj),
68 fSig(0),fTau(0),fN(2.),fPed(0), fAmp(0),fAmpErr(0),fT0(0),fT0Err(0),fChi2(0.),fNDF(0)
69 ,fNfit(0),fTfit(0),fAmpfit(0), fStdFun(0)
70{
71}
72
73//____________________________________________________________________________
74AliCaloFastAltroFitv0::~AliCaloFastAltroFitv0()
75{
76 if(fTfit) delete [] fTfit;
77 if(fAmpfit) delete [] fAmpfit;
78}
79
80//____________________________________________________________________________
81AliCaloFastAltroFitv0& AliCaloFastAltroFitv0::operator= (const AliCaloFastAltroFitv0 &/*obj*/)
82{
83 // Not implemented yet
84 return (*this);
85}
86
87void AliCaloFastAltroFitv0::FastFit(Int_t* t, Int_t* y, Int_t nPoints, Double_t sig, Double_t tau,
88 Double_t n, Double_t ped, Double_t tMax)
89{
90 // n 2 here and unused
91 n = 2.;
92 Reset();
93
94 fSig = sig;
95 fTau = tau;
96 fPed = ped;
97
40df175f 98 Int_t ii=0;
99 CutRightPart(t,y,nPoints,tMax, ii);
100 nPoints = ii;
101
102 fNfit = 0;
103 fTfit = new Double_t[nPoints];
104 fAmpfit = new Double_t[nPoints];
105
106
107 DeductPedestal(t,y,nPoints, tau,ped, fTfit,fAmpfit,fNfit);
108 // printf(" n %i : fNfit %i : ped %f \n", n, fNfit, ped);
109 // for(int i=0; i<fNfit; i++)
110 // printf(" i %i : fAmpfit %7.2f : fTfit %7.2f \n", i, fAmpfit[i], fTfit[i]);
111
112 if(fNfit>=2) {
113 FastFit(fTfit,fAmpfit,fNfit,sig,tau, fAmp,fAmpErr, fT0,fT0Err,fChi2);
114
115 if(fChi2> 0.0) fNDF = fNfit - 2;
116 else fNDF = 0;
117 } else if(fNfit==1){
118 Reset(); // What to do here => fT0 = fTfit[0]; fAmp = fAmpFit[0] ??
119 } else {
120 Reset();
121 }
122}
123
124//____________________________________________________________________________
125void AliCaloFastAltroFitv0::FastFit(TH1F* h, Double_t sig, Double_t tau, Double_t n,
126Double_t ped, Double_t tMax)
127{
128 // Service method for convinience only
129 Reset();
130
131 if(h==0) return;
132 Int_t nPoints = h->GetNbinsX();
61f3c0f2 133 if(nPoints<2) return; // Sep 07, 09
40df175f 134
135 Int_t* t = new Int_t[nPoints];
136 Int_t* y = new Int_t[nPoints];
137
138 for(Int_t i=0; i<nPoints; i++) {
139 t[i] = i+1;
140 y[i] = Int_t(h->GetBinContent(i+1));
141 }
142
143 if(nPoints>=2) {
144 FastFit(t,y,nPoints, sig,tau,n,ped, tMax);
145 }
146
147 delete [] t;
148 delete [] y;
149}
150
151void AliCaloFastAltroFitv0::Reset()
152{
153 // Reset variables
154 fSig = fTau = 0.0;
155 fAmp = fAmpErr = fT0 = fT0Err = 0.0;
156 fChi2 = -1.;
157 fNDF = fNfit = 0;
61f3c0f2 158
159 if(fTfit) delete [] fTfit;
160 if(fAmpfit) delete [] fAmpfit;
40df175f 161 fTfit = fAmpfit = 0;
162}
163
164
165void AliCaloFastAltroFitv0::GetFitResult(Double_t &amp,Double_t &eamp,Double_t &t0,Double_t &et0,
166Double_t &chi2, Int_t &ndf) const
167{
168 // Return results of fitting procedure
169 amp = fAmp;
170 eamp = fAmpErr;
171 t0 = fT0;
172 et0 = fT0Err;
173 chi2 = fChi2;
174 ndf = fNDF;
175}
176
177void AliCaloFastAltroFitv0::GetFittedPoints(Int_t &nfit, Double_t* ar[2]) const
178{
179 nfit = fNfit;
180 ar[0] = fTfit;
181 ar[1] = fAmpfit;
182}
183//
184// static functions
185//
186void AliCaloFastAltroFitv0::CutRightPart(Int_t *t,Int_t *y,Int_t nPoints,Double_t tMax, Int_t &ii)
187{
61f3c0f2 188 // Cut right part of altro sample : static function
40df175f 189 Int_t tt=0;
190 for(Int_t i=0; i<nPoints; i++) {
191 tt = t[i];
192 if(tMax && tt <= Int_t(tMax)) {
193 t[ii] = tt;
194 y[ii] = y[i];
195 ii++;
196 }
197 }
198 if(0) printf(" ii %i -> ii %i : tMax %7.2f \n", nPoints, ii, tMax);
199}
200
201void AliCaloFastAltroFitv0::DeductPedestal(Int_t* t, Int_t* y, Int_t n, Double_t tau, Double_t ped,
202 Double_t* tn, Double_t* yn, Int_t &nn)
203{
61f3c0f2 204 // Pedestal deduction if ped is positive : static function
40df175f 205 // Discard part od samle if it is not compact.
206 static Double_t yMinUnderPed=2.; // should be tune
207 Int_t ymax=0, nmax=0;
208 for(Int_t i=0; i<n; i++){
209 if(y[i]>ymax) {
210 ymax = y[i];
211 nmax = i;
212 }
213 }
214 Int_t i1 = nmax - Int_t(tau);
215 //i1 = 0;
216 i1 = i1<0?0:i1;
217 Int_t i2 = n;
218
219 nn = 0;
220 Double_t yd=0.0, tdiff=0.0;;
221 for(Int_t i=i1; i<i2; i++) {
222 if(ped>0.0) {
223 yd = Double_t(y[i]) - ped;
224 } else {
225 yd = Double_t(y[i]);
226 }
227 if(yd < yMinUnderPed) continue;
228
229 if(i>i1 && nn>0){
230 tdiff = t[i] - tn[nn-1];
231 // printf(" i %i : nn %i : tdiff %6.2f : tn[nn] %6.2f \n", i,nn, tdiff, tn[nn-1]);
232 if(tdiff>1.) {
233 // discard previous points if its are before maximum point and with gap>1
234 if(i<nmax ) {
235 nn = 0; // nn--;
236 // if point with gap after maximum - finish selection
237 } else if(i>=nmax ) {
238 break;
239 }
240 }
241 // Far away from maximum
242 //if(i-nmax > Int_t(5*tau)) break;
243 }
244 tn[nn] = Double_t(t[i]);
245 yn[nn] = yd;
246 //printf("i %i : nn %i : tn %6.2f : yn %6.2f \n", i, nn, tn[nn], yn[nn]);
247 nn++;
248 }
249 //printf(" nmax %i : n %i : nn %i i1 %i \n", nmax, n, nn, i1);
250}
251
252void AliCaloFastAltroFitv0::FastFit(const Double_t* t, const Double_t* y, const Int_t n,
253 const Double_t sig, const Double_t tau,
254 Double_t &amp, Double_t &eamp, Double_t &t0, Double_t &et0, Double_t &chi2)
255{
61f3c0f2 256 // static function
40df175f 257 // It is case of n=k=2 : fnn = x*x*exp(2 - 2*x)
258 // Input:
259 // n - number of points
260 // t[n] - array of time bins
261 // y[n] - array of amplitudes after pedestal subtractions;
262 // sig - error of amplitude measurement (one value for all channels)
263 // tau - filter time response (in timebin units)
264 // Output:
265 // amp - amplitude at t0;
266 // t0 - time of max amplitude;
267 static Double_t xx; // t/tau
268 static Double_t a, b, c;
269 static Double_t f02, f12, f22; // functions
270 static Double_t f02d, f12d, f22d; // functions derivations
271
272 chi2 = -1.;
273
274 if(n<2) {
275 printf(" AliCaloFastAltroFitv0::FastFit : n<=%i \n", n);
276 return;
277 }
278
279 a = b = c = 0.0;
280 for(Int_t i=0; i<n; i++){
281 xx = t[i]/tau;
282 f02 = exp(-2.*xx);
283 f12 = xx*f02;
284 f22 = xx*f12;
285 // Derivations
286 f02d = -2.*f02;
287 f12d = f02 - 2.*f12;
288 f22d = 2.*(f12 - f22);
289 //
290 a += f02d * y[i];
291 b -= 2.*f12d * y[i];
292 c += f22d * y[i];
293 }
294 Double_t t01=0.0, t02=0.0;
295 Double_t amp1=0.0, amp2=0.0, chi21=0.0, chi22=0.0;
296 if(QuadraticRoots(a,b,c, t01,t02)) {
297 t01 *= tau;
298 t02 *= tau;
299 Amplitude(t,y,n, sig, tau, t01, amp1, chi21);
300 Amplitude(t,y,n, sig, tau, t02, amp2, chi22);
301 if(0) {
302 printf(" t01 %f : t02 %f \n", t01, t02);
303 printf(" amp1 %f : amp2 %f \n", amp1, amp2);
304 printf(" chi21 %f : chi22 %f \n", chi21, chi22);
305 }
306 // t0 less on one tau with comparing with value from "canonical equation"
307 amp = amp1;
308 t0 = t01;
309 chi2 = chi21;
310 if(chi21 > chi22) {
311 amp = amp2;
312 t0 = t02;
313 chi2 = chi22;
314 }
315 if(tau<3.) { // EMCAL case : small tau
316 t0 += -0.03;
317 Amplitude(t,y,n, sig, tau, t0, amp, chi2);
318 }
319 CalculateParsErrors(t, y, n, sig, tau, amp, t0, eamp, et0);
320
321 // Fill1();
322
323 // DrawFastFunction(amp, t0, fUtils->GetPedestalValue(), "1");
324 // DrawFastFunction(amp1, t01, fUtils->GetPedestalValue(), "1");
325 // DrawFastFunction(amp2, t02, fUtils->GetPedestalValue(), "2");
326 } else {
327 chi2 = t01; // no roots, bad fit - negative chi2
328 }
329}
330
331Bool_t AliCaloFastAltroFitv0::QuadraticRoots(const Double_t a, const Double_t b, const Double_t c,
332 Double_t &x1, Double_t &x2)
333{
334 // Resolve quadratic equations a*x**2 + b*x + c
335 //printf(" a %12.5e b %12.5e c %12.5e \n", a, b, c);
336 static Double_t dtmp = 0.0, dtmpCut = -1.e-6;
337 static Int_t ierr=0;
338 dtmp = b*b - 4.*a*c;
339
340 if(dtmp>=dtmpCut && dtmp<0.0) {
341 if(ierr<5 || ierr%1000==0)
61f3c0f2 342 printf("%i small neg. sq. : dtmp %12.5e \n", ierr, dtmp);
343 ierr++;
40df175f 344 dtmp = 0.0;
345 }
346 if(dtmp>=0.0) {
347 dtmp = sqrt(dtmp);
348 x1 = (-b + dtmp) / (2.*a);
349 x2 = (-b - dtmp) / (2.*a);
350
351 // printf(" x1 %f : x2 %f \n", x1, x2);
352 return kTRUE;
353 } else {
354 x1 = dtmp;
61f3c0f2 355 if(ierr<5 || ierr%1000==0)
356 printf(" %i neg. sq. : dtmp %12.5e \n", ierr, dtmp);
357 ierr++;
40df175f 358 return kFALSE;
359 }
360}
361
362void AliCaloFastAltroFitv0::Amplitude(const Double_t* t,const Double_t* y,const Int_t n,
363 const Double_t sig, const Double_t tau, const Double_t t0,
364 Double_t &amp, Double_t &chi2)
365{
366 // Calculate parameters error too - Mar 24,09
367 // sig is independent from points
368 amp = 0.;
369 Double_t x=0.0, f=0.0, den=0.0, f02;
370 for(Int_t i=0; i<n; i++){
371 x = (t[i] - t0)/tau;
372 f02 = exp(-2.*x);
373 f = x*x*f02;
374 amp += f*y[i];
375 den += f*f;
376 }
377 if(den>0.0) amp /= den;
378 //
379 // chi2 calculation
380 //
381 Double_t dy=0.0;
382 chi2=0.;
383 for(Int_t i=0; i<n; i++){
384 x = (t[i] - t0)/tau;
385 f02 = exp(-2.*x);
386 f = amp*x*x*f02;
387 dy = y[i]-f;
388 chi2 += dy*dy;
389 // printf(" %i : y %f -> f %f : dy %f \n", i, y[i], f, dy);
390 }
391 chi2 /= (sig*sig);
392}
393
394void AliCaloFastAltroFitv0::CalculateParsErrors(const Double_t* t, const Double_t* /*y*/, const Int_t n,
395 const Double_t sig, const Double_t tau,
396 Double_t &amp, Double_t &t0, Double_t &eamp, Double_t &et0)
397{
398 // Remember that fmax = exp(-n);
399 // fmax_nk = (n/k)**n*exp(-n) => n=k=2 => exp(-n) = exp(-2.)
400 static Double_t cc = exp(-2.);
401 // static Double_t cc = exp(-fN); // mean(N)~1.5 ??
402
403 Double_t sumf2=0.0, sumfd2=0.0, x, f02, f12, f22, f22d;
404
405 for(Int_t i=0; i<n; i++){
406 x = (t[i] - t0)/tau;
407 f02 = exp(-2.*x);
408 f12 = x*f02;
409 f22 = x*f12;
410 sumf2 += f22 * f22;
411 //
412 f22d = 2.*(f12 - f22);
413 sumfd2 += f22d * f22d;
414 }
415 et0 = (sig/amp)/sqrt(sumfd2);
416 eamp = sig/sqrt(sumf2);
417
418 amp *= cc;
419 eamp *= cc;
420}
421
422//
423// Drawing
424//
425TCanvas* AliCaloFastAltroFitv0::DrawFastFunction()
426{
427 // QA of fitting
428 if(fNfit<=0) return 0; // no points
429
430 static TCanvas *c = 0;
431 if(c==0) {
432 c = new TCanvas("fastFun","fastFun",800,600);
433 }
434
435 c->cd();
436
437 Double_t* eamp = new Double_t[fNfit];
438 Double_t* et = new Double_t[fNfit];
439
440 for(Int_t i=0; i<fNfit; i++) {
441 eamp[i] = fSig;
442 et[i] = 0.0;
443 }
444
445 TGraphErrors *gr = new TGraphErrors(fNfit, fTfit,fAmpfit, et,eamp);
446 gr->Draw("Ap");
447 gr->SetTitle(Form("Fast Fit : #chi^{2}/ndf = %8.2f / %i", GetChi2(), GetNDF()));
448 gr->GetHistogram()->SetXTitle(" time bin ");
449 gr->GetHistogram()->SetYTitle(" amplitude ");
450
451 if(fStdFun==0) {
452 fStdFun = new TF1("stdFun", StdResponseFunction, 0., fTfit[fNfit-1]+2., 5);
453 fStdFun->SetParNames("amp","t0","tau","N","ped");
454 }
455 fStdFun->SetParameter(0, GetEnergy());
456 fStdFun->SetParameter(1, GetTime() + GetTau());
457 fStdFun->SetParameter(2, GetTau()); //
458 fStdFun->SetParameter(3, GetN()); // 2
459 fStdFun->SetParameter(4, 0.); //
460
461 fStdFun->SetLineColor(kBlue);
462 fStdFun->SetLineWidth(1);
463
464 fStdFun->Draw("same");
465
466 delete [] eamp;
467 delete [] et;
468
469 c->Update();
470
471 return c;
472}
473
474Double_t AliCaloFastAltroFitv0::StdResponseFunction(const Double_t *x, const Double_t *par)
475{
61f3c0f2 476 // Static Standard Response Function :
40df175f 477 // look to Double_t AliEMCALRawUtils::RawResponseFunction(Double_t *x, Double_t *par)
478 // Using for drawing only.
479 //
480 // Shape of the electronics raw reponse:
481 // It is a semi-gaussian, 2nd order Gamma function (n=2) of the general form
482 //
483 // t' = (t - t0 + tau) / tau
484 // F = A * t**N * exp( N * ( 1 - t) ) for t >= 0
485 // F = 0 for t < 0
486 //
487 // parameters:
488 // A: par[0] // Amplitude = peak value
489 // t0: par[1]
490 // tau: par[2]
491 // N: par[3]
492 // ped: par[4]
493 //
494 static Double_t signal , tau, n, ped, xx;
495
496 tau = par[2];
497 n = par[3];
498 ped = par[4];
499 xx = ( x[0] - par[1] + tau ) / tau ;
500
501 if (xx <= 0)
502 signal = ped ;
503 else {
504 signal = ped + par[0] * TMath::Power(xx , n) * TMath::Exp(n * (1 - xx )) ;
505 }
506 return signal ;
507}