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