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