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