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