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