]> git.uio.no Git - u/mrichter/AliRoot.git/blame - FMD/AliFMDPedestalDA.cxx
general code audit, added documentation about AliESDtrack update sequence
[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"
408bf2b4 32#include <iostream>
33#include <fstream>
3bd993ba 34#include "AliLog.h"
35#include "TF1.h"
f7f0b643 36#include "TObject.h"
93519ec4 37#include "TMath.h"
408bf2b4 38#include <TSystem.h>
6cf6e7a0 39#include <TDatime.h>
2a082c96 40#include <TH2.h>
3bd993ba 41
42//_____________________________________________________________________
43ClassImp(AliFMDPedestalDA)
44
45//_____________________________________________________________________
2a082c96 46AliFMDPedestalDA::AliFMDPedestalDA()
47 : AliFMDBaseDA(),
48 fCurrentChannel(1),
49 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
50 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
51 fZSfileFMD1(),
52 fZSfileFMD2(),
53 fZSfileFMD3(),
54 fMinTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
55 fMaxTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
56 fSummaryFMD1i(0),
57 fSummaryFMD2i(0),
58 fSummaryFMD2o(0),
59 fSummaryFMD3i(0),
60 fSummaryFMD3o(0)
3bd993ba 61{
5cf05dbb 62 // Default constructor
6cf6e7a0 63 Rotate("peds.csv", 3);
3bd993ba 64 fOutputFile.open("peds.csv");
6cf6e7a0 65 Rotate("ddl3072.csv", 10);
7bce699b 66 fZSfileFMD1.open("ddl3072.csv");
6cf6e7a0 67 Rotate("ddl3073.csv", 10);
7bce699b 68 fZSfileFMD2.open("ddl3073.csv");
6cf6e7a0 69 Rotate("ddl3074.csv", 10);
7bce699b 70 fZSfileFMD3.open("ddl3074.csv");
3bd993ba 71}
72
73//_____________________________________________________________________
74AliFMDPedestalDA::AliFMDPedestalDA(const AliFMDPedestalDA & pedDA) :
f7f0b643 75 AliFMDBaseDA(pedDA),
f14ede67 76 fCurrentChannel(1),
f7f0b643 77 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
7bce699b 78 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
79 fZSfileFMD1(),
80 fZSfileFMD2(),
5cf05dbb 81 fZSfileFMD3(),
82 fMinTimebin(pedDA.fMinTimebin),
2a082c96 83 fMaxTimebin(pedDA.fMaxTimebin),
84 fSummaryFMD1i(pedDA.fSummaryFMD1i),
85 fSummaryFMD2i(pedDA.fSummaryFMD2i),
86 fSummaryFMD2o(pedDA.fSummaryFMD2o),
87 fSummaryFMD3i(pedDA.fSummaryFMD3i),
88 fSummaryFMD3o(pedDA.fSummaryFMD3o)
3bd993ba 89{
5cf05dbb 90 // Copy constructor
3bd993ba 91}
92
93//_____________________________________________________________________
e9c06036 94AliFMDPedestalDA::~AliFMDPedestalDA()
95{
5cf05dbb 96 // Destructor.
3bd993ba 97}
98
99//_____________________________________________________________________
e9c06036 100void AliFMDPedestalDA::Init()
101{
5cf05dbb 102 // Initialise
3bd993ba 103 SetRequiredEvents(1000);
5cf05dbb 104 fMinTimebin.Reset(1024);
105 fMaxTimebin.Reset(-1);
2a082c96 106
107
3bd993ba 108}
109
110//_____________________________________________________________________
111void AliFMDPedestalDA::AddChannelContainer(TObjArray* sectorArray,
112 UShort_t det,
e9c06036 113 Char_t ring,
3bd993ba 114 UShort_t sec,
e9c06036 115 UShort_t strip)
116{
5cf05dbb 117 // Add a channel to the containers.
118 //
119 // Parameters:
120 // sectorArray Array of sectors
121 // det Detector
122 // ring Ring
123 // sec Sector
124 // strip Strip
e9c06036 125 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 126 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
e9c06036 127 TObjArray* sampleArray = new TObjArray(samples);
7bce699b 128 sampleArray->SetOwner();
129 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 130 TH1S* hSample = new TH1S(Form("FMD%d%c[%02d,03%d]_%d",
131 det,ring,sec,strip,sample),
132 Form("FMD%d%c[%02d,%03%d]_%d",
133 det,ring,sec,strip),
134 1024,-.5,1023.5);
135 hSample->SetXTitle("ADC");
136 hSample->SetYTitle("Events");
7bce699b 137 hSample->SetDirectory(0);
e9c06036 138 sampleArray->AddAt(hSample, sample);
139 }
140 sectorArray->AddAtAndExpand(sampleArray, strip);
3bd993ba 141}
142
143//_____________________________________________________________________
e9c06036 144void AliFMDPedestalDA::FillChannels(AliFMDDigit* digit)
145{
5cf05dbb 146 // Fill ADC values from a digit into the corresponding histogram.
147 //
148 // Parameters:
149 // digit Digit to fill ADC values for.
3bd993ba 150 UShort_t det = digit->Detector();
151 Char_t ring = digit->Ring();
152 UShort_t sec = digit->Sector();
153 UShort_t strip = digit->Strip();
15e37b0a 154
e9c06036 155 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 156 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 157 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 158 TH1S* hSample = GetChannel(det, ring, sec, strip, sample);
159 hSample->Fill(digit->Count(sample));
160 }
7bce699b 161
3bd993ba 162}
163
2a082c96 164//_____________________________________________________________________
165void AliFMDPedestalDA::MakeSummary(UShort_t det, Char_t ring)
166{
8e23c82d 167 //Create summary hists for FMD pedestals
2a082c96 168 std::cout << "Making summary for FMD" << det << ring << " ..." << std::endl;
169 switch (det) {
170 case 1:
171 fSummaryFMD1i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
172 break;
173 case 2:
174 switch (ring) {
175 case 'I': case 'i':
176 fSummaryFMD2i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
177 break;
178 case 'O': case 'o':
179 fSummaryFMD2o = MakeSummaryHistogram("ped", "Pedestals", det, ring);
180 break;
181 }
182 break;
183 case 3:
184 switch (ring) {
185 case 'I': case 'i':
186 fSummaryFMD3i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
187 break;
188 case 'O': case 'o':
189 fSummaryFMD3o = MakeSummaryHistogram("ped", "Pedestals", det, ring);
190 break;
191 }
192 break;
193 }
194}
195
3bd993ba 196//_____________________________________________________________________
197void AliFMDPedestalDA::Analyse(UShort_t det,
e9c06036 198 Char_t ring,
3bd993ba 199 UShort_t sec,
5cf05dbb 200 UShort_t strip)
201{
202 // Analyse a strip. That is, compute the mean and spread of the ADC
203 // spectra for all strips. Also output on files the values.
204 //
205 // Parameters:
206 // det Detector
207 // ring Ring
208 // sec Sector
209 // strip Strip.
7bce699b 210 AliFMDParameters* pars = AliFMDParameters::Instance();
2a082c96 211 TH2* summary = 0;
212 switch (det) {
213 case 1: summary = fSummaryFMD1i; break;
214 case 2:
215 switch (ring) {
216 case 'I': summary = fSummaryFMD2i; break;
217 case 'O': summary = fSummaryFMD2o; break;
218 }
219 break;
220 case 3:
221 switch (ring) {
222 case 'I': summary = fSummaryFMD3i; break;
223 case 'O': summary = fSummaryFMD3o; break;
224 }
225 break;
226 }
227 static bool first = true;
228 if (summary && first) {
229 std::cout << "Filling summary " << summary->GetName() << std::endl;
230 first = false;
231 }
232
762e54b7 233 // Float_t factor = pars->GetPedestalFactor();
5cf05dbb 234 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 235 for (UShort_t sample = 0; sample < samples; sample++) {
3bd993ba 236
5cf05dbb 237 TH1S* hChannel = GetChannel(det, ring, sec, strip,sample);
7bce699b 238 if(hChannel->GetEntries() == 0) {
239 //AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
240 // det,ring,sec,strip));
241 return;
e9c06036 242 }
f7f0b643 243
5cf05dbb 244 AliDebug(50, Form("Fitting FMD%d%c_%d_%d with %d entries",
245 det,ring,sec,strip, hChannel->GetEntries()));
7bce699b 246 TF1 fitFunc("fitFunc","gausn",0,300);
247 fitFunc.SetParameters(100,100,1);
248 hChannel->Fit("fitFunc","Q0+","",10,200);
249
250 Float_t mean = hChannel->GetMean();
251 Float_t rms = hChannel->GetRMS();
252
f7f0b643 253
f7f0b643 254
7bce699b 255 hChannel->GetXaxis()->SetRangeUser(mean-5*rms,mean+5*rms);
f7f0b643 256
7bce699b 257 mean = hChannel->GetMean();
258 rms = hChannel->GetRMS();
93519ec4 259
7bce699b 260
261 UShort_t ddl, board, altro, channel;
262 UShort_t timebin;
263
5cf05dbb 264 pars->Detector2Hardware(det,ring,sec,strip,sample,
265 ddl,board,altro,channel,timebin);
266 Int_t idx = HWIndex(ddl, board, altro, channel);
267 if (idx >= 0) {
268 fMinTimebin[idx] = TMath::Min(Short_t(timebin), fMinTimebin[idx]);
269 fMaxTimebin[idx] = TMath::Max(Short_t(timebin+1), fMaxTimebin[idx]);
270 }
7bce699b 271
5cf05dbb 272 std::ostream* zsFile = 0;
7bce699b 273 switch(det) {
5cf05dbb 274 case 1: zsFile = &fZSfileFMD1; break;
275 case 2: zsFile = &fZSfileFMD2; break;
276 case 3: zsFile = &fZSfileFMD3; break;
277 default: AliWarning("Unknown sample!"); break;
7bce699b 278
279 }
5cf05dbb 280 *zsFile << board << ','
281 << altro << ','
282 << channel << ','
283 << timebin << ','
284 << mean << ','
276b1261 285 << rms << "\n";
7bce699b 286
287 Float_t chi2ndf = 0;
15e37b0a 288
289
7bce699b 290 if(fitFunc.GetNDF())
291 chi2ndf = fitFunc.GetChisquare() / fitFunc.GetNDF();
292
5cf05dbb 293
15e37b0a 294 Int_t sampleToWrite = 2;
5cf05dbb 295 if (samples == 2) sampleToWrite = 1;
296 else if (samples < 2) sampleToWrite = 0;
15e37b0a 297
5cf05dbb 298 if(sample != sampleToWrite) continue;
15e37b0a 299
15e37b0a 300
5cf05dbb 301 fOutputFile << det << ','
302 << ring << ','
303 << sec << ','
304 << strip << ','
305 << mean << ','
306 << rms << ','
307 << fitFunc.GetParameter(1) << ','
308 << fitFunc.GetParameter(2) << ','
309 << chi2ndf <<"\n";
2a082c96 310
311 if (summary) {
312 Int_t bin = summary->FindBin(sec, strip);
313 summary->SetBinContent(bin, mean);
314 summary->SetBinError(bin, rms);
315 }
316
5cf05dbb 317 if(fSaveHistograms ) {
318 gDirectory->cd(GetSectorPath(det, ring, sec, kTRUE));
319 TH1F* sumPed = dynamic_cast<TH1F*>(gDirectory->Get("Pedestals"));
320 TH1F* sumNoise = dynamic_cast<TH1F*>(gDirectory->Get("Noise"));
321 Int_t nStr = (ring == 'I' ? 512 : 256);
322 if (!sumPed) {
323 sumPed = new TH1F("Pedestals",
324 Form("Summary of pedestals in FMD%d%c[%02d]",
325 det, ring, sec),
326 nStr, -.5, nStr-.5);
327 sumPed->SetXTitle("Strip");
328 sumPed->SetYTitle("Pedestal [ADC]");
329 sumPed->SetDirectory(gDirectory);
330 }
331 if (!sumNoise) {
332 sumNoise = new TH1F("Noise",
333 Form("Summary of noise in FMD%d%c[%02d]",
7bce699b 334 det, ring, sec),
335 nStr, -.5, nStr-.5);
5cf05dbb 336 sumNoise->SetXTitle("Strip");
337 sumNoise->SetYTitle("Noise [ADC]");
7bce699b 338
5cf05dbb 339 sumNoise->SetDirectory(gDirectory);
7bce699b 340 }
5cf05dbb 341 sumPed->SetBinContent(strip+1, mean);
342 sumPed->SetBinError(strip+1, rms);
343 sumNoise->SetBinContent(strip+1, rms);
344
345 if(sumNoise->GetEntries() == nStr)
346 sumNoise->Write(sumNoise->GetName(),TObject::kOverwrite);
347 if(sumPed->GetEntries() == nStr)
348 sumPed->Write(sumPed->GetName(),TObject::kOverwrite);
349
350 fPedSummary.SetBinContent(fCurrentChannel,mean);
351
352 fNoiseSummary.SetBinContent(fCurrentChannel,rms);
353 fCurrentChannel++;
354
355 gDirectory->cd(GetStripPath(det, ring, sec, strip, kTRUE));
356 hChannel->GetXaxis()->SetRange(1,1024);
357
358 hChannel->Write();
7bce699b 359 }
360 }
3bd993ba 361}
362
f7f0b643 363//_____________________________________________________________________
364void AliFMDPedestalDA::Terminate(TFile* diagFile)
365{
5cf05dbb 366 // Called at the end of a job. Fills in missing time-bins and
367 // closes output files
7bce699b 368 if(fSaveHistograms) {
369 diagFile->cd();
370
371 fPedSummary.Write();
372 fNoiseSummary.Write();
373 }
5cf05dbb 374 AliFMDAltroMapping* map = AliFMDParameters::Instance()->GetAltroMap();
276b1261 375 for (Int_t i = 0; i < 3; i++) {
376 std::ofstream& out = (i == 0 ? fZSfileFMD1 :
377 i == 1 ? fZSfileFMD2 :
378 fZSfileFMD3);
379 if (out.is_open() && fSeenDetectors[i]) {
380 FillinTimebins(out, map->Detector2DDL(i+1));
381 }
408bf2b4 382 if (!fSeenDetectors[i]) {
383 TString n(Form("ddl%d.csv",3072+map->Detector2DDL(i+1)));
384 gSystem->Unlink(n.Data());
385 }
386 }
f7f0b643 387
5cf05dbb 388}
389
390//_____________________________________________________________________
7af3df7f 391void AliFMDPedestalDA::FillinTimebins(std::ofstream& out, UShort_t /*ddl*/)
5cf05dbb 392{
762e54b7 393#if 0
5cf05dbb 394 unsigned short boards[] = { 0x0, 0x1, 0x10, 0x11, 0xFFFF };
395 unsigned short* board = boards;
396 while ((*boards) != 0xFFFF) {
397 for (UShort_t altro = 0; altro < 3; altro++) {
398 for (UShort_t channel = 0; channel < 16; channel++) {
399 Int_t idx = HWIndex(ddl, *board, altro, channel);
400 if (idx < 0) {
401 AliWarning(Form("Invalid index for %4d/0x%02x/0x%x/0x%x: %d",
402 ddl, *board, altro, channel, idx));
403 continue;
404 }
405 Short_t min = fMinTimebin[idx];
406 Short_t max = fMaxTimebin[idx];
407
408 // Channel not seen at all.
409 if (min > 1023 || max < 0) continue;
410
411 out << "# Extra timebins for 0x" << std::hex
412 << board << ',' << altro << ',' << channel
413 << " got time-bins " << min << " to " << max-1
414 << std::dec << std::endl;
415
416 for (UShort_t t = 15; t < min; t++)
417 // Write a phony line
418 out << board << "," << altro << "," << channel << ","
419 << t << "," << 1023 << "," << 0 << std::endl;
420
421 for (UShort_t t = max; t < 1024; t++)
422 // Write a phony line
423 out << board << "," << altro << "," << channel << ","
424 << t << "," << 1023 << "," << 0 << std::endl;
425 } // channel loop
426 } // altro loop
427 } // board loop
428 // Write trailer, and close
762e54b7 429#endif
5cf05dbb 430 out.write("# EOF\n", 6);
431 out.close();
f7f0b643 432}
433
3bd993ba 434//_____________________________________________________________________
e9c06036 435void AliFMDPedestalDA::WriteHeaderToFile()
436{
5cf05dbb 437 // Write headers to output files
80fdb9f3 438 AliFMDParameters* pars = AliFMDParameters::Instance();
439 fOutputFile.write(Form("# %s \n",pars->GetPedestalShuttleID()),13);
6cf6e7a0 440 TDatime now;
441 fOutputFile << "# This file created from run # " << fRunno
442 << " @ " << now.AsString() << std::endl;
f7f0b643 443 fOutputFile.write("# Detector, "
444 "Ring, "
445 "Sector, "
e9c06036 446 "Strip, "
e9c06036 447 "Pedestal, "
448 "Noise, "
449 "Mu, "
450 "Sigma, "
f7f0b643 451 "Chi2/NDF \n", 71);
5cf05dbb 452
453 std::ostream* zss[] = { &fZSfileFMD1, &fZSfileFMD2, &fZSfileFMD3, 0 };
6cf6e7a0 454 for (size_t i = 0; i < 3; i++) {
455 *(zss[i]) << "# FMD " << (i+1) << " pedestals \n"
5cf05dbb 456 << "# board, "
457 << "altro, "
458 << "channel, "
459 << "timebin, "
460 << "pedestal, "
762e54b7 461 << "noise\n";
6cf6e7a0 462 *(zss[i]) << "# This file created from run # " << fRunno
463 << " @ " << now.AsString() << std::endl;
464 }
3bd993ba 465}
466
467//_____________________________________________________________________
e9c06036 468TH1S* AliFMDPedestalDA::GetChannel(UShort_t det,
469 Char_t ring,
470 UShort_t sec,
7bce699b 471 UShort_t strip,
5cf05dbb 472 UInt_t sample)
e9c06036 473{
5cf05dbb 474 // Get the histogram corresponding to a strip sample.
475 //
476 // Parameters:
477 // det Detector
478 // ring Ring
479 // sec Sector
480 // strip Strip
481 // sample Sample
482 //
483 // Return:
484 // ADC spectra of a strip.
485 UShort_t iring = (ring == 'O' ? 0 : 1);
486 TObjArray* detArray = static_cast<TObjArray*>(fDetectorArray.At(det));
487 TObjArray* ringArray = static_cast<TObjArray*>(detArray->At(iring));
488 TObjArray* secArray = static_cast<TObjArray*>(ringArray->At(sec));
489 TObjArray* sampleArray = static_cast<TObjArray*>(secArray->At(strip));
490 TH1S* hSample = static_cast<TH1S*>(sampleArray->At(sample));
e9c06036 491 return hSample;
7bce699b 492
3bd993ba 493}
93519ec4 494
3bd993ba 495//_____________________________________________________________________
496//
497//EOF
498//