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 a
34 TObjarray. Integer clno gives total number of clusters in the
37 fEdepCell 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 "AliPMDcludata.h"
51 #include "AliPMDcluster.h"
52 #include "AliPMDClustering.h"
53 #include "AliPMDClusteringV1.h"
57 ClassImp(AliPMDClusteringV1)
59 const Double_t AliPMDClusteringV1::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
61 AliPMDClusteringV1::AliPMDClusteringV1():
62 pmdclucont(new TObjArray()),
65 for(Int_t i = 0; i < kNDIMX; i++)
67 for(Int_t j = 0; j < kNDIMY; j++)
69 fCoord[0][i][j] = i+j/2.;
70 fCoord[1][i][j] = fgkSqroot3by2*j;
75 // ------------------------------------------------------------------------ //
76 AliPMDClusteringV1::~AliPMDClusteringV1()
80 // ------------------------------------------------------------------------ //
81 void AliPMDClusteringV1::DoClust(Int_t idet, Int_t ismn,
82 Double_t celladc[48][96], TObjArray *pmdcont)
84 // main function to call other necessary functions to do clustering
86 AliPMDcluster *pmdcl = 0;
88 int id and jd defined to read the input data.
89 It is assumed that for data we have 0 <= id <= 48
93 Int_t i, i1, i2, j, nmx1, incr, id, jd;
94 Int_t celldataX[15], celldataY[15];
99 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
101 // ndimXr and ndimYr are different because of different module size
111 else if (ismn >= 12 && ismn <= 23)
117 for (Int_t i = 0; i < kNDIMX; i++)
119 for (Int_t j = 0; j < kNDIMY; j++)
122 fCellTrNo[i][j] = -1;
126 for (id = 0; id < ndimXr; id++)
128 for (jd = 0; jd < ndimYr; jd++)
131 i = id+(ndimYr/2-1)-(jd/2);
135 fEdepCell[i][j] = celladc[jd][id];
136 fCellTrNo[i][j] = jd*10000+id; /* for association */
138 else if (ismn >= 12 && ismn <= 23)
140 fEdepCell[i][j] = celladc[id][jd];
141 fCellTrNo[i][j] = id*10000+jd; /* for association */
146 Order(); // order the data
147 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
151 for(j = 0;j < kNMX; j++)
155 if(fEdepCell[i1][i2] > 0.)
157 ave += fEdepCell[i1][i2];
159 if(fEdepCell[i1][i2] > cutoff )
165 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
167 if (nmx1 == 0) nmx1 = 1;
170 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
173 incr = CrClust(ave, cutoff, nmx1);
175 Int_t nentries1 = pmdclucont->GetEntries();
176 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, nentries1));
177 AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
178 for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
180 AliPMDcludata *pmdcludata =
181 (AliPMDcludata*)pmdclucont->UncheckedAt(ient1);
182 Float_t cluXC = pmdcludata->GetClusX();
183 Float_t cluYC = pmdcludata->GetClusY();
184 Float_t cluADC = pmdcludata->GetClusADC();
185 Float_t cluCELLS = pmdcludata->GetClusCells();
186 Float_t cluSIGX = pmdcludata->GetClusSigmaX();
187 Float_t cluSIGY = pmdcludata->GetClusSigmaY();
189 Float_t cluY0 = ktwobysqrt3*cluYC;
190 Float_t cluX0 = cluXC - cluY0/2.;
193 // Cluster X centroid is back transformed
197 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
199 else if ( ismn >= 12 && ismn <= 23)
201 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
205 clusdata[2] = cluADC;
206 clusdata[3] = cluCELLS;
207 clusdata[4] = cluSIGX;
208 clusdata[5] = cluSIGY;
211 // Cells associated with a cluster
213 for (Int_t ihit = 0; ihit < 15; ihit++)
218 celldataX[ihit] = fClTr[ihit][i1]%10000;
219 celldataY[ihit] = fClTr[ihit][i1]/10000;
221 else if (ismn >= 12 && ismn <= 23)
223 celldataX[ihit] = fClTr[ihit][i1]/10000;
224 celldataY[ihit] = fClTr[ihit][i1]%10000;
227 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
234 // ------------------------------------------------------------------------ //
235 void AliPMDClusteringV1::Order()
238 // sorts the ADC values from higher to lower
244 for(i1=0; i1 < kNDIMX; i1++)
246 for(i2=0; i2 < kNDIMY; i2++)
250 dd[i] = fEdepCell[i1][i2];
254 TMath::Sort(kNMX,dd,iord1); //PH Using much better algorithm...
255 for(i=0; i<kNMX; i++)
264 // ------------------------------------------------------------------------ //
265 Int_t AliPMDClusteringV1::CrClust(Double_t ave, Double_t cutoff, Int_t nmx1)
267 // Does crude clustering
268 // Finds out only the big patch by just searching the
271 Int_t i,j,k,id1,id2,icl, numcell, clust[2][5000];
272 Int_t jd1,jd2, icell, cellcount;
273 static Int_t neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
275 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
276 // cell. There are six neighbours.
277 // cellcount --- total number of cells having nonzero ener dep
278 // numcell --- number of cells in a given supercluster
280 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
282 for (j = 0; j < kNDIMX; j++)
284 for(k = 0; k < kNDIMY; k++)
286 fInfocl[0][j][k] = 0;
287 fInfocl[1][j][k] = 0;
290 for(i=0; i < kNMX; i++)
295 if(fEdepCell[id1][id2] <= cutoff)
297 fInfocl[0][id1][id2] = -1;
300 // ---------------------------------------------------------------
301 // crude clustering begins. Start with cell having largest adc
302 // count and loop over the cells in descending order of adc count
303 // ---------------------------------------------------------------
307 for(icell = 0; icell <= nmx1; icell++)
309 id1 = fIord[0][icell];
310 id2 = fIord[1][icell];
311 if(fInfocl[0][id1][id2] == 0 )
316 fInfocl[0][id1][id2] = 1;
317 fInfocl[1][id1][id2] = icl;
318 fInfcl[0][cellcount] = icl;
319 fInfcl[1][cellcount] = id1;
320 fInfcl[2][cellcount] = id2;
322 clust[0][numcell] = id1;
323 clust[1][numcell] = id2;
325 for(i = 1; i < 5000; i++)
329 // ---------------------------------------------------------------
330 // check for adc count in neib. cells. If ne 0 put it in this clust
331 // ---------------------------------------------------------------
332 for(i = 0; i < 6; i++)
334 jd1 = id1 + neibx[i];
335 jd2 = id2 + neiby[i];
336 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
337 fInfocl[0][jd1][jd2] == 0)
340 fInfocl[0][jd1][jd2] = 2;
341 fInfocl[1][jd1][jd2] = icl;
342 clust[0][numcell] = jd1;
343 clust[1][numcell] = jd2;
345 fInfcl[0][cellcount] = icl;
346 fInfcl[1][cellcount] = jd1;
347 fInfcl[2][cellcount] = jd2;
350 // ---------------------------------------------------------------
351 // check adc count for neighbour's neighbours recursively and
352 // if nonzero, add these to the cluster.
353 // ---------------------------------------------------------------
354 for(i = 1; i < 5000;i++)
360 for(j = 0; j < 6 ; j++)
362 jd1 = id1 + neibx[j];
363 jd2 = id2 + neiby[j];
364 if( (jd1 >= 0 && jd1 < kNDIMX) &&
365 (jd2 >= 0 && jd2 < kNDIMY) &&
366 fInfocl[0][jd1][jd2] == 0 )
368 fInfocl[0][jd1][jd2] = 2;
369 fInfocl[1][jd1][jd2] = icl;
371 clust[0][numcell] = jd1;
372 clust[1][numcell] = jd2;
374 fInfcl[0][cellcount] = icl;
375 fInfcl[1][cellcount] = jd1;
376 fInfcl[2][cellcount] = jd2;
385 // ------------------------------------------------------------------------ //
386 void AliPMDClusteringV1::RefClust(Int_t incr)
388 // Does the refining of clusters
389 // Takes the big patch and does gaussian fitting and
390 // finds out the more refined clusters
393 const Int_t kdim = 4500;
394 Int_t i, j, k, i1, i2, id, icl, itest,ihld, ig, nsupcl,clno;
395 Double_t x1, y1, z1, x2, y2, z2, dist,rr,sum;
397 Int_t t[kdim],cellCount[kdim];
398 Int_t ncl[kdim], iord[kdim], lev1[20], lev2[20];
399 Double_t x[kdim], y[kdim], z[kdim];
400 Double_t xc[kdim], yc[kdim], zc[kdim], cells[kdim], rc[kdim];
403 for(Int_t kk = 0; kk < 6; kk++)
409 for(i = 0; i<kdim; i++)
414 // clno counts the final clusters
415 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
416 // x, y and z store (x,y) coordinates of and energy deposited in a cell
417 // xc, yc store (x,y) coordinates of the cluster center
418 // zc stores the energy deposited in a cluster
419 // rc is cluster radius
423 for(i = 0; i < kdim; i++)
427 for(i = 0; i <= incr; i++)
429 if(fInfcl[0][i] != nsupcl)
435 AliWarning("RefClust: Too many superclusters!");
442 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
446 for(i = 0; i <= nsupcl; i++)
454 AliWarning("RefClust: Too many clusters! more than 5000");
461 clusdata[0] = fCoord[0][i1][i2];
462 clusdata[1] = fCoord[1][i1][i2];
463 clusdata[2] = fEdepCell[i1][i2];
467 pmdcludata = new AliPMDcludata(clusdata);
468 pmdclucont->Add(pmdcludata);
472 fClTr[0][clno] = fCellTrNo[i1][i2];
473 for(Int_t icltr = 1; icltr < 14; icltr++)
475 fClTr[icltr][clno] = -1;
484 AliWarning("RefClust: Too many clusters! more than 5000");
490 x1 = fCoord[0][i1][i2];
491 y1 = fCoord[1][i1][i2];
492 z1 = fEdepCell[i1][i2];
495 fClTr[0][clno] = fCellTrNo[i1][i2];
501 x2 = fCoord[0][i1][i2];
502 y2 = fCoord[1][i1][i2];
503 z2 = fEdepCell[i1][i2];
507 fClTr[1][clno] = fCellTrNo[i1][i2];
508 for(Int_t icltr = 2; icltr < 14; icltr++)
510 fClTr[icltr][clno] = -1;
514 clusdata[0] = (x1*z1+x2*z2)/(z1+z2);
515 clusdata[1] = (y1*z1+y2*z2)/(z1+z2);
520 pmdcludata = new AliPMDcludata(clusdata);
521 pmdclucont->Add(pmdcludata);
526 for(Int_t icg = 0; icg < kdim; icg++)
534 // super-cluster of more than two cells - broken up into smaller
535 // clusters gaussian centers computed. (peaks separated by > 1 cell)
536 // Begin from cell having largest energy deposited This is first
540 x[0] = fCoord[0][i1][i2];
541 y[0] = fCoord[1][i1][i2];
542 z[0] = fEdepCell[i1][i2];
545 t[0] = fCellTrNo[i1][i2];
549 for(j = 1; j <= ncl[i]; j++)
555 x[j] = fCoord[0][i1][i2];
556 y[j] = fCoord[1][i1][i2];
557 z[j] = fEdepCell[i1][i2];
559 t[j] = fCellTrNo[i1][i2];
564 // arranging cells within supercluster in decreasing order
566 for(j = 1;j <= ncl[i]; j++)
570 for(i1 = 0; i1 < j; i1++)
572 if(itest == 0 && z[iord[i1]] < z[ihld])
575 for(i2 = j-1; i2 >= i1; i2--)
577 iord[i2+1] = iord[i2];
583 // compute the number of Gaussians and their centers ( first
585 // centers must be separated by cells having smaller ener. dep.
586 // neighbouring centers should be either strong or well-separated
591 for(j = 1; j <= ncl[i]; j++)
596 for(k = 0; k <= ig; k++)
600 rr = Distance(x1,y1,x2,y2);
601 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
605 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
623 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
625 // compute the number of cells belonging to each cluster.
626 // cell is shared between several clusters ( if they are equidistant
627 // from it ) in the ratio of cluster energy deposition
628 for(j = 0; j <= ig; j++)
634 for(j = 0; j <= ncl[i]; j++)
638 for(k = 0; k <= ig; k++)
640 dist = Distance(x[j], y[j], xc[k], yc[k]);
641 if(dist < TMath::Sqrt(3.) )
644 fClTr[cellCount[k]][clno+k+1] = t[j];
670 for(k = 1; k <= lev1[0]; k++)
674 for(k = 1; k <= lev1[0]; k++)
676 cells[lev1[k]] += zc[lev1[k]]/sum;
689 for( k = 1; k <= lev2[0]; k++)
693 for(k = 1; k <= lev2[0]; k++)
695 cells[lev2[k]] += zc[lev2[k]]/sum;
702 // zero rest of the cell array
704 for( k = 0; k <= ig; k++)
706 for(Int_t icltr = cellCount[k]; icltr < 14; icltr++)
708 fClTr[icltr][clno] = -1;
713 for(j = 0; j <= ig; j++)
718 AliWarning("RefClust: Too many clusters! more than 5000");
728 clusdata[3] = ncl[i];
732 clusdata[3] = cells[j];
734 pmdcludata = new AliPMDcludata(clusdata);
735 pmdclucont->Add(pmdcludata);
740 // ------------------------------------------------------------------------ //
741 void AliPMDClusteringV1::GaussFit(Int_t ncell, Int_t nclust, Double_t &x,
742 Double_t &y ,Double_t &z, Double_t &xc,
743 Double_t &yc, Double_t &zc, Double_t &rc)
745 // Does gaussian fitting
747 const Int_t kdim = 4500;
748 Int_t i, j, i1, i2, novar, idd, jj;
749 Double_t sum, dx, dy, str, str1, aint, sum1, rr, dum;
750 Double_t x1, x2, y1, y2;
751 Double_t xx[kdim], yy[kdim], zz[kdim], xxc[kdim], yyc[kdim];
752 Double_t a[kdim], b[kdim], c[kdim], d[kdim], ha[kdim], hb[kdim];
753 Double_t hc[kdim], hd[kdim], zzc[kdim], rrc[kdim];
754 Int_t neib[kdim][50];
764 for(i = 0; i <= ncell; i++)
771 for(i=0; i<=nclust; i++)
779 for(i = 0; i <= nclust; i++)
781 zzc[i] = str/str1*zzc[i];
789 for(i = 0; i <= ncell; i++)
794 for(j = 0; j <= nclust; j++)
798 if(Distance(x1,y1,x2,y2) <= 3.)
807 for(i1 = 0; i1 <= ncell; i1++)
811 for(i2 = 1; i2 <= idd; i2++)
814 dx = xx[i1] - xxc[jj];
815 dy = yy[i1] - yyc[jj];
816 dum = rrc[j]*rrc[jj] + rr*rr;
817 aint += exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
819 sum += (aint - zz[i1])*(aint - zz[i1])/str;
822 for(i = 0; i <= nclust; i++)
824 a[i] = xxc[i] + 0.6*(rnd.Uniform() - 0.5);
825 b[i] = yyc[i] + 0.6*(rnd.Uniform() - 0.5);
826 c[i] = zzc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.2);
828 d[i] = rrc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.1);
835 for(i = 0; i <= nclust; i++)
837 c[i] = c[i]*str/str1;
840 for(i1 = 0; i1 <= ncell; i1++)
844 for(i2 = 1; i2 <= idd; i2++)
849 dum = d[jj]*d[jj]+rr*rr;
850 aint += exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
852 sum1 += (aint - zz[i1])*(aint - zz[i1])/str;
857 for(i2 = 0; i2 <= nclust; i2++)
866 for(j = 0; j <= nclust; j++)
874 // ------------------------------------------------------------------------ //
875 Double_t AliPMDClusteringV1::Distance(Double_t x1, Double_t y1,
876 Double_t x2, Double_t y2)
878 return TMath::Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
880 // ------------------------------------------------------------------------ //
881 void AliPMDClusteringV1::SetEdepCut(Float_t decut)
885 // ------------------------------------------------------------------------ //