New methods for retrieving QA histograms + coding conventions (F.Prino)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDCMN.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 <TFile.h>
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
31 ClassImp(AliITSOnlineSDDCMN)
32 //______________________________________________________________________
33   AliITSOnlineSDDCMN::AliITSOnlineSDDCMN():AliITSOnlineSDD(),fNEvents(0),fMinCorrNoise(0.),fMaxCorrNoise(0.),fNSigmaNoise(0.)
34 {
35   // default constructor
36   Reset();
37   SetMinNoise();
38   SetMaxNoise();
39   SetNSigmaNoise();
40 }
41 //______________________________________________________________________
42   AliITSOnlineSDDCMN::AliITSOnlineSDDCMN(Int_t mod, Int_t sid):AliITSOnlineSDD(mod,sid),fNEvents(0),fMinCorrNoise(0.),fMaxCorrNoise(0.),fNSigmaNoise(0.)
43 {
44   // default constructor
45   Reset();
46   SetMinNoise();
47   SetMaxNoise();
48   SetNSigmaNoise();
49 }
50 //______________________________________________________________________
51 AliITSOnlineSDDCMN::~AliITSOnlineSDDCMN(){
52   // Destructor
53 }
54 //______________________________________________________________________
55 void AliITSOnlineSDDCMN::Reset(){
56   //
57   fNEvents=0;
58   for(Int_t i=0;i<fgkNAnodes;i++){
59     fGoodAnode[i]=1;
60     fBaseline[i]=0.;
61     fRawNoise[i]=0.;
62     fCMN[i]=0.;
63     fSumCorrNoise[i]=0.;
64   }
65   ReadBaselines();
66 }
67 //______________________________________________________________________
68 void AliITSOnlineSDDCMN::ReadBaselines(){
69   // assume baselines and good anodes are taken from previous run
70   Char_t basfilnam[100];
71   sprintf(basfilnam,"SDDbase_step1_mod%03d_sid%d.data",fModuleId,fSide);
72   FILE* basf=fopen(basfilnam,"r");
73   if(basf==0){
74     AliWarning("Baselinefile not present, Set all baselines to 50\n");
75     for(Int_t ian=0;ian<fgkNAnodes;ian++){ 
76       fBaseline[ian]=50.;
77       fGoodAnode[ian]=1;
78     }
79     return;
80   }
81   Int_t n,ok;
82   Float_t base,rms,cmn,corrnoi;
83   for(Int_t ian=0;ian<fgkNAnodes;ian++){
84     fscanf(basf,"%d %d %f %f %f %f\n",&n,&ok,&base,&rms,&cmn,&corrnoi);
85     fGoodAnode[ian]=ok;
86     fBaseline[ian]=base;
87     fRawNoise[ian]=rms;
88     fCMN[ian]=cmn;
89   }
90   fclose(basf);
91 }
92 //______________________________________________________________________
93 void  AliITSOnlineSDDCMN::ValidateAnodes(){
94   //
95   for(Int_t ian=0;ian<fgkNAnodes;ian++){
96     if(!fGoodAnode[ian]) continue;
97     if(GetAnodeCorrNoise(ian)>fMaxCorrNoise || GetAnodeCorrNoise(ian)<fMinCorrNoise) fGoodAnode[ian]=0;
98     if(GetAnodeCorrNoise(ian)>fNSigmaNoise*CalcMeanNoise()) fGoodAnode[ian]=0;
99   }
100 }
101
102 //______________________________________________________________________
103 void AliITSOnlineSDDCMN::AddEvent(TH2F* hrawd){
104   // 
105   fNEvents++;
106   const Int_t kTimeBins=fLastGoodTB-fFirstGoodTB+1;
107   TH2F* hcorrd=new TH2F("hcorrd","",hrawd->GetNbinsX(),hrawd->GetXaxis()->GetXmin(),hrawd->GetXaxis()->GetXmax(),hrawd->GetNbinsY(),hrawd->GetYaxis()->GetXmin(),hrawd->GetYaxis()->GetXmax());
108   for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
109     Float_t sumEven=0., sumOdd=0.;
110     Int_t countEven=0, countOdd=0;
111     for(Int_t ian=0;ian<fgkNAnodes;ian+=2){
112       if(!fGoodAnode[ian]) continue;
113       sumEven+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
114       countEven++;
115     }
116     for(Int_t ian=1;ian<fgkNAnodes;ian+=2){
117       if(!fGoodAnode[ian]) continue;
118       sumOdd+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
119       countOdd++;
120     }
121     for(Int_t ian=0;ian<fgkNAnodes;ian++){
122       if(!fGoodAnode[ian]) continue;
123       Float_t meanN;
124       if(ian%2==0) meanN=sumEven/(Float_t)countEven;
125       else meanN=sumOdd/(Float_t)countOdd;
126       Float_t cntCorr=hrawd->GetBinContent(itb+1,ian+1)-fCMN[ian]*meanN;
127       hcorrd->SetBinContent(itb+1,ian+1,cntCorr);
128     }
129   }
130
131   for(Int_t ian=0;ian<fgkNAnodes;ian++){
132     if(!fGoodAnode[ian]) continue;
133     Float_t sumQ=0.;
134     for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
135       sumQ+=TMath::Power(hcorrd->GetBinContent(itb+1,ian+1)-fBaseline[ian],2);      
136     }
137     fSumCorrNoise[ian]+=TMath::Sqrt(sumQ/(Float_t)kTimeBins);
138   }
139   delete hcorrd;
140 }
141 //______________________________________________________________________
142 Float_t AliITSOnlineSDDCMN::CalcMeanNoise() const{
143   //
144   Float_t meanns=0.;
145   Int_t cnt=0;
146   for(Int_t ian=0;ian<fgkNAnodes;ian++){
147     if(!fGoodAnode[ian]) continue;  
148     meanns+=GetAnodeCorrNoise(ian);
149     cnt++;
150   }
151   if(cnt>0) meanns/=(Float_t)cnt;
152   return meanns;
153 }
154 //______________________________________________________________________
155 void AliITSOnlineSDDCMN::WriteToASCII(){
156   //
157   Char_t outfilnam[100];
158   sprintf(outfilnam,"SDDbase_step2_mod%03d_sid%d.data",fModuleId,fSide);
159   FILE* outf=fopen(outfilnam,"w");
160   for(Int_t ian=0;ian<fgkNAnodes;ian++){
161     fprintf(outf,"%d %d %8.3f %8.3f %8.3f %8.3f\n",ian,IsAnodeGood(ian),GetAnodeBaseline(ian),GetAnodeRawNoise(ian),GetAnodeCommonMode(ian),GetAnodeCorrNoise(ian));
162   }
163   fclose(outf);  
164 }
165
166 //______________________________________________________________________
167 TH1F* AliITSOnlineSDDCMN::GetBaselineAnodeHisto() const {
168   //
169   Char_t hisnam[20];  
170   sprintf(hisnam,"hb%03ds%d",fModuleId,fSide);
171   TH1F* h=new TH1F(hisnam,"",256,-0.5,255.5);
172   for(Int_t ian=0;ian<fgkNAnodes;ian++){
173     h->SetBinContent(ian+1,GetAnodeBaseline(ian));
174   }
175   return h;
176 }
177 //______________________________________________________________________
178 TH1F* AliITSOnlineSDDCMN::GetRawNoiseAnodeHisto() const {
179   //
180   Char_t hisnam[20];  
181   sprintf(hisnam,"hn%03ds%d",fModuleId,fSide);
182   TH1F* h=new TH1F(hisnam,"",256,-0.5,255.5);
183   for(Int_t ian=0;ian<fgkNAnodes;ian++){
184     h->SetBinContent(ian+1,GetAnodeRawNoise(ian));
185   }
186   return h;
187 }
188 //______________________________________________________________________
189 TH1F* AliITSOnlineSDDCMN::GetCorrNoiseAnodeHisto() const {
190   //
191   Char_t hisnam[20];  
192   sprintf(hisnam,"hc%03ds%d",fModuleId,fSide);
193   TH1F* h=new TH1F(hisnam,"",256,-0.5,255.5);
194   for(Int_t ian=0;ian<fgkNAnodes;ian++){
195     h->SetBinContent(ian+1,GetAnodeCorrNoise(ian));
196   }
197   return h;
198 }
199 //______________________________________________________________________
200 TH1F* AliITSOnlineSDDCMN::GetBaselineHisto() const {
201   //
202   Char_t hisnam[20];  
203   sprintf(hisnam,"hdb%03ds%d",fModuleId,fSide);
204   TH1F* h=new TH1F(hisnam,"",100,0.,150.);
205   for(Int_t ian=0;ian<fgkNAnodes;ian++){
206     h->Fill(GetAnodeBaseline(ian));
207   }
208   return h;
209 }
210 //______________________________________________________________________
211 TH1F* AliITSOnlineSDDCMN::GetRawNoiseHisto() const {
212   //
213   Char_t hisnam[20];  
214   sprintf(hisnam,"hdn%03ds%d",fModuleId,fSide);
215   TH1F* h=new TH1F(hisnam,"",100,0.,8.);
216   for(Int_t ian=0;ian<fgkNAnodes;ian++){
217     h->Fill(GetAnodeRawNoise(ian));
218   }
219   return h;
220 }
221 //______________________________________________________________________
222 TH1F* AliITSOnlineSDDCMN::GetCorrNoiseHisto() const {
223   //
224   Char_t hisnam[20];  
225   sprintf(hisnam,"hdc%03ds%d",fModuleId,fSide);
226   TH1F* h=new TH1F(hisnam,"",100,0.,8.);
227   for(Int_t ian=0;ian<fgkNAnodes;ian++){
228     h->Fill(GetAnodeCorrNoise(ian));
229   }
230   return h;
231 }
232 //______________________________________________________________________
233 Bool_t AliITSOnlineSDDCMN::WriteToROOT(TFile *fil){
234   //
235   if(fil==0){ 
236     AliWarning("Invalid pointer to ROOT file");
237     return kFALSE;    
238   }
239   Char_t hisnam[20];
240   fil->cd();
241   sprintf(hisnam,"hgood%03ds%d",fModuleId,fSide);
242   TH1F hgood(hisnam,"",256,-0.5,255.5);
243   sprintf(hisnam,"hbase%03ds%d",fModuleId,fSide);
244   TH1F hbase(hisnam,"",256,-0.5,255.5);
245   sprintf(hisnam,"hnois%03ds%d",fModuleId,fSide);
246   TH1F hnois(hisnam,"",256,-0.5,255.5);
247   sprintf(hisnam,"hcmn%03ds%d",fModuleId,fSide);
248   TH1F hcmn(hisnam,"",256,-0.5,255.5);
249   sprintf(hisnam,"hcorn%03ds%d",fModuleId,fSide);
250   TH1F hcorn(hisnam,"",256,-0.5,255.5);
251   for(Int_t ian=0;ian<fgkNAnodes;ian++){
252     hgood.SetBinContent(ian+1,float(IsAnodeGood(ian)));
253     hbase.SetBinContent(ian+1,GetAnodeBaseline(ian));
254     hnois.SetBinContent(ian+1,GetAnodeRawNoise(ian));
255     hcmn.SetBinContent(ian+1,GetAnodeCommonMode(ian));
256     hcorn.SetBinContent(ian+1,GetAnodeCorrNoise(ian));
257   }
258   hgood.Write();
259   hbase.Write();
260   hnois.Write();
261   hcmn.Write();
262   hcorn.Write();
263   return kTRUE;
264 }