]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TOF/AliTOFT0maker.cxx
tuning T0-TOF algorithm
[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 //
144
2a258f40 145 Double_t t0tof[6];
536031f2 146
03bd764d 147 if(fKmask) ApplyMask(esd);
148
2a258f40 149 Double_t t0fill = 0.;
150
151 fPIDesd->GetTOFResponse().ResetT0info();
152
5b4ed716 153 /* get T0 spread from TOFcalib if available otherwise use default value */
154 if (fTOFcalib && esd) {
155 AliTOFRunParams *runParams = fTOFcalib->GetRunParams();
156 if (runParams && runParams->GetTimestamp(0) != 0) {
157 Float_t t0spread = runParams->EvalT0Spread(esd->GetTimeStamp());
2a258f40 158 if(fT0spreadExt > 0) SetT0FillWidth(fT0spreadExt);
159 else{
160 SetT0FillWidth(t0spread);
161 t0fill = fT0fillExt;
162 }
163 }
164 else{
90beec49 165 if(fT0spreadExt > 0) SetT0FillWidth(fT0spreadExt);
2a258f40 166 t0fill = fT0fillExt;
5b4ed716 167 }
536031f2 168 }
03bd764d 169
bf40b7fa 170 Float_t thrGood = TMath::Max(Float_t(500.),fT0width*3);
171
5b4ed716 172 fT0TOF->Init(esd);
173 AliTOFT0v1* t0maker= fT0TOF;
5b4ed716 174
175 t0maker->DefineT0("all",1.5,3.0);
176 t0tof[0] = t0maker->GetResult(0);
177 t0tof[1] = t0maker->GetResult(1);
178 t0tof[2] = t0maker->GetResult(2);
179 t0tof[3] = t0maker->GetResult(3);
2a258f40 180 t0tof[4] = t0maker->GetResult(4);
181 t0tof[5] = t0maker->GetResult(5);
536031f2 182
8f589502 183 Float_t lT0Current=0.;
536031f2 184 fT0sigma=1000;
185
03bd764d 186// Int_t nrun = esd->GetRunNumber();
5b4ed716 187
03bd764d 188 t0time += t0fill;
8f589502 189
5b4ed716 190 Float_t sigmaFill = fT0width;
191
192 if(sigmaFill < 20) sigmaFill = 140;
8f589502 193
03bd764d 194 fCalculated[0]=-1000*t0tof[0]; // best t0
195 fCalculated[1]=1000*t0tof[1]; // sigma best t0
196 fCalculated[2] = t0fill; //t0 fill
197 fCalculated[3] = t0tof[2]; // n TOF tracks
198 fCalculated[4]=-1000*t0tof[0]; // TOF t0
199 fCalculated[5]=1000*t0tof[1]; // TOF t0 sigma
200 fCalculated[6]=sigmaFill; // sigma t0 fill
201 fCalculated[7] = t0tof[3]; // n TOF tracks used for T0
202
bf40b7fa 203 if(fCalculated[7] > 30) thrGood = 10000000;
204
2a258f40 205 //statistics
206 fCalculated[8] = t0tof[4]; // real time in s
207 fCalculated[9] = t0tof[5]; // cpu time in s
208
bf40b7fa 209 if(fCalculated[1] < sigmaFill && TMath::Abs(fCalculated[0] - t0fill) < thrGood && fCalculated[1] < fTimeResolution*1.2){
6a4e212e 210 fT0sigma=fCalculated[1];
211 lT0Current=fCalculated[0];
536031f2 212 }
03bd764d 213 else{
214 fCalculated[4] = t0fill;
215 fCalculated[5] = sigmaFill;
216 }
217
5b4ed716 218 if(fCalculated[1] < 1 || fT0sigma > sigmaFill || fCalculated[1] > fTimeResolution* 1.2){
03bd764d 219 fT0sigma =1000;
220 fCalculated[4] = t0fill;
221 fCalculated[5] = sigmaFill;
222 }
536031f2 223
224 if(t0sigma < 1000){
225 if(fT0sigma < 1000){
226 Double_t w1 = 1./t0sigma/t0sigma;
6a4e212e 227 Double_t w2 = 1./fCalculated[1]/fCalculated[1];
536031f2 228
229 Double_t wtot = w1+w2;
230
6a4e212e 231 lT0Current = (w1*t0time + w2*fCalculated[0]) / wtot;
536031f2 232 fT0sigma = TMath::Sqrt(1./wtot);
233 }
234 else{
8f589502 235 lT0Current=t0time;
536031f2 236 fT0sigma=t0sigma;
237 }
238 }
239
bf40b7fa 240 if(fT0sigma < sigmaFill && TMath::Abs(lT0Current - t0fill) < thrGood){
03bd764d 241 fCalculated[1]=fT0sigma;
242 fCalculated[0]=lT0Current;
243 }
244
245 if(fT0sigma >= 1000 || fNoTOFT0){
8f589502 246 lT0Current = t0fill;
03bd764d 247 fT0sigma = sigmaFill;
8f589502 248
6a4e212e 249 fCalculated[0] = t0fill;
03bd764d 250 fCalculated[1] = sigmaFill;
536031f2 251 }
252
5b4ed716 253 // T0 pt bin
2a258f40 254 if(fCalculated[7] < 100){
255 for(Int_t i=0;i<fNmomBins;i++){
256 t0maker->DefineT0("all",fPIDesd->GetTOFResponse().GetMinMom(i),fPIDesd->GetTOFResponse().GetMaxMom(i));
257 t0tof[0] = t0maker->GetResult(0);
258 t0tof[1] = t0maker->GetResult(1);
259 t0tof[2] = t0maker->GetResult(2);
260 t0tof[3] = t0maker->GetResult(3);
261
262
263 Float_t t0bin =-1000*t0tof[0]; // best t0
264 Float_t t0binRes =1000*t0tof[1]; // sigma best t0
265
bf40b7fa 266 if(t0binRes < sigmaFill && t0binRes < fTimeResolution * 1.2 && TMath::Abs(t0bin - t0fill) < thrGood){
2a258f40 267 // Ok T0
268 if(t0sigma < 1000){
269 Double_t w1 = 1./t0sigma/t0sigma;
270 Double_t w2 = 1./t0binRes/t0binRes;
271
272 Double_t wtot = w1+w2;
273
274 t0bin = (w1*t0time + w2*t0bin) / wtot;
275 t0binRes = TMath::Sqrt(1./wtot);
276 }
277 }
278 else{
279 t0bin = t0fill;
280 t0binRes = sigmaFill;
281 if(t0sigma < 1000){
282 t0bin = t0time;
283 t0binRes= t0sigma;
284 }
285 }
286 fPIDesd->GetTOFResponse().SetT0bin(i,t0bin);
287 fPIDesd->GetTOFResponse().SetT0binRes(i,t0binRes);
5b4ed716 288 }
2a258f40 289 }
290 else{
291 for(Int_t i=0;i<fNmomBins;i++){
292 fPIDesd->GetTOFResponse().SetT0bin(i,lT0Current);
293 fPIDesd->GetTOFResponse().SetT0binRes(i,fT0sigma);
5b4ed716 294 }
295 }
536031f2 296
6a4e212e 297 return fCalculated;
536031f2 298}
299//____________________________________________________________________________
5b4ed716 300Double_t *AliTOFT0maker::GetT0p(Float_t p){// [0]=to -- [1] = sigma T0
2a258f40 301 Int_t i=fPIDesd->GetTOFResponse().GetMomBin(p);
03bd764d 302
2a258f40 303 fT0cur[0] = fPIDesd->GetTOFResponse().GetT0bin(i);
304 fT0cur[1] = fPIDesd->GetTOFResponse().GetT0binRes(i);
305
5b4ed716 306 return fT0cur;
536031f2 307}
308//____________________________________________________________________________
5b4ed716 309void AliTOFT0maker::SetTOFResponse(){
2a258f40 310 fPIDesd->GetTOFResponse().SetTimeResolution(fTimeResolution);
5b4ed716 311}
312//____________________________________________________________________________
313Float_t AliTOFT0maker::GetExpectedSigma(Float_t mom, Float_t tof, Float_t mass){
5b4ed716 314 Float_t sigma = fPIDesd->GetTOFResponse().GetExpectedSigma(mom,tof,mass);
8f589502 315
5b4ed716 316 return sigma;
536031f2 317}
318//____________________________________________________________________________
5b4ed716 319void AliTOFT0maker::ApplyT0TOF(AliESDEvent *esd){
8f589502 320 //
5b4ed716 321 // Recalculate TOF PID probabilities
8f589502 322 //
323
90beec49 324// subtruct t0 for each track
325 Int_t ntracks = esd->GetNumberOfTracks();
536031f2 326
90beec49 327 while (ntracks--) {
328 AliESDtrack *t=esd->GetTrack(ntracks);
03bd764d 329
90beec49 330 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
5b4ed716 331
90beec49 332 Double_t time=t->GetTOFsignal();
333 Float_t p = t->GetP();
334
335 Double_t *t0=GetT0p(p);
336 time -= t0[0];
337 t->SetTOFsignal(time);
338 }
339
340 for(Int_t i=0;i<fNmomBins;i++){
341 fPIDesd->GetTOFResponse().SetT0bin(i,0.0);
342 }
343
5b4ed716 344 //
536031f2 345}
03bd764d 346//____________________________________________________________________________
347void AliTOFT0maker::LoadChannelMap(char *filename){
348 // Load the histo with the channel off map
349 TFile *f= new TFile(filename);
350 if(!f){
351 printf("Cannot open the channel map file (%s)\n",filename);
352 return;
353 }
354
355 fHmapChannel = (TH1F *) f->Get("hChEnabled");
356
357 if(!fHmapChannel){
358 printf("Cannot laod the channel map histo (from %s)\n",filename);
359 return;
360 }
361
362}
363//____________________________________________________________________________
364void AliTOFT0maker::ApplyMask(AliESDEvent * const esd){
365 // Switch off the disable channel
366 if(!fHmapChannel){
367 printf("Channel Map is not available\n");
368 return;
369 }
370
371 Int_t ntracks = esd->GetNumberOfTracks();
372
373 while (ntracks--) {
374 AliESDtrack *t=esd->GetTrack(ntracks);
375
376 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
377
378 Int_t chan = t->GetTOFCalChannel();
379
380 if(fHmapChannel->GetBinContent(chan) < 0.01){
381 t->ResetStatus(AliESDtrack::kTOFout);
382 }
383 }
384}
385
5b4ed716 386Float_t
387AliTOFT0maker::TuneForMC(AliESDEvent *esd){ // return true T0 event
388 //
389 // tune for MC data
390 //
391
392 Float_t TOFtimeResolutionDefault=80;
393
394 Float_t t0 = gRandom->Gaus(0.,fT0width);
395
396 Float_t extraSmearing = 0;
397
398 if(fTimeResolution > TOFtimeResolutionDefault){
399 extraSmearing = TMath::Sqrt(fTimeResolution*fTimeResolution - TOFtimeResolutionDefault*TOFtimeResolutionDefault);
400 }
401
402 // subtruct t0 for each track
403 Int_t ntracks = esd->GetNumberOfTracks();
404
405 while (ntracks--) {
406 AliESDtrack *t=esd->GetTrack(ntracks);
407
408 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) continue;
409
410 /* check if channel is enabled */
411 if (fTOFcalib && !fTOFcalib->IsChannelEnabled(t->GetTOFCalChannel())) {
412 /* reset TOF status */
413 t->ResetStatus(AliESDtrack::kTOFin);
414 t->ResetStatus(AliESDtrack::kTOFout);
90beec49 415 t->ResetStatus(AliESDtrack::kTOFmismatch);
5b4ed716 416 t->ResetStatus(AliESDtrack::kTOFpid);
417 }
418
419 Double_t time=t->GetTOFsignal();
420
421 time += t0;
422
423 if(extraSmearing>0){
424 Float_t smearing = gRandom->Gaus(0.,extraSmearing);
425 time += smearing;
426 }
427
428 t->SetTOFsignal(time);
429 }
430 //
431 return t0;
432}
f858b00e 433//_________________________________________________________________________
434void AliTOFT0maker::WriteInESD(AliESDEvent *esd){
435 //
436 // Write t0Gen, t0ResGen, nt0;
437 // t0resESD[0:nt0], it0ESD[0:nt0]
438 // in the AliESDEvent
439 //
440 Int_t nMomBins = fPIDesd->GetTOFResponse().GetNmomBins();
441
442 Int_t nt0=0;
443 Float_t *t0 = new Float_t[nMomBins];
444 Float_t *t0res = new Float_t[nMomBins];
445 Int_t *multT0 = new Int_t[nMomBins];
446
447 for(Int_t i=0;i<nMomBins;i++){
448// printf("START %i) %f %f\n",i,fT0event[i],fT0resolution[i]);
449 multT0[i]=0;
450 Bool_t kNewT0=kTRUE;
451 for(Int_t j=0;j < nt0;j++){
452 if(TMath::Abs(fPIDesd->GetTOFResponse().GetT0bin(i) - t0[j])<0.1){
453 kNewT0=kFALSE;
454 multT0[j]++;
455 j=nMomBins*10;
456 }
457 }
458 if(kNewT0){
459 t0[nt0]=fPIDesd->GetTOFResponse().GetT0bin(i);
460 t0res[nt0]=fPIDesd->GetTOFResponse().GetT0binRes(i);
461 nt0++;
462 }
463 }
464
465 Int_t iMultT0=0,nmult=0;
466 for(Int_t j=0;j < nt0;j++){ // find the most frequent
467 if(multT0[j] > nmult){
468 iMultT0 = j;
469 nmult = multT0[j];
470 }
471 }
472
473 Float_t *t0ESD = new Float_t[nMomBins];
474 Float_t *t0resESD = new Float_t[nMomBins];
475 Int_t *it0ESD = new Int_t[nMomBins];
476
477 Float_t t0Gen,t0ResGen;
478 t0Gen = t0[iMultT0];
479 t0ResGen = t0res[iMultT0];
480 nt0=0;
481 // printf("T0 to ESD\n%f %f\n",t0Gen,t0ResGen);
482 for(Int_t i=0;i<nMomBins;i++){
483 if(TMath::Abs(fPIDesd->GetTOFResponse().GetT0bin(i) - t0Gen)>0.1){
484 t0ESD[nt0]=fPIDesd->GetTOFResponse().GetT0bin(i);
485 t0resESD[nt0]=fPIDesd->GetTOFResponse().GetT0binRes(i);
486 it0ESD[nt0]=i;
487// printf("%i) %f %f %i\n",nt0,t0ESD[nt0],t0resESD[nt0],it0ESD[nt0]);
488 nt0++;
489 }
490 }
491
492 // Write t0Gen,t0ResGen; nt0; t0resESD[0:nt0],it0ESD[0:nt0] in the AliESDEvent
493
494 AliTOFHeader *tofHeader =
495 new AliTOFHeader(t0Gen,t0ResGen,nt0,
c85a1b24 496 t0ESD,t0resESD,it0ESD,fTimeResolution,fT0width);
f858b00e 497
498 esd->SetTOFHeader(tofHeader);
499
c85a1b24 500 AliDebug(1,Form("resTOF=%f T0spread=%f t0Gen=%f t0resGen=%f",fTimeResolution,fT0width,t0Gen,t0ResGen));
501 AliDebug(1,Form("%d ",nt0));
f858b00e 502 for (Int_t ii=0; ii<nt0; ii++)
c85a1b24 503 AliDebug(1,Form("pBin=%d t0val=%f t0res=%f",it0ESD[ii],t0ESD[ii],t0resESD[ii]));
f858b00e 504
505 delete[] t0;
506 t0 = NULL;
507 delete[] t0res;
508 t0res = NULL;
509 delete[] multT0;
510 multT0 = NULL;
511 delete[] t0ESD;
512 t0ESD = NULL;
513 delete[] t0resESD;
514 t0resESD = NULL;
515 delete[] it0ESD;
516 it0ESD = NULL;
517
518}