1 #include "AliMUONClusterFinderAZ.h"
16 #include "AliHeader.h"
19 #include "AliMUONChamber.h"
20 #include "AliMUONDigit.h"
21 #include "AliMUONHit.h"
22 #include "AliMUONChamber.h"
23 #include "AliMUONRawCluster.h"
24 #include "AliMUONClusterInput.h"
25 #include "AliMUONPixel.h"
27 // This function is used for fitting
28 void fcn1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag);
30 ClassImp(AliMUONClusterFinderAZ)
32 AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = NULL;
33 TMinuit* AliMUONClusterFinderAZ::fgMinuit = NULL;
35 //_____________________________________________________________________________
36 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw=0, Int_t iReco=0)
39 for (Int_t i=0; i<4; i++) {fHist[i] = 0;}
41 fSegmentation[1] = fSegmentation[0] = 0;
42 if (!fgClusterFinder) fgClusterFinder = this;
43 if (!fgMinuit) fgMinuit = new TMinuit(8);
46 fPixArray = new TObjArray(20);
52 fNextCathode = kFALSE;
57 //_____________________________________________________________________________
58 AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ()
61 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
63 // Delete space point structure
64 if (fPoints) fPoints->Delete();
68 if (fPhits) fPhits->Delete();
72 if (fRpoints) fRpoints->Delete();
78 //_____________________________________________________________________________
79 void AliMUONClusterFinderAZ::FindRawClusters()
81 // To provide the same interface as in AliMUONClusterFinderVS
83 EventLoop (gAlice->GetHeader()->GetEvent(), AliMUONClusterInput::Instance()->Chamber());
86 //_____________________________________________________________________________
87 void AliMUONClusterFinderAZ::EventLoop(Int_t nev=0, Int_t ch=0)
95 Double_t p1[3]={0}, p2[3];
99 lun = fopen("pool.dat","w");
100 c1 = new TCanvas("c1","Clusters",0,0,600,700);
102 new TCanvas("c2","Mlem",700,0,600,350);
106 Int_t nparticles = 0, nent;
107 if (!fReco) nparticles = gAlice->GetEvent(nev);
108 else nparticles = gAlice->GetNtrack();
109 cout << "nev " << nev <<endl;
110 cout << "nparticles " << nparticles <<endl;
111 if (nparticles <= 0) return;
113 TTree *TH = gAlice->TreeH();
114 Int_t ntracks = (Int_t) TH->GetEntries();
115 cout<<"ntracks "<<ntracks<<endl;
117 // Get pointers to Alice detectors and Digits containers
118 AliMUON *MUON = (AliMUON*) gAlice->GetModule("MUON");
120 // TClonesArray *Particles = gAlice->Particles();
122 TR = gAlice->TreeR();
124 MUON->ResetRawClusters();
125 nent = (Int_t) TR->GetEntries();
127 cout << "Error in MUONdrawClust" << endl;
128 cout << " nent = " << nent << " not equal to 1" << endl;
134 TTree *TD = gAlice->TreeD();
135 //MUON->ResetDigits();
137 TClonesArray *MUONrawclust;
138 AliMUONChamber* iChamber = 0;
140 // As default draw the first cluster of the chamber #0
143 if (ch > 9) {if (fReco) return; nev++; ch = 0; goto newev;}
144 //gAlice->ResetDigits();
145 fMuonDigits = MUON->DigitsAddress(ch);
146 if (fMuonDigits == 0) return;
147 iChamber = &(MUON->Chamber(ch));
148 fSegmentation[0] = iChamber->SegmentationModel(1);
149 fSegmentation[1] = iChamber->SegmentationModel(2);
150 fResponse = iChamber->ResponseModel();
155 nent = (Int_t) TD->GetEntries();
156 //printf(" entries %d \n", nent);
159 Int_t ndigits[2]={9,9}, nShown[2]={0};
160 for (Int_t i=0; i<2; i++) {
161 for (Int_t j=0; j<kDim; j++) {fUsed[i][j]=kFALSE;}
165 if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) {
169 goto newchamber; // next chamber
171 Float_t xpad, ypad, zpad, zpad0;
174 Bool_t first = kTRUE;
175 cout << " *** Event # " << nev << " chamber: " << ch << endl;
176 fnPads[0] = fnPads[1] = 0;
177 for (Int_t i=0; i<kDim; i++) {fPadIJ[1][i] = 0;}
178 //for (Int_t iii = 0; iii<999; iii++) {
179 for (Int_t iii = 0; iii<2; iii++) {
180 Int_t cath = TMath::Odd(iii);
181 gAlice->ResetDigits();
183 fMuonDigits = MUON->DigitsAddress(ch);
185 ndigits[cath] = fMuonDigits->GetEntriesFast();
186 if (!ndigits[0] && !ndigits[1]) {if (fReco) return; ch++; goto newchamber;}
187 if (ndigits[cath] == 0) continue;
188 cout << " ndigits: " << ndigits[cath] << " " << cath << endl;
193 Bool_t EOC = kTRUE; // end-of-cluster
194 for (digit = 0; digit < ndigits[cath]; digit++) {
195 mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
196 if (mdig->Cathode() != cath) continue;
198 // Find first unused pad
199 if (fUsed[cath][digit]) continue;
200 fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0);
202 if (fUsed[cath][digit]) continue;
203 fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
204 if (TMath::Abs(zpad-zpad0)>0.1) continue; // different slats
205 // Find a pad overlapping with the cluster
206 if (!Overlap(cath,mdig)) continue;
208 // Add pad - recursive call
211 if (digit >= 0) break;
214 // No more unused pads
215 if (cath == 0) continue; // on cathode #0 - check #1
220 goto newchamber; // next chamber
223 if (EOC) break; // cluster found
225 cout << " nPads: " << fnPads[cath] << " " << nShown[cath]+fnPads[cath] << " " << cath << endl;
226 } // for (Int_t iii = 0;
229 if (fReco) goto skip;
231 for (Int_t cath = 0; cath<2; cath++) {
233 if (fHist[cath*2]) {fHist[cath*2]->Delete(); fHist[cath*2] = 0;}
234 if (fHist[cath*2+1]) {fHist[cath*2+1]->Delete(); fHist[cath*2+1] = 0;}
235 if (fnPads[cath] == 0) continue; // cluster on one cathode only
236 Float_t wxMin=999, wxMax=0, wyMin=999, wyMax=0;
237 Int_t minDx=0, maxDx=0, minDy=0, maxDy=0;
238 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
239 if (fPadIJ[0][i] != cath) continue;
240 if (fXyq[3][i] < wxMin) {wxMin = fXyq[3][i]; minDx = i;}
241 if (fXyq[3][i] > wxMax) {wxMax = fXyq[3][i]; maxDx = i;}
242 if (fXyq[4][i] < wyMin) {wyMin = fXyq[4][i]; minDy = i;}
243 if (fXyq[4][i] > wyMax) {wyMax = fXyq[4][i]; maxDy = i;}
245 cout << minDx << maxDx << minDy << maxDy << endl;
246 Int_t nx, ny, padSize;
247 Float_t xmin=9999, xmax=-9999, ymin=9999, ymax=-9999;
248 if (TMath::Nint(fXyq[3][minDx]*1000) == TMath::Nint(fXyq[3][maxDx]*1000) &&
249 TMath::Nint(fXyq[4][minDy]*1000) == TMath::Nint(fXyq[4][maxDy]*1000)) {
250 // the same segmentation
251 cout << " Same" << endl;
252 cout << fXyq[3][minDx] << " " << fXyq[3][maxDx] << " " << fXyq[4][minDy] << " " << fXyq[4][maxDy] << endl;
253 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
254 if (fPadIJ[0][i] != cath) continue;
255 if (fXyq[0][i] < xmin) xmin = fXyq[0][i];
256 if (fXyq[0][i] > xmax) xmax = fXyq[0][i];
257 if (fXyq[1][i] < ymin) ymin = fXyq[1][i];
258 if (fXyq[1][i] > ymax) ymax = fXyq[1][i];
260 xmin -= fXyq[3][minDx]; xmax += fXyq[3][minDx];
261 ymin -= fXyq[4][minDy]; ymax += fXyq[4][minDy];
262 nx = TMath::Nint ((xmax-xmin)/wxMin/2);
263 ny = TMath::Nint ((ymax-ymin)/wyMin/2);
264 sprintf(hName,"h%d",cath*2);
265 fHist[cath*2] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
266 cout << fHist[cath*2] << " " << fnPads[cath] << endl;
267 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
268 if (fPadIJ[0][i] != cath) continue;
269 fHist[cath*2]->Fill(fXyq[0][i],fXyq[1][i],fXyq[2][i]);
270 //cout << fXyq[0][i] << fXyq[1][i] << fXyq[2][i] << endl;
273 // different segmentation in the cluster
274 cout << " Different" << endl;
275 cout << fXyq[3][minDx] << " " << fXyq[3][maxDx] << " " << fXyq[4][minDy] << " " << fXyq[4][maxDy] << endl;
277 Int_t indx, locMin, locMax;
278 if (TMath::Nint(fXyq[3][minDx]*1000) != TMath::Nint(fXyq[3][maxDx]*1000)) {
279 // different segmentation along x
284 // different segmentation along y
290 for (Int_t i=0; i<2; i++) {
291 // loop over different pad sizes
292 if (i>0) loc = locMax;
293 padSize = TMath::Nint(fXyq[indx+3][loc]*1000);
294 xmin = 9999; xmax = -9999; ymin = 9999; ymax = -9999;
295 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
296 if (fPadIJ[0][j] != cath) continue;
297 if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
299 xmin = TMath::Min (xmin,fXyq[0][j]);
300 xmax = TMath::Max (xmax,fXyq[0][j]);
301 ymin = TMath::Min (ymin,fXyq[1][j]);
302 ymax = TMath::Max (ymax,fXyq[1][j]);
304 xmin -= fXyq[3][loc]; xmax += fXyq[3][loc];
305 ymin -= fXyq[4][loc]; ymax += fXyq[4][loc];
306 nx = TMath::Nint ((xmax-xmin)/fXyq[3][loc]/2);
307 ny = TMath::Nint ((ymax-ymin)/fXyq[4][loc]/2);
308 sprintf(hName,"h%d",cath*2+i);
309 fHist[cath*2+i] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax);
310 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
311 if (fPadIJ[0][j] != cath) continue;
312 if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue;
313 fHist[cath*2+i]->Fill(fXyq[0][j],fXyq[1][j],fXyq[2][j]);
316 if (nOK != fnPads[cath]) cout << " *** Too many segmentations: nPads, nOK " << fnPads[cath] << " " << nOK << endl;
317 } // if (TMath::Nint(fXyq[3][minDx]*1000)
318 } // for (Int_t cath = 0;
320 // Draw histograms and coordinates
321 for (Int_t cath=0; cath<2; cath++) {
322 if (cath == 0) ModifyHistos();
323 if (fnPads[cath] == 0) continue; // cluster on one cathode only
328 Double_t x, y, x0, y0, r1=999, r2=0;
329 if (fHist[cath*2+1]) {
331 x0 = fHist[cath*2]->GetXaxis()->GetXmin() - 1000*TMath::Cos(30*TMath::Pi()/180);
332 y0 = fHist[cath*2]->GetYaxis()->GetXmin() - 1000*TMath::Sin(30*TMath::Pi()/180);
335 for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
336 y = fHist[ihist]->GetYaxis()->GetBinCenter(iy)
337 + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
338 for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
339 if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
340 x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
341 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
342 r1 = TMath::Max (r1,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
347 for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) {
348 y = fHist[ihist]->GetYaxis()->GetBinCenter(iy)
349 + fHist[ihist]->GetYaxis()->GetBinWidth(iy);
350 for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) {
351 if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) {
352 x = fHist[ihist]->GetXaxis()->GetBinCenter(ix)
353 + fHist[ihist]->GetXaxis()->GetBinWidth(ix);
354 r2 = TMath::Max (r2,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
358 cout << r1 << " " << r2 << endl;
359 } // if (fHist[cath*2+1])
361 //fHist[cath*2]->Draw("lego1");
362 fHist[cath*2]->Draw("lego1Fb");
363 //if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBb");
364 if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBbFb");
366 //fHist[cath*2+1]->Draw("lego1");
367 fHist[cath*2+1]->Draw("lego1Fb");
368 //fHist[cath*2]->Draw("lego1SameAxisBb");
369 fHist[cath*2]->Draw("lego1SameAxisFbBb");
373 } // for (Int_t cath = 0;
375 // Draw generated hits
377 hist = fHist[0] ? fHist[0] : fHist[2];
378 p2[2] = hist->GetMaximum();
380 if (c1) view = c1->Pad()->GetView();
381 cout << " *** GEANT hits *** " << endl;
384 for (Int_t i=0; i<ntracks; i++) {
386 for (AliMUONHit* mHit=(AliMUONHit*)MUON->FirstHit(-1);
388 mHit=(AliMUONHit*)MUON->NextHit()) {
389 if (mHit->Chamber() != ch+1) continue; // chamber number
390 if (TMath::Abs(mHit->Z()-zpad0) > 1) continue; // different slat
391 p2[0] = p1[0] = mHit->X(); // x-pos of hit
392 p2[1] = p1[1] = mHit->Y(); // y-pos
393 if (p1[0] < hist->GetXaxis()->GetXmin() ||
394 p1[0] > hist->GetXaxis()->GetXmax()) continue;
395 if (p1[1] < hist->GetYaxis()->GetXmin() ||
396 p1[1] > hist->GetYaxis()->GetXmax()) continue;
397 // Check if track comes thru pads with signal
399 for (Int_t ihist=0; ihist<4; ihist++) {
400 if (!fHist[ihist]) continue;
401 ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
402 iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
403 if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
406 gStyle->SetLineColor(1);
407 if (TMath::Abs((Int_t)mHit->Particle()) == 13) {
408 gStyle->SetLineColor(4);
411 fxyMu[fnMu-1][0] = p1[0];
412 fxyMu[fnMu-1][1] = p1[1];
415 printf(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mHit->Z());
417 view->WCtoNDC(p1, &xNDC[0]);
418 view->WCtoNDC(p2, &xNDC[3]);
419 for (Int_t ipad=1; ipad<3; ipad++) {
421 //c1->DrawLine(xpad[0],xpad[1],xpad[3],xpad[4]);
422 line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
423 line[nLine++]->Draw();
426 } // for (AliMUONHit* mHit=
427 } // for (Int_t i=0; i<ntracks;
429 // Draw reconstructed coordinates
430 MUONrawclust = MUON->RawClustAddress(ch);
432 //cout << MUONrawclust << " " << MUONrawclust->GetEntries() << endl;
433 AliMUONRawCluster *mRaw;
434 gStyle->SetLineColor(3);
435 cout << " *** Reconstructed hits *** " << endl;
436 for (Int_t i=0; i<MUONrawclust->GetEntries(); i++) {
437 mRaw = (AliMUONRawCluster*)MUONrawclust->UncheckedAt(i);
438 if (TMath::Abs(mRaw->fZ[0]-zpad0) > 1) continue; // different slat
439 p2[0] = p1[0] = mRaw->fX[0]; // x-pos of hit
440 p2[1] = p1[1] = mRaw->fY[0]; // y-pos
441 if (p1[0] < hist->GetXaxis()->GetXmin() ||
442 p1[0] > hist->GetXaxis()->GetXmax()) continue;
443 if (p1[1] < hist->GetYaxis()->GetXmin() ||
444 p1[1] > hist->GetYaxis()->GetXmax()) continue;
447 cout << mRaw->fMultiplicity[0] << mRaw->fMultiplicity[1] << endl;
448 for (Int_t j=0; j<mRaw->fMultiplicity[cath]; j++) {
449 Int_t digit = mRaw->fIndexMap[j][cath];
450 cout << ((AliMUONDigit*)fMuonDigits->UncheckedAt(digit))->Signal() << endl;
453 // Check if track comes thru pads with signal
455 for (Int_t ihist=0; ihist<4; ihist++) {
456 if (!fHist[ihist]) continue;
457 ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]);
458 iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]);
459 if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;}
462 printf(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mRaw->fZ[0]);
464 view->WCtoNDC(p1, &xNDC[0]);
465 view->WCtoNDC(p2, &xNDC[3]);
466 for (Int_t ipad=1; ipad<3; ipad++) {
468 line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]);
469 line[nLine++]->Draw();
472 } // for (Int_t i=0; i<MUONrawclust->GetEntries();
473 if (fDraw) c1->Update();
476 // Use MLEM for cluster finder
478 Int_t nMax = 1, localMax[100], maxPos[100];
479 Double_t maxVal[100];
481 if (CheckPrecluster(nShown)) {
483 if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(localMax, maxVal);
484 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
485 for (Int_t i=0; i<nMax; i++) {
486 if (nMax > 1) FindCluster(localMax, maxPos[i]);
487 if (!MainLoop()) cout << " MainLoop failed " << endl;
489 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
490 if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
492 fXyq[2][j] = fXyq[5][j]; // use backup charge value
497 if (fReco) goto next;
499 for (Int_t i=0; i<fnMu; i++) {
500 // Check again if muon come thru the used pads (due to extra splitting)
501 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
502 if (TMath::Abs(fxyMu[i][0]-fXyq[0][j])<fXyq[3][j] &&
503 TMath::Abs(fxyMu[i][1]-fXyq[1][j])<fXyq[4][j]) {
504 printf("%12.3e %12.3e %12.3e %12.3e\n",fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]);
505 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]);
509 } // for (Int_t i=0; i<fnMu;
513 cout << " What is next? " << endl;
515 if (fDraw) gets(command);
516 if (command[0] == 'n' || command[0] == 'N') {nev++; goto newev;} // next event
517 else if (command[0] == 'q' || command[0] == 'Q') {fclose(lun); return;} // exit display
518 //else if (command[0] == 'r' || command[0] == 'R') goto redraw; // redraw points
519 else if (command[0] == 'c' || command[0] == 'C') {
521 sscanf(command+1,"%d",&ch);
524 else if (command[0] == 'e' || command[0] == 'E') {
526 sscanf(command+1,"%d",&nev);
529 else goto next; // Next cluster
532 //_____________________________________________________________________________
533 void AliMUONClusterFinderAZ::ModifyHistos(void)
535 // Modify histograms to bring them to the same size
537 Float_t hlim[4][4], hbin[4][4]; // first index - xmin, xmax, ymin, ymax
538 Float_t binMin[4] = {999,999,999,999};
540 for (Int_t i=0; i<4; i++) {
541 if (!fHist[i]) continue;
542 hlim[0][nhist] = fHist[i]->GetXaxis()->GetXmin(); // xmin
543 hlim[1][nhist] = fHist[i]->GetXaxis()->GetXmax(); // xmax
544 hlim[2][nhist] = fHist[i]->GetYaxis()->GetXmin(); // ymin
545 hlim[3][nhist] = fHist[i]->GetYaxis()->GetXmax(); // ymax
546 hbin[0][nhist] = hbin[1][nhist] = fHist[i]->GetXaxis()->GetBinWidth(1);
547 hbin[2][nhist] = hbin[3][nhist] = fHist[i]->GetYaxis()->GetBinWidth(1);
548 binMin[0] = TMath::Min(binMin[0],hbin[0][nhist]);
549 binMin[2] = TMath::Min(binMin[2],hbin[2][nhist]);
552 binMin[1] = binMin[0];
553 binMin[3] = binMin[2];
554 cout << " Nhist: " << nhist << endl;
557 for (Int_t lim=0; lim<4; lim++) {
559 imin = TMath::LocMin(nhist,hlim[lim]);
560 imax = TMath::LocMax(nhist,hlim[lim]);
561 if (TMath::Abs(hlim[lim][imin]-hlim[lim][imax])<0.01*binMin[lim]) break;
562 if (lim == 0 || lim == 2) {
564 hlim[lim][imax] -= hbin[lim][imax];
567 hlim[lim][imin] += hbin[lim][imin];
572 // Rebuild histograms
576 Double_t x, y, cont, cmax=0;
578 for (Int_t ihist=0; ihist<4; ihist++) {
579 if (!fHist[ihist]) continue;
580 nx = TMath::Nint((hlim[1][nhist]-hlim[0][nhist])/hbin[0][nhist]);
581 ny = TMath::Nint((hlim[3][nhist]-hlim[2][nhist])/hbin[2][nhist]);
582 //hist = new TH2F("h","hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
583 sprintf(hName,"hh%d",ihist);
584 hist = new TH2F(hName,"hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]);
585 for (Int_t i=1; i<=fHist[ihist]->GetNbinsX(); i++) {
586 x = fHist[ihist]->GetXaxis()->GetBinCenter(i);
587 for (Int_t j=1; j<=fHist[ihist]->GetNbinsY(); j++) {
588 y = fHist[ihist]->GetYaxis()->GetBinCenter(j);
589 cont = fHist[ihist]->GetCellContent(i,j);
590 hist->Fill(x,y,cont);
593 cmax = TMath::Max (cmax,hist->GetMaximum());
594 fHist[ihist]->Delete();
595 fHist[ihist] = new TH2F(*hist);
599 printf("%f \n",cmax);
601 for (Int_t ihist=0; ihist<4; ihist++) {
602 if (!fHist[ihist]) continue;
603 fHist[ihist]->SetMaximum(cmax);
607 //_____________________________________________________________________________
608 void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
610 // Add pad to the cluster
611 AliMUONDigit *mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit);
613 Int_t charge = mdig->Signal();
614 // get the center of the pad
615 Float_t xpad, ypad, zpad;
616 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
618 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
619 Int_t nPads = fnPads[0] + fnPads[1];
620 fXyq[0][nPads] = xpad;
621 fXyq[1][nPads] = ypad;
622 fXyq[2][nPads] = charge;
623 fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2;
624 fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2;
625 fXyq[5][nPads] = digit;
626 fPadIJ[0][nPads] = cath;
627 fPadIJ[1][nPads] = 0;
628 fUsed[cath][digit] = kTRUE;
629 //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;
633 Int_t nn, ix, iy, xList[10], yList[10];
636 Int_t ndigits = fMuonDigits->GetEntriesFast();
637 fSegmentation[cath]->Neighbours(mdig->PadX(),mdig->PadY(),&nn,xList,yList);
638 for (Int_t in=0; in<nn; in++) {
641 for (Int_t digit1 = 0; digit1 < ndigits; digit1++) {
642 if (digit1 == digit) continue;
643 mdig1 = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit1);
644 if (mdig1->Cathode() != cath) continue;
645 if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) {
646 fUsed[cath][digit1] = kTRUE;
647 // Add pad - recursive call
650 } //for (Int_t digit1 = 0;
651 } // for (Int_t in=0;
654 //_____________________________________________________________________________
655 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, TObject *dig)
657 // Check if the pad from one cathode overlaps with a pad
658 // in the precluster on the other cathode
660 AliMUONDigit *mdig = (AliMUONDigit*) dig;
662 Float_t xpad, ypad, zpad;
663 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
664 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
666 Float_t xy1[4], xy12[4];
667 xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2;
668 xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec);
669 xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2;
670 xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec);
671 //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
673 Int_t cath1 = TMath::Even(cath);
674 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
675 if (fPadIJ[0][i] != cath1) continue;
676 if (Overlap(xy1, i, xy12, 0)) return kTRUE;
681 //_____________________________________________________________________________
682 Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
684 // Check if the pads xy1 and iPad overlap and return overlap area
687 xy2[0] = fXyq[0][iPad] - fXyq[3][iPad];
688 xy2[1] = fXyq[0][iPad] + fXyq[3][iPad];
689 if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE;
690 xy2[2] = fXyq[1][iPad] - fXyq[4][iPad];
691 xy2[3] = fXyq[1][iPad] + fXyq[4][iPad];
692 if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE;
693 if (!iSkip) return kTRUE; // just check overlap (w/out computing the area)
694 xy12[0] = TMath::Max (xy1[0],xy2[0]);
695 xy12[1] = TMath::Min (xy1[1],xy2[1]);
696 xy12[2] = TMath::Max (xy1[2],xy2[2]);
697 xy12[3] = TMath::Min (xy1[3],xy2[3]);
701 //_____________________________________________________________________________
703 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t i, Int_t j, Float_t *xy12, Int_t iSkip)
705 // Check if the pads i and j overlap and return overlap area
707 Float_t xy1[4], xy2[4];
708 return Overlap(xy1, xy2, xy12, iSkip);
711 //_____________________________________________________________________________
712 Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
714 // Check precluster in order to attempt to simplify it (mostly for
715 // two-cathode preclusters)
718 Float_t xy1[4], xy12[4];
720 Int_t npad = fnPads[0] + fnPads[1];
722 // If pads have the same size take average of pads on both cathodes
723 Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
725 Double_t xSize = -1, ySize = 0;
726 for (Int_t i=0; i<npad; i++) {
727 if (fXyq[2][i] < 0) continue;
728 if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; }
729 if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 || TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; }
732 if (sameSize && (fnPads[0] > 2 || fnPads[1] > 2)) {
733 nShown[0] += fnPads[0];
734 nShown[1] += fnPads[1];
735 fnPads[0] = fnPads[1] = 0;
737 for (Int_t i=0; i<npad; i++) {
738 if (fXyq[2][i] < 0) continue; // used pad
739 fXyq[2][fnPads[0]] = fXyq[2][i];
741 for (Int_t j=i+1; j<npad; j++) {
742 if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode
743 if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue;
744 if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue;
745 fXyq[2][fnPads[0]] += fXyq[2][j];
750 fXyq[2][fnPads[0]] /= div;
751 fXyq[0][fnPads[0]] = fXyq[0][i];
752 fXyq[1][fnPads[0]] = fXyq[1][i];
753 fPadIJ[0][fnPads[0]++] = 0;
757 // Check if one-cathode precluster
758 i1 = fnPads[0]!=0 ? 0 : 1;
759 i2 = fnPads[1]!=0 ? 1 : 0;
761 if (i1 != i2) { // two-cathode
763 Int_t *flags = new Int_t[npad];
764 for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
766 // Check pad overlaps
767 for (Int_t i=0; i<npad; i++) {
768 if (fPadIJ[0][i] != i1) continue;
769 xy1[0] = fXyq[0][i] - fXyq[3][i];
770 xy1[1] = fXyq[0][i] + fXyq[3][i];
771 xy1[2] = fXyq[1][i] - fXyq[4][i];
772 xy1[3] = fXyq[1][i] + fXyq[4][i];
773 for (Int_t j=0; j<npad; j++) {
774 if (fPadIJ[0][j] != i2) continue;
775 if (!Overlap(xy1, j, xy12, 0)) continue;
776 flags[i] = flags[j] = 1; // mark overlapped pads
780 // Check if all pads overlap
781 Int_t digit=0, cath, nFlags=0;
782 for (Int_t i=0; i<npad; i++) {nFlags += !flags[i];}
783 if (nFlags) cout << " nFlags = " << nFlags << endl;
784 //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
786 for (Int_t i=0; i<npad; i++) {
787 if (flags[i]) continue;
788 digit = TMath::Nint (fXyq[5][i]);
790 fUsed[cath][digit] = kFALSE; // release pad
796 // Check correlations of cathode charges
797 if (fnPads[0] && fnPads[1]) { // two-cathode
799 Int_t over[2] = {1, 1};
800 for (Int_t i=0; i<npad; i++) {
802 if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
803 if (fXyq[2][i] > fResponse->MaxAdc()-1) over[cath] = 0;
805 cout << " Total charge: " << sum[0] << " " << sum[1] << endl;
806 if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
807 cout << " Release " << endl;
809 cath = sum[0]>sum[1] ? 0 : 1;
812 Double_t *dist = new Double_t[npad];
813 for (Int_t i=0; i<npad; i++) {
814 if (fPadIJ[0][i] != cath) continue;
815 if (fXyq[2][i] < cmax) continue;
819 // Arrange pads according to their distance to the max,
820 // normalized to the pad size
821 for (Int_t i=0; i<npad; i++) {
823 if (fPadIJ[0][i] != cath) continue;
824 if (i == imax) continue;
825 if (fXyq[2][i] < 0) continue;
826 dist[i] = (fXyq[0][i]-fXyq[0][imax])*(fXyq[0][i]-fXyq[0][imax])/
827 fXyq[3][imax]/fXyq[3][imax]/4;
828 dist[i] += (fXyq[1][i]-fXyq[1][imax])*(fXyq[1][i]-fXyq[1][imax])/
829 fXyq[4][imax]/fXyq[4][imax]/4;
830 dist[i] = TMath::Sqrt (dist[i]);
832 TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
835 for (Int_t i=0; i<npad; i++) {
837 if (fPadIJ[0][indx] != cath) continue;
838 if (fXyq[2][indx] < 0) continue;
839 if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax)<1.e-3) {
841 if (TMath::Abs(dist[indx]-xmax)<1.e-3)
842 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
843 else cmax = fXyq[2][indx];
845 digit = TMath::Nint (fXyq[5][indx]);
846 fUsed[cath][digit] = kFALSE;
849 // xmax = dist[i]; // Bug?
853 delete [] dist; dist = 0;
854 } // TMath::Abs(sum[0]-sum[1])...
855 } // if (fnPads[0] && fnPads[1])
856 delete [] flags; flags = 0;
859 if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
861 // Move released pads to the right
862 Int_t beg = 0, end = npad-1, padij;
865 if (fXyq[2][beg] > 0) { beg++; continue; }
866 for (Int_t j=end; j>beg; j--) {
867 if (fXyq[2][j] < 0) continue;
869 for (Int_t j1=0; j1<2; j1++) {
870 padij = fPadIJ[j1][beg];
871 fPadIJ[j1][beg] = fPadIJ[j1][j];
872 fPadIJ[j1][j] = padij;
874 for (Int_t j1=0; j1<6; j1++) {
876 fXyq[j1][beg] = fXyq[j1][j];
880 } // for (Int_t j=end;
883 npad = fnPads[0] + fnPads[1];
884 if (npad > 500) { cout << " ***** Too large cluster. Give up. " << npad << endl; return kFALSE; }
885 // Back up charge value
886 for (Int_t j=0; j<npad; j++) fXyq[5][j] = fXyq[2][j];
891 //_____________________________________________________________________________
892 void AliMUONClusterFinderAZ::BuildPixArray()
894 // Build pixel array for MLEM method
896 Int_t nPix=0, i1, i2;
897 Float_t xy1[4], xy12[4];
898 AliMUONPixel *pixPtr=0;
900 Int_t npad = fnPads[0] + fnPads[1];
902 // One cathode is empty
903 i1 = fnPads[0]!=0 ? 0 : 1;
904 i2 = fnPads[1]!=0 ? 1 : 0;
906 // Build array of pixels on anode plane
907 if (i1 == i2) { // one-cathode precluster
908 for (Int_t j=0; j<npad; j++) {
909 pixPtr = new AliMUONPixel();
910 for (Int_t i=0; i<2; i++) {
911 pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates
912 pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size
914 pixPtr->SetCharge(fXyq[2][j]); // charge
915 fPixArray->Add((TObject*)pixPtr);
918 } else { // two-cathode precluster
919 for (Int_t i=0; i<npad; i++) {
920 if (fPadIJ[0][i] != i1) continue;
921 xy1[0] = fXyq[0][i] - fXyq[3][i];
922 xy1[1] = fXyq[0][i] + fXyq[3][i];
923 xy1[2] = fXyq[1][i] - fXyq[4][i];
924 xy1[3] = fXyq[1][i] + fXyq[4][i];
925 for (Int_t j=0; j<npad; j++) {
926 if (fPadIJ[0][j] != i2) continue;
927 if (!Overlap(xy1, j, xy12, 1)) continue;
928 pixPtr = new AliMUONPixel();
929 for (Int_t k=0; k<2; k++) {
930 pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates
931 pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size
933 pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
934 fPixArray->Add((TObject*)pixPtr);
940 Float_t wxmin=999, wymin=999;
941 for (Int_t i=0; i<npad; i++) {
942 if (fPadIJ[0][i] == i1) wymin = TMath::Min (wymin,fXyq[4][i]);
943 if (fPadIJ[0][i] == i2) wxmin = TMath::Min (wxmin,fXyq[3][i]);
945 cout << wxmin << " " << wymin << endl;
947 // Check if small pixel X-size
948 AjustPixel(wxmin, 0);
949 // Check if small pixel Y-size
950 AjustPixel(wymin, 1);
951 // Check if large pixel size
952 AjustPixel(wxmin, wymin);
954 // Remove discarded pixels
955 for (Int_t i=0; i<nPix; i++) {
956 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
958 if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
960 fPixArray->Compress();
961 nPix = fPixArray->GetEntriesFast();
964 cout << nPix << endl;
965 // Too many pixels - sort and remove pixels with the lowest signal
967 for (Int_t i=npad; i<nPix; i++) {
968 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
970 fPixArray->RemoveAt(i);
974 } // if (nPix > npad)
976 // Set pixel charges to the same value (for MLEM)
977 for (Int_t i=0; i<nPix; i++) {
978 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
979 //pixPtr->SetCharge(10);
980 cout << i+1 << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(0) << " " << pixPtr->Size(1) << endl;
984 //_____________________________________________________________________________
985 void AliMUONClusterFinderAZ::AjustPixel(Float_t width, Int_t ixy)
987 // Check if some pixels have small size (ajust if necessary)
989 AliMUONPixel *pixPtr, *pixPtr1 = 0;
990 Int_t ixy1 = TMath::Even(ixy);
991 Int_t nPix = fPixArray->GetEntriesFast();
993 for (Int_t i=0; i<nPix; i++) {
994 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
995 if (pixPtr->Charge() < 1) continue; // discarded pixel
996 if (pixPtr->Size(ixy)-width < -1.e-4) {
998 cout << " Small X or Y: " << ixy << " " << pixPtr->Size(ixy) << " " << width << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << endl;
999 for (Int_t j=i+1; j<nPix; j++) {
1000 pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j);
1001 if (pixPtr1->Charge() < 1) continue; // discarded pixel
1002 if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size
1003 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns
1004 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) {
1006 pixPtr->SetSize(ixy, width);
1007 pixPtr->SetCoord(ixy, (pixPtr->Coord(ixy)+pixPtr1->Coord(ixy))/2);
1008 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
1009 pixPtr1->SetCharge(0);
1013 } // for (Int_t j=i+1;
1014 //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ???
1015 //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) {
1016 if (pixPtr1 || i == nPix-1) {
1017 // edge pixel - just increase its size
1018 cout << " Edge ..." << endl;
1019 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1020 // ???if (fPadIJ[0][j] != i1) continue;
1021 if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue;
1022 if (pixPtr->Coord(ixy) < fXyq[ixy][j])
1023 pixPtr->Shift(ixy, -pixPtr->Size(ixy));
1024 else pixPtr->Shift(ixy, pixPtr->Size(ixy));
1025 pixPtr->SetSize(ixy, width);
1029 } // if (pixPtr->Size(ixy)-width < -1.e-4)
1030 } // for (Int_t i=0; i<nPix;
1034 //_____________________________________________________________________________
1035 void AliMUONClusterFinderAZ::AjustPixel(Float_t wxmin, Float_t wymin)
1037 // Check if some pixels have large size (ajust if necessary)
1040 Int_t nPix = fPixArray->GetEntriesFast();
1041 AliMUONPixel *pixPtr, *pixPtr1, pix;
1043 // Check if large pixel size
1044 for (Int_t i=0; i<nPix; i++) {
1045 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1046 if (pixPtr->Charge() < 1) continue; // discarded pixel
1047 if (pixPtr->Size(0)-wxmin > 1.e-4 || pixPtr->Size(1)-wymin > 1.e-4) {
1048 cout << " Different " << pixPtr->Size(0) << " " << wxmin << " " << pixPtr->Size(1) << " " << wymin << endl;
1050 nx = TMath::Nint (pix.Size(0)/wxmin);
1051 ny = TMath::Nint (pix.Size(1)/wymin);
1052 pix.Shift(0, -pix.Size(0)-wxmin);
1053 pix.Shift(1, -pix.Size(1)-wymin);
1054 pix.SetSize(0, wxmin);
1055 pix.SetSize(1, wymin);
1056 for (Int_t ii=0; ii<nx; ii++) {
1057 pix.Shift(0, wxmin*2);
1058 for (Int_t jj=0; jj<ny; jj++) {
1059 pix.Shift(1, wymin*2);
1060 pixPtr1 = new AliMUONPixel(pix);
1061 fPixArray->Add((TObject*)pixPtr1);
1064 pixPtr->SetCharge(0);
1066 } // for (Int_t i=0; i<nPix;
1070 //_____________________________________________________________________________
1071 Bool_t AliMUONClusterFinderAZ::MainLoop()
1073 // Repeat MLEM algorithm until pixel size becomes sufficiently small
1078 //Int_t nn, xList[10], yList[10];
1079 Int_t nPix = fPixArray->GetEntriesFast();
1080 Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0;
1081 AliMUONPixel *pixPtr = 0;
1082 Double_t *coef = 0, *probi = 0;
1083 for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++;
1087 mlem = (TH2D*) gROOT->FindObject("mlem");
1088 if (mlem) mlem->Delete();
1089 // Calculate coefficients
1090 cout << " nPix, npadTot, npadOK " << nPix << " " << npadTot << " " << npadOK << endl;
1092 // Calculate coefficients and pixel visibilities
1093 coef = new Double_t [npadTot*nPix];
1094 probi = new Double_t [nPix];
1095 Int_t indx = 0, cath;
1096 for (Int_t ipix=0; ipix<nPix; ipix++) {
1097 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1099 for (Int_t j=0; j<npadTot; j++) {
1100 if (fPadIJ[1][j] < 0) { coef[j*nPix+ipix] = 0; continue; }
1101 cath = fPadIJ[0][j];
1102 fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
1103 fSegmentation[cath]->SetPad(ix,iy);
1105 fSegmentation[cath]->Neighbours(ix,iy,&nn,xList,yList);
1108 for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
1113 fSegmentation[cath]->SetHit(pixPtr->Coord(0),pixPtr->Coord(1),fZpad);
1114 sum += fResponse->IntXY(fSegmentation[cath]);
1115 indx = j*nPix + ipix;
1117 probi[ipix] += coef[indx];
1118 //cout << j << " " << ipix << " " << coef[indx] << endl;
1119 } // for (Int_t j=0;
1120 //cout << " prob: " << probi[ipix] << endl;
1121 if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel
1122 } // for (Int_t ipix=0;
1127 Double_t xylim[4] = {999, 999, 999, 999};
1128 for (Int_t ipix=0; ipix<nPix; ipix++) {
1129 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1130 for (Int_t i=0; i<4; i++)
1131 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1132 //cout << ipix+1; pixPtr->Print();
1134 for (Int_t i=0; i<4; i++) {
1135 xylim[i] -= pixPtr->Size(i/2); cout << (i%2 ? -1 : 1)*xylim[i] << " "; }
1138 // Ajust histogram to approximately the same limits as for the pads
1139 // (for good presentation)
1143 xypads[0] = fHist[0]->GetXaxis()->GetXmin();
1144 xypads[1] = -fHist[0]->GetXaxis()->GetXmax();
1145 xypads[2] = fHist[0]->GetYaxis()->GetXmin();
1146 xypads[3] = -fHist[0]->GetYaxis()->GetXmax();
1147 for (Int_t i=0; i<4; i++) {
1149 if (xylim[i] < xypads[i]) break;
1150 xylim[i] -= 2*pixPtr->Size(i/2);
1156 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1157 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1158 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1159 for (Int_t ipix=0; ipix<nPix; ipix++) {
1160 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1161 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
1163 //gPad->GetCanvas()->cd(3);
1165 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1168 mlem->Draw("lego1Fb");
1173 // Check if the total charge of pixels is too low
1175 for (Int_t i=0; i<nPix; i++) {
1176 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1177 qTot += pixPtr->Charge();
1179 if (qTot < 1.e-4 || npadOK < 3 && qTot < 50) {
1180 delete [] coef; delete [] probi; coef = 0; probi = 0;
1181 fPixArray->Delete();
1185 // Plot data - expectation
1187 Double_t x, y, cont;
1188 for (Int_t j=0; j<npadTot; j++) {
1190 for (Int_t i=0; i<nPix; i++) {
1191 // Caculate expectation
1192 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1193 sum1 += pixPtr->Charge()*coef[j*nPix+i];
1195 sum1 = TMath::Min (sum1,(Double_t)fResponse->MaxAdc());
1198 cath = fPadIJ[0][j];
1199 Int_t ihist = cath*2;
1200 ix = fHist[ihist]->GetXaxis()->FindBin(x);
1201 iy = fHist[ihist]->GetYaxis()->FindBin(y);
1202 cont = fHist[ihist]->GetCellContent(ix,iy);
1203 if (cont == 0 && fHist[ihist+1]) {
1205 ix = fHist[ihist]->GetXaxis()->FindBin(x);
1206 iy = fHist[ihist]->GetYaxis()->FindBin(y);
1208 fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
1210 ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
1211 //gPad->SetTheta(55);
1213 //mlem->Draw("lego1");
1215 ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
1219 // Calculate position of the center-of-gravity around the maximum pixel
1221 FindCOG(mlem, xyCOG);
1223 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break;
1224 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) {
1225 // Sort pixels according to the charge
1228 for (Int_t i=0; i<nPix; i++) {
1229 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1230 cout << i+1; pixPtr->Print();
1233 Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
1234 pixMin = TMath::Min (pixMin,50.);
1236 // Decrease pixel size and shift pixels to make them centered at
1238 indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1239 Double_t width = 0, shift[2]={0};
1241 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1242 Int_t nPix1 = nPix; nPix = 0;
1243 for (Int_t ipix=0; ipix<nPix1; ipix++) {
1244 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1245 if (nPix >= npadOK) { // too many pixels already
1246 fPixArray->RemoveAt(ipix);
1250 if (pixPtr->Charge() < pixMin) { // low charge
1251 fPixArray->RemoveAt(ipix);
1255 for (Int_t i=0; i<2; i++) {
1257 pixPtr->SetCharge(10);
1258 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1259 width = -pixPtr->Size(indx);
1260 pixPtr->Shift(indx, width);
1261 // Shift pixel position
1264 for (Int_t j=0; j<2; j++) {
1265 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1266 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1268 //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1270 pixPtr->Shift(0, -shift[0]);
1271 pixPtr->Shift(1, -shift[1]);
1273 pixPtr = new AliMUONPixel(*pixPtr);
1274 pixPtr->Shift(indx, -2*width);
1275 fPixArray->Add((TObject*)pixPtr);
1278 for (Int_t i=0; i<4; i++)
1279 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1280 } // for (Int_t i=0; i<2;
1282 } // for (Int_t ipix=0;
1284 fPixArray->Compress();
1285 nPix = fPixArray->GetEntriesFast();
1287 // Remove excessive pixels
1288 if (nPix > npadOK) {
1289 for (Int_t ipix=npadOK; ipix<nPix; ipix++) {
1290 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1291 fPixArray->RemoveAt(ipix);
1295 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1296 // add pixels if the maximum is at the limit of pixel area
1297 // start from Y-direction
1299 for (Int_t i=3; i>-1; i--) {
1300 if (nPix < npadOK &&
1301 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) {
1302 pixPtr = new AliMUONPixel(*pixPtr);
1303 pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1304 j = TMath::Even (i/2);
1305 pixPtr->SetCoord(j, xyCOG[j]);
1306 fPixArray->Add((TObject*)pixPtr);
1312 fPixArray->Compress();
1313 nPix = fPixArray->GetEntriesFast();
1314 delete [] coef; delete [] probi; coef = 0; probi = 0;
1317 // remove pixels with low signal or low visibility
1318 // Cuts are empirical !!!
1319 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1320 thresh = TMath::Min (thresh,50.);
1321 Double_t cmax = -1, charge = 0;
1322 for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]);
1323 // Mark pixels which should be removed
1324 for (Int_t i=0; i<nPix; i++) {
1325 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1326 charge = pixPtr->Charge();
1327 if (charge < thresh) pixPtr->SetCharge(-charge);
1328 else if (cmax > 1.91) {
1329 if (probi[i] < 1.9) pixPtr->SetCharge(-charge);
1331 else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
1333 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1335 for (Int_t i=0; i<nPix; i++) {
1336 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1337 charge = pixPtr->Charge();
1338 if (charge > 0) continue;
1339 near = FindNearest(pixPtr);
1340 pixPtr->SetCharge(0);
1341 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near);
1342 pixPtr->SetCharge(pixPtr->Charge() - charge);
1345 for (Int_t i=0; i<nPix; i++) {
1346 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1347 ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1348 iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1349 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1352 ((TCanvas*)gROOT->FindObject("c2"))->cd();
1355 mlem->Draw("lego1Fb");
1359 fxyMu[0][6] = fxyMu[1][6] = 9999;
1360 // Try to split into clusters
1362 if (mlem->GetSum() < 1) ok = kFALSE;
1363 else Split(mlem, coef);
1364 delete [] coef; delete [] probi; coef = 0; probi = 0;
1365 fPixArray->Delete();
1369 //_____________________________________________________________________________
1370 void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi)
1372 // Use MLEM to find pixel charges
1374 Int_t nPix = fPixArray->GetEntriesFast();
1375 Int_t npad = fnPads[0] + fnPads[1];
1376 Double_t *probi1 = new Double_t [nPix];
1378 AliMUONPixel *pixPtr;
1380 for (Int_t iter=0; iter<15; iter++) {
1382 for (Int_t ipix=0; ipix<nPix; ipix++) {
1383 // Correct each pixel
1384 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1386 probi1[ipix] = probi[ipix];
1387 for (Int_t j=0; j<npad; j++) {
1388 if (fPadIJ[1][j] < 0) continue;
1391 indx = indx1 + ipix;
1392 for (Int_t i=0; i<nPix; i++) {
1393 // Caculate expectation
1394 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1395 sum1 += pixPtr->Charge()*coef[indx1+i];
1396 } // for (Int_t i=0;
1397 if (fXyq[2][j] > fResponse->MaxAdc()-1 && sum1 > fResponse->MaxAdc()) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows
1398 //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl;
1399 if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1;
1400 } // for (Int_t j=0;
1401 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1402 if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1403 } // for (Int_t ipix=0;
1404 } // for (Int_t iter=0;
1409 //_____________________________________________________________________________
1410 void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1412 // Calculate position of the center-of-gravity around the maximum pixel
1414 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1415 Int_t i1 = -9, j1 = -9;
1416 mlem->GetMaximumBin(ixmax,iymax,ix);
1417 Int_t nx = mlem->GetNbinsX();
1418 Int_t ny = mlem->GetNbinsY();
1419 Double_t thresh = mlem->GetMaximum()/10;
1420 Double_t x, y, cont, xq=0, yq=0, qq=0;
1422 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1423 y = mlem->GetYaxis()->GetBinCenter(i);
1424 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1425 cont = mlem->GetCellContent(j,i);
1426 if (cont < thresh) continue;
1427 if (i != i1) {i1 = i; nsumy++;}
1428 if (j != j1) {j1 = j; nsumx++;}
1429 x = mlem->GetXaxis()->GetBinCenter(j);
1438 Int_t i2 = 0, j2 = 0;
1441 // one bin in Y - add one more (with the largest signal)
1442 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1443 if (i == iymax) continue;
1444 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1445 cont = mlem->GetCellContent(j,i);
1448 x = mlem->GetXaxis()->GetBinCenter(j);
1449 y = mlem->GetYaxis()->GetBinCenter(i);
1458 if (i2 != i1) nsumy++;
1459 if (j2 != j1) nsumx++;
1461 } // if (nsumy == 1)
1464 // one bin in X - add one more (with the largest signal)
1466 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1467 if (j == ixmax) continue;
1468 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1469 cont = mlem->GetCellContent(j,i);
1472 x = mlem->GetXaxis()->GetBinCenter(j);
1473 y = mlem->GetYaxis()->GetBinCenter(i);
1482 if (i2 != i1) nsumy++;
1483 if (j2 != j1) nsumx++;
1485 } // if (nsumx == 1)
1487 xyc[0] = xq/qq; xyc[1] = yq/qq;
1488 cout << xyc[0] << " " << xyc[1] << " " << qq << " " << nsum << " " << nsumx << " " << nsumy << endl;
1492 //_____________________________________________________________________________
1493 Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1495 // Find the pixel nearest to the given one
1496 // (algorithm may be not very efficient)
1498 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1499 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1500 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1501 AliMUONPixel *pixPtr;
1503 for (Int_t i=0; i<nPix; i++) {
1504 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1505 if (pixPtr->Charge() < 0.5) continue;
1506 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1507 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1508 r = dx *dx + dy * dy;
1509 if (r < rmin) { rmin = r; imin = i; }
1514 //_____________________________________________________________________________
1515 void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
1517 // The main steering function to work with clusters of pixels in anode
1518 // plane (find clusters, decouple them from each other, merge them (if
1519 // necessary), pick up coupled pads, call the fitting function)
1521 Int_t nx = mlem->GetNbinsX();
1522 Int_t ny = mlem->GetNbinsY();
1523 Int_t nPix = fPixArray->GetEntriesFast();
1525 Bool_t *used = new Bool_t[ny*nx];
1527 Int_t nclust = 0, indx, indx1;
1529 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1531 TObjArray *clusters[200]={0};
1534 // Find clusters of histogram bins (easier to work in 2-D space)
1535 for (Int_t i=1; i<=ny; i++) {
1536 for (Int_t j=1; j<=nx; j++) {
1537 indx = (i-1)*nx + j - 1;
1538 if (used[indx]) continue;
1539 cont = mlem->GetCellContent(j,i);
1540 if (cont < 0.5) continue;
1541 pix = new TObjArray(20);
1543 pix->Add(BinToPix(mlem,j,i));
1544 AddBin(mlem, i, j, 0, used, pix); // recursive call
1545 clusters[nclust++] = pix;
1546 if (nclust > 200) { cout << " Too many clusters " << endl; ::exit(0); }
1547 } // for (Int_t j=1; j<=nx; j++) {
1548 } // for (Int_t i=1; i<=ny;
1549 cout << nclust << endl;
1550 delete [] used; used = 0;
1552 // Compute couplings between clusters and clusters to pads
1553 Int_t npad = fnPads[0] + fnPads[1];
1555 // Exclude pads with overflows
1556 for (Int_t j=0; j<npad; j++) {
1557 if (fXyq[2][j] > fResponse->MaxAdc()-1) fPadIJ[1][j] = -9;
1558 else fPadIJ[1][j] = 0;
1561 // Compute couplings of clusters to pads
1562 TMatrixD *aij_clu_pad = new TMatrixD(nclust,npad);
1565 for (Int_t iclust=0; iclust<nclust; iclust++) {
1566 pix = clusters[iclust];
1567 npxclu = pix->GetEntriesFast();
1568 for (Int_t i=0; i<npxclu; i++) {
1569 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
1570 for (Int_t j=0; j<npad; j++) {
1571 // Exclude overflows
1572 if (fPadIJ[1][j] < 0) continue;
1573 if (coef[j*nPix+indx] < kCouplMin) continue;
1574 (*aij_clu_pad)(iclust,j) += coef[j*nPix+indx];
1578 // Compute couplings between clusters
1579 TMatrixD *aij_clu_clu = new TMatrixD(nclust,nclust);
1581 for (Int_t iclust=0; iclust<nclust; iclust++) {
1582 for (Int_t j=0; j<npad; j++) {
1583 // Exclude overflows
1584 if (fPadIJ[1][j] < 0) continue;
1585 if ((*aij_clu_pad)(iclust,j) < kCouplMin) continue;
1586 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1587 if ((*aij_clu_pad)(iclust1,j) < kCouplMin) continue;
1588 (*aij_clu_clu)(iclust,iclust1) +=
1589 TMath::Sqrt ((*aij_clu_pad)(iclust,j)*(*aij_clu_pad)(iclust1,j));
1593 for (Int_t iclust=0; iclust<nclust; iclust++) {
1594 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1595 (*aij_clu_clu)(iclust1,iclust) = (*aij_clu_clu)(iclust,iclust1);
1599 if (nclust > 1) aij_clu_clu->Print();
1601 // Find groups of coupled clusters
1602 used = new Bool_t[nclust];
1603 for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
1604 Int_t *clustNumb = new Int_t[nclust];
1605 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
1608 for (Int_t igroup=0; igroup<nclust; igroup++) {
1609 if (used[igroup]) continue;
1610 used[igroup] = kTRUE;
1611 clustNumb[0] = igroup;
1613 // Find group of coupled clusters
1614 AddCluster(igroup, nclust, aij_clu_clu, used, clustNumb, nCoupled); // recursive
1615 cout << " nCoupled: " << nCoupled << endl;
1616 for (Int_t i=0; i<nCoupled; i++) cout << clustNumb[i] << " "; cout << endl;
1618 while (nCoupled > 0) {
1622 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
1624 // Too many coupled clusters to fit - try to decouple them
1625 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
1626 // all the others in the group
1627 for (Int_t j=0; j<3; j++) minGroup[j] = -1;
1628 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aij_clu_clu, minGroup);
1630 // Flag clusters for fit
1632 while (minGroup[nForFit] >= 0 && nForFit < 3) {
1633 cout << clustNumb[minGroup[nForFit]] << " ";
1634 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1635 clustNumb[minGroup[nForFit]] -= 999;
1638 cout << nForFit << " " << coupl << endl;
1641 // Select pads for fit.
1642 if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aij_clu_pad) < 3 && nCoupled > 1) {
1644 for (Int_t j=0; j<npad; j++) if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0;
1645 // Merge the failed cluster candidates (with too few pads to fit) with
1646 // the one with the strongest coupling
1647 Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aij_clu_clu, aij_clu_pad);
1650 nfit = Fit(nForFit, clustFit, clusters, parOk);
1653 // Subtract the fitted charges from pads with strong coupling and/or
1654 // return pads for further use
1655 UpdatePads(nfit, parOk);
1658 for (Int_t j=0; j<npad; j++) {if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;}
1660 // Sort the clusters (move to the right the used ones)
1661 Int_t beg = 0, end = nCoupled - 1;
1663 if (clustNumb[beg] >= 0) { beg++; continue; }
1664 for (Int_t j=end; j>beg; j--) {
1665 if (clustNumb[j] < 0) continue;
1667 indx = clustNumb[beg];
1668 clustNumb[beg] = clustNumb[j];
1669 clustNumb[j] = indx;
1675 nCoupled -= nForFit;
1677 // Remove couplings of used clusters
1678 for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) {
1679 indx = clustNumb[iclust] + 999;
1680 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1681 indx1 = clustNumb[iclust1];
1682 (*aij_clu_clu)(indx,indx1) = (*aij_clu_clu)(indx1,indx) = 0;
1686 // Update the remaining clusters couplings (exclude couplings from
1688 for (Int_t j=0; j<npad; j++) {
1689 if (fPadIJ[1][j] != -1) continue;
1690 for (Int_t iclust=0; iclust<nCoupled; iclust++) {
1691 indx = clustNumb[iclust];
1692 if ((*aij_clu_pad)(indx,j) < kCouplMin) continue;
1693 for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1694 indx1 = clustNumb[iclust1];
1695 if ((*aij_clu_pad)(indx1,j) < kCouplMin) continue;
1697 (*aij_clu_clu)(indx,indx1) -=
1698 TMath::Sqrt ((*aij_clu_pad)(indx,j)*(*aij_clu_pad)(indx1,j));
1699 (*aij_clu_clu)(indx1,indx) = (*aij_clu_clu)(indx,indx1);
1703 } // for (Int_t j=0; j<npad;
1704 } // if (nCoupled > 3)
1705 } // while (nCoupled > 0)
1706 } // for (Int_t igroup=0; igroup<nclust;
1708 //delete aij_clu; aij_clu = 0; delete aij_clu_pad; aij_clu_pad = 0;
1709 aij_clu_clu->Delete(); aij_clu_pad->Delete();
1710 for (Int_t iclust=0; iclust<nclust; iclust++) {
1711 pix = clusters[iclust];
1713 delete pix; pix = 0;
1715 delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1718 //_____________________________________________________________________________
1719 void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1721 // Add a bin to the cluster
1723 Int_t nx = mlem->GetNbinsX();
1724 Int_t ny = mlem->GetNbinsY();
1725 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
1726 AliMUONPixel *pixPtr = 0;
1728 for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
1729 for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
1730 if (i != ic && j != jc) continue;
1731 if (used[(i-1)*nx+j-1]) continue;
1732 cont1 = mlem->GetCellContent(j,i);
1733 if (mode && cont1 > cont) continue;
1734 used[(i-1)*nx+j-1] = kTRUE;
1735 if (cont1 < 0.5) continue;
1736 if (pix) pix->Add(BinToPix(mlem,j,i));
1738 pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j),
1739 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1740 fPixArray->Add((TObject*)pixPtr);
1742 AddBin(mlem, i, j, mode, used, pix); // recursive call
1747 //_____________________________________________________________________________
1748 TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1750 // Translate histogram bin to pixel
1752 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1753 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1755 Int_t nPix = fPixArray->GetEntriesFast();
1756 AliMUONPixel *pixPtr;
1758 // Compare pixel and bin positions
1759 for (Int_t i=0; i<nPix; i++) {
1760 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1761 if (pixPtr->Charge() < 0.5) continue;
1762 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr;
1764 cout << " Something wrong ??? " << endl;
1768 //_____________________________________________________________________________
1769 void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aij_clu_clu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
1771 // Add a cluster to the group of coupled clusters
1773 for (Int_t i=0; i<nclust; i++) {
1774 if (used[i]) continue;
1775 if ((*aij_clu_clu)(i,ic) < kCouplMin) continue;
1777 clustNumb[nCoupled++] = i;
1778 AddCluster(i, nclust, aij_clu_clu, used, clustNumb, nCoupled);
1782 //_____________________________________________________________________________
1783 Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aij_clu_clu, Int_t *minGroup)
1785 // Find group of clusters with minimum coupling to all the others
1787 Int_t i123max = TMath::Min(3,nCoupled/2);
1788 Int_t indx, indx1, indx2, indx3, nTot = 0;
1789 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1791 for (Int_t i123=1; i123<=i123max; i123++) {
1794 coupl1 = new Double_t [nCoupled];
1795 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1797 else if (i123 == 2) {
1798 nTot = nCoupled*nCoupled;
1799 coupl2 = new Double_t [nTot];
1800 for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1802 nTot = nTot*nCoupled;
1803 coupl3 = new Double_t [nTot];
1804 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1807 for (Int_t i=0; i<nCoupled; i++) {
1808 indx1 = clustNumb[i];
1809 for (Int_t j=i+1; j<nCoupled; j++) {
1810 indx2 = clustNumb[j];
1812 coupl1[i] += (*aij_clu_clu)(indx1,indx2);
1813 coupl1[j] += (*aij_clu_clu)(indx1,indx2);
1815 else if (i123 == 2) {
1816 indx = i*nCoupled + j;
1817 coupl2[indx] = coupl1[i] + coupl1[j];
1818 coupl2[indx] -= 2 * ((*aij_clu_clu)(indx1,indx2));
1820 for (Int_t k=j+1; k<nCoupled; k++) {
1821 indx3 = clustNumb[k];
1822 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1823 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1824 coupl3[indx] -= 2 * ((*aij_clu_clu)(indx1,indx3)+(*aij_clu_clu)(indx2,indx3));
1827 } // for (Int_t j=i+1;
1828 } // for (Int_t i=0;
1829 } // for (Int_t i123=1;
1831 // Find minimum coupling
1832 Double_t couplMin = 9999;
1835 for (Int_t i123=1; i123<=i123max; i123++) {
1837 locMin = TMath::LocMin(nCoupled, coupl1);
1838 couplMin = coupl1[locMin];
1839 minGroup[0] = locMin;
1840 delete [] coupl1; coupl1 = 0;
1842 else if (i123 == 2) {
1843 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1844 if (coupl2[locMin] < couplMin) {
1845 couplMin = coupl2[locMin];
1846 minGroup[0] = locMin/nCoupled;
1847 minGroup[1] = locMin%nCoupled;
1849 delete [] coupl2; coupl2 = 0;
1851 locMin = TMath::LocMin(nTot, coupl3);
1852 if (coupl3[locMin] < couplMin) {
1853 couplMin = coupl3[locMin];
1854 minGroup[0] = locMin/nCoupled/nCoupled;
1855 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1856 minGroup[2] = locMin%nCoupled;
1858 delete [] coupl3; coupl3 = 0;
1860 } // for (Int_t i123=1;
1864 //_____________________________________________________________________________
1865 Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aij_clu_pad)
1867 // Select pads for fit. If too many coupled clusters, find pads giving
1868 // the strongest coupling with the rest of clusters and exclude them from the fit.
1870 Int_t npad = fnPads[0] + fnPads[1];
1871 Double_t *pad_pix = 0;
1874 pad_pix = new Double_t[npad];
1875 for (Int_t i=0; i<npad; i++) pad_pix[i] = 0;
1878 Int_t nOK = 0, indx, indx1;
1879 for (Int_t iclust=0; iclust<nForFit; iclust++) {
1880 indx = clustFit[iclust];
1881 for (Int_t j=0; j<npad; j++) {
1882 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1883 if ((*aij_clu_pad)(indx,j) < kCouplMin) continue;
1884 fPadIJ[1][j] = 1; // pad to be used in fit
1887 // Check other clusters
1888 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1889 indx1 = clustNumb[iclust1];
1890 if (indx1 < 0) continue;
1891 if ((*aij_clu_pad)(indx1,j) < kCouplMin) continue;
1892 pad_pix[j] += (*aij_clu_pad)(indx1,j);
1894 } // if (nCoupled > 3)
1895 } // for (Int_t j=0; j<npad;
1896 } // for (Int_t iclust=0; iclust<nForFit
1897 if (nCoupled < 4) return nOK;
1900 for (Int_t j=0; j<npad; j++) {
1901 if (pad_pix[j] < kCouplMin) continue;
1902 cout << j << " " << pad_pix[j] << " ";
1903 cout << fXyq[0][j] << " " << fXyq[1][j] << endl;
1905 fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1908 delete [] pad_pix; pad_pix = 0;
1912 //_____________________________________________________________________________
1913 void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aij_clu_clu, TMatrixD *aij_clu_pad)
1915 // Merge the group of clusters with the one having the strongest coupling with them
1917 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1918 TObjArray *pix, *pix1;
1921 for (Int_t icl=0; icl<nForFit; icl++) {
1922 indx = clustFit[icl];
1923 pix = clusters[indx];
1924 npxclu = pix->GetEntriesFast();
1926 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1927 indx1 = clustNumb[icl1];
1928 if (indx1 < 0) continue;
1929 if ((*aij_clu_clu)(indx,indx1) > couplMax) {
1930 couplMax = (*aij_clu_clu)(indx,indx1);
1933 } // for (Int_t icl1=0;
1934 /*if (couplMax < kCouplMin) {
1935 cout << " Oops " << couplMax << endl;
1936 aij_clu_clu->Print();
1937 cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1941 pix1 = clusters[imax];
1942 npxclu1 = pix1->GetEntriesFast();
1944 for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
1945 cout << " New number of pixels: " << npxclu1 << " " << pix1->GetEntriesFast() << endl;
1946 //Add cluster-to-cluster couplings
1947 //aij_clu_clu->Print();
1948 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1949 indx1 = clustNumb[icl1];
1950 if (indx1 < 0 || indx1 == imax) continue;
1951 (*aij_clu_clu)(indx1,imax) += (*aij_clu_clu)(indx,indx1);
1952 (*aij_clu_clu)(imax,indx1) = (*aij_clu_clu)(indx1,imax);
1954 (*aij_clu_clu)(indx,imax) = (*aij_clu_clu)(imax,indx) = 0;
1955 //aij_clu_clu->Print();
1956 //Add cluster-to-pad couplings
1957 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1958 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1959 (*aij_clu_pad)(imax,j) += (*aij_clu_pad)(indx,j);
1960 (*aij_clu_pad)(indx,j) = 0;
1962 } // for (Int_t icl=0; icl<nForFit;
1965 //_____________________________________________________________________________
1966 Int_t AliMUONClusterFinderAZ::Fit(Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
1968 // Find selected clusters to selected pad charges
1970 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
1971 //Int_t nx = mlem->GetNbinsX();
1972 //Int_t ny = mlem->GetNbinsY();
1973 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
1974 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
1975 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
1976 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
1977 //Double_t qmin = 0, qmax = 1;
1978 Double_t step[3]={0.01,0.002,0.02};
1980 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8];
1984 // Number of pads to use
1986 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {if (fPadIJ[1][i] == 1) npads++;}
1987 for (Int_t i=0; i<nfit; i++) {cout << i+1 << " " << clustFit[i] << " ";}
1988 cout << nfit << endl;
1989 cout << " Number of pads to fit: " << npads << endl;
1992 if (npads < 2) return 0;
1994 // Take cluster maxima as fitting seeds
1995 AliMUONPixel *pixPtr;
1996 Double_t xyseed[3][2], qseed[3];
1997 for (Int_t ifit=1; ifit<=nfit; ifit++) {
1999 pix = clusters[clustFit[ifit-1]];
2000 npxclu = pix->GetEntriesFast();
2001 for (Int_t clu=0; clu<npxclu; clu++) {
2002 pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu);
2003 cont = pixPtr->Charge();
2007 xseed = pixPtr->Coord(0);
2008 yseed = pixPtr->Coord(1);
2011 xyseed[ifit-1][0] = xseed;
2012 xyseed[ifit-1][1] = yseed;
2013 qseed[ifit-1] = cmax;
2014 } // for (Int_t ifit=1;
2016 Int_t nDof, maxSeed[3];
2017 Double_t fmin, chi2o = 9999, chi2n;
2019 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
2020 // lower, try 3-track (if number of pads is sufficient).
2022 TMath::Sort(nfit, qseed, maxSeed, kTRUE); // in decreasing order
2023 nfit = TMath::Min (nfit, (npads + 1) / 3);
2025 Double_t *gin = 0, func0, func1, param[8], param0[2][8], deriv[2][8], step0[8];
2026 Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
2027 Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
2028 Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
2030 for (Int_t iseed=0; iseed<nfit; iseed++) {
2032 for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j];
2033 param[fNpar] = xyseed[maxSeed[iseed]][0];
2034 parmin[fNpar] = xmin;
2035 parmax[fNpar++] = xmax;
2036 param[fNpar] = xyseed[maxSeed[iseed]][1];
2037 parmin[fNpar] = ymin;
2038 parmax[fNpar++] = ymax;
2040 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
2042 parmax[fNpar++] = 1;
2045 // Try new algorithm
2046 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
2050 fcn1(fNpar, gin, func0, param, 1); nCall++;
2051 //cout << " Func: " << func0 << endl;
2054 for (Int_t j=0; j<fNpar; j++) {
2055 param0[max][j] = param[j];
2056 delta[j] = step0[j];
2057 param[j] += delta[j] / 10;
2058 if (j > 0) param[j-1] -= delta[j-1] / 10;
2059 fcn1(fNpar, gin, func1, param, 1); nCall++;
2060 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
2061 //cout << j << " " << deriv[max][j] << endl;
2062 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
2063 (param0[0][j] - param0[1][j]) : 0; // second derivative
2065 param[fNpar-1] -= delta[fNpar-1] / 10;
2066 if (nCall > 2000) ::exit(0);
2068 min = func2[0] < func2[1] ? 0 : 1;
2069 nFail = min == max ? 0 : nFail + 1;
2071 stepMax = derMax = estim = 0;
2072 for (Int_t j=0; j<fNpar; j++) {
2073 // Estimated distance to minimum
2075 if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
2076 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0;
2077 else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
2078 || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) {
2079 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
2081 if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2085 shift[j] = -deriv[min][j] / dder[j];
2088 if (TMath::Abs(shift[j])/step0[j] > estim) {
2089 estim = TMath::Abs(shift[j])/step0[j];
2094 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
2096 // Failed to improve minimum
2099 param[j] = param0[min][j];
2100 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2;
2101 else shift[j] /= -2;
2105 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
2106 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
2108 // Introduce step relaxation factor
2109 if (memory[j] < 3) {
2110 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
2111 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
2112 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
2114 param[j] += shift[j];
2116 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
2117 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
2118 if (TMath::Abs(deriv[min][j]) > derMax) {
2120 derMax = TMath::Abs (deriv[min][j]);
2122 } // for (Int_t j=0; j<fNpar;
2123 //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl;
2124 if (estim < 1 && derMax < 2 || nLoop > 100) break; // minimum was found
2127 // Check for small step
2128 if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; }
2129 if (!memory[idMax] && derMax > 0.5 && nLoop > 10) {
2130 //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl;
2131 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) {
2132 if (min == max) dder[idMax] = -dder[idMax];
2133 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
2134 param[idMax] += shift[idMax];
2135 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
2136 //cout << shift[idMax] << " " << param[idMax] << endl;
2137 if (min == max) shiftSave = shift[idMax];
2140 param[idMax] -= shift[idMax];
2141 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
2142 param[idMax] += shift[idMax];
2143 //cout << shift[idMax] << endl;
2149 nDof = npads - fNpar;
2150 chi2n = nDof ? fmin/nDof : 0;
2152 if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
2153 // Save parameters and errors
2154 for (Int_t i=0; i<fNpar; i++) {
2155 parOk[i] = param0[min][i];
2159 cout << chi2o << " " << chi2n << endl;
2161 if (fmin < 0.1) break; // !!!???
2162 } // for (Int_t iseed=0;
2164 for (Int_t i=0; i<fNpar; i++) {
2165 if (i == 4 || i == 7) continue;
2166 cout << parOk[i] << " " << errOk[i] << endl;
2168 nfit = (fNpar + 1) / 3;
2172 for (Int_t j=0; j<nfit; j++) {
2173 indx = j<2 ? j*2 : j*2+1;
2174 AddRawCluster (parOk[indx], parOk[indx+1], errOk[indx]);
2178 for (Int_t i=0; i<fnMu; i++) {
2180 for (Int_t j=0; j<nfit; j++) {
2181 indx = j<2 ? j*2 : j*2+1;
2182 rad = (fxyMu[i][0]-parOk[indx])*(fxyMu[i][0]-parOk[indx]) +
2183 (fxyMu[i][1]-parOk[indx+1])*(fxyMu[i][1]-parOk[indx+1]);
2188 fxyMu[i][2] = parOk[imax] - fxyMu[i][0];
2189 fxyMu[i][4] = parOk[imax+1] - fxyMu[i][1];
2190 fxyMu[i][3] = errOk[imax];
2191 fxyMu[i][5] = errOk[imax+1];
2198 //_____________________________________________________________________________
2199 void fcn1(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
2201 // Fit for one track
2202 AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);
2204 Int_t cath, ix, iy, indx, npads=0;
2205 Double_t charge, delta, coef=0, chi2=0;
2206 for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) {
2207 if (c.fPadIJ[1][j] != 1) continue;
2208 cath = c.fPadIJ[0][j];
2210 c.fSegmentation[cath]->GetPadI(c.fXyq[0][j],c.fXyq[1][j],c.fZpad,ix,iy);
2211 c.fSegmentation[cath]->SetPad(ix,iy);
2213 for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2214 indx = i<2 ? 2*i : 2*i+1;
2215 c.fSegmentation[cath]->SetHit(par[indx],par[indx+1],c.fZpad);
2216 //charge += c.fResponse->IntXY(c.fSegmentation[cath])*par[icl*3+2];
2217 if (c.fNpar == 2) coef = 1;
2218 else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef;
2219 //coef = TMath::Max (coef, 0.);
2220 if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2221 //coef = TMath::Max (coef, 0.);
2222 charge += c.fResponse->IntXY(c.fSegmentation[cath])*coef;
2225 //if (c.fXyq[2][j] > c.fResponse->MaxAdc()-1 && charge >
2226 // c.fResponse->MaxAdc()) charge = c.fResponse->MaxAdc();
2227 delta = charge - c.fXyq[2][j];
2228 delta /= TMath::Sqrt ((Double_t)c.fXyq[2][j]);
2229 //chi2 += TMath::Abs(delta);
2230 chi2 += delta*delta;
2231 } // for (Int_t j=0;
2233 Double_t qAver = c.fQtot/npads; //(c.fnPads[0]+c.fnPads[1]);
2237 //_____________________________________________________________________________
2238 void AliMUONClusterFinderAZ::UpdatePads(Int_t nfit, Double_t *par)
2240 // Subtract the fitted charges from pads with strong coupling
2242 Int_t cath, ix, iy, indx;
2243 Double_t charge, coef=0;
2244 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2245 if (fPadIJ[1][j] != -1) continue;
2247 cath = fPadIJ[0][j];
2248 fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy);
2249 fSegmentation[cath]->SetPad(ix,iy);
2251 for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2252 indx = i<2 ? 2*i : 2*i+1;
2253 fSegmentation[cath]->SetHit(par[indx],par[indx+1],fZpad);
2254 if (fNpar == 2) coef = 1;
2255 else coef = i==fNpar/3 ? par[indx+2] : 1-coef;
2256 if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2257 charge += fResponse->IntXY(fSegmentation[cath])*coef;
2260 fXyq[2][j] -= charge;
2261 } // if (fNpar != 0)
2262 if (fXyq[2][j] > fResponse->ZeroSuppression()) fPadIJ[1][j] = 0; // return pad for further using
2263 } // for (Int_t j=0;
2266 //_____________________________________________________________________________
2267 Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t t) {
2268 // Test if track was user selected
2271 if (fTrack[0]==-1 || fTrack[1]==-1) {
2273 } else if (t==fTrack[0] || t==fTrack[1]) {
2281 //_____________________________________________________________________________
2282 void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t fmin)
2285 // Add a raw cluster copy to the list
2287 AliMUONRawCluster cnew;
2288 AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON");
2289 //pMUON->AddRawCluster(fInput->Chamber(),c);
2292 for (cath=0; cath<2; cath++) {
2295 cnew.fZ[cath] = fZpad;
2296 cnew.fQ[cath] = 100;
2297 cnew.fPeakSignal[cath] = 20;
2298 cnew.fMultiplicity[cath] = 5;
2299 cnew.fNcluster[cath] = 1;
2300 cnew.fChi2[cath] = fmin; //0.1;
2302 cnew.fMultiplicity[cath]=c->fMultiplicity[cath];
2303 for (i=0; i<fMul[cath]; i++) {
2304 cnew.fIndexMap[i][cath]=c->fIndexMap[i][cath];
2305 fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]);
2307 fprintf(stderr,"\nRawCluster %d cath %d\n",ico,cath);
2308 fprintf(stderr,"mult_av %d\n",c->fMultiplicity[cath]);
2309 FillCluster(&cnew,cath);
2312 //cnew.fClusterType=cnew.PhysicsContribution();
2313 pMUON->AddRawCluster(AliMUONClusterInput::Instance()->Chamber(),cnew);
2317 //_____________________________________________________________________________
2318 Int_t AliMUONClusterFinderAZ::FindLocalMaxima(Int_t *localMax, Double_t *maxVal)
2320 // Find local maxima in pixel space for large preclusters in order to
2321 // try to split them into smaller pieces (to speed up the MLEM procedure)
2323 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2324 if (hist) hist->Delete();
2326 Double_t xylim[4] = {999, 999, 999, 999};
2327 Int_t nPix = fPixArray->GetEntriesFast();
2328 AliMUONPixel *pixPtr = 0;
2329 for (Int_t ipix=0; ipix<nPix; ipix++) {
2330 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2331 for (Int_t i=0; i<4; i++)
2332 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
2334 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
2336 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
2337 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
2338 hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2339 for (Int_t ipix=0; ipix<nPix; ipix++) {
2340 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
2341 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
2344 ((TCanvas*)gROOT->FindObject("c2"))->cd();
2347 hist->Draw("lego1Fb");
2353 Int_t nMax = 0, indx;
2354 Int_t *isLocalMax = new Int_t[ny*nx];
2355 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
2357 for (Int_t i=1; i<=ny; i++) {
2359 for (Int_t j=1; j<=nx; j++) {
2360 if (hist->GetCellContent(j,i) < 0.5) continue;
2361 //if (isLocalMax[indx+j-1] < 0) continue;
2362 if (isLocalMax[indx+j-1] != 0) continue;
2363 FlagLocalMax(hist, i, j, isLocalMax);
2367 for (Int_t i=1; i<=ny; i++) {
2369 for (Int_t j=1; j<=nx; j++) {
2370 if (isLocalMax[indx+j-1] > 0) {
2371 localMax[nMax] = indx + j - 1;
2372 maxVal[nMax++] = hist->GetCellContent(j,i);
2374 if (nMax > 99) { cout << " Too many local maxima !!!" << endl; ::exit(0); }
2377 cout << " Local max: " << nMax << endl;
2378 delete [] isLocalMax; isLocalMax = 0;
2382 //_____________________________________________________________________________
2383 void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2385 // Flag pixels (whether or not local maxima)
2387 Int_t nx = hist->GetNbinsX();
2388 Int_t ny = hist->GetNbinsY();
2389 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
2392 for (Int_t i1=i-1; i1<i+2; i1++) {
2393 if (i1 < 1 || i1 > ny) continue;
2394 for (Int_t j1=j-1; j1<j+2; j1++) {
2395 if (j1 < 1 || j1 > nx) continue;
2396 if (i == i1 && j == j1) continue;
2397 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
2398 if (cont < cont1) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2399 else if (cont > cont1) isLocalMax[(i1-1)*nx+j1-1] = -1;
2400 else { // the same charge
2401 isLocalMax[(i-1)*nx+j-1] = 1;
2402 if (isLocalMax[(i1-1)*nx+j1-1] == 0) {
2403 FlagLocalMax(hist, i1, j1, isLocalMax);
2404 if (isLocalMax[(i1-1)*nx+j1-1] < 0) { isLocalMax[(i-1)*nx+j-1] = -1; return; }
2405 else isLocalMax[(i1-1)*nx+j1-1] = -1;
2410 isLocalMax[(i-1)*nx+j-1] = 1; // local maximum
2413 //_____________________________________________________________________________
2414 void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2416 // Find pixel cluster around local maximum #iMax and pick up pads
2417 // overlapping with it
2419 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2420 Int_t nx = hist->GetNbinsX();
2421 Int_t ny = hist->GetNbinsY();
2422 Int_t ic = localMax[iMax] / nx + 1;
2423 Int_t jc = localMax[iMax] % nx + 1;
2424 Bool_t *used = new Bool_t[ny*nx];
2425 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
2427 // Drop all pixels from the array - pick up only the ones from the cluster
2428 fPixArray->Delete();
2430 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
2431 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
2432 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
2433 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
2434 Double_t cont = hist->GetCellContent(jc,ic);
2435 AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont);
2436 fPixArray->Add((TObject*)pixPtr);
2437 used[(ic-1)*nx+jc-1] = kTRUE;
2438 AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
2440 Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1];
2441 for (Int_t i=0; i<nPix; i++) {
2442 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx);
2443 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy);
2445 cout << iMax << " " << nPix << endl;
2447 Float_t xy[4], xy12[4];
2448 // Pick up pads which overlap with found pixels
2449 for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1;
2450 for (Int_t i=0; i<nPix; i++) {
2451 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
2452 for (Int_t j=0; j<4; j++)
2453 xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2);
2454 for (Int_t j=0; j<npad; j++)
2455 if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use
2458 delete [] used; used = 0;