Fixes for cmake
[u/mrichter/AliRoot.git] / PHOS / AliPHOSRawFitterv1.cxx
CommitLineData
379c5c09 1/**************************************************************************
2 * Copyright(c) 2007, 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// This class extracts the signal parameters (energy, time, quality)
19// from ALTRO samples. Energy is in ADC counts, time is in time bin units.
20// A fitting algorithm evaluates the energy and the time from Minuit minimization
21//
22// Typical use case:
23// AliPHOSRawFitter *fitter=new AliPHOSRawFitter();
379c5c09 24// fitter->SetChannelGeo(module,cellX,cellZ,caloFlag);
25// fitter->SetCalibData(fgCalibData) ;
1dfadc32 26// fitter->Eval(sig,sigStart,sigLength);
379c5c09 27// Double_t amplitude = fitter.GetEnergy();
28// Double_t time = fitter.GetTime();
29// Bool_t isLowGain = fitter.GetCaloFlag()==0;
30
31// Author: Dmitri Peressounko (Oct.2008)
32// Modified: Yuri Kharlov (Jul.2009)
33
34// --- ROOT system ---
35#include "TArrayD.h"
36#include "TList.h"
37#include "TMath.h"
38#include "TMinuit.h"
39#include "TCanvas.h"
40#include "TH1.h"
41#include "TH2.h"
42#include "TF1.h"
43#include "TROOT.h"
44
45// --- AliRoot header files ---
46#include "AliLog.h"
47#include "AliPHOSCalibData.h"
48#include "AliPHOSRawFitterv1.h"
49#include "AliPHOSPulseGenerator.h"
50
51ClassImp(AliPHOSRawFitterv1)
52
53//-----------------------------------------------------------------------------
54AliPHOSRawFitterv1::AliPHOSRawFitterv1():
55 AliPHOSRawFitterv0(),
56 fSampleParamsLow(0x0),
57 fSampleParamsHigh(0x0),
58 fToFit(0x0)
59{
60 //Default constructor.
61 if(!gMinuit)
62 gMinuit = new TMinuit(100);
63 fSampleParamsHigh =new TArrayD(7) ;
64 fSampleParamsHigh->AddAt(2.174,0) ;
65 fSampleParamsHigh->AddAt(0.106,1) ;
66 fSampleParamsHigh->AddAt(0.173,2) ;
67 fSampleParamsHigh->AddAt(0.06106,3) ;
68 //last two parameters are pedestal and overflow
69 fSampleParamsLow=new TArrayD(7) ;
70 fSampleParamsLow->AddAt(2.456,0) ;
71 fSampleParamsLow->AddAt(0.137,1) ;
72 fSampleParamsLow->AddAt(2.276,2) ;
73 fSampleParamsLow->AddAt(0.08246,3) ;
74 fToFit = new TList() ;
75}
76
77//-----------------------------------------------------------------------------
78AliPHOSRawFitterv1::~AliPHOSRawFitterv1()
79{
80 //Destructor.
81 if(fSampleParamsLow){
82 delete fSampleParamsLow ;
83 fSampleParamsLow=0 ;
84 }
85 if(fSampleParamsHigh){
86 delete fSampleParamsHigh ;
87 fSampleParamsHigh=0;
88 }
89 if(fToFit){
90 delete fToFit ;
91 fToFit=0 ;
92 }
93}
94
95//-----------------------------------------------------------------------------
96AliPHOSRawFitterv1::AliPHOSRawFitterv1(const AliPHOSRawFitterv1 &phosFitter ):
97 AliPHOSRawFitterv0(phosFitter),
98 fSampleParamsLow(0x0),
99 fSampleParamsHigh(0x0),
100 fToFit(0x0)
101{
102 //Copy constructor.
103 fToFit = new TList() ;
104 fSampleParamsLow =new TArrayD(*(phosFitter.fSampleParamsLow)) ;
105 fSampleParamsHigh=new TArrayD(*(phosFitter.fSampleParamsHigh)) ;
106}
107
108//-----------------------------------------------------------------------------
109AliPHOSRawFitterv1& AliPHOSRawFitterv1::operator = (const AliPHOSRawFitterv1 &phosFitter)
110{
111 //Assignment operator.
112
113 fToFit = new TList() ;
114 if(fSampleParamsLow){
115 fSampleParamsLow = phosFitter.fSampleParamsLow ;
116 fSampleParamsHigh= phosFitter.fSampleParamsHigh ;
117 }
118 else{
119 fSampleParamsLow =new TArrayD(*(phosFitter.fSampleParamsLow)) ;
120 fSampleParamsHigh=new TArrayD(*(phosFitter.fSampleParamsHigh)) ;
121 }
122 return *this;
123}
124
125//-----------------------------------------------------------------------------
92236b27 126Bool_t AliPHOSRawFitterv1::Eval(const UShort_t *signal, Int_t sigStart, Int_t sigLength)
379c5c09 127{
128 //Extract an energy deposited in the crystal,
129 //crystal' position (module,column,row),
130 //time and gain (high or low).
131 //First collects sample, then evaluates it and if it has
132 //reasonable shape, fits it with Gamma2 function and extracts
133 //energy and time.
134
92236b27 135 if (fCaloFlag == 2 || fNBunches > 1) {
379c5c09 136 fQuality = 1000;
137 return kTRUE;
138 }
139
140 Float_t pedMean = 0;
141 Float_t pedRMS = 0;
142 Int_t nPed = 0;
143 const Float_t kBaseLine = 1.0;
144 const Int_t kPreSamples = 10;
145
92236b27 146 TArrayI *fSamples = new TArrayI(sigLength); // array of sample amplitudes
147 TArrayI *fTimes = new TArrayI(sigLength); // array of sample time stamps
148 for (Int_t i=0; i<sigLength; i++) {
379c5c09 149 if (i<kPreSamples) {
150 nPed++;
92236b27 151 pedMean += signal[i];
152 pedRMS += signal[i]*signal[i] ;
379c5c09 153 }
92236b27 154 fSamples->AddAt(signal[i],i);
155 fTimes ->AddAt( i ,i);
379c5c09 156 }
157
379c5c09 158 fEnergy = -111;
159 fQuality= 999. ;
160 const Float_t sampleMaxHG=102.332 ; //maximal height of HG sample with given parameterization
161 const Float_t sampleMaxLG=277.196 ; //maximal height of LG sample with given parameterization
162 const Float_t maxEtoFit=5 ; //fit only samples above this energy, accept all samples (with good aRMS) below it
163 Double_t pedestal = 0;
164
165 if (fPedSubtract) {
166 if (nPed > 0) {
167 fPedestalRMS=(pedRMS - pedMean*pedMean/nPed)/nPed ;
168 if(fPedestalRMS > 0.)
169 fPedestalRMS = TMath::Sqrt(fPedestalRMS) ;
170 fEnergy -= (Double_t)(pedMean/nPed); // pedestal subtraction
171 }
172 else
173 return kFALSE;
174 }
175 else {
176 //take pedestals from DB
177 pedestal = (Double_t) fAmpOffset ;
178 if (fCalibData) {
179 Float_t truePed = fCalibData->GetADCpedestalEmc(fModule, fCellZ, fCellX) ;
180 Int_t altroSettings = fCalibData->GetAltroOffsetEmc(fModule, fCellZ, fCellX) ;
181 pedestal += truePed - altroSettings ;
182 }
183 else{
184 AliWarning(Form("Can not read data from OCDB")) ;
185 }
186 fEnergy-=pedestal ;
187 }
188
189 if (fEnergy < kBaseLine) fEnergy = 0;
190
191 //calculate time and energy
92236b27 192 Int_t maxBin=0 ;
193 Int_t maxAmp=0 ;
194 Double_t aMean =0. ;
195 Double_t aRMS =0. ;
196 Double_t wts =0 ;
197 Int_t tStart=0 ;
198
199 for (Int_t i=0; i<sigLength; i++){
200 if(signal[i] > pedestal){
201 Double_t de = signal[i] - pedestal ;
202 if(de > 1.) {
203 aMean += de*i ;
204 aRMS += de*i*i ;
205 wts += de;
379c5c09 206 }
92236b27 207 if(de > 2 && tStart==0)
208 tStart = i ;
209 if(maxAmp < signal[i]){
210 maxBin = i ;
211 maxAmp = signal[i] ;
379c5c09 212 }
213 }
214 }
92236b27 215
216 if (maxBin==sigLength-1){//bad "rising" sample
217 fEnergy = 0. ;
218 fTime = -999. ;
219 fQuality= 999. ;
379c5c09 220 return kTRUE ;
221 }
92236b27 222
379c5c09 223 fEnergy=Double_t(maxAmp)-pedestal ;
224 fOverflow =0 ; //look for plato on the top of sample
92236b27 225 if (fEnergy>500 && //this is not fluctuation of soft sample
226 maxBin<sigLength-1 && fSamples->At(maxBin+1)==maxAmp){ //and there is a plato
379c5c09 227 fOverflow = kTRUE ;
228 }
92236b27 229
230 if (wts > 0) {
231 aMean /= wts;
232 aRMS = aRMS/wts - aMean*aMean;
379c5c09 233 }
234
235 //do not take too small energies
92236b27 236 if (fEnergy < kBaseLine)
379c5c09 237 fEnergy = 0;
238
239 //do not test quality of too soft samples
92236b27 240 if (fEnergy < maxEtoFit){
241 fTime = tStart;
242 if (aRMS < 2.) //sigle peak
243 fQuality = 999. ;
379c5c09 244 else
92236b27 245 fQuality = 0. ;
379c5c09 246 return kTRUE ;
247 }
248
92236b27 249 // if sample has reasonable mean and RMS, try to fit it with gamma2
379c5c09 250
251 gMinuit->mncler(); // Reset Minuit's list of paramters
252 gMinuit->SetPrintLevel(-1) ; // No Printout
253 gMinuit->SetFCN(AliPHOSRawFitterv1::UnfoldingChiSquare) ;
254 // To set the address of the minimization function
255
256 fToFit->Clear("nodelete") ;
257 Double_t b=0,bmin=0,bmax=0 ;
92236b27 258 if (fCaloFlag == 0){ // Low gain
379c5c09 259 fSampleParamsLow->AddAt(pedestal,4) ;
92236b27 260 if (fOverflow)
379c5c09 261 fSampleParamsLow->AddAt(double(maxAmp),5) ;
262 else
263 fSampleParamsLow->AddAt(double(1023),5) ;
92236b27 264 fSampleParamsLow->AddAt(double(sigLength),6) ;
379c5c09 265 fToFit->AddFirst((TObject*)fSampleParamsLow) ;
266 b=fSampleParamsLow->At(2) ;
267 bmin=0.5 ;
268 bmax=10. ;
269 }
92236b27 270 else if (fCaloFlag == 1){ // High gain
379c5c09 271 fSampleParamsHigh->AddAt(pedestal,4) ;
92236b27 272 if (fOverflow)
379c5c09 273 fSampleParamsHigh->AddAt(double(maxAmp),5) ;
274 else
275 fSampleParamsHigh->AddAt(double(1023),5);
92236b27 276 fSampleParamsHigh->AddAt(double(sigLength),6);
379c5c09 277 fToFit->AddFirst((TObject*)fSampleParamsHigh) ;
278 b=fSampleParamsHigh->At(2) ;
279 bmin=0.05 ;
280 bmax=0.4 ;
281 }
282 fToFit->AddLast((TObject*)fSamples) ;
283 fToFit->AddLast((TObject*)fTimes) ;
284
285 gMinuit->SetObjectFit((TObject*)fToFit) ; // To tranfer pointer to UnfoldingChiSquare
286 Int_t ierflg ;
287 gMinuit->mnparm(0, "t0", 1.*tStart, 0.01, -500., 500., ierflg) ;
288 if(ierflg != 0){
289 // AliWarning(Form("Unable to set initial value for fit procedure : t0=%e\n",1.*tStart) ) ;
92236b27 290 fEnergy = 0. ;
291 fTime =-999. ;
292 fQuality= 999. ;
379c5c09 293 return kTRUE ; //will scan further
294 }
295 Double_t amp0=0;
92236b27 296 if (fCaloFlag == 0) // Low gain
297 amp0 = fEnergy/sampleMaxLG;
298 else if (fCaloFlag == 1) // High gain
299 amp0 = fEnergy/sampleMaxHG;
379c5c09 300
301 gMinuit->mnparm(1, "Energy", amp0 , 0.01*amp0, 0, 0, ierflg) ;
302 if(ierflg != 0){
303 // AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;
92236b27 304 fEnergy = 0. ;
305 fTime =-999. ;
306 fQuality= 999. ;
379c5c09 307 return kTRUE ; //will scan further
308 }
309
310 gMinuit->mnparm(2, "p2", b, 0.01*b, bmin, bmax, ierflg) ;
311 if(ierflg != 0){
312 // AliWarning(Form("Unable to set initial value for fit procedure : E=%e\n", amp0)) ;
92236b27 313 fEnergy = 0. ;
314 fTime =-999. ;
315 fQuality= 999. ;
379c5c09 316 return kTRUE ; //will scan further
317 }
318
379c5c09 319 Double_t p0 = 0.0001 ; // "Tolerance" Evaluation stops when EDM = 0.0001*p0 ; The number of function call slightly
320 // depends on it.
321 Double_t p1 = 1.0 ;
322 Double_t p2 = 0.0 ;
323 gMinuit->mnexcm("SET STR", &p2, 0, ierflg) ; // force TMinuit to reduce function calls
324 gMinuit->mnexcm("SET GRA", &p1, 1, ierflg) ; // force TMinuit to use my gradient
325 // gMinuit->SetMaxIterations(100);
326 gMinuit->mnexcm("SET NOW", &p2 , 0, ierflg) ; // No Warnings
327
328 gMinuit->mnexcm("MIGRAD", &p0, 0, ierflg) ; // minimize
329
330 Double_t err,t0err ;
331 Double_t t0,efit ;
92236b27 332 gMinuit->GetParameter(0,t0, t0err) ;
333 gMinuit->GetParameter(1,efit, err) ;
379c5c09 334
335 Double_t bfit, berr ;
336 gMinuit->GetParameter(2,bfit,berr) ;
337
338 //Calculate total energy
92236b27 339 //this is parameterization of dependence of pulse height on parameter b
379c5c09 340 if(fCaloFlag == 0) // Low gain
92236b27 341 efit *= 99.54910 + 78.65038*bfit ;
379c5c09 342 else if(fCaloFlag == 1) // High gain
92236b27 343 efit *= 80.33109 + 128.6433*bfit ;
379c5c09 344
92236b27 345 if(efit < 0. || efit > 10000.){
379c5c09 346 //set energy to previously found max
92236b27 347 fTime =-999.;
348 fQuality= 999 ;
379c5c09 349 return kTRUE;
350 }
351
352 //evaluate fit quality
353 Double_t fmin,fedm,errdef ;
354 Int_t npari,nparx,istat;
355 gMinuit->mnstat(fmin,fedm,errdef,npari,nparx,istat) ;
92236b27 356 fQuality = fmin/sigLength ;
379c5c09 357 //compare quality with some parameterization
92236b27 358 if (fCaloFlag == 0) // Low gain
359 fQuality /= 2.00 + 0.0020*fEnergy ;
360 else if (fCaloFlag == 1) // High gain
361 fQuality /= 0.75 + 0.0025*fEnergy ;
379c5c09 362
92236b27 363 fEnergy = efit ;
364 fTime = t0 - 4.024*bfit ; //-10.402*bfit+4.669*bfit*bfit ; //Correction for 70 samples
365 fTime += sigStart;
379c5c09 366
367 delete fSamples ;
368 delete fTimes ;
369 return kTRUE;
370}
371//_____________________________________________________________________________
372void AliPHOSRawFitterv1::UnfoldingChiSquare(Int_t & /*nPar*/, Double_t * Grad, Double_t & fret, Double_t * x, Int_t iflag)
373{
374 // Number of parameters, Gradient, Chi squared, parameters, what to do
375
376 TList * toFit= (TList*)gMinuit->GetObjectFit() ;
377 TArrayD * params=(TArrayD*)toFit->At(0) ;
378 TArrayI * samples = (TArrayI*)toFit->At(1) ;
379 TArrayI * times = (TArrayI*)toFit->At(2) ;
380
381 fret = 0. ;
382 if(iflag == 2)
383 for(Int_t iparam = 0 ; iparam < 3 ; iparam++)
384 Grad[iparam] = 0 ; // Will evaluate gradient
385
386 Double_t t0=x[0] ;
387 Double_t en=x[1] ;
388 Double_t b=x[2] ;
389 Double_t n=params->At(0) ;
390 Double_t alpha=params->At(1) ;
391 Double_t beta=params->At(3) ;
392 Double_t ped=params->At(4) ;
393
394 Double_t overflow=params->At(5) ;
395 Int_t iBin = (Int_t) params->At(6) ;
396 Int_t nSamples=TMath::Min(iBin+70,samples->GetSize()) ; //Here we set number of points to fit (70)
397 // iBin - first non-zero sample
398 Int_t tStep=times->At(iBin+1)-times->At(iBin) ;
399 Double_t ddt=times->At(iBin)-t0-tStep ;
400 Double_t exp1=TMath::Exp(-alpha*ddt) ;
401 Double_t exp2=TMath::Exp(-beta*ddt) ;
402 Double_t dexp1=TMath::Exp(-alpha*tStep) ;
403 Double_t dexp2=TMath::Exp(-beta*tStep) ;
404 for(Int_t i = iBin; i<nSamples ; i++) {
405 Double_t dt=double(times->At(i))-t0 ;
406 Double_t fsample = double(samples->At(i)) ;
407 if(fsample>=overflow)
408 continue ;
409 Double_t diff ;
410 exp1*=dexp1 ;
411 exp2*=dexp2 ;
412 if(dt<=0.){
413 diff=fsample - ped ;
414 fret += diff*diff ;
415 continue ;
416 }
417 Double_t dtn=TMath::Power(dt,n) ;
418 Double_t dtnE=dtn*exp1 ;
419 Double_t dt2E=dt*dt*exp2 ;
420 Double_t fit=ped+en*(dtnE + b*dt2E) ;
421 diff = fsample - fit ;
422 fret += diff*diff ;
423 if(iflag == 2){ // calculate gradient
424 Grad[0] += en*diff*(dtnE*(n/dt-alpha)+b*dt2E*(2./dt-beta)) ; //derivative over t0
425 Grad[1] -= diff*(dtnE+b*dt2E) ;
426 Grad[2] -= en*diff*dt2E ;
427 }
428 }
429 if(iflag == 2)
430 for(Int_t iparam = 0 ; iparam < 3 ; iparam++)
431 Grad[iparam] *= 2. ;
432}
433//-----------------------------------------------------------------------------
434Double_t AliPHOSRawFitterv1::Gamma2(Double_t dt,Double_t en,Double_t b,TArrayD * params){ //Function for fitting samples
435 //parameters:
436 //dt-time after start
437 //en-amplutude
438 //function parameters
439
440 Double_t ped=params->At(4) ;
441 if(dt<0.)
442 return ped ; //pedestal
443 else
444 return ped+en*(TMath::Power(dt,params->At(0))*TMath::Exp(-dt*params->At(1))+b*dt*dt*TMath::Exp(-dt*params->At(3))) ;
445}