]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PMD/AliPMDClustering.cxx
clustering algorithm
[u/mrichter/AliRoot.git] / PMD / AliPMDClustering.cxx
1 //-----------------------------------------------------//
2 //                                                     //
3 //  Source File : PMDClustering.cxx, Version 00        //
4 //                                                     //
5 //  Date   : September 26 2002                         //
6 //                                                     //
7 //  clustering code for alice pmd                      //
8 //                                                     //
9 //-----------------------------------------------------//
10
11 /* 
12    --------------------------------------------------------------------
13    Code developed by S. C. Phatak, Institute of Physics, 
14    Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
15    ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
16    builds up superclusters and breaks them into clusters. The input is 
17    in array d[ndimx][ndimy] and cluster information is in array
18    clusters[5][5000]. integer clno gives total number of clusters in the 
19    supermodule.
20
21    d, clno  and clusters are the only global ( public ) variables. Others 
22    are local ( private ) to the code. 
23
24    At the moment, the data is read for whole detector ( all supermodules
25    and pmd as well as cpv. This will have to be modify later )
26
27    LAST UPDATE  :  October 23, 2002
28 -----------------------------------------------------------------------
29 */
30
31
32
33 #include <TNtuple.h>
34 #include <TObjArray.h>
35 #include "AliPMDContainer.h"
36 #include "AliPMDcluster.h"
37 #include "AliPMDClustering.h"
38 #include <stdio.h>
39
40 ClassImp(AliPMDClustering)
41
42 AliPMDClustering::AliPMDClustering()
43 {
44   fMessage = 0;
45   for(int i = 0; i < ndimx; i++)
46     {
47       for(int j = 0; j < ndimy; j++)
48         {
49           coord[0][i][j] = i+j/2.;
50           coord[1][i][j] = sqrth*j;
51         }
52     }
53 }
54 AliPMDClustering::~AliPMDClustering()
55 {
56
57 }
58
59 //void AliPMDClustering::DoClust(int idet, int isup, double d1[72][72], AliPMDContainer *pmdc)
60 void AliPMDClustering::DoClust(int idet, int isup, double d1[72][72], TObjArray *pmdcont)
61 {
62
63   AliPMDcluster *pmdcl = 0;
64
65   int i, i1, i2, j, nmx1, incr;
66   double  cutoff, ave;
67   Float_t clusdata[5];
68
69   const float twobysqrt3 = 1.1547; // 2./sqrt(3.)
70
71   //  if (fMessage == 1)
72     {
73       cout << " supermodule no. " << idet << " " << isup << endl;
74     }
75
76   for (i = 0; i < ndimx; i++)
77     {
78       for (j = 0; j < ndimy; j++)
79         {
80           d[i][j] = d1[i][j];
81         }
82     }
83   order(idet); // order the data
84   cutoff=400.; // cutoff used to discard cells having ener. dep. 
85   ave=0.; 
86   nmx1=-1;
87
88   for(j=0;j<nmx; j++)
89     {
90       i1 = iord[0][j];
91       i2 = iord[1][j];
92       if (d[i1][i2] > 0.) {ave=ave+d[i1][i2];}
93       if (d[i1][i2] >= cutoff ) nmx1 = nmx1 + 1;
94     }
95   // nmx1 --- number of cells having ener dep >= cutoff
96   if (fMessage == 1)
97     {
98       cout << " nmx1 " << nmx1 << endl;
99     }
100   ave=ave/nmx1;
101   if (fMessage == 1)
102     {
103       cout <<"nmx " << nmx << " nmx1 " << nmx1<< " ave "<<ave<<
104         " cutoff " << cutoff << endl;
105     }
106
107   incr = crclust(ave, cutoff, nmx1, idet);
108
109   refclust(incr, i, idet);
110   if (fMessage == 1)
111     {
112       if(idet == 0)cout <<" supermodule ( pmd ) = "<< isup <<" done "
113                         <<endl;
114       if(idet == 1)cout <<" supermodule ( cpv ) = "<< isup <<" done "
115                         <<endl;
116       cout << "clno " << clno << endl;
117     }
118
119
120   for(i1=0; i1<clno; i1++)
121     {
122       float clu_xc    = (float) clusters[0][i1];
123       float clu_yc    = (float) clusters[1][i1];
124       float clu_adc   = (float) clusters[2][i1];
125       float clu_cells = (float) clusters[3][i1];
126       float clu_rad   = (float) clusters[4][i1];
127       
128       float clu_y0 = twobysqrt3*clu_yc;
129       float clu_x0 = clu_xc - clu_y0/2.;
130
131       clusdata[0] = clu_cells;
132       clusdata[1] = clu_x0;
133       clusdata[2] = clu_y0;
134       clusdata[3] = clu_adc;
135       clusdata[4] = clu_rad;
136       
137       pmdcl = new AliPMDcluster(clusdata);
138       pmdcont->Add(pmdcl);
139     }
140
141   delete pmdcl;
142
143 }
144
145 void AliPMDClustering::order(int idet)
146 {
147   // using simple sort
148   double dd[nmx], adum;// matrix d converted into 
149   // one dimensional array dd. adum a place holder for double
150   int i, j, i1, i2, iord1[nmx], itst, idum; // information of 
151   // ordering is stored in iord1, original array not ordered
152   //
153   // define arrays dd and iord1
154   for(i1=0; i1 < ndimx; i1++){
155     for(i2=0; i2 < ndimy; i2++){
156       i=i1+i2*ndimx;
157       iord1[i]=i; dd[i]=d[i1][i2];
158     }
159   }
160   // sort and store sorting information in iord1 
161   for(j=1; j < nmx; j++){
162     itst=0; adum=dd[j]; idum=iord1[j];
163     for(i1=0; i1 < j ; i1++){
164       if(adum > dd[i1] && itst == 0){
165         itst=1;
166         for(i2=j-1; i2 >= i1 ; i2=i2--){
167           dd[i2+1]=dd[i2]; 
168           iord1[i2+1]=iord1[i2];
169         }
170         dd[i1]=adum; iord1[i1]=idum;
171       }
172     }
173   }
174   // store the sorted information in iord for later use
175   for(i=0; i<nmx; i++){
176     j=iord1[i]; i2=j/ndimx; 
177     i1=j-i2*ndimx; 
178     iord[0][i]=i1; 
179     iord[1][i]=i2;
180   }
181 }
182
183 void AliPMDClustering::refclust(int incr, int supmod, int idet)
184 {
185   int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest; 
186   int ihld;
187   int ig, nsupcl, lev1[20], lev2[20];
188   double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
189   double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
190   // clno counts the final clusters
191   // nsupcl =  # of superclusters; ncl[i]= # of cells in supercluster i
192   // x, y and z store (x,y) coordinates of and energy deposited in a cell
193   // xc, yc store (x,y) coordinates of the cluster center
194   // zc stores the energy deposited in a cluster
195   // rc is cluster radius
196   // finally the cluster information is put in 2-dimensional array clusters
197   //  ofstream ofl1("checking.5",ios::app);
198   clno=-1;
199   nsupcl=-1;
200   for(i=0; i<4500; i++){ncl[i]=-1;}
201   for(i=0; i<incr; i++){
202     if(infcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
203     ncl[nsupcl]=ncl[nsupcl]+1;
204   }
205   if (fMessage == 1)
206     {
207       cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
208            << endl;
209     }
210   id=-1;
211   icl=-1;
212   for(i=0; i<nsupcl; i++){
213     if(ncl[i] == 0){ 
214       id=id+1; 
215       icl=icl+1;
216       // one  cell super-clusters --> single cluster
217       // cluster center at the centyer of the cell
218       // cluster radius = half cell dimension
219       clno=clno+1; 
220       i1=infcl[1][id]; 
221       i2=infcl[2][id];
222       clusters[0][clno]=coord[0][i1][i2]; 
223       clusters[1][clno]=coord[1][i1][i2];
224       clusters[2][clno]=d[i1][i2]; 
225       clusters[3][clno]=1.; 
226       clusters[4][clno]=0.5;
227       //ofl1 << icl << " " << coord[0][i1][i2] << " " << coord[1][i1][i2] << 
228       //" " << d[i1][i2] << " " << clusters[3][clno] <<endl; 
229     }else if(ncl[i] == 1){
230       // two cell super-cluster --> single cluster
231       // cluster center is at ener. dep.-weighted mean of two cells
232       // cluster radius == half cell dimension
233       id=id+1; 
234       icl=icl+1;
235       clno=clno+1; 
236       i1=infcl[1][id]; 
237       i2=infcl[2][id]; 
238       x1=coord[0][i1][i2];
239       y1=coord[1][i1][i2]; 
240       z1=d[i1][i2];
241       id=id+1; 
242       i1=infcl[1][id]; 
243       i2=infcl[2][id];
244       x2=coord[0][i1][i2]; 
245       y2=coord[1][i1][i2]; 
246       z2=d[i1][i2];
247       clusters[0][clno]=(x1*z1+x2*z2)/(z1+z2); 
248       clusters[1][clno]=(y1*z1+y2*z2)/(z1+z2);
249       clusters[2][clno]=z1+z2; 
250       clusters[3][clno]=2.; 
251       clusters[4][clno]=0.5;
252       //ofl1 << icl << " " << clusters[0][clno] << " " << clusters[1][clno]
253       //   << " " << clusters[2][clno] << " " <<clusters[3][clno] <<endl; 
254     }else{
255       id=id+1; 
256       iord[0]=0;
257       // super-cluster of more than two cells - broken up into smaller 
258       // clusters gaussian centers computed. (peaks separated by > 1 cell) 
259       // Begin from cell having largest energy deposited This is first
260       // cluster center
261       i1=infcl[1][id]; 
262       i2=infcl[2][id];
263       x[0]=coord[0][i1][i2]; 
264       y[0]=coord[1][i1][i2]; 
265       z[0]=d[i1][i2];
266       iord[0]=0;
267       for(j=1;j<=ncl[i];j++){
268         id=id+1;
269         i1=infcl[1][id]; 
270         i2=infcl[2][id];
271         iord[j]=j;
272         x[j]=coord[0][i1][i2]; 
273         y[j]=coord[1][i1][i2]; 
274         z[j]=d[i1][i2];
275       }
276       // arranging cells within supercluster in decreasing order 
277       for(j=1;j<=ncl[i];j++){
278         itest=0; ihld=iord[j];
279         for(i1=0;i1<j;i1++){
280           if(itest == 0 && z[iord[i1]] < z[ihld]){
281             itest=1;
282             for(i2=j-1;i2>=i1;i2--){
283               iord[i2+1]=iord[i2];
284             }
285             iord[i1]=ihld;
286           }
287         }
288       }
289       // compute the number of Gaussians and their centers ( first 
290       // guess ) 
291       // centers must be separated by cells having smaller ener. dep.
292       // neighbouring centers should be either strong or well-separated
293       ig=0;
294       xc[ig]=x[iord[0]]; 
295       yc[ig]=y[iord[0]]; 
296       zc[ig]=z[iord[0]];
297       for(j=1;j<=ncl[i];j++){
298         itest=-1; 
299         x1=x[iord[j]]; 
300         y1=y[iord[j]];
301         for(k=0;k<=ig;k++){
302           x2=xc[k]; y2=yc[k]; 
303           rr=Dist(x1,y1,x2,y2);
304           if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
305             itest=itest+1;
306           if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
307             itest=itest+1;
308           if( rr >= 2.1)itest=itest+1;
309         } 
310         if(itest == ig){
311           ig=ig+1; 
312           xc[ig]=x1; 
313           yc[ig]=y1; 
314           zc[ig]=z[iord[j]];
315         }
316       }
317       // for(j=0; j<=ig; j++){
318       //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
319       //}
320       // gaussfit to adjust cluster parameters to minimize
321       gaussfit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
322       icl=icl+ig+1;
323       // compute the number of cells belonging to each cluster.
324       // cell is shared between several clusters ( if they are equidistant 
325       // from it ) in the ratio of cluster energy deposition
326       for(j=0; j<=ig; j++){
327         cells[j]=0.;
328       }
329       if(ig > 0){
330         for(j=0; j<=ncl[i]; j++){
331           lev1[0]=0; 
332           lev2[0]=0;
333           for(k=0; k<=ig; k++){
334             dist=Dist(x[j], y[j], xc[k], yc[k]);
335             if(dist < sqrt(3.) ){
336               lev1[0]++; 
337               i1=lev1[0]; 
338               lev1[i1]=k;
339             }else{
340               if(dist < 2.1){
341                 lev2[0]++; 
342                 i1=lev2[0]; 
343                 lev2[i1]=k;
344               }
345             }
346           }
347           if(lev1[0] != 0){
348             if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
349             else{
350               sum=0.;
351               for(k=1; k<=lev1[0]; k++){
352                 sum=sum+zc[lev1[k]];
353               }
354               for(k=1; k<=lev1[0]; k++){
355                 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
356               }
357             }
358           }else{
359             if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
360             else{
361               sum=0.;
362               for(k=1; k<=lev2[0]; k++){
363                 sum=sum+zc[lev2[k]];
364               }
365               for(k=1; k<=lev2[0]; k++){
366                 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
367               }
368             }
369           }
370         }
371       }
372       for(j=0; j<=ig; j++){
373         clno=clno+1; 
374         clusters[0][clno]=xc[j]; 
375         clusters[1][clno]=yc[j]; 
376         clusters[2][clno]=zc[j];
377         clusters[4][clno]=rc[j];
378         if(ig == 0){
379           clusters[3][clno]=ncl[i];
380         }else{
381           clusters[3][clno]=cells[j];
382         }
383       }
384     }
385   }
386
387 }
388
389
390 void AliPMDClustering::gaussfit(int ncell, int nclust, double &x, double &y ,double &z, double &xc, double &yc, double &zc, double &rc)
391 {
392   int i, j, i1, i2, jmax, novar, idd, jj;
393   double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500]; 
394   double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
395   double hc[4500], hd[4500], zzc[4500], rrc[4500];
396   int neib[4500][50];
397   double sum, dx, dy, str, str1, aint, sum1, rr, dum;
398   double x1, x2, y1, y2;
399   str=0.; 
400   str1=0.; 
401   rr=0.3; 
402   novar=0;
403
404   j = 0;  // Just put not to see the compiler warning, BKN
405
406
407   for(i=0; i<=ncell; i++){
408     xx[i]=*(&x+i); 
409     yy[i]=*(&y+i); 
410     zz[i]=*(&z+i);
411     str=str+zz[i];
412   }
413   for(i=0; i<=nclust; i++){
414     xxc[i]=*(&xc+i); 
415     yyc[i]=*(&yc+i); 
416     zzc[i]=*(&zc+i); 
417     str1=str1+zzc[i]; 
418     rrc[i]=0.5;
419
420   }
421   for(i=0; i<=nclust; i++){
422     zzc[i]=str/str1*zzc[i];
423     ha[i]=xxc[i]; 
424     hb[i]=yyc[i]; 
425     hc[i]=zzc[i]; 
426     hd[i]=rrc[i];
427     x1=xxc[i]; 
428     y1=yyc[i];
429   }
430   for(i=0; i<=ncell; i++){
431     idd=0; 
432     x1=xx[i]; 
433     y1=yy[i];
434     for(j=0; j<=nclust; j++){
435       x2=xxc[j]; 
436       y2=yyc[j];
437       if(Dist(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
438     }
439
440     neib[i][0]=idd;
441   }
442   sum=0.;
443   for(i1=0; i1<=ncell; i1++){
444     aint=0.; 
445     idd=neib[i1][0];
446     for(i2=1; i2<=idd; i2++){
447       jj=neib[i1][i2];
448       dx=xx[i1]-xxc[jj]; 
449       dy=yy[i1]-yyc[jj]; 
450       dum=rrc[j]*rrc[jj]+rr*rr;
451       aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
452     }
453     sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
454   }
455   jmax=nclust*1000; 
456   if(nclust > 20)jmax=20000;
457   for(j=0; j<jmax; j++){
458     str1=0.;
459     for(i=0; i<=nclust; i++){
460       a[i]=xxc[i]+0.6*(ranmar()-0.5); 
461       b[i]=yyc[i]+0.6*(ranmar()-0.5);
462       c[i]=zzc[i]*(1.+(ranmar()-0.5)*0.2); 
463       str1=str1+zzc[i];
464       d[i]=rrc[i]*(1.+(ranmar()-0.5)*0.1);
465       if(d[i] < 0.25)d[i]=0.25;
466     }
467     for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
468     sum1=0.;
469     for(i1=0; i1<=ncell; i1++){
470       aint=0.; 
471       idd=neib[i1][0];
472       for(i2=1; i2<=idd; i2++){
473         jj=neib[i1][i2];
474         dx=xx[i1]-a[jj]; 
475         dy=yy[i1]-b[jj]; 
476         dum=d[jj]*d[jj]+rr*rr;
477         aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
478       }
479       sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
480     }
481
482     if(sum1 < sum){
483       for(i2=0; i2<=nclust; i2++){
484         xxc[i2]=a[i2]; 
485         yyc[i2]=b[i2]; 
486         zzc[i2]=c[i2]; 
487         rrc[i2]=d[i2]; 
488         sum=sum1;
489
490       }
491     }
492   }
493   for(j=0; j<=nclust; j++){
494     *(&xc+j)=xxc[j]; 
495     *(&yc+j)=yyc[j]; 
496     *(&zc+j)=zzc[j]; 
497     *(&rc+j)=rrc[j];
498   }
499 }
500
501
502 double AliPMDClustering::Dist(double x1, double y1, double x2, double y2)
503 {
504   return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
505 }
506
507   
508 int AliPMDClustering::crclust(double ave, double cutoff, int nmx1, int idet)
509 {
510   int i,j,k,id1,id2,icl, numcell, clust[2][5000];
511   int jd1,jd2, icell, cellcount;
512   static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
513   // neibx and neiby define ( incremental ) (i,j) for the neighbours of a 
514   // cell. There are six neighbours.
515   // cellcount --- total number of cells having nonzero ener dep
516   // numcell --- number of cells in a given supercluster
517   //ofstream ofl0("cells_loc",ios::out);
518   // initialize infocl[2][ndimx][ndimy] 
519   for (j=0; j < 72; j++){
520     for(k=0; k < 72; k++){
521       infocl[0][j][k] = 0; 
522       infocl[1][j][k] = 0;
523     }
524   }
525   for(i=0; i < nmx; i++){
526     infcl[0][i] = -1;
527     id1=iord[0][i]; 
528     id2=iord[1][i];
529     if(d[id1][id2] <= cutoff){infocl[0][id1][id2]=-1;}
530   }
531   // ---------------------------------------------------------------
532   // crude clustering begins. Start with cell having largest adc 
533   // count and loop over the cells in descending order of adc count
534   // ---------------------------------------------------------------
535   icl=-1;
536   cellcount=-1;
537   for(icell=0; icell <= nmx1; icell++){
538     id1=iord[0][icell]; 
539     id2=iord[1][icell]; 
540     if(infocl[0][id1][id2] == 0 ){
541       // ---------------------------------------------------------------
542       // icl -- cluster #, numcell -- # of cells in it, clust -- stores 
543       // coordinates of the cells in a cluster, infocl[0][i1][i2] is 1 for 
544       // primary and 2 for secondary cells, 
545       // infocl[1][i1][i2] stores cluster #
546       // ---------------------------------------------------------------
547       icl=icl+1; 
548       numcell=0; 
549       cellcount=cellcount+1;
550       infocl[0][id1][id2]=1; 
551       infocl[1][id1][id2]=icl;
552       infcl[0][cellcount]=icl; 
553       infcl[1][cellcount]=id1; 
554       infcl[2][cellcount]=id2;
555       clust[0][numcell]=id1;
556       clust[1][numcell]=id2;
557       for(i=1; i<5000; i++)clust[0][i]=0;
558       // ---------------------------------------------------------------
559       // check for adc count in neib. cells. If ne 0 put it in this clust
560       // ---------------------------------------------------------------
561       for(i=0; i<6; i++){
562         jd1=id1+neibx[i]; 
563         jd2=id2+neiby[i];
564         if( (jd1 >= 0 && jd1 < 72) && (jd2 >= 0 && jd2 < 72) && 
565             infocl[0][jd1][jd2] == 0){
566           numcell=numcell+1;
567           infocl[0][jd1][jd2]=2; 
568           infocl[1][jd1][jd2]=icl;
569           clust[0][numcell]=jd1;
570           clust[1][numcell]=jd2;
571           cellcount=cellcount+1;
572           infcl[0][cellcount]=icl; 
573           infcl[1][cellcount]=jd1; 
574           infcl[2][cellcount]=jd2;
575         }
576       }
577       // ---------------------------------------------------------------
578       // check adc count for neighbour's neighbours recursively and 
579       // if nonzero, add these to the cluster.
580       // ---------------------------------------------------------------
581       for(i=1;i < 5000;i++){
582         if(clust[0][i] != 0){
583           id1=clust[0][i]; 
584           id2=clust[1][i];
585           for(j=0; j<6 ; j++){
586             jd1=id1+neibx[j]; 
587             jd2=id2+neiby[j];
588             if( (jd1 >= 0 && jd1 < 72) && (jd2 >= 0 && jd2 < 72) && 
589                 infocl[0][jd1][jd2] == 0 ){
590               infocl[0][jd1][jd2]=2; 
591               infocl[1][jd1][jd2]=icl;
592               numcell=numcell+1; 
593               clust[0][numcell]=jd1;
594               clust[1][numcell]=jd2;
595               cellcount=cellcount+1;
596               infcl[0][cellcount]=icl; 
597               infcl[1][cellcount]=jd1; 
598               infcl[2][cellcount]=jd2;
599             }
600           }
601         }
602       }
603     }
604   }
605   //  for(icell=0; icell<=cellcount; icell++){
606   //    ofl0 << infcl[0][icell] << " " << infcl[1][icell] << " " << 
607   //      infcl[2][icell] << endl;
608   //  }
609   return cellcount;
610 }
611
612 double AliPMDClustering::ranmar()
613 {
614   /*                                   C==========================C*/
615   /*===================================C==========================*/
616   /*  Universal random number generator proposed by Marsaglia and Zaman
617       in report FSU-SCRI-87-50 */
618
619   //  clock_t start;
620   int ii, jj;
621   static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
622   static double u[97], c, cd, cm, s, t;
623   static double uni;
624   int count1,count2,idum;
625   /*    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  */
626   if (itest == 0) {
627     //*******************************************************
628     // following three lines if the seed to be provided by computer
629     // start = time(NULL);
630     // ii=start;
631     // jj=start;
632     //*******************************************************
633     //following two lines for fixed seed ( during testing only. Else
634     //use preceeing three lines
635     ii=8263;
636     jj=5726;
637     if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
638     if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
639     itest=itest+1;
640     if((( ii > 0 ) &&  ( ii <= 31328 )) && (( jj > 0 ) && 
641                                             ( jj <= 30081 ))){
642       i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169; 
643       i4 = jj - (i3-1)*169;
644       count1=0;
645       while ( count1 < 97 ){
646         s=0.;
647         t=0.5;
648         count2=0;
649         while( count2 < 24 ){
650           idum=i1*i2/179;
651           idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
652           i5=idum-(idum/179)*179;
653           i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
654           if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
655           t=0.5*t;
656           count2=count2+1;
657         }
658         u[count1] = s;
659         count1 = count1 +1;
660       }
661       c = 362436./16777216.;  cd = 7654321./16777216.; 
662       cm = 16777213./16777216.;
663     }
664     else{
665       cout << " wrong initialization " << endl;
666     }
667   }
668   else{
669     uni = u[i] - u[j]; if( uni < 0.) uni = uni + 1; u[i] = uni; 
670     i = i -1;
671     if( i < 0 ) i = 96; j = j - 1; if ( j < 0 ) j = 96; c = c - cd;
672     if( c < 0. ) c = c+cm; uni = uni-c ; if( uni < 0. )uni = uni+1.;
673     //    return uni;
674   }
675   return uni;
676
677 }   
678
679 void AliPMDClustering::ConvertL2G(int smnumber, double xcell, double ycell, double &xpos, double &ypos)
680 {
681   float xreal = -999., yreal = -999.;
682   float cell_rad=0.25, celldia_x=0.5, celldia_y=0.4330127;
683   float xcon, ycon;
684   float xoff1, xoff2, yoff=0.2886751, yoff3;
685   float xhex1 = -27.09375, yhex1 = -15.652584;
686   float xhex2 =  27.09375, yhex2 = -15.652584;
687   float xhex3 = 0.0, yhex3 = 31.285168;
688
689
690   double xcorner[27] =
691   {
692     9.435395, 45.560394,  81.685394, -8.627106,
693     27.497894, 63.622894, -26.689606,  9.435394,
694     45.560394, 9.435344,  -8.627106, -26.689556,
695     45.560345, 27.497894, 9.435445,   81.685341,
696     63.622894, 45.560444, -18.870789, -36.933388,
697     -54.995991, -36.933189, -54.995789, -73.058388,
698     -54.995586, -73.058189, -91.120789
699   };
700   
701   double ycorner[27] = 
702   {
703     -16.342583, -16.34258, -16.34258, -47.627750, -47.627750,
704     -47.627750, -78.912918, -78.912918, -78.912918, 16.342611,
705     47.627808, 78.913002, 16.342554, 47.627750, 78.912949,
706     16.342495, 47.627693, 78.912888, -0.000116, -31.285227,
707     -62.570335, 31.285110, 0.000000, -31.285110, 62.570335,
708     31.285227, 0.000116
709   };
710  
711   if (smnumber<=8)
712     {
713       xcon  = xcorner[smnumber]+xhex1;
714       ycon  = ycorner[smnumber]+yhex1;
715       xoff1 = celldia_x+(ycell-1)*cell_rad;
716       xreal = xcon+xoff1+celldia_x*(xcell-1);
717       yreal = ycon+yoff+celldia_y*(ycell-1);
718     }
719
720   if (smnumber>8 && smnumber<=17)
721     {
722       xcon  = xcorner[smnumber]+xhex2;
723       ycon  = ycorner[smnumber]+yhex2;
724       xoff2 = celldia_x+(xcell-1)*cell_rad;
725       xreal = xcon-(xoff2+celldia_x*(ycell-1));
726       yreal = ycon+yoff+celldia_y*(xcell-1);
727     }           
728     
729   if (smnumber>17)
730     {
731       xcon  = xcorner[smnumber]+xhex3;
732       ycon  = ycorner[smnumber]+yhex3;
733       yoff3 = celldia_x * 0.8660254  + cell_rad * 0.57735027;
734       xreal = xcon+(ycell-xcell)*cell_rad;
735       yreal = ycon-(yoff3+(xcell+ycell-2)*celldia_y);
736     }
737
738   xpos = xreal;
739   ypos = yreal;
740 }
741
742 void AliPMDClustering::cell_pos(Int_t isup, Int_t j, int k, Float_t &xp, Float_t &yp){
743
744   /*
745     This converts PMD cluster or CELL coordinates
746     to Global coordinates.
747     Written by Prof. S.C. Phatak
748   */
749
750   Int_t i;
751   Float_t celldia = 0.5;
752   const Float_t pi = 3.14159;
753   const double sqrth=0.8660254;  // sqrth = sqrt(3.)/2.
754   /*
755     isup --> supermodule no ( 0 - 26 )
756     idet --> detector ( pmd or cpv : not required now )
757     j --> xpad ( goes from 1 to 72 )
758     k --> ypad ( goes from 1 to 72 )
759     xp --> global x coordinate
760     yp --> global y coordinate
761     
762     (xp0,yp0) corner positions of all supermodules in global
763     coordinate system. That is the origin
764     of the local ( supermodule ) coordinate system.
765 */ 
766   
767   Float_t xp0[27] = 
768   {
769     -17.9084, 18.2166, 54.3416, -35.9709, 0.154144, 
770     36.2791, -54.0334, -17.9084, 18.2166, 36.7791, 
771     18.7166, 0.654194, 72.9041, 54.8416, 36.7792, 
772     109.029, 90.9666, 72.9042, -18.8708, -36.9334, 
773     -54.996, -36.9332, -54.9958, -73.0584, -54.9956, 
774     -73.0582, -91.1208
775   };
776
777   Float_t yp0[27] = 
778   {
779     -32.1395, -32.1395, -32.1395, -63.4247, -63.4247, 
780     -63.4247, -94.7098, -94.7098, -94.7098, 0.545689, 
781     31.8309, 63.1161, 0.545632, 31.8308, 63.116, 
782     0.545573, 31.8308, 63.116, 31.5737, 0.288616, 
783     -30.9965, 62.859, 31.5738, 0.288733, 94.1442, 
784     62.8591, 31.574
785   };
786
787   /* 
788      angles of rotation for three sets of supermodules
789      The angle is same for first nine, next nine and last nine 
790      supermodules 
791   */
792   
793   Float_t th[3] = {0., -2.*pi/3., 2.*pi/3.};
794   Float_t xr, yr, xinit, yinit, cs, sn;
795   
796   /* 
797      xinit and yinit are coordinates of the cell in local coordinate system
798   */
799   
800   xinit = (j)*celldia+(k)/2.*celldia;
801   yinit = sqrth*(k)/2.;
802   i=isup/9;
803   cs=cos(th[i]);
804   sn=sin(th[i]);
805   //
806   // rotate first
807   //
808   xr=cs*xinit+sn*yinit;
809   yr=-sn*xinit+cs*yinit;
810   //
811   // then translate
812   //
813   xp=xr+xp0[isup];
814   yp=yr+yp0[isup];
815
816 }
817 void AliPMDClustering::SetMessage(Int_t imessage)
818 {
819   fMessage = imessage;
820 }