Changing once more (hopefully we get it correct this time...) the logic to trig the...
[u/mrichter/AliRoot.git] / ITS / AnalyzeSDDInjectorsAllMod.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 #include <TH2F.h>
3 #include <TCanvas.h>
4 #include <TGraphErrors.h>
5 #include <TStopwatch.h>
6 #include <TStyle.h>
7 #include <TLatex.h>
8 #include <TFile.h>
9 #include <TMath.h>
10 #include <TNtuple.h>
11 #include <TGrid.h>
12 #include <TF1.h>
13 #include <TLine.h>
14 #include "AliRawReader.h"
15 #include "AliRawReaderDate.h"
16 #include "AliRawReaderRoot.h"
17 #include "AliITSOnlineSDDInjectors.h"
18 #include "AliITSRawStreamSDD.h"
19 #include "AliITSRawStreamSDDCompressed.h"
20 #include "AliITSDDLModuleMapSDD.h"
21 #endif
22
23 // Macro for the analysis of PULSER runs (equivalent to ITSSDDINJda.cxx)
24 // Two functions named AnalyzeSDDInjectorsAllModules: 
25 // The first is for analyzing a local raw data file and takes as agrument the file name.
26 // The second is for running on ALIEN
27 // All DDLs are analyzed, the argument nDDL selects the DDL to be plotted
28 // Origin: F. Prino (prino@to.infn.it)
29
30
31 void AnalyzeSDDInjectorsAllMod(Char_t *datafil, 
32                                Int_t adcfreq=20, 
33                                Int_t nDDL=0, 
34                                Int_t firstEv=18, 
35                                Int_t lastEv=30,
36                                Int_t jpad=20, 
37                                Int_t statuscut=7){
38
39
40   const Int_t kTotDDL=24;
41   const Int_t kModPerDDL=12;
42   const Int_t kSides=2;
43   Bool_t writtenoutput=kFALSE;
44
45   AliITSDDLModuleMapSDD* dmap=new AliITSDDLModuleMapSDD();
46   dmap->SetJun09Map();
47
48   TNtuple* ntsp=new TNtuple("ntsp","","mod:sid:an:stat:vall:v23:v13:v12:c1:c2:c3");
49   Float_t xnt[11];
50   TH2F** histo = new TH2F*[kTotDDL*kModPerDDL*kSides];
51   Int_t nWrittenEv[kTotDDL*kModPerDDL*kSides];
52   TGraphErrors** gvel = new TGraphErrors*[kTotDDL*kModPerDDL*kSides];
53   AliITSOnlineSDDInjectors **anal=new AliITSOnlineSDDInjectors*[kTotDDL*kModPerDDL*kSides];
54   AliITSOnlineSDDInjectors **anal23=new AliITSOnlineSDDInjectors*[kTotDDL*kModPerDDL*kSides];
55   AliITSOnlineSDDInjectors **anal13=new AliITSOnlineSDDInjectors*[kTotDDL*kModPerDDL*kSides];
56   AliITSOnlineSDDInjectors **anal12=new AliITSOnlineSDDInjectors*[kTotDDL*kModPerDDL*kSides];
57   TH1F** hvdriftl=new TH1F*[260];  
58   TH1F** hvdriftr=new TH1F*[260];  
59   Char_t hisnam[20];
60   for(Int_t idet=0; idet<260;idet++){
61     sprintf(hisnam,"vdriftl%03d",idet);
62     hvdriftl[idet]=new TH1F(hisnam,"",500,5.5,8.0);
63     sprintf(hisnam,"vdriftr%03d",idet);
64     hvdriftr[idet]=new TH1F(hisnam,"",500,5.5,8.0);
65   }
66   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
67     for(Int_t imod=0; imod<kModPerDDL;imod++){
68       for(Int_t isid=0;isid<kSides;isid++){
69         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
70         sprintf(hisnam,"h%02dc%02ds%d",iddl,imod,isid);
71
72         histo[index]=new TH2F(hisnam,"",256,-0.5,255.5,256,-0.5,255.5);
73         anal[index]=new AliITSOnlineSDDInjectors(iddl,imod,isid);
74         if(adcfreq==40) anal[index]->Set40MHzConfig();
75         else anal[index]->Set20MHzConfig();
76
77         anal23[index]=new AliITSOnlineSDDInjectors(iddl,imod,isid);
78         if(adcfreq==40) anal23[index]->Set40MHzConfig();
79         else anal23[index]->Set20MHzConfig();
80         anal23[index]->SetUseLine(0,kFALSE);
81
82         anal13[index]=new AliITSOnlineSDDInjectors(iddl,imod,isid);
83         if(adcfreq==40) anal13[index]->Set40MHzConfig();
84         else anal13[index]->Set20MHzConfig();
85         anal13[index]->SetUseLine(1,kFALSE);
86
87         anal12[index]=new AliITSOnlineSDDInjectors(iddl,imod,isid);
88         if(adcfreq==40) anal12[index]->Set40MHzConfig();
89         else anal12[index]->Set20MHzConfig();
90         anal12[index]->SetUseLine(2,kFALSE);
91
92         nWrittenEv[index]=0;
93       }
94     }
95   }
96   TGraphErrors *gvvsmod0=new TGraphErrors(0);
97   TGraphErrors *gvvsmod1=new TGraphErrors(0);
98   TGraphErrors *gtvsmod0=new TGraphErrors(0);
99   TGraphErrors *gtvsmod1=new TGraphErrors(0);
100   Float_t gvmin=6.0, gvmax=7.5;
101   Float_t gtmin=288., gtmax=308.;
102   TH1F* hanst=new TH1F("hanst","",8,-0.5,7.5);
103   TH1F* hpad7l=new TH1F("hpad7l","",33,-0.5,32.5);
104   TH1F* hpad7r=new TH1F("hpad7r","",33,-0.5,32.5);
105
106   TCanvas* c0 = new TCanvas("c0","Event display",900,900);
107   gStyle->SetPalette(1);
108   TCanvas* c1 = new TCanvas("c1","Drift Speed vs. anode",900,900);
109   Char_t text[50];
110   UInt_t timeSt=0;
111
112   Int_t iev=firstEv;
113   AliRawReader *rd; 
114   if(strstr(datafil,".root")!=0){
115     rd=new AliRawReaderRoot(datafil,iev);
116   }else{
117     rd=new AliRawReaderDate(datafil,iev);
118   }
119
120   Char_t gname[15];
121   TF1 *funz=new TF1("funz","[0]+[1]*x+[2]*x*x+[3]*x*x*x",0.,255.);
122   TLatex *t0=new TLatex();
123   t0->SetNDC();
124   t0->SetTextSize(0.06);
125   t0->SetTextColor(4);
126   Int_t readEv=0;
127   do{
128     c0->Clear();
129     c0->Divide(4,6,0.001,0.001);
130     c1->Clear();
131     c1->Divide(4,6,0.001,0.001);
132     printf("Event # %d\n",iev);
133     timeSt=rd->GetTimestamp();
134     rd->Reset();
135     for(Int_t iddl=0; iddl<kTotDDL;iddl++){
136       for(Int_t imod=0; imod<kModPerDDL;imod++){
137         for(Int_t isid=0;isid<kSides;isid++){
138           Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
139           histo[index]->Reset();
140         }
141       }
142     }
143
144
145     UChar_t cdhAttr=AliITSRawStreamSDD::ReadBlockAttributes(rd);
146     UInt_t amSamplFreq=AliITSRawStreamSDD::ReadAMSamplFreqFromCDH(cdhAttr);
147     AliITSRawStream* s=AliITSRawStreamSDD::CreateRawStreamSDD(rd,cdhAttr);
148     if(!writtenoutput){
149       printf("Use %s raw stream, sampling frequency %d MHz\n",s->ClassName(),amSamplFreq);
150       writtenoutput=kTRUE;
151     }
152     while(s->Next()){
153       Int_t iDDL=rd->GetDDLID();
154       Int_t iCarlos=s->GetCarlosId();
155       if(s->IsCompletedModule()) continue;
156       if(s->IsCompletedDDL()) continue;
157       if(iDDL>=0 && iDDL<kTotDDL){ 
158         Int_t index=kSides*(kModPerDDL*iDDL+iCarlos)+s->GetChannel(); 
159         histo[index]->Fill(s->GetCoord2(),s->GetCoord1(),s->GetSignal());
160       }
161     }
162     
163     for(Int_t iddl=0; iddl<kTotDDL;iddl++){
164       for(Int_t imod=0; imod<kModPerDDL;imod++){
165         for(Int_t isid=0;isid<kSides;isid++){
166           Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
167           anal[index]->AnalyzeEvent(histo[index]); 
168           anal23[index]->AnalyzeEvent(histo[index]); 
169           anal13[index]->AnalyzeEvent(histo[index]); 
170           anal12[index]->AnalyzeEvent(histo[index]); 
171           nWrittenEv[index]++;
172           Int_t iMod=dmap->GetModuleNumber(iddl,imod);
173           if(iMod!=-1){     
174             for(Int_t ipad=0;ipad<33;ipad++){
175               Int_t st=anal[index]->GetInjPadStatus(ipad);
176               hanst->Fill(st);
177               if(anal[index]->GetInjPadStatus(ipad)>=statuscut){
178                 if(isid==0) hpad7l->Fill(ipad);
179                 if(isid==1) hpad7r->Fill(ipad);
180               }
181               xnt[0]=(Float_t)iMod;
182               xnt[1]=(Float_t)isid;
183               xnt[2]=(Float_t)ipad;
184               xnt[3]=(Float_t)st;
185               xnt[4]=anal[index]->GetDriftSpeed(ipad);
186               xnt[5]=anal23[index]->GetDriftSpeed(ipad);
187               xnt[6]=anal13[index]->GetDriftSpeed(ipad);
188               xnt[7]=anal12[index]->GetDriftSpeed(ipad);
189               xnt[8]=anal[index]->GetCentroid(ipad,0);
190               xnt[9]=anal[index]->GetCentroid(ipad,1);
191               xnt[10]=anal[index]->GetCentroid(ipad,2);
192               ntsp->Fill(xnt);
193             }
194             if(anal[index]->GetInjPadStatus(jpad)>=statuscut){
195               Float_t vel=anal[index]->GetDriftSpeed(jpad);
196               if(isid==0) hvdriftl[iMod-240]->Fill(vel);
197               if(isid==1) hvdriftr[iMod-240]->Fill(vel);
198             }
199           }
200           if(iddl==nDDL){
201             Int_t index2=kSides*imod+isid;
202             c0->cd(index2+1);
203             histo[index]->SetMaximum(100.);
204             histo[index]->DrawCopy("colz");
205             sprintf(text,"DDL %d channel %d Side %d",nDDL,imod,isid);
206             t0->DrawLatex(0.15,0.92,text);
207             c0->Update();
208             c1->cd(index2+1);
209             gvel[index]=anal[index]->GetDriftSpeedGraph();
210             gvel[index]->SetMarkerStyle(20);
211             gvel[index]->SetTitle("");
212             sprintf(gname,"gvel%dev%d",index,iev);
213             gvel[index]->SetName(gname);
214             //      gvel[index]->SetMinimum(0);
215             //gvel[index]->SetMaximum(200);
216
217             gvel[index]->GetXaxis()->SetLimits(0,256);
218             gvel[index]->GetXaxis()->SetTitle("Anode");
219             gvel[index]->GetYaxis()->SetTitle("Drift vel.");
220             gvel[index]->GetXaxis()->SetTitleSize(0.07);
221             gvel[index]->GetYaxis()->SetTitleSize(0.07);
222             gvel[index]->GetXaxis()->SetTitleOffset(0.6);
223             gvel[index]->GetYaxis()->SetTitleOffset(0.6);
224             if(gvel[index]->GetN()>0) gvel[index]->Draw("AP");
225             Double_t *param=anal[index]->GetDriftSpeedFitParam();
226             funz->SetParameters(param[0],param[1],param[2],param[3]);
227             funz->SetLineColor(2);
228             funz->DrawCopy("LSAME");
229             t0->DrawLatex(0.15,0.92,text);
230             c1->Update();
231           }
232         }
233       }
234     }
235     delete s;
236     iev++;
237     readEv++;
238     printf(" --- OK\n");
239   }while(rd->NextEvent()&&iev<=lastEv);
240   printf("Total number of events = %d\n",readEv);
241   Float_t nfac=1./(Float_t)readEv/33./520.;
242   hanst->Scale(nfac);
243   nfac=1./(Float_t)readEv;
244   hpad7l->Scale(nfac);
245   hpad7r->Scale(nfac);
246
247   TFile *outfil1=new TFile("DriftSpeedVsAnode.root","recreate");  
248   for(Int_t iddl=0; iddl<kTotDDL;iddl++){
249     for(Int_t imod=0; imod<kModPerDDL;imod++){
250       for(Int_t isid=0;isid<kSides;isid++){
251         Int_t index=kSides*(kModPerDDL*iddl+imod)+isid;
252         anal[index]->FitMeanDriftSpeedVsAnode();
253         anal[index]->WriteToASCII(0,timeSt,0);
254         anal[index]->WriteInjectorStatusToASCII();
255         anal[index]->WriteToROOT(outfil1);
256       }
257     }
258   }
259   outfil1->Close();
260
261   Int_t ipt0=0, ipt1=0;
262   Float_t Edrift=(1800-45)/291/0.012;  
263   TFile *outfil=new TFile("DriftSpeedHistos.root","recreate");
264   ntsp->Write();
265   for(Int_t iMod=0; iMod<260; iMod++){
266     outfil->cd();
267     hvdriftl[iMod]->Write();    
268     hvdriftr[iMod]->Write();
269     Float_t modid=iMod+240;
270     if(hvdriftl[iMod]->GetEntries()>0){
271       Float_t avevell=hvdriftl[iMod]->GetMean();
272       Float_t rmsvell=hvdriftl[iMod]->GetRMS();
273       if(avevell > 5.5 && avevell < 8.5){
274         gvvsmod0->SetPoint(ipt0,modid,avevell);
275         gvvsmod0->SetPointError(ipt0,0,rmsvell);
276         Float_t mob=avevell*1.E5/Edrift;  
277         Float_t temper=293.15*TMath::Power((mob/1350.),-1/2.4); 
278         gtvsmod0->SetPoint(ipt0,modid,temper);
279         ++ipt0;
280       }
281     }
282     if(hvdriftr[iMod]->GetEntries()>0){
283       Float_t avevelr=hvdriftr[iMod]->GetMean();
284       Float_t rmsvelr=hvdriftr[iMod]->GetRMS();
285       if(avevelr > 5.5 && avevelr < 8.5){
286         gvvsmod1->SetPoint(ipt1,modid,avevelr);
287         gvvsmod1->SetPointError(ipt1,0,rmsvelr);
288         Float_t mob=avevelr*1.E5/Edrift;
289         Float_t temper=293.15*TMath::Power((mob/1350.),-1./2.4); 
290         gtvsmod1->SetPoint(ipt1,modid,temper);
291         ++ipt1;
292       }
293     }
294   }
295   gvvsmod0->SetName("gvvsmod0");
296   gvvsmod1->SetName("gvvsmod1");
297   gtvsmod0->SetName("gtvsmod0");
298   gtvsmod1->SetName("gtvsmod1");
299   outfil->cd();
300   gvvsmod0->Write();
301   gvvsmod1->Write();
302   gtvsmod0->Write();
303   gtvsmod1->Write();
304   outfil->Close();
305
306   TCanvas* c8=new TCanvas("c8","Drift Speed vs. mod");
307   gvvsmod0->SetTitle("");
308   gvvsmod1->SetTitle("");
309   gvvsmod0->SetMarkerStyle(20);
310   gvvsmod1->SetMarkerStyle(21);
311   gvvsmod1->SetMarkerColor(2);
312   gvvsmod0->Draw("AP");
313   gvvsmod0->SetMinimum(gvmin);
314   gvvsmod0->SetMaximum(gvmax);
315   gvvsmod0->GetXaxis()->SetTitle("Module Number");
316   Char_t title[50];
317   sprintf(title,"Vdrift at injector pad %d",jpad);
318   gvvsmod0->GetYaxis()->SetTitle(title);  
319   gvvsmod1->Draw("PSAME");
320   TLatex* tleft=new TLatex(0.7,0.82,"Side 0");
321   tleft->SetNDC();
322   tleft->SetTextColor(1);
323   tleft->Draw();
324   TLatex* tright=new TLatex(0.7,0.75,"Side 1");
325   tright->SetNDC();
326   tright->SetTextColor(2);
327   tright->Draw();
328
329   TLine *lin=new TLine(323,gvmin,323,gvmax);
330   lin->SetLineColor(4);
331   lin->Draw();
332   c8->Update();
333   c8->SaveAs("VdriftVsMod.gif");
334
335   TCanvas* c8t=new TCanvas("c8t","Temeprature vs. mod");
336   gtvsmod0->SetTitle("");
337   gtvsmod1->SetTitle("");
338   gtvsmod0->SetMarkerStyle(20);
339   gtvsmod1->SetMarkerStyle(21);
340   gtvsmod1->SetMarkerColor(2);
341   gtvsmod0->Draw("AP");
342   gtvsmod0->SetMinimum(gtmin);
343   gtvsmod0->SetMaximum(gtmax);
344   gtvsmod0->GetXaxis()->SetTitle("Module Number");
345   sprintf(title,"Estimated Temperature (K)");
346   gtvsmod0->GetYaxis()->SetTitle(title);  
347   gtvsmod1->Draw("PSAME");
348   tleft->Draw();
349   tright->Draw();
350   TLine *lint=new TLine(323,gtmin,323,gtmax);
351   lint->SetLineColor(4);
352   lint->Draw();
353   c8t->Update();
354   c8t->SaveAs("TempVsMod.gif");
355
356   TCanvas* c9=new TCanvas("c9","Injector status");
357   hanst->SetStats(0);
358   hanst->Draw();
359   hanst->GetXaxis()->SetTitle("Injector pad status");
360   hanst->GetXaxis()->CenterTitle();
361   c9->SaveAs("InjStatus.gif");
362
363 //   TCanvas* c10=new TCanvas("c10","Pad status 7",1200,600);
364 //   hpad7l->SetStats(0);
365 //   hpad7r->SetStats(0);
366 //   c10->Divide(2,1);
367 //   c10->cd(1);
368 //   hpad7l->Draw(); 
369 //   hpad7l->GetXaxis()->SetTitle("Side Left -- Pad number");
370 //   hpad7l->GetXaxis()->CenterTitle();
371 //   hpad7l->GetYaxis()->SetTitle("Number of status 7");
372 //   c10->cd(2);
373 //   hpad7r->Draw();
374 //   hpad7r->GetXaxis()->SetTitle("Side Right -- Pad number");
375 //   hpad7r->GetXaxis()->CenterTitle();
376 //   hpad7r->GetYaxis()->SetTitle("Number of status 7");
377 //   printf("Side 0, maximum pad=%d\n",hpad7l->GetMaximumBin());
378 //   printf("Side 1, maximum pad=%d\n",hpad7r->GetMaximumBin());
379   
380
381 }
382
383 void AnalyzeSDDInjectorsAllMod(Int_t nrun, Int_t n2, Int_t year=2009, Char_t* dir="LHC09b_SDD",
384                                Int_t adcfreq=20, 
385                                Int_t nDDL=0, 
386                                Int_t firstEv=18, 
387                                Int_t lastEv=20,
388                                Int_t jpad=20, 
389                                Int_t statuscut=7){
390
391   TGrid::Connect("alien:",0,0,"t");
392   Char_t filnam[200];
393   sprintf(filnam,"alien:///alice/data/%d/%s/%09d/raw/%02d%09d%03d.10.root",year,dir,nrun,year-2000,nrun,n2);
394   printf("Open file %s\n",filnam);
395   AnalyzeSDDInjectorsAllMod(filnam,adcfreq,nDDL,firstEv,lastEv,jpad,statuscut);
396 }
397
398
399