reading RAW without data
[u/mrichter/AliRoot.git] / T0 / AliT0CalibLaserData.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id:  */
17
18 //____________________________________________________________________
19 //                                                                          
20 // T0 - T0. 
21 //
22 // This class privides GIU service for reading RAW data from Laser
23 // during electronics test 
24 //                                                       
25 #include "TH1F.h"
26 #include "TH2F.h"
27 #include "TMap.h"
28 #include "TFile.h"
29 #include "TTree.h"
30 #include "TBranch.h"
31 #include "AliT0RawReader.h"
32 #include "TGLabel.h"
33 #include "TGFileDialog.h"
34 //#include <iostream.h>
35
36 #include "AliT0CalibLaserData.h"
37
38 #include "AliCDBManager.h"
39 #include "AliRawReader.h"
40 #include "AliRawReaderRoot.h"
41 #include "AliT0LookUpValue.h"
42 #include "AliT0LookUpKey.h"
43 #include "AliT0Parameters.h"
44 #include "AliT0RawReader.h"
45
46 ClassImp(AliT0CalibLaserData)
47   //const char *fFileName;
48 AliT0CalibLaserData::AliT0CalibLaserData() : TObject(),
49                                              fTEntry(0),
50                                              fFileName(" ")
51   
52 {
53 //
54 }
55 /*
56 //________________________________________________________________
57
58 AliT0CalibLaserData::AliT0CalibLaserData(const AliT0CalibLaserData& calibda) : TObject(),
59                                          fRunNumber(905)
60 {
61 //copy constructor
62   
63 }
64 //________________________________________________________________
65
66 AliT0CalibLaserData &AliT0CalibLaserData::operator =(const AliT0CalibLaserData& calibda)
67 {
68 // assignment operator
69
70   return *this;
71 }
72 //________________________________________________________________
73 AliT0CalibLaserData::~AliT0CalibLaserData()
74 {
75   //
76 }
77 */
78 //________________________________________________________________
79
80 void AliT0CalibLaserData::ReadHistSize()
81 {
82   //build GUI frame for reading:
83   // - run number
84   // - histograms rates
85
86     
87     TGMainFrame* fMain = new TGMainFrame(0,1500,1500);
88     fMain->SetLayoutManager( new TGMatrixLayout(fMain,7,7) );
89  
90     fMain->AddFrame( new TGLabel(fMain, " Histogram") );
91     fMain->AddFrame( new TGLabel(fMain, "X min") );
92     fMain->AddFrame( new TGLabel(fMain, "X max") );
93     fMain->AddFrame( new TGLabel(fMain, "X N# channels") );
94   
95     fMain->AddFrame( new TGLabel(fMain, "Y min") );
96     fMain->AddFrame( new TGLabel(fMain, "Y max") );
97     fMain->AddFrame( new TGLabel(fMain, "Y N# channels") );
98
99     fMain->AddFrame( new TGLabel(fMain, "QTC" ) );
100     fEntries[0] = new TGNumberEntry(fMain, 0);
101     fMain->AddFrame(fEntries[0]);
102     fEntries[1] = new TGNumberEntry(fMain, 10000);
103     fMain->AddFrame(fEntries[1]);
104     fEntries[2] = new TGNumberEntry(fMain, 10000);
105     fMain->AddFrame(fEntries[2]);
106     fMain->AddFrame( new TGLabel(fMain, " ") );
107     fMain->AddFrame( new TGLabel(fMain, " ") );
108     fMain->AddFrame( new TGLabel(fMain, " ") );
109   
110     fMain->AddFrame( new TGLabel(fMain, "LED - CFD" ) );
111     fEntries[3] = new TGNumberEntry(fMain, 0);
112     fMain->AddFrame(fEntries[3]);
113     fEntries[4] = new TGNumberEntry(fMain, 1000);
114     fMain->AddFrame(fEntries[4]);
115     fEntries[5] = new TGNumberEntry(fMain, 1000);
116     fMain->AddFrame(fEntries[5]);
117     fMain->AddFrame( new TGLabel(fMain, " ") );
118     fMain->AddFrame( new TGLabel(fMain, " ") );
119     fMain->AddFrame( new TGLabel(fMain, " ") );
120     fMain->AddFrame( new TGLabel(fMain, "CFD vs QTC " ) );
121  // QTC axis X
122     fEntries[6] = new TGNumberEntry(fMain, 1000.);
123     fMain->AddFrame(fEntries[6]);
124     fEntries[7] = new TGNumberEntry(fMain, 8000.5);
125     fMain->AddFrame(fEntries[7]);
126     fEntries[8] = new TGNumberEntry(fMain, 700);
127     fMain->AddFrame(fEntries[8]);
128 // CFD axis Y 
129    fEntries[9] = new TGNumberEntry(fMain, 2000.5);
130     fMain->AddFrame(fEntries[9]);
131     fEntries[10] = new TGNumberEntry(fMain, 3000.5);
132     fMain->AddFrame(fEntries[10]);
133     fEntries[11] = new TGNumberEntry(fMain, 1000);
134     fMain->AddFrame(fEntries[11]);
135 //
136     fMain->AddFrame( new TGLabel(fMain, "CFD vs LED-CFD " ) );
137 //LED-CFD axis X
138     fEntries[12] = new TGNumberEntry(fMain, 0);
139     fMain->AddFrame(fEntries[12]);
140     fEntries[13] = new TGNumberEntry(fMain, 500);
141     fMain->AddFrame(fEntries[13]);
142     fEntries[14] = new TGNumberEntry(fMain, 500);
143     fMain->AddFrame(fEntries[14]);
144 // CFD axis Y
145     fEntries[15] = new TGNumberEntry(fMain, 2000);
146     fMain->AddFrame(fEntries[15]);
147     fEntries[16] = new TGNumberEntry(fMain, 3000);
148     fMain->AddFrame(fEntries[16]);
149     fEntries[17] = new TGNumberEntry(fMain, 1000);
150     fMain->AddFrame(fEntries[17]);
151  
152    fMain->AddFrame( new TGLabel(fMain, " File name") );
153    fTEntry = new TGTextEntry(fMain,"");
154    fMain->AddFrame(fTEntry);
155    
156    for ( int i=0; i<18; i++ ) fEntries[i]->SetWidth(70);
157    fTEntry->SetWidth(80);
158     //    printf( "Max Length %d\n", fEntries[0]->GetMaxWidth() );
159
160     TGTextButton *fOk = new TGTextButton(fMain, "OK");
161     fOk->Connect("Clicked()","AliT0CalibLaserData",this,"DoOk()");
162     //    fOk->SetCommand(".q");
163     fMain->AddFrame(fOk);
164     
165     fMain->MapSubwindows();
166     fMain->Resize();
167     fMain->SetWindowName("Dialog");
168     fMain->MapWindow();
169 }
170     
171 void AliT0CalibLaserData::DoOk()
172 {
173   
174   fFileName = (fTEntry->GetText());
175   //OpenFile();
176   printf(" DoOK >>  File %s\n",fFileName);
177   for( int i=0; i<18; i++ ) 
178     fHistLimits[i] = fEntries[i]->GetNumber();
179   
180   ReadData();
181 }
182
183 void AliT0CalibLaserData::ReadData()
184 {
185   // reading RAW data from test LCS
186   // filling histograms
187   // fillinf tree
188   
189   
190   TH1F *hChannel[105];  TH1F *hQTC[24];  
191   TH2F *hCFDvsQTC[24]; TH2F *hCFDvsLED[24]; TH1F *h1CFDminLED[24];
192
193   Int_t allData[110][5];
194   Int_t numberOfHits[105];
195   
196   Char_t  buf1[20], buf2[20], buf3[20], buf4[20], buf7[20];
197
198   TTree* digitsTree = new TTree("testData","Tree of test data Digits");
199   TBranch *b[106];
200   
201   Int_t channels[106];
202   
203   TString names[106], type;
204   AliT0LookUpKey* lookkey= new AliT0LookUpKey();
205   AliT0LookUpValue*  lookvalue= new AliT0LookUpValue();
206   AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
207   AliCDBManager::Instance()->SetRun(0);
208   AliT0Parameters *fParam = AliT0Parameters::Instance();
209   fParam->Init();
210   TMap *lookup = fParam->GetMapLookup();
211   TMapIter *iter = new TMapIter(lookup);
212   
213   for( Int_t iline=0; iline<106; iline++)
214     {
215       lookvalue = ( AliT0LookUpValue*) iter->Next();
216       lookkey = (AliT0LookUpKey*) lookup->GetValue((TObject*)lookvalue);
217       if(lookkey){
218         Int_t key=lookkey->GetKey();
219         names[key]=lookkey->GetChannelName();
220         if(names[key].Contains("QT0"))
221           hChannel[key] = new TH1F(names[key].Data(),names[key].Data(),1000,3000,5000);
222         else
223           hChannel[key] = new TH1F(names[key].Data(),names[key].Data(),1000,2000,3000);
224         
225         //      hitsname="xHits" + names[key];
226         //      hNumHits[key] = new TH1F(hitsname.Data(),hitsname.Data(),50,-0.25,24.25);
227         type =names[key] + "/I";
228         b[key]=digitsTree->Branch(names[key].Data(),&channels[key], type);
229       }
230       else
231         {printf(" no such value %i \n", iline);}
232       
233     } 
234   for(Int_t ic=0; ic<24; ic++) {
235     {
236       sprintf(buf1,"QTC%i",ic+1);
237       sprintf(buf2,"CFDvsQTC%i",ic+1);
238       sprintf(buf3,"CFDvsLED%i",ic+1);
239       sprintf(buf4,"LEDminCFD%i",ic+1);
240       sprintf(buf7,"mpd%i",ic+1);
241       
242       hQTC[ic] = new TH1F(buf1,"QTC",(Int_t)fHistLimits[2],fHistLimits[0],fHistLimits[1]);
243       h1CFDminLED[ic] = new TH1F(buf4,"LED - CFD",(Int_t)fHistLimits[5],fHistLimits[3],fHistLimits[4]);
244       
245       hCFDvsQTC[ic] = new TH2F(buf2,"CFD vs     QTC",
246                                (Int_t)fHistLimits[8],fHistLimits[6],fHistLimits[7],
247                                (Int_t)fHistLimits[11],fHistLimits[9],fHistLimits[10]);
248       hCFDvsLED[ic] = new TH2F(buf3,"CFD vs LED-CFD",
249                                (Int_t)fHistLimits[14],fHistLimits[12],fHistLimits[13],
250                                (Int_t)fHistLimits[17],fHistLimits[15],fHistLimits[16]);
251
252       
253     }
254
255
256   }
257   
258   TH1F*hEffCFD= new TH1F("hEffCFD","Effeciency",50,-0.25,24.25);
259   TH1F*hEffLED= new TH1F("hEffLED","Effeciency",50,-0.25,24.25);
260   TH1F*hEffQT0= new TH1F("hEffQT0","Effeciency",50,-0.25,24.25);
261   TH1F*hEffQT1= new TH1F("hEffQT1","Effeciency",50,-0.25,24.25);
262
263       
264      // Char_t filename[80];
265   //     sprintf(filename,"t0%i.raw",fRunNumber);
266   AliRawReader *reader = new AliRawReaderRoot(fFileName);
267    reader->LoadEquipmentIdsMap("T0map.txt");
268   //    reader->RequireHeader(kFALSE);
269   reader->RequireHeader(kTRUE);
270   AliT0RawReader *start = new AliT0RawReader(reader);
271   //  start->SetNumberOfTRM(1);
272   for (Int_t i0=0; i0<105; i0++)
273     {
274       for (Int_t j0=0; j0<5; j0++) allData[i0][j0]=0;   
275       numberOfHits[i0]=0;
276     }
277   Int_t event=0;
278   
279   while (reader->NextEvent()) {
280     start->Next();
281     for (Int_t i=0; i<105; i++) {
282       for (Int_t iHit=0; iHit<5; iHit++) 
283         {
284           allData[i][iHit]= start->GetData(i,iHit);
285         }
286     }
287     
288     // if(event%1000 == 0) 
289     printf("Event:%d\n",event);
290     
291     //          if(event > 200000) break;
292     
293     for (Int_t it = 0; it<24; it=it+2)
294       {
295         for (Int_t iHit=0; iHit<5; iHit++)
296           {
297             if(allData[it+25][iHit] != 0 && allData[it+26][iHit] !=0)
298               {
299                 Int_t cc=it/2;
300                 hQTC[cc]->Fill(allData[it+25][iHit]-allData[it+26][iHit]);
301                 if(allData[cc+1][iHit] != 0 ) hCFDvsQTC[cc]->Fill(allData[it+25][iHit]-allData[it+26][iHit],allData[cc+1][iHit]-allData[0][0]);
302                 if(allData[cc+1][iHit] != 0 && allData[cc+13][iHit]!=0 ) 
303                   {
304                     hCFDvsLED[cc]->Fill(allData[cc+13][iHit]-allData[cc+1][iHit],allData[cc+1][iHit]-allData[0][0]);  
305                     h1CFDminLED[cc]->Fill(allData[cc+13][iHit]-allData[cc+1][iHit]);
306                   }
307               }
308           }
309       }
310     for (Int_t it = 24; it<48; it=it+2)
311       {
312         for (Int_t iHit=0; iHit<5; iHit++)
313           {
314             if(allData[it+57][iHit] != 0 && allData[it+58][iHit] !=0)
315               {
316                 Int_t cc=(Int_t)(it/2.0);
317                 hQTC[cc]->Fill(allData[it+57][iHit]-allData[it+58][iHit]);
318                 //                  hmpd[cc]->Fill(allData[it+26][iHit]-allData[it+25][iHit]);
319               if(allData[cc+1][iHit] != 0 ) hCFDvsQTC[cc]->Fill(allData[it+25][iHit]-allData[it+26][iHit],allData[cc+45][iHit]-allData[0][0]);
320               if(allData[cc+57][iHit] != 0 && allData[cc+45][iHit]!=0 ) 
321                 {
322                   hCFDvsLED[cc]->Fill(allData[cc+57][iHit]-allData[cc+45][iHit],allData[cc+1][iHit]-allData[0][0]);  
323                   h1CFDminLED[cc]->Fill(allData[cc+57][iHit]-allData[cc+45][iHit]);
324                 }
325               }
326         }
327       }
328     
329     
330     for (Int_t iHit=0; iHit<5; iHit++) 
331       {
332         
333         for(Int_t ik=1; ik<105; ik++)
334           { 
335             channels[ik] = -100;
336             if((allData[ik][iHit] - allData[0][0]) > 0 ) 
337               {
338                 numberOfHits[ik]++;
339                 hChannel[ik] -> Fill(allData[ik][iHit] - allData[0][0]);
340                 channels[ik] = allData[ik][iHit] - allData[0][0];
341               }
342           }
343               digitsTree->Fill();   
344       }
345
346     event++;
347     
348   } //event
349
350
351   if (event>1)
352     {
353       printf("efficiency for %i events \n",event);
354       for (Int_t i0=1; i0<13;  i0++)
355         {
356           printf("%s  %f  %s  %f %s  %f  %s  %f \n",
357                  names[i0].Data(), Float_t(numberOfHits[i0])/Float_t(event),
358                  names[i0+13].Data(),Float_t(numberOfHits[i0+13])/Float_t(event),
359                  names[i0+57].Data(),Float_t(numberOfHits[i0+57])/Float_t(event),
360                  names[i0+69].Data(),Float_t(numberOfHits[i0+69])/Float_t(event));
361           
362           hEffCFD->Fill(i0,Float_t(numberOfHits[i0])  / Float_t(event));
363           hEffLED->Fill(i0,Float_t(numberOfHits[i0+13]) / Float_t(event));
364           hEffCFD->Fill(i0+12,Float_t(numberOfHits[i0+57]) /Float_t(event));
365           hEffLED->Fill(i0+12,Float_t(numberOfHits[i0+69]) /Float_t(event));
366         }
367       
368       for (Int_t i0=0; i0<24;  i0=i0+2)
369         {
370           hEffQT1->Fill(i0, Float_t (numberOfHits[i0+25]) / Float_t(event));
371           hEffQT0->Fill(i0, Float_t (numberOfHits[i0]+26) / Float_t(event));
372           hEffQT1->Fill((i0+12), Float_t (numberOfHits[i0]+81) /  Float_t(event));
373           hEffQT0->Fill((i0+12), Float_t (numberOfHits[i0]+82) /  Float_t(event));
374
375           printf("%s  %f  %s  %f %s  %f  %s  %f \n",
376                  names[i0+25].Data(), Float_t(numberOfHits[i0+25])/Float_t(event),
377                  names[i0+26].Data(),Float_t(numberOfHits[i0+26])/Float_t(event),
378                  names[i0+81].Data(),Float_t(numberOfHits[i0+81])/Float_t(event),
379                  names[i0+82].Data(),Float_t(numberOfHits[i0+82])/Float_t(event));
380         }
381     }         
382   
383
384   Char_t filehist[40]; 
385   sprintf(filehist,"t0tree%s",fFileName);
386   //  sprintf(filehist,"test.root",runNumber);
387      TFile *hist = new TFile(filehist,"RECREATE");
388      hist->cd();
389      //  digitsTree->Write("",TObject::kOverwrite);
390      
391      hEffCFD->Write();
392      hEffLED->Write();
393      hEffQT0->Write();
394       hEffQT1->Write();
395       
396       for(Int_t ik=0; ik<105; ik++)     hChannel[ik] ->Write();
397       
398       for (Int_t i=0; i<24; i++)
399         {
400           hQTC[i]->Write();
401           hCFDvsQTC[i]->Write();
402           hCFDvsLED[i]->Write();
403           h1CFDminLED[i]->Write();
404         }
405       
406 }
407
408 void AliT0CalibLaserData::OpenFile()
409 {
410
411 const char *ft[]={"T0 raw files","*.root","All files","*",0,0};
412   TString dir(".");
413   TGFileInfo fi; fi.fFileTypes=ft; fi.fIniDir=StrDup(dir);
414   new TGFileDialog(gClient->GetRoot(), 0x0, kFDOpen, &fi);
415   if(!fi.fFilename) return;
416   // fFileName =*( fi.fFilename);
417   fFileName = fi.fFilename;
418   printf(" AliT0CalibLaserData::OpenFile %s %s\n",fi.fFilename, fFileName );
419   //  if(gFile){ gFile->Close(); gFile=0;}
420
421   // gFile=TFile::Open(fi.fFilename);
422
423 }
424