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