]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSClusterFinderSDD.cxx
Data members of AliITSdigit classes defined as protected (They were public)
[u/mrichter/AliRoot.git] / ITS / AliITSClusterFinderSDD.cxx
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 /*
16   $Id$
17   $Log$
18   Revision 1.33  2003/07/21 14:20:51  masera
19   Fix to track labes in SDD Rec-points
20
21   Revision 1.31.2.1  2003/07/16 13:18:04  masera
22   Proper fix to track labels associated to SDD rec-points
23
24   Revision 1.31  2003/05/19 14:44:41  masera
25   Fix to track labels associated to SDD rec-points
26
27   Revision 1.30  2003/03/03 16:34:35  masera
28   Corrections to comply with coding conventions
29
30   Revision 1.29  2002/10/25 18:54:22  barbera
31   Various improvements and updates from B.S.Nilsen and T. Virgili
32
33   Revision 1.28  2002/10/22 14:45:29  alibrary
34   Introducing Riostream.h
35
36   Revision 1.27  2002/10/14 14:57:00  hristov
37   Merging the VirtualMC branch to the main development branch (HEAD)
38
39   Revision 1.23.4.2  2002/10/14 13:14:07  hristov
40   Updating VirtualMC to v3-09-02
41
42   Revision 1.26  2002/09/09 17:23:28  nilsen
43   Minor changes in support of changes to AliITSdigitS?D class'.
44
45   Revision 1.25  2002/05/10 22:29:40  nilsen
46   Change my Massimo Masera in the default constructor to bring things into
47   compliance.
48
49   Revision 1.24  2002/04/24 22:02:31  nilsen
50   New SDigits and Digits routines, and related changes,  (including new
51   noise values).
52
53  */
54 // 
55 //  Cluster finder 
56 //  for Silicon
57 //  Drift Detector
58 //
59 #include <Riostream.h>
60
61 #include <TMath.h>
62 #include <math.h>
63
64 #include "AliITSClusterFinderSDD.h"
65 #include "AliITSMapA1.h"
66 #include "AliITS.h"
67 #include "AliITSdigit.h"
68 #include "AliITSRawCluster.h"
69 #include "AliITSRecPoint.h"
70 #include "AliITSsegmentation.h"
71 #include "AliITSresponseSDD.h"
72 #include "AliRun.h"
73
74 ClassImp(AliITSClusterFinderSDD)
75
76 //______________________________________________________________________
77 AliITSClusterFinderSDD::AliITSClusterFinderSDD(AliITSsegmentation *seg,
78                                                AliITSresponse *response,
79                                                TClonesArray *digits,
80                                                TClonesArray *recp){
81     // standard constructor
82
83     fSegmentation = seg;
84     fResponse     = response;
85     fDigits       = digits;
86     fClusters     = recp;
87     fNclusters    = fClusters->GetEntriesFast();
88     SetCutAmplitude();
89     SetDAnode();
90     SetDTime();
91     SetMinPeak((Int_t)(((AliITSresponseSDD*)fResponse)->GetNoiseAfterElectronics()*5));
92     //    SetMinPeak();
93     SetMinNCells();
94     SetMaxNCells();
95     SetTimeCorr();
96     SetMinCharge();
97     fMap = new AliITSMapA1(fSegmentation,fDigits,fCutAmplitude);
98 }
99 //______________________________________________________________________
100 AliITSClusterFinderSDD::AliITSClusterFinderSDD(){
101     // default constructor
102
103     fSegmentation = 0;
104     fResponse     = 0;
105     fDigits       = 0;
106     fClusters     = 0;
107     fNclusters    = 0;
108     fMap          = 0;
109     fCutAmplitude = 0;
110     fDAnode = 0;
111     fDTime = 0;
112     fMinPeak = 0;
113     fMinNCells = 0;
114     fMaxNCells = 0;
115     fTimeCorr = 0;
116     fMinCharge = 0;
117     /*
118     SetDAnode();
119     SetDTime();
120     SetMinPeak((Int_t)(((AliITSresponseSDD*)fResponse)->GetNoiseAfterElectronics()*5));
121     SetMinNCells();
122     SetMaxNCells();
123     SetTimeCorr();
124     SetMinCharge();
125     */
126 }
127 //____________________________________________________________________________
128 AliITSClusterFinderSDD::~AliITSClusterFinderSDD(){
129     // destructor
130
131     if(fMap) delete fMap;
132 }
133 //______________________________________________________________________
134 void AliITSClusterFinderSDD::SetCutAmplitude(Float_t nsigma){
135     // set the signal threshold for cluster finder
136     Float_t baseline,noise,noiseAfterEl;
137
138     fResponse->GetNoiseParam(noise,baseline);
139     noiseAfterEl = ((AliITSresponseSDD*)fResponse)->GetNoiseAfterElectronics();
140     fCutAmplitude = (Int_t)((baseline + nsigma*noiseAfterEl));
141 }
142 //______________________________________________________________________
143 void AliITSClusterFinderSDD::Find1DClusters(){
144     // find 1D clusters
145     static AliITS *iTS = (AliITS*)gAlice->GetModule("ITS");
146   
147     // retrieve the parameters 
148     Int_t fNofMaps       = fSegmentation->Npz();
149     Int_t fMaxNofSamples = fSegmentation->Npx();
150     Int_t fNofAnodes     = fNofMaps/2;
151     Int_t dummy          = 0;
152     Float_t fTimeStep    = fSegmentation->Dpx(dummy);
153     Float_t fSddLength   = fSegmentation->Dx();
154     Float_t fDriftSpeed  = fResponse->DriftSpeed();  
155     Float_t anodePitch   = fSegmentation->Dpz(dummy);
156
157     // map the signal
158     fMap->ClearMap();
159     fMap->SetThreshold(fCutAmplitude);
160     fMap->FillMap();
161   
162     Float_t noise;
163     Float_t baseline;
164     fResponse->GetNoiseParam(noise,baseline);
165   
166     Int_t nofFoundClusters = 0;
167     Int_t i;
168     Float_t **dfadc = new Float_t*[fNofAnodes];
169     for(i=0;i<fNofAnodes;i++) dfadc[i] = new Float_t[fMaxNofSamples];
170     Float_t fadc  = 0.;
171     Float_t fadc1 = 0.;
172     Float_t fadc2 = 0.;
173     Int_t j,k,idx,l,m;
174     for(j=0;j<2;j++) {
175         for(k=0;k<fNofAnodes;k++) {
176             idx = j*fNofAnodes+k;
177             // signal (fadc) & derivative (dfadc)
178             dfadc[k][255]=0.;
179             for(l=0; l<fMaxNofSamples; l++) {
180                 fadc2=(Float_t)fMap->GetSignal(idx,l);
181                 if(l>0) fadc1=(Float_t)fMap->GetSignal(idx,l-1);
182                 if(l>0) dfadc[k][l-1] = fadc2-fadc1;
183             } // samples
184         } // anodes
185
186         for(k=0;k<fNofAnodes;k++) {
187         //cout << "Anode: " << k+1 << ", Wing: " << j+1 << endl;
188             idx = j*fNofAnodes+k;
189             Int_t imax  = 0;
190             Int_t imaxd = 0;
191             Int_t it    = 0;
192             while(it <= fMaxNofSamples-3) {
193                 imax  = it;
194                 imaxd = it;
195                 // maximum of signal          
196                 Float_t fadcmax  = 0.;
197                 Float_t dfadcmax = 0.;
198                 Int_t lthrmina   = 1;
199                 Int_t lthrmint   = 3;
200                 Int_t lthra      = 1;
201                 Int_t lthrt      = 0;
202                 for(m=0;m<20;m++) {
203                     Int_t id = it+m;
204                     if(id>=fMaxNofSamples) break;
205                     fadc=(float)fMap->GetSignal(idx,id);
206                     if(fadc > fadcmax) { fadcmax = fadc; imax = id;}
207                     if(fadc > (float)fCutAmplitude) { 
208                         lthrt++; 
209                     } // end if
210                     if(dfadc[k][id] > dfadcmax) {
211                         dfadcmax = dfadc[k][id];
212                         imaxd    = id;
213                     } // end if
214                 } // end for m
215                 it = imaxd;
216                 if(fMap->TestHit(idx,imax) == kEmpty) {it++; continue;}
217                 // cluster charge
218                 Int_t tstart = it-2;
219                 if(tstart < 0) tstart = 0;
220                 Bool_t ilcl = 0;
221                 if(lthrt >= lthrmint && lthra >= lthrmina) ilcl = 1;
222                 if(ilcl) {
223                     nofFoundClusters++;
224                     Int_t tstop      = tstart;
225                     Float_t dfadcmin = 10000.;
226                     Int_t ij;
227                     for(ij=0; ij<20; ij++) {
228                         if(tstart+ij > 255) { tstop = 255; break; }
229                         fadc=(float)fMap->GetSignal(idx,tstart+ij);
230                         if((dfadc[k][tstart+ij] < dfadcmin) && 
231                            (fadc > fCutAmplitude)) {
232                             tstop = tstart+ij+5;
233                             if(tstop > 255) tstop = 255;
234                             dfadcmin = dfadc[k][it+ij];
235                         } // end if
236                     } // end for ij
237
238                     Float_t clusterCharge = 0.;
239                     Float_t clusterAnode  = k+0.5;
240                     Float_t clusterTime   = 0.;
241                     Int_t   clusterMult   = 0;
242                     Float_t clusterPeakAmplitude = 0.;
243                     Int_t its,peakpos     = -1;
244                     Float_t n, baseline;
245                     fResponse->GetNoiseParam(n,baseline);
246                     for(its=tstart; its<=tstop; its++) {
247                         fadc=(float)fMap->GetSignal(idx,its);
248                         if(fadc>baseline) fadc -= baseline;
249                         else fadc = 0.;
250                         clusterCharge += fadc;
251                         // as a matter of fact we should take the peak
252                         // pos before FFT
253                         // to get the list of tracks !!!
254                         if(fadc > clusterPeakAmplitude) {
255                             clusterPeakAmplitude = fadc;
256                             //peakpos=fMap->GetHitIndex(idx,its);
257                             Int_t shift = (int)(fTimeCorr/fTimeStep);
258                             if(its>shift && its<(fMaxNofSamples-shift))
259                                 peakpos  = fMap->GetHitIndex(idx,its+shift);
260                             else peakpos = fMap->GetHitIndex(idx,its);
261                             if(peakpos<0) peakpos =fMap->GetHitIndex(idx,its);
262                         } // end if
263                         clusterTime += fadc*its;
264                         if(fadc > 0) clusterMult++;
265                         if(its == tstop) {
266                             clusterTime /= (clusterCharge/fTimeStep);   // ns
267                             if(clusterTime>fTimeCorr) clusterTime -=fTimeCorr;
268                             //ns
269                         } // end if
270                     } // end for its
271
272                     Float_t clusteranodePath = (clusterAnode - fNofAnodes/2)*
273                                                anodePitch;
274                     Float_t clusterDriftPath = clusterTime*fDriftSpeed;
275                     clusterDriftPath = fSddLength-clusterDriftPath;
276                     if(clusterCharge <= 0.) break;
277                     AliITSRawClusterSDD clust(j+1,//i
278                                               clusterAnode,clusterTime,//ff
279                                               clusterCharge, //f
280                                               clusterPeakAmplitude, //f
281                                               peakpos, //i
282                                               0.,0.,clusterDriftPath,//fff
283                                               clusteranodePath, //f
284                                               clusterMult, //i
285                                               0,0,0,0,0,0,0);//7*i
286                     iTS->AddCluster(1,&clust);
287                     it = tstop;
288                 } // ilcl
289                 it++;
290             } // while (samples)
291         } // anodes
292     } // detectors (2)
293
294     for(i=0;i<fNofAnodes;i++) delete[] dfadc[i];
295     delete [] dfadc;
296
297     return;
298 }
299
300
301
302 //______________________________________________________________________
303 void AliITSClusterFinderSDD::Find1DClustersE(){
304     // find 1D clusters
305     static AliITS *iTS=(AliITS*)gAlice->GetModule("ITS");
306     // retrieve the parameters 
307     Int_t fNofMaps = fSegmentation->Npz();
308     Int_t fMaxNofSamples = fSegmentation->Npx();
309     Int_t fNofAnodes = fNofMaps/2;
310     Int_t dummy=0;
311     Float_t fTimeStep = fSegmentation->Dpx( dummy );
312     Float_t fSddLength = fSegmentation->Dx();
313     Float_t fDriftSpeed = fResponse->DriftSpeed();
314     Float_t anodePitch = fSegmentation->Dpz( dummy );
315     Float_t n, baseline;
316     fResponse->GetNoiseParam( n, baseline );
317     // map the signal
318     fMap->ClearMap();
319     fMap->SetThreshold( fCutAmplitude );
320     fMap->FillMap();
321     
322     Int_t nClu = 0;
323     //        cout << "Search  cluster... "<< endl;
324     for( Int_t j=0; j<2; j++ ){
325         for( Int_t k=0; k<fNofAnodes; k++ ){
326             Int_t idx = j*fNofAnodes+k;
327             Bool_t on = kFALSE;
328             Int_t start = 0;
329             Int_t nTsteps = 0;
330             Float_t fmax = 0.;
331             Int_t lmax = 0;
332             Float_t charge = 0.;
333             Float_t time = 0.;
334             Float_t anode = k+0.5;
335             Int_t peakpos = -1;
336             for( Int_t l=0; l<fMaxNofSamples; l++ ){
337                 Float_t fadc = (Float_t)fMap->GetSignal( idx, l );
338                 if( fadc > 0.0 ){
339                     if( on == kFALSE && l<fMaxNofSamples-4){
340                         // star RawCluster (reset var.)
341                         Float_t fadc1 = (Float_t)fMap->GetSignal( idx, l+1 );
342                         if( fadc1 < fadc ) continue;
343                         start = l;
344                         fmax = 0.;
345                         lmax = 0;
346                         time = 0.;
347                         charge = 0.; 
348                         on = kTRUE; 
349                         nTsteps = 0;
350                     } // end if on...
351                     nTsteps++ ;
352                     if( fadc > baseline ) fadc -= baseline;
353                     else fadc=0.;
354                     charge += fadc;
355                     time += fadc*l;
356                     if( fadc > fmax ){ 
357                         fmax = fadc; 
358                         lmax = l; 
359                         Int_t shift = (Int_t)(fTimeCorr/fTimeStep + 0.5);
360                         if( l > shift && l < (fMaxNofSamples-shift) )  
361                             peakpos = fMap->GetHitIndex( idx, l+shift );
362                         else
363                             peakpos = fMap->GetHitIndex( idx, l );
364                         if( peakpos < 0) peakpos = fMap->GetHitIndex( idx, l );
365                     } // end if fadc
366                 }else{ // end fadc>0
367                     if( on == kTRUE ){        
368                         if( nTsteps > 2 ){
369                             //  min # of timesteps for a RawCluster
370                             // Found a RawCluster...
371                             Int_t stop = l-1;
372                             time /= (charge/fTimeStep);   // ns
373                                 // time = lmax*fTimeStep;   // ns
374                             if( time > fTimeCorr ) time -= fTimeCorr;   // ns
375                             Float_t anodePath = (anode - fNofAnodes/2)*anodePitch;
376                             Float_t driftPath = time*fDriftSpeed;
377                             driftPath = fSddLength-driftPath;
378                             AliITSRawClusterSDD clust(j+1,anode,time,charge,
379                                                       fmax, peakpos,0.,0.,
380                                                       driftPath,anodePath,
381                                                       nTsteps,start,stop,
382                                                       start, stop, 1, k, k );
383                             iTS->AddCluster( 1, &clust );
384                             //        clust.PrintInfo();
385                             nClu++;
386                         } // end if nTsteps
387                         on = kFALSE;
388                     } // end if on==kTRUE
389                 } // end if fadc>0
390             } // samples
391         } // anodes
392     } // wings
393     //        cout << "# Rawclusters " << nClu << endl;         
394     return; 
395 }
396 //_______________________________________________________________________
397 Int_t AliITSClusterFinderSDD::SearchPeak(Float_t *spect,Int_t xdim,Int_t zdim,
398                                          Int_t *peakX, Int_t *peakZ, 
399                                          Float_t *peakAmp, Float_t minpeak ){
400     // search peaks on a 2D cluster
401     Int_t npeak = 0;    // # peaks
402     Int_t i,j;
403     // search peaks
404     for( Int_t z=1; z<zdim-1; z++ ){
405         for( Int_t x=1; x<xdim-2; x++ ){
406             Float_t sxz = spect[x*zdim+z];
407             Float_t sxz1 = spect[(x+1)*zdim+z];
408             Float_t sxz2 = spect[(x-1)*zdim+z];
409             // search a local max. in s[x,z]
410             if( sxz < minpeak || sxz1 <= 0 || sxz2 <= 0 ) continue;
411             if( sxz >= spect[(x+1)*zdim+z  ] && sxz >= spect[(x-1)*zdim+z  ] &&
412                 sxz >= spect[x*zdim    +z+1] && sxz >= spect[x*zdim    +z-1] &&
413                 sxz >= spect[(x+1)*zdim+z+1] && sxz >= spect[(x+1)*zdim+z-1] &&
414                 sxz >= spect[(x-1)*zdim+z+1] && sxz >= spect[(x-1)*zdim+z-1] ){
415                 // peak found
416                 peakX[npeak] = x;
417                 peakZ[npeak] = z;
418                 peakAmp[npeak] = sxz;
419                 npeak++;
420             } // end if ....
421         } // end for x
422     } // end for z
423     // search groups of peaks with same amplitude.
424     Int_t *flag = new Int_t[npeak];
425     for( i=0; i<npeak; i++ ) flag[i] = 0;
426     for( i=0; i<npeak; i++ ){
427         for( j=0; j<npeak; j++ ){
428             if( i==j) continue;
429             if( flag[j] > 0 ) continue;
430             if( peakAmp[i] == peakAmp[j] && 
431                 TMath::Abs(peakX[i]-peakX[j])<=1 && 
432                 TMath::Abs(peakZ[i]-peakZ[j])<=1 ){
433                 if( flag[i] == 0) flag[i] = i+1;
434                 flag[j] = flag[i];
435             } // end if ...
436         } // end for j
437     } // end for i
438     // make average of peak groups        
439     for( i=0; i<npeak; i++ ){
440         Int_t nFlag = 1;
441         if( flag[i] <= 0 ) continue;
442         for( j=0; j<npeak; j++ ){
443             if( i==j ) continue;
444             if( flag[j] != flag[i] ) continue;
445             peakX[i] += peakX[j];
446             peakZ[i] += peakZ[j];
447             nFlag++;
448             npeak--;
449             for( Int_t k=j; k<npeak; k++ ){
450                 peakX[k] = peakX[k+1];
451                 peakZ[k] = peakZ[k+1];
452                 peakAmp[k] = peakAmp[k+1];
453                 flag[k] = flag[k+1];
454             } // end for k        
455             j--;
456         } // end for j
457         if( nFlag > 1 ){
458             peakX[i] /= nFlag;
459             peakZ[i] /= nFlag;
460         } // end fi nFlag
461     } // end for i
462     delete [] flag;
463     return( npeak );
464 }
465 //______________________________________________________________________
466 void AliITSClusterFinderSDD::PeakFunc( Int_t xdim, Int_t zdim, Float_t *par,
467                                        Float_t *spe, Float_t *integral){
468     // function used to fit the clusters
469     // par -> parameters..
470     // par[0]  number of peaks.
471     // for each peak i=1, ..., par[0]
472     //                 par[i] = Ampl.
473     //                 par[i+1] = xpos
474     //                 par[i+2] = zpos
475     //                 par[i+3] = tau
476     //                 par[i+4] = sigma.
477     Int_t electronics = fResponse->Electronics(); // 1 = PASCAL, 2 = OLA
478     const Int_t knParam = 5;
479     Int_t npeak = (Int_t)par[0];
480
481     memset( spe, 0, sizeof( Float_t )*zdim*xdim );
482
483     Int_t k = 1;
484     for( Int_t i=0; i<npeak; i++ ){
485         if( integral != 0 ) integral[i] = 0.;
486         Float_t sigmaA2 = par[k+4]*par[k+4]*2.;
487         Float_t t2 = par[k+3];   // PASCAL
488         if( electronics == 2 ) { t2 *= t2; t2 *= 2; } // OLA
489         for( Int_t z=0; z<zdim; z++ ){
490             for( Int_t x=0; x<xdim; x++ ){
491                 Float_t z2 = (z-par[k+2])*(z-par[k+2])/sigmaA2;
492                 Float_t x2 = 0.;
493                 Float_t signal = 0.;
494                 if( electronics == 1 ){ // PASCAL
495                     x2 = (x-par[k+1]+t2)/t2;
496                     signal = (x2>0.) ? par[k]*x2*exp(-x2+1.-z2) :0.0; // RCCR2
497                 //  signal =(x2>0.) ? par[k]*x2*x2*exp(-2*x2+2.-z2 ):0.0;//RCCR
498                 }else if( electronics == 2 ) { // OLA
499                     x2 = (x-par[k+1])*(x-par[k+1])/t2;
500                     signal = par[k]  * exp( -x2 - z2 );
501                 } else {
502                   Warning("PeakFunc","Wrong SDD Electronics = %d",electronics);
503                     // exit( 1 );
504                 } // end if electronicx
505                 spe[x*zdim+z] += signal;
506                 if( integral != 0 ) integral[i] += signal;
507             } // end for x
508         } // end for z
509         k += knParam;
510     } // end for i
511     return;
512 }
513 //__________________________________________________________________________
514 Float_t AliITSClusterFinderSDD::ChiSqr( Int_t xdim, Int_t zdim, Float_t *spe,
515                                         Float_t *speFit ) const{
516     // EVALUATES UNNORMALIZED CHI-SQUARED
517     Float_t chi2 = 0.;
518     for( Int_t z=0; z<zdim; z++ ){
519         for( Int_t x=1; x<xdim-1; x++ ){
520             Int_t index = x*zdim+z;
521             Float_t tmp = spe[index] - speFit[index];
522             chi2 += tmp*tmp;
523         } // end for x
524     } // end for z
525     return( chi2 );
526 }
527 //_______________________________________________________________________
528 void AliITSClusterFinderSDD::Minim( Int_t xdim, Int_t zdim, Float_t *param,
529                                     Float_t *prm0,Float_t *steprm,
530                                     Float_t *chisqr,Float_t *spe,
531                                     Float_t *speFit ){
532     // 
533     Int_t   k, nnn, mmm, i;
534     Float_t p1, delta, d1, chisq1, p2, chisq2, t, p3, chisq3, a, b, p0, chisqt;
535     const Int_t knParam = 5;
536     Int_t npeak = (Int_t)param[0];
537     for( k=1; k<(npeak*knParam+1); k++ ) prm0[k] = param[k];
538     for( k=1; k<(npeak*knParam+1); k++ ){
539         p1 = param[k];
540         delta = steprm[k];
541         d1 = delta;
542         // ENSURE THAT STEP SIZE IS SENSIBLY LARGER THAN MACHINE ROUND OFF
543         if( fabs( p1 ) > 1.0E-6 ) 
544             if ( fabs( delta/p1 ) < 1.0E-4 ) delta = p1/1000;
545             else  delta = (Float_t)1.0E-4;
546         //  EVALUATE CHI-SQUARED AT FIRST TWO SEARCH POINTS
547         PeakFunc( xdim, zdim, param, speFit );
548         chisq1 = ChiSqr( xdim, zdim, spe, speFit );
549         p2 = p1+delta;
550         param[k] = p2;
551         PeakFunc( xdim, zdim, param, speFit );
552         chisq2 = ChiSqr( xdim, zdim, spe, speFit );
553         if( chisq1 < chisq2 ){
554             // REVERSE DIRECTION OF SEARCH IF CHI-SQUARED IS INCREASING
555             delta = -delta;
556             t = p1;
557             p1 = p2;
558             p2 = t;
559             t = chisq1;
560             chisq1 = chisq2;
561             chisq2 = t;
562         } // end if
563         i = 1; nnn = 0;
564         do {   // INCREMENT param(K) UNTIL CHI-SQUARED STARTS TO INCREASE
565             nnn++;
566             p3 = p2 + delta;
567             mmm = nnn - (nnn/5)*5;  // multiplo de 5
568             if( mmm == 0 ){
569                 d1 = delta;
570                 // INCREASE STEP SIZE IF STEPPING TOWARDS MINIMUM IS TOO SLOW 
571                 delta *= 5;
572             } // end if
573             param[k] = p3;
574             // Constrain paramiters
575             Int_t kpos = (k-1) % knParam;
576             switch( kpos ){
577             case 0 :
578                 if( param[k] <= 20 ) param[k] = fMinPeak;
579                 break;
580             case 1 :
581                 if( fabs( param[k] - prm0[k] ) > 1.5 ) param[k] = prm0[k];
582                 break;
583             case 2 :
584                 if( fabs( param[k] - prm0[k] ) > 1. ) param[k] = prm0[k];
585                 break;
586             case 3 :
587                 if( param[k] < .5 ) param[k] = .5;        
588                 break;
589             case 4 :
590                 if( param[k] < .288 ) param[k] = .288;        // 1/sqrt(12) = 0.288
591                 if( param[k] > zdim*.5 ) param[k] = zdim*.5;
592                 break;
593             }; // end switch
594             PeakFunc( xdim, zdim, param, speFit );
595             chisq3 = ChiSqr( xdim, zdim, spe, speFit );
596             if( chisq3 < chisq2 && nnn < 50 ){
597                 p1 = p2;
598                 p2 = p3;
599                 chisq1 = chisq2;
600                 chisq2 = chisq3;
601             }else i=0;
602         } while( i );
603         // FIND MINIMUM OF PARABOLA DEFINED BY LAST THREE POINTS
604         a = chisq1*(p2-p3)+chisq2*(p3-p1)+chisq3*(p1-p2);
605         b = chisq1*(p2*p2-p3*p3)+chisq2*(p3*p3-p1*p1)+chisq3*(p1*p1-p2*p2);
606         if( a!=0 ) p0 = (Float_t)(0.5*b/a);
607         else p0 = 10000;
608         //--IN CASE OF NEARLY EQUAL CHI-SQUARED AND TOO SMALL STEP SIZE PREVENT
609         //   ERRONEOUS EVALUATION OF PARABOLA MINIMUM
610         //---NEXT TWO LINES CAN BE OMITTED FOR HIGHER PRECISION MACHINES
611         //dp = (Float_t) max (fabs(p3-p2), fabs(p2-p1));
612         //if( fabs( p2-p0 ) > dp ) p0 = p2;
613         param[k] = p0;
614         // Constrain paramiters
615         Int_t kpos = (k-1) % knParam;
616         switch( kpos ){
617         case 0 :
618             if( param[k] <= 20 ) param[k] = fMinPeak;   
619             break;
620         case 1 :
621             if( fabs( param[k] - prm0[k] ) > 1.5 ) param[k] = prm0[k];
622             break;
623         case 2 :
624             if( fabs( param[k] - prm0[k] ) > 1. ) param[k] = prm0[k];
625             break;
626         case 3 :
627             if( param[k] < .5 ) param[k] = .5;        
628             break;
629         case 4 :
630             if( param[k] < .288 ) param[k] = .288;  // 1/sqrt(12) = 0.288
631             if( param[k] > zdim*.5 ) param[k] = zdim*.5;
632             break;
633         }; // end switch
634         PeakFunc( xdim, zdim, param, speFit );
635         chisqt = ChiSqr( xdim, zdim, spe, speFit );
636         // DO NOT ALLOW ERRONEOUS INTERPOLATION
637         if( chisqt <= *chisqr ) *chisqr = chisqt;
638         else param[k] = prm0[k];
639         // OPTIMIZE SEARCH STEP FOR EVENTUAL NEXT CALL OF MINIM
640         steprm[k] = (param[k]-prm0[k])/5;
641         if( steprm[k] >= d1 ) steprm[k] = d1/5;
642     } // end for k
643     // EVALUATE FIT AND CHI-SQUARED FOR OPTIMIZED PARAMETERS
644     PeakFunc( xdim, zdim, param, speFit );
645     *chisqr = ChiSqr( xdim, zdim, spe, speFit );
646     return;
647 }
648 //_________________________________________________________________________
649 Int_t AliITSClusterFinderSDD::NoLinearFit( Int_t xdim, Int_t zdim, 
650                                            Float_t *param, Float_t *spe, 
651                                            Int_t *niter, Float_t *chir ){
652     // fit method from Comput. Phys. Commun 46(1987) 149
653     const Float_t kchilmt = 0.01;  //        relative accuracy           
654     const Int_t   knel = 3;        //        for parabolic minimization  
655     const Int_t   knstop = 50;     //        Max. iteration number          
656     const Int_t   knParam = 5;
657     Int_t npeak = (Int_t)param[0];
658     // RETURN IF NUMBER OF DEGREES OF FREEDOM IS NOT POSITIVE 
659     if( (xdim*zdim - npeak*knParam) <= 0 ) return( -1 );
660     Float_t degFree = (xdim*zdim - npeak*knParam)-1;
661     Int_t   n, k, iterNum = 0;
662     Float_t *prm0 = new Float_t[npeak*knParam+1];
663     Float_t *step = new Float_t[npeak*knParam+1];
664     Float_t *schi = new Float_t[npeak*knParam+1]; 
665     Float_t *sprm[3];
666     sprm[0] = new Float_t[npeak*knParam+1];
667     sprm[1] = new Float_t[npeak*knParam+1];
668     sprm[2] = new Float_t[npeak*knParam+1];
669     Float_t  chi0, chi1, reldif, a, b, prmin, dp;
670     Float_t *speFit = new Float_t[ xdim*zdim ];
671     PeakFunc( xdim, zdim, param, speFit );
672     chi0 = ChiSqr( xdim, zdim, spe, speFit );
673     chi1 = chi0;
674     for( k=1; k<(npeak*knParam+1); k++) prm0[k] = param[k];
675         for( k=1 ; k<(npeak*knParam+1); k+=knParam ){
676             step[k] = param[k] / 20.0 ;
677             step[k+1] = param[k+1] / 50.0;
678             step[k+2] = param[k+2] / 50.0;                 
679             step[k+3] = param[k+3] / 20.0;                 
680             step[k+4] = param[k+4] / 20.0;                 
681         } // end for k
682     Int_t out = 0;
683     do{
684         iterNum++;
685             chi0 = chi1;
686             Minim( xdim, zdim, param, prm0, step, &chi1, spe, speFit );
687             reldif = ( chi1 > 0 ) ? ((Float_t) fabs( chi1-chi0)/chi1 ) : 0;
688         // EXIT conditions
689         if( reldif < (float) kchilmt ){
690             *chir  = (chi1>0) ? (float) TMath::Sqrt (chi1/degFree) :0;
691             *niter = iterNum;
692             out = 0;
693             break;
694         } // end if
695         if( (reldif < (float)(5*kchilmt)) && (iterNum > knstop) ){
696             *chir = (chi1>0) ?(float) TMath::Sqrt (chi1/degFree):0;
697             *niter = iterNum;
698             out = 0;
699             break;
700         } // end if
701         if( iterNum > 5*knstop ){
702             *chir  = (chi1>0) ?(float) TMath::Sqrt (chi1/degFree):0;
703             *niter = iterNum;
704             out = 1;
705             break;
706         } // end if
707         if( iterNum <= knel ) continue;
708         n = iterNum - (iterNum/knel)*knel; // EXTRAPOLATION LIMIT COUNTER N
709         if( n > 3 || n == 0 ) continue;
710         schi[n-1] = chi1;
711         for( k=1; k<(npeak*knParam+1); k++ ) sprm[n-1][k] = param[k];
712         if( n != 3 ) continue;
713         // -EVALUATE EXTRAPOLATED VALUE OF EACH PARAMETER BY FINDING MINIMUM OF
714         //    PARABOLA DEFINED BY LAST THREE CALLS OF MINIM
715         for( k=1; k<(npeak*knParam+1); k++ ){
716             Float_t tmp0 = sprm[0][k];
717             Float_t tmp1 = sprm[1][k];
718             Float_t tmp2 = sprm[2][k];
719             a  = schi[0]*(tmp1-tmp2) + schi[1]*(tmp2-tmp0);
720             a += (schi[2]*(tmp0-tmp1));
721             b  = schi[0]*(tmp1*tmp1-tmp2*tmp2);
722             b += (schi[1]*(tmp2*tmp2-tmp0*tmp0)+(schi[2]*
723                                              (tmp0*tmp0-tmp1*tmp1)));
724             if ((double)a < 1.0E-6) prmin = 0;
725             else prmin = (float) (0.5*b/a);
726             dp = 5*(tmp2-tmp0);
727             if( fabs(prmin-tmp2) > fabs(dp) ) prmin = tmp2+dp;
728             param[k] = prmin;
729             step[k]  = dp/10; // OPTIMIZE SEARCH STEP
730         } // end for k
731     } while( kTRUE );
732     delete [] prm0;
733     delete [] step;
734     delete [] schi; 
735     delete [] sprm[0];
736     delete [] sprm[1];
737     delete [] sprm[2];
738     delete [] speFit;
739     return( out );
740 }
741
742 //______________________________________________________________________
743 void AliITSClusterFinderSDD::ResolveClustersE(){
744     // The function to resolve clusters if the clusters overlapping exists
745     Int_t i;
746     static AliITS *iTS = (AliITS*)gAlice->GetModule( "ITS" );
747     // get number of clusters for this module
748     Int_t nofClusters = fClusters->GetEntriesFast();
749     nofClusters -= fNclusters;
750     Int_t fNofMaps = fSegmentation->Npz();
751     Int_t fNofAnodes = fNofMaps/2;
752 //    Int_t fMaxNofSamples = fSegmentation->Npx();
753     Int_t dummy=0;
754     Double_t fTimeStep = fSegmentation->Dpx( dummy );
755     Double_t fSddLength = fSegmentation->Dx();
756     Double_t fDriftSpeed = fResponse->DriftSpeed();
757     Double_t anodePitch = fSegmentation->Dpz( dummy );
758     Float_t n, baseline;
759     fResponse->GetNoiseParam( n, baseline );
760     Int_t electronics = fResponse->Electronics(); // 1 = PASCAL, 2 = OLA
761
762     for( Int_t j=0; j<nofClusters; j++ ){ 
763         // get cluster information
764         AliITSRawClusterSDD *clusterJ=(AliITSRawClusterSDD*) fClusters->At(j);
765         Int_t astart = clusterJ->Astart();
766         Int_t astop = clusterJ->Astop();
767         Int_t tstart = clusterJ->Tstartf();
768         Int_t tstop = clusterJ->Tstopf();
769         Int_t wing = (Int_t)clusterJ->W();
770         if( wing == 2 ){
771             astart += fNofAnodes; 
772             astop  += fNofAnodes;
773         } // end if 
774         Int_t xdim = tstop-tstart+3;
775         Int_t zdim = astop-astart+3;
776         if( xdim > 50 || zdim > 30 ) { 
777             Warning("ResolveClustersE","xdim: %d , zdim: %d ",xdim,zdim);
778             continue;
779         }
780         Float_t *sp = new Float_t[ xdim*zdim+1 ];
781         memset( sp, 0, sizeof(Float_t)*(xdim*zdim+1) );
782         
783         // make a local map from cluster region
784         for( Int_t ianode=astart; ianode<=astop; ianode++ ){
785             for( Int_t itime=tstart; itime<=tstop; itime++ ){
786                 Float_t fadc = fMap->GetSignal( ianode, itime );
787                 if( fadc > baseline ) fadc -= (Double_t)baseline;
788                 else fadc = 0.;
789                 Int_t index = (itime-tstart+1)*zdim+(ianode-astart+1);
790                 sp[index] = fadc;
791             } // time loop
792         } // anode loop
793         
794         // search peaks on cluster
795         const Int_t kNp = 150;
796         Int_t peakX1[kNp];
797         Int_t peakZ1[kNp];
798         Float_t peakAmp1[kNp];
799         Int_t npeak = SearchPeak(sp,xdim,zdim,peakX1,peakZ1,peakAmp1,fMinPeak);
800
801         // if multiple peaks, split cluster
802         if( npeak >= 1 )
803         {
804             //        cout << "npeak " << npeak << endl;
805             //        clusterJ->PrintInfo();
806             Float_t *par = new Float_t[npeak*5+1];
807             par[0] = (Float_t)npeak;                
808             // Initial parameters in cell dimentions
809             Int_t k1 = 1;
810             for( i=0; i<npeak; i++ ){
811                 par[k1] = peakAmp1[i];
812                 par[k1+1] = peakX1[i]; // local time pos. [timebin]
813                 par[k1+2] = peakZ1[i]; // local anode pos. [anodepitch]
814                 if( electronics == 1 ) 
815                     par[k1+3] = 2.; // PASCAL
816                 else if( electronics == 2 ) 
817                     par[k1+3] = 0.7; // tau [timebin]  OLA 
818                 par[k1+4] = .4;    // sigma        [anodepich]
819                 k1+=5;
820             } // end for i                        
821             Int_t niter;
822             Float_t chir;                        
823             NoLinearFit( xdim, zdim, par, sp, &niter, &chir );
824             Float_t peakX[kNp];
825             Float_t peakZ[kNp];
826             Float_t sigma[kNp];
827             Float_t tau[kNp];
828             Float_t peakAmp[kNp];
829             Float_t integral[kNp];
830             //get integrals => charge for each peak
831             PeakFunc( xdim, zdim, par, sp, integral );
832             k1 = 1;
833             for( i=0; i<npeak; i++ ){
834                 peakAmp[i] = par[k1];
835                 peakX[i]   = par[k1+1];
836                 peakZ[i]   = par[k1+2];
837                 tau[i]     = par[k1+3];
838                 sigma[i]   = par[k1+4];
839                 k1+=5;
840             } // end for i
841             // calculate parameter for new clusters
842             for( i=0; i<npeak; i++ ){
843                 AliITSRawClusterSDD clusterI( *clusterJ );
844             
845                 Int_t newAnode = peakZ1[i]-1 + astart;
846
847             //    Int_t newiTime = peakX1[i]-1 + tstart;
848             //    Int_t shift = (Int_t)(fTimeCorr/fTimeStep + 0.5);
849             //    if( newiTime > shift && newiTime < (fMaxNofSamples-shift) ) 
850             //        shift = 0;
851             //    Int_t peakpos = fMap->GetHitIndex( newAnode, newiTime+shift );
852             //    clusterI.SetPeakPos( peakpos );
853             
854                 clusterI.SetPeakAmpl( peakAmp1[i] );
855                 Float_t newAnodef = peakZ[i] - 0.5 + astart;
856                 Float_t newiTimef = peakX[i] - 1 + tstart;
857                 if( wing == 2 ) newAnodef -= fNofAnodes; 
858                 Float_t anodePath = (newAnodef - fNofAnodes/2)*anodePitch;
859                 newiTimef *= fTimeStep;
860                 if( newiTimef > fTimeCorr ) newiTimef -= fTimeCorr;
861                 if( electronics == 1 ){
862                 //    newiTimef *= 0.999438;    // PASCAL
863                 //    newiTimef += (6./fDriftSpeed - newiTimef/3000.);
864                 }else if( electronics == 2 )
865                     newiTimef *= 0.99714;    // OLA
866                     
867                 Int_t timeBin = (Int_t)(newiTimef/fTimeStep+0.5);    
868                 Int_t peakpos = fMap->GetHitIndex( newAnode, timeBin );
869                 if( peakpos < 0 ) { 
870                     for( Int_t ii=0; ii<3; ii++ ) {
871                         peakpos = fMap->GetHitIndex( newAnode, timeBin+ii );
872                         if( peakpos > 0 ) break;
873                         peakpos = fMap->GetHitIndex( newAnode, timeBin-ii );
874                         if( peakpos > 0 ) break;
875                     }
876                 }
877                 
878                 if( peakpos < 0 ) { 
879              //      Warning( "ResolveClustersE", "Digit not found for cluster:\n" );
880              //      clusterI.PrintInfo(); 
881                    continue;
882                 }
883                 clusterI.SetPeakPos( peakpos );    
884                 Float_t driftPath = fSddLength - newiTimef * fDriftSpeed;
885                 Float_t sign = ( wing == 1 ) ? -1. : 1.;
886                 clusterI.SetX( driftPath*sign * 0.0001 );        
887                 clusterI.SetZ( anodePath * 0.0001 );
888                 clusterI.SetAnode( newAnodef );
889                 clusterI.SetTime( newiTimef );
890                 clusterI.SetAsigma( sigma[i]*anodePitch );
891                 clusterI.SetTsigma( tau[i]*fTimeStep );
892                 clusterI.SetQ( integral[i] );
893                 
894                 iTS->AddCluster( 1, &clusterI );
895             } // end for i
896             fClusters->RemoveAt( j );
897             delete [] par;
898         } else {  // something odd
899             Warning( "ResolveClustersE","--- Peak not found!!!!  minpeak=%d ,cluster peak= %f , module= %d",
900                       fMinPeak, clusterJ->PeakAmpl(), fModule ); 
901             clusterJ->PrintInfo();
902             Warning( "ResolveClustersE"," xdim= %d zdim= %d", xdim-2, zdim-2 );
903         }
904         delete [] sp;
905     } // cluster loop
906     fClusters->Compress();
907 //    fMap->ClearMap(); 
908 }
909
910
911 //________________________________________________________________________
912 void  AliITSClusterFinderSDD::GroupClusters(){
913     // group clusters
914     Int_t dummy=0;
915     Float_t fTimeStep = fSegmentation->Dpx(dummy);
916     // get number of clusters for this module
917     Int_t nofClusters = fClusters->GetEntriesFast();
918     nofClusters -= fNclusters;
919     AliITSRawClusterSDD *clusterI;
920     AliITSRawClusterSDD *clusterJ;
921     Int_t *label = new Int_t [nofClusters];
922     Int_t i,j;
923     for(i=0; i<nofClusters; i++) label[i] = 0;
924     for(i=0; i<nofClusters; i++) { 
925         if(label[i] != 0) continue;
926         for(j=i+1; j<nofClusters; j++) { 
927             if(label[j] != 0) continue;
928             clusterI = (AliITSRawClusterSDD*) fClusters->At(i);
929             clusterJ = (AliITSRawClusterSDD*) fClusters->At(j);
930             // 1.3 good
931             if(clusterI->T() < fTimeStep*60) fDAnode = 4.2;  // TB 3.2  
932             if(clusterI->T() < fTimeStep*10) fDAnode = 1.5;  // TB 1.
933             Bool_t pair = clusterI->Brother(clusterJ,fDAnode,fDTime);
934             if(!pair) continue;
935             //      clusterI->PrintInfo();
936             //      clusterJ->PrintInfo();
937             clusterI->Add(clusterJ);
938             label[j] = 1;
939             fClusters->RemoveAt(j);
940             j=i; // <- Ernesto
941         } // J clusters  
942         label[i] = 1;
943     } // I clusters
944     fClusters->Compress();
945
946     delete [] label;
947     return;
948 }
949 //________________________________________________________________________
950 void AliITSClusterFinderSDD::SelectClusters(){
951     // get number of clusters for this module
952     Int_t nofClusters = fClusters->GetEntriesFast();
953
954     nofClusters -= fNclusters;
955     Int_t i;
956     for(i=0; i<nofClusters; i++) { 
957         AliITSRawClusterSDD *clusterI =(AliITSRawClusterSDD*) fClusters->At(i);
958         Int_t rmflg = 0;
959         Float_t wy = 0.;
960         if(clusterI->Anodes() != 0.) {
961             wy = ((Float_t) clusterI->Samples())/clusterI->Anodes();
962         } // end if
963         Int_t amp = (Int_t) clusterI->PeakAmpl();
964         Int_t cha = (Int_t) clusterI->Q();
965         if(amp < fMinPeak) rmflg = 1;  
966         if(cha < fMinCharge) rmflg = 1;
967         if(wy < fMinNCells) rmflg = 1;
968         //if(wy > fMaxNCells) rmflg = 1;
969         if(rmflg) fClusters->RemoveAt(i);
970     } // I clusters
971     fClusters->Compress();
972     return;
973 }
974 //__________________________________________________________________________
975 void AliITSClusterFinderSDD::ResolveClusters(){
976     // The function to resolve clusters if the clusters overlapping exists
977 /*    AliITS *iTS=(AliITS*)gAlice->GetModule("ITS");
978     // get number of clusters for this module
979     Int_t nofClusters = fClusters->GetEntriesFast();
980     nofClusters -= fNclusters;
981     //cout<<"Resolve Cl: nofClusters, fNclusters ="<<nofClusters<<","
982     // <<fNclusters<<endl;
983     Int_t fNofMaps = fSegmentation->Npz();
984     Int_t fNofAnodes = fNofMaps/2;
985     Int_t dummy=0;
986     Double_t fTimeStep = fSegmentation->Dpx(dummy);
987     Double_t fSddLength = fSegmentation->Dx();
988     Double_t fDriftSpeed = fResponse->DriftSpeed();
989     Double_t anodePitch = fSegmentation->Dpz(dummy);
990     Float_t n, baseline;
991     fResponse->GetNoiseParam(n,baseline);
992     Float_t dzz_1A = anodePitch * anodePitch / 12;
993     // fill Map of signals
994     fMap->FillMap(); 
995     Int_t j,i,ii,ianode,anode,itime;
996     Int_t wing,astart,astop,tstart,tstop,nanode;
997     Double_t fadc,ClusterTime;
998     Double_t q[400],x[400],z[400]; // digit charges and coordinates
999     for(j=0; j<nofClusters; j++) { 
1000         AliITSRawClusterSDD *clusterJ=(AliITSRawClusterSDD*) fClusters->At(j);
1001         Int_t ndigits = 0;
1002         astart=clusterJ->Astart();
1003         astop=clusterJ->Astop();
1004         tstart=clusterJ->Tstartf();
1005         tstop=clusterJ->Tstopf();
1006         nanode=clusterJ->Anodes();  // <- Ernesto
1007         wing=(Int_t)clusterJ->W();
1008         if(wing == 2) {
1009             astart += fNofAnodes; 
1010             astop  += fNofAnodes;
1011         }  // end if
1012         // cout<<"astart,astop,tstart,tstop ="<<astart<<","<<astop<<","
1013         //      <<tstart<<","<<tstop<<endl;
1014         // clear the digit arrays
1015         for(ii=0; ii<400; ii++) { 
1016             q[ii] = 0.; 
1017             x[ii] = 0.;
1018             z[ii] = 0.;
1019         } // end for ii
1020
1021         for(ianode=astart; ianode<=astop; ianode++) { 
1022             for(itime=tstart; itime<=tstop; itime++) { 
1023                 fadc=fMap->GetSignal(ianode,itime);
1024                 if(fadc>baseline) {
1025                     fadc-=(Double_t)baseline;
1026                     q[ndigits] = fadc*(fTimeStep/160);  // KeV
1027                     anode = ianode;
1028                     if(wing == 2) anode -= fNofAnodes;
1029                     z[ndigits] = (anode + 0.5 - fNofAnodes/2)*anodePitch;
1030                     ClusterTime = itime*fTimeStep;
1031                     if(ClusterTime > fTimeCorr) ClusterTime -= fTimeCorr;// ns
1032                     x[ndigits] = fSddLength - ClusterTime*fDriftSpeed;
1033                     if(wing == 1) x[ndigits] *= (-1);
1034                     // cout<<"ianode,itime,fadc ="<<ianode<<","<<itime<<","
1035                     //     <<fadc<<endl;
1036                     // cout<<"wing,anode,ndigits,charge ="<<wing<<","
1037                     //      <<anode<<","<<ndigits<<","<<q[ndigits]<<endl;
1038                     ndigits++;
1039                     continue;
1040                 } //  end if
1041                 fadc=0;
1042                 //              cout<<"fadc=0, ndigits ="<<ndigits<<endl;
1043             } // time loop
1044         } // anode loop
1045         //     cout<<"for new cluster ndigits ="<<ndigits<<endl;
1046         // Fit cluster to resolve for two separate ones --------------------
1047         Double_t qq=0., xm=0., zm=0., xx=0., zz=0., xz=0.;
1048         Double_t dxx=0., dzz=0., dxz=0.;
1049         Double_t scl = 0., tmp, tga, elps = -1.;
1050         Double_t xfit[2], zfit[2], qfit[2];
1051         Double_t pitchz = anodePitch*1.e-4;             // cm
1052         Double_t pitchx = fTimeStep*fDriftSpeed*1.e-4;  // cm
1053         Double_t sigma2;
1054         Int_t nfhits;
1055         Int_t nbins = ndigits;
1056         Int_t separate = 0;
1057         // now, all lengths are in microns
1058         for (ii=0; ii<nbins; ii++) {
1059             qq += q[ii];
1060             xm += x[ii]*q[ii];
1061             zm += z[ii]*q[ii];
1062             xx += x[ii]*x[ii]*q[ii];
1063             zz += z[ii]*z[ii]*q[ii];
1064             xz += x[ii]*z[ii]*q[ii];
1065         } // end for ii
1066         xm /= qq;
1067         zm /= qq;
1068         xx /= qq;
1069         zz /= qq;
1070         xz /= qq;
1071         dxx = xx - xm*xm;
1072         dzz = zz - zm*zm;
1073         dxz = xz - xm*zm;
1074
1075         // shrink the cluster in the time direction proportionaly to the 
1076         // dxx/dzz, which lineary depends from the drift path
1077         // new  Ernesto........         
1078         if( nanode == 1 ){
1079             dzz = dzz_1A; // for one anode cluster dzz = anode**2/12
1080             scl = TMath::Sqrt( 7.2/(-0.57*xm*1.e-3+71.8) );
1081         } // end if
1082         if( nanode == 2 ){
1083             scl = TMath::Sqrt( (-0.18*xm*1.e-3+21.3)/(-0.57*xm*1.e-3+71.8) );
1084         } // end if
1085         if( nanode == 3 ){
1086             scl = TMath::Sqrt( (-0.5*xm*1.e-3+34.5)/(-0.57*xm*1.e-3+71.8) );
1087         } // end if
1088         if( nanode > 3 ){
1089             scl = TMath::Sqrt( (1.3*xm*1.e-3+49.)/(-0.57*xm*1.e-3+71.8) );
1090         } // end if
1091         //   cout<<"1 microns: zm,dzz,xm,dxx,dxz,qq ="<<zm<<","<<dzz<<","
1092         //  <<xm<<","<<dxx<<","<<dxz<<","<<qq<<endl;
1093         //  old Boris.........
1094         //  tmp=29730. - 585.*fabs(xm/1000.); 
1095         //  scl=TMath::Sqrt(tmp/130000.);
1096    
1097         xm *= scl;
1098         xx *= scl*scl;
1099         xz *= scl;
1100
1101         dxx = xx - xm*xm;
1102         //   dzz = zz - zm*zm;
1103         dxz = xz - xm*zm;
1104         //   cout<<"microns: zm,dzz,xm,dxx,xz,dxz,qq ="<<zm<<","<<dzz<<","
1105         // <<xm<<","<<dxx<<","<<xz<<","<<dxz<<","<<qq<<endl;
1106         // if(dzz < 7200.) dzz=7200.;//for one anode cluster dzz = anode**2/12
1107   
1108         if (dxx < 0.) dxx=0.;
1109         // the data if no cluster overlapping (the coordunates are in cm) 
1110         nfhits = 1;
1111         xfit[0] = xm*1.e-4;
1112         zfit[0] = zm*1.e-4;
1113         qfit[0] = qq;
1114         //   if(nbins < 7) cout<<"**** nbins ="<<nbins<<endl;
1115   
1116         if (nbins >= 7) {
1117             if (dxz==0.) tga=0.;
1118             else {
1119                 tmp=0.5*(dzz-dxx)/dxz;
1120                 tga = (dxz<0.) ? tmp-TMath::Sqrt(tmp*tmp+1) : 
1121                                                    tmp+TMath::Sqrt(tmp*tmp+1);
1122             } // end if dxz
1123             elps=(tga*tga*dxx-2*tga*dxz+dzz)/(dxx+2*tga*dxz+tga*tga*dzz);
1124             // change from microns to cm
1125             xm *= 1.e-4; 
1126             zm *= 1.e-4; 
1127             zz *= 1.e-8;
1128             xx *= 1.e-8;
1129             xz *= 1.e-8;
1130             dxz *= 1.e-8;
1131             dxx *= 1.e-8;
1132             dzz *= 1.e-8;
1133             //   cout<<"cm: zm,dzz,xm,dxx,xz,dxz,qq ="<<zm<<","<<dzz<<","
1134             //  <<xm<<","<<dxx<<","<<xz<<","<<dxz<<","<<qq<<endl;
1135             for (i=0; i<nbins; i++) {     
1136                 x[i] = x[i] *= scl;
1137                 x[i] = x[i] *= 1.e-4;
1138                 z[i] = z[i] *= 1.e-4;
1139             } // end for i
1140             //     cout<<"!!! elps ="<<elps<<endl;
1141             if (elps < 0.3) { // try to separate hits 
1142                 separate = 1;
1143                 tmp=atan(tga);
1144                 Double_t cosa=cos(tmp),sina=sin(tmp);
1145                 Double_t a1=0., x1=0., xxx=0.;
1146                 for (i=0; i<nbins; i++) {
1147                     tmp=x[i]*cosa + z[i]*sina;
1148                     if (q[i] > a1) {
1149                         a1=q[i];
1150                         x1=tmp;
1151                     } // end if
1152                     xxx += tmp*tmp*tmp*q[i];
1153                 } // end for i
1154                 xxx /= qq;
1155                 Double_t z12=-sina*xm + cosa*zm;
1156                 sigma2=(sina*sina*xx-2*cosa*sina*xz+cosa*cosa*zz) - z12*z12;
1157                 xm=cosa*xm + sina*zm;
1158                 xx=cosa*cosa*xx + 2*cosa*sina*xz + sina*sina*zz;
1159                 Double_t x2=(xx - xm*x1 - sigma2)/(xm - x1);
1160                 Double_t r=a1*2*TMath::ACos(-1.)*sigma2/(qq*pitchx*pitchz);
1161                 for (i=0; i<33; i++) { // solve a system of equations
1162                     Double_t x1_old=x1, x2_old=x2, r_old=r;
1163                     Double_t c11=x1-x2;
1164                     Double_t c12=r;
1165                     Double_t c13=1-r;
1166                     Double_t c21=x1*x1 - x2*x2;
1167                     Double_t c22=2*r*x1;
1168                     Double_t c23=2*(1-r)*x2;
1169                     Double_t c31=3*sigma2*(x1-x2) + x1*x1*x1 - x2*x2*x2;
1170                     Double_t c32=3*r*(sigma2 + x1*x1);
1171                     Double_t c33=3*(1-r)*(sigma2 + x2*x2);
1172                     Double_t f1=-(r*x1 + (1-r)*x2 - xm);
1173                     Double_t f2=-(r*(sigma2+x1*x1)+(1-r)*(sigma2+x2*x2)- xx);
1174                     Double_t f3=-(r*x1*(3*sigma2+x1*x1)+(1-r)*x2*
1175                                                          (3*sigma2+x2*x2)-xxx);
1176                     Double_t d=c11*c22*c33+c21*c32*c13+c12*c23*c31-
1177                                        c31*c22*c13 - c21*c12*c33 - c32*c23*c11;
1178                     if (d==0.) {
1179                         cout<<"*********** d=0 ***********\n";
1180                         break;
1181                     } // end if
1182                     Double_t dr=f1*c22*c33 + f2*c32*c13 + c12*c23*f3 -
1183                         f3*c22*c13 - f2*c12*c33 - c32*c23*f1;
1184                     Double_t d1=c11*f2*c33 + c21*f3*c13 + f1*c23*c31 -
1185                         c31*f2*c13 - c21*f1*c33 - f3*c23*c11;
1186                     Double_t d2=c11*c22*f3 + c21*c32*f1 + c12*f2*c31 -
1187                         c31*c22*f1 - c21*c12*f3 - c32*f2*c11;
1188                     r  += dr/d;
1189                     x1 += d1/d;
1190                     x2 += d2/d;
1191                     if (fabs(x1-x1_old) > 0.0001) continue;
1192                     if (fabs(x2-x2_old) > 0.0001) continue;
1193                     if (fabs(r-r_old)/5 > 0.001) continue;
1194                     a1=r*qq*pitchx*pitchz/(2*TMath::ACos(-1.)*sigma2);
1195                     Double_t a2=a1*(1-r)/r;
1196                     qfit[0]=a1; xfit[0]=x1*cosa - z12*sina; zfit[0]=x1*sina + 
1197                                                                 z12*cosa;
1198                     qfit[1]=a2; xfit[1]=x2*cosa - z12*sina; zfit[1]=x2*sina + 
1199                                                                 z12*cosa;
1200                     nfhits=2;
1201                     break; // Ok !
1202                 } // end for i
1203                 if (i==33) cerr<<"No more iterations ! "<<endl;
1204             } // end of attempt to separate overlapped clusters
1205         } // end of nbins cut 
1206         if(elps < 0.) cout<<" elps=-1 ="<<elps<<endl;
1207         if(elps >0. && elps< 0.3 && nfhits == 1) cout<<" small elps, nfh=1 ="
1208                                                      <<elps<<","<<nfhits<<endl;
1209         if(nfhits == 2) cout<<" nfhits=2 ="<<nfhits<<endl;
1210         for (i=0; i<nfhits; i++) {
1211             xfit[i] *= (1.e+4/scl);
1212             if(wing == 1) xfit[i] *= (-1);
1213             zfit[i] *= 1.e+4;
1214             //       cout<<" ---------  i,xfiti,zfiti,qfiti ="<<i<<","
1215             // <<xfit[i]<<","<<zfit[i]<<","<<qfit[i]<<endl;
1216         } // end for i
1217         Int_t ncl = nfhits;
1218         if(nfhits == 1 && separate == 1) {
1219             cout<<"!!!!! no separate"<<endl;
1220             ncl = -2;
1221         }  // end if
1222         if(nfhits == 2) {
1223             cout << "Split cluster: " << endl;
1224             clusterJ->PrintInfo();
1225             cout << " in: " << endl;
1226             for (i=0; i<nfhits; i++) {
1227                 // AliITSRawClusterSDD *clust = new AliITSRawClusterSDD(wing,
1228                                                -1,-1,(Float_t)qfit[i],ncl,0,0,
1229                                                (Float_t)xfit[i],
1230                                                (Float_t)zfit[i],0,0,0,0,
1231                                                 tstart,tstop,astart,astop);
1232             //        AliITSRawClusterSDD *clust = new AliITSRawClusterSDD(wing,-1,
1233             //                                 -1,(Float_t)qfit[i],0,0,0,
1234             //                                  (Float_t)xfit[i],
1235             //                                  (Float_t)zfit[i],0,0,0,0,
1236             //                                  tstart,tstop,astart,astop,ncl);
1237             // ???????????
1238             // if(wing == 1) xfit[i] *= (-1);
1239             Float_t Anode = (zfit[i]/anodePitch+fNofAnodes/2-0.5);
1240             Float_t Time = (fSddLength - xfit[i])/fDriftSpeed;
1241             Float_t clusterPeakAmplitude = clusterJ->PeakAmpl();
1242             Float_t peakpos = clusterJ->PeakPos();
1243             Float_t clusteranodePath = (Anode - fNofAnodes/2)*anodePitch;
1244             Float_t clusterDriftPath = Time*fDriftSpeed;
1245             clusterDriftPath = fSddLength-clusterDriftPath;
1246             AliITSRawClusterSDD *clust = new AliITSRawClusterSDD(wing,Anode,
1247                                                                  Time,qfit[i],
1248                                                clusterPeakAmplitude,peakpos,
1249                                                0.,0.,clusterDriftPath,
1250                                          clusteranodePath,clusterJ->Samples()/2
1251                                     ,tstart,tstop,0,0,0,astart,astop);
1252             clust->PrintInfo();
1253             iTS->AddCluster(1,clust);
1254             //        cout<<"new cluster added: tstart,tstop,astart,astop,x,ncl ="
1255             // <<tstart<<","<<tstop<<","<<astart<<","<<astop<<","<<xfit[i]
1256             // <<","<<ncl<<endl;
1257             delete clust;
1258         }// nfhits loop
1259         fClusters->RemoveAt(j);
1260     } // if nfhits = 2
1261 } // cluster loop
1262 fClusters->Compress();
1263 fMap->ClearMap(); 
1264 */
1265     return;
1266 }
1267 //______________________________________________________________________
1268 void AliITSClusterFinderSDD::GetRecPoints(){
1269     // get rec points
1270     static AliITS *iTS=(AliITS*)gAlice->GetModule("ITS");
1271     // get number of clusters for this module
1272     Int_t nofClusters = fClusters->GetEntriesFast();
1273     nofClusters -= fNclusters;
1274     const Float_t kconvGeV = 1.e-6; // GeV -> KeV
1275     const Float_t kconv = 1.0e-4; 
1276     const Float_t kRMSx = 38.0*kconv; // microns->cm ITS TDR Table 1.3
1277     const Float_t kRMSz = 28.0*kconv; // microns->cm ITS TDR Table 1.3
1278     Int_t i;
1279     Int_t ix, iz, idx=-1;
1280     AliITSdigitSDD *dig=0;
1281     Int_t ndigits=fDigits->GetEntriesFast();
1282     for(i=0; i<nofClusters; i++) { 
1283         AliITSRawClusterSDD *clusterI = (AliITSRawClusterSDD*)fClusters->At(i);
1284         if(!clusterI) Error("SDD: GetRecPoints","i clusterI ",i,clusterI);
1285         if(clusterI) idx=clusterI->PeakPos();
1286         if(idx>ndigits) Error("SDD: GetRecPoints","idx ndigits",idx,ndigits);
1287         // try peak neighbours - to be done 
1288         if(idx&&idx<= ndigits) dig =(AliITSdigitSDD*)fDigits->UncheckedAt(idx);
1289         if(!dig) {
1290             // try cog
1291             fSegmentation->GetPadIxz(clusterI->X(),clusterI->Z(),ix,iz);
1292             dig = (AliITSdigitSDD*)fMap->GetHit(iz-1,ix-1);
1293             // if null try neighbours
1294             if (!dig) dig = (AliITSdigitSDD*)fMap->GetHit(iz-1,ix); 
1295             if (!dig) dig = (AliITSdigitSDD*)fMap->GetHit(iz-1,ix+1); 
1296             if (!dig) printf("SDD: cannot assign the track number!\n");
1297         } //  end if !dig
1298         AliITSRecPoint rnew;
1299         rnew.SetX(clusterI->X());
1300         rnew.SetZ(clusterI->Z());
1301         rnew.SetQ(clusterI->Q());   // in KeV - should be ADC
1302         rnew.SetdEdX(kconvGeV*clusterI->Q());
1303         rnew.SetSigmaX2(kRMSx*kRMSx);
1304         rnew.SetSigmaZ2(kRMSz*kRMSz);
1305
1306         if(dig) rnew.fTracks[0]=dig->GetTrack(0);
1307         if(dig) rnew.fTracks[1]=dig->GetTrack(1);
1308         if(dig) rnew.fTracks[2]=dig->GetTrack(2);
1309
1310         iTS->AddRecPoint(rnew);
1311     } // I clusters
1312 //    fMap->ClearMap();
1313 }
1314 //______________________________________________________________________
1315 void AliITSClusterFinderSDD::FindRawClusters(Int_t mod){
1316     // find raw clusters
1317     
1318     fModule = mod;
1319     
1320     Find1DClustersE();
1321     GroupClusters();
1322     SelectClusters();
1323     ResolveClustersE();
1324     GetRecPoints();
1325 }
1326 //_______________________________________________________________________
1327 void AliITSClusterFinderSDD::Print() const{
1328     // Print SDD cluster finder Parameters
1329
1330     cout << "**************************************************" << endl;
1331     cout << " Silicon Drift Detector Cluster Finder Parameters " << endl;
1332     cout << "**************************************************" << endl;
1333     cout << "Number of Clusters: " << fNclusters << endl;
1334     cout << "Anode Tolerance: " << fDAnode << endl;
1335     cout << "Time  Tolerance: " << fDTime << endl;
1336     cout << "Time  correction (electronics): " << fTimeCorr << endl;
1337     cout << "Cut Amplitude (threshold): " << fCutAmplitude << endl;
1338     cout << "Minimum Amplitude: " << fMinPeak << endl;
1339     cout << "Minimum Charge: " << fMinCharge << endl;
1340     cout << "Minimum number of cells/clusters: " << fMinNCells << endl;
1341     cout << "Maximum number of cells/clusters: " << fMaxNCells << endl;
1342     cout << "**************************************************" << endl;
1343 }