feature added to store and fetch from DB for online gain calibration
[u/mrichter/AliRoot.git] / PMD / AliPMDCalibGain.cxx
CommitLineData
4fc06b3a 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
19#include "TF1.h"
20#include "TFile.h"
21#include "TObjString.h"
22#include "TROOT.h"
23#include "TClonesArray.h"
24#include "TH1F.h"
25#include "TObjArray.h"
26#include "TTree.h"
27
28// --- Standard library ---
29
30// --- AliRoot header files ---
31#include "AliDAQ.h"
32#include "AliLog.h"
33#include "AliRawReader.h"
34#include "AliPMDRawStream.h"
35#include "AliPMDddldata.h"
36#include "AliPMDCalibGain.h"
37
38ClassImp(AliPMDCalibGain)
39
1da50520 40AliPMDCalibGain::AliPMDCalibGain():
41 TObject(),
42 fpw(NULL)
4fc06b3a 43{
44 // Standard Constructor
45 for(Int_t idet = 0; idet < kDet; idet++)
46 {
1da50520 47 fDetCount[kDet] =0.;
4fc06b3a 48 for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
49 {
0ab3a530 50 fSMIso[idet][ismn] = 0.;
51 fSMCount[idet][ismn] = 0.;
4fc06b3a 52 for(Int_t jrow = 0; jrow < kMaxRow; jrow++)
53 {
54 for(Int_t kcol = 0; kcol < kMaxCol; kcol++)
55 {
0ab3a530 56 fCellIso[idet][ismn][jrow][kcol] = 0.;
57 fCellCount[idet][ismn][jrow][kcol] = 0.;
1da50520 58 fPedMeanRMS[idet][ismn][jrow][kcol] = 0.;
59
4fc06b3a 60 }
61 }
62 }
63 }
0ab3a530 64
1da50520 65
66
4fc06b3a 67}
68// ------------------------------------------------------------------------ //
69AliPMDCalibGain::AliPMDCalibGain(const AliPMDCalibGain &pmdcalibgain):
1da50520 70 TObject(pmdcalibgain),
71 fpw(NULL)
4fc06b3a 72{
73 for(Int_t idet = 0; idet < kDet; idet++)
74 {
1da50520 75 fDetCount[idet] = pmdcalibgain.fDetCount[idet];
76 fDetIso[idet] = pmdcalibgain.fDetIso[idet];
4fc06b3a 77 for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
78 {
0ab3a530 79 fSMIso[idet][ismn] = pmdcalibgain.fSMIso[idet][ismn] ;
80 fSMCount[idet][ismn] = pmdcalibgain.fSMCount[idet][ismn] ;
4fc06b3a 81 for(Int_t jrow = 0; jrow < kMaxRow; jrow++)
82 {
83 for(Int_t kcol = 0; kcol < kMaxCol; kcol++)
84 {
1da50520 85 fCellIso[idet][ismn][jrow][kcol] =
86 pmdcalibgain.fCellIso[idet][ismn][jrow][kcol];
87 fCellCount[idet][ismn][jrow][kcol] =
88 pmdcalibgain.fCellCount[idet][ismn][jrow][kcol];
89 fPedMeanRMS[idet][ismn][jrow][kcol] =
90 pmdcalibgain.fPedMeanRMS[idet][ismn][jrow][kcol];
91
4fc06b3a 92 }
93 }
94 }
95 }
96
97}
98// ------------------------------------------------------------------------ //
99AliPMDCalibGain &AliPMDCalibGain::operator=(const AliPMDCalibGain &pmdcalibgain)
100{
101 if(this != &pmdcalibgain)
102 {
1da50520 103 this->fpw = pmdcalibgain.fpw;
4fc06b3a 104 for(Int_t idet = 0; idet < kDet; idet++)
105 {
106 for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
107 {
0ab3a530 108 fSMIso[idet][ismn] = pmdcalibgain.fSMIso[idet][ismn];
109 fSMCount[idet][ismn] = pmdcalibgain.fSMCount[idet][ismn];
4fc06b3a 110 for(Int_t jrow = 0; jrow < kMaxRow;jrow++)
111 {
112 for(Int_t kcol = 0; kcol < kMaxCol; kcol++)
1da50520 113 {
0ab3a530 114 fCellIso[idet][ismn][jrow][kcol] =
1da50520 115 pmdcalibgain.fCellIso[idet][ismn][jrow][kcol];
0ab3a530 116 fCellCount[idet][ismn][jrow][kcol] =
1da50520 117 pmdcalibgain.fCellCount[idet][ismn][jrow][kcol];
118 fPedMeanRMS[idet][ismn][jrow][kcol] =
119 pmdcalibgain.fPedMeanRMS[idet][ismn][jrow][kcol];
120
4fc06b3a 121 }
122 }
123 }
124 }
125 }
126 return *this;
127}
128// ------------------------------------------------------------------------ //
129AliPMDCalibGain::~AliPMDCalibGain()
130{
131 // dtor
0ab3a530 132
4fc06b3a 133}
1da50520 134
135// ------------------------------------------------------------------------ //
136
d31b0352 137Int_t AliPMDCalibGain::ExtractPedestal(const Char_t *rootFile)
1da50520 138{
139 // Pedestal extraction from the PMD_PED.root file
140 // To be called once at the beginning
141
142 Int_t det, sm, row, col;
143 Float_t mean, rms;
144
d31b0352 145 TFile *pedfile = new TFile(rootFile);
1da50520 146
147 if(!pedfile)
148 {
149 printf("ERROR --- NO PEDESTAL (PMD_PED1.root) FILE IS FOUND --- STOP GAIN DA\n");
150 return -3;
151 }
152
153
154 TTree *ped =(TTree*)pedfile->Get("ped");
155
156 ped->SetBranchAddress("det",&det);
157 ped->SetBranchAddress("sm",&sm);
158 ped->SetBranchAddress("row",&row);
159 ped->SetBranchAddress("col",&col);
160 ped->SetBranchAddress("mean",&mean);
161 ped->SetBranchAddress("rms",&rms);
162
163 Int_t nentries = (Int_t)ped->GetEntries();
164
165 for (Int_t ient = 0; ient < nentries; ient++)
166 {
167 ped->GetEntry(ient);
168 fPedMeanRMS[det][sm][row][col] = mean + 3.*rms;
169 //printf("Mean= %f, RMS= %f, PedMeanRMS=%f\n",mean,rms,fPedMeanRMS[det][sm][row][col]);
170
171 }
172
173 pedfile->Close();
174 delete pedfile;
175 pedfile = 0x0;
176
177 return 1;
178}
179// ------------------------------------------------------------------------ //
180
d31b0352 181void AliPMDCalibGain::ReadTempFile(const Char_t *tempFile)
1da50520 182{
183 // Read the variables from the file
184
d31b0352 185 fpw = fopen(tempFile,"r");
1da50520 186
187 Float_t detcount, detiso;
188 Float_t smcount, smiso;
189 Float_t cellcount, celliso;
190
191 for (Int_t idet = 0; idet < kDet; idet++)
192 {
193 fscanf(fpw,"%d %f %f",&idet,&detcount,&detiso);
194 fDetCount[idet] = detcount;
195 fDetIso[idet] = detiso;
196 }
197
198 for (Int_t idet = 0; idet < kDet; idet++)
199 {
200 for (Int_t ism = 0; ism < kMaxSMN; ism++)
201 {
202 fscanf(fpw,"%d %d %f %f",&idet,&ism,&smcount,&smiso);
203
204 fSMCount[idet][ism] = smcount;
205 fSMIso[idet][ism] = smiso;
206 }
207 }
208
209 for (Int_t idet = 0; idet < kDet; idet++)
210 {
211 for (Int_t ism = 0; ism < kMaxSMN; ism++)
212 {
213 for (Int_t irow = 0; irow < kMaxRow; irow++)
214 {
215 for (Int_t icol = 0; icol < kMaxCol; icol++)
216 {
217 fscanf(fpw,"%d %d %d %d %f %f",&idet,&ism,&irow,&icol,
218 &cellcount,&celliso);
219
220 fCellCount[idet][ism][irow][icol] = cellcount;
221 fCellIso[idet][ism][irow][icol] = celliso;
222 }
223 }
224 }
225 }
226
227 fclose(fpw);
228
229}
4fc06b3a 230// ------------------------------------------------------------------------ //
d31b0352 231void AliPMDCalibGain::WriteTempFile(const Char_t *tempFile)
1da50520 232{
d31b0352 233 // Write the Temporary file if the required statics is not achieved
1da50520 234
1da50520 235
236 /*
d31b0352 237 Following variables to be written to a file
1da50520 238 fDetIso[idet] ;
239 fSMIso[idet][ismn];
240 fCellIso[idet][ismn][irow][icol];
241
242 fDetCount[idet];
243 fSMCount[idet][ismn];
244 fCellCount[idet][ismn][irow][icol];
245 */
246
d31b0352 247
248 fpw = fopen(tempFile,"w+");
1da50520 249
250 for (Int_t idet = 0; idet < kDet; idet++)
251 {
252 fprintf(fpw,"%d %f %f\n",idet,fDetCount[idet],fDetIso[idet]);
253 }
254
255 for (Int_t idet = 0; idet < kDet; idet++)
256 {
257 for (Int_t ism = 0; ism < kMaxSMN; ism++)
258 {
259 fprintf(fpw,"%d %d %f %f\n",idet,ism, fSMCount[idet][ism],fSMIso[idet][ism]);
260 }
261 }
262
263 for (Int_t idet = 0; idet < kDet; idet++)
264 {
265 for (Int_t ism = 0; ism < kMaxSMN; ism++)
266 {
267 for (Int_t irow = 0; irow < kMaxRow; irow++)
268 {
269 for (Int_t icol = 0; icol < kMaxCol; icol++)
270 {
271 fprintf(fpw,"%d %d %d %d %f %f\n",idet,ism,irow,icol,
272 fCellCount[idet][ism][irow][icol],
273 fCellIso[idet][ism][irow][icol]);
274 }
275 }
276 }
277 }
278
279 fclose(fpw);
280
281}
282
283// ------------------------------------------------------------------------ //
284
338c285a 285Bool_t AliPMDCalibGain::ProcessEvent(AliRawReader *rawReader, TObjArray *pmdddlcont)
4fc06b3a 286{
287 // Calculates the ADC of isolated cell
288
4fc06b3a 289 const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
290 const Int_t kCellNeighbour = 6;
4fc06b3a 291 Int_t neibx[6] = {1,0,-1,-1,0,1};
292 Int_t neiby[6] = {0,1,1,0,-1,-1};
293
294 Int_t id1,jd1; //neighbour row/col
295 Int_t isocount; //number of neighbours with 0 signal
296
0ab3a530 297 Float_t d1[kDet][kMaxSMN][kMaxRow][kMaxCol];
4fc06b3a 298
299 for(Int_t idet = 0; idet < kDet; idet++)
300 {
301 for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
302 {
303 for(Int_t irow = 0; irow < kMaxRow; irow++)
304 {
305 for(Int_t icol = 0; icol < kMaxCol; icol++)
306 {
0ab3a530 307 d1[idet][ismn][irow][icol] = 0.;
4fc06b3a 308 }
309 }
310 }
311 }
312
313 AliPMDRawStream rawStream(rawReader);
314
1da50520 315 Int_t iddl = -1;
722ccc67 316
317 Int_t numberofDDLs = 0;
318
338c285a 319 while ((iddl = rawStream.DdlData(pmdddlcont)) >=0) {
722ccc67 320 numberofDDLs++;
321
338c285a 322 Int_t ientries = pmdddlcont->GetEntries();
1da50520 323
4fc06b3a 324 for (Int_t ient = 0; ient < ientries; ient++)
325 {
338c285a 326 AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont->UncheckedAt(ient);
4fc06b3a 327
328 Int_t idet = pmdddl->GetDetector();
329 Int_t ismn = pmdddl->GetSMN();
d31b0352 330 Int_t mcm = pmdddl->GetMCM();
4fc06b3a 331 //Int_t ichno = pmdddl->GetChannel();
332 Int_t irow = pmdddl->GetRow();
333 Int_t icol = pmdddl->GetColumn();
334 Int_t isig = pmdddl->GetSignal();
1da50520 335
d31b0352 336 // This is the protection not to crash the code
337
338 if(mcm == 0) continue;
339 if (irow < 0 || icol < 0 || irow > 47 || icol > 95) continue;
1da50520 340
341 // Pedestal subtraction
342
4fc06b3a 343 if (isig>0)
1da50520 344 {
345 d1[idet][ismn][irow][icol] =
346 (Float_t) isig - fPedMeanRMS[idet][ismn][irow][icol];
347//printf("Signal_ped_subtracted=%f, pedestal=%f\n",d1[idet][ismn][irow][icol]),fPedMeanRMS[idet][ismn][irow][icol];
348 }
4fc06b3a 349 }
338c285a 350 pmdddlcont->Delete();
4fc06b3a 351 }
352
353 for(Int_t idet=0; idet < kDet; idet++)
354 {
355 for(Int_t ismn = 0; ismn < kMaxSMN; ismn++)
356 {
357 for(Int_t irow = 0; irow < kMaxRow; irow++)
358 {
359 for(Int_t icol = 0; icol < kMaxCol; icol++)
360 {
361 if(d1[idet][ismn][irow][icol] > 0)
362 {
363 isocount = 0;
364 for(Int_t ii = 0; ii < kCellNeighbour; ii++)
365 {
366 id1 = irow + neibx[ii];
367 jd1 = icol + neiby[ii];
368 if(d1[idet][ismn][id1][jd1] == 0)
369 {
370 isocount++;
371 if(isocount == kCellNeighbour)
372 {
1da50520 373 fDetIso[idet] += d1[idet][ismn][irow][icol];
0ab3a530 374 fSMIso[idet][ismn] += d1[idet][ismn][irow][icol];
375 fCellIso[idet][ismn][irow][icol] += d1[idet][ismn][irow][icol];
1da50520 376 fDetCount[idet]++;
0ab3a530 377 fSMCount[idet][ismn]++;
378 fCellCount[idet][ismn][irow][icol]++;
379
4fc06b3a 380 }
381 }
382 } // neigh cell cond.
383 } // d>0 cond.
384 }
385 }
386 }
4fc06b3a 387 }
722ccc67 388
389 if (numberofDDLs < kDDL)
390 return kFALSE;
391 return kTRUE;
392
4fc06b3a 393}
394// ------------------------------------------------------------------------ //
395void AliPMDCalibGain::Analyse(TTree *gaintree)
396{
397 // Calculates the mean
0ab3a530 398 Int_t det, sm, row, col;
399 Float_t gain;
400 Float_t modmean = 0.;
401 Float_t cellmean = 0.;
1da50520 402 Float_t detmean =0.;
4fc06b3a 403
0ab3a530 404 gaintree->Branch("det",&det,"det/I");
405 gaintree->Branch("sm",&sm,"sm/I");
406 gaintree->Branch("row",&row,"row/I");
407 gaintree->Branch("col",&col,"col/I");
408 gaintree->Branch("gain",&gain,"gain/F");
4fc06b3a 409
410 for(Int_t idet = 0; idet < kDet; idet++)
1da50520 411 {
412 if (fDetCount[idet]>0 )
413 detmean=fDetIso[idet]/fDetCount[idet];
4fc06b3a 414 for(Int_t ism = 0; ism < kMaxSMN; ism++)
1da50520 415 {
0ab3a530 416 if (fSMCount[idet][ism] > 0)
1da50520 417 modmean = fSMIso[idet][ism]/fSMCount[idet][ism];
4fc06b3a 418 for(Int_t irow = 0; irow < kMaxRow; irow++)
1da50520 419 {
4fc06b3a 420 for(Int_t icol = 0; icol < kMaxCol; icol++)
1da50520 421 {
1da50520 422 if (fCellCount[idet][ism][irow][icol] > 0.)
423 {
424 cellmean = fCellIso[idet][ism][irow][icol]/fCellCount[idet][ism][irow][icol];
425 }
0ab3a530 426 det = idet;
427 sm = ism;
428 row = irow;
429 col = icol;
1da50520 430 if (cellmean > 0.0 && fCellCount[idet][ism][irow][icol]>0.)
431 {
432 gain = cellmean/detmean;
433 }
434 else
435 {
436 gain = -1.;
437 }
438 //if(fCellCount[idet][ism][irow][icol]>0.) printf("CellCount =%f, gain= %f\n",fCellCount[idet][ism][irow][icol],gain);
4fc06b3a 439 gaintree->Fill();
1da50520 440 }
441 }
442 }
443 }
4fc06b3a 444
445}
446// ------------------------------------------------------------------------ //