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 edepcell[kNMX] and cluster information is in a
34 TObjarray. Integer clno gives total number of clusters in the
37 fClusters is 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>
51 #include "AliPMDcludata.h"
52 #include "AliPMDcluster.h"
53 #include "AliPMDClustering.h"
54 #include "AliPMDClusteringV1.h"
57 ClassImp(AliPMDClusteringV1)
59 const Double_t AliPMDClusteringV1::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
61 AliPMDClusteringV1::AliPMDClusteringV1():
62 fPMDclucont(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;
74 // ------------------------------------------------------------------------ //
75 AliPMDClusteringV1::AliPMDClusteringV1(const AliPMDClusteringV1& pmdclv1):
76 AliPMDClustering(pmdclv1),
81 AliError("Copy constructor not allowed ");
84 // ------------------------------------------------------------------------ //
85 AliPMDClusteringV1 &AliPMDClusteringV1::operator=(const AliPMDClusteringV1& /*pmdclv1*/)
88 AliError("Assignment operator not allowed ");
91 // ------------------------------------------------------------------------ //
92 AliPMDClusteringV1::~AliPMDClusteringV1()
96 // ------------------------------------------------------------------------ //
97 void AliPMDClusteringV1::DoClust(Int_t idet, Int_t ismn,
98 Double_t celladc[48][96], TObjArray *pmdcont)
100 // main function to call other necessary functions to do clustering
103 AliPMDcluster *pmdcl = 0;
105 Int_t i, j, nmx1, incr, id, jd;
106 Int_t celldataX[15], celldataY[15];
108 Double_t cutoff, ave;
109 Double_t edepcell[kNMX];
111 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
113 // ndimXr and ndimYr are different because of different module size
123 else if (ismn >= 12 && ismn <= 23)
130 for (Int_t i = 0; i < kNDIMX; i++)
132 for (Int_t j = 0; j < kNDIMY; j++)
134 fCellTrNo[i][j] = -1;
140 for (id = 0; id < ndimXr; id++)
142 for (jd = 0; jd < ndimYr; jd++)
145 i = id+(ndimYr/2-1)-(jd/2);
147 //PH Int_t ij = i + j*kNDIMX;
148 Int_t ij = i + j*ndimXr;
152 edepcell[ij] = celladc[jd][id];
153 fCellTrNo[i][j] = jd*10000+id; // for association
155 else if (ismn >= 12 && ismn <= 23)
157 edepcell[ij] = celladc[id][jd];
158 fCellTrNo[i][j] = id*10000+jd; // for association
165 TMath::Sort(kNMX,edepcell,iord1);// order the data
166 cutoff = fCutoff; // cutoff to discard cells
169 for(i = 0;i < kNMX; i++)
175 if(edepcell[i] > cutoff )
181 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
183 if (nmx1 == 0) nmx1 = 1;
186 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
189 incr = CrClust(ave, cutoff, nmx1,iord1, edepcell );
190 RefClust(incr,edepcell);
191 Int_t nentries1 = fPMDclucont->GetEntries();
192 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, nentries1));
193 AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
195 for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
197 AliPMDcludata *pmdcludata =
198 (AliPMDcludata*)fPMDclucont->UncheckedAt(ient1);
199 Float_t cluXC = pmdcludata->GetClusX();
200 Float_t cluYC = pmdcludata->GetClusY();
201 Float_t cluADC = pmdcludata->GetClusADC();
202 Float_t cluCELLS = pmdcludata->GetClusCells();
203 Float_t cluSIGX = pmdcludata->GetClusSigmaX();
204 Float_t cluSIGY = pmdcludata->GetClusSigmaY();
206 Float_t cluY0 = ktwobysqrt3*cluYC;
207 Float_t cluX0 = cluXC - cluY0/2.;
210 // Cluster X centroid is back transformed
214 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
216 else if ( ismn >= 12 && ismn <= 23)
218 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
222 clusdata[2] = cluADC;
223 clusdata[3] = cluCELLS;
224 clusdata[4] = cluSIGX;
225 clusdata[5] = cluSIGY;
228 // Cells associated with a cluster
231 for (Int_t ihit = 0; ihit < 15; ihit++)
235 celldataX[ihit] = pmdcludata->GetCellXY(ihit)%10000;
236 celldataY[ihit] = pmdcludata->GetCellXY(ihit)/10000;
238 else if (ismn >= 12 && ismn <= 23)
240 celldataX[ihit] = pmdcludata->GetCellXY(ihit)/10000;
241 celldataY[ihit] = pmdcludata->GetCellXY(ihit)%10000;
244 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
248 fPMDclucont->Clear();
251 // ------------------------------------------------------------------------ //
252 Int_t AliPMDClusteringV1::CrClust(Double_t ave, Double_t cutoff, Int_t nmx1,
253 Int_t iord1[], Double_t edepcell[])
255 // Does crude clustering
256 // Finds out only the big patch by just searching the
259 Int_t i,j,k,id1,id2,icl, numcell, clust[2][5000];
260 Int_t jd1,jd2, icell, cellcount;
261 static Int_t neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
263 AliDebug(1,Form("kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f",kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff));
265 for (j = 0; j < kNDIMX; j++)
267 for(k = 0; k < kNDIMY; k++)
269 fInfocl[0][j][k] = 0;
270 fInfocl[1][j][k] = 0;
273 for(i=0; i < kNMX; i++)
281 if(edepcell[j] <= cutoff)
283 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 // ---------------------------------------------------------------
295 for(icell = 0; icell <= nmx1; icell++)
301 if(fInfocl[0][id1][id2] == 0 )
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;
315 for(i = 1; i < 5000; i++)
319 // ---------------------------------------------------------------
320 // check for adc count in neib. cells. If ne 0 put it in this clust
321 // ---------------------------------------------------------------
322 for(i = 0; i < 6; i++)
324 jd1 = id1 + neibx[i];
325 jd2 = id2 + neiby[i];
326 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
327 fInfocl[0][jd1][jd2] == 0)
330 fInfocl[0][jd1][jd2] = 2;
331 fInfocl[1][jd1][jd2] = icl;
332 clust[0][numcell] = jd1;
333 clust[1][numcell] = jd2;
335 fInfcl[0][cellcount] = icl;
336 fInfcl[1][cellcount] = jd1;
337 fInfcl[2][cellcount] = jd2;
340 // ---------------------------------------------------------------
341 // check adc count for neighbour's neighbours recursively and
342 // if nonzero, add these to the cluster.
343 // ---------------------------------------------------------------
344 for(i = 1; i < 5000;i++)
350 for(j = 0; j < 6 ; j++)
352 jd1 = id1 + neibx[j];
353 jd2 = id2 + neiby[j];
354 if( (jd1 >= 0 && jd1 < kNDIMX) &&
355 (jd2 >= 0 && jd2 < kNDIMY) &&
356 fInfocl[0][jd1][jd2] == 0 )
358 fInfocl[0][jd1][jd2] = 2;
359 fInfocl[1][jd1][jd2] = icl;
361 clust[0][numcell] = jd1;
362 clust[1][numcell] = jd2;
364 fInfcl[0][cellcount] = icl;
365 fInfcl[1][cellcount] = jd1;
366 fInfcl[2][cellcount] = jd2;
375 // ------------------------------------------------------------------------ //
376 void AliPMDClusteringV1::RefClust(Int_t incr, Double_t edepcell[])
378 // Does the refining of clusters
379 // Takes the big patch and does gaussian fitting and
380 // finds out the more refined clusters
385 AliPMDcludata *pmdcludata = 0;
387 Int_t *cellCount = 0x0;
388 Int_t **cellXY = 0x0;
389 const Int_t kdim = 4500;
391 Int_t i, j, k, i1, i2, id, icl, itest,ihld, ig, nsupcl,clno;
393 Int_t ncl[kdim], iord[kdim], lev1[kdim], lev2[kdim];
396 Double_t x1, y1, z1, x2, y2, z2, dist,rr,sum;
397 Double_t x[kdim], y[kdim], z[kdim];
398 Double_t xc[kdim], yc[kdim], zc[kdim], cells[kdim], rc[kdim];
401 for(i = 0; i<kdim; i++)
405 if (i < 6) clusdata[i] = 0.;
406 if (i < 15) clxy[i] = 0;
409 // clno counts the final clusters
410 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
411 // x, y and z store (x,y) coordinates of and energy deposited in a cell
412 // xc, yc store (x,y) coordinates of the cluster center
413 // zc stores the energy deposited in a cluster
414 // rc is cluster radius
419 for(i = 0; i <= incr; i++)
421 if(fInfcl[0][i] != nsupcl)
427 AliWarning("RefClust: Too many superclusters!");
434 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
439 for(i = 0; i <= nsupcl; i++)
447 AliWarning("RefClust: Too many clusters! more than 5000");
454 Int_t i12 = i1 + i2*kNDIMX;
456 clusdata[0] = fCoord[0][i1][i2];
457 clusdata[1] = fCoord[1][i1][i2];
458 clusdata[2] = edepcell[i12];
463 clxy[0] = fCellTrNo[i1][i2]; //association
464 for(Int_t icltr = 1; icltr < 15; icltr++)
468 pmdcludata = new AliPMDcludata(clusdata,clxy);
469 fPMDclucont->Add(pmdcludata);
477 AliWarning("RefClust: Too many clusters! more than 5000");
483 Int_t i12 = i1 + i2*kNDIMX;
485 x1 = fCoord[0][i1][i2];
486 y1 = fCoord[1][i1][i2];
488 clxy[0] = fCellTrNo[i1][i2]; //asso
493 Int_t i22 = i1 + i2*kNDIMX;
494 x2 = fCoord[0][i1][i2];
495 y2 = fCoord[1][i1][i2];
497 clxy[1] = fCellTrNo[i1][i2]; //asso
498 for(Int_t icltr = 2; icltr < 15; icltr++)
503 clusdata[0] = (x1*z1+x2*z2)/(z1+z2);
504 clusdata[1] = (y1*z1+y2*z2)/(z1+z2);
509 pmdcludata = new AliPMDcludata(clusdata,clxy);
510 fPMDclucont->Add(pmdcludata);
516 // super-cluster of more than two cells - broken up into smaller
517 // clusters gaussian centers computed. (peaks separated by > 1 cell)
518 // Begin from cell having largest energy deposited This is first
522 Int_t i12 = i1 + i2*kNDIMX;
524 x[0] = fCoord[0][i1][i2];
525 y[0] = fCoord[1][i1][i2];
526 z[0] = edepcell[i12];
528 t[0] = fCellTrNo[i1][i2]; //asso
531 for(j = 1; j <= ncl[i]; j++)
536 Int_t i12 = i1 + i2*kNDIMX;
539 x[j] = fCoord[0][i1][i2];
540 y[j] = fCoord[1][i1][i2];
541 z[j] = edepcell[i12];
543 t[j] = fCellTrNo[i1][i2]; //asso
546 // arranging cells within supercluster in decreasing order
548 for(j = 1;j <= ncl[i]; j++)
552 for(i1 = 0; i1 < j; i1++)
554 if(itest == 0 && z[iord[i1]] < z[ihld])
557 for(i2 = j-1; i2 >= i1; i2--)
559 iord[i2+1] = iord[i2];
565 // compute the number of Gaussians and their centers ( first
567 // centers must be separated by cells having smaller ener. dep.
568 // neighbouring centers should be either strong or well-separated
573 for(j = 1; j <= ncl[i]; j++)
578 for(k = 0; k <= ig; k++)
582 rr = Distance(x1,y1,x2,y2);
583 if(rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)itest++;
584 if(rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)itest++;
585 if( rr >= 2.1)itest++;
595 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
597 // compute the number of cells belonging to each cluster.
598 // cell is shared between several clusters ( if they are equidistant
599 // from it ) in the ratio of cluster energy deposition
602 cellCount = new Int_t [ig+1];
603 cellXY = new Int_t *[jj];
604 for(Int_t ij = 0; ij < 15; ij++) cellXY[ij] = new Int_t [ig+1];
606 for(j = 0; j <= ig; j++)
614 for(j = 0; j <= ncl[i]; j++)
618 for(k = 0; k <= ig; k++)
620 dist = Distance(x[j], y[j], xc[k], yc[k]);
621 if(dist < TMath::Sqrt(3.) )
624 if (cellCount[k] < 15)
626 cellXY[cellCount[k]][k] = t[j];
653 for(k = 1; k <= lev1[0]; k++)
657 for(k = 1; k <= lev1[0]; k++)
659 cells[lev1[k]] += zc[lev1[k]]/sum;
672 for( k = 1; k <= lev2[0]; k++)
676 for(k = 1; k <= lev2[0]; k++)
678 cells[lev2[k]] += zc[lev2[k]]/sum;
685 // zero rest of the cell array
687 for( k = 0; k <= ig; k++)
689 for(Int_t icltr = cellCount[k]; icltr < 15; icltr++)
691 cellXY[icltr][k] = -1;
696 for(j = 0; j <= ig; j++)
701 AliWarning("RefClust: Too many clusters! more than 5000");
711 clusdata[3] = ncl[i];
715 clusdata[3] = cells[j];
719 for (Int_t ii=0; ii < 15; ii++)
721 clxy[ii] = cellXY[ii][j];
723 pmdcludata = new AliPMDcludata(clusdata,clxy);
724 fPMDclucont->Add(pmdcludata);
727 for(Int_t jj = 0; jj < 15; jj++)delete [] cellXY[jj];
732 // ------------------------------------------------------------------------ //
733 void AliPMDClusteringV1::GaussFit(Int_t ncell, Int_t nclust, Double_t &x,
734 Double_t &y ,Double_t &z, Double_t &xc,
735 Double_t &yc, Double_t &zc, Double_t &rc)
737 // Does gaussian fitting
740 const Int_t kdim = 4500;
741 Int_t i, j, i1, i2, novar, idd, jj;
742 Int_t neib[kdim][50];
744 Double_t sum, dx, dy, str, str1, aint, sum1, rr, dum;
745 Double_t x1, x2, y1, y2;
746 Double_t xx[kdim], yy[kdim], zz[kdim], xxc[kdim], yyc[kdim];
747 Double_t a[kdim], b[kdim], c[kdim], d[kdim], ha[kdim], hb[kdim];
748 Double_t hc[kdim], hd[kdim], zzc[kdim], rrc[kdim];
758 for(i = 0; i <= ncell; i++)
765 for(i=0; i<=nclust; i++)
773 for(i = 0; i <= nclust; i++)
775 zzc[i] = str/str1*zzc[i];
784 for(i = 0; i <= ncell; i++)
789 for(j = 0; j <= nclust; j++)
793 if(Distance(x1,y1,x2,y2) <= 3.)
802 for(i1 = 0; i1 <= ncell; i1++)
806 for(i2 = 1; i2 <= idd; i2++)
809 dx = xx[i1] - xxc[jj];
810 dy = yy[i1] - yyc[jj];
811 dum = rrc[j]*rrc[jj] + rr*rr;
812 aint += exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
814 sum += (aint - zz[i1])*(aint - zz[i1])/str;
818 for(i = 0; i <= nclust; i++)
820 a[i] = xxc[i] + 0.6*(rnd.Uniform() - 0.5);
821 b[i] = yyc[i] + 0.6*(rnd.Uniform() - 0.5);
822 c[i] = zzc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.2);
824 d[i] = rrc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.1);
831 for(i = 0; i <= nclust; i++)
833 c[i] = c[i]*str/str1;
837 for(i1 = 0; i1 <= ncell; i1++)
841 for(i2 = 1; i2 <= idd; i2++)
846 dum = d[jj]*d[jj]+rr*rr;
847 aint += exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
849 sum1 += (aint - zz[i1])*(aint - zz[i1])/str;
854 for(i2 = 0; i2 <= nclust; i2++)
863 for(j = 0; j <= nclust; j++)
871 // ------------------------------------------------------------------------ //
872 Double_t AliPMDClusteringV1::Distance(Double_t x1, Double_t y1,
873 Double_t x2, Double_t y2)
875 return TMath::Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
877 // ------------------------------------------------------------------------ //
878 void AliPMDClusteringV1::SetEdepCut(Float_t decut)
882 // ------------------------------------------------------------------------ //