]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSPreprocessor.cxx
Bug fix: corrected file name (Levente)
[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     if(ledOK) return 0;
84     else
85       return 1;
86   }
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();
94
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
106 Bool_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
169 Float_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
197   AliInfo(Form("%s: %d entries, mean=%.3f, peak=%.3f, rms= %.3f. HG/LG = %.3f\n",
198            h1->GetTitle(),h1->GetEntries(),h1->GetMean(),max,h1->GetRMS(),
199            gaus1->GetParameter("Mean"))); 
200
201   return gaus1->GetParameter("Mean");
202
203 }
204
205 Bool_t AliPHOSPreprocessor::FindBadChannelsEmc()
206 {
207   //Creates the bad channels map for PHOS EMC.
208
209   // The file fileName contains histograms which have been produced by DA2 detector algorithm.
210   // It is a responsibility of the SHUTTLE framework to form the fileName.
211
212   AliPHOSEmcBadChannelsMap badMap;
213
214   TList* list = GetFileSources(kDAQ, "BAD_CHANNELS");
215
216   if(!list) {
217     Log("Sources list for BAD_CHANNELS not found, exit.");
218     return kFALSE;
219   }
220
221   if(!list->GetEntries()) {
222     Log(Form("Got empty sources list. It seems DA2 did not produce any files!"));
223     return kFALSE;
224   }
225
226   TIter iter(list);
227   TObjString *source;
228   char hnam[80];
229   TH1F* h1=0;
230
231   const Float_t fQualityCut = 1.;
232   Int_t nGoods[5] = {0,0,0,0,0};
233   
234   while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
235
236     AliInfo(Form("found source %s", source->String().Data()));
237
238     TString fileName = GetFile(kDAQ, "BAD_CHANNELS", source->GetName());
239     AliInfo(Form("Got filename: %s",fileName.Data()));
240
241     TFile f(fileName);
242
243     if(!f.IsOpen()) {
244       Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
245       return kFALSE;
246     }
247     
248     Log(Form("Begin check for bad channels."));
249     
250     for(Int_t mod=0; mod<5; mod++) {
251       for(Int_t iX=0; iX<64; iX++) {
252         for(Int_t iZ=0; iZ<56; iZ++) {
253
254           sprintf(hnam,"%d_%d_%d_%d",mod,iX,iZ,1); // high gain 
255           h1 = (TH1F*)f.Get(hnam);
256
257           if(h1) {
258             Double_t mean = h1->GetMean();
259             
260             if(mean)
261               Log(Form("iX=%d iZ=%d gain=%d   mean=%.3f\n",iX,iZ,1,mean));
262
263             if( mean>0 && mean<fQualityCut ) { 
264               nGoods[mod]++; 
265             }
266             else
267               badMap.SetBadChannel(mod+1,iZ+1,iX+1); //module, col,row
268           }
269
270         }
271       }
272
273       if(nGoods[mod])
274         Log(Form("Module %d: %d good channels.",mod,nGoods[mod]));
275     }
276
277     
278   } // end of loop over sources
279   
280   // Store the bad channels map.
281   
282   AliCDBMetaData md;
283   md.SetResponsible("Boris Polishchuk");
284
285   // Data valid from current run fRun until being updated (validityInfinite=kTRUE)
286   Bool_t result = Store("Calib", "EmcBadChannels", &badMap, &md, fRun, kTRUE);
287   return result;
288
289 }
290
291 Bool_t AliPHOSPreprocessor::CalibrateEmc()
292 {
293   // I.  Calculates the set of calibration coefficients to equalyze the mean energies deposited at high gain.
294   // II. Extracts High_Gain/Low_Gain ratio for each channel.
295
296   // The file fileName contains histograms which have been produced by DA1 detector algorithm.
297   // It is a responsibility of the SHUTTLE framework to form the fileName.
298
299   gRandom->SetSeed(0); //the seed is set to the current  machine clock!
300   AliPHOSEmcCalibData calibData;
301   
302   TList* list = GetFileSources(kDAQ, "AMPLITUDES");
303   
304   if(!list) {
305     Log("Sources list not found, exit.");
306     return 1;
307   }
308
309   if(!list->GetEntries()) {
310     Log(Form("Got empty sources list. It seems DA1 did not produce any files!"));
311     return kFALSE;
312   }
313
314   // Retrieve Bad Channels Map (BCM)
315
316   const AliPHOSEmcBadChannelsMap* badMap=0;
317   AliCDBEntry* entryBCM = GetFromOCDB("Calib", "EmcBadChannels");
318
319   if(!entryBCM)
320     Log(Form("WARNING!! Cannot find any AliCDBEntry for [Calib, EmcBadChannels]!"));
321   else
322     badMap = (AliPHOSEmcBadChannelsMap*)entryBCM->GetObject();
323
324   if(!badMap)
325     Log(Form("WARNING!! No Bad Channels Map in AliCDBEntry. All cells considered _GOOD_!"));
326
327   TIter iter(list);
328   TObjString *source;
329   
330   while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
331     AliInfo(Form("found source %s", source->String().Data()));
332
333     TString fileName = GetFile(kDAQ, "AMPLITUDES", source->GetName());
334     AliInfo(Form("Got filename: %s",fileName.Data()));
335
336     TFile f(fileName);
337
338     if(!f.IsOpen()) {
339       Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
340       return 1;
341     }
342     
343     const Int_t nMod=5; // 1:5 modules
344     const Int_t nCol=56; //1:56 columns in each module
345     const Int_t nRow=64; //1:64 rows in each module
346
347     Double_t coeff;
348     char hnam[80];
349     TH2F* h2=0;
350     TH1D* h1=0;
351     
352     //Get the reference histogram
353     //(author: Gustavo Conesa Balbastre)
354
355     TList * keylist = f.GetListOfKeys();
356     Int_t nkeys   = f.GetNkeys();
357     Bool_t ok = kFALSE;
358     TKey  *key;
359     Int_t ikey = 0;
360     Int_t counter = 0;
361     TH1D* hRef = 0;
362
363     //Check if the file contains any histogram
364     
365     if(nkeys< 2){
366       Log(Form("Not enough histograms (%d) for calibration.",nkeys));
367       return 1;
368     }
369     
370     while(!ok){
371       ikey = gRandom->Integer(nkeys);
372       key = (TKey*)keylist->At(ikey);
373       TObject* obj = f.Get(key->GetName());
374       TString cname(obj->ClassName());
375       if(cname == "TH2F") {
376         h2 = (TH2F*)obj;
377         TString htitl = h2->GetTitle();
378         if(htitl.Contains("and gain 1")) {
379           hRef = h2->ProjectionX();
380           hRef->GetXaxis()->SetRange(10,1000); // to cut off saturation peak and noise
381           // Check if the reference histogram has too little statistics
382           if(hRef->GetMean() && hRef->GetEntries()>2) ok=kTRUE;
383
384           const TString delim = "_";
385           TString str = hRef->GetName();
386           TObjArray* tks = str.Tokenize(delim);
387           const Int_t md = ((TObjString*)tks->At(0))->GetString().Atoi();
388           const Int_t X   = ((TObjString*)tks->At(1))->GetString().Atoi();
389           const Int_t Z   = ((TObjString*)tks->At(2))->GetString().Atoi();
390
391           if(badMap) {
392             if(badMap->IsBadChannel(md+1,Z+1,X+1)) {
393               AliInfo(Form("Cell mod=%d col=%d row=%d is bad. Histogram %s rejected.",
394                            md+1,Z+1,X+1,hRef->GetName()));
395               ok=kFALSE;
396             }
397           }
398
399         }
400       }
401       
402       counter++;
403       
404       if(!ok && counter > nkeys){
405         Log("No histogram with enough statistics for reference. Exit.");
406         return 1;
407       }
408     }
409     
410     Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
411              hRef->GetName(),hRef->GetEntries(),
412              hRef->GetMean(),hRef->GetRMS()));
413
414     Double_t refMean=hRef->GetMean();
415     
416     // Calculates relative calibration coefficients for all non-zero channels
417     
418     for(Int_t mod=0; mod<nMod; mod++) {
419       for(Int_t col=0; col<nCol; col++) {
420         for(Int_t row=0; row<nRow; row++) {
421
422           //High Gain to Low Gain ratio
423           Float_t ratio = HG2LG(mod,row,col,&f);
424           calibData.SetHighLowRatioEmc(mod+1,col+1,row+1,ratio);
425           
426           sprintf(hnam,"%d_%d_%d_1",mod,row,col); // high gain!
427           h2 = (TH2F*)f.Get(hnam);
428
429           //TODO: dead channels exclusion!
430           if(h2) {
431             h1 = h2->ProjectionX();
432             h1->GetXaxis()->SetRange(10,1000); //to cut off saturation peak and noise
433             coeff = h1->GetMean()/refMean;
434             if(coeff>0)
435               calibData.SetADCchannelEmc(mod+1,col+1,row+1,1./coeff);
436             else 
437               calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.);
438             AliInfo(Form("mod %d col %d row %d  coeff %f\n",mod,col,row,coeff));
439           }
440           else
441             calibData.SetADCchannelEmc(mod+1,col+1,row+1,1.); 
442         }
443       }
444     }
445     
446     f.Close();
447   }
448   
449   //Store EMC calibration data
450   
451   AliCDBMetaData emcMetaData;
452   Bool_t result = Store("Calib", "EmcGainPedestals", &calibData, &emcMetaData, 0, kTRUE); // valid from 0 to infinity);
453   return result;
454
455 }
456
457      
458