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