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