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 fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
33 fClusters[5][5000]. integer fClno gives total number of clusters in the
36 fEdepCell, fClno and fClusters are the only global ( public ) variables.
37 Others 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 -----------------------------------------------------------------------
47 #include "Riostream.h"
49 #include <TObjArray.h>
50 #include "AliPMDcluster.h"
51 #include "AliPMDClustering.h"
54 ClassImp(AliPMDClustering)
56 const Double_t AliPMDClustering::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
58 AliPMDClustering::AliPMDClustering()
62 for(int i = 0; i < kNDIMX; i++)
64 for(int j = 0; j < kNDIMY; j++)
66 fCoord[0][i][j] = i+j/2.;
67 fCoord[1][i][j] = fgkSqroot3by2*j;
71 AliPMDClustering::~AliPMDClustering()
76 void AliPMDClustering::DoClust(Double_t celladc[48][96], TObjArray *pmdcont)
78 // main function to call other necessary functions to do clustering
80 AliPMDcluster *pmdcl = 0;
82 int i, i1, i2, j, nmx1, incr;
86 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
89 for (i = 0; i < kNDIMX; i++)
91 for (j = 0; j < kNDIMY; j++)
93 fEdepCell[i][j] = celladc[i][j];
96 Order(); // order the data
97 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
105 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
106 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
108 // nmx1 --- number of cells having ener dep >= cutoff
111 cout << " nmx1 " << nmx1 << endl;
116 cout <<"kNMX " << kNMX << " nmx1 " << nmx1<< " ave "<<ave<<
117 " cutoff " << cutoff << endl;
120 incr = CrClust(ave, cutoff, nmx1);
126 cout << "fClno " << fClno << endl;
129 for(i1=0; i1<fClno; i1++)
131 Float_t cluXC = (Float_t) fClusters[0][i1];
132 Float_t cluYC = (Float_t) fClusters[1][i1];
133 Float_t cluADC = (Float_t) fClusters[2][i1];
134 Float_t cluCELLS = (Float_t) fClusters[3][i1];
135 Float_t cluRAD = (Float_t) fClusters[4][i1];
136 Float_t cluY0 = ktwobysqrt3*cluYC;
137 Float_t cluX0 = cluXC - cluY0/2.;
140 clusdata[2] = cluADC;
141 clusdata[3] = cluCELLS;
142 clusdata[4] = cluRAD;
144 pmdcl = new AliPMDcluster(clusdata);
151 void AliPMDClustering::Order()
154 // sorts the ADC values from higher to lower
156 double dd[kNMX], adum;
157 // matrix fEdepCell converted into
158 // one dimensional array dd. adum a place holder for double
159 int i, j, i1, i2, iord1[kNMX], itst, idum;
161 // ordering is stored in iord1, original array not ordered
163 // define arrays dd and iord1
164 for(i1=0; i1 < kNDIMX; i1++)
166 for(i2=0; i2 < kNDIMY; i2++)
170 dd[i] = fEdepCell[i1][i2];
173 // sort and store sorting information in iord1
174 for(j=1; j < kNMX; j++)
179 for(i1=0; i1 < j ; i1++)
181 if(adum > dd[i1] && itst == 0)
184 for(i2=j-1; i2 >= i1 ; i2=i2--)
187 iord1[i2+1] = iord1[i2];
194 // store the sorted information in fIord for later use
195 for(i=0; i<kNMX; i++)
205 int AliPMDClustering::CrClust(double ave, double cutoff, int nmx1)
207 // Does crude clustering
208 // Finds out only the big patch by just searching the
211 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
212 int jd1,jd2, icell, cellcount;
213 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
214 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
215 // cell. There are six neighbours.
216 // cellcount --- total number of cells having nonzero ener dep
217 // numcell --- number of cells in a given supercluster
218 // ofstream ofl0("cells_loc",ios::out);
219 // initialize fInfocl[2][kNDIMX][kNDIMY]
223 printf(" *** Inside CrClust ** kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f\n",
224 kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff);
226 for (j=0; j < kNDIMX; j++){
227 for(k=0; k < kNDIMY; k++){
228 fInfocl[0][j][k] = 0;
229 fInfocl[1][j][k] = 0;
232 for(i=0; i < kNMX; i++){
236 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
238 // ---------------------------------------------------------------
239 // crude clustering begins. Start with cell having largest adc
240 // count and loop over the cells in descending order of adc count
241 // ---------------------------------------------------------------
244 for(icell=0; icell <= nmx1; icell++){
247 if(fInfocl[0][id1][id2] == 0 ){
248 // ---------------------------------------------------------------
249 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
250 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
251 // primary and 2 for secondary cells,
252 // fInfocl[1][i1][i2] stores cluster #
253 // ---------------------------------------------------------------
256 cellcount=cellcount+1;
257 fInfocl[0][id1][id2]=1;
258 fInfocl[1][id1][id2]=icl;
259 fInfcl[0][cellcount]=icl;
260 fInfcl[1][cellcount]=id1;
261 fInfcl[2][cellcount]=id2;
264 clust[0][numcell]=id1;
265 clust[1][numcell]=id2;
266 for(i=1; i<5000; i++)clust[0][i]=0;
267 // ---------------------------------------------------------------
268 // check for adc count in neib. cells. If ne 0 put it in this clust
269 // ---------------------------------------------------------------
273 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
274 fInfocl[0][jd1][jd2] == 0){
276 fInfocl[0][jd1][jd2]=2;
277 fInfocl[1][jd1][jd2]=icl;
278 clust[0][numcell]=jd1;
279 clust[1][numcell]=jd2;
280 cellcount=cellcount+1;
281 fInfcl[0][cellcount]=icl;
282 fInfcl[1][cellcount]=jd1;
283 fInfcl[2][cellcount]=jd2;
286 // ---------------------------------------------------------------
287 // check adc count for neighbour's neighbours recursively and
288 // if nonzero, add these to the cluster.
289 // ---------------------------------------------------------------
290 for(i=1;i < 5000;i++){
291 if(clust[0][i] != 0){
297 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
298 fInfocl[0][jd1][jd2] == 0 ){
299 fInfocl[0][jd1][jd2] = 2;
300 fInfocl[1][jd1][jd2] = icl;
301 numcell = numcell + 1;
302 clust[0][numcell] = jd1;
303 clust[1][numcell] = jd2;
304 cellcount = cellcount+1;
305 fInfcl[0][cellcount] = icl;
306 fInfcl[1][cellcount] = jd1;
307 fInfcl[2][cellcount] = jd2;
314 // for(icell=0; icell<=cellcount; icell++){
315 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
316 // fInfcl[2][icell] << endl;
321 void AliPMDClustering::RefClust(int incr)
323 // Does the refining of clusters
324 // Takes the big patch and does gaussian fitting and
325 // finds out the more refined clusters
327 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
329 int ig, nsupcl, lev1[20], lev2[20];
330 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
331 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
332 // fClno counts the final clusters
333 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
334 // x, y and z store (x,y) coordinates of and energy deposited in a cell
335 // xc, yc store (x,y) coordinates of the cluster center
336 // zc stores the energy deposited in a cluster
337 // rc is cluster radius
338 // finally the cluster information is put in 2-dimensional array clusters
339 // ofstream ofl1("checking.5",ios::app);
342 for(i=0; i<4500; i++){ncl[i]=-1;}
343 for(i=0; i<incr; i++){
344 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
345 ncl[nsupcl]=ncl[nsupcl]+1;
349 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
354 for(i=0; i<nsupcl; i++){
358 // one cell super-clusters --> single cluster
359 // cluster center at the centyer of the cell
360 // cluster radius = half cell dimension
364 fClusters[0][fClno] = fCoord[0][i1][i2];
365 fClusters[1][fClno] = fCoord[1][i1][i2];
366 fClusters[2][fClno] = fEdepCell[i1][i2];
367 fClusters[3][fClno] = 1.;
368 fClusters[4][fClno] = 0.5;
369 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
370 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
371 }else if(ncl[i] == 1){
372 // two cell super-cluster --> single cluster
373 // cluster center is at ener. dep.-weighted mean of two cells
374 // cluster radius == half cell dimension
380 x1 = fCoord[0][i1][i2];
381 y1 = fCoord[1][i1][i2];
382 z1 = fEdepCell[i1][i2];
386 x2 = fCoord[0][i1][i2];
387 y2 = fCoord[1][i1][i2];
388 z2 = fEdepCell[i1][i2];
389 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
390 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
391 fClusters[2][fClno] = z1+z2;
392 fClusters[3][fClno] = 2.;
393 fClusters[4][fClno] = 0.5;
394 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
395 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
400 // super-cluster of more than two cells - broken up into smaller
401 // clusters gaussian centers computed. (peaks separated by > 1 cell)
402 // Begin from cell having largest energy deposited This is first
406 x[0] = fCoord[0][i1][i2];
407 y[0] = fCoord[1][i1][i2];
408 z[0] = fEdepCell[i1][i2];
410 for(j=1;j<=ncl[i];j++){
416 x[j] = fCoord[0][i1][i2];
417 y[j] = fCoord[1][i1][i2];
418 z[j] = fEdepCell[i1][i2];
420 // arranging cells within supercluster in decreasing order
421 for(j=1;j<=ncl[i];j++){
425 if(itest == 0 && z[iord[i1]] < z[ihld]){
427 for(i2=j-1;i2>=i1;i2--){
436 // compute the number of Gaussians and their centers ( first
438 // centers must be separated by cells having smaller ener. dep.
439 // neighbouring centers should be either strong or well-separated
444 for(j=1;j<=ncl[i];j++){
450 rr=Distance(x1,y1,x2,y2);
451 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
453 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
455 if( rr >= 2.1)itest=itest+1;
464 // for(j=0; j<=ig; j++){
465 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
467 // GaussFit to adjust cluster parameters to minimize
468 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
470 // compute the number of cells belonging to each cluster.
471 // cell is shared between several clusters ( if they are equidistant
472 // from it ) in the ratio of cluster energy deposition
473 for(j=0; j<=ig; j++){
477 for(j=0; j<=ncl[i]; j++){
480 for(k=0; k<=ig; k++){
481 dist=Distance(x[j], y[j], xc[k], yc[k]);
482 if(dist < sqrt(3.) ){
495 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
498 for(k=1; k<=lev1[0]; k++){
501 for(k=1; k<=lev1[0]; k++){
502 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
506 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
509 for(k=1; k<=lev2[0]; k++){
512 for(k=1; k<=lev2[0]; k++){
513 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
519 for(j=0; j<=ig; j++){
521 fClusters[0][fClno] = xc[j];
522 fClusters[1][fClno] = yc[j];
523 fClusters[2][fClno] = zc[j];
524 fClusters[4][fClno] = rc[j];
526 fClusters[3][fClno] = ncl[i];
528 fClusters[3][fClno] = cells[j];
535 void AliPMDClustering::GaussFit(Int_t ncell, Int_t nclust, Double_t &x, Double_t &y ,Double_t &z, Double_t &xc, Double_t &yc, Double_t &zc, Double_t &rc)
537 // Does gaussian fitting
539 int i, j, i1, i2, jmax, novar, idd, jj;
540 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
541 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
542 double hc[4500], hd[4500], zzc[4500], rrc[4500];
544 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
545 double x1, x2, y1, y2;
550 j = 0; // Just put not to see the compiler warning, BKN
552 for(i=0; i<=ncell; i++)
559 for(i=0; i<=nclust; i++)
564 str1 = str1 + zzc[i];
567 for(i=0; i<=nclust; i++)
569 zzc[i] = str/str1*zzc[i];
577 for(i=0; i<=ncell; i++){
581 for(j=0; j<=nclust; j++){
584 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
590 for(i1=0; i1<=ncell; i1++){
593 for(i2=1; i2<=idd; i2++){
597 dum=rrc[j]*rrc[jj]+rr*rr;
598 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
600 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
603 if(nclust > 20)jmax=20000;
604 for(j=0; j<jmax; j++){
606 for(i=0; i<=nclust; i++){
607 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
608 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
609 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
611 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
612 if(d[i] < 0.25)d[i]=0.25;
614 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
616 for(i1=0; i1<=ncell; i1++){
619 for(i2=1; i2<=idd; i2++){
623 dum=d[jj]*d[jj]+rr*rr;
624 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
626 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
630 for(i2=0; i2<=nclust; i2++){
640 for(j=0; j<=nclust; j++){
649 double AliPMDClustering::Distance(double x1, double y1, double x2, double y2)
651 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
654 double AliPMDClustering::Ranmar() const
656 // Universal random number generator proposed by Marsaglia and Zaman
657 // in report FSU-SCRI-87-50
661 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
662 static double u[97], c, cd, cm, s, t;
664 int count1,count2,idum;
665 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
667 //*******************************************************
668 // following three lines if the seed to be provided by computer
669 // start = time(NULL);
672 //*******************************************************
673 //following two lines for fixed seed ( during testing only. Else
674 //use preceeing three lines
677 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
678 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
680 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
682 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
683 i4 = jj - (i3-1)*169;
685 while ( count1 < 97 ){
689 while( count2 < 24 ){
691 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
692 i5=idum-(idum/179)*179;
693 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
694 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
701 c = 362436./16777216.; cd = 7654321./16777216.;
702 cm = 16777213./16777216.;
705 cout << " wrong initialization " << endl;
710 if( uni < 0.) uni = uni + 1;
717 if( c < 0. ) c = c+cm;
719 if( uni < 0. )uni = uni+1.;
724 void AliPMDClustering::SetEdepCut(Float_t decut)
728 void AliPMDClustering::SetDebug(Int_t idebug)