Macros to chack SDD online calibrations updated for 2011 run
[u/mrichter/AliRoot.git] / ITS / macrosSDD / PlotDriftSpeedSDDVsTime.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 #include <Riostream.h>
3 #include <TFile.h>
4 #include <TH1F.h>
5 #include <TH2F.h>
6 #include <TGraph.h>
7 #include <TStyle.h>
8 #include <TLegend.h>
9 #include <TLegendEntry.h>
10 #include <TLine.h>
11 #include <TSystem.h>
12 #include <TGrid.h>
13 #include <TMath.h>
14 #include <TGraphErrors.h>
15 #include <TCanvas.h>
16 #include <TLatex.h>
17 #include <TObjArray.h>
18 #include "AliCDBEntry.h"
19 #include "AliITSDriftSpeedArraySDD.h"
20 #include "AliITSgeomTGeo.h"
21 #endif
22
23 /*  $Id: PlotDriftSpeedSDDVsTime.C 41510 2010-06-01 09:21:24Z prino $    */
24
25
26 // Macro to plot the drift speed vs. time from the OCDB files 
27 // created from INJECTOR runs (OCDB/ITS/Calib/DriftSpeedSDD)
28 // Origin: F. Prino (prino@to.infn.it)
29
30 void FillErrors(Float_t errSpeed[260]);
31
32 void PlotDriftSpeedSDDVsTime(Int_t year=2011, Int_t firstRun=142600, 
33                              Int_t lastRun=999999999,
34                              Int_t anode=128){
35   TGrid::Connect("alien:",0,0,"t");
36   Float_t errSpeed[260];
37   FillErrors(errSpeed);
38   TString cmd=Form("gbbox find \"/alice/data/%d/OCDB/ITS/Calib/DriftSpeedSDD\" \"Run*.root\" > runSpeedAlien.txt",year);
39   gSystem->Exec(cmd.Data());
40   FILE* listruns=fopen("runSpeedAlien.txt","r");
41   Char_t filnam[200],filnamalien[200];
42   TGraphErrors** gvdrvstime=new TGraphErrors*[520];
43   TGraphErrors** gvdrvsrun=new TGraphErrors*[520];
44   TGraph* gGoodInjVsRun=new TGraph(0);
45   gGoodInjVsRun->SetName("gGoodInjVsRun");
46   TGraph* gAverSpeedVsRun=new TGraph(0);
47   gAverSpeedVsRun->SetName("gAverSpeedVsRun");
48   TGraph* gGoodInjVsTime=new TGraph(0);
49   gGoodInjVsTime->SetName("gGoodInjVsTime");
50   TGraph* gAverSpeedVsTime=new TGraph(0);
51   gAverSpeedVsTime->SetName("gAverSpeedVsIime");
52   
53   for(Int_t iMod=0; iMod<260;iMod++){
54     gvdrvstime[iMod]=new TGraphErrors(0);    
55     gvdrvstime[iMod]->SetTitle(Form("Module %d",iMod+240));
56     gvdrvstime[iMod]->SetName(Form("gspmod%dt",iMod+240));
57     gvdrvsrun[iMod]=new TGraphErrors(0);    
58     gvdrvsrun[iMod]->SetTitle(Form("Module %d",iMod+240));
59     gvdrvsrun[iMod]->SetName(Form("gspmod%dr",iMod+240));
60   }
61   Float_t Edrift=(1800-45)/291/0.012;  
62   Int_t nrun,nrun2,nv,ns;
63   Float_t timeZero;
64   if(year==2009) timeZero=1247762992;
65   else if(year==2010) timeZero=1262300400;
66   else timeZero=1293861600;
67
68   while(!feof(listruns)){
69     fscanf(listruns,"%s\n",filnam);
70     Char_t directory[100];
71     sprintf(directory,"/alice/data/%d",year);
72     if(!strstr(filnam,directory)) continue;
73        sscanf(filnam,"/alice/data/%d/OCDB/ITS/Calib/DriftSpeedSDD/Run%d_%d_v%d_s%d.root",&year,&nrun,&nrun2,&nv,&ns);
74
75        if(year==2009 && (nrun<85639 && nrun2> 85639)) continue;// protection for files with swapped ladders 4-5 of layer 3 
76        if(year==2009 && (nrun>100000 && nv< 325)) continue; // protection for files with swapped ladder 0-1 of layer 4
77   
78     if(nrun<firstRun) continue;
79     if(nrun>lastRun) continue;
80     sprintf(filnamalien,"alien://%s",filnam);
81     printf("Open file: %s\n",filnam);
82     TFile *f= TFile::Open(filnamalien);
83     AliCDBEntry *ent=(AliCDBEntry*)f->Get("AliCDBEntry");
84     TObjArray *drspSDD = (TObjArray *)ent->GetObject();
85     
86     Int_t iGoodInj=0;
87     Int_t iAverSpeed=0;
88     Float_t timeday=0;
89     
90     AliITSDriftSpeedArraySDD *vdriftarr;
91     AliITSDriftSpeedArraySDD *vdriftarr0;
92     AliITSDriftSpeedArraySDD *vdriftarr1;
93
94     for(Int_t iMod=0; iMod<260;iMod++){
95       Int_t index=-1;
96       if(anode<256) index=2*iMod;
97       else index=2*iMod+1;
98       vdriftarr=(AliITSDriftSpeedArraySDD*)drspSDD->At(index);
99           
100       Int_t i0=2*iMod;
101       Int_t i1=1+2*iMod;
102       vdriftarr0=(AliITSDriftSpeedArraySDD*)drspSDD->At(i0);
103       vdriftarr1=(AliITSDriftSpeedArraySDD*)drspSDD->At(i1);
104           
105       Int_t statusInj0=vdriftarr0->GetInjectorStatus();
106       Int_t statusInj1=vdriftarr1->GetInjectorStatus();
107       if(statusInj0>0) iGoodInj++;
108       else iAverSpeed++;
109       if(statusInj1>0) iGoodInj++;
110       else iAverSpeed++;
111           
112       Int_t iAn=anode;
113       if(anode>256) iAn=anode-256;
114       Float_t vdrift=vdriftarr->GetDriftSpeed(0,iAn);
115       if(vdrift<4. || vdrift > 8.) continue;
116       if(statusInj0==0) continue;
117       Int_t npt=gvdrvsrun[iMod]->GetN();
118       gvdrvsrun[iMod]->SetPoint(npt,(Float_t)nrun,vdrift);
119       gvdrvsrun[iMod]->SetPointError(npt,0,errSpeed[iMod]);
120       
121       UInt_t timest=vdriftarr->GetTimestamp(0);
122       if(timest==0) continue;
123       if(timest<timeZero) continue;
124       timeday=float(timest-timeZero)/60./60./24.;
125       Float_t mob=vdrift*1.E5/Edrift;  
126       Float_t temper=293.15*TMath::Power((mob/1350.),-1/2.4); 
127       if(iMod==497-240) printf("Run %s   Time %d Day %f Speed=%f Temp=%f\n",filnam,timest,timeday,vdrift,temper);
128       npt=gvdrvstime[iMod]->GetN();
129       gvdrvstime[iMod]->SetPoint(npt,timeday,vdrift);
130       gvdrvstime[iMod]->SetPointError(npt,0,errSpeed[iMod]);
131     }
132     Int_t npt=gGoodInjVsRun->GetN();
133     gGoodInjVsRun->SetPoint(npt,(Float_t)nrun,iGoodInj);
134     gAverSpeedVsRun->SetPoint(npt,(Float_t)nrun,iAverSpeed);
135         
136     npt=gGoodInjVsTime->GetN();
137     gGoodInjVsTime->SetPoint(npt,timeday,iGoodInj);
138     gAverSpeedVsTime->SetPoint(npt,timeday,iAverSpeed);
139         
140     printf("Number of half-modules with drift speed from injectors = %d\n",iGoodInj);
141     printf("Number of half-modules with average drift speed        = %d\n",iAverSpeed);
142     f->Close();
143   }
144
145   Int_t mod1=244-240;
146   Int_t mod2=277-240;
147   //  Int_t mod2=259-240;
148 //   Int_t mod2=274-240;
149   Int_t mod3=327-240;
150    Int_t mod4=453-240;
151    //  Int_t mod4=497-240;
152   Int_t lay1,lad1,det1;
153   Int_t lay2,lad2,det2;
154   Int_t lay3,lad3,det3;
155   Int_t lay4,lad4,det4;
156   AliITSgeomTGeo::GetModuleId(mod1+240,lay1,lad1,det1);
157   AliITSgeomTGeo::GetModuleId(mod2+240,lay2,lad2,det2);
158   AliITSgeomTGeo::GetModuleId(mod3+240,lay3,lad3,det3);
159   AliITSgeomTGeo::GetModuleId(mod4+240,lay4,lad4,det4);
160   Char_t filout[100];
161   sprintf(filout,"DriftSpVsTime_%d.root",year);
162   TFile *ofil=new TFile(filout,"recreate");
163   for(Int_t iMod=0; iMod<260;iMod++){
164     gvdrvstime[iMod]->Write();
165     gvdrvsrun[iMod]->Write();
166   }
167   gGoodInjVsRun->Write();
168   gGoodInjVsTime->Write();
169   gAverSpeedVsRun->Write();
170   gAverSpeedVsTime->Write();
171   ofil->Close();
172
173   gStyle->SetOptTitle(0);
174   TCanvas* c0=new TCanvas("c0","Vdrift vs. time");
175   c0->SetGridx();
176   c0->SetGridy();
177   gvdrvstime[mod1]->SetMarkerStyle(20);
178   gvdrvstime[mod2]->SetMarkerStyle(22);
179   gvdrvstime[mod2]->SetMarkerColor(2);
180   gvdrvstime[mod2]->SetLineColor(2);
181   gvdrvstime[mod3]->SetMarkerStyle(29);
182   gvdrvstime[mod3]->SetMarkerColor(3);
183   gvdrvstime[mod3]->SetLineColor(3);
184   gvdrvstime[mod4]->SetMarkerStyle(27);
185   gvdrvstime[mod4]->SetMarkerColor(4);
186   gvdrvstime[mod4]->SetLineColor(4);
187   gvdrvstime[mod1]->Draw("AP");
188   gvdrvstime[mod1]->SetMinimum(6.3);
189   gvdrvstime[mod1]->SetMaximum(6.75);
190   Char_t title[100];
191   if(year==2009){
192     sprintf(title,"Time (days since July 16th 2009)");
193   }else if (year==2010){
194     sprintf(title,"Time (days since January 1st 2010)");
195   }else{
196     sprintf(title,"Time (days since January 1st 2011)");
197   }
198   gvdrvstime[mod1]->GetXaxis()->SetTitle(title);
199   gvdrvstime[mod1]->GetYaxis()->SetTitle("Drift speed (#mum/ns)");
200   gvdrvstime[mod2]->Draw("PSAME");
201   gvdrvstime[mod3]->Draw("PSAME");
202   gvdrvstime[mod4]->Draw("PSAME");
203   TLegend* leg=new TLegend(0.6,0.7,0.89,0.89);
204   leg->SetBorderSize(0);
205   leg->SetFillColor(0);
206   leg->SetFillStyle(0);
207   TLegendEntry* lent=leg->AddEntry(gvdrvstime[mod1],Form("Lay %d Lad %d Det %d",lay1,lad1,det1),"P");
208   lent=leg->AddEntry(gvdrvstime[mod2],Form("Lay %d Lad %d Det %d",lay2,lad2,det2),"P");
209   lent->SetTextColor(2);
210   lent=leg->AddEntry(gvdrvstime[mod3],Form("Lay %d Lad %d Det %d",lay3,lad3,det3),"P");
211   lent->SetTextColor(3);
212   lent=leg->AddEntry(gvdrvstime[mod4],Form("Lay %d Lad %d Det %d",lay4,lad4,det4),"P");
213   lent->SetTextColor(4);
214   leg->Draw();
215
216   TCanvas* c1=new TCanvas("c1","Vdrift vs. run");
217   c1->SetGridx();
218   c1->SetGridy();
219   gvdrvsrun[mod1]->SetMarkerStyle(20);
220   gvdrvsrun[mod2]->SetMarkerStyle(22);
221   gvdrvsrun[mod2]->SetMarkerColor(2);
222   gvdrvsrun[mod2]->SetLineColor(2);
223   gvdrvsrun[mod3]->SetMarkerStyle(29);
224   gvdrvsrun[mod3]->SetMarkerColor(3);
225   gvdrvsrun[mod3]->SetLineColor(3);
226   gvdrvsrun[mod4]->SetMarkerStyle(27);
227   gvdrvsrun[mod4]->SetMarkerColor(4);
228   gvdrvsrun[mod4]->SetLineColor(4);
229   gvdrvsrun[mod1]->Draw("AP");
230   gvdrvsrun[mod1]->SetMinimum(6.3);
231   gvdrvsrun[mod1]->SetMaximum(6.75);
232
233   gvdrvsrun[mod1]->GetXaxis()->SetTitle("Run number");
234   gvdrvsrun[mod1]->GetYaxis()->SetTitle("Drift speed (#mum/ns)");
235   gvdrvsrun[mod2]->Draw("PSAME");
236   gvdrvsrun[mod3]->Draw("PSAME");
237   gvdrvsrun[mod4]->Draw("PSAME");
238   leg->Draw();
239
240
241   TH2F* hlay3=new TH2F("hlay3","Variation of the drift speed (%) Layer 3",6,-0.5,5.5,14,-0.5,13.5);
242   hlay3->GetXaxis()->SetTitle("Detector");
243   hlay3->GetYaxis()->SetTitle("Ladder");
244   hlay3->GetXaxis()->SetTickLength(0);
245   hlay3->GetYaxis()->SetTickLength(0);
246   hlay3->SetStats(0);
247
248   TH2F* hlay4=new TH2F("hlay4","Variation of the drift speed (%) Layer 4",8,-0.5,7.5,22,-0.5,21.5);
249   hlay4->GetXaxis()->SetTitle("Detector");
250   hlay4->GetYaxis()->SetTitle("Ladder");
251   hlay4->GetXaxis()->SetTickLength(0);
252   hlay4->GetYaxis()->SetTickLength(0);
253   hlay4->GetYaxis()->SetTitle("Ladder");
254   hlay4->SetStats(0);
255
256   Double_t run1,run2,vdr1,vdr2;
257   Int_t lay,lad,det;
258   for(Int_t iMod=0; iMod<260; iMod++){
259     Int_t lastPoint=gvdrvsrun[iMod]->GetN()-1;
260     gvdrvsrun[iMod]->GetPoint(lastPoint,run2,vdr2);
261     gvdrvsrun[iMod]->GetPoint(lastPoint-1,run1,vdr1);
262     Float_t diff=0.;
263     if(vdr1>0.) diff=100*(vdr2-vdr1)/vdr1;
264     AliITSgeomTGeo::GetModuleId(iMod+240,lay,lad,det);
265     if(lay==3) hlay3->SetBinContent(det,lad,diff);
266     if(lay==4) hlay4->SetBinContent(det,lad,diff);
267   }
268   TLine* lin=new TLine(0,0,0,23);  
269   gStyle->SetPalette(1);
270
271   TCanvas *c0b=new TCanvas("c0b","Percent difference Last Run - Previous Run",900,600);
272   c0b->Divide(2,1);
273   c0b->cd(1);
274   hlay3->DrawCopy("colz");
275   for(Int_t i=0;i<6;i++){
276     lin->SetY1(-0.5);
277     lin->SetY2(13.5);
278     lin->SetX1(i+0.5);
279     lin->SetX2(i+0.5);
280     lin->DrawClone();
281   }
282   for(Int_t i=0;i<14;i++){
283     lin->SetX1(-0.5);
284     lin->SetX2(5.5);
285     lin->SetY1(i+0.5);
286     lin->SetY2(i+0.5);
287     lin->DrawClone();
288   }
289   c0b->cd(2);
290   hlay4->DrawCopy("colz");
291   for(Int_t i=0;i<8;i++){
292     lin->SetY1(-0.5);
293     lin->SetY2(21.5);
294     lin->SetX1(i+0.5);
295     lin->SetX2(i+0.5);
296     lin->DrawClone();
297   }
298   for(Int_t i=0;i<22;i++){
299     lin->SetX1(-0.5);
300     lin->SetX2(7.5);
301     lin->SetY1(i+0.5);
302     lin->SetY2(i+0.5);
303     lin->DrawClone();
304   }
305
306   TCanvas* c4=new TCanvas("c4","GoodMod vs. run");
307   c4->SetGridx();
308   c4->SetGridy();
309   gGoodInjVsRun->SetMarkerStyle(20);
310   gGoodInjVsRun->SetMarkerColor(2);
311   gGoodInjVsRun->GetXaxis()->SetTitle("Run number");
312   gGoodInjVsRun->GetYaxis()->SetTitle("Half-modules with drift speed from injectors");
313   gGoodInjVsRun->Draw("AP");
314   
315   TCanvas* c5=new TCanvas("c5","GoodMod vs. time");
316   c5->SetGridx();
317   c5->SetGridy();
318   gGoodInjVsTime->SetMarkerStyle(20);
319   gGoodInjVsTime->SetMarkerColor(2);
320   gGoodInjVsTime->GetXaxis()->SetTitle(title);
321   gGoodInjVsTime->GetYaxis()->SetTitle("Half-modules with drift speed from injectors");
322   gGoodInjVsTime->Draw("AP");
323 }
324
325 void FillErrors(Float_t errSpeed[260]){
326   Float_t err[260]={
327     0.002308,0.005120,0.004632,0.001000,0.001735,
328     0.001000,0.001000,0.002667,0.004237,0.005297,
329     0.001000,0.005460,0.005149,0.003921,0.001000,
330     0.003906,0.001000,0.004871,0.001000,0.001000,
331     0.001000,0.001000,0.002261,0.002986,0.002056,
332     0.002848,0.001000,0.001777,0.002822,0.004651,
333     0.001000,0.003551,0.006466,0.001000,0.002083,
334     0.004531,0.001000,0.002213,0.001000,0.001000,
335     0.001000,0.001000,0.001000,0.003223,0.002800,
336     0.002147,0.001000,0.003364,0.001000,0.001000,
337     0.002515,0.003229,0.002552,0.005765,0.002368,
338     0.003473,0.002363,0.001000,0.003413,0.001000,
339     0.004906,0.001000,0.004346,0.004887,0.007138,
340     0.007242,0.004289,0.003970,0.002914,0.002199,
341     0.001000,0.003483,0.002154,0.002914,0.003097,
342     0.006034,0.003101,0.001000,0.002425,0.002651,
343     0.002771,0.002409,0.002260,0.003109,0.001000,
344     0.003384,0.003374,0.002212,0.004441,0.001000,
345     0.001000,0.001000,0.003578,0.001000,0.001000,
346     0.003517,0.003590,0.001787,0.003329,0.001000,
347     0.002770,0.001000,0.004032,0.003059,0.001000,
348     0.001000,0.001000,0.001000,0.001000,0.001000,
349     0.001000,0.004556,0.001000,0.001000,0.001000,
350     0.001000,0.001000,0.001000,0.004819,0.002100,
351     0.002624,0.003784,0.003772,0.002483,0.002792,
352     0.001000,0.004713,0.003214,0.003180,0.002145,
353     0.002470,0.003078,0.001000,0.007131,0.002770,
354     0.002533,0.001000,0.004362,0.002819,0.001000,
355     0.003630,0.004215,0.002975,0.001000,0.003790,
356     0.002345,0.001000,0.003999,0.004555,0.003989,
357     0.001000,0.001000,0.001000,0.003136,0.002426,
358     0.005144,0.002844,0.002310,0.002467,0.002503,
359     0.003811,0.003440,0.004773,0.003114,0.001000,
360     0.000583,0.001000,0.001000,0.003385,0.001000,
361     0.001000,0.001000,0.001000,0.003108,0.002109,
362     0.005325,0.003750,0.002810,0.003559,0.001000,
363     0.001000,0.003262,0.003903,0.001000,0.003622,
364     0.002533,0.002121,0.003733,0.005353,0.002221,
365     0.004767,0.003267,0.004892,0.002152,0.003398,
366     0.001000,0.003146,0.001000,0.002952,0.003310,
367     0.002644,0.002573,0.001000,0.003989,0.001000,
368     0.005294,0.003095,0.003479,0.002250,0.001000,
369     0.001000,0.005221,0.001000,0.001653,0.004330,
370     0.013188,0.007375,0.003226,0.003875,0.001000,
371     0.003653,0.001000,0.002655,0.001000,0.001000,
372     0.001000,0.001000,0.004718,0.001000,0.001000,
373     0.001000,0.002780,0.003680,0.001000,0.002787,
374     0.001000,0.004617,0.001000,0.001000,0.003231,
375     0.001887,0.002090,0.003326,0.129970,0.004907,
376     0.004334,0.001000,0.001000,0.003489,0.002573,
377     0.002566,0.002982,0.001000,0.001000,0.003436,
378     0.004016,0.003736,0.001784,0.004775,0.008090};
379   for(Int_t i=0;i<260;i++) errSpeed[i]=err[i];
380   
381   
382 }