AliRoot Core  3dc7879 (3dc7879)
AliFMDPedestalDA.cxx
Go to the documentation of this file.
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 
21 //
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.
28 //
29 
30 #include "AliFMDPedestalDA.h"
31 #include "AliFMDAltroMapping.h"
32 #include "AliFMDParameters.h"
33 #include "AliFMDCalibPedestal.h"
34 #include "AliFMDDigit.h"
35 #include "AliLog.h"
36 #include <iostream>
37 #include <fstream>
38 #include <iomanip>
39 #include <TFile.h>
40 #include <TF1.h>
41 #include <TObject.h>
42 #include <TMath.h>
43 #include <TSystem.h>
44 #include <TDatime.h>
45 #include <TH2.h>
46 #include <TROOT.h>
47 
48 //_____________________________________________________________________
49 ClassImp(AliFMDPedestalDA)
50 
51 //_____________________________________________________________________
53  : AliFMDBaseDA(),
54  fCurrentChannel(1),
55  fPedSummary("PedestalSummary","pedestals",51200,0,51200),
56  fNoiseSummary("NoiseSummary","noise",51200,0,51200),
57  fZSfileFMD1(),
58  fZSfileFMD2(),
59  fZSfileFMD3(),
60  fMinTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
61  fMaxTimebin(3 * 4 * 3 * 16), // 3 ddls, 4 FECs, 3 Altros, 16 channels
62  fSummaryFMD1i(0),
63  fSummaryFMD2i(0),
64  fSummaryFMD2o(0),
65  fSummaryFMD3i(0),
66  fSummaryFMD3o(0)
67 {
68  // Default constructor
69  fDiagnosticsFilename = "diagnosticsPedestal.root";
70 }
71 
72 //_____________________________________________________________________
74  AliFMDBaseDA(pedDA),
75  fCurrentChannel(1),
76  fPedSummary("PedestalSummary","pedestals",51200,0,51200),
77  fNoiseSummary("NoiseSummary","noise",51200,0,51200),
78  fZSfileFMD1(),
79  fZSfileFMD2(),
80  fZSfileFMD3(),
81  fMinTimebin(pedDA.fMinTimebin),
82  fMaxTimebin(pedDA.fMaxTimebin),
83  fSummaryFMD1i(pedDA.fSummaryFMD1i),
84  fSummaryFMD2i(pedDA.fSummaryFMD2i),
85  fSummaryFMD2o(pedDA.fSummaryFMD2o),
86  fSummaryFMD3i(pedDA.fSummaryFMD3i),
87  fSummaryFMD3o(pedDA.fSummaryFMD3o)
88 {
89  // Copy constructor
90 }
91 
92 //_____________________________________________________________________
94 {
95  // Destructor.
96 }
97 
98 //_____________________________________________________________________
99 Bool_t
101 {
102  if (!AliFMDBaseDA::OpenFiles(appendRun)) return false;
103  if (!appendRun || fRunno == 0) {
104  Rotate("peds.csv", 3);
105  fOutputFile.open("peds.csv");
106  }
107  else
108  fOutputFile.open(Form("peds_%09d.csv",fRunno));
109 
110  if (!fOutputFile) {
111  Error("OpenFiles", "Failed to open pedestal file");
112  return false;
113  }
114  return true;
115 }
116 
117 //_____________________________________________________________________
119 {
120  Int_t ddl = 3072+(d-1);
121  std::ofstream& out = (d==1 ? fZSfileFMD1 : d==2 ? fZSfileFMD2 : fZSfileFMD3);
122  TString base; base.Form("ddl%d.csv", ddl);
123  TString tmp; tmp.Form("%s.tmp", base.Data());
124  if (out.is_open()) {
125  out.write("# EOF\n", 6);
126  out.close();
127  }
128  if (!fSeenDetectors[d-1]) {
129  gSystem->Unlink(tmp.Data());
130  return;
131  }
132 
133  Rotate(base, 10);
134  if (!gSystem->AccessPathName(tmp.Data()))
135  gSystem->Rename(tmp.Data(), base.Data());
136 }
137 //_____________________________________________________________________
139 {
141  InstallFile(1);
142  InstallFile(2);
143  InstallFile(3);
144 }
145 //_____________________________________________________________________
147 {
148  // Initialise
149  SetRequiredEvents(1000);
150  fMinTimebin.Reset(1024);
151  fMaxTimebin.Reset(-1);
152 }
153 
154 //_____________________________________________________________________
156  UShort_t det,
157  Char_t ring,
158  UShort_t sec,
159  UShort_t strip)
160 {
161  // Add a channel to the containers.
162  //
163  // Parameters:
164  // sectorArray Array of sectors
165  // det Detector
166  // ring Ring
167  // sec Sector
168  // strip Strip
170  UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
171  for (UInt_t sample = 0; sample < samples; sample++) {
172  TString name(Form("FMD%d%c[%02d,%03d]_%d", det,ring,sec,strip,sample));
173  TH1S* hSample = new TH1S(name.Data(),name.Data(), 1024,-.5,1023.5);
174  hSample->SetXTitle("ADC");
175  hSample->SetYTitle("Events");
176  hSample->SetDirectory(0);
177  hSample->ResetBit(TObject::kMustCleanup);
178  sampleArray->AddAtAndExpand(hSample, sample);
179  }
180 }
181 
182 //_____________________________________________________________________
184  UShort_t det,
185  Char_t ring,
186  UShort_t sec,
187  UShort_t nStr)
188 {
189  TH1F* sumPed = new TH1F("Pedestals",
190  Form("Summary of pedestals in FMD%d%c[%02d]",
191  det, ring, sec),
192  nStr, -.5, nStr-.5);
193  sumPed->SetXTitle("Strip");
194  sumPed->SetYTitle("Pedestal [ADC]");
195  sumPed->SetDirectory(0);
196  sumPed->ResetBit(TObject::kMustCleanup);
197 
198  TH1F* sumNoise = static_cast<TH1F*>(sumPed->Clone("Noise"));
199  sumNoise->SetYTitle("Noise [ADC]");
200  sumNoise->SetDirectory(0);
201  sumNoise->ResetBit(TObject::kMustCleanup);
202 
203  Int_t n = sectorArray->GetEntriesFast();
204  sectorArray->AddAtAndExpand(sumPed, n + kPedestalOffset - 1);
205  sectorArray->AddAtAndExpand(sumNoise, n + kNoiseOffset - 1);
206 }
207 
208 //_____________________________________________________________________
210 {
211  // Fill ADC values from a digit into the corresponding histogram.
212  //
213  // Parameters:
214  // digit Digit to fill ADC values for.
215  UShort_t det = digit->Detector();
216  Char_t ring = digit->Ring();
217  UShort_t sec = digit->Sector();
218  UShort_t strip = digit->Strip();
219 
221  UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
222  for (UInt_t sample = 0; sample < samples; sample++) {
223  TH1S* hSample = GetChannel(det, ring, sec, strip, sample);
224  if (!hSample) continue;
225 
226  hSample->Fill(digit->Count(sample));
227  }
228 
229 }
230 
231 //_____________________________________________________________________
232 void AliFMDPedestalDA::MakeSummary(UShort_t det, Char_t ring)
233 {
234  //Create summary hists for FMD pedestals
235  // std::cout << "Making summary for FMD" << det << ring << " ..."
236  // << std::endl;
237  switch (det) {
238  case 1:
239  fSummaryFMD1i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
240  break;
241  case 2:
242  switch (ring) {
243  case 'I': case 'i':
244  fSummaryFMD2i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
245  break;
246  case 'O': case 'o':
247  fSummaryFMD2o = MakeSummaryHistogram("ped", "Pedestals", det, ring);
248  break;
249  }
250  break;
251  case 3:
252  switch (ring) {
253  case 'I': case 'i':
254  fSummaryFMD3i = MakeSummaryHistogram("ped", "Pedestals", det, ring);
255  break;
256  case 'O': case 'o':
257  fSummaryFMD3o = MakeSummaryHistogram("ped", "Pedestals", det, ring);
258  break;
259  }
260  break;
261  }
262 }
263 
264 //_____________________________________________________________________
265 void AliFMDPedestalDA::Analyse(UShort_t det,
266  Char_t ring,
267  UShort_t sec,
268  UShort_t strip)
269 {
270  // Analyse a strip. That is, compute the mean and spread of the ADC
271  // spectra for all strips. Also output on files the values.
272  //
273  // Parameters:
274  // det Detector
275  // ring Ring
276  // sec Sector
277  // strip Strip.
279  TH2* summary = 0;
280  switch (det) {
281  case 1: summary = fSummaryFMD1i; break;
282  case 2:
283  switch (ring) {
284  case 'I': summary = fSummaryFMD2i; break;
285  case 'O': summary = fSummaryFMD2o; break;
286  }
287  break;
288  case 3:
289  switch (ring) {
290  case 'I': summary = fSummaryFMD3i; break;
291  case 'O': summary = fSummaryFMD3o; break;
292  }
293  break;
294  }
295 
296  UInt_t samples = pars->GetSampleRate(det, ring, sec, strip);
297  for (UShort_t sample = 0; sample < samples; sample++) {
298  TH1S* hChannel = GetChannel(det, ring, sec, strip,sample);
299  if(!hChannel || hChannel->GetEntries() == 0) {
300  //AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",
301  // det,ring,sec,strip));
302  return;
303  }
304 
305  AliDebugF(50, "Fitting FMD%d%c[%02d,%03d] with %d entries",
306  det,ring,sec,strip, int(hChannel->GetEntries()));
307  TF1* fitFunc = new TF1("fitFunc","gausn",0,300);
308  fitFunc->ResetBit(TObject::kMustCleanup);
309  fitFunc->SetParameters(100,100,1);
310  hChannel->Fit(fitFunc,"Q0","",10,200);
311  hChannel->GetListOfFunctions()->Remove(fitFunc);
312  gROOT->GetListOfFunctions()->Remove(fitFunc);
313 
314  Float_t mean = hChannel->GetMean();
315  Float_t rms = hChannel->GetRMS();
316 
317  hChannel->GetXaxis()->SetRangeUser(mean-5*rms,mean+5*rms);
318  mean = hChannel->GetMean();
319  rms = hChannel->GetRMS();
320 
321 
322  UShort_t ddl, board, altro, channel;
323  UShort_t timebin;
324 
325  pars->Detector2Hardware(det,ring,sec,strip,sample,
326  ddl,board,altro,channel,timebin);
327  Int_t idx = HWIndex(ddl, board, altro, channel);
328  if (idx >= 0) {
329  fMinTimebin[idx] = TMath::Min(Short_t(timebin), fMinTimebin[idx]);
330  fMaxTimebin[idx] = TMath::Max(Short_t(timebin+1), fMaxTimebin[idx]);
331  }
332 
333  std::ostream* zsFile = 0;
334  switch(det) {
335  case 1: zsFile = &fZSfileFMD1; break;
336  case 2: zsFile = &fZSfileFMD2; break;
337  case 3: zsFile = &fZSfileFMD3; break;
338  default: AliWarning("Unknown sample!"); break;
339 
340  }
341  *zsFile << board << ','
342  << altro << ','
343  << channel << ','
344  << timebin << ','
345  << mean << ','
346  << rms << "\n";
347 
348  Float_t chi2ndf = 0;
349 
350 
351  if(fitFunc->GetNDF())
352  chi2ndf = fitFunc->GetChisquare() / fitFunc->GetNDF();
353 
354 
355  Int_t sampleToWrite = 2;
356  if (samples == 2) sampleToWrite = 1;
357  else if (samples < 2) sampleToWrite = 0;
358 
359  hChannel->GetXaxis()->SetRange(1,1024);
360 
361  if(sample != sampleToWrite) continue;
362 
363 
364  fOutputFile << det << ','
365  << ring << ','
366  << sec << ','
367  << strip << ','
368  << mean << ','
369  << rms << ','
370  << fitFunc->GetParameter(1) << ','
371  << fitFunc->GetParameter(2) << ','
372  << chi2ndf <<"\n";
373 
374  delete fitFunc;
375 
376  if (summary) {
377  Int_t bin = summary->FindBin(sec, strip);
378  summary->SetBinContent(bin, mean);
379  summary->SetBinError(bin, rms);
380  }
381 
382  if(fSaveHistograms ) {
383  TH1F* sumPed = GetSectorSummary(det, ring, sec, true);
384  TH1F* sumNoise = GetSectorSummary(det, ring, sec, false);
385  sumPed->SetBinContent(strip+1, mean);
386  sumPed->SetBinError(strip+1, rms);
387  sumNoise->SetBinContent(strip+1, rms);
388 
389  fPedSummary.SetBinContent(fCurrentChannel,mean);
390  fNoiseSummary.SetBinContent(fCurrentChannel,rms);
391  fCurrentChannel++;
392  }
393  }
394 }
395 
396 //_____________________________________________________________________
397 void AliFMDPedestalDA::Terminate(TFile* diagFile)
398 {
399  // Called at the end of a job. Fills in missing time-bins and
400  // closes output files
401  if(fSaveHistograms && diagFile) {
402  diagFile->cd();
403 
404  fPedSummary.Write();
405  fNoiseSummary.Write();
406  }
407 }
408 
409 //_____________________________________________________________________
411 {
412  //
413  // Write headers to output files
414  //
416  fOutputFile.write(Form("# %s \n",pars->GetPedestalShuttleID()),13);
417  TDatime now;
418  fOutputFile << "# This file created from run # " << fRunno
419  << " @ " << now.AsString() << std::endl;
420  fOutputFile.write("# Detector, "
421  "Ring, "
422  "Sector, "
423  "Strip, "
424  "Pedestal, "
425  "Noise, "
426  "Mu, "
427  "Sigma, "
428  "Chi2/NDF \n", 71);
429 
430  fZSfileFMD1.open("ddl3072.csv.tmp");
431  fZSfileFMD2.open("ddl3073.csv.tmp");
432  fZSfileFMD3.open("ddl3074.csv.tmp");
433 
434  std::ostream* zss[] = { &fZSfileFMD1, &fZSfileFMD2, &fZSfileFMD3, 0 };
435  for (size_t i = 0; i < 3; i++) {
436  *(zss[i]) << "# FMD " << (i+1) << " pedestals \n"
437  << "# board, "
438  << "altro, "
439  << "channel, "
440  << "timebin, "
441  << "pedestal, "
442  << "noise\n";
443  *(zss[i]) << "# This file created from run # " << fRunno
444  << " @ " << now.AsString() << std::endl;
445  }
446 }
447 
448 //_____________________________________________________________________
449 TH1S* AliFMDPedestalDA::GetChannel(UShort_t det,
450  Char_t ring,
451  UShort_t sec,
452  UShort_t strip,
453  UInt_t sample)
454 {
455  // Get the histogram corresponding to a strip sample.
456  //
457  // Parameters:
458  // det Detector
459  // ring Ring
460  // sec Sector
461  // strip Strip
462  // sample Sample
463  //
464  // Return:
465  // ADC spectra of a strip.
466  Array* sampleArray = GetStripArray(det, ring, sec, strip);
467  if (!sampleArray) return 0;
468  TH1S* hSample = static_cast<TH1S*>(sampleArray->At(sample));
469  if (!hSample) {
470  AliErrorF("No channel histogram for FMD%d%c[%02d,%03d]_%d",
471  det, ring, sec, strip, sample);
472  sampleArray->ls();
473  AliErrorF("Path is %s <- %s <- %s <- %s",
474  sampleArray->GetName(),
475  GetSectorArray(det, ring, sec)->GetName(),
476  GetRingArray(det, ring)->GetName(),
477  GetDetectorArray(det)->GetName());
478 
479  }
480  return hSample;
481 
482 }
483 //_____________________________________________________________________
485  Char_t ring,
486  UShort_t sec,
487  Bool_t pedNotNoise)
488 {
489  Array* secArray = GetSectorArray(det, ring, sec);
490  Int_t n = secArray->GetEntriesFast();
491  Int_t i = n - (pedNotNoise ? kNoiseOffset : kPedestalOffset);
492  return static_cast<TH1F*>(secArray->At(i));
493 }
494 
495 //_____________________________________________________________________
496 //
497 //EOF
498 //
class for digits
Definition: AliFMDDigit.h:28
self initialized array, used for adding constraints
void MakeSummary(UShort_t det, Char_t ring)
void SetRequiredEvents(Int_t nEvents)
Definition: AliFMDBaseDA.h:114
std::ofstream fZSfileFMD2
Array * GetDetectorArray(UShort_t det)
Int_t HWIndex(UShort_t ddl, UShort_t board, UShort_t altro, UShort_t chan) const
Digits for the FMD.
void Rotate(const char *base, int max) const
void AddChannelContainer(Array *sectorArray, UShort_t det, Char_t ring, UShort_t sec, UShort_t strip)
UShort_t Sector() const
Char_t Ring() const
std::ofstream fOutputFile
Definition: AliFMDBaseDA.h:383
This class is a singleton that handles various parameters of the FMD detectors. This class reads from...
Manager of FMD parameters.
virtual ~AliFMDPedestalDA()
Array * GetRingArray(UShort_t det, Char_t ring)
TROOT * gROOT
Bool_t OpenFiles(Bool_t appendRun=false)
Per strip pedestal calibration.
void Terminate(TFile *)
#define AliWarning(message)
Definition: AliLog.h:541
std::ofstream fZSfileFMD1
Map HW address to detector coordinates and back again.
void FillChannels(AliFMDDigit *digit)
TH2 * MakeSummaryHistogram(const char *prefix, const char *title, UShort_t det, Char_t ring)
TH1S * GetChannel(UShort_t det, Char_t ring, UShort_t sec, UShort_t strip, UInt_t sample)
UShort_t Strip() const
std::ofstream fZSfileFMD3
static AliFMDParameters * Instance()
Bool_t fSaveHistograms
Definition: AliFMDBaseDA.h:385
virtual void AddSectorSummary(Array *secArray, UShort_t det, Char_t ring, UShort_t sector, UShort_t nStrip)
void InstallFile(Int_t d)
void Analyse(UShort_t det, Char_t ring, UShort_t sec, UShort_t strip)
virtual void CloseFiles()
Bool_t Detector2Hardware(UShort_t det, Char_t ring, UShort_t sec, UShort_t str, UShort_t sam, UShort_t &ddl, UShort_t &board, UShort_t &altro, UShort_t &channel, UShort_t &timebin) const
UShort_t Detector() const
virtual Bool_t OpenFiles(Bool_t appendRun=false)
static const char * GetPedestalShuttleID()
Array * GetSectorArray(UShort_t det, Char_t ring, UShort_t sector)
Int_t Count(UShort_t i=0) const
Definition: AliFMDDigit.h:156
#define AliErrorF(message,...)
Definition: AliLog.h:606
#define AliDebugF(logLevel, format,...)
Definition: AliLog.h:338
UShort_t GetSampleRate(UShort_t detector, Char_t ring, UShort_t sector, UShort_t strip) const
Array * GetStripArray(UShort_t det, Char_t ring, UShort_t sector, UShort_t strip)
Bool_t fSeenDetectors[3]
Definition: AliFMDBaseDA.h:390
TH1F * GetSectorSummary(UShort_t det, Char_t ring, UShort_t sec, Bool_t pedNotNoise)