Fix for the case of non-existent calibration files
[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),fLowThreshold(0),fHighThreshold(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 nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fNEvents(0),fLowThreshold(0),fHighThreshold(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   // Reset counters
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   TString basfilnam;
71   basfilnam.Form("SDDbase_step1_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
72   FILE* basf=fopen(basfilnam.Data(),"r");
73   if(basf==0){
74     AliWarning(Form("Baseline file not present (ddl %d  carlos %d side %d, Set all baselines to 50\n",fDDL,fCarlos,fSide));
75     for(Int_t ian=0;ian<fgkNAnodes;ian++){ 
76       fBaseline[ian]=50.;
77       fEqBaseline[ian]=50;
78       fOffsetBaseline[ian]=0;
79       fGoodAnode[ian]=1;
80     }
81     return;
82   }
83   Int_t check = fscanf(basf,"%d\n",&fHighThreshold);
84   if(check<1)AliError("Error while reading file with baselines");
85   check = fscanf(basf,"%d\n",&fLowThreshold);
86   if(check<1)AliError("Error while reading file with baselines");
87   Int_t n,ok,eqbase,offbase;
88   Float_t base,rms,cmn,corrnoi;
89   for(Int_t ian=0;ian<fgkNAnodes;ian++){
90     check = fscanf(basf,"%d %d %f %d %d %f %f %f\n",&n,&ok,&base,&eqbase,&offbase,&rms,&cmn,&corrnoi);
91     if(check<1)AliError("Error while reading file with baselines");
92     fGoodAnode[ian]=ok;
93     fBaseline[ian]=base;
94     fEqBaseline[ian]=eqbase;
95     fOffsetBaseline[ian]=offbase;
96     fRawNoise[ian]=rms;
97     fCMN[ian]=cmn;
98   }
99   fclose(basf);
100 }
101 //______________________________________________________________________
102 void  AliITSOnlineSDDCMN::ValidateAnodes(){
103   // Tag good/bad anodes
104   for(Int_t ian=0;ian<fgkNAnodes;ian++){
105     if(!fGoodAnode[ian]) continue;
106     if(GetAnodeCorrNoise(ian)>fMaxCorrNoise || GetAnodeCorrNoise(ian)<fMinCorrNoise) fGoodAnode[ian]=0;
107     if(GetAnodeCorrNoise(ian)>fNSigmaNoise*CalcMeanNoise()) fGoodAnode[ian]=0;
108   }
109 }
110
111 //______________________________________________________________________
112 TH2F* AliITSOnlineSDDCMN::GetCleanEvent(const TH2F* hrawd) const {
113   // Fills an histogram with counts corrected for common mode noise
114
115   TH2F* hcorrd=new TH2F("hcorrd","",hrawd->GetNbinsX(),hrawd->GetXaxis()->GetXmin(),hrawd->GetXaxis()->GetXmax(),hrawd->GetNbinsY(),hrawd->GetYaxis()->GetXmin(),hrawd->GetYaxis()->GetXmax());
116   for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
117     Float_t sumEven=0., sumOdd=0.;
118     Int_t countEven=0, countOdd=0;
119     for(Int_t ian=0;ian<fgkNAnodes;ian+=2){
120       if(!fGoodAnode[ian]) continue;
121       sumEven+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
122       countEven++;
123     }
124     for(Int_t ian=1;ian<fgkNAnodes;ian+=2){
125       if(!fGoodAnode[ian]) continue;
126       sumOdd+=hrawd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
127       countOdd++;
128     }
129     for(Int_t ian=0;ian<fgkNAnodes;ian++){
130       if(!fGoodAnode[ian]) continue;
131       Float_t meanN;
132       if(ian%2==0) meanN=sumEven/(Float_t)countEven;
133       else meanN=sumOdd/(Float_t)countOdd;
134       Float_t cntCorr=hrawd->GetBinContent(itb+1,ian+1)-fCMN[ian]*meanN;
135       hcorrd->SetBinContent(itb+1,ian+1,cntCorr);
136     }
137   }
138   return hcorrd;
139 }
140 //______________________________________________________________________
141 void AliITSOnlineSDDCMN::AddEvent(TH2F* hrawd){
142   // analyzes one event and adds its ontribution to the various counters
143
144   fNEvents++;
145   TH2F* hcorrd=GetCleanEvent(hrawd);
146
147   for(Int_t ian=0;ian<fgkNAnodes;ian++){
148     if(!fGoodAnode[ian]) continue;
149     Float_t sumQ=0.;
150     Int_t cnt=0;
151     for(Int_t itb=fFirstGoodTB;itb<=fLastGoodTB;itb++){
152       Float_t cntdiff=hcorrd->GetBinContent(itb+1,ian+1)-fBaseline[ian];
153       sumQ+=cntdiff*cntdiff;
154       cnt++;    
155     }
156     if(cnt != 0)fSumCorrNoise[ian]+=TMath::Sqrt(sumQ/(Float_t)cnt);
157   }
158   delete hcorrd;
159 }
160 //______________________________________________________________________
161 Float_t AliITSOnlineSDDCMN::CalcMeanNoise() const{
162   // compute average noise
163
164   Float_t meanns=0.;
165   Int_t cnt=0;
166   for(Int_t ian=0;ian<fgkNAnodes;ian++){
167     if(!fGoodAnode[ian]) continue;  
168     meanns+=GetAnodeCorrNoise(ian);
169     cnt++;
170   }
171   if(cnt>0) meanns/=(Float_t)cnt;
172   return meanns;
173 }
174 //______________________________________________________________________
175 void AliITSOnlineSDDCMN::WriteToASCII(){
176   // writes parameters of each channel into an ASCII file 
177   // to be then read by the PULSER DA (AliITSOnlineSDDTP)
178
179   TString outfilnam;
180   outfilnam.Form("SDDbase_step2_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
181   FILE* outf=fopen(outfilnam.Data(),"w");
182   fprintf(outf,"%d\n",fHighThreshold);
183   fprintf(outf,"%d\n",fLowThreshold);
184   for(Int_t ian=0;ian<fgkNAnodes;ian++){
185     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));
186   }
187   fclose(outf);  
188 }
189
190 //______________________________________________________________________
191 TH1F* AliITSOnlineSDDCMN::GetBaselineAnodeHisto() const {
192   // produce histogram with baseline vs. anode number
193   TString hisnam;  
194   hisnam.Form("hbase%02dc%02ds%d",fDDL,fCarlos,fSide);
195   TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
196   for(Int_t ian=0;ian<fgkNAnodes;ian++){
197     h->SetBinContent(ian+1,GetAnodeBaseline(ian));
198   }
199   return h;
200 }
201 //______________________________________________________________________
202 TH1F* AliITSOnlineSDDCMN::GetRawNoiseAnodeHisto() const {
203   // produce histogram with raw noise vs. anode number
204   TString hisnam;  
205   hisnam.Form("hnois%02dc%02ds%d",fDDL,fCarlos,fSide);
206   TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
207   for(Int_t ian=0;ian<fgkNAnodes;ian++){
208     h->SetBinContent(ian+1,GetAnodeRawNoise(ian));
209   }
210   return h;
211 }
212 //______________________________________________________________________
213 TH1F* AliITSOnlineSDDCMN::GetCorrNoiseAnodeHisto() const {
214   // produce histogram with corrected noise vs. anode number
215   TString hisnam;  
216   hisnam.Form("hcorn%02dc%02ds%d",fDDL,fCarlos,fSide);
217   TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
218   for(Int_t ian=0;ian<fgkNAnodes;ian++){
219     h->SetBinContent(ian+1,GetAnodeCorrNoise(ian));
220   }
221   return h;
222 }
223 //______________________________________________________________________
224 TH1F* AliITSOnlineSDDCMN::GetCMNCoefAnodeHisto() const {
225   // produce histogram with coefficients for common mode noise subtraction
226   TString hisnam;  
227   hisnam.Form("hcmn%02dc%02ds%d",fDDL,fCarlos,fSide);
228   TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
229   for(Int_t ian=0;ian<fgkNAnodes;ian++){
230     h->SetBinContent(ian+1,GetAnodeCommonMode(ian));
231   }
232   return h;
233 }
234 //______________________________________________________________________
235 TH1F* AliITSOnlineSDDCMN::GetStatusAnodeHisto() const {
236   // produce histogram with status bit of each anode
237   TString hisnam;  
238   hisnam.Form("hgood%02dc%02ds%d",fDDL,fCarlos,fSide);
239   TH1F* h=new TH1F(hisnam.Data(),"",256,-0.5,255.5);
240   for(Int_t ian=0;ian<fgkNAnodes;ian++){
241     h->SetBinContent(ian+1,float(IsAnodeGood(ian)));
242   }
243   return h;
244 }
245 //______________________________________________________________________
246 TH1F* AliITSOnlineSDDCMN::GetBaselineHisto() const {
247   // produce histogram with baseline distribution
248   TString hisnam;  
249   hisnam.Form("hdbd%02dc%02ds%d",fDDL,fCarlos,fSide);
250   TH1F* h=new TH1F(hisnam.Data(),"",100,0.,150.);
251   for(Int_t ian=0;ian<fgkNAnodes;ian++){
252     h->Fill(GetAnodeBaseline(ian));
253   }
254   return h;
255 }
256 //______________________________________________________________________
257 TH1F* AliITSOnlineSDDCMN::GetRawNoiseHisto() const {
258   // produce histogram with raw noise distribution
259   TString hisnam;  
260   hisnam.Form("hdnd%02dc%02ds%d",fDDL,fCarlos,fSide);
261   TH1F* h=new TH1F(hisnam.Data(),"",100,0.,8.);
262   for(Int_t ian=0;ian<fgkNAnodes;ian++){
263     h->Fill(GetAnodeRawNoise(ian));
264   }
265   return h;
266 }
267 //______________________________________________________________________
268 TH1F* AliITSOnlineSDDCMN::GetCorrNoiseHisto() const {
269   // produce histogram with corrected noise distribution
270   TString hisnam;  
271   hisnam.Form("hdcd%02dc%02ds%d",fDDL,fCarlos,fSide);
272   TH1F* h=new TH1F(hisnam.Data(),"",100,0.,8.);
273   for(Int_t ian=0;ian<fgkNAnodes;ian++){
274     h->Fill(GetAnodeCorrNoise(ian));
275   }
276   return h;
277 }
278 //______________________________________________________________________
279 Bool_t AliITSOnlineSDDCMN::WriteToROOT(TFile *fil){
280   // writes output into a root file
281   if(fil==0){ 
282     AliWarning("Invalid pointer to ROOT file");
283     return kFALSE;    
284   }
285   TString hisnam;
286   fil->cd();
287   hisnam.Form("hgood%02dc%02ds%d",fDDL,fCarlos,fSide);
288   TH1F hgood(hisnam.Data(),"",256,-0.5,255.5);
289   hisnam.Form("hbase%02dc%02ds%d",fDDL,fCarlos,fSide);
290   TH1F hbase(hisnam.Data(),"",256,-0.5,255.5);
291   hisnam.Form("hnois%02dc%02ds%d",fDDL,fCarlos,fSide);
292   TH1F hnois(hisnam.Data(),"",256,-0.5,255.5);
293   hisnam.Form("hcmn%02dc%02ds%d",fDDL,fCarlos,fSide);
294   TH1F hcmn(hisnam.Data(),"",256,-0.5,255.5);
295   hisnam.Form("hcorn%02dc%02ds%d",fDDL,fCarlos,fSide);
296   TH1F hcorn(hisnam.Data(),"",256,-0.5,255.5);
297   for(Int_t ian=0;ian<fgkNAnodes;ian++){
298     hgood.SetBinContent(ian+1,float(IsAnodeGood(ian)));
299     hbase.SetBinContent(ian+1,GetAnodeBaseline(ian));
300     hnois.SetBinContent(ian+1,GetAnodeRawNoise(ian));
301     hcmn.SetBinContent(ian+1,GetAnodeCommonMode(ian));
302     hcorn.SetBinContent(ian+1,GetAnodeCorrNoise(ian));
303   }
304   hgood.Write();
305   hbase.Write();
306   hnois.Write();
307   hcmn.Write();
308   hcorn.Write();
309   return kTRUE;
310 }