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];
112 Double_t *cellenergy = new Double_t [11424];// Ajay
114 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
116 // ndimXr and ndimYr are different because of different module size
126 else if (ismn >= 12 && ismn <= 23)
132 for (Int_t i =0; i < 11424; i++)
139 for (Int_t i = 0; i < kNDIMX; i++)
141 for (Int_t j = 0; j < kNDIMY; j++)
143 fCellTrNo[i][j] = -1;
149 for (id = 0; id < ndimXr; id++)
151 for (jd = 0; jd < ndimYr; jd++)
154 i = id+(ndimYr/2-1)-(jd/2);
156 Int_t ij = i + j*kNDIMX;
157 // BKN Int_t ij = i + j*ndimXr;
161 //edepcell[ij] = celladc[jd][id];
162 cellenergy[ij] = celladc[jd][id];//Ajay
163 fCellTrNo[i][j] = jd*10000+id; // for association
165 else if (ismn >= 12 && ismn <= 23)
167 //edepcell[ij] = celladc[id][jd];
168 cellenergy[ij] = celladc[id][jd];//Ajay
169 fCellTrNo[i][j] = id*10000+jd; // for association
175 for (Int_t i = 0; i < kNMX; i++)
177 edepcell[i] = cellenergy[i];
180 delete [] cellenergy;
183 TMath::Sort((Int_t)kNMX,edepcell,iord1);// order the data
184 cutoff = fCutoff; // cutoff to discard cells
187 for(i = 0;i < kNMX; i++)
193 if(edepcell[i] > cutoff )
199 AliDebug(1,Form("Number of cells having energy >= %f are %d",cutoff,nmx1));
201 if (nmx1 == 0) nmx1 = 1;
203 AliDebug(1,Form("Number of cells in a SuperM = %d and Average = %f",
206 incr = CrClust(ave, cutoff, nmx1,iord1, edepcell );
207 RefClust(incr,edepcell);
208 Int_t nentries1 = fPMDclucont->GetEntries();
209 AliDebug(1,Form("Detector Plane = %d Serial Module No = %d Number of clusters = %d",idet, ismn, nentries1));
210 AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
212 for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
214 AliPMDcludata *pmdcludata =
215 (AliPMDcludata*)fPMDclucont->UncheckedAt(ient1);
216 Float_t cluXC = pmdcludata->GetClusX();
217 Float_t cluYC = pmdcludata->GetClusY();
218 Float_t cluADC = pmdcludata->GetClusADC();
219 Float_t cluCELLS = pmdcludata->GetClusCells();
220 Float_t cluSIGX = pmdcludata->GetClusSigmaX();
221 Float_t cluSIGY = pmdcludata->GetClusSigmaY();
223 Float_t cluY0 = ktwobysqrt3*cluYC;
224 Float_t cluX0 = cluXC - cluY0/2.;
227 // Cluster X centroid is back transformed
231 clusdata[0] = cluX0 - (24-1) + cluY0/2.;
233 else if ( ismn >= 12 && ismn <= 23)
235 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
239 clusdata[2] = cluADC;
240 clusdata[3] = cluCELLS;
241 clusdata[4] = cluSIGX;
242 clusdata[5] = cluSIGY;
245 // Cells associated with a cluster
248 for (Int_t ihit = 0; ihit < 15; ihit++)
252 celldataX[ihit] = pmdcludata->GetCellXY(ihit)%10000;
253 celldataY[ihit] = pmdcludata->GetCellXY(ihit)/10000;
255 else if (ismn >= 12 && ismn <= 23)
257 celldataX[ihit] = pmdcludata->GetCellXY(ihit)/10000;
258 celldataY[ihit] = pmdcludata->GetCellXY(ihit)%10000;
261 pmdcl = new AliPMDcluster(idet, ismn, clusdata, celldataX, celldataY);
265 fPMDclucont->Clear();
268 // ------------------------------------------------------------------------ //
269 Int_t AliPMDClusteringV1::CrClust(Double_t ave, Double_t cutoff, Int_t nmx1,
270 Int_t iord1[], Double_t edepcell[])
272 // Does crude clustering
273 // Finds out only the big patch by just searching the
276 Int_t i,j,k,id1,id2,icl, numcell, clust[2][5000];
277 Int_t jd1,jd2, icell, cellcount;
278 static Int_t neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
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++)
298 if(edepcell[j] <= cutoff)
300 fInfocl[0][id1][id2] = -1;
304 // ---------------------------------------------------------------
305 // crude clustering begins. Start with cell having largest adc
306 // count and loop over the cells in descending order of adc count
307 // ---------------------------------------------------------------
312 for(icell = 0; icell <= nmx1; icell++)
318 if(fInfocl[0][id1][id2] == 0 )
323 fInfocl[0][id1][id2] = 1;
324 fInfocl[1][id1][id2] = icl;
325 fInfcl[0][cellcount] = icl;
326 fInfcl[1][cellcount] = id1;
327 fInfcl[2][cellcount] = id2;
329 clust[0][numcell] = id1;
330 clust[1][numcell] = id2;
332 for(i = 1; i < 5000; i++)
336 // ---------------------------------------------------------------
337 // check for adc count in neib. cells. If ne 0 put it in this clust
338 // ---------------------------------------------------------------
339 for(i = 0; i < 6; i++)
341 jd1 = id1 + neibx[i];
342 jd2 = id2 + neiby[i];
343 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
344 fInfocl[0][jd1][jd2] == 0)
347 fInfocl[0][jd1][jd2] = 2;
348 fInfocl[1][jd1][jd2] = icl;
349 clust[0][numcell] = jd1;
350 clust[1][numcell] = jd2;
352 fInfcl[0][cellcount] = icl;
353 fInfcl[1][cellcount] = jd1;
354 fInfcl[2][cellcount] = jd2;
357 // ---------------------------------------------------------------
358 // check adc count for neighbour's neighbours recursively and
359 // if nonzero, add these to the cluster.
360 // ---------------------------------------------------------------
361 for(i = 1; i < 5000;i++)
367 for(j = 0; j < 6 ; j++)
369 jd1 = id1 + neibx[j];
370 jd2 = id2 + neiby[j];
371 if( (jd1 >= 0 && jd1 < kNDIMX) &&
372 (jd2 >= 0 && jd2 < kNDIMY) &&
373 fInfocl[0][jd1][jd2] == 0 )
375 fInfocl[0][jd1][jd2] = 2;
376 fInfocl[1][jd1][jd2] = icl;
378 clust[0][numcell] = jd1;
379 clust[1][numcell] = jd2;
381 fInfcl[0][cellcount] = icl;
382 fInfcl[1][cellcount] = jd1;
383 fInfcl[2][cellcount] = jd2;
392 // ------------------------------------------------------------------------ //
393 void AliPMDClusteringV1::RefClust(Int_t incr, Double_t edepcell[])
395 // Does the refining of clusters
396 // Takes the big patch and does gaussian fitting and
397 // finds out the more refined clusters
402 AliPMDcludata *pmdcludata = 0;
404 Int_t *cellCount = 0x0;
405 Int_t **cellXY = 0x0;
406 const Int_t kdim = 4500;
408 Int_t i, j, k, i1, i2, id, icl, itest,ihld, ig, nsupcl,clno;
410 Int_t ncl[kdim], iord[kdim], lev1[kdim], lev2[kdim];
413 Double_t x1, y1, z1, x2, y2, z2, dist,rr,sum;
414 Double_t x[kdim], y[kdim], z[kdim];
415 Double_t xc[kdim], yc[kdim], zc[kdim], cells[kdim], rc[kdim];
418 for(i = 0; i<kdim; i++)
422 if (i < 6) clusdata[i] = 0.;
423 if (i < 15) clxy[i] = 0;
426 // clno counts the final clusters
427 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
428 // x, y and z store (x,y) coordinates of and energy deposited in a cell
429 // xc, yc store (x,y) coordinates of the cluster center
430 // zc stores the energy deposited in a cluster
431 // rc is cluster radius
436 for(i = 0; i <= incr; i++)
438 if(fInfcl[0][i] != nsupcl)
444 AliWarning("RefClust: Too many superclusters!");
451 AliDebug(1,Form("Number of cells = %d Number of Superclusters = %d",
456 for(i = 0; i <= nsupcl; i++)
464 AliWarning("RefClust: Too many clusters! more than 5000");
471 Int_t i12 = i1 + i2*kNDIMX;
473 clusdata[0] = fCoord[0][i1][i2];
474 clusdata[1] = fCoord[1][i1][i2];
475 clusdata[2] = edepcell[i12];
480 clxy[0] = fCellTrNo[i1][i2]; //association
481 for(Int_t icltr = 1; icltr < 15; icltr++)
485 pmdcludata = new AliPMDcludata(clusdata,clxy);
486 fPMDclucont->Add(pmdcludata);
494 AliWarning("RefClust: Too many clusters! more than 5000");
500 Int_t i12 = i1 + i2*kNDIMX;
502 x1 = fCoord[0][i1][i2];
503 y1 = fCoord[1][i1][i2];
505 clxy[0] = fCellTrNo[i1][i2]; //asso
510 Int_t i22 = i1 + i2*kNDIMX;
511 x2 = fCoord[0][i1][i2];
512 y2 = fCoord[1][i1][i2];
514 clxy[1] = fCellTrNo[i1][i2]; //asso
515 for(Int_t icltr = 2; icltr < 15; icltr++)
520 clusdata[0] = (x1*z1+x2*z2)/(z1+z2);
521 clusdata[1] = (y1*z1+y2*z2)/(z1+z2);
526 pmdcludata = new AliPMDcludata(clusdata,clxy);
527 fPMDclucont->Add(pmdcludata);
533 // super-cluster of more than two cells - broken up into smaller
534 // clusters gaussian centers computed. (peaks separated by > 1 cell)
535 // Begin from cell having largest energy deposited This is first
539 Int_t i12 = i1 + i2*kNDIMX;
541 x[0] = fCoord[0][i1][i2];
542 y[0] = fCoord[1][i1][i2];
543 z[0] = edepcell[i12];
545 t[0] = fCellTrNo[i1][i2]; //asso
548 for(j = 1; j <= ncl[i]; j++)
553 Int_t i12 = i1 + i2*kNDIMX;
556 x[j] = fCoord[0][i1][i2];
557 y[j] = fCoord[1][i1][i2];
558 z[j] = edepcell[i12];
560 t[j] = fCellTrNo[i1][i2]; //asso
563 // arranging cells within supercluster in decreasing order
565 for(j = 1;j <= ncl[i]; j++)
569 for(i1 = 0; i1 < j; i1++)
571 if(itest == 0 && z[iord[i1]] < z[ihld])
574 for(i2 = j-1; i2 >= i1; i2--)
576 iord[i2+1] = iord[i2];
582 // compute the number of Gaussians and their centers ( first
584 // centers must be separated by cells having smaller ener. dep.
585 // neighbouring centers should be either strong or well-separated
590 for(j = 1; j <= ncl[i]; j++)
595 for(k = 0; k <= ig; k++)
599 rr = Distance(x1,y1,x2,y2);
600 if(rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)itest++;
601 if(rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)itest++;
602 if( rr >= 2.1)itest++;
612 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
614 // compute the number of cells belonging to each cluster.
615 // cell is shared between several clusters ( if they are equidistant
616 // from it ) in the ratio of cluster energy deposition
619 cellCount = new Int_t [ig+1];
620 cellXY = new Int_t *[jj];
621 for(Int_t ij = 0; ij < 15; ij++) cellXY[ij] = new Int_t [ig+1];
623 for(j = 0; j <= ig; j++)
631 for(j = 0; j <= ncl[i]; j++)
635 for(k = 0; k <= ig; k++)
637 dist = Distance(x[j], y[j], xc[k], yc[k]);
638 if(dist < TMath::Sqrt(3.) )
641 if (cellCount[k] < 15)
643 cellXY[cellCount[k]][k] = 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 < 15; icltr++)
708 cellXY[icltr][k] = -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];
736 for (Int_t ii=0; ii < 15; ii++)
738 clxy[ii] = cellXY[ii][j];
740 pmdcludata = new AliPMDcludata(clusdata,clxy);
741 fPMDclucont->Add(pmdcludata);
744 for(Int_t jj = 0; jj < 15; jj++)delete [] cellXY[jj];
749 // ------------------------------------------------------------------------ //
750 void AliPMDClusteringV1::GaussFit(Int_t ncell, Int_t nclust, Double_t &x,
751 Double_t &y ,Double_t &z, Double_t &xc,
752 Double_t &yc, Double_t &zc, Double_t &rc)
754 // Does gaussian fitting
757 const Int_t kdim = 4500;
758 Int_t i, j, i1, i2, novar, idd, jj;
759 Int_t neib[kdim][50];
761 Double_t sum, dx, dy, str, str1, aint, sum1, rr, dum;
762 Double_t x1, x2, y1, y2;
763 Double_t xx[kdim], yy[kdim], zz[kdim], xxc[kdim], yyc[kdim];
764 Double_t a[kdim], b[kdim], c[kdim], d[kdim], ha[kdim], hb[kdim];
765 Double_t hc[kdim], hd[kdim], zzc[kdim], rrc[kdim];
775 for(i = 0; i <= ncell; i++)
782 for(i=0; i<=nclust; i++)
790 for(i = 0; i <= nclust; i++)
792 zzc[i] = str/str1*zzc[i];
801 for(i = 0; i <= ncell; i++)
806 for(j = 0; j <= nclust; j++)
810 if(Distance(x1,y1,x2,y2) <= 3.)
819 for(i1 = 0; i1 <= ncell; i1++)
823 for(i2 = 1; i2 <= idd; i2++)
826 dx = xx[i1] - xxc[jj];
827 dy = yy[i1] - yyc[jj];
828 dum = rrc[j]*rrc[jj] + rr*rr;
829 aint += exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
831 sum += (aint - zz[i1])*(aint - zz[i1])/str;
835 for(i = 0; i <= nclust; i++)
837 a[i] = xxc[i] + 0.6*(rnd.Uniform() - 0.5);
838 b[i] = yyc[i] + 0.6*(rnd.Uniform() - 0.5);
839 c[i] = zzc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.2);
841 d[i] = rrc[i]*(1.+ ( rnd.Uniform() - 0.5)*0.1);
848 for(i = 0; i <= nclust; i++)
850 c[i] = c[i]*str/str1;
854 for(i1 = 0; i1 <= ncell; i1++)
858 for(i2 = 1; i2 <= idd; i2++)
863 dum = d[jj]*d[jj]+rr*rr;
864 aint += exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
866 sum1 += (aint - zz[i1])*(aint - zz[i1])/str;
871 for(i2 = 0; i2 <= nclust; i2++)
880 for(j = 0; j <= nclust; j++)
888 // ------------------------------------------------------------------------ //
889 Double_t AliPMDClusteringV1::Distance(Double_t x1, Double_t y1,
890 Double_t x2, Double_t y2)
892 return TMath::Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
894 // ------------------------------------------------------------------------ //
895 void AliPMDClusteringV1::SetEdepCut(Float_t decut)
899 // ------------------------------------------------------------------------ //