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