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