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