Bug fixed in copy ctor + small mods (obsolete lines removed).
[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>
3bd993ba 39
40//_____________________________________________________________________
41ClassImp(AliFMDPedestalDA)
42
43//_____________________________________________________________________
f7f0b643 44AliFMDPedestalDA::AliFMDPedestalDA() : AliFMDBaseDA(),
f14ede67 45 fCurrentChannel(1),
f7f0b643 46 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
7bce699b 47 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
48 fZSfileFMD1(),
49 fZSfileFMD2(),
5cf05dbb 50 fZSfileFMD3(),
51 fMinTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
52 fMaxTimebin(3 * 4 * 3 * 16) // 3 ddls, 4 FECs, 3 Altros, 16 channels
3bd993ba 53{
5cf05dbb 54 // Default constructor
3bd993ba 55 fOutputFile.open("peds.csv");
7bce699b 56 fZSfileFMD1.open("ddl3072.csv");
57 fZSfileFMD2.open("ddl3073.csv");
58 fZSfileFMD3.open("ddl3074.csv");
3bd993ba 59}
60
61//_____________________________________________________________________
62AliFMDPedestalDA::AliFMDPedestalDA(const AliFMDPedestalDA & pedDA) :
f7f0b643 63 AliFMDBaseDA(pedDA),
f14ede67 64 fCurrentChannel(1),
f7f0b643 65 fPedSummary("PedestalSummary","pedestals",51200,0,51200),
7bce699b 66 fNoiseSummary("NoiseSummary","noise",51200,0,51200),
67 fZSfileFMD1(),
68 fZSfileFMD2(),
5cf05dbb 69 fZSfileFMD3(),
70 fMinTimebin(pedDA.fMinTimebin),
71 fMaxTimebin(pedDA.fMaxTimebin)
3bd993ba 72{
5cf05dbb 73 // Copy constructor
3bd993ba 74}
75
76//_____________________________________________________________________
e9c06036 77AliFMDPedestalDA::~AliFMDPedestalDA()
78{
5cf05dbb 79 // Destructor.
3bd993ba 80}
81
82//_____________________________________________________________________
e9c06036 83void AliFMDPedestalDA::Init()
84{
5cf05dbb 85 // Initialise
3bd993ba 86 SetRequiredEvents(1000);
5cf05dbb 87 fMinTimebin.Reset(1024);
88 fMaxTimebin.Reset(-1);
3bd993ba 89}
90
91//_____________________________________________________________________
92void AliFMDPedestalDA::AddChannelContainer(TObjArray* sectorArray,
93 UShort_t det,
e9c06036 94 Char_t ring,
3bd993ba 95 UShort_t sec,
e9c06036 96 UShort_t strip)
97{
5cf05dbb 98 // Add a channel to the containers.
99 //
100 // Parameters:
101 // sectorArray Array of sectors
102 // det Detector
103 // ring Ring
104 // sec Sector
105 // strip Strip
e9c06036 106 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 107 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
e9c06036 108 TObjArray* sampleArray = new TObjArray(samples);
7bce699b 109 sampleArray->SetOwner();
110 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 111 TH1S* hSample = new TH1S(Form("FMD%d%c[%02d,03%d]_%d",
112 det,ring,sec,strip,sample),
113 Form("FMD%d%c[%02d,%03%d]_%d",
114 det,ring,sec,strip),
115 1024,-.5,1023.5);
116 hSample->SetXTitle("ADC");
117 hSample->SetYTitle("Events");
7bce699b 118 hSample->SetDirectory(0);
e9c06036 119 sampleArray->AddAt(hSample, sample);
120 }
121 sectorArray->AddAtAndExpand(sampleArray, strip);
3bd993ba 122}
123
124//_____________________________________________________________________
e9c06036 125void AliFMDPedestalDA::FillChannels(AliFMDDigit* digit)
126{
5cf05dbb 127 // Fill ADC values from a digit into the corresponding histogram.
128 //
129 // Parameters:
130 // digit Digit to fill ADC values for.
3bd993ba 131 UShort_t det = digit->Detector();
132 Char_t ring = digit->Ring();
133 UShort_t sec = digit->Sector();
134 UShort_t strip = digit->Strip();
15e37b0a 135
e9c06036 136 AliFMDParameters* pars = AliFMDParameters::Instance();
5cf05dbb 137 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 138 for (UInt_t sample = 0; sample < samples; sample++) {
e9c06036 139 TH1S* hSample = GetChannel(det, ring, sec, strip, sample);
140 hSample->Fill(digit->Count(sample));
141 }
7bce699b 142
3bd993ba 143}
144
145//_____________________________________________________________________
146void AliFMDPedestalDA::Analyse(UShort_t det,
e9c06036 147 Char_t ring,
3bd993ba 148 UShort_t sec,
5cf05dbb 149 UShort_t strip)
150{
151 // Analyse a strip. That is, compute the mean and spread of the ADC
152 // spectra for all strips. Also output on files the values.
153 //
154 // Parameters:
155 // det Detector
156 // ring Ring
157 // sec Sector
158 // strip Strip.
7bce699b 159 AliFMDParameters* pars = AliFMDParameters::Instance();
762e54b7 160 // Float_t factor = pars->GetPedestalFactor();
5cf05dbb 161 UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
7bce699b 162 for (UShort_t sample = 0; sample < samples; sample++) {
3bd993ba 163
5cf05dbb 164 TH1S* hChannel = GetChannel(det, ring, sec, strip,sample);
7bce699b 165 if(hChannel->GetEntries() == 0) {
166 //AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
167 // det,ring,sec,strip));
168 return;
e9c06036 169 }
f7f0b643 170
5cf05dbb 171 AliDebug(50, Form("Fitting FMD%d%c_%d_%d with %d entries",
172 det,ring,sec,strip, hChannel->GetEntries()));
7bce699b 173 TF1 fitFunc("fitFunc","gausn",0,300);
174 fitFunc.SetParameters(100,100,1);
175 hChannel->Fit("fitFunc","Q0+","",10,200);
176
177 Float_t mean = hChannel->GetMean();
178 Float_t rms = hChannel->GetRMS();
179
f7f0b643 180
f7f0b643 181
7bce699b 182 hChannel->GetXaxis()->SetRangeUser(mean-5*rms,mean+5*rms);
f7f0b643 183
7bce699b 184 mean = hChannel->GetMean();
185 rms = hChannel->GetRMS();
93519ec4 186
7bce699b 187
188 UShort_t ddl, board, altro, channel;
189 UShort_t timebin;
190
5cf05dbb 191 pars->Detector2Hardware(det,ring,sec,strip,sample,
192 ddl,board,altro,channel,timebin);
193 Int_t idx = HWIndex(ddl, board, altro, channel);
194 if (idx >= 0) {
195 fMinTimebin[idx] = TMath::Min(Short_t(timebin), fMinTimebin[idx]);
196 fMaxTimebin[idx] = TMath::Max(Short_t(timebin+1), fMaxTimebin[idx]);
197 }
7bce699b 198
5cf05dbb 199 std::ostream* zsFile = 0;
7bce699b 200 switch(det) {
5cf05dbb 201 case 1: zsFile = &fZSfileFMD1; break;
202 case 2: zsFile = &fZSfileFMD2; break;
203 case 3: zsFile = &fZSfileFMD3; break;
204 default: AliWarning("Unknown sample!"); break;
7bce699b 205
206 }
5cf05dbb 207 *zsFile << board << ','
208 << altro << ','
209 << channel << ','
210 << timebin << ','
211 << mean << ','
762e54b7 212 << rms << "\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));
408bf2b4 299 for (Int_t i = 0; i < 3; i++) {
300 if (!fSeenDetectors[i]) {
301 TString n(Form("ddl%d.csv",3072+map->Detector2DDL(i+1)));
302 gSystem->Unlink(n.Data());
303 }
304 }
f7f0b643 305
5cf05dbb 306}
307
308//_____________________________________________________________________
7af3df7f 309void AliFMDPedestalDA::FillinTimebins(std::ofstream& out, UShort_t /*ddl*/)
5cf05dbb 310{
762e54b7 311#if 0
5cf05dbb 312 unsigned short boards[] = { 0x0, 0x1, 0x10, 0x11, 0xFFFF };
313 unsigned short* board = boards;
314 while ((*boards) != 0xFFFF) {
315 for (UShort_t altro = 0; altro < 3; altro++) {
316 for (UShort_t channel = 0; channel < 16; channel++) {
317 Int_t idx = HWIndex(ddl, *board, altro, channel);
318 if (idx < 0) {
319 AliWarning(Form("Invalid index for %4d/0x%02x/0x%x/0x%x: %d",
320 ddl, *board, altro, channel, idx));
321 continue;
322 }
323 Short_t min = fMinTimebin[idx];
324 Short_t max = fMaxTimebin[idx];
325
326 // Channel not seen at all.
327 if (min > 1023 || max < 0) continue;
328
329 out << "# Extra timebins for 0x" << std::hex
330 << board << ',' << altro << ',' << channel
331 << " got time-bins " << min << " to " << max-1
332 << std::dec << std::endl;
333
334 for (UShort_t t = 15; t < min; t++)
335 // Write a phony line
336 out << board << "," << altro << "," << channel << ","
337 << t << "," << 1023 << "," << 0 << std::endl;
338
339 for (UShort_t t = max; t < 1024; t++)
340 // Write a phony line
341 out << board << "," << altro << "," << channel << ","
342 << t << "," << 1023 << "," << 0 << std::endl;
343 } // channel loop
344 } // altro loop
345 } // board loop
346 // Write trailer, and close
762e54b7 347#endif
5cf05dbb 348 out.write("# EOF\n", 6);
349 out.close();
f7f0b643 350}
351
352//_____________________________________________________________________
e9c06036 353void AliFMDPedestalDA::WriteHeaderToFile()
354{
5cf05dbb 355 // Write headers to output files
80fdb9f3 356 AliFMDParameters* pars = AliFMDParameters::Instance();
357 fOutputFile.write(Form("# %s \n",pars->GetPedestalShuttleID()),13);
f7f0b643 358 fOutputFile.write("# Detector, "
359 "Ring, "
360 "Sector, "
e9c06036 361 "Strip, "
e9c06036 362 "Pedestal, "
363 "Noise, "
364 "Mu, "
365 "Sigma, "
f7f0b643 366 "Chi2/NDF \n", 71);
5cf05dbb 367
368 std::ostream* zss[] = { &fZSfileFMD1, &fZSfileFMD2, &fZSfileFMD3, 0 };
369 for (size_t i = 0; i < 3; i++)
370 *(zss[i]) << "# FMD 1 pedestals \n"
371 << "# board, "
372 << "altro, "
373 << "channel, "
374 << "timebin, "
375 << "pedestal, "
762e54b7 376 << "noise\n";
3bd993ba 377}
378
379//_____________________________________________________________________
e9c06036 380TH1S* AliFMDPedestalDA::GetChannel(UShort_t det,
381 Char_t ring,
382 UShort_t sec,
7bce699b 383 UShort_t strip,
5cf05dbb 384 UInt_t sample)
e9c06036 385{
5cf05dbb 386 // Get the histogram corresponding to a strip sample.
387 //
388 // Parameters:
389 // det Detector
390 // ring Ring
391 // sec Sector
392 // strip Strip
393 // sample Sample
394 //
395 // Return:
396 // ADC spectra of a strip.
397 UShort_t iring = (ring == 'O' ? 0 : 1);
398 TObjArray* detArray = static_cast<TObjArray*>(fDetectorArray.At(det));
399 TObjArray* ringArray = static_cast<TObjArray*>(detArray->At(iring));
400 TObjArray* secArray = static_cast<TObjArray*>(ringArray->At(sec));
401 TObjArray* sampleArray = static_cast<TObjArray*>(secArray->At(strip));
402 TH1S* hSample = static_cast<TH1S*>(sampleArray->At(sample));
e9c06036 403 return hSample;
7bce699b 404
3bd993ba 405}
93519ec4 406
3bd993ba 407//_____________________________________________________________________
408//
409//EOF
410//