Fixes for reading zero-suppressed data. These should be propagated to
[u/mrichter/AliRoot.git] / FMD / AliFMDPedestalDA.cxx
CommitLineData
3bd993ba 1/**************************************************************************
2 * Copyright(c) 2004, 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/** @file AliFMDPedestalDA.cxx
17 @author Hans Hjersing Dalsgaard <canute@nbi.dk>
18 @date Mon Mar 10 09:46:05 2008
19 @brief Derived class for the pedestal detector algorithm.
20*/
21//
e9c06036 22// This class implements the virtual functions of the AliFMDBaseDA
23// class. The most important of these functions, FillChannels(..) and
24// Analyse(...) collect and analyse the data of each channel. The
25// resulting pedestal and noise values are written to a comma
26// separated values (csv) file on the go. The csv files produced in
27// this way are the basic input to the AliFMDPreprocessor.
3bd993ba 28//
29
30#include "AliFMDPedestalDA.h"
5cf05dbb 31#include "AliFMDAltroMapping.h"
3bd993ba 32#include "iostream"
33#include "fstream"
34#include "AliLog.h"
35#include "TF1.h"
f7f0b643 36#include "TObject.h"
93519ec4 37#include "TMath.h"
3bd993ba 38
39//_____________________________________________________________________
40ClassImp(AliFMDPedestalDA)
41
42//_____________________________________________________________________
f7f0b643 43AliFMDPedestalDA::AliFMDPedestalDA() : AliFMDBaseDA(),
f14ede67 44 fCurrentChannel(1),
f7f0b643 45 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
7bce699b 46 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
47 fZSfileFMD1(),
48 fZSfileFMD2(),
5cf05dbb 49 fZSfileFMD3(),
50 fMinTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
51 fMaxTimebin(3 * 4 * 3 * 16) // 3 ddls, 4 FECs, 3 Altros, 16 channels
3bd993ba 52{
5cf05dbb 53 // Default constructor
3bd993ba 54 fOutputFile.open("peds.csv");
7bce699b 55 fZSfileFMD1.open("ddl3072.csv");
56 fZSfileFMD2.open("ddl3073.csv");
57 fZSfileFMD3.open("ddl3074.csv");
3bd993ba 58}
59
60//_____________________________________________________________________
61AliFMDPedestalDA::AliFMDPedestalDA(const AliFMDPedestalDA & pedDA) :
f7f0b643 62 AliFMDBaseDA(pedDA),
f14ede67 63 fCurrentChannel(1),
f7f0b643 64 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
7bce699b 65 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
66 fZSfileFMD1(),
67 fZSfileFMD2(),
5cf05dbb 68 fZSfileFMD3(),
69 fMinTimebin(pedDA.fMinTimebin),
70 fMaxTimebin(pedDA.fMaxTimebin)
3bd993ba 71{
5cf05dbb 72 // Copy constructor
3bd993ba 73}
74
75//_____________________________________________________________________
e9c06036 76AliFMDPedestalDA::~AliFMDPedestalDA()
77{
5cf05dbb 78 // Destructor.
3bd993ba 79}
80
81//_____________________________________________________________________
e9c06036 82void AliFMDPedestalDA::Init()
83{
5cf05dbb 84 // Initialise
3bd993ba 85 SetRequiredEvents(1000);
5cf05dbb 86 fMinTimebin.Reset(1024);
87 fMaxTimebin.Reset(-1);
3bd993ba 88}
89
90//_____________________________________________________________________
91void AliFMDPedestalDA::AddChannelContainer(TObjArray* sectorArray,
92 UShort_t det,
e9c06036 93 Char_t ring,
3bd993ba 94 UShort_t sec,
e9c06036 95 UShort_t strip)
96{
5cf05dbb 97 // Add a channel to the containers.
98 //
99 // Parameters:
100 // sectorArray Array of sectors
101 // det Detector
102 // ring Ring
103 // sec Sector
104 // strip Strip
e9c06036 105 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 106 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
e9c06036 107 TObjArray* sampleArray = new TObjArray(samples);
7bce699b 108 sampleArray->SetOwner();
109 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 110 TH1S* hSample = new TH1S(Form("FMD%d%c[%02d,03%d]_%d",
111 det,ring,sec,strip,sample),
112 Form("FMD%d%c[%02d,%03%d]_%d",
113 det,ring,sec,strip),
114 1024,-.5,1023.5);
115 hSample->SetXTitle("ADC");
116 hSample->SetYTitle("Events");
7bce699b 117 hSample->SetDirectory(0);
e9c06036 118 sampleArray->AddAt(hSample, sample);
119 }
120 sectorArray->AddAtAndExpand(sampleArray, strip);
3bd993ba 121}
122
123//_____________________________________________________________________
e9c06036 124void AliFMDPedestalDA::FillChannels(AliFMDDigit* digit)
125{
5cf05dbb 126 // Fill ADC values from a digit into the corresponding histogram.
127 //
128 // Parameters:
129 // digit Digit to fill ADC values for.
3bd993ba 130 UShort_t det = digit->Detector();
131 Char_t ring = digit->Ring();
132 UShort_t sec = digit->Sector();
133 UShort_t strip = digit->Strip();
15e37b0a 134
e9c06036 135 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 136 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 137 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 138 TH1S* hSample = GetChannel(det, ring, sec, strip, sample);
139 hSample->Fill(digit->Count(sample));
140 }
7bce699b 141
3bd993ba 142}
143
144//_____________________________________________________________________
145void AliFMDPedestalDA::Analyse(UShort_t det,
e9c06036 146 Char_t ring,
3bd993ba 147 UShort_t sec,
5cf05dbb 148 UShort_t strip)
149{
150 // Analyse a strip. That is, compute the mean and spread of the ADC
151 // spectra for all strips. Also output on files the values.
152 //
153 // Parameters:
154 // det Detector
155 // ring Ring
156 // sec Sector
157 // strip Strip.
7bce699b 158 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 159 Float_t factor = pars->GetPedestalFactor();
160 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 161 for (UShort_t sample = 0; sample < samples; sample++) {
3bd993ba 162
5cf05dbb 163 TH1S* hChannel = GetChannel(det, ring, sec, strip,sample);
7bce699b 164 if(hChannel->GetEntries() == 0) {
165 //AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
166 // det,ring,sec,strip));
167 return;
e9c06036 168 }
f7f0b643 169
5cf05dbb 170 AliDebug(50, Form("Fitting FMD%d%c_%d_%d with %d entries",
171 det,ring,sec,strip, hChannel->GetEntries()));
7bce699b 172 TF1 fitFunc("fitFunc","gausn",0,300);
173 fitFunc.SetParameters(100,100,1);
174 hChannel->Fit("fitFunc","Q0+","",10,200);
175
176 Float_t mean = hChannel->GetMean();
177 Float_t rms = hChannel->GetRMS();
178
f7f0b643 179
f7f0b643 180
7bce699b 181 hChannel->GetXaxis()->SetRangeUser(mean-5*rms,mean+5*rms);
f7f0b643 182
7bce699b 183 mean = hChannel->GetMean();
184 rms = hChannel->GetRMS();
93519ec4 185
7bce699b 186
187 UShort_t ddl, board, altro, channel;
188 UShort_t timebin;
189
5cf05dbb 190 pars->Detector2Hardware(det,ring,sec,strip,sample,
191 ddl,board,altro,channel,timebin);
192 Int_t idx = HWIndex(ddl, board, altro, channel);
193 if (idx >= 0) {
194 fMinTimebin[idx] = TMath::Min(Short_t(timebin), fMinTimebin[idx]);
195 fMaxTimebin[idx] = TMath::Max(Short_t(timebin+1), fMaxTimebin[idx]);
196 }
7bce699b 197
5cf05dbb 198 std::ostream* zsFile = 0;
7bce699b 199 switch(det) {
5cf05dbb 200 case 1: zsFile = &fZSfileFMD1; break;
201 case 2: zsFile = &fZSfileFMD2; break;
202 case 3: zsFile = &fZSfileFMD3; break;
203 default: AliWarning("Unknown sample!"); break;
7bce699b 204
205 }
5cf05dbb 206 *zsFile << board << ','
207 << altro << ','
208 << channel << ','
209 << timebin << ','
210 << mean << ','
211 << rms << ','
212 << factor << "\n"; break;
7bce699b 213
214 Float_t chi2ndf = 0;
15e37b0a 215
216
7bce699b 217 if(fitFunc.GetNDF())
218 chi2ndf = fitFunc.GetChisquare() / fitFunc.GetNDF();
219
5cf05dbb 220
15e37b0a 221 Int_t sampleToWrite = 2;
5cf05dbb 222 if (samples == 2) sampleToWrite = 1;
223 else if (samples < 2) sampleToWrite = 0;
15e37b0a 224
5cf05dbb 225 if(sample != sampleToWrite) continue;
15e37b0a 226
15e37b0a 227
5cf05dbb 228 fOutputFile << det << ','
229 << ring << ','
230 << sec << ','
231 << strip << ','
232 << mean << ','
233 << rms << ','
234 << fitFunc.GetParameter(1) << ','
235 << fitFunc.GetParameter(2) << ','
236 << chi2ndf <<"\n";
7bce699b 237
5cf05dbb 238 if(fSaveHistograms ) {
239 gDirectory->cd(GetSectorPath(det, ring, sec, kTRUE));
240 TH1F* sumPed = dynamic_cast<TH1F*>(gDirectory->Get("Pedestals"));
241 TH1F* sumNoise = dynamic_cast<TH1F*>(gDirectory->Get("Noise"));
242 Int_t nStr = (ring == 'I' ? 512 : 256);
243 if (!sumPed) {
244 sumPed = new TH1F("Pedestals",
245 Form("Summary of pedestals in FMD%d%c[%02d]",
246 det, ring, sec),
247 nStr, -.5, nStr-.5);
248 sumPed->SetXTitle("Strip");
249 sumPed->SetYTitle("Pedestal [ADC]");
250 sumPed->SetDirectory(gDirectory);
251 }
252 if (!sumNoise) {
253 sumNoise = new TH1F("Noise",
254 Form("Summary of noise in FMD%d%c[%02d]",
7bce699b 255 det, ring, sec),
256 nStr, -.5, nStr-.5);
5cf05dbb 257 sumNoise->SetXTitle("Strip");
258 sumNoise->SetYTitle("Noise [ADC]");
7bce699b 259
5cf05dbb 260 sumNoise->SetDirectory(gDirectory);
7bce699b 261 }
5cf05dbb 262 sumPed->SetBinContent(strip+1, mean);
263 sumPed->SetBinError(strip+1, rms);
264 sumNoise->SetBinContent(strip+1, rms);
265
266 if(sumNoise->GetEntries() == nStr)
267 sumNoise->Write(sumNoise->GetName(),TObject::kOverwrite);
268 if(sumPed->GetEntries() == nStr)
269 sumPed->Write(sumPed->GetName(),TObject::kOverwrite);
270
271 fPedSummary.SetBinContent(fCurrentChannel,mean);
272
273 fNoiseSummary.SetBinContent(fCurrentChannel,rms);
274 fCurrentChannel++;
275
276 gDirectory->cd(GetStripPath(det, ring, sec, strip, kTRUE));
277 hChannel->GetXaxis()->SetRange(1,1024);
278
279 hChannel->Write();
7bce699b 280 }
281 }
3bd993ba 282}
283
284//_____________________________________________________________________
f7f0b643 285void AliFMDPedestalDA::Terminate(TFile* diagFile)
286{
5cf05dbb 287 // Called at the end of a job. Fills in missing time-bins and
288 // closes output files
7bce699b 289 if(fSaveHistograms) {
290 diagFile->cd();
291
292 fPedSummary.Write();
293 fNoiseSummary.Write();
294 }
5cf05dbb 295 AliFMDAltroMapping* map = AliFMDParameters::Instance()->GetAltroMap();
296 if(fZSfileFMD1.is_open()) FillinTimebins(fZSfileFMD1, map->Detector2DDL(1));
297 if(fZSfileFMD2.is_open()) FillinTimebins(fZSfileFMD2, map->Detector2DDL(2));
298 if(fZSfileFMD3.is_open()) FillinTimebins(fZSfileFMD3, map->Detector2DDL(3));
f7f0b643 299
5cf05dbb 300}
301
302//_____________________________________________________________________
303void AliFMDPedestalDA::FillinTimebins(std::ofstream& out, UShort_t ddl)
304{
305 unsigned short boards[] = { 0x0, 0x1, 0x10, 0x11, 0xFFFF };
306 unsigned short* board = boards;
307 while ((*boards) != 0xFFFF) {
308 for (UShort_t altro = 0; altro < 3; altro++) {
309 for (UShort_t channel = 0; channel < 16; channel++) {
310 Int_t idx = HWIndex(ddl, *board, altro, channel);
311 if (idx < 0) {
312 AliWarning(Form("Invalid index for %4d/0x%02x/0x%x/0x%x: %d",
313 ddl, *board, altro, channel, idx));
314 continue;
315 }
316 Short_t min = fMinTimebin[idx];
317 Short_t max = fMaxTimebin[idx];
318
319 // Channel not seen at all.
320 if (min > 1023 || max < 0) continue;
321
322 out << "# Extra timebins for 0x" << std::hex
323 << board << ',' << altro << ',' << channel
324 << " got time-bins " << min << " to " << max-1
325 << std::dec << std::endl;
326
327 for (UShort_t t = 15; t < min; t++)
328 // Write a phony line
329 out << board << "," << altro << "," << channel << ","
330 << t << "," << 1023 << "," << 0 << std::endl;
331
332 for (UShort_t t = max; t < 1024; t++)
333 // Write a phony line
334 out << board << "," << altro << "," << channel << ","
335 << t << "," << 1023 << "," << 0 << std::endl;
336 } // channel loop
337 } // altro loop
338 } // board loop
339 // Write trailer, and close
340 out.write("# EOF\n", 6);
341 out.close();
f7f0b643 342}
343
344//_____________________________________________________________________
e9c06036 345void AliFMDPedestalDA::WriteHeaderToFile()
346{
5cf05dbb 347 // Write headers to output files
80fdb9f3 348 AliFMDParameters* pars = AliFMDParameters::Instance();
349 fOutputFile.write(Form("# %s \n",pars->GetPedestalShuttleID()),13);
f7f0b643 350 fOutputFile.write("# Detector, "
351 "Ring, "
352 "Sector, "
e9c06036 353 "Strip, "
e9c06036 354 "Pedestal, "
355 "Noise, "
356 "Mu, "
357 "Sigma, "
f7f0b643 358 "Chi2/NDF \n", 71);
5cf05dbb 359
360 std::ostream* zss[] = { &fZSfileFMD1, &fZSfileFMD2, &fZSfileFMD3, 0 };
361 for (size_t i = 0; i < 3; i++)
362 *(zss[i]) << "# FMD 1 pedestals \n"
363 << "# board, "
364 << "altro, "
365 << "channel, "
366 << "timebin, "
367 << "pedestal, "
368 << "noise, "
369 << "factor\n";
3bd993ba 370}
371
372//_____________________________________________________________________
e9c06036 373TH1S* AliFMDPedestalDA::GetChannel(UShort_t det,
374 Char_t ring,
375 UShort_t sec,
7bce699b 376 UShort_t strip,
5cf05dbb 377 UInt_t sample)
e9c06036 378{
5cf05dbb 379 // Get the histogram corresponding to a strip sample.
380 //
381 // Parameters:
382 // det Detector
383 // ring Ring
384 // sec Sector
385 // strip Strip
386 // sample Sample
387 //
388 // Return:
389 // ADC spectra of a strip.
390 UShort_t iring = (ring == 'O' ? 0 : 1);
391 TObjArray* detArray = static_cast<TObjArray*>(fDetectorArray.At(det));
392 TObjArray* ringArray = static_cast<TObjArray*>(detArray->At(iring));
393 TObjArray* secArray = static_cast<TObjArray*>(ringArray->At(sec));
394 TObjArray* sampleArray = static_cast<TObjArray*>(secArray->At(strip));
395 TH1S* hSample = static_cast<TH1S*>(sampleArray->At(sample));
e9c06036 396 return hSample;
7bce699b 397
3bd993ba 398}
93519ec4 399
3bd993ba 400//_____________________________________________________________________
401//
402//EOF
403//