Fixes for Coverity warnings (M. van Leeuwen)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDCMN.cxx
CommitLineData
7f347565 1/**************************************************************************
2 * Copyright(c) 2007-2009, 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 **************************************************************************/
e44f571c 15#include <TFile.h>
7f347565 16#include "AliITSOnlineSDDCMN.h"
17#include "AliLog.h"
18#include <TH2F.h>
19#include <TMath.h>
20
21
22///////////////////////////////////////////////////////////////////
23// //
24// Implementation of the class used for analysis of SDD noise //
25// corrected for common mode //
26// Origin: F.Prino, Torino, prino@to.infn.it //
27// //
28///////////////////////////////////////////////////////////////////
29
30
31ClassImp(AliITSOnlineSDDCMN)
32//______________________________________________________________________
e7610d19 33 AliITSOnlineSDDCMN::AliITSOnlineSDDCMN():AliITSOnlineSDD(),fNEvents(0),fLowThreshold(0),fHighThreshold(0),fMinCorrNoise(0.),fMaxCorrNoise(0.),fNSigmaNoise(0.)
7f347565 34{
35 // default constructor
36 Reset();
37 SetMinNoise();
38 SetMaxNoise();
39 SetNSigmaNoise();
40}
41//______________________________________________________________________
e7610d19 42 AliITSOnlineSDDCMN::AliITSOnlineSDDCMN(Int_t nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fNEvents(0),fLowThreshold(0),fHighThreshold(0),fMinCorrNoise(0.),fMaxCorrNoise(0.),fNSigmaNoise(0.)
7f347565 43{
44 // default constructor
45 Reset();
46 SetMinNoise();
47 SetMaxNoise();
48 SetNSigmaNoise();
49}
50//______________________________________________________________________
51AliITSOnlineSDDCMN::~AliITSOnlineSDDCMN(){
52 // Destructor
53}
54//______________________________________________________________________
55void AliITSOnlineSDDCMN::Reset(){
776c19a3 56 // Reset counters
7f347565 57 fNEvents=0;
58 for(Int_t i=0;i<fgkNAnodes;i++){
59 fGoodAnode[i]=1;
60 fBaseline[i]=0.;
3083967f 61 fRawNoise[i]=0.;
7f347565 62 fCMN[i]=0.;
63 fSumCorrNoise[i]=0.;
64 }
65 ReadBaselines();
66}
67//______________________________________________________________________
68void AliITSOnlineSDDCMN::ReadBaselines(){
69 // assume baselines and good anodes are taken from previous run
2c4e6a6a 70 TString basfilnam;
71 basfilnam.Form("SDDbase_step1_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
72 FILE* basf=fopen(basfilnam.Data(),"r");
7f347565 73 if(basf==0){
e7610d19 74 AliWarning(Form("Baseline file not present (ddl %d carlos %d side %d, Set all baselines to 50\n",fDDL,fCarlos,fSide));
7f347565 75 for(Int_t ian=0;ian<fgkNAnodes;ian++){
76 fBaseline[ian]=50.;
d8303ac8 77 fEqBaseline[ian]=50;
78 fOffsetBaseline[ian]=0;
7f347565 79 fGoodAnode[ian]=1;
80 }
81 return;
82 }
e7610d19 83 fscanf(basf,"%d\n",&fHighThreshold);
84 fscanf(basf,"%d\n",&fLowThreshold);
d8303ac8 85 Int_t n,ok,eqbase,offbase;
7f347565 86 Float_t base,rms,cmn,corrnoi;
87 for(Int_t ian=0;ian<fgkNAnodes;ian++){
d8303ac8 88 fscanf(basf,"%d %d %f %d %d %f %f %f\n",&n,&ok,&base,&eqbase,&offbase,&rms,&cmn,&corrnoi);
7f347565 89 fGoodAnode[ian]=ok;
90 fBaseline[ian]=base;
d8303ac8 91 fEqBaseline[ian]=eqbase;
92 fOffsetBaseline[ian]=offbase;
7f347565 93 fRawNoise[ian]=rms;
94 fCMN[ian]=cmn;
95 }
96 fclose(basf);
97}
98//______________________________________________________________________
99void AliITSOnlineSDDCMN::ValidateAnodes(){
776c19a3 100 // Tag good/bad anodes
7f347565 101 for(Int_t ian=0;ian<fgkNAnodes;ian++){
102 if(!fGoodAnode[ian]) continue;
103 if(GetAnodeCorrNoise(ian)>fMaxCorrNoise || GetAnodeCorrNoise(ian)<fMinCorrNoise) fGoodAnode[ian]=0;
104 if(GetAnodeCorrNoise(ian)>fNSigmaNoise*CalcMeanNoise()) fGoodAnode[ian]=0;
105 }
106}
107
108//______________________________________________________________________
b255f56d 109TH2F* AliITSOnlineSDDCMN::GetCleanEvent(TH2F* hrawd) const {
776c19a3 110 // Fills an histogram with counts corrected for common mode noise
111
7f347565 112 TH2F* hcorrd=new TH2F("hcorrd","",hrawd->GetNbinsX(),hrawd->GetXaxis()->GetXmin(),hrawd->GetXaxis()->GetXmax(),hrawd->GetNbinsY(),hrawd->GetYaxis()->GetXmin(),hrawd->GetYaxis()->GetXmax());
750296dd 113 for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
7f347565 114 Float_t sumEven=0., sumOdd=0.;
115 Int_t countEven=0, countOdd=0;
116 for(Int_t ian=0;ian<fgkNAnodes;ian+=2){
117 if(!fGoodAnode[ian]) continue;
118 sumEven+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
119 countEven++;
120 }
121 for(Int_t ian=1;ian<fgkNAnodes;ian+=2){
122 if(!fGoodAnode[ian]) continue;
123 sumOdd+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
124 countOdd++;
125 }
126 for(Int_t ian=0;ian<fgkNAnodes;ian++){
127 if(!fGoodAnode[ian]) continue;
128 Float_t meanN;
129 if(ian%2==0) meanN=sumEven/(Float_t)countEven;
130 else meanN=sumOdd/(Float_t)countOdd;
131 Float_t cntCorr=hrawd->GetBinContent(itb+1,ian+1)-fCMN[ian]*meanN;
132 hcorrd->SetBinContent(itb+1,ian+1,cntCorr);
133 }
134 }
b255f56d 135 return hcorrd;
136}
137//______________________________________________________________________
138void AliITSOnlineSDDCMN::AddEvent(TH2F* hrawd){
776c19a3 139 // analyzes one event and adds its ontribution to the various counters
140
b255f56d 141 fNEvents++;
142 TH2F* hcorrd=GetCleanEvent(hrawd);
7f347565 143
144 for(Int_t ian=0;ian<fgkNAnodes;ian++){
145 if(!fGoodAnode[ian]) continue;
146 Float_t sumQ=0.;
b255f56d 147 Int_t cnt=0;
750296dd 148 for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
a8cd2ab2 149 Float_t cntdiff=hcorrd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
150 sumQ+=cntdiff*cntdiff;
b255f56d 151 cnt++;
7f347565 152 }
b255f56d 153 fSumCorrNoise[ian]+=TMath::Sqrt(sumQ/(Float_t)cnt);
7f347565 154 }
155 delete hcorrd;
156}
157//______________________________________________________________________
beb262b4 158Float_t AliITSOnlineSDDCMN::CalcMeanNoise() const{
776c19a3 159 // compute average noise
160
7f347565 161 Float_t meanns=0.;
162 Int_t cnt=0;
163 for(Int_t ian=0;ian<fgkNAnodes;ian++){
164 if(!fGoodAnode[ian]) continue;
165 meanns+=GetAnodeCorrNoise(ian);
166 cnt++;
167 }
168 if(cnt>0) meanns/=(Float_t)cnt;
169 return meanns;
170}
171//______________________________________________________________________
e44f571c 172void AliITSOnlineSDDCMN::WriteToASCII(){
776c19a3 173 // writes parameters of each channel into an ASCII file
174 // to be then read by the PULSER DA (AliITSOnlineSDDTP)
175
2c4e6a6a 176 TString outfilnam;
177 outfilnam.Form("SDDbase_step2_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
178 FILE* outf=fopen(outfilnam.Data(),"w");
e7610d19 179 fprintf(outf,"%d\n",fHighThreshold);
180 fprintf(outf,"%d\n",fLowThreshold);
7f347565 181 for(Int_t ian=0;ian<fgkNAnodes;ian++){
d8303ac8 182 fprintf(outf,"%d %d %8.3f %d %d %8.3f %8.3f %8.3f\n",ian,IsAnodeGood(ian),GetAnodeBaseline(ian),GetAnodeEqualizedBaseline(ian),GetAnodeBaselineOffset(ian),GetAnodeRawNoise(ian),GetAnodeCommonMode(ian),GetAnodeCorrNoise(ian));
7f347565 183 }
184 fclose(outf);
185}
4ff6aa93 186
187//______________________________________________________________________
188TH1F* AliITSOnlineSDDCMN::GetBaselineAnodeHisto() const {
776c19a3 189 // produce histogram with baseline vs. anode number
2c4e6a6a 190 TString hisnam;
191 hisnam.Form("hbase%02dc%02ds%d",fDDL,fCarlos,fSide);
192 TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
4ff6aa93 193 for(Int_t ian=0;ian<fgkNAnodes;ian++){
194 h->SetBinContent(ian+1,GetAnodeBaseline(ian));
195 }
196 return h;
197}
198//______________________________________________________________________
199TH1F* AliITSOnlineSDDCMN::GetRawNoiseAnodeHisto() const {
776c19a3 200 // produce histogram with raw noise vs. anode number
2c4e6a6a 201 TString hisnam;
202 hisnam.Form("hnois%02dc%02ds%d",fDDL,fCarlos,fSide);
203 TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
4ff6aa93 204 for(Int_t ian=0;ian<fgkNAnodes;ian++){
205 h->SetBinContent(ian+1,GetAnodeRawNoise(ian));
206 }
207 return h;
208}
209//______________________________________________________________________
210TH1F* AliITSOnlineSDDCMN::GetCorrNoiseAnodeHisto() const {
776c19a3 211 // produce histogram with corrected noise vs. anode number
2c4e6a6a 212 TString hisnam;
213 hisnam.Form("hcorn%02dc%02ds%d",fDDL,fCarlos,fSide);
214 TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
4ff6aa93 215 for(Int_t ian=0;ian<fgkNAnodes;ian++){
216 h->SetBinContent(ian+1,GetAnodeCorrNoise(ian));
217 }
218 return h;
219}
220//______________________________________________________________________
e5b2f7f2 221TH1F* AliITSOnlineSDDCMN::GetCMNCoefAnodeHisto() const {
776c19a3 222 // produce histogram with coefficients for common mode noise subtraction
2c4e6a6a 223 TString hisnam;
224 hisnam.Form("hcmn%02dc%02ds%d",fDDL,fCarlos,fSide);
225 TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
e5b2f7f2 226 for(Int_t ian=0;ian<fgkNAnodes;ian++){
227 h->SetBinContent(ian+1,GetAnodeCommonMode(ian));
228 }
229 return h;
230}
231//______________________________________________________________________
232TH1F* AliITSOnlineSDDCMN::GetStatusAnodeHisto() const {
776c19a3 233 // produce histogram with status bit of each anode
2c4e6a6a 234 TString hisnam;
235 hisnam.Form("hgood%02dc%02ds%d",fDDL,fCarlos,fSide);
236 TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
e5b2f7f2 237 for(Int_t ian=0;ian<fgkNAnodes;ian++){
238 h->SetBinContent(ian+1,float(IsAnodeGood(ian)));
239 }
240 return h;
241}
242//______________________________________________________________________
4ff6aa93 243TH1F* AliITSOnlineSDDCMN::GetBaselineHisto() const {
776c19a3 244 // produce histogram with baseline distribution
2c4e6a6a 245 TString hisnam;
246 hisnam.Form("hdbd%02dc%02ds%d",fDDL,fCarlos,fSide);
247 TH1F* h=new TH1F(hisnam.Data(),"",100,0.,150.);
4ff6aa93 248 for(Int_t ian=0;ian<fgkNAnodes;ian++){
249 h->Fill(GetAnodeBaseline(ian));
250 }
251 return h;
252}
253//______________________________________________________________________
254TH1F* AliITSOnlineSDDCMN::GetRawNoiseHisto() const {
776c19a3 255 // produce histogram with raw noise distribution
2c4e6a6a 256 TString hisnam;
257 hisnam.Form("hdnd%02dc%02ds%d",fDDL,fCarlos,fSide);
258 TH1F* h=new TH1F(hisnam.Data(),"",100,0.,8.);
4ff6aa93 259 for(Int_t ian=0;ian<fgkNAnodes;ian++){
260 h->Fill(GetAnodeRawNoise(ian));
261 }
262 return h;
263}
264//______________________________________________________________________
265TH1F* AliITSOnlineSDDCMN::GetCorrNoiseHisto() const {
776c19a3 266 // produce histogram with corrected noise distribution
2c4e6a6a 267 TString hisnam;
268 hisnam.Form("hdcd%02dc%02ds%d",fDDL,fCarlos,fSide);
269 TH1F* h=new TH1F(hisnam.Data(),"",100,0.,8.);
4ff6aa93 270 for(Int_t ian=0;ian<fgkNAnodes;ian++){
271 h->Fill(GetAnodeCorrNoise(ian));
272 }
273 return h;
274}
e44f571c 275//______________________________________________________________________
276Bool_t AliITSOnlineSDDCMN::WriteToROOT(TFile *fil){
776c19a3 277 // writes output into a root file
e44f571c 278 if(fil==0){
279 AliWarning("Invalid pointer to ROOT file");
280 return kFALSE;
281 }
2c4e6a6a 282 TString hisnam;
e44f571c 283 fil->cd();
2c4e6a6a 284 hisnam.Form("hgood%02dc%02ds%d",fDDL,fCarlos,fSide);
285 TH1F hgood(hisnam.Data(),"",256,-0.5,255.5);
286 hisnam.Form("hbase%02dc%02ds%d",fDDL,fCarlos,fSide);
287 TH1F hbase(hisnam.Data(),"",256,-0.5,255.5);
288 hisnam.Form("hnois%02dc%02ds%d",fDDL,fCarlos,fSide);
289 TH1F hnois(hisnam.Data(),"",256,-0.5,255.5);
290 hisnam.Form("hcmn%02dc%02ds%d",fDDL,fCarlos,fSide);
291 TH1F hcmn(hisnam.Data(),"",256,-0.5,255.5);
292 hisnam.Form("hcorn%02dc%02ds%d",fDDL,fCarlos,fSide);
293 TH1F hcorn(hisnam.Data(),"",256,-0.5,255.5);
e44f571c 294 for(Int_t ian=0;ian<fgkNAnodes;ian++){
295 hgood.SetBinContent(ian+1,float(IsAnodeGood(ian)));
296 hbase.SetBinContent(ian+1,GetAnodeBaseline(ian));
297 hnois.SetBinContent(ian+1,GetAnodeRawNoise(ian));
298 hcmn.SetBinContent(ian+1,GetAnodeCommonMode(ian));
299 hcorn.SetBinContent(ian+1,GetAnodeCorrNoise(ian));
300 }
301 hgood.Write();
302 hbase.Write();
303 hnois.Write();
304 hcmn.Write();
305 hcorn.Write();
306 return kTRUE;
307}