]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG1/TRD/AliTRDcalibration.cxx
implement LocalInit function
[u/mrichter/AliRoot.git] / PWG1 / TRD / AliTRDcalibration.cxx
1
2 /**************************************************************************
3 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 *                                                                        *
5 * Author: The ALICE Off-line Project.                                    *
6 * Contributors are mentioned in the code where appropriate.              *
7 *                                                                        *
8 * Permission to use, copy, modify and distribute this software and its   *
9 * documentation strictly for non-commercial purposes is hereby granted   *
10 * without fee, provided that the above copyright notice appears in all   *
11 * copies and that both the copyright notice and this permission notice   *
12 * appear in the supporting documentation. The authors make no claims     *
13 * about the suitability of this software for any purpose. It is          *
14 * provided "as is" without express or implied warranty.                  *
15 **************************************************************************/
16
17 /////////////////////////////////////////////////////////////////////////////////
18 //                                                                             
19 // AliTRDcalibration                                                            
20 //                                                                             
21 // Task to run the calibration offline.
22 // Author:
23 //   R. Bailhache (rbailhache@ikf.uni-frankfurt.de, R.Bailhache@gsi.de)
24 //           
25 //////////////////////////////////////////////////////////////////////////////////
26
27
28 #include "Riostream.h"
29 #include "TChain.h"
30 #include "TTree.h"
31 #include "TProfile2D.h"
32 #include "TH2I.h"
33 #include "TH1F.h"
34 #include "TList.h"
35 #include "TMath.h"
36 #include "TCanvas.h"
37 #include "TObject.h"
38 #include "TFile.h"
39 #include "TObjArray.h"
40 #include "TGraph.h"
41 #include "TStyle.h"
42 #include "TLegend.h"
43 #include "TGraphErrors.h"
44
45 #include "AliTRDrecoTask.h"
46 #include "AliAnalysisManager.h"
47
48 #include "AliESDInputHandler.h"
49 #include "AliTRDtrackV1.h"
50 #include "AliTRDseedV1.h"
51 #include "AliTRDcluster.h"
52 #include "info/AliTRDtrackInfo.h"
53 #include "AliTRDcalibDB.h"
54
55 #include "AliTRDCalibraFillHisto.h"
56 #include "AliTRDCalibraFit.h"
57 #include "AliTRDCalibraVdriftLinearFit.h"
58 #include "AliTRDCalibraMode.h"
59 #include "AliTRDCalibraVector.h"
60 #include "./Cal/AliTRDCalPad.h"
61 #include "./Cal/AliTRDCalDet.h"
62
63 #include "AliLog.h"
64
65 #include "AliTRDcalibration.h"
66
67
68 ClassImp(AliTRDcalibration)
69
70 //________________________________________________________________________
71 AliTRDcalibration::AliTRDcalibration() 
72   :AliTRDrecoTask()
73   ,fTrackInfo(0)
74   ,ftrdTrack(0)
75   ,fcl(0)
76   ,fTRDCalibraFillHisto(0)
77   ,fNbTRDTrack(0)
78   ,fNbTRDTrackOffline(0)
79   ,fNbTRDTrackStandalone(0)
80   ,fNbTRDTracklet(0)
81   ,fNbTRDTrackletOffline(0)
82   ,fNbTRDTrackletStandalone(0)
83   ,fNbTimeBin(0x0)
84   ,fNbTimeBinOffline(0x0)
85   ,fNbTimeBinStandalone(0x0)
86   ,fNbClusters(0)
87   ,fNbClustersOffline(0)
88   ,fNbClustersStandalone(0)
89   ,fPHSM(0)
90   ,fCHSM(0)
91   ,fPHSum(0)
92   ,fCHSum(0)
93   ,fDetSum(0)
94   ,fDetSumVector(0)
95   ,fHisto2d(kTRUE)
96   ,fVector2d(kFALSE)
97   ,fVdriftLinear(kTRUE)
98   ,flow(0)
99   ,fhigh(30)
100   ,fNbTimeBins(0)
101   ,ffillZero(kFALSE)
102   ,fnormalizeNbOfCluster(kFALSE)
103   ,fmaxCluster(0)
104   ,fOfflineTracks(kFALSE)
105   ,fStandaloneTracks(kFALSE)
106   ,fCompressPerDetector(kFALSE)
107   ,fGraph(0x0)
108   ,fArrayCalib(0x0)
109   ,fPostProcess(kFALSE)
110 {
111   // Constructor
112   
113   fNRefFigures = 17;
114
115   for(Int_t k = 0; k < 3; k++)
116     {
117       fNz[k]=0;
118       fNrphi[k]=0;
119     }
120
121 }  
122
123 AliTRDcalibration::AliTRDcalibration(char* name) 
124   :AliTRDrecoTask(name, "Calibration on tracks")
125   ,fTrackInfo(0)
126   ,ftrdTrack(0)
127   ,fcl(0)
128   ,fTRDCalibraFillHisto(0)
129   ,fNbTRDTrack(0)
130   ,fNbTRDTrackOffline(0)
131   ,fNbTRDTrackStandalone(0)
132   ,fNbTRDTracklet(0)
133   ,fNbTRDTrackletOffline(0)
134   ,fNbTRDTrackletStandalone(0)
135   ,fNbTimeBin(0x0)
136   ,fNbTimeBinOffline(0x0)
137   ,fNbTimeBinStandalone(0x0)
138   ,fNbClusters(0)
139   ,fNbClustersOffline(0)
140   ,fNbClustersStandalone(0)
141   ,fPHSM(0)
142   ,fCHSM(0)
143   ,fPHSum(0)
144   ,fCHSum(0)
145   ,fDetSum(0)
146   ,fDetSumVector(0)
147   ,fHisto2d(kTRUE)
148   ,fVector2d(kFALSE)
149   ,fVdriftLinear(kTRUE)
150   ,flow(0)
151   ,fhigh(30)
152   ,fNbTimeBins(0)
153   ,ffillZero(kFALSE)
154   ,fnormalizeNbOfCluster(kFALSE)
155   ,fmaxCluster(0)
156   ,fOfflineTracks(kFALSE)
157   ,fStandaloneTracks(kFALSE)
158   ,fCompressPerDetector(kFALSE)
159   ,fGraph(0x0)
160   ,fArrayCalib(0x0)
161   ,fPostProcess(kFALSE)
162 {
163   // Constructor
164   
165   fNRefFigures = 17;
166
167   for(Int_t k = 0; k < 3; k++)
168     {
169       fNz[k]=0;
170       fNrphi[k]=0;
171     }
172
173 }  
174
175 //________________________________________________________________________
176 AliTRDcalibration::~AliTRDcalibration() 
177 {
178   // Default destructor
179
180   if(fNbTRDTrack) delete fNbTRDTrack;
181   if(fNbTRDTrackOffline) delete fNbTRDTrackOffline;
182   if(fNbTRDTrackStandalone) delete fNbTRDTrackStandalone;
183   if(fNbTRDTracklet) delete fNbTRDTracklet;
184   if(fNbTRDTrackletOffline) delete fNbTRDTrackletOffline;
185   if(fNbTRDTrackletStandalone) delete fNbTRDTrackletStandalone;
186   if(fNbTimeBin) delete fNbTimeBin;
187   if(fNbTimeBinOffline) delete fNbTimeBinOffline;
188   if(fNbTimeBinStandalone) delete fNbTimeBinStandalone;
189   if(fNbClusters) delete fNbClusters;
190   if(fNbClustersOffline) delete fNbClustersOffline;
191   if(fNbClustersStandalone) delete fNbClustersStandalone;
192   if(fPHSM) delete fPHSM;
193   if(fCHSM) delete fCHSM;
194   if(fPHSum) delete fPHSum;
195   if(fCHSum) delete fCHSum;
196   if(fDetSum) delete fDetSum;
197   if(fDetSumVector) delete fDetSumVector;
198   if(fGraph){fGraph->Delete(); delete fGraph;}
199   if(fArrayCalib){fArrayCalib->Delete(); delete fArrayCalib;}
200    
201 }
202 //________________________________________________________________________
203 void AliTRDcalibration::UserCreateOutputObjects() 
204 {
205   // Create output objects
206
207   OpenFile(1, "RECREATE");
208   
209   // Number of time bins
210   if(fNbTimeBins==0) {
211     AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
212     fNbTimeBins = cal->GetNumberOfTimeBinsDCS();
213     if(fNbTimeBins <= 0){ 
214       AliWarning(Form("No of TimeBins from DB [%d] use default [30]", fNbTimeBins));
215       fNbTimeBins = 30;
216     }
217   }
218   
219   // instance calibration: what to calibrate
220   fTRDCalibraFillHisto = AliTRDCalibraFillHisto::Instance();
221   fTRDCalibraFillHisto->SetHisto2d(fHisto2d); // choose to use histograms
222   fTRDCalibraFillHisto->SetVector2d(fVector2d); // choose to use vectors
223   fTRDCalibraFillHisto->SetCH2dOn();  // choose to calibrate the gain
224   fTRDCalibraFillHisto->SetPH2dOn();  // choose to calibrate the drift velocity
225   fTRDCalibraFillHisto->SetPRF2dOn(); // choose to look at the PRF
226   fTRDCalibraFillHisto->SetLinearFitterOn(fVdriftLinear); // Other possibility vdrift VDRIFT
227   fTRDCalibraFillHisto->SetLinearFitterDebugOn(fVdriftLinear); // Other possibility vdrift
228
229   // segmentation (should be per default the max and add at the end)
230   for(Int_t k = 0; k < 3; k++){
231     if(((fNz[k] != 10) && (fNrphi[k] != 10)) && ((fNz[k] != 100) && (fNrphi[k] != 100))) {
232       fTRDCalibraFillHisto->SetNz(k,fNz[k]);                                    // Mode calibration
233       fTRDCalibraFillHisto->SetNrphi(k,fNrphi[k]);                             // Mode calibration
234     }
235     else {
236       if((fNz[k] == 100) && (fNrphi[k] == 100))  {
237         if(fVector2d) AliInfo("The mode all together is not supported by the vector method");
238         fTRDCalibraFillHisto->SetAllTogether(k);
239       }
240       if((fNz[k] == 10) && (fNrphi[k] == 10))  {
241         if(fVector2d) AliInfo("The mode per supermodule is not supported by the vector method");
242         fTRDCalibraFillHisto->SetPerSuperModule(k);
243       }
244     }
245   }
246
247   // Debug level
248   fTRDCalibraFillHisto->SetDebugLevel(DebugLevel()); //debug stuff
249
250   // Init the stuff
251   fTRDCalibraFillHisto->Init2Dhistos(fNbTimeBins); // initialise the histos
252
253   // cuts
254   fTRDCalibraFillHisto->SetNumberClusters(flow); // At least flow clusters
255   fTRDCalibraFillHisto->SetNumberClustersf(fhigh); // The more fhigh clusters
256   fTRDCalibraFillHisto->SetFillWithZero(ffillZero); // Fill zeros
257   fTRDCalibraFillHisto->SetNormalizeNbOfCluster(fnormalizeNbOfCluster); // For iterations
258
259   // Add them to the container
260   fContainer = new TObjArray();
261   if(fHisto2d) {
262     fContainer->Add(fTRDCalibraFillHisto->GetCH2d()); //TH2I
263     fContainer->Add(fTRDCalibraFillHisto->GetPH2d()); //TProfile2D
264     fContainer->Add(fTRDCalibraFillHisto->GetPRF2d()); //TProfile2D
265   }
266   if(fVdriftLinear) fContainer->Add(fTRDCalibraFillHisto->GetVdriftLinearFit()); // Other drift velocity 
267   if(fVector2d) fContainer->Add(fTRDCalibraFillHisto->GetCalibraVector()); //calibra vector
268       
269   if(DebugLevel()) {
270     
271     // Init the debug histos
272     fNbTRDTrack = new TH1F("TRDTrack","TRDTrack",500,0,500);
273     fNbTRDTrack->Sumw2();
274     fNbTRDTrackOffline = new TH1F("TRDTrackOffline","TRDTrackOffline",500,0,500);
275     fNbTRDTrackOffline->Sumw2();
276     fNbTRDTrackStandalone = new TH1F("TRDTrackStandalone","TRDTrackStandalone",500,0,500);
277     fNbTRDTrackStandalone->Sumw2();
278     //
279     fNbTRDTracklet = new TH1F("TRDTracklet","TRDTracklet",540,0.,540.);
280     fNbTRDTracklet->Sumw2();
281     fNbTRDTrackletOffline = new TH1F("TRDTrackletOffline","TRDTrackletOffline",540,0.,540.);
282     fNbTRDTrackletOffline->Sumw2();
283     fNbTRDTrackletStandalone = new TH1F("TRDTrackletStandalone","TRDTrackletStandalone",540,0.,540.);
284     fNbTRDTrackletStandalone->Sumw2();
285     //
286     fNbTimeBin = new TH1F("TimeBin","TimeBin",35,0,35);
287     fNbTimeBin->Sumw2();
288     fNbTimeBinOffline = new TH1F("TimeBinOffline","TimeBinOffline",35,0,35);
289     fNbTimeBinOffline->Sumw2();
290     fNbTimeBinStandalone = new TH1F("TimeBinStandalone","TimeBinStandalone",35,0,35);
291     fNbTimeBinStandalone->Sumw2();
292     //
293     fNbClusters = new TH1F("NbClusters","",35,0,35);
294     fNbClusters->Sumw2();
295     fNbClustersOffline = new TH1F("NbClustersOffline","",35,0,35);
296     fNbClustersOffline->Sumw2();
297     fNbClustersStandalone = new TH1F("NbClustersStandalone","",35,0,35);
298     fNbClustersStandalone->Sumw2();
299     //
300     fPHSM = new TProfile2D("PH2dSM","Nz10Nrphi10"
301                             ,fNbTimeBins,-0.05,(Double_t)((fNbTimeBins-0.5)/10.0)
302                             ,18,0,18);
303     fPHSM->SetYTitle("Det/pad groups");
304     fPHSM->SetXTitle("time [#mus]");
305     fPHSM->SetZTitle("<PH> [a.u.]");
306     fPHSM->SetStats(0);
307     //
308     fCHSM = new TH2I("CH2dSM","Nz10Nrphi10",50,0,300,18,0,18);
309     fCHSM->SetYTitle("Det/pad groups");
310     fCHSM->SetXTitle("charge deposit [a.u]");
311     fCHSM->SetZTitle("counts");
312     fCHSM->SetStats(0);
313     fCHSM->Sumw2();
314     //
315     fPHSum = new TProfile2D("PH2dSum","Nz100Nrphi100"
316                             ,fNbTimeBins,-0.05,(Double_t)((fNbTimeBins-0.5)/10.0)
317                             ,1,0,1);
318     fPHSum->SetYTitle("Det/pad groups");
319     fPHSum->SetXTitle("time [#mus]");
320     fPHSum->SetZTitle("<PH> [a.u.]");
321     fPHSum->SetStats(0);
322     //
323     fCHSum = new TH2I("CH2dSum","Nz100Nrphi100",50,0,300,1,0,1);
324     fCHSum->SetYTitle("Det/pad groups");
325     fCHSum->SetXTitle("charge deposit [a.u]");
326     fCHSum->SetZTitle("counts");
327     fCHSum->SetStats(0);
328     fCHSum->Sumw2();
329     
330     // Add them
331     fContainer->Add(fNbTRDTrack);
332     fContainer->Add(fNbTRDTrackOffline);
333     fContainer->Add(fNbTRDTrackStandalone);
334     fContainer->Add(fNbTRDTracklet);
335     fContainer->Add(fNbTRDTrackletOffline);
336     fContainer->Add(fNbTRDTrackletStandalone);
337     fContainer->Add(fNbTimeBin);
338     fContainer->Add(fNbTimeBinOffline);
339     fContainer->Add(fNbTimeBinStandalone);
340     fContainer->Add(fNbClusters);
341     fContainer->Add(fNbClustersOffline);
342     fContainer->Add(fNbClustersStandalone);
343     fContainer->Add(fPHSM);
344     fContainer->Add(fCHSM);
345     fContainer->Add(fPHSum);
346     fContainer->Add(fCHSum);
347
348   }
349
350 }
351
352 //________________________________________________________________________
353 void AliTRDcalibration::UserExec(Option_t *) 
354 {
355   //
356   // Execute function where the reference data are filled
357   //
358
359   if(!fTracks) return;
360   
361   // In total
362   Int_t nbTrdTracks = 0;
363   // standalone
364   Int_t nbTrdTracksStandalone = 0;
365   // offline
366   Int_t nbTrdTracksOffline = 0;
367   
368
369   //
370   // Loop on track in the event
371   //
372   //printf("Total of %d\n",fTracks->GetEntriesFast());
373   for(Int_t itrk=0; itrk < fTracks->GetEntriesFast(); itrk++){
374     
375     //printf("itrk %d\n",itrk);
376
377     fTrackInfo = (AliTRDtrackInfo*)fTracks->UncheckedAt(itrk);
378     ftrdTrack = fTrackInfo->GetTrack();
379     if(!ftrdTrack) continue;
380
381     nbTrdTracks++;
382   
383     fTRDCalibraFillHisto->UpdateHistogramsV1(ftrdTrack);
384
385     if(DebugLevel()) {
386       
387       Bool_t standalonetracklet = kFALSE;  
388       const AliTRDseedV1 *tracklet = 0x0;
389       //
390       // Loop on tracklet in the event
391       //
392       for(Int_t itr = 0; itr < 6; itr++){
393         //printf("itr %d\n",itr);
394         if(!(tracklet = ftrdTrack->GetTracklet(itr))) continue;
395         if(!tracklet->IsOK()) continue;
396         // standalone
397         if(tracklet->IsStandAlone()) standalonetracklet = kTRUE;
398         Int_t nbclusters = 0;
399         // For PH
400         Double_t phtb[AliTRDseedV1::kNtb];
401         memset(phtb, 0, AliTRDseedV1::kNtb*sizeof(Double_t));
402         // For CH
403         Double_t sum = 0.0;
404         // normalisation
405         Float_t normalisation = 6.67;
406         Int_t detector = 0;
407         Int_t sector = 0;
408         Int_t crossrow = 0;
409         // Check no shared clusters
410         for(int icc=AliTRDseedV1::kNtb; icc<AliTRDseedV1::kNclusters; icc++){
411           if((fcl = tracklet->GetClusters(icc)))  crossrow = 1;
412         }
413         // Loop on clusters
414         for(int ic=0; ic<AliTRDseedV1::kNtb; ic++){
415           if(!(fcl = tracklet->GetClusters(ic))) continue;
416           nbclusters++;
417           Int_t time = fcl->GetPadTime();
418           Float_t ch =  tracklet->GetdQdl(ic);
419           Float_t qcl = TMath::Abs(fcl->GetQ());
420           detector = fcl->GetDetector();          
421           if((time>-1) && (time<fNbTimeBins)) phtb[time]=qcl;
422           sum += ch/normalisation;
423           fNbTimeBin->Fill(time);
424           if(tracklet->IsStandAlone()) fNbTimeBinStandalone->Fill(time);
425           else fNbTimeBinOffline->Fill(time);
426         }
427         sector = AliTRDgeometry::GetSector(detector);
428
429         fNbTRDTracklet->Fill(detector);
430         if(tracklet->IsStandAlone()) fNbTRDTrackletStandalone->Fill(detector);
431         else fNbTRDTrackletOffline->Fill(detector);
432         
433         fNbClusters->Fill(nbclusters);
434         if(tracklet->IsStandAlone())  fNbClustersStandalone->Fill(nbclusters);
435         else  fNbClustersOffline->Fill(nbclusters);
436         
437         if((nbclusters > flow) && (nbclusters < fhigh)){
438           fCHSM->Fill(sum/20.0,sector+0.5);
439           fCHSum->Fill(sum/20.0,0.5);
440           for(int ic=0; ic<fNbTimeBins; ic++){
441             if(ffillZero) {
442               fPHSum->Fill((Double_t)(ic/10.0),0.5,(Double_t)phtb[ic]);
443               fPHSM->Fill((Double_t)(ic/10.0),sector+0.5,(Double_t)phtb[ic]);
444             }
445             else {
446               if(phtb[ic] > 0.0) {
447                 fPHSum->Fill((Double_t)(ic/10.0),0.0,(Double_t)phtb[ic]);
448                 fPHSM->Fill((Double_t)(ic/10.0),sector+0.5,(Double_t)phtb[ic]);
449               }
450             }
451           }
452         }
453       }
454     
455     if(standalonetracklet) nbTrdTracksStandalone++;
456     else nbTrdTracksOffline++;
457     
458     }
459     
460   }
461   
462   if(DebugLevel()) {
463     
464     //Fill Histos
465     fNbTRDTrack->Fill(nbTrdTracks);
466     fNbTRDTrackStandalone->Fill(nbTrdTracksStandalone);
467     fNbTRDTrackOffline->Fill(nbTrdTracksOffline);
468     
469   }
470
471   //printf("Nbof tracks %d\n",nbTrdTracks);
472   
473   // Post output data
474   PostData(1, fContainer);
475
476   //printf("post container\n");
477   
478 }      
479 //________________________________________________________________________
480 void AliTRDcalibration::Terminate(Option_t *) 
481 {
482   // Draw result to the screen
483   // Called once at the end of the query
484
485   //printf("terminate\n");
486
487   if(fTRDCalibraFillHisto) fTRDCalibraFillHisto->DestroyDebugStreamer();
488  
489 }
490 //________________________________________________________
491 Bool_t AliTRDcalibration::GetRefFigure(Int_t ifig)
492 {
493   //
494   // Draw filled histos
495   //
496   
497   gStyle->SetPalette(1);
498   gStyle->SetOptStat(1111);
499   gStyle->SetPadBorderMode(0);
500   gStyle->SetCanvasColor(10);
501   gStyle->SetPadLeftMargin(0.13);
502   gStyle->SetPadRightMargin(0.13);
503
504   if(!fContainer) return kFALSE;
505   
506   switch(ifig){
507   case kNbTrack:{
508     TCanvas *c0 = new TCanvas("c0","c0",10,10,510,510);
509     TLegend *legNbTrack = new TLegend(.7, .7, .98, .98);
510     legNbTrack->SetBorderSize(1);
511     TH1F *h  = 0x0;
512     TH1F *ho = 0x0;
513     TH1F *hs = 0x0;
514     if(!(h = (TH1F *)fContainer->FindObject("TRDTrack"))) break;
515     if(!(ho = (TH1F *)fContainer->FindObject("TRDTrackOffline"))) break;
516     if(!(hs = (TH1F *)fContainer->FindObject("TRDTrackStandalone"))) break;
517     c0->cd();
518     //gPad->SetLogy();
519     gPad->SetGridy();
520     gPad->SetGridx();
521     h->Draw();
522     ho->Draw("same");
523     hs->Draw("same");
524     legNbTrack->AddEntry(h, "all", "p");
525     legNbTrack->AddEntry(ho, "offline", "p");
526     legNbTrack->AddEntry(hs, "standalone", "p");
527     legNbTrack->Draw("same");
528     return kTRUE;
529   }
530   case kNbTracklet:{
531     TLegend *legNbTracklet = new TLegend(.7, .7, .98, .98);
532     legNbTracklet->SetBorderSize(1);
533     TH1F *h = 0x0;
534     TH1F *ho = 0x0;
535     TH1F *hs = 0x0;
536     if(!(h = (TH1F *)fContainer->FindObject("TRDTracklet"))) break;
537     if(!(ho = (TH1F *)fContainer->FindObject("TRDTrackletOffline"))) break;
538     if(!(hs = (TH1F *)fContainer->FindObject("TRDTrackletStandalone"))) break;
539     h->Draw();
540     ho->Draw("same");
541     hs->Draw("same");
542     legNbTracklet->AddEntry(h, "all", "p");
543     legNbTracklet->AddEntry(ho, "offline", "p");
544     legNbTracklet->AddEntry(hs, "standalone", "p");
545     legNbTracklet->Draw("same");
546     gPad->SetLogy();
547     gPad->SetGridy();
548     gPad->SetGridx();
549     return kTRUE;
550   }
551   case kNbTimeBin:{
552     TLegend *legNbTimeBin = new TLegend(.7, .7, .98, .98);
553     legNbTimeBin->SetBorderSize(1);
554     TH1F *h = 0x0;
555     TH1F *ho = 0x0;
556     TH1F *hs = 0x0;
557     if(!(h = (TH1F *)fContainer->FindObject("TimeBin"))) break;
558     if(!(ho = (TH1F *)fContainer->FindObject("TimeBinOffline"))) break;
559     if(!(hs = (TH1F *)fContainer->FindObject("TimeBinStandalone"))) break;
560     h->Draw();
561     ho->Draw("same");
562     hs->Draw("same");
563     legNbTimeBin->AddEntry(h, "all", "p");
564     legNbTimeBin->AddEntry(ho, "offline", "p");
565     legNbTimeBin->AddEntry(hs, "standalone", "p");
566     legNbTimeBin->Draw("same");
567     //gPad->SetLogy();
568     gPad->SetGridy();
569     gPad->SetGridx();
570     return kTRUE;
571   }
572   case kNbClusters:{
573     TLegend *legNbClusters = new TLegend(.7, .7, .98, .98);
574     legNbClusters->SetBorderSize(1);
575     TH1F *h = 0x0;
576     TH1F *ho = 0x0;
577     TH1F *hs = 0x0;
578     if(!(h = (TH1F *)fContainer->FindObject("NbClusters"))) break;
579     if(!(ho = (TH1F *)fContainer->FindObject("NbClustersOffline"))) break;
580     if(!(hs = (TH1F *)fContainer->FindObject("NbClustersStandalone"))) break;
581     h->Draw();
582     ho->Draw("same");
583     hs->Draw("same");
584     legNbClusters->AddEntry(h, "all", "p");
585     legNbClusters->AddEntry(ho, "offline", "p");
586     legNbClusters->AddEntry(hs, "standalone", "p");
587     legNbClusters->Draw("same");
588     gPad->SetLogy();
589     gPad->SetGridy();
590     gPad->SetGridx();
591     return kTRUE;
592   }
593   case kPHSum:{
594     TProfile2D *h = 0x0;
595     if(!(h = (TProfile2D *)fContainer->FindObject("PH2dSum"))) break;
596     TH1D *projh = h->ProjectionX("projh",1,1,"e");
597     projh->Draw();
598     gPad->SetGridy();
599     gPad->SetGridx();
600     return kTRUE;
601   }
602   case kCHSum:{
603     TH2I *h = 0x0;
604     if(!(h = (TH2I *)fContainer->FindObject("CH2dSum"))) break;
605     TH1D *projh = h->ProjectionX("projhh",1,1,"e");
606     projh->Draw();
607     gPad->SetGridy();
608     gPad->SetGridx();
609     return kTRUE;
610   }
611   case kPH2D:{
612     if(!fHisto2d) {
613       AliInfo("Histo was not filled!");
614       break;
615     }
616     TProfile2D *h = 0x0;
617     if(!(h = (TProfile2D *)fContainer->FindObject("PH2d"))) break;
618     h->Draw("lego");
619     return kTRUE;
620   }
621   case kCH2D:{
622     if(!fHisto2d) {
623       AliInfo("Histo was not filled!");
624       break;
625     }
626     TH2I *h = 0x0;
627     if(!(h = (TH2I *)fContainer->FindObject("CH2d"))) break;
628     h->Draw("lego");
629     return kTRUE;
630   }
631   case kPRF2D:{
632     if(!fHisto2d) {
633       AliInfo("Histo was not filled!");
634       break;
635     }
636     TProfile2D *h = 0x0;
637     if(!(h = (TProfile2D *)fContainer->FindObject("PRF2d"))) break;
638     h->Draw("lego");
639     return kTRUE;
640   }
641   case kPH2DVector:{
642     if(!fVector2d) {
643       AliInfo("vector was not filled!");
644       break;
645     }
646     AliTRDCalibraVector *v = 0x0;
647     TGraphErrors *vdet = 0x0; 
648     if(!(v = (AliTRDCalibraVector *)fContainer->FindObject("AliTRDCalibraVector"))) break;
649     Int_t detectormax = -1;
650     Int_t groupmax    = -1;
651     if(!v->FindTheMaxEntries(1,detectormax,groupmax)) break;
652     if(!(vdet = v->ConvertVectorPHTGraphErrors((Int_t)detectormax,groupmax,"plotPH2dVector"))) break;
653     Int_t nbeentries = 0;
654     TH1F *ko = v->CorrectTheError(vdet,nbeentries);
655     ko->Draw();
656     AliInfo(Form("There are %d entries in the detector %d and group %d",nbeentries,detectormax,groupmax));
657     return kTRUE;
658   }
659 case kCH2DVector:{
660     if(!fVector2d) {
661       AliInfo("vector was not filled!");
662       break;
663     }
664     AliTRDCalibraVector *v = 0x0;
665     TH1F *vdet = 0x0; 
666     if(!(v = (AliTRDCalibraVector *)fContainer->FindObject("AliTRDCalibraVector"))) break;
667     Int_t detectormax = -1;
668     Int_t groupmax    = -1;
669     if(!v->FindTheMaxEntries(0,detectormax,groupmax)) break;
670     if(!(vdet = v->ConvertVectorCHHisto((Int_t)detectormax,groupmax,"plotCH2dVector"))) break;
671     vdet->Draw();
672     AliInfo(Form("The detectormax and groupmax are %d and %d",detectormax,groupmax));
673     return kTRUE;
674   }
675   case kPRF2DVector:{
676     if(!fVector2d) {
677       AliInfo("vector was not filled!");
678       break;
679     }
680     AliTRDCalibraVector *v = 0x0;
681     TGraphErrors *vdet = 0x0; 
682     if(!(v = (AliTRDCalibraVector *)fContainer->FindObject("AliTRDCalibraVector"))) break;
683     Int_t detectormax  = -1;
684     Int_t groupmax     = -1;
685     Int_t nbeentries   = 0;
686     if(!v->FindTheMaxEntries(2,detectormax,groupmax)) break;
687     if(!(vdet = v->ConvertVectorPRFTGraphErrors((Int_t)detectormax,groupmax,"plotPRF2dVector"))) break;
688     TH1F *ko = v->CorrectTheError(vdet,nbeentries);
689     ko->Draw();
690     AliInfo(Form("The detectormax and groupmax are %d and %d",detectormax,groupmax));
691     return kTRUE;
692   }
693   case kLinearFitter:{
694     if(!fVdriftLinear) {
695       AliInfo("vdrift linear was not filled!");
696       break;
697     }
698     AliTRDCalibraVdriftLinearFit *h = 0x0;
699     TH2S *hdetector = 0x0; 
700     if(!(h = (AliTRDCalibraVdriftLinearFit *)fContainer->FindObject("AliTRDCalibraVdriftLinearFit"))) break;
701     Double_t entries[540];
702     for(Int_t k = 0; k < 540; k++){
703       entries[k] = 0.0;
704       hdetector = 0x0;
705       if(!(hdetector = (TH2S *)h->GetLinearFitterHisto(k,kFALSE))) continue;
706       entries[k] = hdetector->GetEntries();
707     }
708     Double_t max = -10.0;
709     Double_t detectormax = -1;
710     for(Int_t k = 0; k < 540; k++){
711       if(entries[k] > max) {
712         max = entries[k];
713         detectormax = k;
714       }
715     }
716     hdetector = 0x0;
717     if((TMath::Abs(max) <= 0.001) || (detectormax <0.0) || (detectormax >=540.0)) break;
718     if(!(hdetector = (TH2S *)h->GetLinearFitterHisto((Int_t)detectormax,kFALSE))) break;
719     AliInfo(Form("The detector with the maximum of entries is %d",detectormax));
720     hdetector->Draw();
721     return kTRUE;
722   }
723   case kGainFactor:{
724     if(!fPostProcess){
725       if(!PostProcess()) break;
726     }
727     TGraph *fgain = (TGraph *) fGraph->At(0);
728     if(!fgain) break;
729     fgain->Draw("ALP");
730     return kTRUE;
731   }
732   case kVdriftT0Factor:{
733     if(!fPostProcess){
734       if(!PostProcess()) break;
735     }
736     TCanvas *c = new TCanvas("c","c",10,10,510,510);
737     c->Divide(2,1);
738     TGraph *fvd = (TGraph *) fGraph->At(1);
739     if(fvd){
740       c->cd(1);
741       fvd->Draw("ALP");
742     } 
743     TGraph *ft0 = (TGraph *) fGraph->At(2);
744     if(ft0){
745       c->cd(2);
746       ft0->Draw("ALP");
747     } 
748     return kTRUE;
749   }
750   case kVdriftLorentzAngleFactor:{
751     if(!fVdriftLinear) {
752       AliInfo("vdrift linear was not filled!");
753       break;
754     }
755     if(!fPostProcess){
756       if(!PostProcess()) break;
757     }
758     TCanvas *c = new TCanvas("c","c",10,10,510,510);
759     c->Divide(2,1);
760     TGraph *fvdl = (TGraph *) fGraph->At(3);
761     if(fvdl){
762       c->cd(1);
763       fvdl->Draw("ALP");
764     } 
765     TGraph *flal = (TGraph *) fGraph->At(4);
766     if(flal){
767       c->cd(2);
768       flal->Draw("ALP");
769     } 
770     return kTRUE;
771   }
772   case kPRFFactor:{
773     if(!fPostProcess){
774       if(!PostProcess()) break;
775     }
776     TGraph *fprf = (TGraph *) fGraph->At(5);
777     if(!fprf) break;
778     fprf->Draw("ALP");
779     return kTRUE;
780   }
781   }
782   
783   return kFALSE;
784   
785 }
786 //________________________________________________________________________
787 Bool_t AliTRDcalibration::PostProcess()
788 {
789   // 
790   // Fit the filled histos
791   // Put the calibration object in fArrayCalib
792   // 0 and 1 AliTRDCalDet and AliTRDCalPad gain
793   // 2 and 3 AliTRDCalDet and AliTRDCalPad vdrift PH
794   // 4 and 5 AliTRDCalDet and AliTRDCalPad timeoffset PH
795   // 6 AliTRDCalPad PRF
796   // 7 and 8 AliTRDCalDet and AliTRDCalPad vdrift second
797   // 9 and 10 AliTRDCalDet and AliTRDCalPad lorentz angle second
798   //
799
800   if(!fArrayCalib){
801     fArrayCalib = new TObjArray(11);
802     fArrayCalib->SetOwner();
803   }
804   else {
805     delete fArrayCalib;
806     PostProcess();
807   }
808   
809   if(!fGraph){
810     fGraph = new TObjArray(6);
811     fGraph->SetOwner();
812   }
813   else {
814     delete fGraph;
815     PostProcess();
816   }
817
818   Bool_t storage[3] = {kFALSE,kFALSE,kFALSE};
819
820   // Objects for fitting
821   AliTRDCalibraFit *calibra = AliTRDCalibraFit::Instance();
822   calibra->SetDebugLevel(2); // 0 rien, 1 fitvoir, 2 debug files, 3 one detector  
823   
824   // Take the stuff
825   if (!fContainer) {
826     Printf("ERROR: list not available");
827     return kFALSE;
828   }
829
830   if(fHisto2d && fVector2d) AliInfo("We will only look at histos. Set fHisto2d off if you don't want");
831   AliTRDCalibraVector *calibraVector = 0x0;
832   if(fVector2d) calibraVector = (AliTRDCalibraVector *) fContainer->FindObject("CalibraVector");
833   //
834   // GAIN TH2I
835   //
836   Bool_t pass = kFALSE; 
837   AliTRDCalibraVector *vvect = 0x0;
838   if(fHisto2d) {
839     TH2I *histogain = (TH2I *) fContainer->FindObject("CH2d");  
840     if(histogain) {
841       histogain->SetDirectory(0);
842       calibra->SetMinEntries(20); 
843       if(fCompressPerDetector){
844         if(AddStatsPerDetector(histogain)) pass = calibra->AnalyseCH(fCHSum);
845       }
846       else pass = calibra->AnalyseCH(histogain);
847     }
848   }
849   else {
850     if(fVector2d && calibraVector) {
851       calibra->SetMinEntries(20); 
852       if(fCompressPerDetector){
853         if(!(vvect = calibraVector->AddStatsPerDetectorCH())) return kFALSE;
854         pass = calibra->AnalyseCH(vvect);
855       }
856       else pass = calibra->AnalyseCH(calibraVector);
857     }
858   }
859   
860   if(pass)
861     {
862       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(0))
863         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(0));
864       Int_t nbfit = calibra->GetNumberFit();  //Number of fits
865       Int_t nbE   = calibra->GetNumberEnt();  //Number of detector mit entries
866       // enough statistics
867       if ((nbtg >                  0) && 
868           (nbfit        >= 0.001*nbE))
869         {
870           // create the cal objects
871           calibra->PutMeanValueOtherVectorFit(1,kTRUE); 
872           TObjArray object           = calibra->GetVectorFit();
873           AliTRDCalDet *objgaindet   = calibra->CreateDetObjectGain(&object);
874           TObject *objgainpad        = calibra->CreatePadObjectGain();
875           // store
876           fArrayCalib->AddAt(objgaindet,0);
877           fArrayCalib->AddAt(objgainpad,1);
878           storage[0] = kTRUE;
879           // Make graph
880           TGraph *graph = 0x0;
881           if(FillGraphIndex(&object,graph)){ 
882             fGraph->AddAt(graph,0);
883           }
884         }//if(enough statistics?)
885       calibra->ResetVectorFit();
886     }
887   else return kFALSE;
888   
889   //
890   // VDRIFT average pulse height
891   //
892   pass = kFALSE; 
893   if(fHisto2d) {
894     TProfile2D *histodriftvelocity = (TProfile2D *) fContainer->FindObject("PH2d");  
895     if(histodriftvelocity) {
896       histodriftvelocity->SetDirectory(0);  
897       calibra->SetMinEntries(20*20);  
898       if(fCompressPerDetector){
899         if(AddStatsPerDetector(histodriftvelocity,1)) {
900           pass = calibra->AnalysePH(fDetSumVector);
901         }
902       }
903       else pass = calibra->AnalysePH(histodriftvelocity);
904     }
905   }
906   else {
907     if(fVector2d && calibraVector) {
908       calibra->SetMinEntries(20*20);  
909       if(fCompressPerDetector){
910         if(!(vvect = calibraVector->AddStatsPerDetectorPH())) return kFALSE;
911         pass = calibra->AnalysePH(vvect);
912       }
913       else pass = calibra->AnalysePH(calibraVector);  
914     }
915   }
916
917   if(pass) {
918     Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(1))
919       + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(1));
920     Int_t nbfit  = calibra->GetNumberFit();
921     Int_t nbE    = calibra->GetNumberEnt();
922     // enough statistics
923     if ((nbtg >                  0) && 
924         (nbfit        >= 0.001*nbE))
925       {
926         // create the cal objects
927         calibra->PutMeanValueOtherVectorFit(1,kTRUE);
928         calibra->PutMeanValueOtherVectorFit2(1,kTRUE); 
929         TObjArray object  = calibra->GetVectorFit();
930         AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
931         TObject *objdriftvelocitypad      = calibra->CreatePadObjectVdrift();
932         TObjArray objectt          = calibra->GetVectorFit2();
933         AliTRDCalDet *objtime0det  = calibra->CreateDetObjectT0(&object,kTRUE);
934         TObject *objtime0pad       = calibra->CreatePadObjectT0();
935         // store
936         fArrayCalib->AddAt(objdriftvelocitydet,2);
937         fArrayCalib->AddAt(objdriftvelocitypad,3);
938         //
939         fArrayCalib->AddAt(objtime0det,4);
940         fArrayCalib->AddAt(objtime0pad,5);
941         // Make graph
942         TGraph *graph = 0x0;
943         if(FillGraphIndex(&object,graph)){ 
944           fGraph->AddAt(graph,1);
945         }
946         TGraph *graphh = 0x0;
947         if(FillGraphIndex(&objectt,graphh)){ 
948           fGraph->AddAt(graphh,2);
949         }
950       }//if(enough statistics)
951     calibra->ResetVectorFit();
952   }
953   else return kFALSE;
954   
955   //
956   // PRF
957   //
958   pass = kFALSE; 
959   if(fHisto2d) {
960     TProfile2D *histoprf = (TProfile2D *) fContainer->FindObject("PRF2d");
961     if (histoprf) {
962       histoprf->SetDirectory(0);  
963       calibra->SetMinEntries(600); 
964       if(fCompressPerDetector){
965         if(AddStatsPerDetector(histoprf,2)) pass = calibra->AnalysePRFMarianFit(fDetSumVector);
966       }
967       else pass = calibra->AnalysePRFMarianFit(histoprf);
968     }
969   }
970   else {
971     if(fVector2d && calibraVector) {
972       calibra->SetMinEntries(600);  
973       if(fCompressPerDetector){
974         if(!(vvect =calibraVector->AddStatsPerDetectorPRF())) return kFALSE;
975         pass = calibra->AnalysePRFMarianFit(vvect);
976       }
977       else pass = calibra->AnalysePRFMarianFit(calibraVector);  
978     }
979   }
980   
981   if(pass){
982     Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
983       + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
984     Int_t nbfit        = calibra->GetNumberFit();
985     Int_t nbE          = calibra->GetNumberEnt();
986     // enough statistics
987     if ((nbtg >                  0) && 
988         (nbfit        >= 0.001*nbE)) {
989       TObjArray object            = calibra->GetVectorFit();
990       TObject *objPRFpad          = calibra->CreatePadObjectPRF(&object);
991       // store
992       fArrayCalib->AddAt(objPRFpad,6);
993       // Make graph
994       TGraph *graph = 0x0;
995       if(FillGraphIndex(&object,graph)){ 
996         fGraph->AddAt(graph,5);
997       }
998     }
999     calibra->ResetVectorFit();
1000   }
1001   else return kFALSE;
1002   
1003   //
1004   // VDRIFT linear fit 
1005   //
1006   AliTRDCalibraVdriftLinearFit *vlinearfit = (AliTRDCalibraVdriftLinearFit *) fContainer->FindObject("LinearVdriftFit"); 
1007   if (vlinearfit) {
1008     calibra->SetMinEntries(20*20);     
1009     if(calibra->AnalyseLinearFitters(vlinearfit)) {
1010       
1011       Int_t nbtg = 6*4*18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb0(2))
1012         + 6*  18*((Int_t) ((AliTRDCalibraMode *)calibra->GetCalibraMode())->GetDetChamb2(2));
1013       Int_t nbfit  = calibra->GetNumberFit();
1014       Int_t nbE    = calibra->GetNumberEnt();
1015       // enough statistics
1016       if ((nbtg >                  0) && 
1017           (nbfit        >= 0.001*nbE))
1018         {
1019           // create the cal objects
1020           calibra->PutMeanValueOtherVectorFit(1,kTRUE);
1021           calibra->PutMeanValueOtherVectorFit2(1,kTRUE); 
1022           TObjArray object  = calibra->GetVectorFit();
1023           AliTRDCalDet *objdriftvelocitydet = calibra->CreateDetObjectVdrift(&object,kTRUE);
1024           TObject *objdriftvelocitypad      = calibra->CreatePadObjectVdrift();
1025           TObjArray objectt          = calibra->GetVectorFit2();
1026           AliTRDCalDet *objtime0det  = calibra->CreateDetObjectT0(&object,kTRUE);
1027           TObject *objtime0pad       = calibra->CreatePadObjectT0();
1028           // store dummy
1029           fArrayCalib->AddAt(objdriftvelocitydet,7);
1030           fArrayCalib->AddAt(objdriftvelocitypad,8);
1031           //
1032           fArrayCalib->AddAt(objtime0det,9);
1033           fArrayCalib->AddAt(objtime0pad,10);
1034           // Make graph
1035           TGraph *graph = 0x0;
1036           if(FillGraphIndex(&object,graph)){ 
1037             fGraph->AddAt(graph,3);
1038           }
1039           TGraph *graphh = 0x0;
1040           if(FillGraphIndex(&objectt,graphh)){ 
1041             fGraph->AddAt(graphh,4);
1042           }
1043         }//if(enough statistics)
1044     }// if fit
1045     calibra->ResetVectorFit();
1046   }
1047   else return kFALSE;
1048   
1049   fPostProcess = kTRUE;
1050   
1051   return kTRUE;
1052   
1053 }
1054
1055 //________________________________________________________________________
1056 Bool_t AliTRDcalibration::FillGraphIndex(const TObjArray *vectora,TGraph *graph) const
1057 {
1058   //
1059   // Fill one value (the first one) per detector
1060   //
1061
1062   Int_t loop = (Int_t) vectora->GetEntriesFast();
1063   if(loop != 540) {
1064     AliInfo("The Vector Fit is not complete!");
1065     return kFALSE;
1066   }
1067   
1068   Double_t x[540];
1069   Double_t y[540];
1070   for (Int_t k = 0; k < loop; k++) {
1071     if(!vectora->At(k)){
1072       x[k] = -1.0;
1073       y[k] = -1.0;
1074       continue;
1075     }
1076     x[k]  = ((AliTRDCalibraFit::AliTRDFitInfo *) vectora->At(k))->GetDetector();
1077     y[k]  = ((Float_t *)((AliTRDCalibraFit::AliTRDFitInfo *) vectora->At(k))->GetCoef())[0];
1078   }
1079
1080   if(!graph) graph = new TGraph(540,&x[0],&y[0]);
1081   else{ 
1082     graph->~TGraph();
1083     new(graph) TGraph(540,&x[0],&y[0]);
1084   }
1085
1086   return kTRUE;
1087
1088 }
1089 //________________________________________________________________________
1090 Bool_t AliTRDcalibration::AddStatsPerDetector(const TH2I *ch) 
1091 {
1092   //
1093   // Add statistic per detector
1094   //
1095   
1096   AliTRDCalibraMode calibMode = AliTRDCalibraMode();
1097   const char *name = ch->GetTitle();
1098   if(!SetNzFromTObject(name,0,&calibMode)) return 0x0;
1099   if(!SetNrphiFromTObject(name,0,&calibMode)) return 0x0;
1100   if(((calibMode.GetNz(0) == 100) && (calibMode.GetNrphi(0) == 100)) || ((calibMode.GetNz(0) == 10) && (calibMode.GetNrphi(0) == 10))) return kFALSE;
1101
1102   Int_t    nybins  = ch->GetNbinsY();// groups number
1103   Int_t    nxbins  = ch->GetNbinsX();// number of bins X
1104   TAxis   *xaxis   = ch->GetXaxis();
1105   Double_t lowedge  = xaxis->GetBinLowEdge(1);
1106   Double_t upedge   = xaxis->GetBinUpEdge(nxbins);
1107
1108   // number per chamber 2
1109   calibMode.ModePadCalibration(2,0);
1110   calibMode.ModePadFragmentation(0,2,0,0);
1111   calibMode.SetDetChamb2(0);
1112   Int_t perChamber2 = (Int_t) calibMode.GetDetChamb2(0);
1113
1114   // number per other chamber
1115   calibMode.ModePadCalibration(0,0);
1116   calibMode.ModePadFragmentation(0,0,0,0);
1117   calibMode.SetDetChamb0(0);
1118   Int_t perChamber0 = (Int_t) calibMode.GetDetChamb0(0);
1119
1120   if(nybins != (6*18*perChamber2+6*4*18*perChamber0)) return kFALSE;
1121
1122   // Create Histo
1123   TString nname((const char *)ch->GetName());
1124   nname  += "PerDetector";
1125   TString title("Nz");
1126   title += 0;
1127   title += "Nrphi";
1128   title += 0;
1129   if(!fCHSum) fCHSum = new TH2I((const char *)nname,(const char *)title
1130                                 ,nxbins,lowedge,upedge,540,0,540);
1131   else{ 
1132     fCHSum->~TH2I();
1133     new(fCHSum) TH2I((const Char_t *) nname,(const char *)title
1134                      ,nxbins,lowedge,upedge,540,0,540);
1135   }
1136   fCHSum->SetYTitle("Detector number");
1137   fCHSum->SetXTitle("charge deposit [a.u]");
1138   fCHSum->SetZTitle("counts");
1139   fCHSum->SetStats(0);
1140   fCHSum->Sumw2();
1141
1142   Int_t counter = 0;
1143   
1144   for(Int_t det = 0; det < 540; det++){
1145
1146     Int_t numberofgroup = 0;
1147     if(AliTRDgeometry::GetStack(det) == 2) numberofgroup = perChamber2;
1148     else numberofgroup = perChamber0;
1149     TH1I *projch = (TH1I *) ch->ProjectionX("projch",counter+1,counter+numberofgroup,(Option_t *)"e");
1150     projch->SetDirectory(0);
1151        
1152     for(Int_t nx = 0; nx <= nxbins; nx++) {
1153       fCHSum->SetBinContent(nx,det+1,projch->GetBinContent(nx));
1154       fCHSum->SetBinError(nx,det+1,projch->GetBinError(nx));
1155     }
1156
1157     counter += numberofgroup;
1158     
1159     delete projch;
1160
1161   }
1162
1163   return kTRUE;
1164
1165 }
1166 //_____________________________________________________________________________________________________________________
1167 Bool_t AliTRDcalibration::AddStatsPerDetector(const TProfile2D *ph,Int_t i)
1168 {
1169   //
1170   // Add statistic per detector
1171   //
1172
1173   AliTRDCalibraMode calibMode = AliTRDCalibraMode();
1174   const char *name = ph->GetTitle();
1175   //printf("name %s\n",name);
1176   if(!SetNzFromTObject(name,0,&calibMode)) return kFALSE;
1177   if(!SetNrphiFromTObject(name,0,&calibMode)) return kFALSE;
1178   if(((calibMode.GetNz(0) == 100) && (calibMode.GetNrphi(0) == 100)) || ((calibMode.GetNz(0) == 10) && (calibMode.GetNrphi(0) == 10))) return kFALSE;
1179   //printf("Found mode Mz %d, Nrphi %d\n",calibMode.GetNz(0),calibMode.GetNrphi(0));  
1180
1181
1182   Int_t    nybins  = ph->GetNbinsY();// groups number
1183   Int_t    nxbins  = ph->GetNbinsX();// number of bins X
1184   TAxis   *xaxis = ph->GetXaxis();
1185   Double_t lowedge  = xaxis->GetBinLowEdge(1);
1186   Double_t upedge   = xaxis->GetBinUpEdge(nxbins);
1187
1188   // number per chamber 2
1189   calibMode.ModePadCalibration(2,0);
1190   calibMode.ModePadFragmentation(0,2,0,0);
1191   calibMode.SetDetChamb2(0);
1192   Int_t perChamber2 = (Int_t) calibMode.GetDetChamb2(0);
1193
1194   // number per other chamber
1195   calibMode.ModePadCalibration(0,0);
1196   calibMode.ModePadFragmentation(0,0,0,0);
1197   calibMode.SetDetChamb0(0);
1198   Int_t perChamber0 = (Int_t) calibMode.GetDetChamb0(0);
1199
1200   if(nybins != (6*18*perChamber2+6*4*18*perChamber0)) return kFALSE;
1201   
1202   // Create calvector 
1203   TString nbname((const char *)ph->GetName());
1204   nbname  += "PerDetectorVector";
1205   if(!fDetSumVector) fDetSumVector = new AliTRDCalibraVector();
1206   else{ 
1207     fDetSumVector->~AliTRDCalibraVector();
1208     new(fDetSumVector) AliTRDCalibraVector();
1209   }
1210   if(i==1){
1211     fDetSumVector->SetTimeMax(nxbins);
1212   }
1213   if(i==2){
1214     fDetSumVector->SetNumberBinPRF(nxbins);
1215     fDetSumVector->SetPRFRange(TMath::Abs(lowedge));
1216   }
1217   fDetSumVector->SetDetCha0(i,1);
1218   fDetSumVector->SetDetCha2(i,1);
1219   fDetSumVector->SetNzNrphi(i,0,0);
1220   if(i==2) {
1221     Int_t nbg = GetNumberOfGroupsPRF((const char *)name);
1222     fDetSumVector->SetNbGroupPRF(nbg);
1223   }
1224
1225   // Create Histo
1226   TString nname((const char *)ph->GetName());
1227   nname  += "PerDetector";
1228   TString title("Nz");
1229   title += 0;
1230   title += "Nrphi";
1231   title += 0;
1232   if(!fDetSum) fDetSum = new TH2D((const char *)nname,(const Char_t *) title
1233                                 ,nxbins,lowedge,upedge,540,0,540);
1234   else{ 
1235     fDetSum->~TH2D();
1236     new(fDetSum) TH2D((const Char_t *) nname,(const Char_t *) title
1237                      ,nxbins,lowedge,upedge,540,0,540);
1238   }
1239   fDetSum->SetYTitle("Detector number");
1240   fDetSum->SetXTitle(xaxis->GetTitle());
1241   fDetSum->SetStats(0);
1242   
1243   Int_t counter = 0;
1244
1245   for(Int_t det = 0; det < 540; det++){
1246
1247     Int_t numberofgroup = 0;
1248     if(AliTRDgeometry::GetStack(det) == 2) numberofgroup = perChamber2;
1249     else numberofgroup = perChamber0;
1250     
1251     for(Int_t nx = 1; nx <= nxbins; nx++) {
1252       
1253       Double_t entries = 0.0;
1254       Double_t sumw2 = 0.0;
1255       Double_t sumw = 0.0;
1256
1257       for(Int_t k = counter+1; k <= (counter+numberofgroup); k++){
1258         Int_t  binnumber = ph->GetBin(nx,k);
1259         entries += ph->GetBinEntries(binnumber);
1260         sumw2 += (ph->GetBinError(binnumber)*ph->GetBinError(binnumber)+ph->GetBinContent(binnumber)*ph->GetBinContent(binnumber))*ph->GetBinEntries(binnumber);
1261         sumw += ph->GetBinContent(binnumber)*ph->GetBinEntries(binnumber);
1262       }
1263
1264       Double_t mean = 0.0;
1265       if(entries > 0.0) mean = sumw/entries;
1266       Double_t squaremean = 0.0;
1267       if(entries > 0.0) squaremean = sumw2/entries;
1268       Double_t errorf = squaremean - mean*mean;
1269       Double_t error = 0.0;
1270       if(entries > 0.0) error = TMath::Sqrt(TMath::Abs(errorf)/entries);
1271       
1272       fDetSum->SetBinContent(nx,det+1,mean);
1273       fDetSum->SetBinError(nx,det+1,error);
1274
1275       if(i==1) fDetSumVector->FillVectorPH(det,0,nx-1,(Int_t)entries,(Float_t)mean,(Float_t)squaremean);
1276       if(i==2) fDetSumVector->FillVectorPRF(det,0,nx-1,(Int_t)entries,(Float_t)mean,(Float_t)squaremean);
1277       
1278     }
1279     
1280     counter += numberofgroup;
1281
1282   }
1283
1284   return kTRUE;
1285
1286   
1287 }
1288 //_____________________________________________________________________________
1289 Bool_t AliTRDcalibration::SetNrphiFromTObject(const char *name, Int_t i, AliTRDCalibraMode *calibMode) const
1290 {
1291   //
1292   // Set the granularity from object
1293   //  
1294   
1295   const Char_t *patternrphi0 = "Nrphi0";
1296   const Char_t *patternrphi1 = "Nrphi1";
1297   const Char_t *patternrphi2 = "Nrphi2";
1298   const Char_t *patternrphi3 = "Nrphi3";
1299   const Char_t *patternrphi4 = "Nrphi4";
1300   const Char_t *patternrphi5 = "Nrphi5";
1301   const Char_t *patternrphi6 = "Nrphi6";
1302
1303   
1304   const Char_t *patternrphi10 = "Nrphi10";
1305   const Char_t *patternrphi100 = "Nrphi100";
1306   const Char_t *patternz10 = "Nz10";
1307   const Char_t *patternz100 = "Nz100";
1308
1309   // Nrphi mode
1310   if ((strstr(name,patternrphi100)) && (strstr(name,patternz100))) {
1311     calibMode->SetAllTogether(i);
1312     return kTRUE;
1313   }
1314   if ((strstr(name,patternrphi10)) && (strstr(name,patternz10))) {
1315     calibMode->SetPerSuperModule(i);
1316     return kTRUE;
1317   }
1318   
1319   if (strstr(name,patternrphi0)) {
1320     calibMode->SetNrphi(i ,0);
1321     return kTRUE;
1322   }
1323   if (strstr(name,patternrphi1)) {
1324     calibMode->SetNrphi(i, 1);
1325     return kTRUE;
1326   }
1327   if (strstr(name,patternrphi2)) {
1328     calibMode->SetNrphi(i, 2);
1329     return kTRUE;
1330   }
1331   if (strstr(name,patternrphi3)) {
1332     calibMode->SetNrphi(i, 3);
1333     return kTRUE;
1334   }
1335   if (strstr(name,patternrphi4)) {
1336     calibMode->SetNrphi(i, 4);
1337     return kTRUE;
1338   }
1339   if (strstr(name,patternrphi5)) {
1340     calibMode->SetNrphi(i, 5);
1341     return kTRUE;
1342   }
1343   if (strstr(name,patternrphi6)) {
1344     calibMode->SetNrphi(i, 6);
1345     return kTRUE;
1346   }
1347   
1348   calibMode->SetNrphi(i ,0);
1349   return kFALSE;
1350   
1351 }
1352 //_____________________________________________________________________________
1353 Bool_t AliTRDcalibration::SetNzFromTObject(const char *name, Int_t i, AliTRDCalibraMode *calibMode) const
1354 {
1355   //
1356   // Set fNz[i] of the AliTRDCalibraFit::Instance()
1357   // corresponding to the given TObject
1358   //
1359
1360   // Some patterns
1361   const Char_t *patternz0    = "Nz0";
1362   const Char_t *patternz1    = "Nz1";
1363   const Char_t *patternz2    = "Nz2";
1364   const Char_t *patternz3    = "Nz3";
1365   const Char_t *patternz4    = "Nz4";
1366
1367   const Char_t *patternrphi10 = "Nrphi10";
1368   const Char_t *patternrphi100 = "Nrphi100";
1369   const Char_t *patternz10 = "Nz10";
1370   const Char_t *patternz100 = "Nz100";
1371
1372   if ((strstr(name,patternrphi100)) && (strstr(name,patternz100))) {
1373     calibMode->SetAllTogether(i);
1374     return kTRUE;
1375   }
1376   if ((strstr(name,patternrphi10)) && (strstr(name,patternz10))) {
1377     calibMode->SetPerSuperModule(i);
1378     return kTRUE;
1379   }
1380   if (strstr(name,patternz0)) {
1381     calibMode->SetNz(i, 0);
1382     return kTRUE;
1383   }
1384   if (strstr(name,patternz1)) {
1385     calibMode->SetNz(i ,1);
1386     return kTRUE;
1387   }
1388   if (strstr(name,patternz2)) {
1389     calibMode->SetNz(i ,2);
1390     return kTRUE;
1391   }
1392   if (strstr(name,patternz3)) {
1393     calibMode->SetNz(i ,3);
1394     return kTRUE;  
1395   }
1396   if (strstr(name,patternz4)) {
1397     calibMode->SetNz(i ,4);
1398     return kTRUE;
1399   }
1400  
1401   calibMode->SetNz(i ,0);
1402   return kFALSE;
1403 }
1404 //____________________________________________________________________________________________________
1405 Int_t AliTRDcalibration::GetNumberOfGroupsPRF(const char* nametitle) const
1406 {
1407   //
1408   // Get numberofgroupsprf
1409   //
1410   
1411   // Some patterns
1412   const Char_t *pattern0 = "Ngp0";
1413   const Char_t *pattern1 = "Ngp1";
1414   const Char_t *pattern2 = "Ngp2";
1415   const Char_t *pattern3 = "Ngp3";
1416   const Char_t *pattern4 = "Ngp4";
1417   const Char_t *pattern5 = "Ngp5";
1418   const Char_t *pattern6 = "Ngp6";
1419
1420   // Nrphi mode
1421   if (strstr(nametitle,pattern0)) {
1422     return 0;
1423   }
1424   if (strstr(nametitle,pattern1)) {
1425     return 1;
1426   }
1427   if (strstr(nametitle,pattern2)) {
1428     return 2;
1429   }
1430   if (strstr(nametitle,pattern3)) {
1431     return 3;
1432   }
1433   if (strstr(nametitle,pattern4)) {
1434     return 4;
1435   }
1436   if (strstr(nametitle,pattern5)) {
1437     return 5;
1438   }
1439   if (strstr(nametitle,pattern6)){
1440     return 6;
1441   }
1442   else return -1;
1443 }