6a7a227a3a03382fc2434e61445eac0840f041ec
[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 {
45     //
46     // default constructor
47     //
48
49     for (int i = 0; i < kDet; i++)
50     {
51         for (int j = 0; j < kMaxSMN; j++)
52         {
53             for (int k = 0; k < kMaxRow; k++)
54             {
55                 for (int l = 0; l < kMaxCol; l++)
56                 {
57                     fPedVal[i][j][k][l]   = 0.;
58                     fPedValSq[i][j][k][l] = 0.;
59                     fPedCount[i][j][k][l] = 0.;
60                     fPedChain[i][j][k][l] = 0;
61                 }
62             }
63         }
64     }
65
66
67 }
68 //_____________________________________________________________________
69 AliPMDCalibPedestal::AliPMDCalibPedestal(const AliPMDCalibPedestal &ped) :
70   TObject(ped)
71 {
72     //
73     // copy constructor
74     //
75     for (int i = 0; i < kDet; i++)
76     {
77         for (int j = 0; j < kMaxSMN; j++)
78         {
79             for (int k = 0; k < kMaxRow; k++)
80             {
81                 for (int l = 0; l < kMaxCol; l++)
82                 {
83                     fPedVal[i][j][k][l]   = ped.fPedVal[i][j][k][l];
84                     fPedValSq[i][j][k][l] = ped.fPedValSq[i][j][k][l];
85                     fPedCount[i][j][k][l] = ped.fPedCount[i][j][k][l];
86                     fPedChain[i][j][k][l] = ped.fPedChain[i][j][k][l];
87                 }
88             }
89         }
90     }
91     
92 }
93 //_____________________________________________________________________
94 AliPMDCalibPedestal& AliPMDCalibPedestal::operator = (const  AliPMDCalibPedestal &source)
95 {
96   //
97   // assignment operator
98   //
99   if (&source == this) return *this;
100   new (this) AliPMDCalibPedestal(source);
101
102   return *this;
103 }
104 //_____________________________________________________________________
105 AliPMDCalibPedestal::~AliPMDCalibPedestal()
106 {
107     //
108     // destructor
109     //
110 }
111 //_____________________________________________________________________
112 Bool_t AliPMDCalibPedestal::ProcessEvent(AliRawReader *rawReader)
113 {
114   //
115   //  Event processing loop - AliRawReader
116   //
117
118     const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
119
120     UInt_t busmcmch;
121     UInt_t pbus, mcm, chno;
122
123     AliPMDRawStream rawStream(rawReader);
124
125     TObjArray pmdddlcont;
126     Int_t iddl = -1;
127     Int_t numberofDDLs = 0;
128
129     while ((iddl = rawStream.DdlData(&pmdddlcont)) >=0) {
130       numberofDDLs++;
131       Int_t ientries = pmdddlcont.GetEntries();
132       //printf("iddl = %d ientries = %d\n",iddl, ientries);
133       for (Int_t ient = 0; ient < ientries; ient++)
134         {
135             AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont.UncheckedAt(ient);
136             
137             Int_t det = pmdddl->GetDetector();
138             Int_t smn = pmdddl->GetSMN();
139             Int_t row = pmdddl->GetRow();
140             Int_t col = pmdddl->GetColumn();
141             Float_t sig = (Float_t) pmdddl->GetSignal();
142
143             pbus = (UInt_t) pmdddl->GetPatchBusId();
144             mcm  = (UInt_t) pmdddl->GetMCM();
145             chno = (UInt_t) pmdddl->GetChannel();
146
147             busmcmch = 0;
148             AliBitPacking::PackWord(chno,busmcmch,0,7);
149             AliBitPacking::PackWord(mcm,busmcmch,8,15);
150             AliBitPacking::PackWord(pbus,busmcmch,16,23);
151
152
153             if (fPedChain[det][smn][row][col] == 0)
154                 fPedChain[det][smn][row][col]   = busmcmch;
155
156             fPedVal[det][smn][row][col]   += sig;
157             fPedValSq[det][smn][row][col] += sig*sig;
158             fPedCount[det][smn][row][col]++;
159         }
160       pmdddlcont.Delete();
161     }
162     if (numberofDDLs < kDDL)
163       return kFALSE;
164     return kTRUE;
165 }
166 //_____________________________________________________________________
167
168 void AliPMDCalibPedestal::Analyse(TTree *pedtree)
169 {
170     //
171     //  Calculate pedestal Mean and RMS
172     //
173
174     FILE *fpw0 = fopen("pedestal2304.ped","w");
175     FILE *fpw1 = fopen("pedestal2305.ped","w");
176     FILE *fpw2 = fopen("pedestal2306.ped","w");
177     FILE *fpw3 = fopen("pedestal2307.ped","w");
178     FILE *fpw4 = fopen("pedestal2308.ped","w");
179     FILE *fpw5 = fopen("pedestal2309.ped","w");
180
181     UInt_t  busmcmch;
182     UInt_t  pbus, mcm, chno;
183     Int_t   ddlno;
184     Int_t   det, sm, row, col;
185     Float_t mean, rms;
186     Float_t meansq, diff;
187
188
189
190     pedtree->Branch("det",&det,"det/I");
191     pedtree->Branch("sm",&sm,"sm/I");
192     pedtree->Branch("row",&row,"row/I");
193     pedtree->Branch("col",&col,"col/I");
194     pedtree->Branch("mean",&mean,"mean/F");
195     pedtree->Branch("rms",&rms,"rms/F");
196
197     for (int idet = 0; idet < kDet; idet++)
198     {
199         for (int ism = 0; ism < kMaxSMN; ism++)
200         {
201             ConvertDDL(idet,ism,ddlno);
202             for (int irow = 0; irow < kMaxRow; irow++)
203             {
204                 for (int icol = 0; icol < kMaxCol; icol++)
205                 {
206                     det  = idet;
207                     sm   = ism;
208                     row  = irow;
209                     col  = icol;
210                     mean = 0.;
211                     rms  = 0.;
212
213                     if (fPedCount[idet][ism][irow][icol] > 0)
214                     {
215                       mean = fPedVal[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
216
217                       meansq = fPedValSq[idet][ism][irow][icol]/fPedCount[idet][ism][irow][icol];
218
219                         diff = meansq - mean*mean;
220                         if (diff > 0.)
221                         {
222                             rms  = sqrt(diff);
223                         }
224                         else
225                         {
226                             rms = 0.;
227                         }
228                         pedtree->Fill();
229
230                         busmcmch = fPedChain[idet][ism][irow][icol];
231
232                         chno = busmcmch & 0x00FF;
233                         mcm  = (busmcmch >> 8) & 0x00FF;
234                         pbus = (busmcmch >> 16) & 0x00FF;
235                         
236                         if (ddlno == 0)
237                         {
238                             fprintf(fpw0,"%d %d %d %f %f\n",
239                                     pbus, mcm, chno, mean, rms);
240                         }
241                         else if (ddlno == 1)
242                         {
243                             fprintf(fpw1,"%d %d %d %f %f\n",
244                                     pbus, mcm, chno, mean, rms);
245                         }
246                         else if (ddlno == 2)
247                         {
248                             fprintf(fpw2,"%d %d %d %f %f\n",
249                                     pbus, mcm, chno, mean, rms);
250                         }
251                         else if (ddlno == 3)
252                         {
253                             fprintf(fpw3,"%d %d %d %f %f\n",
254                                     pbus, mcm, chno, mean, rms);
255                         }
256                         else if (ddlno == 4)
257                         {
258                             fprintf(fpw4,"%d %d %d %f %f\n",
259                                     pbus, mcm, chno, mean, rms);
260                         }
261                         else if (ddlno == 5)
262                         {
263                             fprintf(fpw5,"%d %d %d %f %f\n",
264                                     pbus, mcm, chno, mean, rms);
265                         }
266                         
267                     }
268                     
269                 }
270             }
271         }
272     }
273     
274     fclose(fpw0);
275     fclose(fpw1);
276     fclose(fpw2);
277     fclose(fpw3);
278     fclose(fpw4);
279     fclose(fpw5);
280 }
281
282
283 // -------------------------------------------------------------------
284
285 void AliPMDCalibPedestal::ConvertDDL(Int_t det, Int_t smn, Int_t &ddlno)
286 {
287 // Given the plane number and serial module number, ddlno is calculated
288
289     if (det == 0)
290     {
291         if (smn <= 5)
292         {
293             ddlno = 0;
294         }
295         else if (smn > 5 && smn <= 11)
296         {
297             ddlno = 1;
298         }
299         else if (smn > 11 && smn <= 17)
300         {
301             ddlno = 2;
302         }
303         else if (smn > 17 && smn <= 23)
304         {
305             ddlno = 3;
306         }
307     }
308     else if (det == 1)
309     {
310         if (smn <= 5 || (smn >= 18 && smn <=23))
311         {
312             ddlno = 4;
313         }
314         else if (smn >= 6 && smn <= 17)
315         {
316             ddlno = 5;
317         }
318     }
319
320 }
321