1 //-----------------------------------------------------//
3 // Source File : PMDClustering.cxx, Version 00 //
5 // Date : September 26 2002 //
7 // clustering code for alice pmd //
9 //-----------------------------------------------------//
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
21 d, clno and clusters are the only global ( public ) variables. Others
22 are local ( private ) to the code.
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 )
27 LAST UPDATE : October 23, 2002
28 -----------------------------------------------------------------------
34 #include <TObjArray.h>
35 #include "AliPMDContainer.h"
36 #include "AliPMDcluster.h"
37 #include "AliPMDClustering.h"
40 ClassImp(AliPMDClustering)
42 AliPMDClustering::AliPMDClustering()
45 for(int i = 0; i < ndimx; i++)
47 for(int j = 0; j < ndimy; j++)
49 coord[0][i][j] = i+j/2.;
50 coord[1][i][j] = sqrth*j;
54 AliPMDClustering::~AliPMDClustering()
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)
63 AliPMDcluster *pmdcl = 0;
65 int i, i1, i2, j, nmx1, incr;
69 const float twobysqrt3 = 1.1547; // 2./sqrt(3.)
73 cout << " supermodule no. " << idet << " " << isup << endl;
76 for (i = 0; i < ndimx; i++)
78 for (j = 0; j < ndimy; j++)
83 order(idet); // order the data
84 cutoff=400.; // cutoff used to discard cells having ener. dep.
92 if (d[i1][i2] > 0.) {ave=ave+d[i1][i2];}
93 if (d[i1][i2] >= cutoff ) nmx1 = nmx1 + 1;
95 // nmx1 --- number of cells having ener dep >= cutoff
98 cout << " nmx1 " << nmx1 << endl;
103 cout <<"nmx " << nmx << " nmx1 " << nmx1<< " ave "<<ave<<
104 " cutoff " << cutoff << endl;
107 incr = crclust(ave, cutoff, nmx1, idet);
109 refclust(incr, i, idet);
112 if(idet == 0)cout <<" supermodule ( pmd ) = "<< isup <<" done "
114 if(idet == 1)cout <<" supermodule ( cpv ) = "<< isup <<" done "
116 cout << "clno " << clno << endl;
120 for(i1=0; i1<clno; i1++)
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];
128 float clu_y0 = twobysqrt3*clu_yc;
129 float clu_x0 = clu_xc - clu_y0/2.;
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;
137 pmdcl = new AliPMDcluster(clusdata);
145 void AliPMDClustering::order(int idet)
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
153 // define arrays dd and iord1
154 for(i1=0; i1 < ndimx; i1++){
155 for(i2=0; i2 < ndimy; i2++){
157 iord1[i]=i; dd[i]=d[i1][i2];
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){
166 for(i2=j-1; i2 >= i1 ; i2=i2--){
168 iord1[i2+1]=iord1[i2];
170 dd[i1]=adum; iord1[i1]=idum;
174 // store the sorted information in iord for later use
175 for(i=0; i<nmx; i++){
176 j=iord1[i]; i2=j/ndimx;
183 void AliPMDClustering::refclust(int incr, int supmod, int idet)
185 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
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);
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;
207 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
212 for(i=0; i<nsupcl; i++){
216 // one cell super-clusters --> single cluster
217 // cluster center at the centyer of the cell
218 // cluster radius = half cell dimension
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
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;
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
263 x[0]=coord[0][i1][i2];
264 y[0]=coord[1][i1][i2];
267 for(j=1;j<=ncl[i];j++){
272 x[j]=coord[0][i1][i2];
273 y[j]=coord[1][i1][i2];
276 // arranging cells within supercluster in decreasing order
277 for(j=1;j<=ncl[i];j++){
278 itest=0; ihld=iord[j];
280 if(itest == 0 && z[iord[i1]] < z[ihld]){
282 for(i2=j-1;i2>=i1;i2--){
289 // compute the number of Gaussians and their centers ( first
291 // centers must be separated by cells having smaller ener. dep.
292 // neighbouring centers should be either strong or well-separated
297 for(j=1;j<=ncl[i];j++){
303 rr=Dist(x1,y1,x2,y2);
304 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
306 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
308 if( rr >= 2.1)itest=itest+1;
317 // for(j=0; j<=ig; j++){
318 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
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]);
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++){
330 for(j=0; j<=ncl[i]; j++){
333 for(k=0; k<=ig; k++){
334 dist=Dist(x[j], y[j], xc[k], yc[k]);
335 if(dist < sqrt(3.) ){
348 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
351 for(k=1; k<=lev1[0]; k++){
354 for(k=1; k<=lev1[0]; k++){
355 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
359 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
362 for(k=1; k<=lev2[0]; k++){
365 for(k=1; k<=lev2[0]; k++){
366 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
372 for(j=0; j<=ig; j++){
374 clusters[0][clno]=xc[j];
375 clusters[1][clno]=yc[j];
376 clusters[2][clno]=zc[j];
377 clusters[4][clno]=rc[j];
379 clusters[3][clno]=ncl[i];
381 clusters[3][clno]=cells[j];
390 void AliPMDClustering::gaussfit(int ncell, int nclust, double &x, double &y ,double &z, double &xc, double &yc, double &zc, double &rc)
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];
397 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
398 double x1, x2, y1, y2;
404 j = 0; // Just put not to see the compiler warning, BKN
407 for(i=0; i<=ncell; i++){
413 for(i=0; i<=nclust; i++){
421 for(i=0; i<=nclust; i++){
422 zzc[i]=str/str1*zzc[i];
430 for(i=0; i<=ncell; i++){
434 for(j=0; j<=nclust; j++){
437 if(Dist(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
443 for(i1=0; i1<=ncell; i1++){
446 for(i2=1; i2<=idd; i2++){
450 dum=rrc[j]*rrc[jj]+rr*rr;
451 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
453 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
456 if(nclust > 20)jmax=20000;
457 for(j=0; j<jmax; j++){
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);
464 d[i]=rrc[i]*(1.+(ranmar()-0.5)*0.1);
465 if(d[i] < 0.25)d[i]=0.25;
467 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
469 for(i1=0; i1<=ncell; i1++){
472 for(i2=1; i2<=idd; i2++){
476 dum=d[jj]*d[jj]+rr*rr;
477 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
479 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
483 for(i2=0; i2<=nclust; i2++){
493 for(j=0; j<=nclust; j++){
502 double AliPMDClustering::Dist(double x1, double y1, double x2, double y2)
504 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
508 int AliPMDClustering::crclust(double ave, double cutoff, int nmx1, int idet)
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++){
525 for(i=0; i < nmx; i++){
529 if(d[id1][id2] <= cutoff){infocl[0][id1][id2]=-1;}
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 // ---------------------------------------------------------------
537 for(icell=0; icell <= nmx1; 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 // ---------------------------------------------------------------
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 // ---------------------------------------------------------------
564 if( (jd1 >= 0 && jd1 < 72) && (jd2 >= 0 && jd2 < 72) &&
565 infocl[0][jd1][jd2] == 0){
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;
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){
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;
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;
605 // for(icell=0; icell<=cellcount; icell++){
606 // ofl0 << infcl[0][icell] << " " << infcl[1][icell] << " " <<
607 // infcl[2][icell] << endl;
612 double AliPMDClustering::ranmar()
614 /* C==========================C*/
615 /*===================================C==========================*/
616 /* Universal random number generator proposed by Marsaglia and Zaman
617 in report FSU-SCRI-87-50 */
621 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
622 static double u[97], c, cd, cm, s, t;
624 int count1,count2,idum;
625 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
627 //*******************************************************
628 // following three lines if the seed to be provided by computer
629 // start = time(NULL);
632 //*******************************************************
633 //following two lines for fixed seed ( during testing only. Else
634 //use preceeing three lines
637 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
638 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
640 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
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;
645 while ( count1 < 97 ){
649 while( count2 < 24 ){
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;
661 c = 362436./16777216.; cd = 7654321./16777216.;
662 cm = 16777213./16777216.;
665 cout << " wrong initialization " << endl;
669 uni = u[i] - u[j]; if( uni < 0.) uni = uni + 1; u[i] = uni;
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.;
679 void AliPMDClustering::ConvertL2G(int smnumber, double xcell, double ycell, double &xpos, double &ypos)
681 float xreal = -999., yreal = -999.;
682 float cell_rad=0.25, celldia_x=0.5, celldia_y=0.4330127;
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;
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
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,
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);
720 if (smnumber>8 && smnumber<=17)
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);
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);
742 void AliPMDClustering::cell_pos(Int_t isup, Int_t j, int k, Float_t &xp, Float_t &yp){
745 This converts PMD cluster or CELL coordinates
746 to Global coordinates.
747 Written by Prof. S.C. Phatak
751 Float_t celldia = 0.5;
752 const Float_t pi = 3.14159;
753 const double sqrth=0.8660254; // sqrth = sqrt(3.)/2.
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
762 (xp0,yp0) corner positions of all supermodules in global
763 coordinate system. That is the origin
764 of the local ( supermodule ) coordinate system.
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,
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,
788 angles of rotation for three sets of supermodules
789 The angle is same for first nine, next nine and last nine
793 Float_t th[3] = {0., -2.*pi/3., 2.*pi/3.};
794 Float_t xr, yr, xinit, yinit, cs, sn;
797 xinit and yinit are coordinates of the cell in local coordinate system
800 xinit = (j)*celldia+(k)/2.*celldia;
801 yinit = sqrth*(k)/2.;
808 xr=cs*xinit+sn*yinit;
809 yr=-sn*xinit+cs*yinit;
817 void AliPMDClustering::SetMessage(Int_t imessage)