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