1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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
23 // Author: Boris Polichtchouk, 4 October 2006
24 ///////////////////////////////////////////////////////////////////////////////
26 #include "AliPHOSPreprocessor.h"
28 #include "AliCDBMetaData.h"
29 #include "AliCDBEntry.h"
30 #include "AliPHOSEmcCalibData.h"
39 #include "TObjString.h"
40 #include "TObjArray.h"
45 #include "AliPHOSEmcBadChannelsMap.h"
47 ClassImp(AliPHOSPreprocessor)
49 Double_t rgaus(Double_t *x, Double_t *par)
51 Double_t gaus = par[0] * TMath::Exp( -(x[0]-par[1])*(x[0]-par[1]) /
56 //_______________________________________________________________________________________
57 AliPHOSPreprocessor::AliPHOSPreprocessor() :
58 AliPreprocessor("PHS",0)
63 //_______________________________________________________________________________________
64 AliPHOSPreprocessor::AliPHOSPreprocessor(AliShuttleInterface* shuttle):
65 AliPreprocessor("PHS",shuttle)
69 AddRunType("PHYSICS");
73 //_______________________________________________________________________________________
74 UInt_t AliPHOSPreprocessor::Process(TMap* /*valueSet*/)
76 // process data retrieved by the Shuttle
78 TString runType = GetRunType();
79 Log(Form("Run type: %s",runType.Data()));
82 Bool_t ledOK = ProcessLEDRun();
83 Bool_t badmap_OK = FindBadChannelsEmc();
84 if(!badmap_OK) Log(Form("WARNING! FindBadChannels() completed with BAD status!"));
90 if(runType=="PHYSICS") {
92 Bool_t calibEmc_OK = CalibrateEmc();
94 if(calibEmc_OK) return 0;
99 Log(Form("Unknown run type %s. Do nothing and return OK.",runType.Data()));
105 Bool_t AliPHOSPreprocessor::ProcessLEDRun()
107 //Process LED run, update High Gain/Low Gain ratios.
109 AliPHOSEmcCalibData calibData;
111 TList* list = GetFileSources(kDAQ, "LED");
113 Log("Sources list for LED run not found, exit.");
117 if(!list->GetEntries()) {
118 Log("Sources list for LED run is empty, exit.");
122 //Retrieve the last EMC calibration object
123 const AliPHOSEmcCalibData* clb=0;
124 AliCDBEntry* entryCalib = GetFromOCDB("Calib", "EmcGainPedestals");
127 Log(Form("Cannot find any AliCDBEntry for [Calib, EmcGainPedestals]!"));
129 clb = (AliPHOSEmcCalibData*)entryCalib->GetObject();
134 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
136 AliInfo(Form("found source %s", source->String().Data()));
138 TString fileName = GetFile(kDAQ, "LED", source->GetName());
139 AliInfo(Form("Got filename: %s",fileName.Data()));
144 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
148 TH1I* fFiredCells = (TH1I*)f.Get("fFiredCells");
151 const Double_t nFiredCells = fFiredCells->GetMean();
152 Log(Form("Number of fired cells per event is %.1f",nFiredCells));
155 const Int_t nMod=5; // 1:5 modules
156 const Int_t nCol=56; //1:56 columns in each module
157 const Int_t nRow=64; //1:64 rows in each module
159 for(Int_t mod=0; mod<nMod; mod++) {
160 for(Int_t col=0; col<nCol; col++) {
161 for(Int_t row=0; row<nRow; row++) {
164 Float_t hg2lg = clb->GetHighLowRatioEmc(5-mod,col+1,row+1);
165 Double_t coeff = clb->GetADCchannelEmc(5-mod,col+1,row+1);
166 calibData.SetADCchannelEmc(5-mod,col+1,row+1,coeff);
167 calibData.SetHighLowRatioEmc(5-mod,col+1,row+1,hg2lg);
170 //High Gain to Low Gain ratio
171 Float_t ratio = HG2LG(mod,row,col,&f);
173 calibData.SetHighLowRatioEmc(5-mod,col+1,row+1,ratio);
174 AliInfo(Form("mod %d iX %d iZ %d ratio %.3f\n",mod,row,col,ratio));
180 } // end of loop over files
182 //Store the updated High Gain/Low Gain ratios
183 AliCDBMetaData emcMetaData;
185 //Data valid from current run until updated (validityInfinite=kTRUE)
186 Bool_t result = Store("Calib","EmcGainPedestals",&calibData,&emcMetaData,0,kTRUE);
191 Float_t AliPHOSPreprocessor::HG2LG(Int_t mod, Int_t X, Int_t Z, TFile* f)
193 //Calculates High gain to Low gain ratio
194 //for crystal at the position (X,Z) in the PHOS module mod.
197 sprintf(hname,"%d_%d_%d",mod,X,Z);
199 TH1F* h1 = (TH1F*)f->Get(hname);
202 if(h1->GetEntries()<2000.) return 16.;
204 if(h1->GetMaximum()<10.) h1->Rebin(4);
205 if(h1->GetMaximum()<10.) return 16.;
207 Double_t max = h1->GetBinCenter(h1->GetMaximumBin()); // peak
208 Double_t xmin = max - (h1->GetRMS()/3);
209 Double_t xmax = max + (h1->GetRMS()/2);
210 // Double_t xmin = max - (h1->GetRMS());
211 // Double_t xmax = max + (h1->GetRMS());
213 TF1* gaus1 = new TF1("gaus1",rgaus,xmin,xmax,3);
214 gaus1->SetParNames("Constant","Mean","Sigma");
215 gaus1->SetParameter("Constant",h1->GetMaximum());
216 gaus1->SetParameter("Mean",max);
217 gaus1->SetParameter("Sigma",1.);
218 gaus1->SetLineColor(kBlue);
220 Double_t mean_min = h1->GetXaxis()->GetXmin();
221 Double_t mean_max = h1->GetXaxis()->GetXmax();
222 gaus1->SetParLimits(1,mean_min,mean_max);
224 h1->Fit(gaus1,"RQ+");
225 Double_t hg2lg = gaus1->GetParameter("Mean");
226 if( (hg2lg-mean_min<0.001) || (mean_max-hg2lg<0.001)) hg2lg=max;
228 AliInfo(Form("%s: %.1f entries, mean=%.3f, peak=%.3f, rms= %.3f. HG/LG = %.3f\n",
229 h1->GetTitle(),h1->GetEntries(),h1->GetMean(),max,h1->GetRMS(),hg2lg));
235 Bool_t AliPHOSPreprocessor::FindBadChannelsEmc()
237 //Loop over two systems: DAQ and HLT.
238 //For each system the same algorithm implemented in DoFindBadChannelsEmc() invokes.
243 Int_t system[2] = { kDAQ, kHLT };
244 const char* sysn[] = { "DAQ","HLT" };
245 Bool_t result[2] = { kTRUE, kTRUE };
247 for (Int_t i=0; i<2; i++) {
249 if(system[i] == kHLT) continue;
251 AliPHOSEmcBadChannelsMap badMap;
252 list = GetFileSources(system[i], "BAD_CHANNELS");
255 Log(Form("%s sources list for BAD_CHANNELS not found!",sysn[i]));
260 if(!list->GetEntries()) {
261 Log(Form("Got empty sources list. It seems %s DA2 did not produce any files!",sysn[i]));
266 result[i] *= DoFindBadChannelsEmc(system[i],list,badMap);
268 // Store the bad channels map.
271 md.SetResponsible("Boris Polishchuk");
273 if(system[i] == kDAQ)
278 // Data valid from current run until being updated (validityInfinite=kTRUE)
279 result[i] *= Store(path.Data(), "EmcBadChannels", &badMap, &md, 0, kTRUE);
283 if(result[0] || result[1]) return kTRUE;
287 Bool_t AliPHOSPreprocessor::DoFindBadChannelsEmc(Int_t system, TList* list, AliPHOSEmcBadChannelsMap& badMap)
289 //Creates the bad channels map for PHOS EMC.
291 // The file fileName contains histograms which have been produced by DA2 detector algorithm.
292 // It is a responsibility of the SHUTTLE framework to form the fileName.
299 const Float_t fQualityCut = 1.;
300 Int_t nGoods[5] = {0,0,0,0,0};
302 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
304 AliInfo(Form("found source %s", source->String().Data()));
306 TString fileName = GetFile(system, "BAD_CHANNELS", source->GetName());
307 AliInfo(Form("Got filename: %s",fileName.Data()));
312 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
316 Log(Form("Begin check for bad channels."));
318 for(Int_t mod=0; mod<5; mod++) {
319 for(Int_t iX=0; iX<64; iX++) {
320 for(Int_t iZ=0; iZ<56; iZ++) {
322 sprintf(hnam,"%d_%d_%d_%d",mod,iX,iZ,1); // high gain
323 h1 = (TH1F*)f.Get(hnam);
326 Double_t mean = h1->GetMean();
329 Log(Form("iX=%d iZ=%d gain=%d mean=%.3f\n",iX,iZ,1,mean));
331 if( mean>0 && mean<fQualityCut ) {
335 badMap.SetBadChannel(mod+1,iZ+1,iX+1); //module, col,row
342 Log(Form("Module %d: %d good channels.",mod,nGoods[mod]));
346 } // end of loop over sources
351 Bool_t AliPHOSPreprocessor::CalibrateEmc()
353 //Loop over two systems: DAQ and HLT.
354 //For each system the same algorithm implemented in DoCalibrateEmc() invokes.
356 AliPHOSEmcCalibData* lastCalib=0;
357 const AliPHOSEmcBadChannelsMap* badMap=0;
358 AliCDBEntry* entryBCM=0;
359 AliCDBEntry* entryEmc=0;
363 Int_t system[2] = { kDAQ, kHLT };
364 const char* sysn[] = { "DAQ","HLT" };
365 Bool_t result[2] = { kTRUE, kTRUE };
367 for (Int_t i=0; i<2; i++) {
369 if(system[i] == kHLT) continue;
371 AliPHOSEmcCalibData calibData;
372 list = GetFileSources(system[i], "AMPLITUDES");
375 Log(Form("%s sources list not found!",sysn[i]));
380 if(!list->GetEntries()) {
381 Log(Form("Got empty sources list. It seems %s DA1 did not produce any files!",sysn[i]));
386 // Retrieve the Bad Channels Map (BCM)
388 if(system[i] == kDAQ)
393 entryBCM = GetFromOCDB(path.Data(), "EmcBadChannels");
396 Log(Form("WARNING!! Cannot find any AliCDBEntry for [%s, EmcBadChannels]!",path.Data()));
398 badMap = (AliPHOSEmcBadChannelsMap*)entryBCM->GetObject();
401 Log(Form("WARNING!! Nothing for %s in AliCDBEntry. All cells considered GOOD!",sysn[i]));
403 // Retrieve the last EMC calibration object
405 entryEmc = GetFromOCDB(path.Data(), "EmcGainPedestals");
408 Log(Form("Cannot find any EmcGainPedestals entry for this run and path %s",path.Data()));
410 lastCalib = (AliPHOSEmcCalibData*)entryEmc->GetObject();
413 result[i] *= DoCalibrateEmc(system[i],list,badMap,*lastCalib);
415 result[i] *= DoCalibrateEmc(system[i],list,badMap,calibData);
417 //Store EMC calibration data
418 AliCDBMetaData emcMetaData;
421 result[i] *= Store(path.Data(), "EmcGainPedestals", lastCalib, &emcMetaData, 0, kFALSE);
423 result[i] *= Store(path.Data(), "EmcGainPedestals", &calibData, &emcMetaData, 0, kFALSE);
425 //Store reference data
426 Bool_t refOK = StoreReferenceEmc(system[i],list);
427 if(refOK) Log(Form("Reference data for %s amplitudes successfully stored.",sysn[i]));
431 if(result[0] || result[1]) return kTRUE;
435 Bool_t AliPHOSPreprocessor::StoreReferenceEmc(Int_t system, TList* list)
437 //Put 2D calibration histograms (E vs Time) prepared by DAQ/HLT to the reference storage.
438 //system is DAQ or HLT, TList is the list of FES sources.
440 if(system!=kDAQ) return kFALSE;
442 TObjString *source = dynamic_cast<TObjString *> (list->First());
443 if(!source) return kFALSE;
445 TString fileName = GetFile(system, "AMPLITUDES", source->GetName());
447 Bool_t resultRef = StoreReferenceFile(fileName.Data(),"CalibRefPHOS.root");
453 Bool_t AliPHOSPreprocessor::DoCalibrateEmc(Int_t system, TList* list, const AliPHOSEmcBadChannelsMap* badMap, AliPHOSEmcCalibData& calibData)
455 // Return kTRUE if OK.
456 // I. Calculates the set of calibration coefficients to equalyze the mean energies deposited at high gain.
457 // II. Extracts High_Gain/Low_Gain ratio for each channel.
459 // The file fileName contains histograms which have been produced by DA1 detector algorithm.
460 // It is a responsibility of the SHUTTLE framework to form the fileName.
462 gRandom->SetSeed(0); //the seed is set to the current machine clock!
463 Int_t minEntries=1000; // recalculate calibration coeff. if Nentries > minEntries.
468 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
469 AliInfo(Form("found source %s", source->String().Data()));
471 TString fileName = GetFile(system, "AMPLITUDES", source->GetName());
472 AliInfo(Form("Got filename: %s",fileName.Data()));
477 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
481 const Int_t nMod=5; // 1:5 modules
482 const Int_t nCol=56; //1:56 columns in each module
483 const Int_t nRow=64; //1:64 rows in each module
490 //Get the reference histogram
491 //(author: Gustavo Conesa Balbastre)
493 TList * keylist = f.GetListOfKeys();
494 Int_t nkeys = f.GetNkeys();
501 //Check if the file contains any histogram
504 Log(Form("Not enough histograms (%d) for calibration.",nkeys));
505 return 1; // it's not fatal! May be short run..
509 ikey = gRandom->Integer(nkeys);
510 key = (TKey*)keylist->At(ikey);
511 TObject* obj = f.Get(key->GetName());
512 TString cname(obj->ClassName());
513 if(cname == "TH2F") {
515 TString htitl = h2->GetTitle();
516 if(htitl.Contains("and gain 1")) {
517 hRef = h2->ProjectionX();
518 hRef->GetXaxis()->SetRangeUser(10.,1000.); // to cut off saturation peak and noise
519 // Check if the reference histogram has too little statistics
520 if(hRef->GetMean() && hRef->GetEntries()>minEntries) ok=kTRUE;
522 const TString delim = "_";
523 TString str = hRef->GetName();
524 TObjArray* tks = str.Tokenize(delim);
525 const Int_t md = ((TObjString*)tks->At(0))->GetString().Atoi();
526 const Int_t X = ((TObjString*)tks->At(1))->GetString().Atoi();
527 const Int_t Z = ((TObjString*)tks->At(2))->GetString().Atoi();
530 if(badMap->IsBadChannel(5-md,Z+1,X+1)) {
531 AliInfo(Form("Cell mod=%d col=%d row=%d is bad. Histogram %s rejected.",
532 5-md,Z+1,X+1,hRef->GetName()));
542 if(!ok && counter > nkeys){
543 Log("No histogram with enough statistics for reference. Exit.");
544 return 1; // Not fatal, just wait..
548 Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
549 hRef->GetName(),hRef->GetEntries(),
550 hRef->GetMean(),hRef->GetRMS()));
552 Double_t refMean=hRef->GetMean();
554 // Calculates relative calibration coefficients for all non-zero channels
556 for(Int_t mod=0; mod<nMod; mod++) {
557 for(Int_t col=0; col<nCol; col++) {
558 for(Int_t row=0; row<nRow; row++) {
560 sprintf(hnam,"%d_%d_%d_1",mod,row,col); // high gain!
561 h2 = (TH2F*)f.Get(hnam);
563 //TODO: dead channels exclusion!
565 h1 = h2->ProjectionX();
566 h1->GetXaxis()->SetRangeUser(10.,1000.); //to cut off saturation peak and noise
567 coeff = h1->GetMean()/refMean;
568 if(coeff>0 && h1->GetEntries()>minEntries) {
569 calibData.SetADCchannelEmc(5-mod,col+1,row+1,0.005/coeff);
570 AliInfo(Form("mod %d col %d row %d coeff %f\n",mod,col,row,coeff));