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