Compiler Warning Removed
[u/mrichter/AliRoot.git] / PMD / AliPMDClustering.cxx
CommitLineData
ed228cbc 1/***************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
deb0fc73 16//-----------------------------------------------------//
17// //
18// Source File : PMDClustering.cxx, Version 00 //
19// //
20// Date : September 26 2002 //
21// //
22// clustering code for alice pmd //
23// //
24//-----------------------------------------------------//
25
e1287360 26/* --------------------------------------------------------------------
27 Code developed by S. C. Phatak, Institute of Physics,
deb0fc73 28 Bhubaneswar 751 005 ( phatak@iopb.res.in ) Given the energy deposited
29 ( or ADC value ) in each cell of supermodule ( pmd or cpv ), the code
e1287360 30 builds up superclusters and breaks them into clusters. The input is
a918d77a 31 in array fEdepCell[kNDIMX][kNDIMY] and cluster information is in array
e1287360 32 fClusters[5][5000]. integer fClno gives total number of clusters in the
deb0fc73 33 supermodule.
34
a918d77a 35 fEdepCell, fClno and fClusters are the only global ( public ) variables.
e1287360 36 Others are local ( private ) to the code.
deb0fc73 37 At the moment, the data is read for whole detector ( all supermodules
38 and pmd as well as cpv. This will have to be modify later )
deb0fc73 39 LAST UPDATE : October 23, 2002
e1287360 40-----------------------------------------------------------------------*/
deb0fc73 41
a918d77a 42#include "Riostream.h"
deb0fc73 43#include <TNtuple.h>
44#include <TObjArray.h>
deb0fc73 45#include "AliPMDcluster.h"
46#include "AliPMDClustering.h"
47#include <stdio.h>
48
49ClassImp(AliPMDClustering)
50
a918d77a 51const Double_t AliPMDClustering::fgkSqroot3by2=0.8660254; // sqrt(3.)/2.
a854e5de 52
deb0fc73 53AliPMDClustering::AliPMDClustering()
54{
ed228cbc 55 fDebug = 0;
a918d77a 56 fCutoff = 0.0;
57 for(int i = 0; i < kNDIMX; i++)
deb0fc73 58 {
a918d77a 59 for(int j = 0; j < kNDIMY; j++)
deb0fc73 60 {
a918d77a 61 fCoord[0][i][j] = i+j/2.;
62 fCoord[1][i][j] = fgkSqroot3by2*j;
deb0fc73 63 }
64 }
65}
66AliPMDClustering::~AliPMDClustering()
67{
68
69}
70
a918d77a 71void AliPMDClustering::DoClust(Double_t celladc[48][96], TObjArray *pmdcont)
deb0fc73 72{
a918d77a 73 // main function to call other necessary functions to do clustering
74 //
deb0fc73 75 AliPMDcluster *pmdcl = 0;
e1287360 76 /*
77 int id and jd defined to read the input data.
78 It is assumed that for data we have 0 <= id <= 48
79 and 0 <= jd <=96
80 */
81 int i, i1, i2, j, nmx1, incr, id, jd;
deb0fc73 82 double cutoff, ave;
83 Float_t clusdata[5];
84
a918d77a 85 const float ktwobysqrt3 = 1.1547; // 2./sqrt(3.)
deb0fc73 86
e1287360 87 for (id = 0; id < kNDIMXr; id++)
deb0fc73 88 {
e1287360 89 for (jd = 0; jd < kNDIMYr; jd++)
deb0fc73 90 {
e1287360 91 j=jd;
92 i=id+(kNDIMYr/2-1)-(jd/2);
93 fEdepCell[i][j] = celladc[id][jd];
deb0fc73 94 }
95 }
a918d77a 96 Order(); // order the data
e1287360 97 cutoff = fCutoff; // cutoff used to discard cells having ener. dep.
98 ave=0.;
deb0fc73 99 nmx1=-1;
e1287360 100
a918d77a 101 for(j=0;j<kNMX; j++)
deb0fc73 102 {
a918d77a 103 i1 = fIord[0][j];
104 i2 = fIord[1][j];
105 if (fEdepCell[i1][i2] > 0.) {ave = ave + fEdepCell[i1][i2];}
106 if (fEdepCell[i1][i2] > cutoff ) nmx1 = nmx1 + 1;
deb0fc73 107 }
108 // nmx1 --- number of cells having ener dep >= cutoff
ed228cbc 109 if (fDebug == 1)
deb0fc73 110 {
111 cout << " nmx1 " << nmx1 << endl;
112 }
113 ave=ave/nmx1;
ed228cbc 114 if (fDebug == 1)
deb0fc73 115 {
a918d77a 116 cout <<"kNMX " << kNMX << " nmx1 " << nmx1<< " ave "<<ave<<
deb0fc73 117 " cutoff " << cutoff << endl;
118 }
e1287360 119
a918d77a 120 incr = CrClust(ave, cutoff, nmx1);
a918d77a 121 RefClust(incr);
ed228cbc 122 if (fDebug == 1)
deb0fc73 123 {
a918d77a 124 cout << "fClno " << fClno << endl;
deb0fc73 125 }
e1287360 126
a918d77a 127 for(i1=0; i1<fClno; i1++)
deb0fc73 128 {
a918d77a 129 Float_t cluXC = (Float_t) fClusters[0][i1];
130 Float_t cluYC = (Float_t) fClusters[1][i1];
131 Float_t cluADC = (Float_t) fClusters[2][i1];
132 Float_t cluCELLS = (Float_t) fClusters[3][i1];
133 Float_t cluRAD = (Float_t) fClusters[4][i1];
134 Float_t cluY0 = ktwobysqrt3*cluYC;
135 Float_t cluX0 = cluXC - cluY0/2.;
e1287360 136 //
137 // Cluster X centroid is back transformed
138 //
139 clusdata[0] = cluX0 - (48-1) + cluY0/2.;
a918d77a 140 clusdata[1] = cluY0;
141 clusdata[2] = cluADC;
142 clusdata[3] = cluCELLS;
143 clusdata[4] = cluRAD;
e1287360 144
deb0fc73 145 pmdcl = new AliPMDcluster(clusdata);
146 pmdcont->Add(pmdcl);
147 }
deb0fc73 148 delete pmdcl;
bdfa73a6 149
deb0fc73 150}
151
a918d77a 152void AliPMDClustering::Order()
deb0fc73 153{
a918d77a 154 // Sorting algorithm
155 // sorts the ADC values from higher to lower
156 //
157 double dd[kNMX], adum;
e1287360 158 // matrix fEdepCell converted into
deb0fc73 159 // one dimensional array dd. adum a place holder for double
a918d77a 160 int i, j, i1, i2, iord1[kNMX], itst, idum;
e1287360 161 // information of
deb0fc73 162 // ordering is stored in iord1, original array not ordered
163 //
164 // define arrays dd and iord1
a918d77a 165 for(i1=0; i1 < kNDIMX; i1++)
166 {
167 for(i2=0; i2 < kNDIMY; i2++)
168 {
169 i = i1 + i2*kNDIMX;
170 iord1[i] = i;
171 dd[i] = fEdepCell[i1][i2];
172 }
deb0fc73 173 }
e1287360 174 // sort and store sorting information in iord1
a918d77a 175 for(j=1; j < kNMX; j++)
176 {
177 itst = 0;
178 adum = dd[j];
179 idum = iord1[j];
180 for(i1=0; i1 < j ; i1++)
181 {
182 if(adum > dd[i1] && itst == 0)
183 {
184 itst = 1;
185 for(i2=j-1; i2 >= i1 ; i2=i2--)
186 {
e1287360 187 dd[i2+1] = dd[i2];
a918d77a 188 iord1[i2+1] = iord1[i2];
189 }
190 dd[i1] = adum;
191 iord1[i1] = idum;
192 }
deb0fc73 193 }
deb0fc73 194 }
a918d77a 195 // store the sorted information in fIord for later use
196 for(i=0; i<kNMX; i++)
197 {
198 j = iord1[i];
e1287360 199 i2 = j/kNDIMX;
200 i1 = j-i2*kNDIMX;
201 fIord[0][i]=i1;
a918d77a 202 fIord[1][i]=i2;
203 }
deb0fc73 204}
e1287360 205
a918d77a 206int AliPMDClustering::CrClust(double ave, double cutoff, int nmx1)
ed228cbc 207{
a918d77a 208 // Does crude clustering
209 // Finds out only the big patch by just searching the
210 // connected cells
211 //
ed228cbc 212 int i,j,k,id1,id2,icl, numcell, clust[2][5000];
213 int jd1,jd2, icell, cellcount;
214 static int neibx[6]={1,0,-1,-1,0,1}, neiby[6]={0,1,1,0,-1,-1};
e1287360 215 // neibx and neiby define ( incremental ) (i,j) for the neighbours of a
ed228cbc 216 // cell. There are six neighbours.
217 // cellcount --- total number of cells having nonzero ener dep
218 // numcell --- number of cells in a given supercluster
a918d77a 219 // ofstream ofl0("cells_loc",ios::out);
e1287360 220 // initialize fInfocl[2][kNDIMX][kNDIMY]
ed228cbc 221
222 if (fDebug == 1)
223 {
a918d77a 224 printf(" *** Inside CrClust ** kNMX = %d nmx1 = %d kNDIMX = %d kNDIMY = %d ave = %f cutoff = %f\n",
225 kNMX,nmx1,kNDIMX,kNDIMY,ave,cutoff);
ed228cbc 226 }
a918d77a 227 for (j=0; j < kNDIMX; j++){
228 for(k=0; k < kNDIMY; k++){
e1287360 229 fInfocl[0][j][k] = 0;
a918d77a 230 fInfocl[1][j][k] = 0;
ed228cbc 231 }
232 }
a918d77a 233 for(i=0; i < kNMX; i++){
234 fInfcl[0][i] = -1;
e1287360 235 id1=fIord[0][i];
a918d77a 236 id2=fIord[1][i];
237 if(fEdepCell[id1][id2] <= cutoff){fInfocl[0][id1][id2]=-1;}
ed228cbc 238 }
239 // ---------------------------------------------------------------
e1287360 240 // crude clustering begins. Start with cell having largest adc
ed228cbc 241 // count and loop over the cells in descending order of adc count
242 // ---------------------------------------------------------------
243 icl=-1;
244 cellcount=-1;
245 for(icell=0; icell <= nmx1; icell++){
e1287360 246 id1=fIord[0][icell];
247 id2=fIord[1][icell];
a918d77a 248 if(fInfocl[0][id1][id2] == 0 ){
ed228cbc 249 // ---------------------------------------------------------------
e1287360 250 // icl -- cluster #, numcell -- # of cells in it, clust -- stores
251 // coordinates of the cells in a cluster, fInfocl[0][i1][i2] is 1 for
252 // primary and 2 for secondary cells,
a918d77a 253 // fInfocl[1][i1][i2] stores cluster #
ed228cbc 254 // ---------------------------------------------------------------
e1287360 255 icl=icl+1;
256 numcell=0;
257 cellcount = cellcount + 1;
258 fInfocl[0][id1][id2]=1;
a918d77a 259 fInfocl[1][id1][id2]=icl;
e1287360 260 fInfcl[0][cellcount]=icl;
261 fInfcl[1][cellcount]=id1;
a918d77a 262 fInfcl[2][cellcount]=id2;
ed228cbc 263
ed228cbc 264 clust[0][numcell]=id1;
265 clust[1][numcell]=id2;
266 for(i=1; i<5000; i++)clust[0][i]=0;
267 // ---------------------------------------------------------------
268 // check for adc count in neib. cells. If ne 0 put it in this clust
269 // ---------------------------------------------------------------
270 for(i=0; i<6; i++){
e1287360 271 jd1=id1+neibx[i];
ed228cbc 272 jd2=id2+neiby[i];
e1287360 273 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
a918d77a 274 fInfocl[0][jd1][jd2] == 0){
ed228cbc 275 numcell=numcell+1;
e1287360 276 fInfocl[0][jd1][jd2]=2;
a918d77a 277 fInfocl[1][jd1][jd2]=icl;
ed228cbc 278 clust[0][numcell]=jd1;
279 clust[1][numcell]=jd2;
280 cellcount=cellcount+1;
e1287360 281 fInfcl[0][cellcount]=icl;
282 fInfcl[1][cellcount]=jd1;
a918d77a 283 fInfcl[2][cellcount]=jd2;
ed228cbc 284 }
285 }
286 // ---------------------------------------------------------------
e1287360 287 // check adc count for neighbour's neighbours recursively and
ed228cbc 288 // if nonzero, add these to the cluster.
289 // ---------------------------------------------------------------
290 for(i=1;i < 5000;i++){
291 if(clust[0][i] != 0){
e1287360 292 id1=clust[0][i];
ed228cbc 293 id2=clust[1][i];
294 for(j=0; j<6 ; j++){
e1287360 295 jd1=id1+neibx[j];
ed228cbc 296 jd2=id2+neiby[j];
e1287360 297 if( (jd1 >= 0 && jd1 < kNDIMX) && (jd2 >= 0 && jd2 < kNDIMY) &&
a918d77a 298 fInfocl[0][jd1][jd2] == 0 ){
e1287360 299 fInfocl[0][jd1][jd2] = 2;
a918d77a 300 fInfocl[1][jd1][jd2] = icl;
e1287360 301 numcell = numcell + 1;
a918d77a 302 clust[0][numcell] = jd1;
303 clust[1][numcell] = jd2;
304 cellcount = cellcount+1;
e1287360 305 fInfcl[0][cellcount] = icl;
306 fInfcl[1][cellcount] = jd1;
a918d77a 307 fInfcl[2][cellcount] = jd2;
ed228cbc 308 }
309 }
310 }
311 }
312 }
313 }
314 // for(icell=0; icell<=cellcount; icell++){
e1287360 315 // ofl0 << fInfcl[0][icell] << " " << fInfcl[1][icell] << " " <<
a918d77a 316 // fInfcl[2][icell] << endl;
ed228cbc 317 // }
318 return cellcount;
319}
320
a918d77a 321void AliPMDClustering::RefClust(int incr)
deb0fc73 322{
a918d77a 323 // Does the refining of clusters
324 // Takes the big patch and does gaussian fitting and
325 // finds out the more refined clusters
326 //
e1287360 327 int i, j, k, i1, i2, id, icl, ncl[4500], iord[4500], itest;
deb0fc73 328 int ihld;
329 int ig, nsupcl, lev1[20], lev2[20];
330 double x[4500], y[4500], z[4500], x1, y1, z1, x2, y2, z2, dist;
331 double xc[4500], yc[4500], zc[4500], cells[4500], sum, rc[4500], rr;
a918d77a 332 // fClno counts the final clusters
deb0fc73 333 // nsupcl = # of superclusters; ncl[i]= # of cells in supercluster i
334 // x, y and z store (x,y) coordinates of and energy deposited in a cell
335 // xc, yc store (x,y) coordinates of the cluster center
336 // zc stores the energy deposited in a cluster
337 // rc is cluster radius
338 // finally the cluster information is put in 2-dimensional array clusters
a918d77a 339 // ofstream ofl1("checking.5",ios::app);
340 fClno = -1;
341 nsupcl = -1;
deb0fc73 342 for(i=0; i<4500; i++){ncl[i]=-1;}
343 for(i=0; i<incr; i++){
a918d77a 344 if(fInfcl[0][i] != nsupcl){ nsupcl=nsupcl+1; }
deb0fc73 345 ncl[nsupcl]=ncl[nsupcl]+1;
346 }
ed228cbc 347 if (fDebug == 1)
deb0fc73 348 {
349 cout << " # of cells " <<incr+1 << " # of superclusters " << nsupcl+1
350 << endl;
351 }
352 id=-1;
353 icl=-1;
354 for(i=0; i<nsupcl; i++){
e1287360 355 if(ncl[i] == 0){
356 id=id+1;
deb0fc73 357 icl=icl+1;
358 // one cell super-clusters --> single cluster
359 // cluster center at the centyer of the cell
360 // cluster radius = half cell dimension
e1287360 361 fClno = fClno + 1;
362 i1 = fInfcl[1][id];
a918d77a 363 i2 = fInfcl[2][id];
e1287360 364 fClusters[0][fClno] = fCoord[0][i1][i2];
a918d77a 365 fClusters[1][fClno] = fCoord[1][i1][i2];
e1287360 366 fClusters[2][fClno] = fEdepCell[i1][i2];
367 fClusters[3][fClno] = 1.;
a918d77a 368 fClusters[4][fClno] = 0.5;
e1287360 369 //ofl1 << icl << " " << fCoord[0][i1][i2] << " " << fCoord[1][i1][i2] <<
370 //" " << fEdepCell[i1][i2] << " " << fClusters[3][fClno] <<endl;
deb0fc73 371 }else if(ncl[i] == 1){
372 // two cell super-cluster --> single cluster
373 // cluster center is at ener. dep.-weighted mean of two cells
374 // cluster radius == half cell dimension
e1287360 375 id = id + 1;
a918d77a 376 icl = icl+1;
e1287360 377 fClno = fClno+1;
378 i1 = fInfcl[1][id];
379 i2 = fInfcl[2][id];
a918d77a 380 x1 = fCoord[0][i1][i2];
e1287360 381 y1 = fCoord[1][i1][i2];
a918d77a 382 z1 = fEdepCell[i1][i2];
e1287360 383 id = id+1;
384 i1 = fInfcl[1][id];
a918d77a 385 i2 = fInfcl[2][id];
e1287360 386 x2 = fCoord[0][i1][i2];
387 y2 = fCoord[1][i1][i2];
a918d77a 388 z2 = fEdepCell[i1][i2];
e1287360 389 fClusters[0][fClno] = (x1*z1+x2*z2)/(z1+z2);
a918d77a 390 fClusters[1][fClno] = (y1*z1+y2*z2)/(z1+z2);
e1287360 391 fClusters[2][fClno] = z1+z2;
392 fClusters[3][fClno] = 2.;
a918d77a 393 fClusters[4][fClno] = 0.5;
394 //ofl1 << icl << " " << fClusters[0][fClno] << " " << fClusters[1][fClno]
e1287360 395 // << " " << fClusters[2][fClno] << " " <<fClusters[3][fClno] <<endl;
396 }
397 else{
398 id = id + 1;
a918d77a 399 iord[0] = 0;
e1287360 400 // super-cluster of more than two cells - broken up into smaller
401 // clusters gaussian centers computed. (peaks separated by > 1 cell)
deb0fc73 402 // Begin from cell having largest energy deposited This is first
403 // cluster center
e1287360 404 i1 = fInfcl[1][id];
a918d77a 405 i2 = fInfcl[2][id];
e1287360 406 x[0] = fCoord[0][i1][i2];
407 y[0] = fCoord[1][i1][i2];
a918d77a 408 z[0] = fEdepCell[i1][i2];
409 iord[0] = 0;
deb0fc73 410 for(j=1;j<=ncl[i];j++){
ed228cbc 411
a918d77a 412 id = id + 1;
e1287360 413 i1 = fInfcl[1][id];
a918d77a 414 i2 = fInfcl[2][id];
415 iord[j] = j;
e1287360 416 x[j] = fCoord[0][i1][i2];
417 y[j] = fCoord[1][i1][i2];
a918d77a 418 z[j] = fEdepCell[i1][i2];
deb0fc73 419 }
e1287360 420 // arranging cells within supercluster in decreasing order
deb0fc73 421 for(j=1;j<=ncl[i];j++){
a918d77a 422 itest=0;
423 ihld=iord[j];
deb0fc73 424 for(i1=0;i1<j;i1++){
425 if(itest == 0 && z[iord[i1]] < z[ihld]){
426 itest=1;
427 for(i2=j-1;i2>=i1;i2--){
428 iord[i2+1]=iord[i2];
429 }
430 iord[i1]=ihld;
431 }
432 }
433 }
ed228cbc 434
e1287360 435 // compute the number of Gaussians and their centers ( first
436 // guess )
deb0fc73 437 // centers must be separated by cells having smaller ener. dep.
438 // neighbouring centers should be either strong or well-separated
439 ig=0;
e1287360 440 xc[ig]=x[iord[0]];
441 yc[ig]=y[iord[0]];
deb0fc73 442 zc[ig]=z[iord[0]];
443 for(j=1;j<=ncl[i];j++){
e1287360 444 itest=-1;
445 x1=x[iord[j]];
deb0fc73 446 y1=y[iord[j]];
447 for(k=0;k<=ig;k++){
e1287360 448 x2=xc[k]; y2=yc[k];
a918d77a 449 rr=Distance(x1,y1,x2,y2);
deb0fc73 450 if( rr >= 1.1 && rr < 1.8 && z[iord[j]] > zc[k]/4.)
451 itest=itest+1;
452 if( rr >= 1.8 && rr < 2.1 && z[iord[j]] > zc[k]/10.)
453 itest=itest+1;
454 if( rr >= 2.1)itest=itest+1;
e1287360 455 }
deb0fc73 456 if(itest == ig){
e1287360 457 ig=ig+1;
458 xc[ig]=x1;
459 yc[ig]=y1;
deb0fc73 460 zc[ig]=z[iord[j]];
461 }
462 }
463 // for(j=0; j<=ig; j++){
464 //ofl1 << icl+j+1 << " " << xc[j] << " " <<yc[j] <<" "<<zc[j]<<endl;
465 //}
a918d77a 466 // GaussFit to adjust cluster parameters to minimize
467 GaussFit(ncl[i], ig, x[0], y[0] ,z[0], xc[0], yc[0], zc[0], rc[0]);
deb0fc73 468 icl=icl+ig+1;
469 // compute the number of cells belonging to each cluster.
e1287360 470 // cell is shared between several clusters ( if they are equidistant
deb0fc73 471 // from it ) in the ratio of cluster energy deposition
472 for(j=0; j<=ig; j++){
473 cells[j]=0.;
474 }
475 if(ig > 0){
476 for(j=0; j<=ncl[i]; j++){
e1287360 477 lev1[0]=0;
deb0fc73 478 lev2[0]=0;
479 for(k=0; k<=ig; k++){
a918d77a 480 dist=Distance(x[j], y[j], xc[k], yc[k]);
deb0fc73 481 if(dist < sqrt(3.) ){
e1287360 482 lev1[0]++;
483 i1=lev1[0];
deb0fc73 484 lev1[i1]=k;
485 }else{
486 if(dist < 2.1){
e1287360 487 lev2[0]++;
488 i1=lev2[0];
deb0fc73 489 lev2[i1]=k;
490 }
491 }
492 }
493 if(lev1[0] != 0){
494 if(lev1[0] == 1){cells[lev1[1]]=cells[lev1[1]]+1.;}
495 else{
496 sum=0.;
497 for(k=1; k<=lev1[0]; k++){
498 sum=sum+zc[lev1[k]];
499 }
500 for(k=1; k<=lev1[0]; k++){
501 cells[lev1[k]]=cells[lev1[k]]+zc[lev1[k]]/sum;
502 }
503 }
504 }else{
505 if(lev2[0] == 0){cells[lev2[1]]=cells[lev2[1]]+1.;}
506 else{
507 sum=0.;
508 for(k=1; k<=lev2[0]; k++){
509 sum=sum+zc[lev2[k]];
510 }
511 for(k=1; k<=lev2[0]; k++){
512 cells[lev2[k]]=cells[lev2[k]]+zc[lev2[k]]/sum;
513 }
514 }
515 }
516 }
517 }
518 for(j=0; j<=ig; j++){
e1287360 519 fClno = fClno + 1;
520 fClusters[0][fClno] = xc[j];
521 fClusters[1][fClno] = yc[j];
a918d77a 522 fClusters[2][fClno] = zc[j];
523 fClusters[4][fClno] = rc[j];
deb0fc73 524 if(ig == 0){
a918d77a 525 fClusters[3][fClno] = ncl[i];
deb0fc73 526 }else{
a918d77a 527 fClusters[3][fClno] = cells[j];
deb0fc73 528 }
529 }
530 }
531 }
ed228cbc 532}
deb0fc73 533
a918d77a 534void AliPMDClustering::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)
deb0fc73 535{
a918d77a 536 // Does gaussian fitting
537 //
deb0fc73 538 int i, j, i1, i2, jmax, novar, idd, jj;
e1287360 539 double xx[4500], yy[4500], zz[4500], xxc[4500], yyc[4500];
deb0fc73 540 double a[4500], b[4500], c[4500], d[4500], ha[4500], hb[4500];
541 double hc[4500], hd[4500], zzc[4500], rrc[4500];
542 int neib[4500][50];
543 double sum, dx, dy, str, str1, aint, sum1, rr, dum;
544 double x1, x2, y1, y2;
e1287360 545 str = 0.;
546 str1 = 0.;
547 rr = 0.3;
a918d77a 548 novar = 0;
deb0fc73 549 j = 0; // Just put not to see the compiler warning, BKN
550
a918d77a 551 for(i=0; i<=ncell; i++)
552 {
e1287360 553 xx[i] = *(&x+i);
554 yy[i] = *(&y+i);
a918d77a 555 zz[i] = *(&z+i);
556 str = str + zz[i];
557 }
558 for(i=0; i<=nclust; i++)
559 {
e1287360 560 xxc[i] = *(&xc+i);
561 yyc[i] = *(&yc+i);
562 zzc[i] = *(&zc+i);
563 str1 = str1 + zzc[i];
a918d77a 564 rrc[i] = 0.5;
565 }
566 for(i=0; i<=nclust; i++)
567 {
568 zzc[i] = str/str1*zzc[i];
e1287360 569 ha[i] = xxc[i];
570 hb[i] = yyc[i];
571 hc[i] = zzc[i];
a918d77a 572 hd[i] = rrc[i];
e1287360 573 x1 = xxc[i];
a918d77a 574 y1 = yyc[i];
575 }
deb0fc73 576 for(i=0; i<=ncell; i++){
e1287360 577 idd=0;
578 x1=xx[i];
deb0fc73 579 y1=yy[i];
580 for(j=0; j<=nclust; j++){
e1287360 581 x2=xxc[j];
deb0fc73 582 y2=yyc[j];
a918d77a 583 if(Distance(x1,y1,x2,y2) <= 3.){ idd=idd+1; neib[i][idd]=j; }
deb0fc73 584 }
deb0fc73 585 neib[i][0]=idd;
586 }
587 sum=0.;
588 for(i1=0; i1<=ncell; i1++){
e1287360 589 aint=0.;
deb0fc73 590 idd=neib[i1][0];
591 for(i2=1; i2<=idd; i2++){
592 jj=neib[i1][i2];
e1287360 593 dx=xx[i1]-xxc[jj];
594 dy=yy[i1]-yyc[jj];
deb0fc73 595 dum=rrc[j]*rrc[jj]+rr*rr;
596 aint=aint+exp(-(dx*dx+dy*dy)/dum)*zzc[idd]*rr*rr/dum;
597 }
598 sum=sum+(aint-zz[i1])*(aint-zz[i1])/str;
599 }
e1287360 600 jmax=nclust*1000;
deb0fc73 601 if(nclust > 20)jmax=20000;
602 for(j=0; j<jmax; j++){
603 str1=0.;
604 for(i=0; i<=nclust; i++){
e1287360 605 a[i]=xxc[i]+0.6*(Ranmar()-0.5);
a918d77a 606 b[i]=yyc[i]+0.6*(Ranmar()-0.5);
e1287360 607 c[i]=zzc[i]*(1.+(Ranmar()-0.5)*0.2);
deb0fc73 608 str1=str1+zzc[i];
a918d77a 609 d[i]=rrc[i]*(1.+(Ranmar()-0.5)*0.1);
deb0fc73 610 if(d[i] < 0.25)d[i]=0.25;
611 }
612 for(i=0; i<=nclust; i++){ c[i]=c[i]*str/str1; }
613 sum1=0.;
614 for(i1=0; i1<=ncell; i1++){
e1287360 615 aint=0.;
deb0fc73 616 idd=neib[i1][0];
617 for(i2=1; i2<=idd; i2++){
618 jj=neib[i1][i2];
e1287360 619 dx=xx[i1]-a[jj];
620 dy=yy[i1]-b[jj];
deb0fc73 621 dum=d[jj]*d[jj]+rr*rr;
622 aint=aint+exp(-(dx*dx+dy*dy)/dum)*c[i2]*rr*rr/dum;
623 }
624 sum1=sum1+(aint-zz[i1])*(aint-zz[i1])/str;
625 }
626
627 if(sum1 < sum){
628 for(i2=0; i2<=nclust; i2++){
e1287360 629 xxc[i2]=a[i2];
630 yyc[i2]=b[i2];
631 zzc[i2]=c[i2];
632 rrc[i2]=d[i2];
deb0fc73 633 sum=sum1;
deb0fc73 634 }
635 }
636 }
637 for(j=0; j<=nclust; j++){
e1287360 638 *(&xc+j)=xxc[j];
639 *(&yc+j)=yyc[j];
640 *(&zc+j)=zzc[j];
deb0fc73 641 *(&rc+j)=rrc[j];
642 }
643}
644
a918d77a 645double AliPMDClustering::Distance(double x1, double y1, double x2, double y2)
deb0fc73 646{
647 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
648}
649
a918d77a 650double AliPMDClustering::Ranmar() const
deb0fc73 651{
a918d77a 652 // Universal random number generator proposed by Marsaglia and Zaman
653 // in report FSU-SCRI-87-50
deb0fc73 654
655 // clock_t start;
656 int ii, jj;
657 static int i=96, j=32, itest=0, i1, i2, i3, i4, i5;
658 static double u[97], c, cd, cm, s, t;
659 static double uni;
660 int count1,count2,idum;
661 /* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
662 if (itest == 0) {
663 //*******************************************************
664 // following three lines if the seed to be provided by computer
665 // start = time(NULL);
666 // ii=start;
667 // jj=start;
668 //*******************************************************
669 //following two lines for fixed seed ( during testing only. Else
670 //use preceeing three lines
671 ii=8263;
672 jj=5726;
673 if(ii > 31328 ) ii = ii - ( ii / 31328 ) * 31328;
674 if(jj > 30081 ) jj = jj - ( jj / 30081 ) * 30081;
675 itest=itest+1;
e1287360 676 if((( ii > 0 ) && ( ii <= 31328 )) && (( jj > 0 ) &&
deb0fc73 677 ( jj <= 30081 ))){
e1287360 678 i1=ii/177+2; i2=ii-(i1-2)*177+2; i3=jj/169+1; i4=jj-(i3-1)*169;
deb0fc73 679 i4 = jj - (i3-1)*169;
680 count1=0;
681 while ( count1 < 97 ){
682 s=0.;
683 t=0.5;
684 count2=0;
685 while( count2 < 24 ){
686 idum=i1*i2/179;
687 idum=( i1*i2 - (i1*i2/179)*179 ) * i3;
688 i5=idum-(idum/179)*179;
689 i1=i2; i2=i3; i3=i5; idum=53*i4+1; i4=idum-(idum/169)*169;
690 if( i4*i5-((i4*i5)/64)*64 >= 32 ) s=s+t;
691 t=0.5*t;
692 count2=count2+1;
693 }
694 u[count1] = s;
695 count1 = count1 +1;
696 }
e1287360 697 c = 362436./16777216.; cd = 7654321./16777216.;
deb0fc73 698 cm = 16777213./16777216.;
699 }
700 else{
701 cout << " wrong initialization " << endl;
702 }
703 }
704 else{
a918d77a 705 uni = u[i] - u[j];
706 if( uni < 0.) uni = uni + 1;
e1287360 707 u[i] = uni;
deb0fc73 708 i = i -1;
a918d77a 709 if( i < 0 ) i = 96;
710 j = j - 1;
711 if ( j < 0 ) j = 96;
712 c = c - cd;
713 if( c < 0. ) c = c+cm;
714 uni = uni-c ;
715 if( uni < 0. )uni = uni+1.;
deb0fc73 716 }
717 return uni;
e1287360 718}
deb0fc73 719
ed228cbc 720void AliPMDClustering::SetEdepCut(Float_t decut)
721{
722 fCutoff = decut;
723}
724void AliPMDClustering::SetDebug(Int_t idebug)
deb0fc73 725{
ed228cbc 726 fDebug = idebug;
deb0fc73 727}