implementation of effc++
[u/mrichter/AliRoot.git] / PMD / AliPMDCalibrator.cxx
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 // *                                                                        *
5 // * Author: The ALICE Off-line Project.                                    *
6 // * Contributors are mentioned in the code where appropriate.              *
7 // *                                                                        *
8 // * Permission to use, copy, modify and distribute this software and its   *
9 // * documentation strictly for non-commercial purposes is hereby granted   *
10 // * without fee, provided that the above copyright notice appears in all   *
11 // * copies and that both the copyright notice and this permission notice   *
12 // * appear in the supporting documentation. The authors make no claims     *
13 // * about the suitability of this software for any purpose. It is          *
14 // * provided "as is" without express or implied warranty.                  *
15 // **************************************************************************/
16 //
17 //////////////////////////////////////////////////////////////////////////////
18 // Author : Z. Ahamed
19
20 #include "TF1.h"
21 #include "TFile.h"
22 #include "TObjString.h"
23 #include "TROOT.h"
24 #include "TClonesArray.h"
25 #include "TH1F.h"
26 #include "TObjArray.h"
27
28 // --- Standard library ---
29
30 // --- AliRoot header files ---
31 #include "AliLog.h"
32 #include "AliRawReaderFile.h"
33 #include "AliPMDCalibrator.h"
34 #include "AliRawReaderDate.h"
35 #include "AliPMDRawStream.h"
36 #include "AliPMDCalibData.h"
37 #include "AliPMDddldata.h"
38 #include "AliCDBManager.h"
39 #include "AliCDBId.h"
40 #include "AliCDBMetaData.h"
41 #include "AliDAQ.h"
42
43 ClassImp(AliPMDCalibrator)
44
45 const Int_t kDet =2;
46 const Int_t kMaxSMN = 24;
47 const Int_t kMaxRow =96;
48 const Int_t kMaxCol =96;
49
50 AliPMDCalibrator::AliPMDCalibrator():
51   fCalibData(new AliPMDCalibData())
52 {
53   // Standard Constructor
54   for(Int_t d=0;d<2;d++)
55     {
56       for(Int_t i=0;i<24;i++)
57         {
58           fHsmIso[d][i] = NULL ;
59           for(Int_t j=0;j<96;j++)
60             {
61               for(Int_t k=0;k<96;k++)
62                 {
63                   fGainFact[d][i][j][k] = 0.0;
64                   fHadcIso[d][i][j][k]  = NULL;
65                 }
66             }
67         }
68     }
69 }
70 // ------------------------------------------------------------------------ //
71 AliPMDCalibrator::AliPMDCalibrator(const AliPMDCalibrator &pmdcalibrator):
72   fCalibData(new AliPMDCalibData())
73 {
74   for(Int_t d=0;d<2;d++)
75     {
76       for(Int_t i=0;i<24;i++)
77         {
78           fHsmIso[d][i] = pmdcalibrator.fHsmIso[d][i] ;
79           for(Int_t j=0;j<96;j++)
80             {
81               for(Int_t k=0;k<96;k++)
82                 {
83                   fGainFact[d][i][j][k] = pmdcalibrator.fGainFact[d][i][j][k];
84                   fHadcIso[d][i][j][k]  = pmdcalibrator.fHadcIso[d][i][j][k];
85                 }
86             }
87         }
88     }
89
90 }
91 // ------------------------------------------------------------------------ //
92 AliPMDCalibrator &AliPMDCalibrator::operator=(const AliPMDCalibrator &pmdcalibrator)
93 {
94   if(this != &pmdcalibrator)
95     {
96       for(Int_t d=0;d<2;d++)
97         {
98           for(Int_t i=0;i<24;i++)
99             {
100               fHsmIso[d][i] = pmdcalibrator.fHsmIso[d][i] ;
101               for(Int_t j=0;j<96;j++)
102                 {
103                   for(Int_t k=0;k<96;k++)
104                     {
105                       fGainFact[d][i][j][k] =
106                         pmdcalibrator.fGainFact[d][i][j][k];
107                       fHadcIso[d][i][j][k]  =
108                         pmdcalibrator.fHadcIso[d][i][j][k];
109                     }
110                 }
111             }
112         }
113     }
114   return *this;
115 }
116 // ------------------------------------------------------------------------ //
117 AliPMDCalibrator::~AliPMDCalibrator()
118 {
119   // dtor
120   if(fHsmIso)  delete fHsmIso ;
121   if(fHadcIso) delete fHadcIso ;
122   delete fCalibData;
123 }
124 // ------------------------------------------------------------------------ //
125
126 void AliPMDCalibrator::Exec()
127 {
128   // reads parameters and does the calibration
129   CalculateIsoCell() ;
130
131 }
132 // ------------------------------------------------------------------------ //
133
134 void AliPMDCalibrator::Init()
135 {
136   // intializes everything
137   char hname[kMaxSMN];
138   char hname24[kMaxSMN];
139   char hnameiso[120];
140   char htitle1[120];
141
142   for(Int_t d=0;d<2;d++) {
143     for(Int_t i1=0; i1<kMaxSMN;i1++) {
144       sprintf(hname,"det_%d_iso_sm_%2d",d,i1);
145       sprintf(hname24,"det_%d_iso_sm_%2d",d,i1);
146       fHsmIso[d][i1]= new TH1F(hname,hname24,100,0,1000);
147       for(Int_t j1 = 0; j1 < kMaxRow; j1++) {
148         for(Int_t k1 = 0; k1 < kMaxCol; k1++) {
149           sprintf(hnameiso,"Isolated Cell ADC for det_%d_cell_sm%d_row%d_col%d"
150                   ,d,i1,j1,k1);
151           sprintf(htitle1,"Isolated Cell ADC for det_%d_cell_sm%d_row%d_col%d"
152                   ,d,i1,j1,k1);
153           
154           TObject *old=gDirectory->GetList()->FindObject(hnameiso);
155           if (old) gDirectory->GetList()->Remove(old);
156           fHadcIso[d][i1][j1][k1] = new TH1F(hnameiso,htitle1,100,0.,4000.);
157         }
158       }
159     }
160   }
161   
162 }
163
164 // ------------------------------------------------------------------------ //
165
166 void AliPMDCalibrator::CalculateIsoCell()
167 {
168   // Calculates the ADC of isolated cell
169
170   TObjArray pmdddlcont;
171   const Int_t kDDL           = AliDAQ::NumberOfDdls("PMD");
172   const Int_t kMaxHit        = 60000;
173   const Int_t kCellNeighbour = 6;
174
175   Int_t neibx[6] = {1,0,-1,-1,0,1};
176   Int_t neiby[6] = {0,1,1,0,-1,-1};
177   
178   Int_t id1,jd1; //neighbour row/col
179   Int_t countisocell = 0 ;//number of isilated cell
180   Int_t isocount; //number of neighbours with 0 signal
181   Int_t d1[kDet][kMaxSMN][kMaxRow][kMaxCol];
182   Int_t ch[kDet][kMaxSMN][kMaxRow][kMaxCol];
183   Int_t maxhit;
184   
185   
186   Int_t det[kMaxHit],smn[kMaxHit];
187   Int_t row[kMaxHit],col[kMaxHit],sig[kMaxHit],chno[kMaxHit];
188
189   for(Int_t idet = 0; idet < kDet; idet++)
190     {
191       for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
192         {
193           for(Int_t irow = 0; irow < kMaxRow; irow++)
194             {
195               for(Int_t icol = 0; icol < kMaxCol; icol++)
196                 {
197                   d1[idet][ismn][irow][icol] = 0;
198                   ch[idet][ismn][irow][icol] = 0;
199                 }
200             }
201         }
202     }
203   //accessing raw data
204   AliRawReaderFile reader(".");
205   AliPMDRawStream stream(&reader);
206   while(reader.NextEvent())
207     {
208       // printf("In CalculateIsoCell before while(stream.Next()), ...\n");
209       
210       /*
211       while(stream.Next())
212         {
213           Int_t idet = stream.GetDetector();
214           Int_t ismn = stream.GetSMN();
215           Int_t ichno = stream.GetChannel();
216           Int_t irow = stream.GetRow();
217           Int_t icol = stream.GetColumn();
218           Int_t isig = stream.GetSignal();
219           
220           if (isig>0)
221             {
222               d1[idet][ismn][irow][icol] = isig;
223               ch[idet][ismn][irow][icol] = ichno;
224             }
225         }
226       */
227       // New PMD Reader is plugged in
228       
229       for (Int_t iddl = 0; iddl < kDDL; iddl++)
230         {
231           reader.Select("PMD", iddl, iddl);
232           stream.DdlData(&pmdddlcont);
233           
234           Int_t ientries = pmdddlcont.GetEntries();
235           for (Int_t ient = 0; ient < ientries; ient++)
236             {
237               AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont.UncheckedAt(ient);
238               
239               Int_t idet = pmdddl->GetDetector();
240               Int_t ismn = pmdddl->GetSMN();
241               //Int_t mcm = pmdddl->GetMCM();
242               Int_t ichno = pmdddl->GetChannel();
243               Int_t irow = pmdddl->GetRow();
244               Int_t icol = pmdddl->GetColumn();
245               Int_t isig = pmdddl->GetSignal();
246               
247               if (isig>0)
248                 {
249                   d1[idet][ismn][irow][icol] = isig;
250                   ch[idet][ismn][irow][icol] = ichno;
251                 }
252             }
253           pmdddlcont.Clear();
254         }
255
256
257       maxhit = 0;
258       
259       for(Int_t idet=0; idet < kDet; idet++)
260         {
261           for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
262             {
263               for(Int_t irow = 0; irow < kMaxRow; irow++)
264                 {
265                   for(Int_t icol = 0; icol < kMaxCol; icol++)
266                     {
267                       
268                       //printf("d1[%d][%d][%d][%d]=%d\n",
269                       //det,ksmn,irow,jcol, d1[det][ksmn][irow][jcol] ); 
270                       //printf("ch[%d][%d][%d][%d]=%d\n",
271                       //det,ksmn,irow,jcol, ch[det][ksmn][irow][jcol] ); 
272                       
273                       if(d1[idet][ismn][irow][icol] > 0)
274                         {
275                           isocount = 0;
276                           for(Int_t ii = 0; ii < kCellNeighbour; ii++)
277                             {
278                               id1 = irow + neibx[ii];
279                               jd1 = icol + neiby[ii];
280                               if(d1[idet][ismn][id1][jd1] == 0)
281                                 {
282                                   isocount++;
283                                   if(isocount == kCellNeighbour)
284                                     {
285                                       countisocell++;
286                                       det[maxhit]  = idet;
287                                       smn[maxhit]  = ismn;
288                                       row[maxhit]  = irow;
289                                       col[maxhit]  = icol;
290                                       sig[maxhit]  = d1[idet][ismn][irow][icol];
291                                       chno[maxhit] = ch[idet][ismn][irow][icol];
292                                       maxhit++;
293                                       fHsmIso[idet][ismn]->Fill(d1[idet][ismn][irow][icol]);
294                                       fHadcIso[idet][ismn][irow][icol]->Fill(d1[idet][ismn][irow][icol]);
295                                     }
296                                 }
297                             }  // neigh cell cond.
298                         }     // d>0 cond.
299                     }
300                 }
301             }
302         } //event loop
303     }
304   Double_t histMean[2][24];
305   Double_t isoMean[2][24][96][96];
306   for(Int_t d1=0;d1<2;d1++)
307     {
308       for(Int_t i1=0;i1<24;i1++)
309         {
310           histMean[d1][i1]= fHsmIso[d1][i1]->GetMean();
311           for(Int_t j1=0;j1<96;j1++)
312             {
313               for(Int_t k1=0;k1<96;k1++)
314                 {
315                   isoMean[d1][i1][j1][k1]=fHadcIso[d1][i1][j1][k1]->GetMean();
316                   if(isoMean[d1][i1][j1][k1]>0.0 && histMean[d1][i1]>0.0)
317                     {
318                       fGainFact[d1][i1][j1][k1]=isoMean[d1][i1][k1][j1]/histMean[d1][i1];
319                       Float_t gain=fGainFact[d1][i1][j1][k1];
320                       fCalibData->SetGainFact(d1,i1,j1,k1,gain);
321                     }                              
322                 }
323             }
324         }
325     }
326   
327 }
328 // ------------------------------------------------------------------------ //
329 Bool_t AliPMDCalibrator::Store()
330 {
331   AliCDBManager *man = AliCDBManager::Instance();
332   man->SetDefaultStorage("local://$ALICE_ROOT");
333   AliCDBId id("PMD/Calib/Data",0,0);
334   AliCDBMetaData md;
335   md.SetResponsible("Zubayer");
336   md.SetBeamPeriod(0);
337   md.SetAliRootVersion("28.02.2006");
338   md.SetComment("Test");
339   
340   printf("\n\n\n fCalibData\n");
341   //fCalibData->Print(0);
342   //printf("\n\n\n fCalibData\n");
343   
344   Bool_t result = man->Put(fCalibData,id,&md);
345
346   return result;
347 }
348