gcc 4.3 warnings fixed
[u/mrichter/AliRoot.git] / ITS / AliITSClusterFinderSPD.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 ////////////////////////////////////////////////////////////////////////////
16 //  Cluster finder                                                       ///
17 //  for Silicon pixels                                                    // 
18 //                                                                        //
19 ////////////////////////////////////////////////////////////////////////////        
20
21 #include "AliITSClusterFinderSPD.h"
22 #include "AliITSDetTypeRec.h"
23 #include "AliITSRawClusterSPD.h"
24 #include "AliITSRecPoint.h"
25 #include "AliITSdigitSPD.h"
26 #include "AliITSsegmentationSPD.h"
27 #include "AliITSgeom.h"
28 #include "AliLog.h"
29
30 //#define DEBUG
31
32 ClassImp(AliITSClusterFinderSPD)
33
34 //______________________________________________________________________
35 AliITSClusterFinderSPD::AliITSClusterFinderSPD():AliITSClusterFinder(),
36 fDz(0.0),
37 fDx(0.0),
38 fMinNCells(0){
39     // constructor
40 }
41 //----------------------------------------------------------
42 AliITSClusterFinderSPD::AliITSClusterFinderSPD(AliITSDetTypeRec* dettyp):
43 AliITSClusterFinder(dettyp),
44 fDz(0.0),
45 fDx(0.0),
46 fMinNCells(0){
47     // constructor
48
49     SetDx();
50     SetDz();
51 }
52 //----------------------------------------------------------
53 AliITSClusterFinderSPD::AliITSClusterFinderSPD(AliITSDetTypeRec* dettyp,
54                                                TClonesArray *digits,
55                                                TClonesArray *recp):
56 AliITSClusterFinder(dettyp,digits),
57 fDz(0.0),
58 fDx(0.0),
59 fMinNCells(0){
60     // constructor
61
62     SetDigits(digits);
63     SetClusters(recp);
64     SetDx();
65     SetDz();
66 }
67 /*
68 //______________________________________________________________________
69 AliITSClusterFinderSPD::AliITSClusterFinderSPD(const AliITSClusterFinderSPD &source) : AliITSClusterFinder(source) {
70   // Copy constructor
71   // Copies are not allowed. The method is protected to avoid misuse.
72   Fatal("AliITSClusterFinderSPD","Copy constructor not allowed\n");
73 }
74 */
75 //______________________________________________________________________
76 //AliITSClusterFinderSPD& AliITSClusterFinderSPD::operator=(const AliITSClusterFinderSPD& /* source */){
77   // Assignment operator
78   // Assignment is not allowed. The method is protected to avoid misuse.
79   //Fatal("= operator","Assignment operator not allowed\n");
80   //return *this;
81 //}
82 //______________________________________________________________________
83 void AliITSClusterFinderSPD::FindRawClusters(Int_t module){   
84     // input of Cluster Finder
85     Int_t   digitcount  = 0;
86     Int_t   numberd     = 100000;
87     Int_t   *digx       = new Int_t[numberd];
88     Int_t   *digz       = new Int_t[numberd];
89     Int_t   *digtr1     = new Int_t[numberd];
90     Int_t   *digtr2     = new Int_t[numberd];
91     Int_t   *digtr3     = new Int_t[numberd];
92     Int_t   *digtr4     = new Int_t[numberd];
93     //  output of Cluster Finder    
94     Int_t   numberc     = 10000;
95     Double_t *xcenterl   = new Double_t[numberc];
96     Double_t *zcenterl   = new Double_t[numberc];
97     Double_t *errxcenter = new Double_t[numberc];
98     Double_t *errzcenter = new Double_t[numberc];
99     Int_t   *tr1clus    = new Int_t[numberc];
100     Int_t   *tr2clus    = new Int_t[numberc];
101     Int_t   *tr3clus    = new Int_t[numberc];
102     Int_t   nclus;
103
104     SetModule(module);
105     digitcount=0;
106     Int_t ndigits = Digits()->GetEntriesFast();  
107     if (!ndigits) return;
108
109     AliITSdigitSPD *dig;
110     Int_t ndig=0,i;
111     /*
112     AliDebug(4," ");
113         scanf("%d",&ndig);
114     */
115     for(ndig=0; ndig<ndigits; ndig++) {
116         dig= (AliITSdigitSPD*)GetDigit(ndig);
117         digx[digitcount] = dig->GetCoord2()+1;  //starts at 1
118         digz[digitcount] = dig->GetCoord1()+1;  //starts at 1
119         digtr1[digitcount] = dig->GetTrack(0);
120         digtr2[digitcount] = -3;
121         digtr3[digitcount] = -3;
122         AliDebug(5,Form("digtr1[%d]=%d fTracks[%d]=%d: ",
123                         digitcount,digtr1[digitcount],0,dig->GetTrack(0)));
124         i=1;
125         while(digtr1[digitcount]==dig->GetTrack(i) && i<dig->GetNTracks()) i++;
126         AliDebug(5,Form(" fTracks[%d]=%d",i,dig->GetTrack(i)));
127         if(i<dig->GetNTracks()){
128             digtr2[digitcount] = dig->GetTrack(i);
129             AliDebug(5,Form("digtr2[%d]=%d: ",digitcount,digtr2[digitcount]));
130             while((digtr1[digitcount]==dig->GetTrack(i) || 
131                    digtr2[digitcount]==dig->GetTrack(i))&&
132                   i<=dig->GetNTracks()) i++;
133             if(i<dig->GetNTracks()) digtr3[digitcount] = dig->GetTrack(i);
134             AliDebug(5,Form(" fTracks[%d]=%d digtr3[%d]=%d",
135                             i,i<dig->GetNTracks()?dig->GetTrack(i):-1,digitcount,digtr3[digitcount]));
136         } // end if
137     //        if(GetDebug(4)) cout<<endl;
138         digtr4[digitcount] = dig->GetSignal();
139         digitcount++;
140     } // end for ndig
141     ClusterFinder(digitcount,digx,digz,digtr1,digtr2,digtr3,digtr4,
142                   nclus,xcenterl,zcenterl,errxcenter,errzcenter,
143                   tr1clus, tr2clus, tr3clus);
144     DigitToPoint(nclus,xcenterl,zcenterl,errxcenter,errzcenter,
145                  tr1clus, tr2clus, tr3clus);
146     delete[] digx;
147     delete[] digz;
148     delete[] digtr1;
149     delete[] digtr2;
150     delete[] digtr3;
151     delete[] digtr4;
152     delete[] xcenterl;
153     delete[] zcenterl;
154     delete[] errxcenter;
155     delete[] errzcenter;
156     delete[] tr1clus;
157     delete[] tr2clus;
158     delete[] tr3clus;
159 }
160 //----------------------------------------------------------------------
161 void AliITSClusterFinderSPD::ClusterFinder(Int_t ndigits,Int_t digx[],
162                                            Int_t digz[],Int_t digtr1[],
163                                            Int_t digtr2[],Int_t digtr3[],
164                                            Int_t digtr4[],Int_t &nclus,
165                                            Double_t xcenter[],Double_t zcenter[],
166                                            Double_t errxcenter[],
167                                            Double_t errzcenter[],
168                                            Int_t tr1clus[],Int_t tr2clus[],
169                                            Int_t tr3clus[]){
170     // Search for clusters of fired pixels (digits). Two digits are linked
171     // inside a cluster if they are countiguous both in row or column
172     // direction.  Diagonal digits are not linked.
173     // xcenter, ycenter, zcenter are the coordinates of the center
174     // of each found cluster, calculated from the averaging the corresponding
175     // coordinate of the center of the linked digits. The coordinates are
176     // given in the local reference sistem. 
177     // errxcenter, errycenter, errzcenter are the errors associated to
178     // the corresponding average.
179     Int_t   if1, min, max, nd;
180     Int_t   x1, z1, t1, t2, t3, t4;
181     Int_t   ndx, ndz, ndxmin=0, ndxmax=0, ndzmin=0, ndzmax=0;
182     Double_t dx, dz; 
183     Int_t   i,k,ipos=0;
184     Float_t xdum, zdum;      
185     Int_t   kmax, sigmax;
186     Double_t deltax, deltaz;
187     Double_t ndig;
188     Int_t   numberd = 10000;
189     Int_t   *ifpad  = new Int_t[numberd];
190     Int_t   *xpad   = new Int_t[numberd];
191     Int_t   *zpad   = new Int_t[numberd];
192     Int_t   *tr1pad = new Int_t[numberd];
193     Int_t   *tr2pad = new Int_t[numberd];
194     Int_t   *tr3pad = new Int_t[numberd];
195     Int_t   *tr4pad = new Int_t[numberd];
196     Int_t   *iclus  = new Int_t[numberd];
197
198     nclus=1;
199     for (i=0; i < ndigits ; i++){
200         ifpad[i] = -1;
201         iclus[i] = 0;
202     } // end for i
203
204     ifpad[0]=0;
205     for (i=0; i < ndigits-1 ; i++) {
206         if ( ifpad[i] == -1 ) { 
207             nclus++;
208             ipos++;
209             ifpad[i]=nclus-1;
210         } // end if ipad[i]
211         for (Int_t j=i+1 ; j < ndigits ; j++)  {  
212             if (ifpad[j]== -1 ) {
213                 dx = TMath::Abs(digx[i]-digx[j]);
214                 dz = TMath::Abs(digz[i]-digz[j]);
215                 // if ( ( dx+dz )==1 )  //clusters are not diagonal
216                 if(( dx+dz )==1 || (dx==1 && dz==1)){
217                     //diagonal clusters allowed
218                     ipos++;
219                     ifpad[j] = ifpad[i];
220                     
221                     x1         = digx[j];
222                     z1         = digz[j];
223                     digx[j]    = digx[ipos];
224                     digz[j]    = digz[ipos];
225                     digx[ipos] = x1;
226                     digz[ipos] = z1;
227                     
228                     t1 = digtr1[j];
229                     t2 = digtr2[j];
230                     t3 = digtr3[j];
231                     t4 = digtr4[j];
232                     digtr1[j] = digtr1[ipos];
233                     digtr2[j] = digtr2[ipos];
234                     digtr3[j] = digtr3[ipos];
235                     digtr4[j] = digtr4[ipos];
236                     digtr1[ipos] = t1;
237                     digtr2[ipos] = t2;
238                     digtr3[ipos] = t3;
239                     digtr4[ipos] = t4;
240                     
241                     if1 = ifpad[j];
242                     ifpad[j] = ifpad[ipos];
243                     ifpad[ipos] = if1;
244                 } // end dx+dx...
245             }// end if ifpad[j]== -1 
246         } // end for j
247     }//end loop on digits
248     if ( ifpad[ndigits-1] == -1 ) {
249         nclus++;
250         ifpad[ndigits-1]=nclus-1;
251     } // end if ifpad[ndigits-1] == -1
252     
253     for (i=0 ; i < ndigits ; i++) iclus[ifpad[i]]++;
254
255     min=0;
256     max=0;
257     // loop on found clusters 
258     for (i=0 ; i < nclus ; i++){
259         min = max;
260         max += iclus[i];
261         deltax = GetSeg()->Dpx(0);
262         if (iclus[i]!=1){
263             //cluster with more than one digit
264             nd=iclus[i];
265             ndig=(Double_t) nd;
266             Int_t count=0;
267             for (k=min;k<min+nd;k++){
268                 xpad[count] = digx[k];     
269                 zpad[count] = digz[k];
270
271                 tr1pad[count] = digtr1[k];         
272                 tr2pad[count] = digtr2[k];         
273                 tr3pad[count] = digtr3[k];         
274                 tr4pad[count] = digtr4[k];         
275                 
276                 count++; 
277             } // end for k
278             ndxmin = xpad[TMath::LocMin(nd,xpad)];
279             ndxmax = xpad[TMath::LocMax(nd,xpad)];
280             ndzmin = zpad[TMath::LocMin(nd,zpad)];
281             ndzmax = zpad[TMath::LocMax(nd,zpad)];
282             ndx = ndxmax - ndxmin+1;
283             ndz = ndzmax - ndzmin+1;
284             
285             // calculate x and z coordinates of the center of the cluster
286             GetSeg()->GetPadCxz(digx[min],digz[min]-1,xdum, zdum);
287             
288             if (ndx == 1) {
289                 xcenter[i] = xdum;
290             }else{ 
291                 xcenter[i] = 0.;
292                 for (k=0;k<nd;k++) {
293                     GetSeg()->GetPadCxz(xpad[k],zpad[k]-1,xdum,zdum);
294                     xcenter[i] += (xdum / nd);
295                 } // end for k               
296             } // end if ndx
297             
298             if (ndz == 1) {
299                 zcenter[i] = zdum;
300             } else {
301                 zcenter[i] = 0.;
302                 for (k=0;k<nd;k++) {          
303                     GetSeg()->GetPadCxz(xpad[k],zpad[k]-1,xdum,zdum);
304                     zcenter[i] += (zdum / nd);
305                 } // end for k
306             } // end if ndz
307             
308             // error on points in x and z directions
309             
310             if (ndx == 1) {
311                 errxcenter[i] = deltax / TMath::Sqrt(12.);
312             } else {
313                 errxcenter[i] = 0.;                     
314                 for (k=0;k<nd;k++){ 
315                     GetSeg()->GetPadCxz(xpad[k],zpad[k]-1,xdum,zdum);
316                     errxcenter[i] += ((xdum-xcenter[i])*(xdum-xcenter[i]))/
317                         (nd*(nd-1)); 
318                 } // end for k
319                 errxcenter[i] = TMath::Sqrt(errxcenter[i]);
320             } // end if ndx     
321             if (ndz == 1) {
322                 deltaz = GetSeg()->Dpz(digz[min]);                    
323                 errzcenter[i] = deltaz / TMath::Sqrt(12.);
324             } else {
325                 errzcenter[i] = 0.;
326                 for (k=0;k<nd;k++){ 
327                     GetSeg()->GetPadCxz(xpad[k],zpad[k]-1,xdum,zdum);
328                     errzcenter[i] += ((zdum-zcenter[i])*(zdum-zcenter[i]))/
329                         (nd*(nd-1));
330                 } // end for k
331                 errzcenter[i] = TMath::Sqrt(errzcenter[i]);
332             } // end if ndz
333             // take three track numbers for the cluster
334             // choose the track numbers of the digit with higher signal 
335             kmax = 0;
336             sigmax = 0;
337             for (k=0;k<nd;k++){
338                 if(tr4pad[k] > sigmax){
339                     sigmax = tr4pad[k];
340                     kmax   = k;
341                 } // end if tr4pad[k]
342             } // end for k
343             if(sigmax != 0) {
344                 tr1clus[i]= tr1pad[kmax];
345                 tr2clus[i]= tr2pad[kmax];
346                 tr3clus[i]= tr3pad[kmax];
347             } else {
348                 tr1clus[i]= -2;
349                 tr2clus[i]= -2;
350                 tr3clus[i]= -2;
351             } // end if sigmax
352         } else {
353             // cluster with single digit
354             ndig= 1.;
355             ndx = 1;
356             ndz = 1;
357             GetSeg()->GetPadCxz(digx[min],digz[min]-1,xdum,zdum);
358             xcenter[i] = xdum;
359             zcenter[i] = zdum;
360             tr1clus[i]=digtr1[min];
361             tr2clus[i]=digtr2[min];
362             tr3clus[i]=digtr3[min];
363             deltaz = GetSeg()->Dpz(digz[min]);
364             errxcenter[i] = deltax / TMath::Sqrt(12.);
365             errzcenter[i] = deltaz / TMath::Sqrt(12.);
366         } // end if iclus[i]
367
368         // store the cluster information to the AliITSRawCLusterSPD object
369    
370
371         //put the cluster center in local reference frame of the detector
372         // and in microns
373         xcenter[i] = xcenter[i] - GetSeg()->Dx()/2.; 
374         zcenter[i] = zcenter[i] - GetSeg()->Dz()/2.;
375
376         AliITSRawClusterSPD *clust = new AliITSRawClusterSPD(zcenter[i], //f
377                                                              xcenter[i], //f
378                                                              ndig, //f
379                                                              ndz,ndx, //ii
380                                                              ndxmin,ndxmax,//ii
381                                                              (Double_t) ndzmin,
382                                                              (Double_t) ndzmax,
383                                                              0,GetModule());
384         fDetTypeRec->AddCluster(0,clust);
385         delete clust;
386     }//end loop on clusters   
387     delete[] ifpad;
388     delete[] xpad ;
389     delete[] zpad ;
390     delete[] iclus;
391     delete[] tr1pad;
392     delete[] tr2pad;
393     delete[] tr3pad;
394     delete[] tr4pad;
395 }
396 //______________________________________________________----------------
397 void AliITSClusterFinderSPD::DigitToPoint(Int_t nclus,
398                                           Double_t *xcenter,Double_t *zcenter,
399                                           Double_t *errxcenter,
400                                           Double_t *errzcenter, 
401                                           Int_t *tr1clus, Int_t *tr2clus,
402                                           Int_t *tr3clus){
403     // A point is associated to each cluster of SPD digits. The points
404     // and their associated errors are stored in the file galiceSP.root.
405     Double_t l[3],xg,zg;
406     const Double_t kconv = 1.0e-4; // micron -> cm
407
408     Int_t lay,lad,det;
409     fDetTypeRec->GetITSgeom()->GetModuleId(fModule,lay,lad,det);
410     Int_t ind=(lad-1)*fDetTypeRec->GetITSgeom()->GetNdetectors(lay)+(det-1);
411     Int_t lyr=(lay-1);
412     // get rec points
413     for (Int_t i=0; i<nclus; i++){
414         l[0] = kconv*xcenter[i];
415         l[1] = kconv*GetSeg()->Dy()/2.;
416         l[2] = kconv*zcenter[i];
417
418         xg = l[0]; 
419         zg = l[2]; 
420
421         Double_t sigma2x = (kconv*errxcenter[i]) * (kconv*errxcenter[i]);
422         Double_t sigma2z = (kconv*errzcenter[i]) * (kconv*errzcenter[i]);
423
424         Int_t lab[4] = {tr1clus[i],tr2clus[i],tr3clus[i],ind};
425         Float_t hit[5] = {xg,zg,sigma2x,sigma2z,1.0};
426         Int_t info[3] = {0,0,lyr};
427
428         AliITSRecPoint rnew(lab,hit,info,kTRUE);
429         rnew.SetdEdX(0.);
430
431         fDetTypeRec->AddRecPoint(rnew); 
432     } // end for i
433 }