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