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 **************************************************************************/
18 //-----------------------------------------------------//
20 // Source File : PMDClusteringV1.cxx, Version 00 //
22 // Date : September 26 2002 //
24 // clustering code for alice pmd //
26 //-----------------------------------------------------//
28 /* --------------------------------------------------------------------
29 Code developed by S. C. Phatak, Institute of Physics,
30 Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
31 ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
32 builds up superclusters and breaks them into clusters. The input is
33 in array fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
34 fClusters[5][5000]. integer fClno gives total number of clusters in the
37 fEdepCell, fClno and fClusters are the only global ( public ) variables.
38 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 )
41 LAST UPDATE : October 23, 2002
42 -----------------------------------------------------------------------*/
44 #include <Riostream.h>
47 #include <TObjArray.h>
50 #include "AliPMDcluster.h"
51 #include "AliPMDClustering.h"
52 #include "AliPMDClusteringV1.h"
55 ClassImp(AliPMDClusteringV1)
57 const Double_t AliPMDClusteringV1::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
59 AliPMDClusteringV1::AliPMDClusteringV1():
63 for(int i = 0; i < kNDIMX; i++)
65 for(int j = 0; j < kNDIMY; j++)
67 fCoord[0][i][j] = i+j/2.;
68 fCoord[1][i][j] = fgkSqroot3by2*j;
73 // ------------------------------------------------------------------------ //
74 AliPMDClusteringV1::~AliPMDClusteringV1()
78 // ------------------------------------------------------------------------ //
79 void AliPMDClusteringV1::DoClust(Int_t idet, Int_t ismn, Double_t celladc[48][96], TObjArray *pmdcont)
81 // main function to call other necessary functions to do clustering
83 AliPMDcluster *pmdcl = 0;
85 int id and jd defined to read the input data.
86 It is assumed that for data we have 0 <= id <= 48
90 int i, i1, i2, j, nmx1, incr, id, jd;
91 Int_t celldataX[15], celldataY[15];
96 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
98 // ndimXr and ndimYr are different because of different module size
108 else if (ismn >= 12 && ismn <= 23)
114 for (Int_t i =0; i < kNDIMX; i++)
116 for (Int_t j =0; j < kNDIMY; j++)
119 fCellTrNo[i][j] = -1;
123 for (id = 0; id < ndimXr; id++)
125 for (jd = 0; jd < ndimYr; jd++)
128 i=id+(ndimYr/2-1)-(jd/2);
132 fEdepCell[i][j] = celladc[jd][id];
133 fCellTrNo[i][j] = jd*10000+id; /* for association */
135 else if (ismn >= 12 && ismn <= 23)
137 fEdepCell[i][j] = celladc[id][jd];
138 fCellTrNo[i][j] = id*10000+jd; /* for association */
143 Order(); // order the data
144 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
152 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
153 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
156 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
158 if (nmx1 == 0) nmx1 = 1;
161 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
164 incr = CrClust(ave, cutoff, nmx1);
167 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, fClno));
170 for(i1=0; i1<=fClno; i1++)
172 Float_t cluXC = (Float_t) fClusters[0][i1];
173 Float_t cluYC = (Float_t) fClusters[1][i1];
174 Float_t cluADC = (Float_t) fClusters[2][i1];
175 Float_t cluCELLS = (Float_t) fClusters[3][i1];
176 Float_t cluRAD = (Float_t) fClusters[4][i1];
177 Float_t cluY0 = ktwobysqrt3*cluYC;
178 Float_t cluX0 = cluXC - cluY0/2.;
182 // Cluster X centroid is back transformed
186 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
188 else if (ismn >= 12 && ismn <= 23)
190 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
194 clusdata[2] = cluADC;
195 clusdata[3] = cluCELLS;
196 clusdata[4] = cluRAD;
200 // Cells associated with a cluster
202 for (Int_t ihit = 0; ihit < 15; ihit++)
207 celldataX[ihit] = fClTr[ihit][i1]%10000;
208 celldataY[ihit] = fClTr[ihit][i1]/10000;
210 else if (ismn >= 12 && ismn <= 23)
212 celldataX[ihit] = fClTr[ihit][i1]/10000;
213 celldataY[ihit] = fClTr[ihit][i1]%10000;
216 //printf("%d %f %f\n",idet,cluXC,cluYC );
217 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
221 // ------------------------------------------------------------------------ //
222 void AliPMDClusteringV1::Order()
225 // sorts the ADC values from higher to lower
228 // matrix fEdepCell converted into
229 // one dimensional array dd. adum a place holder for double
230 int i, j, i1, i2, iord1[kNMX];
232 // ordering is stored in iord1, original array not ordered
234 // define arrays dd and iord1
235 for(i1=0; i1 < kNDIMX; i1++)
237 for(i2=0; i2 < kNDIMY; i2++)
241 dd[i] = fEdepCell[i1][i2];
245 TMath::Sort(kNMX,dd,iord1); //PH Using much better algorithm...
246 // store the sorted information in fIord for later use
247 for(i=0; i<kNMX; i++)
256 // ------------------------------------------------------------------------ //
257 int AliPMDClusteringV1::CrClust(double ave, double cutoff, int nmx1)
259 // Does crude clustering
260 // Finds out only the big patch by just searching the
263 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
264 int jd1,jd2, icell, cellcount;
265 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
266 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
267 // cell. There are six neighbours.
268 // cellcount --- total number of cells having nonzero ener dep
269 // numcell --- number of cells in a given supercluster
270 // ofstream ofl0("cells_loc",ios::out);
271 // initialize fInfocl[2][kNDIMX][kNDIMY]
273 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
275 for (j=0; j < kNDIMX; j++){
276 for(k=0; k < kNDIMY; k++){
277 fInfocl[0][j][k] = 0;
278 fInfocl[1][j][k] = 0;
281 for(i=0; i < kNMX; i++){
285 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
287 // ---------------------------------------------------------------
288 // crude clustering begins. Start with cell having largest adc
289 // count and loop over the cells in descending order of adc count
290 // ---------------------------------------------------------------
293 for(icell=0; icell <= nmx1; icell++){
296 if(fInfocl[0][id1][id2] == 0 ){
297 // ---------------------------------------------------------------
298 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
299 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
300 // primary and 2 for secondary cells,
301 // fInfocl[1][i1][i2] stores cluster #
302 // ---------------------------------------------------------------
305 cellcount = cellcount + 1;
306 fInfocl[0][id1][id2]=1;
307 fInfocl[1][id1][id2]=icl;
308 fInfcl[0][cellcount]=icl;
309 fInfcl[1][cellcount]=id1;
310 fInfcl[2][cellcount]=id2;
312 clust[0][numcell]=id1;
313 clust[1][numcell]=id2;
314 for(i=1; i<5000; i++)clust[0][i]=0;
315 // ---------------------------------------------------------------
316 // check for adc count in neib. cells. If ne 0 put it in this clust
317 // ---------------------------------------------------------------
321 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
322 fInfocl[0][jd1][jd2] == 0){
324 fInfocl[0][jd1][jd2]=2;
325 fInfocl[1][jd1][jd2]=icl;
326 clust[0][numcell]=jd1;
327 clust[1][numcell]=jd2;
328 cellcount=cellcount+1;
329 fInfcl[0][cellcount]=icl;
330 fInfcl[1][cellcount]=jd1;
331 fInfcl[2][cellcount]=jd2;
334 // ---------------------------------------------------------------
335 // check adc count for neighbour's neighbours recursively and
336 // if nonzero, add these to the cluster.
337 // ---------------------------------------------------------------
338 for(i=1;i < 5000;i++){
339 if(clust[0][i] != 0){
345 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
346 fInfocl[0][jd1][jd2] == 0 ){
347 fInfocl[0][jd1][jd2] = 2;
348 fInfocl[1][jd1][jd2] = icl;
349 numcell = numcell + 1;
350 clust[0][numcell] = jd1;
351 clust[1][numcell] = jd2;
352 cellcount = cellcount+1;
353 fInfcl[0][cellcount] = icl;
354 fInfcl[1][cellcount] = jd1;
355 fInfcl[2][cellcount] = jd2;
363 // for(icell=0; icell<=cellcount; icell++){
364 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
365 // fInfcl[2][icell] << endl;
370 // ------------------------------------------------------------------------ //
371 void AliPMDClusteringV1::RefClust(int incr)
373 // Does the refining of clusters
374 // Takes the big patch and does gaussian fitting and
375 // finds out the more refined clusters
377 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
379 int ig, nsupcl, lev1[20], lev2[20];
380 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
381 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
385 Int_t t[4500],cellCount[4500];
386 for(i=0; i<4500; i++)
393 // fClno counts the final clusters
394 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
395 // x, y and z store (x,y) coordinates of and energy deposited in a cell
396 // xc, yc store (x,y) coordinates of the cluster center
397 // zc stores the energy deposited in a cluster
398 // rc is cluster radius
399 // finally the cluster information is put in 2-dimensional array clusters
400 //ofstream ofl1("checking.5",ios::app);
403 for(i=0; i<4500; i++){ncl[i]=-1;}
404 for(i=0; i<= incr; i++){
405 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
407 AliWarning("RefClust: Too many superclusters!");
412 ncl[nsupcl]=ncl[nsupcl]+1;
415 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
419 for(i=0; i<=nsupcl; i++) {
423 // one cell super-clusters --> single cluster
424 // cluster center at the centyer of the cell
425 // cluster radius = half cell dimension
427 AliWarning("RefClust: Too many clusters! more than 5000");
433 fClusters[0][fClno] = fCoord[0][i1][i2];
434 fClusters[1][fClno] = fCoord[1][i1][i2];
435 fClusters[2][fClno] = fEdepCell[i1][i2];
436 fClusters[3][fClno] = 1.;
437 fClusters[4][fClno] = 0.5;
441 fClTr[0][fClno]=fCellTrNo[i1][i2];
442 for(Int_t icltr=1;icltr<14;icltr++)
444 fClTr[icltr][fClno]=-1;
447 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
448 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
450 }else if(ncl[i] == 1){
451 // two cell super-cluster --> single cluster
452 // cluster center is at ener. dep.-weighted mean of two cells
453 // cluster radius == half cell dimension
457 AliWarning("RefClust: Too many clusters! more than 5000");
463 x1 = fCoord[0][i1][i2];
464 y1 = fCoord[1][i1][i2];
465 z1 = fEdepCell[i1][i2];
468 fClTr[0][fClno]=fCellTrNo[i1][i2];
474 x2 = fCoord[0][i1][i2];
475 y2 = fCoord[1][i1][i2];
476 z2 = fEdepCell[i1][i2];
480 fClTr[1][fClno]=fCellTrNo[i1][i2];
481 for(Int_t icltr=2;icltr<14;icltr++)
483 fClTr[icltr][fClno] = -1;
487 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
488 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
489 fClusters[2][fClno] = z1+z2;
490 fClusters[3][fClno] = 2.;
491 fClusters[4][fClno] = 0.5;
494 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
495 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
500 for(Int_t icg=0;icg<4500;icg++)
508 // super-cluster of more than two cells - broken up into smaller
509 // clusters gaussian centers computed. (peaks separated by > 1 cell)
510 // Begin from cell having largest energy deposited This is first
514 x[0] = fCoord[0][i1][i2];
515 y[0] = fCoord[1][i1][i2];
516 z[0] = fEdepCell[i1][i2];
519 t[0]=fCellTrNo[i1][i2];
523 for(j=1;j<=ncl[i];j++){
529 x[j] = fCoord[0][i1][i2];
530 y[j] = fCoord[1][i1][i2];
531 z[j] = fEdepCell[i1][i2];
534 t[j]=fCellTrNo[i1][i2];
539 // arranging cells within supercluster in decreasing order
540 for(j=1;j<=ncl[i];j++){
544 if(itest == 0 && z[iord[i1]] < z[ihld]){
546 for(i2=j-1;i2>=i1;i2--){
554 // compute the number of Gaussians and their centers ( first
556 // centers must be separated by cells having smaller ener. dep.
557 // neighbouring centers should be either strong or well-separated
562 for(j=1;j<=ncl[i];j++){
568 rr=Distance(x1,y1,x2,y2);
569 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
571 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
573 if( rr >= 2.1)itest=itest+1;
582 // for(j=0; j<=ig; j++){
583 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
585 // GaussFit to adjust cluster parameters to minimize
586 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
588 // compute the number of cells belonging to each cluster.
589 // cell is shared between several clusters ( if they are equidistant
590 // from it ) in the ratio of cluster energy deposition
591 for(j=0; j<=ig; j++){
595 for(j=0; j<=ncl[i]; j++){
598 for(k=0; k<=ig; k++){
599 dist=Distance(x[j], y[j], xc[k], yc[k]);
600 if(dist < sqrt(3.) ){
603 fClTr[cellCount[k]][fClno+k+1]=t[j];
619 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
622 for(k=1; k<=lev1[0]; k++){
625 for(k=1; k<=lev1[0]; k++){
626 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
630 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
633 for(k=1; k<=lev2[0]; k++){
636 for(k=1; k<=lev2[0]; k++){
637 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
644 // zero rest of the cell array
648 for(Int_t icltr=cellCount[k];icltr<14;icltr++)
650 fClTr[icltr][fClno]=-1;
657 for(j=0; j<=ig; j++){
659 AliWarning("RefClust: Too many clusters! more than 5000");
663 fClusters[0][fClno] = xc[j];
664 fClusters[1][fClno] = yc[j];
665 fClusters[2][fClno] = zc[j];
666 fClusters[4][fClno] = rc[j];
668 fClusters[3][fClno] = ncl[i];
670 fClusters[3][fClno] = cells[j];
676 // ------------------------------------------------------------------------ //
677 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)
679 // Does gaussian fitting
681 int i, j, i1, i2, novar, idd, jj;
682 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
683 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
684 double hc[4500], hd[4500], zzc[4500], rrc[4500];
686 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
687 double x1, x2, y1, y2;
692 j = 0; // Just put not to see the compiler warning, BKN
694 for(i=0; i<=ncell; i++)
701 for(i=0; i<=nclust; i++)
706 str1 = str1 + zzc[i];
709 for(i=0; i<=nclust; i++)
711 zzc[i] = str/str1*zzc[i];
719 for(i=0; i<=ncell; i++){
723 for(j=0; j<=nclust; j++){
726 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
731 for(i1=0; i1<=ncell; i1++){
734 for(i2=1; i2<=idd; i2++){
738 dum=rrc[j]*rrc[jj]+rr*rr;
739 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
741 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
744 // if(nclust > 20)jmax=20000;
745 // for(j=0; j<jmax; j++){
747 for(i=0; i<=nclust; i++){
748 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
749 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
750 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
752 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
753 if(d[i] < 0.25)d[i]=0.25;
755 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
757 for(i1=0; i1<=ncell; i1++){
760 for(i2=1; i2<=idd; i2++){
764 dum=d[jj]*d[jj]+rr*rr;
765 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
767 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
771 for(i2=0; i2<=nclust; i2++){
780 for(j=0; j<=nclust; j++){
787 // ------------------------------------------------------------------------ //
788 double AliPMDClusteringV1::Distance(double x1, double y1, double x2, double y2)
790 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
792 // ------------------------------------------------------------------------ //
793 double AliPMDClusteringV1::Ranmar() const
795 // Universal random number generator proposed by Marsaglia and Zaman
796 // in report FSU-SCRI-87-50
800 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
801 static double u[97], c, cd, cm, s, t;
803 int count1,count2,idum;
804 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
806 //*******************************************************
807 // following three lines if the seed to be provided by computer
808 // start = time(NULL);
811 //*******************************************************
812 //following two lines for fixed seed ( during testing only. Else
813 //use preceeing three lines
816 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
817 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
819 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
821 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
822 i4 = jj - (i3-1)*169;
824 while ( count1 < 97 ){
828 while( count2 < 24 ){
830 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
831 i5=idum-(idum/179)*179;
832 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
833 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
840 c = 362436./16777216.; cd = 7654321./16777216.;
841 cm = 16777213./16777216.;
844 AliWarning("Wrong initialization");
849 if( uni < 0.) uni = uni + 1;
856 if( c < 0. ) c = c+cm;
858 if( uni < 0. )uni = uni+1.;
862 // ------------------------------------------------------------------------ //
863 void AliPMDClusteringV1::SetEdepCut(Float_t decut)
867 // ------------------------------------------------------------------------ //