1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 // Clusterizer class developped by Zitchenko (Dubna)
21 #include <Riostream.h>
32 #include "AliMUONClusterFinderAZ.h"
33 #include "AliHeader.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"
46 ClassImp(AliMUONClusterFinderAZ)
48 const Double_t AliMUONClusterFinderAZ::fgkCouplMin = 1.e-3; // threshold on coupling
49 AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = 0x0;
50 TMinuit* AliMUONClusterFinderAZ::fgMinuit = 0x0;
52 //_____________________________________________________________________________
53 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw, Int_t iReco)
54 : AliMUONClusterFinderVS()
57 for (Int_t i=0; i<4; i++) {fHist[i] = 0;}
59 fSegmentation[1] = fSegmentation[0] = 0;
60 fgClusterFinder = 0x0;
62 if (!fgClusterFinder) fgClusterFinder = this;
63 if (!fgMinuit) fgMinuit = new TMinuit(8);
66 fPixArray = new TObjArray(20);
72 fNextCathode = kFALSE;
77 //_____________________________________________________________________________
78 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(const AliMUONClusterFinderAZ& rhs)
79 : AliMUONClusterFinderVS(rhs)
81 // Protected copy constructor
83 AliFatal("Not implemented.");
86 //_____________________________________________________________________________
87 AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ()
90 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
92 // Delete space point structure
93 if (fPoints) fPoints->Delete();
97 if (fPhits) fPhits->Delete();
101 if (fRpoints) fRpoints->Delete();
107 //_____________________________________________________________________________
108 void AliMUONClusterFinderAZ::FindRawClusters()
110 // To provide the same interface as in AliMUONClusterFinderVS
112 EventLoop (gAlice->GetHeader()->GetEvent(), AliMUONClusterInput::Instance()->Chamber());
115 //_____________________________________________________________________________
116 void AliMUONClusterFinderAZ::EventLoop(Int_t nev=0, Int_t ch=0)
124 Double_t p1[3]={0}, p2[3];
128 lun = fopen("pool.dat","w");
129 c1 = new TCanvas("c1","Clusters",0,0,600,700);
131 new TCanvas("c2","Mlem",700,0,600,350);
135 Int_t nparticles = 0, nent;
138 AliRunLoader * rl = AliRunLoader::GetRunLoader();
139 AliLoader * gime = rl->GetLoader("MUONLoader");
141 if (!fReco) nparticles = rl->GetEvent(nev);
142 else nparticles = gAlice->GetMCApp()->GetNtrack();
143 AliInfo(Form("nev %d",nev));
144 AliInfo(Form("nparticles %d",nparticles));
145 if (nparticles <= 0) return;
147 TTree *treeH = gime->TreeH();
148 Int_t ntracks = (Int_t) treeH->GetEntries();
149 AliInfo(Form("ntracks %d",ntracks));
151 // Get pointers to Alice detectors and Digits containers
152 AliMUON *muon = (AliMUON*) gAlice->GetModule("MUON");
154 // TClonesArray *Particles = gAlice->Particles();
156 treeR = gime->TreeR();
158 muon->ResetRawClusters();
159 nent = (Int_t) treeR->GetEntries();
161 AliError(Form("nent = %d not equal to 1",nent));
167 TTree *treeD = gime->TreeD();
168 //muon->ResetDigits();
170 TClonesArray *listMUONrawclust ;
171 AliMUONChamber* iChamber = 0;
173 // As default draw the first cluster of the chamber #0
176 if (ch > 9) {if (fReco) return; nev++; ch = 0; goto newev;}
177 //gAlice->ResetDigits();
178 fMuonDigits = muon->GetMUONData()->Digits(ch);
179 if (fMuonDigits == 0) return;
180 iChamber = &(muon->Chamber(ch));
181 fSegmentation[0] = iChamber->SegmentationModel(1);
182 fSegmentation[1] = iChamber->SegmentationModel(2);
183 fResponse = iChamber->ResponseModel();
188 nent = (Int_t) treeD->GetEntries();
189 //printf(" entries %d \n", nent);
192 Int_t ndigits[2]={9,9}, nShown[2]={0};
193 for (Int_t i=0; i<2; i++) {
194 for (Int_t j=0; j<fgkDim; j++) {fUsed[i][j]=kFALSE;}
198 if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) {
202 goto newchamber; // next chamber
204 Float_t xpad, ypad, zpad, zpad0;
207 Bool_t first = kTRUE;
208 AliInfo(Form(" *** Event # %d chamber: %d " , nev ,ch ));
209 fnPads[0] = fnPads[1] = 0;
210 for (Int_t i=0; i<fgkDim; i++) {fPadIJ[1][i] = 0;}
211 //for (Int_t iii = 0; iii<999; iii++) {
212 for (Int_t iii = 0; iii<2; iii++) {
213 Int_t cath = TMath::Odd(iii);
214 gAlice->ResetDigits();
215 treeD->GetEvent(cath);
216 fMuonDigits = muon->GetMUONData()->Digits(ch);
218 ndigits[cath] = fMuonDigits->GetEntriesFast();
219 if (!ndigits[0] && !ndigits[1]) {if (fReco) return; ch++; goto newchamber;}
220 if (ndigits[cath] == 0) continue;
221 AliInfo(Form(" ndigits: %d %d " , ndigits[cath] , cath));
226 Bool_t eEOC = kTRUE; // end-of-cluster
227 for (digit = 0; digit < ndigits[cath]; digit++) {
228 mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
229 if (mdig->Cathode() != cath) continue;
231 // Find first unused pad
232 if (fUsed[cath][digit]) continue;
233 fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0);
235 if (fUsed[cath][digit]) continue;
236 fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
237 if (TMath::Abs(zpad-zpad0)>0.1) continue; // different slats
238 // Find a pad overlapping with the cluster
239 if (!Overlap(cath,mdig)) continue;
241 // Add pad - recursive call
244 if (digit >= 0) break;
247 // No more unused pads
248 if (cath == 0) continue; // on cathode #0 - check #1
253 goto newchamber; // next chamber
256 if (eEOC) break; // cluster found
258 AliInfo(Form(" nPads: %d %d %d ",fnPads[cath] ,nShown[cath]+fnPads[cath],cath));
259 } // for (Int_t iii = 0;
262 if (fReco) goto skip;
264 for (Int_t cath = 0; cath<2; cath++) {
266 if (fHist[cath*2]) {fHist[cath*2]->Delete(); fHist[cath*2] = 0;}
267 if (fHist[cath*2+1]) {fHist[cath*2+1]->Delete(); fHist[cath*2+1] = 0;}
268 if (fnPads[cath] == 0) continue; // cluster on one cathode only
269 Float_t wxMin=999, wxMax=0, wyMin=999, wyMax=0;
270 Int_t minDx=0, maxDx=0, minDy=0, maxDy=0;
271 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
272 if (fPadIJ[0][i] != cath) continue;
273 if (fXyq[3][i] < wxMin) {wxMin = fXyq[3][i]; minDx = i;}
274 if (fXyq[3][i] > wxMax) {wxMax = fXyq[3][i]; maxDx = i;}
275 if (fXyq[4][i] < wyMin) {wyMin = fXyq[4][i]; minDy = i;}
276 if (fXyq[4][i] > wyMax) {wyMax = fXyq[4][i]; maxDy = i;}
278 AliInfo(Form("%d %d %d %d", minDx , maxDx , minDy , maxDy ));
279 Int_t nx, ny, padSize;
280 Float_t xmin=9999, xmax=-9999, ymin=9999, ymax=-9999;
281 if (TMath::Nint(fXyq[3][minDx]*1000) == TMath::Nint(fXyq[3][maxDx]*1000) &&
282 TMath::Nint(fXyq[4][minDy]*1000) == TMath::Nint(fXyq[4][maxDy]*1000)) {
283 // the same segmentation
285 AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
286 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
287 if (fPadIJ[0][i] != cath) continue;
288 if (fXyq[0][i] < xmin) xmin = fXyq[0][i];
289 if (fXyq[0][i] > xmax) xmax = fXyq[0][i];
290 if (fXyq[1][i] < ymin) ymin = fXyq[1][i];
291 if (fXyq[1][i] > ymax) ymax = fXyq[1][i];
293 xmin -= fXyq[3][minDx]; xmax += fXyq[3][minDx];
294 ymin -= fXyq[4][minDy]; ymax += fXyq[4][minDy];
295 nx = TMath::Nint ((xmax-xmin)/wxMin/2);
296 ny = TMath::Nint ((ymax-ymin)/wyMin/2);
297 sprintf(hName,"h%d",cath*2);
298 fHist[cath*2] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
299 cout << fHist[cath*2] << " " << fnPads[cath] << endl;
300 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
301 if (fPadIJ[0][i] != cath) continue;
302 fHist[cath*2]->Fill(fXyq[0][i],fXyq[1][i],fXyq[2][i]);
303 //cout << fXyq[0][i] << fXyq[1][i] << fXyq[2][i] << endl;
306 // different segmentation in the cluster
307 AliInfo(" Different\n");
308 AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
310 Int_t indx, locMin, locMax;
311 if (TMath::Nint(fXyq[3][minDx]*1000) != TMath::Nint(fXyq[3][maxDx]*1000)) {
312 // different segmentation along x
317 // different segmentation along y
323 for (Int_t i=0; i<2; i++) {
324 // loop over different pad sizes
325 if (i>0) loc = locMax;
326 padSize = TMath::Nint(fXyq[indx+3][loc]*1000);
327 xmin = 9999; xmax = -9999; ymin = 9999; ymax = -9999;
328 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
329 if (fPadIJ[0][j] != cath) continue;
330 if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
332 xmin = TMath::Min (xmin,fXyq[0][j]);
333 xmax = TMath::Max (xmax,fXyq[0][j]);
334 ymin = TMath::Min (ymin,fXyq[1][j]);
335 ymax = TMath::Max (ymax,fXyq[1][j]);
337 xmin -= fXyq[3][loc]; xmax += fXyq[3][loc];
338 ymin -= fXyq[4][loc]; ymax += fXyq[4][loc];
339 nx = TMath::Nint ((xmax-xmin)/fXyq[3][loc]/2);
340 ny = TMath::Nint ((ymax-ymin)/fXyq[4][loc]/2);
341 sprintf(hName,"h%d",cath*2+i);
342 fHist[cath*2+i] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
343 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
344 if (fPadIJ[0][j] != cath) continue;
345 if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
346 fHist[cath*2+i]->Fill(fXyq[0][j],fXyq[1][j],fXyq[2][j]);
349 if (nOK != fnPads[cath])
350 AliInfo(Form(" *** Too many segmentations: nPads, nOK %d %d",fnPads[cath],nOK));
351 } // if (TMath::Nint(fXyq[3][minDx]*1000)
352 } // for (Int_t cath = 0;
354 // Draw histograms and coordinates
355 for (Int_t cath=0; cath<2; cath++) {
356 if (cath == 0) ModifyHistos();
357 if (fnPads[cath] == 0) continue; // cluster on one cathode only
362 Double_t x, y, x0, y0, r1=999, r2=0;
363 if (fHist[cath*2+1]) {
365 x0 = fHist[cath*2]->GetXaxis()->GetXmin() - 1000*TMath::Cos(30*TMath::Pi()/180);
366 y0 = fHist[cath*2]->GetYaxis()->GetXmin() - 1000*TMath::Sin(30*TMath::Pi()/180);
369 for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
370 y = fHist[ihist]->GetYaxis()->GetBinCenter(iy)
371 + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
372 for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
373 if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
374 x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
375 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
376 r1 = TMath::Max (r1,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
381 for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
382 y = fHist[ihist]->GetYaxis()->GetBinCenter(iy)
383 + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
384 for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
385 if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
386 x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
387 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
388 r2 = TMath::Max (r2,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
392 AliInfo(Form("%f %f \n",r1,r2));
393 } // if (fHist[cath*2+1])
395 //fHist[cath*2]->Draw("lego1");
396 fHist[cath*2]->Draw("lego1Fb");
397 //if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBb");
398 if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBbFb");
400 //fHist[cath*2+1]->Draw("lego1");
401 fHist[cath*2+1]->Draw("lego1Fb");
402 //fHist[cath*2]->Draw("lego1SameAxisBb");
403 fHist[cath*2]->Draw("lego1SameAxisFbBb");
407 } // for (Int_t cath = 0;
409 // Draw generated hits
411 hist = fHist[0] ? fHist[0] : fHist[2];
412 p2[2] = hist->GetMaximum();
414 if (c1) view = c1->Pad()->GetView();
415 AliInfo(" *** GEANT hits *** ");
418 for (Int_t i=0; i<ntracks; i++) {
420 for (AliMUONHit* mHit=(AliMUONHit*)muon->FirstHit(-1);
422 mHit=(AliMUONHit*)muon->NextHit()) {
423 if (mHit->Chamber() != ch+1) continue; // chamber number
424 if (TMath::Abs(mHit->Z()-zpad0) > 1) continue; // different slat
425 p2[0] = p1[0] = mHit->X(); // x-pos of hit
426 p2[1] = p1[1] = mHit->Y(); // y-pos
427 if (p1[0] < hist->GetXaxis()->GetXmin() ||
428 p1[0] > hist->GetXaxis()->GetXmax()) continue;
429 if (p1[1] < hist->GetYaxis()->GetXmin() ||
430 p1[1] > hist->GetYaxis()->GetXmax()) continue;
431 // Check if track comes thru pads with signal
433 for (Int_t ihist=0; ihist<4; ihist++) {
434 if (!fHist[ihist]) continue;
435 ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
436 iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
437 if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
440 gStyle->SetLineColor(1);
441 if (TMath::Abs((Int_t)mHit->Particle()) == 13) {
442 gStyle->SetLineColor(4);
445 fxyMu[fnMu-1][0] = p1[0];
446 fxyMu[fnMu-1][1] = p1[1];
449 AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mHit->Z()));
451 view->WCtoNDC(p1, &xNDC[0]);
452 view->WCtoNDC(p2, &xNDC[3]);
453 for (Int_t ipad=1; ipad<3; ipad++) {
455 //c1->DrawLine(xpad[0],xpad[1],xpad[3],xpad[4]);
456 line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
457 line[nLine++]->Draw();
460 } // for (AliMUONHit* mHit=
461 } // for (Int_t i=0; i<ntracks;
463 // Draw reconstructed coordinates
464 listMUONrawclust = muon->GetMUONData()->RawClusters(ch);
466 //cout << listMUONrawclust << " " << listMUONrawclust ->GetEntries() << endl;
467 AliMUONRawCluster *mRaw;
468 gStyle->SetLineColor(3);
469 AliInfo(" *** Reconstructed hits *** ");
470 for (Int_t i=0; i<listMUONrawclust ->GetEntries(); i++) {
471 mRaw = (AliMUONRawCluster*)listMUONrawclust ->UncheckedAt(i);
472 if (TMath::Abs(mRaw->GetZ(0)-zpad0) > 1) continue; // different slat
473 p2[0] = p1[0] = mRaw->GetX(0); // x-pos of hit
474 p2[1] = p1[1] = mRaw->GetY(0); // y-pos
475 if (p1[0] < hist->GetXaxis()->GetXmin() ||
476 p1[0] > hist->GetXaxis()->GetXmax()) continue;
477 if (p1[1] < hist->GetYaxis()->GetXmin() ||
478 p1[1] > hist->GetYaxis()->GetXmax()) continue;
480 treeD->GetEvent(cath);
481 cout << mRaw->fMultiplicity[0] << mRaw->fMultiplicity[1] << endl;
482 for (Int_t j=0; j<mRaw->fMultiplicity[cath]; j++) {
483 Int_t digit = mRaw->fIndexMap[j][cath];
484 cout << ((AliMUONDigit*)fMuonDigits->UncheckedAt(digit))->Signal() << endl;
487 // Check if track comes thru pads with signal
489 for (Int_t ihist=0; ihist<4; ihist++) {
490 if (!fHist[ihist]) continue;
491 ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
492 iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
493 if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
496 AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mRaw->GetZ(0)));
498 view->WCtoNDC(p1, &xNDC[0]);
499 view->WCtoNDC(p2, &xNDC[3]);
500 for (Int_t ipad=1; ipad<3; ipad++) {
502 line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
503 line[nLine++]->Draw();
506 } // for (Int_t i=0; i<listMUONrawclust ->GetEntries();
507 if (fDraw) c1->Update();
510 // Use MLEM for cluster finder
512 Int_t nMax = 1, localMax[100], maxPos[100];
513 Double_t maxVal[100];
515 if (CheckPrecluster(nShown)) {
517 if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(localMax, maxVal);
518 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
519 for (Int_t i=0; i<nMax; i++) {
520 if (nMax > 1) FindCluster(localMax, maxPos[i]);
521 if (!MainLoop()) AliInfo(" MainLoop failed ");
523 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
524 if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
526 fXyq[2][j] = fXyq[5][j]; // use backup charge value
531 if (fReco) goto next;
533 for (Int_t i=0; i<fnMu; i++) {
534 // Check again if muon come thru the used pads (due to extra splitting)
535 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
536 if (TMath::Abs(fxyMu[i][0]-fXyq[0][j])<fXyq[3][j] &&
537 TMath::Abs(fxyMu[i][1]-fXyq[1][j])<fXyq[4][j]) {
538 AliInfo(Form("%12.3e %12.3e %12.3e %12.3e\n",fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]));
539 if (lun) fprintf(lun,"%4d %2d %12.3e %12.3e %12.3e %12.3e\n",nev,ch,fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]);
543 } // for (Int_t i=0; i<fnMu;
547 AliInfo(" What is next? ");
549 if (fDraw) gets(command);
550 if (command[0] == 'n' || command[0] == 'N') {nev++; goto newev;} // next event
551 else if (command[0] == 'q' || command[0] == 'Q') {fclose(lun); return;} // exit display
552 //else if (command[0] == 'r' || command[0] == 'R') goto redraw; // redraw points
553 else if (command[0] == 'c' || command[0] == 'C') {
555 sscanf(command+1,"%d",&ch);
558 else if (command[0] == 'e' || command[0] == 'E') {
560 sscanf(command+1,"%d",&nev);
563 else goto next; // Next cluster
566 //_____________________________________________________________________________
567 void AliMUONClusterFinderAZ::ModifyHistos(void)
569 // Modify histograms to bring them to the same size
571 Float_t hlim[4][4], hbin[4][4]; // first index - xmin, xmax, ymin, ymax
572 Float_t binMin[4] = {999,999,999,999};
574 for (Int_t i=0; i<4; i++) {
575 if (!fHist[i]) continue;
576 hlim[0][nhist] = fHist[i]->GetXaxis()->GetXmin(); // xmin
577 hlim[1][nhist] = fHist[i]->GetXaxis()->GetXmax(); // xmax
578 hlim[2][nhist] = fHist[i]->GetYaxis()->GetXmin(); // ymin
579 hlim[3][nhist] = fHist[i]->GetYaxis()->GetXmax(); // ymax
580 hbin[0][nhist] = hbin[1][nhist] = fHist[i]->GetXaxis()->GetBinWidth(1);
581 hbin[2][nhist] = hbin[3][nhist] = fHist[i]->GetYaxis()->GetBinWidth(1);
582 binMin[0] = TMath::Min(binMin[0],hbin[0][nhist]);
583 binMin[2] = TMath::Min(binMin[2],hbin[2][nhist]);
586 binMin[1] = binMin[0];
587 binMin[3] = binMin[2];
588 AliInfo(Form(" Nhist: %d",nhist));
591 for (Int_t lim=0; lim<4; lim++) {
593 imin = TMath::LocMin(nhist,hlim[lim]);
594 imax = TMath::LocMax(nhist,hlim[lim]);
595 if (TMath::Abs(hlim[lim][imin]-hlim[lim][imax])<0.01*binMin[lim]) break;
596 if (lim == 0 || lim == 2) {
598 hlim[lim][imax] -= hbin[lim][imax];
601 hlim[lim][imin] += hbin[lim][imin];
606 // Rebuild histograms
610 Double_t x, y, cont, cmax=0;
612 for (Int_t ihist=0; ihist<4; ihist++) {
613 if (!fHist[ihist]) continue;
614 nx = TMath::Nint((hlim[1][nhist]-hlim[0][nhist])/hbin[0][nhist]);
615 ny = TMath::Nint((hlim[3][nhist]-hlim[2][nhist])/hbin[2][nhist]);
616 //hist = new TH2F("h","hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
617 sprintf(hName,"hh%d",ihist);
618 hist = new TH2F(hName,"hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
619 for (Int_t i=1; i<=fHist[ihist]->GetNbinsX(); i++) {
620 x = fHist[ihist]->GetXaxis()->GetBinCenter(i);
621 for (Int_t j=1; j<=fHist[ihist]->GetNbinsY(); j++) {
622 y = fHist[ihist]->GetYaxis()->GetBinCenter(j);
623 cont = fHist[ihist]->GetCellContent(i,j);
624 hist->Fill(x,y,cont);
627 cmax = TMath::Max (cmax,hist->GetMaximum());
628 fHist[ihist]->Delete();
629 fHist[ihist] = new TH2F(*hist);
633 AliInfo(Form("%f \n",cmax));
635 for (Int_t ihist=0; ihist<4; ihist++) {
636 if (!fHist[ihist]) continue;
637 fHist[ihist]->SetMaximum(cmax);
641 //_____________________________________________________________________________
642 void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
644 // Add pad to the cluster
645 AliMUONDigit *mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
647 Int_t charge = mdig->Signal();
648 // get the center of the pad
649 Float_t xpad, ypad, zpad;
650 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
652 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
653 Int_t nPads = fnPads[0] + fnPads[1];
654 fXyq[0][nPads] = xpad;
655 fXyq[1][nPads] = ypad;
656 fXyq[2][nPads] = charge;
657 fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2;
658 fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2;
659 fXyq[5][nPads] = digit;
660 fPadIJ[0][nPads] = cath;
661 fPadIJ[1][nPads] = 0;
662 fUsed[cath][digit] = kTRUE;
663 //cout << " bbb " << fXyq[cath][2][nPads] << " " << fXyq[cath][0][nPads] << " " << fXyq[cath][1][nPads] << " " << fXyq[cath][3][nPads] << " " << fXyq[cath][4][nPads] << " " << zpad << " " << nPads << endl;
667 Int_t nn, ix, iy, xList[10], yList[10];
670 Int_t ndigits = fMuonDigits->GetEntriesFast();
671 fSegmentation[cath]->Neighbours(mdig->PadX(),mdig->PadY(),&nn,xList,yList);
672 for (Int_t in=0; in<nn; in++) {
675 for (Int_t digit1 = 0; digit1 < ndigits; digit1++) {
676 if (digit1 == digit) continue;
677 mdig1 = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit1);
678 if (mdig1->Cathode() != cath) continue;
679 if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) {
680 fUsed[cath][digit1] = kTRUE;
681 // Add pad - recursive call
684 } //for (Int_t digit1 = 0;
685 } // for (Int_t in=0;
688 //_____________________________________________________________________________
689 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, TObject *dig)
691 // Check if the pad from one cathode overlaps with a pad
692 // in the precluster on the other cathode
694 AliMUONDigit *mdig = (AliMUONDigit*) dig;
696 Float_t xpad, ypad, zpad;
697 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
698 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
700 Float_t xy1[4], xy12[4];
701 xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2;
702 xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec);
703 xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2;
704 xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec);
705 //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
707 Int_t cath1 = TMath::Even(cath);
708 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
709 if (fPadIJ[0][i] != cath1) continue;
710 if (Overlap(xy1, i, xy12, 0)) return kTRUE;
715 //_____________________________________________________________________________
716 Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
718 // Check if the pads xy1 and iPad overlap and return overlap area
721 xy2[0] = fXyq[0][iPad] - fXyq[3][iPad];
722 xy2[1] = fXyq[0][iPad] + fXyq[3][iPad];
723 if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE;
724 xy2[2] = fXyq[1][iPad] - fXyq[4][iPad];
725 xy2[3] = fXyq[1][iPad] + fXyq[4][iPad];
726 if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE;
727 if (!iSkip) return kTRUE; // just check overlap (w/out computing the area)
728 xy12[0] = TMath::Max (xy1[0],xy2[0]);
729 xy12[1] = TMath::Min (xy1[1],xy2[1]);
730 xy12[2] = TMath::Max (xy1[2],xy2[2]);
731 xy12[3] = TMath::Min (xy1[3],xy2[3]);
735 //_____________________________________________________________________________
737 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t i, Int_t j, Float_t *xy12, Int_t iSkip)
739 // Check if the pads i and j overlap and return overlap area
741 Float_t xy1[4], xy2[4];
742 return Overlap(xy1, xy2, xy12, iSkip);
745 //_____________________________________________________________________________
746 Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
748 // Check precluster in order to attempt to simplify it (mostly for
749 // two-cathode preclusters)
752 Float_t xy1[4], xy12[4];
754 Int_t npad = fnPads[0] + fnPads[1];
756 // If pads have the same size take average of pads on both cathodes
757 Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
759 Double_t xSize = -1, ySize = 0;
760 for (Int_t i=0; i<npad; i++) {
761 if (fXyq[2][i] < 0) continue;
762 if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; }
763 if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 || TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; }
766 if (sameSize && (fnPads[0] > 2 || fnPads[1] > 2)) {
767 nShown[0] += fnPads[0];
768 nShown[1] += fnPads[1];
769 fnPads[0] = fnPads[1] = 0;
771 for (Int_t i=0; i<npad; i++) {
772 if (fXyq[2][i] < 0) continue; // used pad
773 fXyq[2][fnPads[0]] = fXyq[2][i];
775 for (Int_t j=i+1; j<npad; j++) {
776 if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode
777 if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue;
778 if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue;
779 fXyq[2][fnPads[0]] += fXyq[2][j];
784 fXyq[2][fnPads[0]] /= div;
785 fXyq[0][fnPads[0]] = fXyq[0][i];
786 fXyq[1][fnPads[0]] = fXyq[1][i];
787 fPadIJ[0][fnPads[0]++] = 0;
791 // Check if one-cathode precluster
792 i1 = fnPads[0]!=0 ? 0 : 1;
793 i2 = fnPads[1]!=0 ? 1 : 0;
795 if (i1 != i2) { // two-cathode
797 Int_t *flags = new Int_t[npad];
798 for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
800 // Check pad overlaps
801 for (Int_t i=0; i<npad; i++) {
802 if (fPadIJ[0][i] != i1) continue;
803 xy1[0] = fXyq[0][i] - fXyq[3][i];
804 xy1[1] = fXyq[0][i] + fXyq[3][i];
805 xy1[2] = fXyq[1][i] - fXyq[4][i];
806 xy1[3] = fXyq[1][i] + fXyq[4][i];
807 for (Int_t j=0; j<npad; j++) {
808 if (fPadIJ[0][j] != i2) continue;
809 if (!Overlap(xy1, j, xy12, 0)) continue;
810 flags[i] = flags[j] = 1; // mark overlapped pads
814 // Check if all pads overlap
815 Int_t digit=0, cath, nFlags=0;
816 for (Int_t i=0; i<npad; i++) {nFlags += !flags[i];}
817 if (nFlags) AliInfo(Form(" nFlags = %d",nFlags));
818 //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
820 for (Int_t i=0; i<npad; i++) {
821 if (flags[i]) continue;
822 digit = TMath::Nint (fXyq[5][i]);
824 fUsed[cath][digit] = kFALSE; // release pad
830 // Check correlations of cathode charges
831 if (fnPads[0] && fnPads[1]) { // two-cathode
833 Int_t over[2] = {1, 1};
834 for (Int_t i=0; i<npad; i++) {
836 if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
837 if (fXyq[2][i] > fResponse->MaxAdc()-1) over[cath] = 0;
839 AliInfo(Form(" Total charge: %f %f",sum[0],sum[1]));
840 if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
841 AliInfo(" Release ");
843 cath = sum[0]>sum[1] ? 0 : 1;
846 Double_t *dist = new Double_t[npad];
847 for (Int_t i=0; i<npad; i++) {
848 if (fPadIJ[0][i] != cath) continue;
849 if (fXyq[2][i] < cmax) continue;
853 // Arrange pads according to their distance to the max,
854 // normalized to the pad size
855 for (Int_t i=0; i<npad; i++) {
857 if (fPadIJ[0][i] != cath) continue;
858 if (i == imax) continue;
859 if (fXyq[2][i] < 0) continue;
860 dist[i] = (fXyq[0][i]-fXyq[0][imax])*(fXyq[0][i]-fXyq[0][imax])/
861 fXyq[3][imax]/fXyq[3][imax]/4;
862 dist[i] += (fXyq[1][i]-fXyq[1][imax])*(fXyq[1][i]-fXyq[1][imax])/
863 fXyq[4][imax]/fXyq[4][imax]/4;
864 dist[i] = TMath::Sqrt (dist[i]);
866 TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
869 for (Int_t i=0; i<npad; i++) {
871 if (fPadIJ[0][indx] != cath) continue;
872 if (fXyq[2][indx] < 0) continue;
873 if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax)<1.e-3) {
875 if (TMath::Abs(dist[indx]-xmax)<1.e-3)
876 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
877 else cmax = fXyq[2][indx];
879 digit = TMath::Nint (fXyq[5][indx]);
880 fUsed[cath][digit] = kFALSE;
883 // xmax = dist[i]; // Bug?
887 delete [] dist; dist = 0;
888 } // TMath::Abs(sum[0]-sum[1])...
889 } // if (fnPads[0] && fnPads[1])
890 delete [] flags; flags = 0;
893 if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
895 // Move released pads to the right
896 Int_t beg = 0, end = npad-1, padij;
899 if (fXyq[2][beg] > 0) { beg++; continue; }
900 for (Int_t j=end; j>beg; j--) {
901 if (fXyq[2][j] < 0) continue;
903 for (Int_t j1=0; j1<2; j1++) {
904 padij = fPadIJ[j1][beg];
905 fPadIJ[j1][beg] = fPadIJ[j1][j];
906 fPadIJ[j1][j] = padij;
908 for (Int_t j1=0; j1<6; j1++) {
910 fXyq[j1][beg] = fXyq[j1][j];
914 } // for (Int_t j=end;
917 npad = fnPads[0] + fnPads[1];
918 if (npad > 500) { AliInfo(Form(" ***** Too large cluster. Give up. ",npad )); return kFALSE; }
919 // Back up charge value
920 for (Int_t j=0; j<npad; j++) fXyq[5][j] = fXyq[2][j];
925 //_____________________________________________________________________________
926 void AliMUONClusterFinderAZ::BuildPixArray()
928 // Build pixel array for MLEM method
930 Int_t nPix=0, i1, i2;
931 Float_t xy1[4], xy12[4];
932 AliMUONPixel *pixPtr=0;
934 Int_t npad = fnPads[0] + fnPads[1];
936 // One cathode is empty
937 i1 = fnPads[0]!=0 ? 0 : 1;
938 i2 = fnPads[1]!=0 ? 1 : 0;
940 // Build array of pixels on anode plane
941 if (i1 == i2) { // one-cathode precluster
942 for (Int_t j=0; j<npad; j++) {
943 pixPtr = new AliMUONPixel();
944 for (Int_t i=0; i<2; i++) {
945 pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates
946 pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size
948 pixPtr->SetCharge(fXyq[2][j]); // charge
949 fPixArray->Add((TObject*)pixPtr);
952 } else { // two-cathode precluster
953 for (Int_t i=0; i<npad; i++) {
954 if (fPadIJ[0][i] != i1) continue;
955 xy1[0] = fXyq[0][i] - fXyq[3][i];
956 xy1[1] = fXyq[0][i] + fXyq[3][i];
957 xy1[2] = fXyq[1][i] - fXyq[4][i];
958 xy1[3] = fXyq[1][i] + fXyq[4][i];
959 for (Int_t j=0; j<npad; j++) {
960 if (fPadIJ[0][j] != i2) continue;
961 if (!Overlap(xy1, j, xy12, 1)) continue;
962 pixPtr = new AliMUONPixel();
963 for (Int_t k=0; k<2; k++) {
964 pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates
965 pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size
967 pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
968 fPixArray->Add((TObject*)pixPtr);
974 Float_t wxmin=999, wymin=999;
975 for (Int_t i=0; i<npad; i++) {
976 if (fPadIJ[0][i] == i1) wymin = TMath::Min (wymin,fXyq[4][i]);
977 if (fPadIJ[0][i] == i2) wxmin = TMath::Min (wxmin,fXyq[3][i]);
979 AliInfo(Form("%f %f ",wxmin,wymin));
981 // Check if small pixel X-size
982 AjustPixel(wxmin, 0);
983 // Check if small pixel Y-size
984 AjustPixel(wymin, 1);
985 // Check if large pixel size
986 AjustPixel(wxmin, wymin);
988 // Remove discarded pixels
989 for (Int_t i=0; i<nPix; i++) {
990 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
992 if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
994 fPixArray->Compress();
995 nPix = fPixArray->GetEntriesFast();
998 AliInfo(Form("nPix %d ",nPix));
999 // Too many pixels - sort and remove pixels with the lowest signal
1001 for (Int_t i=npad; i<nPix; i++) {
1002 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1004 fPixArray->RemoveAt(i);
1008 } // if (nPix > npad)
1010 // Set pixel charges to the same value (for MLEM)
1011 for (Int_t i=0; i<nPix; i++) {
1012 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1013 //pixPtr->SetCharge(10);
1014 AliInfo(Form("%d %f %f %f %f",i+1,pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Size(0),pixPtr->Size(1)));
1018 //_____________________________________________________________________________
1019 void AliMUONClusterFinderAZ::AjustPixel(Float_t width, Int_t ixy)
1021 // Check if some pixels have small size (ajust if necessary)
1023 AliMUONPixel *pixPtr, *pixPtr1 = 0;
1024 Int_t ixy1 = TMath::Even(ixy);
1025 Int_t nPix = fPixArray->GetEntriesFast();
1027 for (Int_t i=0; i<nPix; i++) {
1028 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1029 if (pixPtr->Charge() < 1) continue; // discarded pixel
1030 if (pixPtr->Size(ixy)-width < -1.e-4) {
1032 AliInfo(Form(" Small X or Y: %d %f %f %f %f",ixy,pixPtr->Size(ixy),width,pixPtr->Coord(0),pixPtr->Coord(1)));
1033 for (Int_t j=i+1; j<nPix; j++) {
1034 pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j);
1035 if (pixPtr1->Charge() < 1) continue; // discarded pixel
1036 if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size
1037 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns
1038 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) {
1040 pixPtr->SetSize(ixy, width);
1041 pixPtr->SetCoord(ixy, (pixPtr->Coord(ixy)+pixPtr1->Coord(ixy))/2);
1042 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
1043 pixPtr1->SetCharge(0);
1047 } // for (Int_t j=i+1;
1048 //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ???
1049 //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) {
1050 if (pixPtr1 || i == nPix-1) {
1051 // edge pixel - just increase its size
1052 AliInfo(" Edge ...");
1053 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1054 // ???if (fPadIJ[0][j] != i1) continue;
1055 if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue;
1056 if (pixPtr->Coord(ixy) < fXyq[ixy][j])
1057 pixPtr->Shift(ixy, -pixPtr->Size(ixy));
1058 else pixPtr->Shift(ixy, pixPtr->Size(ixy));
1059 pixPtr->SetSize(ixy, width);
1063 } // if (pixPtr->Size(ixy)-width < -1.e-4)
1064 } // for (Int_t i=0; i<nPix;
1068 //_____________________________________________________________________________
1069 void AliMUONClusterFinderAZ::AjustPixel(Float_t wxmin, Float_t wymin)
1071 // Check if some pixels have large size (ajust if necessary)
1074 Int_t nPix = fPixArray->GetEntriesFast();
1075 AliMUONPixel *pixPtr, *pixPtr1, pix;
1077 // Check if large pixel size
1078 for (Int_t i=0; i<nPix; i++) {
1079 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1080 if (pixPtr->Charge() < 1) continue; // discarded pixel
1081 if (pixPtr->Size(0)-wxmin > 1.e-4 || pixPtr->Size(1)-wymin > 1.e-4) {
1082 AliInfo(Form(" Different %f %f %f %f",pixPtr->Size(0),wxmin,pixPtr->Size(1),wymin));
1084 nx = TMath::Nint (pix.Size(0)/wxmin);
1085 ny = TMath::Nint (pix.Size(1)/wymin);
1086 pix.Shift(0, -pix.Size(0)-wxmin);
1087 pix.Shift(1, -pix.Size(1)-wymin);
1088 pix.SetSize(0, wxmin);
1089 pix.SetSize(1, wymin);
1090 for (Int_t ii=0; ii<nx; ii++) {
1091 pix.Shift(0, wxmin*2);
1092 for (Int_t jj=0; jj<ny; jj++) {
1093 pix.Shift(1, wymin*2);
1094 pixPtr1 = new AliMUONPixel(pix);
1095 fPixArray->Add((TObject*)pixPtr1);
1098 pixPtr->SetCharge(0);
1100 } // for (Int_t i=0; i<nPix;
1104 //_____________________________________________________________________________
1105 Bool_t AliMUONClusterFinderAZ::MainLoop()
1107 // Repeat MLEM algorithm until pixel size becomes sufficiently small
1112 //Int_t nn, xList[10], yList[10];
1113 Int_t nPix = fPixArray->GetEntriesFast();
1114 Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0;
1115 AliMUONPixel *pixPtr = 0;
1116 Double_t *coef = 0, *probi = 0;
1117 for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++;
1121 mlem = (TH2D*) gROOT->FindObject("mlem");
1122 if (mlem) mlem->Delete();
1123 // Calculate coefficients
1124 AliInfo(Form(" nPix, npadTot, npadOK %d %d %d ", nPix , npadTot , npadOK ));
1126 // Calculate coefficients and pixel visibilities
1127 coef = new Double_t [npadTot*nPix];
1128 probi = new Double_t [nPix];
1129 Int_t indx = 0, cath;
1130 for (Int_t ipix=0; ipix<nPix; ipix++) {
1131 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1133 for (Int_t j=0; j<npadTot; j++) {
1134 if (fPadIJ[1][j] < 0) { coef[j*nPix+ipix] = 0; continue; }
1135 cath = fPadIJ[0][j];
1136 fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
1137 fSegmentation[cath]->SetPad(ix,iy);
1139 fSegmentation[cath]->Neighbours(ix,iy,&nn,xList,yList);
1142 for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
1147 fSegmentation[cath]->SetHit(pixPtr->Coord(0),pixPtr->Coord(1),fZpad);
1148 sum += fResponse->IntXY(fSegmentation[cath]);
1149 indx = j*nPix + ipix;
1151 probi[ipix] += coef[indx];
1152 //cout << j << " " << ipix << " " << coef[indx] << endl;
1153 } // for (Int_t j=0;
1154 //cout << " prob: " << probi[ipix] << endl;
1155 if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel
1156 } // for (Int_t ipix=0;
1161 Double_t xylim[4] = {999, 999, 999, 999};
1162 for (Int_t ipix=0; ipix<nPix; ipix++) {
1163 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1164 for (Int_t i=0; i<4; i++)
1165 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1166 //cout << ipix+1; pixPtr->Print();
1168 for (Int_t i=0; i<4; i++) {
1169 xylim[i] -= pixPtr->Size(i/2);
1170 AliInfo(Form("%f ",(i%2 ? -1 : 1)*xylim[i]));
1173 // Ajust histogram to approximately the same limits as for the pads
1174 // (for good presentation)
1178 xypads[0] = fHist[0]->GetXaxis()->GetXmin();
1179 xypads[1] = -fHist[0]->GetXaxis()->GetXmax();
1180 xypads[2] = fHist[0]->GetYaxis()->GetXmin();
1181 xypads[3] = -fHist[0]->GetYaxis()->GetXmax();
1182 for (Int_t i=0; i<4; i++) {
1184 if (xylim[i] < xypads[i]) break;
1185 xylim[i] -= 2*pixPtr->Size(i/2);
1191 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1192 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1193 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1194 for (Int_t ipix=0; ipix<nPix; ipix++) {
1195 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1196 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
1198 //gPad->GetCanvas()->cd(3);
1200 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1203 mlem->Draw("lego1Fb");
1208 // Check if the total charge of pixels is too low
1210 for (Int_t i=0; i<nPix; i++) {
1211 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1212 qTot += pixPtr->Charge();
1214 if (qTot < 1.e-4 || npadOK < 3 && qTot < 50) {
1215 delete [] coef; delete [] probi; coef = 0; probi = 0;
1216 fPixArray->Delete();
1220 // Plot data - expectation
1222 Double_t x, y, cont;
1223 for (Int_t j=0; j<npadTot; j++) {
1225 for (Int_t i=0; i<nPix; i++) {
1226 // Caculate expectation
1227 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1228 sum1 += pixPtr->Charge()*coef[j*nPix+i];
1230 sum1 = TMath::Min (sum1,(Double_t)fResponse->MaxAdc());
1233 cath = fPadIJ[0][j];
1234 Int_t ihist = cath*2;
1235 ix = fHist[ihist]->GetXaxis()->FindBin(x);
1236 iy = fHist[ihist]->GetYaxis()->FindBin(y);
1237 cont = fHist[ihist]->GetCellContent(ix,iy);
1238 if (cont == 0 && fHist[ihist+1]) {
1240 ix = fHist[ihist]->GetXaxis()->FindBin(x);
1241 iy = fHist[ihist]->GetYaxis()->FindBin(y);
1243 fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
1245 ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
1246 //gPad->SetTheta(55);
1248 //mlem->Draw("lego1");
1250 ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
1254 // Calculate position of the center-of-gravity around the maximum pixel
1256 FindCOG(mlem, xyCOG);
1258 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break;
1259 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) {
1260 // Sort pixels according to the charge
1263 for (Int_t i=0; i<nPix; i++) {
1264 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1265 cout << i+1; pixPtr->Print();
1268 Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
1269 pixMin = TMath::Min (pixMin,50.);
1271 // Decrease pixel size and shift pixels to make them centered at
1273 indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1274 Double_t width = 0, shift[2]={0};
1276 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1277 Int_t nPix1 = nPix; nPix = 0;
1278 for (Int_t ipix=0; ipix<nPix1; ipix++) {
1279 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1280 if (nPix >= npadOK) { // too many pixels already
1281 fPixArray->RemoveAt(ipix);
1285 if (pixPtr->Charge() < pixMin) { // low charge
1286 fPixArray->RemoveAt(ipix);
1290 for (Int_t i=0; i<2; i++) {
1292 pixPtr->SetCharge(10);
1293 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1294 width = -pixPtr->Size(indx);
1295 pixPtr->Shift(indx, width);
1296 // Shift pixel position
1299 for (Int_t j=0; j<2; j++) {
1300 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1301 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1303 //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1305 pixPtr->Shift(0, -shift[0]);
1306 pixPtr->Shift(1, -shift[1]);
1308 pixPtr = new AliMUONPixel(*pixPtr);
1309 pixPtr->Shift(indx, -2*width);
1310 fPixArray->Add((TObject*)pixPtr);
1313 for (Int_t i=0; i<4; i++)
1314 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1315 } // for (Int_t i=0; i<2;
1317 } // for (Int_t ipix=0;
1319 fPixArray->Compress();
1320 nPix = fPixArray->GetEntriesFast();
1322 // Remove excessive pixels
1323 if (nPix > npadOK) {
1324 for (Int_t ipix=npadOK; ipix<nPix; ipix++) {
1325 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1326 fPixArray->RemoveAt(ipix);
1330 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1331 // add pixels if the maximum is at the limit of pixel area
1332 // start from Y-direction
1334 for (Int_t i=3; i>-1; i--) {
1335 if (nPix < npadOK &&
1336 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) {
1337 pixPtr = new AliMUONPixel(*pixPtr);
1338 pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1339 j = TMath::Even (i/2);
1340 pixPtr->SetCoord(j, xyCOG[j]);
1341 fPixArray->Add((TObject*)pixPtr);
1347 fPixArray->Compress();
1348 nPix = fPixArray->GetEntriesFast();
1349 delete [] coef; delete [] probi; coef = 0; probi = 0;
1352 // remove pixels with low signal or low visibility
1353 // Cuts are empirical !!!
1354 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1355 thresh = TMath::Min (thresh,50.);
1356 Double_t cmax = -1, charge = 0;
1357 for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]);
1358 // Mark pixels which should be removed
1359 for (Int_t i=0; i<nPix; i++) {
1360 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1361 charge = pixPtr->Charge();
1362 if (charge < thresh) pixPtr->SetCharge(-charge);
1363 else if (cmax > 1.91) {
1364 if (probi[i] < 1.9) pixPtr->SetCharge(-charge);
1366 else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
1368 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1370 for (Int_t i=0; i<nPix; i++) {
1371 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1372 charge = pixPtr->Charge();
1373 if (charge > 0) continue;
1374 near = FindNearest(pixPtr);
1375 pixPtr->SetCharge(0);
1376 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near);
1377 pixPtr->SetCharge(pixPtr->Charge() - charge);
1380 for (Int_t i=0; i<nPix; i++) {
1381 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1382 ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1383 iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1384 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1387 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1390 mlem->Draw("lego1Fb");
1394 fxyMu[0][6] = fxyMu[1][6] = 9999;
1395 // Try to split into clusters
1397 if (mlem->GetSum() < 1) ok = kFALSE;
1398 else Split(mlem, coef);
1399 delete [] coef; delete [] probi; coef = 0; probi = 0;
1400 fPixArray->Delete();
1404 //_____________________________________________________________________________
1405 void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi)
1407 // Use MLEM to find pixel charges
1409 Int_t nPix = fPixArray->GetEntriesFast();
1410 Int_t npad = fnPads[0] + fnPads[1];
1411 Double_t *probi1 = new Double_t [nPix];
1413 AliMUONPixel *pixPtr;
1415 for (Int_t iter=0; iter<15; iter++) {
1417 for (Int_t ipix=0; ipix<nPix; ipix++) {
1418 // Correct each pixel
1419 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1421 probi1[ipix] = probi[ipix];
1422 for (Int_t j=0; j<npad; j++) {
1423 if (fPadIJ[1][j] < 0) continue;
1426 indx = indx1 + ipix;
1427 for (Int_t i=0; i<nPix; i++) {
1428 // Caculate expectation
1429 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1430 sum1 += pixPtr->Charge()*coef[indx1+i];
1431 } // for (Int_t i=0;
1432 if (fXyq[2][j] > fResponse->MaxAdc()-1 && sum1 > fResponse->MaxAdc()) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows
1433 //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl;
1434 if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1;
1435 } // for (Int_t j=0;
1436 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1437 if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1438 } // for (Int_t ipix=0;
1439 } // for (Int_t iter=0;
1444 //_____________________________________________________________________________
1445 void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1447 // Calculate position of the center-of-gravity around the maximum pixel
1449 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1450 Int_t i1 = -9, j1 = -9;
1451 mlem->GetMaximumBin(ixmax,iymax,ix);
1452 Int_t nx = mlem->GetNbinsX();
1453 Int_t ny = mlem->GetNbinsY();
1454 Double_t thresh = mlem->GetMaximum()/10;
1455 Double_t x, y, cont, xq=0, yq=0, qq=0;
1457 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1458 y = mlem->GetYaxis()->GetBinCenter(i);
1459 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1460 cont = mlem->GetCellContent(j,i);
1461 if (cont < thresh) continue;
1462 if (i != i1) {i1 = i; nsumy++;}
1463 if (j != j1) {j1 = j; nsumx++;}
1464 x = mlem->GetXaxis()->GetBinCenter(j);
1473 Int_t i2 = 0, j2 = 0;
1476 // one bin in Y - add one more (with the largest signal)
1477 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1478 if (i == iymax) continue;
1479 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1480 cont = mlem->GetCellContent(j,i);
1483 x = mlem->GetXaxis()->GetBinCenter(j);
1484 y = mlem->GetYaxis()->GetBinCenter(i);
1493 if (i2 != i1) nsumy++;
1494 if (j2 != j1) nsumx++;
1496 } // if (nsumy == 1)
1499 // one bin in X - add one more (with the largest signal)
1501 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1502 if (j == ixmax) continue;
1503 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1504 cont = mlem->GetCellContent(j,i);
1507 x = mlem->GetXaxis()->GetBinCenter(j);
1508 y = mlem->GetYaxis()->GetBinCenter(i);
1517 if (i2 != i1) nsumy++;
1518 if (j2 != j1) nsumx++;
1520 } // if (nsumx == 1)
1522 xyc[0] = xq/qq; xyc[1] = yq/qq;
1523 AliInfo(Form("%f %f %f %d %d %d",xyc[0],xyc[1],qq,nsum,nsumx,nsumy));
1527 //_____________________________________________________________________________
1528 Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1530 // Find the pixel nearest to the given one
1531 // (algorithm may be not very efficient)
1533 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1534 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1535 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1536 AliMUONPixel *pixPtr;
1538 for (Int_t i=0; i<nPix; i++) {
1539 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1540 if (pixPtr->Charge() < 0.5) continue;
1541 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1542 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1543 r = dx *dx + dy * dy;
1544 if (r < rmin) { rmin = r; imin = i; }
1549 //_____________________________________________________________________________
1550 void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
1552 // The main steering function to work with clusters of pixels in anode
1553 // plane (find clusters, decouple them from each other, merge them (if
1554 // necessary), pick up coupled pads, call the fitting function)
1556 Int_t nx = mlem->GetNbinsX();
1557 Int_t ny = mlem->GetNbinsY();
1558 Int_t nPix = fPixArray->GetEntriesFast();
1560 Bool_t *used = new Bool_t[ny*nx];
1562 Int_t nclust = 0, indx, indx1;
1564 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1566 TObjArray *clusters[200]={0};
1569 // Find clusters of histogram bins (easier to work in 2-D space)
1570 for (Int_t i=1; i<=ny; i++) {
1571 for (Int_t j=1; j<=nx; j++) {
1572 indx = (i-1)*nx + j - 1;
1573 if (used[indx]) continue;
1574 cont = mlem->GetCellContent(j,i);
1575 if (cont < 0.5) continue;
1576 pix = new TObjArray(20);
1578 pix->Add(BinToPix(mlem,j,i));
1579 AddBin(mlem, i, j, 0, used, pix); // recursive call
1580 clusters[nclust++] = pix;
1581 if (nclust > 200) { AliInfo(" Too many clusters "); ::exit(0); }
1582 } // for (Int_t j=1; j<=nx; j++) {
1583 } // for (Int_t i=1; i<=ny;
1584 AliInfo(Form("%d ",nclust));
1585 delete [] used; used = 0;
1587 // Compute couplings between clusters and clusters to pads
1588 Int_t npad = fnPads[0] + fnPads[1];
1590 // Exclude pads with overflows
1591 for (Int_t j=0; j<npad; j++) {
1592 if (fXyq[2][j] > fResponse->MaxAdc()-1) fPadIJ[1][j] = -9;
1593 else fPadIJ[1][j] = 0;
1596 // Compute couplings of clusters to pads
1597 TMatrixD *aijclupad = new TMatrixD(nclust,npad);
1600 for (Int_t iclust=0; iclust<nclust; iclust++) {
1601 pix = clusters[iclust];
1602 npxclu = pix->GetEntriesFast();
1603 for (Int_t i=0; i<npxclu; i++) {
1604 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
1605 for (Int_t j=0; j<npad; j++) {
1606 // Exclude overflows
1607 if (fPadIJ[1][j] < 0) continue;
1608 if (coef[j*nPix+indx] < fgkCouplMin) continue;
1609 (*aijclupad)(iclust,j) += coef[j*nPix+indx];
1613 // Compute couplings between clusters
1614 TMatrixD *aijcluclu = new TMatrixD(nclust,nclust);
1616 for (Int_t iclust=0; iclust<nclust; iclust++) {
1617 for (Int_t j=0; j<npad; j++) {
1618 // Exclude overflows
1619 if (fPadIJ[1][j] < 0) continue;
1620 if ((*aijclupad)(iclust,j) < fgkCouplMin) continue;
1621 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1622 if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue;
1623 (*aijcluclu)(iclust,iclust1) +=
1624 TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j));
1628 for (Int_t iclust=0; iclust<nclust; iclust++) {
1629 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1630 (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1);
1634 if (nclust > 1) aijcluclu->Print();
1636 // Find groups of coupled clusters
1637 used = new Bool_t[nclust];
1638 for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
1639 Int_t *clustNumb = new Int_t[nclust];
1640 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
1643 for (Int_t igroup=0; igroup<nclust; igroup++) {
1644 if (used[igroup]) continue;
1645 used[igroup] = kTRUE;
1646 clustNumb[0] = igroup;
1648 // Find group of coupled clusters
1649 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
1650 AliInfo(Form(" nCoupled: %d",nCoupled));
1651 for (Int_t i=0; i<nCoupled; i++) AliInfo(Form(" %d ",clustNumb[i]));
1653 while (nCoupled > 0) {
1657 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
1659 // Too many coupled clusters to fit - try to decouple them
1660 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
1661 // all the others in the group
1662 for (Int_t j=0; j<3; j++) minGroup[j] = -1;
1663 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
1665 // Flag clusters for fit
1667 while (minGroup[nForFit] >= 0 && nForFit < 3) {
1668 AliInfo(Form("%d ",clustNumb[minGroup[nForFit]]));
1669 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1670 clustNumb[minGroup[nForFit]] -= 999;
1673 AliInfo(Form("%d %f ",nForFit,coupl));
1676 // Select pads for fit.
1677 if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) {
1679 for (Int_t j=0; j<npad; j++) if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0;
1680 // Merge the failed cluster candidates (with too few pads to fit) with
1681 // the one with the strongest coupling
1682 Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
1685 nfit = Fit(nForFit, clustFit, clusters, parOk);
1688 // Subtract the fitted charges from pads with strong coupling and/or
1689 // return pads for further use
1690 UpdatePads(nfit, parOk);
1693 for (Int_t j=0; j<npad; j++) {if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;}
1695 // Sort the clusters (move to the right the used ones)
1696 Int_t beg = 0, end = nCoupled - 1;
1698 if (clustNumb[beg] >= 0) { beg++; continue; }
1699 for (Int_t j=end; j>beg; j--) {
1700 if (clustNumb[j] < 0) continue;
1702 indx = clustNumb[beg];
1703 clustNumb[beg] = clustNumb[j];
1704 clustNumb[j] = indx;
1710 nCoupled -= nForFit;
1712 // Remove couplings of used clusters
1713 for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) {
1714 indx = clustNumb[iclust] + 999;
1715 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1716 indx1 = clustNumb[iclust1];
1717 (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0;
1721 // Update the remaining clusters couplings (exclude couplings from
1723 for (Int_t j=0; j<npad; j++) {
1724 if (fPadIJ[1][j] != -1) continue;
1725 for (Int_t iclust=0; iclust<nCoupled; iclust++) {
1726 indx = clustNumb[iclust];
1727 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1728 for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1729 indx1 = clustNumb[iclust1];
1730 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1732 (*aijcluclu)(indx,indx1) -=
1733 TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j));
1734 (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1);
1738 } // for (Int_t j=0; j<npad;
1739 } // if (nCoupled > 3)
1740 } // while (nCoupled > 0)
1741 } // for (Int_t igroup=0; igroup<nclust;
1743 //delete aij_clu; aij_clu = 0; delete aijclupad; aijclupad = 0;
1744 aijcluclu->Delete(); aijclupad->Delete();
1745 for (Int_t iclust=0; iclust<nclust; iclust++) {
1746 pix = clusters[iclust];
1748 delete pix; pix = 0;
1750 delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1753 //_____________________________________________________________________________
1754 void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1756 // Add a bin to the cluster
1758 Int_t nx = mlem->GetNbinsX();
1759 Int_t ny = mlem->GetNbinsY();
1760 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
1761 AliMUONPixel *pixPtr = 0;
1763 for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
1764 for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
1765 if (i != ic && j != jc) continue;
1766 if (used[(i-1)*nx+j-1]) continue;
1767 cont1 = mlem->GetCellContent(j,i);
1768 if (mode && cont1 > cont) continue;
1769 used[(i-1)*nx+j-1] = kTRUE;
1770 if (cont1 < 0.5) continue;
1771 if (pix) pix->Add(BinToPix(mlem,j,i));
1773 pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j),
1774 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1775 fPixArray->Add((TObject*)pixPtr);
1777 AddBin(mlem, i, j, mode, used, pix); // recursive call
1782 //_____________________________________________________________________________
1783 TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1785 // Translate histogram bin to pixel
1787 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1788 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1790 Int_t nPix = fPixArray->GetEntriesFast();
1791 AliMUONPixel *pixPtr;
1793 // Compare pixel and bin positions
1794 for (Int_t i=0; i<nPix; i++) {
1795 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1796 if (pixPtr->Charge() < 0.5) continue;
1797 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr;
1799 AliWarning(" Something wrong ??? ");
1803 //_____________________________________________________________________________
1804 void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
1806 // Add a cluster to the group of coupled clusters
1808 for (Int_t i=0; i<nclust; i++) {
1809 if (used[i]) continue;
1810 if ((*aijcluclu)(i,ic) < fgkCouplMin) continue;
1812 clustNumb[nCoupled++] = i;
1813 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
1817 //_____________________________________________________________________________
1818 Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup)
1820 // Find group of clusters with minimum coupling to all the others
1822 Int_t i123max = TMath::Min(3,nCoupled/2);
1823 Int_t indx, indx1, indx2, indx3, nTot = 0;
1824 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1826 for (Int_t i123=1; i123<=i123max; i123++) {
1829 coupl1 = new Double_t [nCoupled];
1830 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1832 else if (i123 == 2) {
1833 nTot = nCoupled*nCoupled;
1834 coupl2 = new Double_t [nTot];
1835 for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1837 nTot = nTot*nCoupled;
1838 coupl3 = new Double_t [nTot];
1839 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1842 for (Int_t i=0; i<nCoupled; i++) {
1843 indx1 = clustNumb[i];
1844 for (Int_t j=i+1; j<nCoupled; j++) {
1845 indx2 = clustNumb[j];
1847 coupl1[i] += (*aijcluclu)(indx1,indx2);
1848 coupl1[j] += (*aijcluclu)(indx1,indx2);
1850 else if (i123 == 2) {
1851 indx = i*nCoupled + j;
1852 coupl2[indx] = coupl1[i] + coupl1[j];
1853 coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2));
1855 for (Int_t k=j+1; k<nCoupled; k++) {
1856 indx3 = clustNumb[k];
1857 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1858 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1859 coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3));
1862 } // for (Int_t j=i+1;
1863 } // for (Int_t i=0;
1864 } // for (Int_t i123=1;
1866 // Find minimum coupling
1867 Double_t couplMin = 9999;
1870 for (Int_t i123=1; i123<=i123max; i123++) {
1872 locMin = TMath::LocMin(nCoupled, coupl1);
1873 couplMin = coupl1[locMin];
1874 minGroup[0] = locMin;
1875 delete [] coupl1; coupl1 = 0;
1877 else if (i123 == 2) {
1878 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1879 if (coupl2[locMin] < couplMin) {
1880 couplMin = coupl2[locMin];
1881 minGroup[0] = locMin/nCoupled;
1882 minGroup[1] = locMin%nCoupled;
1884 delete [] coupl2; coupl2 = 0;
1886 locMin = TMath::LocMin(nTot, coupl3);
1887 if (coupl3[locMin] < couplMin) {
1888 couplMin = coupl3[locMin];
1889 minGroup[0] = locMin/nCoupled/nCoupled;
1890 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1891 minGroup[2] = locMin%nCoupled;
1893 delete [] coupl3; coupl3 = 0;
1895 } // for (Int_t i123=1;
1899 //_____________________________________________________________________________
1900 Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad)
1902 // Select pads for fit. If too many coupled clusters, find pads giving
1903 // the strongest coupling with the rest of clusters and exclude them from the fit.
1905 Int_t npad = fnPads[0] + fnPads[1];
1906 Double_t *padpix = 0;
1909 padpix = new Double_t[npad];
1910 for (Int_t i=0; i<npad; i++) padpix[i] = 0;
1913 Int_t nOK = 0, indx, indx1;
1914 for (Int_t iclust=0; iclust<nForFit; iclust++) {
1915 indx = clustFit[iclust];
1916 for (Int_t j=0; j<npad; j++) {
1917 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1918 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1919 fPadIJ[1][j] = 1; // pad to be used in fit
1922 // Check other clusters
1923 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1924 indx1 = clustNumb[iclust1];
1925 if (indx1 < 0) continue;
1926 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1927 padpix[j] += (*aijclupad)(indx1,j);
1929 } // if (nCoupled > 3)
1930 } // for (Int_t j=0; j<npad;
1931 } // for (Int_t iclust=0; iclust<nForFit
1932 if (nCoupled < 4) return nOK;
1935 for (Int_t j=0; j<npad; j++) {
1936 if (padpix[j] < fgkCouplMin) continue;
1937 AliInfo(Form("%d %f ",j , padpix[j]));
1938 AliInfo(Form("%f %f ",fXyq[0][j],fXyq[1][j]));
1940 fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1943 delete [] padpix; padpix = 0;
1947 //_____________________________________________________________________________
1948 void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad)
1950 // Merge the group of clusters with the one having the strongest coupling with them
1952 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1953 TObjArray *pix, *pix1;
1956 for (Int_t icl=0; icl<nForFit; icl++) {
1957 indx = clustFit[icl];
1958 pix = clusters[indx];
1959 npxclu = pix->GetEntriesFast();
1961 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1962 indx1 = clustNumb[icl1];
1963 if (indx1 < 0) continue;
1964 if ((*aijcluclu)(indx,indx1) > couplMax) {
1965 couplMax = (*aijcluclu)(indx,indx1);
1968 } // for (Int_t icl1=0;
1969 /*if (couplMax < fgkCouplMin) {
1970 cout << " Oops " << couplMax << endl;
1972 cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1976 pix1 = clusters[imax];
1977 npxclu1 = pix1->GetEntriesFast();
1979 for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
1980 AliInfo(Form(" New number of pixels: %d %d ",npxclu1 ,pix1->GetEntriesFast() ));
1981 //Add cluster-to-cluster couplings
1982 //aijcluclu->Print();
1983 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1984 indx1 = clustNumb[icl1];
1985 if (indx1 < 0 || indx1 == imax) continue;
1986 (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1);
1987 (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax);
1989 (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0;
1990 //aijcluclu->Print();
1991 //Add cluster-to-pad couplings
1992 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1993 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1994 (*aijclupad)(imax,j) += (*aijclupad)(indx,j);
1995 (*aijclupad)(indx,j) = 0;
1997 } // for (Int_t icl=0; icl<nForFit;
2000 //_____________________________________________________________________________
2001 Int_t AliMUONClusterFinderAZ::Fit(Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
2003 // Find selected clusters to selected pad charges
2005 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
2006 //Int_t nx = mlem->GetNbinsX();
2007 //Int_t ny = mlem->GetNbinsY();
2008 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
2009 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
2010 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
2011 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
2012 //Double_t qmin = 0, qmax = 1;
2013 Double_t step[3]={0.01,0.002,0.02};
2015 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8];
2019 // Number of pads to use
2021 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {if (fPadIJ[1][i] == 1) npads++;}
2022 for (Int_t i=0; i<nfit; i++) {AliInfo(Form("%d %d ",i+1 ,clustFit[i]));}
2023 AliInfo(Form("%d ",nfit));
2024 AliInfo(Form(" Number of pads to fit: %d ",npads));
2027 if (npads < 2) return 0;
2029 // Take cluster maxima as fitting seeds
2030 AliMUONPixel *pixPtr;
2031 Double_t xyseed[3][2], qseed[3];
2032 for (Int_t ifit=1; ifit<=nfit; ifit++) {
2034 pix = clusters[clustFit[ifit-1]];
2035 npxclu = pix->GetEntriesFast();
2036 for (Int_t clu=0; clu<npxclu; clu++) {
2037 pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu);
2038 cont = pixPtr->Charge();
2042 xseed = pixPtr->Coord(0);
2043 yseed = pixPtr->Coord(1);
2046 xyseed[ifit-1][0] = xseed;
2047 xyseed[ifit-1][1] = yseed;
2048 qseed[ifit-1] = cmax;
2049 } // for (Int_t ifit=1;
2051 Int_t nDof, maxSeed[3];
2052 Double_t fmin, chi2o = 9999, chi2n;
2054 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
2055 // lower, try 3-track (if number of pads is sufficient).
2057 TMath::Sort(nfit, qseed, maxSeed, kTRUE); // in decreasing order
2058 nfit = TMath::Min (nfit, (npads + 1) / 3);
2060 Double_t *gin = 0, func0, func1, param[8], param0[2][8], deriv[2][8], step0[8];
2061 Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
2062 Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
2063 Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
2065 for (Int_t iseed=0; iseed<nfit; iseed++) {
2067 for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j];
2068 param[fNpar] = xyseed[maxSeed[iseed]][0];
2069 parmin[fNpar] = xmin;
2070 parmax[fNpar++] = xmax;
2071 param[fNpar] = xyseed[maxSeed[iseed]][1];
2072 parmin[fNpar] = ymin;
2073 parmax[fNpar++] = ymax;
2075 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
2077 parmax[fNpar++] = 1;
2080 // Try new algorithm
2081 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
2085 Fcn1(fNpar, gin, func0, param, 1); nCall++;
2086 //cout << " Func: " << func0 << endl;
2089 for (Int_t j=0; j<fNpar; j++) {
2090 param0[max][j] = param[j];
2091 delta[j] = step0[j];
2092 param[j] += delta[j] / 10;
2093 if (j > 0) param[j-1] -= delta[j-1] / 10;
2094 Fcn1(fNpar, gin, func1, param, 1); nCall++;
2095 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
2096 //cout << j << " " << deriv[max][j] << endl;
2097 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
2098 (param0[0][j] - param0[1][j]) : 0; // second derivative
2100 param[fNpar-1] -= delta[fNpar-1] / 10;
2101 if (nCall > 2000) ::exit(0);
2103 min = func2[0] < func2[1] ? 0 : 1;
2104 nFail = min == max ? 0 : nFail + 1;
2106 stepMax = derMax = estim = 0;
2107 for (Int_t j=0; j<fNpar; j++) {
2108 // Estimated distance to minimum
2110 if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
2111 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0;
2112 else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
2113 || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) {
2114 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
2116 if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2120 shift[j] = -deriv[min][j] / dder[j];
2123 if (TMath::Abs(shift[j])/step0[j] > estim) {
2124 estim = TMath::Abs(shift[j])/step0[j];
2129 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
2131 // Failed to improve minimum
2134 param[j] = param0[min][j];
2135 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2;
2136 else shift[j] /= -2;
2140 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
2141 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
2143 // Introduce step relaxation factor
2144 if (memory[j] < 3) {
2145 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
2146 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
2147 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
2149 param[j] += shift[j];
2151 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
2152 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
2153 if (TMath::Abs(deriv[min][j]) > derMax) {
2155 derMax = TMath::Abs (deriv[min][j]);
2157 } // for (Int_t j=0; j<fNpar;
2158 //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl;
2159 if (estim < 1 && derMax < 2 || nLoop > 100) break; // minimum was found
2162 // Check for small step
2163 if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; }
2164 if (!memory[idMax] && derMax > 0.5 && nLoop > 10) {
2165 //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl;
2166 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) {
2167 if (min == max) dder[idMax] = -dder[idMax];
2168 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
2169 param[idMax] += shift[idMax];
2170 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
2171 //cout << shift[idMax] << " " << param[idMax] << endl;
2172 if (min == max) shiftSave = shift[idMax];
2175 param[idMax] -= shift[idMax];
2176 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
2177 param[idMax] += shift[idMax];
2178 //cout << shift[idMax] << endl;
2184 nDof = npads - fNpar;
2185 chi2n = nDof ? fmin/nDof : 0;
2187 if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
2188 // Save parameters and errors
2189 for (Int_t i=0; i<fNpar; i++) {
2190 parOk[i] = param0[min][i];
2194 AliInfo(Form("%f %f ",chi2o ,chi2n));
2196 if (fmin < 0.1) break; // !!!???
2197 } // for (Int_t iseed=0;
2199 for (Int_t i=0; i<fNpar; i++) {
2200 if (i == 4 || i == 7) continue;
2201 AliInfo(Form("%f %f ",parOk[i],errOk[i]));
2203 nfit = (fNpar + 1) / 3;
2207 for (Int_t j=0; j<nfit; j++) {
2208 indx = j<2 ? j*2 : j*2+1;
2209 AddRawCluster (parOk[indx], parOk[indx+1], errOk[indx]);
2213 for (Int_t i=0; i<fnMu; i++) {
2215 for (Int_t j=0; j<nfit; j++) {
2216 indx = j<2 ? j*2 : j*2+1;
2217 rad = (fxyMu[i][0]-parOk[indx])*(fxyMu[i][0]-parOk[indx]) +
2218 (fxyMu[i][1]-parOk[indx+1])*(fxyMu[i][1]-parOk[indx+1]);
2223 fxyMu[i][2] = parOk[imax] - fxyMu[i][0];
2224 fxyMu[i][4] = parOk[imax+1] - fxyMu[i][1];
2225 fxyMu[i][3] = errOk[imax];
2226 fxyMu[i][5] = errOk[imax+1];
2233 //_____________________________________________________________________________
2234 void AliMUONClusterFinderAZ::Fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2236 // Fit for one track
2237 AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);
2239 Int_t cath, ix, iy, indx, npads=0;
2240 Double_t charge, delta, coef=0, chi2=0;
2241 for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) {
2242 if (c.fPadIJ[1][j] != 1) continue;
2243 cath = c.fPadIJ[0][j];
2245 c.fSegmentation[cath]->GetPadI(c.fXyq[0][j],c.fXyq[1][j],c.fZpad,ix,iy);
2246 c.fSegmentation[cath]->SetPad(ix,iy);
2248 for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2249 indx = i<2 ? 2*i : 2*i+1;
2250 c.fSegmentation[cath]->SetHit(par[indx],par[indx+1],c.fZpad);
2251 //charge += c.fResponse->IntXY(c.fSegmentation[cath])*par[icl*3+2];
2252 if (c.fNpar == 2) coef = 1;
2253 else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef;
2254 //coef = TMath::Max (coef, 0.);
2255 if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2256 //coef = TMath::Max (coef, 0.);
2257 charge += c.fResponse->IntXY(c.fSegmentation[cath])*coef;
2260 //if (c.fXyq[2][j] > c.fResponse->MaxAdc()-1 && charge >
2261 // c.fResponse->MaxAdc()) charge = c.fResponse->MaxAdc();
2262 delta = charge - c.fXyq[2][j];
2263 delta /= TMath::Sqrt ((Double_t)c.fXyq[2][j]);
2264 //chi2 += TMath::Abs(delta);
2265 chi2 += delta*delta;
2266 } // for (Int_t j=0;
2268 Double_t qAver = c.fQtot/npads; //(c.fnPads[0]+c.fnPads[1]);
2272 //_____________________________________________________________________________
2273 void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par)
2275 // Subtract the fitted charges from pads with strong coupling
2277 Int_t cath, ix, iy, indx;
2278 Double_t charge, coef=0;
2279 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2280 if (fPadIJ[1][j] != -1) continue;
2282 cath = fPadIJ[0][j];
2283 fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
2284 fSegmentation[cath]->SetPad(ix,iy);
2286 for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2287 indx = i<2 ? 2*i : 2*i+1;
2288 fSegmentation[cath]->SetHit(par[indx],par[indx+1],fZpad);
2289 if (fNpar == 2) coef = 1;
2290 else coef = i==fNpar/3 ? par[indx+2] : 1-coef;
2291 if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2292 charge += fResponse->IntXY(fSegmentation[cath])*coef;
2295 fXyq[2][j] -= charge;
2296 } // if (fNpar != 0)
2297 if (fXyq[2][j] > fResponse->ZeroSuppression()) fPadIJ[1][j] = 0; // return pad for further using
2298 } // for (Int_t j=0;
2301 //_____________________________________________________________________________
2302 Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) const {
2303 // Test if track was user selected
2306 if (fTrack[0]==-1 || fTrack[1]==-1) {
2308 } else if (t==fTrack[0] || t==fTrack[1]) {
2316 //_____________________________________________________________________________
2317 void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t fmin)
2320 // Add a raw cluster copy to the list
2322 AliMUONRawCluster cnew;
2323 AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2324 //pMUON->AddRawCluster(fInput->Chamber(),c);
2327 for (cath=0; cath<2; cath++) {
2330 cnew.SetZ(cath, fZpad);
2331 cnew.SetCharge(cath, 100);
2332 cnew.SetPeakSignal(cath,20);
2333 cnew.SetMultiplicity(cath, 5);
2334 cnew.SetNcluster(cath, 1);
2335 cnew.SetChi2(cath, fmin); //0.1;
2337 cnew.fMultiplicity[cath]=c->fMultiplicity[cath];
2338 for (i=0; i<fMul[cath]; i++) {
2339 cnew.fIndexMap[i][cath]=c->fIndexMap[i][cath];
2340 fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
2342 fprintf(stderr,"\nRawCluster %d cath %d\n",ico,cath);
2343 fprintf(stderr,"mult_av %d\n",c->fMultiplicity[cath]);
2344 FillCluster(&cnew,cath);
2347 //cnew.fClusterType=cnew.PhysicsContribution();
2348 pMUON->GetMUONData()->AddRawCluster(AliMUONClusterInput::Instance()->Chamber(),cnew);
2352 //_____________________________________________________________________________
2353 Int_t AliMUONClusterFinderAZ::FindLocalMaxima(Int_t *localMax, Double_t *maxVal)
2355 // Find local maxima in pixel space for large preclusters in order to
2356 // try to split them into smaller pieces (to speed up the MLEM procedure)
2358 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2359 if (hist) hist->Delete();
2361 Double_t xylim[4] = {999, 999, 999, 999};
2362 Int_t nPix = fPixArray->GetEntriesFast();
2363 AliMUONPixel *pixPtr = 0;
2364 for (Int_t ipix=0; ipix<nPix; ipix++) {
2365 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2366 for (Int_t i=0; i<4; i++)
2367 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
2369 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
2371 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
2372 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
2373 hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2374 for (Int_t ipix=0; ipix<nPix; ipix++) {
2375 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2376 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
2379 ((TCanvas*)gROOT->FindObject("c2"))->cd();
2382 hist->Draw("lego1Fb");
2388 Int_t nMax = 0, indx;
2389 Int_t *isLocalMax = new Int_t[ny*nx];
2390 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
2392 for (Int_t i=1; i<=ny; i++) {
2394 for (Int_t j=1; j<=nx; j++) {
2395 if (hist->GetCellContent(j,i) < 0.5) continue;
2396 //if (isLocalMax[indx+j-1] < 0) continue;
2397 if (isLocalMax[indx+j-1] != 0) continue;
2398 FlagLocalMax(hist, i, j, isLocalMax);
2402 for (Int_t i=1; i<=ny; i++) {
2404 for (Int_t j=1; j<=nx; j++) {
2405 if (isLocalMax[indx+j-1] > 0) {
2406 localMax[nMax] = indx + j - 1;
2407 maxVal[nMax++] = hist->GetCellContent(j,i);
2409 if (nMax > 99) { AliWarning(" Too many local maxima !!!" ); ::exit(0); }
2412 AliInfo(Form(" Local max: %d",nMax));
2413 delete [] isLocalMax; isLocalMax = 0;
2417 //_____________________________________________________________________________
2418 void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2420 // Flag pixels (whether or not local maxima)
2422 Int_t nx = hist->GetNbinsX();
2423 Int_t ny = hist->GetNbinsY();
2424 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
2427 for (Int_t i1=i-1; i1<i+2; i1++) {
2428 if (i1 < 1 || i1 > ny) continue;
2429 for (Int_t j1=j-1; j1<j+2; j1++) {
2430 if (j1 < 1 || j1 > nx) continue;
2431 if (i == i1 && j == j1) continue;
2432 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
2433 if (cont < cont1) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2434 else if (cont > cont1) isLocalMax[(i1-1)*nx+j1-1] = -1;
2435 else { // the same charge
2436 isLocalMax[(i-1)*nx+j-1] = 1;
2437 if (isLocalMax[(i1-1)*nx+j1-1] == 0) {
2438 FlagLocalMax(hist, i1, j1, isLocalMax);
2439 if (isLocalMax[(i1-1)*nx+j1-1] < 0) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2440 else isLocalMax[(i1-1)*nx+j1-1] = -1;
2445 isLocalMax[(i-1)*nx+j-1] = 1; // local maximum
2448 //_____________________________________________________________________________
2449 void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2451 // Find pixel cluster around local maximum #iMax and pick up pads
2452 // overlapping with it
2454 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2455 Int_t nx = hist->GetNbinsX();
2456 Int_t ny = hist->GetNbinsY();
2457 Int_t ic = localMax[iMax] / nx + 1;
2458 Int_t jc = localMax[iMax] % nx + 1;
2459 Bool_t *used = new Bool_t[ny*nx];
2460 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
2462 // Drop all pixels from the array - pick up only the ones from the cluster
2463 fPixArray->Delete();
2465 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
2466 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
2467 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
2468 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
2469 Double_t cont = hist->GetCellContent(jc,ic);
2470 AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont);
2471 fPixArray->Add((TObject*)pixPtr);
2472 used[(ic-1)*nx+jc-1] = kTRUE;
2473 AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
2475 Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1];
2476 for (Int_t i=0; i<nPix; i++) {
2477 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx);
2478 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy);
2480 AliInfo(Form("%d %d ",iMax,nPix));
2482 Float_t xy[4], xy12[4];
2483 // Pick up pads which overlap with found pixels
2484 for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1;
2485 for (Int_t i=0; i<nPix; i++) {
2486 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
2487 for (Int_t j=0; j<4; j++)
2488 xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2);
2489 for (Int_t j=0; j<npad; j++)
2490 if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use
2493 delete [] used; used = 0;
2496 //_____________________________________________________________________________
2497 AliMUONClusterFinderAZ&
2498 AliMUONClusterFinderAZ::operator=(const AliMUONClusterFinderAZ& rhs)
2500 // Protected assignement operator
2502 if (this == &rhs) return *this;
2504 AliFatal("Not implemented.");