Correcting the comments changed in the previous commit
[u/mrichter/AliRoot.git] / PMD / AliPMDCalibPedestal.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
17 //Root includes
18 #include <TObjArray.h>
19 #include <TH1F.h>
20 #include <TString.h>
21 #include <TMath.h>
22 #include <TF1.h>
23 #include <TRandom.h>
24 #include <TDirectory.h>
25 #include <TFile.h>
26 #include "TTreeStream.h"
27
28 //AliRoot includes
29 #include "AliDAQ.h"
30 #include "AliLog.h"
31 #include "AliRawReader.h"
32 #include "AliPMDRawStream.h"
33 #include "AliPMDddldata.h"
34 #include "AliBitPacking.h"
35 //header file
36 #include "AliPMDCalibPedestal.h"
37
38
39 ClassImp(AliPMDCalibPedestal)
40
41
42 AliPMDCalibPedestal::AliPMDCalibPedestal() :
43   TObject(),
44   fRunNumber(-1),
45   fEventNumber(0)
46 {
47     //
48     // default constructor
49     //
50
51     for (int i = 0; i < kDet; i++)
52     {
53         for (int j = 0; j < kMaxSMN; j++)
54         {
55             for (int k = 0; k < kMaxRow; k++)
56             {
57                 for (int l = 0; l < kMaxCol; l++)
58                 {
59                     fPedVal[i][j][k][l]   = 0.;
60                     fPedValSq[i][j][k][l] = 0.;
61                     fPedCount[i][j][k][l] = 0.;
62                 }
63             }
64         }
65     }
66
67     for (int i = 0; i < 6; i++)
68     {
69         for (int j = 0; j < 51; j++)
70         {
71             for (int k = 0; k < 25; k++)
72             {
73                 for (int l = 0; l < 64; l++)
74                 {
75                     fPedChain[i][j][k][l] = -1;
76                 }
77             }
78         }
79     }
80
81
82 }
83 //_____________________________________________________________________
84 AliPMDCalibPedestal::AliPMDCalibPedestal(const AliPMDCalibPedestal &ped) :
85   TObject(ped),
86   fRunNumber(ped.fRunNumber),
87   fEventNumber(ped.fEventNumber)
88 {
89     //
90     // copy constructor
91     //
92     for (int i = 0; i < kDet; i++)
93     {
94         for (int j = 0; j < kMaxSMN; j++)
95         {
96             for (int k = 0; k < kMaxRow; k++)
97             {
98                 for (int l = 0; l < kMaxCol; l++)
99                 {
100                     fPedVal[i][j][k][l]   = ped.fPedVal[i][j][k][l];
101                     fPedValSq[i][j][k][l] = ped.fPedValSq[i][j][k][l];
102                     fPedCount[i][j][k][l] = ped.fPedCount[i][j][k][l];
103                 }
104             }
105         }
106     }
107
108     for (int i = 0; i < 6; i++)
109     {
110         for (int j = 0; j < 51; j++)
111         {
112             for (int k = 0; k < 25; k++)
113             {
114                 for (int l = 0; l < 64; l++)
115                 {
116                     fPedChain[i][j][k][l] = -1;
117                 }
118             }
119         }
120     }
121     
122 }
123 //_____________________________________________________________________
124 AliPMDCalibPedestal& AliPMDCalibPedestal::operator = (const  AliPMDCalibPedestal &source)
125 {
126   //
127   // assignment operator
128   //
129   if (&source == this) return *this;
130   new (this) AliPMDCalibPedestal(source);
131
132   return *this;
133 }
134 //_____________________________________________________________________
135 AliPMDCalibPedestal::~AliPMDCalibPedestal()
136 {
137     //
138     // destructor
139     //
140 }
141 //_____________________________________________________________________
142 Bool_t AliPMDCalibPedestal::ProcessEvent(AliRawReader *rawReader, TObjArray *pmdddlcont)
143 {
144   //
145   //  Event processing loop - AliRawReader
146   //
147
148     const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
149
150     UInt_t detsmnrowcol = 0;
151     UInt_t pbus = 0, mcm = 0, chno = 0;
152
153     fRunNumber = rawReader->GetRunNumber();
154
155     AliPMDRawStream rawStream(rawReader);
156
157     fEventNumber++;
158
159     Int_t iddl = -1;
160     Int_t numberofDDLs = 0;
161
162     while ((iddl = rawStream.DdlData(pmdddlcont)) >=0) {
163       numberofDDLs++;
164       Int_t ientries = pmdddlcont->GetEntries();
165       //printf("iddl = %d ientries = %d\n",iddl, ientries);
166       for (Int_t ient = 0; ient < ientries; ient++)
167         {
168             AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont->UncheckedAt(ient);
169             
170             Int_t det = pmdddl->GetDetector();
171             Int_t smn = pmdddl->GetSMN();
172             Int_t row = pmdddl->GetRow();
173             Int_t col = pmdddl->GetColumn();
174             Float_t sig = (Float_t) pmdddl->GetSignal();
175
176             pbus = (UInt_t) pmdddl->GetPatchBusId();
177             mcm  = (UInt_t) pmdddl->GetMCM();
178             chno = (UInt_t) pmdddl->GetChannel();
179
180             detsmnrowcol = 0;
181             AliBitPacking::PackWord(det,detsmnrowcol,0,7);
182             AliBitPacking::PackWord(smn,detsmnrowcol,8,15);
183             AliBitPacking::PackWord(row,detsmnrowcol,16,23);
184             AliBitPacking::PackWord(col,detsmnrowcol,24,31);
185
186             if (fPedChain[iddl][pbus][mcm][chno] == -1)
187                 fPedChain[iddl][pbus][mcm][chno] = (Int_t)detsmnrowcol;
188
189
190             fPedVal[det][smn][row][col]   += sig;
191             fPedValSq[det][smn][row][col] += sig*sig;
192             fPedCount[det][smn][row][col]++;
193         }
194       pmdddlcont->Delete();
195     }
196
197     if (numberofDDLs < kDDL)
198       return kFALSE;
199     return kTRUE;
200 }
201 //_____________________________________________________________________
202
203 void AliPMDCalibPedestal::Analyse(TTree *pedtree)
204 {
205     //
206     //  Calculate pedestal Mean and RMS
207     //
208
209     UInt_t  detsmnrowcol = 0;
210     Int_t   det = 0, sm = 0, row = 0, col = 0;
211     Int_t   idet = 0, ism = 0, irow = 0, icol = 0;
212     Float_t mean = 0., rms = 0.;
213     Double_t meansq = 0., diff = 0.;
214
215     FILE *fpw0 = fopen("pedestal2304.ped","w");
216     FILE *fpw1 = fopen("pedestal2305.ped","w");
217     FILE *fpw2 = fopen("pedestal2306.ped","w");
218     FILE *fpw3 = fopen("pedestal2307.ped","w");
219     FILE *fpw4 = fopen("pedestal2308.ped","w");
220     FILE *fpw5 = fopen("pedestal2309.ped","w");
221
222     fprintf(fpw0,"//=============================================\n");
223     fprintf(fpw0,"//     Pedestal file Calculated by Online DA\n");
224     fprintf(fpw0,"//=============================================\n");
225     fprintf(fpw0,"//     RUN             :%d\n",fRunNumber);
226     fprintf(fpw0,"//     Statistics      :%d\n",fEventNumber);
227     fprintf(fpw0,"//---------------------------------------------\n");
228     fprintf(fpw0,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
229     fprintf(fpw0,"//---------------------------------------------\n");
230
231     fprintf(fpw1,"//=============================================\n");
232     fprintf(fpw1,"//     Pedestal file Calculated by Online DA\n");
233     fprintf(fpw1,"//=============================================\n");
234     fprintf(fpw1,"//     RUN             :%d\n",fRunNumber);
235     fprintf(fpw1,"//     Statistics      :%d\n",fEventNumber);
236     fprintf(fpw1,"//---------------------------------------------\n");
237     fprintf(fpw1,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
238
239     fprintf(fpw2,"//=============================================\n");
240     fprintf(fpw2,"//     Pedestal file Calculated by Online DA\n");
241     fprintf(fpw2,"//=============================================\n");
242     fprintf(fpw2,"//     RUN             :%d\n",fRunNumber);
243     fprintf(fpw2,"//     Statistics      :%d\n",fEventNumber);
244     fprintf(fpw2,"//---------------------------------------------\n");
245     fprintf(fpw2,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
246     fprintf(fpw2,"//---------------------------------------------\n");
247
248     fprintf(fpw3,"//=============================================\n");
249     fprintf(fpw3,"//     Pedestal file Calculated by Online DA\n");
250     fprintf(fpw3,"//=============================================\n");
251     fprintf(fpw3,"//     RUN             :%d\n",fRunNumber);
252     fprintf(fpw3,"//     Statistics      :%d\n",fEventNumber);
253     fprintf(fpw3,"//---------------------------------------------\n");
254     fprintf(fpw3,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
255     fprintf(fpw3,"//---------------------------------------------\n");
256
257     fprintf(fpw4,"//=============================================\n");
258     fprintf(fpw4,"//     Pedestal file Calculated by Online DA\n");
259     fprintf(fpw4,"//=============================================\n");
260     fprintf(fpw4,"//     RUN             :%d\n",fRunNumber);
261     fprintf(fpw4,"//     Statistics      :%d\n",fEventNumber);
262     fprintf(fpw4,"//---------------------------------------------\n");
263     fprintf(fpw4,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
264     fprintf(fpw4,"//---------------------------------------------\n");
265
266     fprintf(fpw5,"//=============================================\n");
267     fprintf(fpw5,"//     Pedestal file Calculated by Online DA\n");
268     fprintf(fpw5,"//=============================================\n");
269     fprintf(fpw5,"//     RUN             :%d\n",fRunNumber);
270     fprintf(fpw5,"//     Statistics      :%d\n",fEventNumber);
271     fprintf(fpw5,"//---------------------------------------------\n");
272     fprintf(fpw5,"//format:CHAIN_NO  FEE_ID  CHANNEL  MEAN  SIGMA\n");
273     fprintf(fpw5,"//---------------------------------------------\n");
274
275
276     for(Int_t iddl = 0; iddl < 6; iddl++)
277       {
278         for(Int_t ibus = 1; ibus < 51; ibus++)
279           {
280             for(Int_t imcm = 1; imcm < 25; imcm++)
281               {
282                 for(Int_t ich = 0; ich < 64; ich++)
283                   {
284
285                     if (fPedChain[iddl][ibus][imcm][ich] != -1)
286                       {
287                         detsmnrowcol = (UInt_t)fPedChain[iddl][ibus][imcm][ich];
288
289                         idet =  detsmnrowcol & 0x00FF;
290                         ism  = (detsmnrowcol >> 8) & 0x00FF;
291                         irow = (detsmnrowcol >> 16) & 0x00FF;
292                         icol = (detsmnrowcol >> 24) & 0x00FF;
293                         
294                         mean = 0.;
295                         rms  = 0.;
296                         if (fPedCount[idet][ism][irow][icol] > 0)
297                           {
298                             mean = fPedVal[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
299                             
300                             meansq = fPedValSq[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
301                             
302                             diff = meansq - mean*mean;
303                             if (diff > 0.)
304                               {
305                                 rms  = sqrt(diff);
306                               }
307                             else
308                               {
309                                 rms = 0.;
310                               }
311
312
313                             if (iddl == 0)
314                               {
315                                 fprintf(fpw0,"%d %d %d %f %f\n",
316                                         ibus, imcm, ich, mean, rms);
317                               }
318                             else if (iddl == 1)
319                               {
320                                 fprintf(fpw1,"%d %d %d %f %f\n",
321                                         ibus, imcm, ich, mean, rms);
322                               }
323                             else if (iddl == 2)
324                               {
325                                 fprintf(fpw2,"%d %d %d %f %f\n",
326                                         ibus, imcm, ich, mean, rms);
327                               }
328                             else if (iddl == 3)
329                               {
330                                 fprintf(fpw3,"%d %d %d %f %f\n",
331                                         ibus, imcm, ich, mean, rms);
332                               }
333                             else if (iddl == 4)
334                               {
335                                 fprintf(fpw4,"%d %d %d %f %f\n",
336                                         ibus, imcm, ich, mean, rms);
337                               }
338                             else if (iddl == 5)
339                               {
340                                 fprintf(fpw5,"%d %d %d %f %f\n",
341                                         ibus, imcm, ich, mean, rms);
342                               }
343                           }
344                       }
345                   }
346               }
347           }
348       }
349     
350
351     fclose(fpw0);
352     fclose(fpw1);
353     fclose(fpw2);
354     fclose(fpw3);
355     fclose(fpw4);
356     fclose(fpw5);
357
358     pedtree->Branch("det",&det,"det/I");
359     pedtree->Branch("sm",&sm,"sm/I");
360     pedtree->Branch("row",&row,"row/I");
361     pedtree->Branch("col",&col,"col/I");
362     pedtree->Branch("mean",&mean,"mean/F");
363     pedtree->Branch("rms",&rms,"rms/F");
364
365     for (idet = 0; idet < kDet; idet++)
366       {
367         for (ism = 0; ism < kMaxSMN; ism++)
368           {
369             for (irow = 0; irow < kMaxRow; irow++)
370               {
371                 for (icol = 0; icol < kMaxCol; icol++)
372                   {
373                     det  = idet;
374                     sm   = ism;
375                     row  = irow;
376                     col  = icol;
377                     mean = 0.;
378                     rms  = 0.;
379                     
380                     if (fPedCount[idet][ism][irow][icol] > 0)
381                       {
382                         mean = fPedVal[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
383                         
384                         meansq = fPedValSq[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
385                         
386                         diff = meansq - mean*mean;
387                         if (diff > 0.)
388                           {
389                             rms  = sqrt(diff);
390                           }
391                         else
392                           {
393                             rms = 0.;
394                           }
395                         pedtree->Fill();
396                       }
397                     
398                   }
399               }
400           }
401       }
402 }
403 // -------------------------------------------------------------------