This is the pedestal implementation of the FMD detector algorithm.
[u/mrichter/AliRoot.git] / FMD / AliFMDPedestalDA.cxx
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 //
22 // This class implements the virtual functions of the AliFMDBaseDA class.
23 // The most important of these functions, FillChannels(..) and Analyse(...) collect 
24 // and analyse the data of each channel. The resulting pedestal and noise values are
25 // written to a comma separated values (csv) file on the go. The csv files produced 
26 // in this way are the basic input to the AliFMDPreprocessor.
27 //
28
29 #include "AliFMDPedestalDA.h"
30 #include "iostream"
31 #include "fstream"
32 #include "AliLog.h"
33 #include "TF1.h"
34
35 //_____________________________________________________________________
36 ClassImp(AliFMDPedestalDA)
37
38 //_____________________________________________________________________
39 AliFMDPedestalDA::AliFMDPedestalDA() : AliFMDBaseDA()
40 {
41   fOutputFile.open("peds.csv");
42   
43 }
44
45 //_____________________________________________________________________
46 AliFMDPedestalDA::AliFMDPedestalDA(const AliFMDPedestalDA & pedDA) : 
47   AliFMDBaseDA(pedDA)
48 {
49   
50 }
51
52 //_____________________________________________________________________
53 AliFMDPedestalDA::~AliFMDPedestalDA() {
54  
55 }
56
57 //_____________________________________________________________________
58 void AliFMDPedestalDA::Init() {
59  
60   SetRequiredEvents(1000);
61 }
62
63 //_____________________________________________________________________
64 void AliFMDPedestalDA::AddChannelContainer(TObjArray* sectorArray, 
65                                            UShort_t det, 
66                                            Char_t ring, 
67                                            UShort_t sec, 
68                                            UShort_t strip) {
69
70   TH1S* hChannel = new TH1S(Form("hFMD%d%c_%d_%d",det,ring,sec,strip),
71                             Form("hFMD%d%c_%d_%d",det,ring,sec,strip),
72                             1024,0,1023);
73   
74   hChannel->SetDirectory(0);
75   sectorArray->AddAtAndExpand(hChannel,strip);
76 }
77
78 //_____________________________________________________________________
79 void AliFMDPedestalDA::FillChannels(AliFMDDigit* digit) {
80   UShort_t det   = digit->Detector();
81   Char_t   ring  = digit->Ring();
82   UShort_t sec   = digit->Sector();
83   UShort_t strip = digit->Strip();
84     
85   TH1S* hChannel       = GetChannel(det, ring, sec, strip);
86   hChannel->Fill(digit->Counts());
87   
88 }
89
90 //_____________________________________________________________________
91 void AliFMDPedestalDA::Analyse(UShort_t det, 
92                                Char_t ring, 
93                                UShort_t sec, 
94                                UShort_t strip) {
95
96   TH1S* hChannel       = GetChannel(det, ring, sec, strip);
97   if(hChannel->GetEntries()==0) {
98     AliWarning(Form("No entries for FMD%d%c, sector %d, strip %d",det,ring,sec,strip));
99     return;
100   }
101   //std::cout<<"Fitting Channel #"<<Form("FMD%d%c_%d_%d",det,ring,sec,strip)<<"   \r"<<std::flush;
102   
103   Float_t mean = hChannel->GetMean();
104   Float_t rms  = hChannel->GetRMS();
105   
106   for(Int_t i=1;i<hChannel->GetNbinsX();i++)
107     if(hChannel->GetBinCenter(i)>mean+5*rms || hChannel->GetBinCenter(i)<mean-5*rms)
108       hChannel->SetBinContent(i,0);
109   
110   mean = hChannel->GetMean();
111   rms  = hChannel->GetRMS();
112   
113   hChannel->Fit("gaus","QO","QO",mean-5*rms,mean+5*rms);
114   TF1* fitFunc = hChannel->GetFunction("gaus");
115     
116   UInt_t ddl, board, chip, channel;
117   
118   UShort_t relStrip = strip%128;
119   AliFMDParameters* pars = AliFMDParameters::Instance();
120   pars->Detector2Hardware(det,ring,sec,strip,ddl,board,chip,channel);
121   Float_t chi2ndf = 0;
122   if(fitFunc->GetNDF())
123     chi2ndf = fitFunc->GetChisquare() / fitFunc->GetNDF();
124   ddl = ddl + kBaseDDL;
125   
126   fOutputFile << ddl                         << ','
127               << board                       << ','
128               << chip                        << ','
129               << channel                     << ','
130               << relStrip                    << ','
131               << 0                           << ','
132               << 0                           << ','
133               << mean                        << ','
134               << rms                         << ','
135               << fitFunc->GetParameter(1)    << ','
136               << fitFunc->GetParameter(2)    << ','
137               << chi2ndf                     <<"\n";
138   
139   if(fSaveHistograms) {
140     gDirectory->cd(Form("%s:FMD%d%c/sector_%d/strip_%d",fDiagnosticsFilename,det,ring,sec,strip));
141     hChannel->Write();
142     
143   }
144   
145 }
146
147 //_____________________________________________________________________
148 void AliFMDPedestalDA::WriteHeaderToFile() {
149   fOutputFile.write("# Pedestals \n",13);
150   fOutputFile.write("# Rcu, Board, Chip, Channel, Strip, Sample, TimeBin, Pedestal, Noise, Mu, Sigma, Chi2/NDF \n",91);
151   
152 }
153
154 //_____________________________________________________________________
155 TH1S* AliFMDPedestalDA::GetChannel(UShort_t det, Char_t ring, UShort_t sec, UShort_t strip) {
156   
157   UShort_t  Ring = 1;
158   if(ring == 'O')
159     Ring = 0;
160   
161   
162   TObjArray* detArray  = static_cast<TObjArray*>(fDetectorArray.At(det));
163   TObjArray* ringArray = static_cast<TObjArray*>(detArray->At(Ring));
164   TObjArray* secArray  = static_cast<TObjArray*>(ringArray->At(sec));
165   TH1S* hChannel       = static_cast<TH1S*>(secArray->At(strip));
166   
167   return hChannel;
168 }
169 //_____________________________________________________________________
170 //
171 //EOF
172 //