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