]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TOF/AliTOFT0maker.cxx
Fixed striong (Svein)
[u/mrichter/AliRoot.git] / TOF / AliTOFT0maker.cxx
CommitLineData
8f589502 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 **************************************************************************/
6a4e212e 15/* $Id: AliTOFT0maker.cxx,v 1.8 2010/01/19 16:32:20 noferini Exp $ */
8f589502 16
17/////////////////////////////////////////////////////////////////////////////
18// //
19// This class contains the basic functions for the time zero //
6a4e212e 20// evaluation with TOF detector informations. //
21// Use case in an analysis task: //
22// //
23// Create the object in the task constructor (fTOFmaker is a private var) //
5b4ed716 24// AliESDpid *extPID=new AliESDpid(); //
25// fTOFmaker = new AliTOFT0maker(extPID); //
26// fTOFmaker->SetTimeResolution(100.0); // if you want set the TOF res //
6a4e212e 27// 115 ps is the TOF default resolution value //
28// //
29// Use the RemakePID method in the task::Exec //
30// Double_t* calcolot0; //
31// calcolot0=fTOFmaker->RemakePID(fESD); //
32// //calcolot0[0] = calculated event time //
33// //calcolot0[1] = event time time resolution //
34// //calcolot0[2] = average event time for the current fill //
03bd764d 35// //calcolot0[3] = tracks at TOF //
36// //calcolot0[4] = calculated event time (only TOF) //
37// //calcolot0[5] = event time time resolution (only TOF) //
38// //calcolot0[6] = sigma t0 fill //
39// //calcolot0[7] = tracks at TOF really used in tht algorithm //
6a4e212e 40// //
41// Let consider that: //
42// - the PIF is automatically recalculated with the event time subtrction //
8f589502 43// //
44/////////////////////////////////////////////////////////////////////////////
45
536031f2 46#include "AliTOFT0v1.h"
47#include "AliTOFT0maker.h"
536031f2 48#include "AliPID.h"
f858b00e 49#include "AliLog.h"
536031f2 50#include "AliESDpid.h"
03bd764d 51#include "AliESDEvent.h"
52#include "TFile.h"
53#include "TH1F.h"
5b4ed716 54#include "AliTOFcalib.h"
55#include "AliTOFRunParams.h"
56#include "TRandom.h"
f858b00e 57#include "AliTOFHeader.h"
536031f2 58
59ClassImp(AliTOFT0maker)
60
61//____________________________________________________________________________
5b4ed716 62AliTOFT0maker::AliTOFT0maker():
63 TObject(),
64 fT0TOF(NULL),
65 fPIDesd(NULL),
66 fExternalPIDFlag(kFALSE),
67 fTOFcalib(NULL),
68 fNoTOFT0(0),
2a258f40 69 fNmomBins(0),
5b4ed716 70 fTimeResolution(100),
71 fT0sigma(1000),
72 fHmapChannel(0),
73 fKmask(0),
2a258f40 74 fT0width(150.),
75 fT0spreadExt(-1.),
76 fT0fillExt(0)
5b4ed716 77{
78 // ctr
79 fCalculated[0] = 0;
80 fCalculated[1] = 0;
81 fCalculated[2] = 0;
82 fCalculated[3] = 0;
83
5b4ed716 84 if(AliPID::ParticleMass(0) == 0) new AliPID();
85
86 fPIDesd = new AliESDpid();
87
2a258f40 88 fNmomBins = fPIDesd->GetTOFResponse().GetNmomBins();
89 SetTOFResponse();
90
91 fT0TOF = new AliTOFT0v1(fPIDesd);
92
5b4ed716 93}
94//____________________________________________________________________________
95AliTOFT0maker::AliTOFT0maker(AliESDpid *externalPID, AliTOFcalib *tofCalib):
96 TObject(),
97 fT0TOF(NULL),
98 fPIDesd(externalPID),
99 fExternalPIDFlag(kTRUE),
100 fTOFcalib(tofCalib),
03bd764d 101 fNoTOFT0(0),
2a258f40 102 fNmomBins(0),
5b4ed716 103 fTimeResolution(100),
03bd764d 104 fT0sigma(1000),
105 fHmapChannel(0),
5b4ed716 106 fKmask(0),
2a258f40 107 fT0width(150.),
108 fT0spreadExt(-1.),
109 fT0fillExt(0)
536031f2 110{
6a4e212e 111 // ctr
6a4e212e 112 fCalculated[0] = 0;
113 fCalculated[1] = 0;
114 fCalculated[2] = 0;
03bd764d 115 fCalculated[3] = 0;
6a4e212e 116
536031f2 117 if(AliPID::ParticleMass(0) == 0) new AliPID();
03bd764d 118
5b4ed716 119 if(!fPIDesd){
120 fPIDesd = new AliESDpid();
121 fExternalPIDFlag = kFALSE;
122 }
123
2a258f40 124 fNmomBins = fPIDesd->GetTOFResponse().GetNmomBins();
125 SetTOFResponse();
5b4ed716 126
2a258f40 127 fT0TOF = new AliTOFT0v1(fPIDesd);
8f589502 128
8f589502 129}
5b4ed716 130
8f589502 131//____________________________________________________________________________
536031f2 132AliTOFT0maker::~AliTOFT0maker()
133{
134 // dtor
5b4ed716 135
136 delete fT0TOF;
137 if (!fExternalPIDFlag) delete fPIDesd;
536031f2 138}
139//____________________________________________________________________________
5b4ed716 140Double_t* AliTOFT0maker::ComputeT0TOF(AliESDEvent *esd,Double_t t0time,Double_t t0sigma){
8f589502 141 //
142 // Remake TOF PID probabilities
143 //
2a258f40 144 Double_t t0tof[6];
536031f2 145
03bd764d 146 if(fKmask) ApplyMask(esd);
147
2a258f40 148 Double_t t0fill = 0.;
149
150 fPIDesd->GetTOFResponse().ResetT0info();
151
5b4ed716 152 /* get T0 spread from TOFcalib if available otherwise use default value */
153 if (fTOFcalib && esd) {
154 AliTOFRunParams *runParams = fTOFcalib->GetRunParams();
155 if (runParams && runParams->GetTimestamp(0) != 0) {
156 Float_t t0spread = runParams->EvalT0Spread(esd->GetTimeStamp());
2a258f40 157 if(fT0spreadExt > 0) SetT0FillWidth(fT0spreadExt);
158 else{
159 SetT0FillWidth(t0spread);
160 t0fill = fT0fillExt;
161 }
162 }
163 else{
90beec49 164 if(fT0spreadExt > 0) SetT0FillWidth(fT0spreadExt);
2a258f40 165 t0fill = fT0fillExt;
5b4ed716 166 }
536031f2 167 }
bc4fdef6 168 else if(esd){
169 Float_t t0spread = esd->GetSigma2DiamondZ(); // vertex pread ^2
170 if(t0spread > 0) t0spread = TMath::Sqrt(t0spread)/0.0299792458;
171
172 if(fT0spreadExt > 0) SetT0FillWidth(fT0spreadExt);
173 else{
174 SetT0FillWidth(t0spread);
175 t0fill = fT0fillExt;
176 }
177 }
03bd764d 178
bf40b7fa 179 Float_t thrGood = TMath::Max(Float_t(500.),fT0width*3);
180
5b4ed716 181 fT0TOF->Init(esd);
182 AliTOFT0v1* t0maker= fT0TOF;
5b4ed716 183
184 t0maker->DefineT0("all",1.5,3.0);
185 t0tof[0] = t0maker->GetResult(0);
186 t0tof[1] = t0maker->GetResult(1);
187 t0tof[2] = t0maker->GetResult(2);
188 t0tof[3] = t0maker->GetResult(3);
2a258f40 189 t0tof[4] = t0maker->GetResult(4);
190 t0tof[5] = t0maker->GetResult(5);
536031f2 191
8f589502 192 Float_t lT0Current=0.;
536031f2 193 fT0sigma=1000;
194
03bd764d 195// Int_t nrun = esd->GetRunNumber();
5b4ed716 196
03bd764d 197 t0time += t0fill;
8f589502 198
5b4ed716 199 Float_t sigmaFill = fT0width;
200
201 if(sigmaFill < 20) sigmaFill = 140;
8f589502 202
03bd764d 203 fCalculated[0]=-1000*t0tof[0]; // best t0
204 fCalculated[1]=1000*t0tof[1]; // sigma best t0
205 fCalculated[2] = t0fill; //t0 fill
206 fCalculated[3] = t0tof[2]; // n TOF tracks
207 fCalculated[4]=-1000*t0tof[0]; // TOF t0
208 fCalculated[5]=1000*t0tof[1]; // TOF t0 sigma
209 fCalculated[6]=sigmaFill; // sigma t0 fill
210 fCalculated[7] = t0tof[3]; // n TOF tracks used for T0
211
bf40b7fa 212 if(fCalculated[7] > 30) thrGood = 10000000;
213
2a258f40 214 //statistics
215 fCalculated[8] = t0tof[4]; // real time in s
216 fCalculated[9] = t0tof[5]; // cpu time in s
217
bf40b7fa 218 if(fCalculated[1] < sigmaFill && TMath::Abs(fCalculated[0] - t0fill) < thrGood && fCalculated[1] < fTimeResolution*1.2){
6a4e212e 219 fT0sigma=fCalculated[1];
220 lT0Current=fCalculated[0];
536031f2 221 }
03bd764d 222 else{
223 fCalculated[4] = t0fill;
224 fCalculated[5] = sigmaFill;
225 }
226
5b4ed716 227 if(fCalculated[1] < 1 || fT0sigma > sigmaFill || fCalculated[1] > fTimeResolution* 1.2){
03bd764d 228 fT0sigma =1000;
229 fCalculated[4] = t0fill;
230 fCalculated[5] = sigmaFill;
231 }
536031f2 232
233 if(t0sigma < 1000){
234 if(fT0sigma < 1000){
235 Double_t w1 = 1./t0sigma/t0sigma;
6a4e212e 236 Double_t w2 = 1./fCalculated[1]/fCalculated[1];
536031f2 237
238 Double_t wtot = w1+w2;
239
6a4e212e 240 lT0Current = (w1*t0time + w2*fCalculated[0]) / wtot;
536031f2 241 fT0sigma = TMath::Sqrt(1./wtot);
242 }
243 else{
8f589502 244 lT0Current=t0time;
536031f2 245 fT0sigma=t0sigma;
246 }
247 }
248
bf40b7fa 249 if(fT0sigma < sigmaFill && TMath::Abs(lT0Current - t0fill) < thrGood){
03bd764d 250 fCalculated[1]=fT0sigma;
251 fCalculated[0]=lT0Current;
252 }
253
254 if(fT0sigma >= 1000 || fNoTOFT0){
8f589502 255 lT0Current = t0fill;
03bd764d 256 fT0sigma = sigmaFill;
8f589502 257
6a4e212e 258 fCalculated[0] = t0fill;
03bd764d 259 fCalculated[1] = sigmaFill;
536031f2 260 }
261
5b4ed716 262 // T0 pt bin
721ed687 263 Float_t *t0values = new Float_t[fNmomBins];
264 Float_t *t0resolution = new Float_t[fNmomBins];
2a258f40 265 if(fCalculated[7] < 100){
266 for(Int_t i=0;i<fNmomBins;i++){
267 t0maker->DefineT0("all",fPIDesd->GetTOFResponse().GetMinMom(i),fPIDesd->GetTOFResponse().GetMaxMom(i));
268 t0tof[0] = t0maker->GetResult(0);
269 t0tof[1] = t0maker->GetResult(1);
270 t0tof[2] = t0maker->GetResult(2);
271 t0tof[3] = t0maker->GetResult(3);
2a258f40 272
273 Float_t t0bin =-1000*t0tof[0]; // best t0
274 Float_t t0binRes =1000*t0tof[1]; // sigma best t0
275
bf40b7fa 276 if(t0binRes < sigmaFill && t0binRes < fTimeResolution * 1.2 && TMath::Abs(t0bin - t0fill) < thrGood){
2a258f40 277 // Ok T0
278 if(t0sigma < 1000){
279 Double_t w1 = 1./t0sigma/t0sigma;
280 Double_t w2 = 1./t0binRes/t0binRes;
281
282 Double_t wtot = w1+w2;
283
284 t0bin = (w1*t0time + w2*t0bin) / wtot;
285 t0binRes = TMath::Sqrt(1./wtot);
286 }
287 }
288 else{
289 t0bin = t0fill;
290 t0binRes = sigmaFill;
291 if(t0sigma < 1000){
292 t0bin = t0time;
293 t0binRes= t0sigma;
294 }
295 }
721ed687 296 t0values[i] = t0bin;
297 t0resolution[i] = t0binRes;
5b4ed716 298 }
2a258f40 299 }
300 else{
301 for(Int_t i=0;i<fNmomBins;i++){
721ed687 302 t0values[i] = lT0Current;
303 t0resolution[i] = fT0sigma;
5b4ed716 304 }
305 }
721ed687 306 for(Int_t i=0;i<fNmomBins;i++){
307 fPIDesd->GetTOFResponse().SetT0bin(i,t0values[i]);
308 fPIDesd->GetTOFResponse().SetT0binRes(i,t0resolution[i]);
309 }
310
311 delete[] t0values;
312 delete[] t0resolution;
536031f2 313
6a4e212e 314 return fCalculated;
536031f2 315}
316//____________________________________________________________________________
5b4ed716 317Double_t *AliTOFT0maker::GetT0p(Float_t p){// [0]=to -- [1] = sigma T0
2a258f40 318 Int_t i=fPIDesd->GetTOFResponse().GetMomBin(p);
03bd764d 319
2a258f40 320 fT0cur[0] = fPIDesd->GetTOFResponse().GetT0bin(i);
321 fT0cur[1] = fPIDesd->GetTOFResponse().GetT0binRes(i);
322
5b4ed716 323 return fT0cur;
536031f2 324}
325//____________________________________________________________________________
5b4ed716 326void AliTOFT0maker::SetTOFResponse(){
2a258f40 327 fPIDesd->GetTOFResponse().SetTimeResolution(fTimeResolution);
5b4ed716 328}
329//____________________________________________________________________________
330Float_t AliTOFT0maker::GetExpectedSigma(Float_t mom, Float_t tof, Float_t mass){
5b4ed716 331 Float_t sigma = fPIDesd->GetTOFResponse().GetExpectedSigma(mom,tof,mass);
8f589502 332
5b4ed716 333 return sigma;
536031f2 334}
335//____________________________________________________________________________
5b4ed716 336void AliTOFT0maker::ApplyT0TOF(AliESDEvent *esd){
8f589502 337 //
5b4ed716 338 // Recalculate TOF PID probabilities
8f589502 339 //
340
90beec49 341// subtruct t0 for each track
342 Int_t ntracks = esd->GetNumberOfTracks();
536031f2 343
90beec49 344 while (ntracks--) {
345 AliESDtrack *t=esd->GetTrack(ntracks);
03bd764d 346
90beec49 347 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
5b4ed716 348
90beec49 349 Double_t time=t->GetTOFsignal();
350 Float_t p = t->GetP();
351
352 Double_t *t0=GetT0p(p);
353 time -= t0[0];
354 t->SetTOFsignal(time);
355 }
356
357 for(Int_t i=0;i<fNmomBins;i++){
358 fPIDesd->GetTOFResponse().SetT0bin(i,0.0);
359 }
360
5b4ed716 361 //
536031f2 362}
03bd764d 363//____________________________________________________________________________
364void AliTOFT0maker::LoadChannelMap(char *filename){
365 // Load the histo with the channel off map
366 TFile *f= new TFile(filename);
367 if(!f){
368 printf("Cannot open the channel map file (%s)\n",filename);
369 return;
370 }
371
372 fHmapChannel = (TH1F *) f->Get("hChEnabled");
373
374 if(!fHmapChannel){
375 printf("Cannot laod the channel map histo (from %s)\n",filename);
376 return;
377 }
378
379}
380//____________________________________________________________________________
381void AliTOFT0maker::ApplyMask(AliESDEvent * const esd){
382 // Switch off the disable channel
383 if(!fHmapChannel){
384 printf("Channel Map is not available\n");
385 return;
386 }
387
388 Int_t ntracks = esd->GetNumberOfTracks();
389
390 while (ntracks--) {
391 AliESDtrack *t=esd->GetTrack(ntracks);
392
393 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
394
395 Int_t chan = t->GetTOFCalChannel();
396
397 if(fHmapChannel->GetBinContent(chan) < 0.01){
398 t->ResetStatus(AliESDtrack::kTOFout);
399 }
400 }
401}
402
5b4ed716 403Float_t
404AliTOFT0maker::TuneForMC(AliESDEvent *esd){ // return true T0 event
405 //
406 // tune for MC data
407 //
408
409 Float_t TOFtimeResolutionDefault=80;
410
411 Float_t t0 = gRandom->Gaus(0.,fT0width);
412
413 Float_t extraSmearing = 0;
414
415 if(fTimeResolution > TOFtimeResolutionDefault){
416 extraSmearing = TMath::Sqrt(fTimeResolution*fTimeResolution - TOFtimeResolutionDefault*TOFtimeResolutionDefault);
417 }
418
419 // subtruct t0 for each track
420 Int_t ntracks = esd->GetNumberOfTracks();
421
422 while (ntracks--) {
423 AliESDtrack *t=esd->GetTrack(ntracks);
424
425 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
426
427 /* check if channel is enabled */
bc4fdef6 428 if (fTOFcalib){
429 if(!fTOFcalib->IsChannelEnabled(t->GetTOFCalChannel())) {
430 /* reset TOF status */
431 t->ResetStatus(AliESDtrack::kTOFin);
432 t->ResetStatus(AliESDtrack::kTOFout);
433 t->ResetStatus(AliESDtrack::kTOFmismatch);
434 t->ResetStatus(AliESDtrack::kTOFpid);
435 }
5b4ed716 436 }
437
438 Double_t time=t->GetTOFsignal();
439
440 time += t0;
441
442 if(extraSmearing>0){
443 Float_t smearing = gRandom->Gaus(0.,extraSmearing);
444 time += smearing;
445 }
446
447 t->SetTOFsignal(time);
448 }
449 //
450 return t0;
451}
f858b00e 452//_________________________________________________________________________
453void AliTOFT0maker::WriteInESD(AliESDEvent *esd){
454 //
455 // Write t0Gen, t0ResGen, nt0;
456 // t0resESD[0:nt0], it0ESD[0:nt0]
457 // in the AliESDEvent
458 //
459 Int_t nMomBins = fPIDesd->GetTOFResponse().GetNmomBins();
460
461 Int_t nt0=0;
462 Float_t *t0 = new Float_t[nMomBins];
463 Float_t *t0res = new Float_t[nMomBins];
464 Int_t *multT0 = new Int_t[nMomBins];
465
466 for(Int_t i=0;i<nMomBins;i++){
467// printf("START %i) %f %f\n",i,fT0event[i],fT0resolution[i]);
468 multT0[i]=0;
469 Bool_t kNewT0=kTRUE;
470 for(Int_t j=0;j < nt0;j++){
471 if(TMath::Abs(fPIDesd->GetTOFResponse().GetT0bin(i) - t0[j])<0.1){
472 kNewT0=kFALSE;
473 multT0[j]++;
474 j=nMomBins*10;
475 }
476 }
477 if(kNewT0){
478 t0[nt0]=fPIDesd->GetTOFResponse().GetT0bin(i);
479 t0res[nt0]=fPIDesd->GetTOFResponse().GetT0binRes(i);
480 nt0++;
481 }
482 }
483
484 Int_t iMultT0=0,nmult=0;
485 for(Int_t j=0;j < nt0;j++){ // find the most frequent
486 if(multT0[j] > nmult){
487 iMultT0 = j;
488 nmult = multT0[j];
489 }
490 }
491
492 Float_t *t0ESD = new Float_t[nMomBins];
493 Float_t *t0resESD = new Float_t[nMomBins];
494 Int_t *it0ESD = new Int_t[nMomBins];
495
496 Float_t t0Gen,t0ResGen;
497 t0Gen = t0[iMultT0];
498 t0ResGen = t0res[iMultT0];
499 nt0=0;
500 // printf("T0 to ESD\n%f %f\n",t0Gen,t0ResGen);
501 for(Int_t i=0;i<nMomBins;i++){
502 if(TMath::Abs(fPIDesd->GetTOFResponse().GetT0bin(i) - t0Gen)>0.1){
503 t0ESD[nt0]=fPIDesd->GetTOFResponse().GetT0bin(i);
504 t0resESD[nt0]=fPIDesd->GetTOFResponse().GetT0binRes(i);
505 it0ESD[nt0]=i;
506// printf("%i) %f %f %i\n",nt0,t0ESD[nt0],t0resESD[nt0],it0ESD[nt0]);
507 nt0++;
508 }
509 }
510
511 // Write t0Gen,t0ResGen; nt0; t0resESD[0:nt0],it0ESD[0:nt0] in the AliESDEvent
512
513 AliTOFHeader *tofHeader =
514 new AliTOFHeader(t0Gen,t0ResGen,nt0,
c85a1b24 515 t0ESD,t0resESD,it0ESD,fTimeResolution,fT0width);
f858b00e 516
517 esd->SetTOFHeader(tofHeader);
518
c85a1b24 519 AliDebug(1,Form("resTOF=%f T0spread=%f t0Gen=%f t0resGen=%f",fTimeResolution,fT0width,t0Gen,t0ResGen));
520 AliDebug(1,Form("%d ",nt0));
f858b00e 521 for (Int_t ii=0; ii<nt0; ii++)
c85a1b24 522 AliDebug(1,Form("pBin=%d t0val=%f t0res=%f",it0ESD[ii],t0ESD[ii],t0resESD[ii]));
f858b00e 523
524 delete[] t0;
525 t0 = NULL;
526 delete[] t0res;
527 t0res = NULL;
528 delete[] multT0;
529 multT0 = NULL;
530 delete[] t0ESD;
531 t0ESD = NULL;
532 delete[] t0resESD;
533 t0resESD = NULL;
534 delete[] it0ESD;
535 it0ESD = NULL;
536
537}