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