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 : PMDClusteringV2.cxx //
20 // clustering code for alice pmd //
22 //-----------------------------------------------------//
24 /* --------------------------------------------------------------------
25 Code developed by S. C. Phatak, Institute of Physics,
26 Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
27 ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
28 builds up superclusters and breaks them into clusters. The input is
29 in array fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
30 fClusters[5][5000]. integer fClno gives total number of clusters in the
33 fEdepCell, fClno and fClusters are the only global ( public ) variables.
34 Others are local ( private ) to the code.
35 At the moment, the data is read for whole detector ( all supermodules
36 and pmd as well as cpv. This will have to be modify later )
37 LAST UPDATE : October 23, 2002
38 -----------------------------------------------------------------------*/
40 #include "Riostream.h"
41 #include <TObjArray.h>
44 #include "AliPMDcluster.h"
45 #include "AliPMDClustering.h"
46 #include "AliPMDClusteringV2.h"
49 ClassImp(AliPMDClusteringV2)
51 const Double_t AliPMDClusteringV2::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
53 AliPMDClusteringV2::AliPMDClusteringV2():
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 AliPMDClusteringV2::~AliPMDClusteringV2()
72 // ------------------------------------------------------------------------ //
73 void AliPMDClusteringV2::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_t i, i1, i2, j, nmx1, incr, id, jd;
80 Int_t celldataX[15], celldataY[15];
84 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
94 else if (ismn >= 12 && ismn <= 23)
100 for (Int_t i =0; i < kNDIMX; i++)
102 for (Int_t j =0; j < kNDIMY; j++)
109 for (id = 0; id < ndimXr; id++)
111 for (jd = 0; jd < ndimYr; jd++)
114 i=id+(ndimYr/2-1)-(jd/2);
118 fEdepCell[i][j] = celladc[jd][id];
120 else if (ismn >= 12 && ismn <= 23)
122 fEdepCell[i][j] = celladc[id][jd];
128 Order(); // order the data
129 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
137 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
138 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
140 // nmx1 --- number of cells having ener dep >= cutoff
142 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
144 if (nmx1 == 0) nmx1 = 1;
147 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
150 incr = CrClust(ave, cutoff, nmx1);
153 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, fClno));
155 for(i1=0; i1<=fClno; i1++)
157 Float_t cluXC = (Float_t) fClusters[0][i1];
158 Float_t cluYC = (Float_t) fClusters[1][i1];
159 Float_t cluADC = (Float_t) fClusters[2][i1];
160 Float_t cluCELLS = (Float_t) fClusters[3][i1];
161 Float_t sigmaX = (Float_t) fClusters[4][i1];
162 Float_t sigmaY = (Float_t) fClusters[5][i1];
163 Float_t cluY0 = ktwobysqrt3*cluYC;
164 Float_t cluX0 = cluXC - cluY0/2.;
166 // Cluster X centroid is back transformed
170 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
172 else if (ismn >= 12 && ismn <= 23)
174 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
178 clusdata[2] = cluADC;
179 clusdata[3] = cluCELLS;
180 clusdata[4] = sigmaX;
181 clusdata[5] = sigmaY;
184 // Cells associated with a cluster
186 for (Int_t ihit = 0; ihit < 15; ihit++)
188 celldataX[ihit] = 1; // dummy nos. -- will be changed
189 celldataY[ihit] = 1; // dummy nos. -- will be changed
192 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
196 // ------------------------------------------------------------------------ //
197 void AliPMDClusteringV2::Order()
200 // sorts the ADC values from higher to lower
203 // matrix fEdepCell converted into
204 // one dimensional array dd. adum a place holder for double
205 int i, j, i1, i2, iord1[kNMX];
207 // ordering is stored in iord1, original array not ordered
209 // define arrays dd and iord1
210 for(i1=0; i1 < kNDIMX; i1++)
212 for(i2=0; i2 < kNDIMY; i2++)
216 dd[i] = fEdepCell[i1][i2];
219 // sort and store sorting information in iord1
221 TMath::Sort(kNMX,dd,iord1);
223 // store the sorted information in fIord for later use
224 for(i=0; i<kNMX; i++)
233 // ------------------------------------------------------------------------ //
234 Int_t AliPMDClusteringV2::CrClust(Double_t ave, Double_t cutoff, Int_t nmx1)
236 // Does crude clustering
237 // Finds out only the big patch by just searching the
241 int i,j,k,id1,id2,icl, numcell;
242 int jd1,jd2, icell, cellcount;
244 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
246 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
247 // cell. There are six neighbours.
248 // cellcount --- total number of cells having nonzero ener dep
249 // numcell --- number of cells in a given supercluster
250 // ofstream ofl0("cells_loc",ios::out);
251 // initialize fInfocl[2][kNDIMX][kNDIMY]
253 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
255 for (j=0; j < kNDIMX; j++){
256 for(k=0; k < kNDIMY; k++){
257 fInfocl[0][j][k] = 0;
258 fInfocl[1][j][k] = 0;
261 for(i=0; i < kNMX; i++){
265 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
267 // ---------------------------------------------------------------
268 // crude clustering begins. Start with cell having largest adc
269 // count and loop over the cells in descending order of adc count
270 // ---------------------------------------------------------------
273 for(icell=0; icell <= nmx1; icell++){
276 if(fInfocl[0][id1][id2] == 0 ){
277 // ---------------------------------------------------------------
278 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
279 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
280 // primary and 2 for secondary cells,
281 // fInfocl[1][i1][i2] stores cluster #
282 // ---------------------------------------------------------------
285 cellcount = cellcount + 1;
286 fInfocl[0][id1][id2]=1;
287 fInfocl[1][id1][id2]=icl;
288 fInfcl[0][cellcount]=icl;
289 fInfcl[1][cellcount]=id1;
290 fInfcl[2][cellcount]=id2;
292 clust[0][numcell]=id1;
293 clust[1][numcell]=id2;
294 for(i=1; i<5000; i++)clust[0][i] = -1;
295 // ---------------------------------------------------------------
296 // check for adc count in neib. cells. If ne 0 put it in this clust
297 // ---------------------------------------------------------------
301 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
302 fInfocl[0][jd1][jd2] == 0){
304 fInfocl[0][jd1][jd2]=2;
305 fInfocl[1][jd1][jd2]=icl;
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;
314 // ---------------------------------------------------------------
315 // check adc count for neighbour's neighbours recursively and
316 // if nonzero, add these to the cluster.
317 // ---------------------------------------------------------------
318 for(i=1;i < 5000;i++){
319 if(clust[0][i] != -1){
325 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
326 fInfocl[0][jd1][jd2] == 0 ){
327 fInfocl[0][jd1][jd2] = 2;
328 fInfocl[1][jd1][jd2] = icl;
329 numcell = numcell + 1;
330 clust[0][numcell] = jd1;
331 clust[1][numcell] = jd2;
332 cellcount = cellcount+1;
333 fInfcl[0][cellcount] = icl;
334 fInfcl[1][cellcount] = jd1;
335 fInfcl[2][cellcount] = jd2;
342 // for(icell=0; icell<=cellcount; icell++){
343 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
344 // fInfcl[2][icell] << endl;
348 // ------------------------------------------------------------------------ //
349 void AliPMDClusteringV2::RefClust(Int_t incr)
351 // Does the refining of clusters
352 // Takes the big patch and does gaussian fitting and
353 // finds out the more refined clusters
356 const Int_t kndim = 4500;
358 int i, j, k, i1, i2, id, icl, itest;
361 int ncl[kndim], iord[kndim];
363 double x1, y1, z1, x2, y2, z2;
366 double x[kndim], y[kndim], z[kndim];
367 double xc[kndim], yc[kndim], zc[kndim], cells[kndim];
368 double rcl[kndim], rcs[kndim];
370 // fClno counts the final clusters
371 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
372 // x, y and z store (x,y) coordinates of and energy deposited in a cell
373 // xc, yc store (x,y) coordinates of the cluster center
374 // zc stores the energy deposited in a cluster
375 // rc is cluster radius
376 // finally the cluster information is put in 2-dimensional array clusters
377 // ofstream ofl1("checking.5",ios::app);
381 for(i=0; i<4500; i++){ncl[i]=-1;}
382 for(i=0; i<incr; i++){
383 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
385 AliWarning("RefClust: Too many superclusters!");
389 ncl[nsupcl]=ncl[nsupcl]+1;
392 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
397 for(i=0; i<nsupcl; i++){
401 // one cell super-clusters --> single cluster
402 // cluster center at the centyer of the cell
403 // cluster radius = half cell dimension
405 AliWarning("RefClust: Too many clusters! more than 5000");
411 fClusters[0][fClno] = fCoord[0][i1][i2];
412 fClusters[1][fClno] = fCoord[1][i1][i2];
413 fClusters[2][fClno] = fEdepCell[i1][i2];
414 fClusters[3][fClno] = 1.;
415 fClusters[4][fClno] = 0.0;
416 fClusters[5][fClno] = 0.0;
417 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
418 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
419 }else if(ncl[i] == 1){
420 // two cell super-cluster --> single cluster
421 // cluster center is at ener. dep.-weighted mean of two cells
422 // cluster radius == half cell dimension
426 AliWarning("RefClust: Too many clusters! more than 5000");
432 x1 = fCoord[0][i1][i2];
433 y1 = fCoord[1][i1][i2];
434 z1 = fEdepCell[i1][i2];
439 x2 = fCoord[0][i1][i2];
440 y2 = fCoord[1][i1][i2];
441 z2 = fEdepCell[i1][i2];
443 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
444 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
445 fClusters[2][fClno] = z1+z2;
446 fClusters[3][fClno] = 2.;
447 fClusters[4][fClno] = sqrt(z1*z2)/(z1+z2);
448 fClusters[5][fClno] = 0; // sigma large nonzero, sigma small zero
450 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
451 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
456 // super-cluster of more than two cells - broken up into smaller
457 // clusters gaussian centers computed. (peaks separated by > 1 cell)
458 // Begin from cell having largest energy deposited This is first
460 // *****************************************************************
461 // NOTE --- POSSIBLE MODIFICATION: ONE MAY NOT BREAKING SUPERCLUSTERS
462 // IF NO. OF CELLS IS NOT TOO LARGE ( SAY 5 OR 6 )
463 // SINCE WE EXPECT THE SUPERCLUSTER
464 // TO BE A SINGLE CLUSTER
465 //*******************************************************************
469 x[0] = fCoord[0][i1][i2];
470 y[0] = fCoord[1][i1][i2];
471 z[0] = fEdepCell[i1][i2];
473 for(j=1;j<=ncl[i];j++){
479 x[j] = fCoord[0][i1][i2];
480 y[j] = fCoord[1][i1][i2];
481 z[j] = fEdepCell[i1][i2];
483 // arranging cells within supercluster in decreasing order
484 for(j=1;j<=ncl[i];j++)
488 for(i1=0; i1<j; i1++)
490 if(itest == 0 && z[iord[i1]] < z[ihld])
493 for(i2=j-1;i2>=i1;i2--)
495 iord[i2+1] = iord[i2];
502 // compute the number of clusters and their centers ( first
504 // centers must be separated by cells having smaller ener. dep.
505 // neighbouring centers should be either strong or well-separated
510 for(j=1;j<=ncl[i];j++){
517 rr = Distance(x1,y1,x2,y2);
518 //***************************************************************
519 // finetuning cluster splitting
520 // the numbers zc/4 and zc/10 may need to be changed.
521 // Also one may need to add one more layer because our
522 // cells are smaller in absolute scale
523 //****************************************************************
526 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
528 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
530 if( rr >= 2.1)itest++;
540 ClustDetails(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0],
541 rcl[0], rcs[0], cells[0]);
549 AliWarning("RefClust: Too many clusters! more than 5000");
553 fClusters[0][fClno] = xc[j];
554 fClusters[1][fClno] = yc[j];
555 fClusters[2][fClno] = zc[j];
556 fClusters[4][fClno] = rcl[j];
557 fClusters[5][fClno] = rcs[j];
560 fClusters[3][fClno] = ncl[i];
564 fClusters[3][fClno] = cells[j];
574 // ------------------------------------------------------------------------ //
576 void AliPMDClusteringV2::ClustDetails(Int_t ncell, Int_t nclust,
577 Double_t &x, Double_t &y, Double_t &z,
578 Double_t &xc, Double_t &yc, Double_t &zc,
579 Double_t &rcl, Double_t &rcs,
585 const Int_t kndim1 = 4500;
586 const Int_t kndim2 = 10;
587 const Int_t kndim3 = 100;
590 int cluster[kndim1][kndim2];
592 double x1, y1, x2, y2, rr;
593 double sumx, sumy, sumxy, sumxx;
594 double sum, sum1, sumyy;
597 double xx[kndim1], yy[kndim1], zz[kndim1];
598 double xxc[kndim1], yyc[kndim1];
603 double xcl[kndim1], ycl[kndim1], cln[kndim1];
604 double clustcell[kndim1][kndim3];
606 for(i=0; i<=nclust; i++){
612 for(i=0; i<=ncell; i++){
618 for(i=0; i<4500; i++){
619 for(j=0; j<100; j++){
633 // more than one cluster
634 // checking cells shared between several clusters.
635 // First check if the cell is within
636 // one cell unit ( nearest neighbour). Else,
637 // if it is within 1.74 cell units ( next nearest )
638 // Else if it is upto 2 cell units etc.
640 for (i=0; i<=ncell; i++){
644 // distance <= 1 cell unit
645 for(j=0; j<=nclust; j++)
649 rr = Distance(x1, y1, x2, y2);
657 // next nearest neighbour
658 if(cluster[i][0] == 0)
660 for(j=0; j<=nclust; j++)
664 rr = Distance(x1, y1, x2, y2);
673 // next-to-next nearest neighbour
674 if(cluster[i][0] == 0)
676 for(j=0; j<=nclust; j++)
680 rr = Distance(x1, y1, x2, y2);
690 if(cluster[i][0] == 0)
692 for(j=0; j<=nclust; j++)
696 rr = Distance(x1, y1, x2, y2);
708 // computing cluster strength. Some cells are shared.
709 for(i=0; i<=ncell; i++){
710 if(cluster[i][0] != 0){
712 for(j=1; j<=i1; j++){
714 str[i2] = str[i2]+zz[i]/i1;
721 for(i=0; i<=ncell; i++)
723 if(cluster[i][0] != 0)
729 sum=sum+str[cluster[i][j]];
735 str1[i2] = str1[i2] + zz[i]*str[i2]/sum;
736 clustcell[i2][i] = zz[i]*str[i2]/sum;
742 for(j=0; j<=nclust; j++)
749 for(i=0; i<=nclust; i++){
754 for(j=0; j<=ncell; j++){
755 if(clustcell[i][j] != 0){
756 sumx = sumx+clustcell[i][j]*xx[j];
757 sumy = sumy+clustcell[i][j]*yy[j];
758 sum = sum+clustcell[i][j];
759 sum1 = sum1+clustcell[i][j]/zz[j];
762 //***** xcl and ycl are cluster centroid positions ( center of gravity )
770 for(j=0; j<=ncell; j++){
771 sumxx = sumxx+clustcell[i][j]*(xx[j]-xcl[i])*(xx[j]-xcl[i])/sum;
772 sumyy = sumyy+clustcell[i][j]*(yy[j]-ycl[i])*(yy[j]-ycl[i])/sum;
773 sumxy = sumxy+clustcell[i][j]*(xx[j]-xcl[i])*(yy[j]-ycl[i])/sum;
776 c = sumxx*sumyy-sumxy*sumxy;
777 // ******************r1 and r2 are major and minor axes ( r1 > r2 ).
778 r1 = b/2.+sqrt(b*b/4.-c);
779 r2 = b/2.-sqrt(b*b/4.-c);
780 // final assignments to proper external variables
784 *(&cells + i) = cln[i];
794 for(j=0; j<=ncell; j++){
795 sumx = sumx+zz[j]*xx[j];
796 sumy = sumy+zz[j]*yy[j];
806 for(j=0; j<=ncell; j++){
807 sumxx = sumxx+clustcell[i][j]*(xx[j]-xcl[i])*(xx[j]-xcl[i])/sum;
808 sumyy = sumyy+clustcell[i][j]*(yy[j]-ycl[i])*(yy[j]-ycl[i])/sum;
809 sumxy = sumxy+clustcell[i][j]*(xx[j]-xcl[i])*(yy[j]-ycl[i])/sum;
812 c = sumxx*sumyy-sumxy*sumxy;
813 r1 = b/2.+sqrt(b*b/4.-c);
814 r2 = b/2.-sqrt(b*b/4.-c);
819 *(&cells + i) = cln[i];
825 // ------------------------------------------------------------------------ //
826 Double_t AliPMDClusteringV2::Distance(Double_t x1, Double_t y1,
827 Double_t x2, Double_t y2)
829 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
831 // ------------------------------------------------------------------------ //
832 void AliPMDClusteringV2::SetEdepCut(Float_t decut)
836 // ------------------------------------------------------------------------ //