Run type changed: PHYSICS only
[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{
108 //Process LED run, fill bad channels map.
109
110 AliPHOSEmcBadChannelsMap badMap;
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
118 TIter iter(list);
119 TObjString *source;
120 char hnam[80];
121 TH1F* histo=0;
122
123 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
124
125 AliInfo(Form("found source %s", source->String().Data()));
126
127 TString fileName = GetFile(kDAQ, "LED", source->GetName());
128 AliInfo(Form("Got filename: %s",fileName.Data()));
129
130 TFile f(fileName);
131
132 if(!f.IsOpen()) {
133 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
134 return kFALSE;
135 }
136
137 const Int_t nMod=5; // 1:5 modules
138 const Int_t nCol=56; //1:56 columns in each module
139 const Int_t nRow=64; //1:64 rows in each module
140
141 // Check for dead channels
142 Log(Form("Begin check for dead channels."));
143
144 for(Int_t mod=0; mod<nMod; mod++) {
145 for(Int_t col=0; col<nCol; col++) {
146 for(Int_t row=0; row<nRow; row++) {
147 sprintf(hnam,"mod%dcol%drow%d",mod,col,row);
148 histo = (TH1F*)f.Get(hnam);
149 if(histo)
150 if (histo->GetMean()<1) {
151 Log(Form("Channel: [%d,%d,%d] seems dead, <E>=%.1f.",mod,col,row,histo->GetMean()));
152 badMap.SetBadChannel(mod,col,row);
153 }
154 }
155 }
156 }
157
158 }
159
160 //Store bad channels map
161 AliCDBMetaData badMapMetaData;
162
163 //Bad channels data valid from current run fRun until updated (validityInfinite=kTRUE)
164 Bool_t result = Store("Calib", "EmcBadChannels", &badMap, &badMapMetaData, fRun, kTRUE);
165 return result;
166
167}
168
169Float_t AliPHOSPreprocessor::HG2LG(Int_t mod, Int_t X, Int_t Z, TFile* f)
170{
171 //Calculates High gain to Low gain ratio
172 //for crystal at the position (X,Z) in the PHOS module mod.
173
174 char hname[128];
175 sprintf(hname,"%d_%d_%d",mod,X,Z);
176
177 TH1F* h1 = (TH1F*)f->Get(hname);
178 if(!h1) return 16.;
179
180 if(!h1->GetEntries()) return 16.;
181 if(h1->GetMaximum()<10.) return 16.;
182
183 Double_t max = h1->GetBinCenter(h1->GetMaximumBin()); // peak
184 Double_t xmin = max - (h1->GetRMS()/3);
185 Double_t xmax = max + (h1->GetRMS()/2);
186 // Double_t xmin = max - (h1->GetRMS());
187 // Double_t xmax = max + (h1->GetRMS());
188
189 TF1* gaus1 = new TF1("gaus1",rgaus,xmin,xmax,3);
190 gaus1->SetParNames("Constant","Mean","Sigma");
191 gaus1->SetParameter("Constant",h1->GetMaximum());
192 gaus1->SetParameter("Mean",max);
193 gaus1->SetParameter("Sigma",1.);
194 gaus1->SetLineColor(kBlue);
195 h1->Fit(gaus1,"LERQ+");
196
3f5f9893 197 AliInfo(Form("%s: %d entries, mean=%.3f, peak=%.3f, rms= %.3f. HG/LG = %.3f\n",
9f43d7da 198 h1->GetTitle(),h1->GetEntries(),h1->GetMean(),max,h1->GetRMS(),
199 gaus1->GetParameter("Mean")));
200
201 return gaus1->GetParameter("Mean");
202
203}
204
205Bool_t AliPHOSPreprocessor::FindBadChannelsEmc()
206{
5db29457 207 //Loop over two systems: DAQ and HLT.
208 //For each system the same algorithm implemented in DoFindBadChannelsEmc() invokes.
9f43d7da 209
5db29457 210 TList* list=0;
211 TString path;
212
213 Int_t system[2] = { kDAQ, kHLT };
214 const char* sysn[] = { "DAQ","HLT" };
215 Bool_t result[2] = { kTRUE, kTRUE };
9f43d7da 216
5db29457 217 for (Int_t i=0; i<2; i++) {
9f43d7da 218
5db29457 219 AliPHOSEmcBadChannelsMap badMap;
220 list = GetFileSources(system[i], "BAD_CHANNELS");
9f43d7da 221
5db29457 222 if(!list) {
223 Log(Form("%s sources list for BAD_CHANNELS not found!",sysn[i]));
224 result[i] = kFALSE;
225 continue;
226 }
9f43d7da 227
5db29457 228 if(!list->GetEntries()) {
229 Log(Form("Got empty sources list. It seems %s DA2 did not produce any files!",sysn[i]));
230 result[i] = kFALSE;
231 continue;
232 }
233
234 result[i] *= DoFindBadChannelsEmc(system[i],list,badMap);
235
236 // Store the bad channels map.
237
238 AliCDBMetaData md;
239 md.SetResponsible("Boris Polishchuk");
240
241 if(system[i] == kDAQ)
242 path = "Calib";
243 else
244 path = "HLT";
245
246 // Data valid from current run fRun until being updated (validityInfinite=kTRUE)
247 result[i] *= Store(path.Data(), "EmcBadChannels", &badMap, &md, fRun, kTRUE);
248
9f43d7da 249 }
5db29457 250
251 if(result[0] || result[1]) return kTRUE;
252 else return kFALSE;
253}
254
255Bool_t AliPHOSPreprocessor::DoFindBadChannelsEmc(Int_t system, TList* list, AliPHOSEmcBadChannelsMap& badMap)
256{
257 //Creates the bad channels map for PHOS EMC.
258
259 // The file fileName contains histograms which have been produced by DA2 detector algorithm.
260 // It is a responsibility of the SHUTTLE framework to form the fileName.
9f43d7da 261
262 TIter iter(list);
263 TObjString *source;
264 char hnam[80];
265 TH1F* h1=0;
266
267 const Float_t fQualityCut = 1.;
268 Int_t nGoods[5] = {0,0,0,0,0};
269
270 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
271
272 AliInfo(Form("found source %s", source->String().Data()));
273
5db29457 274 TString fileName = GetFile(system, "BAD_CHANNELS", source->GetName());
9f43d7da 275 AliInfo(Form("Got filename: %s",fileName.Data()));
276
277 TFile f(fileName);
278
279 if(!f.IsOpen()) {
280 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
281 return kFALSE;
282 }
283
284 Log(Form("Begin check for bad channels."));
285
286 for(Int_t mod=0; mod<5; mod++) {
287 for(Int_t iX=0; iX<64; iX++) {
288 for(Int_t iZ=0; iZ<56; iZ++) {
289
290 sprintf(hnam,"%d_%d_%d_%d",mod,iX,iZ,1); // high gain
291 h1 = (TH1F*)f.Get(hnam);
292
293 if(h1) {
294 Double_t mean = h1->GetMean();
295
296 if(mean)
297 Log(Form("iX=%d iZ=%d gain=%d mean=%.3f\n",iX,iZ,1,mean));
298
299 if( mean>0 && mean<fQualityCut ) {
300 nGoods[mod]++;
301 }
302 else
303 badMap.SetBadChannel(mod+1,iZ+1,iX+1); //module, col,row
304 }
305
306 }
307 }
308
309 if(nGoods[mod])
310 Log(Form("Module %d: %d good channels.",mod,nGoods[mod]));
311 }
312
313
314 } // end of loop over sources
315
5db29457 316 return kTRUE;
9f43d7da 317}
318
319Bool_t AliPHOSPreprocessor::CalibrateEmc()
320{
5db29457 321 //Loop over two systems: DAQ and HLT.
322 //For each system the same algorithm implemented in DoCalibrateEmc() invokes.
9f43d7da 323
5db29457 324 const AliPHOSEmcBadChannelsMap* badMap=0;
325 AliCDBEntry* entryBCM=0;
326 TList* list=0;
327 TString path;
328
329 Int_t system[2] = { kDAQ, kHLT };
330 const char* sysn[] = { "DAQ","HLT" };
331 Bool_t result[2] = { kTRUE, kTRUE };
eba66a50 332
5db29457 333 for (Int_t i=0; i<2; i++) {
334
335 AliPHOSEmcCalibData calibData;
336 list = GetFileSources(system[i], "AMPLITUDES");
9f43d7da 337
5db29457 338 if(!list) {
339 Log(Form("%s sources list not found!",sysn[i]));
340 result[i] = kFALSE;
341 continue;
342 }
343
344 if(!list->GetEntries()) {
345 Log(Form("Got empty sources list. It seems %s DA1 did not produce any files!",sysn[i]));
346 result[i] = kFALSE;
347 continue;
348 }
349
350 // Retrieve the Bad Channels Map (BCM)
351
352 if(system[i] == kDAQ)
353 path = "Calib";
354 else
355 path = "HLT";
9f43d7da 356
5db29457 357 entryBCM = GetFromOCDB(path.Data(), "EmcBadChannels");
1ab07e55 358
5db29457 359 if(!entryBCM)
360 Log(Form("WARNING!! Cannot find any AliCDBEntry for [%s, EmcBadChannels]!",path.Data()));
361 else
362 badMap = (AliPHOSEmcBadChannelsMap*)entryBCM->GetObject();
363
364 if(!badMap)
365 Log(Form("WARNING!! Nothing for %s in AliCDBEntry. All cells considered GOOD!",sysn[i]));
366
367 result[i] *= DoCalibrateEmc(system[i],list,badMap,calibData);
368
369 //Store EMC calibration data
370
371 AliCDBMetaData emcMetaData;
372 result[i] *= Store(path.Data(), "EmcGainPedestals", &calibData, &emcMetaData, 0, kTRUE);
373
9f43d7da 374 }
5db29457 375
376 if(result[0] || result[1]) return kTRUE;
377 else return kFALSE;
378}
9f43d7da 379
30a0d5a2 380
5db29457 381Bool_t AliPHOSPreprocessor::DoCalibrateEmc(Int_t system, TList* list, const AliPHOSEmcBadChannelsMap* badMap, AliPHOSEmcCalibData& calibData)
382{
383 // Return kTRUE if OK.
384 // I. Calculates the set of calibration coefficients to equalyze the mean energies deposited at high gain.
385 // II. Extracts High_Gain/Low_Gain ratio for each channel.
30a0d5a2 386
5db29457 387 // The file fileName contains histograms which have been produced by DA1 detector algorithm.
388 // It is a responsibility of the SHUTTLE framework to form the fileName.
30a0d5a2 389
5db29457 390 gRandom->SetSeed(0); //the seed is set to the current machine clock!
30a0d5a2 391
516e7ab3 392 TIter iter(list);
393 TObjString *source;
3c0265c1 394
516e7ab3 395 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
396 AliInfo(Form("found source %s", source->String().Data()));
f97eb136 397
5db29457 398 TString fileName = GetFile(system, "AMPLITUDES", source->GetName());
516e7ab3 399 AliInfo(Form("Got filename: %s",fileName.Data()));
f97eb136 400
516e7ab3 401 TFile f(fileName);
f97eb136 402
516e7ab3 403 if(!f.IsOpen()) {
404 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
5db29457 405 return kFALSE;
516e7ab3 406 }
9f43d7da 407
516e7ab3 408 const Int_t nMod=5; // 1:5 modules
409 const Int_t nCol=56; //1:56 columns in each module
410 const Int_t nRow=64; //1:64 rows in each module
411
412 Double_t coeff;
413 char hnam[80];
450717bc 414 TH2F* h2=0;
415 TH1D* h1=0;
3c0265c1 416
516e7ab3 417 //Get the reference histogram
418 //(author: Gustavo Conesa Balbastre)
419
420 TList * keylist = f.GetListOfKeys();
421 Int_t nkeys = f.GetNkeys();
422 Bool_t ok = kFALSE;
423 TKey *key;
516e7ab3 424 Int_t ikey = 0;
425 Int_t counter = 0;
450717bc 426 TH1D* hRef = 0;
427
516e7ab3 428 //Check if the file contains any histogram
429
430 if(nkeys< 2){
431 Log(Form("Not enough histograms (%d) for calibration.",nkeys));
5db29457 432 return 0;
f97eb136 433 }
450717bc 434
516e7ab3 435 while(!ok){
436 ikey = gRandom->Integer(nkeys);
437 key = (TKey*)keylist->At(ikey);
450717bc 438 TObject* obj = f.Get(key->GetName());
439 TString cname(obj->ClassName());
440 if(cname == "TH2F") {
441 h2 = (TH2F*)obj;
442 TString htitl = h2->GetTitle();
443 if(htitl.Contains("and gain 1")) {
444 hRef = h2->ProjectionX();
306e5917 445 hRef->GetXaxis()->SetRange(10,1000); // to cut off saturation peak and noise
450717bc 446 // Check if the reference histogram has too little statistics
306e5917 447 if(hRef->GetMean() && hRef->GetEntries()>2) ok=kTRUE;
30a0d5a2 448
449 const TString delim = "_";
450 TString str = hRef->GetName();
451 TObjArray* tks = str.Tokenize(delim);
452 const Int_t md = ((TObjString*)tks->At(0))->GetString().Atoi();
453 const Int_t X = ((TObjString*)tks->At(1))->GetString().Atoi();
454 const Int_t Z = ((TObjString*)tks->At(2))->GetString().Atoi();
455
456 if(badMap) {
457 if(badMap->IsBadChannel(md+1,Z+1,X+1)) {
458 AliInfo(Form("Cell mod=%d col=%d row=%d is bad. Histogram %s rejected.",
459 md+1,Z+1,X+1,hRef->GetName()));
460 ok=kFALSE;
461 }
462 }
463
450717bc 464 }
516e7ab3 465 }
b8093789 466
450717bc 467 counter++;
b8093789 468
469 if(!ok && counter > nkeys){
470 Log("No histogram with enough statistics for reference. Exit.");
5db29457 471 return 0;
b8093789 472 }
516e7ab3 473 }
3c0265c1 474
516e7ab3 475 Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
476 hRef->GetName(),hRef->GetEntries(),
477 hRef->GetMean(),hRef->GetRMS()));
478
479 Double_t refMean=hRef->GetMean();
3c0265c1 480
516e7ab3 481 // Calculates relative calibration coefficients for all non-zero channels
3c0265c1 482
516e7ab3 483 for(Int_t mod=0; mod<nMod; mod++) {
484 for(Int_t col=0; col<nCol; col++) {
485 for(Int_t row=0; row<nRow; row++) {
450717bc 486
487 //High Gain to Low Gain ratio
488 Float_t ratio = HG2LG(mod,row,col,&f);
489 calibData.SetHighLowRatioEmc(mod+1,col+1,row+1,ratio);
490
491 sprintf(hnam,"%d_%d_%d_1",mod,row,col); // high gain!
492 h2 = (TH2F*)f.Get(hnam);
493
516e7ab3 494 //TODO: dead channels exclusion!
450717bc 495 if(h2) {
496 h1 = h2->ProjectionX();
306e5917 497 h1->GetXaxis()->SetRange(10,1000); //to cut off saturation peak and noise
450717bc 498 coeff = h1->GetMean()/refMean;
64d8f3f1 499 if(coeff>0)
da2ee9fc 500 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1./coeff);
64d8f3f1 501 else
da2ee9fc 502 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
516e7ab3 503 AliInfo(Form("mod %d col %d row %d coeff %f\n",mod,col,row,coeff));
504 }
505 else
da2ee9fc 506 calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
1ab07e55 507 }
1ab07e55 508 }
509 }
516e7ab3 510
511 f.Close();
1ab07e55 512 }
516e7ab3 513
5db29457 514 return 1;
1ab07e55 515}
450717bc 516
9f43d7da 517
450717bc 518