]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONClusterFinderVS.cxx
Default wise used dummy mapping for new segmentation (tmp solution) (Christian)
[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         //      c->SetDetElementID(fInput->DetElemId());
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                                 cnew.SetMultiplicity(cath,c->GetMultiplicity(cath));
460                                 for (i=0; i<fMul[cath]; i++) {
461                                   cnew.SetIndex(i, cath, c->GetIndex(i,cath));
462                                   if (fSegmentationType == 1) 
463                                     fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
464                                   else 
465                                     fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
466                                 }
467                                 AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
468                                 AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
469                                 FillCluster(&cnew,cath);
470                             } 
471                             cnew.SetClusterType(cnew.PhysicsContribution());
472                             AddRawCluster(cnew);
473                             fNPeaks++;
474                         }
475                     }
476                 }
477             }
478         }
479         
480 // ******* iacc = 2 *******
481 // Two combinations found between the 2 cathodes
482         if (iacc==2) {
483 // Was the same maximum taken twice
484             if ((accepted[0]&&accepted[1]) || (accepted[2]&&accepted[3])) {
485                 AliDebug(1,"\n Maximum taken twice !!!\n");
486
487 // Have a try !! with that
488                 if (accepted[0]&&accepted[3]) {
489                     fXInit[0]=xm[0][1];
490                     fYInit[0]=ym[0][0];
491                     fXInit[1]=xm[1][1];
492                     fYInit[1]=ym[1][0];
493                 } else {
494                     fXInit[0]=xm[2][1];
495                     fYInit[0]=ym[2][0];
496                     fXInit[1]=xm[3][1];
497                     fYInit[1]=ym[3][0];
498                 }
499                 AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
500                 chi2=CombiDoubleMathiesonFit(c);
501 //                  Int_t ndf = fgNbins[0]+fgNbins[1]-6;
502 //                  Float_t prob = TMath::Prob(chi2,ndf);
503 //                  prob2->Fill(prob);
504 //                  chi2_2->Fill(chi2);
505                 Split(c);
506                 
507             } else {
508 // No ghosts ! No Problems ! -  Perform one fit only !
509                 if (accepted[0]&&accepted[3]) {
510                     fXInit[0]=xm[0][1];
511                     fYInit[0]=ym[0][0];
512                     fXInit[1]=xm[3][1];
513                     fYInit[1]=ym[3][0];
514                 } else {
515                     fXInit[0]=xm[1][1];
516                     fYInit[0]=ym[1][0];
517                     fXInit[1]=xm[2][1];
518                     fYInit[1]=ym[2][0];
519                 }
520                 AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
521                 chi2=CombiDoubleMathiesonFit(c);
522 //                  Int_t ndf = fgNbins[0]+fgNbins[1]-6;
523 //                  Float_t prob = TMath::Prob(chi2,ndf);
524 //                  prob2->Fill(prob);
525 //                  chi2_2->Fill(chi2);
526                 AliDebug(1,Form(" chi2 %f\n",chi2));
527                 Split(c);
528             }
529             
530 // ******* iacc = 4 *******
531 // Four combinations found between the 2 cathodes
532 // Ghost !!
533         } else if (iacc==4) {
534 // Perform fits for the two possibilities !!    
535 // Accept if charges are compatible on both cathodes
536 // If none are compatible, keep everything
537             fXInit[0]=xm[0][1];
538             fYInit[0]=ym[0][0];
539             fXInit[1]=xm[3][1];
540             fYInit[1]=ym[3][0];
541             AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
542             chi2=CombiDoubleMathiesonFit(c);
543 //              Int_t ndf = fgNbins[0]+fgNbins[1]-6;
544 //              Float_t prob = TMath::Prob(chi2,ndf);
545 //              prob2->Fill(prob);
546 //              chi2_2->Fill(chi2);
547             AliDebug(1,Form(" chi2 %f\n",chi2));
548             // store results of fit and postpone decision
549             Double_t sXFit[2],sYFit[2],sQrFit[2];
550             Float_t sChi2[2];
551             for (Int_t i=0;i<2;i++) {
552                 sXFit[i]=fXFit[i];
553                 sYFit[i]=fYFit[i];
554                 sQrFit[i]=fQrFit[i];
555                 sChi2[i]=fChi2[i];
556             }
557             fXInit[0]=xm[1][1];
558             fYInit[0]=ym[1][0];
559             fXInit[1]=xm[2][1];
560             fYInit[1]=ym[2][0];
561             AliDebug(1,"\n cas (2) CombiDoubleMathiesonFit(c)\n");
562             chi2=CombiDoubleMathiesonFit(c);
563 //              ndf = fgNbins[0]+fgNbins[1]-6;
564 //              prob = TMath::Prob(chi2,ndf);
565 //              prob2->Fill(prob);
566 //              chi2_2->Fill(chi2);
567             AliDebug(1,Form(" chi2 %f\n",chi2));
568             // We have all informations to perform the decision
569             // Compute the chi2 for the 2 possibilities
570             Float_t chi2fi,chi2si,chi2f,chi2s;
571
572             chi2f = (TMath::Log(fInput->TotalCharge(0)*fQrFit[0]
573                   /  (fInput->TotalCharge(1)*fQrFit[1]) )
574                   / fInput->ChargeCorrel() );
575             chi2f *=chi2f;
576             chi2fi = (TMath::Log(fInput->TotalCharge(0)*(1-fQrFit[0])
577                   /  (fInput->TotalCharge(1)*(1-fQrFit[1])) )
578                   / fInput->ChargeCorrel() );
579             chi2f += chi2fi*chi2fi;
580
581             chi2s = (TMath::Log(fInput->TotalCharge(0)*sQrFit[0]
582                   /  (fInput->TotalCharge(1)*sQrFit[1]) )
583                   / fInput->ChargeCorrel() );
584             chi2s *=chi2s;
585             chi2si = (TMath::Log(fInput->TotalCharge(0)*(1-sQrFit[0])
586                   /  (fInput->TotalCharge(1)*(1-sQrFit[1])) )
587                   / fInput->ChargeCorrel() );
588             chi2s += chi2si*chi2si;
589
590             // usefull to store the charge matching chi2 in the cluster
591             // fChi2[0]=sChi2[1]=chi2f;
592             // fChi2[1]=sChi2[0]=chi2s;
593
594             if (chi2f<=fGhostChi2Cut && chi2s<=fGhostChi2Cut)
595                 c->SetGhost(1);
596             if   (chi2f>fGhostChi2Cut && chi2s>fGhostChi2Cut) {
597                 // we keep the ghost
598                 c->SetGhost(2);
599                 chi2s=-1;
600                 chi2f=-1;
601             }
602             if (chi2f<=fGhostChi2Cut)
603                 Split(c);
604             if (chi2s<=fGhostChi2Cut) {
605                 // retreive saved values
606                 for (Int_t i=0;i<2;i++) {
607                     fXFit[i]=sXFit[i];
608                     fYFit[i]=sYFit[i];
609                     fQrFit[i]=sQrFit[i];
610                     fChi2[i]=sChi2[i];
611                 }
612                 Split(c);
613             }
614             c->SetGhost(0);
615         }
616
617     } else if (fNLocal[0]==2 &&  fNLocal[1]==1) {
618 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
619 //  (3) Two local maxima on cathode 1 and one maximum on cathode 2 
620 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
621 //
622         Float_t xm[4][2], ym[4][2];
623         Float_t dpx, dpy, dx, dy;
624         Int_t ixm[4][2], iym[4][2];
625         Int_t isec, im1, ico;
626 //
627 //  Form the 2x2 combinations
628 //  0-0, 0-1, 1-0, 1-1  
629         ico=0;
630         for (im1=0; im1<2; im1++) {
631             xm[ico][0]=fX[fIndLocal[im1][0]][0];
632             ym[ico][0]=fY[fIndLocal[im1][0]][0];
633             xm[ico][1]=fX[fIndLocal[0][1]][1];
634             ym[ico][1]=fY[fIndLocal[0][1]][1];
635             
636             ixm[ico][0]=fIx[fIndLocal[im1][0]][0];
637             iym[ico][0]=fIy[fIndLocal[im1][0]][0];
638             ixm[ico][1]=fIx[fIndLocal[0][1]][1];
639             iym[ico][1]=fIy[fIndLocal[0][1]][1];
640             ico++;
641         }
642 // ico = 0 : first local maximum on cathodes 1 and 2
643 // ico = 1 : second local maximum on cathode 1 and first on cathode 2
644
645 // Analyse the combinations and keep those that are possible !
646 // For each combination check consistency in x and y    
647         Int_t iacc;
648         Bool_t accepted[4];
649         iacc=0;
650         // In case of staggering maxima are displaced by exactly half the pad-size in y. 
651         // We have to take into account the numerical precision in the consistency check;
652         
653         Float_t eps = 1.e-5;
654
655         for (ico=0; ico<2; ico++) {
656             accepted[ico]=kFALSE;
657             if (fSegmentationType == 1) {
658               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
659               dpx=fSeg[0]->Dpx(isec)/2.;
660             } else {
661               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
662               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
663             }
664             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
665             if (fSegmentationType == 1) {
666               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
667               dpy=fSeg[1]->Dpy(isec)/2.;
668             } else {
669               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
670               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
671             }
672             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
673             AliDebug(2,Form("\n %i %f %f %f %f \n", ico, ym[ico][0], ym[ico][1], dy, dpy ));
674             if ((dx <= dpx) && (dy <= dpy+eps)) {
675                 // consistent
676                 accepted[ico]=kTRUE;
677                 iacc++;
678             } else {
679                 // reject
680                 accepted[ico]=kFALSE;
681             }
682         }
683         
684         Float_t chi21 = 100;
685         Float_t chi22 = 100;
686         Float_t chi23 = 100;
687
688         //  Initial value for charge ratios
689         fQrInit[0]=Float_t(fQ[fIndLocal[0][0]][0])/
690             Float_t(fQ[fIndLocal[0][0]][0]+fQ[fIndLocal[1][0]][0]);
691         fQrInit[1]=fQrInit[0];
692         
693         if (accepted[0] && accepted[1]) {
694             
695             fXInit[0]=0.5*(xm[0][1]+xm[0][0]);
696             fYInit[0]=ym[0][0];
697             fXInit[1]=0.5*(xm[0][1]+xm[1][0]);
698             fYInit[1]=ym[1][0];
699             fQrInit[0]=0.5;
700             fQrInit[1]=0.5;
701             chi23=CombiDoubleMathiesonFit(c);
702             if (chi23<10) {
703                 Split(c);
704                 Float_t yst;
705                 yst = fYFit[0];
706                 fYFit[0] = fYFit[1];
707                 fYFit[1] = yst;
708                 Split(c);
709             }
710         } else if (accepted[0]) {
711             fXInit[0]=xm[0][1];
712             fYInit[0]=ym[0][0];
713             fXInit[1]=xm[1][0];
714             fYInit[1]=ym[1][0];
715             chi21=CombiDoubleMathiesonFit(c);
716 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
717 //          Float_t prob = TMath::Prob(chi2,ndf);
718 //          prob2->Fill(prob);
719 //          chi2_2->Fill(chi21);
720             AliDebug(1,Form(" chi2 %f\n",chi21));
721             if (chi21<10) Split(c);
722         } else if (accepted[1]) {
723             fXInit[0]=xm[1][1];
724             fYInit[0]=ym[1][0];
725             fXInit[1]=xm[0][0];
726             fYInit[1]=ym[0][0];
727             chi22=CombiDoubleMathiesonFit(c);
728 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
729 //          Float_t prob = TMath::Prob(chi2,ndf);
730 //          prob2->Fill(prob);
731 //          chi2_2->Fill(chi22);
732             AliDebug(1,Form(" chi2 %f\n",chi22));
733             if (chi22<10) Split(c);
734         }
735
736         if (chi21 > 10 && chi22 > 10 && chi23 > 10) {
737 // We keep only the combination found (X->cathode 2, Y->cathode 1)
738             for (Int_t ico=0; ico<2; ico++) {
739                 if (accepted[ico]) {
740                     AliMUONRawCluster cnew;
741                     Int_t cath;    
742                     for (cath=0; cath<2; cath++) {
743                         cnew.SetX(cath, Float_t(xm[ico][1]));
744                         cnew.SetY(cath, Float_t(ym[ico][0]));
745                         cnew.SetZ(cath, fZPlane);
746                         cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
747                         for (i=0; i<fMul[cath]; i++) {
748                             cnew.SetIndex(i, cath, c->GetIndex(i, cath));
749                             if (fSegmentationType == 1) 
750                               fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
751                             else
752                               fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
753
754                         }
755                         AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
756                         AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
757                         
758                         FillCluster(&cnew,cath);
759                     } 
760                     cnew.SetClusterType(cnew.PhysicsContribution());
761                     AddRawCluster(cnew);
762                     fNPeaks++;
763                 }
764             }
765         }
766         
767 //  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
768 //  (3') One local maximum on cathode 1 and two maxima on cathode 2 
769 //  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
770     } else if (fNLocal[0]==1 && fNLocal[1]==2) {
771         Float_t xm[4][2], ym[4][2];
772         Float_t dpx, dpy, dx, dy;
773         Int_t ixm[4][2], iym[4][2];
774         Int_t isec, im1, ico;
775 //
776 //  Form the 2x2 combinations
777 //  0-0, 0-1, 1-0, 1-1  
778         ico=0;
779         for (im1=0; im1<2; im1++) {
780             xm[ico][0]=fX[fIndLocal[0][0]][0];
781             ym[ico][0]=fY[fIndLocal[0][0]][0];
782             xm[ico][1]=fX[fIndLocal[im1][1]][1];
783             ym[ico][1]=fY[fIndLocal[im1][1]][1];
784             
785             ixm[ico][0]=fIx[fIndLocal[0][0]][0];
786             iym[ico][0]=fIy[fIndLocal[0][0]][0];
787             ixm[ico][1]=fIx[fIndLocal[im1][1]][1];
788             iym[ico][1]=fIy[fIndLocal[im1][1]][1];
789             ico++;
790         }
791 // ico = 0 : first local maximum on cathodes 1 and 2
792 // ico = 1 : first local maximum on cathode 1 and second on cathode 2
793
794 // Analyse the combinations and keep those that are possible !
795 // For each combination check consistency in x and y    
796         Int_t iacc;
797         Bool_t accepted[4];
798         iacc=0;
799         // In case of staggering maxima are displaced by exactly half the pad-size in y. 
800         // We have to take into account the numerical precision in the consistency check;       
801         Float_t eps = 1.e-5;
802
803         
804         for (ico=0; ico<2; ico++) {
805             accepted[ico]=kFALSE;
806             if (fSegmentationType == 1) {
807               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
808               dpx=fSeg[0]->Dpx(isec)/2.;
809             } else {
810               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
811               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
812             }
813             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
814             if (fSegmentationType == 1) {
815               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
816               dpy=fSeg[1]->Dpy(isec)/2.;
817             } else {
818               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
819               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
820             }
821             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
822             AliDebug(1,Form("\n %i %f %f %f %f \n", ico, ym[ico][0], ym[ico][1], dy, dpy ));
823             if ((dx <= dpx) && (dy <= dpy+eps)) {
824                 // consistent
825                 accepted[ico]=kTRUE;
826                 AliDebug(1,Form("ico %d\n",ico));
827                 iacc++;
828             } else {
829                 // reject
830                 accepted[ico]=kFALSE;
831             }
832         }
833
834         Float_t chi21 = 100;
835         Float_t chi22 = 100;
836         Float_t chi23 = 100;
837
838         fQrInit[1]=Float_t(fQ[fIndLocal[0][1]][1])/
839             Float_t(fQ[fIndLocal[0][1]][1]+fQ[fIndLocal[1][1]][1]);
840         
841         fQrInit[0]=fQrInit[1];
842
843         
844         if (accepted[0] && accepted[1]) {
845             fXInit[0]=xm[0][1];
846             fYInit[0]=0.5*(ym[0][0]+ym[0][1]);
847             fXInit[1]=xm[1][1];
848             fYInit[1]=0.5*(ym[0][0]+ym[1][1]);
849             fQrInit[0]=0.5;
850             fQrInit[1]=0.5;
851             chi23=CombiDoubleMathiesonFit(c);
852             if (chi23<10) {
853                 Split(c);
854                 Float_t yst;
855                 yst = fYFit[0];
856                 fYFit[0] = fYFit[1];
857                 fYFit[1] = yst;
858                 Split(c);
859             }
860         } else if (accepted[0]) {
861             fXInit[0]=xm[0][0];
862             fYInit[0]=ym[0][1];
863             fXInit[1]=xm[1][1];
864             fYInit[1]=ym[1][1];
865             chi21=CombiDoubleMathiesonFit(c);
866 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
867 //          Float_t prob = TMath::Prob(chi2,ndf);
868 //          prob2->Fill(prob);
869 //          chi2_2->Fill(chi21);
870             AliDebug(1,Form(" chi2 %f\n",chi21));
871             if (chi21<10) Split(c);
872         } else if (accepted[1]) {
873             fXInit[0]=xm[1][0];
874             fYInit[0]=ym[1][1];
875             fXInit[1]=xm[0][1];
876             fYInit[1]=ym[0][1];
877             chi22=CombiDoubleMathiesonFit(c);
878 //          Int_t ndf = fgNbins[0]+fgNbins[1]-6;
879 //          Float_t prob = TMath::Prob(chi2,ndf);
880 //          prob2->Fill(prob);
881 //          chi2_2->Fill(chi22);
882             AliDebug(1,Form(" chi2 %f\n",chi22));
883             if (chi22<10) Split(c);
884         }
885
886         if (chi21 > 10 && chi22 > 10 && chi23 > 10) {
887 //We keep only the combination found (X->cathode 2, Y->cathode 1)
888             for (Int_t ico=0; ico<2; ico++) {
889                 if (accepted[ico]) {
890                     AliMUONRawCluster cnew;
891                     Int_t cath;    
892                     for (cath=0; cath<2; cath++) {
893                         cnew.SetX(cath, Float_t(xm[ico][1]));
894                         cnew.SetY(cath, Float_t(ym[ico][0]));
895                         cnew.SetZ(cath, fZPlane);
896                         cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
897                         for (i=0; i<fMul[cath]; i++) {
898                             cnew.SetIndex(i, cath, c->GetIndex(i, cath));
899                             if (fSegmentationType == 1) 
900                               fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
901                             else 
902                               fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
903                         }
904                         AliDebug(1,Form("\nRawCluster %d cath %d\n",ico,cath));
905                         AliDebug(1,Form("mult_av %d\n",c->GetMultiplicity(cath)));
906                         FillCluster(&cnew,cath);
907                     } 
908                     cnew.SetClusterType(cnew.PhysicsContribution());
909                     AddRawCluster(cnew);
910                     fNPeaks++;
911                 }
912             }
913         }
914
915 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
916 //  (4) At least three local maxima on cathode 1 or on cathode 2 
917 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
918     } else if (fNLocal[0]>2 || fNLocal[1]>2) {
919         Int_t param = fNLocal[0]*fNLocal[1];
920         Int_t ii;
921
922         Float_t ** xm = new Float_t * [param];
923         for (ii=0; ii<param; ii++) xm[ii]=new Float_t [2];
924         Float_t ** ym = new Float_t * [param];
925         for (ii=0; ii<param; ii++) ym[ii]=new Float_t [2];
926         Int_t ** ixm = new Int_t * [param];
927         for (ii=0; ii<param; ii++) ixm[ii]=new Int_t [2];
928         Int_t ** iym = new Int_t * [param];
929         for (ii=0; ii<param; ii++) iym[ii]=new Int_t [2];
930         
931         Int_t isec, ico;
932         Float_t dpx, dpy, dx, dy;
933
934         ico=0;
935         for (Int_t im1=0; im1<fNLocal[0]; im1++) {
936             for (Int_t im2=0; im2<fNLocal[1]; im2++) {
937                 xm[ico][0]=fX[fIndLocal[im1][0]][0];
938                 ym[ico][0]=fY[fIndLocal[im1][0]][0];
939                 xm[ico][1]=fX[fIndLocal[im2][1]][1];
940                 ym[ico][1]=fY[fIndLocal[im2][1]][1];
941
942                 ixm[ico][0]=fIx[fIndLocal[im1][0]][0];
943                 iym[ico][0]=fIy[fIndLocal[im1][0]][0];
944                 ixm[ico][1]=fIx[fIndLocal[im2][1]][1];
945                 iym[ico][1]=fIy[fIndLocal[im2][1]][1];
946                 ico++;
947             }
948         }
949         
950         Int_t nIco = ico;
951         AliDebug(1,Form("nIco %d\n",nIco));
952         for (ico=0; ico<nIco; ico++) {
953             AliDebug(1,Form("ico = %d\n",ico));
954             if (fSegmentationType == 1) {
955               isec=fSeg[0]->Sector(ixm[ico][0], iym[ico][0]);
956               dpx=fSeg[0]->Dpx(isec)/2.;
957             } else {
958               isec=fSeg2[0]->Sector(fInput->DetElemId(), ixm[ico][0], iym[ico][0]);
959               dpx=fSeg2[0]->Dpx(fInput->DetElemId(), isec)/2.;
960             } 
961             dx=TMath::Abs(xm[ico][0]-xm[ico][1]);
962             if (fSegmentationType == 1) {
963               isec=fSeg[1]->Sector(ixm[ico][1], iym[ico][1]);
964               dpy=fSeg[1]->Dpy(isec)/2.;
965             } else {
966               isec=fSeg2[1]->Sector(fInput->DetElemId(), ixm[ico][1], iym[ico][1]);
967               dpy=fSeg2[1]->Dpy(fInput->DetElemId(), isec)/2.;
968             }
969             dy=TMath::Abs(ym[ico][0]-ym[ico][1]);
970                 AliDebug(1,Form("dx %f dpx %f dy %f dpy %f\n",dx,dpx,dy,dpy));
971                 AliDebug(1,Form("  X %f Y %f\n",xm[ico][1],ym[ico][0]));
972             if ((dx <= dpx) && (dy <= dpy)) {
973                         AliDebug(1,"ok\n");
974                 Int_t cath;    
975                 AliMUONRawCluster cnew;
976                 for (cath=0; cath<2; cath++) {
977                     cnew.SetX(cath, Float_t(xm[ico][1]));
978                     cnew.SetY(cath, Float_t(ym[ico][0]));
979                     cnew.SetZ(cath, fZPlane);
980                     cnew.SetMultiplicity(cath, c->GetMultiplicity(cath));
981                     for (i=0; i<fMul[cath]; i++) {
982                         cnew.SetIndex(i, cath, c->GetIndex(i, cath));
983                         if (fSegmentationType == 1) 
984                           fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
985                         else 
986                           fSeg2[cath]->SetPad(fInput->DetElemId(), fIx[i][cath], fIy[i][cath]);
987                     }
988                     FillCluster(&cnew,cath);
989                 } 
990                 cnew.SetClusterType(cnew.PhysicsContribution());
991                 //              cnew.SetDetElementID(fInput->DetElemId());
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       // slat ID from digit
1363       
1364     }
1365   }
1366 }
1367
1368 void  AliMUONClusterFinderVS::FillCluster(AliMUONRawCluster* c, Int_t cath) 
1369 {
1370   //
1371   //  Completes cluster information starting from list of digits
1372   //
1373   static Float_t dr0;
1374   
1375   AliMUONDigit* dig;
1376   
1377   if (cath==0) {
1378     dr0 = 10000;
1379   }
1380   
1381   Float_t xpad, ypad, zpad;
1382   Float_t dx, dy, dr;
1383   
1384   for (Int_t i=0; i<c->GetMultiplicity(cath); i++)
1385     {
1386       dig = fInput->Digit(cath,c->GetIndex(i,cath));
1387       if (fSegmentationType == 1) 
1388         fSeg[cath]->
1389           GetPadC(dig->PadX(),dig->PadY(),xpad,ypad, zpad);
1390       else
1391         fSeg2[cath]->
1392           GetPadC(fInput->DetElemId(),dig->PadX(),dig->PadY(),xpad,ypad, zpad);
1393       AliDebug(1,Form("x %f y %f cx %f cy %f\n",xpad,ypad,c->GetX(0),c->GetY(0)));
1394       dx = xpad - c->GetX(0);
1395       dy = ypad - c->GetY(0);
1396       dr = TMath::Sqrt(dx*dx+dy*dy);
1397       
1398       if (dr < dr0) {
1399         dr0 = dr;
1400         AliDebug(1,Form(" dr %f\n",dr));
1401         Int_t q=dig->Signal();
1402         if (dig->Physics() >= dig->Signal()) {
1403           c->SetPhysics(i,2);
1404         } else if (dig->Physics() == 0) {
1405           c->SetPhysics(i,0);
1406         } else  c->SetPhysics(i,1);
1407         c->SetPeakSignal(cath,q);
1408         c->SetTrack(0,dig->Hit());
1409         c->SetTrack(1,dig->Track(0));
1410         c->SetTrack(2,dig->Track(1));
1411         
1412         AliDebug(1,Form(" c->fTracks[0] %d c->fTracks[1] %d\n",dig->Hit(),
1413                         dig->Track(0)));
1414       }
1415       //
1416     } // loop over digits
1417   
1418   //  apply correction to the coordinate along the anode wire
1419   // Force on anod
1420   if (fSegmentationType == 1) 
1421     c->SetX(cath,fSeg[cath]->GetAnod(c->GetX(cath)));
1422   else
1423     c->SetX(cath,fSeg2[cath]->GetAnod(fInput->DetElemId(), c->GetX(cath)));
1424 }
1425
1426 void  AliMUONClusterFinderVS::FindCluster(Int_t i, Int_t j, Int_t cath, AliMUONRawCluster &c){
1427
1428
1429 //
1430 //  Find a super cluster on both cathodes
1431 //
1432 //
1433 //  Add i,j as element of the cluster
1434 //
1435     
1436     Int_t idx = fHitMap[cath]->GetHitIndex(i,j);
1437     AliMUONDigit* dig = (AliMUONDigit*) fHitMap[cath]->GetHit(i,j);
1438     Int_t q=dig->Signal();
1439     Int_t theX=dig->PadX();
1440     Int_t theY=dig->PadY(); 
1441    
1442     if (q > TMath::Abs(c.GetPeakSignal(0)) && q > TMath::Abs(c.GetPeakSignal(1))) {
1443         c.SetPeakSignal(cath,q);
1444         c.SetTrack(0,dig->Hit());
1445         c.SetTrack(1,dig->Track(0));
1446         c.SetTrack(2,dig->Track(1));
1447     }
1448
1449 //
1450 //  Make sure that list of digits is ordered 
1451 // 
1452     Int_t mu=c.GetMultiplicity(cath);
1453     c.SetIndex(mu, cath, idx);
1454     
1455     if (dig->Physics() >= dig->Signal()) {
1456         c.SetPhysics(mu,2);
1457     } else if (dig->Physics() == 0) {
1458         c.SetPhysics(mu,0);
1459     } else  c.SetPhysics(mu,1);
1460
1461     
1462     if (mu > 0) {
1463         for (Int_t ind = mu-1; ind >= 0; ind--) {
1464             Int_t ist=c.GetIndex(ind,cath);
1465             Int_t ql=fInput->Digit(cath, ist)->Signal();
1466             Int_t ix=fInput->Digit(cath, ist)->PadX();
1467             Int_t iy=fInput->Digit(cath, ist)->PadY();
1468             
1469             if (q>ql || (q==ql && theX > ix && theY < iy)) {
1470                 c.SetIndex(ind, cath, idx);
1471                 c.SetIndex(ind+1, cath, ist);
1472             } else {
1473                 
1474                 break;
1475             }
1476         }
1477     }
1478
1479     c.SetMultiplicity(cath, c.GetMultiplicity(cath)+1);
1480     if (c.GetMultiplicity(cath) >= 50 ) {
1481       AliDebug(1,Form("FindCluster - multiplicity >50  %d \n",c.GetMultiplicity(0)));
1482         c.SetMultiplicity(cath, 49);
1483     }
1484
1485 // Prepare center of gravity calculation
1486     Float_t x, y, z;
1487     if (fSegmentationType == 1) 
1488       fSeg[cath]->GetPadC(i, j, x, y, z);
1489     else
1490       fSeg2[cath]->GetPadC(fInput->DetElemId(), i, j, x, y, z);
1491     c.AddX(cath,q*x);
1492     c.AddY(cath,q*y);
1493     c.AddCharge(cath,q);
1494 //
1495 // Flag hit as "taken"  
1496     fHitMap[cath]->FlagHit(i,j);
1497 //
1498 //  Now look recursively for all neighbours and pad hit on opposite cathode
1499 //
1500 //  Loop over neighbours
1501     Int_t ix,iy;
1502     ix=iy=0;
1503     Int_t nn;
1504     Int_t xList[10], yList[10];
1505     if (fSegmentationType == 1) 
1506       fSeg[cath]->Neighbours(i,j,&nn,xList,yList);
1507     else 
1508       fSeg2[cath]->Neighbours(fInput->DetElemId(), i,j,&nn,xList,yList);
1509     for (Int_t in=0; in<nn; in++) {
1510         ix=xList[in];
1511         iy=yList[in];
1512         
1513         if (fHitMap[cath]->TestHit(ix,iy)==kUnused) {
1514             AliDebug(2,Form("\n Neighbours %d %d %d", cath, ix, iy));
1515             FindCluster(ix, iy, cath, c);
1516         }
1517         
1518    }
1519     Int_t nOpp=0;
1520     Int_t iXopp[50], iYopp[50];
1521     
1522 //  Neighbours on opposite cathode 
1523 //  Take into account that several pads can overlap with the present pad
1524     Int_t isec;
1525     if (fSegmentationType == 1) 
1526       isec=fSeg[cath]->Sector(i,j);    
1527     else
1528       isec=fSeg2[cath]->Sector(fInput->DetElemId(), i,j);    
1529
1530     Int_t iop;
1531     Float_t dx, dy;
1532
1533     if (fSegmentationType == 1) {
1534       if (cath==0) {
1535         iop = 1;
1536         dx  = (fSeg[cath]->Dpx(isec))/2.;
1537         dy  = 0.;
1538       } else {
1539         iop = 0;
1540         dx  = 0.;
1541         dy  = (fSeg[cath]->Dpy(isec))/2;
1542       }
1543    
1544
1545     
1546       // loop over pad neighbours on opposite cathode
1547       for (fSeg[iop]->FirstPad(x, y, fZPlane, dx, dy);
1548            fSeg[iop]->MorePads();
1549            fSeg[iop]->NextPad())
1550         {
1551         
1552           ix = fSeg[iop]->Ix(); iy = fSeg[iop]->Iy();
1553           AliDebug(2,Form("\n ix, iy: %f %f %f %d %d %d", x,y,z,ix, iy, fSector));
1554           if (fHitMap[iop]->TestHit(ix,iy)==kUnused){
1555             iXopp[nOpp]=ix;
1556             iYopp[nOpp++]=iy;
1557             AliDebug(2,Form("\n Opposite %d %d %d", iop, ix, iy));
1558           }
1559         
1560         } // Loop over pad neighbours
1561       //  This had to go outside the loop since recursive calls inside the iterator are not possible
1562       //
1563       Int_t jopp;
1564       for (jopp=0; jopp<nOpp; jopp++) {
1565         if (fHitMap[iop]->TestHit(iXopp[jopp],iYopp[jopp]) == kUnused) 
1566           FindCluster(iXopp[jopp], iYopp[jopp], iop, c);
1567       }
1568     } else {
1569
1570       if (cath==0) {
1571         iop = 1;
1572         dx  = (fSeg2[cath]->Dpx(fInput->DetElemId(), isec))/2.;
1573         dy  = 0.;
1574       } else {
1575         iop = 0;
1576         dx  = 0.;
1577         dy  = (fSeg2[cath]->Dpy(fInput->DetElemId(), isec))/2;
1578       }
1579    
1580
1581     
1582       // loop over pad neighbours on opposite cathode
1583       for (fSeg2[iop]->FirstPad(fInput->DetElemId(), x, y, fZPlane, dx, dy);
1584            fSeg2[iop]->MorePads(fInput->DetElemId());
1585            fSeg2[iop]->NextPad(fInput->DetElemId()))
1586         {
1587         
1588           ix = fSeg2[iop]->Ix(); iy = fSeg2[iop]->Iy();
1589           AliDebug(2,Form("\n ix, iy: %f %f %f %d %d %d", x,y,z,ix, iy, fSector));
1590           if (fHitMap[iop]->TestHit(ix,iy)==kUnused){
1591             iXopp[nOpp]=ix;
1592             iYopp[nOpp++]=iy;
1593             AliDebug(2,Form("\n Opposite %d %d %d", iop, ix, iy));
1594           }
1595         
1596         } // Loop over pad neighbours
1597       //  This had to go outside the loop since recursive calls inside the iterator are not possible
1598       //
1599       Int_t jopp;
1600       for (jopp=0; jopp<nOpp; jopp++) {
1601         if (fHitMap[iop]->TestHit(iXopp[jopp],iYopp[jopp]) == kUnused) 
1602           FindCluster(iXopp[jopp], iYopp[jopp], iop, c);
1603       }
1604     }
1605 }
1606
1607 //_____________________________________________________________________________
1608
1609 void AliMUONClusterFinderVS::FindRawClusters()
1610 {
1611   //
1612   // MUON cluster finder from digits -- finds neighbours on both cathodes and 
1613   // fills the tree with raw clusters
1614   //
1615
1616     ResetRawClusters();
1617 //  Return if no input datad available
1618     if (!fInput->NDigits(0) && !fInput->NDigits(1)) return;
1619
1620     fSegmentationType = fInput->GetSegmentationType();
1621
1622     if (fSegmentationType == 1) {
1623       fSeg[0] = fInput->Segmentation(0);
1624       fSeg[1] = fInput->Segmentation(1);
1625
1626       fHitMap[0]  = new AliMUONHitMapA1(fSeg[0], fInput->Digits(0));
1627       fHitMap[1]  = new AliMUONHitMapA1(fSeg[1], fInput->Digits(1));
1628
1629     } else {
1630       fSeg2[0] = fInput->Segmentation2(0);
1631       fSeg2[1] = fInput->Segmentation2(1);
1632       
1633       fHitMap[0]  = new AliMUONHitMapA1(fInput->DetElemId(), fSeg2[0], fInput->Digits(0));
1634       fHitMap[1]  = new AliMUONHitMapA1(fInput->DetElemId(), fSeg2[1], fInput->Digits(1));
1635     }
1636  
1637     AliMUONDigit *dig;
1638
1639     Int_t ndig, cath;
1640     Int_t nskip=0;
1641     Int_t ncls=0;
1642     fHitMap[0]->FillHits();
1643     fHitMap[1]->FillHits();
1644 //
1645 //  Outer Loop over Cathodes
1646     for (cath=0; cath<2; cath++) {
1647       
1648         for (ndig=0; ndig<fInput->NDigits(cath); ndig++) {
1649           dig = fInput->Digit(cath, ndig);
1650           Int_t padx = dig->PadX();
1651           Int_t pady = dig->PadY();
1652           if (fHitMap[cath]->TestHit(padx,pady)==kUsed ||fHitMap[0]->TestHit(padx,pady)==kEmpty) {
1653             nskip++;
1654             continue;
1655           }
1656           AliDebug(1,Form("\n CATHODE %d CLUSTER %d\n",cath,ncls));
1657           AliMUONRawCluster clus;
1658           clus.SetMultiplicity(0, 0);
1659           clus.SetMultiplicity(1, 0);
1660           clus.SetPeakSignal(cath,dig->Signal());
1661           clus.SetTrack(0, dig->Hit());
1662           clus.SetTrack(1, dig->Track(0));
1663           clus.SetTrack(2, dig->Track(1));
1664           
1665           AliDebug(1,Form("idDE %d Padx %d Pady %d", fInput->DetElemId(), padx, pady));
1666           
1667           // tag the beginning of cluster list in a raw cluster
1668           clus.SetNcluster(0,-1);
1669           Float_t xcu, ycu;
1670           if (fSegmentationType == 1) {
1671             fSeg[cath]->GetPadC(padx,pady, xcu, ycu, fZPlane);
1672             fSector= fSeg[cath]->Sector(padx,pady)/100;
1673           } else {
1674             fSeg2[cath]->GetPadC(fInput->DetElemId(), padx, pady, xcu, ycu, fZPlane);
1675             fSector= fSeg2[cath]->Sector(fInput->DetElemId(), padx, pady)/100;
1676           }
1677           
1678           
1679           
1680           FindCluster(padx,pady,cath,clus);
1681           //^^^^^^^^^^^^^^^^^^^^^^^^
1682           // center of gravity
1683           if (clus.GetX(0)!=0.) clus.SetX(0, clus.GetX(0)/clus.GetCharge(0)); // clus.fX[0] /= clus.fQ[0];
1684           
1685           // Force on anod
1686           if (fSegmentationType == 1) 
1687             clus.SetX(0,fSeg[0]->GetAnod(clus.GetX(0)));
1688           else 
1689             clus.SetX(0,fSeg2[0]->GetAnod(fInput->DetElemId(), clus.GetX(0)));
1690           if (clus.GetY(0)!=0.) clus.SetY(0, clus.GetY(0)/clus.GetCharge(0)); // clus.fY[0] /= clus.fQ[0];
1691           
1692           if(clus.GetCharge(1)!=0.) clus.SetX(1, clus.GetX(1)/clus.GetCharge(1));  // clus.fX[1] /= clus.fQ[1];
1693           
1694           // Force on anod
1695           if (fSegmentationType == 1) 
1696             clus.SetX(1, fSeg[0]->GetAnod(clus.GetX(1)));
1697           else 
1698             clus.SetX(1, fSeg2[0]->GetAnod(fInput->DetElemId(),clus.GetX(1)));
1699           if(clus.GetCharge(1)!=0.) clus.SetY(1, clus.GetY(1)/clus.GetCharge(1));// clus.fY[1] /= clus.fQ[1];
1700           
1701           clus.SetZ(0, fZPlane);
1702           clus.SetZ(1, fZPlane);            
1703           
1704           AliDebug(1,Form("\n Cathode 1 multiplicite %d X(CG) %f Y(CG) %f\n",
1705                           clus.GetMultiplicity(0),clus.GetX(0),clus.GetY(0)));
1706           AliDebug(1,Form(" Cathode 2 multiplicite %d X(CG) %f Y(CG) %f\n",
1707                           clus.GetMultiplicity(1),clus.GetX(1),clus.GetY(1)));
1708           //      Analyse cluster and decluster if necessary
1709           //    
1710           ncls++;
1711           clus.SetNcluster(1,fNRawClusters);
1712           clus.SetClusterType(clus.PhysicsContribution());
1713           
1714           fNPeaks=0;
1715           //
1716           //
1717           Decluster(&clus);
1718           //
1719           //      reset Cluster object
1720           { // begin local scope
1721             for (int k=0;k<clus.GetMultiplicity(0);k++) clus.SetIndex(k, 0, 0);
1722           } // end local scope
1723           
1724           { // begin local scope
1725             for (int k=0;k<clus.GetMultiplicity(1);k++) clus.SetIndex(k, 1, 0);
1726           } // end local scope
1727           
1728           clus.SetMultiplicity(0,0);
1729           clus.SetMultiplicity(1,0);
1730           
1731         
1732         } // end loop ndig
1733     } // end loop cathodes
1734     delete fHitMap[0];
1735     delete fHitMap[1];
1736 }
1737
1738 Float_t AliMUONClusterFinderVS::SingleMathiesonFit(AliMUONRawCluster *c, Int_t cath)
1739 {
1740 // Performs a single Mathieson fit on one cathode
1741 // 
1742     Double_t arglist[20];
1743     Int_t ierflag=0;
1744     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1745     
1746     clusterInput.Fitter()->SetFCN(fcnS1);
1747     clusterInput.Fitter()->mninit(2,10,7);
1748     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1749     arglist[0]=-1;
1750     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1751 // Set starting values 
1752     static Double_t vstart[2];
1753     vstart[0]=c->GetX(1);
1754     vstart[1]=c->GetY(0);
1755     
1756     
1757 // lower and upper limits
1758     static Double_t lower[2], upper[2];
1759     Int_t ix,iy, isec;
1760     if (fSegmentationType == 1) {
1761       fSeg[cath]->GetPadI(c->GetX(cath), c->GetY(cath), fZPlane, ix, iy);
1762       isec=fSeg[cath]->Sector(ix, iy);
1763
1764       lower[0]=vstart[0]-fSeg[cath]->Dpx(isec)/2;
1765       lower[1]=vstart[1]-fSeg[cath]->Dpy(isec)/2;
1766     
1767       upper[0]=lower[0]+fSeg[cath]->Dpx(isec);
1768       upper[1]=lower[1]+fSeg[cath]->Dpy(isec);
1769
1770     } else {
1771       fSeg2[cath]->GetPadI(fInput->DetElemId(), c->GetX(cath), c->GetY(cath), fZPlane, ix, iy);
1772       isec=fSeg2[cath]->Sector(fInput->DetElemId(), ix, iy);
1773
1774       lower[0]=vstart[0]-fSeg2[cath]->Dpx(fInput->DetElemId(), isec)/2;
1775       lower[1]=vstart[1]-fSeg2[cath]->Dpy(fInput->DetElemId(), isec)/2;
1776     
1777       upper[0]=lower[0]+fSeg2[cath]->Dpx(fInput->DetElemId(), isec);
1778       upper[1]=lower[1]+fSeg2[cath]->Dpy(fInput->DetElemId(), isec);
1779     }
1780
1781 // step sizes
1782     static Double_t step[2]={0.0005, 0.0005};
1783     
1784     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
1785     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
1786 // ready for minimisation       
1787     arglist[0]= -1;
1788     arglist[1]= 0;
1789     
1790     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
1791     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
1792     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
1793     Double_t fmin, fedm, errdef;
1794     Int_t   npari, nparx, istat;
1795       
1796     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
1797     fFitStat=istat;
1798     
1799 // Print results
1800 // Get fitted parameters
1801     Double_t xrec, yrec;
1802     TString chname;
1803     Double_t epxz, b1, b2;
1804     Int_t ierflg;
1805     clusterInput.Fitter()->mnpout(0, chname, xrec, epxz, b1, b2, ierflg);       
1806     clusterInput.Fitter()->mnpout(1, chname, yrec, epxz, b1, b2, ierflg);       
1807     fXFit[cath]=xrec;
1808     fYFit[cath]=yrec;
1809     return fmin;
1810 }
1811
1812 Float_t AliMUONClusterFinderVS::CombiSingleMathiesonFit(AliMUONRawCluster * /*c*/)
1813 {
1814 // Perform combined Mathieson fit on both cathode planes
1815 //
1816     Double_t arglist[20];
1817     Int_t ierflag=0;
1818     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1819     clusterInput.Fitter()->SetFCN(fcnCombiS1);
1820     clusterInput.Fitter()->mninit(2,10,7);
1821     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1822     arglist[0]=-1;
1823     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1824     static Double_t vstart[2];
1825     vstart[0]=fXInit[0];
1826     vstart[1]=fYInit[0];
1827     
1828     
1829 // lower and upper limits
1830     static Float_t lower[2], upper[2];
1831     Int_t ix,iy,isec;
1832     Float_t dpy, dpx;
1833
1834     if (fSegmentationType == 1) {
1835       fSeg[0]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
1836       isec=fSeg[0]->Sector(ix, iy);
1837       dpy=fSeg[0]->Dpy(isec);
1838       fSeg[1]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
1839       isec=fSeg[1]->Sector(ix, iy);
1840       dpx=fSeg[1]->Dpx(isec);
1841
1842     } else {
1843       fSeg2[0]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
1844       isec=fSeg2[0]->Sector(fInput->DetElemId(), ix, iy);
1845       dpy=fSeg2[0]->Dpy(fInput->DetElemId(), isec);
1846       fSeg2[1]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
1847       isec=fSeg2[1]->Sector(fInput->DetElemId(), ix, iy);
1848       dpx=fSeg2[1]->Dpx(fInput->DetElemId(), isec);
1849
1850     }
1851     Int_t icount;
1852     Float_t xdum, ydum, zdum;
1853
1854 //  Find save upper and lower limits    
1855     
1856     icount = 0;
1857     if (fSegmentationType == 1) {
1858       for (fSeg[1]->FirstPad(fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
1859            fSeg[1]->MorePads(); 
1860            fSeg[1]->NextPad())
1861         {
1862           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
1863           fSeg[1]->GetPadC(ix,iy, upper[0], ydum, zdum);        
1864           if (icount ==0) lower[0]=upper[0];
1865           icount++;
1866         }
1867     } else {
1868       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
1869            fSeg2[1]->MorePads(fInput->DetElemId()); 
1870            fSeg2[1]->NextPad(fInput->DetElemId()))
1871         {
1872           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
1873           fSeg2[1]->GetPadC(fInput->DetElemId(), ix,iy, upper[0], ydum, zdum);  
1874           if (icount ==0) lower[0]=upper[0];
1875           icount++;
1876         }
1877     }
1878     if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}
1879         
1880     icount=0;
1881     AliDebug(1,Form("\n single y %f %f", fXInit[0], fYInit[0]));
1882     
1883     if (fSegmentationType == 1) {
1884       for (fSeg[0]->FirstPad(fXInit[0], fYInit[0], fZPlane, 0., dpy); 
1885            fSeg[0]->MorePads(); 
1886            fSeg[0]->NextPad())
1887         {
1888           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
1889           fSeg[0]->GetPadC(ix,iy,xdum,upper[1],zdum);   
1890           if (icount ==0) lower[1]=upper[1];
1891           icount++;
1892           AliDebug(1,Form("\n upper lower %d %f %f", icount, upper[1], lower[1]));
1893         }
1894     } else {
1895       for (fSeg2[0]->FirstPad(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, 0., dpy); 
1896            fSeg2[0]->MorePads(fInput->DetElemId()); 
1897            fSeg2[0]->NextPad(fInput->DetElemId()))
1898         {
1899           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
1900           fSeg2[0]->GetPadC(fInput->DetElemId(), ix,iy,xdum,upper[1],zdum);     
1901           if (icount ==0) lower[1]=upper[1];
1902           icount++;
1903           AliDebug(1,Form("\n upper lower %d %f %f", icount, upper[1], lower[1]));
1904         }
1905     }
1906     if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}
1907
1908 // step sizes
1909     static Double_t step[2]={0.00001, 0.0001};
1910     
1911     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
1912     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
1913 // ready for minimisation       
1914     arglist[0]= -1;
1915     arglist[1]= 0;
1916     
1917     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
1918     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
1919     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
1920     Double_t fmin, fedm, errdef;
1921     Int_t   npari, nparx, istat;
1922       
1923     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
1924     fFitStat=istat;
1925     
1926 // Print results
1927 // Get fitted parameters
1928     Double_t xrec, yrec;
1929     TString chname;
1930     Double_t epxz, b1, b2;
1931     Int_t ierflg;
1932     clusterInput.Fitter()->mnpout(0, chname, xrec, epxz, b1, b2, ierflg);       
1933     clusterInput.Fitter()->mnpout(1, chname, yrec, epxz, b1, b2, ierflg);       
1934     fXFit[0]=xrec;
1935     fYFit[0]=yrec;
1936     return fmin;
1937 }
1938
1939 Bool_t AliMUONClusterFinderVS::DoubleMathiesonFit(AliMUONRawCluster * /*c*/, Int_t cath)
1940 {
1941 // Performs a double Mathieson fit on one cathode
1942 // 
1943
1944 //
1945 //  Initialise global variables for fit
1946     Double_t arglist[20];
1947     Int_t ierflag=0;
1948     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
1949     clusterInput.Fitter()->SetFCN(fcnS2);
1950     clusterInput.Fitter()->mninit(5,10,7);
1951     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
1952     arglist[0]=-1;
1953     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
1954 // Set starting values 
1955     static Double_t vstart[5];
1956     vstart[0]=fX[fIndLocal[0][cath]][cath];
1957     vstart[1]=fY[fIndLocal[0][cath]][cath];     
1958     vstart[2]=fX[fIndLocal[1][cath]][cath];
1959     vstart[3]=fY[fIndLocal[1][cath]][cath];     
1960     vstart[4]=Float_t(fQ[fIndLocal[0][cath]][cath])/
1961         Float_t(fQ[fIndLocal[0][cath]][cath]+fQ[fIndLocal[1][cath]][cath]);
1962 // lower and upper limits
1963     static Float_t lower[5], upper[5];
1964     Int_t isec;
1965
1966     if (fSegmentationType == 1) {
1967       isec=fSeg[cath]->Sector(fIx[fIndLocal[0][cath]][cath], fIy[fIndLocal[0][cath]][cath]);
1968       lower[0]=vstart[0]-fSeg[cath]->Dpx(isec);
1969       lower[1]=vstart[1]-fSeg[cath]->Dpy(isec);
1970     
1971       upper[0]=lower[0]+2.*fSeg[cath]->Dpx(isec);
1972       upper[1]=lower[1]+2.*fSeg[cath]->Dpy(isec);
1973     
1974       isec=fSeg[cath]->Sector(fIx[fIndLocal[1][cath]][cath], fIy[fIndLocal[1][cath]][cath]);
1975       lower[2]=vstart[2]-fSeg[cath]->Dpx(isec)/2;
1976       lower[3]=vstart[3]-fSeg[cath]->Dpy(isec)/2;
1977     
1978       upper[2]=lower[2]+fSeg[cath]->Dpx(isec);
1979       upper[3]=lower[3]+fSeg[cath]->Dpy(isec);
1980
1981     } else {
1982       isec=fSeg2[cath]->Sector(fInput->DetElemId(),fIx[fIndLocal[0][cath]][cath], 
1983                                fIy[fIndLocal[0][cath]][cath]);
1984       lower[0]=vstart[0]-fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1985       lower[1]=vstart[1]-fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1986     
1987       upper[0]=lower[0]+2.*fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1988       upper[1]=lower[1]+2.*fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1989     
1990       isec=fSeg2[cath]->Sector(fInput->DetElemId(),fIx[fIndLocal[1][cath]][cath], 
1991                               fIy[fIndLocal[1][cath]][cath]);
1992       lower[2]=vstart[2]-fSeg2[cath]->Dpx(fInput->DetElemId(),isec)/2;
1993       lower[3]=vstart[3]-fSeg2[cath]->Dpy(fInput->DetElemId(),isec)/2;
1994     
1995       upper[2]=lower[2]+fSeg2[cath]->Dpx(fInput->DetElemId(),isec);
1996       upper[1]=lower[1]+2.*fSeg2[cath]->Dpy(fInput->DetElemId(),isec);
1997
1998     }
1999
2000     lower[4]=0.;
2001     upper[4]=1.;
2002 // step sizes
2003     static Double_t step[5]={0.0005, 0.0005, 0.0005, 0.0005, 0.0001};
2004     
2005     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
2006     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
2007     clusterInput.Fitter()->mnparm(2,"x2",vstart[2],step[2],lower[2],upper[2],ierflag);
2008     clusterInput.Fitter()->mnparm(3,"y2",vstart[3],step[3],lower[3],upper[3],ierflag);
2009     clusterInput.Fitter()->mnparm(4,"a0",vstart[4],step[4],lower[4],upper[4],ierflag);
2010 // ready for minimisation       
2011     arglist[0]= -1;
2012     arglist[1]= 0;
2013     
2014     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
2015     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
2016     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
2017 // Get fitted parameters
2018     Double_t xrec[2], yrec[2], qfrac;
2019     TString chname;
2020     Double_t epxz, b1, b2;
2021     Int_t ierflg;
2022     clusterInput.Fitter()->mnpout(0, chname, xrec[0], epxz, b1, b2, ierflg);    
2023     clusterInput.Fitter()->mnpout(1, chname, yrec[0], epxz, b1, b2, ierflg);    
2024     clusterInput.Fitter()->mnpout(2, chname, xrec[1], epxz, b1, b2, ierflg);    
2025     clusterInput.Fitter()->mnpout(3, chname, yrec[1], epxz, b1, b2, ierflg);    
2026     clusterInput.Fitter()->mnpout(4, chname, qfrac,   epxz, b1, b2, ierflg);    
2027
2028     Double_t fmin, fedm, errdef;
2029     Int_t   npari, nparx, istat;
2030       
2031     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
2032     fFitStat=istat;
2033     return kTRUE;
2034 }
2035
2036 Float_t AliMUONClusterFinderVS::CombiDoubleMathiesonFit(AliMUONRawCluster * /*c*/)
2037 {
2038 //
2039 // Perform combined double Mathieson fit on both cathode planes
2040 //
2041     Double_t arglist[20];
2042     Int_t ierflag=0;
2043     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
2044     clusterInput.Fitter()->SetFCN(fcnCombiS2);
2045     clusterInput.Fitter()->mninit(6,10,7);
2046     clusterInput.Fitter()->SetPrintLevel(-1+fDebugLevel);
2047     arglist[0]=-1;
2048     clusterInput.Fitter()->mnexcm("SET NOW", arglist, 0, ierflag);
2049 // Set starting values 
2050     static Double_t vstart[6];
2051     vstart[0]=fXInit[0];
2052     vstart[1]=fYInit[0];
2053     vstart[2]=fXInit[1];
2054     vstart[3]=fYInit[1];
2055     vstart[4]=fQrInit[0];
2056     vstart[5]=fQrInit[1];
2057 // lower and upper limits
2058     static Float_t lower[6], upper[6];
2059     Int_t ix,iy,isec;
2060     Float_t dpx, dpy;
2061   if (fSegmentationType == 1) {
2062     fSeg[1]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
2063     isec=fSeg[1]->Sector(ix, iy);
2064     dpx=fSeg[1]->Dpx(isec);
2065
2066     fSeg[0]->GetPadI(fXInit[0], fYInit[0], fZPlane, ix, iy);
2067     isec=fSeg[0]->Sector(ix, iy);
2068     dpy=fSeg[0]->Dpy(isec);
2069
2070   } else {
2071     fSeg2[1]->GetPadI(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, ix, iy);
2072     isec=fSeg2[1]->Sector(fInput->DetElemId(),ix, iy);
2073     dpx=fSeg2[1]->Dpx(fInput->DetElemId(), isec);
2074
2075     fSeg2[0]->GetPadI(fInput->DetElemId(), fXInit[0], fYInit[0], fZPlane, ix, iy);
2076     isec=fSeg2[0]->Sector(fInput->DetElemId(), ix, iy);
2077     dpy=fSeg2[0]->Dpy(fInput->DetElemId(), isec);
2078
2079   }
2080
2081     Int_t icount;
2082     Float_t xdum, ydum, zdum;
2083     AliDebug(1,Form("\n Cluster Finder: %f %f %f %f  ", fXInit[0], fXInit[1],fYInit[0], fYInit[1] ));
2084
2085     if (fSegmentationType == 1) {
2086  
2087       //  Find save upper and lower limits    
2088       icount = 0;
2089     
2090       for (fSeg[1]->FirstPad(fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
2091            fSeg[1]->MorePads(); 
2092            fSeg[1]->NextPad())
2093         {
2094           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
2095           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2096           fSeg[1]->GetPadC(ix,iy,upper[0],ydum,zdum);   
2097           if (icount ==0) lower[0]=upper[0];
2098           icount++;
2099         }
2100       if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}    
2101       //    vstart[0] = 0.5*(lower[0]+upper[0]);
2102
2103     
2104       icount=0;
2105     
2106       for (fSeg[0]->FirstPad(fXInit[0], fYInit[0], fZPlane, 0., dpy); 
2107            fSeg[0]->MorePads(); 
2108            fSeg[0]->NextPad())
2109         {
2110           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
2111           //    if (fHitMap[0]->TestHit(ix, iy) == kEmpty) continue;
2112           fSeg[0]->GetPadC(ix,iy,xdum,upper[1],zdum);   
2113           if (icount ==0) lower[1]=upper[1];
2114           icount++;
2115         }
2116     
2117       if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}    
2118       //     vstart[1] = 0.5*(lower[1]+upper[1]);
2119
2120
2121       fSeg[1]->GetPadI(fXInit[1], fYInit[1], fZPlane, ix, iy);
2122       isec=fSeg[1]->Sector(ix, iy);
2123       dpx=fSeg[1]->Dpx(isec);
2124       fSeg[0]->GetPadI(fXInit[1], fYInit[1], fZPlane, ix, iy);
2125       isec=fSeg[0]->Sector(ix, iy);
2126       dpy=fSeg[0]->Dpy(isec);
2127
2128
2129       //  Find save upper and lower limits    
2130
2131       icount=0;
2132     
2133       for (fSeg[1]->FirstPad(fXInit[1], fYInit[1], fZPlane, dpx, 0); 
2134            fSeg[1]->MorePads(); fSeg[1]->NextPad())
2135         {
2136           ix=fSeg[1]->Ix(); iy=fSeg[1]->Iy();
2137           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2138           fSeg[1]->GetPadC(ix,iy,upper[2],ydum,zdum);   
2139           if (icount ==0) lower[2]=upper[2];
2140           icount++;
2141         }
2142       if (lower[2]>upper[2]) {xdum=lower[2]; lower[2]=upper[2]; upper[2]=xdum;}    
2143       //    vstart[2] = 0.5*(lower[2]+upper[2]);
2144
2145       icount=0;
2146     
2147       for (fSeg[0]->FirstPad(fXInit[1], fYInit[1], fZPlane, 0, dpy); 
2148            fSeg[0]-> MorePads(); fSeg[0]->NextPad())
2149         {
2150           ix=fSeg[0]->Ix(); iy=fSeg[0]->Iy();
2151           //    if (fHitMap[0]->TestHit(ix, iy) != kEmpty) continue;
2152         
2153           fSeg[0]->GetPadC(ix,iy,xdum,upper[3],zdum);   
2154           if (icount ==0) lower[3]=upper[3];
2155           icount++;
2156
2157         }
2158       if (lower[3]>upper[3]) {xdum=lower[3]; lower[3]=upper[3]; upper[3]=xdum;}    
2159     
2160       //     vstart[3] = 0.5*(lower[3]+upper[3]);
2161     } else {
2162
2163     //  Find save upper and lower limits    
2164       icount = 0;
2165     
2166       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, dpx, 0.); 
2167            fSeg2[1]->MorePads(fInput->DetElemId()); 
2168            fSeg2[1]->NextPad(fInput->DetElemId()))
2169         {
2170           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
2171           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2172           fSeg2[1]->GetPadC(fInput->DetElemId(),ix,iy,upper[0],ydum,zdum);      
2173           if (icount ==0) lower[0]=upper[0];
2174           icount++;
2175         }
2176       if (lower[0]>upper[0]) {xdum=lower[0]; lower[0]=upper[0]; upper[0]=xdum;}    
2177       //    vstart[0] = 0.5*(lower[0]+upper[0]);
2178
2179     
2180       icount=0;
2181     
2182       for (fSeg2[0]->FirstPad(fInput->DetElemId(),fXInit[0], fYInit[0], fZPlane, 0., dpy); 
2183            fSeg2[0]->MorePads(fInput->DetElemId()); 
2184            fSeg2[0]->NextPad(fInput->DetElemId()))
2185         {
2186           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
2187           //    if (fHitMap[0]->TestHit(ix, iy) == kEmpty) continue;
2188           fSeg2[0]->GetPadC(fInput->DetElemId(),ix,iy,xdum,upper[1],zdum);      
2189           if (icount ==0) lower[1]=upper[1];
2190           icount++;
2191         }
2192     
2193       if (lower[1]>upper[1]) {xdum=lower[1]; lower[1]=upper[1]; upper[1]=xdum;}    
2194       //     vstart[1] = 0.5*(lower[1]+upper[1]);
2195
2196
2197       fSeg2[1]->GetPadI(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, ix, iy);
2198       isec=fSeg2[1]->Sector(fInput->DetElemId(),ix, iy);
2199       dpx=fSeg2[1]->Dpx(fInput->DetElemId(),isec);
2200       fSeg2[0]->GetPadI(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, ix, iy);
2201       isec=fSeg2[0]->Sector(fInput->DetElemId(),ix, iy);
2202       dpy=fSeg2[0]->Dpy(fInput->DetElemId(),isec);
2203
2204
2205       //  Find save upper and lower limits    
2206
2207       icount=0;
2208     
2209       for (fSeg2[1]->FirstPad(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, dpx, 0); 
2210            fSeg2[1]->MorePads(fInput->DetElemId()); 
2211            fSeg2[1]->NextPad(fInput->DetElemId()))
2212         {
2213           ix=fSeg2[1]->Ix(); iy=fSeg2[1]->Iy();
2214           //    if (fHitMap[1]->TestHit(ix, iy) == kEmpty) continue;
2215           fSeg2[1]->GetPadC(fInput->DetElemId(),ix,iy,upper[2],ydum,zdum);      
2216           if (icount ==0) lower[2]=upper[2];
2217           icount++;
2218         }
2219       if (lower[2]>upper[2]) {xdum=lower[2]; lower[2]=upper[2]; upper[2]=xdum;}    
2220       //    vstart[2] = 0.5*(lower[2]+upper[2]);
2221
2222       icount=0;
2223     
2224       for (fSeg2[0]->FirstPad(fInput->DetElemId(),fXInit[1], fYInit[1], fZPlane, 0, dpy); 
2225            fSeg2[0]-> MorePads(fInput->DetElemId()); 
2226            fSeg2[0]->NextPad(fInput->DetElemId()))
2227         {
2228           ix=fSeg2[0]->Ix(); iy=fSeg2[0]->Iy();
2229           //    if (fHitMap[0]->TestHit(ix, iy) != kEmpty) continue;
2230         
2231           fSeg2[0]->GetPadC(fInput->DetElemId(),ix,iy,xdum,upper[3],zdum);      
2232           if (icount ==0) lower[3]=upper[3];
2233           icount++;
2234
2235         }
2236       if (lower[3]>upper[3]) {xdum=lower[3]; lower[3]=upper[3]; upper[3]=xdum;}  
2237     }
2238     lower[4]=0.;
2239     upper[4]=1.;
2240     lower[5]=0.;
2241     upper[5]=1.;
2242
2243 // step sizes
2244     static Double_t step[6]={0.0005, 0.0005, 0.0005, 0.0005, 0.001, 0.001};
2245     clusterInput.Fitter()->mnparm(0,"x1",vstart[0],step[0],lower[0],upper[0],ierflag);
2246     clusterInput.Fitter()->mnparm(1,"y1",vstart[1],step[1],lower[1],upper[1],ierflag);
2247     clusterInput.Fitter()->mnparm(2,"x2",vstart[2],step[2],lower[2],upper[2],ierflag);
2248     clusterInput.Fitter()->mnparm(3,"y2",vstart[3],step[3],lower[3],upper[3],ierflag);
2249     clusterInput.Fitter()->mnparm(4,"a0",vstart[4],step[4],lower[4],upper[4],ierflag);
2250     clusterInput.Fitter()->mnparm(5,"a1",vstart[5],step[5],lower[5],upper[5],ierflag);
2251 // ready for minimisation       
2252     arglist[0]= -1;
2253     arglist[1]= 0;
2254     
2255     clusterInput.Fitter()->mnexcm("SET NOGR", arglist, 0, ierflag);
2256     clusterInput.Fitter()->mnexcm("MIGRAD", arglist, 0, ierflag);
2257     //    clusterInput.Fitter()->mnexcm("EXIT" , arglist, 0, ierflag);
2258 // Get fitted parameters
2259     TString chname;
2260     Double_t epxz, b1, b2;
2261     Int_t ierflg;
2262     clusterInput.Fitter()->mnpout(0, chname, fXFit[0],  epxz, b1, b2, ierflg);  
2263     clusterInput.Fitter()->mnpout(1, chname, fYFit[0],  epxz, b1, b2, ierflg);  
2264     clusterInput.Fitter()->mnpout(2, chname, fXFit[1],  epxz, b1, b2, ierflg);  
2265     clusterInput.Fitter()->mnpout(3, chname, fYFit[1],  epxz, b1, b2, ierflg);  
2266     clusterInput.Fitter()->mnpout(4, chname, fQrFit[0], epxz, b1, b2, ierflg);  
2267     clusterInput.Fitter()->mnpout(5, chname, fQrFit[1], epxz, b1, b2, ierflg);  
2268
2269     Double_t fmin, fedm, errdef;
2270     Int_t   npari, nparx, istat;
2271       
2272     clusterInput.Fitter()->mnstat(fmin, fedm, errdef, npari, nparx, istat);  
2273     fFitStat=istat;
2274     
2275     fChi2[0]=fmin;
2276     fChi2[1]=fmin;
2277     return fmin;
2278 }
2279
2280 void AliMUONClusterFinderVS::Split(AliMUONRawCluster* c)
2281 {
2282 //
2283 // One cluster for each maximum
2284 //
2285     Int_t i, j, cath;
2286     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());
2287     for (j=0; j<2; j++) {
2288         AliMUONRawCluster cnew;
2289         cnew.SetGhost(c->GetGhost());
2290         for (cath=0; cath<2; cath++) {
2291             cnew.SetChi2(cath,fChi2[0]);
2292             // ?? why not cnew.fChi2[cath]=fChi2[cath];
2293             
2294             if (fNPeaks == 0) {
2295                 cnew.SetNcluster(0,-1);
2296                 cnew.SetNcluster(1,fNRawClusters);
2297             } else {
2298                 cnew.SetNcluster(0,fNPeaks);
2299                 cnew.SetNcluster(1,0);
2300             }
2301             cnew.SetMultiplicity(cath,0);
2302             cnew.SetX(cath, Float_t(fXFit[j]));
2303             cnew.SetY(cath, Float_t(fYFit[j]));
2304             cnew.SetZ(cath, fZPlane);
2305             if (j==0) {
2306                 cnew.SetCharge(cath, Int_t(clusterInput.TotalCharge(cath)*fQrFit[cath]));
2307             } else {
2308                 cnew.SetCharge(cath, Int_t(clusterInput.TotalCharge(cath)*(1-fQrFit[cath])));
2309             }
2310             if (fSegmentationType == 1) 
2311               fSeg[cath]->SetHit(fXFit[j],fYFit[j],fZPlane);
2312             else 
2313               fSeg2[cath]->SetHit(fInput->DetElemId(), fXFit[j],fYFit[j],fZPlane);
2314
2315             for (i=0; i<fMul[cath]; i++) {
2316               Float_t q1;
2317                 cnew.SetIndex(cnew.GetMultiplicity(cath), cath, c->GetIndex(i,cath));
2318                 if (fSegmentationType == 1) {
2319                   fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
2320                   q1 = fInput->Mathieson()->IntXY(fSeg[cath]);
2321                 } else {
2322                   fSeg2[cath]->SetPad(fInput->DetElemId(),fIx[i][cath], fIy[i][cath]);
2323                   q1 = fInput->Mathieson()->IntXY(fInput->DetElemId(),fSeg2[cath]);
2324                 }
2325                 cnew.SetContrib(i, cath, q1*Float_t(cnew.GetCharge(cath))/Float_t(fQ[i][cath]));
2326                 cnew.SetMultiplicity(cath, cnew.GetMultiplicity(cath)+1 );
2327             }
2328             FillCluster(&cnew,0,cath);
2329         } // cathode loop
2330         cnew.SetClusterType(cnew.PhysicsContribution());
2331         if (cnew.GetCharge(0)>0 && cnew.GetCharge(1)>0) AddRawCluster(cnew);
2332         fNPeaks++;
2333     }
2334 }
2335 void AliMUONClusterFinderVS::AddRawCluster(AliMUONRawCluster& c)
2336 {
2337   //
2338   // Add a raw cluster copy to the list
2339   //
2340   //     AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2341   //     pMUON->GetMUONData()->AddRawCluster(fInput->Chamber(),c); 
2342   //     fNRawClusters++;
2343   
2344   // Setting detection element in raw cluster for alignment
2345   // BB 19/05/05
2346   c.SetDetElementID(fInput->DetElemId());
2347   
2348   TClonesArray &lrawcl = *fRawClusters;
2349   new(lrawcl[fNRawClusters++]) AliMUONRawCluster(c);
2350   AliDebug(1,Form("\nfNRawClusters %d\n",fNRawClusters));
2351 }
2352
2353 AliMUONClusterFinderVS& AliMUONClusterFinderVS
2354 ::operator = (const AliMUONClusterFinderVS& rhs)
2355 {
2356 // Protected assignement operator
2357
2358   if (this == &rhs) return *this;
2359
2360   AliFatal("Not implemented.");
2361     
2362   return *this;  
2363 }
2364
2365 //
2366 // Minimisation functions
2367 // Single Mathieson
2368 void fcnS1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2369 {
2370     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2371     Int_t i;
2372     Float_t delta;
2373     Float_t chisq=0;
2374     Float_t qcont=0;
2375     Float_t qtot=0;
2376
2377     for (i=0; i<clusterInput.Nmul(0); i++) {
2378         Float_t q0=clusterInput.Charge(i,0);
2379         Float_t q1=clusterInput.DiscrChargeS1(i,par);
2380         delta=(q0-q1)/q0;
2381         chisq+=delta*delta;
2382         qcont+=q1;
2383         qtot+=q0;
2384     }
2385     f=chisq;
2386 }
2387
2388 void fcnCombiS1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2389 {
2390     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2391     Int_t i, cath;
2392     Float_t delta;
2393     Float_t chisq=0;
2394     Float_t qcont=0;
2395     Float_t qtot=0;
2396
2397     for (cath=0; cath<2; cath++) {
2398         for (i=0; i<clusterInput.Nmul(cath); i++) {
2399             Float_t q0=clusterInput.Charge(i,cath);
2400             Float_t q1=clusterInput.DiscrChargeCombiS1(i,par,cath);
2401             delta=(q0-q1)/q0;
2402             chisq+=delta*delta;
2403             qcont+=q1;
2404             qtot+=q0;
2405         }
2406     }
2407     f=chisq;
2408 }
2409
2410 // Double Mathieson
2411 void fcnS2(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2412 {
2413     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2414     Int_t i;
2415     Float_t delta;
2416     Float_t chisq=0;
2417     Float_t qcont=0;
2418     Float_t qtot=0;
2419     
2420     for (i=0; i<clusterInput.Nmul(0); i++) {
2421
2422         Float_t q0=clusterInput.Charge(i,0);
2423         Float_t q1=clusterInput.DiscrChargeS2(i,par);
2424         delta=(q0-q1)/q0;
2425         chisq+=delta*delta;
2426         qcont+=q1;
2427         qtot+=q0;
2428     }
2429     f=chisq;
2430 }
2431
2432 // Double Mathieson
2433 void fcnCombiS2(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2434 {
2435     AliMUONClusterInput& clusterInput = *(AliMUONClusterInput::Instance());    
2436     Int_t i, cath;
2437     Float_t delta;
2438     Float_t chisq=0;
2439     Float_t qcont=0;
2440     Float_t qtot=0;
2441     for (cath=0; cath<2; cath++) {
2442         for (i=0; i<clusterInput.Nmul(cath); i++) {
2443             Float_t q0=clusterInput.Charge(i,cath);
2444             Float_t q1=clusterInput.DiscrChargeCombiS2(i,par,cath);
2445             delta=(q0-q1)/q0;
2446             chisq+=delta*delta;
2447             qcont+=q1;
2448             qtot+=q0;
2449         }
2450     }
2451     f=chisq;
2452 }