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 : PMDClusteringV1.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"
49 #include "AliPMDClusteringV1.h"
52 ClassImp(AliPMDClusteringV1)
54 const Double_t AliPMDClusteringV1::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
56 AliPMDClusteringV1::AliPMDClusteringV1():
59 for(int i = 0; i < kNDIMX; i++)
61 for(int j = 0; j < kNDIMY; j++)
63 fCoord[0][i][j] = i+j/2.;
64 fCoord[1][i][j] = fgkSqroot3by2*j;
69 // ------------------------------------------------------------------------ //
70 AliPMDClusteringV1::~AliPMDClusteringV1()
74 // ------------------------------------------------------------------------ //
75 void AliPMDClusteringV1::DoClust(Int_t idet, Int_t ismn, Double_t celladc[48][96], TObjArray *pmdcont)
77 // main function to call other necessary functions to do clustering
79 AliPMDcluster *pmdcl = 0;
81 int id and jd defined to read the input data.
82 It is assumed that for data we have 0 <= id <= 48
86 int i, i1, i2, j, nmx1, incr, id, jd;
87 Int_t celldataX[15], celldataY[15];
92 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
94 // ndimXr and ndimYr are different because of different module size
104 else if (ismn >= 12 && ismn <= 23)
110 for (Int_t i =0; i < kNDIMX; i++)
112 for (Int_t j =0; j < kNDIMY; j++)
115 fCellTrNo[i][j] = -1;
119 for (id = 0; id < ndimXr; id++)
121 for (jd = 0; jd < ndimYr; jd++)
124 i=id+(ndimYr/2-1)-(jd/2);
128 fEdepCell[i][j] = celladc[jd][id];
129 fCellTrNo[i][j] = jd*10000+id; /* for association */
131 else if (ismn >= 12 && ismn <= 23)
133 fEdepCell[i][j] = celladc[id][jd];
134 fCellTrNo[i][j] = id*10000+jd; /* for association */
139 Order(); // order the data
140 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
148 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
149 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
152 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
154 if (nmx1 == 0) nmx1 = 1;
157 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
160 incr = CrClust(ave, cutoff, nmx1);
163 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, fClno));
166 for(i1=0; i1<=fClno; i1++)
168 Float_t cluXC = (Float_t) fClusters[0][i1];
169 Float_t cluYC = (Float_t) fClusters[1][i1];
170 Float_t cluADC = (Float_t) fClusters[2][i1];
171 Float_t cluCELLS = (Float_t) fClusters[3][i1];
172 Float_t cluRAD = (Float_t) fClusters[4][i1];
173 Float_t cluY0 = ktwobysqrt3*cluYC;
174 Float_t cluX0 = cluXC - cluY0/2.;
178 // Cluster X centroid is back transformed
182 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
184 else if (ismn >= 12 && ismn <= 23)
186 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
190 clusdata[2] = cluADC;
191 clusdata[3] = cluCELLS;
192 clusdata[4] = cluRAD;
196 // Cells associated with a cluster
198 for (Int_t ihit = 0; ihit < 15; ihit++)
203 celldataX[ihit] = fClTr[ihit][i1]%10000;
204 celldataY[ihit] = fClTr[ihit][i1]/10000;
206 else if (ismn >= 12 && ismn <= 23)
208 celldataX[ihit] = fClTr[ihit][i1]/10000;
209 celldataY[ihit] = fClTr[ihit][i1]%10000;
212 //printf("%d %f %f\n",idet,cluXC,cluYC );
213 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
217 // ------------------------------------------------------------------------ //
218 void AliPMDClusteringV1::Order()
221 // sorts the ADC values from higher to lower
224 // matrix fEdepCell converted into
225 // one dimensional array dd. adum a place holder for double
226 int i, j, i1, i2, iord1[kNMX];
228 // ordering is stored in iord1, original array not ordered
230 // define arrays dd and iord1
231 for(i1=0; i1 < kNDIMX; i1++)
233 for(i2=0; i2 < kNDIMY; i2++)
237 dd[i] = fEdepCell[i1][i2];
241 TMath::Sort(kNMX,dd,iord1); //PH Using much better algorithm...
242 // store the sorted information in fIord for later use
243 for(i=0; i<kNMX; i++)
252 // ------------------------------------------------------------------------ //
253 int AliPMDClusteringV1::CrClust(double ave, double cutoff, int nmx1)
255 // Does crude clustering
256 // Finds out only the big patch by just searching the
259 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
260 int jd1,jd2, icell, cellcount;
261 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
262 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
263 // cell. There are six neighbours.
264 // cellcount --- total number of cells having nonzero ener dep
265 // numcell --- number of cells in a given supercluster
266 // ofstream ofl0("cells_loc",ios::out);
267 // initialize fInfocl[2][kNDIMX][kNDIMY]
269 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
271 for (j=0; j < kNDIMX; j++){
272 for(k=0; k < kNDIMY; k++){
273 fInfocl[0][j][k] = 0;
274 fInfocl[1][j][k] = 0;
277 for(i=0; i < kNMX; i++){
281 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
283 // ---------------------------------------------------------------
284 // crude clustering begins. Start with cell having largest adc
285 // count and loop over the cells in descending order of adc count
286 // ---------------------------------------------------------------
289 for(icell=0; icell <= nmx1; icell++){
292 if(fInfocl[0][id1][id2] == 0 ){
293 // ---------------------------------------------------------------
294 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
295 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
296 // primary and 2 for secondary cells,
297 // fInfocl[1][i1][i2] stores cluster #
298 // ---------------------------------------------------------------
301 cellcount = cellcount + 1;
302 fInfocl[0][id1][id2]=1;
303 fInfocl[1][id1][id2]=icl;
304 fInfcl[0][cellcount]=icl;
305 fInfcl[1][cellcount]=id1;
306 fInfcl[2][cellcount]=id2;
308 clust[0][numcell]=id1;
309 clust[1][numcell]=id2;
310 for(i=1; i<5000; i++)clust[0][i]=0;
311 // ---------------------------------------------------------------
312 // check for adc count in neib. cells. If ne 0 put it in this clust
313 // ---------------------------------------------------------------
317 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
318 fInfocl[0][jd1][jd2] == 0){
320 fInfocl[0][jd1][jd2]=2;
321 fInfocl[1][jd1][jd2]=icl;
322 clust[0][numcell]=jd1;
323 clust[1][numcell]=jd2;
324 cellcount=cellcount+1;
325 fInfcl[0][cellcount]=icl;
326 fInfcl[1][cellcount]=jd1;
327 fInfcl[2][cellcount]=jd2;
330 // ---------------------------------------------------------------
331 // check adc count for neighbour's neighbours recursively and
332 // if nonzero, add these to the cluster.
333 // ---------------------------------------------------------------
334 for(i=1;i < 5000;i++){
335 if(clust[0][i] != 0){
341 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
342 fInfocl[0][jd1][jd2] == 0 ){
343 fInfocl[0][jd1][jd2] = 2;
344 fInfocl[1][jd1][jd2] = icl;
345 numcell = numcell + 1;
346 clust[0][numcell] = jd1;
347 clust[1][numcell] = jd2;
348 cellcount = cellcount+1;
349 fInfcl[0][cellcount] = icl;
350 fInfcl[1][cellcount] = jd1;
351 fInfcl[2][cellcount] = jd2;
359 // for(icell=0; icell<=cellcount; icell++){
360 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
361 // fInfcl[2][icell] << endl;
366 // ------------------------------------------------------------------------ //
367 void AliPMDClusteringV1::RefClust(int incr)
369 // Does the refining of clusters
370 // Takes the big patch and does gaussian fitting and
371 // finds out the more refined clusters
373 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
375 int ig, nsupcl, lev1[20], lev2[20];
376 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
377 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
381 Int_t t[4500],cellCount[4500];
382 for(i=0; i<4500; i++)
389 // fClno counts the final clusters
390 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
391 // x, y and z store (x,y) coordinates of and energy deposited in a cell
392 // xc, yc store (x,y) coordinates of the cluster center
393 // zc stores the energy deposited in a cluster
394 // rc is cluster radius
395 // finally the cluster information is put in 2-dimensional array clusters
396 //ofstream ofl1("checking.5",ios::app);
399 for(i=0; i<4500; i++){ncl[i]=-1;}
400 for(i=0; i<= incr; i++){
401 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
403 AliWarning("RefClust: Too many superclusters!");
408 ncl[nsupcl]=ncl[nsupcl]+1;
411 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
415 for(i=0; i<=nsupcl; i++) {
419 // one cell super-clusters --> single cluster
420 // cluster center at the centyer of the cell
421 // cluster radius = half cell dimension
423 AliWarning("RefClust: Too many clusters! more than 5000");
429 fClusters[0][fClno] = fCoord[0][i1][i2];
430 fClusters[1][fClno] = fCoord[1][i1][i2];
431 fClusters[2][fClno] = fEdepCell[i1][i2];
432 fClusters[3][fClno] = 1.;
433 fClusters[4][fClno] = 0.5;
437 fClTr[0][fClno]=fCellTrNo[i1][i2];
438 for(Int_t icltr=1;icltr<14;icltr++)
440 fClTr[icltr][fClno]=-1;
443 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
444 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
446 }else if(ncl[i] == 1){
447 // two cell super-cluster --> single cluster
448 // cluster center is at ener. dep.-weighted mean of two cells
449 // cluster radius == half cell dimension
453 AliWarning("RefClust: Too many clusters! more than 5000");
459 x1 = fCoord[0][i1][i2];
460 y1 = fCoord[1][i1][i2];
461 z1 = fEdepCell[i1][i2];
464 fClTr[0][fClno]=fCellTrNo[i1][i2];
470 x2 = fCoord[0][i1][i2];
471 y2 = fCoord[1][i1][i2];
472 z2 = fEdepCell[i1][i2];
476 fClTr[1][fClno]=fCellTrNo[i1][i2];
477 for(Int_t icltr=2;icltr<14;icltr++)
479 fClTr[icltr][fClno] = -1;
483 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
484 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
485 fClusters[2][fClno] = z1+z2;
486 fClusters[3][fClno] = 2.;
487 fClusters[4][fClno] = 0.5;
490 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
491 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
496 for(Int_t icg=0;icg<4500;icg++)
504 // super-cluster of more than two cells - broken up into smaller
505 // clusters gaussian centers computed. (peaks separated by > 1 cell)
506 // Begin from cell having largest energy deposited This is first
510 x[0] = fCoord[0][i1][i2];
511 y[0] = fCoord[1][i1][i2];
512 z[0] = fEdepCell[i1][i2];
515 t[0]=fCellTrNo[i1][i2];
519 for(j=1;j<=ncl[i];j++){
525 x[j] = fCoord[0][i1][i2];
526 y[j] = fCoord[1][i1][i2];
527 z[j] = fEdepCell[i1][i2];
530 t[j]=fCellTrNo[i1][i2];
535 // arranging cells within supercluster in decreasing order
536 for(j=1;j<=ncl[i];j++){
540 if(itest == 0 && z[iord[i1]] < z[ihld]){
542 for(i2=j-1;i2>=i1;i2--){
550 // compute the number of Gaussians and their centers ( first
552 // centers must be separated by cells having smaller ener. dep.
553 // neighbouring centers should be either strong or well-separated
558 for(j=1;j<=ncl[i];j++){
564 rr=Distance(x1,y1,x2,y2);
565 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
567 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
569 if( rr >= 2.1)itest=itest+1;
578 // for(j=0; j<=ig; j++){
579 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
581 // GaussFit to adjust cluster parameters to minimize
582 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
584 // compute the number of cells belonging to each cluster.
585 // cell is shared between several clusters ( if they are equidistant
586 // from it ) in the ratio of cluster energy deposition
587 for(j=0; j<=ig; j++){
591 for(j=0; j<=ncl[i]; j++){
594 for(k=0; k<=ig; k++){
595 dist=Distance(x[j], y[j], xc[k], yc[k]);
596 if(dist < sqrt(3.) ){
599 fClTr[cellCount[k]][fClno+k+1]=t[j];
615 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
618 for(k=1; k<=lev1[0]; k++){
621 for(k=1; k<=lev1[0]; k++){
622 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
626 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
629 for(k=1; k<=lev2[0]; k++){
632 for(k=1; k<=lev2[0]; k++){
633 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
640 // zero rest of the cell array
644 for(Int_t icltr=cellCount[k];icltr<14;icltr++)
646 fClTr[icltr][fClno]=-1;
653 for(j=0; j<=ig; j++){
655 AliWarning("RefClust: Too many clusters! more than 5000");
659 fClusters[0][fClno] = xc[j];
660 fClusters[1][fClno] = yc[j];
661 fClusters[2][fClno] = zc[j];
662 fClusters[4][fClno] = rc[j];
664 fClusters[3][fClno] = ncl[i];
666 fClusters[3][fClno] = cells[j];
672 // ------------------------------------------------------------------------ //
673 void AliPMDClusteringV1::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)
675 // Does gaussian fitting
677 int i, j, i1, i2, novar, idd, jj;
678 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
679 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
680 double hc[4500], hd[4500], zzc[4500], rrc[4500];
682 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
683 double x1, x2, y1, y2;
688 j = 0; // Just put not to see the compiler warning, BKN
690 for(i=0; i<=ncell; i++)
697 for(i=0; i<=nclust; i++)
702 str1 = str1 + zzc[i];
705 for(i=0; i<=nclust; i++)
707 zzc[i] = str/str1*zzc[i];
715 for(i=0; i<=ncell; i++){
719 for(j=0; j<=nclust; j++){
722 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
727 for(i1=0; i1<=ncell; i1++){
730 for(i2=1; i2<=idd; i2++){
734 dum=rrc[j]*rrc[jj]+rr*rr;
735 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
737 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
740 // if(nclust > 20)jmax=20000;
741 // for(j=0; j<jmax; j++){
743 for(i=0; i<=nclust; i++){
744 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
745 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
746 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
748 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
749 if(d[i] < 0.25)d[i]=0.25;
751 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
753 for(i1=0; i1<=ncell; i1++){
756 for(i2=1; i2<=idd; i2++){
760 dum=d[jj]*d[jj]+rr*rr;
761 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
763 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
767 for(i2=0; i2<=nclust; i2++){
776 for(j=0; j<=nclust; j++){
783 // ------------------------------------------------------------------------ //
784 double AliPMDClusteringV1::Distance(double x1, double y1, double x2, double y2)
786 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
788 // ------------------------------------------------------------------------ //
789 double AliPMDClusteringV1::Ranmar() const
791 // Universal random number generator proposed by Marsaglia and Zaman
792 // in report FSU-SCRI-87-50
796 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
797 static double u[97], c, cd, cm, s, t;
799 int count1,count2,idum;
800 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
802 //*******************************************************
803 // following three lines if the seed to be provided by computer
804 // start = time(NULL);
807 //*******************************************************
808 //following two lines for fixed seed ( during testing only. Else
809 //use preceeing three lines
812 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
813 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
815 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
817 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
818 i4 = jj - (i3-1)*169;
820 while ( count1 < 97 ){
824 while( count2 < 24 ){
826 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
827 i5=idum-(idum/179)*179;
828 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
829 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
836 c = 362436./16777216.; cd = 7654321./16777216.;
837 cm = 16777213./16777216.;
840 AliWarning("Wrong initialization");
845 if( uni < 0.) uni = uni + 1;
852 if( c < 0. ) c = c+cm;
854 if( uni < 0. )uni = uni+1.;
858 // ------------------------------------------------------------------------ //
859 void AliPMDClusteringV1::SetEdepCut(Float_t decut)
863 // ------------------------------------------------------------------------ //