]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PMD/AliPMDClustering.cxx
25e237ae457b5f382bcf6a1a934e9d83deac1c88
[u/mrichter/AliRoot.git] / PMD / AliPMDClustering.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 //-----------------------------------------------------//
17 //                                                     //
18 //  Source File : PMDClustering.cxx, Version 00        //
19 //                                                     //
20 //  Date   : September 26 2002                         //
21 //                                                     //
22 //  clustering code for alice pmd                      //
23 //                                                     //
24 //-----------------------------------------------------//
25
26 /* --------------------------------------------------------------------
27    Code developed by S. C. Phatak, Institute of Physics,
28    Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
29    ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
30    builds up superclusters and breaks them into clusters. The input is
31    in array fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
32    fClusters[5][5000]. integer fClno gives total number of clusters in the
33    supermodule.
34
35    fEdepCell, fClno  and fClusters are the only global ( public ) variables.
36    Others are local ( private ) to the code.
37    At the moment, the data is read for whole detector ( all supermodules
38    and pmd as well as cpv. This will have to be modify later )
39    LAST UPDATE  :  October 23, 2002
40 -----------------------------------------------------------------------*/
41
42 #include "Riostream.h"
43 #include <TNtuple.h>
44 #include <TObjArray.h>
45 #include "AliPMDcluster.h"
46 #include "AliPMDClustering.h"
47 #include <stdio.h>
48
49 ClassImp(AliPMDClustering)
50
51 const Double_t AliPMDClustering::fgkSqroot3by2=0.8660254;  // sqrt(3.)/2.
52
53 AliPMDClustering::AliPMDClustering():
54   fDebug(0),
55   fCutoff(0.0)
56 {
57   for(int i = 0; i < kNDIMX; i++)
58     {
59       for(int j = 0; j < kNDIMY; j++)
60         {
61           fCoord[0][i][j] = i+j/2.;
62           fCoord[1][i][j] = fgkSqroot3by2*j;
63           fEdepCell[i][j] = 0;
64         }
65     }
66 }
67 // ------------------------------------------------------------------------ //
68 AliPMDClustering::~AliPMDClustering()
69 {
70
71 }
72 // ------------------------------------------------------------------------ //
73 void AliPMDClustering::DoClust(Int_t idet, Int_t ismn, Double_t celladc[48][96], TObjArray *pmdcont)
74 {
75   // main function to call other necessary functions to do clustering
76   //
77   AliPMDcluster *pmdcl = 0;
78   /*
79     int id and jd defined to read the input data.
80     It is assumed that for data we have 0 <= id <= 48
81     and 0 <= jd <=96
82   */
83   int i, i1, i2, j, nmx1, incr, id, jd;
84   double  cutoff, ave;
85   Float_t clusdata[7];
86
87   const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
88
89   for (id = 0; id < kNDIMXr; id++)
90     {
91       for (jd = 0; jd < kNDIMYr; jd++)
92         {
93           j=jd;
94           i=id+(kNDIMYr/2-1)-(jd/2);
95           fEdepCell[i][j] = celladc[id][jd];
96         }
97     }
98   Order(); // order the data
99   cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
100   ave=0.;
101   nmx1=-1;
102
103   for(j=0;j<kNMX; j++)
104     {
105       i1 = fIord[0][j];
106       i2 = fIord[1][j];
107       if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
108       if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
109     }
110   // nmx1 --- number of cells having ener dep >= cutoff
111   if (fDebug == 1)
112     {
113       cout << " nmx1 " << nmx1 << endl;
114     }
115
116   //  if (nmx1 == 0 | nmx1 == -1) return;
117
118   if (nmx1 == 0) nmx1 = 1;
119   ave=ave/nmx1;
120   if (fDebug == 1)
121     {
122       cout <<"kNMX " << kNMX << " nmx1 " << nmx1<< " ave "<<ave<<
123         " cutoff " << cutoff << endl;
124     }
125
126   incr = CrClust(ave, cutoff, nmx1);
127   RefClust(incr);
128   if (fDebug == 1)
129     {
130       cout << "fClno " << fClno << endl;
131     }
132
133   for(i1=0; i1<=fClno; i1++)
134     {
135       Float_t cluXC    = (Float_t) fClusters[0][i1];
136       Float_t cluYC    = (Float_t) fClusters[1][i1];
137       Float_t cluADC   = (Float_t) fClusters[2][i1];
138       Float_t cluCELLS = (Float_t) fClusters[3][i1];
139       Float_t cluRAD   = (Float_t) fClusters[4][i1];
140       Float_t cluY0    = ktwobysqrt3*cluYC;
141       Float_t cluX0    = cluXC - cluY0/2.;
142       // 
143       // Cluster X centroid is back transformed
144       //
145       clusdata[0]      = cluX0 - (48-1) + cluY0/2.;
146       clusdata[1]      = cluY0;
147       clusdata[2]      = cluADC;
148       clusdata[3]      = cluCELLS;
149       clusdata[4]      = cluRAD;
150
151       pmdcl = new AliPMDcluster(idet, ismn, clusdata);
152       pmdcont->Add(pmdcl);
153     }
154 }
155 // ------------------------------------------------------------------------ //
156 void AliPMDClustering::Order()
157 {
158   // Sorting algorithm
159   // sorts the ADC values from higher to lower
160   //
161   double dd[kNMX];
162   // matrix fEdepCell converted into
163   // one dimensional array dd. adum a place holder for double
164   int i, j, i1, i2, iord1[kNMX];
165   // information of
166   // ordering is stored in iord1, original array not ordered
167   //
168   // define arrays dd and iord1
169   for(i1=0; i1 < kNDIMX; i1++)
170     {
171       for(i2=0; i2 < kNDIMY; i2++)
172         {
173           i        = i1 + i2*kNDIMX;
174           iord1[i] = i;
175           dd[i]    = fEdepCell[i1][i2];
176         }
177     }
178   // sort and store sorting information in iord1
179 //   for(j=1; j < kNMX; j++)
180 //     {
181 //       itst = 0;
182 //       adum = dd[j];
183 //       idum = iord1[j];
184 //       for(i1=0; i1 < j ; i1++)
185 //      {
186 //        if(adum > dd[i1] && itst == 0)
187 //          {
188 //            itst = 1;
189 //            for(i2=j-1; i2 >= i1 ; i2=i2--)
190 //              {
191 //                dd[i2+1]    = dd[i2];
192 //                iord1[i2+1] = iord1[i2];
193 //              }
194 //            dd[i1]    = adum;
195 //            iord1[i1] = idum;
196 //          }
197 //      }
198 //     }
199
200   TMath::Sort(kNMX,dd,iord1); //PH Using much better algorithm...
201   // store the sorted information in fIord for later use
202   for(i=0; i<kNMX; i++)
203     {
204       j  = iord1[i];
205       i2 = j/kNDIMX;
206       i1 = j-i2*kNDIMX;
207       fIord[0][i]=i1;
208       fIord[1][i]=i2;
209     }
210 }
211 // ------------------------------------------------------------------------ //
212 int AliPMDClustering::CrClust(double ave, double cutoff, int nmx1)
213 {
214   // Does crude clustering
215   // Finds out only the big patch by just searching the
216   // connected cells
217   //
218   int i,j,k,id1,id2,icl, numcell, clust[2][5000];
219   int jd1,jd2, icell, cellcount;
220   static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
221   // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
222   // cell. There are six neighbours.
223   // cellcount --- total number of cells having nonzero ener dep
224   // numcell --- number of cells in a given supercluster
225   // ofstream ofl0("cells_loc",ios::out);
226   // initialize fInfocl[2][kNDIMX][kNDIMY]
227
228   if (fDebug == 1)
229     {
230       printf(" *** Inside CrClust **  kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f\n",
231              kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff);
232     }
233   for (j=0; j < kNDIMX; j++){
234     for(k=0; k < kNDIMY; k++){
235       fInfocl[0][j][k] = 0;
236       fInfocl[1][j][k] = 0;
237     }
238   }
239   for(i=0; i < kNMX; i++){
240     fInfcl[0][i] = -1;
241     id1=fIord[0][i];
242     id2=fIord[1][i];
243     if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
244   }
245   // ---------------------------------------------------------------
246   // crude clustering begins. Start with cell having largest adc
247   // count and loop over the cells in descending order of adc count
248   // ---------------------------------------------------------------
249   icl=-1;
250   cellcount=-1;
251   for(icell=0; icell <= nmx1; icell++){
252     id1=fIord[0][icell];
253     id2=fIord[1][icell];
254     if(fInfocl[0][id1][id2] == 0 ){
255       // ---------------------------------------------------------------
256       // icl -- cluster #, numcell -- # of cells in it, clust -- stores
257       // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
258       // primary and 2 for secondary cells,
259       // fInfocl[1][i1][i2] stores cluster #
260       // ---------------------------------------------------------------
261       icl=icl+1;
262       numcell=0;
263       cellcount = cellcount + 1;
264       fInfocl[0][id1][id2]=1;
265       fInfocl[1][id1][id2]=icl;
266       fInfcl[0][cellcount]=icl;
267       fInfcl[1][cellcount]=id1;
268       fInfcl[2][cellcount]=id2;
269
270       clust[0][numcell]=id1;
271       clust[1][numcell]=id2;
272       for(i=1; i<5000; i++)clust[0][i]=0;
273       // ---------------------------------------------------------------
274       // check for adc count in neib. cells. If ne 0 put it in this clust
275       // ---------------------------------------------------------------
276       for(i=0; i<6; i++){
277         jd1=id1+neibx[i];
278         jd2=id2+neiby[i];
279         if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
280             fInfocl[0][jd1][jd2] == 0){
281           numcell=numcell+1;
282           fInfocl[0][jd1][jd2]=2;
283           fInfocl[1][jd1][jd2]=icl;
284           clust[0][numcell]=jd1;
285           clust[1][numcell]=jd2;
286           cellcount=cellcount+1;
287           fInfcl[0][cellcount]=icl;
288           fInfcl[1][cellcount]=jd1;
289           fInfcl[2][cellcount]=jd2;
290         }
291       }
292       // ---------------------------------------------------------------
293       // check adc count for neighbour's neighbours recursively and
294       // if nonzero, add these to the cluster.
295       // ---------------------------------------------------------------
296       for(i=1;i < 5000;i++){
297         if(clust[0][i] != 0){
298           id1=clust[0][i];
299           id2=clust[1][i];
300           for(j=0; j<6 ; j++){
301             jd1=id1+neibx[j];
302             jd2=id2+neiby[j];
303             if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
304                 fInfocl[0][jd1][jd2] == 0 ){
305               fInfocl[0][jd1][jd2] = 2;
306               fInfocl[1][jd1][jd2] = icl;
307               numcell              = numcell + 1;
308               clust[0][numcell]    = jd1;
309               clust[1][numcell]    = jd2;
310               cellcount            = cellcount+1;
311               fInfcl[0][cellcount] = icl;
312               fInfcl[1][cellcount] = jd1;
313               fInfcl[2][cellcount] = jd2;
314             }
315           }
316         }
317       }
318     }
319   }
320   //  for(icell=0; icell<=cellcount; icell++){
321   //    ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
322   //      fInfcl[2][icell] << endl;
323   //  }
324   return cellcount;
325 }
326 // ------------------------------------------------------------------------ //
327 void AliPMDClustering::RefClust(int incr)
328 {
329   // Does the refining of clusters
330   // Takes the big patch and does gaussian fitting and
331   // finds out the more refined clusters
332   //
333   int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
334   int ihld;
335   int ig, nsupcl, lev1[20], lev2[20];
336   double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
337   double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
338   // fClno counts the final clusters
339   // nsupcl =  # of superclusters; ncl[i]= # of cells in supercluster i
340   // x, y and z store (x,y) coordinates of and energy deposited in a cell
341   // xc, yc store (x,y) coordinates of the cluster center
342   // zc stores the energy deposited in a cluster
343   // rc is cluster radius
344   // finally the cluster information is put in 2-dimensional array clusters
345   // ofstream ofl1("checking.5",ios::app);
346   fClno  = -1;
347   nsupcl = -1;
348   for(i=0; i<4500; i++){ncl[i]=-1;}
349   for(i=0; i<incr; i++){
350     if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
351     if (nsupcl > 4500) {
352       Error("RefClust", "Too many superclusters!");
353       nsupcl = 4500;
354       break;
355     }
356     ncl[nsupcl]=ncl[nsupcl]+1;
357   }
358   if (fDebug == 1)
359     {
360       cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
361            << endl;
362     }
363   id=-1;
364   icl=-1;
365   for(i=0; i<nsupcl; i++){
366     if(ncl[i] == 0){
367       id=id+1;
368       icl=icl+1;
369       // one  cell super-clusters --> single cluster
370       // cluster center at the centyer of the cell
371       // cluster radius = half cell dimension
372       if (fClno >= 5000) {
373         Error("RefClust", "Too many clusters!");
374         return;
375       }
376       fClno = fClno + 1;
377       i1 = fInfcl[1][id];
378       i2 = fInfcl[2][id];
379       fClusters[0][fClno] = fCoord[0][i1][i2];
380       fClusters[1][fClno] = fCoord[1][i1][i2];
381       fClusters[2][fClno] = fEdepCell[i1][i2];
382       fClusters[3][fClno] = 1.;
383       fClusters[4][fClno] = 0.5;
384       //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
385       //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
386     }else if(ncl[i] == 1){
387       // two cell super-cluster --> single cluster
388       // cluster center is at ener. dep.-weighted mean of two cells
389       // cluster radius == half cell dimension
390       id   = id + 1;
391       icl  = icl+1;
392       if (fClno >= 5000) {
393         Error("RefClust", "Too many clusters!");
394         return;
395       }
396       fClno = fClno+1;
397       i1   = fInfcl[1][id];
398       i2   = fInfcl[2][id];
399       x1   = fCoord[0][i1][i2];
400       y1   = fCoord[1][i1][i2];
401       z1   = fEdepCell[i1][i2];
402       id   = id+1;
403       i1   = fInfcl[1][id];
404       i2   = fInfcl[2][id];
405       x2   = fCoord[0][i1][i2];
406       y2   = fCoord[1][i1][i2];
407       z2   = fEdepCell[i1][i2];
408       fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
409       fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
410       fClusters[2][fClno] = z1+z2;
411       fClusters[3][fClno] = 2.;
412       fClusters[4][fClno] = 0.5;
413       //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
414       //   << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
415     }
416     else{
417       id      = id + 1;
418       iord[0] = 0;
419       // super-cluster of more than two cells - broken up into smaller
420       // clusters gaussian centers computed. (peaks separated by > 1 cell)
421       // Begin from cell having largest energy deposited This is first
422       // cluster center
423       i1      = fInfcl[1][id];
424       i2      = fInfcl[2][id];
425       x[0]    = fCoord[0][i1][i2];
426       y[0]    = fCoord[1][i1][i2];
427       z[0]    = fEdepCell[i1][i2];
428       iord[0] = 0;
429       for(j=1;j<=ncl[i];j++){
430
431         id      = id + 1;
432         i1      = fInfcl[1][id];
433         i2      = fInfcl[2][id];
434         iord[j] = j;
435         x[j]    = fCoord[0][i1][i2];
436         y[j]    = fCoord[1][i1][i2];
437         z[j]    = fEdepCell[i1][i2];
438       }
439       // arranging cells within supercluster in decreasing order
440       for(j=1;j<=ncl[i];j++){
441         itest=0;
442         ihld=iord[j];
443         for(i1=0;i1<j;i1++){
444           if(itest == 0 && z[iord[i1]] < z[ihld]){
445             itest=1;
446             for(i2=j-1;i2>=i1;i2--){
447               iord[i2+1]=iord[i2];
448             }
449             iord[i1]=ihld;
450           }
451         }
452       }
453
454       // compute the number of Gaussians and their centers ( first
455       // guess )
456       // centers must be separated by cells having smaller ener. dep.
457       // neighbouring centers should be either strong or well-separated
458       ig=0;
459       xc[ig]=x[iord[0]];
460       yc[ig]=y[iord[0]];
461       zc[ig]=z[iord[0]];
462       for(j=1;j<=ncl[i];j++){
463         itest=-1;
464         x1=x[iord[j]];
465         y1=y[iord[j]];
466         for(k=0;k<=ig;k++){
467           x2=xc[k]; y2=yc[k];
468           rr=Distance(x1,y1,x2,y2);
469           if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
470             itest=itest+1;
471           if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
472             itest=itest+1;
473           if( rr >= 2.1)itest=itest+1;
474         }
475         if(itest == ig){
476           ig=ig+1;
477           xc[ig]=x1;
478           yc[ig]=y1;
479           zc[ig]=z[iord[j]];
480         }
481       }
482       // for(j=0; j<=ig; j++){
483       //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
484       //}
485       // GaussFit to adjust cluster parameters to minimize
486       GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
487       icl=icl+ig+1;
488       // compute the number of cells belonging to each cluster.
489       // cell is shared between several clusters ( if they are equidistant
490       // from it ) in the ratio of cluster energy deposition
491       for(j=0; j<=ig; j++){
492         cells[j]=0.;
493       }
494       if(ig > 0){
495         for(j=0; j<=ncl[i]; j++){
496           lev1[0]=0;
497           lev2[0]=0;
498           for(k=0; k<=ig; k++){
499             dist=Distance(x[j], y[j], xc[k], yc[k]);
500             if(dist < sqrt(3.) ){
501               lev1[0]++;
502               i1=lev1[0];
503               lev1[i1]=k;
504             }else{
505               if(dist < 2.1){
506                 lev2[0]++;
507                 i1=lev2[0];
508                 lev2[i1]=k;
509               }
510             }
511           }
512           if(lev1[0] != 0){
513             if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
514             else{
515               sum=0.;
516               for(k=1; k<=lev1[0]; k++){
517                 sum=sum+zc[lev1[k]];
518               }
519               for(k=1; k<=lev1[0]; k++){
520                 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
521               }
522             }
523           }else{
524             if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
525             else{
526               sum=0.;
527               for(k=1; k<=lev2[0]; k++){
528                 sum=sum+zc[lev2[k]];
529               }
530               for(k=1; k<=lev2[0]; k++){
531                 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
532               }
533             }
534           }
535         }
536       }
537       for(j=0; j<=ig; j++){
538         if (fClno >= 5000) {
539           Error("RefClust", "Too many clusters!");
540           return;
541         }
542         fClno               = fClno + 1;
543         fClusters[0][fClno] = xc[j];
544         fClusters[1][fClno] = yc[j];
545         fClusters[2][fClno] = zc[j];
546         fClusters[4][fClno] = rc[j];
547         if(ig == 0){
548           fClusters[3][fClno] = ncl[i];
549         }else{
550           fClusters[3][fClno] = cells[j];
551         }
552       }
553     }
554   }
555 }
556 // ------------------------------------------------------------------------ //
557 void AliPMDClustering::GaussFit(Int_t ncell, Int_t nclust, Double_t &x, Double_t &y ,Double_t &z, Double_t &xc, Double_t &yc, Double_t &zc, Double_t &rc)
558 {
559   // Does gaussian fitting
560   //
561   int i, j, i1, i2, novar, idd, jj;
562   double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
563   double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
564   double hc[4500], hd[4500], zzc[4500], rrc[4500];
565   int neib[4500][50];
566   double sum, dx, dy, str, str1, aint, sum1, rr, dum;
567   double x1, x2, y1, y2;
568   str   = 0.;
569   str1  = 0.;
570   rr    = 0.3;
571   novar = 0;
572   j = 0;  // Just put not to see the compiler warning, BKN
573
574   for(i=0; i<=ncell; i++)
575     {
576       xx[i] = *(&x+i);
577       yy[i] = *(&y+i);
578       zz[i] = *(&z+i);
579       str   = str + zz[i];
580     }
581   for(i=0; i<=nclust; i++)
582     {
583       xxc[i] = *(&xc+i);
584       yyc[i] = *(&yc+i);
585       zzc[i] = *(&zc+i);
586       str1   = str1 + zzc[i];
587       rrc[i] = 0.5;
588     }
589   for(i=0; i<=nclust; i++)
590     {
591       zzc[i] = str/str1*zzc[i];
592       ha[i]  = xxc[i];
593       hb[i]  = yyc[i];
594       hc[i]  = zzc[i];
595       hd[i]  = rrc[i];
596       x1     = xxc[i];
597       y1     = yyc[i];
598     }
599   for(i=0; i<=ncell; i++){
600     idd=0;
601     x1=xx[i];
602     y1=yy[i];
603     for(j=0; j<=nclust; j++){
604       x2=xxc[j];
605       y2=yyc[j];
606       if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
607     }
608     neib[i][0]=idd;
609   }
610   sum=0.;
611   for(i1=0; i1<=ncell; i1++){
612     aint=0.;
613     idd=neib[i1][0];
614     for(i2=1; i2<=idd; i2++){
615       jj=neib[i1][i2];
616       dx=xx[i1]-xxc[jj];
617       dy=yy[i1]-yyc[jj];
618       dum=rrc[j]*rrc[jj]+rr*rr;
619       aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
620     }
621     sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
622   }
623 //   jmax=nclust*1000;
624 //   if(nclust > 20)jmax=20000;
625 //   for(j=0; j<jmax; j++){
626     str1=0.;
627     for(i=0; i<=nclust; i++){
628       a[i]=xxc[i]+0.6*(Ranmar()-0.5);
629       b[i]=yyc[i]+0.6*(Ranmar()-0.5);
630       c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
631       str1=str1+zzc[i];
632       d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
633       if(d[i] < 0.25)d[i]=0.25;
634     }
635     for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
636     sum1=0.;
637     for(i1=0; i1<=ncell; i1++){
638       aint=0.;
639       idd=neib[i1][0];
640       for(i2=1; i2<=idd; i2++){
641         jj=neib[i1][i2];
642         dx=xx[i1]-a[jj];
643         dy=yy[i1]-b[jj];
644         dum=d[jj]*d[jj]+rr*rr;
645         aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
646       }
647       sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
648     }
649
650     if(sum1 < sum){
651       for(i2=0; i2<=nclust; i2++){
652         xxc[i2]=a[i2];
653         yyc[i2]=b[i2];
654         zzc[i2]=c[i2];
655         rrc[i2]=d[i2];
656         sum=sum1;
657       }
658     }
659 //   }
660   for(j=0; j<=nclust; j++){
661     *(&xc+j)=xxc[j];
662     *(&yc+j)=yyc[j];
663     *(&zc+j)=zzc[j];
664     *(&rc+j)=rrc[j];
665   }
666 }
667 // ------------------------------------------------------------------------ //
668 double AliPMDClustering::Distance(double x1, double y1, double x2, double y2)
669 {
670   return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
671 }
672 // ------------------------------------------------------------------------ //
673 double AliPMDClustering::Ranmar() const
674 {
675   //  Universal random number generator proposed by Marsaglia and Zaman
676   //  in report FSU-SCRI-87-50
677
678   //  clock_t start;
679   int ii, jj;
680   static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
681   static double u[97], c, cd, cm, s, t;
682   static double uni;
683   int count1,count2,idum;
684   /*    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  */
685   if (itest == 0) {
686     //*******************************************************
687     // following three lines if the seed to be provided by computer
688     // start = time(NULL);
689     // ii=start;
690     // jj=start;
691     //*******************************************************
692     //following two lines for fixed seed ( during testing only. Else
693     //use preceeing three lines
694     ii=8263;
695     jj=5726;
696     if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
697     if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
698     itest=itest+1;
699     if((( ii > 0 ) &&  ( ii <= 31328 )) && (( jj > 0 ) &&
700                                             ( jj <= 30081 ))){
701       i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
702       i4 = jj - (i3-1)*169;
703       count1=0;
704       while ( count1 < 97 ){
705         s=0.;
706         t=0.5;
707         count2=0;
708         while( count2 < 24 ){
709           idum=i1*i2/179;
710           idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
711           i5=idum-(idum/179)*179;
712           i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
713           if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
714           t=0.5*t;
715           count2=count2+1;
716         }
717         u[count1] = s;
718         count1 = count1 +1;
719       }
720       c = 362436./16777216.;  cd = 7654321./16777216.;
721       cm = 16777213./16777216.;
722     }
723     else{
724       cout << " wrong initialization " << endl;
725     }
726   }
727   else{
728     uni = u[i] - u[j];
729     if( uni < 0.) uni = uni + 1;
730     u[i] = uni;
731     i = i -1;
732     if( i < 0 ) i = 96;
733     j = j - 1;
734     if ( j < 0 ) j = 96;
735     c = c - cd;
736     if( c < 0. ) c = c+cm;
737     uni = uni-c ;
738     if( uni < 0. )uni = uni+1.;
739   }
740   return uni;
741 }
742 // ------------------------------------------------------------------------ //
743 void AliPMDClustering::SetEdepCut(Float_t decut)
744 {
745   fCutoff = decut;
746 }
747 // ------------------------------------------------------------------------ //
748 void AliPMDClustering::SetDebug(Int_t idebug)
749 {
750   fDebug = idebug;
751 }