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