Adding pad gain factors to be used in dEdx calculation - (Marian)
[u/mrichter/AliRoot.git] / PHOS / AliPHOSPreprocessor.cxx
CommitLineData
1ab07e55 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/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// PHOS Preprocessor class. It runs by Shuttle at the end of the run,
20// calculates calibration coefficients and dead/bad channels
21// to be posted in OCDB
22//
23// Author: Boris Polichtchouk, 4 October 2006
24///////////////////////////////////////////////////////////////////////////////
25
26#include "AliPHOSPreprocessor.h"
27#include "AliLog.h"
28#include "AliCDBMetaData.h"
30a0d5a2 29#include "AliCDBEntry.h"
1ab07e55 30#include "AliPHOSEmcCalibData.h"
31#include "TFile.h"
32#include "TH1.h"
450717bc 33#include "TF1.h"
34#include "TH2.h"
1ab07e55 35#include "TMap.h"
36#include "TRandom.h"
f97eb136 37#include "TKey.h"
516e7ab3 38#include "TList.h"
39#include "TObjString.h"
30a0d5a2 40#include "TObjArray.h"
450717bc 41#include "TObject.h"
42#include "TString.h"
43#include "TMath.h"
306e5917 44#include "TAxis.h"
3c0265c1 45#include "AliPHOSEmcBadChannelsMap.h"
1ab07e55 46
47ClassImp(AliPHOSPreprocessor)
48
450717bc 49 Double_t rgaus(Double_t *x, Double_t *par)
50{
51 Double_t gaus = par[0] * TMath::Exp( -(x[0]-par[1])*(x[0]-par[1]) /
52 (2*par[2]*par[2]) );
53 return gaus;
54}
55
1ab07e55 56//_______________________________________________________________________________________
57AliPHOSPreprocessor::AliPHOSPreprocessor() :
81587b1e 58AliPreprocessor("PHS",0)
1ab07e55 59{
60 //default constructor
61}
62
63//_______________________________________________________________________________________
81587b1e 64AliPHOSPreprocessor::AliPHOSPreprocessor(AliShuttleInterface* shuttle):
65AliPreprocessor("PHS",shuttle)
1ab07e55 66{
67 // Constructor
ce2c6758 68
69 AddRunType("PHYSICS");
70 AddRunType("STANDALONE");
1ab07e55 71}
72
73//_______________________________________________________________________________________
74UInt_t AliPHOSPreprocessor::Process(TMap* /*valueSet*/)
75{
76 // process data retrieved by the Shuttle
516e7ab3 77
3c0265c1 78 TString runType = GetRunType();
79 Log(Form("Run type: %s",runType.Data()));
80
9f43d7da 81 if(runType=="STANDALONE") {
eba66a50 82 Bool_t ledOK = ProcessLEDRun();
83 if(ledOK) return 0;
84 else
85 return 1;
86 }
9f43d7da 87
88 if(runType=="PHYSICS") {
89
90 Bool_t badmap_OK = FindBadChannelsEmc();
91 if(!badmap_OK) Log(Form("WARNING!! FindBadChannels() completed with BAD status!"));
92
93 Bool_t calibEmc_OK = CalibrateEmc();
5db29457 94
9f43d7da 95 if(calibEmc_OK && badmap_OK) return 0;
96 else
97 return 1;
98 }
99
100 Log(Form("Unknown run type %s. Do nothing and return OK.",runType.Data()));
101 return 0;
102
103}
104
105
106Bool_t AliPHOSPreprocessor::ProcessLEDRun()
107{
12661472 108 //Process LED run, update High Gain/Low Gain ratios.
9f43d7da 109
12661472 110 AliPHOSEmcCalibData calibData;
9f43d7da 111
112 TList* list = GetFileSources(kDAQ, "LED");
113 if(!list) {
114 Log("Sources list for LED run not found, exit.");
115 return kFALSE;
116 }
117
12661472 118 if(!list->GetEntries()) {
119 Log("Sources list for LED run is empty, exit.");
120 return kFALSE;
121 }
122
123 //Retrieve the last EMC calibration object
124 const AliPHOSEmcCalibData* clb=0;
125 AliCDBEntry* entryCalib = GetFromOCDB("Calib", "EmcGainPedestals");
126
127 if(!entryCalib)
128 Log(Form("Cannot find any AliCDBEntry for [Calib, EmcGainPedestals]!"));
129 else
130 clb = (AliPHOSEmcCalibData*)entryCalib->GetObject();
131
9f43d7da 132 TIter iter(list);
133 TObjString *source;
9f43d7da 134
135 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
12661472 136
9f43d7da 137 AliInfo(Form("found source %s", source->String().Data()));
138
139 TString fileName = GetFile(kDAQ, "LED", source->GetName());
140 AliInfo(Form("Got filename: %s",fileName.Data()));
141
142 TFile f(fileName);
143
144 if(!f.IsOpen()) {
145 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
146 return kFALSE;
147 }
148
149 const Int_t nMod=5; // 1:5 modules
150 const Int_t nCol=56; //1:56 columns in each module
151 const Int_t nRow=64; //1:64 rows in each module
12661472 152
9f43d7da 153 for(Int_t mod=0; mod<nMod; mod++) {
154 for(Int_t col=0; col<nCol; col++) {
155 for(Int_t row=0; row<nRow; row++) {
12661472 156
157 //High Gain to Low Gain ratio
158 Float_t ratio = HG2LG(mod,row,col,&f);
159 calibData.SetHighLowRatioEmc(mod+1,col+1,row+1,ratio);
160 if(ratio != 16.)
161 AliInfo(Form("mod %d iX %d iZ %d ratio %.3f\n",mod,row,col,ratio));
162
163 if(clb) {
164 Double_t coeff = clb->GetADCchannelEmc(mod+1,col+1,row+1);
165 calibData.SetADCchannelEmc(mod+1,col+1,row+1,coeff);
166 }
167 else
168 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
169
9f43d7da 170 }
171 }
172 }
173
12661472 174 } // end of loop over files
9f43d7da 175
176 //Store bad channels map
12661472 177 AliCDBMetaData emcMetaData;
9f43d7da 178
12661472 179 //Data valid from current run fRun until updated (validityInfinite=kTRUE)
180 Bool_t result = Store("Calib","EmcGainPedestals",&calibData,&emcMetaData,fRun,kTRUE);
9f43d7da 181 return result;
182
183}
184
185Float_t AliPHOSPreprocessor::HG2LG(Int_t mod, Int_t X, Int_t Z, TFile* f)
186{
187 //Calculates High gain to Low gain ratio
188 //for crystal at the position (X,Z) in the PHOS module mod.
189
190 char hname[128];
191 sprintf(hname,"%d_%d_%d",mod,X,Z);
192
193 TH1F* h1 = (TH1F*)f->Get(hname);
194 if(!h1) return 16.;
195
196 if(!h1->GetEntries()) return 16.;
197 if(h1->GetMaximum()<10.) return 16.;
198
199 Double_t max = h1->GetBinCenter(h1->GetMaximumBin()); // peak
200 Double_t xmin = max - (h1->GetRMS()/3);
201 Double_t xmax = max + (h1->GetRMS()/2);
202 // Double_t xmin = max - (h1->GetRMS());
203 // Double_t xmax = max + (h1->GetRMS());
204
205 TF1* gaus1 = new TF1("gaus1",rgaus,xmin,xmax,3);
206 gaus1->SetParNames("Constant","Mean","Sigma");
207 gaus1->SetParameter("Constant",h1->GetMaximum());
208 gaus1->SetParameter("Mean",max);
209 gaus1->SetParameter("Sigma",1.);
210 gaus1->SetLineColor(kBlue);
211 h1->Fit(gaus1,"LERQ+");
212
3f5f9893 213 AliInfo(Form("%s: %d entries, mean=%.3f, peak=%.3f, rms= %.3f. HG/LG = %.3f\n",
9f43d7da 214 h1->GetTitle(),h1->GetEntries(),h1->GetMean(),max,h1->GetRMS(),
215 gaus1->GetParameter("Mean")));
216
217 return gaus1->GetParameter("Mean");
218
219}
220
221Bool_t AliPHOSPreprocessor::FindBadChannelsEmc()
222{
5db29457 223 //Loop over two systems: DAQ and HLT.
224 //For each system the same algorithm implemented in DoFindBadChannelsEmc() invokes.
9f43d7da 225
5db29457 226 TList* list=0;
227 TString path;
228
229 Int_t system[2] = { kDAQ, kHLT };
230 const char* sysn[] = { "DAQ","HLT" };
231 Bool_t result[2] = { kTRUE, kTRUE };
9f43d7da 232
5db29457 233 for (Int_t i=0; i<2; i++) {
9f43d7da 234
5db29457 235 AliPHOSEmcBadChannelsMap badMap;
236 list = GetFileSources(system[i], "BAD_CHANNELS");
9f43d7da 237
5db29457 238 if(!list) {
239 Log(Form("%s sources list for BAD_CHANNELS not found!",sysn[i]));
240 result[i] = kFALSE;
241 continue;
242 }
9f43d7da 243
5db29457 244 if(!list->GetEntries()) {
245 Log(Form("Got empty sources list. It seems %s DA2 did not produce any files!",sysn[i]));
246 result[i] = kFALSE;
247 continue;
248 }
249
250 result[i] *= DoFindBadChannelsEmc(system[i],list,badMap);
251
252 // Store the bad channels map.
253
254 AliCDBMetaData md;
255 md.SetResponsible("Boris Polishchuk");
256
257 if(system[i] == kDAQ)
258 path = "Calib";
259 else
260 path = "HLT";
261
262 // Data valid from current run fRun until being updated (validityInfinite=kTRUE)
263 result[i] *= Store(path.Data(), "EmcBadChannels", &badMap, &md, fRun, kTRUE);
264
9f43d7da 265 }
5db29457 266
267 if(result[0] || result[1]) return kTRUE;
268 else return kFALSE;
269}
270
271Bool_t AliPHOSPreprocessor::DoFindBadChannelsEmc(Int_t system, TList* list, AliPHOSEmcBadChannelsMap& badMap)
272{
273 //Creates the bad channels map for PHOS EMC.
274
275 // The file fileName contains histograms which have been produced by DA2 detector algorithm.
276 // It is a responsibility of the SHUTTLE framework to form the fileName.
9f43d7da 277
278 TIter iter(list);
279 TObjString *source;
280 char hnam[80];
281 TH1F* h1=0;
282
283 const Float_t fQualityCut = 1.;
284 Int_t nGoods[5] = {0,0,0,0,0};
285
286 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
287
288 AliInfo(Form("found source %s", source->String().Data()));
289
5db29457 290 TString fileName = GetFile(system, "BAD_CHANNELS", source->GetName());
9f43d7da 291 AliInfo(Form("Got filename: %s",fileName.Data()));
292
293 TFile f(fileName);
294
295 if(!f.IsOpen()) {
296 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
297 return kFALSE;
298 }
299
300 Log(Form("Begin check for bad channels."));
301
302 for(Int_t mod=0; mod<5; mod++) {
303 for(Int_t iX=0; iX<64; iX++) {
304 for(Int_t iZ=0; iZ<56; iZ++) {
305
306 sprintf(hnam,"%d_%d_%d_%d",mod,iX,iZ,1); // high gain
307 h1 = (TH1F*)f.Get(hnam);
308
309 if(h1) {
310 Double_t mean = h1->GetMean();
311
312 if(mean)
313 Log(Form("iX=%d iZ=%d gain=%d mean=%.3f\n",iX,iZ,1,mean));
314
315 if( mean>0 && mean<fQualityCut ) {
316 nGoods[mod]++;
317 }
318 else
319 badMap.SetBadChannel(mod+1,iZ+1,iX+1); //module, col,row
320 }
321
322 }
323 }
324
325 if(nGoods[mod])
326 Log(Form("Module %d: %d good channels.",mod,nGoods[mod]));
327 }
328
329
330 } // end of loop over sources
331
5db29457 332 return kTRUE;
9f43d7da 333}
334
335Bool_t AliPHOSPreprocessor::CalibrateEmc()
336{
5db29457 337 //Loop over two systems: DAQ and HLT.
338 //For each system the same algorithm implemented in DoCalibrateEmc() invokes.
9f43d7da 339
5db29457 340 const AliPHOSEmcBadChannelsMap* badMap=0;
341 AliCDBEntry* entryBCM=0;
342 TList* list=0;
343 TString path;
344
345 Int_t system[2] = { kDAQ, kHLT };
346 const char* sysn[] = { "DAQ","HLT" };
347 Bool_t result[2] = { kTRUE, kTRUE };
eba66a50 348
5db29457 349 for (Int_t i=0; i<2; i++) {
350
351 AliPHOSEmcCalibData calibData;
352 list = GetFileSources(system[i], "AMPLITUDES");
9f43d7da 353
5db29457 354 if(!list) {
355 Log(Form("%s sources list not found!",sysn[i]));
356 result[i] = kFALSE;
357 continue;
358 }
359
360 if(!list->GetEntries()) {
361 Log(Form("Got empty sources list. It seems %s DA1 did not produce any files!",sysn[i]));
362 result[i] = kFALSE;
363 continue;
364 }
365
366 // Retrieve the Bad Channels Map (BCM)
367
368 if(system[i] == kDAQ)
369 path = "Calib";
370 else
371 path = "HLT";
9f43d7da 372
5db29457 373 entryBCM = GetFromOCDB(path.Data(), "EmcBadChannels");
1ab07e55 374
5db29457 375 if(!entryBCM)
376 Log(Form("WARNING!! Cannot find any AliCDBEntry for [%s, EmcBadChannels]!",path.Data()));
377 else
378 badMap = (AliPHOSEmcBadChannelsMap*)entryBCM->GetObject();
379
380 if(!badMap)
381 Log(Form("WARNING!! Nothing for %s in AliCDBEntry. All cells considered GOOD!",sysn[i]));
382
383 result[i] *= DoCalibrateEmc(system[i],list,badMap,calibData);
384
385 //Store EMC calibration data
386
387 AliCDBMetaData emcMetaData;
388 result[i] *= Store(path.Data(), "EmcGainPedestals", &calibData, &emcMetaData, 0, kTRUE);
389
9f43d7da 390 }
5db29457 391
392 if(result[0] || result[1]) return kTRUE;
393 else return kFALSE;
394}
9f43d7da 395
30a0d5a2 396
5db29457 397Bool_t AliPHOSPreprocessor::DoCalibrateEmc(Int_t system, TList* list, const AliPHOSEmcBadChannelsMap* badMap, AliPHOSEmcCalibData& calibData)
398{
399 // Return kTRUE if OK.
400 // I. Calculates the set of calibration coefficients to equalyze the mean energies deposited at high gain.
401 // II. Extracts High_Gain/Low_Gain ratio for each channel.
30a0d5a2 402
5db29457 403 // The file fileName contains histograms which have been produced by DA1 detector algorithm.
404 // It is a responsibility of the SHUTTLE framework to form the fileName.
30a0d5a2 405
5db29457 406 gRandom->SetSeed(0); //the seed is set to the current machine clock!
30a0d5a2 407
516e7ab3 408 TIter iter(list);
409 TObjString *source;
3c0265c1 410
516e7ab3 411 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
412 AliInfo(Form("found source %s", source->String().Data()));
f97eb136 413
5db29457 414 TString fileName = GetFile(system, "AMPLITUDES", source->GetName());
516e7ab3 415 AliInfo(Form("Got filename: %s",fileName.Data()));
f97eb136 416
516e7ab3 417 TFile f(fileName);
f97eb136 418
516e7ab3 419 if(!f.IsOpen()) {
420 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
5db29457 421 return kFALSE;
516e7ab3 422 }
9f43d7da 423
516e7ab3 424 const Int_t nMod=5; // 1:5 modules
425 const Int_t nCol=56; //1:56 columns in each module
426 const Int_t nRow=64; //1:64 rows in each module
427
428 Double_t coeff;
429 char hnam[80];
450717bc 430 TH2F* h2=0;
431 TH1D* h1=0;
3c0265c1 432
516e7ab3 433 //Get the reference histogram
434 //(author: Gustavo Conesa Balbastre)
435
436 TList * keylist = f.GetListOfKeys();
437 Int_t nkeys = f.GetNkeys();
438 Bool_t ok = kFALSE;
439 TKey *key;
516e7ab3 440 Int_t ikey = 0;
441 Int_t counter = 0;
450717bc 442 TH1D* hRef = 0;
443
516e7ab3 444 //Check if the file contains any histogram
445
446 if(nkeys< 2){
447 Log(Form("Not enough histograms (%d) for calibration.",nkeys));
5db29457 448 return 0;
f97eb136 449 }
450717bc 450
516e7ab3 451 while(!ok){
452 ikey = gRandom->Integer(nkeys);
453 key = (TKey*)keylist->At(ikey);
450717bc 454 TObject* obj = f.Get(key->GetName());
455 TString cname(obj->ClassName());
456 if(cname == "TH2F") {
457 h2 = (TH2F*)obj;
458 TString htitl = h2->GetTitle();
459 if(htitl.Contains("and gain 1")) {
460 hRef = h2->ProjectionX();
306e5917 461 hRef->GetXaxis()->SetRange(10,1000); // to cut off saturation peak and noise
450717bc 462 // Check if the reference histogram has too little statistics
306e5917 463 if(hRef->GetMean() && hRef->GetEntries()>2) ok=kTRUE;
30a0d5a2 464
465 const TString delim = "_";
466 TString str = hRef->GetName();
467 TObjArray* tks = str.Tokenize(delim);
468 const Int_t md = ((TObjString*)tks->At(0))->GetString().Atoi();
469 const Int_t X = ((TObjString*)tks->At(1))->GetString().Atoi();
470 const Int_t Z = ((TObjString*)tks->At(2))->GetString().Atoi();
471
472 if(badMap) {
473 if(badMap->IsBadChannel(md+1,Z+1,X+1)) {
474 AliInfo(Form("Cell mod=%d col=%d row=%d is bad. Histogram %s rejected.",
475 md+1,Z+1,X+1,hRef->GetName()));
476 ok=kFALSE;
477 }
478 }
479
450717bc 480 }
516e7ab3 481 }
b8093789 482
450717bc 483 counter++;
b8093789 484
485 if(!ok && counter > nkeys){
486 Log("No histogram with enough statistics for reference. Exit.");
5db29457 487 return 0;
b8093789 488 }
516e7ab3 489 }
3c0265c1 490
516e7ab3 491 Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
492 hRef->GetName(),hRef->GetEntries(),
493 hRef->GetMean(),hRef->GetRMS()));
494
495 Double_t refMean=hRef->GetMean();
3c0265c1 496
516e7ab3 497 // Calculates relative calibration coefficients for all non-zero channels
3c0265c1 498
516e7ab3 499 for(Int_t mod=0; mod<nMod; mod++) {
500 for(Int_t col=0; col<nCol; col++) {
501 for(Int_t row=0; row<nRow; row++) {
450717bc 502
503 //High Gain to Low Gain ratio
504 Float_t ratio = HG2LG(mod,row,col,&f);
505 calibData.SetHighLowRatioEmc(mod+1,col+1,row+1,ratio);
506
507 sprintf(hnam,"%d_%d_%d_1",mod,row,col); // high gain!
508 h2 = (TH2F*)f.Get(hnam);
509
516e7ab3 510 //TODO: dead channels exclusion!
450717bc 511 if(h2) {
512 h1 = h2->ProjectionX();
306e5917 513 h1->GetXaxis()->SetRange(10,1000); //to cut off saturation peak and noise
450717bc 514 coeff = h1->GetMean()/refMean;
64d8f3f1 515 if(coeff>0)
da2ee9fc 516 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1./coeff);
64d8f3f1 517 else
da2ee9fc 518 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
516e7ab3 519 AliInfo(Form("mod %d col %d row %d coeff %f\n",mod,col,row,coeff));
520 }
521 else
da2ee9fc 522 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
1ab07e55 523 }
1ab07e55 524 }
525 }
516e7ab3 526
527 f.Close();
1ab07e55 528 }
516e7ab3 529
5db29457 530 return 1;
1ab07e55 531}
450717bc 532
9f43d7da 533
450717bc 534