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 /// \class AliMUONClusterSplitterMLEM
20 /// Splitter class for the MLEM algorithm...
22 /// FIXME: describe it a little bit more here...
24 /// \author Laurent Aphecetche (for the "new" C++ structure) and
25 /// Alexander Zinchenko, JINR Dubna, for the hardcore of it ;-)
27 #include "AliMUONClusterSplitterMLEM.h"
29 #include "AliMUONCluster.h"
30 #include "AliMUONPad.h"
31 #include "AliMUONPad.h"
32 #include "AliMpStationType.h"
33 #include "AliMUONConstants.h"
34 #include "AliMpDEManager.h"
35 #include "AliMUONMathieson.h"
39 #include <TClonesArray.h>
43 #include <TObjArray.h>
47 ClassImp(AliMUONClusterSplitterMLEM)
49 const Double_t AliMUONClusterSplitterMLEM::fgkCouplMin = 1.e-3; // threshold on coupling
51 //_____________________________________________________________________________
52 AliMUONClusterSplitterMLEM::AliMUONClusterSplitterMLEM(Int_t detElemId,
57 fDetElemId(detElemId),
64 AliMp::StationType stationType = AliMpDEManager::GetStationType(fDetElemId);
66 Float_t kx3 = AliMUONConstants::SqrtKx3();
67 Float_t ky3 = AliMUONConstants::SqrtKy3();
68 Float_t pitch = AliMUONConstants::Pitch();
70 if ( stationType == AliMp::kStation1 )
72 kx3 = AliMUONConstants::SqrtKx3St1();
73 ky3 = AliMUONConstants::SqrtKy3St1();
74 pitch = AliMUONConstants::PitchSt1();
77 fMathieson = new AliMUONMathieson;
79 fMathieson->SetPitch(pitch);
80 fMathieson->SetSqrtKx3AndDeriveKx2Kx4(kx3);
81 fMathieson->SetSqrtKy3AndDeriveKy2Ky4(ky3);
85 //_____________________________________________________________________________
86 AliMUONClusterSplitterMLEM::~AliMUONClusterSplitterMLEM()
91 //_____________________________________________________________________________
93 AliMUONClusterSplitterMLEM::AddBin(TH2 *mlem,
94 Int_t ic, Int_t jc, Int_t mode,
95 Bool_t *used, TObjArray *pix)
97 /// Add a bin to the cluster
99 Int_t nx = mlem->GetNbinsX();
100 Int_t ny = mlem->GetNbinsY();
101 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
102 AliMUONPad *pixPtr = 0;
104 for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
105 for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
106 if (i != ic && j != jc) continue;
107 if (used[(i-1)*nx+j-1]) continue;
108 cont1 = mlem->GetCellContent(j,i);
109 if (mode && cont1 > cont) continue;
110 used[(i-1)*nx+j-1] = kTRUE;
111 if (cont1 < 0.5) continue;
112 if (pix) pix->Add(BinToPix(mlem,j,i));
114 pixPtr = new AliMUONPad (mlem->GetXaxis()->GetBinCenter(j),
115 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
116 fPixArray->Add((TObject*)pixPtr);
118 AddBin(mlem, i, j, mode, used, pix); // recursive call
123 //_____________________________________________________________________________
125 AliMUONClusterSplitterMLEM::AddCluster(Int_t ic, Int_t nclust,
127 Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
129 /// Add a cluster to the group of coupled clusters
131 for (Int_t i=0; i<nclust; i++) {
132 if (used[i]) continue;
133 if (aijcluclu(i,ic) < fgkCouplMin) continue;
135 clustNumb[nCoupled++] = i;
136 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
140 //_____________________________________________________________________________
142 AliMUONClusterSplitterMLEM::BinToPix(TH2 *mlem,
145 /// Translate histogram bin to pixel
147 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
148 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
150 Int_t nPix = fPixArray->GetEntriesFast();
151 AliMUONPad *pixPtr = NULL;
153 // Compare pixel and bin positions
154 for (Int_t i=0; i<nPix; i++) {
155 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
156 if (pixPtr->Charge() < 0.5) continue;
157 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4)
159 return (TObject*) pixPtr;
162 AliError(Form(" Something wrong ??? %f %f ", xc, yc));
166 //_____________________________________________________________________________
168 AliMUONClusterSplitterMLEM::ChargeIntegration(Double_t x, Double_t y,
169 const AliMUONPad& pad)
171 /// Compute the Mathieson integral on pad area, assuming the center
172 /// of the Mathieson is at (x,y)
174 TVector2 lowerLeft(TVector2(x,y)-pad.Position()-pad.Dimensions());
175 TVector2 upperRight(lowerLeft + pad.Dimensions()*2.0);
177 return fMathieson->IntXY(lowerLeft.X(),lowerLeft.Y(),
178 upperRight.X(),upperRight.Y());
181 //_____________________________________________________________________________
183 AliMUONClusterSplitterMLEM::Fcn1(const AliMUONCluster& cluster,
184 Int_t & /*fNpar*/, Double_t * /*gin*/,
185 Double_t &f, Double_t *par, Int_t /*iflag*/)
187 /// Fit for one track
190 Double_t charge, delta, coef=0, chi2=0, qTot = 0;
192 for (Int_t j=0; j< cluster.Multiplicity(); ++j)
194 AliMUONPad* pad = cluster.Pad(j);
195 if ( pad->Status() != 1 ) continue;
196 if ( pad->DX() > 0 ) npads++; // exclude virtual pads
197 qTot += pad->Charge(); // c.fXyq[2][j];
199 for (Int_t i=fNpar/3; i>=0; --i)
201 indx = i<2 ? 2*i : 2*i+1;
208 coef = i==fNpar/3 ? par[indx+2] : 1-coef;
210 coef = TMath::Max (coef, 0.);
211 if ( fNpar == 8 && i < 2)
213 coef = i==1 ? coef*par[indx+2] : coef - par[7];
215 coef = TMath::Max (coef, 0.);
216 charge += ChargeIntegration(par[indx],par[indx+1],*pad);
219 delta = charge - pad->Charge(); //c.fXyq[2][j];
221 delta /= pad->Charge(); //c.fXyq[2][j];
225 Double_t qAver = qTot/npads;
229 //_____________________________________________________________________________
231 AliMUONClusterSplitterMLEM::Fit(const AliMUONCluster& cluster,
232 Int_t iSimple, Int_t nfit,
233 Int_t *clustFit, TObjArray **clusters,
235 TObjArray& clusterList)
237 /// Find selected clusters to selected pad charges
239 // AliDebug(2,Form("iSimple=%d nfit=%d",iSimple,nfit));
241 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
242 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
243 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
244 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
245 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
246 Double_t step[3]={0.01,0.002,0.02}, xPad = 0, yPad = 99999;
248 // Number of pads to use and number of virtual pads
249 Int_t npads = 0, nVirtual = 0, nfit0 = nfit;
250 for (Int_t i=0; i<cluster.Multiplicity(); ++i )
252 AliMUONPad* pad = cluster.Pad(i);
253 if ( pad->DX() < 0 ) ++nVirtual;
254 if ( pad->Status() !=1 ) continue;
260 xPad = pad->X();//fXyq[0][i];
261 yPad = pad->Y();//fXyq[1][i];
265 if (pad->DY() < pad->DX() ) //fXyq[4][i] < fXyq[3][i])
267 yPad = pad->Y();//fXyq[1][i];
271 xPad = pad->X();//fXyq[0][i];
280 if (npads < 2) return 0;
282 // FIXME : AliWarning("Reconnect the following code for hit/track passing ?");
284 // Int_t tracks[3] = {-1, -1, -1};
288 AliMUONDigit *mdig = 0;
289 for (Int_t cath=0; cath<2; cath++) {
290 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
291 if (fPadIJ[0][i] != cath) continue;
292 if (fPadIJ[1][i] != 1) continue;
293 if (fXyq[3][i] < 0) continue; // exclude virtual pads
294 digit = TMath::Nint (fXyq[5][i]);
295 if (digit >= 0) mdig = fInput->Digit(cath,digit);
296 else mdig = fInput->Digit(TMath::Even(cath),-digit-1);
297 //if (!mdig) mdig = fInput->Digit(TMath::Even(cath),digit);
298 if (!mdig) continue; // protection for cluster display
299 if (mdig->Hit() >= 0) {
301 tracks[0] = mdig->Hit();
302 tracks[1] = mdig->Track(0);
303 } else if (mdig->Track(0) < tracks[1]) {
304 tracks[0] = mdig->Hit();
305 tracks[1] = mdig->Track(0);
308 if (mdig->Track(1) >= 0 && mdig->Track(1) != tracks[1]) {
309 if (tracks[2] < 0) tracks[2] = mdig->Track(1);
310 else tracks[2] = TMath::Min (tracks[2], mdig->Track(1));
313 } // for (Int_t cath=0;
316 // Get number of pads in X and Y
317 // Int_t nInX = 0, nInY;
318 // PadsInXandY(cluster,nInX, nInY);
319 const Int_t kStatusToTest(1);
321 AliMpIntPair nofPads = cluster.NofPads(kStatusToTest);
322 Int_t nInX = nofPads.GetFirst();
323 Int_t nInY = nofPads.GetSecond();
324 //cout << " nInX and Y: " << nInX << " " << nInY << endl;
327 nfitMax = TMath::Min (nfitMax, (npads + 1) / 3);
329 if (nInX < 3 && nInY < 3 || nInX == 3 && nInY < 3 || nInX < 3 && nInY == 3) nfitMax = 1; // not enough pads in each direction
331 if (nfit > nfitMax) nfit = nfitMax;
333 // Take cluster maxima as fitting seeds
337 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8], qq = 0;
338 Double_t xyseed[3][2], qseed[3], xyCand[3][2] = {{0},{0}}, sigCand[3][2] = {{0},{0}};
340 for (Int_t ifit=1; ifit<=nfit0; ifit++)
343 pix = clusters[clustFit[ifit-1]];
344 npxclu = pix->GetEntriesFast();
346 for (Int_t clu=0; clu<npxclu; ++clu)
348 pixPtr = (AliMUONPad*) pix->UncheckedAt(clu);
349 cont = pixPtr->Charge();
354 xseed = pixPtr->Coord(0);
355 yseed = pixPtr->Coord(1);
358 xyCand[0][0] += pixPtr->Coord(0) * cont;
359 xyCand[0][1] += pixPtr->Coord(1) * cont;
360 sigCand[0][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
361 sigCand[0][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
363 xyseed[ifit-1][0] = xseed;
364 xyseed[ifit-1][1] = yseed;
365 qseed[ifit-1] = cmax;
366 } // for (Int_t ifit=1;
368 xyCand[0][0] /= qq; // <x>
369 xyCand[0][1] /= qq; // <y>
370 sigCand[0][0] = sigCand[0][0]/qq - xyCand[0][0]*xyCand[0][0]; // <x^2> - <x>^2
371 sigCand[0][0] = sigCand[0][0] > 0 ? TMath::Sqrt (sigCand[0][0]) : 0;
372 sigCand[0][1] = sigCand[0][1]/qq - xyCand[0][1]*xyCand[0][1]; // <y^2> - <y>^2
373 sigCand[0][1] = sigCand[0][1] > 0 ? TMath::Sqrt (sigCand[0][1]) : 0;
374 // if (fDebug) cout << xyCand[0][0] << " " << xyCand[0][1] << " " << sigCand[0][0] << " " << sigCand[0][1] << endl;
376 Int_t nDof, maxSeed[3];//, nMax = 0;
377 Double_t fmin, chi2o = 9999, chi2n;
379 TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
381 Double_t *gin = 0, func0, func1, param[8], step0[8];
382 Double_t param0[2][8]={{0},{0}}, deriv[2][8]={{0},{0}};
383 Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
384 Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
385 Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
386 Double_t rad, dist[3] = {0};
388 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
389 // lower, try 3-track (if number of pads is sufficient).
390 for (Int_t iseed=0; iseed<nfit; iseed++)
395 for (Int_t j=0; j<fNpar; j++)
399 } // for bounded params
401 for (Int_t j=0; j<3; j++)
403 step0[fNpar+j] = shift[fNpar+j] = step[j];
408 param[fNpar] = xyCand[0][0]; // take COG
412 param[fNpar] = xyseed[maxSeed[iseed]][0];
414 parmin[fNpar] = xmin;
415 parmax[fNpar++] = xmax;
418 param[fNpar] = xyCand[0][1]; // take COG
422 param[fNpar] = xyseed[maxSeed[iseed]][1];
424 parmin[fNpar] = ymin;
425 parmax[fNpar++] = ymax;
428 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
434 for (Int_t j=0; j<fNpar; j++)
441 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
446 Fcn1(cluster,fNpar, gin, func0, param, 1); nCall++;
447 //cout << " Func: " << func0 << endl;
450 for (Int_t j=0; j<fNpar; j++)
452 param0[max][j] = param[j];
454 param[j] += delta[j] / 10;
455 if (j > 0) param[j-1] -= delta[j-1] / 10;
456 Fcn1(cluster,fNpar, gin, func1, param, 1); nCall++;
457 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
458 //cout << j << " " << deriv[max][j] << endl;
459 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
460 (param0[0][j] - param0[1][j]) : 0; // second derivative
462 param[fNpar-1] -= delta[fNpar-1] / 10;
463 if (nCall > 2000) break;
465 min = func2[0] < func2[1] ? 0 : 1;
466 nFail = min == max ? 0 : nFail + 1;
468 stepMax = derMax = estim = 0;
469 for (Int_t j=0; j<fNpar; j++)
471 // Estimated distance to minimum
475 shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
477 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3)
481 else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
482 || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3 || TMath::Abs(dder[j]) < 1.e-6)
484 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
496 shift[j] = dder[j] != 0 ? -deriv[min][j] / dder[j] : 0;
500 if (TMath::Abs(shift[j])/step0[j] > estim)
502 estim = TMath::Abs(shift[j])/step0[j];
507 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
509 // Failed to improve minimum
513 param[j] = param0[min][j];
514 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j])
516 shift[j] = (shift[j] + shift0) / 2;
525 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
527 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
530 // Introduce step relaxation factor
533 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
534 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
536 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
539 param[j] += shift[j];
540 //MLEM Check parameter limits 27-12-2004
541 if (param[j] < parmin[j])
543 shift[j] = parmin[j] - param[j];
544 param[j] = parmin[j];
546 else if (param[j] > parmax[j])
548 shift[j] = parmax[j] - param[j];
549 param[j] = parmax[j];
551 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
552 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
553 if (TMath::Abs(deriv[min][j]) > derMax)
556 derMax = TMath::Abs (deriv[min][j]);
558 } // for (Int_t j=0; j<fNpar;
560 if (estim < 1 && derMax < 2 || nLoop > 150) break; // minimum was found
564 // Check for small step
565 if (shift[idMax] == 0)
567 shift[idMax] = step0[idMax]/10;
568 param[idMax] += shift[idMax];
572 if (!memory[idMax] && derMax > 0.5 && nLoop > 10)
574 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10)
576 if (min == max) dder[idMax] = -dder[idMax];
577 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
578 param[idMax] += shift[idMax];
579 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
580 if (min == max) shiftSave = shift[idMax];
584 param[idMax] -= shift[idMax];
585 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
586 param[idMax] += shift[idMax];
593 nDof = npads - fNpar + nVirtual;
596 // if (fDebug) cout << " Chi2 " << chi2n << " " << fNpar << endl;
598 if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
600 // Save parameters and errors
603 // One pad per direction
604 for (Int_t i=0; i<fNpar; i++) if (i == 0 || i == 2 || i == 5) param0[min][i] = xPad;
607 // One pad per direction
608 for (Int_t i=0; i<fNpar; i++) if (i == 1 || i == 3 || i == 6) param0[min][i] = yPad;
613 // Find distance to the nearest neighbour
614 dist[0] = dist[1] = TMath::Sqrt ((param0[min][0]-param0[min][2])*
615 (param0[min][0]-param0[min][2])
616 +(param0[min][1]-param0[min][3])*
617 (param0[min][1]-param0[min][3]));
619 dist[2] = TMath::Sqrt ((param0[min][0]-param0[min][5])*
620 (param0[min][0]-param0[min][5])
621 +(param0[min][1]-param0[min][6])*
622 (param0[min][1]-param0[min][6]));
623 rad = TMath::Sqrt ((param0[min][2]-param0[min][5])*
624 (param0[min][2]-param0[min][5])
625 +(param0[min][3]-param0[min][6])*
626 (param0[min][3]-param0[min][6]));
627 if (dist[2] < dist[0]) dist[0] = dist[2];
628 if (rad < dist[1]) dist[1] = rad;
629 if (rad < dist[2]) dist[2] = rad;
631 cout << dist[0] << " " << dist[1] << " " << dist[2] << endl;
632 if (dist[TMath::LocMin(iseed+1,dist)] < 1.) { fNpar -= 3; break; }
636 for (Int_t i=0; i<fNpar; i++) {
637 parOk[i] = param0[min][i];
641 parOk[i] = TMath::Max (parOk[i], parmin[i]);
642 parOk[i] = TMath::Min (parOk[i], parmax[i]);
646 if (fmin < 0.1) break; // !!!???
647 } // for (Int_t iseed=0;
650 // for (Int_t i=0; i<fNpar; i++) {
651 // if (i == 4 || i == 7) {
652 // if (i == 7 || i == 4 && fNpar < 7) cout << parOk[i] << endl;
653 // else cout << parOk[i] * (1-parOk[7]) << endl;
656 // cout << parOk[i] << " " << errOk[i] << endl;
659 nfit = (fNpar + 1) / 3;
660 dist[0] = dist[1] = dist[2] = 0;
663 // Find distance to the nearest neighbour
664 dist[0] = dist[1] = TMath::Sqrt ((parOk[0]-parOk[2])*
666 +(parOk[1]-parOk[3])*
667 (parOk[1]-parOk[3]));
669 dist[2] = TMath::Sqrt ((parOk[0]-parOk[5])*
671 +(parOk[1]-parOk[6])*
672 (parOk[1]-parOk[6]));
673 rad = TMath::Sqrt ((parOk[2]-parOk[5])*
675 +(parOk[3]-parOk[6])*
676 (parOk[3]-parOk[6]));
677 if (dist[2] < dist[0]) dist[0] = dist[2];
678 if (rad < dist[1]) dist[1] = rad;
679 if (rad < dist[2]) dist[2] = rad;
688 if (iSimple) fnCoupled = 0;
689 //for (Int_t j=0; j<nfit; j++) {
690 for (Int_t j=nfit-1; j>=0; j--) {
691 indx = j<2 ? j*2 : j*2+1;
692 if (nfit == 1) coef = 1;
693 else coef = j==nfit-1 ? parOk[indx+2] : 1-coef;
694 coef = TMath::Max (coef, 0.);
695 if (nfit == 3 && j < 2) coef = j==1 ? coef*parOk[indx+2] : coef - parOk[7];
696 coef = TMath::Max (coef, 0.);
698 //void AliMUONClusterFinderMLEM::AddRawCluster(Double_t x, Double_t y,
699 // Double_t qTot, Double_t fmin,
700 // Int_t nfit, Int_t *tracks,
701 // Double_t /*sigx*/,
702 // Double_t /*sigy*/,
703 // Double_t /*dist*/)
705 if ( coef*fQtot >= 14 )
707 AliMUONCluster* cluster = new AliMUONCluster();
709 cluster->SetCharge(coef*fQtot,coef*fQtot);
710 cluster->SetPosition(TVector2(parOk[indx],parOk[indx+1]),TVector2(sigCand[0][0],sigCand[0][1]));
711 cluster->SetChi2(dist[TMath::LocMin(nfit,dist)]);
713 // FIXME: we miss some information in this cluster, as compared to
714 // the original AddRawCluster code.
716 AliDebug(2,Form("Adding RawCluster detElemId %4d mult %2d charge %5d (xl,yl)=(%9.6g,%9.6g)",
717 fDetElemId,cluster->Multiplicity(),(Int_t)cluster->Charge(),
718 cluster->Position().X(),cluster->Position().Y()));
720 clusterList.Add(cluster);
722 // AddRawCluster (parOk[indx], // double x
723 // parOk[indx+1], // double y
724 // coef*qTot, // double charge
725 // errOk[indx], // double fmin
726 // nfit0+10*nfit+100*nMax+10000*fnCoupled, // int nfit
727 // tracks, // int* tracks
728 // sigCand[0][0], // double sigx
729 // sigCand[0][1], // double sigy
730 // dist[TMath::LocMin(nfit,dist)] // double dist
737 //_____________________________________________________________________________
739 AliMUONClusterSplitterMLEM::Split(const AliMUONCluster& cluster,
742 TObjArray& clusterList)
744 /// The main steering function to work with clusters of pixels in anode
745 /// plane (find clusters, decouple them from each other, merge them (if
746 /// necessary), pick up coupled pads, call the fitting function)
748 Int_t nx = mlem->GetNbinsX();
749 Int_t ny = mlem->GetNbinsY();
750 Int_t nPix = fPixArray->GetEntriesFast();
752 Bool_t *used = new Bool_t[ny*nx];
754 Int_t nclust = 0, indx, indx1;
756 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
758 TObjArray *clusters[200]={0};
761 // Find clusters of histogram bins (easier to work in 2-D space)
762 for (Int_t i=1; i<=ny; i++)
764 for (Int_t j=1; j<=nx; j++)
766 indx = (i-1)*nx + j - 1;
767 if (used[indx]) continue;
768 cont = mlem->GetCellContent(j,i);
769 if (cont < 0.5) continue;
770 pix = new TObjArray(20);
772 pix->Add(BinToPix(mlem,j,i));
773 AddBin(mlem, i, j, 0, used, pix); // recursive call
774 if (nclust >= 200) AliFatal(" Too many clusters !!!");
775 clusters[nclust++] = pix;
776 } // for (Int_t j=1; j<=nx; j++) {
777 } // for (Int_t i=1; i<=ny;
778 // if (fDebug) cout << nclust << endl;
779 delete [] used; used = 0;
781 // Compute couplings between clusters and clusters to pads
782 Int_t npad = cluster.Multiplicity();
784 // Exclude pads with overflows
785 for (Int_t j=0; j<npad; ++j)
787 AliMUONPad* pad = cluster.Pad(j);
788 if ( pad->IsSaturated() )
798 // Compute couplings of clusters to pads
799 TMatrixD aijclupad(nclust,npad);
802 for (Int_t iclust=0; iclust<nclust; ++iclust)
804 pix = clusters[iclust];
805 npxclu = pix->GetEntriesFast();
806 for (Int_t i=0; i<npxclu; ++i)
808 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
809 for (Int_t j=0; j<npad; ++j)
811 AliMUONPad* pad = cluster.Pad(j);
812 if ( pad->Status() < 0 && pad->Status() != -5) continue;
813 if (coef[j*nPix+indx] < fgkCouplMin) continue;
814 aijclupad(iclust,j) += coef[j*nPix+indx];
819 // Compute couplings between clusters
820 TMatrixD aijcluclu(nclust,nclust);
822 for (Int_t iclust=0; iclust<nclust; ++iclust)
824 for (Int_t j=0; j<npad; ++j)
827 if ( cluster.Pad(j)->Status() < 0) continue;
828 if (aijclupad(iclust,j) < fgkCouplMin) continue;
829 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++)
831 if (aijclupad(iclust1,j) < fgkCouplMin) continue;
832 aijcluclu(iclust,iclust1) +=
833 TMath::Sqrt (aijclupad(iclust,j)*aijclupad(iclust1,j));
837 for (Int_t iclust=0; iclust<nclust; ++iclust)
839 for (Int_t iclust1=iclust+1; iclust1<nclust; ++iclust1)
841 aijcluclu(iclust1,iclust) = aijcluclu(iclust,iclust1);
845 // Find groups of coupled clusters
846 used = new Bool_t[nclust];
847 for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
848 Int_t *clustNumb = new Int_t[nclust];
849 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
852 for (Int_t igroup=0; igroup<nclust; igroup++)
854 if (used[igroup]) continue;
855 used[igroup] = kTRUE;
856 clustNumb[0] = igroup;
858 // Find group of coupled clusters
859 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
862 // cout << " nCoupled: " << nCoupled << endl;
863 // for (Int_t i=0; i<nCoupled; i++) cout << clustNumb[i] << " "; cout << endl;
866 fnCoupled = nCoupled;
873 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
877 // Too many coupled clusters to fit - try to decouple them
878 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
879 // all the others in the group
880 for (Int_t j=0; j<3; j++) minGroup[j] = -1;
881 /*Double_t coupl =*/ MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
883 // Flag clusters for fit
885 while (minGroup[nForFit] >= 0 && nForFit < 3)
887 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
888 clustNumb[minGroup[nForFit]] -= 999;
893 // Select pads for fit.
894 if (SelectPad(cluster,nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1)
897 for (Int_t j=0; j<npad; ++j)
899 AliMUONPad* pad = cluster.Pad(j);
900 if ( pad->Status()==1 ) pad->SetStatus(0);
901 if ( pad->Status()==-9) pad->SetStatus(-5);
903 // Merge the failed cluster candidates (with too few pads to fit) with
904 // the one with the strongest coupling
905 Merge(cluster,nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
910 nfit = Fit(cluster,0, nForFit, clustFit, clusters, parOk, clusterList);
913 // Subtract the fitted charges from pads with strong coupling and/or
914 // return pads for further use
915 UpdatePads(cluster,nfit, parOk);
918 for (Int_t j=0; j<npad; ++j)
920 AliMUONPad* pad = cluster.Pad(j);
921 if ( pad->Status()==1 ) pad->SetStatus(-1);
922 if ( pad->Status()==-9) pad->SetStatus(-5);
925 // Sort the clusters (move to the right the used ones)
926 Int_t beg = 0, end = nCoupled - 1;
929 if (clustNumb[beg] >= 0) { ++beg; continue; }
930 for (Int_t j=end; j>beg; --j)
932 if (clustNumb[j] < 0) continue;
934 indx = clustNumb[beg];
935 clustNumb[beg] = clustNumb[j];
945 // Remove couplings of used clusters
946 for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit;++ iclust)
948 indx = clustNumb[iclust] + 999;
949 for (Int_t iclust1=0; iclust1<nCoupled; ++iclust1)
951 indx1 = clustNumb[iclust1];
952 aijcluclu(indx,indx1) = aijcluclu(indx1,indx) = 0;
956 // Update the remaining clusters couplings (exclude couplings from
958 for (Int_t j=0; j<npad; ++j)
960 AliMUONPad* pad = cluster.Pad(j);
961 if ( pad->Status() != -1) continue;
962 for (Int_t iclust=0; iclust<nCoupled; ++iclust)
964 indx = clustNumb[iclust];
965 if (aijclupad(indx,j) < fgkCouplMin) continue;
966 for (Int_t iclust1=iclust+1; iclust1<nCoupled; ++iclust1)
968 indx1 = clustNumb[iclust1];
969 if (aijclupad(indx1,j) < fgkCouplMin) continue;
971 aijcluclu(indx,indx1) -=
972 TMath::Sqrt (aijclupad(indx,j)*aijclupad(indx1,j));
973 aijcluclu(indx1,indx) = aijcluclu(indx,indx1);
977 } // for (Int_t j=0; j<npad;
978 } // if (nCoupled > 3)
979 } // while (nCoupled > 0)
980 } // for (Int_t igroup=0; igroup<nclust;
982 for (Int_t iclust=0; iclust<nclust; iclust++)
984 pix = clusters[iclust];
996 //_____________________________________________________________________________
998 AliMUONClusterSplitterMLEM::Merge(const AliMUONCluster& cluster,
999 Int_t nForFit, Int_t nCoupled,
1000 Int_t *clustNumb, Int_t *clustFit,
1001 TObjArray **clusters,
1002 TMatrixD& aijcluclu, TMatrixD& aijclupad)
1004 /// Merge the group of clusters with the one having the strongest coupling with them
1006 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1007 TObjArray *pix, *pix1;
1010 for (Int_t icl=0; icl<nForFit; ++icl)
1012 indx = clustFit[icl];
1013 pix = clusters[indx];
1014 npxclu = pix->GetEntriesFast();
1016 for (Int_t icl1=0; icl1<nCoupled; ++icl1)
1018 indx1 = clustNumb[icl1];
1019 if (indx1 < 0) continue;
1020 if ( aijcluclu(indx,indx1) > couplMax)
1022 couplMax = aijcluclu(indx,indx1);
1025 } // for (Int_t icl1=0;
1027 pix1 = clusters[imax];
1028 npxclu1 = pix1->GetEntriesFast();
1030 for (Int_t i=0; i<npxclu; ++i)
1032 pix1->Add(pix->UncheckedAt(i));
1036 //Add cluster-to-cluster couplings
1037 for (Int_t icl1=0; icl1<nCoupled; ++icl1)
1039 indx1 = clustNumb[icl1];
1040 if (indx1 < 0 || indx1 == imax) continue;
1041 aijcluclu(indx1,imax) += aijcluclu(indx,indx1);
1042 aijcluclu(imax,indx1) = aijcluclu(indx1,imax);
1044 aijcluclu(indx,imax) = aijcluclu(imax,indx) = 0;
1046 //Add cluster-to-pad couplings
1047 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1049 AliMUONPad* pad = cluster.Pad(j);
1050 if ( pad->Status() < 0 && pad->Status() != -5 ) continue;// exclude used pads
1051 aijclupad(imax,j) += aijclupad(indx,j);
1052 aijclupad(indx,j) = 0;
1054 } // for (Int_t icl=0; icl<nForFit;
1058 //_____________________________________________________________________________
1060 AliMUONClusterSplitterMLEM::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb,
1061 TMatrixD& aijcluclu, Int_t *minGroup)
1063 /// Find group of clusters with minimum coupling to all the others
1065 Int_t i123max = TMath::Min(3,nCoupled/2);
1066 Int_t indx, indx1, indx2, indx3, nTot = 0;
1067 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1069 for (Int_t i123=1; i123<=i123max; i123++) {
1072 coupl1 = new Double_t [nCoupled];
1073 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1075 else if (i123 == 2) {
1076 nTot = nCoupled*nCoupled;
1077 coupl2 = new Double_t [nTot];
1078 for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1080 nTot = nTot*nCoupled;
1081 coupl3 = new Double_t [nTot];
1082 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1085 for (Int_t i=0; i<nCoupled; i++) {
1086 indx1 = clustNumb[i];
1087 for (Int_t j=i+1; j<nCoupled; j++) {
1088 indx2 = clustNumb[j];
1090 coupl1[i] += aijcluclu(indx1,indx2);
1091 coupl1[j] += aijcluclu(indx1,indx2);
1093 else if (i123 == 2) {
1094 indx = i*nCoupled + j;
1095 coupl2[indx] = coupl1[i] + coupl1[j];
1096 coupl2[indx] -= 2 * (aijcluclu(indx1,indx2));
1098 for (Int_t k=j+1; k<nCoupled; k++) {
1099 indx3 = clustNumb[k];
1100 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1101 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1102 coupl3[indx] -= 2 * (aijcluclu(indx1,indx3)+aijcluclu(indx2,indx3));
1105 } // for (Int_t j=i+1;
1106 } // for (Int_t i=0;
1107 } // for (Int_t i123=1;
1109 // Find minimum coupling
1110 Double_t couplMin = 9999;
1113 for (Int_t i123=1; i123<=i123max; i123++) {
1115 locMin = TMath::LocMin(nCoupled, coupl1);
1116 couplMin = coupl1[locMin];
1117 minGroup[0] = locMin;
1118 delete [] coupl1; coupl1 = 0;
1120 else if (i123 == 2) {
1121 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1122 if (coupl2[locMin] < couplMin) {
1123 couplMin = coupl2[locMin];
1124 minGroup[0] = locMin/nCoupled;
1125 minGroup[1] = locMin%nCoupled;
1127 delete [] coupl2; coupl2 = 0;
1129 locMin = TMath::LocMin(nTot, coupl3);
1130 if (coupl3[locMin] < couplMin) {
1131 couplMin = coupl3[locMin];
1132 minGroup[0] = locMin/nCoupled/nCoupled;
1133 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1134 minGroup[2] = locMin%nCoupled;
1136 delete [] coupl3; coupl3 = 0;
1138 } // for (Int_t i123=1;
1142 //_____________________________________________________________________________
1144 AliMUONClusterSplitterMLEM::SelectPad(const AliMUONCluster& cluster,
1145 Int_t nCoupled, Int_t nForFit,
1146 Int_t *clustNumb, Int_t *clustFit,
1147 TMatrixD& aijclupad)
1149 /// Select pads for fit. If too many coupled clusters, find pads giving
1150 /// the strongest coupling with the rest of clusters and exclude them from the fit.
1152 Int_t npad = cluster.Multiplicity();
1153 Double_t *padpix = 0;
1157 padpix = new Double_t[npad];
1158 for (Int_t i=0; i<npad; i++) padpix[i] = 0;
1161 Int_t nOK = 0, indx, indx1;
1162 for (Int_t iclust=0; iclust<nForFit; ++iclust)
1164 indx = clustFit[iclust];
1165 for (Int_t j=0; j<npad; j++)
1167 if ( aijclupad(indx,j) < fgkCouplMin) continue;
1168 AliMUONPad* pad = cluster.Pad(j);
1169 if ( pad->Status() == -5 ) pad->SetStatus(-0); // flag overflow
1170 if ( pad->Status() < 0 ) continue; // exclude overflows and used pads
1171 if ( !pad->Status() )
1174 ++nOK; // pad to be used in fit
1178 // Check other clusters
1179 for (Int_t iclust1=0; iclust1<nCoupled; ++iclust1)
1181 indx1 = clustNumb[iclust1];
1182 if (indx1 < 0) continue;
1183 if ( aijclupad(indx1,j) < fgkCouplMin ) continue;
1184 padpix[j] += aijclupad(indx1,j);
1186 } // if (nCoupled > 3)
1187 } // for (Int_t j=0; j<npad;
1188 } // for (Int_t iclust=0; iclust<nForFit
1189 if (nCoupled < 4) return nOK;
1192 for (Int_t j=0; j<npad; ++j)
1194 if (padpix[j] < fgkCouplMin) continue;
1196 cluster.Pad(j)->SetStatus(-1); // exclude pads with strong coupling to the other clusters
1204 //_____________________________________________________________________________
1206 AliMUONClusterSplitterMLEM::UpdatePads(const AliMUONCluster& cluster,
1207 Int_t /*nfit*/, Double_t *par)
1209 /// Subtract the fitted charges from pads with strong coupling
1212 Double_t charge, coef=0;
1214 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1216 AliMUONPad* pad = cluster.Pad(j);
1217 if ( pad->Status() != 1 ) continue;
1221 for (Int_t i=fNpar/3; i>=0; --i)
1224 indx = i<2 ? 2*i : 2*i+1;
1231 coef = i==fNpar/3 ? par[indx+2] : 1-coef;
1233 coef = TMath::Max (coef, 0.);
1234 if (fNpar == 8 && i < 2)
1236 coef = i==1 ? coef*par[indx+2] : coef - par[7];
1238 coef = TMath::Max (coef, 0.);
1239 charge += ChargeIntegration(par[indx],par[indx+1],*pad);
1242 pad->SetCharge(pad->Charge()-charge);
1243 } // if (fNpar != 0)
1245 if (pad->Charge() > 6 /*fgkZeroSuppression*/) pad->SetStatus(0);
1246 // return pad for further using // FIXME: remove usage of zerosuppression here
1248 } // for (Int_t j=0;