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>
47 #include "AliPMDcluster.h"
48 #include "AliPMDClustering.h"
51 ClassImp(AliPMDClustering)
53 const Double_t AliPMDClustering::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
55 AliPMDClustering::AliPMDClustering():
58 for(int i = 0; i < kNDIMX; i++)
60 for(int j = 0; j < kNDIMY; j++)
62 fCoord[0][i][j] = i+j/2.;
63 fCoord[1][i][j] = fgkSqroot3by2*j;
68 // ------------------------------------------------------------------------ //
69 AliPMDClustering::~AliPMDClustering()
73 // ------------------------------------------------------------------------ //
74 void AliPMDClustering::DoClust(Int_t idet, Int_t ismn, Double_t celladc[48][96], TObjArray *pmdcont)
76 // main function to call other necessary functions to do clustering
78 AliPMDcluster *pmdcl = 0;
80 int id and jd defined to read the input data.
81 It is assumed that for data we have 0 <= id <= 48
84 int i, i1, i2, j, nmx1, incr, id, jd;
88 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
90 for (id = 0; id < kNDIMXr; id++)
92 for (jd = 0; jd < kNDIMYr; jd++)
95 i=id+(kNDIMYr/2-1)-(jd/2);
96 fEdepCell[i][j] = celladc[id][jd];
99 Order(); // order the data
100 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
108 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
109 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
111 // nmx1 --- number of cells having ener dep >= cutoff
113 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
115 // if (nmx1 == 0 | nmx1 == -1) return;
117 if (nmx1 == 0) nmx1 = 1;
120 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
123 incr = CrClust(ave, cutoff, nmx1);
126 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, fClno));
128 for(i1=0; i1<=fClno; i1++)
130 Float_t cluXC = (Float_t) fClusters[0][i1];
131 Float_t cluYC = (Float_t) fClusters[1][i1];
132 Float_t cluADC = (Float_t) fClusters[2][i1];
133 Float_t cluCELLS = (Float_t) fClusters[3][i1];
134 Float_t cluRAD = (Float_t) fClusters[4][i1];
135 Float_t cluY0 = ktwobysqrt3*cluYC;
136 Float_t cluX0 = cluXC - cluY0/2.;
138 // Cluster X centroid is back transformed
140 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
142 clusdata[2] = cluADC;
143 clusdata[3] = cluCELLS;
144 clusdata[4] = cluRAD;
146 pmdcl = new AliPMDcluster(idet, ismn, clusdata);
150 // ------------------------------------------------------------------------ //
151 void AliPMDClustering::Order()
154 // sorts the ADC values from higher to lower
157 // matrix fEdepCell converted into
158 // one dimensional array dd. adum a place holder for double
159 int i, j, i1, i2, iord1[kNMX];
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--)
186 // dd[i2+1] = dd[i2];
187 // iord1[i2+1] = iord1[i2];
195 TMath::Sort(kNMX,dd,iord1); //PH Using much better algorithm...
196 // store the sorted information in fIord for later use
197 for(i=0; i<kNMX; i++)
206 // ------------------------------------------------------------------------ //
207 int AliPMDClustering::CrClust(double ave, double cutoff, int nmx1)
209 // Does crude clustering
210 // Finds out only the big patch by just searching the
213 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
214 int jd1,jd2, icell, cellcount;
215 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
216 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
217 // cell. There are six neighbours.
218 // cellcount --- total number of cells having nonzero ener dep
219 // numcell --- number of cells in a given supercluster
220 // ofstream ofl0("cells_loc",ios::out);
221 // initialize fInfocl[2][kNDIMX][kNDIMY]
223 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
225 for (j=0; j < kNDIMX; j++){
226 for(k=0; k < kNDIMY; k++){
227 fInfocl[0][j][k] = 0;
228 fInfocl[1][j][k] = 0;
231 for(i=0; i < kNMX; i++){
235 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
237 // ---------------------------------------------------------------
238 // crude clustering begins. Start with cell having largest adc
239 // count and loop over the cells in descending order of adc count
240 // ---------------------------------------------------------------
243 for(icell=0; icell <= nmx1; icell++){
246 if(fInfocl[0][id1][id2] == 0 ){
247 // ---------------------------------------------------------------
248 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
249 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
250 // primary and 2 for secondary cells,
251 // fInfocl[1][i1][i2] stores cluster #
252 // ---------------------------------------------------------------
255 cellcount = cellcount + 1;
256 fInfocl[0][id1][id2]=1;
257 fInfocl[1][id1][id2]=icl;
258 fInfcl[0][cellcount]=icl;
259 fInfcl[1][cellcount]=id1;
260 fInfcl[2][cellcount]=id2;
262 clust[0][numcell]=id1;
263 clust[1][numcell]=id2;
264 for(i=1; i<5000; i++)clust[0][i]=0;
265 // ---------------------------------------------------------------
266 // check for adc count in neib. cells. If ne 0 put it in this clust
267 // ---------------------------------------------------------------
271 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
272 fInfocl[0][jd1][jd2] == 0){
274 fInfocl[0][jd1][jd2]=2;
275 fInfocl[1][jd1][jd2]=icl;
276 clust[0][numcell]=jd1;
277 clust[1][numcell]=jd2;
278 cellcount=cellcount+1;
279 fInfcl[0][cellcount]=icl;
280 fInfcl[1][cellcount]=jd1;
281 fInfcl[2][cellcount]=jd2;
284 // ---------------------------------------------------------------
285 // check adc count for neighbour's neighbours recursively and
286 // if nonzero, add these to the cluster.
287 // ---------------------------------------------------------------
288 for(i=1;i < 5000;i++){
289 if(clust[0][i] != 0){
295 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
296 fInfocl[0][jd1][jd2] == 0 ){
297 fInfocl[0][jd1][jd2] = 2;
298 fInfocl[1][jd1][jd2] = icl;
299 numcell = numcell + 1;
300 clust[0][numcell] = jd1;
301 clust[1][numcell] = jd2;
302 cellcount = cellcount+1;
303 fInfcl[0][cellcount] = icl;
304 fInfcl[1][cellcount] = jd1;
305 fInfcl[2][cellcount] = jd2;
312 // for(icell=0; icell<=cellcount; icell++){
313 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
314 // fInfcl[2][icell] << endl;
318 // ------------------------------------------------------------------------ //
319 void AliPMDClustering::RefClust(int incr)
321 // Does the refining of clusters
322 // Takes the big patch and does gaussian fitting and
323 // finds out the more refined clusters
325 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
327 int ig, nsupcl, lev1[20], lev2[20];
328 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
329 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
330 // fClno counts the final clusters
331 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
332 // x, y and z store (x,y) coordinates of and energy deposited in a cell
333 // xc, yc store (x,y) coordinates of the cluster center
334 // zc stores the energy deposited in a cluster
335 // rc is cluster radius
336 // finally the cluster information is put in 2-dimensional array clusters
337 // ofstream ofl1("checking.5",ios::app);
340 for(i=0; i<4500; i++){ncl[i]=-1;}
341 for(i=0; i<incr; i++){
342 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
344 AliWarning("RefClust: Too many superclusters!");
348 ncl[nsupcl]=ncl[nsupcl]+1;
351 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
356 for(i=0; i<nsupcl; i++){
360 // one cell super-clusters --> single cluster
361 // cluster center at the centyer of the cell
362 // cluster radius = half cell dimension
364 AliWarning("RefClust: Too many clusters! more than 5000");
370 fClusters[0][fClno] = fCoord[0][i1][i2];
371 fClusters[1][fClno] = fCoord[1][i1][i2];
372 fClusters[2][fClno] = fEdepCell[i1][i2];
373 fClusters[3][fClno] = 1.;
374 fClusters[4][fClno] = 0.5;
375 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
376 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
377 }else if(ncl[i] == 1){
378 // two cell super-cluster --> single cluster
379 // cluster center is at ener. dep.-weighted mean of two cells
380 // cluster radius == half cell dimension
384 AliWarning("RefClust: Too many clusters! more than 5000");
390 x1 = fCoord[0][i1][i2];
391 y1 = fCoord[1][i1][i2];
392 z1 = fEdepCell[i1][i2];
396 x2 = fCoord[0][i1][i2];
397 y2 = fCoord[1][i1][i2];
398 z2 = fEdepCell[i1][i2];
399 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
400 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
401 fClusters[2][fClno] = z1+z2;
402 fClusters[3][fClno] = 2.;
403 fClusters[4][fClno] = 0.5;
404 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
405 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
410 // super-cluster of more than two cells - broken up into smaller
411 // clusters gaussian centers computed. (peaks separated by > 1 cell)
412 // Begin from cell having largest energy deposited This is first
416 x[0] = fCoord[0][i1][i2];
417 y[0] = fCoord[1][i1][i2];
418 z[0] = fEdepCell[i1][i2];
420 for(j=1;j<=ncl[i];j++){
426 x[j] = fCoord[0][i1][i2];
427 y[j] = fCoord[1][i1][i2];
428 z[j] = fEdepCell[i1][i2];
430 // arranging cells within supercluster in decreasing order
431 for(j=1;j<=ncl[i];j++){
435 if(itest == 0 && z[iord[i1]] < z[ihld]){
437 for(i2=j-1;i2>=i1;i2--){
445 // compute the number of Gaussians and their centers ( first
447 // centers must be separated by cells having smaller ener. dep.
448 // neighbouring centers should be either strong or well-separated
453 for(j=1;j<=ncl[i];j++){
459 rr=Distance(x1,y1,x2,y2);
460 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
462 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
464 if( rr >= 2.1)itest=itest+1;
473 // for(j=0; j<=ig; j++){
474 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
476 // GaussFit to adjust cluster parameters to minimize
477 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
479 // compute the number of cells belonging to each cluster.
480 // cell is shared between several clusters ( if they are equidistant
481 // from it ) in the ratio of cluster energy deposition
482 for(j=0; j<=ig; j++){
486 for(j=0; j<=ncl[i]; j++){
489 for(k=0; k<=ig; k++){
490 dist=Distance(x[j], y[j], xc[k], yc[k]);
491 if(dist < sqrt(3.) ){
504 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
507 for(k=1; k<=lev1[0]; k++){
510 for(k=1; k<=lev1[0]; k++){
511 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
515 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
518 for(k=1; k<=lev2[0]; k++){
521 for(k=1; k<=lev2[0]; k++){
522 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
528 for(j=0; j<=ig; j++){
530 AliWarning("RefClust: Too many clusters! more than 5000");
534 fClusters[0][fClno] = xc[j];
535 fClusters[1][fClno] = yc[j];
536 fClusters[2][fClno] = zc[j];
537 fClusters[4][fClno] = rc[j];
539 fClusters[3][fClno] = ncl[i];
541 fClusters[3][fClno] = cells[j];
547 // ------------------------------------------------------------------------ //
548 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)
550 // Does gaussian fitting
552 int i, j, i1, i2, novar, idd, jj;
553 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
554 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
555 double hc[4500], hd[4500], zzc[4500], rrc[4500];
557 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
558 double x1, x2, y1, y2;
563 j = 0; // Just put not to see the compiler warning, BKN
565 for(i=0; i<=ncell; i++)
572 for(i=0; i<=nclust; i++)
577 str1 = str1 + zzc[i];
580 for(i=0; i<=nclust; i++)
582 zzc[i] = str/str1*zzc[i];
590 for(i=0; i<=ncell; i++){
594 for(j=0; j<=nclust; j++){
597 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
602 for(i1=0; i1<=ncell; i1++){
605 for(i2=1; i2<=idd; i2++){
609 dum=rrc[j]*rrc[jj]+rr*rr;
610 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
612 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
615 // if(nclust > 20)jmax=20000;
616 // for(j=0; j<jmax; j++){
618 for(i=0; i<=nclust; i++){
619 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
620 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
621 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
623 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
624 if(d[i] < 0.25)d[i]=0.25;
626 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
628 for(i1=0; i1<=ncell; i1++){
631 for(i2=1; i2<=idd; i2++){
635 dum=d[jj]*d[jj]+rr*rr;
636 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
638 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
642 for(i2=0; i2<=nclust; i2++){
651 for(j=0; j<=nclust; j++){
658 // ------------------------------------------------------------------------ //
659 double AliPMDClustering::Distance(double x1, double y1, double x2, double y2)
661 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
663 // ------------------------------------------------------------------------ //
664 double AliPMDClustering::Ranmar() const
666 // Universal random number generator proposed by Marsaglia and Zaman
667 // in report FSU-SCRI-87-50
671 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
672 static double u[97], c, cd, cm, s, t;
674 int count1,count2,idum;
675 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
677 //*******************************************************
678 // following three lines if the seed to be provided by computer
679 // start = time(NULL);
682 //*******************************************************
683 //following two lines for fixed seed ( during testing only. Else
684 //use preceeing three lines
687 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
688 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
690 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
692 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
693 i4 = jj - (i3-1)*169;
695 while ( count1 < 97 ){
699 while( count2 < 24 ){
701 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
702 i5=idum-(idum/179)*179;
703 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
704 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
711 c = 362436./16777216.; cd = 7654321./16777216.;
712 cm = 16777213./16777216.;
715 AliWarning("Wrong initialization");
720 if( uni < 0.) uni = uni + 1;
727 if( c < 0. ) c = c+cm;
729 if( uni < 0. )uni = uni+1.;
733 // ------------------------------------------------------------------------ //
734 void AliPMDClustering::SetEdepCut(Float_t decut)
738 // ------------------------------------------------------------------------ //