]>
Commit | Line | Data |
---|---|---|
92d9f317 | 1 | /************************************************************************** |
2 | * This file is property of and copyright by * | |
3 | * the Relativistic Heavy Ion Group (RHIG), Yale University, US, 2009 * | |
4 | * * | |
5 | * Primary Author: Per Thomas Hille <p.t.hille@fys.uio.no> * | |
6 | * * | |
7 | * Contributors are mentioned in the code where appropriate. * | |
8 | * Please report bugs to p.t.hille@fys.uio.no * | |
9 | * * | |
10 | * Permission to use, copy, modify and distribute this software and its * | |
11 | * documentation strictly for non-commercial purposes is hereby granted * | |
12 | * without fee, provided that the above copyright notice appears in all * | |
13 | * copies and that both the copyright notice and this permission notice * | |
14 | * appear in the supporting documentation. The authors make no claims * | |
15 | * about the suitability of this software for any purpose. It is * | |
16 | * provided "as is" without express or implied warranty. * | |
17 | **************************************************************************/ | |
18 | ||
19 | ||
20 | // Extraction of amplitude and peak position | |
21 | // FRom CALO raw data using | |
22 | // least square fit for the | |
23 | // Moment assuming identical and | |
24 | // independent errors (equivalent with chi square) | |
25 | // | |
26 | ||
27 | #include "AliCaloRawAnalyzerKStandard.h" | |
28 | #include "AliCaloBunchInfo.h" | |
29 | #include "AliCaloFitResults.h" | |
30 | #include "AliLog.h" | |
31 | #include "TMath.h" | |
32 | #include <stdexcept> | |
33 | #include <iostream> | |
34 | #include "TF1.h" | |
35 | #include "TGraph.h" | |
36 | #include "TRandom.h" | |
37 | ||
38 | ||
39 | using namespace std; | |
40 | ||
41 | ||
42 | #define BAD 4 //CRAP PTH | |
43 | ||
44 | ClassImp( AliCaloRawAnalyzerKStandard ) | |
45 | ||
46 | ||
396baaf6 | 47 | AliCaloRawAnalyzerKStandard::AliCaloRawAnalyzerKStandard() : AliCaloRawAnalyzerFitter("Chi Square ( kStandard )", "KStandard") |
48 | // fkEulerSquared(7.389056098930650227), | |
49 | // fTf1(0) | |
50 | // fTau(2.35), | |
51 | // fFixTau(kTRUE) | |
92d9f317 | 52 | { |
53 | ||
54 | fAlgo = Algo::kStandard; | |
55 | //comment | |
396baaf6 | 56 | |
57 | /* | |
92d9f317 | 58 | for(int i=0; i < ALTROMAXSAMPLES; i++) |
59 | { | |
60 | fXaxis[i] = i; | |
61 | } | |
396baaf6 | 62 | */ |
63 | ||
64 | // fTf1 = new TF1( "myformula", "[0]*((x - [1])/[2])^2*exp(-2*(x -[1])/[2])", 0, 30 ); | |
65 | ||
66 | // InitFormula(fTf1); | |
67 | ||
68 | /* | |
92d9f317 | 69 | if (fFixTau) |
70 | { | |
396baaf6 | 71 | fTf1->FixParameter(2, fTau); |
92d9f317 | 72 | } |
73 | else | |
74 | { | |
75 | fTf1->ReleaseParameter(2); // allow par. to vary | |
76 | fTf1->SetParameter(2, fTau); | |
77 | } | |
396baaf6 | 78 | */ |
92d9f317 | 79 | } |
80 | ||
81 | ||
82 | AliCaloRawAnalyzerKStandard::~AliCaloRawAnalyzerKStandard() | |
83 | { | |
84 | delete fTf1; | |
85 | } | |
86 | ||
87 | ||
396baaf6 | 88 | /* |
89 | void | |
90 | AliCaloRawAnalyzerKStandard::InitFormula( TF1* f) | |
91 | { | |
92 | f = new TF1( "myformula", "[0]*((x - [1])/[2])^2*exp(-2*(x -[1])/[2])", 0, 30 ); | |
93 | } | |
94 | */ | |
92d9f317 | 95 | |
96 | AliCaloFitResults | |
97 | AliCaloRawAnalyzerKStandard::Evaluate( const vector<AliCaloBunchInfo> &bunchlist, const UInt_t altrocfg1, const UInt_t altrocfg2 ) | |
98 | { | |
99 | ||
100 | Float_t pedEstimate = 0; | |
101 | short maxADC = 0; | |
102 | Int_t first = 0; | |
103 | Int_t last = 0; | |
104 | Int_t bunchIndex = 0; | |
105 | Float_t ampEstimate = 0; | |
106 | short timeEstimate = 0; | |
107 | Float_t time = 0; | |
108 | Float_t amp=0; | |
109 | Float_t chi2 = 0; | |
110 | Int_t ndf = 0; | |
111 | Bool_t fitDone = kFALSE; | |
112 | ||
113 | ||
114 | int nsamples = PreFitEvaluateSamples( bunchlist, altrocfg1, altrocfg2, bunchIndex, ampEstimate, | |
115 | maxADC, timeEstimate, pedEstimate, first, last, fAmpCut ); | |
116 | ||
117 | ||
118 | if (ampEstimate >= fAmpCut ) | |
119 | { | |
120 | time = timeEstimate; | |
121 | Int_t timebinOffset = bunchlist.at(bunchIndex).GetStartBin() - (bunchlist.at(bunchIndex).GetLength()-1); | |
122 | amp = ampEstimate; | |
123 | ||
124 | if ( nsamples > 1 && maxADC< OVERFLOWCUT ) | |
125 | { | |
126 | FitRaw(first, last, amp, time, chi2, fitDone); | |
127 | time += timebinOffset; | |
128 | timeEstimate += timebinOffset; | |
129 | ndf = nsamples - 2; | |
130 | } | |
131 | } | |
132 | if ( fitDone ) | |
133 | { | |
134 | Float_t ampAsymm = (amp - ampEstimate)/(amp + ampEstimate); | |
135 | Float_t timeDiff = time - timeEstimate; | |
136 | ||
137 | if ( (TMath::Abs(ampAsymm) > 0.1) || (TMath::Abs(timeDiff) > 2) ) | |
138 | { | |
139 | amp = ampEstimate; | |
140 | time = timeEstimate; | |
141 | fitDone = kFALSE; | |
142 | } | |
143 | } | |
144 | if (amp >= fAmpCut ) | |
145 | { | |
146 | if ( ! fitDone) | |
147 | { | |
148 | amp += (0.5 - gRandom->Rndm()); | |
149 | } | |
150 | //Int_t id = fGeom->GetAbsCellIdFromCellIndexes(in.GetModule(), in.GetRow(), in.GetColumn()) ; | |
151 | // lowGain = in.IsLowGain(); | |
152 | ||
153 | time = time * TIMEBINWITH; | |
154 | ||
155 | /////////////!!!!!!!!!time -= in.GetL1Phase(); | |
156 | ||
157 | time -= fL1Phase; | |
158 | ||
159 | // AliDebug(2,Form("id %d lowGain %d amp %g", id, lowGain, amp)); | |
160 | // AddDigit(digitsArr, id, lowGain, amp, time, chi2, ndf); | |
161 | ||
162 | ||
163 | return AliCaloFitResults( -99, -99, fAlgo , amp, time, | |
164 | time, chi2, ndf, Ret::kDummy ); | |
165 | ||
166 | ||
167 | // AliCaloFitSubarray(index, maxrev, first, last)); | |
168 | ||
169 | } | |
170 | ||
171 | ||
172 | return AliCaloFitResults( Ret::kInvalid, Ret::kInvalid ); | |
173 | } | |
174 | ||
175 | ||
92d9f317 | 176 | /* |
92d9f317 | 177 | void |
178 | AliCaloRawAnalyzerKStandard::PrintFitResult(const TF1 *f) const | |
179 | { | |
180 | //comment | |
181 | cout << endl; | |
182 | cout << __FILE__ << __LINE__ << "Using this samplerange we get" << endl; | |
183 | cout << __FILE__ << __LINE__ << "AMPLITUDE = " << f->GetParameter(0)/fkEulerSquared << ",.. !!!!" << endl; | |
184 | cout << __FILE__ << __LINE__ << "TOF = " << f->GetParameter(1) << ",.. !!!!" << endl; | |
185 | cout << __FILE__ << __LINE__ << "NDF = " << f->GetNDF() << ",.. !!!!" << endl; | |
186 | // cout << __FILE__ << __LINE__ << "STATUS = " << f->GetStatus() << ",.. !!!!" << endl << endl; | |
187 | cout << endl << endl; | |
188 | } | |
396baaf6 | 189 | */ |
92d9f317 | 190 | |
191 | ||
192 | ||
193 | ||
194 | //____________________________________________________________________________ | |
195 | void | |
196 | AliCaloRawAnalyzerKStandard::FitRaw(const Int_t firstTimeBin, const Int_t lastTimeBin, Float_t & amp, Float_t & time, Float_t & chi2, Bool_t & fitDone) const | |
197 | { // Fits the raw signal time distribution | |
198 | ||
199 | //-------------------------------------------------- | |
200 | //Do the fit, different fitting algorithms available | |
201 | //-------------------------------------------------- | |
202 | ||
203 | // fprintf(fp, "%s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__ ); | |
204 | ||
205 | int nsamples = lastTimeBin - firstTimeBin + 1; | |
206 | fitDone = kFALSE; | |
207 | ||
208 | // switch(fFittingAlgorithm) | |
209 | // { | |
210 | // case Algo::kStandard: | |
211 | // { | |
212 | if (nsamples < 3) { return; } // nothing much to fit | |
213 | //printf("Standard fitter \n"); | |
214 | ||
215 | // Create Graph to hold data we will fit | |
216 | ||
217 | TGraph *gSig = new TGraph( nsamples); | |
218 | ||
219 | for (int i=0; i<nsamples; i++) | |
220 | { | |
221 | Int_t timebin = firstTimeBin + i; | |
222 | gSig->SetPoint(i, timebin, GetReversed(timebin)); | |
223 | } | |
224 | ||
225 | TF1 * signalF = new TF1("signal", RawResponseFunction, 0, TIMEBINS , 5); | |
226 | signalF->SetParameters(10.,5., TAU ,ORDER,0.); //set all defaults once, just to be safe | |
227 | signalF->SetParNames("amp","t0","tau","N","ped"); | |
228 | signalF->FixParameter(2,TAU); // tau in units of time bin | |
229 | signalF->FixParameter(3,ORDER); // order | |
230 | signalF->FixParameter(4, 0); // pedestal should be subtracted when we get here | |
231 | signalF->SetParameter(1, time); | |
232 | signalF->SetParameter(0, amp); | |
233 | // set rather loose parameter limits | |
234 | signalF->SetParLimits(0, 0.5*amp, 2*amp ); | |
235 | signalF->SetParLimits(1, time - 4, time + 4); | |
236 | ||
237 | try { | |
238 | gSig->Fit(signalF, "QROW"); // Note option 'W': equal errors on all points | |
239 | // assign fit results | |
240 | amp = signalF->GetParameter(0); | |
241 | time = signalF->GetParameter(1); | |
242 | chi2 = signalF->GetChisquare(); | |
243 | fitDone = kTRUE; | |
244 | } | |
245 | catch (const std::exception & e) { | |
246 | AliError( Form("TGraph Fit exception %s", e.what()) ); | |
247 | // stay with default amp and time in case of exception, i.e. no special action required | |
248 | fitDone = kFALSE; | |
249 | } | |
250 | delete signalF; | |
251 | ||
252 | //printf("Std : Amp %f, time %g\n",amp, time); | |
253 | delete gSig; // delete TGraph | |
254 | ||
255 | // break; | |
256 | // }//kStandard Fitter | |
257 | //---------------------------- | |
258 | ||
259 | /* | |
260 | case Algo::kLogFit: | |
261 | { | |
262 | if (nsamples < 3) { return; } // nothing much to fit | |
263 | //printf("LogFit \n"); | |
264 | ||
265 | // Create Graph to hold data we will fit | |
266 | TGraph *gSigLog = new TGraph( nsamples); | |
267 | for (int i=0; i<nsamples; i++) { | |
268 | Int_t timebin = firstTimeBin + i; | |
269 | gSigLog->SetPoint(timebin, timebin, TMath::Log(fRawAnalyzer->GetReversed(timebin) ) ); | |
270 | } | |
271 | ||
272 | TF1 * signalFLog = new TF1("signalLog", RawResponseFunctionLog, 0, TIMEBINS , 5); | |
273 | signalFLog->SetParameters(2.3, 5.,TAU,ORDER,0.); //set all defaults once, just to be safe | |
274 | signalFLog->SetParNames("amplog","t0","tau","N","ped"); | |
275 | signalFLog->FixParameter(2,TAU); // tau in units of time bin | |
276 | signalFLog->FixParameter(3, ORDER); // order | |
277 | signalFLog->FixParameter(4, 0); // pedestal should be subtracted when we get here | |
278 | signalFLog->SetParameter(1, time); | |
279 | if (amp>=1) { | |
280 | signalFLog->SetParameter(0, TMath::Log(amp)); | |
281 | } | |
282 | ||
283 | gSigLog->Fit(signalFLog, "QROW"); // Note option 'W': equal errors on all points | |
284 | ||
285 | // assign fit results | |
286 | Double_t amplog = signalFLog->GetParameter(0); //Not Amp, but Log of Amp | |
287 | amp = TMath::Exp(amplog); | |
288 | time = signalFLog->GetParameter(1); | |
289 | fitDone = kTRUE; | |
290 | ||
291 | delete signalFLog; | |
292 | //printf("LogFit: Amp %f, time %g\n",amp, time); | |
293 | delete gSigLog; | |
294 | break; | |
295 | } //kLogFit | |
296 | //---------------------------- | |
297 | //---------------------------- | |
298 | }//switch fitting algorithms | |
299 | */ | |
300 | return; | |
301 | } | |
302 | ||
303 | ||
304 | //__________________________________________________________________ | |
305 | void | |
306 | AliCaloRawAnalyzerKStandard::FitParabola(const TGraph *gSig, Float_t & amp) const | |
307 | { | |
308 | //BEG YS alternative methods to calculate the amplitude | |
309 | Double_t * ymx = gSig->GetX() ; | |
310 | Double_t * ymy = gSig->GetY() ; | |
311 | const Int_t kN = 3 ; | |
312 | Double_t ymMaxX[kN] = {0., 0., 0.} ; | |
313 | Double_t ymMaxY[kN] = {0., 0., 0.} ; | |
314 | Double_t ymax = 0. ; | |
315 | // find the maximum amplitude | |
316 | Int_t ymiMax = 0 ; | |
317 | for (Int_t ymi = 0; ymi < gSig->GetN(); ymi++) { | |
318 | if (ymy[ymi] > ymMaxY[0] ) { | |
319 | ymMaxY[0] = ymy[ymi] ; //<========== This is the maximum amplitude | |
320 | ymMaxX[0] = ymx[ymi] ; | |
321 | ymiMax = ymi ; | |
322 | } | |
323 | } | |
324 | // find the maximum by fitting a parabola through the max and the two adjacent samples | |
325 | if ( ymiMax < gSig->GetN()-1 && ymiMax > 0) { | |
326 | ymMaxY[1] = ymy[ymiMax+1] ; | |
327 | ymMaxY[2] = ymy[ymiMax-1] ; | |
328 | ymMaxX[1] = ymx[ymiMax+1] ; | |
329 | ymMaxX[2] = ymx[ymiMax-1] ; | |
330 | if (ymMaxY[0]*ymMaxY[1]*ymMaxY[2] > 0) { | |
331 | //fit a parabola through the 3 points y= a+bx+x*x*x | |
332 | Double_t sy = 0 ; | |
333 | Double_t sx = 0 ; | |
334 | Double_t sx2 = 0 ; | |
335 | Double_t sx3 = 0 ; | |
336 | Double_t sx4 = 0 ; | |
337 | Double_t sxy = 0 ; | |
338 | Double_t sx2y = 0 ; | |
339 | for (Int_t i = 0; i < kN ; i++) { | |
340 | sy += ymMaxY[i] ; | |
341 | sx += ymMaxX[i] ; | |
342 | sx2 += ymMaxX[i]*ymMaxX[i] ; | |
343 | sx3 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ; | |
344 | sx4 += ymMaxX[i]*ymMaxX[i]*ymMaxX[i]*ymMaxX[i] ; | |
345 | sxy += ymMaxX[i]*ymMaxY[i] ; | |
346 | sx2y += ymMaxX[i]*ymMaxX[i]*ymMaxY[i] ; | |
347 | } | |
348 | Double_t cN = (sx2y*kN-sy*sx2)*(sx3*sx-sx2*sx2)-(sx2y*sx-sxy*sx2)*(sx3*kN-sx*sx2); | |
349 | Double_t cD = (sx4*kN-sx2*sx2)*(sx3*sx-sx2*sx2)-(sx4*sx-sx3*sx2)*(sx3*kN-sx*sx2) ; | |
350 | Double_t c = cN / cD ; | |
351 | Double_t b = ((sx2y*kN-sy*sx2)-c*(sx4*kN-sx2*sx2))/(sx3*kN-sx*sx2) ; | |
352 | Double_t a = (sy-b*sx-c*sx2)/kN ; | |
353 | Double_t xmax = -b/(2*c) ; | |
354 | ymax = a + b*xmax + c*xmax*xmax ;//<========== This is the maximum amplitude | |
355 | amp = ymax; | |
356 | } | |
357 | } | |
358 | ||
359 | Double_t diff = TMath::Abs(1-ymMaxY[0]/amp) ; | |
360 | if (diff > 0.1) | |
361 | amp = ymMaxY[0] ; | |
362 | //printf("Yves : Amp %f, time %g\n",amp, time); | |
363 | //END YS | |
364 | return; | |
365 | } | |
366 | ||
367 | ||
368 | ||
369 | //__________________________________________________________________ | |
370 | Double_t | |
371 | AliCaloRawAnalyzerKStandard::RawResponseFunction(Double_t *x, Double_t *par) | |
372 | { | |
373 | // Matches version used in 2007 beam test | |
374 | // | |
375 | // Shape of the electronics raw reponse: | |
376 | // It is a semi-gaussian, 2nd order Gamma function of the general form | |
377 | // | |
378 | // xx = (t - t0 + tau) / tau [xx is just a convenient help variable] | |
379 | // F = A * (xx**N * exp( N * ( 1 - xx) ) for xx >= 0 | |
380 | // F = 0 for xx < 0 | |
381 | // | |
382 | // parameters: | |
383 | // A: par[0] // Amplitude = peak value | |
384 | // t0: par[1] | |
385 | // tau: par[2] | |
386 | // N: par[3] | |
387 | // ped: par[4] | |
388 | // | |
389 | Double_t signal = 0.; | |
390 | Double_t tau = par[2]; | |
391 | Double_t n = par[3]; | |
392 | Double_t ped = par[4]; | |
393 | Double_t xx = ( x[0] - par[1] + tau ) / tau ; | |
394 | ||
395 | if (xx <= 0) | |
396 | signal = ped ; | |
397 | else { | |
398 | signal = ped + par[0] * TMath::Power(xx , n) * TMath::Exp(n * (1 - xx )) ; | |
399 | } | |
400 | return signal ; | |
401 | } | |
402 |