Add to repository the macro to plot the output of SDD calibration runs from LDCs
[u/mrichter/AliRoot.git] / ITS / macrosSDD / CheckCalibs.C
1 #if !defined(__CINT__) || defined(__MAKECINT__)
2 #include <TApplication.h>
3 #include <TGClient.h>
4 #include <TGButton.h>
5 #include <TGFrame.h>
6 #include <TGLayout.h>
7 #include <TGWindow.h>
8 #include <TGLabel.h>
9 #include <TGNumberEntry.h>
10 #include <TString.h>
11 #include <TCanvas.h>
12 #include <TStyle.h>
13 #include <TPad.h>
14 #include <TH1F.h>
15 #include <TH2F.h>
16 #include <TF1.h>
17 #include <TSystem.h>
18 #include <TGraph.h>
19 #include <TLatex.h>
20 #include <TLine.h>
21 #include <TROOT.h>
22 #endif
23
24 // Macro to display the output of SDD calibration runs
25 // -> copies the ASCII files with the DA output from the LDC
26 // -> plot various histograms and graphs
27 // Origin: F. Prino (prino@to.infn.it)
28
29 class CheckCalibInterface : public TGMainFrame {
30
31 private:
32   TGCompositeFrame    *fHor1;
33   TGCompositeFrame    *fHor2;
34   TGCompositeFrame    *fHor3;
35   TGCompositeFrame    *fHor4;
36   TGTextButton        *fCopyFiles;
37   TGTextButton        *fShowPedest;
38   TGTextButton        *fShowPulser;
39   TGTextButton        *fShowInject;
40   TGTextButton        *fShowOneMod;
41   TGTextButton        *fExit;
42   TGGroupFrame        *fGframeALL;
43   TGGroupFrame        *fGframeSING;
44   TGGroupFrame        *fGframe1;
45   TGGroupFrame        *fGframe2;
46   TGNumberEntry       *fDDL;
47   TGNumberEntry       *fChannel;
48   Int_t fNumDDL;
49   Int_t fNumChannel;
50
51 public:
52   CheckCalibInterface(const TGWindow *p, UInt_t w, UInt_t h);
53   virtual ~CheckCalibInterface();
54   void DoSetlabel();
55
56   static void CopyFiles();
57   static void ShowPedestal();
58   static void ShowPulser();
59   static void ShowInjector();
60   static void ShowSingleModule(Int_t iddl, Int_t ichan);
61   static void ClearAll();
62
63   ClassDef(CheckCalibInterface, 0)
64 };
65                           
66 CheckCalibInterface::CheckCalibInterface(const TGWindow *p, UInt_t w, UInt_t h)
67    : TGMainFrame(p, w, h)
68 {
69
70   fHor1 = new TGHorizontalFrame(this);
71   fHor2 = new TGHorizontalFrame(this);
72   fHor3 = new TGHorizontalFrame(this);
73   fHor4 = new TGHorizontalFrame(this);
74
75   TGLayoutHints *lh=new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5);
76   TGLayoutHints *lhc=new TGLayoutHints(kLHintsCenterY | kLHintsExpandY, 5, 5, 5, 5);
77   TGLayoutHints *lh1=new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5);
78
79   fCopyFiles = new TGTextButton(fHor1, "&Copy Files", "CheckCalibInterface::CopyFiles()");
80   fHor1->AddFrame(fCopyFiles, lh1);
81
82   fGframeALL = new TGGroupFrame(fHor2,"All Modules",kHorizontalFrame);
83   fShowPedest = new TGTextButton(fGframeALL, "&Show Pedestal","CheckCalibInterface::ShowPedestal()");
84   fShowPulser = new TGTextButton(fGframeALL, "&Show Pulser","CheckCalibInterface::ShowPulser()");
85   fShowInject = new TGTextButton(fGframeALL, "&Show Injector","CheckCalibInterface::ShowInjector()");    
86   fGframeALL->AddFrame(fShowPedest, lh1);
87   fGframeALL->AddFrame(fShowPulser, lh1);
88   fGframeALL->AddFrame(fShowInject, lh1);
89   fHor2->AddFrame(fGframeALL, lh1);
90
91   fGframeSING = new TGGroupFrame(fHor3,"Single Module",kHorizontalFrame);
92   fGframe1 = new TGGroupFrame(fGframeSING, "DDL");
93   fDDL = new TGNumberEntry(fGframe1, 0, 2,-1, TGNumberFormat::kNESInteger,
94                            TGNumberFormat::kNEANonNegative, 
95                            TGNumberFormat::kNELLimitMinMax,
96                            0, 23);
97   fGframe2 = new TGGroupFrame(fGframeSING, "Channel");
98   fChannel = new TGNumberEntry(fGframe2, 0, 2,-1, TGNumberFormat::kNESInteger,
99                                TGNumberFormat::kNEANonNegative, 
100                                TGNumberFormat::kNELLimitMinMax,
101                                0, 11);
102   fGframe1->AddFrame(fDDL,lh);
103   fGframe2->AddFrame(fChannel,lh);
104   fGframeSING->AddFrame(fGframe1,lh);
105   fGframeSING->AddFrame(fGframe2,lh);
106   
107   fDDL->Connect("ValueSet(Long_t)", "CheckCalibInterface", this, "DoSetlabel()");
108   (fDDL->GetNumberEntry())->Connect("ReturnPressed()", "CheckCalibInterface", this, "DoSetlabel()");
109   
110   fChannel->Connect("ValueSet(Long_t)", "CheckCalibInterface", this, "DoSetlabel()");
111   (fChannel->GetNumberEntry())->Connect("ReturnPressed()", "CheckCalibInterface", this, "DoSetlabel()");
112   fHor3->AddFrame(fGframeSING, lh1);
113   
114   fShowOneMod = new TGTextButton(fGframeSING, "&Show Selected Module","CheckCalibInterface::ShowSingleModule(0,0)");
115   fGframeSING->AddFrame(fShowOneMod, lhc);
116
117   fExit = new TGTextButton(fHor4, "&Exit", "gApplication->Terminate(0)");
118   fHor4->AddFrame(fExit, lh1);
119
120   AddFrame(fHor1,lh1);
121   AddFrame(fHor2,lh1);
122   AddFrame(fHor3,lh1);
123   AddFrame(fHor4,lh1);
124
125   SetCleanup(kDeepCleanup);
126   SetWindowName("Main Control");
127   MapSubwindows();
128   Resize(GetDefaultSize());
129   MapWindow();
130 }
131
132 CheckCalibInterface::~CheckCalibInterface()
133 {
134    // Destructor.
135    
136    Cleanup();
137 }
138
139
140 void CheckCalibInterface::CopyFiles(){
141   //
142   TString command;
143   TString ldcName[6]={"aldaqpc083","aldaqpc084","aldaqpc041",
144                       "aldaqpc082","aldaqpc085","aldaqpc086"};
145   gSystem->Exec("rm -rf calibFiles");
146   gSystem->Exec("mkdir calibFiles");
147   for(Int_t iLDC=0; iLDC<6; iLDC++){
148     Int_t firstDDL=iLDC*4;
149     Int_t lastDDL=iLDC*4+3;
150     command.Form("scp %s:/dateSite/ldc-SDD-%02d-%02d-0/work/SDDbase_step2_LDC.tar calibFiles/.",ldcName[iLDC].Data(),firstDDL,lastDDL);
151     gSystem->Exec(command.Data());
152     command.Form("scp %s:/dateSite/ldc-SDD-%02d-%02d-0/work/SDDbase_LDC.tar calibFiles/.",ldcName[iLDC].Data(),firstDDL,lastDDL);
153     gSystem->Exec(command.Data());
154     command.Form("scp %s:/dateSite/ldc-SDD-%02d-%02d-0/work/SDDinj_LDC.tar calibFiles/.",ldcName[iLDC].Data(),firstDDL,lastDDL);
155     gSystem->Exec(command.Data());
156     gSystem->Exec("cd calibFiles; tar xvf SDDbase_step2_LDC.tar; tar xvf SDDbase_LDC.tar; tar xvf SDDinj_LDC.tar; cd ..");
157   }
158   printf("-------------- DONE ---------------\n");
159
160   return;
161 }
162
163 void CheckCalibInterface::ShowPedestal(){
164   ClearAll();
165
166   TH2F* hbadchan=new TH2F("hbadchan","Number of bad channels",24,-0.25,11.75,24,-0.5,23.5);
167   TH2F* hrawnoisemod=new TH2F("hrawnoisemod","",288,-0.5,287.5,50,0.,10.);
168   TH1F* hrawnoise=new TH1F("hrawnoise","",100,0.,10.);
169   TH2F* hcornoisemod=new TH2F("hcornoisemod","",288,-0.5,287.5,50,0.,10.);
170   TH1F* hcornoise=new TH1F("hcornoise","",100,0.,10.);
171   TH2F* hbasemod=new TH2F("hbasemod","",288,-0.5,287.5,50,0.,150.);
172   TH1F* hbase=new TH1F("hbase","",100,0.,150.);
173   Int_t retfscf;
174   TString inpFileName;
175   Float_t baseline,rawnoise,cmn,corn;
176   Int_t isgoodan,i,basmin,basoff;
177   Int_t th,tl;
178   Int_t nGoodAnodes=0;
179   for(Int_t iddl=0;iddl<24;iddl++){
180     for(Int_t imod=0;imod<12;imod++){
181       for(Int_t isid=0;isid<2;isid++){
182         inpFileName.Form("./calibFiles/SDDbase_step2_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
183         FILE* basFil = fopen(inpFileName.Data(),"read");
184         Int_t sideId=imod*2+isid;
185         Int_t modId=iddl*12+imod;
186         if (basFil == 0) hbadchan->SetBinContent(sideId+1,iddl+1,256);
187         else{
188           retfscf=fscanf(basFil,"%d\n",&th);
189           retfscf=fscanf(basFil,"%d\n",&tl);
190           if(th==255 && tl==20) continue;
191           for(Int_t ian=0;ian<256;ian++){
192             retfscf=fscanf(basFil,"%d %d %f %d %d %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn);
193             hrawnoisemod->Fill(modId,rawnoise);
194             hrawnoise->Fill(rawnoise);
195             hcornoisemod->Fill(modId,corn);
196             hcornoise->Fill(corn);
197             hbasemod->Fill(modId,baseline);
198             hbase->Fill(baseline);
199             if(!isgoodan){
200               hbadchan->SetBinContent(sideId+1,iddl+1, 1+hbadchan->GetBinContent(sideId+1,iddl+1));
201             }else{
202               nGoodAnodes++;
203             }
204           }
205           fclose(basFil);
206         }
207       }
208     }
209   }
210   hrawnoisemod->SetStats(0);
211   hcornoisemod->SetStats(0);
212   hbasemod->SetStats(0);
213   hbadchan->SetStats(0);
214   gStyle->SetPalette(1);
215   TString txtCountGood;
216   Float_t fracGood=100.*(Float_t)nGoodAnodes/(520.*260.);
217   txtCountGood.Form("Number of GoodAnodes = %d (%5.1f\%)\n",nGoodAnodes,fracGood);
218
219   TCanvas* c0=new TCanvas("c0","Bad Channels",800,900);
220   c0->SetRightMargin(0.14);
221   c0->SetBottomMargin(0.2);
222   hbadchan->Draw("colz"); 
223   hbadchan->GetXaxis()->SetTitle("Channel");
224   hbadchan->GetYaxis()->SetTitle("DDL");
225   hbadchan->GetXaxis()->SetTickLength(0);
226   hbadchan->GetYaxis()->SetTickLength(0);
227   c0->cd();
228   TLine** linv=new TLine*[12];
229   for(Int_t i=0;i<12;i++){
230     linv[i]=new TLine(i+0.75,-0.5,i+0.75,23.5);
231     linv[i]->SetLineColor(kGray+1);
232     linv[i]->Draw();
233   }
234   TLine** linh=new TLine*[24];
235   for(Int_t i=0;i<24;i++){
236     linh[i]=new TLine(-0.25,i+0.5,11.75,i+0.5);
237     linh[i]->SetLineColor(kGray+1);
238     linh[i]->Draw();
239   }
240   TLatex* tg=new TLatex(0.1,0.05,txtCountGood.Data());
241   tg->SetNDC();
242   tg->SetTextColor(4);
243   tg->SetTextSize(0.04);
244   tg->Draw();
245   c0->Update();
246  
247   TCanvas* c1=new TCanvas("c1","Baseline",1200,700);
248   c1->Divide(2,1);
249   c1->cd(1);
250   gPad->SetLeftMargin(0.14);
251   gPad->SetRightMargin(0.14);
252   hbase->Draw();
253   hbase->GetXaxis()->SetTitle("Baseline (ADC)");
254   c1->cd(2);
255   gPad->SetLeftMargin(0.14);
256   gPad->SetRightMargin(0.14);
257   hbasemod->Draw("colz");
258   hbasemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
259   hbasemod->GetYaxis()->SetTitle("Baseline (ADC)");
260   hbasemod->GetYaxis()->SetTitleOffset(1.35);
261
262   TCanvas* c2=new TCanvas("c2","Raw Noise",1200,700);
263   c2->Divide(2,1);
264   c2->cd(1);
265   gPad->SetLeftMargin(0.14);
266   gPad->SetRightMargin(0.14);
267   hrawnoise->Draw();
268   hrawnoise->GetXaxis()->SetTitle("Raw Noise (ADC)");
269   c2->cd(2);
270   gPad->SetLeftMargin(0.14);
271   gPad->SetRightMargin(0.14);
272   hrawnoisemod->Draw("colz");
273   hrawnoisemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
274   hrawnoisemod->GetYaxis()->SetTitle("Raw Noise (ADC)");
275   hrawnoisemod->GetYaxis()->SetTitleOffset(1.35);
276
277   TCanvas* c3=new TCanvas("c3","Corr Noise",1200,700);
278   c3->Divide(2,1);
279   c3->cd(1);
280   gPad->SetLeftMargin(0.14);
281   gPad->SetRightMargin(0.14);
282   hcornoise->Draw();
283   hcornoise->GetXaxis()->SetTitle("Raw Noise (ADC)");
284   c3->cd(2);
285   gPad->SetLeftMargin(0.14);
286   gPad->SetRightMargin(0.14);
287   hcornoisemod->Draw("colz");
288   hcornoisemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
289   hcornoisemod->GetYaxis()->SetTitle("Raw Noise (ADC)");
290   hcornoisemod->GetYaxis()->SetTitleOffset(1.35);  
291 }
292
293 void CheckCalibInterface::ShowPulser(){
294   ClearAll();
295
296   TH2F* hbadchan=new TH2F("hbadchan","Number of bad channels",24,-0.25,11.75,24,-0.5,23.5);
297   TH2F* hrawnoisemod=new TH2F("hrawnoisemod","",288,-0.5,287.5,50,0.,10.);
298   TH1F* hrawnoise=new TH1F("hrawnoise","",100,0.,10.);
299   TH2F* hcornoisemod=new TH2F("hcornoisemod","",288,-0.5,287.5,50,0.,10.);
300   TH1F* hcornoise=new TH1F("hcornoise","",100,0.,10.);
301   TH2F* hbasemod=new TH2F("hbasemod","",288,-0.5,287.5,50,0.,150.);
302   TH1F* hbase=new TH1F("hbase","",100,0.,150.);
303   TH2F* hgainmod=new TH2F("hgainmod","",288,-0.5,287.5,50,0.,5.);
304   TH1F* hgain=new TH1F("hgain","",100,0.,5.);
305
306   Int_t retfscf;
307   TString inpFileName;
308   Float_t baseline,rawnoise,cmn,corn,gain;
309   Int_t isgoodan,i,basmin,basoff;
310   Int_t th,tl;
311   Int_t iii,jjj,kkk;
312   Int_t nGoodAnodes=0;
313
314   for(Int_t iddl=0;iddl<24;iddl++){
315     for(Int_t imod=0;imod<12;imod++){
316       for(Int_t isid=0;isid<2;isid++){
317         inpFileName.Form("./calibFiles/SDDbase_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
318         FILE* basFil = fopen(inpFileName.Data(),"read");
319         Int_t sideId=imod*2+isid;
320         Int_t modId=iddl*12+imod;
321         if (basFil == 0) hbadchan->SetBinContent(sideId+1,iddl+1,256);
322         else{
323           retfscf=fscanf(basFil,"%d %d %d\n",&iii,&jjj,&kkk);
324           if(kkk==0) continue;
325           retfscf=fscanf(basFil,"%d\n",&th);
326           retfscf=fscanf(basFil,"%d\n",&tl);
327           if(th==255 && tl==20) continue;
328           for(Int_t ian=0;ian<256;ian++){
329             retfscf=fscanf(basFil,"%d %d %f %d %d %f %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn,&gain);
330             hrawnoisemod->Fill(modId,rawnoise);
331             hrawnoise->Fill(rawnoise);
332             hcornoisemod->Fill(modId,corn);
333             hcornoise->Fill(corn);
334             hbasemod->Fill(modId,baseline);
335             hbase->Fill(baseline);
336             hgainmod->Fill(modId,gain);
337             hgain->Fill(gain);
338             if(!isgoodan){
339               hbadchan->SetBinContent(sideId+1,iddl+1, 1+hbadchan->GetBinContent(sideId+1,iddl+1));
340             }else{
341               nGoodAnodes++;
342             }
343           }
344           fclose(basFil);
345         }
346       }
347     }
348   }
349   hrawnoisemod->SetStats(0);
350   hcornoisemod->SetStats(0);
351   hbasemod->SetStats(0);
352   hgainmod->SetStats(0);
353   hbadchan->SetStats(0);
354   gStyle->SetPalette(1);
355   TString txtCountGood;
356   Float_t fracGood=100.*(Float_t)nGoodAnodes/(520.*260.);
357   txtCountGood.Form("Number of GoodAnodes = %d (%5.1f\%)\n",nGoodAnodes,fracGood);
358
359   TCanvas* c0=new TCanvas("c0","Bad Channels",800,900);
360   c0->SetRightMargin(0.14);
361   c0->SetBottomMargin(0.2);
362   hbadchan->Draw("colz"); 
363   hbadchan->GetXaxis()->SetTitle("Channel");
364   hbadchan->GetYaxis()->SetTitle("DDL");
365   hbadchan->GetXaxis()->SetTickLength(0);
366   hbadchan->GetYaxis()->SetTickLength(0);
367   c0->cd();
368   TLine** linv=new TLine*[12];
369   for(Int_t i=0;i<12;i++){
370     linv[i]=new TLine(i+0.75,-0.5,i+0.75,23.5);
371     linv[i]->SetLineColor(kGray+1);
372     linv[i]->Draw();
373   }
374   TLine** linh=new TLine*[24];
375   for(Int_t i=0;i<24;i++){
376     linh[i]=new TLine(-0.25,i+0.5,11.75,i+0.5);
377     linh[i]->SetLineColor(kGray+1);
378     linh[i]->Draw();
379   }
380   TLatex* tg=new TLatex(0.1,0.05,txtCountGood.Data());
381   tg->SetNDC();
382   tg->SetTextColor(4);
383   tg->SetTextSize(0.04);
384   tg->Draw();
385   c0->Update();
386  
387   TCanvas* c1=new TCanvas("c1","Baseline",1200,700);
388   c1->Divide(2,1);
389   c1->cd(1);
390   gPad->SetLeftMargin(0.14);
391   gPad->SetRightMargin(0.14);
392   hbase->Draw();
393   hbase->GetXaxis()->SetTitle("Baseline (ADC)");
394   c1->cd(2);
395   gPad->SetLeftMargin(0.14);
396   gPad->SetRightMargin(0.14);
397   hbasemod->Draw("colz");
398   hbasemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
399   hbasemod->GetYaxis()->SetTitle("Baseline (ADC)");
400   hbasemod->GetYaxis()->SetTitleOffset(1.35);
401
402   TCanvas* c2=new TCanvas("c2","Raw Noise",1200,700);
403   c2->Divide(2,1);
404   c2->cd(1);
405   gPad->SetLeftMargin(0.14);
406   gPad->SetRightMargin(0.14);
407   hrawnoise->Draw();
408   hrawnoise->GetXaxis()->SetTitle("Raw Noise (ADC)");
409   c2->cd(2);
410   gPad->SetLeftMargin(0.14);
411   gPad->SetRightMargin(0.14);
412   hrawnoisemod->Draw("colz");
413   hrawnoisemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
414   hrawnoisemod->GetYaxis()->SetTitle("Raw Noise (ADC)");
415   hrawnoisemod->GetYaxis()->SetTitleOffset(1.35);
416
417   TCanvas* c3=new TCanvas("c3","Corr Noise",1200,700);
418   c3->Divide(2,1);
419   c3->cd(1);
420   gPad->SetLeftMargin(0.14);
421   gPad->SetRightMargin(0.14);
422   hcornoise->Draw();
423   hcornoise->GetXaxis()->SetTitle("Raw Noise (ADC)");
424   c3->cd(2);
425   gPad->SetLeftMargin(0.14);
426   gPad->SetRightMargin(0.14);
427   hcornoisemod->Draw("colz");
428   hcornoisemod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
429   hcornoisemod->GetYaxis()->SetTitle("Raw Noise (ADC)");
430   hcornoisemod->GetYaxis()->SetTitleOffset(1.35);
431
432   TCanvas* c4=new TCanvas("c4","Gain",1200,700);
433   c4->Divide(2,1);
434   c4->cd(1);
435   gPad->SetLeftMargin(0.14);
436   gPad->SetRightMargin(0.14);
437   hgain->Draw();
438   hgain->GetXaxis()->SetTitle("Gain (ADC/DAC)");
439   c4->cd(2);
440   gPad->SetLeftMargin(0.14);
441   gPad->SetRightMargin(0.14);
442   hgainmod->Draw("colz");
443   hgainmod->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
444   hgainmod->GetYaxis()->SetTitle("Gain (ADC/DAC)");
445   hgainmod->GetYaxis()->SetTitleOffset(1.35);
446
447   return;
448 }
449
450 void CheckCalibInterface::ShowInjector(){
451   ClearAll();
452
453   TH2F* hinjstatus=new TH2F("hinjstatus","Injector Status",24,-0.25,11.75,24,-0.5,23.5);
454   TGraph *vvsmod0=new TGraph(0);
455   TGraph *vvsmod1=new TGraph(0);
456   TGraph *poldegvsmod0=new TGraph(0); 
457   TGraph *poldegvsmod1=new TGraph(0); 
458   TGraph *anmaxvsmod0=new TGraph(0); 
459   TGraph *anmaxvsmod1=new TGraph(0); 
460   TGraph *dvcevsmod0=new TGraph(0);
461   TGraph *dvcevsmod1=new TGraph(0);
462   TGraph *dveevsmod0=new TGraph(0);
463   TGraph *dveevsmod1=new TGraph(0);
464   vvsmod0->SetTitle("Drift Speed vs. mod. number");
465   vvsmod1->SetTitle("Drift Speed vs. mod. number");
466   poldegvsmod0->SetTitle("Degree of poly fit vs. mod. number");
467   poldegvsmod1->SetTitle("Degree of poly fit vs. mod. number");
468   anmaxvsmod0->SetTitle("Anode with max. vdrift vs. mod. number");
469   anmaxvsmod1->SetTitle("Anode with max. vdrift vs. mod. number");
470   dvcevsmod0->SetTitle("Delta Vdrift 128-0 vs. mod. number");
471   dvcevsmod1->SetTitle("Delta Vdrift 128-0 vs. mod. number");
472   dveevsmod0->SetTitle("Delta Vdrift 256-0 vs. mod. number");
473   dveevsmod1->SetTitle("Delta Vdrift 256-0 vs. mod. number");
474
475   TF1* fPoly=new TF1("fPoly","pol3",0.,256.);
476
477   Int_t evNumb,polDeg; 
478   UInt_t timeStamp,statusInj;
479   Int_t retfscf;
480   TString inpFileName;
481   Float_t auxP;
482   Int_t iGoodInj=0;
483   for(Int_t iddl=0;iddl<24;iddl++){
484     for(Int_t imod=0;imod<12;imod++){
485       for(Int_t isid=0;isid<2;isid++){
486         inpFileName.Form("./calibFiles/SDDinj_ddl%02dc%02d_sid%d.data",iddl,imod,isid);
487         FILE* injFil = fopen(inpFileName.Data(),"read");
488         Int_t sideId=imod*2+isid;
489         Int_t modId=iddl*12+imod;
490         if (injFil == 0){ 
491           hinjstatus->SetBinContent(sideId+1,iddl+1,0);
492         }else{
493           Bool_t firstEvent=kTRUE;
494           retfscf=fscanf(injFil,"%d",&polDeg);
495           while (!feof(injFil)){
496             retfscf=fscanf(injFil,"%d %u ",&evNumb,&timeStamp);
497             if(evNumb==-99){
498               statusInj=timeStamp;
499               Int_t n7=(statusInj&(0x1F<<25))>>25;
500               Int_t n6=(statusInj&(0x1F<<20))>>20;
501               Int_t n5=(statusInj&(0x1F<<15))>>15;
502               Int_t n4=(statusInj&(0x1F<<5))>>10;
503               Int_t n3=(statusInj&(0x1F<<5))>>5;
504               Int_t n2=statusInj&0x1F;
505               Float_t aveStatus=(7.*n7+6.*n6+5.*n5+4.*n4+3.*n3+2.*n2)/32;
506               hinjstatus->SetBinContent(sideId+1,iddl+1,aveStatus);
507             }else{
508               if(feof(injFil)) break;
509               for(Int_t ic=0;ic<4;ic++){ 
510                 retfscf=fscanf(injFil,"%f ",&auxP);
511                 fPoly->SetParameter(ic,auxP);           
512               }
513               if(firstEvent && polDeg>0){
514                 firstEvent=kFALSE;
515                 ++iGoodInj;
516                 if(isid==0){
517                   vvsmod0->SetPoint(vvsmod0->GetN(),(Float_t)modId,fPoly->Eval(128));
518                   poldegvsmod0->SetPoint(poldegvsmod0->GetN(),(Float_t)modId,polDeg);
519                   anmaxvsmod0->SetPoint(anmaxvsmod0->GetN(),(Float_t)modId,fPoly->GetMaximumX(0.,256.));
520                   dvcevsmod0->SetPoint(dvcevsmod0->GetN(),(Float_t)modId,fPoly->Eval(128)-fPoly->Eval(0));
521                   dveevsmod0->SetPoint(dveevsmod0->GetN(),(Float_t)modId,fPoly->Eval(256)-fPoly->Eval(0));
522                 }else{
523                   vvsmod1->SetPoint(vvsmod1->GetN(),(Float_t)modId,fPoly->Eval(128));
524                   poldegvsmod1->SetPoint(poldegvsmod1->GetN(),(Float_t)modId,polDeg);
525                   anmaxvsmod1->SetPoint(anmaxvsmod1->GetN(),(Float_t)modId,fPoly->GetMaximumX(0.,256.));
526                   dvcevsmod1->SetPoint(dvcevsmod1->GetN(),(Float_t)modId,fPoly->Eval(128)-fPoly->Eval(0));
527                   dveevsmod1->SetPoint(dveevsmod1->GetN(),(Float_t)modId,fPoly->Eval(256)-fPoly->Eval(0));
528                 }
529               }
530             }
531           }     
532           fclose(injFil);
533         }
534       }
535     }
536   }
537   delete fPoly;
538
539   TString countmods;
540   countmods.Form("Number of half-modules with drift speed from injectors = %d",iGoodInj);
541   gStyle->SetPalette(59);
542   hinjstatus->SetStats(0);
543   hinjstatus->SetMinimum(-0.01);
544   hinjstatus->SetMaximum(7.);
545   TCanvas* c0=new TCanvas("c0","Injector status",800,900);
546   c0->SetRightMargin(0.14);
547   c0->SetBottomMargin(0.2);
548   hinjstatus->Draw("colz"); 
549   hinjstatus->GetXaxis()->SetTitle("Channel");
550   hinjstatus->GetYaxis()->SetTitle("DDL");
551   hinjstatus->GetXaxis()->SetTickLength(0);
552   hinjstatus->GetYaxis()->SetTickLength(0);
553   c0->cd();
554   TLine** linv=new TLine*[12];
555   for(Int_t i=0;i<12;i++){
556     linv[i]=new TLine(i+0.75,-0.5,i+0.75,23.5);
557     linv[i]->SetLineColor(kGray+1);
558     linv[i]->Draw();
559   }
560   TLine** linh=new TLine*[24];
561   for(Int_t i=0;i<24;i++){
562     linh[i]=new TLine(-0.25,i+0.5,11.75,i+0.5);
563     linh[i]->SetLineColor(kGray+1);
564     linh[i]->Draw();
565   }
566   TLatex* t3=new TLatex(0.1,0.05,countmods.Data());
567   t3->SetNDC();
568   t3->SetTextColor(4);
569   t3->SetTextSize(0.03);
570   t3->Draw();
571   c0->Update();
572
573   TLatex* tleft=new TLatex(0.2,0.82,"Side 0");
574   tleft->SetNDC();
575   tleft->SetTextColor(1);
576   TLatex* tright=new TLatex(0.2,0.75,"Side 1");
577   tright->SetNDC();
578   tright->SetTextColor(2);
579
580   TCanvas* c1;
581   c1=new TCanvas("c1","Vdrift vs. mod",1000,700);  
582   vvsmod0->SetMarkerStyle(20);
583   vvsmod0->Draw("AP");
584   vvsmod0->GetXaxis()->SetLimits(-1,290);
585   vvsmod0->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
586   vvsmod0->GetYaxis()->SetTitle("Vdrift (#mum/ns)");
587   vvsmod1->SetMarkerStyle(21);
588   vvsmod1->SetMarkerColor(2);
589   vvsmod1->Draw("SAMEP");
590   tleft->Draw();
591   tright->Draw();
592   TLine* ltop=new TLine(12*8-0.5,vvsmod0->GetYaxis()->GetXmin(),12*8-0.5,vvsmod0->GetYaxis()->GetXmax());
593   ltop->SetLineColor(4);
594   ltop->Draw();
595   TLatex* ttop=new TLatex(12*3.5,vvsmod0->GetYaxis()->GetXmin()+0.05,"TOP");
596   ttop->SetTextColor(4);
597   ttop->Draw();
598   TLine* lmed=new TLine(12*16-0.5,vvsmod0->GetYaxis()->GetXmin(),12*16-0.5,vvsmod0->GetYaxis()->GetXmax());
599   lmed->SetLineColor(4);
600   lmed->Draw();
601   TLatex* tmed=new TLatex(12*11.5,vvsmod0->GetYaxis()->GetXmin()+0.05,"MED");
602   tmed->SetTextColor(4);
603   tmed->Draw();
604   TLatex* tbot=new TLatex(12*19.5,vvsmod0->GetYaxis()->GetXmin()+0.05,"BOT");
605   tbot->SetTextColor(4);
606   tbot->Draw();
607
608
609   TCanvas* c2=new TCanvas("c2","Params vs. mod",900,900);
610   c2->Divide(2,2);
611   
612   c2->cd(1);
613   gPad->SetLeftMargin(0.14);
614   poldegvsmod0->SetMarkerStyle(20);
615   poldegvsmod0->Draw("AP");
616   poldegvsmod0->GetXaxis()->SetLimits(-1,290);
617   poldegvsmod0->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
618   poldegvsmod0->GetYaxis()->SetTitle("Degree of Polynomial fit");
619   poldegvsmod0->GetYaxis()->SetTitleOffset(1.4);
620   poldegvsmod1->SetMarkerStyle(21);
621   poldegvsmod1->SetMarkerColor(2);
622   poldegvsmod1->Draw("SAMEP");
623   tleft->Draw();
624   tright->Draw();
625   c2->cd(2);
626   gPad->SetLeftMargin(0.14);
627   anmaxvsmod0->SetMarkerStyle(20);
628   anmaxvsmod0->Draw("AP");
629   anmaxvsmod0->GetXaxis()->SetLimits(-1,290);
630   anmaxvsmod0->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
631   anmaxvsmod0->GetYaxis()->SetTitle("Anode with max. drift speed");
632   anmaxvsmod0->GetYaxis()->SetTitleOffset(1.4);
633   anmaxvsmod1->SetMarkerStyle(21);
634   anmaxvsmod1->SetMarkerColor(2);
635   anmaxvsmod1->Draw("SAMEP");
636   tleft->Draw();
637   tright->Draw();
638   c2->cd(3);
639   gPad->SetLeftMargin(0.14);
640   dvcevsmod0->SetMarkerStyle(20);
641   dvcevsmod0->Draw("AP");
642   dvcevsmod0->GetXaxis()->SetLimits(-1,290);
643   dvcevsmod0->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
644   dvcevsmod0->GetYaxis()->SetTitle("vdrift(anode128)-vdrift(anode0)");
645   dvcevsmod0->GetYaxis()->SetTitleOffset(1.4);
646   dvcevsmod1->SetMarkerStyle(21);
647   dvcevsmod1->SetMarkerColor(2);
648   dvcevsmod1->Draw("SAMEP");
649   tleft->Draw();
650   tright->Draw();
651   c2->cd(4);
652   gPad->SetLeftMargin(0.14);
653   dveevsmod0->SetMarkerStyle(20);
654   dveevsmod0->Draw("AP");
655   dveevsmod0->GetXaxis()->SetLimits(-1,290);
656   dveevsmod0->GetYaxis()->SetTitleOffset(1.4);
657   dveevsmod0->GetXaxis()->SetTitle("Module index (=DDL*12+Channel)");
658   dveevsmod0->GetYaxis()->SetTitle("vdrift(anode256)-vdrift(anode0)");
659   dveevsmod1->SetMarkerStyle(21);
660   dveevsmod1->SetMarkerColor(2);
661   dveevsmod1->Draw("SAMEP");
662   tleft->Draw();
663   tright->Draw();
664
665   return;
666 }
667
668 void CheckCalibInterface::ShowSingleModule(Int_t iddl, Int_t ichan){
669   //
670   ClearAll();
671   TString inpFileName1,inpFileName2,inpFileName3;
672   Int_t retfscf;
673   Float_t baseline,rawnoise,cmn,corn,gain;
674   Int_t isgoodan,i,basmin,basoff;
675   Int_t th,tl;
676   Int_t iii,jjj,kkk;
677   Int_t evNumb,polDeg; 
678   UInt_t timeStamp,statusInj;
679   Float_t auxP;
680   TF1* fPoly=new TF1("fPoly","pol3",0.,256.);
681
682   TGraph* gbasel=new TGraph(0);
683   TGraph* gbaser=new TGraph(0);
684   TGraph* grawnl=new TGraph(0);
685   TGraph* grawnr=new TGraph(0);
686   TGraph* gcornl=new TGraph(0);
687   TGraph* gcornr=new TGraph(0);
688   TGraph* ggainl=new TGraph(0);
689   TGraph* ggainr=new TGraph(0);
690   TGraph* gstpdl=new TGraph(0);
691   TGraph* gstpdr=new TGraph(0);
692   TGraph* gstpul=new TGraph(0);
693   TGraph* gstpur=new TGraph(0);
694   TGraph* gdrspl=new TGraph(0);
695   TGraph* gdrspr=new TGraph(0);
696   gbasel->SetTitle("Baseline Left");
697   gbaser->SetTitle("Baseline Right");
698   grawnl->SetTitle("Noise Left");
699   grawnr->SetTitle("Noise Right");
700   gcornl->SetTitle("Noise Left");
701   gcornr->SetTitle("Noise Right");
702   ggainl->SetTitle("Gain Left");
703   ggainr->SetTitle("Gain Right");
704   gstpdl->SetTitle("Status Left");
705   gstpdr->SetTitle("Status Right");
706   gstpul->SetTitle("Status Left");
707   gstpur->SetTitle("Status Right");
708   gdrspl->SetTitle("Drift Speed Left");
709   gdrspr->SetTitle("Drift Speed Right");
710
711   for(Int_t isid=0;isid<2;isid++){
712     inpFileName1.Form("./calibFiles/SDDbase_step2_ddl%02dc%02d_sid%d.data",iddl,ichan,isid);
713     inpFileName2.Form("./calibFiles/SDDbase_ddl%02dc%02d_sid%d.data",iddl,ichan,isid);
714     inpFileName3.Form("./calibFiles/SDDinj_ddl%02dc%02d_sid%d.data",iddl,ichan,isid);
715   
716   
717     FILE* basFil = fopen(inpFileName1.Data(),"read");
718     if (basFil != 0){
719       retfscf=fscanf(basFil,"%d\n",&th);
720       retfscf=fscanf(basFil,"%d\n",&tl);
721       if(th==255 && tl==20) continue;
722       for(Int_t ian=0;ian<256;ian++){
723         retfscf=fscanf(basFil,"%d %d %f %d %d %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn);
724         if(isid==0){
725           gbasel->SetPoint(gbasel->GetN(),(Float_t)i,baseline);
726           grawnl->SetPoint(grawnl->GetN(),(Float_t)i,rawnoise);
727           gcornl->SetPoint(gcornl->GetN(),(Float_t)i,corn);
728           gstpdl->SetPoint(gstpdl->GetN(),(Float_t)i,(Float_t)isgoodan);
729         }else{
730           gbaser->SetPoint(gbaser->GetN(),(Float_t)i,baseline);
731           grawnr->SetPoint(grawnr->GetN(),(Float_t)i,rawnoise);
732           gcornr->SetPoint(gcornr->GetN(),(Float_t)i,corn);
733           gstpdr->SetPoint(gstpdr->GetN(),(Float_t)i,(Float_t)isgoodan);
734         }
735       }
736       fclose(basFil);
737     }
738         
739     FILE* pulFil = fopen(inpFileName2.Data(),"read");
740     if (pulFil != 0){
741       retfscf=fscanf(pulFil,"%d %d %d\n",&iii,&jjj,&kkk);
742       retfscf=fscanf(pulFil,"%d\n",&th);
743       retfscf=fscanf(pulFil,"%d\n",&tl);
744       if(th==255 && tl==20) continue;
745       for(Int_t ian=0;ian<256;ian++){
746         retfscf=fscanf(pulFil,"%d %d %f %d %d %f %f %f %f\n",&i,&isgoodan,&baseline,&basmin,&basoff,&rawnoise,&cmn,&corn,&gain);
747         if(isid==0){
748           ggainl->SetPoint(ggainl->GetN(),(Float_t)i,gain);
749           gstpul->SetPoint(gstpul->GetN(),(Float_t)i,(Float_t)isgoodan);
750         }else{
751           ggainr->SetPoint(ggainr->GetN(),(Float_t)i,gain);
752           gstpur->SetPoint(gstpur->GetN(),(Float_t)i,(Float_t)isgoodan);
753         }
754       }
755       fclose(pulFil);
756     }
757     
758     FILE* injFil = fopen(inpFileName3.Data(),"read");
759     Bool_t firstEvent=kTRUE;
760     if (injFil != 0){
761       retfscf=fscanf(injFil,"%d",&polDeg);
762       while (!feof(injFil)){
763         retfscf=fscanf(injFil,"%d %u ",&evNumb,&timeStamp);
764         if(evNumb==-99){
765           statusInj=timeStamp;
766         }else{
767           if(feof(injFil)) break;
768           for(Int_t ic=0;ic<4;ic++){ 
769             retfscf=fscanf(injFil,"%f ",&auxP);
770             fPoly->SetParameter(ic,auxP);
771           }       
772         }
773         if(firstEvent==kTRUE && polDeg>0){
774           firstEvent=kFALSE;
775           for(Int_t ian=0; ian<256; ian+=8){
776             if(isid==0) gdrspl->SetPoint(gdrspl->GetN(),(Float_t)ian,fPoly->Eval(ian));
777             else gdrspr->SetPoint(gdrspr->GetN(),(Float_t)ian,fPoly->Eval(ian));
778             
779           }
780         }
781       }
782     }
783   }
784
785
786   TCanvas * c0=new TCanvas("c0","Baselines",900,600);
787   c0->Divide(2,1);
788   c0->cd(1);
789   gPad->SetLeftMargin(0.14);
790   gbasel->SetMarkerStyle(7);
791   gbasel->Draw("AP");
792   gbasel->GetXaxis()->SetTitle("Anode");
793   gbasel->GetYaxis()->SetTitle("Baseline (ADC)");
794   gbasel->GetYaxis()->SetTitleOffset(1.35);
795   c0->cd(2);
796   gPad->SetLeftMargin(0.14);
797   gbaser->SetMarkerStyle(7);
798   gbaser->Draw("AP");
799   gbaser->GetXaxis()->SetTitle("Anode");
800   gbaser->GetYaxis()->SetTitle("Baseline (ADC)");
801   gbaser->GetYaxis()->SetTitleOffset(1.35);
802
803   TCanvas * c1=new TCanvas("c1","Noise",900,600);
804   c1->Divide(2,1);
805   c1->cd(1);
806   gPad->SetLeftMargin(0.14);
807   TLatex* t1=new TLatex(0.6,0.8,"Raw");
808   t1->SetNDC();
809   TLatex* t2=new TLatex(0.6,0.75,"Corrected");
810   t2->SetTextColor(2);
811   t2->SetNDC();
812   grawnl->SetMarkerStyle(7);
813   grawnl->Draw("AP");
814   grawnl->GetXaxis()->SetTitle("Anode");
815   grawnl->GetYaxis()->SetTitle("Noise (ADC)");
816   grawnl->GetYaxis()->SetTitleOffset(1.35);
817   gcornl->SetMarkerStyle(7);
818   gcornl->SetMarkerColor(2);
819   gcornl->Draw("SAMEP");
820   t1->Draw();
821   t2->Draw();
822   c1->cd(2);
823   gPad->SetLeftMargin(0.14);
824   grawnr->SetMarkerStyle(7);
825   grawnr->Draw("AP");
826   grawnr->GetXaxis()->SetTitle("Anode");
827   grawnr->GetYaxis()->SetTitle("Noise (ADC)");
828   grawnr->GetYaxis()->SetTitleOffset(1.35);
829   gcornr->SetMarkerStyle(7);
830   gcornr->SetMarkerColor(2);
831   gcornr->Draw("SAMEP");
832   t1->Draw();
833   t2->Draw();
834  
835   TCanvas * c2=new TCanvas("c2","Gain",900,600);
836   c2->Divide(2,1);
837   c2->cd(1);
838   gPad->SetLeftMargin(0.14);
839   ggainl->SetMarkerStyle(7);
840   ggainl->Draw("AP");
841   ggainl->GetXaxis()->SetTitle("Anode");
842   ggainl->GetYaxis()->SetTitle("Gain (ADC/DAC)");
843   ggainl->GetYaxis()->SetTitleOffset(1.35);
844   c2->cd(2);
845   gPad->SetLeftMargin(0.14);
846   ggainr->SetMarkerStyle(7);
847   ggainr->Draw("AP");
848   ggainr->GetXaxis()->SetTitle("Anode");
849   ggainr->GetYaxis()->SetTitle("Gain (ADC/DAC)");
850   ggainr->GetYaxis()->SetTitleOffset(1.35);
851
852   TCanvas * c3=new TCanvas("c3","Anode Status",900,600);
853   c3->Divide(2,1);
854   c3->cd(1);
855   gPad->SetLeftMargin(0.14);
856   TLatex* t3=new TLatex(0.6,0.85,"Pedestal");
857   t3->SetNDC();
858   TLatex* t4=new TLatex(0.6,0.8,"Pulser");
859   t4->SetTextColor(4);
860   t4->SetNDC();
861   gstpdl->SetMarkerStyle(7);
862   gstpdl->Draw("AP");
863   gstpdl->SetMinimum(-0.001);
864   gstpdl->SetMaximum(1.2);
865   gstpdl->GetXaxis()->SetTitle("Anode");
866   gstpdl->GetYaxis()->SetTitle("Status (1=OK, 0=BAD)");
867   gstpdl->GetYaxis()->SetTitleOffset(1.35);
868   gstpul->SetMarkerStyle(7);
869   gstpul->SetMarkerColor(4);
870   gstpul->Draw("SAMEP");
871   t3->Draw();
872   t4->Draw();
873   c3->cd(2);
874   gPad->SetLeftMargin(0.14);
875   gstpdr->SetMarkerStyle(7);
876   gstpdr->Draw("AP");
877   gstpdr->SetMinimum(-0.001);
878   gstpdr->SetMaximum(1.2);
879   gstpdr->GetXaxis()->SetTitle("Anode");
880   gstpdr->GetYaxis()->SetTitle("Status (1=OK, 0=BAD)");
881   gstpdr->GetYaxis()->SetTitleOffset(1.35);
882   gstpur->SetMarkerStyle(7);
883   gstpur->SetMarkerColor(4);
884   gstpur->Draw("SAMEP");
885   t3->Draw();
886   t4->Draw();
887
888   TCanvas * c4=new TCanvas("c4","Drift Speed",900,600);
889   c4->Divide(2,1);
890   c4->cd(1);
891   gPad->SetLeftMargin(0.14);
892   gdrspl->SetMarkerStyle(7);
893   gdrspl->Draw("APL");
894   gdrspl->GetXaxis()->SetTitle("Anode");
895   gdrspl->GetYaxis()->SetTitle("Drift Speed (#mum/ns)");
896   gdrspl->GetYaxis()->SetTitleOffset(1.35);
897   c4->cd(2);
898   gPad->SetLeftMargin(0.14);
899   gdrspr->SetMarkerStyle(7);
900   gdrspr->Draw("APL");
901   gdrspr->GetXaxis()->SetTitle("Anode");
902   gdrspr->GetYaxis()->SetTitle("Drift Speed (#mum/ns)");
903   gdrspr->GetYaxis()->SetTitleOffset(1.35);
904
905   return;
906 }
907
908 void CheckCalibInterface::DoSetlabel()
909 {
910    // Slot method connected to the ValueSet(Long_t) signal.
911    // It displays the value set in TGNumberEntry widget.
912   fNumDDL=fDDL->GetNumberEntry()->GetIntNumber();
913   fNumChannel=fChannel->GetNumberEntry()->GetIntNumber();
914   fShowOneMod->SetCommand(Form("CheckCalibInterface::ShowSingleModule(%d,%d)",
915                                fNumDDL,fNumChannel));
916 }
917
918 void CheckCalibInterface::ClearAll(){
919
920   gROOT->Clear();
921   if(gROOT->FindObject("c0")) delete gROOT->FindObject("c0");
922   if(gROOT->FindObject("c1")) delete gROOT->FindObject("c1");
923   if(gROOT->FindObject("c2")) delete gROOT->FindObject("c2");
924   if(gROOT->FindObject("c3")) delete gROOT->FindObject("c3");
925   if(gROOT->FindObject("c4")) delete gROOT->FindObject("c4");
926   if(gROOT->FindObject("hbadchan")) delete gROOT->FindObject("hbadchan");
927   if(gROOT->FindObject("hrawnoisemod")) delete gROOT->FindObject("hrawnoisemod");
928   if(gROOT->FindObject("hrawnoise")) delete gROOT->FindObject("hrawnoise");
929   if(gROOT->FindObject("hcornoisemod")) delete gROOT->FindObject("hcornoisemod");
930   if(gROOT->FindObject("hcornoise")) delete gROOT->FindObject("hcornoise");
931   if(gROOT->FindObject("hbasemod")) delete gROOT->FindObject("hbasemod");
932   if(gROOT->FindObject("hbase")) delete gROOT->FindObject("hbase");
933   if(gROOT->FindObject("hgainmod")) delete gROOT->FindObject("hgainmod");
934   if(gROOT->FindObject("hgain")) delete gROOT->FindObject("hgainnoise");
935
936 }
937
938 void CheckCalibs()
939 {
940    new CheckCalibInterface(gClient->GetRoot(), 400, 400); 
941 }