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 "AliPMDcluster.h"
36 #include "AliPMDClustering.h"
39 ClassImp(AliPMDClustering)
41 AliPMDClustering::AliPMDClustering()
44 for(int i = 0; i < ndimx; i++)
46 for(int j = 0; j < ndimy; j++)
48 coord[0][i][j] = i+j/2.;
49 coord[1][i][j] = sqrth*j;
53 AliPMDClustering::~AliPMDClustering()
58 //void AliPMDClustering::DoClust(int idet, int isup, double d1[72][72], AliPMDContainer *pmdc)
59 void AliPMDClustering::DoClust(int idet, int isup, double d1[72][72], TObjArray *pmdcont)
62 AliPMDcluster *pmdcl = 0;
64 int i, i1, i2, j, nmx1, incr;
68 const float twobysqrt3 = 1.1547; // 2./sqrt(3.)
72 cout << " supermodule no. " << idet << " " << isup << endl;
75 for (i = 0; i < ndimx; i++)
77 for (j = 0; j < ndimy; j++)
82 order(idet); // order the data
83 cutoff=400.; // cutoff used to discard cells having ener. dep.
91 if (d[i1][i2] > 0.) {ave=ave+d[i1][i2];}
92 if (d[i1][i2] >= cutoff ) nmx1 = nmx1 + 1;
94 // nmx1 --- number of cells having ener dep >= cutoff
97 cout << " nmx1 " << nmx1 << endl;
102 cout <<"nmx " << nmx << " nmx1 " << nmx1<< " ave "<<ave<<
103 " cutoff " << cutoff << endl;
106 incr = crclust(ave, cutoff, nmx1, idet);
108 refclust(incr, i, idet);
111 if(idet == 0)cout <<" supermodule ( pmd ) = "<< isup <<" done "
113 if(idet == 1)cout <<" supermodule ( cpv ) = "<< isup <<" done "
115 cout << "clno " << clno << endl;
119 for(i1=0; i1<clno; i1++)
121 float clu_xc = (float) clusters[0][i1];
122 float clu_yc = (float) clusters[1][i1];
123 float clu_adc = (float) clusters[2][i1];
124 float clu_cells = (float) clusters[3][i1];
125 float clu_rad = (float) clusters[4][i1];
127 float clu_y0 = twobysqrt3*clu_yc;
128 float clu_x0 = clu_xc - clu_y0/2.;
130 clusdata[0] = clu_cells;
131 clusdata[1] = clu_x0;
132 clusdata[2] = clu_y0;
133 clusdata[3] = clu_adc;
134 clusdata[4] = clu_rad;
136 pmdcl = new AliPMDcluster(clusdata);
144 void AliPMDClustering::order(int idet)
147 double dd[nmx], adum;// matrix d converted into
148 // one dimensional array dd. adum a place holder for double
149 int i, j, i1, i2, iord1[nmx], itst, idum; // information of
150 // ordering is stored in iord1, original array not ordered
152 // define arrays dd and iord1
153 for(i1=0; i1 < ndimx; i1++){
154 for(i2=0; i2 < ndimy; i2++){
156 iord1[i]=i; dd[i]=d[i1][i2];
159 // sort and store sorting information in iord1
160 for(j=1; j < nmx; j++){
161 itst=0; adum=dd[j]; idum=iord1[j];
162 for(i1=0; i1 < j ; i1++){
163 if(adum > dd[i1] && itst == 0){
165 for(i2=j-1; i2 >= i1 ; i2=i2--){
167 iord1[i2+1]=iord1[i2];
169 dd[i1]=adum; iord1[i1]=idum;
173 // store the sorted information in iord for later use
174 for(i=0; i<nmx; i++){
175 j=iord1[i]; i2=j/ndimx;
182 void AliPMDClustering::refclust(int incr, int supmod, int idet)
184 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
186 int ig, nsupcl, lev1[20], lev2[20];
187 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
188 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
189 // clno counts the final clusters
190 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
191 // x, y and z store (x,y) coordinates of and energy deposited in a cell
192 // xc, yc store (x,y) coordinates of the cluster center
193 // zc stores the energy deposited in a cluster
194 // rc is cluster radius
195 // finally the cluster information is put in 2-dimensional array clusters
196 // ofstream ofl1("checking.5",ios::app);
199 for(i=0; i<4500; i++){ncl[i]=-1;}
200 for(i=0; i<incr; i++){
201 if(infcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
202 ncl[nsupcl]=ncl[nsupcl]+1;
206 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
211 for(i=0; i<nsupcl; i++){
215 // one cell super-clusters --> single cluster
216 // cluster center at the centyer of the cell
217 // cluster radius = half cell dimension
221 clusters[0][clno]=coord[0][i1][i2];
222 clusters[1][clno]=coord[1][i1][i2];
223 clusters[2][clno]=d[i1][i2];
224 clusters[3][clno]=1.;
225 clusters[4][clno]=0.5;
226 //ofl1 << icl << " " << coord[0][i1][i2] << " " << coord[1][i1][i2] <<
227 //" " << d[i1][i2] << " " << clusters[3][clno] <<endl;
228 }else if(ncl[i] == 1){
229 // two cell super-cluster --> single cluster
230 // cluster center is at ener. dep.-weighted mean of two cells
231 // cluster radius == half cell dimension
246 clusters[0][clno]=(x1*z1+x2*z2)/(z1+z2);
247 clusters[1][clno]=(y1*z1+y2*z2)/(z1+z2);
248 clusters[2][clno]=z1+z2;
249 clusters[3][clno]=2.;
250 clusters[4][clno]=0.5;
251 //ofl1 << icl << " " << clusters[0][clno] << " " << clusters[1][clno]
252 // << " " << clusters[2][clno] << " " <<clusters[3][clno] <<endl;
256 // super-cluster of more than two cells - broken up into smaller
257 // clusters gaussian centers computed. (peaks separated by > 1 cell)
258 // Begin from cell having largest energy deposited This is first
262 x[0]=coord[0][i1][i2];
263 y[0]=coord[1][i1][i2];
266 for(j=1;j<=ncl[i];j++){
271 x[j]=coord[0][i1][i2];
272 y[j]=coord[1][i1][i2];
275 // arranging cells within supercluster in decreasing order
276 for(j=1;j<=ncl[i];j++){
277 itest=0; ihld=iord[j];
279 if(itest == 0 && z[iord[i1]] < z[ihld]){
281 for(i2=j-1;i2>=i1;i2--){
288 // compute the number of Gaussians and their centers ( first
290 // centers must be separated by cells having smaller ener. dep.
291 // neighbouring centers should be either strong or well-separated
296 for(j=1;j<=ncl[i];j++){
302 rr=Dist(x1,y1,x2,y2);
303 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
305 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
307 if( rr >= 2.1)itest=itest+1;
316 // for(j=0; j<=ig; j++){
317 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
319 // gaussfit to adjust cluster parameters to minimize
320 gaussfit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
322 // compute the number of cells belonging to each cluster.
323 // cell is shared between several clusters ( if they are equidistant
324 // from it ) in the ratio of cluster energy deposition
325 for(j=0; j<=ig; j++){
329 for(j=0; j<=ncl[i]; j++){
332 for(k=0; k<=ig; k++){
333 dist=Dist(x[j], y[j], xc[k], yc[k]);
334 if(dist < sqrt(3.) ){
347 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
350 for(k=1; k<=lev1[0]; k++){
353 for(k=1; k<=lev1[0]; k++){
354 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
358 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
361 for(k=1; k<=lev2[0]; k++){
364 for(k=1; k<=lev2[0]; k++){
365 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
371 for(j=0; j<=ig; j++){
373 clusters[0][clno]=xc[j];
374 clusters[1][clno]=yc[j];
375 clusters[2][clno]=zc[j];
376 clusters[4][clno]=rc[j];
378 clusters[3][clno]=ncl[i];
380 clusters[3][clno]=cells[j];
389 void AliPMDClustering::gaussfit(int ncell, int nclust, double &x, double &y ,double &z, double &xc, double &yc, double &zc, double &rc)
391 int i, j, i1, i2, jmax, novar, idd, jj;
392 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
393 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
394 double hc[4500], hd[4500], zzc[4500], rrc[4500];
396 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
397 double x1, x2, y1, y2;
403 j = 0; // Just put not to see the compiler warning, BKN
406 for(i=0; i<=ncell; i++){
412 for(i=0; i<=nclust; i++){
420 for(i=0; i<=nclust; i++){
421 zzc[i]=str/str1*zzc[i];
429 for(i=0; i<=ncell; i++){
433 for(j=0; j<=nclust; j++){
436 if(Dist(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
442 for(i1=0; i1<=ncell; i1++){
445 for(i2=1; i2<=idd; i2++){
449 dum=rrc[j]*rrc[jj]+rr*rr;
450 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
452 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
455 if(nclust > 20)jmax=20000;
456 for(j=0; j<jmax; j++){
458 for(i=0; i<=nclust; i++){
459 a[i]=xxc[i]+0.6*(ranmar()-0.5);
460 b[i]=yyc[i]+0.6*(ranmar()-0.5);
461 c[i]=zzc[i]*(1.+(ranmar()-0.5)*0.2);
463 d[i]=rrc[i]*(1.+(ranmar()-0.5)*0.1);
464 if(d[i] < 0.25)d[i]=0.25;
466 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
468 for(i1=0; i1<=ncell; i1++){
471 for(i2=1; i2<=idd; i2++){
475 dum=d[jj]*d[jj]+rr*rr;
476 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
478 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
482 for(i2=0; i2<=nclust; i2++){
492 for(j=0; j<=nclust; j++){
501 double AliPMDClustering::Dist(double x1, double y1, double x2, double y2)
503 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
507 int AliPMDClustering::crclust(double ave, double cutoff, int nmx1, int idet)
509 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
510 int jd1,jd2, icell, cellcount;
511 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
512 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
513 // cell. There are six neighbours.
514 // cellcount --- total number of cells having nonzero ener dep
515 // numcell --- number of cells in a given supercluster
516 //ofstream ofl0("cells_loc",ios::out);
517 // initialize infocl[2][ndimx][ndimy]
518 for (j=0; j < 72; j++){
519 for(k=0; k < 72; k++){
524 for(i=0; i < nmx; i++){
528 if(d[id1][id2] <= cutoff){infocl[0][id1][id2]=-1;}
530 // ---------------------------------------------------------------
531 // crude clustering begins. Start with cell having largest adc
532 // count and loop over the cells in descending order of adc count
533 // ---------------------------------------------------------------
536 for(icell=0; icell <= nmx1; icell++){
539 if(infocl[0][id1][id2] == 0 ){
540 // ---------------------------------------------------------------
541 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
542 // coordinates of the cells in a cluster, infocl[0][i1][i2] is 1 for
543 // primary and 2 for secondary cells,
544 // infocl[1][i1][i2] stores cluster #
545 // ---------------------------------------------------------------
548 cellcount=cellcount+1;
549 infocl[0][id1][id2]=1;
550 infocl[1][id1][id2]=icl;
551 infcl[0][cellcount]=icl;
552 infcl[1][cellcount]=id1;
553 infcl[2][cellcount]=id2;
554 clust[0][numcell]=id1;
555 clust[1][numcell]=id2;
556 for(i=1; i<5000; i++)clust[0][i]=0;
557 // ---------------------------------------------------------------
558 // check for adc count in neib. cells. If ne 0 put it in this clust
559 // ---------------------------------------------------------------
563 if( (jd1 >= 0 && jd1 < 72) && (jd2 >= 0 && jd2 < 72) &&
564 infocl[0][jd1][jd2] == 0){
566 infocl[0][jd1][jd2]=2;
567 infocl[1][jd1][jd2]=icl;
568 clust[0][numcell]=jd1;
569 clust[1][numcell]=jd2;
570 cellcount=cellcount+1;
571 infcl[0][cellcount]=icl;
572 infcl[1][cellcount]=jd1;
573 infcl[2][cellcount]=jd2;
576 // ---------------------------------------------------------------
577 // check adc count for neighbour's neighbours recursively and
578 // if nonzero, add these to the cluster.
579 // ---------------------------------------------------------------
580 for(i=1;i < 5000;i++){
581 if(clust[0][i] != 0){
587 if( (jd1 >= 0 && jd1 < 72) && (jd2 >= 0 && jd2 < 72) &&
588 infocl[0][jd1][jd2] == 0 ){
589 infocl[0][jd1][jd2]=2;
590 infocl[1][jd1][jd2]=icl;
592 clust[0][numcell]=jd1;
593 clust[1][numcell]=jd2;
594 cellcount=cellcount+1;
595 infcl[0][cellcount]=icl;
596 infcl[1][cellcount]=jd1;
597 infcl[2][cellcount]=jd2;
604 // for(icell=0; icell<=cellcount; icell++){
605 // ofl0 << infcl[0][icell] << " " << infcl[1][icell] << " " <<
606 // infcl[2][icell] << endl;
611 double AliPMDClustering::ranmar()
613 /* C==========================C*/
614 /*===================================C==========================*/
615 /* Universal random number generator proposed by Marsaglia and Zaman
616 in report FSU-SCRI-87-50 */
620 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
621 static double u[97], c, cd, cm, s, t;
623 int count1,count2,idum;
624 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
626 //*******************************************************
627 // following three lines if the seed to be provided by computer
628 // start = time(NULL);
631 //*******************************************************
632 //following two lines for fixed seed ( during testing only. Else
633 //use preceeing three lines
636 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
637 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
639 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
641 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
642 i4 = jj - (i3-1)*169;
644 while ( count1 < 97 ){
648 while( count2 < 24 ){
650 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
651 i5=idum-(idum/179)*179;
652 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
653 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
660 c = 362436./16777216.; cd = 7654321./16777216.;
661 cm = 16777213./16777216.;
664 cout << " wrong initialization " << endl;
668 uni = u[i] - u[j]; if( uni < 0.) uni = uni + 1; u[i] = uni;
670 if( i < 0 ) i = 96; j = j - 1; if ( j < 0 ) j = 96; c = c - cd;
671 if( c < 0. ) c = c+cm; uni = uni-c ; if( uni < 0. )uni = uni+1.;
678 void AliPMDClustering::ConvertL2G(int smnumber, double xcell, double ycell, double &xpos, double &ypos)
680 float xreal = -999., yreal = -999.;
681 float cell_rad=0.25, celldia_x=0.5, celldia_y=0.4330127;
683 float xoff1, xoff2, yoff=0.2886751, yoff3;
684 float xhex1 = -27.09375, yhex1 = -15.652584;
685 float xhex2 = 27.09375, yhex2 = -15.652584;
686 float xhex3 = 0.0, yhex3 = 31.285168;
691 9.435395, 45.560394, 81.685394, -8.627106,
692 27.497894, 63.622894, -26.689606, 9.435394,
693 45.560394, 9.435344, -8.627106, -26.689556,
694 45.560345, 27.497894, 9.435445, 81.685341,
695 63.622894, 45.560444, -18.870789, -36.933388,
696 -54.995991, -36.933189, -54.995789, -73.058388,
697 -54.995586, -73.058189, -91.120789
702 -16.342583, -16.34258, -16.34258, -47.627750, -47.627750,
703 -47.627750, -78.912918, -78.912918, -78.912918, 16.342611,
704 47.627808, 78.913002, 16.342554, 47.627750, 78.912949,
705 16.342495, 47.627693, 78.912888, -0.000116, -31.285227,
706 -62.570335, 31.285110, 0.000000, -31.285110, 62.570335,
712 xcon = xcorner[smnumber]+xhex1;
713 ycon = ycorner[smnumber]+yhex1;
714 xoff1 = celldia_x+(ycell-1)*cell_rad;
715 xreal = xcon+xoff1+celldia_x*(xcell-1);
716 yreal = ycon+yoff+celldia_y*(ycell-1);
719 if (smnumber>8 && smnumber<=17)
721 xcon = xcorner[smnumber]+xhex2;
722 ycon = ycorner[smnumber]+yhex2;
723 xoff2 = celldia_x+(xcell-1)*cell_rad;
724 xreal = xcon-(xoff2+celldia_x*(ycell-1));
725 yreal = ycon+yoff+celldia_y*(xcell-1);
730 xcon = xcorner[smnumber]+xhex3;
731 ycon = ycorner[smnumber]+yhex3;
732 yoff3 = celldia_x * 0.8660254 + cell_rad * 0.57735027;
733 xreal = xcon+(ycell-xcell)*cell_rad;
734 yreal = ycon-(yoff3+(xcell+ycell-2)*celldia_y);
741 void AliPMDClustering::cell_pos(Int_t isup, Int_t j, int k, Float_t &xp, Float_t &yp){
744 This converts PMD cluster or CELL coordinates
745 to Global coordinates.
746 Written by Prof. S.C. Phatak
750 Float_t celldia = 0.5;
751 const Float_t pi = 3.14159;
752 const double sqrth=0.8660254; // sqrth = sqrt(3.)/2.
754 isup --> supermodule no ( 0 - 26 )
755 idet --> detector ( pmd or cpv : not required now )
756 j --> xpad ( goes from 1 to 72 )
757 k --> ypad ( goes from 1 to 72 )
758 xp --> global x coordinate
759 yp --> global y coordinate
761 (xp0,yp0) corner positions of all supermodules in global
762 coordinate system. That is the origin
763 of the local ( supermodule ) coordinate system.
768 -17.9084, 18.2166, 54.3416, -35.9709, 0.154144,
769 36.2791, -54.0334, -17.9084, 18.2166, 36.7791,
770 18.7166, 0.654194, 72.9041, 54.8416, 36.7792,
771 109.029, 90.9666, 72.9042, -18.8708, -36.9334,
772 -54.996, -36.9332, -54.9958, -73.0584, -54.9956,
778 -32.1395, -32.1395, -32.1395, -63.4247, -63.4247,
779 -63.4247, -94.7098, -94.7098, -94.7098, 0.545689,
780 31.8309, 63.1161, 0.545632, 31.8308, 63.116,
781 0.545573, 31.8308, 63.116, 31.5737, 0.288616,
782 -30.9965, 62.859, 31.5738, 0.288733, 94.1442,
787 angles of rotation for three sets of supermodules
788 The angle is same for first nine, next nine and last nine
792 Float_t th[3] = {0., -2.*pi/3., 2.*pi/3.};
793 Float_t xr, yr, xinit, yinit, cs, sn;
796 xinit and yinit are coordinates of the cell in local coordinate system
799 xinit = (j)*celldia+(k)/2.*celldia;
800 yinit = sqrth*(k)/2.;
807 xr=cs*xinit+sn*yinit;
808 yr=-sn*xinit+cs*yinit;
816 void AliPMDClustering::SetMessage(Int_t imessage)