1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 Revision 1.1 2006/02/13 16:10:48 arcelli
19 Add classes for TOF Calibration (C.Zampolli)
21 author: Chiara Zampolli, zampolli@bo.infn.it
24 ///////////////////////////////////////////////////////////////////////////////
26 // class for TOF calibration //
28 ///////////////////////////////////////////////////////////////////////////////
30 #include "AliTOFcalib.h"
37 #include "AliTOFcalibESD.h"
43 #include "AliESDtrack.h"
44 #include "AliTOFChannel.h"
45 #include "AliTOFChSim.h"
46 #include "AliTOFGeometry.h"
47 #include "AliTOFdigit.h"
48 #include "TClonesArray.h"
49 #include "AliTOFCal.h"
51 #include "AliTOFcluster.h"
53 #include "AliCDBManager.h"
54 #include "AliCDBMetaData.h"
55 #include "AliCDBStorage.h"
57 #include "AliCDBEntry.h"
60 extern TStyle *gStyle;
64 const Int_t AliTOFcalib::fgkchannel = 5000;
65 const Char_t* AliTOFcalib::ffile[6]={"$ALICE_ROOT/TOF/Spectra/spectrum0_1.root","$ALICE_ROOT/TOF/Spectra/spectrum0_2.root","$ALICE_ROOT/TOF/Spectra/spectrum1_1.root","$ALICE_ROOT/TOF/Spectra/spectrum1_2.root","$ALICE_ROOT/TOF/Spectra/spectrum2_1.root","$ALICE_ROOT/TOF/Spectra/spectrum2_2.root"};
66 //_______________________________________________________________________
67 AliTOFcalib::AliTOFcalib():TTask("AliTOFcalib",""){
81 for (Int_t i = 0;i<6;i++){
86 //____________________________________________________________________________
88 AliTOFcalib::AliTOFcalib(char* headerFile, Int_t nEvents):TTask("AliTOFcalib","")
90 AliRunLoader *rl = AliRunLoader::Open("galice.root",AliConfig::GetDefaultEventFolderName(),"read");
92 TFile *in=(TFile*)gFile;
94 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
95 fNSector = fGeom->NSectors();
96 fNPlate = fGeom->NPlates();
97 fNStripA = fGeom->NStripA();
98 fNStripB = fGeom->NStripB();
99 fNStripC = fGeom->NStripC();
100 fNpadZ = fGeom->NpadZ();
101 fNpadX = fGeom->NpadX();
102 fsize = 2*(fNStripC+fNStripB) + fNStripA;
103 for (Int_t i = 0;i<6;i++){
110 fHeadersFile=headerFile;
113 TFile* file = (TFile*) gROOT->GetFile(fHeadersFile.Data()) ;
115 if(fHeadersFile.Contains("rfio"))
116 file = TFile::Open(fHeadersFile,"update") ;
118 file = new TFile(fHeadersFile.Data(),"update") ;
119 gAlice = (AliRun*)file->Get("gAlice") ;
122 TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ;
123 roottasks->Add(this) ;
125 //____________________________________________________________________________
127 AliTOFcalib::AliTOFcalib(const AliTOFcalib & calib):TTask("AliTOFcalib","")
129 fNSector = calib.fNSector;
130 fNPlate = calib.fNPlate;
131 fNStripA = calib.fNStripA;
132 fNStripB = calib.fNStripB;
133 fNStripC = calib.fNStripC;
134 fNpadZ = calib.fNpadZ;
135 fNpadX = calib.fNpadX;
137 fArrayToT = calib.fArrayToT;
138 fArrayTime = calib.fArrayTime;
139 flistFunc = calib.flistFunc;
140 for (Int_t i = 0;i<6;i++){
141 fhToT[i]=calib.fhToT[i];
143 fTOFCal=calib.fTOFCal;
144 fESDsel = calib.fESDsel;
148 //____________________________________________________________________________
150 AliTOFcalib::~AliTOFcalib()
159 //____________________________________________________________________________
161 void AliTOFcalib::Init(){
164 fTOFCal = new AliTOFCal();
165 fTOFCal->CreateArray();
174 //fsize = fNSector*(2*(fNStripC+fNStripB)+fNStripA())*fNpadZ*fNpadX; //generalized version
176 //____________________________________________________________________________
178 void AliTOFcalib::SetHistos(){
181 for (Int_t i =0;i<6;i++){
182 //for the time being, only one spectrum is used
183 spFile = new TFile("$ALICE_ROOT/TOF/Spectra/spectrumtest0_1.root","read");
185 //spFile = new TFile(ffile[i],"read");
186 spFile->GetObject("ToT",hToT);
188 Int_t nbins = hToT->GetNbinsX();
189 Float_t Delta = hToT->GetBinWidth(1);
190 Float_t maxch = hToT->GetBinLowEdge(nbins)+Delta;
191 Float_t minch = hToT->GetBinLowEdge(1);
192 Float_t max=0,min=0; //maximum and minimum value of the distribution
193 Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
194 for (Int_t ii=nbins; ii>0; ii--){
195 if (hToT->GetBinContent(ii)!= 0) {
196 max = maxch - (nbins-ii-1)*Delta;
200 for (Int_t j=1; j<nbins; j++){
201 if (hToT->GetBinContent(j)!= 0) {
202 min = minch + (j-1)*Delta;
208 fMaxToTDistr[i]=hToT->GetMaximum();
211 //__________________________________________________________________________
213 void AliTOFcalib::SetFitFunctions(){
215 flistFunc = new TList();
216 for (Int_t i =0;i<6;i++){
217 //only one type of spectrum used for the time being
218 spFile = new TFile("$ALICE_ROOT/TOF/Spectra/spectrumtest0_1.root","read");
220 //spFile = new TFile(ffile[i],"read");
221 TH1F * h = (TH1F*)spFile->Get("TimeToTFit");
222 TList * list = (TList*)h->GetListOfFunctions();
223 TF1* f = (TF1*)list->At(0);
224 Double_t par[6] = {0,0,0,0,0,0};
225 Int_t npar=f->GetNpar();
226 for (Int_t ipar=0;ipar<npar;ipar++){
227 par[ipar]=f->GetParameter(ipar);
229 flistFunc->AddLast(f);
233 //__________________________________________________________________________
235 TF1* AliTOFcalib::SetFitFunctions(TH1F *histo){
237 const Int_t nbins = histo->GetNbinsX();
238 Float_t Delta = histo->GetBinWidth(1); //all the bins have the same width
239 Double_t max = histo->GetBinLowEdge(nbins)+Delta;
241 fpol[0]=new TF1("poly3","pol3",5,max);
242 fpol[1]=new TF1("poly4","pol4",5,max);
243 fpol[2]=new TF1("poly5","pol5",5,max);
245 Double_t chi[3]={1E6,1E6,1E6};
246 Int_t ndf[3]={-1,-1,-1};
247 Double_t Nchi[3]={1E6,1E6,1E6};
248 Double_t bestchi=1E6;
251 Int_t numberOfpar =0;
252 for (Int_t j=0; j<nbins; j++){
253 if (histo->GetBinContent(j)!=0) {
259 AliError(" Too few points in the histo. No fit performed.");
262 else if (nonzero<=5) {
264 AliInfo(" Only 3rd order polynomial fit possible.");
266 else if (nonzero<=6) {
268 AliInfo(" Only 3rd and 4th order polynomial fit possible.");
272 AliInfo(" All 3rd, 4th and 5th order polynomial fit possible.");
274 for (Int_t ifun=norderfit-3;ifun<norderfit-2;ifun++){
275 sprintf(npoly,"poly%i",ifun+3);
276 histo->Fit(npoly, "ERN", " ", 5.,14.);
277 chi[ifun] = fpol[ifun]->GetChisquare();
278 ndf[ifun] = fpol[ifun]->GetNDF();
279 Nchi[ifun] = (Double_t)chi[ifun]/ndf[ifun];
280 if (Nchi[ifun]<bestchi) {
283 numberOfpar = fGold->GetNpar();
286 fGold=fpol[2]; //Gold fit function set to pol5 in any case
287 histo->Fit(fGold,"ER " ," ",5.,15.);
290 //____________________________________________________________________________
292 TClonesArray* AliTOFcalib::DecalibrateDigits(TClonesArray * digits){
294 TObjArray ChArray(fsize);
296 for (Int_t kk = 0 ; kk < fsize; kk++){
297 AliTOFChSim * channel = new AliTOFChSim();
298 ChArray.Add(channel);
300 Int_t ndigits = digits->GetEntriesFast();
301 for (Int_t i=0;i<ndigits;i++){
305 AliTOFdigit * element = (AliTOFdigit*)digits->At(i);
308 detId[0] = element->GetSector();
309 detId[1] = element->GetPlate();
310 detId[2] = element->GetStrip();
311 detId[3] = element->GetPadz();
312 detId[4] = element->GetPadx();
313 Int_t index = GetIndex(detId);
315 //select the corresponding channel with its simulated ToT spectrum
316 //summing up everything, index = 0 for all channels:
318 AliTOFChSim *ch = (AliTOFChSim*)ChArray.At(index);
320 if (!ch->IsSlewed()){
321 Double_t rand = gRandom->Uniform(0,6);
322 itype = (Int_t)(6-rand);
323 ch->SetSpectrum(itype);
324 ch->SetSlewedStatus(kTRUE);
326 else itype = ch->GetSpectrum();
327 TH1F * hToT = fhToT[itype];
328 TF1 * f = (TF1*)flistFunc->At(itype);
329 while (SimToT <= triy){
330 trix = gRandom->Rndm(i);
331 triy = gRandom->Rndm(i);
332 trix = (fMaxToT[itype]-fMinToT[itype])*trix + fMinToT[itype];
333 triy = fMaxToTDistr[itype]*triy;
334 Int_t binx=hToT->FindBin(trix);
335 SimToT=hToT->GetBinContent(binx);
339 for(Int_t kk=0;kk<6;kk++){
342 element->SetToT(trix);
343 Int_t nfpar = f->GetNpar();
344 for (Int_t kk = 0; kk< nfpar; kk++){
345 par[kk]=f->GetParameter(kk);
347 Float_t ToT = element->GetToT();
348 element->SetTdcND(element->GetTdc());
349 Float_t tdc = ((element->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
350 Float_t timeoffset=par[0] + ToT*par[1] +ToT*ToT*par[2] +ToT*ToT*ToT*par[3] +ToT*ToT*ToT*ToT*par[4] +ToT*ToT*ToT*ToT*ToT*par[5];
352 Float_t timeSlewed = tdc + timeoffset;
353 element->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());
355 TFile * file = new TFile("ToT.root", "recreate");
357 ChArray.Write("ToT",TObject::kSingleKey);
363 //____________________________________________________________________________
365 void AliTOFcalib::SelectESD(AliESD *event, AliRunLoader *rl)
367 AliLoader *tofl = rl->GetLoader("TOFLoader");
369 AliError("Can not get the TOF Loader");
372 tofl->LoadRecPoints("read");
373 TClonesArray *fClusters =new TClonesArray("AliTOFcluster", 1000);
374 TTree *rTree=tofl->TreeR();
376 cerr<<"Can't get the Rec Points tree !\n";
379 TBranch *branch=rTree->GetBranch("TOF");
381 cerr<<"Cant' get the branch !\n";
384 branch->SetAddress(&fClusters);
386 Float_t LowerMomBound=0.8; // [GeV/c] default value Pb-Pb
387 Float_t UpperMomBound=1.8 ; // [GeV/c] default value Pb-Pb
389 Int_t ngoodtrkfinalToT = 0;
390 ntrk=event->GetNumberOfTracks();
391 fESDsel = new TObjArray(ntrk);
393 TObjArray UCdatatemp(ntrk);
395 Int_t ngoodtrkfinal = 0;
396 Float_t mintime =1E6;
397 for (Int_t itrk=0; itrk<ntrk; itrk++) {
398 AliESDtrack *t=event->GetTrack(itrk);
399 //track selection: reconstrution to TOF:
400 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) {
403 //IsStartedTimeIntegral
404 if ((t->GetStatus()&AliESDtrack::kTIME)==0) {
407 Double_t time=t->GetTOFsignal();
408 time*=1.E-3; // tof given in nanoseconds
409 if(time <= mintime)mintime=time;
410 Double_t mom=t->GetP();
411 if (!(mom<=UpperMomBound && mom>=LowerMomBound))continue;
412 UInt_t AssignedTOFcluster=t->GetTOFcluster();//index of the assigned TOF cluster, >0 ?
413 if(AssignedTOFcluster==0){ // not matched
416 AliTOFcluster * tofcl = (AliTOFcluster *)fClusters->UncheckedAt(AssignedTOFcluster);
417 Int_t isector = tofcl->GetDetInd(0);
418 Int_t iplate = tofcl->GetDetInd(1);
419 Int_t istrip = tofcl->GetDetInd(2);
420 Int_t ipadz = tofcl->GetDetInd(3);
421 Int_t ipadx = tofcl->GetDetInd(4);
422 Float_t ToT = tofcl->GetToT();
423 AliTOFcalibESD *unc = new AliTOFcalibESD;
424 unc->CopyFromAliESD(t);
425 unc->SetSector(isector);
426 unc->SetPlate(iplate);
427 unc->SetStrip(istrip);
432 unc->GetExternalCovariance(c1);
436 for (Int_t i = 0; i < ngoodtrk ; i ++){
437 AliTOFcalibESD *unc = (AliTOFcalibESD*)UCdatatemp.At(i);
438 if((unc->GetTOFsignal()-mintime*1.E3)<5.E3){
446 //_____________________________________________________________________________
448 void AliTOFcalib::CombESDId(){
451 Int_t ntracksinset=6;
452 Float_t exptof[6][3];
453 Float_t momentum[6]={0.,0.,0.,0.,0.,0.};
454 Int_t assparticle[6]={3,3,3,3,3,3};
455 Float_t massarray[3]={0.13957,0.493677,0.9382723};
456 Float_t timeofflight[6]={0.,0.,0.,0.,0.,0.};
457 Float_t beta[6]={0.,0.,0.,0.,0.,0.};
458 Float_t texp[6]={0.,0.,0.,0.,0.,0.};
459 Float_t sqMomError[6]={0.,0.,0.,0.,0.,0.};
460 Float_t sqTrackError[6]={0.,0.,0.,0.,0.,0.};
461 Float_t tracktoflen[6]={0.,0.,0.,0.,0.,0.};
462 Float_t TimeResolution = 0.90e-10; // 90 ps by default
463 Float_t timeresolutioninns=TimeResolution*(1.e+9); // convert in [ns]
464 Float_t timezero[6]={0.,0.,0.,0.,0.,0.};
465 Float_t weightedtimezero[6]={0.,0.,0.,0.,0.,0.};
466 Float_t besttimezero[6]={0.,0.,0.,0.,0.,0.};
467 Float_t bestchisquare[6]={0.,0.,0.,0.,0.,0.};
468 Float_t bestweightedtimezero[6]={0.,0.,0.,0.,0.,0.};
469 Float_t bestsqTrackError[6]={0.,0.,0.,0.,0.,0.};
471 Int_t nelements = fESDsel->GetEntries();
472 Int_t nset= (Int_t)(nelements/ntracksinset);
473 for (Int_t i=0; i< nset; i++) {
475 AliTOFcalibESD **unc=new AliTOFcalibESD*[ntracksinset];
476 for (Int_t itrk=0; itrk<ntracksinset; itrk++) {
477 Int_t index = itrk+i*ntracksinset;
478 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(index);
482 for (Int_t j=0; j<ntracksinset; j++) {
483 AliTOFcalibESD *element=unc[j];
484 Double_t mom=element->GetP();
485 Double_t time=element->GetTOFsignal()*1.E-3; // in ns
486 Double_t exptime[10];
487 element->GetIntegratedTimes(exptime);
488 Double_t toflen=element->GetIntegratedLength()/100.; // in m
489 timeofflight[j]=time+t0offset;
490 tracktoflen[j]=toflen+loffset;
491 exptof[j][0]=exptime[2]*1.E-3+0.005;
492 exptof[j][1]=exptime[3]*1.E-3+0.005;
493 exptof[j][2]=exptime[4]*1.E-3+0.005;
497 Float_t Et0best=999.;
498 Float_t chisquarebest=999.;
499 for (Int_t i1=0; i1<3;i1++) {
500 beta[0]=momentum[0]/sqrt(massarray[i1]*massarray[i1]+momentum[0]*momentum[0]);
501 texp[0]=exptof[0][i1];
502 for (Int_t i2=0; i2<3;i2++) {
503 beta[1]=momentum[1]/sqrt(massarray[i2]*massarray[i2]+momentum[1]*momentum[1]);
504 texp[1]=exptof[1][i2];
505 for (Int_t i3=0; i3<3;i3++) {
506 beta[2]=momentum[2]/sqrt(massarray[i3]*massarray[i3]+momentum[2]*momentum[2]);
507 texp[2]=exptof[2][i3];
508 for (Int_t i4=0; i4<3;i4++) {
509 beta[3]=momentum[3]/sqrt(massarray[i4]*massarray[i4]+momentum[3]*momentum[3]);
510 texp[3]=exptof[3][i4];
512 for (Int_t i5=0; i5<3;i5++) {
513 beta[4]=momentum[4]/sqrt(massarray[i5]*massarray[i5]+momentum[4]*momentum[4]);
514 texp[4]=exptof[4][i5];
515 for (Int_t i6=0; i6<3;i6++) {
516 beta[5]=momentum[5]/sqrt(massarray[i6]*massarray[i6]+momentum[5]*momentum[5]);
517 texp[5]=exptof[5][i6];
519 Float_t sumAllweights=0.;
520 Float_t meantzero=0.;
521 Float_t Emeantzero=0.;
523 for (Int_t itz=0; itz<ntracksinset;itz++) {
525 ((1.-beta[itz]*beta[itz])*0.025)*
526 ((1.-beta[itz]*beta[itz])*0.025)*
528 (0.299792*beta[itz]))*
530 (0.299792*beta[itz]));
536 timezero[itz]=texp[itz]-timeofflight[itz];
537 weightedtimezero[itz]=timezero[itz]/sqTrackError[itz];
538 sumAllweights+=1./sqTrackError[itz];
539 meantzero+=weightedtimezero[itz];
541 } // end loop for (Int_t itz=0; itz<15;itz++)
543 meantzero=meantzero/sumAllweights; // it is given in [ns]
544 Emeantzero=sqrt(1./sumAllweights); // it is given in [ns]
546 // calculate chisquare
548 Float_t chisquare=0.;
549 for (Int_t icsq=0; icsq<ntracksinset;icsq++) {
550 chisquare+=(timezero[icsq]-meantzero)*(timezero[icsq]-meantzero)/sqTrackError[icsq];
551 } // end loop for (Int_t icsq=0; icsq<15;icsq++)
552 // cout << " chisquare " << chisquare << endl;
562 if(chisquare<=chisquarebest && ((Float_t) npion/ ((Float_t) ntracksinset)>0.3)){
563 // if(chisquare<=chisquarebest){
565 for(Int_t iqsq = 0; iqsq<ntracksinset; iqsq++) {
566 bestsqTrackError[iqsq]=sqTrackError[iqsq];
567 besttimezero[iqsq]=timezero[iqsq];
568 bestweightedtimezero[iqsq]=weightedtimezero[iqsq];
569 bestchisquare[iqsq]=(timezero[iqsq]-meantzero)*(timezero[iqsq]-meantzero)/sqTrackError[iqsq];
579 chisquarebest=chisquare;
582 } // close if(dummychisquare<=chisquare)
591 Float_t confLevel=999;
592 if(chisquarebest<999.){
593 Double_t dblechisquare=(Double_t)chisquarebest;
594 confLevel=(Float_t)TMath::Prob(dblechisquare,ntracksinset-1);
596 // assume they are all pions for fake sets
597 if(confLevel<0.01 || confLevel==999. ){
598 for (Int_t itrk=0; itrk<ntracksinset; itrk++)assparticle[itrk]=0;
600 for (Int_t itrk=0; itrk<ntracksinset; itrk++) {
601 Int_t index = itrk+i*ntracksinset;
602 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(index);
603 element->SetCombID(assparticle[itrk]);
608 //_____________________________________________________________________________
610 void AliTOFcalib::CalibrateESD(){
611 Int_t nelements = fESDsel->GetEntries();
612 Int_t *number=new Int_t[fsize];
613 fArrayToT = new AliTOFArray(fsize);
614 fArrayTime = new AliTOFArray(fsize);
615 for (Int_t i=0; i<fsize; i++){
617 fArrayToT->AddArray(i, new TArrayF(fgkchannel));
618 TArrayF * parrToT = fArrayToT->GetArray(i);
619 TArrayF & refaToT = * parrToT;
620 fArrayTime->AddArray(i, new TArrayF(fgkchannel));
621 TArrayF * parrTime = fArrayToT->GetArray(i);
622 TArrayF & refaTime = * parrTime;
623 for (Int_t j = 0;j<AliTOFcalib::fgkchannel;j++){
624 refaToT[j]=0.; //ToT[i][j]=j;
625 refaTime[j]=0.; //Time[i][j]=j;
629 for (Int_t i=0; i< nelements; i++) {
630 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(i);
631 Int_t ipid = element->GetCombID();
632 Double_t etime = 0; //expected time
633 Double_t expTime[10];
634 element->GetIntegratedTimes(expTime);
635 if (ipid == 0) etime = expTime[2]*1E-3; //ns
636 else if (ipid == 1) etime = expTime[3]*1E-3; //ns
637 else if (ipid == 2) etime = expTime[4]*1E-3; //ns
638 else AliError("No pid from combinatorial algo for this track");
639 Double_t mtime = (Double_t)element->GetTOFsignal()*1E-3; //measured time
640 Double_t mToT = (Double_t) element->GetToT(); //measured ToT, ns
643 detId[0] = element->GetSector();
644 detId[1] = element->GetPlate();
645 detId[2] = element->GetStrip();
646 detId[3] = element->GetPadz();
647 detId[4] = element->GetPadx();
648 Int_t index = GetIndex(detId);
650 //select the correspondent channel with its simulated ToT spectrum
651 //summing up everything, index = 0 for all channels:
653 Int_t index2 = number[index];
654 TArrayF * parrToT = fArrayToT->GetArray(index);
655 TArrayF & refaToT = * parrToT;
656 refaToT[index2] = (Float_t)mToT;
657 TArrayF * parrTime = fArrayTime->GetArray(index);
658 TArrayF & refaTime = * parrTime;
659 refaTime[index2] = (Float_t)(mtime-etime);
663 for (Int_t i=0;i<1;i++){
664 TH1F * hProf = Profile(i);
665 TF1* fGold = SetFitFunctions(hProf);
666 Int_t nfpar = fGold->GetNpar();
668 for(Int_t kk=0;kk<6;kk++){
671 for (Int_t kk = 0; kk< nfpar; kk++){
672 par[kk]=fGold->GetParameter(kk);
674 AliTOFChannel * CalChannel = fTOFCal->GetChannel(i);
675 CalChannel->SetSlewPar(par);
680 //___________________________________________________________________________
682 TH1F* AliTOFcalib::Profile(Int_t ich){
683 const Int_t nbinToT = 650;
684 Int_t nbinTime = 400;
685 Float_t minTime = -10.5; //ns
686 Float_t maxTime = 10.5; //ns
687 Float_t minToT = 7.5; //ns
688 Float_t maxToT = 40.; //ns
689 Float_t DeltaToT = (maxToT-minToT)/nbinToT;
690 Double_t mTime[nbinToT+1],mToT[nbinToT+1],meanTime[nbinToT+1], meanTime2[nbinToT+1],ToT[nbinToT+1], ToT2[nbinToT+1],meanToT[nbinToT+1],meanToT2[nbinToT+1],Time[nbinToT+1],Time2[nbinToT+1],xlow[nbinToT+1],sigmaTime[nbinToT+1];
691 Int_t n[nbinToT+1], nentrx[nbinToT+1];
692 Double_t sigmaToT[nbinToT+1];
693 for (Int_t i = 0; i < nbinToT+1 ; i++){
711 TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", nbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
712 TArrayF * parrToT = fArrayToT->GetArray(ich);
713 TArrayF & refaToT = * parrToT;
714 TArrayF * parrTime = fArrayTime->GetArray(ich);
715 TArrayF & refaTime = * parrTime;
716 for (Int_t j = 0; j < AliTOFcalib::fgkchannel; j++){
717 if (refaToT[j] == 0) continue;
718 Int_t nx = (Int_t)((refaToT[j]-minToT)/DeltaToT)+1;
719 if ((refaToT[j] != 0) && (refaTime[j] != 0)){
720 Time[nx]+=refaTime[j];
721 Time2[nx]+=(refaTime[j])*(refaTime[j]);
723 ToT2[nx]+=refaToT[j]*refaToT[j];
725 hSlewing->Fill(refaToT[j],refaTime[j]);
728 Int_t nbinsToT=hSlewing->GetNbinsX();
729 if (nbinsToT != nbinToT) {
730 AliError("Profile :: incompatible numbers of bins");
735 TH1F *histo = new TH1F("histo", "1D Time vs ToT", nbinsToT, minToT, maxToT);
736 for (Int_t i=1;i<=nbinsToT;i++){
738 n[usefulBins]+=nentrx[i];
739 if (n[usefulBins]==0 && i == nbinsToT) {
742 meanTime[usefulBins]+=Time[i];
743 meanTime2[usefulBins]+=Time2[i];
744 meanToT[usefulBins]+=ToT[i];
745 meanToT2[usefulBins]+=ToT2[i];
746 if (n[usefulBins]<20 && i!=nbinsToT) continue;
747 mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
748 mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
749 sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
750 *(meanTime2[usefulBins]-meanTime[usefulBins]
751 *meanTime[usefulBins]/n[usefulBins]));
752 if ((1./n[usefulBins]/n[usefulBins]
753 *(meanToT2[usefulBins]-meanToT[usefulBins]
754 *meanToT[usefulBins]/n[usefulBins]))< 0) {
755 AliError(" too small radical" );
756 sigmaToT[usefulBins]=0;
759 sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
760 *(meanToT2[usefulBins]-meanToT[usefulBins]
761 *meanToT[usefulBins]/n[usefulBins]));
766 for (Int_t i=0;i<usefulBins;i++){
767 Int_t binN = (Int_t)((mToT[i]-minToT)/DeltaToT)+1;
768 histo->Fill(mToT[i],mTime[i]);
769 histo->SetBinError(binN,sigmaTime[i]);
773 //_____________________________________________________________________________
775 void AliTOFcalib::CorrectESDTime(){
776 Int_t nelements = fESDsel->GetEntries();
777 for (Int_t i=0; i< nelements; i++) {
778 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(i);
781 detId[0] = element->GetSector();
782 detId[1] = element->GetPlate();
783 detId[2] = element->GetStrip();
784 detId[3] = element->GetPadz();
785 detId[4] = element->GetPadx();
786 Int_t index = GetIndex(detId);
788 //select the correspondent channel with its simulated ToT spectrum
789 //summing up everything, index = 0 for all channels:
791 Int_t ipid = element->GetCombID();
792 Double_t etime = 0; //expected time
793 Double_t expTime[10];
794 element->GetIntegratedTimes(expTime);
795 if (ipid == 0) etime = expTime[2]*1E-3; //ns
796 else if (ipid == 1) etime = expTime[3]*1E-3; //ns
797 else if (ipid == 2) etime = expTime[4]*1E-3; //ns
799 AliTOFChannel * CalChannel = fTOFCal->GetChannel(index);
800 for (Int_t j = 0; j<6; j++){
801 par[j]=CalChannel->GetSlewPar(j);
803 //Float_t TimeCorr=par[0]+par[1]*ToT+par[2]*ToT*ToT+par[3]*ToT*ToT*ToT+par[4]*ToT*ToT*ToT*ToT+par[5]*ToT*ToT*ToT*ToT*ToT;
806 //_____________________________________________________________________________
808 void AliTOFcalib::CorrectESDTime(AliESD *event, AliRunLoader *rl ){
809 AliLoader *tofl = rl->GetLoader("TOFLoader");
811 AliError("Can not get the TOF Loader");
814 tofl->LoadRecPoints("read");
815 TClonesArray *fClusters =new TClonesArray("AliTOFcluster", 1000);
816 TTree *rTree=tofl->TreeR();
818 cerr<<"Can't get the Rec Points tree !\n";
821 TBranch *branch=rTree->GetBranch("TOF");
823 cerr<<"Cant' get the branch !\n";
826 branch->SetAddress(&fClusters);
829 ntrk=event->GetNumberOfTracks();
830 for (Int_t itrk=0; itrk<ntrk; itrk++) {
831 AliESDtrack *t=event->GetTrack(itrk);
832 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) {
835 //IsStartedTimeIntegral
836 if ((t->GetStatus()&AliESDtrack::kTIME)==0) {
839 UInt_t AssignedTOFcluster=t->GetTOFcluster();//index of the assigned TOF cluster, >0 ?
840 if(AssignedTOFcluster==0){ // not matched
843 AliTOFcluster * tofcl = (AliTOFcluster *)fClusters->UncheckedAt(AssignedTOFcluster);
846 detId[0] = tofcl->GetSector();
847 detId[1] = tofcl->GetPlate();
848 detId[2] = tofcl->GetStrip();
849 detId[3] = tofcl->GetPadz();
850 detId[4] = tofcl->GetPadx();
851 Int_t index = GetIndex(detId);
853 //select the correspondent channel with its simulated ToT spectrum
854 //summing up everything, index = 0 for all channels:
856 AliTOFChannel * CalChannel = fTOFCal->GetChannel(index);
858 for (Int_t j = 0; j<6; j++){
859 par[j]=CalChannel->GetSlewPar(j);
861 Float_t ToT = tofcl->GetToT();
863 TimeCorr=par[0]+par[1]*ToT+par[2]*ToT*ToT+par[3]*ToT*ToT*ToT+par[4]*ToT*ToT*ToT*ToT+par[5]*ToT*ToT*ToT*ToT*ToT;
866 //_____________________________________________________________________________
868 void AliTOFcalib::WriteOnCDB(){
869 AliCDBManager *man = AliCDBManager::Instance();
870 AliCDBId id("TOF/Calib/Constants",1,100);
871 AliCDBMetaData *md = new AliCDBMetaData();
872 md->SetResponsible("Shimmize");
873 man->Put(fTOFCal,id,md);
875 //_____________________________________________________________________________
877 void AliTOFcalib::ReadFromCDB(Char_t *sel, Int_t nrun){
878 AliCDBManager *man = AliCDBManager::Instance();
879 AliCDBEntry *entry = man->Get(sel,nrun);
881 AliError("Retrivial failed");
882 AliCDBStorage *origSto =man->GetDefaultStorage();
883 man->SetDefaultStorage("local://$ALICE_ROOT");
884 entry = man->Get("TOF/Calib/Constants",nrun);
885 man->SetDefaultStorage(origSto);
887 AliTOFCal *cal =(AliTOFCal*)entry->GetObject();
890 //_____________________________________________________________________________
892 Int_t AliTOFcalib::GetIndex(Int_t *detId){
893 Int_t isector = detId[0];
894 if (isector >= fNSector)
895 AliError(Form("Wrong sector number in TOF (%d) !",isector));
896 Int_t iplate = detId[1];
897 if (iplate >= fNPlate)
898 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
899 Int_t istrip = detId[2];
900 Int_t ipadz = detId[3];
901 Int_t ipadx = detId[4];
902 Int_t stripOffset = 0;
908 stripOffset = fNStripC;
911 stripOffset = fNStripC+fNStripB;
914 stripOffset = fNStripC+fNStripB+fNStripA;
917 stripOffset = fNStripC+fNStripB+fNStripA+fNStripB;
920 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
924 Int_t idet = ((2*(fNStripC+fNStripB)+fNStripA)*fNpadZ*fNpadX)*isector +
925 (stripOffset*fNpadZ*fNpadX)+
926 (fNpadZ*fNpadX)*istrip+