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();
133 Bool_t firedOK = kFALSE;
135 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
137 AliInfo(Form("found source %s", source->String().Data()));
139 TString fileName = GetFile(kDAQ, "LED", source->GetName());
140 AliInfo(Form("Got filename: %s",fileName.Data()));
145 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
149 TH1I* fFiredCells = (TH1I*)f.Get("fFiredCells");
152 Log(Form("fFiredCells histogram not found in %s, skip this source.",fileName.Data()));
156 const Double_t nFiredCells = fFiredCells->GetMean();
157 if(nFiredCells>100 && nFiredCells<600) {
159 Log(Form("Number of fired cells per event is %d.",nFiredCells));
162 Log(Form("Number of fired cells per event is %d, possibly not a LED run!",nFiredCells));
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
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++) {
176 //High Gain to Low Gain ratio
178 Float_t ratio = HG2LG(mod,row,col,&f);
179 calibData.SetHighLowRatioEmc(mod+1,col+1,row+1,ratio);
181 AliInfo(Form("mod %d iX %d iZ %d ratio %.3f\n",mod,row,col,ratio));
185 Double_t coeff = clb->GetADCchannelEmc(mod+1,col+1,row+1);
186 calibData.SetADCchannelEmc(mod+1,col+1,row+1,coeff);
189 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.005);
195 } // end of loop over files
197 //Store the updated High Gain/Low Gain ratios
198 AliCDBMetaData emcMetaData;
200 //Data valid from current run fRun until updated (validityInfinite=kTRUE)
201 Bool_t result = Store("Calib","EmcGainPedestals",&calibData,&emcMetaData,fRun,kTRUE);
206 Float_t AliPHOSPreprocessor::HG2LG(Int_t mod, Int_t X, Int_t Z, TFile* f)
208 //Calculates High gain to Low gain ratio
209 //for crystal at the position (X,Z) in the PHOS module mod.
212 sprintf(hname,"%d_%d_%d",mod,X,Z);
214 TH1F* h1 = (TH1F*)f->Get(hname);
217 if(!h1->GetEntries()) return 16.;
220 if(h1->GetMaximum()<10.) return 16.;
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());
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 h1->Fit(gaus1,"LERQ+");
236 AliInfo(Form("%s: %d entries, mean=%.3f, peak=%.3f, rms= %.3f. HG/LG = %.3f\n",
237 h1->GetTitle(),h1->GetEntries(),h1->GetMean(),max,h1->GetRMS(),
238 gaus1->GetParameter("Mean")));
240 return gaus1->GetParameter("Mean");
244 Bool_t AliPHOSPreprocessor::FindBadChannelsEmc()
246 //Loop over two systems: DAQ and HLT.
247 //For each system the same algorithm implemented in DoFindBadChannelsEmc() invokes.
252 Int_t system[2] = { kDAQ, kHLT };
253 const char* sysn[] = { "DAQ","HLT" };
254 Bool_t result[2] = { kTRUE, kTRUE };
256 for (Int_t i=0; i<2; i++) {
258 AliPHOSEmcBadChannelsMap badMap;
259 list = GetFileSources(system[i], "BAD_CHANNELS");
262 Log(Form("%s sources list for BAD_CHANNELS not found!",sysn[i]));
267 if(!list->GetEntries()) {
268 Log(Form("Got empty sources list. It seems %s DA2 did not produce any files!",sysn[i]));
273 result[i] *= DoFindBadChannelsEmc(system[i],list,badMap);
275 // Store the bad channels map.
278 md.SetResponsible("Boris Polishchuk");
280 if(system[i] == kDAQ)
285 // Data valid from current run fRun until being updated (validityInfinite=kTRUE)
286 result[i] *= Store(path.Data(), "EmcBadChannels", &badMap, &md, fRun, kTRUE);
290 if(result[0] || result[1]) return kTRUE;
294 Bool_t AliPHOSPreprocessor::DoFindBadChannelsEmc(Int_t system, TList* list, AliPHOSEmcBadChannelsMap& badMap)
296 //Creates the bad channels map for PHOS EMC.
298 // The file fileName contains histograms which have been produced by DA2 detector algorithm.
299 // It is a responsibility of the SHUTTLE framework to form the fileName.
306 const Float_t fQualityCut = 1.;
307 Int_t nGoods[5] = {0,0,0,0,0};
309 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
311 AliInfo(Form("found source %s", source->String().Data()));
313 TString fileName = GetFile(system, "BAD_CHANNELS", source->GetName());
314 AliInfo(Form("Got filename: %s",fileName.Data()));
319 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
323 TH1I* fFiredCells = (TH1I*)f.Get("fFiredCells");
326 Log(Form("fFiredCells histogram not found in %s, skip this source.",fileName.Data()));
330 const Double_t nFiredCells = fFiredCells->GetMean();
332 if(nFiredCells<100. || nFiredCells>600. ) {
333 Log(Form("Number of fired cells per event is %d, possibly not a LED run!",nFiredCells));
334 continue; // not a LED run!
337 Log(Form("Number of fired cells per event %d. Begin check for bad channels.",nFiredCells));
339 for(Int_t mod=0; mod<5; mod++) {
340 for(Int_t iX=0; iX<64; iX++) {
341 for(Int_t iZ=0; iZ<56; iZ++) {
343 sprintf(hnam,"%d_%d_%d_%d",mod,iX,iZ,1); // high gain
344 h1 = (TH1F*)f.Get(hnam);
347 Double_t mean = h1->GetMean();
350 Log(Form("iX=%d iZ=%d gain=%d mean=%.3f\n",iX,iZ,1,mean));
352 if( mean>0 && mean<fQualityCut ) {
356 badMap.SetBadChannel(mod+1,iZ+1,iX+1); //module, col,row
363 Log(Form("Module %d: %d good channels.",mod,nGoods[mod]));
367 } // end of loop over sources
372 Bool_t AliPHOSPreprocessor::CalibrateEmc()
374 //Loop over two systems: DAQ and HLT.
375 //For each system the same algorithm implemented in DoCalibrateEmc() invokes.
377 const AliPHOSEmcBadChannelsMap* badMap=0;
378 AliCDBEntry* entryBCM=0;
382 Int_t system[2] = { kDAQ, kHLT };
383 const char* sysn[] = { "DAQ","HLT" };
384 Bool_t result[2] = { kTRUE, kTRUE };
386 for (Int_t i=0; i<2; i++) {
388 AliPHOSEmcCalibData calibData;
389 list = GetFileSources(system[i], "AMPLITUDES");
392 Log(Form("%s sources list not found!",sysn[i]));
397 if(!list->GetEntries()) {
398 Log(Form("Got empty sources list. It seems %s DA1 did not produce any files!",sysn[i]));
403 // Retrieve the Bad Channels Map (BCM)
405 if(system[i] == kDAQ)
410 entryBCM = GetFromOCDB(path.Data(), "EmcBadChannels");
413 Log(Form("WARNING!! Cannot find any AliCDBEntry for [%s, EmcBadChannels]!",path.Data()));
415 badMap = (AliPHOSEmcBadChannelsMap*)entryBCM->GetObject();
418 Log(Form("WARNING!! Nothing for %s in AliCDBEntry. All cells considered GOOD!",sysn[i]));
420 result[i] *= DoCalibrateEmc(system[i],list,badMap,calibData);
422 //Store EMC calibration data
424 AliCDBMetaData emcMetaData;
425 result[i] *= Store(path.Data(), "EmcGainPedestals", &calibData, &emcMetaData, 0, kTRUE);
429 if(result[0] || result[1]) return kTRUE;
434 Bool_t AliPHOSPreprocessor::DoCalibrateEmc(Int_t system, TList* list, const AliPHOSEmcBadChannelsMap* badMap, AliPHOSEmcCalibData& calibData)
436 // Return kTRUE if OK.
437 // I. Calculates the set of calibration coefficients to equalyze the mean energies deposited at high gain.
438 // II. Extracts High_Gain/Low_Gain ratio for each channel.
440 // The file fileName contains histograms which have been produced by DA1 detector algorithm.
441 // It is a responsibility of the SHUTTLE framework to form the fileName.
443 gRandom->SetSeed(0); //the seed is set to the current machine clock!
448 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
449 AliInfo(Form("found source %s", source->String().Data()));
451 TString fileName = GetFile(system, "AMPLITUDES", source->GetName());
452 AliInfo(Form("Got filename: %s",fileName.Data()));
457 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
461 const Int_t nMod=5; // 1:5 modules
462 const Int_t nCol=56; //1:56 columns in each module
463 const Int_t nRow=64; //1:64 rows in each module
470 //Get the reference histogram
471 //(author: Gustavo Conesa Balbastre)
473 TList * keylist = f.GetListOfKeys();
474 Int_t nkeys = f.GetNkeys();
481 //Check if the file contains any histogram
484 Log(Form("Not enough histograms (%d) for calibration.",nkeys));
485 return 1; // it's not fatal! May be short run..
489 ikey = gRandom->Integer(nkeys);
490 key = (TKey*)keylist->At(ikey);
491 TObject* obj = f.Get(key->GetName());
492 TString cname(obj->ClassName());
493 if(cname == "TH2F") {
495 TString htitl = h2->GetTitle();
496 if(htitl.Contains("and gain 1")) {
497 hRef = h2->ProjectionX();
498 hRef->GetXaxis()->SetRange(10,1000); // to cut off saturation peak and noise
499 // Check if the reference histogram has too little statistics
500 if(hRef->GetMean() && hRef->GetEntries()>2) ok=kTRUE;
502 const TString delim = "_";
503 TString str = hRef->GetName();
504 TObjArray* tks = str.Tokenize(delim);
505 const Int_t md = ((TObjString*)tks->At(0))->GetString().Atoi();
506 const Int_t X = ((TObjString*)tks->At(1))->GetString().Atoi();
507 const Int_t Z = ((TObjString*)tks->At(2))->GetString().Atoi();
510 if(badMap->IsBadChannel(md+1,Z+1,X+1)) {
511 AliInfo(Form("Cell mod=%d col=%d row=%d is bad. Histogram %s rejected.",
512 md+1,Z+1,X+1,hRef->GetName()));
522 if(!ok && counter > nkeys){
523 Log("No histogram with enough statistics for reference. Exit.");
524 return 1; // Not fatal, just wait..
528 Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
529 hRef->GetName(),hRef->GetEntries(),
530 hRef->GetMean(),hRef->GetRMS()));
532 Double_t refMean=hRef->GetMean();
534 // Calculates relative calibration coefficients for all non-zero channels
536 for(Int_t mod=0; mod<nMod; mod++) {
537 for(Int_t col=0; col<nCol; col++) {
538 for(Int_t row=0; row<nRow; row++) {
540 sprintf(hnam,"%d_%d_%d_1",mod,row,col); // high gain!
541 h2 = (TH2F*)f.Get(hnam);
543 //TODO: dead channels exclusion!
545 h1 = h2->ProjectionX();
546 h1->GetXaxis()->SetRange(10,1000); //to cut off saturation peak and noise
547 coeff = h1->GetMean()/refMean;
549 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.005/coeff);
551 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.005);
552 AliInfo(Form("mod %d col %d row %d coeff %f\n",mod,col,row,coeff));
555 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.005);