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 //-----------------------------------------------------//
26 /* --------------------------------------------------------------------
27 Code developed by S. C. Phatak, Institute of Physics,
28 Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
29 ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
30 builds up superclusters and breaks them into clusters. The input is
31 in array fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
32 fClusters[5][5000]. integer fClno gives total number of clusters in the
35 fEdepCell, fClno and fClusters are the only global ( public ) variables.
36 Others are local ( private ) to the code.
37 At the moment, the data is read for whole detector ( all supermodules
38 and pmd as well as cpv. This will have to be modify later )
39 LAST UPDATE : October 23, 2002
40 -----------------------------------------------------------------------*/
42 #include "Riostream.h"
44 #include <TObjArray.h>
45 #include "AliPMDcluster.h"
46 #include "AliPMDClustering.h"
49 ClassImp(AliPMDClustering)
51 const Double_t AliPMDClustering::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
53 AliPMDClustering::AliPMDClustering():
57 for(int i = 0; i < kNDIMX; i++)
59 for(int j = 0; j < kNDIMY; j++)
61 fCoord[0][i][j] = i+j/2.;
62 fCoord[1][i][j] = fgkSqroot3by2*j;
67 // ------------------------------------------------------------------------ //
68 AliPMDClustering::~AliPMDClustering()
72 // ------------------------------------------------------------------------ //
73 void AliPMDClustering::DoClust(Int_t idet, Int_t ismn, Double_t celladc[48][96], TObjArray *pmdcont)
75 // main function to call other necessary functions to do clustering
77 AliPMDcluster *pmdcl = 0;
79 int id and jd defined to read the input data.
80 It is assumed that for data we have 0 <= id <= 48
83 int i, i1, i2, j, nmx1, incr, id, jd;
87 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
89 for (id = 0; id < kNDIMXr; id++)
91 for (jd = 0; jd < kNDIMYr; jd++)
94 i=id+(kNDIMYr/2-1)-(jd/2);
95 fEdepCell[i][j] = celladc[id][jd];
98 Order(); // order the data
99 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
107 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
108 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
110 // nmx1 --- number of cells having ener dep >= cutoff
113 cout << " nmx1 " << nmx1 << endl;
116 // if (nmx1 == 0 | nmx1 == -1) return;
118 if (nmx1 == 0) nmx1 = 1;
122 cout <<"kNMX " << kNMX << " nmx1 " << nmx1<< " ave "<<ave<<
123 " cutoff " << cutoff << endl;
126 incr = CrClust(ave, cutoff, nmx1);
130 cout << "fClno " << fClno << endl;
133 for(i1=0; i1<=fClno; i1++)
135 Float_t cluXC = (Float_t) fClusters[0][i1];
136 Float_t cluYC = (Float_t) fClusters[1][i1];
137 Float_t cluADC = (Float_t) fClusters[2][i1];
138 Float_t cluCELLS = (Float_t) fClusters[3][i1];
139 Float_t cluRAD = (Float_t) fClusters[4][i1];
140 Float_t cluY0 = ktwobysqrt3*cluYC;
141 Float_t cluX0 = cluXC - cluY0/2.;
143 // Cluster X centroid is back transformed
145 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
147 clusdata[2] = cluADC;
148 clusdata[3] = cluCELLS;
149 clusdata[4] = cluRAD;
151 pmdcl = new AliPMDcluster(idet, ismn, clusdata);
155 // ------------------------------------------------------------------------ //
156 void AliPMDClustering::Order()
159 // sorts the ADC values from higher to lower
161 double dd[kNMX], adum;
162 // matrix fEdepCell converted into
163 // one dimensional array dd. adum a place holder for double
164 int i, j, i1, i2, iord1[kNMX], itst, idum;
166 // ordering is stored in iord1, original array not ordered
168 // define arrays dd and iord1
169 for(i1=0; i1 < kNDIMX; i1++)
171 for(i2=0; i2 < kNDIMY; i2++)
175 dd[i] = fEdepCell[i1][i2];
178 // sort and store sorting information in iord1
179 for(j=1; j < kNMX; j++)
184 for(i1=0; i1 < j ; i1++)
186 if(adum > dd[i1] && itst == 0)
189 for(i2=j-1; i2 >= i1 ; i2=i2--)
192 iord1[i2+1] = iord1[i2];
199 // store the sorted information in fIord for later use
200 for(i=0; i<kNMX; i++)
209 // ------------------------------------------------------------------------ //
210 int AliPMDClustering::CrClust(double ave, double cutoff, int nmx1)
212 // Does crude clustering
213 // Finds out only the big patch by just searching the
216 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
217 int jd1,jd2, icell, cellcount;
218 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
219 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
220 // cell. There are six neighbours.
221 // cellcount --- total number of cells having nonzero ener dep
222 // numcell --- number of cells in a given supercluster
223 // ofstream ofl0("cells_loc",ios::out);
224 // initialize fInfocl[2][kNDIMX][kNDIMY]
228 printf(" *** Inside CrClust ** kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f\n",
229 kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff);
231 for (j=0; j < kNDIMX; j++){
232 for(k=0; k < kNDIMY; k++){
233 fInfocl[0][j][k] = 0;
234 fInfocl[1][j][k] = 0;
237 for(i=0; i < kNMX; i++){
241 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
243 // ---------------------------------------------------------------
244 // crude clustering begins. Start with cell having largest adc
245 // count and loop over the cells in descending order of adc count
246 // ---------------------------------------------------------------
249 for(icell=0; icell <= nmx1; icell++){
252 if(fInfocl[0][id1][id2] == 0 ){
253 // ---------------------------------------------------------------
254 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
255 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
256 // primary and 2 for secondary cells,
257 // fInfocl[1][i1][i2] stores cluster #
258 // ---------------------------------------------------------------
261 cellcount = cellcount + 1;
262 fInfocl[0][id1][id2]=1;
263 fInfocl[1][id1][id2]=icl;
264 fInfcl[0][cellcount]=icl;
265 fInfcl[1][cellcount]=id1;
266 fInfcl[2][cellcount]=id2;
268 clust[0][numcell]=id1;
269 clust[1][numcell]=id2;
270 for(i=1; i<5000; i++)clust[0][i]=0;
271 // ---------------------------------------------------------------
272 // check for adc count in neib. cells. If ne 0 put it in this clust
273 // ---------------------------------------------------------------
277 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
278 fInfocl[0][jd1][jd2] == 0){
280 fInfocl[0][jd1][jd2]=2;
281 fInfocl[1][jd1][jd2]=icl;
282 clust[0][numcell]=jd1;
283 clust[1][numcell]=jd2;
284 cellcount=cellcount+1;
285 fInfcl[0][cellcount]=icl;
286 fInfcl[1][cellcount]=jd1;
287 fInfcl[2][cellcount]=jd2;
290 // ---------------------------------------------------------------
291 // check adc count for neighbour's neighbours recursively and
292 // if nonzero, add these to the cluster.
293 // ---------------------------------------------------------------
294 for(i=1;i < 5000;i++){
295 if(clust[0][i] != 0){
301 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
302 fInfocl[0][jd1][jd2] == 0 ){
303 fInfocl[0][jd1][jd2] = 2;
304 fInfocl[1][jd1][jd2] = icl;
305 numcell = numcell + 1;
306 clust[0][numcell] = jd1;
307 clust[1][numcell] = jd2;
308 cellcount = cellcount+1;
309 fInfcl[0][cellcount] = icl;
310 fInfcl[1][cellcount] = jd1;
311 fInfcl[2][cellcount] = jd2;
318 // for(icell=0; icell<=cellcount; icell++){
319 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
320 // fInfcl[2][icell] << endl;
324 // ------------------------------------------------------------------------ //
325 void AliPMDClustering::RefClust(int incr)
327 // Does the refining of clusters
328 // Takes the big patch and does gaussian fitting and
329 // finds out the more refined clusters
331 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
333 int ig, nsupcl, lev1[20], lev2[20];
334 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
335 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
336 // fClno counts the final clusters
337 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
338 // x, y and z store (x,y) coordinates of and energy deposited in a cell
339 // xc, yc store (x,y) coordinates of the cluster center
340 // zc stores the energy deposited in a cluster
341 // rc is cluster radius
342 // finally the cluster information is put in 2-dimensional array clusters
343 // ofstream ofl1("checking.5",ios::app);
346 for(i=0; i<4500; i++){ncl[i]=-1;}
347 for(i=0; i<incr; i++){
348 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
350 Error("RefClust", "Too many superclusters!");
354 ncl[nsupcl]=ncl[nsupcl]+1;
358 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
363 for(i=0; i<nsupcl; i++){
367 // one cell super-clusters --> single cluster
368 // cluster center at the centyer of the cell
369 // cluster radius = half cell dimension
371 Error("RefClust", "Too many clusters!");
377 fClusters[0][fClno] = fCoord[0][i1][i2];
378 fClusters[1][fClno] = fCoord[1][i1][i2];
379 fClusters[2][fClno] = fEdepCell[i1][i2];
380 fClusters[3][fClno] = 1.;
381 fClusters[4][fClno] = 0.5;
382 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
383 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
384 }else if(ncl[i] == 1){
385 // two cell super-cluster --> single cluster
386 // cluster center is at ener. dep.-weighted mean of two cells
387 // cluster radius == half cell dimension
391 Error("RefClust", "Too many clusters!");
397 x1 = fCoord[0][i1][i2];
398 y1 = fCoord[1][i1][i2];
399 z1 = fEdepCell[i1][i2];
403 x2 = fCoord[0][i1][i2];
404 y2 = fCoord[1][i1][i2];
405 z2 = fEdepCell[i1][i2];
406 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
407 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
408 fClusters[2][fClno] = z1+z2;
409 fClusters[3][fClno] = 2.;
410 fClusters[4][fClno] = 0.5;
411 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
412 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
417 // super-cluster of more than two cells - broken up into smaller
418 // clusters gaussian centers computed. (peaks separated by > 1 cell)
419 // Begin from cell having largest energy deposited This is first
423 x[0] = fCoord[0][i1][i2];
424 y[0] = fCoord[1][i1][i2];
425 z[0] = fEdepCell[i1][i2];
427 for(j=1;j<=ncl[i];j++){
433 x[j] = fCoord[0][i1][i2];
434 y[j] = fCoord[1][i1][i2];
435 z[j] = fEdepCell[i1][i2];
437 // arranging cells within supercluster in decreasing order
438 for(j=1;j<=ncl[i];j++){
442 if(itest == 0 && z[iord[i1]] < z[ihld]){
444 for(i2=j-1;i2>=i1;i2--){
452 // compute the number of Gaussians and their centers ( first
454 // centers must be separated by cells having smaller ener. dep.
455 // neighbouring centers should be either strong or well-separated
460 for(j=1;j<=ncl[i];j++){
466 rr=Distance(x1,y1,x2,y2);
467 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
469 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
471 if( rr >= 2.1)itest=itest+1;
480 // for(j=0; j<=ig; j++){
481 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
483 // GaussFit to adjust cluster parameters to minimize
484 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
486 // compute the number of cells belonging to each cluster.
487 // cell is shared between several clusters ( if they are equidistant
488 // from it ) in the ratio of cluster energy deposition
489 for(j=0; j<=ig; j++){
493 for(j=0; j<=ncl[i]; j++){
496 for(k=0; k<=ig; k++){
497 dist=Distance(x[j], y[j], xc[k], yc[k]);
498 if(dist < sqrt(3.) ){
511 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
514 for(k=1; k<=lev1[0]; k++){
517 for(k=1; k<=lev1[0]; k++){
518 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
522 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
525 for(k=1; k<=lev2[0]; k++){
528 for(k=1; k<=lev2[0]; k++){
529 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
535 for(j=0; j<=ig; j++){
537 Error("RefClust", "Too many clusters!");
541 fClusters[0][fClno] = xc[j];
542 fClusters[1][fClno] = yc[j];
543 fClusters[2][fClno] = zc[j];
544 fClusters[4][fClno] = rc[j];
546 fClusters[3][fClno] = ncl[i];
548 fClusters[3][fClno] = cells[j];
554 // ------------------------------------------------------------------------ //
555 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)
557 // Does gaussian fitting
559 int i, j, i1, i2, jmax, novar, idd, jj;
560 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
561 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
562 double hc[4500], hd[4500], zzc[4500], rrc[4500];
564 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
565 double x1, x2, y1, y2;
570 j = 0; // Just put not to see the compiler warning, BKN
572 for(i=0; i<=ncell; i++)
579 for(i=0; i<=nclust; i++)
584 str1 = str1 + zzc[i];
587 for(i=0; i<=nclust; i++)
589 zzc[i] = str/str1*zzc[i];
597 for(i=0; i<=ncell; i++){
601 for(j=0; j<=nclust; j++){
604 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
609 for(i1=0; i1<=ncell; i1++){
612 for(i2=1; i2<=idd; i2++){
616 dum=rrc[j]*rrc[jj]+rr*rr;
617 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
619 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
622 if(nclust > 20)jmax=20000;
623 for(j=0; j<jmax; j++){
625 for(i=0; i<=nclust; i++){
626 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
627 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
628 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
630 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
631 if(d[i] < 0.25)d[i]=0.25;
633 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
635 for(i1=0; i1<=ncell; i1++){
638 for(i2=1; i2<=idd; i2++){
642 dum=d[jj]*d[jj]+rr*rr;
643 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
645 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
649 for(i2=0; i2<=nclust; i2++){
658 for(j=0; j<=nclust; j++){
665 // ------------------------------------------------------------------------ //
666 double AliPMDClustering::Distance(double x1, double y1, double x2, double y2)
668 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
670 // ------------------------------------------------------------------------ //
671 double AliPMDClustering::Ranmar() const
673 // Universal random number generator proposed by Marsaglia and Zaman
674 // in report FSU-SCRI-87-50
678 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
679 static double u[97], c, cd, cm, s, t;
681 int count1,count2,idum;
682 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
684 //*******************************************************
685 // following three lines if the seed to be provided by computer
686 // start = time(NULL);
689 //*******************************************************
690 //following two lines for fixed seed ( during testing only. Else
691 //use preceeing three lines
694 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
695 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
697 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
699 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
700 i4 = jj - (i3-1)*169;
702 while ( count1 < 97 ){
706 while( count2 < 24 ){
708 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
709 i5=idum-(idum/179)*179;
710 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
711 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
718 c = 362436./16777216.; cd = 7654321./16777216.;
719 cm = 16777213./16777216.;
722 cout << " wrong initialization " << endl;
727 if( uni < 0.) uni = uni + 1;
734 if( c < 0. ) c = c+cm;
736 if( uni < 0. )uni = uni+1.;
740 // ------------------------------------------------------------------------ //
741 void AliPMDClustering::SetEdepCut(Float_t decut)
745 // ------------------------------------------------------------------------ //
746 void AliPMDClustering::SetDebug(Int_t idebug)