]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONClusterFinderAZ.cxx
Using Config file by default and updateing README (Gines)
[u/mrichter/AliRoot.git] / MUON / AliMUONClusterFinderAZ.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 // Clusterizer class developped by Zitchenko (Dubna)
19
20 #include <stdlib.h>
21 #include <Riostream.h>
22 #include <TROOT.h>
23 #include <TCanvas.h>
24 #include <TLine.h>
25 #include <TTree.h>
26 #include <TH2.h>
27 #include <TView.h>
28 #include <TStyle.h>
29 #include <TMinuit.h>
30 #include <TMatrixD.h>
31
32 #include "AliMUONClusterFinderAZ.h"
33 #include "AliHeader.h"
34 #include "AliRun.h"
35 #include "AliMUON.h"
36 #include "AliMUONChamber.h"
37 #include "AliMUONDigit.h"
38 #include "AliMUONHit.h"
39 #include "AliMUONChamber.h"
40 #include "AliMUONRawCluster.h"
41 #include "AliMUONClusterInput.h"
42 #include "AliMUONPixel.h"
43 #include "AliMC.h"
44 #include "AliLog.h"
45
46 ClassImp(AliMUONClusterFinderAZ)
47  
48  const Double_t AliMUONClusterFinderAZ::fgkCouplMin = 1.e-3; // threshold on coupling 
49  AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = 0x0;
50  TMinuit* AliMUONClusterFinderAZ::fgMinuit = 0x0;
51
52 //_____________________________________________________________________________
53 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw, Int_t iReco)
54   : AliMUONClusterFinderVS()
55 {
56 // Constructor
57   for (Int_t i=0; i<4; i++) {fHist[i] = 0;}
58   fMuonDigits = 0;
59   fSegmentation[1] = fSegmentation[0] = 0; 
60   fgClusterFinder = 0x0;
61   fgMinuit = 0x0; 
62   if (!fgClusterFinder) fgClusterFinder = this;
63   if (!fgMinuit) fgMinuit = new TMinuit(8);
64   fDraw = draw;
65   fReco = iReco;
66   fPixArray = new TObjArray(20); 
67   /*
68   fPoints = 0;
69   fPhits = 0;
70   fRpoints = 0;
71   fCanvas = 0;
72   fNextCathode = kFALSE; 
73   fColPad = 0;
74   */
75 }
76
77 //_____________________________________________________________________________
78 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(const AliMUONClusterFinderAZ& rhs)
79   : AliMUONClusterFinderVS(rhs)
80 {
81 // Protected copy constructor
82
83   AliFatal("Not implemented.");
84 }
85
86 //_____________________________________________________________________________
87 AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ()
88 {
89   // Destructor
90   delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
91   /*
92   // Delete space point structure
93   if (fPoints) fPoints->Delete();
94   delete fPoints;
95   fPoints     = 0;
96   //
97   if (fPhits) fPhits->Delete();
98   delete fPhits;
99   fPhits     = 0;
100   //
101   if (fRpoints) fRpoints->Delete();
102   delete fRpoints;
103   fRpoints     = 0;
104   */
105 }
106
107 //_____________________________________________________________________________
108 void AliMUONClusterFinderAZ::FindRawClusters()
109 {
110 // To provide the same interface as in AliMUONClusterFinderVS
111
112   EventLoop (gAlice->GetHeader()->GetEvent(), AliMUONClusterInput::Instance()->Chamber());
113 }
114
115 //_____________________________________________________________________________
116 void AliMUONClusterFinderAZ::EventLoop(Int_t nev=0, Int_t ch=0)
117 {
118 // Loop over events
119   
120   FILE *lun = 0;
121   TCanvas *c1 = 0;
122   TView *view = 0;
123   TH2F *hist = 0;
124   Double_t p1[3]={0}, p2[3];
125   TTree *treeR = 0;
126   if (fDraw) {
127     // File
128     lun = fopen("pool.dat","w");
129     c1 = new TCanvas("c1","Clusters",0,0,600,700);
130     c1->Divide(1,2);
131     new TCanvas("c2","Mlem",700,0,600,350);
132   }
133
134 newev:
135   Int_t nparticles = 0, nent;
136
137   //Loaders
138   AliRunLoader * rl = AliRunLoader::GetRunLoader();
139   AliLoader * gime  = rl->GetLoader("MUONLoader");
140
141   if (!fReco) nparticles = rl->GetEvent(nev);
142   else nparticles = gAlice->GetMCApp()->GetNtrack();
143   AliInfo(Form("nev         %d",nev));
144   AliInfo(Form("nparticles  %d",nparticles));
145   if (nparticles <= 0) return;
146   
147   TTree *treeH = gime->TreeH();
148   Int_t ntracks = (Int_t) treeH->GetEntries();
149   AliInfo(Form("ntracks  %d",ntracks));
150     
151   // Get pointers to Alice detectors and Digits containers
152   AliMUON *muon  = (AliMUON*) gAlice->GetModule("MUON");
153   if (!muon) return;
154   //       TClonesArray *Particles = gAlice->Particles();
155   if (!fReco) {
156     treeR = gime->TreeR();
157     if (treeR) {
158       muon->ResetRawClusters();
159       nent = (Int_t) treeR->GetEntries();
160       if (nent != 1) {
161                 AliError(Form("nent = %d not equal to 1",nent));
162         //exit(0);
163       }
164     } // if (treeR)
165   } // if (!fReco)
166
167   TTree *treeD = gime->TreeD();
168   //muon->ResetDigits();
169
170   TClonesArray *listMUONrawclust ;
171   AliMUONChamber*       iChamber = 0;
172   
173   // As default draw the first cluster of the chamber #0
174       
175 newchamber:
176   if (ch > 9) {if (fReco) return; nev++; ch = 0; goto newev;}
177   //gAlice->ResetDigits();
178   fMuonDigits  = muon->GetMUONData()->Digits(ch);
179   if (fMuonDigits == 0) return;
180   iChamber = &(muon->Chamber(ch));
181   fSegmentation[0] = iChamber->SegmentationModel(1);
182   fSegmentation[1] = iChamber->SegmentationModel(2);
183   fResponse = iChamber->ResponseModel();
184     
185   nent = 0;
186  
187   if (treeD) {
188     nent = (Int_t) treeD->GetEntries();
189     //printf(" entries %d \n", nent);
190   }
191
192   Int_t ndigits[2]={9,9}, nShown[2]={0};
193   for (Int_t i=0; i<2; i++) {
194     for (Int_t j=0; j<fgkDim; j++) {fUsed[i][j]=kFALSE;}
195   }
196
197 next:
198   if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) {
199     // No more clusters
200     if (fReco) return;
201     ch++;
202     goto newchamber; // next chamber
203   }
204   Float_t xpad, ypad, zpad, zpad0;
205   TLine *line[99]={0};
206   Int_t nLine = 0;
207   Bool_t first = kTRUE;
208   AliInfo(Form(" *** Event # %d chamber: %d " , nev ,ch ));
209   fnPads[0] = fnPads[1] = 0;
210   for (Int_t i=0; i<fgkDim; i++) {fPadIJ[1][i] = 0;}
211   //for (Int_t iii = 0; iii<999; iii++) { 
212   for (Int_t iii = 0; iii<2; iii++) { 
213     Int_t cath = TMath::Odd(iii);
214     gAlice->ResetDigits();
215     treeD->GetEvent(cath);
216     fMuonDigits  = muon->GetMUONData()->Digits(ch);
217
218     ndigits[cath] = fMuonDigits->GetEntriesFast();
219     if (!ndigits[0] && !ndigits[1]) {if (fReco) return; ch++; goto newchamber;}
220     if (ndigits[cath] == 0) continue;
221     AliInfo(Form(" ndigits: %d  %d " , ndigits[cath] , cath));
222
223     AliMUONDigit  *mdig;
224     Int_t digit;
225
226     Bool_t eEOC = kTRUE; // end-of-cluster
227     for (digit = 0; digit < ndigits[cath]; digit++) {
228       mdig    = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
229       if (mdig->Cathode() != cath) continue;
230       if (first) {
231         // Find first unused pad
232         if (fUsed[cath][digit]) continue;
233         fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0);
234       } else {
235         if (fUsed[cath][digit]) continue;
236         fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
237         if (TMath::Abs(zpad-zpad0)>0.1) continue; // different slats
238         // Find a pad overlapping with the cluster
239         if (!Overlap(cath,mdig)) continue;
240       }
241       // Add pad - recursive call
242       AddPad(cath,digit);
243       eEOC = kFALSE;
244       if (digit >= 0) break;
245     }
246     if (first && eEOC) {
247       // No more unused pads 
248       if (cath == 0) continue; // on cathode #0 - check #1
249       else {
250         // No more clusters
251         if (fReco) return;
252         ch++;
253         goto newchamber; // next chamber
254       }
255     }
256     if (eEOC) break; // cluster found
257     first = kFALSE;
258         AliInfo(Form(" nPads: %d %d %d ",fnPads[cath] ,nShown[cath]+fnPads[cath],cath));
259   } // for (Int_t iii = 0;
260
261   
262   if (fReco) goto skip;
263   char hName[4];
264   for (Int_t cath = 0; cath<2; cath++) {
265     // Build histograms
266     if (fHist[cath*2]) {fHist[cath*2]->Delete(); fHist[cath*2] = 0;}
267     if (fHist[cath*2+1]) {fHist[cath*2+1]->Delete(); fHist[cath*2+1] = 0;}
268     if (fnPads[cath] == 0) continue; // cluster on one cathode only
269     Float_t wxMin=999, wxMax=0, wyMin=999, wyMax=0; 
270     Int_t minDx=0, maxDx=0, minDy=0, maxDy=0;
271     for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
272       if (fPadIJ[0][i] != cath) continue;
273       if (fXyq[3][i] < wxMin) {wxMin = fXyq[3][i]; minDx = i;}
274       if (fXyq[3][i] > wxMax) {wxMax = fXyq[3][i]; maxDx = i;}
275       if (fXyq[4][i] < wyMin) {wyMin = fXyq[4][i]; minDy = i;}
276       if (fXyq[4][i] > wyMax) {wyMax = fXyq[4][i]; maxDy = i;}
277     }
278     AliInfo(Form("%d %d %d %d", minDx , maxDx , minDy , maxDy ));
279     Int_t nx, ny, padSize;
280     Float_t xmin=9999, xmax=-9999, ymin=9999, ymax=-9999;
281     if (TMath::Nint(fXyq[3][minDx]*1000) == TMath::Nint(fXyq[3][maxDx]*1000) &&
282         TMath::Nint(fXyq[4][minDy]*1000) == TMath::Nint(fXyq[4][maxDy]*1000)) {
283       // the same segmentation
284       AliInfo(" Same");
285       AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
286       for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
287         if (fPadIJ[0][i] != cath) continue;
288         if (fXyq[0][i] < xmin) xmin = fXyq[0][i];
289         if (fXyq[0][i] > xmax) xmax = fXyq[0][i];
290         if (fXyq[1][i] < ymin) ymin = fXyq[1][i];
291         if (fXyq[1][i] > ymax) ymax = fXyq[1][i];
292       }
293       xmin -= fXyq[3][minDx]; xmax += fXyq[3][minDx];
294       ymin -= fXyq[4][minDy]; ymax += fXyq[4][minDy];
295       nx = TMath::Nint ((xmax-xmin)/wxMin/2);
296       ny = TMath::Nint ((ymax-ymin)/wyMin/2);
297       sprintf(hName,"h%d",cath*2);
298       fHist[cath*2] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
299       cout << fHist[cath*2] << " " << fnPads[cath] << endl;
300       for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
301         if (fPadIJ[0][i] != cath) continue;
302         fHist[cath*2]->Fill(fXyq[0][i],fXyq[1][i],fXyq[2][i]);
303         //cout << fXyq[0][i] << fXyq[1][i] << fXyq[2][i] << endl;
304       }
305     } else {
306       // different segmentation in the cluster
307       AliInfo(" Different\n");
308       AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
309       Int_t nOK = 0;
310       Int_t indx, locMin, locMax;
311       if (TMath::Nint(fXyq[3][minDx]*1000) != TMath::Nint(fXyq[3][maxDx]*1000)) {
312         // different segmentation along x
313         indx = 0;
314         locMin = minDx;
315         locMax = maxDx;
316       } else {
317         // different segmentation along y
318         indx = 1;
319         locMin = minDy;
320         locMax = maxDy;
321       }
322       Int_t loc = locMin;
323       for (Int_t i=0; i<2; i++) {
324         // loop over different pad sizes
325         if (i>0) loc = locMax;
326         padSize = TMath::Nint(fXyq[indx+3][loc]*1000);
327         xmin = 9999; xmax = -9999; ymin = 9999; ymax = -9999;
328         for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
329           if (fPadIJ[0][j] != cath) continue;
330           if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
331           nOK++;
332           xmin = TMath::Min (xmin,fXyq[0][j]);
333           xmax = TMath::Max (xmax,fXyq[0][j]);
334           ymin = TMath::Min (ymin,fXyq[1][j]);
335           ymax = TMath::Max (ymax,fXyq[1][j]);
336         }
337         xmin -= fXyq[3][loc]; xmax += fXyq[3][loc];
338         ymin -= fXyq[4][loc]; ymax += fXyq[4][loc];
339         nx = TMath::Nint ((xmax-xmin)/fXyq[3][loc]/2);
340         ny = TMath::Nint ((ymax-ymin)/fXyq[4][loc]/2);
341         sprintf(hName,"h%d",cath*2+i);
342         fHist[cath*2+i] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
343         for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
344           if (fPadIJ[0][j] != cath) continue;
345           if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
346           fHist[cath*2+i]->Fill(fXyq[0][j],fXyq[1][j],fXyq[2][j]);
347         }
348       } // for (Int_t i=0;
349       if (nOK != fnPads[cath]) 
350                 AliInfo(Form(" *** Too many segmentations: nPads, nOK %d %d",fnPads[cath],nOK));
351     } // if (TMath::Nint(fXyq[3][minDx]*1000)
352   } // for (Int_t cath = 0;
353         
354   // Draw histograms and coordinates
355   for (Int_t cath=0; cath<2; cath++) {
356     if (cath == 0) ModifyHistos();
357     if (fnPads[cath] == 0) continue; // cluster on one cathode only
358     if (fDraw) {
359       c1->cd(cath+1);
360       gPad->SetTheta(55);
361       gPad->SetPhi(30);
362       Double_t x, y, x0, y0, r1=999, r2=0;
363       if (fHist[cath*2+1]) {
364         // 
365         x0 = fHist[cath*2]->GetXaxis()->GetXmin() - 1000*TMath::Cos(30*TMath::Pi()/180);
366         y0 = fHist[cath*2]->GetYaxis()->GetXmin() - 1000*TMath::Sin(30*TMath::Pi()/180);
367         r1 = 0;
368         Int_t ihist=cath*2;
369         for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
370           y = fHist[ihist]->GetYaxis()->GetBinCenter(iy) 
371             + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
372           for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
373             if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
374               x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
375                 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
376               r1 = TMath::Max (r1,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
377             }
378           }
379         }
380         ihist = cath*2 + 1 ;
381         for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
382           y = fHist[ihist]->GetYaxis()->GetBinCenter(iy)
383             + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
384           for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
385             if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
386               x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
387                 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
388               r2 = TMath::Max (r2,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
389             }
390           }
391         }
392         AliInfo(Form("%f %f \n",r1,r2));
393       } // if (fHist[cath*2+1])
394       if (r1 > r2) {
395         //fHist[cath*2]->Draw("lego1");
396         fHist[cath*2]->Draw("lego1Fb");
397         //if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBb");
398         if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBbFb");
399       } else {
400         //fHist[cath*2+1]->Draw("lego1");
401         fHist[cath*2+1]->Draw("lego1Fb");
402         //fHist[cath*2]->Draw("lego1SameAxisBb");
403         fHist[cath*2]->Draw("lego1SameAxisFbBb");
404       }
405       c1->Update();
406     } // if (fDraw)
407   } // for (Int_t cath = 0;
408
409   // Draw generated hits
410   Double_t xNDC[6];
411   hist = fHist[0] ? fHist[0] : fHist[2];
412   p2[2] = hist->GetMaximum();
413   view = 0;
414   if (c1) view = c1->Pad()->GetView();
415   AliInfo(" *** GEANT hits *** ");
416   fnMu = 0;
417   Int_t ix, iy, iok;
418   for (Int_t i=0; i<ntracks; i++) {
419     treeH->GetEvent(i);
420     for (AliMUONHit* mHit=(AliMUONHit*)muon->FirstHit(-1); 
421          mHit;
422          mHit=(AliMUONHit*)muon->NextHit()) {
423       if (mHit->Chamber() != ch+1) continue;  // chamber number
424       if (TMath::Abs(mHit->Z()-zpad0) > 1) continue; // different slat
425       p2[0] = p1[0] = mHit->X();        // x-pos of hit
426       p2[1] = p1[1] = mHit->Y();        // y-pos
427       if (p1[0] < hist->GetXaxis()->GetXmin() || 
428           p1[0] > hist->GetXaxis()->GetXmax()) continue;
429       if (p1[1] < hist->GetYaxis()->GetXmin() || 
430           p1[1] > hist->GetYaxis()->GetXmax()) continue;
431       // Check if track comes thru pads with signal
432       iok = 0;
433       for (Int_t ihist=0; ihist<4; ihist++) {
434         if (!fHist[ihist]) continue;
435         ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
436         iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
437         if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
438       }
439       if (!iok) continue;
440       gStyle->SetLineColor(1);
441       if (TMath::Abs((Int_t)mHit->Particle()) == 13) {
442         gStyle->SetLineColor(4);
443         fnMu++;
444         if (fnMu <= 2) {
445           fxyMu[fnMu-1][0] = p1[0];
446           fxyMu[fnMu-1][1] = p1[1];
447         }
448       }     
449       AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mHit->Z()));
450       if (view) {
451         view->WCtoNDC(p1, &xNDC[0]);
452         view->WCtoNDC(p2, &xNDC[3]);
453         for (Int_t ipad=1; ipad<3; ipad++) {
454           c1->cd(ipad);
455           //c1->DrawLine(xpad[0],xpad[1],xpad[3],xpad[4]);
456           line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
457           line[nLine++]->Draw();
458         }
459       }
460     } // for (AliMUONHit* mHit=
461   } // for (Int_t i=0; i<ntracks;
462
463   // Draw reconstructed coordinates
464   listMUONrawclust  = muon->GetMUONData()->RawClusters(ch);
465   treeR->GetEvent(ch);
466   //cout << listMUONrawclust  << " " << listMUONrawclust ->GetEntries() << endl;
467   AliMUONRawCluster *mRaw;
468   gStyle->SetLineColor(3);
469   AliInfo(" *** Reconstructed hits *** ");
470   for (Int_t i=0; i<listMUONrawclust ->GetEntries(); i++) {
471     mRaw = (AliMUONRawCluster*)listMUONrawclust ->UncheckedAt(i);
472     if (TMath::Abs(mRaw->GetZ(0)-zpad0) > 1) continue; // different slat
473     p2[0] = p1[0] = mRaw->GetX(0);        // x-pos of hit
474     p2[1] = p1[1] = mRaw->GetY(0);        // y-pos
475     if (p1[0] < hist->GetXaxis()->GetXmin() || 
476         p1[0] > hist->GetXaxis()->GetXmax()) continue;
477     if (p1[1] < hist->GetYaxis()->GetXmin() || 
478         p1[1] > hist->GetYaxis()->GetXmax()) continue;
479     /*
480       treeD->GetEvent(cath);
481       cout << mRaw->fMultiplicity[0] << mRaw->fMultiplicity[1] << endl;
482       for (Int_t j=0; j<mRaw->fMultiplicity[cath]; j++) {
483       Int_t digit = mRaw->fIndexMap[j][cath];
484       cout << ((AliMUONDigit*)fMuonDigits->UncheckedAt(digit))->Signal() << endl;
485       }
486     */
487     // Check if track comes thru pads with signal
488     iok = 0;
489     for (Int_t ihist=0; ihist<4; ihist++) {
490       if (!fHist[ihist]) continue;
491       ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
492       iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
493       if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
494     }
495     if (!iok) continue;
496     AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mRaw->GetZ(0)));
497     if (view) {
498       view->WCtoNDC(p1, &xNDC[0]);
499       view->WCtoNDC(p2, &xNDC[3]);
500       for (Int_t ipad=1; ipad<3; ipad++) {
501         c1->cd(ipad);
502         line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
503         line[nLine++]->Draw();
504       }
505     }
506   } // for (Int_t i=0; i<listMUONrawclust ->GetEntries();
507   if (fDraw) c1->Update();
508
509 skip:
510   // Use MLEM for cluster finder
511   fZpad = zpad0;
512   Int_t nMax = 1, localMax[100], maxPos[100];
513   Double_t maxVal[100];
514   
515   if (CheckPrecluster(nShown)) {
516     BuildPixArray();
517     if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(localMax, maxVal);
518     if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
519     for (Int_t i=0; i<nMax; i++) {
520       if (nMax > 1) FindCluster(localMax, maxPos[i]);
521       if (!MainLoop()) AliInfo(" MainLoop failed ");
522       if (i < nMax-1) {
523         for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
524           if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
525           fPadIJ[1][j] = 0;
526           fXyq[2][j] = fXyq[5][j]; // use backup charge value
527         }
528       }
529     }
530   }
531   if (fReco) goto next;
532
533   for (Int_t i=0; i<fnMu; i++) {
534     // Check again if muon come thru the used pads (due to extra splitting)
535     for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
536       if (TMath::Abs(fxyMu[i][0]-fXyq[0][j])<fXyq[3][j] && 
537           TMath::Abs(fxyMu[i][1]-fXyq[1][j])<fXyq[4][j]) {
538         AliInfo(Form("%12.3e %12.3e %12.3e %12.3e\n",fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]));
539         if (lun) fprintf(lun,"%4d %2d %12.3e %12.3e %12.3e %12.3e\n",nev,ch,fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]);
540         break;
541       }
542     }
543   } // for (Int_t i=0; i<fnMu;
544
545   // What's next?
546   char command[8];
547   AliInfo(" What is next? ");
548   command[0] = ' '; 
549   if (fDraw) gets(command);
550   if (command[0] == 'n' || command[0] == 'N') {nev++; goto newev;} // next event 
551   else if (command[0] == 'q' || command[0] == 'Q') {fclose(lun); return;} // exit display 
552   //else if (command[0] == 'r' || command[0] == 'R') goto redraw; // redraw points
553   else if (command[0] == 'c' || command[0] == 'C') {
554     // new chamber
555     sscanf(command+1,"%d",&ch);
556     goto newchamber;
557   } 
558   else if (command[0] == 'e' || command[0] == 'E') {
559     // new event
560     sscanf(command+1,"%d",&nev);
561     goto newev;
562   } 
563   else goto next; // Next cluster
564 }
565
566 //_____________________________________________________________________________
567 void AliMUONClusterFinderAZ::ModifyHistos(void)
568 {
569   // Modify histograms to bring them to the same size
570   Int_t nhist = 0;
571   Float_t hlim[4][4], hbin[4][4]; // first index - xmin, xmax, ymin, ymax
572   Float_t binMin[4] = {999,999,999,999};
573
574   for (Int_t i=0; i<4; i++) {
575     if (!fHist[i]) continue;
576     hlim[0][nhist] = fHist[i]->GetXaxis()->GetXmin(); // xmin
577     hlim[1][nhist] = fHist[i]->GetXaxis()->GetXmax(); // xmax
578     hlim[2][nhist] = fHist[i]->GetYaxis()->GetXmin(); // ymin
579     hlim[3][nhist] = fHist[i]->GetYaxis()->GetXmax(); // ymax
580     hbin[0][nhist] = hbin[1][nhist] = fHist[i]->GetXaxis()->GetBinWidth(1);
581     hbin[2][nhist] = hbin[3][nhist] = fHist[i]->GetYaxis()->GetBinWidth(1);
582     binMin[0] = TMath::Min(binMin[0],hbin[0][nhist]);
583     binMin[2] = TMath::Min(binMin[2],hbin[2][nhist]);
584     nhist++;
585   }
586   binMin[1] = binMin[0];
587   binMin[3] = binMin[2];
588   AliInfo(Form(" Nhist: %d",nhist));
589
590   Int_t imin, imax;
591   for (Int_t lim=0; lim<4; lim++) {
592     while (1) {
593       imin = TMath::LocMin(nhist,hlim[lim]);
594       imax = TMath::LocMax(nhist,hlim[lim]);
595       if (TMath::Abs(hlim[lim][imin]-hlim[lim][imax])<0.01*binMin[lim]) break;
596       if (lim == 0 || lim == 2) {
597         // find lower limit
598         hlim[lim][imax] -= hbin[lim][imax];
599       } else {
600         // find upper limit
601         hlim[lim][imin] += hbin[lim][imin];
602       }
603     } // while (1)
604   }
605     
606   // Rebuild histograms 
607   nhist = 0;
608   TH2F *hist = 0;
609   Int_t nx, ny;
610   Double_t x, y, cont, cmax=0;
611   char hName[4];
612   for (Int_t ihist=0; ihist<4; ihist++) {
613     if (!fHist[ihist]) continue;
614     nx = TMath::Nint((hlim[1][nhist]-hlim[0][nhist])/hbin[0][nhist]);
615     ny = TMath::Nint((hlim[3][nhist]-hlim[2][nhist])/hbin[2][nhist]);
616     //hist =  new TH2F("h","hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
617     sprintf(hName,"hh%d",ihist);
618     hist =  new TH2F(hName,"hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
619     for (Int_t i=1; i<=fHist[ihist]->GetNbinsX(); i++) {
620       x = fHist[ihist]->GetXaxis()->GetBinCenter(i);
621       for (Int_t j=1; j<=fHist[ihist]->GetNbinsY(); j++) {
622         y = fHist[ihist]->GetYaxis()->GetBinCenter(j);
623         cont = fHist[ihist]->GetCellContent(i,j);
624         hist->Fill(x,y,cont);
625       }
626     }
627     cmax = TMath::Max (cmax,hist->GetMaximum());
628     fHist[ihist]->Delete();
629     fHist[ihist] = new TH2F(*hist);
630     hist->Delete(); 
631     nhist++;
632   }
633   AliInfo(Form("%f \n",cmax));
634
635   for (Int_t ihist=0; ihist<4; ihist++) {
636     if (!fHist[ihist]) continue;
637     fHist[ihist]->SetMaximum(cmax);
638   }
639 }
640
641 //_____________________________________________________________________________
642 void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
643 {
644   // Add pad to the cluster
645   AliMUONDigit *mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
646
647   Int_t charge = mdig->Signal();
648   // get the center of the pad
649   Float_t xpad, ypad, zpad;
650   fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
651   
652   Int_t   isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
653   Int_t nPads = fnPads[0] + fnPads[1];
654   fXyq[0][nPads] = xpad;
655   fXyq[1][nPads] = ypad;
656   fXyq[2][nPads] = charge;
657   fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2;
658   fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2;
659   fXyq[5][nPads] = digit;
660   fPadIJ[0][nPads] = cath;
661   fPadIJ[1][nPads] = 0;
662   fUsed[cath][digit] = kTRUE;
663   //cout << " bbb " << fXyq[cath][2][nPads] << " " << fXyq[cath][0][nPads] << " " << fXyq[cath][1][nPads] << " " << fXyq[cath][3][nPads] << " " << fXyq[cath][4][nPads] << " " << zpad << " " << nPads << endl;
664   fnPads[cath]++;
665
666   // Check neighbours
667   Int_t nn, ix, iy, xList[10], yList[10];
668   AliMUONDigit  *mdig1;
669
670   Int_t ndigits = fMuonDigits->GetEntriesFast();
671   fSegmentation[cath]->Neighbours(mdig->PadX(),mdig->PadY(),&nn,xList,yList); 
672   for (Int_t in=0; in<nn; in++) {
673     ix=xList[in];
674     iy=yList[in];
675     for (Int_t digit1 = 0; digit1 < ndigits; digit1++) {
676       if (digit1 == digit) continue;
677       mdig1 = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit1);
678       if (mdig1->Cathode() != cath) continue;
679       if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) {
680         fUsed[cath][digit1] = kTRUE;
681         // Add pad - recursive call
682         AddPad(cath,digit1);
683       }
684     } //for (Int_t digit1 = 0;
685   } // for (Int_t in=0;
686 }
687
688 //_____________________________________________________________________________
689 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, TObject *dig)
690 {
691   // Check if the pad from one cathode overlaps with a pad 
692   // in the precluster on the other cathode
693
694   AliMUONDigit *mdig = (AliMUONDigit*) dig;
695
696   Float_t xpad, ypad, zpad;
697   fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
698   Int_t   isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
699
700   Float_t xy1[4], xy12[4];
701   xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2;
702   xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec);
703   xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2;
704   xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec);
705   //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
706
707   Int_t cath1 = TMath::Even(cath);
708   for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
709     if (fPadIJ[0][i] != cath1) continue;
710     if (Overlap(xy1, i, xy12, 0)) return kTRUE;
711   }
712   return kFALSE;
713 }
714
715 //_____________________________________________________________________________
716 Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
717 {
718   // Check if the pads xy1 and iPad overlap and return overlap area
719
720   Float_t xy2[4];
721   xy2[0] = fXyq[0][iPad] - fXyq[3][iPad];
722   xy2[1] = fXyq[0][iPad] + fXyq[3][iPad];
723   if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE;
724   xy2[2] = fXyq[1][iPad] - fXyq[4][iPad];
725   xy2[3] = fXyq[1][iPad] + fXyq[4][iPad];
726   if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE;
727   if (!iSkip) return kTRUE; // just check overlap (w/out computing the area)
728   xy12[0] = TMath::Max (xy1[0],xy2[0]);
729   xy12[1] = TMath::Min (xy1[1],xy2[1]);
730   xy12[2] = TMath::Max (xy1[2],xy2[2]);
731   xy12[3] = TMath::Min (xy1[3],xy2[3]);
732   return kTRUE;
733 }
734
735 //_____________________________________________________________________________
736 /*
737 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t i, Int_t j, Float_t *xy12, Int_t iSkip)
738 {
739   // Check if the pads i and j overlap and return overlap area
740
741   Float_t xy1[4], xy2[4];
742   return Overlap(xy1, xy2, xy12, iSkip);
743 }
744 */
745 //_____________________________________________________________________________
746 Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
747 {
748   // Check precluster in order to attempt to simplify it (mostly for
749   // two-cathode preclusters)
750
751   Int_t i1, i2;
752   Float_t xy1[4], xy12[4];
753   
754   Int_t npad = fnPads[0] + fnPads[1];
755
756   // If pads have the same size take average of pads on both cathodes 
757   Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
758   if (sameSize) {
759     Double_t xSize = -1, ySize = 0;
760     for (Int_t i=0; i<npad; i++) {
761       if (fXyq[2][i] < 0) continue;
762       if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; }
763       if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 ||  TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; }
764     }
765   } // if (sameSize)
766   if (sameSize && (fnPads[0] > 2 || fnPads[1] > 2)) {
767     nShown[0] += fnPads[0];
768     nShown[1] += fnPads[1];
769     fnPads[0] = fnPads[1] = 0;
770     Int_t div;
771     for (Int_t i=0; i<npad; i++) {
772       if (fXyq[2][i] < 0) continue; // used pad
773       fXyq[2][fnPads[0]] = fXyq[2][i];
774       div = 1;
775       for (Int_t j=i+1; j<npad; j++) {
776         if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode
777         if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue;
778         if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue;
779         fXyq[2][fnPads[0]] += fXyq[2][j];
780         div = 2;
781         fXyq[2][j] = -2;
782         break;
783       }
784       fXyq[2][fnPads[0]] /= div;
785       fXyq[0][fnPads[0]] = fXyq[0][i];
786       fXyq[1][fnPads[0]] = fXyq[1][i];
787       fPadIJ[0][fnPads[0]++] = 0;
788     }
789   } // if (sameSize)
790
791   // Check if one-cathode precluster
792   i1 = fnPads[0]!=0 ? 0 : 1;
793   i2 = fnPads[1]!=0 ? 1 : 0;
794
795   if (i1 != i2) { // two-cathode 
796
797     Int_t *flags = new Int_t[npad];
798     for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
799
800     // Check pad overlaps
801     for (Int_t i=0; i<npad; i++) {
802       if (fPadIJ[0][i] != i1) continue;
803       xy1[0] = fXyq[0][i] - fXyq[3][i];
804       xy1[1] = fXyq[0][i] + fXyq[3][i];
805       xy1[2] = fXyq[1][i] - fXyq[4][i];
806       xy1[3] = fXyq[1][i] + fXyq[4][i];
807       for (Int_t j=0; j<npad; j++) {
808         if (fPadIJ[0][j] != i2) continue;
809         if (!Overlap(xy1, j, xy12, 0)) continue;
810         flags[i] = flags[j] = 1; // mark overlapped pads
811       } // for (Int_t j=0;
812     } // for (Int_t i=0;
813
814     // Check if all pads overlap
815     Int_t digit=0, cath, nFlags=0;
816     for (Int_t i=0; i<npad; i++) {nFlags += !flags[i];}
817     if (nFlags) AliInfo(Form(" nFlags = %d",nFlags));
818     //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
819     if (nFlags > 0) {
820       for (Int_t i=0; i<npad; i++) {
821         if (flags[i]) continue;
822         digit = TMath::Nint (fXyq[5][i]);
823         cath = fPadIJ[0][i];
824         fUsed[cath][digit] = kFALSE; // release pad
825         fXyq[2][i] = -2;
826         fnPads[cath]--;
827       }
828     } // if (nFlags > 2)
829
830     // Check correlations of cathode charges
831     if (fnPads[0] && fnPads[1]) { // two-cathode
832       Double_t sum[2]={0};
833       Int_t over[2] = {1, 1};
834       for (Int_t i=0; i<npad; i++) {
835         cath = fPadIJ[0][i];
836         if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
837         if (fXyq[2][i] > fResponse->MaxAdc()-1) over[cath] = 0;
838       }
839       AliInfo(Form(" Total charge: %f %f",sum[0],sum[1]));
840       if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
841         AliInfo(" Release ");
842         // Big difference
843         cath = sum[0]>sum[1] ? 0 : 1;
844         Int_t imax = 0;
845         Double_t cmax=-1;
846         Double_t *dist = new Double_t[npad];
847         for (Int_t i=0; i<npad; i++) {
848           if (fPadIJ[0][i] != cath) continue;
849           if (fXyq[2][i] < cmax) continue;
850           cmax = fXyq[2][i];
851           imax = i;
852         }
853         // Arrange pads according to their distance to the max, 
854         // normalized to the pad size
855         for (Int_t i=0; i<npad; i++) {
856           dist[i] = 0;
857           if (fPadIJ[0][i] != cath) continue;
858           if (i == imax) continue; 
859           if (fXyq[2][i] < 0) continue;
860           dist[i] = (fXyq[0][i]-fXyq[0][imax])*(fXyq[0][i]-fXyq[0][imax])/
861                      fXyq[3][imax]/fXyq[3][imax]/4;
862           dist[i] += (fXyq[1][i]-fXyq[1][imax])*(fXyq[1][i]-fXyq[1][imax])/
863                       fXyq[4][imax]/fXyq[4][imax]/4;
864           dist[i] = TMath::Sqrt (dist[i]);
865         }
866         TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
867         Int_t indx;
868         Double_t xmax = -1;
869         for (Int_t i=0; i<npad; i++) {
870           indx = flags[i];
871           if (fPadIJ[0][indx] != cath) continue;
872           if (fXyq[2][indx] < 0) continue;
873           if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax)<1.e-3) {
874             // Release pads
875             if (TMath::Abs(dist[indx]-xmax)<1.e-3) 
876                 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
877             else cmax = fXyq[2][indx];
878             xmax = dist[indx];
879             digit = TMath::Nint (fXyq[5][indx]);
880             fUsed[cath][digit] = kFALSE; 
881             fXyq[2][indx] = -2;
882             fnPads[cath]--;
883             // xmax = dist[i]; // Bug?
884           }
885           else break;
886         } 
887         delete [] dist; dist = 0;
888       } // TMath::Abs(sum[0]-sum[1])...
889     } // if (fnPads[0] && fnPads[1])
890     delete [] flags; flags = 0;
891   } // if (i1 != i2) 
892
893   if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
894
895   // Move released pads to the right
896   Int_t beg = 0, end = npad-1, padij;
897   Double_t xyq;
898   while (beg < end) {
899     if (fXyq[2][beg] > 0) { beg++; continue; }
900     for (Int_t j=end; j>beg; j--) {
901       if (fXyq[2][j] < 0) continue;
902       end = j - 1;
903       for (Int_t j1=0; j1<2; j1++) {
904         padij = fPadIJ[j1][beg]; 
905         fPadIJ[j1][beg] = fPadIJ[j1][j];
906         fPadIJ[j1][j] = padij;
907       }
908       for (Int_t j1=0; j1<6; j1++) {
909         xyq = fXyq[j1][beg]; 
910         fXyq[j1][beg] = fXyq[j1][j];
911         fXyq[j1][j] = xyq;
912       }
913       break;
914     } // for (Int_t j=end;
915     beg++;
916   } // while
917   npad = fnPads[0] + fnPads[1];
918   if (npad > 500) { AliInfo(Form(" ***** Too large cluster. Give up. ",npad )); return kFALSE; }
919   // Back up charge value
920   for (Int_t j=0; j<npad; j++) fXyq[5][j] = fXyq[2][j];
921
922   return kTRUE;
923 }
924
925 //_____________________________________________________________________________
926 void AliMUONClusterFinderAZ::BuildPixArray()
927 {
928   // Build pixel array for MLEM method
929   
930   Int_t nPix=0, i1, i2;
931   Float_t xy1[4], xy12[4];
932   AliMUONPixel *pixPtr=0;
933
934   Int_t npad = fnPads[0] + fnPads[1];
935
936   // One cathode is empty
937   i1 = fnPads[0]!=0 ? 0 : 1;
938   i2 = fnPads[1]!=0 ? 1 : 0;
939
940   // Build array of pixels on anode plane
941   if (i1 == i2) { // one-cathode precluster
942     for (Int_t j=0; j<npad; j++) {
943       pixPtr = new AliMUONPixel();
944       for (Int_t i=0; i<2; i++) {
945         pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates
946         pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size
947       }
948       pixPtr->SetCharge(fXyq[2][j]); // charge
949       fPixArray->Add((TObject*)pixPtr);
950       nPix++;
951     }
952   } else { // two-cathode precluster    
953     for (Int_t i=0; i<npad; i++) {
954       if (fPadIJ[0][i] != i1) continue;
955       xy1[0] = fXyq[0][i] - fXyq[3][i];
956       xy1[1] = fXyq[0][i] + fXyq[3][i];
957       xy1[2] = fXyq[1][i] - fXyq[4][i];
958       xy1[3] = fXyq[1][i] + fXyq[4][i];
959       for (Int_t j=0; j<npad; j++) {
960         if (fPadIJ[0][j] != i2) continue;
961         if (!Overlap(xy1, j, xy12, 1)) continue;
962         pixPtr = new AliMUONPixel();
963         for (Int_t k=0; k<2; k++) {
964           pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates
965           pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size
966         }
967         pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
968         fPixArray->Add((TObject*)pixPtr);
969         nPix++;
970       } // for (Int_t j=0;
971     } // for (Int_t i=0;
972   } // else
973
974   Float_t wxmin=999, wymin=999;
975   for (Int_t i=0; i<npad; i++) {
976     if (fPadIJ[0][i] == i1) wymin = TMath::Min (wymin,fXyq[4][i]);
977     if (fPadIJ[0][i] == i2) wxmin = TMath::Min (wxmin,fXyq[3][i]);
978   }
979   AliInfo(Form("%f %f ",wxmin,wymin));
980
981   // Check if small pixel X-size
982   AjustPixel(wxmin, 0);
983   // Check if small pixel Y-size
984   AjustPixel(wymin, 1);
985   // Check if large pixel size
986   AjustPixel(wxmin, wymin);
987
988   // Remove discarded pixels
989   for (Int_t i=0; i<nPix; i++) {
990     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
991     //pixPtr->Print();
992     if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
993   }
994   fPixArray->Compress();
995   nPix = fPixArray->GetEntriesFast();
996
997   if (nPix > npad) {
998      AliInfo(Form("nPix %d ",nPix));
999     // Too many pixels - sort and remove pixels with the lowest signal
1000     fPixArray->Sort();
1001     for (Int_t i=npad; i<nPix; i++) {
1002       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1003       //pixPtr->Print();
1004       fPixArray->RemoveAt(i);
1005       delete pixPtr;
1006     }
1007     nPix = npad;
1008   } // if (nPix > npad)
1009
1010   // Set pixel charges to the same value (for MLEM)
1011   for (Int_t i=0; i<nPix; i++) {
1012     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1013     //pixPtr->SetCharge(10);
1014     AliInfo(Form("%d %f %f %f %f",i+1,pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Size(0),pixPtr->Size(1)));
1015   }
1016 }
1017
1018 //_____________________________________________________________________________
1019 void AliMUONClusterFinderAZ::AjustPixel(Float_t width, Int_t ixy)
1020 {
1021   // Check if some pixels have small size (ajust if necessary)
1022
1023   AliMUONPixel *pixPtr, *pixPtr1 = 0;
1024   Int_t ixy1 = TMath::Even(ixy);
1025   Int_t nPix = fPixArray->GetEntriesFast();
1026
1027   for (Int_t i=0; i<nPix; i++) {
1028     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1029     if (pixPtr->Charge() < 1) continue; // discarded pixel
1030     if (pixPtr->Size(ixy)-width < -1.e-4) {
1031       // try to merge 
1032       AliInfo(Form(" Small X or Y: %d %f %f %f %f",ixy,pixPtr->Size(ixy),width,pixPtr->Coord(0),pixPtr->Coord(1)));
1033       for (Int_t j=i+1; j<nPix; j++) {
1034         pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j);
1035         if (pixPtr1->Charge() < 1) continue; // discarded pixel
1036         if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size 
1037         if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns
1038         if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) {
1039           // merge
1040           pixPtr->SetSize(ixy, width);
1041           pixPtr->SetCoord(ixy, (pixPtr->Coord(ixy)+pixPtr1->Coord(ixy))/2);
1042           pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
1043           pixPtr1->SetCharge(0);
1044           pixPtr1 = 0;
1045           break;
1046         }
1047       } // for (Int_t j=i+1;
1048       //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ???
1049       //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) {
1050       if (pixPtr1 || i == nPix-1) {
1051         // edge pixel - just increase its size
1052         AliInfo(" Edge ..."); 
1053         for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1054           // ???if (fPadIJ[0][j] != i1) continue;
1055           if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue;
1056           if (pixPtr->Coord(ixy) < fXyq[ixy][j]) 
1057             pixPtr->Shift(ixy, -pixPtr->Size(ixy));
1058           else pixPtr->Shift(ixy, pixPtr->Size(ixy));
1059           pixPtr->SetSize(ixy, width);
1060           break;
1061         }
1062       }
1063     } // if (pixPtr->Size(ixy)-width < -1.e-4)
1064   } // for (Int_t i=0; i<nPix;
1065   return;
1066 }
1067   
1068 //_____________________________________________________________________________
1069 void AliMUONClusterFinderAZ::AjustPixel(Float_t wxmin, Float_t wymin)
1070 {
1071   // Check if some pixels have large size (ajust if necessary)
1072
1073   Int_t nx, ny;
1074   Int_t nPix = fPixArray->GetEntriesFast();
1075   AliMUONPixel *pixPtr, *pixPtr1, pix;
1076
1077   // Check if large pixel size
1078   for (Int_t i=0; i<nPix; i++) {
1079     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1080     if (pixPtr->Charge() < 1) continue; // discarded pixel
1081     if (pixPtr->Size(0)-wxmin > 1.e-4 || pixPtr->Size(1)-wymin > 1.e-4) {
1082       AliInfo(Form(" Different  %f %f %f %f",pixPtr->Size(0),wxmin,pixPtr->Size(1),wymin));
1083       pix = *pixPtr;
1084       nx = TMath::Nint (pix.Size(0)/wxmin);
1085       ny = TMath::Nint (pix.Size(1)/wymin);
1086       pix.Shift(0, -pix.Size(0)-wxmin);
1087       pix.Shift(1, -pix.Size(1)-wymin);
1088       pix.SetSize(0, wxmin);
1089       pix.SetSize(1, wymin);
1090       for (Int_t ii=0; ii<nx; ii++) {
1091         pix.Shift(0, wxmin*2);
1092         for (Int_t jj=0; jj<ny; jj++) {
1093           pix.Shift(1, wymin*2);
1094           pixPtr1 = new AliMUONPixel(pix);
1095           fPixArray->Add((TObject*)pixPtr1);
1096         }
1097       }
1098       pixPtr->SetCharge(0);
1099     }
1100   } // for (Int_t i=0; i<nPix;
1101   return;
1102 }
1103
1104 //_____________________________________________________________________________
1105 Bool_t AliMUONClusterFinderAZ::MainLoop()
1106 {
1107   // Repeat MLEM algorithm until pixel size becomes sufficiently small
1108   
1109   TH2D *mlem;
1110
1111   Int_t ix, iy;
1112   //Int_t nn, xList[10], yList[10];
1113   Int_t nPix = fPixArray->GetEntriesFast();
1114   Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0;
1115   AliMUONPixel *pixPtr = 0;
1116   Double_t *coef = 0, *probi = 0; 
1117   for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++;
1118
1119   while (1) {
1120
1121     mlem = (TH2D*) gROOT->FindObject("mlem");
1122     if (mlem) mlem->Delete();
1123     // Calculate coefficients
1124     AliInfo(Form(" nPix, npadTot, npadOK %d %d %d ", nPix , npadTot , npadOK ));
1125
1126     // Calculate coefficients and pixel visibilities
1127     coef = new Double_t [npadTot*nPix];
1128     probi = new Double_t [nPix];
1129     Int_t indx = 0, cath;
1130     for (Int_t ipix=0; ipix<nPix; ipix++) {
1131       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1132       probi[ipix] = 0;
1133       for (Int_t j=0; j<npadTot; j++) {
1134         if (fPadIJ[1][j] < 0) { coef[j*nPix+ipix] = 0; continue; }
1135         cath = fPadIJ[0][j];
1136         fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
1137         fSegmentation[cath]->SetPad(ix,iy);
1138         /*
1139           fSegmentation[cath]->Neighbours(ix,iy,&nn,xList,yList); 
1140           if (nn != 4) {
1141           cout << nn << ": ";
1142           for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
1143           cout << endl;
1144           }
1145         */
1146         Double_t sum = 0;
1147         fSegmentation[cath]->SetHit(pixPtr->Coord(0),pixPtr->Coord(1),fZpad);
1148         sum += fResponse->IntXY(fSegmentation[cath]);
1149         indx = j*nPix + ipix;
1150         coef[indx] = sum; 
1151         probi[ipix] += coef[indx];
1152         //cout << j << " " << ipix << " " << coef[indx] << endl;
1153       } // for (Int_t j=0;
1154       //cout << " prob: " << probi[ipix] << endl;
1155       if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel
1156     } // for (Int_t ipix=0;
1157
1158     // MLEM algorithm
1159     Mlem(coef, probi);
1160
1161     Double_t xylim[4] = {999, 999, 999, 999};
1162     for (Int_t ipix=0; ipix<nPix; ipix++) {
1163       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1164       for (Int_t i=0; i<4; i++) 
1165         xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1166       //cout << ipix+1; pixPtr->Print();
1167     }
1168     for (Int_t i=0; i<4; i++) {
1169       xylim[i] -= pixPtr->Size(i/2); 
1170           AliInfo(Form("%f ",(i%2 ? -1 : 1)*xylim[i])); 
1171         }
1172
1173     // Ajust histogram to approximately the same limits as for the pads
1174     // (for good presentation)
1175     //*
1176     Float_t xypads[4];
1177     if (fHist[0]) {
1178       xypads[0] = fHist[0]->GetXaxis()->GetXmin();
1179       xypads[1] = -fHist[0]->GetXaxis()->GetXmax();
1180       xypads[2] = fHist[0]->GetYaxis()->GetXmin();
1181       xypads[3] = -fHist[0]->GetYaxis()->GetXmax();
1182       for (Int_t i=0; i<4; i++) {
1183         while(1) {
1184           if (xylim[i] < xypads[i]) break;
1185           xylim[i] -= 2*pixPtr->Size(i/2);
1186         }
1187       }
1188     } // if (fHist[0])
1189     //*/
1190
1191     Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1192     Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1193     mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1194     for (Int_t ipix=0; ipix<nPix; ipix++) {
1195       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1196       mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
1197     }
1198     //gPad->GetCanvas()->cd(3);
1199     if (fDraw) {
1200       ((TCanvas*)gROOT->FindObject("c2"))->cd();
1201       gPad->SetTheta(55);
1202       gPad->SetPhi(30);
1203       mlem->Draw("lego1Fb");
1204       gPad->Update();
1205       gets((char*)&ix);
1206     }
1207
1208     // Check if the total charge of pixels is too low
1209     Double_t qTot = 0;
1210     for (Int_t i=0; i<nPix; i++) {
1211       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1212       qTot += pixPtr->Charge();
1213     }
1214     if (qTot < 1.e-4 || npadOK < 3 && qTot < 50) {
1215       delete [] coef; delete [] probi; coef = 0; probi = 0;
1216       fPixArray->Delete(); 
1217       return kFALSE; 
1218     }
1219
1220     // Plot data - expectation
1221     /*
1222     Double_t x, y, cont;
1223     for (Int_t j=0; j<npadTot; j++) {
1224       Double_t sum1 = 0;
1225       for (Int_t i=0; i<nPix; i++) {
1226         // Caculate expectation
1227         pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1228         sum1 += pixPtr->Charge()*coef[j*nPix+i];
1229       }
1230       sum1 = TMath::Min (sum1,(Double_t)fResponse->MaxAdc());
1231       x = fXyq[0][j];
1232       y = fXyq[1][j];
1233       cath = fPadIJ[0][j];
1234       Int_t ihist = cath*2;
1235       ix = fHist[ihist]->GetXaxis()->FindBin(x);
1236       iy = fHist[ihist]->GetYaxis()->FindBin(y);
1237       cont = fHist[ihist]->GetCellContent(ix,iy);
1238       if (cont == 0 && fHist[ihist+1]) {
1239         ihist += 1;
1240         ix = fHist[ihist]->GetXaxis()->FindBin(x);
1241         iy = fHist[ihist]->GetYaxis()->FindBin(y);
1242       }
1243       fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
1244     }
1245     ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
1246     //gPad->SetTheta(55);
1247     //gPad->SetPhi(30);
1248     //mlem->Draw("lego1");
1249     gPad->Modified();
1250     ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
1251     gPad->Modified();
1252     */
1253
1254     // Calculate position of the center-of-gravity around the maximum pixel
1255     Double_t xyCOG[2];
1256     FindCOG(mlem, xyCOG);
1257
1258     if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break;
1259     //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) {
1260     // Sort pixels according to the charge
1261     fPixArray->Sort();
1262     /*
1263     for (Int_t i=0; i<nPix; i++) {
1264       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1265       cout << i+1; pixPtr->Print();
1266     }
1267     */
1268     Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
1269     pixMin = TMath::Min (pixMin,50.);
1270
1271     // Decrease pixel size and shift pixels to make them centered at 
1272     // the maximum one
1273     indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1274     Double_t width = 0, shift[2]={0};
1275     ix = 1;
1276     for (Int_t i=0; i<4; i++) xylim[i] = 999;
1277     Int_t nPix1 = nPix; nPix = 0;
1278     for (Int_t ipix=0; ipix<nPix1; ipix++) {
1279       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1280       if (nPix >= npadOK) { // too many pixels already
1281         fPixArray->RemoveAt(ipix); 
1282         delete pixPtr; 
1283         continue;
1284       }
1285       if (pixPtr->Charge() < pixMin) { // low charge
1286         fPixArray->RemoveAt(ipix); 
1287         delete pixPtr; 
1288         continue;
1289       }
1290       for (Int_t i=0; i<2; i++) {
1291         if (!i) {
1292           pixPtr->SetCharge(10);
1293           pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1294           width = -pixPtr->Size(indx);
1295           pixPtr->Shift(indx, width);
1296           // Shift pixel position
1297           if (ix) {
1298             ix = 0;
1299             for (Int_t j=0; j<2; j++) {
1300               shift[j] = pixPtr->Coord(j) - xyCOG[j];
1301               shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1302             }
1303             //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1304           } // if (ix)
1305           pixPtr->Shift(0, -shift[0]);
1306           pixPtr->Shift(1, -shift[1]);
1307         } else {
1308           pixPtr = new AliMUONPixel(*pixPtr);
1309           pixPtr->Shift(indx, -2*width);
1310           fPixArray->Add((TObject*)pixPtr);
1311         } // else
1312         //pixPtr->Print();
1313         for (Int_t i=0; i<4; i++) 
1314           xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1315       } // for (Int_t i=0; i<2;
1316       nPix += 2;
1317     } // for (Int_t ipix=0;
1318
1319     fPixArray->Compress();
1320     nPix = fPixArray->GetEntriesFast();
1321
1322     // Remove excessive pixels
1323     if (nPix > npadOK) {
1324       for (Int_t ipix=npadOK; ipix<nPix; ipix++) { 
1325         pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1326         fPixArray->RemoveAt(ipix); 
1327         delete pixPtr;
1328       }
1329     } else {
1330       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1331       // add pixels if the maximum is at the limit of pixel area
1332       // start from Y-direction
1333       Int_t j = 0;
1334       for (Int_t i=3; i>-1; i--) {
1335         if (nPix < npadOK && 
1336             TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) {
1337           pixPtr = new AliMUONPixel(*pixPtr);
1338           pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1339           j = TMath::Even (i/2);
1340           pixPtr->SetCoord(j, xyCOG[j]);
1341           fPixArray->Add((TObject*)pixPtr);
1342           nPix++;
1343         }
1344       }
1345     } // else    
1346
1347     fPixArray->Compress();
1348     nPix = fPixArray->GetEntriesFast();
1349     delete [] coef; delete [] probi; coef = 0; probi = 0;
1350   } // while (1)
1351
1352   // remove pixels with low signal or low visibility
1353   // Cuts are empirical !!!
1354   Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1355   thresh = TMath::Min (thresh,50.);
1356   Double_t cmax = -1, charge = 0;
1357   for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]); 
1358   // Mark pixels which should be removed
1359   for (Int_t i=0; i<nPix; i++) {
1360     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1361     charge = pixPtr->Charge();
1362     if (charge < thresh) pixPtr->SetCharge(-charge);
1363     else if (cmax > 1.91) {
1364       if (probi[i] < 1.9) pixPtr->SetCharge(-charge);
1365     }
1366     else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
1367   }
1368   // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1369   Int_t near = 0;
1370   for (Int_t i=0; i<nPix; i++) {
1371     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1372     charge = pixPtr->Charge();
1373     if (charge > 0) continue;
1374     near = FindNearest(pixPtr);
1375     pixPtr->SetCharge(0);
1376     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near);
1377     pixPtr->SetCharge(pixPtr->Charge() - charge);
1378   }
1379   // Update histogram
1380   for (Int_t i=0; i<nPix; i++) {
1381     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1382     ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1383     iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1384     mlem->SetBinContent(ix, iy, pixPtr->Charge());
1385   }
1386   if (fDraw) {
1387     ((TCanvas*)gROOT->FindObject("c2"))->cd();
1388     gPad->SetTheta(55);
1389     gPad->SetPhi(30);
1390     mlem->Draw("lego1Fb");
1391     gPad->Update();
1392   }
1393
1394   fxyMu[0][6] = fxyMu[1][6] = 9999;
1395   // Try to split into clusters
1396   Bool_t ok = kTRUE;
1397   if (mlem->GetSum() < 1) ok = kFALSE;
1398   else Split(mlem, coef);
1399   delete [] coef; delete [] probi; coef = 0; probi = 0;
1400   fPixArray->Delete(); 
1401   return ok;
1402 }
1403
1404 //_____________________________________________________________________________
1405 void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi)
1406 {
1407   // Use MLEM to find pixel charges
1408   
1409   Int_t nPix = fPixArray->GetEntriesFast();
1410   Int_t npad = fnPads[0] + fnPads[1];
1411   Double_t *probi1 = new Double_t [nPix];
1412   Int_t indx, indx1;
1413   AliMUONPixel *pixPtr;
1414
1415   for (Int_t iter=0; iter<15; iter++) {
1416     // Do iterations
1417     for (Int_t ipix=0; ipix<nPix; ipix++) {
1418       // Correct each pixel
1419       if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1420       Double_t sum = 0;
1421       probi1[ipix] = probi[ipix];
1422       for (Int_t j=0; j<npad; j++) {
1423         if (fPadIJ[1][j] < 0) continue; 
1424         Double_t sum1 = 0;
1425         indx1 = j*nPix;
1426         indx = indx1 + ipix;
1427         for (Int_t i=0; i<nPix; i++) {
1428           // Caculate expectation
1429           pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1430           sum1 += pixPtr->Charge()*coef[indx1+i];
1431         } // for (Int_t i=0;
1432         if (fXyq[2][j] > fResponse->MaxAdc()-1 && sum1 > fResponse->MaxAdc()) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows
1433         //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl;
1434         if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1;
1435       } // for (Int_t j=0;
1436       pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1437       if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1438     } // for (Int_t ipix=0;
1439   } // for (Int_t iter=0;
1440   delete [] probi1;
1441   return;
1442 }
1443
1444 //_____________________________________________________________________________
1445 void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1446 {
1447   // Calculate position of the center-of-gravity around the maximum pixel
1448
1449   Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1450   Int_t i1 = -9, j1 = -9;
1451   mlem->GetMaximumBin(ixmax,iymax,ix);
1452   Int_t nx = mlem->GetNbinsX();
1453   Int_t ny = mlem->GetNbinsY();
1454   Double_t thresh = mlem->GetMaximum()/10;
1455   Double_t x, y, cont, xq=0, yq=0, qq=0;
1456
1457   for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1458     y = mlem->GetYaxis()->GetBinCenter(i);
1459     for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1460       cont = mlem->GetCellContent(j,i);
1461       if (cont < thresh) continue;
1462       if (i != i1) {i1 = i; nsumy++;}
1463       if (j != j1) {j1 = j; nsumx++;}
1464       x = mlem->GetXaxis()->GetBinCenter(j);
1465       xq += x*cont;
1466       yq += y*cont;
1467       qq += cont;
1468       nsum++;
1469     }
1470   }
1471
1472   Double_t cmax = 0;
1473   Int_t i2 = 0, j2 = 0;
1474   x = y = 0;
1475   if (nsumy == 1) {
1476     // one bin in Y - add one more (with the largest signal)
1477     for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1478       if (i == iymax) continue;
1479       for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1480         cont = mlem->GetCellContent(j,i);
1481         if (cont > cmax) {
1482           cmax = cont;
1483           x = mlem->GetXaxis()->GetBinCenter(j);
1484           y = mlem->GetYaxis()->GetBinCenter(i);
1485           i2 = i;
1486           j2 = j;
1487         }
1488       }
1489     }
1490     xq += x*cmax;
1491     yq += y*cmax;
1492     qq += cmax;
1493     if (i2 != i1) nsumy++;
1494     if (j2 != j1) nsumx++;
1495     nsum++;
1496   } // if (nsumy == 1)
1497
1498   if (nsumx == 1) {
1499     // one bin in X - add one more (with the largest signal)
1500     cmax = x = y = 0;
1501     for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1502       if (j == ixmax) continue;
1503       for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1504         cont = mlem->GetCellContent(j,i);
1505         if (cont > cmax) {
1506           cmax = cont;
1507           x = mlem->GetXaxis()->GetBinCenter(j);
1508           y = mlem->GetYaxis()->GetBinCenter(i);
1509           i2 = i;
1510           j2 = j;
1511         }
1512       }
1513     }
1514     xq += x*cmax;
1515     yq += y*cmax;
1516     qq += cmax;
1517     if (i2 != i1) nsumy++;
1518     if (j2 != j1) nsumx++;
1519     nsum++;
1520   } // if (nsumx == 1)
1521
1522   xyc[0] = xq/qq; xyc[1] = yq/qq;
1523   AliInfo(Form("%f %f %f %d %d %d",xyc[0],xyc[1],qq,nsum,nsumx,nsumy));
1524   return;
1525 }
1526
1527 //_____________________________________________________________________________
1528 Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1529 {
1530   // Find the pixel nearest to the given one
1531   // (algorithm may be not very efficient)
1532
1533   Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1534   Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1535   Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1536   AliMUONPixel *pixPtr;
1537
1538   for (Int_t i=0; i<nPix; i++) {
1539     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1540     if (pixPtr->Charge() < 0.5) continue;
1541     dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1542     dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1543     r = dx *dx + dy * dy;
1544     if (r < rmin) { rmin = r; imin = i; }
1545   }
1546   return imin;
1547 }
1548
1549 //_____________________________________________________________________________
1550 void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
1551 {
1552   // The main steering function to work with clusters of pixels in anode
1553   // plane (find clusters, decouple them from each other, merge them (if
1554   // necessary), pick up coupled pads, call the fitting function)
1555   
1556   Int_t nx = mlem->GetNbinsX();
1557   Int_t ny = mlem->GetNbinsY();
1558   Int_t nPix = fPixArray->GetEntriesFast();
1559
1560   Bool_t *used = new Bool_t[ny*nx];
1561   Double_t cont;
1562   Int_t nclust = 0, indx, indx1;
1563
1564   for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE; 
1565
1566   TObjArray *clusters[200]={0};
1567   TObjArray *pix;
1568
1569   // Find clusters of histogram bins (easier to work in 2-D space)
1570   for (Int_t i=1; i<=ny; i++) {
1571     for (Int_t j=1; j<=nx; j++) {
1572       indx = (i-1)*nx + j - 1;
1573       if (used[indx]) continue;
1574       cont = mlem->GetCellContent(j,i);
1575       if (cont < 0.5) continue;
1576       pix = new TObjArray(20);
1577       used[indx] = 1;
1578       pix->Add(BinToPix(mlem,j,i));
1579       AddBin(mlem, i, j, 0, used, pix); // recursive call
1580       clusters[nclust++] = pix;
1581       if (nclust > 200) { AliInfo(" Too many clusters "); ::exit(0); }
1582     } // for (Int_t j=1; j<=nx; j++) {
1583   } // for (Int_t i=1; i<=ny;
1584   AliInfo(Form("%d ",nclust));
1585   delete [] used; used = 0;
1586   
1587   // Compute couplings between clusters and clusters to pads
1588   Int_t npad = fnPads[0] + fnPads[1];
1589
1590   // Exclude pads with overflows
1591   for (Int_t j=0; j<npad; j++) {
1592     if (fXyq[2][j] > fResponse->MaxAdc()-1) fPadIJ[1][j] = -9;
1593     else fPadIJ[1][j] = 0;
1594   }
1595
1596   // Compute couplings of clusters to pads
1597   TMatrixD *aijclupad = new TMatrixD(nclust,npad);
1598   *aijclupad = 0;
1599   Int_t npxclu;
1600   for (Int_t iclust=0; iclust<nclust; iclust++) {
1601     pix = clusters[iclust];
1602     npxclu = pix->GetEntriesFast();
1603     for (Int_t i=0; i<npxclu; i++) {
1604       indx = fPixArray->IndexOf(pix->UncheckedAt(i));
1605       for (Int_t j=0; j<npad; j++) {
1606         // Exclude overflows
1607         if (fPadIJ[1][j] < 0) continue;
1608         if (coef[j*nPix+indx] < fgkCouplMin) continue;
1609         (*aijclupad)(iclust,j) += coef[j*nPix+indx];
1610       }
1611     }
1612   }
1613   // Compute couplings between clusters
1614   TMatrixD *aijcluclu = new TMatrixD(nclust,nclust);
1615   *aijcluclu = 0;
1616   for (Int_t iclust=0; iclust<nclust; iclust++) {
1617     for (Int_t j=0; j<npad; j++) {
1618       // Exclude overflows
1619       if (fPadIJ[1][j] < 0) continue;
1620       if ((*aijclupad)(iclust,j) < fgkCouplMin) continue;
1621       for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1622         if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue;
1623         (*aijcluclu)(iclust,iclust1) += 
1624           TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j));
1625       }
1626     }
1627   }
1628   for (Int_t iclust=0; iclust<nclust; iclust++) {
1629     for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1630       (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1);
1631     }
1632   }
1633
1634   if (nclust > 1) aijcluclu->Print();
1635
1636   // Find groups of coupled clusters
1637   used = new Bool_t[nclust];
1638   for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
1639   Int_t *clustNumb = new Int_t[nclust];
1640   Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
1641   Double_t parOk[8];
1642
1643   for (Int_t igroup=0; igroup<nclust; igroup++) {
1644     if (used[igroup]) continue;
1645     used[igroup] = kTRUE;
1646     clustNumb[0] = igroup;
1647     nCoupled = 1;
1648     // Find group of coupled clusters
1649     AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
1650     AliInfo(Form(" nCoupled: %d",nCoupled));
1651     for (Int_t i=0; i<nCoupled; i++) AliInfo(Form(" %d ",clustNumb[i])); 
1652
1653     while (nCoupled > 0) {
1654
1655       if (nCoupled < 4) {
1656         nForFit = nCoupled;
1657         for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
1658       } else {
1659         // Too many coupled clusters to fit - try to decouple them
1660         // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with 
1661         // all the others in the group 
1662         for (Int_t j=0; j<3; j++) minGroup[j] = -1;
1663         Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
1664
1665         // Flag clusters for fit
1666         nForFit = 0;
1667         while (minGroup[nForFit] >= 0 && nForFit < 3) {
1668           AliInfo(Form("%d ",clustNumb[minGroup[nForFit]]));
1669           clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1670           clustNumb[minGroup[nForFit]] -= 999;
1671           nForFit++;
1672         }
1673         AliInfo(Form("%d %f ",nForFit,coupl));
1674       } // else
1675
1676       // Select pads for fit. 
1677       if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) {
1678         // Deselect pads
1679         for (Int_t j=0; j<npad; j++) if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0;
1680         // Merge the failed cluster candidates (with too few pads to fit) with 
1681         // the one with the strongest coupling
1682         Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
1683       } else {
1684         // Do the fit
1685         nfit = Fit(nForFit, clustFit, clusters, parOk);
1686       }
1687
1688       // Subtract the fitted charges from pads with strong coupling and/or
1689       // return pads for further use
1690       UpdatePads(nfit, parOk);
1691
1692       // Mark used pads
1693       for (Int_t j=0; j<npad; j++) {if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;}
1694
1695       // Sort the clusters (move to the right the used ones)
1696       Int_t beg = 0, end = nCoupled - 1;
1697       while (beg < end) {
1698         if (clustNumb[beg] >= 0) { beg++; continue; }
1699         for (Int_t j=end; j>beg; j--) {
1700           if (clustNumb[j] < 0) continue;
1701           end = j - 1;
1702           indx = clustNumb[beg];
1703           clustNumb[beg] = clustNumb[j];
1704           clustNumb[j] = indx;
1705           break;
1706         }
1707         beg++;
1708       }
1709
1710       nCoupled -= nForFit;
1711       if (nCoupled > 3) {
1712         // Remove couplings of used clusters
1713         for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) {
1714           indx = clustNumb[iclust] + 999;
1715           for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1716             indx1 = clustNumb[iclust1];
1717             (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0;
1718           }
1719         }
1720
1721         // Update the remaining clusters couplings (exclude couplings from 
1722         // the used pads)
1723         for (Int_t j=0; j<npad; j++) {
1724           if (fPadIJ[1][j] != -1) continue;
1725           for (Int_t iclust=0; iclust<nCoupled; iclust++) {
1726             indx = clustNumb[iclust];
1727             if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1728             for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1729               indx1 = clustNumb[iclust1];
1730               if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1731               // Check this
1732               (*aijcluclu)(indx,indx1) -= 
1733                 TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j));
1734               (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1);
1735             }
1736           }
1737           fPadIJ[1][j] = -9;
1738         } // for (Int_t j=0; j<npad;
1739       } // if (nCoupled > 3)
1740     } // while (nCoupled > 0)
1741   } // for (Int_t igroup=0; igroup<nclust;
1742
1743   //delete aij_clu; aij_clu = 0; delete aijclupad; aijclupad = 0;
1744   aijcluclu->Delete(); aijclupad->Delete();
1745   for (Int_t iclust=0; iclust<nclust; iclust++) {
1746     pix = clusters[iclust]; 
1747     pix->Clear();
1748     delete pix; pix = 0;
1749   }
1750   delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1751 }
1752
1753 //_____________________________________________________________________________
1754 void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1755 {
1756   // Add a bin to the cluster
1757
1758   Int_t nx = mlem->GetNbinsX();
1759   Int_t ny = mlem->GetNbinsY();
1760   Double_t cont1, cont = mlem->GetCellContent(jc,ic);
1761   AliMUONPixel *pixPtr = 0;
1762
1763   for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
1764     for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
1765       if (i != ic && j != jc) continue;
1766       if (used[(i-1)*nx+j-1]) continue;
1767       cont1 = mlem->GetCellContent(j,i);
1768       if (mode && cont1 > cont) continue;
1769       used[(i-1)*nx+j-1] = kTRUE;
1770       if (cont1 < 0.5) continue;
1771       if (pix) pix->Add(BinToPix(mlem,j,i)); 
1772       else {
1773         pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j), 
1774                                    mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1775         fPixArray->Add((TObject*)pixPtr);
1776       }
1777       AddBin(mlem, i, j, mode, used, pix); // recursive call
1778     }
1779   }
1780 }
1781
1782 //_____________________________________________________________________________
1783 TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1784 {
1785   // Translate histogram bin to pixel 
1786   
1787   Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1788   Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1789   
1790   Int_t nPix = fPixArray->GetEntriesFast();
1791   AliMUONPixel *pixPtr;
1792
1793   // Compare pixel and bin positions
1794   for (Int_t i=0; i<nPix; i++) {
1795     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1796     if (pixPtr->Charge() < 0.5) continue;
1797     if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr;
1798   }
1799   AliWarning(" Something wrong ??? ");
1800   return NULL;
1801 }
1802
1803 //_____________________________________________________________________________
1804 void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
1805 {
1806   // Add a cluster to the group of coupled clusters
1807
1808   for (Int_t i=0; i<nclust; i++) {
1809     if (used[i]) continue;
1810     if ((*aijcluclu)(i,ic) < fgkCouplMin) continue;
1811     used[i] = kTRUE;
1812     clustNumb[nCoupled++] = i;
1813     AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
1814   }
1815 }
1816
1817 //_____________________________________________________________________________
1818 Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup)
1819 {
1820   // Find group of clusters with minimum coupling to all the others
1821
1822   Int_t i123max = TMath::Min(3,nCoupled/2); 
1823   Int_t indx, indx1, indx2, indx3, nTot = 0;
1824   Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1825
1826   for (Int_t i123=1; i123<=i123max; i123++) {
1827
1828     if (i123 == 1) {
1829       coupl1 = new Double_t [nCoupled];
1830       for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1831     }
1832     else if (i123 == 2) {
1833       nTot = nCoupled*nCoupled;
1834       coupl2 = new Double_t [nTot];
1835       for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1836     } else {
1837       nTot = nTot*nCoupled;
1838       coupl3 = new Double_t [nTot];
1839       for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1840     } // else
1841
1842     for (Int_t i=0; i<nCoupled; i++) {
1843       indx1 = clustNumb[i];
1844       for (Int_t j=i+1; j<nCoupled; j++) {
1845         indx2 = clustNumb[j];
1846         if (i123 == 1) {
1847           coupl1[i] += (*aijcluclu)(indx1,indx2);
1848           coupl1[j] += (*aijcluclu)(indx1,indx2);
1849         } 
1850         else if (i123 == 2) {
1851           indx = i*nCoupled + j;
1852           coupl2[indx] = coupl1[i] + coupl1[j];
1853           coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2));
1854         } else {
1855           for (Int_t k=j+1; k<nCoupled; k++) {
1856             indx3 = clustNumb[k];
1857             indx = i*nCoupled*nCoupled + j*nCoupled + k;
1858             coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1859             coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3));
1860           }
1861         } // else
1862       } // for (Int_t j=i+1;
1863     } // for (Int_t i=0;
1864   } // for (Int_t i123=1;
1865
1866   // Find minimum coupling
1867   Double_t couplMin = 9999;
1868   Int_t locMin = 0;
1869
1870   for (Int_t i123=1; i123<=i123max; i123++) {
1871     if (i123 == 1) {
1872       locMin = TMath::LocMin(nCoupled, coupl1);
1873       couplMin = coupl1[locMin];
1874       minGroup[0] = locMin;
1875       delete [] coupl1; coupl1 = 0;
1876     } 
1877     else if (i123 == 2) {
1878       locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1879       if (coupl2[locMin] < couplMin) {
1880         couplMin = coupl2[locMin];
1881         minGroup[0] = locMin/nCoupled;
1882         minGroup[1] = locMin%nCoupled;
1883       }
1884       delete [] coupl2; coupl2 = 0;
1885     } else {
1886       locMin = TMath::LocMin(nTot, coupl3);
1887       if (coupl3[locMin] < couplMin) {
1888         couplMin = coupl3[locMin];
1889         minGroup[0] = locMin/nCoupled/nCoupled;
1890         minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1891         minGroup[2] = locMin%nCoupled;
1892       }
1893       delete [] coupl3; coupl3 = 0;
1894     } // else
1895   } // for (Int_t i123=1;
1896   return couplMin;
1897 }
1898
1899 //_____________________________________________________________________________
1900 Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad)
1901 {
1902   // Select pads for fit. If too many coupled clusters, find pads giving 
1903   // the strongest coupling with the rest of clusters and exclude them from the fit.
1904
1905   Int_t npad = fnPads[0] + fnPads[1];
1906   Double_t *padpix = 0;
1907
1908   if (nCoupled > 3) {
1909     padpix = new Double_t[npad];
1910     for (Int_t i=0; i<npad; i++) padpix[i] = 0; 
1911   }
1912
1913   Int_t nOK = 0, indx, indx1;
1914   for (Int_t iclust=0; iclust<nForFit; iclust++) {
1915     indx = clustFit[iclust];
1916     for (Int_t j=0; j<npad; j++) {
1917       if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1918       if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1919       fPadIJ[1][j] = 1; // pad to be used in fit
1920       nOK++;
1921       if (nCoupled > 3) {
1922         // Check other clusters
1923         for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1924           indx1 = clustNumb[iclust1];
1925           if (indx1 < 0) continue;
1926           if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1927           padpix[j] += (*aijclupad)(indx1,j);
1928         }
1929       } // if (nCoupled > 3)
1930     } // for (Int_t j=0; j<npad;
1931   } // for (Int_t iclust=0; iclust<nForFit
1932   if (nCoupled < 4) return nOK;
1933
1934   Double_t aaa = 0;
1935   for (Int_t j=0; j<npad; j++) {
1936     if (padpix[j] < fgkCouplMin) continue;
1937     AliInfo(Form("%d  %f ",j , padpix[j])); 
1938     AliInfo(Form("%f  %f ",fXyq[0][j],fXyq[1][j])); 
1939     aaa += padpix[j];
1940     fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1941     nOK--;
1942   }
1943   delete [] padpix; padpix = 0;
1944   return nOK;
1945 }
1946   
1947 //_____________________________________________________________________________
1948 void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad)
1949 {
1950   // Merge the group of clusters with the one having the strongest coupling with them
1951
1952   Int_t indx, indx1, npxclu, npxclu1, imax=0;
1953   TObjArray *pix, *pix1;
1954   Double_t couplMax;
1955
1956   for (Int_t icl=0; icl<nForFit; icl++) {
1957     indx = clustFit[icl];
1958     pix = clusters[indx];
1959     npxclu = pix->GetEntriesFast();
1960     couplMax = -1;
1961     for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1962       indx1 = clustNumb[icl1];
1963       if (indx1 < 0) continue;
1964       if ((*aijcluclu)(indx,indx1) > couplMax) {
1965         couplMax = (*aijcluclu)(indx,indx1);
1966         imax = indx1;
1967       }
1968     } // for (Int_t icl1=0;
1969     /*if (couplMax < fgkCouplMin) {
1970       cout << " Oops " << couplMax << endl;
1971       aijcluclu->Print();
1972       cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1973       ::exit(0);
1974       }*/
1975     // Add to it
1976     pix1 = clusters[imax];
1977     npxclu1 = pix1->GetEntriesFast();
1978     // Add pixels 
1979     for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
1980     AliInfo(Form(" New number of pixels: %d %d ",npxclu1 ,pix1->GetEntriesFast() ));
1981     //Add cluster-to-cluster couplings
1982     //aijcluclu->Print();
1983     for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1984       indx1 = clustNumb[icl1];
1985       if (indx1 < 0 || indx1 == imax) continue;
1986       (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1);
1987       (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax);
1988     }
1989     (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0;
1990     //aijcluclu->Print();
1991     //Add cluster-to-pad couplings
1992     for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1993       if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1994       (*aijclupad)(imax,j) += (*aijclupad)(indx,j);
1995       (*aijclupad)(indx,j) = 0;
1996     }
1997   } // for (Int_t icl=0; icl<nForFit;
1998 }
1999
2000 //_____________________________________________________________________________
2001 Int_t AliMUONClusterFinderAZ::Fit(Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
2002 {
2003   // Find selected clusters to selected pad charges
2004   
2005   TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
2006   //Int_t nx = mlem->GetNbinsX();
2007   //Int_t ny = mlem->GetNbinsY();
2008   Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
2009   Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
2010   Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
2011   Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
2012   //Double_t qmin = 0, qmax = 1;
2013   Double_t step[3]={0.01,0.002,0.02};
2014
2015   Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8];
2016   TObjArray *pix;
2017   Int_t npxclu;
2018
2019   // Number of pads to use
2020   Int_t npads = 0;
2021   for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {if (fPadIJ[1][i] == 1) npads++;}
2022   for (Int_t i=0; i<nfit; i++) {AliInfo(Form("%d %d ",i+1 ,clustFit[i]));}
2023   AliInfo(Form("%d ",nfit));
2024   AliInfo(Form(" Number of pads to fit: %d ",npads));
2025   fNpar = 0;
2026   fQtot = 0;
2027   if (npads < 2) return 0; 
2028
2029   // Take cluster maxima as fitting seeds
2030   AliMUONPixel *pixPtr;
2031   Double_t xyseed[3][2], qseed[3];
2032   for (Int_t ifit=1; ifit<=nfit; ifit++) {
2033     cmax = 0;
2034     pix = clusters[clustFit[ifit-1]];
2035     npxclu = pix->GetEntriesFast();
2036     for (Int_t clu=0; clu<npxclu; clu++) {
2037       pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu);
2038       cont = pixPtr->Charge();
2039       fQtot += cont;
2040       if (cont > cmax) { 
2041         cmax = cont; 
2042         xseed = pixPtr->Coord(0);
2043         yseed = pixPtr->Coord(1);
2044       }
2045     }
2046     xyseed[ifit-1][0] = xseed;
2047     xyseed[ifit-1][1] = yseed;
2048     qseed[ifit-1] = cmax;
2049   } // for (Int_t ifit=1;
2050
2051   Int_t nDof, maxSeed[3];
2052   Double_t fmin, chi2o = 9999, chi2n;
2053
2054   // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is 
2055   // lower, try 3-track (if number of pads is sufficient).
2056   
2057   TMath::Sort(nfit, qseed, maxSeed, kTRUE); // in decreasing order
2058   nfit = TMath::Min (nfit, (npads + 1) / 3);
2059
2060   Double_t *gin = 0, func0, func1, param[8], param0[2][8], deriv[2][8], step0[8];
2061   Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
2062   Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
2063   Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
2064
2065   for (Int_t iseed=0; iseed<nfit; iseed++) {
2066
2067     for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j];
2068     param[fNpar] = xyseed[maxSeed[iseed]][0];
2069     parmin[fNpar] = xmin; 
2070     parmax[fNpar++] = xmax; 
2071     param[fNpar] = xyseed[maxSeed[iseed]][1];
2072     parmin[fNpar] = ymin; 
2073     parmax[fNpar++] = ymax; 
2074     if (fNpar > 2) {
2075       param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
2076       parmin[fNpar] = 0; 
2077       parmax[fNpar++] = 1; 
2078     }
2079
2080     // Try new algorithm
2081     min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
2082
2083     while (1) {
2084       max = !min;
2085       Fcn1(fNpar, gin, func0, param, 1); nCall++;
2086       //cout << " Func: " << func0 << endl;
2087
2088       func2[max] = func0;
2089       for (Int_t j=0; j<fNpar; j++) {
2090         param0[max][j] = param[j];
2091         delta[j] = step0[j];
2092         param[j] += delta[j] / 10;
2093         if (j > 0) param[j-1] -= delta[j-1] / 10;
2094         Fcn1(fNpar, gin, func1, param, 1); nCall++;
2095         deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
2096         //cout << j << " " << deriv[max][j] << endl;
2097         dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) / 
2098                                                  (param0[0][j] - param0[1][j]) : 0; // second derivative
2099       }
2100       param[fNpar-1] -= delta[fNpar-1] / 10;
2101       if (nCall > 2000) ::exit(0);
2102
2103       min = func2[0] < func2[1] ? 0 : 1;
2104       nFail = min == max ? 0 : nFail + 1;
2105
2106       stepMax = derMax = estim = 0;
2107       for (Int_t j=0; j<fNpar; j++) { 
2108         // Estimated distance to minimum
2109         shift0 = shift[j];
2110         if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
2111         else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0;
2112         else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
2113               || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) {
2114           shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
2115           if (min == max) { 
2116             if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2117             memory[j]++;
2118           }
2119         } else {
2120           shift[j] = -deriv[min][j] / dder[j];
2121           memory[j] = 0;
2122         }
2123         if (TMath::Abs(shift[j])/step0[j] > estim) { 
2124           estim = TMath::Abs(shift[j])/step0[j];
2125           iestMax = j;
2126         }
2127
2128         // Too big step
2129         if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; // 
2130
2131         // Failed to improve minimum
2132         if (min != max) {
2133           memory[j] = 0;
2134           param[j] = param0[min][j];
2135           if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2;
2136           else shift[j] /= -2;
2137         } 
2138
2139         // Too big step
2140         if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min]) 
2141           shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
2142
2143         // Introduce step relaxation factor
2144         if (memory[j] < 3) {
2145           scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
2146           if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax) 
2147             shift[j] = TMath::Sign (shift0*scMax, shift[j]);
2148         }
2149         param[j] += shift[j]; 
2150           
2151         //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
2152         stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
2153         if (TMath::Abs(deriv[min][j]) > derMax) {
2154           idMax = j;
2155           derMax = TMath::Abs (deriv[min][j]);
2156         }
2157       } // for (Int_t j=0; j<fNpar;
2158       //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl;
2159       if (estim < 1 && derMax < 2 || nLoop > 100) break; // minimum was found
2160
2161       nLoop++;
2162       // Check for small step
2163       if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; }
2164       if (!memory[idMax] && derMax > 0.5 && nLoop > 10) {
2165         //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl;
2166         if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) {
2167           if (min == max) dder[idMax] = -dder[idMax];
2168           shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10; 
2169           param[idMax] += shift[idMax];
2170           stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
2171           //cout << shift[idMax] << " " << param[idMax] << endl;
2172           if (min == max) shiftSave = shift[idMax];
2173         }
2174         if (nFail > 10) {
2175           param[idMax] -= shift[idMax];
2176           shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
2177           param[idMax] += shift[idMax];
2178           //cout << shift[idMax] << endl;
2179         }
2180       }      
2181     } // while (1)
2182     fmin = func2[min];
2183
2184     nDof = npads - fNpar;
2185     chi2n = nDof ? fmin/nDof : 0;
2186
2187     if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
2188     // Save parameters and errors
2189     for (Int_t i=0; i<fNpar; i++) {
2190       parOk[i] = param0[min][i];
2191       errOk[i] = fmin;
2192     }
2193
2194     AliInfo(Form("%f %f ",chi2o ,chi2n));
2195     chi2o = chi2n;
2196     if (fmin < 0.1) break; // !!!???
2197   } // for (Int_t iseed=0; 
2198
2199   for (Int_t i=0; i<fNpar; i++) {
2200     if (i == 4 || i == 7) continue;
2201     AliInfo(Form("%f %f ",parOk[i],errOk[i]));
2202   }
2203   nfit = (fNpar + 1) / 3;
2204   Double_t rad;
2205   Int_t indx, imax;
2206   if (fReco) {
2207     for (Int_t j=0; j<nfit; j++) {
2208       indx = j<2 ? j*2 : j*2+1;  
2209       AddRawCluster (parOk[indx], parOk[indx+1], errOk[indx]);
2210     }
2211     return nfit;
2212   } 
2213   for (Int_t i=0; i<fnMu; i++) {
2214     cmax = fxyMu[i][6];
2215     for (Int_t j=0; j<nfit; j++) {
2216       indx = j<2 ? j*2 : j*2+1;  
2217       rad = (fxyMu[i][0]-parOk[indx])*(fxyMu[i][0]-parOk[indx]) +
2218             (fxyMu[i][1]-parOk[indx+1])*(fxyMu[i][1]-parOk[indx+1]);
2219       if (rad < cmax) {
2220         cmax = rad; 
2221         imax = indx;
2222         fxyMu[i][6] = cmax;
2223         fxyMu[i][2] = parOk[imax] - fxyMu[i][0];
2224         fxyMu[i][4] = parOk[imax+1] - fxyMu[i][1];
2225         fxyMu[i][3] = errOk[imax];
2226         fxyMu[i][5] = errOk[imax+1];
2227       }
2228     }      
2229   }
2230   return nfit;
2231 }  
2232
2233 //_____________________________________________________________________________
2234 void AliMUONClusterFinderAZ::Fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2235 {
2236   // Fit for one track
2237   AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);    
2238   
2239   Int_t cath, ix, iy, indx, npads=0;
2240   Double_t charge, delta, coef=0, chi2=0;
2241   for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) {
2242     if (c.fPadIJ[1][j] != 1) continue;
2243     cath = c.fPadIJ[0][j];
2244     npads++;
2245     c.fSegmentation[cath]->GetPadI(c.fXyq[0][j],c.fXyq[1][j],c.fZpad,ix,iy);
2246     c.fSegmentation[cath]->SetPad(ix,iy);
2247     charge = 0;
2248     for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2249       indx = i<2 ? 2*i : 2*i+1;
2250       c.fSegmentation[cath]->SetHit(par[indx],par[indx+1],c.fZpad);
2251       //charge += c.fResponse->IntXY(c.fSegmentation[cath])*par[icl*3+2];
2252       if (c.fNpar == 2) coef = 1;
2253       else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef;
2254       //coef = TMath::Max (coef, 0.);
2255       if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2256       //coef = TMath::Max (coef, 0.);
2257       charge += c.fResponse->IntXY(c.fSegmentation[cath])*coef;
2258     }
2259     charge *= c.fQtot;
2260     //if (c.fXyq[2][j] > c.fResponse->MaxAdc()-1 && charge > 
2261     //  c.fResponse->MaxAdc()) charge = c.fResponse->MaxAdc(); 
2262     delta = charge - c.fXyq[2][j];
2263     delta /= TMath::Sqrt ((Double_t)c.fXyq[2][j]);
2264     //chi2 += TMath::Abs(delta);
2265     chi2 += delta*delta;
2266   } // for (Int_t j=0;
2267   f = chi2; 
2268   Double_t qAver = c.fQtot/npads; //(c.fnPads[0]+c.fnPads[1]);
2269   f = chi2/qAver;
2270 }
2271
2272 //_____________________________________________________________________________
2273 void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par)
2274 {
2275   // Subtract the fitted charges from pads with strong coupling
2276
2277   Int_t cath, ix, iy, indx;
2278   Double_t charge, coef=0;
2279   for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2280     if (fPadIJ[1][j] != -1) continue;
2281     if (fNpar != 0) {
2282       cath = fPadIJ[0][j];
2283       fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
2284       fSegmentation[cath]->SetPad(ix,iy);
2285       charge = 0;
2286       for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2287         indx = i<2 ? 2*i : 2*i+1;
2288         fSegmentation[cath]->SetHit(par[indx],par[indx+1],fZpad);
2289         if (fNpar == 2) coef = 1;
2290         else coef = i==fNpar/3 ? par[indx+2] : 1-coef;
2291         if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2292         charge += fResponse->IntXY(fSegmentation[cath])*coef;
2293       }
2294       charge *= fQtot;
2295       fXyq[2][j] -= charge;
2296     } // if (fNpar != 0)
2297     if (fXyq[2][j] > fResponse->ZeroSuppression()) fPadIJ[1][j] = 0; // return pad for further using
2298   } // for (Int_t j=0;
2299 }  
2300
2301 //_____________________________________________________________________________
2302 Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) const {
2303 // Test if track was user selected
2304   return kTRUE;
2305   /*
2306     if (fTrack[0]==-1 || fTrack[1]==-1) {
2307         return kTRUE;
2308     } else if (t==fTrack[0] || t==fTrack[1]) {
2309         return kTRUE;
2310     } else {
2311         return kFALSE;
2312     }
2313   */
2314 }
2315
2316 //_____________________________________________________________________________
2317 void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t fmin)
2318 {
2319   //
2320   // Add a raw cluster copy to the list
2321   //
2322   AliMUONRawCluster cnew;
2323   AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2324   //pMUON->AddRawCluster(fInput->Chamber(),c); 
2325
2326   Int_t cath;    
2327   for (cath=0; cath<2; cath++) {
2328     cnew.SetX(cath, x);
2329     cnew.SetY(cath, y);
2330     cnew.SetZ(cath, fZpad);
2331     cnew.SetCharge(cath, 100);
2332     cnew.SetPeakSignal(cath,20);
2333     cnew.SetMultiplicity(cath, 5);
2334     cnew.SetNcluster(cath, 1);
2335     cnew.SetChi2(cath, fmin); //0.1;
2336     /*
2337     cnew.fMultiplicity[cath]=c->fMultiplicity[cath];
2338     for (i=0; i<fMul[cath]; i++) {
2339       cnew.fIndexMap[i][cath]=c->fIndexMap[i][cath];
2340       fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
2341     }
2342     fprintf(stderr,"\nRawCluster %d cath %d\n",ico,cath);
2343     fprintf(stderr,"mult_av %d\n",c->fMultiplicity[cath]);
2344     FillCluster(&cnew,cath);
2345     */
2346   } 
2347   //cnew.fClusterType=cnew.PhysicsContribution();
2348   pMUON->GetMUONData()->AddRawCluster(AliMUONClusterInput::Instance()->Chamber(),cnew); 
2349   //fNPeaks++;
2350 }
2351
2352 //_____________________________________________________________________________
2353 Int_t AliMUONClusterFinderAZ::FindLocalMaxima(Int_t *localMax, Double_t *maxVal)
2354 {
2355   // Find local maxima in pixel space for large preclusters in order to
2356   // try to split them into smaller pieces (to speed up the MLEM procedure)
2357
2358   TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2359   if (hist) hist->Delete();
2360
2361   Double_t xylim[4] = {999, 999, 999, 999};
2362   Int_t nPix = fPixArray->GetEntriesFast();
2363   AliMUONPixel *pixPtr = 0;
2364   for (Int_t ipix=0; ipix<nPix; ipix++) {
2365     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2366     for (Int_t i=0; i<4; i++) 
2367          xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
2368   }
2369   for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2); 
2370
2371   Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
2372   Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
2373   hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2374   for (Int_t ipix=0; ipix<nPix; ipix++) {
2375     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2376     hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
2377   }
2378   if (fDraw) {
2379     ((TCanvas*)gROOT->FindObject("c2"))->cd();
2380     gPad->SetTheta(55);
2381     gPad->SetPhi(30);
2382     hist->Draw("lego1Fb");
2383     gPad->Update();
2384     int ia;
2385     cin >> ia;
2386   }
2387
2388   Int_t nMax = 0, indx;
2389   Int_t *isLocalMax = new Int_t[ny*nx];
2390   for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
2391
2392   for (Int_t i=1; i<=ny; i++) {
2393     indx = (i-1) * nx;
2394     for (Int_t j=1; j<=nx; j++) {
2395       if (hist->GetCellContent(j,i) < 0.5) continue;
2396       //if (isLocalMax[indx+j-1] < 0) continue;
2397       if (isLocalMax[indx+j-1] != 0) continue;
2398       FlagLocalMax(hist, i, j, isLocalMax);
2399     }
2400   }
2401
2402   for (Int_t i=1; i<=ny; i++) {
2403     indx = (i-1) * nx;
2404     for (Int_t j=1; j<=nx; j++) {
2405       if (isLocalMax[indx+j-1] > 0) { 
2406         localMax[nMax] = indx + j - 1; 
2407         maxVal[nMax++] = hist->GetCellContent(j,i);
2408       }
2409       if (nMax > 99) { AliWarning(" Too many local maxima !!!" ); ::exit(0); }
2410     }
2411   }
2412   AliInfo(Form(" Local max: %d",nMax));
2413   delete [] isLocalMax; isLocalMax = 0;
2414   return nMax;
2415 }
2416
2417 //_____________________________________________________________________________
2418 void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2419 {
2420   // Flag pixels (whether or not local maxima)
2421
2422   Int_t nx = hist->GetNbinsX();
2423   Int_t ny = hist->GetNbinsY();
2424   Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
2425   Int_t cont1 = 0;
2426
2427   for (Int_t i1=i-1; i1<i+2; i1++) {
2428     if (i1 < 1 || i1 > ny) continue;
2429     for (Int_t j1=j-1; j1<j+2; j1++) {
2430       if (j1 < 1 || j1 > nx) continue;
2431       if (i == i1 && j == j1) continue;
2432       cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
2433       if (cont < cont1) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2434       else if (cont > cont1) isLocalMax[(i1-1)*nx+j1-1] = -1;
2435       else { // the same charge
2436         isLocalMax[(i-1)*nx+j-1] = 1; 
2437         if (isLocalMax[(i1-1)*nx+j1-1] == 0) {
2438           FlagLocalMax(hist, i1, j1, isLocalMax);
2439           if (isLocalMax[(i1-1)*nx+j1-1] < 0) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2440           else isLocalMax[(i1-1)*nx+j1-1] = -1;
2441         }
2442       } 
2443     }
2444   }
2445   isLocalMax[(i-1)*nx+j-1] = 1; // local maximum
2446 }
2447
2448 //_____________________________________________________________________________
2449 void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2450 {
2451   // Find pixel cluster around local maximum #iMax and pick up pads
2452   // overlapping with it
2453
2454   TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2455   Int_t nx = hist->GetNbinsX();
2456   Int_t ny = hist->GetNbinsY();
2457   Int_t ic = localMax[iMax] / nx + 1;
2458   Int_t jc = localMax[iMax] % nx + 1;
2459   Bool_t *used = new Bool_t[ny*nx];
2460   for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
2461
2462   // Drop all pixels from the array - pick up only the ones from the cluster
2463   fPixArray->Delete();
2464
2465   Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2; 
2466   Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;  
2467   Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
2468   Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
2469   Double_t cont = hist->GetCellContent(jc,ic);
2470   AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont);
2471   fPixArray->Add((TObject*)pixPtr);
2472   used[(ic-1)*nx+jc-1] = kTRUE;
2473   AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
2474
2475   Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1];
2476   for (Int_t i=0; i<nPix; i++) {
2477     ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx); 
2478     ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy); 
2479   }
2480   AliInfo(Form("%d %d ",iMax,nPix));
2481
2482   Float_t xy[4], xy12[4];
2483   // Pick up pads which overlap with found pixels
2484   for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1;
2485   for (Int_t i=0; i<nPix; i++) {
2486     pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
2487     for (Int_t j=0; j<4; j++) 
2488       xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2);
2489     for (Int_t j=0; j<npad; j++) 
2490       if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use
2491   }
2492
2493   delete [] used; used = 0;
2494 }
2495
2496 //_____________________________________________________________________________
2497 AliMUONClusterFinderAZ&  
2498 AliMUONClusterFinderAZ::operator=(const AliMUONClusterFinderAZ& rhs)
2499 {
2500 // Protected assignement operator
2501
2502   if (this == &rhs) return *this;
2503
2504   AliFatal("Not implemented.");
2505     
2506   return *this;  
2507 }    
2508