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 **************************************************************************/
17 author: Chiara Zampolli, zampolli@bo.infn.it
20 ///////////////////////////////////////////////////////////////////////////////
22 // class for TOF calibration //
24 ///////////////////////////////////////////////////////////////////////////////
26 #include "AliTOFcalib.h"
33 #include "AliTOFcalibESD.h"
39 #include "AliESDtrack.h"
40 #include "AliTOFChannel.h"
41 #include "AliTOFChSim.h"
42 #include "AliTOFGeometry.h"
43 #include "AliTOFdigit.h"
44 #include "TClonesArray.h"
45 #include "AliTOFCal.h"
47 #include "AliTOFcluster.h"
49 #include "AliCDBManager.h"
50 #include "AliCDBMetaData.h"
51 #include "AliCDBStorage.h"
53 #include "AliCDBEntry.h"
56 extern TStyle *gStyle;
60 const Int_t AliTOFcalib::fgkchannel = 5000;
61 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"};
62 //_______________________________________________________________________
63 AliTOFcalib::AliTOFcalib():TTask("AliTOFcalib",""){
77 for (Int_t i = 0;i<6;i++){
82 //____________________________________________________________________________
84 AliTOFcalib::AliTOFcalib(char* headerFile, Int_t nEvents):TTask("AliTOFcalib","")
86 AliRunLoader *rl = AliRunLoader::Open("galice.root",AliConfig::GetDefaultEventFolderName(),"read");
88 TFile *in=(TFile*)gFile;
90 fGeom = (AliTOFGeometry*)in->Get("TOFgeometry");
91 fNSector = fGeom->NSectors();
92 fNPlate = fGeom->NPlates();
93 fNStripA = fGeom->NStripA();
94 fNStripB = fGeom->NStripB();
95 fNStripC = fGeom->NStripC();
96 fNpadZ = fGeom->NpadZ();
97 fNpadX = fGeom->NpadX();
98 fsize = 2*(fNStripC+fNStripB) + fNStripA;
99 for (Int_t i = 0;i<6;i++){
106 fHeadersFile=headerFile;
109 TFile* file = (TFile*) gROOT->GetFile(fHeadersFile.Data()) ;
111 if(fHeadersFile.Contains("rfio"))
112 file = TFile::Open(fHeadersFile,"update") ;
114 file = new TFile(fHeadersFile.Data(),"update") ;
115 gAlice = (AliRun*)file->Get("gAlice") ;
118 TTask * roottasks = (TTask*)gROOT->GetRootFolder()->FindObject("Tasks") ;
119 roottasks->Add(this) ;
121 //____________________________________________________________________________
123 AliTOFcalib::AliTOFcalib(const AliTOFcalib & calib):TTask("AliTOFcalib","")
125 fNSector = calib.fNSector;
126 fNPlate = calib.fNPlate;
127 fNStripA = calib.fNStripA;
128 fNStripB = calib.fNStripB;
129 fNStripC = calib.fNStripC;
130 fNpadZ = calib.fNpadZ;
131 fNpadX = calib.fNpadX;
133 fArrayToT = calib.fArrayToT;
134 fArrayTime = calib.fArrayTime;
135 flistFunc = calib.flistFunc;
136 for (Int_t i = 0;i<6;i++){
137 fhToT[i]=calib.fhToT[i];
139 fTOFCal=calib.fTOFCal;
140 fESDsel = calib.fESDsel;
144 //____________________________________________________________________________
146 AliTOFcalib::~AliTOFcalib()
155 //____________________________________________________________________________
157 void AliTOFcalib::Init(){
160 fTOFCal = new AliTOFCal();
161 fTOFCal->CreateArray();
170 //fsize = fNSector*(2*(fNStripC+fNStripB)+fNStripA())*fNpadZ*fNpadX; //generalized version
172 //____________________________________________________________________________
174 void AliTOFcalib::SetHistos(){
177 for (Int_t i =0;i<6;i++){
178 //for the time being, only one spectrum is used
179 spFile = new TFile("$ALICE_ROOT/TOF/Spectra/spectrumtest0_1.root","read");
181 //spFile = new TFile(ffile[i],"read");
182 spFile->GetObject("ToT",hToT);
184 Int_t nbins = hToT->GetNbinsX();
185 Float_t Delta = hToT->GetBinWidth(1);
186 Float_t maxch = hToT->GetBinLowEdge(nbins)+Delta;
187 Float_t minch = hToT->GetBinLowEdge(1);
188 Float_t max=0,min=0; //maximum and minimum value of the distribution
189 Int_t maxbin=0,minbin=0; //maximum and minimum bin of the distribution
190 for (Int_t ii=nbins; ii>0; ii--){
191 if (hToT->GetBinContent(ii)!= 0) {
192 max = maxch - (nbins-ii-1)*Delta;
196 for (Int_t j=1; j<nbins; j++){
197 if (hToT->GetBinContent(j)!= 0) {
198 min = minch + (j-1)*Delta;
204 fMaxToTDistr[i]=hToT->GetMaximum();
207 //__________________________________________________________________________
209 void AliTOFcalib::SetFitFunctions(){
211 flistFunc = new TList();
212 for (Int_t i =0;i<6;i++){
213 //only one type of spectrum used for the time being
214 spFile = new TFile("$ALICE_ROOT/TOF/Spectra/spectrumtest0_1.root","read");
216 //spFile = new TFile(ffile[i],"read");
217 TH1F * h = (TH1F*)spFile->Get("TimeToTFit");
218 TList * list = (TList*)h->GetListOfFunctions();
219 TF1* f = (TF1*)list->At(0);
220 Double_t par[6] = {0,0,0,0,0,0};
221 Int_t npar=f->GetNpar();
222 for (Int_t ipar=0;ipar<npar;ipar++){
223 par[ipar]=f->GetParameter(ipar);
225 flistFunc->AddLast(f);
229 //__________________________________________________________________________
231 TF1* AliTOFcalib::SetFitFunctions(TH1F *histo){
233 const Int_t nbins = histo->GetNbinsX();
234 Float_t Delta = histo->GetBinWidth(1); //all the bins have the same width
235 Double_t max = histo->GetBinLowEdge(nbins)+Delta;
237 fpol[0]=new TF1("poly3","pol3",5,max);
238 fpol[1]=new TF1("poly4","pol4",5,max);
239 fpol[2]=new TF1("poly5","pol5",5,max);
241 Double_t chi[3]={1E6,1E6,1E6};
242 Int_t ndf[3]={-1,-1,-1};
243 Double_t Nchi[3]={1E6,1E6,1E6};
244 Double_t bestchi=1E6;
247 Int_t numberOfpar =0;
248 for (Int_t j=0; j<nbins; j++){
249 if (histo->GetBinContent(j)!=0) {
255 AliError(" Too few points in the histo. No fit performed.");
258 else if (nonzero<=5) {
260 AliInfo(" Only 3rd order polynomial fit possible.");
262 else if (nonzero<=6) {
264 AliInfo(" Only 3rd and 4th order polynomial fit possible.");
268 AliInfo(" All 3rd, 4th and 5th order polynomial fit possible.");
270 for (Int_t ifun=norderfit-3;ifun<norderfit-2;ifun++){
271 sprintf(npoly,"poly%i",ifun+3);
272 histo->Fit(npoly, "ERN", " ", 5.,14.);
273 chi[ifun] = fpol[ifun]->GetChisquare();
274 ndf[ifun] = fpol[ifun]->GetNDF();
275 Nchi[ifun] = (Double_t)chi[ifun]/ndf[ifun];
276 if (Nchi[ifun]<bestchi) {
279 numberOfpar = fGold->GetNpar();
282 fGold=fpol[2]; //Gold fit function set to pol5 in any case
283 histo->Fit(fGold,"ER " ," ",5.,15.);
286 //____________________________________________________________________________
288 TClonesArray* AliTOFcalib::DecalibrateDigits(TClonesArray * digits){
290 TObjArray ChArray(fsize);
292 for (Int_t kk = 0 ; kk < fsize; kk++){
293 AliTOFChSim * channel = new AliTOFChSim();
294 ChArray.Add(channel);
296 Int_t ndigits = digits->GetEntriesFast();
297 for (Int_t i=0;i<ndigits;i++){
301 AliTOFdigit * element = (AliTOFdigit*)digits->At(i);
304 detId[0] = element->GetSector();
305 detId[1] = element->GetPlate();
306 detId[2] = element->GetStrip();
307 detId[3] = element->GetPadz();
308 detId[4] = element->GetPadx();
309 Int_t index = GetIndex(detId);
311 //select the corresponding channel with its simulated ToT spectrum
312 //summing up everything, index = 0 for all channels:
314 AliTOFChSim *ch = (AliTOFChSim*)ChArray.At(index);
316 if (!ch->IsSlewed()){
317 Double_t rand = gRandom->Uniform(0,6);
318 itype = (Int_t)(6-rand);
319 ch->SetSpectrum(itype);
320 ch->SetSlewedStatus(kTRUE);
322 else itype = ch->GetSpectrum();
323 TH1F * hToT = fhToT[itype];
324 TF1 * f = (TF1*)flistFunc->At(itype);
325 while (SimToT <= triy){
326 trix = gRandom->Rndm(i);
327 triy = gRandom->Rndm(i);
328 trix = (fMaxToT[itype]-fMinToT[itype])*trix + fMinToT[itype];
329 triy = fMaxToTDistr[itype]*triy;
330 Int_t binx=hToT->FindBin(trix);
331 SimToT=hToT->GetBinContent(binx);
335 for(Int_t kk=0;kk<6;kk++){
338 element->SetToT(trix);
339 Int_t nfpar = f->GetNpar();
340 for (Int_t kk = 0; kk< nfpar; kk++){
341 par[kk]=f->GetParameter(kk);
343 Float_t ToT = element->GetToT();
344 element->SetTdcND(element->GetTdc());
345 Float_t tdc = ((element->GetTdc())*AliTOFGeometry::TdcBinWidth()+32)*1.E-3; //tof signal in ns
346 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];
348 Float_t timeSlewed = tdc + timeoffset;
349 element->SetTdc((timeSlewed*1E3-32)/AliTOFGeometry::TdcBinWidth());
351 TFile * file = new TFile("ToT.root", "recreate");
353 ChArray.Write("ToT",TObject::kSingleKey);
359 //____________________________________________________________________________
361 void AliTOFcalib::SelectESD(AliESD *event, AliRunLoader *rl)
363 AliLoader *tofl = rl->GetLoader("TOFLoader");
365 AliError("Can not get the TOF Loader");
368 tofl->LoadRecPoints("read");
369 TClonesArray *fClusters =new TClonesArray("AliTOFcluster", 1000);
370 TTree *rTree=tofl->TreeR();
372 cerr<<"Can't get the Rec Points tree !\n";
375 TBranch *branch=rTree->GetBranch("TOF");
377 cerr<<"Cant' get the branch !\n";
380 branch->SetAddress(&fClusters);
382 Float_t LowerMomBound=0.8; // [GeV/c] default value Pb-Pb
383 Float_t UpperMomBound=1.8 ; // [GeV/c] default value Pb-Pb
385 Int_t ngoodtrkfinalToT = 0;
386 ntrk=event->GetNumberOfTracks();
387 fESDsel = new TObjArray(ntrk);
389 TObjArray UCdatatemp(ntrk);
391 Int_t ngoodtrkfinal = 0;
392 Float_t mintime =1E6;
393 for (Int_t itrk=0; itrk<ntrk; itrk++) {
394 AliESDtrack *t=event->GetTrack(itrk);
395 //track selection: reconstrution to TOF:
396 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) {
399 //IsStartedTimeIntegral
400 if ((t->GetStatus()&AliESDtrack::kTIME)==0) {
403 Double_t time=t->GetTOFsignal();
404 time*=1.E-3; // tof given in nanoseconds
405 if(time <= mintime)mintime=time;
406 Double_t mom=t->GetP();
407 if (!(mom<=UpperMomBound && mom>=LowerMomBound))continue;
408 UInt_t AssignedTOFcluster=t->GetTOFcluster();//index of the assigned TOF cluster, >0 ?
409 if(AssignedTOFcluster==0){ // not matched
412 AliTOFcluster * tofcl = (AliTOFcluster *)fClusters->UncheckedAt(AssignedTOFcluster);
413 Int_t isector = tofcl->GetDetInd(0);
414 Int_t iplate = tofcl->GetDetInd(1);
415 Int_t istrip = tofcl->GetDetInd(2);
416 Int_t ipadz = tofcl->GetDetInd(3);
417 Int_t ipadx = tofcl->GetDetInd(4);
418 Float_t ToT = tofcl->GetToT();
419 AliTOFcalibESD *unc = new AliTOFcalibESD;
420 unc->CopyFromAliESD(t);
421 unc->SetSector(isector);
422 unc->SetPlate(iplate);
423 unc->SetStrip(istrip);
428 unc->GetExternalCovariance(c1);
432 for (Int_t i = 0; i < ngoodtrk ; i ++){
433 AliTOFcalibESD *unc = (AliTOFcalibESD*)UCdatatemp.At(i);
434 if((unc->GetTOFsignal()-mintime*1.E3)<5.E3){
442 //_____________________________________________________________________________
444 void AliTOFcalib::CombESDId(){
447 Int_t ntracksinset=6;
448 Float_t exptof[6][3];
449 Float_t momentum[6]={0.,0.,0.,0.,0.,0.};
450 Int_t assparticle[6]={3,3,3,3,3,3};
451 Float_t massarray[3]={0.13957,0.493677,0.9382723};
452 Float_t timeofflight[6]={0.,0.,0.,0.,0.,0.};
453 Float_t beta[6]={0.,0.,0.,0.,0.,0.};
454 Float_t texp[6]={0.,0.,0.,0.,0.,0.};
455 Float_t sqMomError[6]={0.,0.,0.,0.,0.,0.};
456 Float_t sqTrackError[6]={0.,0.,0.,0.,0.,0.};
457 Float_t tracktoflen[6]={0.,0.,0.,0.,0.,0.};
458 Float_t TimeResolution = 0.90e-10; // 90 ps by default
459 Float_t timeresolutioninns=TimeResolution*(1.e+9); // convert in [ns]
460 Float_t timezero[6]={0.,0.,0.,0.,0.,0.};
461 Float_t weightedtimezero[6]={0.,0.,0.,0.,0.,0.};
462 Float_t besttimezero[6]={0.,0.,0.,0.,0.,0.};
463 Float_t bestchisquare[6]={0.,0.,0.,0.,0.,0.};
464 Float_t bestweightedtimezero[6]={0.,0.,0.,0.,0.,0.};
465 Float_t bestsqTrackError[6]={0.,0.,0.,0.,0.,0.};
467 Int_t nelements = fESDsel->GetEntries();
468 Int_t nset= (Int_t)(nelements/ntracksinset);
469 for (Int_t i=0; i< nset; i++) {
471 AliTOFcalibESD **unc=new AliTOFcalibESD*[ntracksinset];
472 for (Int_t itrk=0; itrk<ntracksinset; itrk++) {
473 Int_t index = itrk+i*ntracksinset;
474 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(index);
478 for (Int_t j=0; j<ntracksinset; j++) {
479 AliTOFcalibESD *element=unc[j];
480 Double_t mom=element->GetP();
481 Double_t time=element->GetTOFsignal()*1.E-3; // in ns
482 Double_t exptime[10];
483 element->GetIntegratedTimes(exptime);
484 Double_t toflen=element->GetIntegratedLength()/100.; // in m
485 timeofflight[j]=time+t0offset;
486 tracktoflen[j]=toflen+loffset;
487 exptof[j][0]=exptime[2]*1.E-3+0.005;
488 exptof[j][1]=exptime[3]*1.E-3+0.005;
489 exptof[j][2]=exptime[4]*1.E-3+0.005;
493 Float_t Et0best=999.;
494 Float_t chisquarebest=999.;
495 for (Int_t i1=0; i1<3;i1++) {
496 beta[0]=momentum[0]/sqrt(massarray[i1]*massarray[i1]+momentum[0]*momentum[0]);
497 texp[0]=exptof[0][i1];
498 for (Int_t i2=0; i2<3;i2++) {
499 beta[1]=momentum[1]/sqrt(massarray[i2]*massarray[i2]+momentum[1]*momentum[1]);
500 texp[1]=exptof[1][i2];
501 for (Int_t i3=0; i3<3;i3++) {
502 beta[2]=momentum[2]/sqrt(massarray[i3]*massarray[i3]+momentum[2]*momentum[2]);
503 texp[2]=exptof[2][i3];
504 for (Int_t i4=0; i4<3;i4++) {
505 beta[3]=momentum[3]/sqrt(massarray[i4]*massarray[i4]+momentum[3]*momentum[3]);
506 texp[3]=exptof[3][i4];
508 for (Int_t i5=0; i5<3;i5++) {
509 beta[4]=momentum[4]/sqrt(massarray[i5]*massarray[i5]+momentum[4]*momentum[4]);
510 texp[4]=exptof[4][i5];
511 for (Int_t i6=0; i6<3;i6++) {
512 beta[5]=momentum[5]/sqrt(massarray[i6]*massarray[i6]+momentum[5]*momentum[5]);
513 texp[5]=exptof[5][i6];
515 Float_t sumAllweights=0.;
516 Float_t meantzero=0.;
517 Float_t Emeantzero=0.;
519 for (Int_t itz=0; itz<ntracksinset;itz++) {
521 ((1.-beta[itz]*beta[itz])*0.025)*
522 ((1.-beta[itz]*beta[itz])*0.025)*
524 (0.299792*beta[itz]))*
526 (0.299792*beta[itz]));
532 timezero[itz]=texp[itz]-timeofflight[itz];
533 weightedtimezero[itz]=timezero[itz]/sqTrackError[itz];
534 sumAllweights+=1./sqTrackError[itz];
535 meantzero+=weightedtimezero[itz];
537 } // end loop for (Int_t itz=0; itz<15;itz++)
539 meantzero=meantzero/sumAllweights; // it is given in [ns]
540 Emeantzero=sqrt(1./sumAllweights); // it is given in [ns]
542 // calculate chisquare
544 Float_t chisquare=0.;
545 for (Int_t icsq=0; icsq<ntracksinset;icsq++) {
546 chisquare+=(timezero[icsq]-meantzero)*(timezero[icsq]-meantzero)/sqTrackError[icsq];
547 } // end loop for (Int_t icsq=0; icsq<15;icsq++)
548 // cout << " chisquare " << chisquare << endl;
558 if(chisquare<=chisquarebest && ((Float_t) npion/ ((Float_t) ntracksinset)>0.3)){
559 // if(chisquare<=chisquarebest){
561 for(Int_t iqsq = 0; iqsq<ntracksinset; iqsq++) {
562 bestsqTrackError[iqsq]=sqTrackError[iqsq];
563 besttimezero[iqsq]=timezero[iqsq];
564 bestweightedtimezero[iqsq]=weightedtimezero[iqsq];
565 bestchisquare[iqsq]=(timezero[iqsq]-meantzero)*(timezero[iqsq]-meantzero)/sqTrackError[iqsq];
575 chisquarebest=chisquare;
578 } // close if(dummychisquare<=chisquare)
587 Float_t confLevel=999;
588 if(chisquarebest<999.){
589 Double_t dblechisquare=(Double_t)chisquarebest;
590 confLevel=(Float_t)TMath::Prob(dblechisquare,ntracksinset-1);
592 // assume they are all pions for fake sets
593 if(confLevel<0.01 || confLevel==999. ){
594 for (Int_t itrk=0; itrk<ntracksinset; itrk++)assparticle[itrk]=0;
596 for (Int_t itrk=0; itrk<ntracksinset; itrk++) {
597 Int_t index = itrk+i*ntracksinset;
598 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(index);
599 element->SetCombID(assparticle[itrk]);
604 //_____________________________________________________________________________
606 void AliTOFcalib::CalibrateESD(){
607 Int_t nelements = fESDsel->GetEntries();
608 Int_t *number=new Int_t[fsize];
609 fArrayToT = new AliTOFArray(fsize);
610 fArrayTime = new AliTOFArray(fsize);
611 for (Int_t i=0; i<fsize; i++){
613 fArrayToT->AddArray(i, new TArrayF(fgkchannel));
614 TArrayF * parrToT = fArrayToT->GetArray(i);
615 TArrayF & refaToT = * parrToT;
616 fArrayTime->AddArray(i, new TArrayF(fgkchannel));
617 TArrayF * parrTime = fArrayToT->GetArray(i);
618 TArrayF & refaTime = * parrTime;
619 for (Int_t j = 0;j<AliTOFcalib::fgkchannel;j++){
620 refaToT[j]=0.; //ToT[i][j]=j;
621 refaTime[j]=0.; //Time[i][j]=j;
625 for (Int_t i=0; i< nelements; i++) {
626 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(i);
627 Int_t ipid = element->GetCombID();
628 Double_t etime = 0; //expected time
629 Double_t expTime[10];
630 element->GetIntegratedTimes(expTime);
631 if (ipid == 0) etime = expTime[2]*1E-3; //ns
632 else if (ipid == 1) etime = expTime[3]*1E-3; //ns
633 else if (ipid == 2) etime = expTime[4]*1E-3; //ns
634 else AliError("No pid from combinatorial algo for this track");
635 Double_t mtime = (Double_t)element->GetTOFsignal()*1E-3; //measured time
636 Double_t mToT = (Double_t) element->GetToT(); //measured ToT, ns
639 detId[0] = element->GetSector();
640 detId[1] = element->GetPlate();
641 detId[2] = element->GetStrip();
642 detId[3] = element->GetPadz();
643 detId[4] = element->GetPadx();
644 Int_t index = GetIndex(detId);
646 //select the correspondent channel with its simulated ToT spectrum
647 //summing up everything, index = 0 for all channels:
649 Int_t index2 = number[index];
650 TArrayF * parrToT = fArrayToT->GetArray(index);
651 TArrayF & refaToT = * parrToT;
652 refaToT[index2] = (Float_t)mToT;
653 TArrayF * parrTime = fArrayTime->GetArray(index);
654 TArrayF & refaTime = * parrTime;
655 refaTime[index2] = (Float_t)(mtime-etime);
659 for (Int_t i=0;i<1;i++){
660 TH1F * hProf = Profile(i);
661 TF1* fGold = SetFitFunctions(hProf);
662 Int_t nfpar = fGold->GetNpar();
664 for(Int_t kk=0;kk<6;kk++){
667 for (Int_t kk = 0; kk< nfpar; kk++){
668 par[kk]=fGold->GetParameter(kk);
670 AliTOFChannel * CalChannel = fTOFCal->GetChannel(i);
671 CalChannel->SetSlewPar(par);
676 //___________________________________________________________________________
678 TH1F* AliTOFcalib::Profile(Int_t ich){
679 const Int_t nbinToT = 650;
680 Int_t nbinTime = 400;
681 Float_t minTime = -10.5; //ns
682 Float_t maxTime = 10.5; //ns
683 Float_t minToT = 7.5; //ns
684 Float_t maxToT = 40.; //ns
685 Float_t DeltaToT = (maxToT-minToT)/nbinToT;
686 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];
687 Int_t n[nbinToT+1], nentrx[nbinToT+1];
688 Double_t sigmaToT[nbinToT+1];
689 for (Int_t i = 0; i < nbinToT+1 ; i++){
707 TH2F* hSlewing = new TH2F("hSlewing", "hSlewing", nbinToT, minToT, maxToT, nbinTime, minTime, maxTime);
708 TArrayF * parrToT = fArrayToT->GetArray(ich);
709 TArrayF & refaToT = * parrToT;
710 TArrayF * parrTime = fArrayTime->GetArray(ich);
711 TArrayF & refaTime = * parrTime;
712 for (Int_t j = 0; j < AliTOFcalib::fgkchannel; j++){
713 if (refaToT[j] == 0) continue;
714 Int_t nx = (Int_t)((refaToT[j]-minToT)/DeltaToT)+1;
715 if ((refaToT[j] != 0) && (refaTime[j] != 0)){
716 Time[nx]+=refaTime[j];
717 Time2[nx]+=(refaTime[j])*(refaTime[j]);
719 ToT2[nx]+=refaToT[j]*refaToT[j];
721 hSlewing->Fill(refaToT[j],refaTime[j]);
724 Int_t nbinsToT=hSlewing->GetNbinsX();
725 if (nbinsToT != nbinToT) {
726 AliError("Profile :: incompatible numbers of bins");
731 TH1F *histo = new TH1F("histo", "1D Time vs ToT", nbinsToT, minToT, maxToT);
732 for (Int_t i=1;i<=nbinsToT;i++){
734 n[usefulBins]+=nentrx[i];
735 if (n[usefulBins]==0 && i == nbinsToT) {
738 meanTime[usefulBins]+=Time[i];
739 meanTime2[usefulBins]+=Time2[i];
740 meanToT[usefulBins]+=ToT[i];
741 meanToT2[usefulBins]+=ToT2[i];
742 if (n[usefulBins]<20 && i!=nbinsToT) continue;
743 mTime[usefulBins]=meanTime[usefulBins]/n[usefulBins];
744 mToT[usefulBins]=meanToT[usefulBins]/n[usefulBins];
745 sigmaTime[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
746 *(meanTime2[usefulBins]-meanTime[usefulBins]
747 *meanTime[usefulBins]/n[usefulBins]));
748 if ((1./n[usefulBins]/n[usefulBins]
749 *(meanToT2[usefulBins]-meanToT[usefulBins]
750 *meanToT[usefulBins]/n[usefulBins]))< 0) {
751 AliError(" too small radical" );
752 sigmaToT[usefulBins]=0;
755 sigmaToT[usefulBins]=TMath::Sqrt(1./n[usefulBins]/n[usefulBins]
756 *(meanToT2[usefulBins]-meanToT[usefulBins]
757 *meanToT[usefulBins]/n[usefulBins]));
762 for (Int_t i=0;i<usefulBins;i++){
763 Int_t binN = (Int_t)((mToT[i]-minToT)/DeltaToT)+1;
764 histo->Fill(mToT[i],mTime[i]);
765 histo->SetBinError(binN,sigmaTime[i]);
769 //_____________________________________________________________________________
771 void AliTOFcalib::CorrectESDTime(){
772 Int_t nelements = fESDsel->GetEntries();
773 for (Int_t i=0; i< nelements; i++) {
774 AliTOFcalibESD *element=(AliTOFcalibESD*)fESDsel->At(i);
777 detId[0] = element->GetSector();
778 detId[1] = element->GetPlate();
779 detId[2] = element->GetStrip();
780 detId[3] = element->GetPadz();
781 detId[4] = element->GetPadx();
782 Int_t index = GetIndex(detId);
784 //select the correspondent channel with its simulated ToT spectrum
785 //summing up everything, index = 0 for all channels:
787 Int_t ipid = element->GetCombID();
788 Double_t etime = 0; //expected time
789 Double_t expTime[10];
790 element->GetIntegratedTimes(expTime);
791 if (ipid == 0) etime = expTime[2]*1E-3; //ns
792 else if (ipid == 1) etime = expTime[3]*1E-3; //ns
793 else if (ipid == 2) etime = expTime[4]*1E-3; //ns
795 AliTOFChannel * CalChannel = fTOFCal->GetChannel(index);
796 for (Int_t j = 0; j<6; j++){
797 par[j]=CalChannel->GetSlewPar(j);
799 //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;
802 //_____________________________________________________________________________
804 void AliTOFcalib::CorrectESDTime(AliESD *event, AliRunLoader *rl ){
805 AliLoader *tofl = rl->GetLoader("TOFLoader");
807 AliError("Can not get the TOF Loader");
810 tofl->LoadRecPoints("read");
811 TClonesArray *fClusters =new TClonesArray("AliTOFcluster", 1000);
812 TTree *rTree=tofl->TreeR();
814 cerr<<"Can't get the Rec Points tree !\n";
817 TBranch *branch=rTree->GetBranch("TOF");
819 cerr<<"Cant' get the branch !\n";
822 branch->SetAddress(&fClusters);
825 ntrk=event->GetNumberOfTracks();
826 for (Int_t itrk=0; itrk<ntrk; itrk++) {
827 AliESDtrack *t=event->GetTrack(itrk);
828 if ((t->GetStatus()&AliESDtrack::kTOFout)==0) {
831 //IsStartedTimeIntegral
832 if ((t->GetStatus()&AliESDtrack::kTIME)==0) {
835 UInt_t AssignedTOFcluster=t->GetTOFcluster();//index of the assigned TOF cluster, >0 ?
836 if(AssignedTOFcluster==0){ // not matched
839 AliTOFcluster * tofcl = (AliTOFcluster *)fClusters->UncheckedAt(AssignedTOFcluster);
842 detId[0] = tofcl->GetSector();
843 detId[1] = tofcl->GetPlate();
844 detId[2] = tofcl->GetStrip();
845 detId[3] = tofcl->GetPadz();
846 detId[4] = tofcl->GetPadx();
847 Int_t index = GetIndex(detId);
849 //select the correspondent channel with its simulated ToT spectrum
850 //summing up everything, index = 0 for all channels:
852 AliTOFChannel * CalChannel = fTOFCal->GetChannel(index);
854 for (Int_t j = 0; j<6; j++){
855 par[j]=CalChannel->GetSlewPar(j);
857 Float_t ToT = tofcl->GetToT();
859 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;
862 //_____________________________________________________________________________
864 void AliTOFcalib::WriteOnCDB(){
865 AliCDBManager *man = AliCDBManager::Instance();
866 AliCDBId id("TOF/Calib/Constants",1,100);
867 AliCDBMetaData *md = new AliCDBMetaData();
868 md->SetResponsible("Shimmize");
869 man->Put(fTOFCal,id,md);
871 //_____________________________________________________________________________
873 void AliTOFcalib::ReadFromCDB(Char_t *sel, Int_t nrun){
874 AliCDBManager *man = AliCDBManager::Instance();
875 AliCDBEntry *entry = man->Get(sel,nrun);
877 AliError("Retrivial failed");
878 AliCDBStorage *origSto =man->GetDefaultStorage();
879 man->SetDefaultStorage("local://$ALICE_ROOT");
880 entry = man->Get("TOF/Calib/Constants",nrun);
881 man->SetDefaultStorage(origSto);
883 AliTOFCal *cal =(AliTOFCal*)entry->GetObject();
886 //_____________________________________________________________________________
888 Int_t AliTOFcalib::GetIndex(Int_t *detId){
889 Int_t isector = detId[0];
890 if (isector >= fNSector)
891 AliError(Form("Wrong sector number in TOF (%d) !",isector));
892 Int_t iplate = detId[1];
893 if (iplate >= fNPlate)
894 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
895 Int_t istrip = detId[2];
896 Int_t ipadz = detId[3];
897 Int_t ipadx = detId[4];
898 Int_t stripOffset = 0;
904 stripOffset = fNStripC;
907 stripOffset = fNStripC+fNStripB;
910 stripOffset = fNStripC+fNStripB+fNStripA;
913 stripOffset = fNStripC+fNStripB+fNStripA+fNStripB;
916 AliError(Form("Wrong plate number in TOF (%d) !",iplate));
920 Int_t idet = ((2*(fNStripC+fNStripB)+fNStripA)*fNpadZ*fNpadX)*isector +
921 (stripOffset*fNpadZ*fNpadX)+
922 (fNpadZ*fNpadX)*istrip+