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 //-----------------------------------------------------------------------------
19 /// \class AliMUONClusterSplitterMLEM
21 /// Splitter class for the MLEM algorithm. Performs fitting procedure
22 /// with up to 3 hit candidates and tries to split clusters if the number
23 /// of candidates exceeds 3.
25 /// \author Laurent Aphecetche (for the "new" C++ structure) and
26 /// Alexander Zinchenko, JINR Dubna, for the hardcore of it ;-)
27 //-----------------------------------------------------------------------------
29 #include "AliMUONClusterSplitterMLEM.h"
30 #include "AliMUONClusterFinderMLEM.h" // for status flag constants
32 #include "AliMUONCluster.h"
33 #include "AliMUONPad.h"
34 #include "AliMUONPad.h"
35 #include "AliMUONConstants.h"
36 #include "AliMpDEManager.h"
37 #include "AliMUONMathieson.h"
39 #include "AliMpEncodePair.h"
43 #include <TClonesArray.h>
47 #include <TObjArray.h>
49 #include <Riostream.h>
52 ClassImp(AliMUONClusterSplitterMLEM)
55 //const Double_t AliMUONClusterSplitterMLEM::fgkCouplMin = 1.e-3; // threshold on coupling
56 const Double_t AliMUONClusterSplitterMLEM::fgkCouplMin = 1.e-2; // threshold on coupling
58 //_____________________________________________________________________________
59 AliMUONClusterSplitterMLEM::AliMUONClusterSplitterMLEM(Int_t detElemId,
61 Double_t lowestPixelCharge,
62 Double_t lowestPadCharge,
63 Double_t lowestClusterCharge)
67 fDetElemId(detElemId),
72 fLowestPixelCharge(lowestPixelCharge),
73 fLowestPadCharge(lowestPadCharge),
74 fLowestClusterCharge(lowestClusterCharge)
78 AliMq::Station12Type stationType = AliMpDEManager::GetStation12Type(fDetElemId);
80 Float_t kx3 = AliMUONConstants::SqrtKx3();
81 Float_t ky3 = AliMUONConstants::SqrtKy3();
82 Float_t pitch = AliMUONConstants::Pitch();
84 if ( stationType == AliMq::kStation1 )
86 kx3 = AliMUONConstants::SqrtKx3St1();
87 ky3 = AliMUONConstants::SqrtKy3St1();
88 pitch = AliMUONConstants::PitchSt1();
91 fMathieson = new AliMUONMathieson;
93 fMathieson->SetPitch(pitch);
94 fMathieson->SetSqrtKx3AndDeriveKx2Kx4(kx3);
95 fMathieson->SetSqrtKy3AndDeriveKy2Ky4(ky3);
99 //_____________________________________________________________________________
100 AliMUONClusterSplitterMLEM::~AliMUONClusterSplitterMLEM()
107 //_____________________________________________________________________________
109 AliMUONClusterSplitterMLEM::AddBin(TH2 *mlem,
110 Int_t ic, Int_t jc, Int_t mode,
111 Bool_t *used, TObjArray *pix)
113 /// Add a bin to the cluster
115 Int_t nx = mlem->GetNbinsX();
116 Int_t ny = mlem->GetNbinsY();
117 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
118 AliMUONPad *pixPtr = 0;
120 Int_t ie = TMath::Min(ic+1,ny), je = TMath::Min(jc+1,nx);
121 for (Int_t i = TMath::Max(ic-1,1); i <= ie; ++i) {
122 for (Int_t j = TMath::Max(jc-1,1); j <= je; ++j) {
123 if (i != ic && j != jc) continue;
124 if (used[(i-1)*nx+j-1]) continue;
125 cont1 = mlem->GetCellContent(j,i);
126 if (mode && cont1 > cont) continue;
127 used[(i-1)*nx+j-1] = kTRUE;
128 if (cont1 < fLowestPixelCharge) continue;
129 if (pix) pix->Add(BinToPix(mlem,j,i));
131 pixPtr = new AliMUONPad (mlem->GetXaxis()->GetBinCenter(j),
132 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
133 fPixArray->Add(pixPtr);
135 AddBin(mlem, i, j, mode, used, pix); // recursive call
140 //_____________________________________________________________________________
142 AliMUONClusterSplitterMLEM::AddCluster(Int_t ic, Int_t nclust,
144 Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
146 /// Add a cluster to the group of coupled clusters
148 for (Int_t i = 0; i < nclust; ++i) {
149 if (used[i]) continue;
150 if (aijcluclu(i,ic) < fgkCouplMin) continue;
152 clustNumb[nCoupled++] = i;
153 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
157 //_____________________________________________________________________________
159 AliMUONClusterSplitterMLEM::BinToPix(TH2 *mlem,
162 /// Translate histogram bin to pixel
164 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
165 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
167 Int_t nPix = fPixArray->GetEntriesFast();
168 AliMUONPad *pixPtr = NULL;
170 // Compare pixel and bin positions
171 for (Int_t i = 0; i < nPix; ++i) {
172 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
173 if (pixPtr->Charge() < fLowestPixelCharge) continue;
174 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4)
176 //return (TObject*) pixPtr;
180 AliError(Form(" Something wrong ??? %f %f ", xc, yc));
184 //_____________________________________________________________________________
186 AliMUONClusterSplitterMLEM::ChargeIntegration(Double_t x, Double_t y,
187 const AliMUONPad& pad)
189 /// Compute the Mathieson integral on pad area, assuming the center
190 /// of the Mathieson is at (x,y)
192 TVector2 lowerLeft(TVector2(x,y)-pad.Position()-pad.Dimensions());
193 TVector2 upperRight(lowerLeft + pad.Dimensions()*2.0);
195 return fMathieson->IntXY(lowerLeft.X(),lowerLeft.Y(),
196 upperRight.X(),upperRight.Y());
199 //_____________________________________________________________________________
201 AliMUONClusterSplitterMLEM::Fcn1(const AliMUONCluster& cluster,
202 Int_t & /*fNpar*/, Double_t * /*gin*/,
203 Double_t &f, Double_t *par, Int_t iflag)
205 /// Computes the functional to be minimized
208 Double_t charge, delta, coef=0, chi2=0, qTot = 0;
209 static Double_t qAver = 0;
211 Int_t mult = cluster.Multiplicity(), iend = fNpar / 3;
212 for (Int_t j = 0; j < mult; ++j)
214 AliMUONPad* pad = cluster.Pad(j);
215 //if ( pad->Status() !=1 || pad->IsSaturated() ) continue;
216 if ( pad->Status() != AliMUONClusterFinderMLEM::GetUseForFitFlag() ||
217 pad->Charge() == 0 ) continue;
219 if ( pad->IsReal() ) npads++; // exclude virtual pads
220 qTot += pad->Charge();
223 for (Int_t i = 0; i <= iend; ++i)
227 coef = Param2Coef(i, coef, par);
228 charge += ChargeIntegration(par[indx],par[indx+1],*pad) * coef;
231 delta = charge - pad->Charge();
233 delta /= pad->Charge();
236 if (iflag == 0 && npads) qAver = qTot / npads;
239 AliError(Form("Got npads=0. Please check"));
244 //_____________________________________________________________________________
245 Double_t AliMUONClusterSplitterMLEM::Param2Coef(Int_t icand, Double_t coef, Double_t *par) const
247 /// Extract hit contribution scale factor from fit parameters
249 if (fNpar == 2) return 1.;
250 if (fNpar == 5) return icand==0 ? par[2] : TMath::Max(1.-par[2],0.);
251 if (icand == 0) return par[2];
252 if (icand == 1) return TMath::Max((1.-par[2])*par[5], 0.);
253 return TMath::Max(1.-par[2]-coef,0.);
256 //_____________________________________________________________________________
258 AliMUONClusterSplitterMLEM::Fit(const AliMUONCluster& cluster,
259 Int_t iSimple, Int_t nfit,
260 const Int_t *clustFit, TObjArray **clusters,
262 TObjArray& clusterList, TH2 *mlem)
264 /// Steering function and fitting procedure for the fit of pad charge distribution
266 // AliDebug(2,Form("iSimple=%d nfit=%d",iSimple,nfit));
268 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
269 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
270 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
271 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
272 Double_t xPad = 0, yPad = 99999;
274 // Number of pads to use and number of virtual pads
275 Int_t npads = 0, nVirtual = 0, nfit0 = nfit;
276 //cluster.Print("full");
277 Int_t mult = cluster.Multiplicity();
278 for (Int_t i = 0; i < mult; ++i )
280 AliMUONPad* pad = cluster.Pad(i);
281 if ( !pad->IsReal() ) ++nVirtual;
282 //if ( pad->Status() !=1 || pad->IsSaturated() ) continue;
283 if ( pad->Status() != AliMUONClusterFinderMLEM::GetUseForFitFlag() ) continue;
294 if (pad->DY() < pad->DX() )
309 if (npads < 2) return 0;
311 // FIXME : AliWarning("Reconnect the following code for hit/track passing ?");
313 // Int_t tracks[3] = {-1, -1, -1};
317 AliMUONDigit *mdig = 0;
318 for (Int_t cath=0; cath<2; cath++) {
319 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
320 if (fPadIJ[0][i] != cath) continue;
321 if (fPadIJ[1][i] != 1) continue;
322 if (fXyq[3][i] < 0) continue; // exclude virtual pads
323 digit = TMath::Nint (fXyq[5][i]);
324 if (digit >= 0) mdig = fInput->Digit(cath,digit);
325 else mdig = fInput->Digit(TMath::Even(cath),-digit-1);
326 //if (!mdig) mdig = fInput->Digit(TMath::Even(cath),digit);
327 if (!mdig) continue; // protection for cluster display
328 if (mdig->Hit() >= 0) {
330 tracks[0] = mdig->Hit();
331 tracks[1] = mdig->Track(0);
332 } else if (mdig->Track(0) < tracks[1]) {
333 tracks[0] = mdig->Hit();
334 tracks[1] = mdig->Track(0);
337 if (mdig->Track(1) >= 0 && mdig->Track(1) != tracks[1]) {
338 if (tracks[2] < 0) tracks[2] = mdig->Track(1);
339 else tracks[2] = TMath::Min (tracks[2], mdig->Track(1));
342 } // for (Int_t cath=0;
345 // Get number of pads in X and Y
346 //const Int_t kStatusToTest(1);
347 const Int_t kStatusToTest(AliMUONClusterFinderMLEM::GetUseForFitFlag());
349 Long_t nofPads = cluster.NofPads(kStatusToTest);
350 Int_t nInX = AliMp::PairFirst(nofPads);
351 Int_t nInY = AliMp::PairSecond(nofPads);
355 for (Int_t j = 0; j < cluster.Multiplicity(); ++j) {
356 AliMUONPad *pad = cluster.Pad(j);
357 //if (pad->Status() == 1 && !pad->IsSaturated()) npadOK++;
358 if (pad->Status() == AliMUONClusterFinderMLEM::GetUseForFitFlag() && !pad->IsSaturated()) npadOK++;
360 cout << " Number of pads to fit: " << npadOK << endl;
361 cout << " nInX and Y: " << nInX << " " << nInY << endl;
365 nfitMax = TMath::Min (nfitMax, (npads + 1) / 3);
367 if (((nInX < 3) && (nInY < 3)) || ((nInX == 3) && (nInY < 3)) || ((nInX < 3) && (nInY == 3))) nfitMax = 1; // not enough pads in each direction
369 if (nfit > nfitMax) nfit = nfitMax;
371 // Take cluster maxima as fitting seeds
375 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8], qq = 0;
377 for ( int i = 0; i < 8; ++i ) errOk[i]=0.0;
379 Double_t xyseed[3][2], qseed[3], xyCand[3][2] = {{0},{0}}, sigCand[3][2] = {{0},{0}};
381 for (Int_t ifit = 1; ifit <= nfit0; ++ifit)
384 pix = clusters[clustFit[ifit-1]];
385 npxclu = pix->GetEntriesFast();
387 for (Int_t clu = 0; clu < npxclu; ++clu)
389 pixPtr = (AliMUONPad*) pix->UncheckedAt(clu);
390 cont = pixPtr->Charge();
395 xseed = pixPtr->Coord(0);
396 yseed = pixPtr->Coord(1);
399 xyCand[0][0] += pixPtr->Coord(0) * cont;
400 xyCand[0][1] += pixPtr->Coord(1) * cont;
401 sigCand[0][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
402 sigCand[0][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
404 xyseed[ifit-1][0] = xseed;
405 xyseed[ifit-1][1] = yseed;
406 qseed[ifit-1] = cmax;
407 } // for (Int_t ifit=1;
409 xyCand[0][0] /= qq; // <x>
410 xyCand[0][1] /= qq; // <y>
411 sigCand[0][0] = sigCand[0][0]/qq - xyCand[0][0]*xyCand[0][0]; // <x^2> - <x>^2
412 sigCand[0][0] = sigCand[0][0] > 0 ? TMath::Sqrt (sigCand[0][0]) : 0;
413 sigCand[0][1] = sigCand[0][1]/qq - xyCand[0][1]*xyCand[0][1]; // <y^2> - <y>^2
414 sigCand[0][1] = sigCand[0][1] > 0 ? TMath::Sqrt (sigCand[0][1]) : 0;
415 if (fDebug) cout << xyCand[0][0] << " " << xyCand[0][1] << " " << sigCand[0][0] << " " << sigCand[0][1] << endl;
417 Int_t nDof, maxSeed[3];//, nMax = 0;
419 if ( nfit0 < 0 || nfit0 > 3 ) {
420 AliErrorStream() << "Wrong nfit0 value: " << nfit0 << endl;
423 TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
425 Double_t step[3]={0.01,0.002,0.02}, fmin, chi2o = 9999, chi2n;
426 Double_t *gin = 0, func0, func1, param[8]={0}, step0[8]={0};
427 Double_t param0[2][8]={{0},{0}}, deriv[2][8]={{0},{0}};
428 Double_t shift[8]={0}, stepMax, derMax, parmin[8]={0}, parmax[8]={0}, func2[2]={0}, shift0;
429 Double_t delta[8]={0}, scMax, dder[8], estim, shiftSave = 0;
430 Int_t min, max, nCall = 0, nLoop, idMax = 0, iestMax = 0, nFail;
431 Double_t rad, dist[3] = {0};
433 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
434 // lower, try 3-track (if number of pads is sufficient).
435 Int_t iflag = 0; // for the first call of fcn1
436 for (Int_t iseed = 0; iseed < nfit; ++iseed)
439 Int_t memory[8] = {0};
442 for (Int_t j = 0; j < fNpar; ++j)
447 parmin[fNpar] = 1E-9;
453 param[fNpar] = xyCand[0][0]; // take COG
457 param[fNpar] = xyseed[maxSeed[iseed]][0];
458 //param[fNpar] = fNpar==0 ? -16.1651 : -15.2761;
460 parmin[fNpar] = xmin;
461 parmax[fNpar++] = xmax;
464 param[fNpar] = xyCand[0][1]; // take COG
468 param[fNpar] = xyseed[maxSeed[iseed]][1];
469 //param[fNpar] = fNpar==1 ? -15.1737 : -15.8487;
471 parmin[fNpar] = ymin;
472 parmax[fNpar++] = ymax;
474 for (Int_t j = 0; j < fNpar; ++j)
476 step0[j] = shift[j] = step[j%3];
481 for (Int_t j = 0; j < fNpar; ++j)
487 for (Int_t j = 0; j < fNpar; ++j) cout << param[j] << " ";
492 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
497 Fcn1(cluster,fNpar, gin, func0, param, iflag); nCall++;
499 //cout << " Func: " << func0 << endl;
502 for (Int_t j = 0; j < fNpar; ++j)
504 param0[max][j] = param[j];
506 param[j] += delta[j] / 10;
507 if (j > 0) param[j-1] -= delta[j-1] / 10;
508 Fcn1(cluster,fNpar, gin, func1, param, iflag); nCall++;
509 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
510 //cout << j << " " << deriv[max][j] << endl;
511 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
512 (param0[0][j] - param0[1][j]) : 0; // second derivative
514 param[fNpar-1] -= delta[fNpar-1] / 10;
515 if (nCall > 2000) break;
517 min = func2[0] < func2[1] ? 0 : 1;
518 nFail = min == max ? 0 : nFail + 1;
520 stepMax = derMax = estim = 0;
521 for (Int_t j = 0; j < fNpar; ++j)
523 // Estimated distance to minimum
527 shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
529 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3)
533 else if (((deriv[min][j]*deriv[!min][j] > 0) && (TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])))
534 || (TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) || (TMath::Abs(dder[j]) < 1.e-6))
536 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
548 shift[j] = dder[j] != 0 ? -deriv[min][j] / dder[j] : 0;
552 Double_t es = TMath::Abs(shift[j]) / step0[j];
560 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
562 // Failed to improve minimum
566 param[j] = param0[min][j];
567 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j])
569 shift[j] = (shift[j] + shift0) / 2;
578 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
580 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
583 // Introduce step relaxation factor
586 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
587 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
589 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
592 param[j] += shift[j];
593 // Check parameter limits
594 if (param[j] < parmin[j])
596 shift[j] = parmin[j] - param[j];
597 param[j] = parmin[j];
599 else if (param[j] > parmax[j])
601 shift[j] = parmax[j] - param[j];
602 param[j] = parmax[j];
604 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
605 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
606 if (TMath::Abs(deriv[min][j]) > derMax)
609 derMax = TMath::Abs (deriv[min][j]);
611 } // for (Int_t j=0; j<fNpar;
613 if (((estim < 1) && (derMax < 2)) || nLoop > 150) break; // minimum was found
617 // Check for small step
618 if (shift[idMax] == 0)
620 shift[idMax] = step0[idMax]/10;
621 param[idMax] += shift[idMax];
625 if (!memory[idMax] && derMax > 0.5 && nLoop > 10)
627 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10)
629 if (min == max) dder[idMax] = -dder[idMax];
630 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
631 param[idMax] += shift[idMax];
632 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
633 if (min == max) shiftSave = shift[idMax];
637 param[idMax] -= shift[idMax];
638 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
639 param[idMax] += shift[idMax];
646 nDof = npads - fNpar + nVirtual;
649 if (fDebug) cout << " Chi2 " << chi2n << " " << fNpar << endl;
651 //if (fNpar > 2) cout << param0[min][fNpar-3] << " " << chi2n * (1+TMath::Min(1-param0[min][fNpar-3],0.25)) << endl;
652 //if (chi2n*1.2+1.e-6 > chi2o )
653 if (fNpar > 2 && (chi2n > chi2o || ((iseed == nfit-1)
654 && (chi2n * (1+TMath::Min(1-param0[min][fNpar-3],0.25)) > chi2o))))
655 { fNpar -= 3; break; }
657 // Save parameters and errors
660 // One pad per direction
661 //for (Int_t i=0; i<fNpar; ++i) if (i == 0 || i == 2 || i == 5) param0[min][i] = xPad;
662 for (Int_t i=0; i<fNpar; ++i) if (i == 0 || i == 2 || i == 5)
663 param0[min][i] = xyCand[0][0];
666 // One pad per direction
667 //for (Int_t i=0; i<fNpar; ++i) if (i == 1 || i == 3 || i == 6) param0[min][i] = yPad;
668 for (Int_t i=0; i<fNpar; ++i) if (i == 1 || i == 3 || i == 6)
669 param0[min][i] = xyCand[0][1];
674 // Find distance to the nearest neighbour
675 dist[0] = dist[1] = TMath::Sqrt ((param0[min][0]-param0[min][2])*
676 (param0[min][0]-param0[min][2])
677 +(param0[min][1]-param0[min][3])*
678 (param0[min][1]-param0[min][3]));
680 dist[2] = TMath::Sqrt ((param0[min][0]-param0[min][5])*
681 (param0[min][0]-param0[min][5])
682 +(param0[min][1]-param0[min][6])*
683 (param0[min][1]-param0[min][6]));
684 rad = TMath::Sqrt ((param0[min][2]-param0[min][5])*
685 (param0[min][2]-param0[min][5])
686 +(param0[min][3]-param0[min][6])*
687 (param0[min][3]-param0[min][6]));
688 if (dist[2] < dist[0]) dist[0] = dist[2];
689 if (rad < dist[1]) dist[1] = rad;
690 if (rad < dist[2]) dist[2] = rad;
692 cout << dist[0] << " " << dist[1] << " " << dist[2] << endl;
693 if (dist[TMath::LocMin(iseed+1,dist)] < 1.) { fNpar -= 3; break; }
697 for (Int_t i = 0; i < fNpar; ++i) {
698 parOk[i] = param0[min][i];
702 parOk[i] = TMath::Max (parOk[i], parmin[i]);
703 parOk[i] = TMath::Min (parOk[i], parmax[i]);
707 if (fmin < 0.1) break; // !!!???
708 } // for (Int_t iseed=0;
711 for (Int_t i=0; i<fNpar; ++i) {
712 if (i == 4 || i == 7) {
713 if ((i == 7) || ((i == 4) && (fNpar < 7))) cout << parOk[i] << endl;
714 else cout << parOk[i] * (1-parOk[7]) << endl;
717 cout << parOk[i] << " " << errOk[i] << endl;
720 nfit = (fNpar + 1) / 3;
721 dist[0] = dist[1] = dist[2] = 0;
724 // Find distance to the nearest neighbour
725 dist[0] = dist[1] = TMath::Sqrt ((parOk[0]-parOk[2])*
727 +(parOk[1]-parOk[3])*
728 (parOk[1]-parOk[3]));
730 dist[2] = TMath::Sqrt ((parOk[0]-parOk[5])*
732 +(parOk[1]-parOk[6])*
733 (parOk[1]-parOk[6]));
734 rad = TMath::Sqrt ((parOk[2]-parOk[5])*
736 +(parOk[3]-parOk[6])*
737 (parOk[3]-parOk[6]));
738 if (dist[2] < dist[0]) dist[0] = dist[2];
739 if (rad < dist[1]) dist[1] = rad;
740 if (rad < dist[2]) dist[2] = rad;
747 if (iSimple) fnCoupled = 0;
748 for (Int_t j = 0; j < nfit; ++j) {
750 coef = Param2Coef(j, coef, parOk);
752 //void AliMUONClusterFinderMLEM::AddRawCluster(Double_t x, Double_t y,
753 // Double_t qTot, Double_t fmin,
754 // Int_t nfit, Int_t *tracks,
755 // Double_t /*sigx*/,
756 // Double_t /*sigy*/,
757 // Double_t /*dist*/)
759 if ( coef*fQtot >= fLowestClusterCharge )
761 //AZ AliMUONCluster* cluster1 = new AliMUONCluster();
762 AliMUONCluster* cluster1 = new AliMUONCluster(cluster);
764 cluster1->SetCharge(coef*fQtot,coef*fQtot);
765 cluster1->SetPosition(TVector2(parOk[indx],parOk[indx+1]),TVector2(sigCand[0][0],sigCand[0][1]));
766 //cluster1->SetChi2(dist[TMath::LocMin(nfit,dist)]);
767 Int_t idx = TMath::LocMin(nfit,dist);
768 if ( idx < 0 || idx > 2 ) {
769 AliErrorStream() << "Wrong index value: " << idx << endl;
772 cluster1->SetChi2(dist[idx]);
774 // FIXME: we miss some information in this cluster, as compared to
775 // the original AddRawCluster code.
777 AliDebug(2,Form("Adding RawCluster detElemId %4d mult %2d charge %5d (xl,yl)=(%9.6g,%9.6g)",
778 fDetElemId,cluster1->Multiplicity(),(Int_t)cluster1->Charge(),
779 cluster1->Position().X(),cluster1->Position().Y()));
781 clusterList.Add(cluster1);
783 // AddRawCluster (parOk[indx], // double x
784 // parOk[indx+1], // double y
785 // coef*qTot, // double charge
786 // errOk[indx], // double fmin
787 // nfit0+10*nfit+100*nMax+10000*fnCoupled, // int nfit
788 // tracks, // int* tracks
789 // sigCand[0][0], // double sigx
790 // sigCand[0][1], // double sigy
791 // dist[TMath::LocMin(nfit,dist)] // double dist
798 //_____________________________________________________________________________
800 AliMUONClusterSplitterMLEM::Split(const AliMUONCluster& cluster,
801 TH2 *mlem, Double_t *coef,
802 TObjArray& clusterList)
804 /// The main steering function to work with clusters of pixels in anode
805 /// plane (find clusters, decouple them from each other, merge them (if
806 /// necessary), pick up coupled pads, call the fitting function)
808 Int_t nx = mlem->GetNbinsX();
809 Int_t ny = mlem->GetNbinsY();
810 Int_t nPix = fPixArray->GetEntriesFast();
813 Int_t nclust = 0, indx, indx1, nxy = ny * nx;
814 Bool_t *used = new Bool_t[nxy];
816 for (Int_t j = 0; j < nxy; ++j) used[j] = kFALSE;
818 TObjArray *clusters[200]={0};
821 // Find clusters of histogram bins (easier to work in 2-D space)
822 for (Int_t i = 1; i <= ny; ++i)
824 for (Int_t j = 1; j <= nx; ++j)
826 indx = (i-1)*nx + j - 1;
827 if (used[indx]) continue;
828 cont = mlem->GetCellContent(j,i);
829 if (cont < fLowestPixelCharge) continue;
830 pix = new TObjArray(20);
832 pix->Add(BinToPix(mlem,j,i));
833 AddBin(mlem, i, j, 0, used, pix); // recursive call
834 if (nclust >= 200) AliFatal(" Too many clusters !!!");
835 clusters[nclust++] = pix;
836 } // for (Int_t j=1; j<=nx; j++) {
837 } // for (Int_t i=1; i<=ny;
838 if (fDebug) cout << nclust << endl;
841 // Compute couplings between clusters and clusters to pads
842 Int_t npad = cluster.Multiplicity();
844 // Exclude pads with overflows
846 for (Int_t j = 0; j < npad; ++j)
848 AliMUONPad* pad = cluster.Pad(j);
849 if ( pad->IsSaturated() )
860 // Compute couplings of clusters to pads (including overflows)
861 TMatrixD aijclupad(nclust,npad);
864 for (Int_t iclust = 0; iclust < nclust; ++iclust)
866 pix = clusters[iclust];
867 npxclu = pix->GetEntriesFast();
868 for (Int_t i = 0; i < npxclu; ++i)
870 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
871 for (Int_t j = 0; j < npad; ++j)
873 //AliMUONPad* pad = cluster.Pad(j);
874 //if ( pad->Status() < 0 && pad->Status() != -5) continue;
875 if (coef[j*nPix+indx] < fgkCouplMin) continue;
876 aijclupad(iclust,j) += coef[j*nPix+indx];
881 // Compute couplings between clusters (exclude overflows)
882 TMatrixD aijcluclu(nclust,nclust);
884 for (Int_t iclust = 0; iclust < nclust; ++iclust)
886 for (Int_t j = 0; j < npad; ++j)
889 //if ( cluster.Pad(j)->Status() < 0) continue;
890 if ( cluster.Pad(j)->IsSaturated()) continue;
891 if (aijclupad(iclust,j) < fgkCouplMin) continue;
892 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++)
894 if (aijclupad(iclust1,j) < fgkCouplMin) continue;
895 aijcluclu(iclust,iclust1) +=
896 TMath::Sqrt (aijclupad(iclust,j)*aijclupad(iclust1,j));
900 for (Int_t iclust = 0; iclust < nclust; ++iclust)
902 for (Int_t iclust1 = iclust+1; iclust1 < nclust; ++iclust1)
904 aijcluclu(iclust1,iclust) = aijcluclu(iclust,iclust1);
908 if (fDebug && nclust > 1) aijcluclu.Print();
910 // Find groups of coupled clusters
911 used = new Bool_t[nclust];
912 for (Int_t j = 0; j < nclust; ++j) used[j] = kFALSE;
914 Int_t *clustNumb = new Int_t[nclust];
915 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
917 Double_t parOk[8] = {0}; //AZ
919 for (Int_t igroup = 0; igroup < nclust; ++igroup)
921 if (used[igroup]) continue;
922 used[igroup] = kTRUE;
923 clustNumb[0] = igroup;
925 // Find group of coupled clusters
926 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
929 cout << " nCoupled: " << nCoupled << endl;
930 for (Int_t i=0; i<nCoupled; ++i) cout << clustNumb[i] << " "; cout << endl;
933 fnCoupled = nCoupled;
940 for (Int_t i = 0; i < nCoupled; ++i) clustFit[i] = clustNumb[i];
944 // Too many coupled clusters to fit - try to decouple them
945 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
946 // all the others in the group
947 for (Int_t j = 0; j < 3; ++j) minGroup[j] = -1;
948 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
950 // Flag clusters for fit
952 while (nForFit < 3 && minGroup[nForFit] >= 0)
954 if (fDebug) cout << clustNumb[minGroup[nForFit]] << " ";
955 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
956 clustNumb[minGroup[nForFit]] -= 999;
959 if (fDebug) cout << " nForFit " << nForFit << " " << coupl << endl;
962 // Select pads for fit.
963 if (SelectPad(cluster,nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1)
966 for (Int_t j = 0; j < npad; ++j)
968 AliMUONPad* pad = cluster.Pad(j);
969 //if ( pad->Status()==1 ) pad->SetStatus(0);
970 //if ( pad->Status()==-9) pad->SetStatus(-5);
971 if ( pad->Status() == AliMUONClusterFinderMLEM::GetUseForFitFlag() ||
972 pad->Status() == AliMUONClusterFinderMLEM::GetCoupledFlag())
973 pad->SetStatus(AliMUONClusterFinderMLEM::GetZeroFlag());
975 // Merge the failed cluster candidates (with too few pads to fit) with
976 // the one with the strongest coupling
977 Merge(cluster,nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
982 nfit = Fit(cluster,0, nForFit, clustFit, clusters, parOk, clusterList, mlem);
984 //cout << " (nfit == 0) " << fNpar << " " << cluster.Multiplicity() << endl;
985 fNpar = 0; // should be 0 by itself but just in case ...
989 // Subtract the fitted charges from pads with strong coupling and/or
990 // return pads for further use
991 UpdatePads(cluster,nfit, parOk);
994 for (Int_t j = 0; j < npad; ++j)
996 AliMUONPad* pad = cluster.Pad(j);
997 //if ( pad->Status()==1 ) pad->SetStatus(-2);
998 //if ( pad->Status()==-9) pad->SetStatus(-5);
999 if ( pad->Status() == AliMUONClusterFinderMLEM::GetUseForFitFlag() )
1000 pad->SetStatus(AliMUONClusterFinderMLEM::GetModifiedFlag());
1003 // Sort the clusters (move to the right the used ones)
1004 Int_t beg = 0, end = nCoupled - 1;
1007 if (clustNumb[beg] >= 0) { ++beg; continue; }
1008 for (Int_t j = end; j > beg; --j)
1010 if (clustNumb[j] < 0) continue;
1012 indx = clustNumb[beg];
1013 clustNumb[beg] = clustNumb[j];
1014 clustNumb[j] = indx;
1020 nCoupled -= nForFit;
1023 // Remove couplings of used clusters
1024 for (Int_t iclust = nCoupled; iclust < nCoupled+nForFit; ++iclust)
1026 indx = clustNumb[iclust] + 999;
1027 for (Int_t iclust1 = 0; iclust1 < nCoupled; ++iclust1)
1029 indx1 = clustNumb[iclust1];
1030 aijcluclu(indx,indx1) = aijcluclu(indx1,indx) = 0;
1034 // Update the remaining clusters couplings (subtract couplings from
1035 // the used pads) - overflows excluded
1036 for (Int_t j = 0; j < npad; ++j)
1038 AliMUONPad* pad = cluster.Pad(j);
1039 //if ( pad->Status() != -2) continue;
1040 if ( pad->Status() != AliMUONClusterFinderMLEM::GetModifiedFlag()) continue;
1041 for (Int_t iclust=0; iclust<nCoupled; ++iclust)
1043 indx = clustNumb[iclust];
1044 if (aijclupad(indx,j) < fgkCouplMin) continue;
1045 for (Int_t iclust1 = iclust+1; iclust1 < nCoupled; ++iclust1)
1047 indx1 = clustNumb[iclust1];
1048 if (aijclupad(indx1,j) < fgkCouplMin) continue;
1050 aijcluclu(indx,indx1) -=
1051 TMath::Sqrt (aijclupad(indx,j)*aijclupad(indx1,j));
1052 aijcluclu(indx1,indx) = aijcluclu(indx,indx1);
1055 //pad->SetStatus(-8);
1056 pad->SetStatus(AliMUONClusterFinderMLEM::GetOverFlag());
1057 } // for (Int_t j=0; j<npad;
1058 } // if (nCoupled > 3)
1059 } // while (nCoupled > 0)
1060 } // for (Int_t igroup=0; igroup<nclust;
1062 for (Int_t iclust = 0; iclust < nclust; ++iclust)
1064 pix = clusters[iclust];
1068 delete [] clustNumb;
1073 //_____________________________________________________________________________
1075 AliMUONClusterSplitterMLEM::Merge(const AliMUONCluster& cluster,
1076 Int_t nForFit, Int_t nCoupled,
1077 const Int_t *clustNumb, const Int_t *clustFit,
1078 TObjArray **clusters,
1079 TMatrixD& aijcluclu, TMatrixD& aijclupad)
1081 /// Merge the group of clusters with the one having the strongest coupling with them
1083 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1084 TObjArray *pix, *pix1;
1087 for (Int_t icl = 0; icl < nForFit; ++icl)
1089 indx = clustFit[icl];
1090 pix = clusters[indx];
1091 npxclu = pix->GetEntriesFast();
1093 for (Int_t icl1 = 0; icl1 < nCoupled; ++icl1)
1095 indx1 = clustNumb[icl1];
1096 if (indx1 < 0) continue;
1097 if ( aijcluclu(indx,indx1) > couplMax)
1099 couplMax = aijcluclu(indx,indx1);
1102 } // for (Int_t icl1=0;
1104 pix1 = clusters[imax];
1105 npxclu1 = pix1->GetEntriesFast();
1107 for (Int_t i = 0; i < npxclu; ++i)
1109 pix1->Add(pix->UncheckedAt(i));
1113 //Add cluster-to-cluster couplings
1114 for (Int_t icl1 = 0; icl1 < nCoupled; ++icl1)
1116 indx1 = clustNumb[icl1];
1117 if (indx1 < 0 || indx1 == imax) continue;
1118 aijcluclu(indx1,imax) += aijcluclu(indx,indx1);
1119 aijcluclu(imax,indx1) = aijcluclu(indx1,imax);
1121 aijcluclu(indx,imax) = aijcluclu(imax,indx) = 0;
1123 //Add cluster-to-pad couplings
1124 Int_t mult = cluster.Multiplicity();
1125 for (Int_t j = 0; j < mult; ++j)
1127 AliMUONPad* pad = cluster.Pad(j);
1128 //if ( pad->Status() < 0 && pad->Status() != -5 ) continue;// exclude used pads
1129 if ( pad->Status() != AliMUONClusterFinderMLEM::GetZeroFlag()) continue;// exclude used pads
1130 aijclupad(imax,j) += aijclupad(indx,j);
1131 aijclupad(indx,j) = 0;
1133 } // for (Int_t icl=0; icl<nForFit;
1137 //_____________________________________________________________________________
1139 AliMUONClusterSplitterMLEM::MinGroupCoupl(Int_t nCoupled, const Int_t *clustNumb,
1140 const TMatrixD& aijcluclu, Int_t *minGroup)
1142 /// Find group of clusters with minimum coupling to all the others
1144 Int_t i123max = TMath::Min(3,nCoupled/2);
1145 Int_t indx, indx1, indx2, indx3, nTot = 0;
1146 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1148 for (Int_t i123 = 1; i123 <= i123max; ++i123) {
1151 coupl1 = new Double_t [nCoupled];
1152 for (Int_t i = 0; i < nCoupled; ++i) coupl1[i] = 0;
1154 else if (i123 == 2) {
1155 nTot = nCoupled*nCoupled;
1156 coupl2 = new Double_t [nTot];
1157 for (Int_t i = 0; i < nTot; ++i) coupl2[i] = 9999;
1159 nTot = nTot*nCoupled;
1160 coupl3 = new Double_t [nTot];
1161 for (Int_t i = 0; i < nTot; ++i) coupl3[i] = 9999;
1164 for (Int_t i = 0; i < nCoupled; ++i) {
1165 indx1 = clustNumb[i];
1166 for (Int_t j = i+1; j < nCoupled; ++j) {
1167 indx2 = clustNumb[j];
1169 coupl1[i] += aijcluclu(indx1,indx2);
1170 coupl1[j] += aijcluclu(indx1,indx2);
1172 else if (i123 == 2) {
1173 indx = i*nCoupled + j;
1174 coupl2[indx] = coupl1[i] + coupl1[j];
1175 coupl2[indx] -= 2 * (aijcluclu(indx1,indx2));
1177 for (Int_t k = j+1; k < nCoupled; ++k) {
1178 indx3 = clustNumb[k];
1179 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1180 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
1181 coupl3[indx] -= 2 * (aijcluclu(indx1,indx3)+aijcluclu(indx2,indx3));
1184 } // for (Int_t j=i+1;
1185 } // for (Int_t i=0;
1186 } // for (Int_t i123=1;
1188 // Find minimum coupling
1189 Double_t couplMin = 9999;
1192 for (Int_t i123 = 1; i123 <= i123max; ++i123) {
1194 locMin = TMath::LocMin(nCoupled, coupl1);
1195 couplMin = coupl1[locMin];
1196 minGroup[0] = locMin;
1199 else if (i123 == 2) {
1200 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1201 if (coupl2[locMin] < couplMin) {
1202 couplMin = coupl2[locMin];
1203 minGroup[0] = locMin/nCoupled;
1204 minGroup[1] = locMin%nCoupled;
1208 locMin = TMath::LocMin(nTot, coupl3);
1209 if (coupl3[locMin] < couplMin) {
1210 couplMin = coupl3[locMin];
1211 minGroup[0] = locMin/nCoupled/nCoupled;
1212 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1213 minGroup[2] = locMin%nCoupled;
1217 } // for (Int_t i123=1;
1221 //_____________________________________________________________________________
1223 AliMUONClusterSplitterMLEM::SelectPad(const AliMUONCluster& cluster,
1224 Int_t nCoupled, Int_t nForFit,
1225 const Int_t *clustNumb, const Int_t *clustFit,
1226 const TMatrixD& aijclupad)
1228 /// Select pads for fit. If too many coupled clusters, find pads giving
1229 /// the strongest coupling with the rest of clusters and exclude them from the fit.
1231 Int_t npad = cluster.Multiplicity();
1232 Double_t *padpix = 0;
1236 padpix = new Double_t[npad];
1237 for (Int_t i = 0; i < npad; ++i) padpix[i] = 0.;
1240 Int_t nOK = 0, indx, indx1;
1241 for (Int_t iclust = 0; iclust < nForFit; ++iclust)
1243 indx = clustFit[iclust];
1244 for (Int_t j = 0; j < npad; ++j)
1246 if ( aijclupad(indx,j) < fgkCouplMin) continue;
1247 AliMUONPad* pad = cluster.Pad(j);
1249 if ( pad->Status() == -5 ) pad->SetStatus(-9); // flag overflow
1250 if ( pad->Status() < 0 ) continue; // exclude overflows and used pads
1251 if ( !pad->Status() )
1254 ++nOK; // pad to be used in fit
1257 if ( pad->Status() != AliMUONClusterFinderMLEM::GetZeroFlag()
1258 || pad->IsSaturated() ) continue; // used pads and overflows
1259 pad->SetStatus(AliMUONClusterFinderMLEM::GetUseForFitFlag());
1260 ++nOK; // pad to be used in fit
1264 // Check other clusters
1265 for (Int_t iclust1 = 0; iclust1 < nCoupled; ++iclust1)
1267 indx1 = clustNumb[iclust1];
1268 if (indx1 < 0) continue;
1269 if ( aijclupad(indx1,j) < fgkCouplMin ) continue;
1270 padpix[j] += aijclupad(indx1,j);
1272 } // if (nCoupled > 3)
1273 } // for (Int_t j=0; j<npad;
1274 } // for (Int_t iclust=0; iclust<nForFit
1275 if (nCoupled < 4) return nOK;
1278 for (Int_t j = 0; j < npad; ++j)
1280 if (padpix[j] < fgkCouplMin) continue;
1282 //cluster.Pad(j)->SetStatus(-1); // exclude pads with strong coupling to the other clusters
1283 cluster.Pad(j)->SetStatus(AliMUONClusterFinderMLEM::GetCoupledFlag()); // exclude pads with strong coupling to the other clusters
1290 //_____________________________________________________________________________
1292 AliMUONClusterSplitterMLEM::UpdatePads(const AliMUONCluster& cluster,
1293 Int_t /*nfit*/, Double_t *par)
1295 /// Subtract the fitted charges from pads with strong coupling
1297 Int_t indx, mult = cluster.Multiplicity(), iend = fNpar/3;
1298 Double_t charge, coef=0;
1300 for (Int_t j = 0; j < mult; ++j)
1302 AliMUONPad* pad = cluster.Pad(j);
1303 //if ( pad->Status() != -1 ) continue;
1304 if ( pad->Status() != AliMUONClusterFinderMLEM::GetCoupledFlag() ) continue;
1308 for (Int_t i = 0; i <= iend; ++i)
1312 coef = Param2Coef(i, coef, par);
1313 charge += ChargeIntegration(par[indx],par[indx+1],*pad) * coef;
1316 pad->SetCharge(pad->Charge()-charge);
1317 } // if (fNpar != 0)
1319 //if (pad->Charge() > 6 /*fgkZeroSuppression*/) pad->SetStatus(0);
1320 if (pad->Charge() > fLowestPadCharge) pad->SetStatus(AliMUONClusterFinderMLEM::GetZeroFlag());
1321 // return pad for further using // FIXME: remove usage of zerosuppression here
1322 else pad->SetStatus(AliMUONClusterFinderMLEM::GetOverFlag()); // do not use anymore
1324 } // for (Int_t j=0;