Bug fix
[u/mrichter/AliRoot.git] / PHOS / AliPHOSPreprocessor.cxx
CommitLineData
1ab07e55 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 "AliPHOSEmcCalibData.h"
30#include "TFile.h"
31#include "TH1.h"
32#include "TMap.h"
33#include "TRandom.h"
f97eb136 34#include "TKey.h"
516e7ab3 35#include "TList.h"
36#include "TObjString.h"
3c0265c1 37#include "AliPHOSEmcBadChannelsMap.h"
1ab07e55 38
39ClassImp(AliPHOSPreprocessor)
40
41//_______________________________________________________________________________________
42AliPHOSPreprocessor::AliPHOSPreprocessor() :
81587b1e 43AliPreprocessor("PHS",0)
1ab07e55 44{
45 //default constructor
46}
47
48//_______________________________________________________________________________________
81587b1e 49AliPHOSPreprocessor::AliPHOSPreprocessor(AliShuttleInterface* shuttle):
50AliPreprocessor("PHS",shuttle)
1ab07e55 51{
52 // Constructor
53}
54
55//_______________________________________________________________________________________
56UInt_t AliPHOSPreprocessor::Process(TMap* /*valueSet*/)
57{
58 // process data retrieved by the Shuttle
59
60 // The fileName with the histograms which have been produced by
61 // AliPHOSCalibHistoProducer.
62 // It is a responsibility of the SHUTTLE framework to form the fileName
516e7ab3 63
3c0265c1 64 TString runType = GetRunType();
65 Log(Form("Run type: %s",runType.Data()));
66
eba66a50 67 if(runType=="LED") {
68 Bool_t ledOK = ProcessLEDRun();
69 if(ledOK) return 0;
70 else
71 return 1;
72 }
73
c1274f4c 74 gRandom->SetSeed(0); //the seed is set to the current machine clock!
516e7ab3 75 AliPHOSEmcCalibData calibData;
3c0265c1 76
516e7ab3 77 TList* list = GetFileSources(kDAQ, "AMPLITUDES");
78 if(!list) {
79 Log("Sources list not found, exit.");
eba66a50 80 return 1;
1ab07e55 81 }
82
516e7ab3 83 TIter iter(list);
84 TObjString *source;
3c0265c1 85
516e7ab3 86 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
87 AliInfo(Form("found source %s", source->String().Data()));
f97eb136 88
516e7ab3 89 TString fileName = GetFile(kDAQ, "AMPLITUDES", source->GetName());
90 AliInfo(Form("Got filename: %s",fileName.Data()));
f97eb136 91
516e7ab3 92 TFile f(fileName);
f97eb136 93
516e7ab3 94 if(!f.IsOpen()) {
95 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
eba66a50 96 return 1;
516e7ab3 97 }
1ab07e55 98
516e7ab3 99 const Int_t nMod=5; // 1:5 modules
100 const Int_t nCol=56; //1:56 columns in each module
101 const Int_t nRow=64; //1:64 rows in each module
102
103 Double_t coeff;
104 char hnam[80];
105 TH1F* histo=0;
3c0265c1 106
516e7ab3 107 //Get the reference histogram
108 //(author: Gustavo Conesa Balbastre)
109
110 TList * keylist = f.GetListOfKeys();
111 Int_t nkeys = f.GetNkeys();
112 Bool_t ok = kFALSE;
113 TKey *key;
114 TString refHistoName= "";
115 Int_t ikey = 0;
116 Int_t counter = 0;
117 TH1F* hRef = 0;
eba66a50 118
516e7ab3 119 //Check if the file contains any histogram
120
121 if(nkeys< 2){
122 Log(Form("Not enough histograms (%d) for calibration.",nkeys));
4988b8ce 123 return 1;
f97eb136 124 }
f97eb136 125
516e7ab3 126 while(!ok){
127 ikey = gRandom->Integer(nkeys);
128 key = (TKey*)keylist->At(ikey);
129 refHistoName = key->GetName();
130 hRef = (TH1F*)f.Get(refHistoName);
131 counter++;
132 // Check if the reference histogram has too little statistics
133 if(hRef->GetEntries()>2) ok=kTRUE;
134 if(!ok && counter >= nMod*nCol*nRow){
135 Log("No histogram with enough statistics for reference.");
136 return 1;
137 }
138 }
3c0265c1 139
516e7ab3 140 Log(Form("reference histogram %s, %.1f entries, mean=%.3f, rms=%.3f.",
141 hRef->GetName(),hRef->GetEntries(),
142 hRef->GetMean(),hRef->GetRMS()));
143
144 Double_t refMean=hRef->GetMean();
3c0265c1 145
516e7ab3 146 // Calculates relative calibration coefficients for all non-zero channels
3c0265c1 147
516e7ab3 148 for(Int_t mod=0; mod<nMod; mod++) {
149 for(Int_t col=0; col<nCol; col++) {
150 for(Int_t row=0; row<nRow; row++) {
151 sprintf(hnam,"mod%dcol%drow%d",mod,col,row);
152 histo = (TH1F*)f.Get(hnam);
153 //TODO: dead channels exclusion!
154 if(histo) {
155 coeff = histo->GetMean()/refMean;
64d8f3f1 156 if(coeff>0)
157 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.001/coeff);
158 else
159 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.001);
516e7ab3 160 AliInfo(Form("mod %d col %d row %d coeff %f\n",mod,col,row,coeff));
161 }
162 else
163 calibData.SetADCchannelEmc(mod+1,col+1,row+1,0.001);
1ab07e55 164 }
1ab07e55 165 }
166 }
516e7ab3 167
168 f.Close();
1ab07e55 169 }
516e7ab3 170
3c0265c1 171 //Store EMC calibration data
172
173 AliCDBMetaData emcMetaData;
eba66a50 174 Bool_t emcOK = Store("Calib", "EmcGainPedestals", &calibData, &emcMetaData);
175
176 if(emcOK) return 0;
177 else
178 return 1;
179
180}
181
182
183Bool_t AliPHOSPreprocessor::ProcessLEDRun()
184{
185 //Process LED run, fill bad channels map.
186
187 AliPHOSEmcBadChannelsMap badMap;
188
189 TList* list = GetFileSources(kDAQ, "LED");
190 if(!list) {
191 Log("Sources list for LED run not found, exit.");
192 return kFALSE;
193 }
194
195 TIter iter(list);
196 TObjString *source;
197 char hnam[80];
198 TH1F* histo=0;
199
200 while ((source = dynamic_cast<TObjString *> (iter.Next()))) {
201
202 AliInfo(Form("found source %s", source->String().Data()));
203
204 TString fileName = GetFile(kDAQ, "LED", source->GetName());
205 AliInfo(Form("Got filename: %s",fileName.Data()));
206
207 TFile f(fileName);
208
209 if(!f.IsOpen()) {
210 Log(Form("File %s is not opened, something goes wrong!",fileName.Data()));
211 return kFALSE;
212 }
213
214 const Int_t nMod=5; // 1:5 modules
215 const Int_t nCol=56; //1:56 columns in each module
216 const Int_t nRow=64; //1:64 rows in each module
217
218 // Check for dead channels
219 Log(Form("Begin check for dead channels."));
220
221 for(Int_t mod=0; mod<nMod; mod++) {
222 for(Int_t col=0; col<nCol; col++) {
223 for(Int_t row=0; row<nRow; row++) {
224 sprintf(hnam,"mod%dcol%drow%d",mod,col,row);
225 histo = (TH1F*)f.Get(hnam);
226 if(histo)
227 if (histo->GetMean()<1) {
228 Log(Form("Channel: [%d,%d,%d] seems dead, <E>=%.1f.",mod,col,row,histo->GetMean()));
229 badMap.SetBadChannel(mod,col,row);
230 }
231 }
232 }
233 }
234
235 }
236
237 //Store bad channels map
238 AliCDBMetaData badMapMetaData;
239
240 //Bad channels data valid from current run fRun until updated (validityInfinite=kTRUE)
241 Bool_t result = Store("Calib", "EmcBadChannels", &badMap, &badMapMetaData, fRun, kTRUE);
1ab07e55 242 return result;
243
244}