]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCPreprocessorOffline.cxx
Code to analyze the TPC calibration and to produce OCDB entries
[u/mrichter/AliRoot.git] / TPC / AliTPCPreprocessorOffline.cxx
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  **************************************************************************/
15
16
17
18 /*
19   Responsible: marian.ivanov@cern.ch 
20   Code to analyze the TPC calibration and to produce OCDB entries  
21
22
23    .x ~/rootlogon.C
24    gSystem->Load("libANALYSIS");
25    gSystem->Load("libTPCcalib");
26
27    AliTPCPreprocessorOffline proces;
28   process.CalibTimeGain("CalibObjects.root",run0,run1,ocdbPath);
29   // take the raw calibration data from the file CalibObjects.root 
30   // and make a OCDB entry with run  validity run0-run1
31   // results are stored at the ocdbPath - local or alien ...
32   // default storage ""- data stored at current working directory 
33  
34 */
35 #include "Riostream.h"
36 #include <fstream>
37 #include "TMap.h"
38 #include "TGraphErrors.h"
39 #include "AliExternalTrackParam.h"
40 #include "TFile.h"
41 #include "TGraph.h"
42 #include "TMultiGraph.h"
43 #include "TCanvas.h"
44 #include "THnSparse.h"
45 #include "TLegend.h"
46 #include "TPad.h"
47 #include "TH2D.h"
48 #include "AliTPCROC.h"
49 #include "AliTPCCalROC.h"
50 #include "AliESDfriend.h"
51 #include "AliTPCcalibTime.h"
52 #include "AliSplineFit.h"
53 #include "AliCDBMetaData.h"
54 #include "AliCDBId.h"
55 #include "AliCDBManager.h"
56 #include "AliCDBStorage.h"
57 #include "AliTPCcalibBase.h"
58 #include "AliTPCcalibDB.h"
59 #include "AliTPCcalibDButil.h"
60 #include "AliRelAlignerKalman.h"
61 #include "AliTPCParamSR.h"
62 #include "AliTPCcalibTimeGain.h"
63 #include "AliSplineFit.h"
64 #include "AliTPCPreprocessorOffline.h"
65
66
67 ClassImp(AliTPCPreprocessorOffline)
68
69 AliTPCPreprocessorOffline::AliTPCPreprocessorOffline():
70   TNamed("TPCPreprocessorOffline","TPCPreprocessorOffline"),
71   kMinEntries(500),                      // minimal number of entries for fit
72   startRun(0),                         // start Run - used to make fast selection in THnSparse
73   endRun(0),                           // end   Run - used to make fast selection in THnSparse
74   startTime(0),                        // startTime - used to make fast selection in THnSparse
75   endTime(0),                          // endTime   - used to make fast selection in THnSparse
76   ocdbStorage(""),                   // path to the OCDB storage
77   fVdriftArray(new TObjArray),
78   fTimeDrift(0),
79   fGraphMIP(0),                // graph time dependence of MIP
80   fGraphCosmic(0),             // graph time dependence at Plateu
81   fFitMIP(0),                  // fit of dependence - MIP
82   fFitCosmic(0),               // fit of dependence - Plateu
83   fGainArray(new TObjArray),               // array to be stored in the OCDB
84   fGainMIP(0),          // calibration component for MIP
85   fGainCosmic(0)       // calibration component for cosmic
86 {
87   //
88   // default constructor
89   //
90 }
91
92 AliTPCPreprocessorOffline::~AliTPCPreprocessorOffline() {
93   //
94   // Destructor
95   //
96 }
97
98
99
100
101 void AliTPCPreprocessorOffline::GetRunRange(AliTPCcalibTime* timeDrift){
102   //
103   // find the fist and last run
104   //
105   TObjArray *hisArray =timeDrift->GetHistoDrift();
106   {for (Int_t i=0; i<hisArray->GetEntriesFast(); i++){
107     THnSparse* addHist=(THnSparse*)hisArray->UncheckedAt(i);
108     if (addHist->GetEntries()<kMinEntries) continue;
109     if (!addHist) continue;
110     TH1D* histo    =addHist->Projection(3);
111     TH1D* histoTime=addHist->Projection(0);
112     printf("%s\t%f\t%d\t%d\n",histo->GetName(), histo->GetEntries(),histo->FindFirstBinAbove(0),histo->FindLastBinAbove(0));
113
114     if (startRun<=0){ 
115       startRun=histo->FindFirstBinAbove(0);
116       endRun  =histo->FindLastBinAbove(0);
117     }else{
118       startRun=TMath::Min(histo->FindFirstBinAbove(0),startRun);
119       endRun  =TMath::Max(histo->FindLastBinAbove(0),endRun);
120     }
121     if (startTime==0){ 
122       startTime=histoTime->FindFirstBinAbove(0);
123       endTime  =histoTime->FindLastBinAbove(0);
124     }else{
125       startTime=TMath::Min(histoTime->FindFirstBinAbove(0),startTime);
126       endTime  =TMath::Max(histoTime->FindLastBinAbove(0),endTime);
127     }
128     delete histo;
129     delete histoTime;
130   }}
131   if (startRun<0) startRun=0;
132   if (endRun<0) endRun=100000000;
133   printf("Run range  :\t%d-%d\n", startRun, endRun);
134   printf("Time range :\t%d-%d\n", startTime, endTime);
135
136 }
137
138
139
140 void AliTPCPreprocessorOffline::CalibTimeVdrift(Char_t* file, Int_t ustartRun, Int_t uendRun, TString pocdbStorage){
141   //
142   //
143   //
144   if (pocdbStorage.Length()>0) ocdbStorage=pocdbStorage;
145   else
146   ocdbStorage="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
147   //
148   // 1. Initialization and run range setting
149   TFile fcalib(file);
150   fTimeDrift=(AliTPCcalibTime*)fcalib.Get("calibTime");
151   startRun=ustartRun;
152   endRun=ustartRun; 
153   TObjArray *hisArray =fTimeDrift->GetHistoDrift();  
154   GetRunRange(fTimeDrift);
155   for (Int_t i=0; i<hisArray->GetEntriesFast(); i++){
156     THnSparse* addHist=(THnSparse*)hisArray->At(i);
157     if (!addHist) continue;
158     if (startTime<endTime) addHist->GetAxis(0)->SetRange(startTime-1,endTime+1);
159     if (startRun<endRun) addHist->GetAxis(3)->SetRange(startRun-1,endRun+1);
160   }
161   //
162   //
163   // 2. extraction of the information
164   //
165   fVdriftArray = new TObjArray();
166   AddAlignmentGraphs(fVdriftArray,fTimeDrift);
167   AddHistoGraphs(fVdriftArray,fTimeDrift,kMinEntries);
168   AddLaserGraphs(fVdriftArray,fTimeDrift);
169   //
170   // 3. Append QA plots
171   //
172   MakeDefaultPlots(fVdriftArray,fVdriftArray);
173   //
174   //
175   // 4. update of OCDB
176   //
177   //
178   
179   UpdateOCDBDrift(ustartRun,uendRun,ocdbStorage);
180 }
181
182 void AliTPCPreprocessorOffline::UpdateOCDBDrift( Int_t ustartRun, Int_t uendRun,  const char* storagePath ){
183   //
184   // Update OCDB 
185   //
186   AliCDBMetaData *metaData= new AliCDBMetaData();
187   metaData->SetObjectClassName("TObjArray");
188   metaData->SetResponsible("Marian Ivanov");
189   metaData->SetBeamPeriod(1);
190   metaData->SetAliRootVersion("05-25-01"); //root version
191   metaData->SetComment("Calibration of the time dependence of the drift velocity");
192   AliCDBId* id1=NULL;
193   id1=new AliCDBId("TPC/Calib/TimeDrift", ustartRun, uendRun);
194   AliCDBStorage* gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
195   gStorage->Put(fVdriftArray, (*id1), metaData);
196 }
197
198
199
200 void AliTPCPreprocessorOffline::UpdateDriftParam(AliTPCParam *param, TObjArray *arr, Int_t lstartRun){
201   //
202   //  update the OCDB entry for the nominal time0
203   //
204   //
205   //  AliTPCParam * param = AliTPCcalibDB::Instance()->GetParameters();
206   AliTPCParam *paramNew = (AliTPCParam *)param->Clone();
207   TGraphErrors *grT =  (TGraphErrors *)arr->FindObject("ALIGN_ITSM_TPC_T0");
208   Double_t deltaTcm = TMath::Median(grT->GetN(),grT->GetY());
209   Double_t deltaT   = deltaTcm/param->GetDriftV();
210   paramNew->SetL1Delay(param->GetL1Delay()-deltaT);
211   paramNew->Update();
212
213   AliCDBMetaData *metaData= new AliCDBMetaData();
214   metaData->SetObjectClassName("TObjArray");
215   metaData->SetResponsible("Marian Ivanov");
216   metaData->SetBeamPeriod(1);
217   metaData->SetAliRootVersion("05-25-02"); //root version
218   metaData->SetComment("Updated calibration of nominal time 0");
219   AliCDBId* id1=NULL;
220   id1=new AliCDBId("TPC/Calib/Parameters", lstartRun, AliCDBRunRange::Infinity());
221   AliCDBStorage* gStorage = AliCDBManager::Instance()->GetStorage(ocdbStorage);
222   gStorage->Put(param, (*id1), metaData);
223
224 }
225
226
227 void AliTPCPreprocessorOffline::PrintArray(TObjArray *array){
228   //
229   //
230   //
231   Int_t entries = array->GetEntries();
232   for (Int_t i=0; i<entries; i++){
233     if (!array->At(i)) continue;
234     printf("%d\t %s\n", i,  array->At(i)->GetName());
235   }
236 }
237
238
239
240 TGraphErrors* AliTPCPreprocessorOffline::FilterGraphDrift(TGraphErrors * graph, Float_t errSigmaCut, Float_t medianCutAbs){
241   // 2 filters:
242   //    1. filter graph - error cut errSigmaCut
243   //    2. filter graph - medianCutAbs around median
244   //
245   // errSigmaCut   - cut on error
246   // medianCutAbs  - cut on value around median
247   Double_t dummy=0;               //   
248   //
249   // 1. filter graph - error cut errSigmaCut
250   //              
251   TGraphErrors *graphF; 
252   graphF = AliTPCcalibDButil::FilterGraphMedianErr(graph,errSigmaCut,dummy);
253   delete graph;
254   if (!graphF) return 0;
255   graph = AliTPCcalibDButil::FilterGraphMedianErr(graphF,errSigmaCut,dummy);
256   delete graphF;
257   if (!graph) return 0;
258   //
259   // filter graph - kMedianCutAbs around median
260   // 
261   graphF=FilterGraphMedianAbs(graph, medianCutAbs,dummy);
262   delete graph;
263   if (!graphF) return 0;
264   graph=FilterGraphMedianAbs(graphF, medianCutAbs,dummy);
265   delete graphF;
266   if (!graph) return 0;
267   return graph;
268 }
269
270
271
272 TGraphErrors* AliTPCPreprocessorOffline::FilterGraphMedianAbs(TGraphErrors * graph, Float_t cut,Double_t &medianY){
273   //
274   // filter outlyer measurement
275   // Only points around median +- cut filtered 
276   //
277   if (!graph) return  0;
278   Int_t kMinPoints=2;
279   Int_t npoints0 = graph->GetN();
280   Int_t npoints=0;
281   Float_t  rmsY=0;
282   Double_t *outx=new Double_t[npoints0];
283   Double_t *outy=new Double_t[npoints0];
284   Double_t *errx=new Double_t[npoints0];
285   Double_t *erry=new Double_t[npoints0];
286   //
287   //
288   if (npoints0<kMinPoints) return 0;
289   for (Int_t iter=0; iter<3; iter++){
290     npoints=0;
291     for (Int_t ipoint=0; ipoint<npoints0; ipoint++){
292       if (graph->GetY()[ipoint]==0) continue;
293       if (iter>0 &&TMath::Abs(graph->GetY()[ipoint]-medianY)>cut) continue;  
294       outx[npoints]  = graph->GetX()[ipoint];
295       outy[npoints]  = graph->GetY()[ipoint];
296       errx[npoints]  = graph->GetErrorX(ipoint);
297       erry[npoints]  = graph->GetErrorY(ipoint);
298       npoints++;
299     }
300     if (npoints<=1) break;
301     medianY  =TMath::Median(npoints,outy);
302     rmsY   =TMath::RMS(npoints,outy);
303   }
304   TGraphErrors *graphOut=0;
305   if (npoints>1) graphOut= new TGraphErrors(npoints,outx,outy,errx,erry); 
306   return graphOut;
307 }
308
309
310 void AliTPCPreprocessorOffline::AddHistoGraphs(  TObjArray * vdriftArray, AliTPCcalibTime *timeDrift, Int_t minEntries){
311   //
312   // Add graphs corresponding to the alignment
313   //
314   const Double_t kErrSigmaCut=5;      // error sigma cut - for filtering
315   const Double_t kMedianCutAbs=0.03;  // error sigma cut - for filtering
316   //
317   TObjArray * array=timeDrift->GetHistoDrift();
318   if (array){
319     THnSparse* hist=NULL;
320     // 2.a) cosmics with different triggers
321     for (Int_t i=0; i<array->GetEntriesFast();i++){
322       hist=(THnSparseF*)array->UncheckedAt(i);
323       if(!hist) continue;
324       if (hist->GetEntries()<minEntries) continue;
325       //hist->Print();
326       TString name=hist->GetName();
327       Int_t dim[4]={0,1,2,3};
328       THnSparse* newHist=hist->Projection(4,dim);
329       newHist->SetName(name);
330       TGraphErrors* graph=AliTPCcalibBase::FitSlices(newHist,2,0,400,100,0.05,0.95, kTRUE);
331       printf("name=%s graph=%i, N=%i\n", name.Data(), graph==0, graph->GetN());
332       Int_t pos=name.Index("_");
333       name=name(pos,name.Capacity()-pos);
334       TString graphName=graph->ClassName();
335       graphName+=name;
336       graphName.ToUpper();
337       //
338       graph = FilterGraphDrift(graph, kErrSigmaCut, kMedianCutAbs);
339       if (!graph) {
340         printf("Graph =%s filtered out\n", name.Data());
341         continue;
342       }
343       //
344       graph->SetMarkerStyle(i%8+20);
345       graph->SetMarkerColor(i%7);
346       graph->GetXaxis()->SetTitle("Time");
347       graph->GetYaxis()->SetTitle("v_{dcor}");
348       graph->SetName(graphName);
349       graph->SetTitle(graphName);
350       printf("Graph %d\t=\t%s\n", i, graphName.Data());
351       vdriftArray->Add(graph);
352     }
353   }
354 }
355
356
357
358
359 void AliTPCPreprocessorOffline::AddAlignmentGraphs(  TObjArray * vdriftArray, AliTPCcalibTime *timeDrift){
360   //
361   // Add graphs corresponding to alignment to the object array
362   //
363   TObjArray *arrayITS=0;
364   TObjArray *arrayTOF=0;
365   TObjArray *arrayTRD=0;
366   TMatrixD *mstatITS=0;
367   TMatrixD *mstatTOF=0;
368   TMatrixD *mstatTRD=0;
369   //
370   arrayITS=timeDrift->GetAlignITSTPC();
371   arrayTRD=timeDrift->GetAlignTRDTPC();
372   arrayTOF=timeDrift->GetAlignTOFTPC();
373
374   if (arrayITS->GetEntries()>0) mstatITS= AliTPCcalibDButil::MakeStatRelKalman(arrayITS,0.9,50,0.025);
375   if (arrayTOF->GetEntries()>0) mstatTOF= AliTPCcalibDButil::MakeStatRelKalman(arrayTOF,0.9,1000,0.025);
376   if (arrayTRD->GetEntries()>0) mstatTRD= AliTPCcalibDButil::MakeStatRelKalman(arrayTRD,0.9,50,0.025);
377   //
378   TObjArray * arrayITSP= AliTPCcalibDButil::SmoothRelKalman(arrayITS,*mstatITS, 0, 5.);
379   TObjArray * arrayITSM= AliTPCcalibDButil::SmoothRelKalman(arrayITS,*mstatITS, 1, 5.);
380   TObjArray * arrayITSB= AliTPCcalibDButil::SmoothRelKalman(arrayITSP,arrayITSM);
381   TObjArray * arrayTOFP= AliTPCcalibDButil::SmoothRelKalman(arrayTOF,*mstatTOF, 0, 5.);
382   TObjArray * arrayTOFM= AliTPCcalibDButil::SmoothRelKalman(arrayTOF,*mstatTOF, 1, 5.);
383   TObjArray * arrayTOFB= AliTPCcalibDButil::SmoothRelKalman(arrayTOFP,arrayTOFM);
384
385   TObjArray * arrayTRDP= 0x0;
386   TObjArray * arrayTRDM= 0x0;
387   TObjArray * arrayTRDB= 0x0;
388   arrayTRDP= AliTPCcalibDButil::SmoothRelKalman(arrayTRD,*mstatTRD, 0, 5.);
389   arrayTRDM= AliTPCcalibDButil::SmoothRelKalman(arrayTRD,*mstatTRD, 1, 5.);
390   arrayTRDB= AliTPCcalibDButil::SmoothRelKalman(arrayTRDP,arrayTRDM);
391   //
392   //
393   Int_t entries=TMath::Max(arrayITS->GetEntriesFast(),arrayTOF->GetEntriesFast());
394   TObjArray *arrays[12]={arrayITS, arrayITSP, arrayITSM, arrayITSB,
395                          arrayTRD, arrayTRDP, arrayTRDM, arrayTRDB,
396                          arrayTOF, arrayTOFP, arrayTOFM, arrayTOFB};
397   TString   grnames[12]={"ALIGN_ITS", "ALIGN_ITSP", "ALIGN_ITSM", "ALIGN_ITSB",
398                          "ALIGN_TRD", "ALIGN_TRDP", "ALIGN_TRDM","ALIGN_TRDB",
399                          "ALIGN_TOF", "ALIGN_TOFP", "ALIGN_TOFM","ALIGN_TOFB"};
400   TString   grpar[9]={"DELTAPSI", "DELTATHETA", "DELTAPHI",
401                       "DELTAX", "DELTAY", "DELTAZ",
402                       "DRIFTVD", "T0", "VDGY"};
403
404   
405   TVectorD vX(entries);
406   TVectorD vY(entries);
407   TVectorD vEx(entries);
408   TVectorD vEy(entries);
409   TObjArray *arr=0;
410   for (Int_t iarray=0; iarray<12; iarray++){
411     arr = arrays[iarray];
412     if (arr==0) continue;
413     for (Int_t ipar=0; ipar<9; ipar++){      
414       Int_t counter=0;
415       for (Int_t itime=0; itime<arr->GetEntriesFast(); itime++){
416         AliRelAlignerKalman * kalman = (AliRelAlignerKalman *) arr->UncheckedAt(itime);
417         if (!kalman) continue;
418         vX[counter]=kalman->GetTimeStamp();
419         vY[counter]=(*(kalman->GetState()))[ipar];
420         if (ipar==6) vY[counter]=1./(*(kalman->GetState()))[ipar]-1;
421         vEx[counter]=0;
422         vEy[counter]=TMath::Sqrt((*(kalman->GetStateCov()))(ipar,ipar));
423         counter++;
424       }
425     
426       TGraphErrors * graph=new TGraphErrors(counter, vX.GetMatrixArray(),
427                                           vY.GetMatrixArray(),
428                                           vEx.GetMatrixArray(),
429                                           vEy.GetMatrixArray());
430       TString grName=grnames[iarray];
431       grName+="_TPC_";
432       grName+=grpar[ipar];
433       graph->SetName(grName.Data());
434       vdriftArray->AddLast(graph);
435     }
436   }  
437 }
438
439
440
441
442 void AliTPCPreprocessorOffline::AddLaserGraphs(  TObjArray * vdriftArray, AliTPCcalibTime *timeDrift){
443   //
444   // add graphs for laser
445   //
446   const Double_t delayL0L1 = 0.071;  //this is hack for 1/2 weeks
447   THnSparse *hisN=0;
448   TGraphErrors *grLaser[6]={0,0,0,0,0,0};
449   hisN = timeDrift->GetHistVdriftLaserA(0);
450   if (timeDrift->GetHistVdriftLaserA(0)){
451     grLaser[0]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserA(0),0,2,5,delayL0L1);
452     grLaser[0]->SetName("GRAPH_MEAN_DELAY_LASER_ALL_A");
453     vdriftArray->AddLast(grLaser[0]);
454   }    
455   if (timeDrift->GetHistVdriftLaserA(1)){
456     grLaser[1]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserA(1),0,2,5);
457     grLaser[1]->SetName("GRAPH_MEAN_DRIFT_LASER_ALL_A");
458     vdriftArray->AddLast(grLaser[1]);
459   }    
460   if (timeDrift->GetHistVdriftLaserA(2)){
461     grLaser[2]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserA(2),0,2,5);
462     grLaser[2]->SetName("GRAPH_MEAN_GLOBALYGRADIENT_LASER_ALL_A");
463     vdriftArray->AddLast(grLaser[2]);
464   }    
465   if (timeDrift->GetHistVdriftLaserC(0)){
466     grLaser[3]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserC(0),0,2,5,delayL0L1);
467     grLaser[3]->SetName("GRAPH_MEAN_DELAY_LASER_ALL_C");
468     vdriftArray->AddLast(grLaser[3]);
469   }    
470   if (timeDrift->GetHistVdriftLaserC(1)){
471     grLaser[4]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserC(1),0,2,5);
472     grLaser[4]->SetName("GRAPH_MEAN_DRIFT_LASER_ALL_C");
473     vdriftArray->AddLast(grLaser[4]);
474   }    
475   if (timeDrift->GetHistVdriftLaserC(2)){
476     grLaser[5]=MakeGraphFilter0(timeDrift->GetHistVdriftLaserC(2),0,2,5);
477     grLaser[5]->SetName("GRAPH_MEAN_GLOBALYGRADIENT_LASER_ALL_C");    
478     vdriftArray->AddLast(grLaser[5]);
479   }    
480   for (Int_t i=0; i<6;i++){
481     if (grLaser[i]) {
482       SetDefaultGraphDrift(grLaser[i], 1,(i+20));
483       grLaser[i]->GetYaxis()->SetTitle("Laser Correction");
484     }
485   }
486 }
487  
488  
489 TGraphErrors * AliTPCPreprocessorOffline::MakeGraphFilter0(THnSparse *hisN, Int_t itime, Int_t ival, Int_t minEntries, Double_t offset){
490   //
491   // Make graph with mean values and rms
492   //
493   hisN->GetAxis(itime)->SetRange(0,100000000);
494   hisN->GetAxis(ival)->SetRange(0,100000000);
495   TH1 * hisT      = hisN->Projection(itime);
496   TH1 * hisV      = hisN->Projection(ival);
497   //
498   Int_t firstBinA = hisT->FindFirstBinAbove(2);
499   Int_t lastBinA  = hisT->FindLastBinAbove(2);    
500   Int_t firstBinV = hisV->FindFirstBinAbove(0);
501   Int_t lastBinV  = hisV->FindLastBinAbove(0);    
502   hisN->GetAxis(itime)->SetRange(firstBinA,lastBinA);
503   hisN->GetAxis(ival)->SetRange(firstBinV,lastBinV);
504   Int_t entries=0;
505   for (Int_t ibin=firstBinA; ibin<lastBinA; ibin++){
506     Double_t cont = hisT->GetBinContent(ibin);
507     if (cont<minEntries) continue;
508     entries++;
509   }
510   TVectorD vecTime(entries);
511   TVectorD vecMean0(entries);
512   TVectorD vecRMS0(entries);
513   TVectorD vecMean1(entries);
514   TVectorD vecRMS1(entries);
515   entries=0;
516   {for (Int_t ibin=firstBinA; ibin<lastBinA; ibin++){
517       Double_t cont = hisT->GetBinContent(ibin);
518       if (cont<minEntries) continue;
519       hisN->GetAxis(itime)->SetRange(ibin-1,ibin+1);
520       Double_t time = hisT->GetBinCenter(ibin);
521       TH1 * his = hisN->Projection(ival);
522       Double_t nentries0= his->GetBinContent(his->FindBin(0));
523       if (cont-nentries0<minEntries) continue;
524       //
525       his->SetBinContent(his->FindBin(0),0);
526       vecTime[entries]=time;
527       vecMean0[entries]=his->GetMean()+offset;
528       vecMean1[entries]=his->GetMeanError();
529       vecRMS0[entries] =his->GetRMS();
530       vecRMS1[entries] =his->GetRMSError();
531       delete his;  
532       entries++;
533     }}
534   delete hisT;
535   delete hisV;
536   TGraphErrors * graph =  new TGraphErrors(entries,vecTime.GetMatrixArray(), vecMean0.GetMatrixArray(),                                    0, vecMean1.GetMatrixArray());
537   return graph;
538 }
539
540
541
542
543
544
545
546
547 void AliTPCPreprocessorOffline::SetDefaultGraphDrift(TGraph *graph, Int_t color, Int_t style){
548   //
549   //
550   //
551   graph->GetXaxis()->SetTimeDisplay(kTRUE);
552   graph->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}");
553   graph->SetMaximum( 0.025);
554   graph->SetMinimum(-0.025);
555   graph->GetXaxis()->SetTitle("Time");
556   graph->GetYaxis()->SetTitle("v_{dcorr}");
557   //
558   graph->GetYaxis()->SetLabelSize(0.03);
559   graph->GetXaxis()->SetLabelSize(0.03);
560   //
561   graph->GetXaxis()->SetNdivisions(10,5,0);
562   graph->GetYaxis()->SetNdivisions(10,5,0);
563   //
564   graph->GetXaxis()->SetLabelOffset(0.02);
565   graph->GetYaxis()->SetLabelOffset(0.005);
566   //
567   graph->GetXaxis()->SetTitleOffset(1.3);
568   graph->GetYaxis()->SetTitleOffset(1.2);
569   //
570   graph->SetMarkerColor(color);
571   graph->SetLineColor(color);
572   graph->SetMarkerStyle(style);
573 }
574
575 void AliTPCPreprocessorOffline::SetPadStyle(TPad *pad, Float_t mx0, Float_t mx1, Float_t my0, Float_t my1){
576   //
577   //
578   pad->SetTicks(1,1);
579   pad->SetMargin(mx0,mx1,my0,my1);
580 }
581
582
583 void AliTPCPreprocessorOffline::MakeDefaultPlots(TObjArray * arr, TObjArray *picArray){
584   //
585   //
586   //
587   // margins
588   Float_t mx0=0.12, mx1=0.1, my0=0.15, my1=0.1;
589   //
590   TGraphErrors* laserA       =(TGraphErrors*)arr->FindObject("GRAPH_MEAN_DRIFT_LASER_ALL_A");
591   TGraphErrors* laserC       =(TGraphErrors*)arr->FindObject("GRAPH_MEAN_DRIFT_LASER_ALL_C");
592   TGraphErrors* cosmic       =(TGraphErrors*)arr->FindObject("TGRAPHERRORS_MEAN_VDRIFT_COSMICS_ALL");
593   TGraphErrors* cross        =(TGraphErrors*)arr->FindObject("TGRAPHERRORS_VDRIFT_CROSS_ALL");
594   TGraphErrors* itstpcP       =(TGraphErrors*)arr->FindObject("ALIGN_ITSP_TPC_DRIFTVD");
595   TGraphErrors* itstpcM       =(TGraphErrors*)arr->FindObject("ALIGN_ITSM_TPC_DRIFTVD");
596   TGraphErrors* itstpcB       =(TGraphErrors*)arr->FindObject("ALIGN_ITSB_TPC_DRIFTVD");
597   //
598   if (laserA)  SetDefaultGraphDrift(laserA,2,25);
599   if (laserC)  SetDefaultGraphDrift(laserC,4,26);
600   if (cosmic)  SetDefaultGraphDrift(cosmic,3,27);
601   if (cross)   SetDefaultGraphDrift(cross,4,28);
602   if (itstpcP) SetDefaultGraphDrift(itstpcP,2,29);
603   if (itstpcM) SetDefaultGraphDrift(itstpcM,4,30);
604   if (itstpcB) SetDefaultGraphDrift(itstpcB,1,31);
605   //
606   //
607   TPad *pad=0;
608   //
609   // Laser-Laser
610   //
611   if (laserA&&laserC){
612     pad = new TCanvas("TPCLaserVDrift","TPCLaserVDrift");
613     laserA->Draw("alp");
614     SetPadStyle(pad,mx0,mx1,my0,my1);
615     laserA->Draw("apl");
616     laserC->Draw("p");
617     TLegend *legend = new TLegend(mx0+0.01,1-my1-0.2, 0.5, 1-my1-0.01, "Drift velocity correction");
618     legend->AddEntry(laserA,"Laser A side");
619     legend->AddEntry(laserC,"Laser C side");
620     legend->Draw();    
621     picArray->AddLast(pad);
622   }
623
624   if (itstpcP&&itstpcM){
625     pad = new TCanvas("ITSTPC","ITSTPC");
626     itstpcP->Draw("alp");
627     SetPadStyle(pad,mx0,mx1,my0,my1);    
628     itstpcP->Draw("alp");
629     gPad->Clear();
630     itstpcM->Draw("apl");
631     itstpcP->Draw("p");
632     itstpcB->Draw("p");
633     TLegend *legend = new TLegend(mx0+0.01,1-my1-0.2, 0.5, 1-my1-0.01, "Drift velocity correction");
634     legend->AddEntry(itstpcP,"ITS-TPC smooth plus");
635     legend->AddEntry(itstpcM,"ITS-TPC smooth minus");
636     legend->AddEntry(itstpcB,"ITS-TPC smooth ");
637     legend->Draw();    
638     picArray->AddLast(pad);
639   }
640
641   if (itstpcB&&laserA){
642     pad = new TCanvas("ITSTPC_LASER","ITSTPC_LASER");
643     SetPadStyle(pad,mx0,mx1,my0,my1);    
644     laserA->Draw("alp");
645     itstpcP->Draw("p");
646     itstpcM->Draw("p");
647     itstpcB->Draw("p");
648     TLegend *legend = new TLegend(mx0+0.01,1-my1-0.2, 0.5, 1-my1-0.01, "Drift velocity correction");
649     legend->AddEntry(laserA,"TPC laser");
650     legend->AddEntry(itstpcP,"ITS-TPC smooth plus");   
651     legend->AddEntry(itstpcM,"ITS-TPC smooth minus");   
652     legend->AddEntry(itstpcB,"ITS-TPC smooth ");
653     legend->Draw();
654     picArray->AddLast(pad);
655   }
656
657   if (itstpcP&&cross){ 
658     pad = new TCanvas("ITSTPC_CROSS","ITSTPC_CROSS");
659     SetPadStyle(pad,mx0,mx1,my0,my1);    
660     itstpcP->Draw("alp");
661     pad->Clear();
662     cross->Draw("ap");
663     itstpcP->Draw("p");
664     //
665     TLegend *legend = new TLegend(mx0+0.01,1-my1-0.2, 0.5, 1-my1-0.01, "Drift velocity correction");
666
667     legend->AddEntry(cross,"TPC cross tracks");
668     legend->AddEntry(itstpcB,"ITS-TPC smooth");
669     legend->Draw();        
670     picArray->AddLast(pad);
671   }
672   if (itstpcP&&cosmic){ 
673     pad = new TCanvas("ITSTPC_COSMIC","ITSTPC_COSMIC");
674     SetPadStyle(pad,mx0,mx1,my0,my1);    
675     itstpcP->Draw("alp");
676     pad->Clear();
677     cosmic->Draw("ap");
678     itstpcP->Draw("p");
679     //
680     TLegend *legend = new TLegend(mx0+0.01,1-my1-0.2, 0.5, 1-my1-0.01, "Drift velocity correction");
681
682     legend->AddEntry(cosmic,"TPC cross tracks0 up-down");
683     legend->AddEntry(itstpcB,"ITS-TPC smooth");
684     legend->Draw();        
685     picArray->AddLast(pad);
686   }
687 }
688
689
690
691
692 void AliTPCPreprocessorOffline::CalibTimeGain(Char_t* fileName, Int_t startRunNumber, Int_t endRunNumber,  TString  pocdbStorage){
693   //
694   // Update OCDB gain
695   //
696   ReadGainGlobal(fileName);
697   AnalyzeGain(startRunNumber,endRunNumber, 1000,1.43);
698   MakeQAPlot(1.43);  
699   if (pocdbStorage.Length()==0) pocdbStorage+="local://"+gSystem->GetFromPipe("pwd")+"/OCDB";
700   UpdateOCDBGain( startRunNumber, endRunNumber, pocdbStorage.Data());
701 }
702
703
704
705
706 void AliTPCPreprocessorOffline::ReadGainGlobal(Char_t* fileName){
707   //
708   // read calibration entries from file
709   // 
710   TFile fcalib(fileName);
711   TObjArray * array = (TObjArray*)fcalib.Get("TPCCalib");
712   if (array){
713     fGainMIP    = ( AliTPCcalibTimeGain *)array->FindObject("calibTimeGain");
714     fGainCosmic = ( AliTPCcalibTimeGain *)array->FindObject("calibTimeGainCosmic");
715   }else{
716     fGainMIP    = ( AliTPCcalibTimeGain *)fcalib.Get("calibTimeGain");
717     fGainCosmic = ( AliTPCcalibTimeGain *)fcalib.Get("calibTimeGainCosmic");
718   }
719   TH1 * hisT=0;
720   Int_t firstBinA =0, lastBinA=0;
721
722   if (fGainCosmic){ 
723     hisT= fGainCosmic->GetHistGainTime()->Projection(1);
724     firstBinA = hisT->FindFirstBinAbove(2);
725     lastBinA  = hisT->FindLastBinAbove(2);    
726     fGainCosmic->GetHistGainTime()->GetAxis(1)->SetRange(firstBinA,lastBinA);
727     delete hisT;
728   }
729
730   if (fGainMIP){ 
731     hisT= fGainMIP->GetHistGainTime()->Projection(1);
732     firstBinA = hisT->FindFirstBinAbove(2);
733     lastBinA  = hisT->FindLastBinAbove(2);    
734     fGainMIP->GetHistGainTime()->GetAxis(1)->SetRange(firstBinA,lastBinA);
735     delete hisT;
736   }
737
738 }
739
740
741
742 Bool_t AliTPCPreprocessorOffline::AnalyzeGain(Int_t startRunNumber, Int_t endRunNumber, Int_t minEntriesGaussFit,  Float_t FPtoMIPratio){
743   //
744   //
745   //
746   fGainMIP->GetHistGainTime()->GetAxis(5)->SetRangeUser(startRunNumber, endRunNumber);
747   // 1.) try to create MIP spline
748   fGainMIP->GetHistGainTime()->GetAxis(2)->SetRangeUser(1.51,2.49); // only beam data
749   fGainMIP->GetHistGainTime()->GetAxis(4)->SetRangeUser(0.39,0.51); // only MIP pions
750   //
751   fGraphMIP = AliTPCcalibBase::FitSlices(fGainMIP->GetHistGainTime(),0,1,minEntriesGaussFit,10,0.1,0.7);
752   if (fGraphMIP->GetN()==0) fGraphMIP = 0x0;
753   if (fGraphMIP) fFitMIP = AliTPCcalibTimeGain::MakeSplineFit(fGraphMIP);
754   if (fGraphMIP) fGraphMIP->SetName("TGRAPHERRORS_MEAN_GAIN_BEAM_ALL");// set proper names according to naming convention
755   fGainArray->AddAt(fFitMIP,0);
756   
757
758   // 2.) try to create Cosmic spline
759   fGainCosmic->GetHistGainTime()->GetAxis(2)->SetRangeUser(0.51,1.49); // only cosmics
760   fGainCosmic->GetHistGainTime()->GetAxis(4)->SetRangeUser(20,100);    // only Fermi-Plateau muons
761   //
762   fGraphCosmic = AliTPCcalibBase::FitSlices(fGainCosmic->GetHistGainTime(),0,1,minEntriesGaussFit,10);
763   if (fGraphCosmic->GetN()==0) fGraphCosmic = 0x0;
764   //
765   if (fGraphCosmic) {
766     for(Int_t i=0; i < fGraphCosmic->GetN(); i++) {
767       fGraphCosmic->GetY()[i] /= FPtoMIPratio;
768       fGraphCosmic->GetEY()[i] /= FPtoMIPratio;
769     }
770   }
771   //
772   if (fGraphCosmic) fFitCosmic = AliTPCcalibTimeGain::MakeSplineFit(fGraphCosmic);
773   if (fGraphCosmic) fGraphCosmic->SetName("TGRAPHERRORS_MEAN_GAIN_COSMIC_ALL"); // set proper names according to naming convention
774   fGainArray->AddAt(fFitCosmic,1);
775   // with naming convention and backward compatibility
776   fGainArray->AddAt(fGraphMIP,2);
777   fGainArray->AddAt(fGraphCosmic,3);
778   cout << "fGraphCosmic: " << fGraphCosmic << " fGraphMIP " << fGraphMIP << endl;
779   return kTRUE;
780
781 }
782
783
784
785 void AliTPCPreprocessorOffline::UpdateOCDBGain(Int_t startRunNumber, Int_t endRunNumber, const Char_t *storagePath){
786   //
787   // Update OCDB entry
788   //
789   AliCDBMetaData *metaData= new AliCDBMetaData();
790   metaData->SetObjectClassName("TObjArray");
791   metaData->SetResponsible("Alexander Kalweit");
792   metaData->SetBeamPeriod(1);
793   metaData->SetAliRootVersion("05-24-00"); //root version
794   metaData->SetComment("Calibration of the time dependence of the gain due to pressure and temperature changes.");
795   AliCDBId id1("TPC/Calib/TimeGain", startRunNumber, endRunNumber);
796   AliCDBStorage * gStorage = AliCDBManager::Instance()->GetStorage(storagePath);
797   gStorage->Put(fGainArray, id1, metaData);    
798 }
799
800 void AliTPCPreprocessorOffline::MakeQAPlot(Float_t  FPtoMIPratio) {
801   //
802   // Make QA plot to visualize results
803   //
804   //
805   //
806   if (fGraphCosmic) {
807     TCanvas * canvasCosmic = new TCanvas("gain Cosmic", "time dependent gain QA histogram cosmic");
808     canvasCosmic->cd();
809     TH2D * gainHistoCosmic = fGainCosmic->GetHistGainTime()->Projection(0,1);
810     gainHistoCosmic->SetDirectory(0);
811     gainHistoCosmic->SetName("GainHistoCosmic");
812     gainHistoCosmic->GetXaxis()->SetTimeDisplay(kTRUE);
813     gainHistoCosmic->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}");
814     gainHistoCosmic->Draw("colz");
815     fGraphCosmic->SetMarkerStyle(25);
816     fGraphCosmic->Draw("lp");
817     fGraphCosmic->SetMarkerStyle(25);
818     TGraph * grfFitCosmic = fFitCosmic->MakeGraph(fGraphCosmic->GetX()[0],fGraphCosmic->GetX()[fGraphCosmic->GetN()-1],50000,0);
819     if (grfFitCosmic) {
820       for(Int_t i=0; i < grfFitCosmic->GetN(); i++) {
821         grfFitCosmic->GetY()[i] *= FPtoMIPratio;        
822       }
823       for(Int_t i=0; i < fGraphCosmic->GetN(); i++) {
824         fGraphCosmic->GetY()[i] *= FPtoMIPratio;        
825       }
826     }
827     fGraphCosmic->Draw("lp");
828     grfFitCosmic->SetLineColor(2);
829     grfFitCosmic->Draw("lu");
830     fGainArray->AddLast(gainHistoCosmic);
831     fGainArray->AddLast(canvasCosmic->Clone());
832     delete canvasCosmic;    
833   }
834   if (fFitMIP) {
835     TCanvas * canvasMIP = new TCanvas("gain MIP", "time dependent gain QA histogram MIP");
836     canvasMIP->cd();
837     TH2D * gainHistoMIP    = fGainMIP->GetHistGainTime()->Projection(0,1);
838     gainHistoMIP->SetName("GainHistoCosmic");
839     gainHistoMIP->SetDirectory(0);
840     gainHistoMIP->GetXaxis()->SetTimeDisplay(kTRUE);
841     gainHistoMIP->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}");
842     gainHistoMIP->Draw("colz");
843     fGraphMIP->SetMarkerStyle(25);
844     fGraphMIP->Draw("lp");
845     TGraph * grfFitMIP = fFitMIP->MakeGraph(fGraphMIP->GetX()[0],fGraphMIP->GetX()[fGraphMIP->GetN()-1],50000,0);
846     grfFitMIP->SetLineColor(2);
847     grfFitMIP->Draw("lu");    
848     fGainArray->AddLast(gainHistoMIP);
849     fGainArray->AddLast(canvasMIP->Clone());
850     delete canvasMIP;    
851   }  
852 }
853
854
855