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 developed by A. Zinchenko (Dubna), based on the
19 // Expectation-Maximization algorithm
22 #include <Riostream.h>
27 #include "AliMUONClusterFinderAZ.h"
28 #include "AliMUONClusterDrawAZ.h"
29 #include "AliMUONVGeometryDESegmentation.h"
30 #include "AliMUONGeometryModuleTransformer.h"
33 #include "AliMUONDigit.h"
34 #include "AliMUONRawCluster.h"
35 #include "AliMUONClusterInput.h"
36 #include "AliMUONPixel.h"
37 #include "AliMUONMathieson.h"
40 ClassImp(AliMUONClusterFinderAZ)
42 const Double_t AliMUONClusterFinderAZ::fgkCouplMin = 1.e-3; // threshold on coupling
43 const Double_t AliMUONClusterFinderAZ::fgkZeroSuppression = 6; // average zero suppression value
44 const Double_t AliMUONClusterFinderAZ::fgkSaturation = 3000; // average saturation level
45 AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = 0x0;
46 TMinuit* AliMUONClusterFinderAZ::fgMinuit = 0x0;
47 //FILE *lun1 = fopen("nxny.dat","w");
49 //_____________________________________________________________________________
50 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw)
51 : AliMUONClusterFinderVS()
54 fnPads[0]=fnPads[1]=0;
56 for (Int_t i=0; i<7; i++)
57 for (Int_t j=0; j<fgkDim; j++)
60 for (Int_t i=0; i<4; i++)
61 for (Int_t j=0; j<fgkDim; j++)
64 for (Int_t i=0; i<2; i++)
65 for (Int_t j=0; j<fgkDim; j++)
68 fSegmentation[1] = fSegmentation[0] = 0x0;
72 fPadBeg[0] = fPadBeg[1] = fCathBeg = fNpar = fnCoupled = 0;
74 if (!fgMinuit) fgMinuit = new TMinuit(8);
75 if (!fgClusterFinder) fgClusterFinder = this;
76 fPixArray = new TObjArray(20);
84 fDraw = new AliMUONClusterDrawAZ(this);
86 cout << " *** Running AZ cluster finder *** " << endl;
89 //_____________________________________________________________________________
90 AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(const AliMUONClusterFinderAZ& rhs)
91 : AliMUONClusterFinderVS(rhs)
93 // Protected copy constructor
95 AliFatal("Not implemented.");
98 //_____________________________________________________________________________
99 AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ()
102 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
106 //_____________________________________________________________________________
107 void AliMUONClusterFinderAZ::FindRawClusters()
109 // To provide the same interface as in AliMUONClusterFinderVS
112 EventLoop (gAlice->GetEvNumber(), fInput->Chamber());
115 //_____________________________________________________________________________
116 void AliMUONClusterFinderAZ::EventLoop(Int_t nev, Int_t ch)
120 if (fDraw && !fDraw->FindEvCh(nev, ch)) return;
122 fSegmentation[0] = (AliMUONVGeometryDESegmentation*) fInput->
123 Segmentation2(0)->GetDESegmentation(fInput->DetElemId());
124 fSegmentation[1] = (AliMUONVGeometryDESegmentation*) fInput->
125 Segmentation2(1)->GetDESegmentation(fInput->DetElemId());
127 Int_t ndigits[2] = {9,9}, nShown[2] = {0};
128 if (fReco != 2) { // skip initialization for the combined cluster / track
129 fCathBeg = fPadBeg[0] = fPadBeg[1] = 0;
130 for (Int_t i = 0; i < 2; i++) {
131 for (Int_t j = 0; j < fgkDim; j++) { fUsed[i][j] = kFALSE; }
136 if (fReco == 2 && (nShown[0] || nShown[1])) return; // only one precluster for the combined finder
137 if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) return;
139 Float_t xpad, ypad, zpad, zpad0;
140 Bool_t first = kTRUE;
141 if (fDebug) cout << " *** Event # " << nev << " chamber: " << ch << endl;
142 fnPads[0] = fnPads[1] = 0;
143 for (Int_t i = 0; i < fgkDim; i++) fPadIJ[1][i] = 0;
145 for (Int_t iii = fCathBeg; iii < 2; iii++) {
146 Int_t cath = TMath::Odd(iii);
147 ndigits[cath] = fInput->NDigits(cath);
148 if (!ndigits[0] && !ndigits[1]) return;
149 if (ndigits[cath] == 0) continue;
150 if (fDebug) cout << " ndigits: " << ndigits[cath] << " " << cath << endl;
155 Bool_t eEOC = kTRUE; // end-of-cluster
156 for (digit = fPadBeg[cath]; digit < ndigits[cath]; digit++) {
157 mdig = AliMUONClusterInput::Instance()->Digit(cath,digit);
159 // Find first unused pad
160 if (fUsed[cath][digit]) continue;
161 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0)) {
162 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
163 // Handle "non-existing" pads
164 fUsed[cath][digit] = kTRUE;
167 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad0);
169 if (fUsed[cath][digit]) continue;
170 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad)) {
171 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
172 // Handle "non-existing" pads
173 fUsed[cath][digit] = kTRUE;
176 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
177 //if (TMath::Abs(zpad-zpad0) > 0.1) continue; // different slats
178 // Find a pad overlapping with the cluster
179 if (!Overlap(cath,mdig)) continue;
181 // Add pad - recursive call
183 //AZ !!!!!! Temporary fix of St1 overlap regions !!!!!!!!
185 if (cath && ch < 2) {
186 Int_t npads = fnPads[0] + fnPads[1] - 1;
187 Int_t cath1 = fPadIJ[0][npads];
188 Int_t idig = TMath::Nint (fXyq[5][npads]);
189 mdig = AliMUONClusterInput::Instance()->Digit(cath1,idig);
190 //fSegmentation[cath1]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
191 fSegmentation[cath1]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
192 if (TMath::Abs(zpad-zpad0) > 0.1) zpad0 = zpad;
196 if (digit >= 0) break;
199 // No more unused pads
200 if (cath == 0) continue; // on cathode #0 - check #1
201 else return; // No more clusters
203 if (eEOC) break; // cluster found
205 if (fDebug) cout << " nPads: " << fnPads[cath] << " " << nShown[cath]+fnPads[cath] << " " << cath << endl;
206 } // for (Int_t iii = 0;
209 if (fDraw) fDraw->DrawCluster();
211 // Use MLEM for cluster finder
212 Int_t nMax = 1, localMax[100], maxPos[100];
213 Double_t maxVal[100];
215 if (CheckPrecluster(nShown)) {
218 if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(fPixArray, localMax, maxVal);
219 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
220 Int_t iSimple = 0, nInX = -1, nInY;
221 PadsInXandY(nInX, nInY);
222 if (fDebug) cout << "Pads in X and Y: " << nInX << " " << nInY << endl;
223 if (nMax == 1 && nInX < 4 && nInY < 4) iSimple = 1; //1; // simple cluster
226 Int_t iSimple = 0, nInX = -1, nInY;
227 PadsInXandY(nInX, nInY);
228 if (fDebug) cout << "Pads in X and Y: " << nInX << " " << nInY << endl;
229 if (nMax == 1 && nInX < 4 && nInY < 4) iSimple = 1; //1; // simple cluster
230 if (!iSimple) nMax = FindLocalMaxima(fPixArray, localMax, maxVal);
232 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
234 for (Int_t i=0; i<nMax; i++) {
235 if (nMax > 1) FindCluster(localMax, maxPos[i]);
236 if (!MainLoop(iSimple)) cout << " MainLoop failed " << endl;
238 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
239 if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
241 fXyq[2][j] = fXyq[6][j]; // use backup charge value
244 } // for (Int_t i=0; i<nMax;
245 if (nMax > 1) ((TH2D*) gROOT->FindObject("anode"))->Delete();
246 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
247 if (mlem) mlem->Delete();
249 if (!fDraw || fDraw->Next()) goto next;
252 //_____________________________________________________________________________
253 void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
255 // Add pad to the cluster
256 AliMUONDigit *mdig = fInput->Digit(cath,digit);
258 Int_t charge = mdig->Signal();
259 // get the center of the pad
260 Float_t xpad, ypad, zpad0;
261 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0)) { // Handle "non-existing" pads
262 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
263 fUsed[cath][digit] = kTRUE;
266 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad0);
267 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
268 Int_t nPads = fnPads[0] + fnPads[1];
269 fXyq[0][nPads] = xpad;
270 fXyq[1][nPads] = ypad;
271 fXyq[2][nPads] = charge;
272 fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2;
273 fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2;
274 fXyq[5][nPads] = digit;
276 fPadIJ[0][nPads] = cath;
277 fPadIJ[1][nPads] = 0;
278 fPadIJ[2][nPads] = mdig->PadX();
279 fPadIJ[3][nPads] = mdig->PadY();
280 fUsed[cath][digit] = kTRUE;
281 if (fDebug) printf(" bbb %d %d %f %f %f %f %f %4d %3d %3d \n", nPads, cath, xpad, ypad, zpad0, fXyq[3][nPads]*2, fXyq[4][nPads]*2, charge, mdig->PadX(), mdig->PadY());
285 Int_t nn, ix, iy, xList[10], yList[10];
288 Int_t ndigits = fInput->NDigits(cath);
289 fSegmentation[cath]->Neighbours(mdig->PadX(), mdig->PadY(), &nn, xList, yList);
290 for (Int_t in = 0; in < nn; in++) {
293 for (Int_t digit1 = 0; digit1 < ndigits; digit1++) {
294 if (digit1 == digit) continue;
295 mdig1 = fInput->Digit(cath,digit1);
296 if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) {
297 fUsed[cath][digit1] = kTRUE;
298 // Add pad - recursive call
301 } //for (Int_t digit1 = 0;
302 } // for (Int_t in = 0;
305 //_____________________________________________________________________________
306 Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, AliMUONDigit *mdig)
308 // Check if the pad from one cathode overlaps with a pad
309 // in the precluster on the other cathode
311 Float_t xpad, ypad, zpad;
312 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
313 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
315 Float_t xy1[4], xy12[4];
316 xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2;
317 xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec);
318 xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2;
319 xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec);
320 //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
322 Int_t cath1 = TMath::Even(cath);
323 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
324 if (fPadIJ[0][i] != cath1) continue;
325 if (Overlap(xy1, i, xy12, 0)) return kTRUE;
330 //_____________________________________________________________________________
331 Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
333 // Check if the pads xy1 and iPad overlap and return overlap area
336 xy2[0] = fXyq[0][iPad] - fXyq[3][iPad];
337 xy2[1] = fXyq[0][iPad] + fXyq[3][iPad];
338 if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE;
339 xy2[2] = fXyq[1][iPad] - fXyq[4][iPad];
340 xy2[3] = fXyq[1][iPad] + fXyq[4][iPad];
341 if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE;
342 if (!iSkip) return kTRUE; // just check overlap (w/out computing the area)
343 xy12[0] = TMath::Max (xy1[0],xy2[0]);
344 xy12[1] = TMath::Min (xy1[1],xy2[1]);
345 xy12[2] = TMath::Max (xy1[2],xy2[2]);
346 xy12[3] = TMath::Min (xy1[3],xy2[3]);
350 //_____________________________________________________________________________
351 Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
353 // Check precluster in order to attempt to simplify it (mostly for
354 // two-cathode preclusters)
356 Int_t i1, i2, cath=0, digit=0;
357 Float_t xy1[4], xy12[4];
359 Int_t npad = fnPads[0] + fnPads[1];
361 // Disregard one-pad clusters (leftovers from splitting)
362 nShown[0] += fnPads[0];
363 nShown[1] += fnPads[1];
367 // If pads have the same size take average of pads on both cathodes
368 //Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
369 Int_t sameSize = 0; //AZ - 17-01-06
372 Double_t xSize = -1, ySize = 0;
373 for (Int_t i=0; i<npad; i++) {
374 if (fXyq[2][i] < 0) continue;
375 if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; }
376 if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 || TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; }
379 if (sameSize && fnPads[0] == 1 && fnPads[1] == 1) sameSize = 0; //AZ
380 // Handle shift by half a pad in Station 1
382 Int_t cath0 = fPadIJ[0][0];
383 for (Int_t i = 1; i < npad; i++) {
384 if (fPadIJ[0][i] == cath0) continue;
385 Double_t dx = TMath::Abs ((fXyq[0][i] - fXyq[0][0]) / fXyq[3][i] / 2);
386 Int_t idx = (Int_t) TMath::Abs ((fXyq[0][i] - fXyq[0][0]) / fXyq[3][i] / 2);
387 if (TMath::Abs (dx - idx) > 0.001) sameSize = 0;
392 if (sameSize && (fnPads[0] >= 2 || fnPads[1] >= 2)) {
393 nShown[0] += fnPads[0];
394 nShown[1] += fnPads[1];
395 fnPads[0] = fnPads[1] = 0;
397 for (Int_t i=0; i<npad; i++) {
398 if (fXyq[2][i] < 0) continue; // used pad
399 fXyq[2][fnPads[0]] = fXyq[2][i];
402 for (Int_t j=i+1; j<npad; j++) {
403 if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode
404 if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue;
405 if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue;
406 fXyq[2][fnPads[0]] += fXyq[2][j];
409 if (cath) fXyq[5][fnPads[0]] = fXyq[5][j]; // save digit number for cath 0
412 // Flag that the digit from the other cathode
413 if (cath && div == 1) fXyq[5][fnPads[0]] = -fXyq[5][i] - 1;
414 // If low pad charge take the other equal to 0
415 //if (div == 1 && fXyq[2][fnPads[0]] < fgkZeroSuppression + 1.5*3) div = 2;
416 fXyq[2][fnPads[0]] /= div;
417 fXyq[0][fnPads[0]] = fXyq[0][i];
418 fXyq[1][fnPads[0]] = fXyq[1][i];
419 fPadIJ[2][fnPads[0]] = fPadIJ[2][i];
420 fPadIJ[3][fnPads[0]] = fPadIJ[3][i];
421 fPadIJ[0][fnPads[0]++] = 0;
425 // Check if one-cathode precluster
426 i1 = fnPads[0]!=0 ? 0 : 1;
427 i2 = fnPads[1]!=0 ? 1 : 0;
429 if (i1 != i2) { // two-cathode
431 Int_t *flags = new Int_t[npad];
432 for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
434 // Check pad overlaps
435 for (Int_t i=0; i<npad; i++) {
436 if (fPadIJ[0][i] != i1) continue;
437 xy1[0] = fXyq[0][i] - fXyq[3][i];
438 xy1[1] = fXyq[0][i] + fXyq[3][i];
439 xy1[2] = fXyq[1][i] - fXyq[4][i];
440 xy1[3] = fXyq[1][i] + fXyq[4][i];
441 for (Int_t j=0; j<npad; j++) {
442 if (fPadIJ[0][j] != i2) continue;
443 if (!Overlap(xy1, j, xy12, 0)) continue;
444 flags[i] = flags[j] = 1; // mark overlapped pads
448 // Check if all pads overlap
450 for (Int_t i=0; i<npad; i++) {
451 if (flags[i]) continue;
453 if (fDebug) cout << i << " " << fPadIJ[0][i] << " " << fXyq[0][i] << " " << fXyq[1][i] << endl;
455 if (fDebug && nFlags) cout << " nFlags = " << nFlags << endl;
456 //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
458 for (Int_t i=0; i<npad; i++) {
459 if (flags[i]) continue;
460 digit = TMath::Nint (fXyq[5][i]);
462 // Check for edge effect (missing pads on the other cathode)
463 Int_t cath1 = TMath::Even(cath), ix, iy;
465 //if (!fSegmentation[cath1]->GetPadI(fInput->DetElemId(),fXyq[0][i],fXyq[1][i],fZpad,ix,iy)) continue;
466 if (!fSegmentation[cath1]->HasPad(fXyq[0][i], fXyq[1][i], fZpad)) continue;
467 if (nFlags == 1 && fXyq[2][i] < fgkZeroSuppression * 3) continue;
468 fUsed[cath][digit] = kFALSE; // release pad
472 if (fDraw) fDraw->UpdateCluster(npad);
475 // Check correlations of cathode charges
476 if (fnPads[0] && fnPads[1]) { // two-cathode
478 Int_t over[2] = {1, 1};
479 for (Int_t i=0; i<npad; i++) {
481 if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
482 if (fXyq[2][i] > fgkSaturation-1) over[cath] = 0;
484 if (fDebug) cout << " Total charge: " << sum[0] << " " << sum[1] << endl;
485 if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
486 if (fDebug) cout << " Release " << endl;
488 cath = sum[0] > sum[1] ? 0 : 1;
489 Int_t imax = 0, imin = 0;
490 Double_t cmax = -1, cmin = 9999, dxMin = 0, dyMin = 0;
491 Double_t *dist = new Double_t[npad];
492 for (Int_t i = 0; i < npad; i++) {
493 if (fPadIJ[0][i] != cath || fXyq[2][i] < 0) continue;
494 if (fXyq[2][i] < cmin) {
498 if (fXyq[2][i] < cmax) continue;
502 // Arrange pads according to their distance to the max,
503 // normalized to the pad size
504 for (Int_t i = 0; i < npad; i++) {
506 if (fPadIJ[0][i] != cath || fXyq[2][i] < 0) continue;
507 if (i == imax) continue;
508 Double_t dx = (fXyq[0][i] - fXyq[0][imax]) / fXyq[3][imax] / 2;
509 Double_t dy = (fXyq[1][i] - fXyq[1][imax]) / fXyq[4][imax] / 2;
510 dist[i] = TMath::Sqrt (dx * dx + dy * dy);
512 cmin = dist[i] + 0.001; // distance to the pad with minimum charge
517 TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
520 for (Int_t i = 0; i < npad; i++) {
522 if (fPadIJ[0][indx] != cath || fXyq[2][indx] < 0) continue;
523 if (dist[indx] > cmin) {
524 // Farther than the minimum pad
525 Double_t dx = (fXyq[0][indx] - fXyq[0][imax]) / fXyq[3][imax] / 2;
526 Double_t dy = (fXyq[1][indx] - fXyq[1][imax]) / fXyq[4][imax] / 2;
529 if (dx >= 0 && dy >= 0) continue;
530 if (TMath::Abs(dx) > TMath::Abs(dy) && dx >= 0) continue;
531 if (TMath::Abs(dy) > TMath::Abs(dx) && dy >= 0) continue;
533 if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax) < 1.e-3) {
535 if (TMath::Abs(dist[indx]-xmax) < 1.e-3)
536 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
537 else cmax = fXyq[2][indx];
539 digit = TMath::Nint (fXyq[5][indx]);
540 fUsed[cath][digit] = kFALSE;
544 } // for (Int_t i = 0; i < npad;
546 // Check pad overlaps once more
547 for (Int_t j = 0; j < npad; j++) flags[j] = 0;
548 for (Int_t k = 0; k < npad; k++) {
549 if (fXyq[2][k] < 0 || fPadIJ[0][k] != i1) continue;
550 xy1[0] = fXyq[0][k] - fXyq[3][k];
551 xy1[1] = fXyq[0][k] + fXyq[3][k];
552 xy1[2] = fXyq[1][k] - fXyq[4][k];
553 xy1[3] = fXyq[1][k] + fXyq[4][k];
554 for (Int_t j = 0; j < npad; j++) {
555 if (fXyq[2][j] < 0) continue;
556 if (fPadIJ[0][j] != i2) continue;
557 if (!Overlap(xy1, j, xy12, 0)) continue;
558 flags[k] = flags[j] = 1; // mark overlapped pads
559 } // for (Int_t j = 0;
560 } // for (Int_t k = 0;
562 for (Int_t j = 0; j < npad; j++) {
563 if (fXyq[2][j] < 0 || flags[j]) continue;
566 if (nFlags == fnPads[0] + fnPads[1]) {
568 for (Int_t j = 0; j < npad; j++) {
569 if (fXyq[2][j] < 0 || fPadIJ[0][j] != cath) continue;
574 delete [] dist; dist = 0;
575 if (fDraw) fDraw->UpdateCluster(npad);
576 } // TMath::Abs(sum[0]-sum[1])...
577 } // if (fnPads[0] && fnPads[1])
578 delete [] flags; flags = 0;
581 if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
583 // Move released pads to the right
584 Int_t beg = 0, end = npad-1, padij;
587 if (fXyq[2][beg] > 0) { beg++; continue; }
588 for (Int_t j=end; j>beg; j--) {
589 if (fXyq[2][j] < 0) continue;
591 for (Int_t j1=0; j1<4; j1++) {
592 padij = fPadIJ[j1][beg];
593 fPadIJ[j1][beg] = fPadIJ[j1][j];
594 fPadIJ[j1][j] = padij;
596 for (Int_t j1=0; j1<6; j1++) {
598 fXyq[j1][beg] = fXyq[j1][j];
602 } // for (Int_t j=end;
605 npad = fnPads[0] + fnPads[1];
606 if (npad > 500) { cout << " ***** Too large cluster. Give up. " << npad << endl; return kFALSE; }
607 // Back up charge value
608 for (Int_t j = 0; j < npad; j++) fXyq[6][j] = fXyq[2][j];
613 //_____________________________________________________________________________
614 void AliMUONClusterFinderAZ::BuildPixArray()
616 // Build pixel array for MLEM method
618 Int_t nPix=0, i1, i2;
619 Float_t xy1[4], xy12[4];
620 AliMUONPixel *pixPtr=0;
622 Int_t npad = fnPads[0] + fnPads[1];
624 // One cathode is empty
625 i1 = fnPads[0]!=0 ? 0 : 1;
626 i2 = fnPads[1]!=0 ? 1 : 0;
628 // Build array of pixels on anode plane
629 if (i1 == i2) { // one-cathode precluster
630 for (Int_t j=0; j<npad; j++) {
631 pixPtr = new AliMUONPixel();
632 for (Int_t i=0; i<2; i++) {
633 pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates
634 pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size
636 pixPtr->SetCharge(fXyq[2][j]); // charge
637 fPixArray->Add((TObject*)pixPtr);
640 } else { // two-cathode precluster
642 i2 = TMath::Even (i1);
643 for (Int_t i = 0; i < npad; i++) {
644 if (fPadIJ[0][i] != i1) continue;
645 xy1[0] = fXyq[0][i] - fXyq[3][i];
646 xy1[1] = fXyq[0][i] + fXyq[3][i];
647 xy1[2] = fXyq[1][i] - fXyq[4][i];
648 xy1[3] = fXyq[1][i] + fXyq[4][i];
649 for (Int_t j = 1; j < npad; j++) {
650 if (fPadIJ[0][j] != i2) continue;
651 if (!Overlap(xy1, j, xy12, 1)) continue;
652 pixPtr = new AliMUONPixel();
653 for (Int_t k=0; k<2; k++) {
654 pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates
655 pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size
657 pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
658 fPixArray->Add((TObject*)pixPtr);
659 //cout << nPix << " " << pixPtr->Coord(0) << " " << pixPtr->Size(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(1) << " " << pixPtr->Charge() << endl;
665 Float_t xPadMin = 999, yPadMin = 999;
666 for (Int_t i = 0; i < npad; i++) {
667 xPadMin = TMath::Min (xPadMin, fXyq[3][i]);
668 yPadMin = TMath::Min (yPadMin, fXyq[4][i]);
670 if (fDebug) cout << xPadMin << " " << yPadMin << endl;
672 Float_t wxmin = 999, wymin = 999;
673 for (Int_t i = 0; i < nPix; i++) {
674 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
675 wxmin = TMath::Min ((Double_t)wxmin, pixPtr->Size(0));
676 wymin = TMath::Min ((Double_t)wymin, pixPtr->Size(1));
678 if (fDebug) cout << wxmin << " " << wymin << endl;
679 wxmin = TMath::Abs (wxmin - xPadMin/2) > 0.001 ? xPadMin : xPadMin / 2;
680 wymin = TMath::Abs (wymin - yPadMin/2) > 0.001 ? yPadMin : yPadMin / 2;
681 //wxmin = xPadMin; wymin = yPadMin;
683 // Check if small pixel X-size
684 AdjustPixel(wxmin, 0);
685 // Check if small pixel Y-size
686 AdjustPixel(wymin, 1);
687 // Check if large pixel size
688 AdjustPixel(wxmin, wymin);
690 // Remove discarded pixels
691 for (Int_t i=0; i<nPix; i++) {
692 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
694 if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
696 fPixArray->Compress();
697 nPix = fPixArray->GetEntriesFast();
700 if (fDebug) cout << nPix << endl;
701 // Too many pixels - sort and remove pixels with the lowest signal
703 for (Int_t i=npad; i<nPix; i++) {
704 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
706 fPixArray->RemoveAt(i);
710 } // if (nPix > npad)
712 // Set pixel charges to the same value (for MLEM)
713 for (Int_t i=0; i<nPix; i++) {
714 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
715 //pixPtr->SetCharge(10);
716 if (fDebug) cout << i+1 << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(0) << " " << pixPtr->Size(1) << endl;
720 //_____________________________________________________________________________
721 void AliMUONClusterFinderAZ::AdjustPixel(Float_t width, Int_t ixy)
723 // Check if some pixels have small size (adjust if necessary)
725 AliMUONPixel *pixPtr, *pixPtr1 = 0;
726 Int_t ixy1 = TMath::Even(ixy);
727 Int_t nPix = fPixArray->GetEntriesFast();
729 for (Int_t i=0; i<nPix; i++) {
730 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
731 if (pixPtr->Charge() < 1) continue; // discarded pixel
732 if (pixPtr->Size(ixy)-width < -1.e-4) {
734 if (fDebug) cout << i << " Small X or Y: " << ixy << " " << pixPtr->Size(ixy) << " " << width << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << endl;
735 for (Int_t j=i+1; j<nPix; j++) {
736 pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j);
737 if (pixPtr1->Charge() < 1) continue; // discarded pixel
738 if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size
739 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns
740 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) {
742 Double_t tmp = pixPtr->Coord(ixy) + pixPtr1->Size(ixy) *
743 TMath::Sign (1., pixPtr1->Coord(ixy) - pixPtr->Coord(ixy));
744 pixPtr->SetCoord(ixy, tmp);
745 pixPtr->SetSize(ixy, width);
746 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
747 pixPtr1->SetCharge(0);
751 } // for (Int_t j=i+1;
752 //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ???
753 //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) {
754 if (pixPtr1 || i == nPix-1) {
755 // edge pixel - just increase its size
756 if (fDebug) cout << " Edge ..." << endl;
757 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
758 //if (fPadIJ[0][j] != ixy1) continue;
759 //???-check if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue;
760 if (pixPtr->Coord(ixy) < fXyq[ixy][j])
761 //pixPtr->Shift(ixy, -pixPtr->Size(ixy));
762 pixPtr->Shift(ixy, pixPtr->Size(ixy)-width);
763 //else pixPtr->Shift(ixy, pixPtr->Size(ixy));
764 else pixPtr->Shift(ixy, -pixPtr->Size(ixy)+width);
765 pixPtr->SetSize(ixy, width);
769 } // if (pixPtr->Size(ixy)-width < -1.e-4)
770 } // for (Int_t i=0; i<nPix;
774 //_____________________________________________________________________________
775 void AliMUONClusterFinderAZ::AdjustPixel(Float_t wxmin, Float_t wymin)
777 // Check if some pixels have large size (adjust if necessary)
779 Int_t n1[2], n2[2], iOK = 1, nPix = fPixArray->GetEntriesFast();
780 AliMUONPixel *pixPtr, pix;
781 Double_t xy0[2] = {9999, 9999}, wxy[2], dist[2];
783 // Check if large pixel size
784 for (Int_t i = 0; i < nPix; i++) {
785 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
786 if (pixPtr->Charge() < 1) continue; // discarded pixel
787 if (pixPtr->Size(0) - wxmin < 1.e-4) {
788 if (xy0[0] > 9998) xy0[0] = pixPtr->Coord(0); // position of a "normal" pixel
789 if (pixPtr->Size(1) - wymin < 1.e-4) {
790 if (xy0[1] > 9998) xy0[1] = pixPtr->Coord(1); // position of a "normal" pixel
792 } else iOK = 0; // large pixel
794 iOK = 0; // large pixel
795 if (xy0[1] > 9998 && pixPtr->Size(1) - wymin < 1.e-4) xy0[1] = pixPtr->Coord(1); // "normal" pixel
797 if (xy0[0] < 9998 && xy0[1] < 9998) break;
803 //cout << xy0[0] << " " << xy0[1] << endl;
804 for (Int_t i = 0; i < nPix; i++) {
805 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
806 if (pixPtr->Charge() < 1) continue; // discarded pixel
809 for (Int_t j = 0; j < 2; j++) {
810 if (pixPtr->Size(j) - wxy[j] < 1.e-4) continue;
811 dist[j] = (pixPtr->Coord(j) - xy0[j]) / wxy[j] / 2; // normalized distance to "normal" pixel
812 n2[j] = TMath::Nint (pixPtr->Size(j) / wxy[j]);
813 n1[j] = n2[j] == 1 ? TMath::Nint(dist[j]) : (Int_t)dist[j];
815 if (n1[0] > 998 && n1[1] > 998) continue;
816 if (fDebug) cout << " Different " << pixPtr->Size(0) << " " << wxy[0] << " "
817 << pixPtr->Size(1) << " " << wxy[1] <<endl;
819 //if (n2[0] > 2 || n2[1] > 2) { cout << n2[0] << " " << n2[1] << endl; AliFatal("Too large pixel.");}
820 if (n2[0] > 2 || n2[1] > 2) {
821 cout << n2[0] << " " << n2[1] << endl;
822 if (n2[0] > 2 && n1[0] < 999) n1[0]--;
823 if (n2[1] > 2 && n1[1] < 999) n1[1]--;
825 //cout << n1[0] << " " << n2[0] << " " << n1[1] << " " << n2[1] << endl;
827 pix.SetSize(0, wxy[0]); pix.SetSize(1, wxy[1]);
829 for (Int_t ii = 0; ii < n2[0]; ii++) {
830 if (n1[0] < 999) pix.SetCoord(0, xy0[0] + (n1[0] + TMath::Sign(1.,dist[0]) * ii) * 2 * wxy[0]);
831 for (Int_t jj = 0; jj < n2[1]; jj++) {
832 if (n1[1] < 999) pix.SetCoord(1, xy0[1] + (n1[1] + TMath::Sign(1.,dist[1]) * jj) * 2 * wxy[1]);
833 fPixArray->Add(new AliMUONPixel(pix));
837 pixPtr->SetCharge(0);
838 } // for (Int_t i = 0; i < nPix;
841 //_____________________________________________________________________________
842 Bool_t AliMUONClusterFinderAZ::MainLoop(Int_t iSimple)
844 // Repeat MLEM algorithm until pixel size becomes sufficiently small
849 //Int_t nn, xList[10], yList[10];
850 Int_t nPix = fPixArray->GetEntriesFast();
851 AliMUONPixel *pixPtr = 0;
852 Double_t *coef = 0, *probi = 0;
853 AddVirtualPad(); // add virtual pads if necessary
854 Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0;
855 for (Int_t i = 0; i < npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++;
856 if (fDraw) fDraw->ResetMuon();
860 mlem = (TH2D*) gROOT->FindObject("mlem");
861 if (mlem) mlem->Delete();
862 // Calculate coefficients
863 if (fDebug) cout << " nPix, npadTot, npadOK " << nPix << " " << npadTot << " " << npadOK << endl;
865 // Calculate coefficients and pixel visibilities
866 coef = new Double_t [npadTot*nPix];
867 probi = new Double_t [nPix];
868 for (Int_t ipix=0; ipix<nPix; ipix++) probi[ipix] = 0;
869 Int_t indx = 0, indx1 = 0, cath = 0;
871 for (Int_t j=0; j<npadTot; j++) {
873 if (fPadIJ[1][j] == 0) {
877 fSegmentation[cath]->SetPad(ix, iy);
879 fSegmentation[cath]->Neighbours(fInput->DetElemId(),ix,iy,&nn,xList,yList);
882 for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
888 for (Int_t ipix=0; ipix<nPix; ipix++) {
890 if (fPadIJ[1][j] < 0) { coef[indx1] = 0; continue; }
891 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
892 fSegmentation[cath]->SetHit(pixPtr->Coord(0), pixPtr->Coord(1), fZpad);
893 coef[indx1] = fInput->Mathieson()->IntXY(fInput->DetElemId(),fInput->Segmentation2(cath));
894 probi[ipix] += coef[indx1];
895 } // for (Int_t ipix=0;
897 for (Int_t ipix=0; ipix<nPix; ipix++) if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel
900 Mlem(coef, probi, 15);
902 Double_t xylim[4] = {999, 999, 999, 999};
903 for (Int_t ipix=0; ipix<nPix; ipix++) {
904 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
905 //cout << ipix+1; pixPtr->Print();
906 for (Int_t i=0; i<4; i++)
907 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
909 for (Int_t i=0; i<4; i++) {
910 xylim[i] -= pixPtr->Size(i/2); if (fDebug) cout << (i%2 ? -1 : 1)*xylim[i] << " "; }
911 if (fDebug) cout << endl;
913 // Adjust histogram to approximately the same limits as for the pads
914 // (for good presentation)
915 if (fDraw) fDraw->AdjustHist(xylim, pixPtr);
917 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
918 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
920 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
921 for (Int_t ipix=0; ipix<nPix; ipix++) {
922 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
923 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
925 if (fDraw) fDraw->DrawHist("c2", mlem);
927 // Check if the total charge of pixels is too low
929 for (Int_t i=0; i<nPix; i++) {
930 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
931 qTot += pixPtr->Charge();
933 if (qTot < 1.e-4 || npadOK < 3 && qTot < 7) {
934 delete [] coef; delete [] probi; coef = 0; probi = 0;
936 for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) fPadIJ[1][i] = -1;
940 // Plot data - expectation
943 for (Int_t j=0; j<npadTot; j++) {
945 for (Int_t i=0; i<nPix; i++) {
946 // Caculate expectation
947 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
948 sum1 += pixPtr->Charge()*coef[j*nPix+i];
950 sum1 = TMath::Min (sum1,fgkSaturation);
954 Int_t ihist = cath*2;
955 ix = fHist[ihist]->GetXaxis()->FindBin(x);
956 iy = fHist[ihist]->GetYaxis()->FindBin(y);
957 cont = fHist[ihist]->GetCellContent(ix,iy);
958 if (cont == 0 && fHist[ihist+1]) {
960 ix = fHist[ihist]->GetXaxis()->FindBin(x);
961 iy = fHist[ihist]->GetYaxis()->FindBin(y);
963 fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
965 ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
966 //gPad->SetTheta(55);
968 //mlem->Draw("lego1");
970 ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
975 // Simple cluster - skip further passes thru EM-procedure
977 delete [] coef; delete [] probi; coef = 0; probi = 0;
982 // Calculate position of the center-of-gravity around the maximum pixel
984 FindCOG(mlem, xyCOG);
986 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break;
987 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.007 && pixPtr->Size(0) > pixPtr->Size(1)) break;
988 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) {
989 // Sort pixels according to the charge
992 for (Int_t i=0; i<nPix; i++) {
993 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
994 cout << i+1; pixPtr->Print();
997 Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
998 pixMin = TMath::Min (pixMin,50.);
1000 // Decrease pixel size and shift pixels to make them centered at
1002 indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1003 Double_t width = 0, shift[2]={0};
1005 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1006 Int_t nPix1 = nPix; nPix = 0;
1007 for (Int_t ipix=0; ipix<nPix1; ipix++) {
1008 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1009 if (nPix >= npadOK) { // too many pixels already
1010 fPixArray->RemoveAt(ipix);
1014 if (pixPtr->Charge() < pixMin) { // low charge
1015 fPixArray->RemoveAt(ipix);
1019 for (Int_t i=0; i<2; i++) {
1021 pixPtr->SetCharge(10);
1022 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1023 width = -pixPtr->Size(indx);
1024 pixPtr->Shift(indx, width);
1025 // Shift pixel position
1028 for (Int_t j=0; j<2; j++) {
1029 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1030 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1032 //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1034 pixPtr->Shift(0, -shift[0]);
1035 pixPtr->Shift(1, -shift[1]);
1037 pixPtr = new AliMUONPixel(*pixPtr);
1038 pixPtr->Shift(indx, -2*width);
1039 fPixArray->Add((TObject*)pixPtr);
1042 for (Int_t i=0; i<4; i++)
1043 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1044 } // for (Int_t i=0; i<2;
1046 } // for (Int_t ipix=0;
1048 fPixArray->Compress();
1049 nPix = fPixArray->GetEntriesFast();
1051 // Remove excessive pixels
1052 if (nPix > npadOK) {
1053 for (Int_t ipix=npadOK; ipix<nPix; ipix++) {
1054 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1055 fPixArray->RemoveAt(ipix);
1059 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1060 // add pixels if the maximum is at the limit of pixel area
1061 // start from Y-direction
1063 for (Int_t i=3; i>-1; i--) {
1064 if (nPix < npadOK &&
1065 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) {
1066 pixPtr = new AliMUONPixel(*pixPtr);
1067 pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1068 j = TMath::Even (i/2);
1069 pixPtr->SetCoord(j, xyCOG[j]);
1070 fPixArray->Add((TObject*)pixPtr);
1076 fPixArray->Compress();
1077 nPix = fPixArray->GetEntriesFast();
1078 delete [] coef; delete [] probi; coef = 0; probi = 0;
1081 // remove pixels with low signal or low visibility
1082 // Cuts are empirical !!!
1083 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1084 thresh = TMath::Min (thresh,50.);
1085 Double_t cmax = -1, charge = 0;
1086 for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]);
1087 //cout << thresh << " " << cmax << " " << cmax*0.9 << endl;
1088 // Mark pixels which should be removed
1089 for (Int_t i=0; i<nPix; i++) {
1090 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1091 charge = pixPtr->Charge();
1092 if (charge < thresh) pixPtr->SetCharge(-charge);
1093 //else if (cmax > 1.91) {
1094 // if (probi[i] < 1.9) pixPtr->SetCharge(-charge);
1096 //AZ else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
1097 //18-01-06 else if (probi[i] < cmax*0.8) pixPtr->SetCharge(-charge);
1098 //cout << i << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << charge << " " << probi[i] << endl;
1100 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1102 for (Int_t i=0; i<nPix; i++) {
1103 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1104 charge = pixPtr->Charge();
1105 if (charge > 0) continue;
1106 near = FindNearest(pixPtr);
1107 pixPtr->SetCharge(0);
1108 probi[i] = 0; // make it "invisible"
1109 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near);
1110 pixPtr->SetCharge(pixPtr->Charge() + (-charge));
1114 for (Int_t i=0; i<nPix; i++) {
1115 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1116 ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1117 iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1118 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1120 if (fDraw) fDraw->DrawHist("c2", mlem);
1122 // Try to split into clusters
1124 if (mlem->GetSum() < 1) ok = kFALSE;
1125 else Split(mlem, coef);
1126 delete [] coef; delete [] probi; coef = 0; probi = 0;
1127 fPixArray->Delete();
1131 //_____________________________________________________________________________
1132 void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi, Int_t nIter)
1134 // Use MLEM to find pixel charges
1136 Int_t nPix = fPixArray->GetEntriesFast();
1137 Int_t npad = fnPads[0] + fnPads[1];
1138 Double_t *probi1 = new Double_t [nPix];
1139 Double_t probMax = 0;
1141 AliMUONPixel *pixPtr;
1143 for (Int_t ipix=0; ipix<nPix; ipix++) if (probi[ipix] > probMax) probMax = probi[ipix];
1144 for (Int_t iter=0; iter<nIter; iter++) {
1146 for (Int_t ipix=0; ipix<nPix; ipix++) {
1147 // Correct each pixel
1148 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1150 //probi1[ipix] = probi[ipix];
1151 probi1[ipix] = probMax;
1152 for (Int_t j=0; j<npad; j++) {
1153 if (fPadIJ[1][j] < 0) continue;
1156 indx = indx1 + ipix;
1157 for (Int_t i=0; i<nPix; i++) {
1158 // Caculate expectation
1159 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1160 sum1 += pixPtr->Charge()*coef[indx1+i];
1161 } // for (Int_t i=0;
1162 if (fXyq[2][j] > fgkSaturation-1 && sum1 > fXyq[2][j]) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows
1163 //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl;
1164 if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1;
1165 } // for (Int_t j=0;
1166 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1167 if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1168 } // for (Int_t ipix=0;
1169 } // for (Int_t iter=0;
1174 //_____________________________________________________________________________
1175 void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1177 // Calculate position of the center-of-gravity around the maximum pixel
1179 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1180 Int_t i1 = -9, j1 = -9;
1181 mlem->GetMaximumBin(ixmax,iymax,ix);
1182 Int_t nx = mlem->GetNbinsX();
1183 Int_t ny = mlem->GetNbinsY();
1184 Double_t thresh = mlem->GetMaximum()/10;
1185 Double_t x, y, cont, xq=0, yq=0, qq=0;
1187 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1188 y = mlem->GetYaxis()->GetBinCenter(i);
1189 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1190 cont = mlem->GetCellContent(j,i);
1191 if (cont < thresh) continue;
1192 if (i != i1) {i1 = i; nsumy++;}
1193 if (j != j1) {j1 = j; nsumx++;}
1194 x = mlem->GetXaxis()->GetBinCenter(j);
1203 Int_t i2 = 0, j2 = 0;
1206 // one bin in Y - add one more (with the largest signal)
1207 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1208 if (i == iymax) continue;
1209 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1210 cont = mlem->GetCellContent(j,i);
1213 x = mlem->GetXaxis()->GetBinCenter(j);
1214 y = mlem->GetYaxis()->GetBinCenter(i);
1223 if (i2 != i1) nsumy++;
1224 if (j2 != j1) nsumx++;
1226 } // if (nsumy == 1)
1229 // one bin in X - add one more (with the largest signal)
1231 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1232 if (j == ixmax) continue;
1233 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1234 cont = mlem->GetCellContent(j,i);
1237 x = mlem->GetXaxis()->GetBinCenter(j);
1238 y = mlem->GetYaxis()->GetBinCenter(i);
1247 if (i2 != i1) nsumy++;
1248 if (j2 != j1) nsumx++;
1250 } // if (nsumx == 1)
1252 xyc[0] = xq/qq; xyc[1] = yq/qq;
1253 if (fDebug) cout << xyc[0] << " " << xyc[1] << " " << qq << " " << nsum << " " << nsumx << " " << nsumy << endl;
1257 //_____________________________________________________________________________
1258 Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1260 // Find the pixel nearest to the given one
1261 // (algorithm may be not very efficient)
1263 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1264 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1265 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1266 AliMUONPixel *pixPtr;
1268 for (Int_t i=0; i<nPix; i++) {
1269 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1270 if (pixPtr->Charge() < 0.5) continue;
1271 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1272 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1273 r = dx *dx + dy * dy;
1274 if (r < rmin) { rmin = r; imin = i; }
1279 //_____________________________________________________________________________
1280 void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
1282 // The main steering function to work with clusters of pixels in anode
1283 // plane (find clusters, decouple them from each other, merge them (if
1284 // necessary), pick up coupled pads, call the fitting function)
1286 Int_t nx = mlem->GetNbinsX();
1287 Int_t ny = mlem->GetNbinsY();
1288 Int_t nPix = fPixArray->GetEntriesFast();
1290 Bool_t *used = new Bool_t[ny*nx];
1292 Int_t nclust = 0, indx, indx1;
1294 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1296 TObjArray *clusters[200]={0};
1299 // Find clusters of histogram bins (easier to work in 2-D space)
1300 for (Int_t i=1; i<=ny; i++) {
1301 for (Int_t j=1; j<=nx; j++) {
1302 indx = (i-1)*nx + j - 1;
1303 if (used[indx]) continue;
1304 cont = mlem->GetCellContent(j,i);
1305 if (cont < 0.5) continue;
1306 pix = new TObjArray(20);
1308 pix->Add(BinToPix(mlem,j,i));
1309 AddBin(mlem, i, j, 0, used, pix); // recursive call
1310 if (nclust >= 200) AliFatal(" Too many clusters !!!");
1311 clusters[nclust++] = pix;
1312 } // for (Int_t j=1; j<=nx; j++) {
1313 } // for (Int_t i=1; i<=ny;
1314 if (fDebug) cout << nclust << endl;
1315 delete [] used; used = 0;
1317 // Compute couplings between clusters and clusters to pads
1318 Int_t npad = fnPads[0] + fnPads[1];
1320 // Write out some information for algorithm development
1321 Int_t cath=0, npadx[2]={0}, npady[2]={0};
1322 Double_t xlow[2]={9999,9999}, xhig[2]={-9999,-9999};
1323 Double_t ylow[2]={9999,9999}, yhig[2]={-9999,-9999};
1324 for (Int_t j=0; j<npad; j++) {
1325 if (fXyq[3][j] < 0) continue; // exclude virtual pads
1326 cath = fPadIJ[0][j];
1327 if (fXyq[0][j] < xlow[cath]-0.001) {
1328 if (fXyq[0][j]+fXyq[3][j] <= xlow[cath] && npadx[cath]) npadx[cath]++;
1329 xlow[cath] = fXyq[0][j];
1331 if (fXyq[0][j] > xhig[cath]+0.001) {
1332 if (fXyq[0][j]-fXyq[3][j] >= xhig[cath]) npadx[cath]++;
1333 xhig[cath] = fXyq[0][j];
1335 if (fXyq[1][j] < ylow[cath]-0.001) {
1336 if (fXyq[1][j]+fXyq[4][j] <= ylow[cath] && npady[cath]) npady[cath]++;
1337 ylow[cath] = fXyq[1][j];
1339 if (fXyq[1][j] > yhig[cath]+0.001) {
1340 if (fXyq[1][j]-fXyq[4][j] >= yhig[cath]) npady[cath]++;
1341 yhig[cath] = fXyq[1][j];
1344 //if (lun1) fprintf(lun1," %4d %2d %3d %3d %3d %3d \n",gAlice->GetHeader()->GetEvent(),AliMUONClusterInput::Instance()->Chamber(), npadx[0], npadx[1], npady[0], npady[1]);
1346 // Exclude pads with overflows
1347 for (Int_t j=0; j<npad; j++) {
1348 if (fXyq[2][j] > fgkSaturation-1) fPadIJ[1][j] = -5;
1349 else fPadIJ[1][j] = 0;
1352 // Compute couplings of clusters to pads
1353 TMatrixD *aijclupad = new TMatrixD(nclust,npad);
1356 for (Int_t iclust=0; iclust<nclust; iclust++) {
1357 pix = clusters[iclust];
1358 npxclu = pix->GetEntriesFast();
1359 for (Int_t i=0; i<npxclu; i++) {
1360 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
1361 for (Int_t j=0; j<npad; j++) {
1362 if (fPadIJ[1][j] < 0 && fPadIJ[1][j] != -5) continue;
1363 if (coef[j*nPix+indx] < fgkCouplMin) continue;
1364 (*aijclupad)(iclust,j) += coef[j*nPix+indx];
1368 // Compute couplings between clusters
1369 TMatrixD *aijcluclu = new TMatrixD(nclust,nclust);
1371 for (Int_t iclust=0; iclust<nclust; iclust++) {
1372 for (Int_t j=0; j<npad; j++) {
1373 // Exclude overflows
1374 if (fPadIJ[1][j] < 0) continue;
1375 if ((*aijclupad)(iclust,j) < fgkCouplMin) continue;
1376 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1377 if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue;
1378 (*aijcluclu)(iclust,iclust1) +=
1379 TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j));
1383 for (Int_t iclust=0; iclust<nclust; iclust++) {
1384 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
1385 (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1);
1389 if (fDebug && nclust > 1) aijcluclu->Print();
1391 // Find groups of coupled clusters
1392 used = new Bool_t[nclust];
1393 for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
1394 Int_t *clustNumb = new Int_t[nclust];
1395 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
1398 for (Int_t igroup=0; igroup<nclust; igroup++) {
1399 if (used[igroup]) continue;
1400 used[igroup] = kTRUE;
1401 clustNumb[0] = igroup;
1403 // Find group of coupled clusters
1404 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
1406 cout << " nCoupled: " << nCoupled << endl;
1407 for (Int_t i=0; i<nCoupled; i++) cout << clustNumb[i] << " "; cout << endl;
1409 fnCoupled = nCoupled;
1411 while (nCoupled > 0) {
1415 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
1417 // Too many coupled clusters to fit - try to decouple them
1418 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
1419 // all the others in the group
1420 for (Int_t j=0; j<3; j++) minGroup[j] = -1;
1421 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
1423 // Flag clusters for fit
1425 while (minGroup[nForFit] >= 0 && nForFit < 3) {
1426 if (fDebug) cout << clustNumb[minGroup[nForFit]] << " ";
1427 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1428 clustNumb[minGroup[nForFit]] -= 999;
1431 if (fDebug) cout << nForFit << " " << coupl << endl;
1434 // Select pads for fit.
1435 if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) {
1437 for (Int_t j=0; j<npad; j++) {
1438 if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0;
1439 if (TMath::Abs(fPadIJ[1][j]) == -9) fPadIJ[1][j] = -5;
1441 // Merge the failed cluster candidates (with too few pads to fit) with
1442 // the one with the strongest coupling
1443 Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
1446 nfit = Fit(0, nForFit, clustFit, clusters, parOk);
1449 // Subtract the fitted charges from pads with strong coupling and/or
1450 // return pads for further use
1451 UpdatePads(nfit, parOk);
1454 for (Int_t j=0; j<npad; j++) {
1455 if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;
1456 if (fPadIJ[1][j] == -9) fPadIJ[1][j] = -5;
1459 // Sort the clusters (move to the right the used ones)
1460 Int_t beg = 0, end = nCoupled - 1;
1462 if (clustNumb[beg] >= 0) { beg++; continue; }
1463 for (Int_t j=end; j>beg; j--) {
1464 if (clustNumb[j] < 0) continue;
1466 indx = clustNumb[beg];
1467 clustNumb[beg] = clustNumb[j];
1468 clustNumb[j] = indx;
1474 nCoupled -= nForFit;
1476 // Remove couplings of used clusters
1477 for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) {
1478 indx = clustNumb[iclust] + 999;
1479 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1480 indx1 = clustNumb[iclust1];
1481 (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0;
1485 // Update the remaining clusters couplings (exclude couplings from
1487 for (Int_t j=0; j<npad; j++) {
1488 if (fPadIJ[1][j] != -1) continue;
1489 for (Int_t iclust=0; iclust<nCoupled; iclust++) {
1490 indx = clustNumb[iclust];
1491 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1492 for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1493 indx1 = clustNumb[iclust1];
1494 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1496 (*aijcluclu)(indx,indx1) -=
1497 TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j));
1498 (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1);
1502 } // for (Int_t j=0; j<npad;
1503 } // if (nCoupled > 3)
1504 } // while (nCoupled > 0)
1505 } // for (Int_t igroup=0; igroup<nclust;
1507 aijcluclu->Delete(); aijclupad->Delete();
1508 for (Int_t iclust=0; iclust<nclust; iclust++) {
1509 pix = clusters[iclust];
1511 delete pix; pix = 0;
1513 delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1516 //_____________________________________________________________________________
1517 void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1519 // Add a bin to the cluster
1521 Int_t nx = mlem->GetNbinsX();
1522 Int_t ny = mlem->GetNbinsY();
1523 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
1524 AliMUONPixel *pixPtr = 0;
1526 for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
1527 for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
1528 if (i != ic && j != jc) continue;
1529 if (used[(i-1)*nx+j-1]) continue;
1530 cont1 = mlem->GetCellContent(j,i);
1531 if (mode && cont1 > cont) continue;
1532 used[(i-1)*nx+j-1] = kTRUE;
1533 if (cont1 < 0.5) continue;
1534 if (pix) pix->Add(BinToPix(mlem,j,i));
1536 pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j),
1537 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1538 fPixArray->Add((TObject*)pixPtr);
1540 AddBin(mlem, i, j, mode, used, pix); // recursive call
1545 //_____________________________________________________________________________
1546 TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1548 // Translate histogram bin to pixel
1550 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1551 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1553 Int_t nPix = fPixArray->GetEntriesFast();
1554 AliMUONPixel *pixPtr = NULL;
1556 // Compare pixel and bin positions
1557 for (Int_t i=0; i<nPix; i++) {
1558 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1559 if (pixPtr->Charge() < 0.5) continue;
1560 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr;
1562 AliWarning(Form(" Something wrong ??? %f %f %f %f", xc, yc));
1566 //_____________________________________________________________________________
1567 void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
1569 // Add a cluster to the group of coupled clusters
1571 for (Int_t i=0; i<nclust; i++) {
1572 if (used[i]) continue;
1573 if ((*aijcluclu)(i,ic) < fgkCouplMin) continue;
1575 clustNumb[nCoupled++] = i;
1576 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
1580 //_____________________________________________________________________________
1581 Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup)
1583 // Find group of clusters with minimum coupling to all the others
1585 Int_t i123max = TMath::Min(3,nCoupled/2);
1586 Int_t indx, indx1, indx2, indx3, nTot = 0;
1587 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1589 for (Int_t i123=1; i123<=i123max; i123++) {
1592 coupl1 = new Double_t [nCoupled];
1593 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1595 else if (i123 == 2) {
1596 nTot = nCoupled*nCoupled;
1597 coupl2 = new Double_t [nTot];
1598 for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1600 nTot = nTot*nCoupled;
1601 coupl3 = new Double_t [nTot];
1602 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1605 for (Int_t i=0; i<nCoupled; i++) {
1606 indx1 = clustNumb[i];
1607 for (Int_t j=i+1; j<nCoupled; j++) {
1608 indx2 = clustNumb[j];
1610 coupl1[i] += (*aijcluclu)(indx1,indx2);
1611 coupl1[j] += (*aijcluclu)(indx1,indx2);
1613 else if (i123 == 2) {
1614 indx = i*nCoupled + j;
1615 coupl2[indx] = coupl1[i] + coupl1[j];
1616 coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2));
1618 for (Int_t k=j+1; k<nCoupled; k++) {
1619 indx3 = clustNumb[k];
1620 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1621 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1622 coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3));
1625 } // for (Int_t j=i+1;
1626 } // for (Int_t i=0;
1627 } // for (Int_t i123=1;
1629 // Find minimum coupling
1630 Double_t couplMin = 9999;
1633 for (Int_t i123=1; i123<=i123max; i123++) {
1635 locMin = TMath::LocMin(nCoupled, coupl1);
1636 couplMin = coupl1[locMin];
1637 minGroup[0] = locMin;
1638 delete [] coupl1; coupl1 = 0;
1640 else if (i123 == 2) {
1641 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1642 if (coupl2[locMin] < couplMin) {
1643 couplMin = coupl2[locMin];
1644 minGroup[0] = locMin/nCoupled;
1645 minGroup[1] = locMin%nCoupled;
1647 delete [] coupl2; coupl2 = 0;
1649 locMin = TMath::LocMin(nTot, coupl3);
1650 if (coupl3[locMin] < couplMin) {
1651 couplMin = coupl3[locMin];
1652 minGroup[0] = locMin/nCoupled/nCoupled;
1653 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1654 minGroup[2] = locMin%nCoupled;
1656 delete [] coupl3; coupl3 = 0;
1658 } // for (Int_t i123=1;
1662 //_____________________________________________________________________________
1663 Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad)
1665 // Select pads for fit. If too many coupled clusters, find pads giving
1666 // the strongest coupling with the rest of clusters and exclude them from the fit.
1668 Int_t npad = fnPads[0] + fnPads[1];
1669 Double_t *padpix = 0;
1672 padpix = new Double_t[npad];
1673 for (Int_t i=0; i<npad; i++) padpix[i] = 0;
1676 Int_t nOK = 0, indx, indx1;
1677 for (Int_t iclust=0; iclust<nForFit; iclust++) {
1678 indx = clustFit[iclust];
1679 for (Int_t j=0; j<npad; j++) {
1680 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
1681 if (fPadIJ[1][j] == -5) fPadIJ[1][j] = -9; // flag overflow
1682 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1683 if (!fPadIJ[1][j]) { fPadIJ[1][j] = 1; nOK++; } // pad to be used in fit
1685 // Check other clusters
1686 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1687 indx1 = clustNumb[iclust1];
1688 if (indx1 < 0) continue;
1689 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
1690 padpix[j] += (*aijclupad)(indx1,j);
1692 } // if (nCoupled > 3)
1693 } // for (Int_t j=0; j<npad;
1694 } // for (Int_t iclust=0; iclust<nForFit
1695 if (nCoupled < 4) return nOK;
1698 for (Int_t j=0; j<npad; j++) {
1699 if (padpix[j] < fgkCouplMin) continue;
1700 if (fDebug) cout << j << " " << padpix[j] << " " << fXyq[0][j] << " " << fXyq[1][j] << endl;
1702 fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1705 delete [] padpix; padpix = 0;
1709 //_____________________________________________________________________________
1710 void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad)
1712 // Merge the group of clusters with the one having the strongest coupling with them
1714 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1715 TObjArray *pix, *pix1;
1718 for (Int_t icl=0; icl<nForFit; icl++) {
1719 indx = clustFit[icl];
1720 pix = clusters[indx];
1721 npxclu = pix->GetEntriesFast();
1723 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1724 indx1 = clustNumb[icl1];
1725 if (indx1 < 0) continue;
1726 if ((*aijcluclu)(indx,indx1) > couplMax) {
1727 couplMax = (*aijcluclu)(indx,indx1);
1730 } // for (Int_t icl1=0;
1731 /*if (couplMax < fgkCouplMin) {
1732 cout << " Oops " << couplMax << endl;
1734 cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1738 pix1 = clusters[imax];
1739 npxclu1 = pix1->GetEntriesFast();
1741 for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
1742 if (fDebug) cout << " New number of pixels: " << npxclu1 << " " << pix1->GetEntriesFast() << endl;
1743 //Add cluster-to-cluster couplings
1744 //aijcluclu->Print();
1745 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1746 indx1 = clustNumb[icl1];
1747 if (indx1 < 0 || indx1 == imax) continue;
1748 (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1);
1749 (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax);
1751 (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0;
1752 //aijcluclu->Print();
1753 //Add cluster-to-pad couplings
1754 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
1755 if (fPadIJ[1][j] < 0 && fPadIJ[1][j] != -5) continue; // exclude used pads
1756 (*aijclupad)(imax,j) += (*aijclupad)(indx,j);
1757 (*aijclupad)(indx,j) = 0;
1759 } // for (Int_t icl=0; icl<nForFit;
1762 //_____________________________________________________________________________
1763 Int_t AliMUONClusterFinderAZ::Fit(Int_t iSimple, Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
1765 // Find selected clusters to selected pad charges
1767 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
1768 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
1769 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
1770 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
1771 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
1772 Double_t step[3]={0.01,0.002,0.02}, xPad = 0, yPad = 99999;
1774 // Number of pads to use and number of virtual pads
1775 Int_t npads = 0, nVirtual = 0, nfit0 = nfit;
1776 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
1777 if (fXyq[3][i] < 0) nVirtual++;
1778 if (fPadIJ[1][i] != 1) continue;
1779 if (fXyq[3][i] > 0) {
1785 if (fXyq[4][i] < fXyq[3][i]) yPad = fXyq[1][i];
1786 else xPad = fXyq[0][i];
1791 for (Int_t i=0; i<nfit; i++) {cout << i+1 << " " << clustFit[i] << " ";}
1792 cout << nfit << endl;
1793 cout << " Number of pads to fit: " << npads << endl;
1797 if (npads < 2) return 0;
1800 AliMUONDigit *mdig = 0;
1801 Int_t tracks[3] = {-1, -1, -1};
1802 for (Int_t cath=0; cath<2; cath++) {
1803 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
1804 if (fPadIJ[0][i] != cath) continue;
1805 if (fPadIJ[1][i] != 1) continue;
1806 if (fXyq[3][i] < 0) continue; // exclude virtual pads
1807 digit = TMath::Nint (fXyq[5][i]);
1808 if (digit >= 0) mdig = fInput->Digit(cath,digit);
1809 else mdig = fInput->Digit(TMath::Even(cath),-digit-1);
1810 //if (!mdig) mdig = fInput->Digit(TMath::Even(cath),digit);
1811 if (!mdig) continue; // protection for cluster display
1812 if (mdig->Hit() >= 0) {
1813 if (tracks[0] < 0) {
1814 tracks[0] = mdig->Hit();
1815 tracks[1] = mdig->Track(0);
1816 } else if (mdig->Track(0) < tracks[1]) {
1817 tracks[0] = mdig->Hit();
1818 tracks[1] = mdig->Track(0);
1821 if (mdig->Track(1) >= 0 && mdig->Track(1) != tracks[1]) {
1822 if (tracks[2] < 0) tracks[2] = mdig->Track(1);
1823 else tracks[2] = TMath::Min (tracks[2], mdig->Track(1));
1826 //cout << mdig->Hit() << " " << mdig->Track(0) << " " << mdig->Track(1) <<endl;
1827 } // for (Int_t i=0;
1828 } // for (Int_t cath=0;
1829 //cout << tracks[0] << " " << tracks[1] << " " << tracks[2] <<endl;
1831 // Get number of pads in X and Y
1832 Int_t nInX = 0, nInY;
1833 PadsInXandY(nInX, nInY);
1834 //cout << " nInX and Y: " << nInX << " " << nInY << endl;
1837 nfitMax = TMath::Min (nfitMax, (npads + 1) / 3);
1839 if (nInX < 3 && nInY < 3 || nInX == 3 && nInY < 3 || nInX < 3 && nInY == 3) nfitMax = 1; // not enough pads in each direction
1841 if (nfit > nfitMax) nfit = nfitMax;
1843 // Take cluster maxima as fitting seeds
1845 AliMUONPixel *pixPtr;
1847 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8], qq = 0;
1848 Double_t xyseed[3][2], qseed[3], xyCand[3][2] = {{0},{0}}, sigCand[3][2] = {{0},{0}};
1850 for (Int_t ifit=1; ifit<=nfit0; ifit++) {
1852 pix = clusters[clustFit[ifit-1]];
1853 npxclu = pix->GetEntriesFast();
1855 for (Int_t clu=0; clu<npxclu; clu++) {
1856 pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu);
1857 cont = pixPtr->Charge();
1861 xseed = pixPtr->Coord(0);
1862 yseed = pixPtr->Coord(1);
1866 xyCand[ifit-1][0] += pixPtr->Coord(0) * cont;
1867 xyCand[ifit-1][1] += pixPtr->Coord(1) * cont;
1868 sigCand[ifit-1][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
1869 sigCand[ifit-1][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
1871 xyCand[0][0] += pixPtr->Coord(0) * cont;
1872 xyCand[0][1] += pixPtr->Coord(1) * cont;
1873 sigCand[0][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
1874 sigCand[0][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
1876 xyseed[ifit-1][0] = xseed;
1877 xyseed[ifit-1][1] = yseed;
1878 qseed[ifit-1] = cmax;
1880 xyCand[ifit-1][0] /= qq; // <x>
1881 xyCand[ifit-1][1] /= qq; // <y>
1882 sigCand[ifit-1][0] = sigCand[ifit-1][0]/qq - xyCand[ifit-1][0]*xyCand[ifit-1][0]; // <x^2> - <x>^2
1883 sigCand[ifit-1][0] = sigCand[ifit-1][0] > 0 ? TMath::Sqrt (sigCand[ifit-1][0]) : 0;
1884 sigCand[ifit-1][1] = sigCand[ifit-1][1]/qq - xyCand[ifit-1][1]*xyCand[ifit-1][1]; // <y^2> - <y>^2
1885 sigCand[ifit-1][1] = sigCand[ifit-1][1] > 0 ? TMath::Sqrt (sigCand[ifit-1][1]) : 0;
1886 cout << xyCand[ifit-1][0] << " " << xyCand[ifit-1][1] << " " << sigCand[ifit-1][0] << " " << sigCand[ifit-1][1] << endl;
1888 } // for (Int_t ifit=1;
1890 xyCand[0][0] /= qq; // <x>
1891 xyCand[0][1] /= qq; // <y>
1892 sigCand[0][0] = sigCand[0][0]/qq - xyCand[0][0]*xyCand[0][0]; // <x^2> - <x>^2
1893 sigCand[0][0] = sigCand[0][0] > 0 ? TMath::Sqrt (sigCand[0][0]) : 0;
1894 sigCand[0][1] = sigCand[0][1]/qq - xyCand[0][1]*xyCand[0][1]; // <y^2> - <y>^2
1895 sigCand[0][1] = sigCand[0][1] > 0 ? TMath::Sqrt (sigCand[0][1]) : 0;
1896 if (fDebug) cout << xyCand[0][0] << " " << xyCand[0][1] << " " << sigCand[0][0] << " " << sigCand[0][1] << endl;
1898 Int_t nDof, maxSeed[3], nMax = 0;
1899 Double_t fmin, chi2o = 9999, chi2n;
1901 TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
1903 Int_t itmp[100], localMax[100];
1904 Double_t maxVal[100];
1905 if (!iSimple && nfit < nfitMax) {
1906 // Try to split pixel cluster according to local maxima
1908 for (Int_t iclus = 0; iclus < nfit1; iclus++) {
1909 nMax = FindLocalMaxima (clusters[clustFit[maxSeed[iclus]]], localMax, maxVal);
1910 TH2D *hist = (TH2D*) gROOT->FindObject("anode1");
1911 if (nMax == 1) { hist->Delete(); continue; }
1912 // Add extra fitting seeds from local maxima
1913 Int_t ixseed = hist->GetXaxis()->FindBin(xyseed[maxSeed[iclus]][0]);
1914 Int_t iyseed = hist->GetYaxis()->FindBin(xyseed[maxSeed[iclus]][1]);
1915 Int_t nx = hist->GetNbinsX();
1916 TMath::Sort(nMax, maxVal, itmp, kTRUE); // in decreasing order
1917 for (Int_t j = 0; j < nMax; j++) {
1918 Int_t iyc = localMax[itmp[j]] / nx + 1;
1919 Int_t ixc = localMax[itmp[j]] % nx + 1;
1920 if (ixc == ixseed && iyc == iyseed) continue; // local max already taken for seeding
1921 xyseed[nfit][0] = hist->GetXaxis()->GetBinCenter(ixc);
1922 xyseed[nfit][1] = hist->GetYaxis()->GetBinCenter(iyc);
1923 qseed[nfit] = maxVal[itmp[j]];
1924 maxSeed[nfit] = nfit++;
1925 if (nfit >= nfitMax) break;
1928 if (nfit >= nfitMax) break;
1929 } // for (Int_t iclus = 0;
1931 //TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
1932 } //if (!iSimple && nfit < nfitMax)
1935 Double_t *gin = 0, func0, func1, param[8], param0[2][8], deriv[2][8], step0[8];
1936 Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
1937 Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
1938 Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
1939 Double_t rad, dist[3] = {0};
1941 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
1942 // lower, try 3-track (if number of pads is sufficient).
1943 for (Int_t iseed=0; iseed<nfit; iseed++) {
1945 if (iseed) { for (Int_t j=0; j<fNpar; j++) param[j] = parOk[j]; } // for bounded params
1946 for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j];
1947 if (nfit == 1) param[fNpar] = xyCand[0][0]; // take COG
1948 else param[fNpar] = xyseed[maxSeed[iseed]][0];
1949 parmin[fNpar] = xmin;
1950 parmax[fNpar++] = xmax;
1951 if (nfit == 1) param[fNpar] = xyCand[0][1]; // take COG
1952 else param[fNpar] = xyseed[maxSeed[iseed]][1];
1953 parmin[fNpar] = ymin;
1954 parmax[fNpar++] = ymax;
1956 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
1958 parmax[fNpar++] = 1;
1960 if (iseed) { for (Int_t j=0; j<fNpar; j++) param0[1][j] = 0; }
1962 // Try new algorithm
1963 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
1967 Fcn1(fNpar, gin, func0, param, 1); nCall++;
1968 //cout << " Func: " << func0 << endl;
1971 for (Int_t j=0; j<fNpar; j++) {
1972 param0[max][j] = param[j];
1973 delta[j] = step0[j];
1974 param[j] += delta[j] / 10;
1975 if (j > 0) param[j-1] -= delta[j-1] / 10;
1976 Fcn1(fNpar, gin, func1, param, 1); nCall++;
1977 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
1978 //cout << j << " " << deriv[max][j] << endl;
1979 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
1980 (param0[0][j] - param0[1][j]) : 0; // second derivative
1982 param[fNpar-1] -= delta[fNpar-1] / 10;
1983 if (nCall > 2000) break;
1985 min = func2[0] < func2[1] ? 0 : 1;
1986 nFail = min == max ? 0 : nFail + 1;
1988 stepMax = derMax = estim = 0;
1989 for (Int_t j=0; j<fNpar; j++) {
1990 // Estimated distance to minimum
1992 if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
1993 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0;
1994 else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
1995 //|| TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) {
1996 || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3 || TMath::Abs(dder[j]) < 1.e-6) {
1997 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
1999 if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2003 shift[j] = dder[j] != 0 ? -deriv[min][j] / dder[j] : 0;
2006 if (TMath::Abs(shift[j])/step0[j] > estim) {
2007 estim = TMath::Abs(shift[j])/step0[j];
2012 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
2014 // Failed to improve minimum
2017 param[j] = param0[min][j];
2018 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2;
2019 else shift[j] /= -2;
2023 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
2024 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
2026 // Introduce step relaxation factor
2027 if (memory[j] < 3) {
2028 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
2029 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
2030 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
2032 param[j] += shift[j];
2033 //AZ Check parameter limits 27-12-2004
2034 if (param[j] < parmin[j]) {
2035 shift[j] = parmin[j] - param[j];
2036 param[j] = parmin[j];
2037 } else if (param[j] > parmax[j]) {
2038 shift[j] = parmax[j] - param[j];
2039 param[j] = parmax[j];
2041 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
2042 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
2043 if (TMath::Abs(deriv[min][j]) > derMax) {
2045 derMax = TMath::Abs (deriv[min][j]);
2047 } // for (Int_t j=0; j<fNpar;
2048 //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl;
2049 if (estim < 1 && derMax < 2 || nLoop > 150) break; // minimum was found
2052 // Check for small step
2053 if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; }
2054 if (!memory[idMax] && derMax > 0.5 && nLoop > 10) {
2055 //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl;
2056 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) {
2057 if (min == max) dder[idMax] = -dder[idMax];
2058 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
2059 param[idMax] += shift[idMax];
2060 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
2061 //cout << shift[idMax] << " " << param[idMax] << endl;
2062 if (min == max) shiftSave = shift[idMax];
2065 param[idMax] -= shift[idMax];
2066 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
2067 param[idMax] += shift[idMax];
2068 //cout << shift[idMax] << endl;
2074 nDof = npads - fNpar + nVirtual;
2076 chi2n = fmin / nDof;
2077 if (fDebug) cout << " Chi2 " << chi2n << " " << fNpar << endl;
2079 if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
2081 // Save parameters and errors
2084 // One pad per direction
2085 for (Int_t i=0; i<fNpar; i++) if (i == 0 || i == 2 || i == 5) param0[min][i] = xPad;
2088 // One pad per direction
2089 for (Int_t i=0; i<fNpar; i++) if (i == 1 || i == 3 || i == 6) param0[min][i] = yPad;
2094 // Find distance to the nearest neighbour
2095 dist[0] = dist[1] = TMath::Sqrt ((param0[min][0]-param0[min][2])*
2096 (param0[min][0]-param0[min][2])
2097 +(param0[min][1]-param0[min][3])*
2098 (param0[min][1]-param0[min][3]));
2100 dist[2] = TMath::Sqrt ((param0[min][0]-param0[min][5])*
2101 (param0[min][0]-param0[min][5])
2102 +(param0[min][1]-param0[min][6])*
2103 (param0[min][1]-param0[min][6]));
2104 rad = TMath::Sqrt ((param0[min][2]-param0[min][5])*
2105 (param0[min][2]-param0[min][5])
2106 +(param0[min][3]-param0[min][6])*
2107 (param0[min][3]-param0[min][6]));
2108 if (dist[2] < dist[0]) dist[0] = dist[2];
2109 if (rad < dist[1]) dist[1] = rad;
2110 if (rad < dist[2]) dist[2] = rad;
2112 cout << dist[0] << " " << dist[1] << " " << dist[2] << endl;
2113 if (dist[TMath::LocMin(iseed+1,dist)] < 1.) { fNpar -= 3; break; }
2117 for (Int_t i=0; i<fNpar; i++) {
2118 parOk[i] = param0[min][i];
2122 parOk[i] = TMath::Max (parOk[i], parmin[i]);
2123 parOk[i] = TMath::Min (parOk[i], parmax[i]);
2127 if (fmin < 0.1) break; // !!!???
2128 } // for (Int_t iseed=0;
2131 for (Int_t i=0; i<fNpar; i++) {
2132 if (i == 4 || i == 7) {
2133 if (i == 7 || i == 4 && fNpar < 7) cout << parOk[i] << endl;
2134 else cout << parOk[i] * (1-parOk[7]) << endl;
2137 cout << parOk[i] << " " << errOk[i] << endl;
2140 nfit = (fNpar + 1) / 3;
2141 dist[0] = dist[1] = dist[2] = 0;
2144 // Find distance to the nearest neighbour
2145 dist[0] = dist[1] = TMath::Sqrt ((parOk[0]-parOk[2])*
2147 +(parOk[1]-parOk[3])*
2148 (parOk[1]-parOk[3]));
2150 dist[2] = TMath::Sqrt ((parOk[0]-parOk[5])*
2152 +(parOk[1]-parOk[6])*
2153 (parOk[1]-parOk[6]));
2154 rad = TMath::Sqrt ((parOk[2]-parOk[5])*
2156 +(parOk[3]-parOk[6])*
2157 (parOk[3]-parOk[6]));
2158 if (dist[2] < dist[0]) dist[0] = dist[2];
2159 if (rad < dist[1]) dist[1] = rad;
2160 if (rad < dist[2]) dist[2] = rad;
2165 fnPads[1] -= nVirtual;
2168 if (iSimple) fnCoupled = 0;
2169 //for (Int_t j=0; j<nfit; j++) {
2170 for (Int_t j=nfit-1; j>=0; j--) {
2171 indx = j<2 ? j*2 : j*2+1;
2172 if (nfit == 1) coef = 1;
2173 else coef = j==nfit-1 ? parOk[indx+2] : 1-coef;
2174 coef = TMath::Max (coef, 0.);
2175 if (nfit == 3 && j < 2) coef = j==1 ? coef*parOk[indx+2] : coef - parOk[7];
2176 coef = TMath::Max (coef, 0.);
2177 AddRawCluster (parOk[indx], parOk[indx+1], coef*fQtot, errOk[indx], nfit0+10*nfit+100*nMax+10000*fnCoupled, tracks,
2178 //sigCand[maxSeed[j]][0], sigCand[maxSeed[j]][1]);
2179 //sigCand[0][0], sigCand[0][1], dist[j]);
2180 sigCand[0][0], sigCand[0][1], dist[TMath::LocMin(nfit,dist)]);
2182 } else fDraw->FillMuon(nfit, parOk, errOk);
2186 //_____________________________________________________________________________
2187 void AliMUONClusterFinderAZ::Fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
2189 // Fit for one track
2190 //AZ for Muinuit AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);
2191 AliMUONClusterFinderAZ& c = *this; //AZ
2193 Int_t cath, ix, iy, indx, npads=0;
2194 Double_t charge, delta, coef=0, chi2=0, qTot = 0;
2195 for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) {
2196 if (c.fPadIJ[1][j] != 1) continue;
2197 cath = c.fPadIJ[0][j];
2198 if (c.fXyq[3][j] > 0) npads++; // exclude virtual pads
2199 qTot += c.fXyq[2][j];
2200 ix = c.fPadIJ[2][j];
2201 iy = c.fPadIJ[3][j];
2202 c.fSegmentation[cath]->SetPad(ix, iy);
2204 for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2205 indx = i<2 ? 2*i : 2*i+1;
2206 c.fSegmentation[cath]->SetHit(par[indx], par[indx+1], c.fZpad);
2207 if (c.fNpar == 2) coef = 1;
2208 else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef;
2209 coef = TMath::Max (coef, 0.);
2210 if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2211 coef = TMath::Max (coef, 0.);
2212 charge += c.fInput->Mathieson()->IntXY(fInput->DetElemId(), c.fInput->Segmentation2(cath))*coef;
2215 delta = charge - c.fXyq[2][j];
2217 delta /= c.fXyq[2][j];
2218 //if (cath) delta /= 5; // just for test
2220 } // for (Int_t j=0;
2222 Double_t qAver = qTot/npads; //(c.fnPads[0]+c.fnPads[1]);
2226 //_____________________________________________________________________________
2227 void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par)
2229 // Subtract the fitted charges from pads with strong coupling
2231 Int_t cath, ix, iy, indx;
2232 Double_t charge, coef=0;
2233 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2234 if (fPadIJ[1][j] != -1) continue;
2236 cath = fPadIJ[0][j];
2239 fSegmentation[cath]->SetPad(ix, iy);
2241 for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2242 indx = i<2 ? 2*i : 2*i+1;
2243 fSegmentation[cath]->SetHit(par[indx], par[indx+1], fZpad);
2244 if (fNpar == 2) coef = 1;
2245 else coef = i==fNpar/3 ? par[indx+2] : 1-coef;
2246 coef = TMath::Max (coef, 0.);
2247 if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2248 coef = TMath::Max (coef, 0.);
2249 charge += fInput->Mathieson()->IntXY(fInput->DetElemId(),fInput->Segmentation2(cath))*coef;
2252 fXyq[2][j] -= charge;
2253 } // if (fNpar != 0)
2254 if (fXyq[2][j] > fgkZeroSuppression) fPadIJ[1][j] = 0; // return pad for further using
2255 } // for (Int_t j=0;
2258 //_____________________________________________________________________________
2259 Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) const {
2260 // Test if track was user selected
2263 if (fTrack[0]==-1 || fTrack[1]==-1) {
2265 } else if (t==fTrack[0] || t==fTrack[1]) {
2273 //_____________________________________________________________________________
2274 void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t qTot, Double_t fmin, Int_t nfit, Int_t *tracks, Double_t /*sigx*/, Double_t /*sigy*/, Double_t /*dist*/)
2277 // Add a raw cluster copy to the list
2279 if (qTot <= 0.501) return;
2280 AliMUONRawCluster cnew;
2282 Int_t cath, npads[2] = {0}, nover[2] = {0};
2283 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2284 cath = fPadIJ[0][j];
2285 // There was an overflow
2286 if (fPadIJ[1][j] == -9) nover[cath]++;
2287 if (fPadIJ[1][j] != 1 && fPadIJ[1][j] != -9) continue;
2288 cnew.SetMultiplicity(cath,cnew.GetMultiplicity(cath)+1);
2289 if (fXyq[2][j] > cnew.GetPeakSignal(cath)) cnew.SetPeakSignal(cath,TMath::Nint (fXyq[2][j]));
2290 //cnew.SetCharge(cath,cnew.GetCharge(cath) + TMath::Nint (fXyq[2][j]));
2291 cnew.SetContrib(npads[cath],cath,fXyq[2][j]);
2292 cnew.SetIndex(npads[cath],cath,TMath::Nint (fXyq[5][j]));
2293 cnew.SetDetElemId(fInput->DetElemId());
2297 cnew.SetClusterType(nover[0] + nover[1] * 100);
2298 for (Int_t j=0; j<3; j++) cnew.SetTrack(j,tracks[j]);
2300 Double_t xg, yg, zg;
2301 for (cath=0; cath<2; cath++) {
2302 // Perform local-to-global transformation
2303 fInput->Segmentation2(cath)->GetTransformer()->Local2Global(fInput->DetElemId(), x, y, fZpad, xg, yg, zg);
2304 cnew.SetX(cath, xg);
2305 cnew.SetY(cath, yg);
2306 cnew.SetZ(cath, zg);
2307 cnew.SetCharge(cath, TMath::Nint(qTot));
2308 //cnew.SetPeakSignal(cath,20);
2309 //cnew.SetMultiplicity(cath, 5);
2310 cnew.SetNcluster(cath, nfit);
2311 cnew.SetChi2(cath, fmin); //0.;1
2313 // Evaluate measurement errors
2316 cnew.SetGhost(nfit); //cnew.SetX(1,sigx); cnew.SetY(1,sigy); cnew.SetZ(1,dist);
2317 //cnew.fClusterType=cnew.PhysicsContribution();
2318 new((*fRawClusters)[fNRawClusters++]) AliMUONRawCluster(cnew);
2319 if (fDebug) cout << fNRawClusters << " " << fInput->Chamber() << endl;
2323 //_____________________________________________________________________________
2324 Int_t AliMUONClusterFinderAZ::FindLocalMaxima(TObjArray *pixArray, Int_t *localMax, Double_t *maxVal)
2326 // Find local maxima in pixel space for large preclusters in order to
2327 // try to split them into smaller pieces (to speed up the MLEM procedure)
2328 // or to find additional fitting seeds if clusters were not completely resolved
2331 //if (pixArray == fPixArray) hist = (TH2D*) gROOT->FindObject("anode");
2332 //else { hist = (TH2D*) gROOT->FindObject("anode1"); cout << hist << endl; }
2333 //if (hist) hist->Delete();
2335 Double_t xylim[4] = {999, 999, 999, 999};
2336 Int_t nPix = pixArray->GetEntriesFast();
2337 AliMUONPixel *pixPtr = 0;
2338 for (Int_t ipix=0; ipix<nPix; ipix++) {
2339 pixPtr = (AliMUONPixel*) pixArray->UncheckedAt(ipix);
2340 for (Int_t i=0; i<4; i++)
2341 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
2343 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
2345 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
2346 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
2347 if (pixArray == fPixArray) hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2348 else hist = new TH2D("anode1","anode1",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2349 for (Int_t ipix=0; ipix<nPix; ipix++) {
2350 pixPtr = (AliMUONPixel*) pixArray->UncheckedAt(ipix);
2351 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
2353 if (fDraw && pixArray == fPixArray) fDraw->DrawHist("c2", hist);
2355 Int_t nMax = 0, indx;
2356 Int_t *isLocalMax = new Int_t[ny*nx];
2357 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
2359 for (Int_t i=1; i<=ny; i++) {
2361 for (Int_t j=1; j<=nx; j++) {
2362 if (hist->GetCellContent(j,i) < 0.5) continue;
2363 //if (isLocalMax[indx+j-1] < 0) continue;
2364 if (isLocalMax[indx+j-1] != 0) continue;
2365 FlagLocalMax(hist, i, j, isLocalMax);
2369 for (Int_t i=1; i<=ny; i++) {
2371 for (Int_t j=1; j<=nx; j++) {
2372 if (isLocalMax[indx+j-1] > 0) {
2373 localMax[nMax] = indx + j - 1;
2374 maxVal[nMax++] = hist->GetCellContent(j,i);
2375 if (nMax > 99) AliFatal(" Too many local maxima !!!");
2379 if (fDebug) cout << " Local max: " << nMax << endl;
2380 delete [] isLocalMax; isLocalMax = 0;
2384 //_____________________________________________________________________________
2385 void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2387 // Flag pixels (whether or not local maxima)
2389 Int_t nx = hist->GetNbinsX();
2390 Int_t ny = hist->GetNbinsY();
2391 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
2392 Int_t cont1 = 0, indx = (i-1)*nx+j-1, indx1 = 0, indx2 = 0;
2394 for (Int_t i1=i-1; i1<i+2; i1++) {
2395 if (i1 < 1 || i1 > ny) continue;
2396 indx1 = (i1 - 1) * nx;
2397 for (Int_t j1=j-1; j1<j+2; j1++) {
2398 if (j1 < 1 || j1 > nx) continue;
2399 if (i == i1 && j == j1) continue;
2400 indx2 = indx1 + j1 - 1;
2401 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
2402 if (cont < cont1) { isLocalMax[indx] = -1; return; }
2403 else if (cont > cont1) isLocalMax[indx2] = -1;
2404 else { // the same charge
2405 isLocalMax[indx] = 1;
2406 if (isLocalMax[indx2] == 0) {
2407 FlagLocalMax(hist, i1, j1, isLocalMax);
2408 if (isLocalMax[indx2] < 0) { isLocalMax[indx] = -1; return; }
2409 else isLocalMax[indx2] = -1;
2414 isLocalMax[indx] = 1; // local maximum
2417 //_____________________________________________________________________________
2418 void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2420 // Find pixel cluster around local maximum #iMax and pick up pads
2421 // overlapping with it
2423 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2424 Int_t nx = hist->GetNbinsX();
2425 Int_t ny = hist->GetNbinsY();
2426 Int_t ic = localMax[iMax] / nx + 1;
2427 Int_t jc = localMax[iMax] % nx + 1;
2428 Bool_t *used = new Bool_t[ny*nx];
2429 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
2431 // Drop all pixels from the array - pick up only the ones from the cluster
2432 fPixArray->Delete();
2434 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
2435 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
2436 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
2437 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
2438 Double_t cont = hist->GetCellContent(jc,ic);
2439 AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont);
2440 fPixArray->Add((TObject*)pixPtr);
2441 used[(ic-1)*nx+jc-1] = kTRUE;
2442 AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
2444 Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1];
2445 for (Int_t i=0; i<nPix; i++) {
2446 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx);
2447 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy);
2449 if (fDebug) cout << iMax << " " << nPix << endl;
2451 Float_t xy[4], xy12[4];
2452 // Pick up pads which overlap with found pixels
2453 for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1;
2454 for (Int_t i=0; i<nPix; i++) {
2455 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
2456 for (Int_t j=0; j<4; j++)
2457 xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2);
2458 for (Int_t j=0; j<npad; j++)
2459 if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use
2462 delete [] used; used = 0;
2465 //_____________________________________________________________________________
2466 AliMUONClusterFinderAZ&
2467 AliMUONClusterFinderAZ::operator=(const AliMUONClusterFinderAZ& rhs)
2469 // Protected assignement operator
2471 if (this == &rhs) return *this;
2473 AliFatal("Not implemented.");
2478 //_____________________________________________________________________________
2479 void AliMUONClusterFinderAZ::AddVirtualPad()
2481 // Add virtual pad (with small charge) to improve fit for some
2482 // clusters (when pad with max charge is at the extreme of the cluster)
2484 // Get number of pads in X and Y-directions
2485 Int_t nInX = -1, nInY;
2486 PadsInXandY(nInX, nInY);
2489 // Add virtual pad only if number of pads per direction == 2
2490 if (nInX != 2 && nInY != 2) return;
2492 // Find pads with max charge
2493 Int_t maxpad[2][2] = {{-1, -1}, {-1, -1}}, cath;
2494 Double_t sigmax[2] = {0}, aamax[2] = {0};
2495 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2496 if (fPadIJ[1][j] != 0) continue;
2497 cath = fPadIJ[0][j];
2498 if (fXyq[2][j] > sigmax[cath]) {
2499 maxpad[cath][1] = maxpad[cath][0];
2500 aamax[cath] = sigmax[cath];
2501 sigmax[cath] = fXyq[2][j];
2502 maxpad[cath][0] = j;
2505 if (maxpad[0][0] >= 0 && maxpad[0][1] < 0 || maxpad[1][0] >= 0 && maxpad[1][1] < 0) {
2506 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2507 if (fPadIJ[1][j] != 0) continue;
2508 cath = fPadIJ[0][j];
2509 if (j == maxpad[cath][0] || j == maxpad[cath][1]) continue;
2510 if (fXyq[2][j] > aamax[cath]) {
2511 aamax[cath] = fXyq[2][j];
2512 maxpad[cath][1] = j;
2516 // Check for mirrors (side X on cathode 0)
2517 Bool_t mirror = kFALSE;
2518 if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0) {
2519 mirror = fXyq[3][maxpad[0][0]] < fXyq[4][maxpad[0][0]];
2520 if (!mirror && TMath::Abs(fXyq[3][maxpad[0][0]]-fXyq[3][maxpad[1][0]]) < 0.001) {
2521 // Special case when pads on both cathodes have the same size
2523 for (Int_t j = 0; j < fnPads[0]+fnPads[1]; j++) {
2524 cath = fPadIJ[0][j];
2525 if (j == maxpad[cath][0]) continue;
2526 if (fPadIJ[2][j] != fPadIJ[2][maxpad[cath][0]]) continue;
2527 if (fPadIJ[3][j] + 1 == fPadIJ[3][maxpad[cath][0]] ||
2528 fPadIJ[3][j] - 1 == fPadIJ[3][maxpad[cath][0]]) yud[cath]++;
2530 if (!yud[0]) mirror = kTRUE; // take the other cathode
2531 } // if (!mirror &&...
2532 } // if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
2534 // Find neughbours of pads with max charges
2535 Int_t nn, xList[10], yList[10], ix0, iy0, ix, iy, neighb;
2536 for (cath=0; cath<2; cath++) {
2537 if (!cath && maxpad[0][0] < 0) continue; // one-sided cluster - cathode 1
2538 if (cath && maxpad[1][0] < 0) break; // one-sided cluster - cathode 0
2539 if (maxpad[1][0] >= 0) {
2541 if (!cath && nInY != 2) continue;
2542 if (cath && nInX != 2 && (maxpad[0][0] >= 0 || nInY != 2)) continue;
2544 if (!cath && nInX != 2) continue;
2545 if (cath && nInY != 2 && (maxpad[0][0] >= 0 || nInX != 2)) continue;
2549 Int_t iAddX = 0, iAddY = 0, ix1 = 0, iy1 = 0, iPad = 0;
2550 if (maxpad[0][0] < 0) iPad = 1;
2552 for (iPad=0; iPad<2; iPad++) {
2553 if (iPad && !iAddX && !iAddY) break;
2554 if (iPad && fXyq[2][maxpad[cath][1]] / sigmax[cath] < 0.5) break;
2556 Int_t neighbx = 0, neighby = 0;
2557 ix0 = fPadIJ[2][maxpad[cath][iPad]];
2558 iy0 = fPadIJ[3][maxpad[cath][iPad]];
2559 fSegmentation[cath]->Neighbours(ix0, iy0, &nn, xList, yList);
2561 for (Int_t j=0; j<nn; j++) {
2562 if (TMath::Abs(xList[j]-ix0) == 1 || xList[j]*ix0 == -1) neighbx++;
2563 if (TMath::Abs(yList[j]-iy0) == 1 || yList[j]*iy0 == -1) neighby++;
2566 if (cath) neighb = neighbx;
2567 else neighb = neighby;
2568 if (maxpad[0][0] < 0) neighb += neighby;
2569 else if (maxpad[1][0] < 0) neighb += neighbx;
2571 if (!cath) neighb = neighbx;
2572 else neighb = neighby;
2573 if (maxpad[0][0] < 0) neighb += neighbx;
2574 else if (maxpad[1][0] < 0) neighb += neighby;
2577 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2578 if (fPadIJ[0][j] != cath) continue;
2581 if (iy == iy0 && ix == ix0) continue;
2582 for (Int_t k=0; k<nn; k++) {
2583 if (xList[k] != ix || yList[k] != iy) continue;
2585 if ((!cath || maxpad[0][0] < 0) &&
2586 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
2587 if (!iPad && TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) ix1 = xList[k]; //19-12-05
2588 xList[k] = yList[k] = 0;
2592 if ((cath || maxpad[1][0] < 0) &&
2593 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
2594 if (!iPad) ix1 = xList[k]; //19-12-05
2595 xList[k] = yList[k] = 0;
2599 if ((!cath || maxpad[0][0] < 0) &&
2600 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
2601 if (!iPad) ix1 = xList[k]; //19-12-05
2602 xList[k] = yList[k] = 0;
2606 if ((cath || maxpad[1][0] < 0) &&
2607 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
2608 xList[k] = yList[k] = 0;
2613 } // for (Int_t k=0; k<nn;
2615 } // for (Int_t j=0; j<fnPads[0]+fnPads[1];
2616 if (!neighb) continue;
2621 for (Int_t j=0; j<nn; j++) {
2622 if (xList[j] == 0 && yList[j] == 0) continue;
2623 npads = fnPads[0] + fnPads[1];
2624 fPadIJ[0][npads] = cath;
2625 fPadIJ[1][npads] = 0;
2628 if (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) {
2629 if (iy != iy0) continue; // new segmentation - check
2630 if (nInX != 2) continue; // new
2632 if (!cath && maxpad[1][0] >= 0) continue;
2634 if (cath && maxpad[0][0] >= 0) continue;
2636 if (iPad && !iAddX) continue;
2637 fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
2638 if (fXyq[0][npads] > 1.e+5) continue; // temporary fix
2639 if (ix == ix1) continue; //19-12-05
2640 if (ix1 == ix0) continue;
2641 if (maxpad[1][0] < 0 || mirror && maxpad[0][0] >= 0) {
2642 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/100, 5.);
2643 else fXyq[2][npads] = TMath::Min (aamax[0]/100, 5.);
2646 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/100, 5.);
2647 else fXyq[2][npads] = TMath::Min (aamax[1]/100, 5.);
2649 fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
2650 fXyq[3][npads] = -2; // flag
2651 fPadIJ[2][npads] = ix;
2652 fPadIJ[3][npads] = iy;
2655 if (fDebug) printf(" ***** Add virtual pad in X ***** %f %f %f %3d %3d \n", fXyq[2][npads],
2656 fXyq[0][npads], fXyq[1][npads], ix, iy);
2660 if (nInY != 2) continue;
2661 if (!mirror && cath && maxpad[0][0] >= 0) continue;
2662 if (mirror && !cath && maxpad[1][0] >= 0) continue;
2663 if (TMath::Abs(iy-iy0) == 1 || TMath::Abs(iy*iy0) == 1) {
2664 if (ix != ix0) continue; // new segmentation - check
2665 if (iPad && !iAddY) continue;
2666 fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
2667 if (iy1 == iy0) continue;
2668 //if (iPad && iy1 == iy0) continue;
2669 if (maxpad[0][0] < 0 || mirror && maxpad[1][0] >= 0) {
2670 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/15, fgkZeroSuppression);
2671 else fXyq[2][npads] = TMath::Min (aamax[1]/15, fgkZeroSuppression);
2674 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/15, fgkZeroSuppression);
2675 else fXyq[2][npads] = TMath::Min (aamax[0]/15, fgkZeroSuppression);
2677 fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
2678 fXyq[3][npads] = -2; // flag
2679 fPadIJ[2][npads] = ix;
2680 fPadIJ[3][npads] = iy;
2683 if (fDebug) printf(" ***** Add virtual pad in Y ***** %f %f %f %3d %3d \n", fXyq[2][npads],
2684 fXyq[0][npads], fXyq[1][npads], ix, iy);
2687 } // for (Int_t j=0; j<nn;
2688 } // for (Int_t iPad=0;
2689 } // for (cath=0; cath<2;
2693 //_____________________________________________________________________________
2694 void AliMUONClusterFinderAZ::PadsInXandY(Int_t &nInX, Int_t &nInY)
2696 // Find number of pads in X and Y-directions (excluding virtual ones and
2699 static Int_t nXsaved = 0, nYsaved = 0;
2700 nXsaved = nYsaved = 0;
2701 //if (nInX >= 0) {nInX = nXsaved; nInY = nYsaved; return; }
2702 Float_t *xPad0 = NULL, *yPad0 = NULL, *xPad1 = NULL, *yPad1 = NULL;
2703 Float_t wMinX[2] = {99, 99}, wMinY[2] = {99, 99};
2704 Int_t *nPad0 = NULL, *nPad1 = NULL;
2705 Int_t nPads = fnPads[0] + fnPads[1];
2707 xPad0 = new Float_t[nPads];
2708 yPad0 = new Float_t[nPads];
2709 nPad0 = new Int_t[nPads];
2712 xPad1 = new Float_t[nPads];
2713 yPad1 = new Float_t[nPads];
2714 nPad1 = new Int_t[nPads];
2716 Int_t n0 = 0, n1 = 0, cath, npadx[2] = {1, 1}, npady[2] = {1, 1};
2717 for (Int_t j = 0; j < nPads; j++) {
2718 if (nInX < 0 && fPadIJ[1][j] != 0) continue; // before fit
2719 else if (nInX == 0 && fPadIJ[1][j] != 1) continue; // fit - exclude overflows
2720 else if (nInX > 0 && fPadIJ[1][j] != 1 && fPadIJ[1][j] != -9) continue; // exclude non-marked
2721 if (nInX <= 0 && fXyq[2][j] > fgkSaturation-1) continue; // skip overflows
2722 cath = fPadIJ[0][j];
2723 if (fXyq[3][j] > 0) { // exclude virtual pads
2724 wMinX[cath] = TMath::Min (wMinX[cath], fXyq[3][j]);
2725 wMinY[cath] = TMath::Min (wMinY[cath], fXyq[4][j]);
2727 if (cath) { xPad1[n1] = fXyq[0][j]; yPad1[n1++] = fXyq[1][j]; }
2728 else { xPad0[n0] = fXyq[0][j]; yPad0[n0++] = fXyq[1][j]; }
2734 TMath::Sort (n0, xPad0, nPad0); // in X
2735 for (Int_t i = 1; i < n0; i++)
2736 if (xPad0[nPad0[i]] - xPad0[nPad0[i-1]] < -0.01) npadx[0]++;
2737 TMath::Sort (n0, yPad0, nPad0); // in Y
2738 for (Int_t i = 1; i < n0; i++)
2739 if (yPad0[nPad0[i]] - yPad0[nPad0[i-1]] < -0.01) npady[0]++;
2743 TMath::Sort (n1, xPad1, nPad1); // in X
2744 for (Int_t i = 1; i < n1; i++)
2745 if (xPad1[nPad1[i]] - xPad1[nPad1[i-1]] < -0.01) npadx[1]++;
2746 TMath::Sort (n1, yPad1, nPad1); // in Y
2747 for (Int_t i = 1; i < n1; i++)
2748 if (yPad1[nPad1[i]] - yPad1[nPad1[i-1]] < -0.01) npady[1]++;
2750 if (fnPads[0]) { delete [] xPad0; delete [] yPad0; delete [] nPad0; }
2751 if (fnPads[1]) { delete [] xPad1; delete [] yPad1; delete [] nPad1; }
2752 if (TMath::Abs (wMinY[0] - wMinY[1]) < 1.e-3) nInY = TMath::Max (npady[0], npady[1]);
2753 else nInY = wMinY[0] < wMinY[1] ? npady[0] : npady[1];
2754 if (TMath::Abs (wMinX[0] - wMinX[1]) < 1.e-3) nInX = TMath::Max (npadx[0], npadx[1]);
2755 else nInX = wMinX[0] < wMinX[1] ? npadx[0] : npadx[1];
2758 //_____________________________________________________________________________
2759 void AliMUONClusterFinderAZ::Simple()
2761 // Process simple cluster (small number of pads) without EM-procedure
2763 Int_t nForFit = 1, clustFit[1] = {0}, nfit;
2764 Double_t parOk[3] = {0.};
2765 TObjArray *clusters[1];
2766 clusters[0] = fPixArray;
2767 for (Int_t i = 0; i < fnPads[0]+fnPads[1]; i++) {
2768 if (fXyq[2][i] > fgkSaturation-1) fPadIJ[1][i] = -9;
2769 else fPadIJ[1][i] = 1;
2771 nfit = Fit(1, nForFit, clustFit, clusters, parOk);
2774 //_____________________________________________________________________________
2775 void AliMUONClusterFinderAZ::Errors(AliMUONRawCluster *clus)
2777 // Correct reconstructed coordinates for some clusters and evaluate errors
2779 Double_t qTot = clus->GetCharge(0), fmin = clus->GetChi2(0);
2780 Double_t xreco = clus->GetX(0), yreco = clus->GetY(0), zreco = clus->GetZ(0);
2781 Double_t sigmax[2] = {0};
2783 Int_t nInX = 1, nInY, maxdig[2] ={-1, -1}, digit, cath1, isec;
2784 PadsInXandY(nInX, nInY);
2786 // Find pad with maximum signal
2787 for (Int_t cath = 0; cath < 2; cath++) {
2788 for (Int_t j = 0; j < clus->GetMultiplicity(cath); j++) {
2790 digit = clus->GetIndex(j, cath);
2791 if (digit < 0) { cath1 = TMath::Even(cath); digit = -digit - 1; } // from the other cathode
2793 if (clus->GetContrib(j,cath) > sigmax[cath1]) {
2794 sigmax[cath1] = clus->GetContrib(j,cath);
2795 maxdig[cath1] = digit;
2800 // Size of pad with maximum signal and reco coordinate distance from the pad center
2801 AliMUONDigit *mdig = 0;
2802 Double_t wx[2], wy[2], dxc[2], dyc[2];
2803 Float_t xpad, ypad, zpad;
2805 for (Int_t cath = 0; cath < 2; cath++) {
2806 if (maxdig[cath] < 0) continue;
2807 mdig = fInput->Digit(cath,maxdig[cath]);
2808 isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
2809 wx[cath] = fSegmentation[cath]->Dpx(isec);
2810 wy[cath] = fSegmentation[cath]->Dpy(isec);
2811 fSegmentation[cath]->GetPadI(xreco, yreco, zreco, ix, iy);
2812 isec = fSegmentation[cath]->Sector(ix, iy);
2814 fSegmentation[cath]->GetPadC(ix, iy, xpad, ypad, zpad);
2815 dxc[cath] = xreco - xpad;
2816 dyc[cath] = yreco - ypad;
2820 // Check if pad with max charge at the edge (number of neughbours)
2821 Int_t nn, xList[10], yList[10], neighbx[2][2] = {{0,0}, {0,0}}, neighby[2][2]= {{0,0}, {0,0}};
2822 for (Int_t cath = 0; cath < 2; cath++) {
2823 if (maxdig[cath] < 0) continue;
2824 mdig = fInput->Digit(cath,maxdig[cath]);
2825 fSegmentation[cath]->Neighbours(mdig->PadX(), mdig->PadY(), &nn, xList, yList);
2826 isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
2828 Float_t sprX = fResponse->SigmaIntegration() * fResponse->ChargeSpreadX();
2829 Float_t sprY = fResponse->SigmaIntegration() * fResponse->ChargeSpreadY();
2830 //fSegmentation[cath]->FirstPad(fInput->DetElemId(),muons[ihit][1], muons[ihit][2], muons[ihit][3], sprX, sprY);
2831 //fSegmentation[cath]->FirstPad(fInput->DetElemId(),xreco, yreco, zreco, sprX, sprY);
2832 fSegmentation[cath]->FirstPad(xreco, yreco, zreco, sprX, sprY);
2834 //if (fSegmentation[cath]->Sector(fInput->DetElemId(),fSegmentation[cath]->Ix(),fSegmentation[cath]->Iy()) <= 0) {
2835 if (fSegmentation[cath]->Sector(fSegmentation[cath]->Ix(), fSegmentation[cath]->Iy()) <= 0) {
2836 //fSegmentation[cath]->NextPad(fInput->DetElemId());
2837 fSegmentation[cath]->NextPad();
2841 for (Int_t j=0; j<nn; j++) {
2842 //if (border && yList[j] < fSegmentation[cath]->Iy()) continue;
2843 fSegmentation[cath]->GetPadC(xList[j], yList[j], xpad, ypad, zpad);
2844 //cout << ch << " " << xList[j] << " " << yList[j] << " " << border << " " << x << " " << y << " " << xpad << " " << ypad << endl;
2845 if (TMath::Abs(xpad) < 1 && TMath::Abs(ypad) < 1) continue;
2846 if (xList[j] == mdig->PadX()-1 || mdig->PadX() == 1 &&
2847 xList[j] == -1) neighbx[cath][0] = 1;
2848 else if (xList[j] == mdig->PadX()+1 || mdig->PadX() == -1 &&
2849 xList[j] == 1) neighbx[cath][1] = 1;
2850 if (yList[j] == mdig->PadY()-1 || mdig->PadY() == 1 &&
2851 yList[j] == -1) neighby[cath][0] = 1;
2852 else if (yList[j] == mdig->PadY()+1 || mdig->PadY() == -1 &&
2853 yList[j] == 1) neighby[cath][1] = 1;
2854 } // for (Int_t j=0; j<nn;
2855 if (neighbx[cath][0] && neighbx[cath][1]) neighbx[cath][0] = 0;
2856 else if (neighbx[cath][1]) neighbx[cath][0] = -1;
2857 else neighbx[cath][0] = 1;
2858 if (neighby[cath][0] && neighby[cath][1]) neighby[cath][0] = 0;
2859 else if (neighby[cath][1]) neighby[cath][0] = -1;
2860 else neighby[cath][0] = 1;
2863 Int_t iOver = clus->GetClusterType();
2864 // One-sided cluster
2865 if (!clus->GetMultiplicity(0)) {
2866 neighby[0][0] = neighby[1][0];
2868 if (iOver < 99) iOver += 100 * iOver;
2870 } else if (!clus->GetMultiplicity(1)) {
2871 neighbx[1][0] = neighbx[0][0];
2873 if (iOver < 99) iOver += 100 * iOver;
2877 // Apply corrections and evaluate errors
2878 Double_t errY, errX;
2879 Errors(nInY, nInX, neighby[0][0],neighbx[1][0], fmin, wy[0]*10, wx[1]*10, iOver,
2880 dyc[0], dxc[1], qTot, yreco, xreco, errY, errX);
2881 errY = TMath::Max (errY, 0.01);
2883 //errX = TMath::Max (errX, 0.144);
2884 clus->SetX(0, xreco); clus->SetY(0, yreco);
2885 clus->SetErrX(errX); clus->SetErrY(errY);
2888 //_____________________________________________________________________________
2889 void AliMUONClusterFinderAZ::Errors(Int_t ny, Int_t nx, Int_t iby, Int_t ibx, Double_t fmin,
2890 Double_t wy, Double_t wx, Int_t iover,
2891 Double_t dyc, Double_t /*dxc*/, Double_t qtot,
2892 Double_t &yrec, Double_t &xrec, Double_t &erry, Double_t &errx)
2894 // Correct reconstructed coordinates for some clusters and evaluate errors
2898 Int_t iovery = iover % 100;
2905 yrec += iby * (0.1823+0.2008)/2;
2908 // Find "effective pad width"
2909 Double_t width = 0.218 / (1.31e-4 * TMath::Exp (2.688 * TMath::Log(qtot)) + 1) * 2;
2910 width = TMath::Min (width, 0.4);
2911 erry = width / TMath::Sqrt(12.);
2912 erry = TMath::Max (erry, 0.01293);
2917 /* ---> "Bad" fit */
2920 if (ny == 5) erry = 0.06481;
2927 erry = 0.00417; //0.01010
2930 if (dyc * iby > -0.05) {
2931 Double_t dyc2 = dyc * dyc;
2933 corr = 0.019 - 0.602 * dyc + 8.739 * dyc2 - 44.209 * dyc2 * dyc;
2934 corr = TMath::Min (corr, TMath::Abs(-0.25-dyc));
2939 corr = 0.006 + 0.300 * dyc + 6.147 * dyc2 + 42.039 * dyc2 * dyc;
2940 corr = TMath::Min (corr, 0.25-dyc);
2946 erry = (0.00303 + 0.00296) / 2;
2952 /* ---> Overflows */
2959 } else if (TMath::Abs(wy - 5) < 0.1) {
2960 erry = 0.061; //0.06622
2962 erry = 0.00812; // 0.01073
2968 /* ---> "Good" but very high signal */
2970 if (TMath::Abs(wy - 4) < 0.1) {
2972 } else if (fmin < 0.03 && qtot < 6000) {
2980 /* ---> "Good" clusters */
2982 if (TMath::Abs(wy - 5) < 0.1) {
2983 erry = 0.0011; //0.00304
2984 } else if (qtot < 400.) {
2987 erry = 0.00135; // 0.00358
2989 } else if (ny == 3) {
2990 if (TMath::Abs(wy - 4) < 0.1) {
2991 erry = 35.407 / (1 + TMath::Exp(5.511*TMath::Log(qtot/265.51))) + 11.564;
2992 //erry = 83.512 / (1 + TMath::Exp(3.344*TMath::Log(qtot/211.58))) + 12.260;
2994 erry = 147.03 / (1 + TMath::Exp(1.713*TMath::Log(qtot/73.151))) + 9.575;
2995 //erry = 91.743 / (1 + TMath::Exp(2.332*TMath::Log(qtot/151.67))) + 11.453;
3000 if (TMath::Abs(wy - 4) < 0.1) {
3001 erry = 60.800 / (1 + TMath::Exp(3.305*TMath::Log(qtot/104.53))) + 11.702;
3002 //erry = 73.128 / (1 + TMath::Exp(5.676*TMath::Log(qtot/120.93))) + 17.839;
3004 erry = 117.98 / (1 + TMath::Exp(2.005*TMath::Log(qtot/37.649))) + 21.431;
3005 //erry = 99.066 / (1 + TMath::Exp(4.900*TMath::Log(qtot/107.57))) + 25.315;
3012 /* ---> X-coordinate */
3021 if (TMath::Abs(wx - 6) < 0.1) {
3022 if (qtot < 40) errx = 0.1693;
3023 else errx = 0.06241;
3024 } else if (TMath::Abs(wx - 7.5) < 0.1) {
3025 if (qtot < 40) errx = 0.2173;
3026 else errx = 0.07703;
3027 } else if (TMath::Abs(wx - 10) < 0.1) {
3029 if (qtot < 40) errx = 0.2316;
3032 xrec += (0.2115 + 0.1942) / 2 * ibx;
3038 /* ---> "Bad" fit */
3045 if (ibx > 0) { errx = 0.06761; xrec -= 0.03832; }
3046 else { errx = 0.06653; xrec += 0.02581; }
3049 /* ---> Overflows */
3051 if (TMath::Abs(wx - 6) < 0.1) errx = 0.06979;
3052 else if (TMath::Abs(wx - 7.5) < 0.1) errx = 0.1089;
3053 else if (TMath::Abs(wx - 10) < 0.1) errx = 0.09847;
3057 if (TMath::Abs(wx - 6) < 0.1) errx = 0.06022;
3058 else if (TMath::Abs(wx - 7.5) < 0.1) errx = 0.07247;
3059 else if (TMath::Abs(wx - 10) < 0.1) errx = 0.07359;