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