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