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 fSeg2[0] = iChamber->SegmentationModel2(1);
182 fSeg2[1] = iChamber->SegmentationModel2(2);
184 fResponse = iChamber->ResponseModel();
189 nent = (Int_t) treeD->GetEntries();
190 //printf(" entries %d \n", nent);
193 Int_t ndigits[2]={9,9}, nShown[2]={0};
194 for (Int_t i=0; i<2; i++) {
195 for (Int_t j=0; j<fgkDim; j++) {fUsed[i][j]=kFALSE;}
199 if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) {
203 goto newchamber; // next chamber
205 Float_t xpad, ypad, zpad, zpad0;
208 Bool_t first = kTRUE;
209 AliInfo(Form(" *** Event # %d chamber: %d " , nev ,ch ));
210 fnPads[0] = fnPads[1] = 0;
211 for (Int_t i=0; i<fgkDim; i++) {fPadIJ[1][i] = 0;}
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();
216 treeD->GetEvent(cath);
217 fMuonDigits = muon->GetMUONData()->Digits(ch);
219 ndigits[cath] = fMuonDigits->GetEntriesFast();
220 if (!ndigits[0] && !ndigits[1]) {if (fReco) return; ch++; goto newchamber;}
221 if (ndigits[cath] == 0) continue;
222 AliInfo(Form(" ndigits: %d %d " , ndigits[cath] , cath));
227 Bool_t eEOC = kTRUE; // end-of-cluster
228 for (digit = 0; digit < ndigits[cath]; digit++) {
229 mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
230 if (mdig->Cathode() != cath) continue;
232 // Find first unused pad
233 if (fUsed[cath][digit]) continue;
234 fSeg2[cath]->GetPadC(fInput->DetElemId(), mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0);
237 if (fUsed[cath][digit]) continue;
238 fSeg2[cath]->GetPadC(fInput->DetElemId(), mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
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;
244 // Add pad - recursive call
247 if (digit >= 0) break;
250 // No more unused pads
251 if (cath == 0) continue; // on cathode #0 - check #1
256 goto newchamber; // next chamber
259 if (eEOC) break; // cluster found
261 AliInfo(Form(" nPads: %d %d %d ",fnPads[cath] ,nShown[cath]+fnPads[cath],cath));
262 } // for (Int_t iii = 0;
265 if (fReco) goto skip;
267 for (Int_t cath = 0; cath<2; cath++) {
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;}
281 AliInfo(Form("%d %d %d %d", minDx , maxDx , minDy , maxDy ));
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
288 AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
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];
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;
309 // different segmentation in the cluster
310 AliInfo(" Different\n");
311 AliInfo(Form("%f %f %f %f ",fXyq[3][minDx],fXyq[3][maxDx],fXyq[4][minDy],fXyq[4][maxDy]));
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
320 // different segmentation along y
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;
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]);
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]);
352 if (nOK != fnPads[cath])
353 AliInfo(Form(" *** Too many segmentations: nPads, nOK %d %d",fnPads[cath],nOK));
354 } // if (TMath::Nint(fXyq[3][minDx]*1000)
355 } // for (Int_t cath = 0;
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
365 Double_t x, y, x0, y0, r1=999, r2=0;
366 if (fHist[cath*2+1]) {
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);
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)));
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)));
395 AliInfo(Form("%f %f \n",r1,r2));
396 } // if (fHist[cath*2+1])
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");
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");
410 } // for (Int_t cath = 0;
412 // Draw generated hits
414 hist = fHist[0] ? fHist[0] : fHist[2];
415 p2[2] = hist->GetMaximum();
417 if (c1) view = c1->Pad()->GetView();
418 AliInfo(" *** GEANT hits *** ");
421 for (Int_t i=0; i<ntracks; i++) {
423 for (AliMUONHit* mHit=(AliMUONHit*)muon->FirstHit(-1);
425 mHit=(AliMUONHit*)muon->NextHit()) {
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
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;}
443 gStyle->SetLineColor(1);
444 if (TMath::Abs((Int_t)mHit->Particle()) == 13) {
445 gStyle->SetLineColor(4);
448 fxyMu[fnMu-1][0] = p1[0];
449 fxyMu[fnMu-1][1] = p1[1];
452 AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mHit->Z()));
454 view->WCtoNDC(p1, &xNDC[0]);
455 view->WCtoNDC(p2, &xNDC[3]);
456 for (Int_t ipad=1; ipad<3; 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();
463 } // for (AliMUONHit* mHit=
464 } // for (Int_t i=0; i<ntracks;
466 // Draw reconstructed coordinates
467 listMUONrawclust = muon->GetMUONData()->RawClusters(ch);
469 //cout << listMUONrawclust << " " << listMUONrawclust ->GetEntries() << endl;
470 AliMUONRawCluster *mRaw;
471 gStyle->SetLineColor(3);
472 AliInfo(" *** Reconstructed hits *** ");
473 for (Int_t i=0; i<listMUONrawclust ->GetEntries(); i++) {
474 mRaw = (AliMUONRawCluster*)listMUONrawclust ->UncheckedAt(i);
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
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;
483 treeD->GetEvent(cath);
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;
490 // Check if track comes thru pads with signal
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;}
499 AliInfo(Form(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mRaw->GetZ(0)));
501 view->WCtoNDC(p1, &xNDC[0]);
502 view->WCtoNDC(p2, &xNDC[3]);
503 for (Int_t ipad=1; ipad<3; ipad++) {
505 line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
506 line[nLine++]->Draw();
509 } // for (Int_t i=0; i<listMUONrawclust ->GetEntries();
510 if (fDraw) c1->Update();
513 // Use MLEM for cluster finder
515 Int_t nMax = 1, localMax[100], maxPos[100];
516 Double_t maxVal[100];
518 if (CheckPrecluster(nShown)) {
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]);
524 if (!MainLoop()) AliInfo(" MainLoop failed ");
526 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
527 if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
529 fXyq[2][j] = fXyq[5][j]; // use backup charge value
534 if (fReco) goto next;
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]) {
541 AliInfo(Form("%12.3e %12.3e %12.3e %12.3e\n",fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]));
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]);
546 } // for (Int_t i=0; i<fnMu;
550 AliInfo(" What is next? ");
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') {
558 sscanf(command+1,"%d",&ch);
561 else if (command[0] == 'e' || command[0] == 'E') {
563 sscanf(command+1,"%d",&nev);
566 else goto next; // Next cluster
569 //_____________________________________________________________________________
570 void AliMUONClusterFinderAZ::ModifyHistos(void)
572 // Modify histograms to bring them to the same size
574 Float_t hlim[4][4], hbin[4][4]; // first index - xmin, xmax, ymin, ymax
575 Float_t binMin[4] = {999,999,999,999};
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]);
589 binMin[1] = binMin[0];
590 binMin[3] = binMin[2];
591 AliInfo(Form(" Nhist: %d",nhist));
594 for (Int_t lim=0; lim<4; lim++) {
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) {
601 hlim[lim][imax] -= hbin[lim][imax];
604 hlim[lim][imin] += hbin[lim][imin];
609 // Rebuild histograms
613 Double_t x, y, cont, cmax=0;
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);
630 cmax = TMath::Max (cmax,hist->GetMaximum());
631 fHist[ihist]->Delete();
632 fHist[ihist] = new TH2F(*hist);
636 AliInfo(Form("%f \n",cmax));
638 for (Int_t ihist=0; ihist<4; ihist++) {
639 if (!fHist[ihist]) continue;
640 fHist[ihist]->SetMaximum(cmax);
644 //_____________________________________________________________________________
645 void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
647 // Add pad to the cluster
648 AliMUONDigit *mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
650 Int_t charge = mdig->Signal();
651 // get the center of the pad
652 Float_t xpad, ypad, zpad;
653 fSeg2[cath]->GetPadC(fInput->DetElemId(), mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
656 isec = fSeg2[cath]->Sector(fInput->DetElemId(), mdig->PadX(), mdig->PadY());
658 Int_t nPads = fnPads[0] + fnPads[1];
659 fXyq[0][nPads] = xpad;
660 fXyq[1][nPads] = ypad;
661 fXyq[2][nPads] = charge;
663 fXyq[3][nPads] = fSeg2[cath]->Dpx(fInput->DetElemId(),isec)/2;
664 fXyq[4][nPads] = fSeg2[cath]->Dpy(fInput->DetElemId(),isec)/2;
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;
674 Int_t nn, ix, iy, xList[10], yList[10];
677 Int_t ndigits = fMuonDigits->GetEntriesFast();
678 fSeg2[cath]->Neighbours(fInput->DetElemId(), mdig->PadX(),mdig->PadY(),&nn,xList,yList);
680 for (Int_t in=0; in<nn; 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
692 } //for (Int_t digit1 = 0;
693 } // for (Int_t in=0;
696 //_____________________________________________________________________________
697 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, TObject *dig)
699 // Check if the pad from one cathode overlaps with a pad
700 // in the precluster on the other cathode
702 AliMUONDigit *mdig = (AliMUONDigit*) dig;
704 Float_t xpad, ypad, zpad;
706 Float_t xy1[4], xy12[4];
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);
716 //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
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;
726 //_____________________________________________________________________________
727 Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
729 // Check if the pads xy1 and iPad overlap and return overlap area
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]);
746 //_____________________________________________________________________________
748 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t i, Int_t j, Float_t *xy12, Int_t iSkip)
750 // Check if the pads i and j overlap and return overlap area
752 Float_t xy1[4], xy2[4];
753 return Overlap(xy1, xy2, xy12, iSkip);
756 //_____________________________________________________________________________
757 Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
759 // Check precluster in order to attempt to simplify it (mostly for
760 // two-cathode preclusters)
763 Float_t xy1[4], xy12[4];
765 Int_t npad = fnPads[0] + fnPads[1];
767 // If pads have the same size take average of pads on both cathodes
768 Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
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; }
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;
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];
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];
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;
802 // Check if one-cathode precluster
803 i1 = fnPads[0]!=0 ? 0 : 1;
804 i2 = fnPads[1]!=0 ? 1 : 0;
806 if (i1 != i2) { // two-cathode
808 Int_t *flags = new Int_t[npad];
809 for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
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
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];}
828 if (nFlags) AliInfo(Form(" nFlags = %d",nFlags));
829 //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
831 for (Int_t i=0; i<npad; i++) {
832 if (flags[i]) continue;
833 digit = TMath::Nint (fXyq[5][i]);
835 fUsed[cath][digit] = kFALSE; // release pad
841 // Check correlations of cathode charges
842 if (fnPads[0] && fnPads[1]) { // two-cathode
844 Int_t over[2] = {1, 1};
845 for (Int_t i=0; i<npad; i++) {
847 if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
848 if (fXyq[2][i] > fResponse->MaxAdc()-1) over[cath] = 0;
850 AliInfo(Form(" Total charge: %f %f",sum[0],sum[1]));
851 if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
852 AliInfo(" Release ");
854 cath = sum[0]>sum[1] ? 0 : 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;
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++) {
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]);
877 TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
880 for (Int_t i=0; i<npad; 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) {
886 if (TMath::Abs(dist[indx]-xmax)<1.e-3)
887 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
888 else cmax = fXyq[2][indx];
890 digit = TMath::Nint (fXyq[5][indx]);
891 fUsed[cath][digit] = kFALSE;
894 // xmax = dist[i]; // Bug?
898 delete [] dist; dist = 0;
899 } // TMath::Abs(sum[0]-sum[1])...
900 } // if (fnPads[0] && fnPads[1])
901 delete [] flags; flags = 0;
904 if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
906 // Move released pads to the right
907 Int_t beg = 0, end = npad-1, padij;
910 if (fXyq[2][beg] > 0) { beg++; continue; }
911 for (Int_t j=end; j>beg; j--) {
912 if (fXyq[2][j] < 0) continue;
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;
919 for (Int_t j1=0; j1<6; j1++) {
921 fXyq[j1][beg] = fXyq[j1][j];
925 } // for (Int_t j=end;
928 npad = fnPads[0] + fnPads[1];
929 if (npad > 500) { AliInfo(Form(" ***** Too large cluster. Give up. ",npad )); return kFALSE; }
930 // Back up charge value
931 for (Int_t j=0; j<npad; j++) fXyq[5][j] = fXyq[2][j];
936 //_____________________________________________________________________________
937 void AliMUONClusterFinderAZ::BuildPixArray()
939 // Build pixel array for MLEM method
941 Int_t nPix=0, i1, i2;
942 Float_t xy1[4], xy12[4];
943 AliMUONPixel *pixPtr=0;
945 Int_t npad = fnPads[0] + fnPads[1];
947 // One cathode is empty
948 i1 = fnPads[0]!=0 ? 0 : 1;
949 i2 = fnPads[1]!=0 ? 1 : 0;
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
959 pixPtr->SetCharge(fXyq[2][j]); // charge
960 fPixArray->Add((TObject*)pixPtr);
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
978 pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
979 fPixArray->Add((TObject*)pixPtr);
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]);
990 AliInfo(Form("%f %f ",wxmin,wymin));
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);
999 // Remove discarded pixels
1000 for (Int_t i=0; i<nPix; i++) {
1001 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1003 if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
1005 fPixArray->Compress();
1006 nPix = fPixArray->GetEntriesFast();
1009 AliInfo(Form("nPix %d ",nPix));
1010 // Too many pixels - sort and remove pixels with the lowest signal
1012 for (Int_t i=npad; i<nPix; i++) {
1013 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1015 fPixArray->RemoveAt(i);
1019 } // if (nPix > npad)
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);
1025 AliInfo(Form("%d %f %f %f %f",i+1,pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Size(0),pixPtr->Size(1)));
1029 //_____________________________________________________________________________
1030 void AliMUONClusterFinderAZ::AjustPixel(Float_t width, Int_t ixy)
1032 // Check if some pixels have small size (ajust if necessary)
1034 AliMUONPixel *pixPtr, *pixPtr1 = 0;
1035 Int_t ixy1 = TMath::Even(ixy);
1036 Int_t nPix = fPixArray->GetEntriesFast();
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) {
1043 AliInfo(Form(" Small X or Y: %d %f %f %f %f",ixy,pixPtr->Size(ixy),width,pixPtr->Coord(0),pixPtr->Coord(1)));
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) {
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);
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
1063 AliInfo(" Edge ...");
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);
1074 } // if (pixPtr->Size(ixy)-width < -1.e-4)
1075 } // for (Int_t i=0; i<nPix;
1079 //_____________________________________________________________________________
1080 void AliMUONClusterFinderAZ::AjustPixel(Float_t wxmin, Float_t wymin)
1082 // Check if some pixels have large size (ajust if necessary)
1085 Int_t nPix = fPixArray->GetEntriesFast();
1086 AliMUONPixel *pixPtr, *pixPtr1, pix;
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) {
1093 AliInfo(Form(" Different %f %f %f %f",pixPtr->Size(0),wxmin,pixPtr->Size(1),wymin));
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);
1109 pixPtr->SetCharge(0);
1111 } // for (Int_t i=0; i<nPix;
1115 //_____________________________________________________________________________
1116 Bool_t AliMUONClusterFinderAZ::MainLoop()
1118 // Repeat MLEM algorithm until pixel size becomes sufficiently small
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++;
1132 mlem = (TH2D*) gROOT->FindObject("mlem");
1133 if (mlem) mlem->Delete();
1134 // Calculate coefficients
1135 AliInfo(Form(" nPix, npadTot, npadOK %d %d %d ", nPix , npadTot , npadOK ));
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);
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];
1149 fSeg2[cath]->GetPadI(fInput->DetElemId(),fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
1150 fSeg2[cath]->SetPad(fInput->DetElemId(),ix,iy);
1152 fSeg2[cath]->Neighbours(fInput->DetElemId(),ix,iy,&nn,xList,yList);
1155 for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
1159 fSeg2[cath]->SetHit(fInput->DetElemId(),pixPtr->Coord(0),pixPtr->Coord(1),fZpad);
1160 sum += fResponse->IntXY(fInput->DetElemId(),fSeg2[cath]);
1162 indx = j*nPix + ipix;
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;
1174 Double_t xylim[4] = {999, 999, 999, 999};
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();
1181 for (Int_t i=0; i<4; i++) {
1182 xylim[i] -= pixPtr->Size(i/2);
1183 AliInfo(Form("%f ",(i%2 ? -1 : 1)*xylim[i]));
1186 // Ajust histogram to approximately the same limits as for the pads
1187 // (for good presentation)
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++) {
1197 if (xylim[i] < xypads[i]) break;
1198 xylim[i] -= 2*pixPtr->Size(i/2);
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());
1211 //gPad->GetCanvas()->cd(3);
1213 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1216 mlem->Draw("lego1Fb");
1221 // Check if the total charge of pixels is too low
1223 for (Int_t i=0; i<nPix; i++) {
1224 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1225 qTot += pixPtr->Charge();
1227 if (qTot < 1.e-4 || npadOK < 3 && qTot < 50) {
1228 delete [] coef; delete [] probi; coef = 0; probi = 0;
1229 fPixArray->Delete();
1233 // Plot data - expectation
1235 Double_t x, y, cont;
1236 for (Int_t j=0; j<npadTot; j++) {
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];
1243 sum1 = TMath::Min (sum1,(Double_t)fResponse->MaxAdc());
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]) {
1253 ix = fHist[ihist]->GetXaxis()->FindBin(x);
1254 iy = fHist[ihist]->GetYaxis()->FindBin(y);
1256 fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
1258 ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
1259 //gPad->SetTheta(55);
1261 //mlem->Draw("lego1");
1263 ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
1267 // Calculate position of the center-of-gravity around the maximum pixel
1269 FindCOG(mlem, xyCOG);
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
1276 for (Int_t i=0; i<nPix; i++) {
1277 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1278 cout << i+1; pixPtr->Print();
1281 Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
1282 pixMin = TMath::Min (pixMin,50.);
1284 // Decrease pixel size and shift pixels to make them centered at
1286 indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1287 Double_t width = 0, shift[2]={0};
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);
1298 if (pixPtr->Charge() < pixMin) { // low charge
1299 fPixArray->RemoveAt(ipix);
1303 for (Int_t i=0; i<2; 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
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;
1316 //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1318 pixPtr->Shift(0, -shift[0]);
1319 pixPtr->Shift(1, -shift[1]);
1321 pixPtr = new AliMUONPixel(*pixPtr);
1322 pixPtr->Shift(indx, -2*width);
1323 fPixArray->Add((TObject*)pixPtr);
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;
1330 } // for (Int_t ipix=0;
1332 fPixArray->Compress();
1333 nPix = fPixArray->GetEntriesFast();
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);
1343 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1344 // add pixels if the maximum is at the limit of pixel area
1345 // start from Y-direction
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);
1360 fPixArray->Compress();
1361 nPix = fPixArray->GetEntriesFast();
1362 delete [] coef; delete [] probi; coef = 0; probi = 0;
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);
1379 else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
1381 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
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);
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());
1400 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1403 mlem->Draw("lego1Fb");
1407 fxyMu[0][6] = fxyMu[1][6] = 9999;
1408 // Try to split into clusters
1410 if (mlem->GetSum() < 1) ok = kFALSE;
1411 else Split(mlem, coef);
1412 delete [] coef; delete [] probi; coef = 0; probi = 0;
1413 fPixArray->Delete();
1417 //_____________________________________________________________________________
1418 void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi)
1420 // Use MLEM to find pixel charges
1422 Int_t nPix = fPixArray->GetEntriesFast();
1423 Int_t npad = fnPads[0] + fnPads[1];
1424 Double_t *probi1 = new Double_t [nPix];
1426 AliMUONPixel *pixPtr;
1428 for (Int_t iter=0; iter<15; iter++) {
1430 for (Int_t ipix=0; ipix<nPix; ipix++) {
1431 // Correct each pixel
1432 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1434 probi1[ipix] = probi[ipix];
1435 for (Int_t j=0; j<npad; j++) {
1436 if (fPadIJ[1][j] < 0) continue;
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;
1457 //_____________________________________________________________________________
1458 void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1460 // Calculate position of the center-of-gravity around the maximum pixel
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;
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);
1486 Int_t i2 = 0, j2 = 0;
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);
1496 x = mlem->GetXaxis()->GetBinCenter(j);
1497 y = mlem->GetYaxis()->GetBinCenter(i);
1506 if (i2 != i1) nsumy++;
1507 if (j2 != j1) nsumx++;
1509 } // if (nsumy == 1)
1512 // one bin in X - add one more (with the largest signal)
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);
1520 x = mlem->GetXaxis()->GetBinCenter(j);
1521 y = mlem->GetYaxis()->GetBinCenter(i);
1530 if (i2 != i1) nsumy++;
1531 if (j2 != j1) nsumx++;
1533 } // if (nsumx == 1)
1535 xyc[0] = xq/qq; xyc[1] = yq/qq;
1536 AliInfo(Form("%f %f %f %d %d %d",xyc[0],xyc[1],qq,nsum,nsumx,nsumy));
1540 //_____________________________________________________________________________
1541 Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1543 // Find the pixel nearest to the given one
1544 // (algorithm may be not very efficient)
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;
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; }
1562 //_____________________________________________________________________________
1563 void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
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)
1569 Int_t nx = mlem->GetNbinsX();
1570 Int_t ny = mlem->GetNbinsY();
1571 Int_t nPix = fPixArray->GetEntriesFast();
1573 Bool_t *used = new Bool_t[ny*nx];
1575 Int_t nclust = 0, indx, indx1;
1577 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1579 TObjArray *clusters[200]={0};
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);
1591 pix->Add(BinToPix(mlem,j,i));
1592 AddBin(mlem, i, j, 0, used, pix); // recursive call
1593 clusters[nclust++] = pix;
1594 if (nclust > 200) { AliInfo(" Too many clusters "); ::exit(0); }
1595 } // for (Int_t j=1; j<=nx; j++) {
1596 } // for (Int_t i=1; i<=ny;
1597 AliInfo(Form("%d ",nclust));
1598 delete [] used; used = 0;
1600 // Compute couplings between clusters and clusters to pads
1601 Int_t npad = fnPads[0] + fnPads[1];
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;
1609 // Compute couplings of clusters to pads
1610 TMatrixD *aijclupad = new TMatrixD(nclust,npad);
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;
1621 if (coef[j*nPix+indx] < fgkCouplMin) continue;
1622 (*aijclupad)(iclust,j) += coef[j*nPix+indx];
1626 // Compute couplings between clusters
1627 TMatrixD *aijcluclu = new TMatrixD(nclust,nclust);
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;
1633 if ((*aijclupad)(iclust,j) < fgkCouplMin) continue;
1634 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1635 if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue;
1636 (*aijcluclu)(iclust,iclust1) +=
1637 TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j));
1641 for (Int_t iclust=0; iclust<nclust; iclust++) {
1642 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1643 (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1);
1647 if (nclust > 1) aijcluclu->Print();
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;
1656 for (Int_t igroup=0; igroup<nclust; igroup++) {
1657 if (used[igroup]) continue;
1658 used[igroup] = kTRUE;
1659 clustNumb[0] = igroup;
1661 // Find group of coupled clusters
1662 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
1663 AliInfo(Form(" nCoupled: %d",nCoupled));
1664 for (Int_t i=0; i<nCoupled; i++) AliInfo(Form(" %d ",clustNumb[i]));
1666 while (nCoupled > 0) {
1670 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
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;
1676 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
1678 // Flag clusters for fit
1680 while (minGroup[nForFit] >= 0 && nForFit < 3) {
1681 AliInfo(Form("%d ",clustNumb[minGroup[nForFit]]));
1682 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1683 clustNumb[minGroup[nForFit]] -= 999;
1686 AliInfo(Form("%d %f ",nForFit,coupl));
1689 // Select pads for fit.
1690 if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) {
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
1695 Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
1698 nfit = Fit(nForFit, clustFit, clusters, parOk);
1701 // Subtract the fitted charges from pads with strong coupling and/or
1702 // return pads for further use
1703 UpdatePads(nfit, parOk);
1706 for (Int_t j=0; j<npad; j++) {if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;}
1708 // Sort the clusters (move to the right the used ones)
1709 Int_t beg = 0, end = nCoupled - 1;
1711 if (clustNumb[beg] >= 0) { beg++; continue; }
1712 for (Int_t j=end; j>beg; j--) {
1713 if (clustNumb[j] < 0) continue;
1715 indx = clustNumb[beg];
1716 clustNumb[beg] = clustNumb[j];
1717 clustNumb[j] = indx;
1723 nCoupled -= nForFit;
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];
1730 (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0;
1734 // Update the remaining clusters couplings (exclude couplings from
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];
1740 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1741 for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1742 indx1 = clustNumb[iclust1];
1743 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1745 (*aijcluclu)(indx,indx1) -=
1746 TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j));
1747 (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1);
1751 } // for (Int_t j=0; j<npad;
1752 } // if (nCoupled > 3)
1753 } // while (nCoupled > 0)
1754 } // for (Int_t igroup=0; igroup<nclust;
1756 //delete aij_clu; aij_clu = 0; delete aijclupad; aijclupad = 0;
1757 aijcluclu->Delete(); aijclupad->Delete();
1758 for (Int_t iclust=0; iclust<nclust; iclust++) {
1759 pix = clusters[iclust];
1761 delete pix; pix = 0;
1763 delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1766 //_____________________________________________________________________________
1767 void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1769 // Add a bin to the cluster
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;
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));
1786 pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j),
1787 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1788 fPixArray->Add((TObject*)pixPtr);
1790 AddBin(mlem, i, j, mode, used, pix); // recursive call
1795 //_____________________________________________________________________________
1796 TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1798 // Translate histogram bin to pixel
1800 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1801 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1803 Int_t nPix = fPixArray->GetEntriesFast();
1804 AliMUONPixel *pixPtr;
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;
1812 AliWarning(" Something wrong ??? ");
1816 //_____________________________________________________________________________
1817 void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
1819 // Add a cluster to the group of coupled clusters
1821 for (Int_t i=0; i<nclust; i++) {
1822 if (used[i]) continue;
1823 if ((*aijcluclu)(i,ic) < fgkCouplMin) continue;
1825 clustNumb[nCoupled++] = i;
1826 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
1830 //_____________________________________________________________________________
1831 Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup)
1833 // Find group of clusters with minimum coupling to all the others
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;
1839 for (Int_t i123=1; i123<=i123max; i123++) {
1842 coupl1 = new Double_t [nCoupled];
1843 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
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;
1850 nTot = nTot*nCoupled;
1851 coupl3 = new Double_t [nTot];
1852 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
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];
1860 coupl1[i] += (*aijcluclu)(indx1,indx2);
1861 coupl1[j] += (*aijcluclu)(indx1,indx2);
1863 else if (i123 == 2) {
1864 indx = i*nCoupled + j;
1865 coupl2[indx] = coupl1[i] + coupl1[j];
1866 coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2));
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];
1872 coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3));
1875 } // for (Int_t j=i+1;
1876 } // for (Int_t i=0;
1877 } // for (Int_t i123=1;
1879 // Find minimum coupling
1880 Double_t couplMin = 9999;
1883 for (Int_t i123=1; i123<=i123max; i123++) {
1885 locMin = TMath::LocMin(nCoupled, coupl1);
1886 couplMin = coupl1[locMin];
1887 minGroup[0] = locMin;
1888 delete [] coupl1; coupl1 = 0;
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;
1897 delete [] coupl2; coupl2 = 0;
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;
1906 delete [] coupl3; coupl3 = 0;
1908 } // for (Int_t i123=1;
1912 //_____________________________________________________________________________
1913 Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad)
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.
1918 Int_t npad = fnPads[0] + fnPads[1];
1919 Double_t *padpix = 0;
1922 padpix = new Double_t[npad];
1923 for (Int_t i=0; i<npad; i++) padpix[i] = 0;
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
1931 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1932 fPadIJ[1][j] = 1; // pad to be used in fit
1935 // Check other clusters
1936 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1937 indx1 = clustNumb[iclust1];
1938 if (indx1 < 0) continue;
1939 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1940 padpix[j] += (*aijclupad)(indx1,j);
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;
1948 for (Int_t j=0; j<npad; j++) {
1949 if (padpix[j] < fgkCouplMin) continue;
1950 AliInfo(Form("%d %f ",j , padpix[j]));
1951 AliInfo(Form("%f %f ",fXyq[0][j],fXyq[1][j]));
1953 fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1956 delete [] padpix; padpix = 0;
1960 //_____________________________________________________________________________
1961 void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad)
1963 // Merge the group of clusters with the one having the strongest coupling with them
1965 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1966 TObjArray *pix, *pix1;
1969 for (Int_t icl=0; icl<nForFit; icl++) {
1970 indx = clustFit[icl];
1971 pix = clusters[indx];
1972 npxclu = pix->GetEntriesFast();
1974 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1975 indx1 = clustNumb[icl1];
1976 if (indx1 < 0) continue;
1977 if ((*aijcluclu)(indx,indx1) > couplMax) {
1978 couplMax = (*aijcluclu)(indx,indx1);
1981 } // for (Int_t icl1=0;
1982 /*if (couplMax < fgkCouplMin) {
1983 cout << " Oops " << couplMax << endl;
1985 cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1989 pix1 = clusters[imax];
1990 npxclu1 = pix1->GetEntriesFast();
1992 for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
1993 AliInfo(Form(" New number of pixels: %d %d ",npxclu1 ,pix1->GetEntriesFast() ));
1994 //Add cluster-to-cluster couplings
1995 //aijcluclu->Print();
1996 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1997 indx1 = clustNumb[icl1];
1998 if (indx1 < 0 || indx1 == imax) continue;
1999 (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1);
2000 (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax);
2002 (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0;
2003 //aijcluclu->Print();
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
2007 (*aijclupad)(imax,j) += (*aijclupad)(indx,j);
2008 (*aijclupad)(indx,j) = 0;
2010 } // for (Int_t icl=0; icl<nForFit;
2013 //_____________________________________________________________________________
2014 Int_t AliMUONClusterFinderAZ::Fit(Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
2016 // Find selected clusters to selected pad charges
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};
2028 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8];
2032 // Number of pads to use
2034 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {if (fPadIJ[1][i] == 1) npads++;}
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));
2040 if (npads < 2) return 0;
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++) {
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();
2055 xseed = pixPtr->Coord(0);
2056 yseed = pixPtr->Coord(1);
2059 xyseed[ifit-1][0] = xseed;
2060 xyseed[ifit-1][1] = yseed;
2061 qseed[ifit-1] = cmax;
2062 } // for (Int_t ifit=1;
2064 Int_t nDof, maxSeed[3];
2065 Double_t fmin, chi2o = 9999, chi2n;
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).
2070 TMath::Sort(nfit, qseed, maxSeed, kTRUE); // in decreasing order
2071 nfit = TMath::Min (nfit, (npads + 1) / 3);
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;
2078 for (Int_t iseed=0; iseed<nfit; iseed++) {
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;
2088 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
2090 parmax[fNpar++] = 1;
2093 // Try new algorithm
2094 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
2098 Fcn1(fNpar, gin, func0, param, 1); nCall++;
2099 //cout << " Func: " << func0 << endl;
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;
2107 Fcn1(fNpar, gin, func1, param, 1); nCall++;
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
2113 param[fNpar-1] -= delta[fNpar-1] / 10;
2114 if (nCall > 2000) ::exit(0);
2116 min = func2[0] < func2[1] ? 0 : 1;
2117 nFail = min == max ? 0 : nFail + 1;
2119 stepMax = derMax = estim = 0;
2120 for (Int_t j=0; j<fNpar; j++) {
2121 // Estimated distance to minimum
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]));
2129 if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2133 shift[j] = -deriv[min][j] / dder[j];
2136 if (TMath::Abs(shift[j])/step0[j] > estim) {
2137 estim = TMath::Abs(shift[j])/step0[j];
2142 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
2144 // Failed to improve minimum
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;
2153 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
2154 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
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]);
2162 param[j] += shift[j];
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) {
2168 derMax = TMath::Abs (deriv[min][j]);
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
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];
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;
2197 nDof = npads - fNpar;
2198 chi2n = nDof ? fmin/nDof : 0;
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];
2207 AliInfo(Form("%f %f ",chi2o ,chi2n));
2209 if (fmin < 0.1) break; // !!!???
2210 } // for (Int_t iseed=0;
2212 for (Int_t i=0; i<fNpar; i++) {
2213 if (i == 4 || i == 7) continue;
2214 AliInfo(Form("%f %f ",parOk[i],errOk[i]));
2216 nfit = (fNpar + 1) / 3;
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]);
2226 for (Int_t i=0; i<fnMu; i++) {
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]);
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];
2246 //_____________________________________________________________________________
2247 void AliMUONClusterFinderAZ::Fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2249 // Fit for one track
2250 AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);
2252 Int_t cath, ix, iy, indx, npads=0;
2253 Double_t charge, delta, coef=0, chi2=0;
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];
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);
2262 for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2263 indx = i<2 ? 2*i : 2*i+1;
2264 c.fSeg2[cath]->SetHit(fInput->DetElemId(),par[indx],par[indx+1],c.fZpad);
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.);
2271 charge += c.fResponse->IntXY(fInput->DetElemId(),c.fSeg2[cath])*coef;
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;
2283 Double_t qAver = c.fQtot/npads; //(c.fnPads[0]+c.fnPads[1]);
2287 //_____________________________________________________________________________
2288 void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par)
2290 // Subtract the fitted charges from pads with strong coupling
2292 Int_t cath, ix, iy, indx;
2293 Double_t charge, coef=0;
2295 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2296 if (fPadIJ[1][j] != -1) continue;
2298 cath = fPadIJ[0][j];
2299 fSeg2[cath]->GetPadI(fInput->DetElemId(),fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
2300 fSeg2[cath]->SetPad(fInput->DetElemId(),ix,iy);
2302 for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2303 indx = i<2 ? 2*i : 2*i+1;
2304 fSeg2[cath]->SetHit(fInput->DetElemId(),par[indx],par[indx+1],fZpad);
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];
2308 charge += fResponse->IntXY(fInput->DetElemId(),fSeg2[cath])*coef;
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;
2318 //_____________________________________________________________________________
2319 Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) const {
2320 // Test if track was user selected
2323 if (fTrack[0]==-1 || fTrack[1]==-1) {
2325 } else if (t==fTrack[0] || t==fTrack[1]) {
2333 //_____________________________________________________________________________
2334 void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t fmin)
2337 // Add a raw cluster copy to the list
2339 AliMUONRawCluster cnew;
2340 AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2341 //pMUON->AddRawCluster(fInput->Chamber(),c);
2344 for (cath=0; cath<2; cath++) {
2347 cnew.SetZ(cath, fZpad);
2348 cnew.SetCharge(cath, 100);
2349 cnew.SetPeakSignal(cath,20);
2350 cnew.SetMultiplicity(cath, 5);
2351 cnew.SetNcluster(cath, 1);
2352 cnew.SetChi2(cath, fmin); //0.1;
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]);
2359 fprintf(stderr,"\nRawCluster %d cath %d\n",ico,cath);
2360 fprintf(stderr,"mult_av %d\n",c->fMultiplicity[cath]);
2361 FillCluster(&cnew,cath);
2364 //cnew.fClusterType=cnew.PhysicsContribution();
2365 pMUON->GetMUONData()->AddRawCluster(AliMUONClusterInput::Instance()->Chamber(),cnew);
2369 //_____________________________________________________________________________
2370 Int_t AliMUONClusterFinderAZ::FindLocalMaxima(Int_t *localMax, Double_t *maxVal)
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)
2375 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2376 if (hist) hist->Delete();
2378 Double_t xylim[4] = {999, 999, 999, 999};
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));
2386 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
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());
2396 ((TCanvas*)gROOT->FindObject("c2"))->cd();
2399 hist->Draw("lego1Fb");
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;
2409 for (Int_t i=1; i<=ny; i++) {
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);
2419 for (Int_t i=1; i<=ny; i++) {
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);
2426 if (nMax > 99) { AliWarning(" Too many local maxima !!!" ); ::exit(0); }
2429 AliInfo(Form(" Local max: %d",nMax));
2430 delete [] isLocalMax; isLocalMax = 0;
2434 //_____________________________________________________________________________
2435 void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2437 // Flag pixels (whether or not local maxima)
2439 Int_t nx = hist->GetNbinsX();
2440 Int_t ny = hist->GetNbinsY();
2441 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
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;
2462 isLocalMax[(i-1)*nx+j-1] = 1; // local maximum
2465 //_____________________________________________________________________________
2466 void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2468 // Find pixel cluster around local maximum #iMax and pick up pads
2469 // overlapping with it
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;
2479 // Drop all pixels from the array - pick up only the ones from the cluster
2480 fPixArray->Delete();
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
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);
2497 AliInfo(Form("%d %d ",iMax,nPix));
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
2510 delete [] used; used = 0;
2513 //_____________________________________________________________________________
2514 AliMUONClusterFinderAZ&
2515 AliMUONClusterFinderAZ::operator=(const AliMUONClusterFinderAZ& rhs)
2517 // Protected assignement operator
2519 if (this == &rhs) return *this;
2521 AliFatal("Not implemented.");