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