Clean-up of SetDefaults methods in AliITS, AliITSDetTypeSim and AliITSDetTypeRec...
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDInjectors.cxx
CommitLineData
348f80b7 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 "AliITSOnlineSDDInjectors.h"
16#include <TH2F.h>
17#include <TGraphErrors.h>
18#include <TMath.h>
19
4c82df4c 20/* $Id$ */
21
348f80b7 22///////////////////////////////////////////////////////////////////
23// //
24// Implementation of the class used for SDD injector analysis //
25// Origin: F.Prino, Torino, prino@to.infn.it //
26// //
27///////////////////////////////////////////////////////////////////
28
29ClassImp(AliITSOnlineSDDInjectors)
30
31const Float_t AliITSOnlineSDDInjectors::fgkSaturation = 1008.;
9f026db8 32const Float_t AliITSOnlineSDDInjectors::fgkDefaultLThreshold = 5.;
33const Float_t AliITSOnlineSDDInjectors::fgkDefaultHThreshold = 25.;
34const Float_t AliITSOnlineSDDInjectors::fgkDefaultMinSpeed = 5.5;
35const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxSpeed = 9.0;
36const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxErr = 1.5;
37const Int_t AliITSOnlineSDDInjectors::fgkDefaultPolOrder = 3;
38const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMin[kInjLines] = {20,90,170};
39const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMax[kInjLines] = {50,160,240};
348f80b7 40
41//______________________________________________________________________
9f026db8 42AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors():AliITSOnlineSDD(),fHisto(),fTbZero(0.),fParam(),fPolOrder(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0)
348f80b7 43{
44 // default constructor
348f80b7 45 SetPositions();
9f026db8 46 SetDefaults();
348f80b7 47}
48//______________________________________________________________________
9f026db8 49AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors(Int_t nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fHisto(),fTbZero(0.),fParam(),fPolOrder(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0)
beb262b4 50{
51// standard constructor
348f80b7 52 SetPositions();
9f026db8 53 SetDefaults();
348f80b7 54}
55//______________________________________________________________________
56AliITSOnlineSDDInjectors::~AliITSOnlineSDDInjectors(){
57 // Destructor
19f1458a 58 // fHisto should not be deleted here because it points to an histo created
59 // by the external code which calls the method AnalyzeEvent
60 // if(fHisto) delete fHisto;
348f80b7 61 if(fParam) delete [] fParam;
62}
63//______________________________________________________________________
9f026db8 64void AliITSOnlineSDDInjectors::SetDefaults(){
65 for(Int_t i=0;i<kInjLines;i++)
66 SetInjLineRange(i,fgkDefaultTbMin[i],fgkDefaultTbMax[i]);
67 SetThresholds(fgkDefaultLThreshold,fgkDefaultHThreshold);
68 SetPolOrder(fgkDefaultPolOrder);
69 SetMinDriftSpeed(fgkDefaultMinSpeed);
70 SetMaxDriftSpeed(fgkDefaultMaxSpeed);
71 SetMaxDriftSpeedErr(fgkDefaultMaxErr);
72 SetFitLimits(1,kInjPads-2); // exclude first and last pad
73 SetPadStatusCutForFit();
74}
75//______________________________________________________________________
348f80b7 76void AliITSOnlineSDDInjectors::SetPositions(){
77 //
9f026db8 78 Float_t xLinFromCenterUm[kInjLines]={31860.,17460.,660.};
79 Float_t xAnodeFromCenterUm=35085;
80 for(Int_t i=0;i<kInjLines;i++){
81 fPosition[i]=xAnodeFromCenterUm-xLinFromCenterUm[i];
348f80b7 82 fPosition[i]/=10000.; // from microns to cm
83 }
84}
85//______________________________________________________________________
86void AliITSOnlineSDDInjectors::Reset(){
beb262b4 87 //
9f026db8 88 for(Int_t i=0;i<kInjPads;i++){
89 fDriftSpeed[i]=0.;
90 fDriftSpeedErr[i]=0.;
348f80b7 91 }
9f026db8 92 for(Int_t i=0;i<kInjPads;i++){
93 for(Int_t j=0;j<kInjLines;j++){
348f80b7 94 fGoodInj[i][j]=0;
95 fCentroid[i][j]=0.;
96 fRMSCentroid[i][j]=0.;
97 }
98 }
99}
100//______________________________________________________________________
101void AliITSOnlineSDDInjectors::AnalyzeEvent(TH2F* his){
102 //
103 Reset();
104 fHisto=his;
105 FindGoodInjectors();
106 FindCentroids();
107 CalcTimeBinZero();
9f026db8 108 for(Int_t j=0;j<kInjPads;j++) CalcDriftSpeed(j);
109 FitDriftSpeedVsAnode();
348f80b7 110}
111//______________________________________________________________________
9f026db8 112TGraphErrors* AliITSOnlineSDDInjectors::GetTimeVsDistGraph(Int_t jpad) const{
348f80b7 113 //
9f026db8 114 const Int_t kPts=kInjLines+1;
115 Float_t x[kPts],y[kPts],ex[kPts],ey[kPts];
348f80b7 116 x[0]=0.;
117 ex[0]=0.;
118 y[0]=fTbZero;
119 ey[0]=0.;
9f026db8 120 for(Int_t i=0;i<kInjLines;i++){
348f80b7 121 x[i+1]=fPosition[i];
122 ex[i+1]=0.;
9f026db8 123 y[i+1]=fCentroid[jpad][i];
124 ey[i+1]=fRMSCentroid[jpad][i];
348f80b7 125 }
126 TGraphErrors *g=new TGraphErrors(4,x,y,ex,ey);
127 return g;
128}
9f026db8 129
348f80b7 130//______________________________________________________________________
9f026db8 131TGraphErrors* AliITSOnlineSDDInjectors::GetDriftSpeedGraph() const{
348f80b7 132 //
348f80b7 133 Int_t ipt=0;
134 TGraphErrors *g=new TGraphErrors(0);
9f026db8 135 for(Int_t i=0;i<kInjPads;i++){
136 if(fDriftSpeed[i]>0){
137 g->SetPoint(ipt,GetAnodeNumber(i),fDriftSpeed[i]);
138 g->SetPointError(ipt,0,fDriftSpeedErr[i]);
348f80b7 139 ipt++;
140 }
141 }
142 return g;
143}
144//______________________________________________________________________
145void AliITSOnlineSDDInjectors::CalcTimeBinZero(){
beb262b4 146 //
348f80b7 147 Float_t tzero=0.,intCont=0.;
148 for(Int_t ian=0;ian<fgkNAnodes;ian++){
149 for(Int_t itb=1;itb<fTbMin[0];itb++){
150 Float_t cont=fHisto->GetBinContent(itb,ian+1);
9f026db8 151 if(cont>fLowThreshold){
348f80b7 152 tzero+=cont*float(itb);
153 intCont+=cont;
154 }
155 }
156 }
157 if(intCont>0) fTbZero=tzero/intCont;
158}
159//______________________________________________________________________
9f026db8 160void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
161 // fits the anode dependence of drift speed with a polynomial function
4ff6aa93 162 const Int_t kNn=fPolOrder+1;
163 Float_t **mat = new Float_t*[kNn];
164 for(Int_t i=0; i < kNn; i++) mat[i] = new Float_t[kNn];
165 Float_t *vect = new Float_t[kNn];
9f026db8 166
4ff6aa93 167 for(Int_t k1=0;k1<kNn;k1++){
348f80b7 168 vect[k1]=0;
4ff6aa93 169 for(Int_t k2=0;k2<kNn;k2++){
348f80b7 170 mat[k1][k2]=0;
348f80b7 171 }
172 }
4ff6aa93 173 for(Int_t k1=0;k1<kNn;k1++){
9f026db8 174 for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
175 Float_t x=(Float_t)GetAnodeNumber(jpad);
176 if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
177 vect[k1]+=fDriftSpeed[jpad]*TMath::Power(x,k1)/TMath::Power(fDriftSpeedErr[jpad],2);
178 for(Int_t k2=0;k2<kNn;k2++){
179
180 mat[k1][k2]+=TMath::Power(x,k1+k2)/TMath::Power(fDriftSpeedErr[jpad],2);
181 }
182 }
348f80b7 183 }
184 }
4ff6aa93 185 Int_t *iPivot = new Int_t[kNn];
186 Int_t *indxR = new Int_t[kNn];
187 Int_t *indxC = new Int_t[kNn];
188 for(Int_t i=0;i<kNn;i++) iPivot[i]=0;
348f80b7 189 Int_t iCol=-1,iRow=-1;
4ff6aa93 190 for(Int_t i=0;i<kNn;i++){
348f80b7 191 Float_t big=0.;
4ff6aa93 192 for(Int_t j=0;j<kNn;j++){
348f80b7 193 if(iPivot[j]!=1){
4ff6aa93 194 for(Int_t k=0;k<kNn;k++){
348f80b7 195 if(iPivot[k]==0){
196 if(TMath::Abs(mat[j][k])>=big){
197 big=TMath::Abs(mat[j][k]);
198 iRow=j;
199 iCol=k;
200 }
201 }
202 }
203 }
204 }
205 iPivot[iCol]++;
206 Float_t aux;
207 if(iRow!=iCol){
4ff6aa93 208 for(Int_t l=0;l<kNn;l++){
348f80b7 209 aux=mat[iRow][l];
210 mat[iRow][l]=mat[iCol][l];
211 mat[iCol][l]=aux;
212 }
213 aux=vect[iRow];
214 vect[iRow]=vect[iCol];
215 vect[iCol]=aux;
216 }
217 indxR[i]=iRow;
218 indxC[i]=iCol;
219 if(mat[iCol][iCol]==0) break;
220 Float_t pivinv=1./mat[iCol][iCol];
221 mat[iCol][iCol]=1;
4ff6aa93 222 for(Int_t l=0;l<kNn;l++) mat[iCol][l]*=pivinv;
348f80b7 223 vect[iCol]*=pivinv;
4ff6aa93 224 for(Int_t m=0;m<kNn;m++){
348f80b7 225 if(m!=iCol){
226 aux=mat[m][iCol];
227 mat[m][iCol]=0;
4ff6aa93 228 for(Int_t n=0;n<kNn;n++) mat[m][n]-=mat[iCol][n]*aux;
348f80b7 229 vect[m]-=vect[iCol]*aux;
230 }
231 }
232 }
233 delete [] iPivot;
234 delete [] indxR;
235 delete [] indxC;
236
237 if(fParam) delete [] fParam;
4ff6aa93 238 fParam=new Float_t[kNn];
239 for(Int_t i=0; i<kNn;i++)fParam[i]=vect[i];
348f80b7 240
4ff6aa93 241 for(Int_t i=0; i < kNn; i++) delete [] mat[i];
348f80b7 242 delete [] mat;
243 delete [] vect;
244}
245//______________________________________________________________________
9f026db8 246void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
348f80b7 247 //
248 Float_t sumY=0,sumX=0,sumXX=0,sumYY=0.,sumXY=0,sumWEI=0.;
249 Int_t npt=0;
9f026db8 250 Float_t y[kInjLines],ey[kInjLines];
348f80b7 251 Float_t tzero=0,erry=0;
9f026db8 252 for(Int_t i=0;i<kInjLines;i++){
253 y[i]=fCentroid[jpad][i];
254 ey[i]=fRMSCentroid[jpad][i];
348f80b7 255 }
9f026db8 256 for(Int_t i=0;i<kInjLines;i++){
257 if(fGoodInj[jpad][i] && ey[i]!=0){
348f80b7 258 sumY+=y[i]/ey[i]/ey[i];
259 sumX+=fPosition[i]/ey[i]/ey[i];
260 sumXX+=fPosition[i]*fPosition[i]/ey[i]/ey[i];
261 sumYY+=y[i]*y[i]/ey[i]/ey[i];
262 sumXY+=fPosition[i]*y[i]/ey[i]/ey[i];
263 sumWEI+=1./ey[i]/ey[i];
264 tzero=fTbZero/ey[i]/ey[i];
265 erry=ey[i];
266 npt++;
267 }
268 }
269 Float_t vel=0,evel=0;
270 if(npt>1){
271 Float_t slope=(sumWEI*sumXY-sumY*sumX)/(sumWEI*sumXX-sumX*sumX);
272 Float_t eslope=TMath::Sqrt(sumWEI/(sumWEI*sumXX-sumX*sumX));
53855521 273 if(slope!=0){
274 vel=1./slope*10000./25.;// micron/ns
275 evel=eslope/slope/slope*10000./25.;// micron/ns
276 }
348f80b7 277 }
278 if(npt==1){
279 Float_t slope=(sumY-tzero)/sumX;
280 Float_t eslope=erry/sumX;
53855521 281 if(slope!=0){
282 vel=1./slope*10000./25.;// micron/ns
283 evel=eslope/slope/slope*10000./25.;// micron/ns
284 }
348f80b7 285 }
9f026db8 286 if(vel>fMaxDriftSpeed||vel<fMinDriftSpeed || evel>fMaxDriftSpeedErr){
348f80b7 287 vel=0.;
288 evel=0.;
289 }
9f026db8 290 fDriftSpeed[jpad]=vel;
291 fDriftSpeedErr[jpad]=evel;
348f80b7 292}
293//______________________________________________________________________
9f026db8 294Int_t AliITSOnlineSDDInjectors::GetAnodeNumber(Int_t iInjPad) const{
beb262b4 295 //
348f80b7 296 Int_t ian=-1;
9f026db8 297 if(iInjPad>=kInjPads) return ian;
348f80b7 298 if(!fSide){
9f026db8 299 ian=iInjPad*8;
300 if(iInjPad==32) ian--;
348f80b7 301 }else{
9f026db8 302 ian=iInjPad*8-1;
303 if(iInjPad==0) ian=0;
348f80b7 304 }
305 return ian;
306}
84c6e6c0 307//______________________________________________________________________
9f026db8 308Int_t AliITSOnlineSDDInjectors::GetInjPadNumberFromAnode(Int_t nAnode) const{
84c6e6c0 309 //
9f026db8 310 Int_t iInjPad=-1;
84c6e6c0 311 if(!fSide){
9f026db8 312 if(nAnode%8==0) iInjPad=nAnode/8;
313 if(nAnode==255) iInjPad=32;
84c6e6c0 314 }else{
9f026db8 315 if(nAnode%8==7) iInjPad=1+nAnode/8;
316 if(nAnode==0) iInjPad=0;
84c6e6c0 317 }
9f026db8 318 if(nAnode>=256) iInjPad=-1;
319 return iInjPad;
84c6e6c0 320}
321//______________________________________________________________________
9f026db8 322Int_t AliITSOnlineSDDInjectors::GetInjPadStatus(Int_t jpad) const{
323 // returns an integer value with status of injector lines for given pad/anode
324 // status=7 --> 111 all injector are good
325 // status=6 --> 110 1st line (close to anodes) is bad, other two are good
326 // ....
327 // status=1 --> 001 only 1st line (close to anodes) good
328 // status=0 --> 000 all lines are bad
84c6e6c0 329 Int_t istatus=0;
9f026db8 330 if(jpad>=0 && jpad<kInjPads){
331 for(Int_t jlin=0;jlin<kInjLines;jlin++) istatus+=fGoodInj[jpad][jlin]<<jlin;
84c6e6c0 332 }
333 return istatus;
334}
348f80b7 335//______________________________________________________________________
336void AliITSOnlineSDDInjectors::FindGoodInjectors(){
337 //
9f026db8 338 for(Int_t jpad=0;jpad<kInjPads;jpad++){
339 Int_t ian=GetAnodeNumber(jpad);
340 for(Int_t jlin=0;jlin<kInjLines;jlin++){
341 for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
348f80b7 342 Float_t c1=fHisto->GetBinContent(jjj,ian+1);
343 Float_t c2=fHisto->GetBinContent(jjj+1,ian+1);
344 Float_t c3=fHisto->GetBinContent(jjj+2,ian+1);
9f026db8 345 if(c1>fLowThreshold && c2>fHighThreshold && c3>fLowThreshold){
346 fGoodInj[jpad][jlin]=1;
348f80b7 347 break;
348 }
349 }
348f80b7 350 }
351 }
352}
353//______________________________________________________________________
354void AliITSOnlineSDDInjectors::FindCentroids(){
355 //
9f026db8 356 for(Int_t jpad=0;jpad<kInjPads;jpad++){
357 Int_t ian=GetAnodeNumber(jpad);
358 for(Int_t jlin=0;jlin<kInjLines;jlin++){
359 if(!fGoodInj[jpad][jlin]) continue;
348f80b7 360 Float_t maxcont=0;
361 Int_t ilmax=-1;
9f026db8 362 for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
348f80b7 363 Float_t cont=fHisto->GetBinContent(jjj,ian+1);
364 if(cont>maxcont){
365 maxcont=cont;
366 ilmax=jjj;
367 }
368 }
369 Float_t intCont=0;
370 Int_t jjj=ilmax;
371 while(1){
372 Float_t cont=fHisto->GetBinContent(jjj,ian+1);
9f026db8 373 if(cont<fLowThreshold) break;
348f80b7 374 if(cont<fgkSaturation){
9f026db8 375 fCentroid[jpad][jlin]+=cont*(Float_t)jjj;
376 fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
348f80b7 377 intCont+=cont;
378 }
379 jjj--;
380 }
381 jjj=ilmax+1;
382 while(1){
383 Float_t cont=fHisto->GetBinContent(jjj,ian+1);
9f026db8 384 if(cont<fLowThreshold) break;
348f80b7 385 if(cont<fgkSaturation){
9f026db8 386 fCentroid[jpad][jlin]+=cont*float(jjj);
387 fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
348f80b7 388 intCont+=cont;
389 }
390 jjj++;
391 }
392 if(intCont>0){
9f026db8 393 fCentroid[jpad][jlin]/=intCont;
394 fRMSCentroid[jpad][jlin]=TMath::Sqrt(fRMSCentroid[jpad][jlin]/intCont-fCentroid[jpad][jlin]*fCentroid[jpad][jlin]);
348f80b7 395 }
396 else{
9f026db8 397 fCentroid[jpad][jlin]=0.;
398 fRMSCentroid[jpad][jlin]=0.;
399 fGoodInj[jpad][jlin]=0;
348f80b7 400 }
9f026db8 401 if(fRMSCentroid[jpad][jlin]==0) fGoodInj[jpad][jlin]=0;
348f80b7 402 }
403 }
404}
405//______________________________________________________________________
9f026db8 406void AliITSOnlineSDDInjectors::PrintInjectorStatus(){
348f80b7 407 //
9f026db8 408 for(Int_t jpad=0;jpad<kInjPads;jpad++){
409 printf("Line%d-Anode%d: %d %d %d\n",jpad,GetAnodeNumber(jpad),fGoodInj[jpad][0],fGoodInj[jpad][1],fGoodInj[jpad][2]);
348f80b7 410 }
411}
412//______________________________________________________________________
413void AliITSOnlineSDDInjectors::PrintCentroids(){
414 //
9f026db8 415 for(Int_t jpad=0;jpad<kInjPads;jpad++){
416 printf("Line%d-Anode%d: %f+-%f %f+-%f %f+-%f\n",jpad,GetAnodeNumber(jpad),fCentroid[jpad][0],fRMSCentroid[jpad][0],fCentroid[jpad][1],fRMSCentroid[jpad][1],fCentroid[jpad][2],fRMSCentroid[jpad][2]);
348f80b7 417 }
418}
419//______________________________________________________________________
4c82df4c 420void AliITSOnlineSDDInjectors::WriteToASCII(Int_t evNumb, UInt_t timeStamp, Int_t optAppend){
348f80b7 421 //
422 Char_t outfilnam[100];
979b5a5f 423 sprintf(outfilnam,"SDDinj_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);
4c82df4c 424 FILE* outf;
18da6e54 425 if(optAppend==0){
426 outf=fopen(outfilnam,"w");
427 fprintf(outf,"%d\n",fPolOrder);
428 }
4c82df4c 429 else outf=fopen(outfilnam,"a");
430 fprintf(outf,"%d %d ",evNumb,timeStamp);
348f80b7 431 for(Int_t ic=0;ic<fPolOrder+1;ic++){
432 fprintf(outf,"%G ",fParam[ic]);
433 }
434 fprintf(outf,"\n");
435 fclose(outf);
436}