]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONClusterFinderVS.cxx
Introduce extern for the common block references
[u/mrichter/AliRoot.git] / MUON / AliMUONClusterFinderVS.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 #include <TMinuit.h> 
19 #include <TF1.h>
20 #include <TMinuit.h> 
21 #include <Riostream.h>
22
23 #include "AliMUONClusterFinderVS.h"
24 #include "AliMUONDigit.h"
25 #include "AliMUONRawCluster.h"
26 #include "AliSegmentation.h"
27 #include "AliMUONGeometrySegmentation.h"
28 #include "AliMUONMathieson.h"
29 #include "AliMUONClusterInput.h"
30 #include "AliMUONHitMapA1.h"
31 #include "AliLog.h"
32
33 //_____________________________________________________________________
34 // This function is minimized in the double-Mathieson fit
35 void fcnS2(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag);
36 void fcnS1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag);
37 void fcnCombiS1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag);
38 void fcnCombiS2(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag);
39
40 ClassImp(AliMUONClusterFinderVS)
41
42 AliMUONClusterFinderVS::AliMUONClusterFinderVS()
43   : TObject()
44 {
45 // Default constructor
46     fInput=AliMUONClusterInput::Instance();
47 //     cout <<  " TYPE" << fSegmentationType << endl;
48     fHitMap[0] = 0;
49     fHitMap[1] = 0;
50     fTrack[0]=fTrack[1]=-1;
51     fDebugLevel = 0; // make silent default
52     fGhostChi2Cut = 1e6; // nothing done by default
53     fSeg[0]    = 0;
54     fSeg[1]    = 0;
55     fSeg2[0]    = 0;
56     fSeg2[1]    = 0;
57
58     for(Int_t i=0; i<100; i++) {
59       for (Int_t j=0; j<2; j++) {
60         fDig[i][j] = 0;
61       }
62     } 
63     fRawClusters = new TClonesArray("AliMUONRawCluster",1000);
64     fNRawClusters = 0;
65 }
66  //____________________________________________________________________________
67 AliMUONClusterFinderVS::~AliMUONClusterFinderVS()
68 {
69   // Reset tracks information
70    fNRawClusters = 0;
71    if (fRawClusters) {
72      fRawClusters->Delete();
73      delete fRawClusters;
74    }
75 }
76
77 AliMUONClusterFinderVS::AliMUONClusterFinderVS(const AliMUONClusterFinderVS & clusterFinder):TObject(clusterFinder)
78 {
79 // Protected copy constructor
80
81   AliFatal("Not implemented.");
82 }
83 //____________________________________________________________________________
84 void AliMUONClusterFinderVS::ResetRawClusters()
85 {
86   // Reset tracks information
87   fNRawClusters = 0;
88   if (fRawClusters) fRawClusters->Clear();
89 }
90 //____________________________________________________________________________
91 void AliMUONClusterFinderVS::Decluster(AliMUONRawCluster *cluster)
92 {
93 // Decluster by local maxima
94     SplitByLocalMaxima(cluster);
95 }
96 //____________________________________________________________________________
97 void AliMUONClusterFinderVS::SplitByLocalMaxima(AliMUONRawCluster *c)
98 {
99 // Split complex cluster by local maxima 
100     Int_t cath, i;
101
102     fInput->SetCluster(c);
103
104     fMul[0]=c->GetMultiplicity(0);
105     fMul[1]=c->GetMultiplicity(1);
106
107 //
108 //  dump digit information into arrays
109 //
110
111     Float_t qtot;
112     
113     for (cath=0; cath<2; cath++) {
114       qtot=0;
115
116       for (i=0; i<fMul[cath]; i++) {
117         // pointer to digit
118         fDig[i][cath]=fInput->Digit(cath, c->GetIndex(i, cath));
119         // pad coordinates
120         fIx[i][cath]= fDig[i][cath]->PadX();
121         fIy[i][cath]= fDig[i][cath]->PadY();
122         // pad charge
123         fQ[i][cath] = fDig[i][cath]->Signal();
124         // pad centre coordinates
125         if (fSegmentationType == 1)
126           fSeg[cath]->
127             GetPadC(fIx[i][cath], fIy[i][cath], fX[i][cath], fY[i][cath], fZ[i][cath]);
128         else
129           fSeg2[cath]->
130             GetPadC(fInput->DetElemId(), fIx[i][cath], fIy[i][cath], fX[i][cath], fY[i][cath], fZ[i][cath]);
131       } // loop over cluster digits
132
133     }  // loop over cathodes
134
135
136     FindLocalMaxima(c);
137
138 //
139 //  Initialise and perform mathieson fits
140     Float_t chi2, oldchi2;
141 //  ++++++++++++++++++*************+++++++++++++++++++++
142 //  (1) No more than one local maximum per cathode plane 
143 //  +++++++++++++++++++++++++++++++*************++++++++
144     if ((fNLocal[0]==1 && (fNLocal[1]==0 ||  fNLocal[1]==1)) || 
145         (fNLocal[0]==0 && fNLocal[1]==1)) {
146 // Perform combined single Mathieson fit
147 // Initial values for coordinates (x,y) 
148
149         // One local maximum on cathodes 1 and 2 (X->cathode 2, Y->cathode 1)
150         if (fNLocal[0]==1 &&  fNLocal[1]==1) {
151             fXInit[0]=c->GetX(1);
152             fYInit[0]=c->GetY(0);
153             // One local maximum on cathode 1 (X,Y->cathode 1)
154         } else if (fNLocal[0]==1) {
155             fXInit[0]=c->GetX(0);
156             fYInit[0]=c->GetY(0);
157             // One local maximum on cathode 2  (X,Y->cathode 2)
158         } else {
159             fXInit[0]=c->GetX(1);
160             fYInit[0]=c->GetY(1);
161         }
162         AliDebug(1,"cas (1) CombiSingleMathiesonFit(c)");
163         chi2=CombiSingleMathiesonFit(c);
164 //      Int_t ndf = fgNbins[0]+fgNbins[1]-2;
165 //      Float_t prob = TMath::Prob(Double_t(chi2),ndf);
166 //      prob1->Fill(prob);
167 //      chi2_1->Fill(chi2);
168         oldchi2=chi2;
169         AliDebug(1,Form(" chi2 %f ",chi2));        
170
171         c->SetX(0, fXFit[0]);
172         c->SetY(0, fYFit[0]);
173
174         c->SetX(1,fXFit[0]);
175         c->SetY(1,fYFit[0]);
176         c->SetChi2(0,chi2);
177         c->SetChi2(1,chi2);
178         // Force on anod
179         if (fSegmentationType == 1) {
180           c->SetX(0, fSeg[0]->GetAnod(c->GetX(0)));
181           c->SetX(1, fSeg[1]->GetAnod(c->GetX(1)));
182         } else {
183           c->SetX(0, fSeg2[0]->GetAnod(fInput->DetElemId(), c->GetX(0)));
184           c->SetX(1, fSeg2[1]->GetAnod(fInput->DetElemId(), c->GetX(1)));
185         }
186         
187 // If reasonable chi^2 add result to the list of rawclusters
188         if (chi2 < 0.3) {
189             AddRawCluster(*c);
190 // If not try combined double Mathieson Fit
191         } else {
192                 AliDebug(1," MAUVAIS CHI2 !!!\n");
193             if (fNLocal[0]==1 &&  fNLocal[1]==1) {
194                 fXInit[0]=fX[fIndLocal[0][1]][1];
195                 fYInit[0]=fY[fIndLocal[0][0]][0];
196                 fXInit[1]=fX[fIndLocal[0][1]][1];
197                 fYInit[1]=fY[fIndLocal[0][0]][0];
198             } else if (fNLocal[0]==1) {
199                 fXInit[0]=fX[fIndLocal[0][0]][0];
200                 fYInit[0]=fY[fIndLocal[0][0]][0];
201                 fXInit[1]=fX[fIndLocal[0][0]][0];
202                 fYInit[1]=fY[fIndLocal[0][0]][0];
203             } else {
204                 fXInit[0]=fX[fIndLocal[0][1]][1];
205                 fYInit[0]=fY[fIndLocal[0][1]][1];
206                 fXInit[1]=fX[fIndLocal[0][1]][1];
207                 fYInit[1]=fY[fIndLocal[0][1]][1];
208             }
209             
210 //  Initial value for charge ratios
211             fQrInit[0]=0.5;
212             fQrInit[1]=0.5;
213             AliDebug(1,"\n cas (1) CombiDoubleMathiesonFit(c)\n");
214             chi2=CombiDoubleMathiesonFit(c);
215 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
216 //          Float_t prob = TMath::Prob(chi2,ndf);
217 //          prob2->Fill(prob);
218 //          chi2_2->Fill(chi2);
219             
220 // Was this any better ??
221             AliDebug(1,Form(" Old and new chi2 %f %f ", oldchi2, chi2));
222             if (fFitStat!=0 && chi2>0 && (2.*chi2 < oldchi2)) {
223               AliDebug(1,"Split");
224                 // Split cluster into two according to fit result
225                 Split(c);
226             } else {
227               AliDebug(1,"Do not Split");
228                 // Don't split
229                 AddRawCluster(*c);
230             }
231         }
232
233 //  +++++++++++++++++++++++++++++++++++++++
234 //  (2) Two local maxima per cathode plane 
235 //  +++++++++++++++++++++++++++++++++++++++
236     } else if (fNLocal[0]==2 &&  fNLocal[1]==2) {
237 //
238 //  Let's look for ghosts first 
239
240         Float_t xm[4][2], ym[4][2];
241         Float_t dpx, dpy, dx, dy;
242         Int_t ixm[4][2], iym[4][2];
243         Int_t isec, im1, im2, ico;
244 //
245 //  Form the 2x2 combinations
246 //  0-0, 0-1, 1-0, 1-1  
247         ico=0;
248         for (im1=0; im1<2; im1++) {
249             for (im2=0; im2<2; im2++) {     
250                 xm[ico][0]=fX[fIndLocal[im1][0]][0];
251                 ym[ico][0]=fY[fIndLocal[im1][0]][0];
252                 xm[ico][1]=fX[fIndLocal[im2][1]][1];
253                 ym[ico][1]=fY[fIndLocal[im2][1]][1];
254
255                 ixm[ico][0]=fIx[fIndLocal[im1][0]][0];
256                 iym[ico][0]=fIy[fIndLocal[im1][0]][0];
257                 ixm[ico][1]=fIx[fIndLocal[im2][1]][1];
258                 iym[ico][1]=fIy[fIndLocal[im2][1]][1];
259                 ico++;
260             }
261         }
262 // ico = 0 : first local maximum on cathodes 1 and 2
263 // ico = 1 : fisrt local maximum on cathode 1 and second on cathode 2
264 // ico = 2 : second local maximum on cathode 1 and first on cathode 1
265 // ico = 3 : second local maximum on cathodes 1 and 2
266
267 // Analyse the combinations and keep those that are possible !
268 // For each combination check consistency in x and y    
269         Int_t   iacc;
270         Bool_t  accepted[4];
271         Float_t dr[4] = {1.e4, 1.e4, 1.e4, 1.e4};
272         iacc=0;
273
274 // In case of staggering maxima are displaced by exactly half the pad-size in y. 
275 // We have to take into account the numerical precision in the consistency check;       
276         Float_t eps = 1.e-5;
277 //
278         for (ico=0; ico<4; ico++) {
279             accepted[ico]=kFALSE;
280 // cathode one: x-coordinate
281             if (fSegmentationType == 1) {
282               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
283               dpx=fSeg[0]->Dpx(isec)/2.;
284             } else {
285               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
286               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
287             }
288             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
289 // cathode two: y-coordinate
290             if (fSegmentationType == 1) {
291               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
292               dpy=fSeg[1]->Dpy(isec)/2.;
293             } else {
294               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
295               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
296             }
297             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
298             AliDebug(2,Form("\n %i %f %f %f %f %f %f \n", ico, ym[ico][0], ym[ico][1], dy, dpy, dx, dpx ));
299             if ((dx <= dpx) && (dy <= dpy+eps)) {
300                 // consistent
301                 accepted[ico]=kTRUE;
302                 dr[ico] = TMath::Sqrt(dx*dx+dy*dy);
303                 iacc++;
304             } else {
305                 // reject
306                 accepted[ico]=kFALSE;
307             }
308         }
309         AliDebug(1,Form("\n iacc= %d:\n", iacc));
310         if (iacc == 3) {
311             if (accepted[0] && accepted[1]) {
312                 if (dr[0] >= dr[1]) {
313                     accepted[0]=kFALSE;
314                 } else {
315                     accepted[1]=kFALSE;
316                 }
317             }
318
319             if (accepted[2] && accepted[3]) {
320                 if (dr[2] >= dr[3]) {
321                     accepted[2]=kFALSE;
322                 } else {
323                     accepted[3]=kFALSE;
324                 }
325             }
326 /*          
327 // eliminate one candidate
328             Float_t drmax = 0;
329             Int_t icobad = -1;
330
331             for (ico=0; ico<4; ico++) {
332                 if (accepted[ico] && dr[ico] > drmax) {
333                     icobad = ico;
334                     drmax  = dr[ico];
335                 }
336             }
337             
338             accepted[icobad] = kFALSE;
339 */
340             iacc = 2;
341         }
342         
343         
344         AliDebug(1,Form("\n iacc= %d:\n", iacc));
345         if (iacc==2) {
346                 AliDebug(1,"\n iacc=2: No problem ! \n");
347         } else if (iacc==4) {
348                 AliDebug(1,"\n iacc=4: Ok, but ghost problem !!! \n");
349         } else if (iacc==0) {
350                 AliDebug(1,"\n iacc=0: I don't know what to do with this !!!!!!!!! \n");
351         }
352
353 //  Initial value for charge ratios
354         fQrInit[0]=Float_t(fQ[fIndLocal[0][0]][0])/
355             Float_t(fQ[fIndLocal[0][0]][0]+fQ[fIndLocal[1][0]][0]);
356         fQrInit[1]=Float_t(fQ[fIndLocal[0][1]][1])/
357             Float_t(fQ[fIndLocal[0][1]][1]+fQ[fIndLocal[1][1]][1]);
358         
359 // ******* iacc = 0 *******
360 // No combinations found between the 2 cathodes
361 // We keep the center of gravity of the cluster
362         if (iacc==0) {
363             AddRawCluster(*c);
364         }
365
366 // ******* iacc = 1 *******
367 // Only one combination found between the 2 cathodes
368         if (iacc==1) {
369 // Initial values for the 2 maxima (x,y)
370
371 // 1 maximum is initialised with the maximum of the combination found (X->cathode 2, Y->cathode 1)
372 // 1 maximum is initialised with the other maximum of the first cathode  
373             if (accepted[0]){
374                 AliDebug(1,"ico=0");
375                 fXInit[0]=xm[0][1];
376                 fYInit[0]=ym[0][0];
377                 fXInit[1]=xm[3][0];
378                 fYInit[1]=ym[3][0];
379             } else if (accepted[1]){
380                 AliDebug(1,"ico=1");
381                 fXInit[0]=xm[1][1];
382                 fYInit[0]=ym[1][0];
383                 fXInit[1]=xm[2][0];
384                 fYInit[1]=ym[2][0];
385             } else if (accepted[2]){
386                 AliDebug(1,"ico=2");
387                 fXInit[0]=xm[2][1];
388                 fYInit[0]=ym[2][0];
389                 fXInit[1]=xm[1][0];
390                 fYInit[1]=ym[1][0];
391             } else if (accepted[3]){
392                 AliDebug(1,"ico=3");
393                 fXInit[0]=xm[3][1];
394                 fYInit[0]=ym[3][0];
395                 fXInit[1]=xm[0][0];
396                 fYInit[1]=ym[0][0];
397             }
398                 AliDebug(1,"cas (2) CombiDoubleMathiesonFit(c)");
399             chi2=CombiDoubleMathiesonFit(c);
400 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
401 //          Float_t prob = TMath::Prob(chi2,ndf);
402 //          prob2->Fill(prob);
403 //          chi2_2->Fill(chi2);
404             AliDebug(1,Form(" chi2 %f\n",chi2));
405
406 // If reasonable chi^2 add result to the list of rawclusters
407             if (chi2<10) {
408                 Split(c);
409
410             } else {
411 // 1 maximum is initialised with the maximum of the combination found (X->cathode 2, Y->cathode 1)
412 // 1 maximum is initialised with the other maximum of the second cathode  
413                 if (accepted[0]){
414                         AliDebug(1,"ico=0");
415                     fXInit[0]=xm[0][1];
416                     fYInit[0]=ym[0][0];
417                     fXInit[1]=xm[3][1];
418                     fYInit[1]=ym[3][1];
419                 } else if (accepted[1]){
420                         AliDebug(1,"ico=1");
421                     fXInit[0]=xm[1][1];
422                     fYInit[0]=ym[1][0];
423                     fXInit[1]=xm[2][1];
424                     fYInit[1]=ym[2][1];
425                 } else if (accepted[2]){
426                         AliDebug(1,"ico=2");
427                     fXInit[0]=xm[2][1];
428                     fYInit[0]=ym[2][0];
429                     fXInit[1]=xm[1][1];
430                     fYInit[1]=ym[1][1];
431                 } else if (accepted[3]){
432                         AliDebug(1,"ico=3");
433                     fXInit[0]=xm[3][1];
434                     fYInit[0]=ym[3][0];
435                     fXInit[1]=xm[0][1];
436                     fYInit[1]=ym[0][1];
437                 }
438                 AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
439                 chi2=CombiDoubleMathiesonFit(c);
440 //              Int_t ndf = fgNbins[0]+fgNbins[1]-6;
441 //              Float_t prob = TMath::Prob(chi2,ndf);
442 //              prob2->Fill(prob);
443 //              chi2_2->Fill(chi2);
444                 AliDebug(1,Form(" chi2 %f\n",chi2));
445
446 // If reasonable chi^2 add result to the list of rawclusters
447                 if (chi2<10) {
448                     Split(c);
449                 } else {
450 //We keep only the combination found (X->cathode 2, Y->cathode 1)
451                     for (Int_t ico=0; ico<2; ico++) {
452                         if (accepted[ico]) {
453                             AliMUONRawCluster cnew;
454                             Int_t cath;    
455                             for (cath=0; cath<2; cath++) {
456                                 cnew.SetX(cath, Float_t(xm[ico][1]));
457                                 cnew.SetY(cath, Float_t(ym[ico][0]));
458                                 cnew.SetZ(cath, fZPlane);
459                                 
460                                 cnew.SetMultiplicity(cath,c->GetMultiplicity(cath));
461                                 for (i=0; i<fMul[cath]; i++) {
462                                   cnew.SetIndex(i, cath, c->GetIndex(i,cath));
463                                   if (fSegmentationType == 1) 
464                                     fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
465                                   else 
466                                     fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
467                                 }
468                                 AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
469                                 AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
470                                 FillCluster(&cnew,cath);
471                             } 
472                             cnew.SetClusterType(cnew.PhysicsContribution());
473                             AddRawCluster(cnew);
474                             fNPeaks++;
475                         }
476                     }
477                 }
478             }
479         }
480         
481 // ******* iacc = 2 *******
482 // Two combinations found between the 2 cathodes
483         if (iacc==2) {
484 // Was the same maximum taken twice
485             if ((accepted[0]&&accepted[1]) || (accepted[2]&&accepted[3])) {
486                 AliDebug(1,"\n Maximum taken twice !!!\n");
487
488 // Have a try !! with that
489                 if (accepted[0]&&accepted[3]) {
490                     fXInit[0]=xm[0][1];
491                     fYInit[0]=ym[0][0];
492                     fXInit[1]=xm[1][1];
493                     fYInit[1]=ym[1][0];
494                 } else {
495                     fXInit[0]=xm[2][1];
496                     fYInit[0]=ym[2][0];
497                     fXInit[1]=xm[3][1];
498                     fYInit[1]=ym[3][0];
499                 }
500                 AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
501                 chi2=CombiDoubleMathiesonFit(c);
502 //                  Int_t ndf = fgNbins[0]+fgNbins[1]-6;
503 //                  Float_t prob = TMath::Prob(chi2,ndf);
504 //                  prob2->Fill(prob);
505 //                  chi2_2->Fill(chi2);
506                 Split(c);
507                 
508             } else {
509 // No ghosts ! No Problems ! -  Perform one fit only !
510                 if (accepted[0]&&accepted[3]) {
511                     fXInit[0]=xm[0][1];
512                     fYInit[0]=ym[0][0];
513                     fXInit[1]=xm[3][1];
514                     fYInit[1]=ym[3][0];
515                 } else {
516                     fXInit[0]=xm[1][1];
517                     fYInit[0]=ym[1][0];
518                     fXInit[1]=xm[2][1];
519                     fYInit[1]=ym[2][0];
520                 }
521                 AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
522                 chi2=CombiDoubleMathiesonFit(c);
523 //                  Int_t ndf = fgNbins[0]+fgNbins[1]-6;
524 //                  Float_t prob = TMath::Prob(chi2,ndf);
525 //                  prob2->Fill(prob);
526 //                  chi2_2->Fill(chi2);
527                 AliDebug(1,Form(" chi2 %f\n",chi2));
528                 Split(c);
529             }
530             
531 // ******* iacc = 4 *******
532 // Four combinations found between the 2 cathodes
533 // Ghost !!
534         } else if (iacc==4) {
535 // Perform fits for the two possibilities !!    
536 // Accept if charges are compatible on both cathodes
537 // If none are compatible, keep everything
538             fXInit[0]=xm[0][1];
539             fYInit[0]=ym[0][0];
540             fXInit[1]=xm[3][1];
541             fYInit[1]=ym[3][0];
542             AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
543             chi2=CombiDoubleMathiesonFit(c);
544 //              Int_t ndf = fgNbins[0]+fgNbins[1]-6;
545 //              Float_t prob = TMath::Prob(chi2,ndf);
546 //              prob2->Fill(prob);
547 //              chi2_2->Fill(chi2);
548             AliDebug(1,Form(" chi2 %f\n",chi2));
549             // store results of fit and postpone decision
550             Double_t sXFit[2],sYFit[2],sQrFit[2];
551             Float_t sChi2[2];
552             for (Int_t i=0;i<2;i++) {
553                 sXFit[i]=fXFit[i];
554                 sYFit[i]=fYFit[i];
555                 sQrFit[i]=fQrFit[i];
556                 sChi2[i]=fChi2[i];
557             }
558             fXInit[0]=xm[1][1];
559             fYInit[0]=ym[1][0];
560             fXInit[1]=xm[2][1];
561             fYInit[1]=ym[2][0];
562             AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
563             chi2=CombiDoubleMathiesonFit(c);
564 //              ndf = fgNbins[0]+fgNbins[1]-6;
565 //              prob = TMath::Prob(chi2,ndf);
566 //              prob2->Fill(prob);
567 //              chi2_2->Fill(chi2);
568             AliDebug(1,Form(" chi2 %f\n",chi2));
569             // We have all informations to perform the decision
570             // Compute the chi2 for the 2 possibilities
571             Float_t chi2fi,chi2si,chi2f,chi2s;
572
573             chi2f = (TMath::Log(fInput->TotalCharge(0)*fQrFit[0]
574                   /  (fInput->TotalCharge(1)*fQrFit[1]) )
575                   / fInput->ChargeCorrel() );
576             chi2f *=chi2f;
577             chi2fi = (TMath::Log(fInput->TotalCharge(0)*(1-fQrFit[0])
578                   /  (fInput->TotalCharge(1)*(1-fQrFit[1])) )
579                   / fInput->ChargeCorrel() );
580             chi2f += chi2fi*chi2fi;
581
582             chi2s = (TMath::Log(fInput->TotalCharge(0)*sQrFit[0]
583                   /  (fInput->TotalCharge(1)*sQrFit[1]) )
584                   / fInput->ChargeCorrel() );
585             chi2s *=chi2s;
586             chi2si = (TMath::Log(fInput->TotalCharge(0)*(1-sQrFit[0])
587                   /  (fInput->TotalCharge(1)*(1-sQrFit[1])) )
588                   / fInput->ChargeCorrel() );
589             chi2s += chi2si*chi2si;
590
591             // usefull to store the charge matching chi2 in the cluster
592             // fChi2[0]=sChi2[1]=chi2f;
593             // fChi2[1]=sChi2[0]=chi2s;
594
595             if (chi2f<=fGhostChi2Cut && chi2s<=fGhostChi2Cut)
596                 c->SetGhost(1);
597             if   (chi2f>fGhostChi2Cut && chi2s>fGhostChi2Cut) {
598                 // we keep the ghost
599                 c->SetGhost(2);
600                 chi2s=-1;
601                 chi2f=-1;
602             }
603             if (chi2f<=fGhostChi2Cut)
604                 Split(c);
605             if (chi2s<=fGhostChi2Cut) {
606                 // retreive saved values
607                 for (Int_t i=0;i<2;i++) {
608                     fXFit[i]=sXFit[i];
609                     fYFit[i]=sYFit[i];
610                     fQrFit[i]=sQrFit[i];
611                     fChi2[i]=sChi2[i];
612                 }
613                 Split(c);
614             }
615             c->SetGhost(0);
616         }
617
618     } else if (fNLocal[0]==2 &&  fNLocal[1]==1) {
619 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
620 //  (3) Two local maxima on cathode 1 and one maximum on cathode 2 
621 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
622 //
623         Float_t xm[4][2], ym[4][2];
624         Float_t dpx, dpy, dx, dy;
625         Int_t ixm[4][2], iym[4][2];
626         Int_t isec, im1, ico;
627 //
628 //  Form the 2x2 combinations
629 //  0-0, 0-1, 1-0, 1-1  
630         ico=0;
631         for (im1=0; im1<2; im1++) {
632             xm[ico][0]=fX[fIndLocal[im1][0]][0];
633             ym[ico][0]=fY[fIndLocal[im1][0]][0];
634             xm[ico][1]=fX[fIndLocal[0][1]][1];
635             ym[ico][1]=fY[fIndLocal[0][1]][1];
636             
637             ixm[ico][0]=fIx[fIndLocal[im1][0]][0];
638             iym[ico][0]=fIy[fIndLocal[im1][0]][0];
639             ixm[ico][1]=fIx[fIndLocal[0][1]][1];
640             iym[ico][1]=fIy[fIndLocal[0][1]][1];
641             ico++;
642         }
643 // ico = 0 : first local maximum on cathodes 1 and 2
644 // ico = 1 : second local maximum on cathode 1 and first on cathode 2
645
646 // Analyse the combinations and keep those that are possible !
647 // For each combination check consistency in x and y    
648         Int_t iacc;
649         Bool_t accepted[4];
650         iacc=0;
651         // In case of staggering maxima are displaced by exactly half the pad-size in y. 
652         // We have to take into account the numerical precision in the consistency check;
653         
654         Float_t eps = 1.e-5;
655
656         for (ico=0; ico<2; ico++) {
657             accepted[ico]=kFALSE;
658             if (fSegmentationType == 1) {
659               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
660               dpx=fSeg[0]->Dpx(isec)/2.;
661             } else {
662               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
663               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
664             }
665             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
666             if (fSegmentationType == 1) {
667               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
668               dpy=fSeg[1]->Dpy(isec)/2.;
669             } else {
670               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
671               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
672             }
673             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
674             AliDebug(2,Form("\n %i %f %f %f %f \n", ico, ym[ico][0], ym[ico][1], dy, dpy ));
675             if ((dx <= dpx) && (dy <= dpy+eps)) {
676                 // consistent
677                 accepted[ico]=kTRUE;
678                 iacc++;
679             } else {
680                 // reject
681                 accepted[ico]=kFALSE;
682             }
683         }
684         
685         Float_t chi21 = 100;
686         Float_t chi22 = 100;
687         Float_t chi23 = 100;
688
689         //  Initial value for charge ratios
690         fQrInit[0]=Float_t(fQ[fIndLocal[0][0]][0])/
691             Float_t(fQ[fIndLocal[0][0]][0]+fQ[fIndLocal[1][0]][0]);
692         fQrInit[1]=fQrInit[0];
693         
694         if (accepted[0] && accepted[1]) {
695             
696             fXInit[0]=0.5*(xm[0][1]+xm[0][0]);
697             fYInit[0]=ym[0][0];
698             fXInit[1]=0.5*(xm[0][1]+xm[1][0]);
699             fYInit[1]=ym[1][0];
700             fQrInit[0]=0.5;
701             fQrInit[1]=0.5;
702             chi23=CombiDoubleMathiesonFit(c);
703             if (chi23<10) {
704                 Split(c);
705                 Float_t yst;
706                 yst = fYFit[0];
707                 fYFit[0] = fYFit[1];
708                 fYFit[1] = yst;
709                 Split(c);
710             }
711         } else if (accepted[0]) {
712             fXInit[0]=xm[0][1];
713             fYInit[0]=ym[0][0];
714             fXInit[1]=xm[1][0];
715             fYInit[1]=ym[1][0];
716             chi21=CombiDoubleMathiesonFit(c);
717 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
718 //          Float_t prob = TMath::Prob(chi2,ndf);
719 //          prob2->Fill(prob);
720 //          chi2_2->Fill(chi21);
721             AliDebug(1,Form(" chi2 %f\n",chi21));
722             if (chi21<10) Split(c);
723         } else if (accepted[1]) {
724             fXInit[0]=xm[1][1];
725             fYInit[0]=ym[1][0];
726             fXInit[1]=xm[0][0];
727             fYInit[1]=ym[0][0];
728             chi22=CombiDoubleMathiesonFit(c);
729 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
730 //          Float_t prob = TMath::Prob(chi2,ndf);
731 //          prob2->Fill(prob);
732 //          chi2_2->Fill(chi22);
733             AliDebug(1,Form(" chi2 %f\n",chi22));
734             if (chi22<10) Split(c);
735         }
736
737         if (chi21 > 10 && chi22 > 10 && chi23 > 10) {
738 // We keep only the combination found (X->cathode 2, Y->cathode 1)
739             for (Int_t ico=0; ico<2; ico++) {
740                 if (accepted[ico]) {
741                     AliMUONRawCluster cnew;
742                     Int_t cath;    
743                     for (cath=0; cath<2; cath++) {
744                         cnew.SetX(cath, Float_t(xm[ico][1]));
745                         cnew.SetY(cath, Float_t(ym[ico][0]));
746                         cnew.SetZ(cath, fZPlane);
747                         cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
748                         for (i=0; i<fMul[cath]; i++) {
749                             cnew.SetIndex(i, cath, c->GetIndex(i, cath));
750                             if (fSegmentationType == 1) 
751                               fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
752                             else
753                               fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
754
755                         }
756                         AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
757                         AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
758                         
759                         FillCluster(&cnew,cath);
760                     } 
761                     cnew.SetClusterType(cnew.PhysicsContribution());
762                     AddRawCluster(cnew);
763                     fNPeaks++;
764                 }
765             }
766         }
767         
768 //  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
769 //  (3') One local maximum on cathode 1 and two maxima on cathode 2 
770 //  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
771     } else if (fNLocal[0]==1 && fNLocal[1]==2) {
772         Float_t xm[4][2], ym[4][2];
773         Float_t dpx, dpy, dx, dy;
774         Int_t ixm[4][2], iym[4][2];
775         Int_t isec, im1, ico;
776 //
777 //  Form the 2x2 combinations
778 //  0-0, 0-1, 1-0, 1-1  
779         ico=0;
780         for (im1=0; im1<2; im1++) {
781             xm[ico][0]=fX[fIndLocal[0][0]][0];
782             ym[ico][0]=fY[fIndLocal[0][0]][0];
783             xm[ico][1]=fX[fIndLocal[im1][1]][1];
784             ym[ico][1]=fY[fIndLocal[im1][1]][1];
785             
786             ixm[ico][0]=fIx[fIndLocal[0][0]][0];
787             iym[ico][0]=fIy[fIndLocal[0][0]][0];
788             ixm[ico][1]=fIx[fIndLocal[im1][1]][1];
789             iym[ico][1]=fIy[fIndLocal[im1][1]][1];
790             ico++;
791         }
792 // ico = 0 : first local maximum on cathodes 1 and 2
793 // ico = 1 : first local maximum on cathode 1 and second on cathode 2
794
795 // Analyse the combinations and keep those that are possible !
796 // For each combination check consistency in x and y    
797         Int_t iacc;
798         Bool_t accepted[4];
799         iacc=0;
800         // In case of staggering maxima are displaced by exactly half the pad-size in y. 
801         // We have to take into account the numerical precision in the consistency check;       
802         Float_t eps = 1.e-5;
803
804         
805         for (ico=0; ico<2; ico++) {
806             accepted[ico]=kFALSE;
807             if (fSegmentationType == 1) {
808               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
809               dpx=fSeg[0]->Dpx(isec)/2.;
810             } else {
811               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
812               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
813             }
814             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
815             if (fSegmentationType == 1) {
816               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
817               dpy=fSeg[1]->Dpy(isec)/2.;
818             } else {
819               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
820               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
821             }
822             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
823             AliDebug(1,Form("\n %i %f %f %f %f \n", ico, ym[ico][0], ym[ico][1], dy, dpy ));
824             if ((dx <= dpx) && (dy <= dpy+eps)) {
825                 // consistent
826                 accepted[ico]=kTRUE;
827                 AliDebug(1,Form("ico %d\n",ico));
828                 iacc++;
829             } else {
830                 // reject
831                 accepted[ico]=kFALSE;
832             }
833         }
834
835         Float_t chi21 = 100;
836         Float_t chi22 = 100;
837         Float_t chi23 = 100;
838
839         fQrInit[1]=Float_t(fQ[fIndLocal[0][1]][1])/
840             Float_t(fQ[fIndLocal[0][1]][1]+fQ[fIndLocal[1][1]][1]);
841         
842         fQrInit[0]=fQrInit[1];
843
844         
845         if (accepted[0] && accepted[1]) {
846             fXInit[0]=xm[0][1];
847             fYInit[0]=0.5*(ym[0][0]+ym[0][1]);
848             fXInit[1]=xm[1][1];
849             fYInit[1]=0.5*(ym[0][0]+ym[1][1]);
850             fQrInit[0]=0.5;
851             fQrInit[1]=0.5;
852             chi23=CombiDoubleMathiesonFit(c);
853             if (chi23<10) {
854                 Split(c);
855                 Float_t yst;
856                 yst = fYFit[0];
857                 fYFit[0] = fYFit[1];
858                 fYFit[1] = yst;
859                 Split(c);
860             }
861         } else if (accepted[0]) {
862             fXInit[0]=xm[0][0];
863             fYInit[0]=ym[0][1];
864             fXInit[1]=xm[1][1];
865             fYInit[1]=ym[1][1];
866             chi21=CombiDoubleMathiesonFit(c);
867 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
868 //          Float_t prob = TMath::Prob(chi2,ndf);
869 //          prob2->Fill(prob);
870 //          chi2_2->Fill(chi21);
871             AliDebug(1,Form(" chi2 %f\n",chi21));
872             if (chi21<10) Split(c);
873         } else if (accepted[1]) {
874             fXInit[0]=xm[1][0];
875             fYInit[0]=ym[1][1];
876             fXInit[1]=xm[0][1];
877             fYInit[1]=ym[0][1];
878             chi22=CombiDoubleMathiesonFit(c);
879 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
880 //          Float_t prob = TMath::Prob(chi2,ndf);
881 //          prob2->Fill(prob);
882 //          chi2_2->Fill(chi22);
883             AliDebug(1,Form(" chi2 %f\n",chi22));
884             if (chi22<10) Split(c);
885         }
886
887         if (chi21 > 10 && chi22 > 10 && chi23 > 10) {
888 //We keep only the combination found (X->cathode 2, Y->cathode 1)
889             for (Int_t ico=0; ico<2; ico++) {
890                 if (accepted[ico]) {
891                     AliMUONRawCluster cnew;
892                     Int_t cath;    
893                     for (cath=0; cath<2; cath++) {
894                         cnew.SetX(cath, Float_t(xm[ico][1]));
895                         cnew.SetY(cath, Float_t(ym[ico][0]));
896                         cnew.SetZ(cath, fZPlane);
897                         cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
898                         for (i=0; i<fMul[cath]; i++) {
899                             cnew.SetIndex(i, cath, c->GetIndex(i, cath));
900                             if (fSegmentationType == 1) 
901                               fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
902                             else 
903                               fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
904                         }
905                         AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
906                         AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
907                         FillCluster(&cnew,cath);
908                     } 
909                     cnew.SetClusterType(cnew.PhysicsContribution());
910                     AddRawCluster(cnew);
911                     fNPeaks++;
912                 }
913             }
914         }
915
916 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
917 //  (4) At least three local maxima on cathode 1 or on cathode 2 
918 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
919     } else if (fNLocal[0]>2 || fNLocal[1]>2) {
920         Int_t param = fNLocal[0]*fNLocal[1];
921         Int_t ii;
922
923         Float_t ** xm = new Float_t * [param];
924         for (ii=0; ii<param; ii++) xm[ii]=new Float_t [2];
925         Float_t ** ym = new Float_t * [param];
926         for (ii=0; ii<param; ii++) ym[ii]=new Float_t [2];
927         Int_t ** ixm = new Int_t * [param];
928         for (ii=0; ii<param; ii++) ixm[ii]=new Int_t [2];
929         Int_t ** iym = new Int_t * [param];
930         for (ii=0; ii<param; ii++) iym[ii]=new Int_t [2];
931         
932         Int_t isec, ico;
933         Float_t dpx, dpy, dx, dy;
934
935         ico=0;
936         for (Int_t im1=0; im1<fNLocal[0]; im1++) {
937             for (Int_t im2=0; im2<fNLocal[1]; im2++) {
938                 xm[ico][0]=fX[fIndLocal[im1][0]][0];
939                 ym[ico][0]=fY[fIndLocal[im1][0]][0];
940                 xm[ico][1]=fX[fIndLocal[im2][1]][1];
941                 ym[ico][1]=fY[fIndLocal[im2][1]][1];
942
943                 ixm[ico][0]=fIx[fIndLocal[im1][0]][0];
944                 iym[ico][0]=fIy[fIndLocal[im1][0]][0];
945                 ixm[ico][1]=fIx[fIndLocal[im2][1]][1];
946                 iym[ico][1]=fIy[fIndLocal[im2][1]][1];
947                 ico++;
948             }
949         }
950         
951         Int_t nIco = ico;
952         AliDebug(1,Form("nIco %d\n",nIco));
953         for (ico=0; ico<nIco; ico++) {
954             AliDebug(1,Form("ico = %d\n",ico));
955             if (fSegmentationType == 1) {
956               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
957               dpx=fSeg[0]->Dpx(isec)/2.;
958             } else {
959               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
960               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
961             } 
962             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
963             if (fSegmentationType == 1) {
964               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
965               dpy=fSeg[1]->Dpy(isec)/2.;
966             } else {
967               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
968               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
969             }
970             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
971                 AliDebug(1,Form("dx %f dpx %f dy %f dpy %f\n",dx,dpx,dy,dpy));
972                 AliDebug(1,Form("  X %f Y %f\n",xm[ico][1],ym[ico][0]));
973             if ((dx <= dpx) && (dy <= dpy)) {
974                         AliDebug(1,"ok\n");
975                 Int_t cath;    
976                 AliMUONRawCluster cnew;
977                 for (cath=0; cath<2; cath++) {
978                     cnew.SetX(cath, Float_t(xm[ico][1]));
979                     cnew.SetY(cath, Float_t(ym[ico][0]));
980                     cnew.SetZ(cath, fZPlane);
981                     cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
982                     for (i=0; i<fMul[cath]; i++) {
983                         cnew.SetIndex(i, cath, c->GetIndex(i, cath));
984                         if (fSegmentationType == 1) 
985                           fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
986                         else 
987                           fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
988                     }
989                     FillCluster(&cnew,cath);
990                 } 
991                 cnew.SetClusterType(cnew.PhysicsContribution());
992                 AddRawCluster(cnew);
993                 fNPeaks++;
994             }
995         }
996         delete [] xm;
997         delete [] ym;
998         delete [] ixm;
999         delete [] iym;
1000     }
1001 }
1002
1003 void AliMUONClusterFinderVS::FindLocalMaxima(AliMUONRawCluster* /*c*/)
1004 {
1005 // Find all local maxima of a cluster
1006     AliDebug(1,"\n Find Local maxima  !");
1007     
1008     AliMUONDigit* digt;
1009     
1010     Int_t cath, cath1; // loops over cathodes
1011     Int_t i;           // loops over digits
1012     Int_t j;           // loops over cathodes
1013 //
1014 //  Find local maxima
1015 //
1016 //  counters for number of local maxima
1017     fNLocal[0]=fNLocal[1]=0;
1018 //  flags digits as local maximum
1019     Bool_t isLocal[100][2];
1020     for (i=0; i<100;i++) {
1021         isLocal[i][0]=isLocal[i][1]=kFALSE;
1022     }
1023 //  number of next neighbours and arrays to store them 
1024     Int_t nn;
1025     Int_t x[10], y[10];
1026     // loop over cathodes
1027     for (cath=0; cath<2; cath++) {
1028       // loop over cluster digits
1029       for (i=0; i<fMul[cath]; i++) {
1030         // get neighbours for that digit and assume that it is local maximum
1031         Int_t isec;
1032         Float_t a0;
1033
1034         if (fSegmentationType == 1)         
1035           fSeg[cath]->Neighbours(fIx[i][cath], fIy[i][cath], &nn, x, y);
1036         else 
1037           fSeg2[cath]->Neighbours(fInput->DetElemId(), fIx[i][cath], fIy[i][cath], &nn, x, y);
1038           
1039         isLocal[i][cath]=kTRUE;
1040
1041         if (fSegmentationType == 1) {
1042           isec = fSeg[cath]->Sector(fIx[i][cath], fIy[i][cath]);
1043           a0   = fSeg[cath]->Dpx(isec)*fSeg[cath]->Dpy(isec);
1044         } else {
1045           isec = fSeg2[cath]->Sector(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
1046           a0   = fSeg2[cath]->Dpx(fInput->DetElemId(), isec)*fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1047         }
1048         // loop over next neighbours, if at least one neighbour has higher charger assumption
1049         // digit is not local maximum 
1050         for (j=0; j<nn; j++) {
1051           if (fHitMap[cath]->TestHit(x[j], y[j])==kEmpty) continue;
1052           digt=(AliMUONDigit*) fHitMap[cath]->GetHit(x[j], y[j]);
1053           Float_t a1;
1054           if (fSegmentationType == 1) {
1055             isec=fSeg[cath]->Sector(x[j], y[j]);
1056             a1 = fSeg[cath]->Dpx(isec)*fSeg[cath]->Dpy(isec);
1057           } else {
1058             isec=fSeg2[cath]->Sector(fInput->DetElemId(), x[j], y[j]);
1059             a1 = fSeg2[cath]->Dpx(fInput->DetElemId(),isec)*fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1060           }
1061           if (digt->Signal()/a1 > fQ[i][cath]/a0) {
1062             isLocal[i][cath]=kFALSE;
1063             break;
1064             //
1065             // handle special case of neighbouring pads with equal signal
1066           } else if (digt->Signal() == fQ[i][cath]) {
1067             if (fNLocal[cath]>0) {
1068               for (Int_t k=0; k<fNLocal[cath]; k++) {
1069                 if (x[j]==fIx[fIndLocal[k][cath]][cath] 
1070                     && y[j]==fIy[fIndLocal[k][cath]][cath])
1071                   {
1072                     isLocal[i][cath]=kFALSE;
1073                   } 
1074               } // loop over local maxima
1075             } // are there already local maxima
1076           } // same charge ? 
1077         } // loop over next neighbours
1078         if (isLocal[i][cath]) {
1079           fIndLocal[fNLocal[cath]][cath]=i;
1080           fNLocal[cath]++;
1081         } 
1082       } // loop over all digits
1083     } // loop over cathodes
1084
1085     AliDebug(1,Form("\n Found %d %d %d %d local Maxima\n",
1086                fNLocal[0], fNLocal[1], fMul[0], fMul[1]));
1087         AliDebug(1,Form("\n Cathode 1 local Maxima %d Multiplicite %d\n",fNLocal[0], fMul[0]));
1088         AliDebug(1,Form(" Cathode 2 local Maxima %d Multiplicite %d\n",fNLocal[1], fMul[1]));
1089     Int_t ix, iy, isec;
1090     Float_t dpx, dpy;
1091     
1092     
1093     if (fNLocal[1]==2 &&  (fNLocal[0]==1 || fNLocal[0]==0)) {
1094         Int_t iback=fNLocal[0];
1095         
1096 //  Two local maxima on cathode 2 and one maximum on cathode 1 
1097 //  Look for local maxima considering up and down neighbours on the 1st cathode only
1098 //
1099 //  Loop over cluster digits
1100         cath=0;
1101         cath1=1;
1102         
1103         for (i=0; i<fMul[cath]; i++) {
1104           if (fSegmentationType == 1) {
1105             isec=fSeg[cath]->Sector(fIx[i][cath],fIy[i][cath]);
1106             dpy=fSeg[cath]->Dpy(isec);
1107             dpx=fSeg[cath]->Dpx(isec);
1108           } else {
1109             isec=fSeg2[cath]->Sector(fInput->DetElemId(), fIx[i][cath],fIy[i][cath]);
1110             dpy=fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1111             dpx=fSeg2[cath]->Dpx(fInput->DetElemId(), isec);
1112           }
1113             if (isLocal[i][cath]) continue;
1114 // Pad position should be consistent with position of local maxima on the opposite cathode
1115             if ((TMath::Abs(fX[i][cath]-fX[fIndLocal[0][cath1]][cath1]) > dpx/2.) && 
1116                 (TMath::Abs(fX[i][cath]-fX[fIndLocal[1][cath1]][cath1]) > dpx/2.))
1117                 continue;
1118
1119 // get neighbours for that digit and assume that it is local maximum        
1120             isLocal[i][cath]=kTRUE;
1121 // compare signal to that on the two neighbours on the left and on the right
1122 // iNN counts the number of neighbours with signal, it should be 1 or 2
1123             Int_t iNN=0;
1124             if (fSegmentationType == 1) {
1125
1126               for (fSeg[cath]->FirstPad(fX[i][cath], fY[i][cath], fZPlane, 0., dpy);
1127                    fSeg[cath]->MorePads();
1128                    fSeg[cath]->NextPad())
1129                 {
1130                   ix = fSeg[cath]->Ix();
1131                   iy = fSeg[cath]->Iy();
1132                   // skip the current pad
1133                   if (iy == fIy[i][cath]) continue;
1134                 
1135                   if (fHitMap[cath]->TestHit(ix, iy)!=kEmpty) {
1136                     iNN++;
1137                     digt=(AliMUONDigit*) fHitMap[cath]->GetHit(ix,iy);
1138                     if (digt->Signal() > fQ[i][cath]) isLocal[i][cath]=kFALSE;
1139                   }
1140                 } // Loop over pad neighbours in y
1141             } else {
1142
1143               for (fSeg2[cath]->FirstPad(fInput->DetElemId(), fX[i][cath], fY[i][cath], fZPlane, 0., dpy);
1144                    fSeg2[cath]->MorePads(fInput->DetElemId());
1145                    fSeg2[cath]->NextPad(fInput->DetElemId()))
1146                 {
1147                   ix = fSeg2[cath]->Ix();
1148                   iy = fSeg2[cath]->Iy();
1149                   // skip the current pad
1150                   if (iy == fIy[i][cath]) continue;
1151                 
1152                   if (fHitMap[cath]->TestHit(ix, iy)!=kEmpty) {
1153                     iNN++;
1154                     digt=(AliMUONDigit*) fHitMap[cath]->GetHit(ix,iy);
1155                     if (digt->Signal() > fQ[i][cath]) isLocal[i][cath]=kFALSE;
1156                   }
1157                 } // Loop over pad neighbours in y
1158             }
1159             if (isLocal[i][cath] && iNN>0) {
1160                 fIndLocal[fNLocal[cath]][cath]=i;
1161                 fNLocal[cath]++;
1162             } 
1163         } // loop over all digits
1164 // if one additional maximum has been found we are happy 
1165 // if more maxima have been found restore the previous situation
1166         AliDebug(1,Form("\n New search gives %d local maxima for cathode 1 \n",
1167                     fNLocal[0]));
1168         AliDebug(1,Form("                  %d local maxima for cathode 2 \n",
1169                     fNLocal[1]));
1170         if (fNLocal[cath]>2) {
1171             fNLocal[cath]=iback;
1172         }
1173         
1174     } // 1,2 local maxima
1175     
1176     if (fNLocal[0]==2 &&  (fNLocal[1]==1 || fNLocal[1]==0)) {
1177         Int_t iback=fNLocal[1];
1178         
1179 //  Two local maxima on cathode 1 and one maximum on cathode 2 
1180 //  Look for local maxima considering left and right neighbours on the 2nd cathode only
1181         cath=1;
1182         Int_t cath1 = 0;
1183         Float_t eps = 1.e-5;
1184         
1185 //
1186 //  Loop over cluster digits
1187         for (i=0; i<fMul[cath]; i++) {
1188           if (fSegmentationType == 1) {
1189             isec=fSeg[cath]->Sector(fIx[i][cath],fIy[i][cath]);
1190             dpx=fSeg[cath]->Dpx(isec);
1191             dpy=fSeg[cath]->Dpy(isec);
1192           } else {
1193             isec=fSeg2[cath]->Sector(fInput->DetElemId(), fIx[i][cath],fIy[i][cath]);
1194             dpx=fSeg2[cath]->Dpx(fInput->DetElemId(), isec);
1195             dpy=fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1196           }
1197         
1198             if (isLocal[i][cath]) continue;
1199 // Pad position should be consistent with position of local maxima on the opposite cathode
1200             if ((TMath::Abs(fY[i][cath]-fY[fIndLocal[0][cath1]][cath1]) > dpy/2.+eps) && 
1201                 (TMath::Abs(fY[i][cath]-fY[fIndLocal[1][cath1]][cath1]) > dpy/2.+eps))
1202                 continue;
1203             
1204 //
1205 // get neighbours for that digit and assume that it is local maximum        
1206             isLocal[i][cath]=kTRUE;
1207 // compare signal to that on the two neighbours on the left and on the right
1208
1209 // iNN counts the number of neighbours with signal, it should be 1 or 2
1210             Int_t iNN=0;
1211             if (fSegmentationType == 1) {
1212               for (fSeg[cath]->FirstPad(fX[i][cath], fY[i][cath], fZPlane, dpx, 0.);
1213                    fSeg[cath]->MorePads();
1214                    fSeg[cath]->NextPad())
1215                 {
1216
1217                   ix = fSeg[cath]->Ix();
1218                   iy = fSeg[cath]->Iy();
1219
1220                   // skip the current pad
1221                   if (ix == fIx[i][cath]) continue;
1222                 
1223                   if (fHitMap[cath]->TestHit(ix, iy)!=kEmpty) {
1224                     iNN++;
1225                     digt=(AliMUONDigit*) fHitMap[cath]->GetHit(ix,iy);
1226                     if (digt->Signal() > fQ[i][cath]) isLocal[i][cath]=kFALSE;
1227                   }
1228                 } // Loop over pad neighbours in x
1229             } else {
1230               for (fSeg2[cath]->FirstPad(fInput->DetElemId(), fX[i][cath], fY[i][cath], fZPlane, dpx, 0.);
1231                    fSeg2[cath]->MorePads(fInput->DetElemId());
1232                    fSeg2[cath]->NextPad(fInput->DetElemId()))
1233                 {
1234
1235                   ix = fSeg2[cath]->Ix();
1236                   iy = fSeg2[cath]->Iy();
1237
1238                   // skip the current pad
1239                   if (ix == fIx[i][cath]) continue;
1240                 
1241                   if (fHitMap[cath]->TestHit(ix, iy)!=kEmpty) {
1242                     iNN++;
1243                     digt=(AliMUONDigit*) fHitMap[cath]->GetHit(ix,iy);
1244                     if (digt->Signal() > fQ[i][cath]) isLocal[i][cath]=kFALSE;
1245                   }
1246                 } // Loop over pad neighbours in x
1247             }
1248             if (isLocal[i][cath] && iNN>0) {
1249                 fIndLocal[fNLocal[cath]][cath]=i;
1250                 fNLocal[cath]++;
1251             } 
1252         } // loop over all digits
1253 // if one additional maximum has been found we are happy 
1254 // if more maxima have been found restore the previous situation
1255         AliDebug(1,Form("\n New search gives %d local maxima for cathode 1 \n",fNLocal[0]));
1256         AliDebug(1,Form("\n                  %d local maxima for cathode 2 \n",fNLocal[1]));
1257         AliDebug(1,Form("\n New search gives %d %d \n",fNLocal[0],fNLocal[1]));
1258         if (fNLocal[cath]>2) {
1259             fNLocal[cath]=iback;
1260         }
1261     } // 2,1 local maxima
1262 }
1263
1264
1265 void  AliMUONClusterFinderVS::FillCluster(AliMUONRawCluster* c, Int_t flag, Int_t cath) 
1266 {
1267 //
1268 //  Completes cluster information starting from list of digits
1269 //
1270     AliMUONDigit* dig;
1271     Float_t x, y, z;
1272     Int_t  ix, iy;
1273     
1274     if (cath==1) {
1275         c->SetPeakSignal(cath,c->GetPeakSignal(0));     
1276     } else {
1277         c->SetPeakSignal(cath,0);
1278     }
1279     
1280     
1281     if (flag) {
1282         c->SetX(cath,0.);
1283         c->SetY(cath,0.);
1284         c->SetCharge(cath,0);
1285     }
1286
1287     AliDebug(1,Form("\n fPeakSignal %d\n",c->GetPeakSignal(cath)));
1288     for (Int_t i=0; i<c->GetMultiplicity(cath); i++)
1289     {
1290         dig= fInput->Digit(cath,c->GetIndex(i,cath));
1291         ix=dig->PadX()+c->GetOffset(i,cath);
1292         iy=dig->PadY();
1293         Int_t q=dig->Signal();
1294         if (!flag) q=Int_t(q*c->GetContrib(i,cath));
1295 //      fprintf(stderr,"q %d c->fPeakSignal[ %d ] %d\n",q,cath,c->fPeakSignal[cath]);
1296         if (dig->Physics() >= dig->Signal()) {
1297             c->SetPhysics(i,2);
1298         } else if (dig->Physics() == 0) {
1299             c->SetPhysics(i,0);
1300         } else  c->SetPhysics(i,1);
1301 //
1302 // 
1303         AliDebug(2,Form("q %d c->fPeakSignal[cath] %d\n",q,c->GetPeakSignal(cath)));
1304 // peak signal and track list
1305         if (q>c->GetPeakSignal(cath)) {
1306             c->SetPeakSignal(cath, q);
1307             c->SetTrack(0,dig->Hit());
1308             c->SetTrack(1,dig->Track(0));
1309             c->SetTrack(2,dig->Track(1));
1310 //          fprintf(stderr," c->fTracks[0] %d c->fTracks[1] %d\n",dig->fHit,dig->fTracks[0]);
1311         }
1312 //
1313         if (flag) {
1314           if (fSegmentationType == 1) 
1315             fSeg[cath]->GetPadC(ix, iy, x, y, z);
1316           else
1317             fSeg2[cath]->GetPadC(fInput->DetElemId(), ix, iy, x, y, z);
1318         
1319             c->AddX(cath, q*x);
1320             c->AddY(cath, q*y);
1321             c->AddCharge(cath, q);
1322         }
1323     } // loop over digits
1324     AliDebug(1," fin du cluster c\n");
1325
1326
1327     if (flag) {
1328         c->SetX(cath, c->GetX(cath)/c->GetCharge(cath));
1329 // Force on anod
1330         if (fSegmentationType == 1) 
1331           c->SetX(cath, fSeg[cath]->GetAnod(c->GetX(cath)));
1332         else
1333           c->SetX(cath, fSeg2[cath]->GetAnod(fInput->DetElemId(), c->GetX(cath)));
1334         c->SetY(cath, c->GetY(cath)/c->GetCharge(cath)); 
1335 //
1336 //  apply correction to the coordinate along the anode wire
1337 //
1338         x=c->GetX(cath);   
1339         y=c->GetY(cath);
1340         TF1* cogCorr;
1341         Int_t isec;
1342         if (fSegmentationType == 1) {
1343           fSeg[cath]->GetPadI(x, y, fZPlane, ix, iy);
1344           fSeg[cath]->GetPadC(ix, iy, x, y, z);
1345           isec=fSeg[cath]->Sector(ix,iy);
1346           cogCorr = fSeg[cath]->CorrFunc(isec-1);
1347         } else {
1348           fSeg2[cath]->GetPadI(fInput->DetElemId(), x, y, fZPlane, ix, iy);
1349           fSeg2[cath]->GetPadC(fInput->DetElemId(), ix, iy, x, y, z);
1350           isec=fSeg2[cath]->Sector(fInput->DetElemId(), ix,iy);
1351           cogCorr = fSeg2[cath]->CorrFunc(fInput->DetElemId(), isec-1);
1352         }
1353         
1354         if (cogCorr) {
1355           Float_t yOnPad;
1356           if (fSegmentationType == 1) 
1357             yOnPad=(c->GetY(cath)-y)/fSeg[cath]->Dpy(isec);
1358           else 
1359             yOnPad=(c->GetY(cath)-y)/fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1360
1361           c->SetY(cath, c->GetY(cath)-cogCorr->Eval(yOnPad, 0, 0));
1362         }
1363     }
1364 }
1365
1366 void  AliMUONClusterFinderVS::FillCluster(AliMUONRawCluster* c, Int_t cath) 
1367 {
1368 //
1369 //  Completes cluster information starting from list of digits
1370 //
1371     static Float_t dr0;
1372
1373     AliMUONDigit* dig;
1374
1375     if (cath==0) {
1376         dr0 = 10000;
1377     }
1378     
1379     Float_t xpad, ypad, zpad;
1380     Float_t dx, dy, dr;
1381
1382     for (Int_t i=0; i<c->GetMultiplicity(cath); i++)
1383     {
1384         dig = fInput->Digit(cath,c->GetIndex(i,cath));
1385         if (fSegmentationType == 1) 
1386           fSeg[cath]->
1387             GetPadC(dig->PadX(),dig->PadY(),xpad,ypad, zpad);
1388         else
1389           fSeg2[cath]->
1390             GetPadC(fInput->DetElemId(),dig->PadX(),dig->PadY(),xpad,ypad, zpad);
1391         AliDebug(1,Form("x %f y %f cx %f cy %f\n",xpad,ypad,c->GetX(0),c->GetY(0)));
1392         dx = xpad - c->GetX(0);
1393         dy = ypad - c->GetY(0);
1394         dr = TMath::Sqrt(dx*dx+dy*dy);
1395
1396         if (dr < dr0) {
1397             dr0 = dr;
1398             AliDebug(1,Form(" dr %f\n",dr));
1399             Int_t q=dig->Signal();
1400             if (dig->Physics() >= dig->Signal()) {
1401                 c->SetPhysics(i,2);
1402             } else if (dig->Physics() == 0) {
1403                 c->SetPhysics(i,0);
1404             } else  c->SetPhysics(i,1);
1405             c->SetPeakSignal(cath,q);
1406             c->SetTrack(0,dig->Hit());
1407             c->SetTrack(1,dig->Track(0));
1408             c->SetTrack(2,dig->Track(1));
1409             AliDebug(1,Form(" c->fTracks[0] %d c->fTracks[1] %d\n",dig->Hit(),
1410                     dig->Track(0)));
1411         }
1412 //
1413     } // loop over digits
1414
1415 //  apply correction to the coordinate along the anode wire
1416 // Force on anod
1417     if (fSegmentationType == 1) 
1418       c->SetX(cath,fSeg[cath]->GetAnod(c->GetX(cath)));
1419     else
1420       c->SetX(cath,fSeg2[cath]->GetAnod(fInput->DetElemId(), c->GetX(cath)));
1421 }
1422
1423 void  AliMUONClusterFinderVS::FindCluster(Int_t i, Int_t j, Int_t cath, AliMUONRawCluster &c){
1424
1425
1426 //
1427 //  Find a super cluster on both cathodes
1428 //
1429 //
1430 //  Add i,j as element of the cluster
1431 //
1432     
1433     Int_t idx = fHitMap[cath]->GetHitIndex(i,j);
1434     AliMUONDigit* dig = (AliMUONDigit*) fHitMap[cath]->GetHit(i,j);
1435     Int_t q=dig->Signal();
1436     Int_t theX=dig->PadX();
1437     Int_t theY=dig->PadY(); 
1438    
1439     if (q > TMath::Abs(c.GetPeakSignal(0)) && q > TMath::Abs(c.GetPeakSignal(1))) {
1440         c.SetPeakSignal(cath,q);
1441         c.SetTrack(0,dig->Hit());
1442         c.SetTrack(1,dig->Track(0));
1443         c.SetTrack(2,dig->Track(1));
1444     }
1445
1446 //
1447 //  Make sure that list of digits is ordered 
1448 // 
1449     Int_t mu=c.GetMultiplicity(cath);
1450     c.SetIndex(mu, cath, idx);
1451     
1452     if (dig->Physics() >= dig->Signal()) {
1453         c.SetPhysics(mu,2);
1454     } else if (dig->Physics() == 0) {
1455         c.SetPhysics(mu,0);
1456     } else  c.SetPhysics(mu,1);
1457
1458     
1459     if (mu > 0) {
1460         for (Int_t ind = mu-1; ind >= 0; ind--) {
1461             Int_t ist=c.GetIndex(ind,cath);
1462             Int_t ql=fInput->Digit(cath, ist)->Signal();
1463             Int_t ix=fInput->Digit(cath, ist)->PadX();
1464             Int_t iy=fInput->Digit(cath, ist)->PadY();
1465             
1466             if (q>ql || (q==ql && theX > ix && theY < iy)) {
1467                 c.SetIndex(ind, cath, idx);
1468                 c.SetIndex(ind+1, cath, ist);
1469             } else {
1470                 
1471                 break;
1472             }
1473         }
1474     }
1475
1476     c.SetMultiplicity(cath, c.GetMultiplicity(cath)+1);
1477     if (c.GetMultiplicity(cath) >= 50 ) {
1478       AliDebug(1,Form("FindCluster - multiplicity >50  %d \n",c.GetMultiplicity(0)));
1479         c.SetMultiplicity(cath, 49);
1480     }
1481
1482 // Prepare center of gravity calculation
1483     Float_t x, y, z;
1484     if (fSegmentationType == 1) 
1485       fSeg[cath]->GetPadC(i, j, x, y, z);
1486     else
1487       fSeg2[cath]->GetPadC(fInput->DetElemId(), i, j, x, y, z);
1488     c.AddX(cath,q*x);
1489     c.AddY(cath,q*y);
1490     c.AddCharge(cath,q);
1491 //
1492 // Flag hit as "taken"  
1493     fHitMap[cath]->FlagHit(i,j);
1494 //
1495 //  Now look recursively for all neighbours and pad hit on opposite cathode
1496 //
1497 //  Loop over neighbours
1498     Int_t ix,iy;
1499     ix=iy=0;
1500     Int_t nn;
1501     Int_t xList[10], yList[10];
1502     if (fSegmentationType == 1) 
1503       fSeg[cath]->Neighbours(i,j,&nn,xList,yList);
1504     else 
1505       fSeg2[cath]->Neighbours(fInput->DetElemId(), i,j,&nn,xList,yList);
1506     for (Int_t in=0; in<nn; in++) {
1507         ix=xList[in];
1508         iy=yList[in];
1509         
1510         if (fHitMap[cath]->TestHit(ix,iy)==kUnused) {
1511             AliDebug(2,Form("\n Neighbours %d %d %d", cath, ix, iy));
1512             FindCluster(ix, iy, cath, c);
1513         }
1514         
1515    }
1516     Int_t nOpp=0;
1517     Int_t iXopp[50], iYopp[50];
1518     
1519 //  Neighbours on opposite cathode 
1520 //  Take into account that several pads can overlap with the present pad
1521     Int_t isec;
1522     if (fSegmentationType == 1) 
1523       isec=fSeg[cath]->Sector(i,j);    
1524     else
1525       isec=fSeg2[cath]->Sector(fInput->DetElemId(), i,j);    
1526
1527     Int_t iop;
1528     Float_t dx, dy;
1529
1530     if (fSegmentationType == 1) {
1531       if (cath==0) {
1532         iop = 1;
1533         dx  = (fSeg[cath]->Dpx(isec))/2.;
1534         dy  = 0.;
1535       } else {
1536         iop = 0;
1537         dx  = 0.;
1538         dy  = (fSeg[cath]->Dpy(isec))/2;
1539       }
1540    
1541
1542     
1543       // loop over pad neighbours on opposite cathode
1544       for (fSeg[iop]->FirstPad(x, y, fZPlane, dx, dy);
1545            fSeg[iop]->MorePads();
1546            fSeg[iop]->NextPad())
1547         {
1548         
1549           ix = fSeg[iop]->Ix(); iy = fSeg[iop]->Iy();
1550           AliDebug(2,Form("\n ix, iy: %f %f %f %d %d %d", x,y,z,ix, iy, fSector));
1551           if (fHitMap[iop]->TestHit(ix,iy)==kUnused){
1552             iXopp[nOpp]=ix;
1553             iYopp[nOpp++]=iy;
1554             AliDebug(2,Form("\n Opposite %d %d %d", iop, ix, iy));
1555           }
1556         
1557         } // Loop over pad neighbours
1558       //  This had to go outside the loop since recursive calls inside the iterator are not possible
1559       //
1560       Int_t jopp;
1561       for (jopp=0; jopp<nOpp; jopp++) {
1562         if (fHitMap[iop]->TestHit(iXopp[jopp],iYopp[jopp]) == kUnused) 
1563           FindCluster(iXopp[jopp], iYopp[jopp], iop, c);
1564       }
1565     } else {
1566
1567       if (cath==0) {
1568         iop = 1;
1569         dx  = (fSeg2[cath]->Dpx(fInput->DetElemId(), isec))/2.;
1570         dy  = 0.;
1571       } else {
1572         iop = 0;
1573         dx  = 0.;
1574         dy  = (fSeg2[cath]->Dpy(fInput->DetElemId(), isec))/2;
1575       }
1576    
1577
1578     
1579       // loop over pad neighbours on opposite cathode
1580       for (fSeg2[iop]->FirstPad(fInput->DetElemId(), x, y, fZPlane, dx, dy);
1581            fSeg2[iop]->MorePads(fInput->DetElemId());
1582            fSeg2[iop]->NextPad(fInput->DetElemId()))
1583         {
1584         
1585           ix = fSeg2[iop]->Ix(); iy = fSeg2[iop]->Iy();
1586           AliDebug(2,Form("\n ix, iy: %f %f %f %d %d %d", x,y,z,ix, iy, fSector));
1587           if (fHitMap[iop]->TestHit(ix,iy)==kUnused){
1588             iXopp[nOpp]=ix;
1589             iYopp[nOpp++]=iy;
1590             AliDebug(2,Form("\n Opposite %d %d %d", iop, ix, iy));
1591           }
1592         
1593         } // Loop over pad neighbours
1594       //  This had to go outside the loop since recursive calls inside the iterator are not possible
1595       //
1596       Int_t jopp;
1597       for (jopp=0; jopp<nOpp; jopp++) {
1598         if (fHitMap[iop]->TestHit(iXopp[jopp],iYopp[jopp]) == kUnused) 
1599           FindCluster(iXopp[jopp], iYopp[jopp], iop, c);
1600       }
1601     }
1602 }
1603
1604 //_____________________________________________________________________________
1605
1606 void AliMUONClusterFinderVS::FindRawClusters()
1607 {
1608   //
1609   // MUON cluster finder from digits -- finds neighbours on both cathodes and 
1610   // fills the tree with raw clusters
1611   //
1612
1613     ResetRawClusters();
1614 //  Return if no input datad available
1615     if (!fInput->NDigits(0) && !fInput->NDigits(1)) return;
1616
1617     fSegmentationType = fInput->GetSegmentationType();
1618
1619     if (fSegmentationType == 1) {
1620       fSeg[0] = fInput->Segmentation(0);
1621       fSeg[1] = fInput->Segmentation(1);
1622
1623       fHitMap[0]  = new AliMUONHitMapA1(fSeg[0], fInput->Digits(0));
1624       fHitMap[1]  = new AliMUONHitMapA1(fSeg[1], fInput->Digits(1));
1625
1626     } else {
1627       fSeg2[0] = fInput->Segmentation2(0);
1628       fSeg2[1] = fInput->Segmentation2(1);
1629
1630       fHitMap[0]  = new AliMUONHitMapA1(fInput->DetElemId(), fSeg2[0], fInput->Digits(0));
1631       fHitMap[1]  = new AliMUONHitMapA1(fInput->DetElemId(), fSeg2[1], fInput->Digits(1));
1632     }
1633  
1634     AliMUONDigit *dig;
1635
1636     Int_t ndig, cath;
1637     Int_t nskip=0;
1638     Int_t ncls=0;
1639     fHitMap[0]->FillHits();
1640     fHitMap[1]->FillHits();
1641 //
1642 //  Outer Loop over Cathodes
1643     for (cath=0; cath<2; cath++) {
1644
1645         for (ndig=0; ndig<fInput->NDigits(cath); ndig++) {
1646             dig = fInput->Digit(cath, ndig);
1647             Int_t padx = dig->PadX();
1648             Int_t pady = dig->PadY();
1649             if (fHitMap[cath]->TestHit(padx,pady)==kUsed ||fHitMap[0]->TestHit(padx,pady)==kEmpty) {
1650                 nskip++;
1651                 continue;
1652             }
1653             AliDebug(1,Form("\n CATHODE %d CLUSTER %d\n",cath,ncls));
1654             AliMUONRawCluster clus;
1655             clus.SetMultiplicity(0, 0);
1656             clus.SetMultiplicity(1, 0);
1657             clus.SetPeakSignal(cath,dig->Signal());
1658             clus.SetTrack(0, dig->Hit());
1659             clus.SetTrack(1, dig->Track(0));
1660             clus.SetTrack(2, dig->Track(1));
1661
1662             AliDebug(1,Form("idDE %d Padx %d Pady %d", fInput->DetElemId(), padx, pady));
1663
1664             // tag the beginning of cluster list in a raw cluster
1665             clus.SetNcluster(0,-1);
1666             Float_t xcu, ycu;
1667             if (fSegmentationType == 1) {
1668               fSeg[cath]->GetPadC(padx,pady, xcu, ycu, fZPlane);
1669               fSector= fSeg[cath]->Sector(padx,pady)/100;
1670             } else {
1671               fSeg2[cath]->GetPadC(fInput->DetElemId(), padx, pady, xcu, ycu, fZPlane);
1672               fSector= fSeg2[cath]->Sector(fInput->DetElemId(), padx, pady)/100;
1673             }
1674
1675
1676             
1677             FindCluster(padx,pady,cath,clus);
1678 //          ^^^^^^^^^^^^^^^^^^^^^^^^
1679             // center of gravity
1680             if (clus.GetX(0)!=0.) clus.SetX(0, clus.GetX(0)/clus.GetCharge(0)); // clus.fX[0] /= clus.fQ[0];
1681
1682             // Force on anod
1683             if (fSegmentationType == 1) 
1684               clus.SetX(0,fSeg[0]->GetAnod(clus.GetX(0)));
1685             else 
1686               clus.SetX(0,fSeg2[0]->GetAnod(fInput->DetElemId(), clus.GetX(0)));
1687             if (clus.GetY(0)!=0.) clus.SetY(0, clus.GetY(0)/clus.GetCharge(0)); // clus.fY[0] /= clus.fQ[0];
1688             
1689             if(clus.GetCharge(1)!=0.) clus.SetX(1, clus.GetX(1)/clus.GetCharge(1));  // clus.fX[1] /= clus.fQ[1];
1690                                         
1691            // Force on anod
1692             if (fSegmentationType == 1) 
1693               clus.SetX(1, fSeg[0]->GetAnod(clus.GetX(1)));
1694             else 
1695               clus.SetX(1, fSeg2[0]->GetAnod(fInput->DetElemId(),clus.GetX(1)));
1696             if(clus.GetCharge(1)!=0.) clus.SetY(1, clus.GetY(1)/clus.GetCharge(1));// clus.fY[1] /= clus.fQ[1];
1697             
1698             clus.SetZ(0, fZPlane);
1699             clus.SetZ(1, fZPlane);          
1700
1701                 AliDebug(1,Form("\n Cathode 1 multiplicite %d X(CG) %f Y(CG) %f\n",
1702                         clus.GetMultiplicity(0),clus.GetX(0),clus.GetY(0)));
1703                 AliDebug(1,Form(" Cathode 2 multiplicite %d X(CG) %f Y(CG) %f\n",
1704                         clus.GetMultiplicity(1),clus.GetX(1),clus.GetY(1)));
1705 //      Analyse cluster and decluster if necessary
1706 //      
1707         ncls++;
1708         clus.SetNcluster(1,fNRawClusters);
1709         clus.SetClusterType(clus.PhysicsContribution());
1710
1711         fNPeaks=0;
1712 //
1713 //
1714         Decluster(&clus);
1715 //
1716 //      reset Cluster object
1717         { // begin local scope
1718             for (int k=0;k<clus.GetMultiplicity(0);k++) clus.SetIndex(k, 0, 0);
1719         } // end local scope
1720
1721         { // begin local scope
1722             for (int k=0;k<clus.GetMultiplicity(1);k++) clus.SetIndex(k, 1, 0);
1723         } // end local scope
1724         
1725         clus.SetMultiplicity(0,0);
1726         clus.SetMultiplicity(1,0);
1727
1728         
1729         } // end loop ndig
1730     } // end loop cathodes
1731     delete fHitMap[0];
1732     delete fHitMap[1];
1733 }
1734
1735 Float_t AliMUONClusterFinderVS::SingleMathiesonFit(AliMUONRawCluster *c, Int_t cath)
1736 {
1737 // Performs a single Mathieson fit on one cathode
1738 // 
1739     Double_t arglist[20];
1740     Int_t ierflag=0;
1741     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1742     
1743     clusterInput.Fitter()->SetFCN(fcnS1);
1744     clusterInput.Fitter()->mninit(2,10,7);
1745     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1746     arglist[0]=-1;
1747     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1748 // Set starting values 
1749     static Double_t vstart[2];
1750     vstart[0]=c->GetX(1);
1751     vstart[1]=c->GetY(0);
1752     
1753     
1754 // lower and upper limits
1755     static Double_t lower[2], upper[2];
1756     Int_t ix,iy, isec;
1757     if (fSegmentationType == 1) {
1758       fSeg[cath]->GetPadI(c->GetX(cath), c->GetY(cath), fZPlane, ix, iy);
1759       isec=fSeg[cath]->Sector(ix, iy);
1760
1761       lower[0]=vstart[0]-fSeg[cath]->Dpx(isec)/2;
1762       lower[1]=vstart[1]-fSeg[cath]->Dpy(isec)/2;
1763     
1764       upper[0]=lower[0]+fSeg[cath]->Dpx(isec);
1765       upper[1]=lower[1]+fSeg[cath]->Dpy(isec);
1766
1767     } else {
1768       fSeg2[cath]->GetPadI(fInput->DetElemId(), c->GetX(cath), c->GetY(cath), fZPlane, ix, iy);
1769       isec=fSeg2[cath]->Sector(fInput->DetElemId(), ix, iy);
1770
1771       lower[0]=vstart[0]-fSeg2[cath]->Dpx(fInput->DetElemId(), isec)/2;
1772       lower[1]=vstart[1]-fSeg2[cath]->Dpy(fInput->DetElemId(), isec)/2;
1773     
1774       upper[0]=lower[0]+fSeg2[cath]->Dpx(fInput->DetElemId(), isec);
1775       upper[1]=lower[1]+fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1776     }
1777
1778 // step sizes
1779     static Double_t step[2]={0.0005, 0.0005};
1780     
1781     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
1782     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
1783 // ready for minimisation       
1784     arglist[0]= -1;
1785     arglist[1]= 0;
1786     
1787     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
1788     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
1789     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
1790     Double_t fmin, fedm, errdef;
1791     Int_t   npari, nparx, istat;
1792       
1793     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
1794     fFitStat=istat;
1795     
1796 // Print results
1797 // Get fitted parameters
1798     Double_t xrec, yrec;
1799     TString chname;
1800     Double_t epxz, b1, b2;
1801     Int_t ierflg;
1802     clusterInput.Fitter()->mnpout(0, chname, xrec, epxz, b1, b2, ierflg);       
1803     clusterInput.Fitter()->mnpout(1, chname, yrec, epxz, b1, b2, ierflg);       
1804     fXFit[cath]=xrec;
1805     fYFit[cath]=yrec;
1806     return fmin;
1807 }
1808
1809 Float_t AliMUONClusterFinderVS::CombiSingleMathiesonFit(AliMUONRawCluster * /*c*/)
1810 {
1811 // Perform combined Mathieson fit on both cathode planes
1812 //
1813     Double_t arglist[20];
1814     Int_t ierflag=0;
1815     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1816     clusterInput.Fitter()->SetFCN(fcnCombiS1);
1817     clusterInput.Fitter()->mninit(2,10,7);
1818     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1819     arglist[0]=-1;
1820     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1821     static Double_t vstart[2];
1822     vstart[0]=fXInit[0];
1823     vstart[1]=fYInit[0];
1824     
1825     
1826 // lower and upper limits
1827     static Float_t lower[2], upper[2];
1828     Int_t ix,iy,isec;
1829     Float_t dpy, dpx;
1830
1831     if (fSegmentationType == 1) {
1832       fSeg[0]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
1833       isec=fSeg[0]->Sector(ix, iy);
1834       dpy=fSeg[0]->Dpy(isec);
1835       fSeg[1]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
1836       isec=fSeg[1]->Sector(ix, iy);
1837       dpx=fSeg[1]->Dpx(isec);
1838
1839     } else {
1840       fSeg2[0]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
1841       isec=fSeg2[0]->Sector(fInput->DetElemId(), ix, iy);
1842       dpy=fSeg2[0]->Dpy(fInput->DetElemId(), isec);
1843       fSeg2[1]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
1844       isec=fSeg2[1]->Sector(fInput->DetElemId(), ix, iy);
1845       dpx=fSeg2[1]->Dpx(fInput->DetElemId(), isec);
1846
1847     }
1848     Int_t icount;
1849     Float_t xdum, ydum, zdum;
1850
1851 //  Find save upper and lower limits    
1852     
1853     icount = 0;
1854     if (fSegmentationType == 1) {
1855       for (fSeg[1]->FirstPad(fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
1856            fSeg[1]->MorePads(); 
1857            fSeg[1]->NextPad())
1858         {
1859           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
1860           fSeg[1]->GetPadC(ix,iy, upper[0], ydum, zdum);        
1861           if (icount ==0) lower[0]=upper[0];
1862           icount++;
1863         }
1864     } else {
1865       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
1866            fSeg2[1]->MorePads(fInput->DetElemId()); 
1867            fSeg2[1]->NextPad(fInput->DetElemId()))
1868         {
1869           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
1870           fSeg2[1]->GetPadC(fInput->DetElemId(), ix,iy, upper[0], ydum, zdum);  
1871           if (icount ==0) lower[0]=upper[0];
1872           icount++;
1873         }
1874     }
1875     if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}
1876         
1877     icount=0;
1878     AliDebug(1,Form("\n single y %f %f", fXInit[0], fYInit[0]));
1879     
1880     if (fSegmentationType == 1) {
1881       for (fSeg[0]->FirstPad(fXInit[0], fYInit[0], fZPlane, 0., dpy); 
1882            fSeg[0]->MorePads(); 
1883            fSeg[0]->NextPad())
1884         {
1885           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
1886           fSeg[0]->GetPadC(ix,iy,xdum,upper[1],zdum);   
1887           if (icount ==0) lower[1]=upper[1];
1888           icount++;
1889           AliDebug(1,Form("\n upper lower %d %f %f", icount, upper[1], lower[1]));
1890         }
1891     } else {
1892       for (fSeg2[0]->FirstPad(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, 0., dpy); 
1893            fSeg2[0]->MorePads(fInput->DetElemId()); 
1894            fSeg2[0]->NextPad(fInput->DetElemId()))
1895         {
1896           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
1897           fSeg2[0]->GetPadC(fInput->DetElemId(), ix,iy,xdum,upper[1],zdum);     
1898           if (icount ==0) lower[1]=upper[1];
1899           icount++;
1900           AliDebug(1,Form("\n upper lower %d %f %f", icount, upper[1], lower[1]));
1901         }
1902     }
1903     if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}
1904
1905 // step sizes
1906     static Double_t step[2]={0.00001, 0.0001};
1907     
1908     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
1909     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
1910 // ready for minimisation       
1911     arglist[0]= -1;
1912     arglist[1]= 0;
1913     
1914     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
1915     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
1916     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
1917     Double_t fmin, fedm, errdef;
1918     Int_t   npari, nparx, istat;
1919       
1920     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
1921     fFitStat=istat;
1922     
1923 // Print results
1924 // Get fitted parameters
1925     Double_t xrec, yrec;
1926     TString chname;
1927     Double_t epxz, b1, b2;
1928     Int_t ierflg;
1929     clusterInput.Fitter()->mnpout(0, chname, xrec, epxz, b1, b2, ierflg);       
1930     clusterInput.Fitter()->mnpout(1, chname, yrec, epxz, b1, b2, ierflg);       
1931     fXFit[0]=xrec;
1932     fYFit[0]=yrec;
1933     return fmin;
1934 }
1935
1936 Bool_t AliMUONClusterFinderVS::DoubleMathiesonFit(AliMUONRawCluster * /*c*/, Int_t cath)
1937 {
1938 // Performs a double Mathieson fit on one cathode
1939 // 
1940
1941 //
1942 //  Initialise global variables for fit
1943     Double_t arglist[20];
1944     Int_t ierflag=0;
1945     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1946     clusterInput.Fitter()->SetFCN(fcnS2);
1947     clusterInput.Fitter()->mninit(5,10,7);
1948     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1949     arglist[0]=-1;
1950     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1951 // Set starting values 
1952     static Double_t vstart[5];
1953     vstart[0]=fX[fIndLocal[0][cath]][cath];
1954     vstart[1]=fY[fIndLocal[0][cath]][cath];     
1955     vstart[2]=fX[fIndLocal[1][cath]][cath];
1956     vstart[3]=fY[fIndLocal[1][cath]][cath];     
1957     vstart[4]=Float_t(fQ[fIndLocal[0][cath]][cath])/
1958         Float_t(fQ[fIndLocal[0][cath]][cath]+fQ[fIndLocal[1][cath]][cath]);
1959 // lower and upper limits
1960     static Float_t lower[5], upper[5];
1961     Int_t isec;
1962
1963     if (fSegmentationType == 1) {
1964       isec=fSeg[cath]->Sector(fIx[fIndLocal[0][cath]][cath], fIy[fIndLocal[0][cath]][cath]);
1965       lower[0]=vstart[0]-fSeg[cath]->Dpx(isec);
1966       lower[1]=vstart[1]-fSeg[cath]->Dpy(isec);
1967     
1968       upper[0]=lower[0]+2.*fSeg[cath]->Dpx(isec);
1969       upper[1]=lower[1]+2.*fSeg[cath]->Dpy(isec);
1970     
1971       isec=fSeg[cath]->Sector(fIx[fIndLocal[1][cath]][cath], fIy[fIndLocal[1][cath]][cath]);
1972       lower[2]=vstart[2]-fSeg[cath]->Dpx(isec)/2;
1973       lower[3]=vstart[3]-fSeg[cath]->Dpy(isec)/2;
1974     
1975       upper[2]=lower[2]+fSeg[cath]->Dpx(isec);
1976       upper[3]=lower[3]+fSeg[cath]->Dpy(isec);
1977
1978     } else {
1979       isec=fSeg2[cath]->Sector(fInput->DetElemId(),fIx[fIndLocal[0][cath]][cath], 
1980                                fIy[fIndLocal[0][cath]][cath]);
1981       lower[0]=vstart[0]-fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1982       lower[1]=vstart[1]-fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1983     
1984       upper[0]=lower[0]+2.*fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1985       upper[1]=lower[1]+2.*fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1986     
1987       isec=fSeg2[cath]->Sector(fInput->DetElemId(),fIx[fIndLocal[1][cath]][cath], 
1988                               fIy[fIndLocal[1][cath]][cath]);
1989       lower[2]=vstart[2]-fSeg2[cath]->Dpx(fInput->DetElemId(),isec)/2;
1990       lower[3]=vstart[3]-fSeg2[cath]->Dpy(fInput->DetElemId(),isec)/2;
1991     
1992       upper[2]=lower[2]+fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1993       upper[1]=lower[1]+2.*fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1994
1995     }
1996
1997     lower[4]=0.;
1998     upper[4]=1.;
1999 // step sizes
2000     static Double_t step[5]={0.0005, 0.0005, 0.0005, 0.0005, 0.0001};
2001     
2002     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
2003     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
2004     clusterInput.Fitter()->mnparm(2,"x2",vstart[2],step[2],lower[2],upper[2],ierflag);
2005     clusterInput.Fitter()->mnparm(3,"y2",vstart[3],step[3],lower[3],upper[3],ierflag);
2006     clusterInput.Fitter()->mnparm(4,"a0",vstart[4],step[4],lower[4],upper[4],ierflag);
2007 // ready for minimisation       
2008     arglist[0]= -1;
2009     arglist[1]= 0;
2010     
2011     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
2012     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
2013     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
2014 // Get fitted parameters
2015     Double_t xrec[2], yrec[2], qfrac;
2016     TString chname;
2017     Double_t epxz, b1, b2;
2018     Int_t ierflg;
2019     clusterInput.Fitter()->mnpout(0, chname, xrec[0], epxz, b1, b2, ierflg);    
2020     clusterInput.Fitter()->mnpout(1, chname, yrec[0], epxz, b1, b2, ierflg);    
2021     clusterInput.Fitter()->mnpout(2, chname, xrec[1], epxz, b1, b2, ierflg);    
2022     clusterInput.Fitter()->mnpout(3, chname, yrec[1], epxz, b1, b2, ierflg);    
2023     clusterInput.Fitter()->mnpout(4, chname, qfrac,   epxz, b1, b2, ierflg);    
2024
2025     Double_t fmin, fedm, errdef;
2026     Int_t   npari, nparx, istat;
2027       
2028     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
2029     fFitStat=istat;
2030     return kTRUE;
2031 }
2032
2033 Float_t AliMUONClusterFinderVS::CombiDoubleMathiesonFit(AliMUONRawCluster * /*c*/)
2034 {
2035 //
2036 // Perform combined double Mathieson fit on both cathode planes
2037 //
2038     Double_t arglist[20];
2039     Int_t ierflag=0;
2040     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
2041     clusterInput.Fitter()->SetFCN(fcnCombiS2);
2042     clusterInput.Fitter()->mninit(6,10,7);
2043     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
2044     arglist[0]=-1;
2045     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
2046 // Set starting values 
2047     static Double_t vstart[6];
2048     vstart[0]=fXInit[0];
2049     vstart[1]=fYInit[0];
2050     vstart[2]=fXInit[1];
2051     vstart[3]=fYInit[1];
2052     vstart[4]=fQrInit[0];
2053     vstart[5]=fQrInit[1];
2054 // lower and upper limits
2055     static Float_t lower[6], upper[6];
2056     Int_t ix,iy,isec;
2057     Float_t dpx, dpy;
2058   if (fSegmentationType == 1) {
2059     fSeg[1]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
2060     isec=fSeg[1]->Sector(ix, iy);
2061     dpx=fSeg[1]->Dpx(isec);
2062
2063     fSeg[0]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
2064     isec=fSeg[0]->Sector(ix, iy);
2065     dpy=fSeg[0]->Dpy(isec);
2066
2067   } else {
2068     fSeg2[1]->GetPadI(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, ix, iy);
2069     isec=fSeg2[1]->Sector(fInput->DetElemId(),ix, iy);
2070     dpx=fSeg2[1]->Dpx(fInput->DetElemId(), isec);
2071
2072     fSeg2[0]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
2073     isec=fSeg2[0]->Sector(fInput->DetElemId(), ix, iy);
2074     dpy=fSeg2[0]->Dpy(fInput->DetElemId(), isec);
2075
2076   }
2077
2078     Int_t icount;
2079     Float_t xdum, ydum, zdum;
2080     AliDebug(1,Form("\n Cluster Finder: %f %f %f %f  ", fXInit[0], fXInit[1],fYInit[0], fYInit[1] ));
2081
2082     if (fSegmentationType == 1) {
2083  
2084       //  Find save upper and lower limits    
2085       icount = 0;
2086     
2087       for (fSeg[1]->FirstPad(fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
2088            fSeg[1]->MorePads(); 
2089            fSeg[1]->NextPad())
2090         {
2091           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
2092           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2093           fSeg[1]->GetPadC(ix,iy,upper[0],ydum,zdum);   
2094           if (icount ==0) lower[0]=upper[0];
2095           icount++;
2096         }
2097       if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}    
2098       //    vstart[0] = 0.5*(lower[0]+upper[0]);
2099
2100     
2101       icount=0;
2102     
2103       for (fSeg[0]->FirstPad(fXInit[0], fYInit[0], fZPlane, 0., dpy); 
2104            fSeg[0]->MorePads(); 
2105            fSeg[0]->NextPad())
2106         {
2107           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
2108           //    if (fHitMap[0]->TestHit(ix, iy) == kEmpty) continue;
2109           fSeg[0]->GetPadC(ix,iy,xdum,upper[1],zdum);   
2110           if (icount ==0) lower[1]=upper[1];
2111           icount++;
2112         }
2113     
2114       if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}    
2115       //     vstart[1] = 0.5*(lower[1]+upper[1]);
2116
2117
2118       fSeg[1]->GetPadI(fXInit[1], fYInit[1], fZPlane, ix, iy);
2119       isec=fSeg[1]->Sector(ix, iy);
2120       dpx=fSeg[1]->Dpx(isec);
2121       fSeg[0]->GetPadI(fXInit[1], fYInit[1], fZPlane, ix, iy);
2122       isec=fSeg[0]->Sector(ix, iy);
2123       dpy=fSeg[0]->Dpy(isec);
2124
2125
2126       //  Find save upper and lower limits    
2127
2128       icount=0;
2129     
2130       for (fSeg[1]->FirstPad(fXInit[1], fYInit[1], fZPlane, dpx, 0); 
2131            fSeg[1]->MorePads(); fSeg[1]->NextPad())
2132         {
2133           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
2134           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2135           fSeg[1]->GetPadC(ix,iy,upper[2],ydum,zdum);   
2136           if (icount ==0) lower[2]=upper[2];
2137           icount++;
2138         }
2139       if (lower[2]>upper[2]) {xdum=lower[2]; lower[2]=upper[2]; upper[2]=xdum;}    
2140       //    vstart[2] = 0.5*(lower[2]+upper[2]);
2141
2142       icount=0;
2143     
2144       for (fSeg[0]->FirstPad(fXInit[1], fYInit[1], fZPlane, 0, dpy); 
2145            fSeg[0]-> MorePads(); fSeg[0]->NextPad())
2146         {
2147           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
2148           //    if (fHitMap[0]->TestHit(ix, iy) != kEmpty) continue;
2149         
2150           fSeg[0]->GetPadC(ix,iy,xdum,upper[3],zdum);   
2151           if (icount ==0) lower[3]=upper[3];
2152           icount++;
2153
2154         }
2155       if (lower[3]>upper[3]) {xdum=lower[3]; lower[3]=upper[3]; upper[3]=xdum;}    
2156     
2157       //     vstart[3] = 0.5*(lower[3]+upper[3]);
2158     } else {
2159
2160     //  Find save upper and lower limits    
2161       icount = 0;
2162     
2163       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
2164            fSeg2[1]->MorePads(fInput->DetElemId()); 
2165            fSeg2[1]->NextPad(fInput->DetElemId()))
2166         {
2167           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
2168           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2169           fSeg2[1]->GetPadC(fInput->DetElemId(),ix,iy,upper[0],ydum,zdum);      
2170           if (icount ==0) lower[0]=upper[0];
2171           icount++;
2172         }
2173       if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}    
2174       //    vstart[0] = 0.5*(lower[0]+upper[0]);
2175
2176     
2177       icount=0;
2178     
2179       for (fSeg2[0]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, 0., dpy); 
2180            fSeg2[0]->MorePads(fInput->DetElemId()); 
2181            fSeg2[0]->NextPad(fInput->DetElemId()))
2182         {
2183           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
2184           //    if (fHitMap[0]->TestHit(ix, iy) == kEmpty) continue;
2185           fSeg2[0]->GetPadC(fInput->DetElemId(),ix,iy,xdum,upper[1],zdum);      
2186           if (icount ==0) lower[1]=upper[1];
2187           icount++;
2188         }
2189     
2190       if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}    
2191       //     vstart[1] = 0.5*(lower[1]+upper[1]);
2192
2193
2194       fSeg2[1]->GetPadI(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, ix, iy);
2195       isec=fSeg2[1]->Sector(fInput->DetElemId(),ix, iy);
2196       dpx=fSeg2[1]->Dpx(fInput->DetElemId(),isec);
2197       fSeg2[0]->GetPadI(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, ix, iy);
2198       isec=fSeg2[0]->Sector(fInput->DetElemId(),ix, iy);
2199       dpy=fSeg2[0]->Dpy(fInput->DetElemId(),isec);
2200
2201
2202       //  Find save upper and lower limits    
2203
2204       icount=0;
2205     
2206       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, dpx, 0); 
2207            fSeg2[1]->MorePads(fInput->DetElemId()); 
2208            fSeg2[1]->NextPad(fInput->DetElemId()))
2209         {
2210           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
2211           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2212           fSeg2[1]->GetPadC(fInput->DetElemId(),ix,iy,upper[2],ydum,zdum);      
2213           if (icount ==0) lower[2]=upper[2];
2214           icount++;
2215         }
2216       if (lower[2]>upper[2]) {xdum=lower[2]; lower[2]=upper[2]; upper[2]=xdum;}    
2217       //    vstart[2] = 0.5*(lower[2]+upper[2]);
2218
2219       icount=0;
2220     
2221       for (fSeg2[0]->FirstPad(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, 0, dpy); 
2222            fSeg2[0]-> MorePads(fInput->DetElemId()); 
2223            fSeg2[0]->NextPad(fInput->DetElemId()))
2224         {
2225           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
2226           //    if (fHitMap[0]->TestHit(ix, iy) != kEmpty) continue;
2227         
2228           fSeg2[0]->GetPadC(fInput->DetElemId(),ix,iy,xdum,upper[3],zdum);      
2229           if (icount ==0) lower[3]=upper[3];
2230           icount++;
2231
2232         }
2233       if (lower[3]>upper[3]) {xdum=lower[3]; lower[3]=upper[3]; upper[3]=xdum;}  
2234     }
2235     lower[4]=0.;
2236     upper[4]=1.;
2237     lower[5]=0.;
2238     upper[5]=1.;
2239
2240 // step sizes
2241     static Double_t step[6]={0.0005, 0.0005, 0.0005, 0.0005, 0.001, 0.001};
2242     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
2243     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
2244     clusterInput.Fitter()->mnparm(2,"x2",vstart[2],step[2],lower[2],upper[2],ierflag);
2245     clusterInput.Fitter()->mnparm(3,"y2",vstart[3],step[3],lower[3],upper[3],ierflag);
2246     clusterInput.Fitter()->mnparm(4,"a0",vstart[4],step[4],lower[4],upper[4],ierflag);
2247     clusterInput.Fitter()->mnparm(5,"a1",vstart[5],step[5],lower[5],upper[5],ierflag);
2248 // ready for minimisation       
2249     arglist[0]= -1;
2250     arglist[1]= 0;
2251     
2252     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
2253     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
2254     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
2255 // Get fitted parameters
2256     TString chname;
2257     Double_t epxz, b1, b2;
2258     Int_t ierflg;
2259     clusterInput.Fitter()->mnpout(0, chname, fXFit[0],  epxz, b1, b2, ierflg);  
2260     clusterInput.Fitter()->mnpout(1, chname, fYFit[0],  epxz, b1, b2, ierflg);  
2261     clusterInput.Fitter()->mnpout(2, chname, fXFit[1],  epxz, b1, b2, ierflg);  
2262     clusterInput.Fitter()->mnpout(3, chname, fYFit[1],  epxz, b1, b2, ierflg);  
2263     clusterInput.Fitter()->mnpout(4, chname, fQrFit[0], epxz, b1, b2, ierflg);  
2264     clusterInput.Fitter()->mnpout(5, chname, fQrFit[1], epxz, b1, b2, ierflg);  
2265
2266     Double_t fmin, fedm, errdef;
2267     Int_t   npari, nparx, istat;
2268       
2269     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
2270     fFitStat=istat;
2271     
2272     fChi2[0]=fmin;
2273     fChi2[1]=fmin;
2274     return fmin;
2275 }
2276
2277 void AliMUONClusterFinderVS::Split(AliMUONRawCluster* c)
2278 {
2279 //
2280 // One cluster for each maximum
2281 //
2282     Int_t i, j, cath;
2283     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
2284     for (j=0; j<2; j++) {
2285         AliMUONRawCluster cnew;
2286         cnew.SetGhost(c->GetGhost());
2287         for (cath=0; cath<2; cath++) {
2288             cnew.SetChi2(cath,fChi2[0]);
2289             // ?? why not cnew.fChi2[cath]=fChi2[cath];
2290             
2291             if (fNPeaks == 0) {
2292                 cnew.SetNcluster(0,-1);
2293                 cnew.SetNcluster(1,fNRawClusters);
2294             } else {
2295                 cnew.SetNcluster(0,fNPeaks);
2296                 cnew.SetNcluster(1,0);
2297             }
2298             cnew.SetMultiplicity(cath,0);
2299             cnew.SetX(cath, Float_t(fXFit[j]));
2300             cnew.SetY(cath, Float_t(fYFit[j]));
2301             cnew.SetZ(cath, fZPlane);
2302             if (j==0) {
2303                 cnew.SetCharge(cath, Int_t(clusterInput.TotalCharge(cath)*fQrFit[cath]));
2304             } else {
2305                 cnew.SetCharge(cath, Int_t(clusterInput.TotalCharge(cath)*(1-fQrFit[cath])));
2306             }
2307             if (fSegmentationType == 1) 
2308               fSeg[cath]->SetHit(fXFit[j],fYFit[j],fZPlane);
2309             else 
2310               fSeg2[cath]->SetHit(fInput->DetElemId(), fXFit[j],fYFit[j],fZPlane);
2311
2312             for (i=0; i<fMul[cath]; i++) {
2313               Float_t q1;
2314                 cnew.SetIndex(cnew.GetMultiplicity(cath), cath, c->GetIndex(i,cath));
2315                 if (fSegmentationType == 1) {
2316                   fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
2317                   q1 = fInput->Mathieson()->IntXY(fSeg[cath]);
2318                 } else {
2319                   fSeg2[cath]->SetPad(fInput->DetElemId(),fIx[i][cath], fIy[i][cath]);
2320                   q1 = fInput->Mathieson()->IntXY(fInput->DetElemId(),fSeg2[cath]);
2321                 }
2322                 cnew.SetContrib(i, cath, q1*Float_t(cnew.GetCharge(cath))/Float_t(fQ[i][cath]));
2323                 cnew.SetMultiplicity(cath, cnew.GetMultiplicity(cath)+1 );
2324             }
2325             FillCluster(&cnew,0,cath);
2326         } // cathode loop
2327         
2328         cnew.SetClusterType(cnew.PhysicsContribution());
2329         if (cnew.GetCharge(0)>0 && cnew.GetCharge(1)>0) AddRawCluster(cnew);
2330         fNPeaks++;
2331     }
2332 }
2333 void AliMUONClusterFinderVS::AddRawCluster(const AliMUONRawCluster& c)
2334 {
2335   //
2336   // Add a raw cluster copy to the list
2337   //
2338
2339 //     AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2340 //     pMUON->GetMUONData()->AddRawCluster(fInput->Chamber(),c); 
2341 //     fNRawClusters++;
2342
2343   
2344     TClonesArray &lrawcl = *fRawClusters;
2345     new(lrawcl[fNRawClusters++]) AliMUONRawCluster(c);
2346     AliDebug(1,Form("\nfNRawClusters %d\n",fNRawClusters));
2347 }
2348
2349 AliMUONClusterFinderVS& AliMUONClusterFinderVS
2350 ::operator = (const AliMUONClusterFinderVS& rhs)
2351 {
2352 // Protected assignement operator
2353
2354   if (this == &rhs) return *this;
2355
2356   AliFatal("Not implemented.");
2357     
2358   return *this;  
2359 }
2360
2361 //
2362 // Minimisation functions
2363 // Single Mathieson
2364 void fcnS1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2365 {
2366     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2367     Int_t i;
2368     Float_t delta;
2369     Float_t chisq=0;
2370     Float_t qcont=0;
2371     Float_t qtot=0;
2372
2373     for (i=0; i<clusterInput.Nmul(0); i++) {
2374         Float_t q0=clusterInput.Charge(i,0);
2375         Float_t q1=clusterInput.DiscrChargeS1(i,par);
2376         delta=(q0-q1)/q0;
2377         chisq+=delta*delta;
2378         qcont+=q1;
2379         qtot+=q0;
2380     }
2381     f=chisq;
2382 }
2383
2384 void fcnCombiS1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2385 {
2386     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2387     Int_t i, cath;
2388     Float_t delta;
2389     Float_t chisq=0;
2390     Float_t qcont=0;
2391     Float_t qtot=0;
2392
2393     for (cath=0; cath<2; cath++) {
2394         for (i=0; i<clusterInput.Nmul(cath); i++) {
2395             Float_t q0=clusterInput.Charge(i,cath);
2396             Float_t q1=clusterInput.DiscrChargeCombiS1(i,par,cath);
2397             delta=(q0-q1)/q0;
2398             chisq+=delta*delta;
2399             qcont+=q1;
2400             qtot+=q0;
2401         }
2402     }
2403     f=chisq;
2404 }
2405
2406 // Double Mathieson
2407 void fcnS2(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2408 {
2409     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2410     Int_t i;
2411     Float_t delta;
2412     Float_t chisq=0;
2413     Float_t qcont=0;
2414     Float_t qtot=0;
2415     
2416     for (i=0; i<clusterInput.Nmul(0); i++) {
2417
2418         Float_t q0=clusterInput.Charge(i,0);
2419         Float_t q1=clusterInput.DiscrChargeS2(i,par);
2420         delta=(q0-q1)/q0;
2421         chisq+=delta*delta;
2422         qcont+=q1;
2423         qtot+=q0;
2424     }
2425     f=chisq;
2426 }
2427
2428 // Double Mathieson
2429 void fcnCombiS2(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2430 {
2431     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2432     Int_t i, cath;
2433     Float_t delta;
2434     Float_t chisq=0;
2435     Float_t qcont=0;
2436     Float_t qtot=0;
2437     for (cath=0; cath<2; cath++) {
2438         for (i=0; i<clusterInput.Nmul(cath); i++) {
2439             Float_t q0=clusterInput.Charge(i,cath);
2440             Float_t q1=clusterInput.DiscrChargeCombiS2(i,par,cath);
2441             delta=(q0-q1)/q0;
2442             chisq+=delta*delta;
2443             qcont+=q1;
2444             qtot+=q0;
2445         }
2446     }
2447     f=chisq;
2448 }