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