Fix coverity warnings (F. Prino)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDBase.cxx
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  **************************************************************************/
15 #include "AliITSOnlineSDDBase.h"
16 #include <TH2F.h>
17 #include <TMath.h>
18
19
20 ///////////////////////////////////////////////////////////////////
21 //                                                               //
22 // Implementation of the class used for SDD baselines            //
23 // and noise analysis                                            //
24 // Origin: F.Prino, Torino, prino@to.infn.it                     //
25 //                                                               //
26 ///////////////////////////////////////////////////////////////////
27
28 /*  $Id$   */
29
30 const Int_t AliITSOnlineSDDBase::fgkMaxCorr=63; // 6 but correction
31
32 ClassImp(AliITSOnlineSDDBase)
33 //______________________________________________________________________
34   AliITSOnlineSDDBase::AliITSOnlineSDDBase():AliITSOnlineSDD(),fNEvents(0),fMinBaseline(0.),fMaxBaseline(0.),fMinRawNoise(0.),fMaxRawNoise(0.),fNSigmaNoise(0.),fGoldenBaseline(0.),fLowThrFact(0.),fHighThrFact(0.)
35 {
36   // default constructor
37   Reset();
38   SetMinBaseline();
39   SetMaxBaseline();
40   SetMinRawNoise();
41   SetMaxRawNoise();
42   SetNSigmaNoise();
43   SetGoldenBaselineValue();
44   SetZeroSuppThresholds();
45 }
46 //______________________________________________________________________
47 AliITSOnlineSDDBase::AliITSOnlineSDDBase(Int_t nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fNEvents(0),fMinBaseline(0.),fMaxBaseline(0.),fMinRawNoise(0.),fMaxRawNoise(0.),fNSigmaNoise(0.),fGoldenBaseline(0.),fLowThrFact(0.),fHighThrFact(0.)
48 {
49   // default constructor
50   Reset();
51   SetMinBaseline();
52   SetMaxBaseline();
53   SetMinRawNoise();
54   SetMaxRawNoise();
55   SetNSigmaNoise();
56   SetGoldenBaselineValue();
57   SetZeroSuppThresholds();
58 }
59 //______________________________________________________________________
60 AliITSOnlineSDDBase::~AliITSOnlineSDDBase(){
61   // Destructor
62 }
63 //______________________________________________________________________
64 void AliITSOnlineSDDBase::Reset(){
65   //
66   fNEvents=0;
67   for(Int_t i=0;i<fgkNAnodes;i++){
68     fGoodAnode[i]=1;
69     fSumBaseline[i]=0.;
70     fSumRawNoise[i]=0.;
71     fSumCMN[i]=0.;
72   }
73 }
74 //______________________________________________________________________
75 void  AliITSOnlineSDDBase::ValidateAnodes(){
76   //
77   for(Int_t ian=0;ian<fgkNAnodes;ian++){
78     fGoodAnode[ian]=1;
79     Float_t basel=GetAnodeBaseline(ian);
80     Float_t rawn=GetAnodeRawNoise(ian);
81     Float_t ratio=0.;
82     if(rawn>0) ratio=basel/rawn;
83     if(basel>fMaxBaseline || basel<fMinBaseline) fGoodAnode[ian]=0;
84     else if(rawn>fMaxRawNoise || rawn<fMinRawNoise) fGoodAnode[ian]=0;
85     else if(rawn>fNSigmaNoise*CalcMeanRawNoise()) fGoodAnode[ian]=0;
86     else if(ratio<3.) fGoodAnode[ian]=0;
87   }
88 }
89
90 //______________________________________________________________________
91 void AliITSOnlineSDDBase::AddEvent(TH2F* hrawd){
92   // 
93   fNEvents++;
94   const Int_t kTimeBins=fLastGoodTB+1;
95   Float_t sum[fgkNAnodes];
96   for(Int_t ian=0;ian<fgkNAnodes;ian++){
97     Float_t sumQ=0.;
98     sum[ian]=0.;
99     Int_t cnt=0;
100     for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
101       Float_t cbin=hrawd->GetBinContent(itb+1,ian+1);
102       sum[ian]+=cbin;
103       sumQ+=cbin*cbin;
104       cnt++;
105     }
106     sum[ian]/=(Float_t)cnt;
107     sumQ/=(Float_t)cnt;
108     fSumBaseline[ian]+=sum[ian];
109     fSumRawNoise[ian]+=sumQ;
110   }
111   if(fNEvents==1) ValidateAnodes();
112
113   Float_t *cmnEven = new Float_t[kTimeBins];
114   Float_t *cmnOdd  = new Float_t[kTimeBins];
115   for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
116     Float_t sumEven=0., sumOdd=0.;
117     Int_t countEven=0,countOdd=0;
118     for(Int_t ian=0;ian<fgkNAnodes;ian+=2){
119       if(!fGoodAnode[ian]) continue;
120       sumEven+=hrawd->GetBinContent(itb+1,ian+1)-sum[ian];
121       countEven++;
122     }
123     for(Int_t ian=1;ian<fgkNAnodes;ian+=2){
124       if(!fGoodAnode[ian]) continue;
125       sumOdd+=hrawd->GetBinContent(itb+1,ian+1)-sum[ian];
126       countOdd++;
127     }
128     if(countEven>0) cmnEven[itb]=sumEven/countEven;
129     if(countOdd>0) cmnOdd[itb]=sumOdd/countOdd;
130   }
131   for(Int_t ian=0;ian<fgkNAnodes;ian++){
132     Float_t num=0.,den=0.;
133     if(!fGoodAnode[ian]) continue;
134     for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
135       Float_t cmnCoef=cmnOdd[itb];
136       if(ian%2==0) cmnCoef=cmnEven[itb];
137       num+=(hrawd->GetBinContent(itb+1,ian+1)-sum[ian])*cmnCoef;
138       den+=cmnCoef*cmnCoef;
139     }
140     if(den!=0) fSumCMN[ian]+=num/den;
141   }
142
143   delete [] cmnEven;
144   delete [] cmnOdd;
145 }
146 //______________________________________________________________________
147 void AliITSOnlineSDDBase::GetMinAndMaxBaseline(Float_t &basMin, Float_t &basMax) const {
148   // fills mininum and maximum baseline values
149   basMin=1008.;
150   basMax=0.;
151   for(Int_t ian=0;ian<fgkNAnodes;ian++){
152     if(!fGoodAnode[ian]) continue;
153     Float_t bas=GetAnodeBaseline(ian);
154     if(bas>0 && bas < basMin) basMin=bas;
155     if(bas>0 && bas > basMax) basMax=bas;
156   }
157 }
158 //______________________________________________________________________
159 Float_t AliITSOnlineSDDBase::GetMinimumBaseline() const {
160   // returns anode with minum baseline value in hybrid
161   Float_t basMin=1008.;
162   for(Int_t ian=0;ian<fgkNAnodes;ian++){
163     if(!fGoodAnode[ian]) continue;
164     Float_t bas=GetAnodeBaseline(ian);
165     if(bas>0 && bas < basMin) basMin=bas;
166   }
167   return basMin;
168 }
169 //______________________________________________________________________
170 Float_t AliITSOnlineSDDBase::CalcMeanRawNoise() const{
171   //
172   Float_t meanns=0.;
173   Int_t cnt=0;
174   for(Int_t ian=0;ian<fgkNAnodes;ian++){
175     if(!fGoodAnode[ian]) continue;  
176     meanns+=GetAnodeRawNoise(ian);
177     cnt++;
178   }
179   if(cnt>0) meanns/=(Float_t)cnt;
180   return meanns;
181 }
182 //______________________________________________________________________
183 void AliITSOnlineSDDBase::WriteToASCII(){
184   //
185   TString outfilnam;
186   Float_t basMin,basMax;
187   GetMinAndMaxBaseline(basMin,basMax);
188   Float_t finalVal=basMin;
189   if(basMin>fGoldenBaseline && basMax<fGoldenBaseline+fgkMaxCorr) finalVal=fGoldenBaseline;
190   if(basMax<basMin+fgkMaxCorr && basMax>fGoldenBaseline+fgkMaxCorr) finalVal=basMax-fgkMaxCorr;
191
192   Float_t avNoise=CalcMeanRawNoise();
193   Int_t thrL=(Int_t)(finalVal+fLowThrFact*avNoise+0.5);
194   Int_t thrH=(Int_t)(finalVal+fHighThrFact*avNoise+0.5);
195   if(CountGoodAnodes()==0) thrH=255;
196
197   outfilnam.Form("SDDbase_step1_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
198   FILE* outf=fopen(outfilnam.Data(),"w");
199   fprintf(outf,"%d\n",thrH);
200   fprintf(outf,"%d\n",thrL);
201   Float_t corrnoise=2.;
202   for(Int_t ian=0;ian<fgkNAnodes;ian++){
203     Float_t bas=GetAnodeBaseline(ian);
204     Int_t corr=(Int_t)(bas-finalVal+0.5);
205     if(corr>fgkMaxCorr) corr=fgkMaxCorr; // only 6 bits in jtag for correction
206     if(corr<0) corr=0; // avoid negative numbers
207     fprintf(outf,"%d %d %11.6f %d %d %11.6f %11.6f %11.6f\n",ian,IsAnodeGood(ian),GetAnodeBaseline(ian),(Int_t)finalVal,corr,GetAnodeRawNoise(ian),GetAnodeCommonMode(ian),corrnoise);
208   }
209   fclose(outf);  
210 }