]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HMPID/AliHMPIDCalib.cxx
Last modification from LM in agreement with Paolo. Pedestal file structure changed...
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDCalib.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 #include "AliHMPIDCalib.h" //class header
17 #include "AliHMPIDParam.h" //class header
18 #include "AliHMPIDRawStream.h" //class header
19 #include "AliHMPIDDigit.h" //class header
20 #include <fstream>
21 #include <TTree.h>
22 #include <TSystem.h>
23 #include <TTimeStamp.h>
24
25
26
27
28
29 ClassImp(AliHMPIDCalib) 
30
31
32 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33 AliHMPIDCalib::AliHMPIDCalib():
34 faddl(0x0),
35 fsq(0x0),
36 fsq2(0x0),
37 fnpc(0x0),
38 fpedQ0(0x0),
39 fErr(0x0),
40 fPadAdc(0x0),
41 fIsPad(0x0),
42 fFile(0x0),
43 fLdcId(0),
44 fTimeStamp(0),
45 fRunNum(0),
46 fSigCut(0),
47 fWritePads(0),
48 fnDDLInStream(0x0),
49 fnDDLOutStream(0x0),
50 fLargeHisto(kFALSE),
51 fSelectDDL(0),  
52 fDaOut(0),
53 fFeeIn(0)
54 {
55   //
56   //constructor
57   //
58   faddl = new Bool_t[AliHMPIDRawStream::kNDDL];
59   Int_t nPads =  (AliHMPIDParam::kMaxCh+1)*(AliHMPIDParam::kMaxPcx+1)*(AliHMPIDParam::kMaxPcy+1);
60  
61   fpedQ0 = new Int_t***[AliHMPIDRawStream::kNDDL+1];
62   fsq2   = new Float_t ***[AliHMPIDRawStream::kNDDL+1];
63   fsq    = new Float_t ***[AliHMPIDRawStream::kNDDL+1];
64   fnpc   = new Int_t ***[AliHMPIDRawStream::kNDDL+1];
65     fErr = new Int_t*[AliHMPIDRawStream::kNDDL+1];
66    
67   fnDDLInStream  = new Int_t[AliHMPIDRawStream::kNDDL+1];
68   fnDDLOutStream = new Int_t[AliHMPIDRawStream::kNDDL+1];
69
70   
71   for(Int_t iDDL=0;iDDL<AliHMPIDRawStream::kNDDL+1;iDDL++) {
72     
73       fErr[iDDL] = new Int_t[AliHMPIDRawStream::kSumErr+1];
74     fpedQ0[iDDL] = new Int_t**[AliHMPIDRawStream::kNRows+1];
75        fsq[iDDL] = new Float_t**[AliHMPIDRawStream::kNRows+1];
76       fsq2[iDDL] = new Float_t**[AliHMPIDRawStream::kNRows+1];
77       fnpc[iDDL] = new Int_t**[AliHMPIDRawStream::kNRows+1];
78       
79       for(Int_t iRow=0;iRow<AliHMPIDRawStream::kNRows+1;iRow++)  {
80       
81        fpedQ0[iDDL][iRow] = new Int_t*[AliHMPIDRawStream::kNDILOGICAdd+1];
82           fsq[iDDL][iRow] = new Float_t*[AliHMPIDRawStream::kNDILOGICAdd+1];
83          fsq2[iDDL][iRow] = new Float_t*[AliHMPIDRawStream::kNDILOGICAdd+1];
84          fnpc[iDDL][iRow] = new Int_t*[AliHMPIDRawStream::kNDILOGICAdd+1];
85       
86         for(Int_t iDil=1;iDil<AliHMPIDRawStream::kNDILOGICAdd+1;iDil++){
87       
88          fpedQ0[iDDL][iRow][iDil] = new Int_t[AliHMPIDRawStream::kNPadAdd+1];
89            fsq2[iDDL][iRow][iDil] = new Float_t[AliHMPIDRawStream::kNPadAdd+1];
90             fsq[iDDL][iRow][iDil] = new Float_t[AliHMPIDRawStream::kNPadAdd+1];
91            fnpc[iDDL][iRow][iDil] = new Int_t[AliHMPIDRawStream::kNPadAdd+1];
92           }//iDil
93       }//iRow
94    }//iDDL
95     
96    for(Int_t iDDL=0;iDDL<AliHMPIDRawStream::kNDDL+1;iDDL++) {
97         
98      fnDDLInStream[iDDL]=-1;
99      fnDDLOutStream[iDDL]=-1;
100       
101      for(Int_t iErr=0;iErr<AliHMPIDRawStream::kSumErr+1;iErr++)  {fErr[iDDL][iErr]=0;}
102          
103      for(Int_t iRow=0;iRow<AliHMPIDRawStream::kNRows+1;iRow++) {
104         for(Int_t iDil=1;iDil<AliHMPIDRawStream::kNDILOGICAdd+1;iDil++) {
105           for(Int_t iPad=1;iPad<AliHMPIDRawStream::kNPadAdd+1;iPad++) {
106             fpedQ0[iDDL][iRow][iDil][iPad]=0;
107                fsq[iDDL][iRow][iDil][iPad]=0;
108               fsq2[iDDL][iRow][iDil][iPad]=0;
109               fnpc[iDDL][iRow][iDil][iPad]=0;
110         }//iPad
111       }//iDil
112      }//iRow
113    }//iDDL
114     
115   fPadAdc=new TH1I*[nPads];  
116   fIsPad=new Bool_t[nPads];  
117   for(Int_t np=0;np<nPads;np++) {fPadAdc[np]=0x0;   fIsPad[np]=kFALSE;}
118   fWritePads=kFALSE;
119
120
121   Init();
122 }
123 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
124 AliHMPIDCalib::~AliHMPIDCalib()
125 {
126   //
127   //destructor
128   //
129   if (faddl)     { delete [] faddl;   faddl = 0x0;  } 
130   if (fPadAdc)   { delete [] fPadAdc; fPadAdc=0x0;  }  
131   if (fIsPad)    { delete [] fIsPad;  fIsPad=0x0;   }  
132   if (fFile)     { delete    fFile;   fFile=0x0;    }  
133  
134   for(Int_t iErr=0;iErr<AliHMPIDRawStream::kSumErr+1;iErr++) { delete [] fErr[iErr];}  delete [] fErr;
135   
136   for(Int_t iDDL=0; iDDL< AliHMPIDRawStream::kNDDL; iDDL++) 
137    for(Int_t iRow=0;iRow<AliHMPIDRawStream::kNRows+1;iRow++)         
138      for(Int_t iDil=1;iDil<AliHMPIDRawStream::kNDILOGICAdd+1;iDil++)
139       {
140          delete [] fpedQ0[iDDL][iRow][iDil]; //del iPad
141          delete []    fsq[iDDL][iRow][iDil]; //del iPad
142          delete []   fsq2[iDDL][iRow][iDil]; //del iPad
143          delete []   fnpc[iDDL][iRow][iDil]; //del iPad
144        }
145    for(Int_t iDDL=0; iDDL< AliHMPIDRawStream::kNDDL; iDDL++) 
146      for(Int_t iRow=0;iRow<AliHMPIDRawStream::kNRows+1;iRow++)         
147       {
148         delete [] fpedQ0[iDDL][iRow];  //del iRow
149           delete []  fsq[iDDL][iRow];  //del iRow
150           delete [] fsq2[iDDL][iRow];  //del iRow
151           delete [] fnpc[iDDL][iRow];  //del iRow
152         }
153        
154    for(Int_t iDDL=0; iDDL< AliHMPIDRawStream::kNDDL; iDDL++) 
155    {   
156        delete [] fpedQ0[iDDL];        //del iRow
157          delete [] fsq2[iDDL];        //del iRow
158          delete []  fsq[iDDL];        //del iRow
159          delete [] fnpc[iDDL];        //del iRow
160      }
161        
162    delete [] fpedQ0;
163    delete [] fsq2;
164    delete [] fsq;
165    delete [] fnpc;
166     
167   fpedQ0=0;    
168     fsq2=0;
169      fsq=0;
170     fnpc=0;
171     
172   fLdcId=0;
173   fTimeStamp=0;
174   fRunNum=0;
175   fSigCut=0;
176   fWritePads=0;
177   fLargeHisto=kFALSE;
178   fSelectDDL=0;
179 }//dtor
180 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
181 void AliHMPIDCalib::Init()
182 {
183   //
184   //Init the q calc.
185   //Arguments: none
186   //Return: none
187   //
188     
189   fSigCut=3;  //the standard cut
190   fFeeIn="";
191   fDaOut="";
192   
193       
194   for(Int_t iDDL=0; iDDL< AliHMPIDRawStream::kNDDL; iDDL++) 
195       {
196          for(Int_t ierr=0; ierr <AliHMPIDRawStream::kSumErr ; ierr++) {
197             fErr[iDDL][ierr]=0;
198             }
199         
200         faddl[iDDL]=kFALSE;
201       }//DDL
202 }//Init()
203 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
204 void AliHMPIDCalib::SetRunParams(ULong_t runNum,Int_t timeStamp, Int_t ldcId)
205 {
206   //  
207   //Set run parameters for the Pedestal and Error Files
208   //Arguments: run number, time stamp and LDC Id
209   //Returns: none
210   //
211   fRunNum=(Int_t)runNum;
212   fTimeStamp=timeStamp;
213   fLdcId=ldcId;
214 }//SetRunParams()
215 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
216 void AliHMPIDCalib::SetSigCutFromFile(Char_t* name)
217 {
218   //
219   //Set Sigma Cut from the file on the LDC, if the input file is not present default value is set!
220   //Arguments: the name of the SigmaCut file on the LDC
221   //Returns: none
222   //
223   Int_t nSigCut=0;
224   ifstream infile(name);
225   if(!infile.is_open()) {fSigCut=3; return;}
226   while(!infile.eof())
227     {
228     infile>>nSigCut;
229   }
230   infile.close();
231   fSigCut=nSigCut; 
232 }//SetSigCutFromFile()    
233 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
234 void AliHMPIDCalib::SetSigCutFromShell(Char_t *env)
235 {
236   //
237   //Set Sigma Cut from the Shell on the LDC, if the input file is not present default value is set!
238   //Arguments: none
239   //Returns: none
240   //
241   Int_t nSigCut=0;
242   TString ssc(gSystem->Getenv(env));
243   //make it nicer later...
244        if(ssc=="1") {  nSigCut=1;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
245   else if(ssc=="2") {  nSigCut=2;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
246   else if(ssc=="3") {  nSigCut=3;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
247   else if(ssc=="4") {  nSigCut=4;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
248   else if(ssc=="5") {  nSigCut=5;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
249   else if(ssc=="6") {  nSigCut=6;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
250   else if(ssc=="7") {  nSigCut=7;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
251   else if(ssc=="8") {  nSigCut=8;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
252   else if(ssc=="9") {  nSigCut=9;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
253   else if(ssc=="10") {  nSigCut=10;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
254   else if(ssc=="11") {  nSigCut=11;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
255   else if(ssc=="12") {  nSigCut=12;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
256   else if(ssc=="13") {  nSigCut=13;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
257   else if(ssc=="14") {  nSigCut=14;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
258   else if(ssc=="15") {  nSigCut=15;  Printf("DAQ Sigma Cut from shell is: %d",nSigCut);}
259   else  {  nSigCut=3;  Printf("DAQ Sigma Cut from shell is too large set it back to default: %d",nSigCut);}
260   
261   fSigCut=nSigCut; 
262 }//SetSigCutFromFile()    
263 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     void SetDaOutFromShell(Char_t* name);                             //Set out dir. of DA from Bash Shell
264 void AliHMPIDCalib::SetFeeInFromShell(Char_t* env)
265 {
266    //
267   //Set the path where the Fe2C is looking for the threshold files
268   //Arguments: none
269   //Returns: none
270   //
271   TString sFeeIn(gSystem->Getenv(env));
272   if(sFeeIn.Data()=="") sFeeIn="/local/home/hmpid/";
273   if(sFeeIn.EndsWith("/")) fFeeIn=sFeeIn;
274   else fFeeIn=Form("%s/",sFeeIn.Data());
275   if(gSystem->OpenDirectory(fFeeIn.Data())==0) 
276   {
277     Printf("========== HMPID-DA WARNING: directory %s does not exist! Switching to /tmp! ==========",fFeeIn.Data());
278     fFeeIn="/tmp/";
279   }
280   Printf("HMPID_FEE_IN=%s",fFeeIn.Data()); 
281 }
282 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     void SetDaOutFromShell(Char_t* name);                             //Set out dir. of DA from Bash Shell
283 void AliHMPIDCalib::SetDaOutFromShell(Char_t* env)
284 {
285    //
286   //Set the standard path where the DA write the output
287   //Arguments: none
288   //Returns: none
289   //
290   TString sDaOut(gSystem->Getenv(env));
291   if(sDaOut=="") sDaOut="/local/home/hmpid/";
292   if(sDaOut.EndsWith("/")) fDaOut=sDaOut;
293   else fDaOut=Form("%s/",sDaOut.Data());
294   
295   if(gSystem->OpenDirectory(fDaOut.Data())==0) 
296   {
297     Printf("========== HMPID-DA WARNING: directory %s does not exist! Switching to /tmp! ==========",fDaOut.Data());
298     fDaOut="/tmp/";
299   } 
300   Printf("HMPID_DA_OUT=%s",fDaOut.Data()); 
301 }
302 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
303 void AliHMPIDCalib::InitHisto(Int_t q,Int_t histocnt,Char_t* name)
304 {
305   //
306   //Init the pad histos. For one DDL we have 11520 pads. ONLY if ENABLED!
307   //Arguments: q-charge, the absolute number of the histogram (AliHMPIDParam::kMaxCh+1)*(AliHMPIDParam::kMaxPcx+1)*(AliHMPIDParam::kMaxPcy+1) and the name of the histogram (unique) 
308   //Returns: none
309   //
310  if(fWritePads==kFALSE) return;
311  fFile->cd();
312  Double_t lowbin,highbin=0;
313  lowbin=q-40.5; highbin=q+40.5;  
314  
315  if(fIsPad[histocnt]==kTRUE) return;
316  
317  if(fLargeHisto==kFALSE) fPadAdc[histocnt]=new TH1I(name,name,81,lowbin,highbin);
318  if(fLargeHisto==kTRUE) fPadAdc[histocnt]=new TH1I(name,name,4093,-0.5,4092.5);
319  fPadAdc[histocnt]->Sumw2();
320  fIsPad[histocnt]=kTRUE;
321  
322 }//InitHisto()
323 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
324 void AliHMPIDCalib::FillHisto(Int_t histocnt,Int_t q)
325 {
326   //
327   //Fill the ADC histograms for each pad
328   //Arguments:  q-charge, the absolute number of the histogram (AliHMPIDParam::kMaxCh+1)*(AliHMPIDParam::kMaxPcx+1)*(AliHMPIDParam::kMaxPcy+1)
329   //Returns: none
330   //
331   if(fIsPad[histocnt]==kFALSE) return;
332   fFile->cd();
333   fPadAdc[histocnt]->Fill(q);
334  
335 }//InitHisto()
336 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
337 void AliHMPIDCalib::InitFile(Int_t inVal)
338 {
339   //
340   //Initialize the ADC histo output file (one per LDC)
341   //Arguments: LDC Id
342   //Returns: none
343   //
344   if(fWritePads==kFALSE ) return;
345   if(fLargeHisto==kFALSE) fFile=new TFile(Form("HmpidPadsOnLdc%2d.root",inVal),"RECREATE");
346   if(fLargeHisto==kTRUE)  fFile=new TFile(Form("Run%d_DDL%d.root",inVal,fSelectDDL),"RECREATE"); 
347   
348 }//InitFile()
349 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
350 void AliHMPIDCalib::CloseFile()
351 {
352   //
353   //Close the ADC histo output file (one per LDC)
354   //Arguments: LDC Id
355   //Returns: none
356   //
357   fFile->cd();
358   Int_t nPads = (AliHMPIDParam::kMaxCh+1)*(AliHMPIDParam::kMaxPcx+1)*(AliHMPIDParam::kMaxPcy+1);
359   for(Int_t np=0;np<nPads;np++) {if(fIsPad[np]==kTRUE) fPadAdc[np]->Write();} 
360   fFile->Close();
361 }//CloseFile()
362 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
363 void AliHMPIDCalib::FillPedestal(Int_t abspad,Int_t q)
364 {
365   //
366   //Called from the HMPIDda and fills the pedestal values
367   //Arguments: absolute pad number as from AliHMPIDParam and q-charge
368   //Returns: none
369   //
370   if(q<0) AliFatal("Negative charge is read!!!!!!");
371   
372   UInt_t w32;
373   Int_t nDDL=0, row=0, dil=0, adr=0;
374   //The decoding (abs. pad -> ddl,dil,...) is the same as in AliHMPIDDigit::Raw
375   
376   AliHMPIDDigit dig(abspad,q);
377   dig.Raw(w32,nDDL,row,dil,adr);
378   
379   //........... decoding done      
380
381      if(q>0) { 
382         fsq[nDDL][row][dil][adr]+=q;
383       fsq2[nDDL][row][dil][adr]+=q*q;
384       fnpc[nDDL][row][dil][adr]++;                                                     //Count how many times the pad is good (can be different from the good DDL  count)
385                        faddl[nDDL]=kTRUE; 
386                      }
387       else
388       {
389         fpedQ0[nDDL][row][dil][adr]++;                                                 //Count how many times a pad charge is zero
390       }
391       
392      Int_t histocnt=0;   histocnt=(nDDL)*11520+(row-1)*480+(dil-1)*48+adr;             //Histo counter for a single DDL  
393      
394      if(fWritePads==kTRUE)                                                             //works but make it nicer later....
395      { 
396        if( fLargeHisto==kTRUE && nDDL==fSelectDDL) {              
397          InitHisto(q,histocnt,Form("hDDL_%d_Row_%d_Dil_%d_Pad_%d",nDDL,row,dil,adr));  //for large histos use hardware naming
398          FillHisto(histocnt,q);
399         }
400         if(fLargeHisto==kFALSE)
401         {
402          InitHisto(q,histocnt,Form("hPad_Ch_%d_Pc_%d_Px_%d_Py_%d",AliHMPIDParam::A2C(abspad),AliHMPIDParam::A2P(abspad),AliHMPIDParam::A2X(abspad),AliHMPIDParam::A2Y(abspad))); 
403          FillHisto(histocnt,q);  
404         }
405       }//fWritePads
406             
407 }//FillPedestal()
408 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
409 void AliHMPIDCalib::FillErrors(Int_t nDDL,Int_t eType, Int_t nErr)
410 {
411   //
412   //Fill decoding errors from AliHMPIDRawStream
413   //Arguments: nDDL-DDL number, eType- error type as in AliHMPIDRawStream.h and the # of occurence for eType
414   //Retutns: none
415   //
416     if(nErr<=0) return;
417     if(eType < 0 || eType> AliHMPIDRawStream::kSumErr ) return;
418     fErr[nDDL][eType]=fErr[nDDL][eType]+nErr;
419     
420   
421 }//FillErrors()
422 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
423 void AliHMPIDCalib::FillDDLCnt(Int_t iddl,Int_t inDDL, Int_t outDDL)
424 {
425   //
426   //Fill decoding DDL check from RawStream
427   //Arguments: iddl - DDL under setting, inDDL- How many times the DDL is present in the raw stream, outDDL - How many time sthe DDL is succesfylly decoded
428   //Retutns: none
429   //
430  
431   if(inDDL==-1) return;
432   if(fnDDLInStream[iddl]==-1) {fnDDLInStream[iddl]=0; fnDDLOutStream[iddl]=0;}
433   fnDDLInStream[iddl]+=inDDL;
434   fnDDLOutStream[iddl]+=outDDL;
435  
436   
437 }//FillDDLCnt()
438 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
439 Bool_t AliHMPIDCalib::WriteErrors(Int_t nDDL, Char_t* name, Int_t nEv)
440 {
441   //
442   //Write decoding errors to a txt file
443   //Arguments: nDDL-DDL number, name of the error file and number of the read events
444   //Retutns: kTRUE/kFALSE
445   //  
446   if(faddl[nDDL]==kFALSE) return kFALSE;                                                                 //if ddl is missing no error file is created
447   ofstream outerr;  outerr.open(name);                                                                   //open error file
448   outerr << Form("%8s %2d\n","RunNumber",(Int_t)fRunNum);                                                //read run number
449   outerr << Form("%8s %2d\n","LdcId" ,          fLdcId);                                                 //read LDC Id
450   outerr << Form("%8s %2d\n","TimeStamp",       fTimeStamp);                                             //read time stamp
451   outerr << Form("%8s %2d\n","TotNumEvt",       nEv);                                                    //read number of total events processed
452   outerr << Form("%8s %2d\n","TotDDLEvt",       fnDDLInStream[nDDL]);                                    //read number of bad events for DDL # nDDL processed
453   outerr << Form("%8s %2d\n","NumBadEvt",       fnDDLInStream[nDDL]-fnDDLOutStream[nDDL]);               //read number of bad events for DDL # nDDL processed
454   outerr << Form("%8s %2.2f\n","NBadE(%)",      (fnDDLInStream[nDDL]-fnDDLOutStream[nDDL])*100.0/nEv);   //read number of bad events (in %) for DDL # nDDL processed
455   
456   for(Int_t  ierr=0; ierr <AliHMPIDRawStream::kSumErr; ierr++) outerr << Form("%2d\t",fErr[nDDL][ierr]); //write errors
457                                                                outerr << Form("\n");                     //last break
458   /* write out pads with 0 charge read */
459   for(Int_t row = 1; row <= AliHMPIDRawStream::kNRows; row++){
460     for(Int_t dil = 1; dil <= AliHMPIDRawStream::kNDILOGICAdd; dil++){
461       for(Int_t pad = 0; pad < AliHMPIDRawStream::kNPadAdd; pad++){
462         if(fpedQ0[nDDL][row][dil][pad]>0) outerr<< Form("%2d %2d %2d %3d\n",row,dil,pad,fpedQ0[nDDL][row][dil][pad]);
463       }
464     }
465   } 
466                                                                                                                                                                                        
467                                                                
468   outerr.close();                                                                                        //write error file
469   
470   return kTRUE;
471     
472 }//FillErrors()
473 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
474 Bool_t AliHMPIDCalib::CalcPedestal(Int_t nDDL, Char_t* name, Int_t nEv)    
475 {
476   //
477   //Calculate pedestal for each pad  
478   //Arguments: nDDL-DDL number, name of the pedestal file and number of the read events
479   //Retutns: kTRUE/kFALSE
480   //
481   
482   if(faddl[nDDL]==kFALSE) return kFALSE;                   //if ddl is missing no ped file is created (and also for LDC selection). Check with Paolo what he checks for?!  
483   Int_t ddlOffset=1536;
484   Int_t feeOffset=196657;
485   ofstream feeInput[24]; 
486   
487   for(Int_t ir=0;ir<AliHMPIDRawStream::kNRows;ir++) feeInput[ir].open(Form("%sthr%d_%d.dat",fFeeIn.Data(),ddlOffset+nDDL,ir+1));  //for Fe2C every row has a file only with inhard        
488
489   Double_t mean=0,sigma=0;
490   Double_t qs2m=0,qsm2=0;
491   ofstream out;                                           //to write the pedestal text files
492   Int_t inhard;
493   Int_t nEvPerPad=0;
494   out.open(name);
495   out << Form("%8s %2d\n","RunNumber",(Int_t)fRunNum);                                                //read run number
496   out << Form("%8s %2d\n","LdcId" ,         fLdcId);                                                  //read LDC Id
497   out << Form("%8s %2d\n","TimeStamp",      fTimeStamp);                                              //read time stamp
498   out << Form("%8s %2d\n","TotNumEvt",      nEv);                                                     //read number of total events processed
499   out << Form("%8s %2d\n","TotDDLEvt",      fnDDLInStream[nDDL]);                                     //read number of bad events for DDL # nDDL processed
500   out << Form("%8s %2d\n","NumBadEvt",      fnDDLInStream[nDDL]-fnDDLOutStream[nDDL]);                //read number of bad events for DDL # nDDL processed
501   out << Form("%8s %2f\n","NBadE(%)",       (fnDDLInStream[nDDL]-fnDDLOutStream[nDDL])*100.0/nEv);    //read number of bad events (in %) for DDL # nDDL processed
502   out << Form("%8s %2.2d\n","#SigCut",      fSigCut);                                                 //# of sigma cuts
503       
504   for(Int_t row = 1; row <= AliHMPIDRawStream::kNRows; row++){
505     //write thr file for Fe2C
506    
507     for(Int_t dil = 1; dil <= AliHMPIDRawStream::kNDILOGICAdd; dil++){
508       for(Int_t pad = 0; pad < AliHMPIDRawStream::kNPadAdd; pad++){
509         
510         mean  = 50;sigma = 100;
511         
512         nEvPerPad=fnpc[nDDL][row][dil][pad];
513         
514         if(nEvPerPad < 1 ) {                    //if the pad is bad then we assign 100  for the sigma and 50 for the mean
515           mean  = 4000;
516           sigma = 1000;
517         }
518         else{        
519          mean = fsq[nDDL][row][dil][pad]*1.0/nEvPerPad;
520          qs2m = fsq2[nDDL][row][dil][pad]*1.0/nEvPerPad;
521          qsm2 = TMath::Power(fsq[nDDL][row][dil][pad]*1.0/nEvPerPad,2); 
522         sigma = TMath::Sqrt(TMath::Abs(qs2m-qsm2));
523         }
524             
525         inhard=((Int_t(mean+fSigCut*sigma))<<9)+Int_t(mean); //right calculation, xchecked with Paolo 8/4/2008
526         out << Form("%2i %2i %2i %5.3f %5.3f %4.4x \n",row,dil,pad,mean,sigma,inhard);
527
528         feeInput[row-1] << Form("0x%4.4x\n",inhard);                 
529        //if(sigma > 3.0) Printf("WARNING SIGMA DDL: %2d row: %2d dil: %2d pad: %2d mean: %3.2f sigma: %2.2f nEvPerPad: %02d fnDDLOutStream: %02d fpedQ0: %02d",nDDL,row,dil,pad,mean,sigma,nEvPerPad,fnDDLOutStream[nDDL],fpedQ0[nDDL][row][dil][pad]);         
530         }//adr
531         
532         //we have to write up to 64 not 48 in the DILOGIC since they are daisy chained!
533         //offset and format is defined for the Fe2C code
534         for(Int_t idd=0;idd<16;idd++) feeInput[row-1] << Form("0x%4.4x\n",idd+feeOffset);                 
535       }//dil
536       
537       
538     }//row
539     out.close();                                          //write pedestal file
540     for(Int_t ir=0;ir<AliHMPIDRawStream::kNRows;ir++) feeInput[ir].close();
541  
542   return kTRUE;
543 }//CaclPedestal()
544 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
545 Bool_t AliHMPIDCalib::CalcPedestalPaolo(Int_t nDDL, Char_t* /*name*/, Int_t nEv)    
546 {
547   //
548   //Calculate pedestal for each pad  
549   //Arguments: nDDL-DDL number, name of the pedestal file and number of the read events
550   //Retutns: kTRUE/kFALSE
551   //
552   //----------------- write files in the format of Paolo -----------------------
553   if(faddl[nDDL]==kFALSE) return kFALSE;                   //if ddl is missing no ped file is created (and also for LDC selection). Check with Paolo what he checks for?!  
554   Int_t ddlOffset=1536;
555   Int_t cnt=0;
556   Double_t mean1=0,sigma1=0;
557   Double_t qs2m1=0,qsm21=0;
558   Double_t mean2=0,sigma2=0;
559   Double_t qs2m2=0,qsm22=0;
560   Int_t nEvPerPad1=0;
561   Int_t nEvPerPad2=0;
562     
563   ofstream pped[3]; 
564   for(Int_t iseg=1;iseg<4;iseg++) pped[iseg-1].open(Form("%sHmpidPed%d_%d.dat",fDaOut.Data(),nDDL+ddlOffset,iseg));
565     
566   for(Int_t row = 1; row <= AliHMPIDRawStream::kNRows/2; row++){
567      
568       //write header
569       pped[(row-1)/4]<<Form("ID_Nevt_NChan_Row_Row_P0_P1_S0_S1 \n");
570       pped[(row-1)/4]<<Form("%d  %d   %d    %d  %d %3.3lf %3.3lf %3.3lf %3.3lf \n",2*row-1,nEv,480,2*row-1,2*row,999.0,999.0,999.0,999.0);
571        
572       cnt=0; 
573       for(Int_t dil = 1; dil <= AliHMPIDRawStream::kNDILOGICAdd; dil++){
574       for(Int_t pad = 0; pad < AliHMPIDRawStream::kNPadAdd; pad++){
575         
576          nEvPerPad1=fnpc[nDDL][2*row-1][dil][pad];
577          nEvPerPad2=fnpc[nDDL][2*row][dil][pad];
578         
579         if(nEvPerPad1 < 1 ) { mean1  = 4000; sigma1 = 1000; }
580         if(nEvPerPad2 < 1 ) { mean2  = 4000; sigma2 = 1000; }
581                 
582          mean1 = fsq[nDDL][2*row-1][dil][pad]*1.0/nEvPerPad1;
583          qs2m1 = fsq2[nDDL][2*row-1][dil][pad]*1.0/nEvPerPad1;
584          qsm21 = TMath::Power(fsq[nDDL][2*row-1][dil][pad]*1.0/nEvPerPad1,2); 
585         sigma1 = TMath::Sqrt(TMath::Abs(qs2m1-qsm21));
586        
587          mean2 = fsq[nDDL][2*row][dil][pad]*1.0/nEvPerPad2;
588          qs2m2 = fsq2[nDDL][2*row][dil][pad]*1.0/nEvPerPad2;
589          qsm22 = TMath::Power(fsq[nDDL][2*row][dil][pad]*1.0/nEvPerPad2,2); 
590         sigma2 = TMath::Sqrt(TMath::Abs(qs2m2-qsm22));
591         
592         pped[(row-1)/4]<<Form("%d %3.3lf %3.3lf %3.3lf %3.3lf \n",cnt,mean1,sigma1,mean2,sigma2);cnt++;
593       }//pad
594       }//dil 
595      }//row    
596     for(Int_t ir=0;ir<3;ir++) {pped[ir].close();    }  
597    return kTRUE;
598 }//CalcPedestalPaolo()
599 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
600
601