1 /***************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 //-----------------------------------------------------//
18 // Source File : PMDClustering.cxx, Version 00 //
20 // Date : September 26 2002 //
22 // clustering code for alice pmd //
24 //-----------------------------------------------------//
27 --------------------------------------------------------------------
28 Code developed by S. C. Phatak, Institute of Physics,
29 Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
30 ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
31 builds up superclusters and breaks them into clusters. The input is
32 in array d[ndimx][ndimy] and cluster information is in array
33 clusters[5][5000]. integer clno gives total number of clusters in the
36 d, clno and clusters are the only global ( public ) variables. Others
37 are local ( private ) to the code.
39 At the moment, the data is read for whole detector ( all supermodules
40 and pmd as well as cpv. This will have to be modify later )
42 LAST UPDATE : October 23, 2002
43 -----------------------------------------------------------------------
49 #include <TObjArray.h>
50 #include "AliPMDcluster.h"
51 #include "AliPMDClustering.h"
54 ClassImp(AliPMDClustering)
56 const double AliPMDClustering::pi=3.141593;
57 const double AliPMDClustering::sqrth=0.8660254; // sqrth = sqrt(3.)/2.
60 AliPMDClustering::AliPMDClustering()
64 for(int i = 0; i < ndimx; i++)
66 for(int j = 0; j < ndimy; j++)
68 coord[0][i][j] = i+j/2.;
69 coord[1][i][j] = sqrth*j;
73 AliPMDClustering::~AliPMDClustering()
78 void AliPMDClustering::DoClust(double celladc[48][96], TObjArray *pmdcont)
81 AliPMDcluster *pmdcl = 0;
83 int i, i1, i2, j, nmx1, incr;
87 const float twobysqrt3 = 1.1547; // 2./sqrt(3.)
90 for (i = 0; i < ndimx; i++)
92 for (j = 0; j < ndimy; j++)
94 d[i][j] = celladc[i][j];
97 order(); // order the data
98 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
106 if (d[i1][i2] > 0.) {ave=ave+d[i1][i2];}
107 if (d[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
109 // nmx1 --- number of cells having ener dep >= cutoff
112 cout << " nmx1 " << nmx1 << endl;
117 cout <<"nmx " << nmx << " nmx1 " << nmx1<< " ave "<<ave<<
118 " cutoff " << cutoff << endl;
121 incr = crclust(ave, cutoff, nmx1);
127 cout << "clno " << clno << endl;
130 for(i1=0; i1<clno; i1++)
132 float clu_xc = (float) clusters[0][i1];
133 float clu_yc = (float) clusters[1][i1];
134 float clu_adc = (float) clusters[2][i1];
135 float clu_cells = (float) clusters[3][i1];
136 float clu_rad = (float) clusters[4][i1];
138 float clu_y0 = twobysqrt3*clu_yc;
139 float clu_x0 = clu_xc - clu_y0/2.;
141 clusdata[0] = clu_x0;
142 clusdata[1] = clu_y0;
143 clusdata[2] = clu_adc;
144 clusdata[3] = clu_cells;
145 clusdata[4] = clu_rad;
147 pmdcl = new AliPMDcluster(clusdata);
154 void AliPMDClustering::order()
157 double dd[nmx], adum;// matrix d converted into
158 // one dimensional array dd. adum a place holder for double
159 int i, j, i1, i2, iord1[nmx], itst, idum; // information of
160 // ordering is stored in iord1, original array not ordered
162 // define arrays dd and iord1
163 for(i1=0; i1 < ndimx; i1++){
164 for(i2=0; i2 < ndimy; i2++){
166 iord1[i]=i; dd[i]=d[i1][i2];
169 // sort and store sorting information in iord1
170 for(j=1; j < nmx; j++){
171 itst=0; adum=dd[j]; idum=iord1[j];
172 for(i1=0; i1 < j ; i1++){
173 if(adum > dd[i1] && itst == 0){
175 for(i2=j-1; i2 >= i1 ; i2=i2--){
177 iord1[i2+1]=iord1[i2];
179 dd[i1]=adum; iord1[i1]=idum;
183 // store the sorted information in iord for later use
184 for(i=0; i<nmx; i++){
193 int AliPMDClustering::crclust(double ave, double cutoff, int nmx1)
195 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
196 int jd1,jd2, icell, cellcount;
197 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
198 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
199 // cell. There are six neighbours.
200 // cellcount --- total number of cells having nonzero ener dep
201 // numcell --- number of cells in a given supercluster
202 //ofstream ofl0("cells_loc",ios::out);
203 // initialize infocl[2][ndimx][ndimy]
207 printf(" *** Inside crclust ** nmx = %d nmx1 = %d ndimx = %d ndimy = %d ave = %f cutoff = %f\n",
208 nmx,nmx1,ndimx,ndimy,ave,cutoff);
210 for (j=0; j < ndimx; j++){
211 for(k=0; k < ndimy; k++){
216 for(i=0; i < nmx; i++){
220 if(d[id1][id2] <= cutoff){infocl[0][id1][id2]=-1;}
222 // ---------------------------------------------------------------
223 // crude clustering begins. Start with cell having largest adc
224 // count and loop over the cells in descending order of adc count
225 // ---------------------------------------------------------------
228 for(icell=0; icell <= nmx1; icell++){
231 if(infocl[0][id1][id2] == 0 ){
232 // ---------------------------------------------------------------
233 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
234 // coordinates of the cells in a cluster, infocl[0][i1][i2] is 1 for
235 // primary and 2 for secondary cells,
236 // infocl[1][i1][i2] stores cluster #
237 // ---------------------------------------------------------------
240 cellcount=cellcount+1;
241 infocl[0][id1][id2]=1;
242 infocl[1][id1][id2]=icl;
243 infcl[0][cellcount]=icl;
244 infcl[1][cellcount]=id1;
245 infcl[2][cellcount]=id2;
248 clust[0][numcell]=id1;
249 clust[1][numcell]=id2;
250 for(i=1; i<5000; i++)clust[0][i]=0;
251 // ---------------------------------------------------------------
252 // check for adc count in neib. cells. If ne 0 put it in this clust
253 // ---------------------------------------------------------------
257 if( (jd1 >= 0 && jd1 < ndimx) && (jd2 >= 0 && jd2 < ndimy) &&
258 infocl[0][jd1][jd2] == 0){
260 infocl[0][jd1][jd2]=2;
261 infocl[1][jd1][jd2]=icl;
262 clust[0][numcell]=jd1;
263 clust[1][numcell]=jd2;
264 cellcount=cellcount+1;
265 infcl[0][cellcount]=icl;
266 infcl[1][cellcount]=jd1;
267 infcl[2][cellcount]=jd2;
270 // ---------------------------------------------------------------
271 // check adc count for neighbour's neighbours recursively and
272 // if nonzero, add these to the cluster.
273 // ---------------------------------------------------------------
274 for(i=1;i < 5000;i++){
275 if(clust[0][i] != 0){
281 if( (jd1 >= 0 && jd1 < ndimx) && (jd2 >= 0 && jd2 < ndimy) &&
282 infocl[0][jd1][jd2] == 0 ){
283 infocl[0][jd1][jd2]=2;
284 infocl[1][jd1][jd2]=icl;
286 clust[0][numcell]=jd1;
287 clust[1][numcell]=jd2;
288 cellcount=cellcount+1;
289 infcl[0][cellcount]=icl;
290 infcl[1][cellcount]=jd1;
291 infcl[2][cellcount]=jd2;
298 // for(icell=0; icell<=cellcount; icell++){
299 // ofl0 << infcl[0][icell] << " " << infcl[1][icell] << " " <<
300 // infcl[2][icell] << endl;
305 void AliPMDClustering::refclust(int incr)
307 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
309 int ig, nsupcl, lev1[20], lev2[20];
310 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
311 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
312 // clno counts the final clusters
313 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
314 // x, y and z store (x,y) coordinates of and energy deposited in a cell
315 // xc, yc store (x,y) coordinates of the cluster center
316 // zc stores the energy deposited in a cluster
317 // rc is cluster radius
318 // finally the cluster information is put in 2-dimensional array clusters
319 // ofstream ofl1("checking.5",ios::app);
322 for(i=0; i<4500; i++){ncl[i]=-1;}
323 for(i=0; i<incr; i++){
324 if(infcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
325 ncl[nsupcl]=ncl[nsupcl]+1;
329 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
334 for(i=0; i<nsupcl; i++){
338 // one cell super-clusters --> single cluster
339 // cluster center at the centyer of the cell
340 // cluster radius = half cell dimension
344 clusters[0][clno]=coord[0][i1][i2];
345 clusters[1][clno]=coord[1][i1][i2];
346 clusters[2][clno]=d[i1][i2];
347 clusters[3][clno]=1.;
348 clusters[4][clno]=0.5;
349 //ofl1 << icl << " " << coord[0][i1][i2] << " " << coord[1][i1][i2] <<
350 //" " << d[i1][i2] << " " << clusters[3][clno] <<endl;
351 }else if(ncl[i] == 1){
352 // two cell super-cluster --> single cluster
353 // cluster center is at ener. dep.-weighted mean of two cells
354 // cluster radius == half cell dimension
369 clusters[0][clno]=(x1*z1+x2*z2)/(z1+z2);
370 clusters[1][clno]=(y1*z1+y2*z2)/(z1+z2);
371 clusters[2][clno]=z1+z2;
372 clusters[3][clno]=2.;
373 clusters[4][clno]=0.5;
374 //ofl1 << icl << " " << clusters[0][clno] << " " << clusters[1][clno]
375 // << " " << clusters[2][clno] << " " <<clusters[3][clno] <<endl;
380 // super-cluster of more than two cells - broken up into smaller
381 // clusters gaussian centers computed. (peaks separated by > 1 cell)
382 // Begin from cell having largest energy deposited This is first
386 x[0]=coord[0][i1][i2];
387 y[0]=coord[1][i1][i2];
390 for(j=1;j<=ncl[i];j++){
396 x[j]=coord[0][i1][i2];
397 y[j]=coord[1][i1][i2];
400 // arranging cells within supercluster in decreasing order
401 for(j=1;j<=ncl[i];j++){
402 itest=0; ihld=iord[j];
404 if(itest == 0 && z[iord[i1]] < z[ihld]){
406 for(i2=j-1;i2>=i1;i2--){
415 // compute the number of Gaussians and their centers ( first
417 // centers must be separated by cells having smaller ener. dep.
418 // neighbouring centers should be either strong or well-separated
423 for(j=1;j<=ncl[i];j++){
429 rr=Dist(x1,y1,x2,y2);
430 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
432 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
434 if( rr >= 2.1)itest=itest+1;
443 // for(j=0; j<=ig; j++){
444 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
446 // gaussfit to adjust cluster parameters to minimize
447 gaussfit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
449 // compute the number of cells belonging to each cluster.
450 // cell is shared between several clusters ( if they are equidistant
451 // from it ) in the ratio of cluster energy deposition
452 for(j=0; j<=ig; j++){
456 for(j=0; j<=ncl[i]; j++){
459 for(k=0; k<=ig; k++){
460 dist=Dist(x[j], y[j], xc[k], yc[k]);
461 if(dist < sqrt(3.) ){
474 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
477 for(k=1; k<=lev1[0]; k++){
480 for(k=1; k<=lev1[0]; k++){
481 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
485 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
488 for(k=1; k<=lev2[0]; k++){
491 for(k=1; k<=lev2[0]; k++){
492 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
498 for(j=0; j<=ig; j++){
500 clusters[0][clno]=xc[j];
501 clusters[1][clno]=yc[j];
502 clusters[2][clno]=zc[j];
503 clusters[4][clno]=rc[j];
505 clusters[3][clno]=ncl[i];
507 clusters[3][clno]=cells[j];
514 void AliPMDClustering::gaussfit(int ncell, int nclust, double &x, double &y ,double &z, double &xc, double &yc, double &zc, double &rc)
516 int i, j, i1, i2, jmax, novar, idd, jj;
517 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
518 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
519 double hc[4500], hd[4500], zzc[4500], rrc[4500];
521 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
522 double x1, x2, y1, y2;
528 j = 0; // Just put not to see the compiler warning, BKN
531 for(i=0; i<=ncell; i++){
537 for(i=0; i<=nclust; i++){
545 for(i=0; i<=nclust; i++){
546 zzc[i]=str/str1*zzc[i];
554 for(i=0; i<=ncell; i++){
558 for(j=0; j<=nclust; j++){
561 if(Dist(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
567 for(i1=0; i1<=ncell; i1++){
570 for(i2=1; i2<=idd; i2++){
574 dum=rrc[j]*rrc[jj]+rr*rr;
575 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
577 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
580 if(nclust > 20)jmax=20000;
581 for(j=0; j<jmax; j++){
583 for(i=0; i<=nclust; i++){
584 a[i]=xxc[i]+0.6*(ranmar()-0.5);
585 b[i]=yyc[i]+0.6*(ranmar()-0.5);
586 c[i]=zzc[i]*(1.+(ranmar()-0.5)*0.2);
588 d[i]=rrc[i]*(1.+(ranmar()-0.5)*0.1);
589 if(d[i] < 0.25)d[i]=0.25;
591 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
593 for(i1=0; i1<=ncell; i1++){
596 for(i2=1; i2<=idd; i2++){
600 dum=d[jj]*d[jj]+rr*rr;
601 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
603 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
607 for(i2=0; i2<=nclust; i2++){
617 for(j=0; j<=nclust; j++){
626 double AliPMDClustering::Dist(double x1, double y1, double x2, double y2)
628 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
631 double AliPMDClustering::ranmar()
633 /* C==========================C*/
634 /*===================================C==========================*/
635 /* Universal random number generator proposed by Marsaglia and Zaman
636 in report FSU-SCRI-87-50 */
640 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
641 static double u[97], c, cd, cm, s, t;
643 int count1,count2,idum;
644 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
646 //*******************************************************
647 // following three lines if the seed to be provided by computer
648 // start = time(NULL);
651 //*******************************************************
652 //following two lines for fixed seed ( during testing only. Else
653 //use preceeing three lines
656 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
657 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
659 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
661 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
662 i4 = jj - (i3-1)*169;
664 while ( count1 < 97 ){
668 while( count2 < 24 ){
670 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
671 i5=idum-(idum/179)*179;
672 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
673 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
680 c = 362436./16777216.; cd = 7654321./16777216.;
681 cm = 16777213./16777216.;
684 cout << " wrong initialization " << endl;
688 uni = u[i] - u[j]; if( uni < 0.) uni = uni + 1; u[i] = uni;
690 if( i < 0 ) i = 96; j = j - 1; if ( j < 0 ) j = 96; c = c - cd;
691 if( c < 0. ) c = c+cm; uni = uni-c ; if( uni < 0. )uni = uni+1.;
698 void AliPMDClustering::SetEdepCut(Float_t decut)
702 void AliPMDClustering::SetDebug(Int_t idebug)