]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONClusterFinderAZ.cxx
Cleanuo Effc++ warnings (Sasha)
[u/mrichter/AliRoot.git] / MUON / AliMUONClusterFinderAZ.cxx
CommitLineData
30178c30 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
16/* $Id$ */
17
d19b6003 18// -------------------------------
19// Class AliMUONClusterFinderAZ
20// -------------------------------
21// Clusterizer class based on the Expectation-Maximization algorithm
22// Author: Alexander Zinchenko, JINR Dubna
0df3ca52 23
ae17f568 24#include <stdlib.h>
0df3ca52 25#include <Riostream.h>
0df3ca52 26#include <TH2.h>
0df3ca52 27#include <TMinuit.h>
28#include <TMatrixD.h>
29
30178c30 30#include "AliMUONClusterFinderAZ.h"
1af223d7 31#include "AliMUONClusterDrawAZ.h"
0627f609 32#include "AliMUONVGeometryDESegmentation.h"
33#include "AliMUONGeometryModuleTransformer.h"
0df3ca52 34#include "AliRun.h"
35#include "AliMUON.h"
0df3ca52 36#include "AliMUONDigit.h"
0df3ca52 37#include "AliMUONRawCluster.h"
38#include "AliMUONClusterInput.h"
39#include "AliMUONPixel.h"
0627f609 40#include "AliMUONMathieson.h"
8c343c7c 41#include "AliLog.h"
0df3ca52 42
13985652 43/// \cond CLASSIMP
0df3ca52 44ClassImp(AliMUONClusterFinderAZ)
13985652 45/// \endcond
0558a292 46
343146bf 47 const Double_t AliMUONClusterFinderAZ::fgkCouplMin = 1.e-3; // threshold on coupling
0627f609 48 const Double_t AliMUONClusterFinderAZ::fgkZeroSuppression = 6; // average zero suppression value
49 const Double_t AliMUONClusterFinderAZ::fgkSaturation = 3000; // average saturation level
0558a292 50 AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = 0x0;
51 TMinuit* AliMUONClusterFinderAZ::fgMinuit = 0x0;
2b1e4f0e 52//FILE *lun1 = fopen("nxny.dat","w");
0df3ca52 53
0df3ca52 54//_____________________________________________________________________________
1af223d7 55AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw)
54d7ba50 56 : AliMUONClusterFinderVS(),
57 fZpad(0),
58 fNpar(0),
59 fQtot(0),
60 fReco(1),
61 fCathBeg(0),
62 fDraw(0x0),
63 fPixArray(0x0),
64 fnCoupled(0),
65 fDebug(0)
0df3ca52 66{
d19b6003 67/// Constructor
af34d705 68 fnPads[0]=fnPads[1]=0;
69
70 for (Int_t i=0; i<7; i++)
71 for (Int_t j=0; j<fgkDim; j++)
72 fXyq[i][j]= 9999.;
73
0627f609 74 for (Int_t i=0; i<4; i++)
75 for (Int_t j=0; j<fgkDim; j++)
af34d705 76 fPadIJ[i][j]=-1;
0627f609 77
78 for (Int_t i=0; i<2; i++)
79 for (Int_t j=0; j<fgkDim; j++)
af34d705 80 fUsed[i][j] = 0;
af34d705 81
0627f609 82 fSegmentation[1] = fSegmentation[0] = 0x0;
af34d705 83
54d7ba50 84 fPadBeg[0] = fPadBeg[1] = 0;
af34d705 85
0df3ca52 86 if (!fgMinuit) fgMinuit = new TMinuit(8);
af34d705 87 if (!fgClusterFinder) fgClusterFinder = this;
0df3ca52 88 fPixArray = new TObjArray(20);
af34d705 89
1af223d7 90 if (draw) {
91 fDebug = 1;
cc87ebcd 92 fReco = 0;
1af223d7 93 fDraw = new AliMUONClusterDrawAZ(this);
94 }
cc87ebcd 95 cout << " *** Running AZ cluster finder *** " << endl;
0df3ca52 96}
97
98//_____________________________________________________________________________
99AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ()
100{
d19b6003 101/// Destructor
0df3ca52 102 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
1af223d7 103 delete fDraw;
0df3ca52 104}
105
106//_____________________________________________________________________________
107void AliMUONClusterFinderAZ::FindRawClusters()
108{
d19b6003 109/// To provide the same interface as in AliMUONClusterFinderVS
0df3ca52 110
1af223d7 111 ResetRawClusters();
0bf8d810 112 EventLoop (gAlice->GetEvNumber(), fInput->Chamber());
0df3ca52 113}
114
115//_____________________________________________________________________________
1af223d7 116void AliMUONClusterFinderAZ::EventLoop(Int_t nev, Int_t ch)
0df3ca52 117{
d19b6003 118/// Loop over digits
0df3ca52 119
1af223d7 120 if (fDraw && !fDraw->FindEvCh(nev, ch)) return;
0df3ca52 121
0627f609 122 fSegmentation[0] = (AliMUONVGeometryDESegmentation*) fInput->
123 Segmentation2(0)->GetDESegmentation(fInput->DetElemId());
124 fSegmentation[1] = (AliMUONVGeometryDESegmentation*) fInput->
125 Segmentation2(1)->GetDESegmentation(fInput->DetElemId());
0df3ca52 126
1af223d7 127 Int_t ndigits[2] = {9,9}, nShown[2] = {0};
cc87ebcd 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; }
132 }
0df3ca52 133 }
134
135next:
cc87ebcd 136 if (fReco == 2 && (nShown[0] || nShown[1])) return; // only one precluster for the combined finder
1af223d7 137 if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) return;
138
0df3ca52 139 Float_t xpad, ypad, zpad, zpad0;
0df3ca52 140 Bool_t first = kTRUE;
2b1e4f0e 141 if (fDebug) cout << " *** Event # " << nev << " chamber: " << ch << endl;
0df3ca52 142 fnPads[0] = fnPads[1] = 0;
cc87ebcd 143 for (Int_t i = 0; i < fgkDim; i++) fPadIJ[1][i] = 0;
2b1e4f0e 144
cc87ebcd 145 for (Int_t iii = fCathBeg; iii < 2; iii++) {
0df3ca52 146 Int_t cath = TMath::Odd(iii);
0627f609 147 ndigits[cath] = fInput->NDigits(cath);
2b1e4f0e 148 if (!ndigits[0] && !ndigits[1]) return;
0df3ca52 149 if (ndigits[cath] == 0) continue;
2b1e4f0e 150 if (fDebug) cout << " ndigits: " << ndigits[cath] << " " << cath << endl;
0df3ca52 151
152 AliMUONDigit *mdig;
153 Int_t digit;
154
c1aed84f 155 Bool_t eEOC = kTRUE; // end-of-cluster
cc87ebcd 156 for (digit = fPadBeg[cath]; digit < ndigits[cath]; digit++) {
2b1e4f0e 157 mdig = AliMUONClusterInput::Instance()->Digit(cath,digit);
0df3ca52 158 if (first) {
159 // Find first unused pad
160 if (fUsed[cath][digit]) continue;
0627f609 161 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0)) {
162 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
5a051e34 163 // Handle "non-existing" pads
164 fUsed[cath][digit] = kTRUE;
165 continue;
166 }
0627f609 167 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad0);
0df3ca52 168 } else {
169 if (fUsed[cath][digit]) continue;
0627f609 170 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad)) {
171 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
5a051e34 172 // Handle "non-existing" pads
173 fUsed[cath][digit] = kTRUE;
174 continue;
175 }
0627f609 176 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
177 //if (TMath::Abs(zpad-zpad0) > 0.1) continue; // different slats
0df3ca52 178 // Find a pad overlapping with the cluster
179 if (!Overlap(cath,mdig)) continue;
180 }
181 // Add pad - recursive call
182 AddPad(cath,digit);
2b1e4f0e 183 //AZ !!!!!! Temporary fix of St1 overlap regions !!!!!!!!
0627f609 184 /*
2b1e4f0e 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);
0627f609 190 //fSegmentation[cath1]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad);
191 fSegmentation[cath1]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
2b1e4f0e 192 if (TMath::Abs(zpad-zpad0) > 0.1) zpad0 = zpad;
193 }
0627f609 194 */
c1aed84f 195 eEOC = kFALSE;
0df3ca52 196 if (digit >= 0) break;
197 }
c1aed84f 198 if (first && eEOC) {
0df3ca52 199 // No more unused pads
200 if (cath == 0) continue; // on cathode #0 - check #1
2b1e4f0e 201 else return; // No more clusters
0df3ca52 202 }
c1aed84f 203 if (eEOC) break; // cluster found
0df3ca52 204 first = kFALSE;
2b1e4f0e 205 if (fDebug) cout << " nPads: " << fnPads[cath] << " " << nShown[cath]+fnPads[cath] << " " << cath << endl;
0df3ca52 206 } // for (Int_t iii = 0;
207
2b1e4f0e 208 fZpad = zpad0;
1af223d7 209 if (fDraw) fDraw->DrawCluster();
2b1e4f0e 210
211 // Use MLEM for cluster finder
212 Int_t nMax = 1, localMax[100], maxPos[100];
213 Double_t maxVal[100];
0df3ca52 214
2b1e4f0e 215 if (CheckPrecluster(nShown)) {
216 BuildPixArray();
0627f609 217 //*
218 if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(fPixArray, localMax, maxVal);
2b1e4f0e 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;
5a051e34 223 if (nMax == 1 && nInX < 4 && nInY < 4) iSimple = 1; //1; // simple cluster
0627f609 224 //*/
225 /* For test
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);
231 nMax = 1;
232 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
233 */
2b1e4f0e 234 for (Int_t i=0; i<nMax; i++) {
235 if (nMax > 1) FindCluster(localMax, maxPos[i]);
af82d8d2 236 MainLoop(iSimple);
2b1e4f0e 237 if (i < nMax-1) {
238 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
239 if (fPadIJ[1][j] == 0) continue; // pad charge was not modified
240 fPadIJ[1][j] = 0;
241 fXyq[2][j] = fXyq[6][j]; // use backup charge value
242 }
243 }
0627f609 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();
2b1e4f0e 248 }
1af223d7 249 if (!fDraw || fDraw->Next()) goto next;
0df3ca52 250}
251
252//_____________________________________________________________________________
253void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit)
254{
d19b6003 255/// Add pad to the cluster
256
0627f609 257 AliMUONDigit *mdig = fInput->Digit(cath,digit);
0df3ca52 258
259 Int_t charge = mdig->Signal();
260 // get the center of the pad
0627f609 261 Float_t xpad, ypad, zpad0;
262 //if (!fSegmentation[cath]->GetPadC(fInput->DetElemId(),mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0)) { // Handle "non-existing" pads
263 if (!fSegmentation[cath]->HasPad(mdig->PadX(), mdig->PadY())) {
5a051e34 264 fUsed[cath][digit] = kTRUE;
265 return;
266 }
0627f609 267 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad0);
268 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
0df3ca52 269 Int_t nPads = fnPads[0] + fnPads[1];
270 fXyq[0][nPads] = xpad;
271 fXyq[1][nPads] = ypad;
272 fXyq[2][nPads] = charge;
0627f609 273 fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2;
274 fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2;
0df3ca52 275 fXyq[5][nPads] = digit;
2b1e4f0e 276 fXyq[6][nPads] = 0;
0df3ca52 277 fPadIJ[0][nPads] = cath;
278 fPadIJ[1][nPads] = 0;
0627f609 279 fPadIJ[2][nPads] = mdig->PadX();
280 fPadIJ[3][nPads] = mdig->PadY();
0df3ca52 281 fUsed[cath][digit] = kTRUE;
0627f609 282 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());
0df3ca52 283 fnPads[cath]++;
284
285 // Check neighbours
286 Int_t nn, ix, iy, xList[10], yList[10];
287 AliMUONDigit *mdig1;
288
0627f609 289 Int_t ndigits = fInput->NDigits(cath);
290 fSegmentation[cath]->Neighbours(mdig->PadX(), mdig->PadY(), &nn, xList, yList);
291 for (Int_t in = 0; in < nn; in++) {
292 ix = xList[in];
293 iy = yList[in];
0df3ca52 294 for (Int_t digit1 = 0; digit1 < ndigits; digit1++) {
295 if (digit1 == digit) continue;
0627f609 296 mdig1 = fInput->Digit(cath,digit1);
0df3ca52 297 if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) {
298 fUsed[cath][digit1] = kTRUE;
299 // Add pad - recursive call
300 AddPad(cath,digit1);
301 }
302 } //for (Int_t digit1 = 0;
0627f609 303 } // for (Int_t in = 0;
0df3ca52 304}
305
306//_____________________________________________________________________________
2b1e4f0e 307Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, AliMUONDigit *mdig)
0df3ca52 308{
d19b6003 309/// Check if the pad from one cathode overlaps with a pad
310/// in the precluster on the other cathode
0df3ca52 311
0df3ca52 312 Float_t xpad, ypad, zpad;
0627f609 313 fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad);
314 Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
002920d1 315
2b1e4f0e 316 Float_t xy1[4], xy12[4];
0627f609 317 xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2;
318 xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec);
319 xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2;
320 xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec);
0df3ca52 321 //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl;
322
323 Int_t cath1 = TMath::Even(cath);
324 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
325 if (fPadIJ[0][i] != cath1) continue;
326 if (Overlap(xy1, i, xy12, 0)) return kTRUE;
327 }
328 return kFALSE;
329}
330
331//_____________________________________________________________________________
332Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip)
333{
d19b6003 334/// Check if the pads xy1 and iPad overlap and return overlap area
0df3ca52 335
336 Float_t xy2[4];
337 xy2[0] = fXyq[0][iPad] - fXyq[3][iPad];
338 xy2[1] = fXyq[0][iPad] + fXyq[3][iPad];
339 if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE;
340 xy2[2] = fXyq[1][iPad] - fXyq[4][iPad];
341 xy2[3] = fXyq[1][iPad] + fXyq[4][iPad];
342 if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE;
343 if (!iSkip) return kTRUE; // just check overlap (w/out computing the area)
344 xy12[0] = TMath::Max (xy1[0],xy2[0]);
345 xy12[1] = TMath::Min (xy1[1],xy2[1]);
346 xy12[2] = TMath::Max (xy1[2],xy2[2]);
347 xy12[3] = TMath::Min (xy1[3],xy2[3]);
348 return kTRUE;
349}
350
0df3ca52 351//_____________________________________________________________________________
352Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown)
353{
d19b6003 354/// Check precluster in order to attempt to simplify it (mostly for
355/// two-cathode preclusters)
0df3ca52 356
2b1e4f0e 357 Int_t i1, i2, cath=0, digit=0;
0df3ca52 358 Float_t xy1[4], xy12[4];
359
360 Int_t npad = fnPads[0] + fnPads[1];
2b1e4f0e 361 if (npad == 1) {
362 // Disregard one-pad clusters (leftovers from splitting)
363 nShown[0] += fnPads[0];
364 nShown[1] += fnPads[1];
365 return kFALSE;
366 }
0df3ca52 367
368 // If pads have the same size take average of pads on both cathodes
0627f609 369 //Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0;
370 Int_t sameSize = 0; //AZ - 17-01-06
371
0df3ca52 372 if (sameSize) {
373 Double_t xSize = -1, ySize = 0;
374 for (Int_t i=0; i<npad; i++) {
375 if (fXyq[2][i] < 0) continue;
376 if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; }
377 if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 || TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; }
378 }
379 } // if (sameSize)
2b1e4f0e 380 if (sameSize && fnPads[0] == 1 && fnPads[1] == 1) sameSize = 0; //AZ
5a051e34 381 // Handle shift by half a pad in Station 1
382 if (sameSize) {
383 Int_t cath0 = fPadIJ[0][0];
384 for (Int_t i = 1; i < npad; i++) {
385 if (fPadIJ[0][i] == cath0) continue;
386 Double_t dx = TMath::Abs ((fXyq[0][i] - fXyq[0][0]) / fXyq[3][i] / 2);
387 Int_t idx = (Int_t) TMath::Abs ((fXyq[0][i] - fXyq[0][0]) / fXyq[3][i] / 2);
388 if (TMath::Abs (dx - idx) > 0.001) sameSize = 0;
389 break;
390 }
391 } // if (sameSize)
392
2b1e4f0e 393 if (sameSize && (fnPads[0] >= 2 || fnPads[1] >= 2)) {
0df3ca52 394 nShown[0] += fnPads[0];
395 nShown[1] += fnPads[1];
396 fnPads[0] = fnPads[1] = 0;
397 Int_t div;
398 for (Int_t i=0; i<npad; i++) {
399 if (fXyq[2][i] < 0) continue; // used pad
400 fXyq[2][fnPads[0]] = fXyq[2][i];
401 div = 1;
2b1e4f0e 402 cath = fPadIJ[0][i];
0df3ca52 403 for (Int_t j=i+1; j<npad; j++) {
404 if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode
405 if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue;
406 if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue;
407 fXyq[2][fnPads[0]] += fXyq[2][j];
408 div = 2;
409 fXyq[2][j] = -2;
2b1e4f0e 410 if (cath) fXyq[5][fnPads[0]] = fXyq[5][j]; // save digit number for cath 0
0df3ca52 411 break;
412 }
2b1e4f0e 413 // Flag that the digit from the other cathode
414 if (cath && div == 1) fXyq[5][fnPads[0]] = -fXyq[5][i] - 1;
415 // If low pad charge take the other equal to 0
0627f609 416 //if (div == 1 && fXyq[2][fnPads[0]] < fgkZeroSuppression + 1.5*3) div = 2;
0df3ca52 417 fXyq[2][fnPads[0]] /= div;
418 fXyq[0][fnPads[0]] = fXyq[0][i];
419 fXyq[1][fnPads[0]] = fXyq[1][i];
0627f609 420 fPadIJ[2][fnPads[0]] = fPadIJ[2][i];
421 fPadIJ[3][fnPads[0]] = fPadIJ[3][i];
0df3ca52 422 fPadIJ[0][fnPads[0]++] = 0;
423 }
424 } // if (sameSize)
425
426 // Check if one-cathode precluster
427 i1 = fnPads[0]!=0 ? 0 : 1;
428 i2 = fnPads[1]!=0 ? 1 : 0;
429
430 if (i1 != i2) { // two-cathode
431
432 Int_t *flags = new Int_t[npad];
433 for (Int_t i=0; i<npad; i++) { flags[i] = 0; }
434
435 // Check pad overlaps
436 for (Int_t i=0; i<npad; i++) {
437 if (fPadIJ[0][i] != i1) continue;
438 xy1[0] = fXyq[0][i] - fXyq[3][i];
439 xy1[1] = fXyq[0][i] + fXyq[3][i];
440 xy1[2] = fXyq[1][i] - fXyq[4][i];
441 xy1[3] = fXyq[1][i] + fXyq[4][i];
442 for (Int_t j=0; j<npad; j++) {
443 if (fPadIJ[0][j] != i2) continue;
444 if (!Overlap(xy1, j, xy12, 0)) continue;
445 flags[i] = flags[j] = 1; // mark overlapped pads
446 } // for (Int_t j=0;
447 } // for (Int_t i=0;
448
449 // Check if all pads overlap
2b1e4f0e 450 Int_t nFlags=0;
451 for (Int_t i=0; i<npad; i++) {
452 if (flags[i]) continue;
453 nFlags ++;
1af223d7 454 if (fDebug) cout << i << " " << fPadIJ[0][i] << " " << fXyq[0][i] << " " << fXyq[1][i] << endl;
2b1e4f0e 455 }
456 if (fDebug && nFlags) cout << " nFlags = " << nFlags << endl;
0df3ca52 457 //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice
0627f609 458 if (nFlags > 0) {
0df3ca52 459 for (Int_t i=0; i<npad; i++) {
460 if (flags[i]) continue;
461 digit = TMath::Nint (fXyq[5][i]);
462 cath = fPadIJ[0][i];
1af223d7 463 // Check for edge effect (missing pads on the other cathode)
464 Int_t cath1 = TMath::Even(cath), ix, iy;
0627f609 465 ix = iy = 0;
466 //if (!fSegmentation[cath1]->GetPadI(fInput->DetElemId(),fXyq[0][i],fXyq[1][i],fZpad,ix,iy)) continue;
467 if (!fSegmentation[cath1]->HasPad(fXyq[0][i], fXyq[1][i], fZpad)) continue;
468 if (nFlags == 1 && fXyq[2][i] < fgkZeroSuppression * 3) continue;
0df3ca52 469 fUsed[cath][digit] = kFALSE; // release pad
470 fXyq[2][i] = -2;
471 fnPads[cath]--;
472 }
cc87ebcd 473 if (fDraw) fDraw->UpdateCluster(npad);
0df3ca52 474 } // if (nFlags > 2)
475
476 // Check correlations of cathode charges
477 if (fnPads[0] && fnPads[1]) { // two-cathode
478 Double_t sum[2]={0};
479 Int_t over[2] = {1, 1};
480 for (Int_t i=0; i<npad; i++) {
481 cath = fPadIJ[0][i];
482 if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i];
0627f609 483 if (fXyq[2][i] > fgkSaturation-1) over[cath] = 0;
0df3ca52 484 }
2b1e4f0e 485 if (fDebug) cout << " Total charge: " << sum[0] << " " << sum[1] << endl;
0df3ca52 486 if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference
2b1e4f0e 487 if (fDebug) cout << " Release " << endl;
0df3ca52 488 // Big difference
cc87ebcd 489 cath = sum[0] > sum[1] ? 0 : 1;
490 Int_t imax = 0, imin = 0;
491 Double_t cmax = -1, cmin = 9999, dxMin = 0, dyMin = 0;
0df3ca52 492 Double_t *dist = new Double_t[npad];
cc87ebcd 493 for (Int_t i = 0; i < npad; i++) {
494 if (fPadIJ[0][i] != cath || fXyq[2][i] < 0) continue;
495 if (fXyq[2][i] < cmin) {
496 cmin = fXyq[2][i];
497 imin = i;
498 }
0df3ca52 499 if (fXyq[2][i] < cmax) continue;
500 cmax = fXyq[2][i];
501 imax = i;
502 }
503 // Arrange pads according to their distance to the max,
504 // normalized to the pad size
cc87ebcd 505 for (Int_t i = 0; i < npad; i++) {
0df3ca52 506 dist[i] = 0;
cc87ebcd 507 if (fPadIJ[0][i] != cath || fXyq[2][i] < 0) continue;
0df3ca52 508 if (i == imax) continue;
cc87ebcd 509 Double_t dx = (fXyq[0][i] - fXyq[0][imax]) / fXyq[3][imax] / 2;
510 Double_t dy = (fXyq[1][i] - fXyq[1][imax]) / fXyq[4][imax] / 2;
511 dist[i] = TMath::Sqrt (dx * dx + dy * dy);
512 if (i == imin) {
e8fb921b 513 cmin = dist[i] + 0.001; // distance to the pad with minimum charge
cc87ebcd 514 dxMin = dx;
515 dyMin = dy;
516 }
0df3ca52 517 }
518 TMath::Sort(npad, dist, flags, kFALSE); // in increasing order
519 Int_t indx;
520 Double_t xmax = -1;
cc87ebcd 521 for (Int_t i = 0; i < npad; i++) {
0df3ca52 522 indx = flags[i];
cc87ebcd 523 if (fPadIJ[0][indx] != cath || fXyq[2][indx] < 0) continue;
524 if (dist[indx] > cmin) {
525 // Farther than the minimum pad
526 Double_t dx = (fXyq[0][indx] - fXyq[0][imax]) / fXyq[3][imax] / 2;
527 Double_t dy = (fXyq[1][indx] - fXyq[1][imax]) / fXyq[4][imax] / 2;
528 dx *= dxMin;
529 dy *= dyMin;
530 if (dx >= 0 && dy >= 0) continue;
531 if (TMath::Abs(dx) > TMath::Abs(dy) && dx >= 0) continue;
532 if (TMath::Abs(dy) > TMath::Abs(dx) && dy >= 0) continue;
533 }
534 if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax) < 1.e-3) {
0df3ca52 535 // Release pads
cc87ebcd 536 if (TMath::Abs(dist[indx]-xmax) < 1.e-3)
cd747ddb 537 cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax);
0df3ca52 538 else cmax = fXyq[2][indx];
539 xmax = dist[indx];
540 digit = TMath::Nint (fXyq[5][indx]);
541 fUsed[cath][digit] = kFALSE;
542 fXyq[2][indx] = -2;
543 fnPads[cath]--;
cc87ebcd 544 }
545 } // for (Int_t i = 0; i < npad;
546
547 // Check pad overlaps once more
548 for (Int_t j = 0; j < npad; j++) flags[j] = 0;
549 for (Int_t k = 0; k < npad; k++) {
550 if (fXyq[2][k] < 0 || fPadIJ[0][k] != i1) continue;
551 xy1[0] = fXyq[0][k] - fXyq[3][k];
552 xy1[1] = fXyq[0][k] + fXyq[3][k];
553 xy1[2] = fXyq[1][k] - fXyq[4][k];
554 xy1[3] = fXyq[1][k] + fXyq[4][k];
555 for (Int_t j = 0; j < npad; j++) {
556 if (fXyq[2][j] < 0) continue;
557 if (fPadIJ[0][j] != i2) continue;
558 if (!Overlap(xy1, j, xy12, 0)) continue;
559 flags[k] = flags[j] = 1; // mark overlapped pads
560 } // for (Int_t j = 0;
561 } // for (Int_t k = 0;
562 nFlags = 0;
563 for (Int_t j = 0; j < npad; j++) {
564 if (fXyq[2][j] < 0 || flags[j]) continue;
565 nFlags ++;
566 }
567 if (nFlags == fnPads[0] + fnPads[1]) {
568 // No overlap
569 for (Int_t j = 0; j < npad; j++) {
570 if (fXyq[2][j] < 0 || fPadIJ[0][j] != cath) continue;
571 fXyq[2][j] = -2;
572 fnPads[cath]--;
0df3ca52 573 }
cc87ebcd 574 }
0df3ca52 575 delete [] dist; dist = 0;
cc87ebcd 576 if (fDraw) fDraw->UpdateCluster(npad);
0df3ca52 577 } // TMath::Abs(sum[0]-sum[1])...
578 } // if (fnPads[0] && fnPads[1])
579 delete [] flags; flags = 0;
580 } // if (i1 != i2)
581
582 if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; }
583
584 // Move released pads to the right
585 Int_t beg = 0, end = npad-1, padij;
586 Double_t xyq;
587 while (beg < end) {
588 if (fXyq[2][beg] > 0) { beg++; continue; }
589 for (Int_t j=end; j>beg; j--) {
590 if (fXyq[2][j] < 0) continue;
591 end = j - 1;
0627f609 592 for (Int_t j1=0; j1<4; j1++) {
0df3ca52 593 padij = fPadIJ[j1][beg];
594 fPadIJ[j1][beg] = fPadIJ[j1][j];
595 fPadIJ[j1][j] = padij;
596 }
597 for (Int_t j1=0; j1<6; j1++) {
598 xyq = fXyq[j1][beg];
599 fXyq[j1][beg] = fXyq[j1][j];
600 fXyq[j1][j] = xyq;
601 }
602 break;
603 } // for (Int_t j=end;
604 beg++;
605 } // while
606 npad = fnPads[0] + fnPads[1];
94eb555e 607 if (npad > 500) {
608 AliWarning(Form(" *** Too large cluster. Give up. %d ", npad));
609 return kFALSE;
610 }
0df3ca52 611 // Back up charge value
1af223d7 612 for (Int_t j = 0; j < npad; j++) fXyq[6][j] = fXyq[2][j];
0df3ca52 613
614 return kTRUE;
615}
616
617//_____________________________________________________________________________
618void AliMUONClusterFinderAZ::BuildPixArray()
619{
d19b6003 620/// Build pixel array for MLEM method
0df3ca52 621
622 Int_t nPix=0, i1, i2;
623 Float_t xy1[4], xy12[4];
624 AliMUONPixel *pixPtr=0;
625
626 Int_t npad = fnPads[0] + fnPads[1];
627
628 // One cathode is empty
629 i1 = fnPads[0]!=0 ? 0 : 1;
630 i2 = fnPads[1]!=0 ? 1 : 0;
631
632 // Build array of pixels on anode plane
633 if (i1 == i2) { // one-cathode precluster
634 for (Int_t j=0; j<npad; j++) {
635 pixPtr = new AliMUONPixel();
636 for (Int_t i=0; i<2; i++) {
637 pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates
638 pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size
639 }
640 pixPtr->SetCharge(fXyq[2][j]); // charge
641 fPixArray->Add((TObject*)pixPtr);
642 nPix++;
643 }
644 } else { // two-cathode precluster
5a051e34 645 i1 = fPadIJ[0][0];
646 i2 = TMath::Even (i1);
647 for (Int_t i = 0; i < npad; i++) {
0df3ca52 648 if (fPadIJ[0][i] != i1) continue;
649 xy1[0] = fXyq[0][i] - fXyq[3][i];
650 xy1[1] = fXyq[0][i] + fXyq[3][i];
651 xy1[2] = fXyq[1][i] - fXyq[4][i];
652 xy1[3] = fXyq[1][i] + fXyq[4][i];
5a051e34 653 for (Int_t j = 1; j < npad; j++) {
0df3ca52 654 if (fPadIJ[0][j] != i2) continue;
655 if (!Overlap(xy1, j, xy12, 1)) continue;
656 pixPtr = new AliMUONPixel();
657 for (Int_t k=0; k<2; k++) {
658 pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates
659 pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size
660 }
661 pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge
662 fPixArray->Add((TObject*)pixPtr);
2b1e4f0e 663 //cout << nPix << " " << pixPtr->Coord(0) << " " << pixPtr->Size(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(1) << " " << pixPtr->Charge() << endl;
0df3ca52 664 nPix++;
665 } // for (Int_t j=0;
666 } // for (Int_t i=0;
667 } // else
668
5a051e34 669 Float_t xPadMin = 999, yPadMin = 999;
670 for (Int_t i = 0; i < npad; i++) {
671 xPadMin = TMath::Min (xPadMin, fXyq[3][i]);
672 yPadMin = TMath::Min (yPadMin, fXyq[4][i]);
673 }
674 if (fDebug) cout << xPadMin << " " << yPadMin << endl;
675
676 Float_t wxmin = 999, wymin = 999;
677 for (Int_t i = 0; i < nPix; i++) {
678 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
679 wxmin = TMath::Min ((Double_t)wxmin, pixPtr->Size(0));
680 wymin = TMath::Min ((Double_t)wymin, pixPtr->Size(1));
0df3ca52 681 }
2b1e4f0e 682 if (fDebug) cout << wxmin << " " << wymin << endl;
5a051e34 683 wxmin = TMath::Abs (wxmin - xPadMin/2) > 0.001 ? xPadMin : xPadMin / 2;
684 wymin = TMath::Abs (wymin - yPadMin/2) > 0.001 ? yPadMin : yPadMin / 2;
685 //wxmin = xPadMin; wymin = yPadMin;
0df3ca52 686
687 // Check if small pixel X-size
2b1e4f0e 688 AdjustPixel(wxmin, 0);
0df3ca52 689 // Check if small pixel Y-size
2b1e4f0e 690 AdjustPixel(wymin, 1);
0df3ca52 691 // Check if large pixel size
2b1e4f0e 692 AdjustPixel(wxmin, wymin);
0df3ca52 693
694 // Remove discarded pixels
695 for (Int_t i=0; i<nPix; i++) {
696 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
697 //pixPtr->Print();
698 if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel
699 }
700 fPixArray->Compress();
701 nPix = fPixArray->GetEntriesFast();
702
703 if (nPix > npad) {
2b1e4f0e 704 if (fDebug) cout << nPix << endl;
0df3ca52 705 // Too many pixels - sort and remove pixels with the lowest signal
706 fPixArray->Sort();
707 for (Int_t i=npad; i<nPix; i++) {
708 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
709 //pixPtr->Print();
710 fPixArray->RemoveAt(i);
711 delete pixPtr;
712 }
713 nPix = npad;
714 } // if (nPix > npad)
715
716 // Set pixel charges to the same value (for MLEM)
717 for (Int_t i=0; i<nPix; i++) {
718 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
719 //pixPtr->SetCharge(10);
2b1e4f0e 720 if (fDebug) cout << i+1 << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(0) << " " << pixPtr->Size(1) << endl;
0df3ca52 721 }
722}
723
724//_____________________________________________________________________________
2b1e4f0e 725void AliMUONClusterFinderAZ::AdjustPixel(Float_t width, Int_t ixy)
0df3ca52 726{
d19b6003 727/// Check if some pixels have small size (adjust if necessary)
0df3ca52 728
729 AliMUONPixel *pixPtr, *pixPtr1 = 0;
730 Int_t ixy1 = TMath::Even(ixy);
731 Int_t nPix = fPixArray->GetEntriesFast();
732
733 for (Int_t i=0; i<nPix; i++) {
734 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
735 if (pixPtr->Charge() < 1) continue; // discarded pixel
736 if (pixPtr->Size(ixy)-width < -1.e-4) {
737 // try to merge
2b1e4f0e 738 if (fDebug) cout << i << " Small X or Y: " << ixy << " " << pixPtr->Size(ixy) << " " << width << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << endl;
0df3ca52 739 for (Int_t j=i+1; j<nPix; j++) {
740 pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j);
741 if (pixPtr1->Charge() < 1) continue; // discarded pixel
742 if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size
743 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns
744 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) {
745 // merge
2b1e4f0e 746 Double_t tmp = pixPtr->Coord(ixy) + pixPtr1->Size(ixy) *
747 TMath::Sign (1., pixPtr1->Coord(ixy) - pixPtr->Coord(ixy));
748 pixPtr->SetCoord(ixy, tmp);
0df3ca52 749 pixPtr->SetSize(ixy, width);
0df3ca52 750 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
751 pixPtr1->SetCharge(0);
752 pixPtr1 = 0;
753 break;
754 }
755 } // for (Int_t j=i+1;
756 //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ???
757 //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) {
758 if (pixPtr1 || i == nPix-1) {
759 // edge pixel - just increase its size
2b1e4f0e 760 if (fDebug) cout << " Edge ..." << endl;
0df3ca52 761 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2b1e4f0e 762 //if (fPadIJ[0][j] != ixy1) continue;
5a051e34 763 //???-check if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue;
0df3ca52 764 if (pixPtr->Coord(ixy) < fXyq[ixy][j])
2b1e4f0e 765 //pixPtr->Shift(ixy, -pixPtr->Size(ixy));
766 pixPtr->Shift(ixy, pixPtr->Size(ixy)-width);
767 //else pixPtr->Shift(ixy, pixPtr->Size(ixy));
768 else pixPtr->Shift(ixy, -pixPtr->Size(ixy)+width);
0df3ca52 769 pixPtr->SetSize(ixy, width);
770 break;
771 }
772 }
773 } // if (pixPtr->Size(ixy)-width < -1.e-4)
774 } // for (Int_t i=0; i<nPix;
775 return;
776}
777
778//_____________________________________________________________________________
2b1e4f0e 779void AliMUONClusterFinderAZ::AdjustPixel(Float_t wxmin, Float_t wymin)
0df3ca52 780{
d19b6003 781/// Check if some pixels have large size (adjust if necessary)
0df3ca52 782
cc87ebcd 783 Int_t n1[2], n2[2], iOK = 1, nPix = fPixArray->GetEntriesFast();
784 AliMUONPixel *pixPtr, pix;
af82d8d2 785 Double_t xy0[2] = {9999, 9999}, wxy[2], dist[2] = {0};
0df3ca52 786
787 // Check if large pixel size
cc87ebcd 788 for (Int_t i = 0; i < nPix; i++) {
0df3ca52 789 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
790 if (pixPtr->Charge() < 1) continue; // discarded pixel
cc87ebcd 791 if (pixPtr->Size(0) - wxmin < 1.e-4) {
792 if (xy0[0] > 9998) xy0[0] = pixPtr->Coord(0); // position of a "normal" pixel
793 if (pixPtr->Size(1) - wymin < 1.e-4) {
794 if (xy0[1] > 9998) xy0[1] = pixPtr->Coord(1); // position of a "normal" pixel
795 continue;
796 } else iOK = 0; // large pixel
797 } else {
798 iOK = 0; // large pixel
799 if (xy0[1] > 9998 && pixPtr->Size(1) - wymin < 1.e-4) xy0[1] = pixPtr->Coord(1); // "normal" pixel
800 }
801 if (xy0[0] < 9998 && xy0[1] < 9998) break;
802 }
803 if (iOK) return;
804
805 wxy[0] = wxmin;
806 wxy[1] = wymin;
807 //cout << xy0[0] << " " << xy0[1] << endl;
808 for (Int_t i = 0; i < nPix; i++) {
809 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
810 if (pixPtr->Charge() < 1) continue; // discarded pixel
811 n1[0] = n1[1] = 999;
812 n2[0] = n2[1] = 1;
813 for (Int_t j = 0; j < 2; j++) {
814 if (pixPtr->Size(j) - wxy[j] < 1.e-4) continue;
815 dist[j] = (pixPtr->Coord(j) - xy0[j]) / wxy[j] / 2; // normalized distance to "normal" pixel
816 n2[j] = TMath::Nint (pixPtr->Size(j) / wxy[j]);
817 n1[j] = n2[j] == 1 ? TMath::Nint(dist[j]) : (Int_t)dist[j];
818 }
819 if (n1[0] > 998 && n1[1] > 998) continue;
820 if (fDebug) cout << " Different " << pixPtr->Size(0) << " " << wxy[0] << " "
821 << pixPtr->Size(1) << " " << wxy[1] <<endl;
822
0627f609 823 if (n2[0] > 2 || n2[1] > 2) {
94eb555e 824 //cout << n2[0] << " " << n2[1] << endl;
0627f609 825 if (n2[0] > 2 && n1[0] < 999) n1[0]--;
826 if (n2[1] > 2 && n1[1] < 999) n1[1]--;
827 }
cc87ebcd 828 //cout << n1[0] << " " << n2[0] << " " << n1[1] << " " << n2[1] << endl;
829 pix = *pixPtr;
830 pix.SetSize(0, wxy[0]); pix.SetSize(1, wxy[1]);
831 //pixPtr->Print();
832 for (Int_t ii = 0; ii < n2[0]; ii++) {
833 if (n1[0] < 999) pix.SetCoord(0, xy0[0] + (n1[0] + TMath::Sign(1.,dist[0]) * ii) * 2 * wxy[0]);
834 for (Int_t jj = 0; jj < n2[1]; jj++) {
835 if (n1[1] < 999) pix.SetCoord(1, xy0[1] + (n1[1] + TMath::Sign(1.,dist[1]) * jj) * 2 * wxy[1]);
836 fPixArray->Add(new AliMUONPixel(pix));
837 //pix.Print();
0df3ca52 838 }
0df3ca52 839 }
cc87ebcd 840 pixPtr->SetCharge(0);
841 } // for (Int_t i = 0; i < nPix;
0df3ca52 842}
843
844//_____________________________________________________________________________
2b1e4f0e 845Bool_t AliMUONClusterFinderAZ::MainLoop(Int_t iSimple)
0df3ca52 846{
d19b6003 847/// Repeat MLEM algorithm until pixel size becomes sufficiently small
0df3ca52 848
849 TH2D *mlem;
850
851 Int_t ix, iy;
852 //Int_t nn, xList[10], yList[10];
853 Int_t nPix = fPixArray->GetEntriesFast();
0df3ca52 854 AliMUONPixel *pixPtr = 0;
855 Double_t *coef = 0, *probi = 0;
2b1e4f0e 856 AddVirtualPad(); // add virtual pads if necessary
857 Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0;
1af223d7 858 for (Int_t i = 0; i < npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++;
859 if (fDraw) fDraw->ResetMuon();
0df3ca52 860
861 while (1) {
862
863 mlem = (TH2D*) gROOT->FindObject("mlem");
864 if (mlem) mlem->Delete();
865 // Calculate coefficients
2b1e4f0e 866 if (fDebug) cout << " nPix, npadTot, npadOK " << nPix << " " << npadTot << " " << npadOK << endl;
0df3ca52 867
868 // Calculate coefficients and pixel visibilities
869 coef = new Double_t [npadTot*nPix];
870 probi = new Double_t [nPix];
2b1e4f0e 871 for (Int_t ipix=0; ipix<nPix; ipix++) probi[ipix] = 0;
872 Int_t indx = 0, indx1 = 0, cath = 0;
873
874 for (Int_t j=0; j<npadTot; j++) {
875 indx = j*nPix;
876 if (fPadIJ[1][j] == 0) {
0df3ca52 877 cath = fPadIJ[0][j];
0627f609 878 ix = fPadIJ[2][j];
879 iy = fPadIJ[3][j];
880 fSegmentation[cath]->SetPad(ix, iy);
2b1e4f0e 881 /*
882 fSegmentation[cath]->Neighbours(fInput->DetElemId(),ix,iy,&nn,xList,yList);
883 if (nn != 4) {
884 cout << nn << ": ";
885 for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";}
886 cout << endl;
887 }
888 */
889 }
890
891 for (Int_t ipix=0; ipix<nPix; ipix++) {
892 indx1 = indx + ipix;
893 if (fPadIJ[1][j] < 0) { coef[indx1] = 0; continue; }
894 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
0627f609 895 fSegmentation[cath]->SetHit(pixPtr->Coord(0), pixPtr->Coord(1), fZpad);
896 coef[indx1] = fInput->Mathieson()->IntXY(fInput->DetElemId(),fInput->Segmentation2(cath));
2b1e4f0e 897 probi[ipix] += coef[indx1];
898 } // for (Int_t ipix=0;
899 } // for (Int_t j=0;
900 for (Int_t ipix=0; ipix<nPix; ipix++) if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel
0df3ca52 901
902 // MLEM algorithm
2b1e4f0e 903 Mlem(coef, probi, 15);
0df3ca52 904
cd747ddb 905 Double_t xylim[4] = {999, 999, 999, 999};
0df3ca52 906 for (Int_t ipix=0; ipix<nPix; ipix++) {
907 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
cc87ebcd 908 //cout << ipix+1; pixPtr->Print();
0df3ca52 909 for (Int_t i=0; i<4; i++)
910 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
0df3ca52 911 }
912 for (Int_t i=0; i<4; i++) {
2b1e4f0e 913 xylim[i] -= pixPtr->Size(i/2); if (fDebug) cout << (i%2 ? -1 : 1)*xylim[i] << " "; }
914 if (fDebug) cout << endl;
0df3ca52 915
2b1e4f0e 916 // Adjust histogram to approximately the same limits as for the pads
0df3ca52 917 // (for good presentation)
1af223d7 918 if (fDraw) fDraw->AdjustHist(xylim, pixPtr);
919
0df3ca52 920 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
921 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
2b1e4f0e 922
0df3ca52 923 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
924 for (Int_t ipix=0; ipix<nPix; ipix++) {
925 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
926 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
927 }
1af223d7 928 if (fDraw) fDraw->DrawHist("c2", mlem);
0df3ca52 929
930 // Check if the total charge of pixels is too low
931 Double_t qTot = 0;
932 for (Int_t i=0; i<nPix; i++) {
933 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
934 qTot += pixPtr->Charge();
935 }
2b1e4f0e 936 if (qTot < 1.e-4 || npadOK < 3 && qTot < 7) {
0df3ca52 937 delete [] coef; delete [] probi; coef = 0; probi = 0;
938 fPixArray->Delete();
2b1e4f0e 939 for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) fPadIJ[1][i] = -1;
0df3ca52 940 return kFALSE;
941 }
942
943 // Plot data - expectation
944 /*
945 Double_t x, y, cont;
946 for (Int_t j=0; j<npadTot; j++) {
947 Double_t sum1 = 0;
948 for (Int_t i=0; i<nPix; i++) {
949 // Caculate expectation
950 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
951 sum1 += pixPtr->Charge()*coef[j*nPix+i];
952 }
0627f609 953 sum1 = TMath::Min (sum1,fgkSaturation);
0df3ca52 954 x = fXyq[0][j];
955 y = fXyq[1][j];
956 cath = fPadIJ[0][j];
957 Int_t ihist = cath*2;
958 ix = fHist[ihist]->GetXaxis()->FindBin(x);
959 iy = fHist[ihist]->GetYaxis()->FindBin(y);
960 cont = fHist[ihist]->GetCellContent(ix,iy);
961 if (cont == 0 && fHist[ihist+1]) {
962 ihist += 1;
963 ix = fHist[ihist]->GetXaxis()->FindBin(x);
964 iy = fHist[ihist]->GetYaxis()->FindBin(y);
965 }
966 fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1);
967 }
968 ((TCanvas*)gROOT->FindObject("c1"))->cd(1);
969 //gPad->SetTheta(55);
970 //gPad->SetPhi(30);
971 //mlem->Draw("lego1");
972 gPad->Modified();
973 ((TCanvas*)gROOT->FindObject("c1"))->cd(2);
974 gPad->Modified();
975 */
976
2b1e4f0e 977 if (iSimple) {
978 // Simple cluster - skip further passes thru EM-procedure
2b1e4f0e 979 Simple();
980 delete [] coef; delete [] probi; coef = 0; probi = 0;
981 fPixArray->Delete();
982 return kTRUE;
983 }
984
0df3ca52 985 // Calculate position of the center-of-gravity around the maximum pixel
986 Double_t xyCOG[2];
987 FindCOG(mlem, xyCOG);
988
989 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break;
2b1e4f0e 990 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.007 && pixPtr->Size(0) > pixPtr->Size(1)) break;
0df3ca52 991 //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) {
992 // Sort pixels according to the charge
993 fPixArray->Sort();
994 /*
995 for (Int_t i=0; i<nPix; i++) {
996 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
997 cout << i+1; pixPtr->Print();
998 }
999 */
1000 Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge();
1001 pixMin = TMath::Min (pixMin,50.);
1002
1003 // Decrease pixel size and shift pixels to make them centered at
1004 // the maximum one
1005 indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1006 Double_t width = 0, shift[2]={0};
1007 ix = 1;
1008 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1009 Int_t nPix1 = nPix; nPix = 0;
1010 for (Int_t ipix=0; ipix<nPix1; ipix++) {
1011 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1012 if (nPix >= npadOK) { // too many pixels already
1013 fPixArray->RemoveAt(ipix);
1014 delete pixPtr;
1015 continue;
1016 }
1017 if (pixPtr->Charge() < pixMin) { // low charge
1018 fPixArray->RemoveAt(ipix);
1019 delete pixPtr;
1020 continue;
1021 }
1022 for (Int_t i=0; i<2; i++) {
1023 if (!i) {
1024 pixPtr->SetCharge(10);
1025 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1026 width = -pixPtr->Size(indx);
1027 pixPtr->Shift(indx, width);
1028 // Shift pixel position
1029 if (ix) {
1030 ix = 0;
1031 for (Int_t j=0; j<2; j++) {
1032 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1033 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1034 }
1035 //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl;
1036 } // if (ix)
1037 pixPtr->Shift(0, -shift[0]);
1038 pixPtr->Shift(1, -shift[1]);
1039 } else {
1040 pixPtr = new AliMUONPixel(*pixPtr);
1041 pixPtr->Shift(indx, -2*width);
1042 fPixArray->Add((TObject*)pixPtr);
1043 } // else
1044 //pixPtr->Print();
1045 for (Int_t i=0; i<4; i++)
1046 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1047 } // for (Int_t i=0; i<2;
1048 nPix += 2;
1049 } // for (Int_t ipix=0;
1050
1051 fPixArray->Compress();
1052 nPix = fPixArray->GetEntriesFast();
1053
1054 // Remove excessive pixels
1055 if (nPix > npadOK) {
1056 for (Int_t ipix=npadOK; ipix<nPix; ipix++) {
1057 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1058 fPixArray->RemoveAt(ipix);
1059 delete pixPtr;
1060 }
1061 } else {
1062 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0);
1063 // add pixels if the maximum is at the limit of pixel area
1064 // start from Y-direction
1065 Int_t j = 0;
1066 for (Int_t i=3; i>-1; i--) {
1067 if (nPix < npadOK &&
1068 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) {
1069 pixPtr = new AliMUONPixel(*pixPtr);
1070 pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1071 j = TMath::Even (i/2);
1072 pixPtr->SetCoord(j, xyCOG[j]);
1073 fPixArray->Add((TObject*)pixPtr);
1074 nPix++;
1075 }
1076 }
1077 } // else
1078
1079 fPixArray->Compress();
1080 nPix = fPixArray->GetEntriesFast();
1081 delete [] coef; delete [] probi; coef = 0; probi = 0;
1082 } // while (1)
1083
1084 // remove pixels with low signal or low visibility
1085 // Cuts are empirical !!!
1086 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1087 thresh = TMath::Min (thresh,50.);
1088 Double_t cmax = -1, charge = 0;
1089 for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]);
2b1e4f0e 1090 //cout << thresh << " " << cmax << " " << cmax*0.9 << endl;
0df3ca52 1091 // Mark pixels which should be removed
1092 for (Int_t i=0; i<nPix; i++) {
1093 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1094 charge = pixPtr->Charge();
1095 if (charge < thresh) pixPtr->SetCharge(-charge);
2b1e4f0e 1096 //else if (cmax > 1.91) {
1097 // if (probi[i] < 1.9) pixPtr->SetCharge(-charge);
1098 //}
1099 //AZ else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge);
0627f609 1100 //18-01-06 else if (probi[i] < cmax*0.8) pixPtr->SetCharge(-charge);
2b1e4f0e 1101 //cout << i << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << charge << " " << probi[i] << endl;
0df3ca52 1102 }
1103 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1104 Int_t near = 0;
1105 for (Int_t i=0; i<nPix; i++) {
1106 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1107 charge = pixPtr->Charge();
1108 if (charge > 0) continue;
1109 near = FindNearest(pixPtr);
1110 pixPtr->SetCharge(0);
2b1e4f0e 1111 probi[i] = 0; // make it "invisible"
0df3ca52 1112 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near);
2b1e4f0e 1113 pixPtr->SetCharge(pixPtr->Charge() + (-charge));
0df3ca52 1114 }
2b1e4f0e 1115 Mlem(coef,probi,2);
0df3ca52 1116 // Update histogram
1117 for (Int_t i=0; i<nPix; i++) {
1118 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1119 ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1120 iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1121 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1122 }
1af223d7 1123 if (fDraw) fDraw->DrawHist("c2", mlem);
0df3ca52 1124
0df3ca52 1125 // Try to split into clusters
1126 Bool_t ok = kTRUE;
1127 if (mlem->GetSum() < 1) ok = kFALSE;
1128 else Split(mlem, coef);
1129 delete [] coef; delete [] probi; coef = 0; probi = 0;
1130 fPixArray->Delete();
1131 return ok;
1132}
1133
1134//_____________________________________________________________________________
2b1e4f0e 1135void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi, Int_t nIter)
0df3ca52 1136{
d19b6003 1137/// Use MLEM to find pixel charges
0df3ca52 1138
1139 Int_t nPix = fPixArray->GetEntriesFast();
1140 Int_t npad = fnPads[0] + fnPads[1];
1141 Double_t *probi1 = new Double_t [nPix];
2b1e4f0e 1142 Double_t probMax = 0;
0df3ca52 1143 Int_t indx, indx1;
1144 AliMUONPixel *pixPtr;
1145
2b1e4f0e 1146 for (Int_t ipix=0; ipix<nPix; ipix++) if (probi[ipix] > probMax) probMax = probi[ipix];
1147 for (Int_t iter=0; iter<nIter; iter++) {
0df3ca52 1148 // Do iterations
1149 for (Int_t ipix=0; ipix<nPix; ipix++) {
1150 // Correct each pixel
1151 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1152 Double_t sum = 0;
2b1e4f0e 1153 //probi1[ipix] = probi[ipix];
1154 probi1[ipix] = probMax;
0df3ca52 1155 for (Int_t j=0; j<npad; j++) {
1156 if (fPadIJ[1][j] < 0) continue;
1157 Double_t sum1 = 0;
1158 indx1 = j*nPix;
1159 indx = indx1 + ipix;
1160 for (Int_t i=0; i<nPix; i++) {
1161 // Caculate expectation
1162 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1163 sum1 += pixPtr->Charge()*coef[indx1+i];
1164 } // for (Int_t i=0;
0627f609 1165 if (fXyq[2][j] > fgkSaturation-1 && sum1 > fXyq[2][j]) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows
0df3ca52 1166 //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl;
1167 if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1;
1168 } // for (Int_t j=0;
1169 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix);
1170 if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1171 } // for (Int_t ipix=0;
1172 } // for (Int_t iter=0;
1173 delete [] probi1;
1174 return;
1175}
1176
1177//_____________________________________________________________________________
1178void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc)
1179{
d19b6003 1180/// Calculate position of the center-of-gravity around the maximum pixel
0df3ca52 1181
1182 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1183 Int_t i1 = -9, j1 = -9;
1184 mlem->GetMaximumBin(ixmax,iymax,ix);
1185 Int_t nx = mlem->GetNbinsX();
1186 Int_t ny = mlem->GetNbinsY();
1187 Double_t thresh = mlem->GetMaximum()/10;
1188 Double_t x, y, cont, xq=0, yq=0, qq=0;
1189
1190 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1191 y = mlem->GetYaxis()->GetBinCenter(i);
1192 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1193 cont = mlem->GetCellContent(j,i);
1194 if (cont < thresh) continue;
1195 if (i != i1) {i1 = i; nsumy++;}
1196 if (j != j1) {j1 = j; nsumx++;}
1197 x = mlem->GetXaxis()->GetBinCenter(j);
1198 xq += x*cont;
1199 yq += y*cont;
1200 qq += cont;
1201 nsum++;
1202 }
1203 }
1204
1205 Double_t cmax = 0;
1206 Int_t i2 = 0, j2 = 0;
1207 x = y = 0;
1208 if (nsumy == 1) {
1209 // one bin in Y - add one more (with the largest signal)
1210 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1211 if (i == iymax) continue;
1212 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1213 cont = mlem->GetCellContent(j,i);
1214 if (cont > cmax) {
1215 cmax = cont;
1216 x = mlem->GetXaxis()->GetBinCenter(j);
1217 y = mlem->GetYaxis()->GetBinCenter(i);
1218 i2 = i;
1219 j2 = j;
1220 }
1221 }
1222 }
1223 xq += x*cmax;
1224 yq += y*cmax;
1225 qq += cmax;
1226 if (i2 != i1) nsumy++;
1227 if (j2 != j1) nsumx++;
1228 nsum++;
1229 } // if (nsumy == 1)
1230
1231 if (nsumx == 1) {
1232 // one bin in X - add one more (with the largest signal)
1233 cmax = x = y = 0;
1234 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1235 if (j == ixmax) continue;
1236 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1237 cont = mlem->GetCellContent(j,i);
1238 if (cont > cmax) {
1239 cmax = cont;
1240 x = mlem->GetXaxis()->GetBinCenter(j);
1241 y = mlem->GetYaxis()->GetBinCenter(i);
1242 i2 = i;
1243 j2 = j;
1244 }
1245 }
1246 }
1247 xq += x*cmax;
1248 yq += y*cmax;
1249 qq += cmax;
1250 if (i2 != i1) nsumy++;
1251 if (j2 != j1) nsumx++;
1252 nsum++;
1253 } // if (nsumx == 1)
1254
1255 xyc[0] = xq/qq; xyc[1] = yq/qq;
2b1e4f0e 1256 if (fDebug) cout << xyc[0] << " " << xyc[1] << " " << qq << " " << nsum << " " << nsumx << " " << nsumy << endl;
0df3ca52 1257 return;
1258}
1259
1260//_____________________________________________________________________________
1261Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0)
1262{
d19b6003 1263/// Find the pixel nearest to the given one
1264/// (algorithm may be not very efficient)
0df3ca52 1265
1266 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1267 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1268 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1269 AliMUONPixel *pixPtr;
1270
1271 for (Int_t i=0; i<nPix; i++) {
1272 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1273 if (pixPtr->Charge() < 0.5) continue;
1274 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1275 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1276 r = dx *dx + dy * dy;
1277 if (r < rmin) { rmin = r; imin = i; }
1278 }
1279 return imin;
1280}
1281
1282//_____________________________________________________________________________
1283void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef)
1284{
d19b6003 1285/// The main steering function to work with clusters of pixels in anode
1286/// plane (find clusters, decouple them from each other, merge them (if
1287/// necessary), pick up coupled pads, call the fitting function)
0df3ca52 1288
1289 Int_t nx = mlem->GetNbinsX();
1290 Int_t ny = mlem->GetNbinsY();
1291 Int_t nPix = fPixArray->GetEntriesFast();
1292
1293 Bool_t *used = new Bool_t[ny*nx];
1294 Double_t cont;
1295 Int_t nclust = 0, indx, indx1;
1296
1297 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1298
1299 TObjArray *clusters[200]={0};
1300 TObjArray *pix;
1301
1302 // Find clusters of histogram bins (easier to work in 2-D space)
1303 for (Int_t i=1; i<=ny; i++) {
1304 for (Int_t j=1; j<=nx; j++) {
1305 indx = (i-1)*nx + j - 1;
1306 if (used[indx]) continue;
1307 cont = mlem->GetCellContent(j,i);
1308 if (cont < 0.5) continue;
1309 pix = new TObjArray(20);
1310 used[indx] = 1;
1311 pix->Add(BinToPix(mlem,j,i));
1312 AddBin(mlem, i, j, 0, used, pix); // recursive call
2b1e4f0e 1313 if (nclust >= 200) AliFatal(" Too many clusters !!!");
0df3ca52 1314 clusters[nclust++] = pix;
0df3ca52 1315 } // for (Int_t j=1; j<=nx; j++) {
1316 } // for (Int_t i=1; i<=ny;
2b1e4f0e 1317 if (fDebug) cout << nclust << endl;
0df3ca52 1318 delete [] used; used = 0;
1319
1320 // Compute couplings between clusters and clusters to pads
1321 Int_t npad = fnPads[0] + fnPads[1];
1322
2b1e4f0e 1323 // Write out some information for algorithm development
1324 Int_t cath=0, npadx[2]={0}, npady[2]={0};
1325 Double_t xlow[2]={9999,9999}, xhig[2]={-9999,-9999};
1326 Double_t ylow[2]={9999,9999}, yhig[2]={-9999,-9999};
1327 for (Int_t j=0; j<npad; j++) {
1328 if (fXyq[3][j] < 0) continue; // exclude virtual pads
1329 cath = fPadIJ[0][j];
1330 if (fXyq[0][j] < xlow[cath]-0.001) {
1331 if (fXyq[0][j]+fXyq[3][j] <= xlow[cath] && npadx[cath]) npadx[cath]++;
1332 xlow[cath] = fXyq[0][j];
1333 }
1334 if (fXyq[0][j] > xhig[cath]+0.001) {
1335 if (fXyq[0][j]-fXyq[3][j] >= xhig[cath]) npadx[cath]++;
1336 xhig[cath] = fXyq[0][j];
1337 }
1338 if (fXyq[1][j] < ylow[cath]-0.001) {
1339 if (fXyq[1][j]+fXyq[4][j] <= ylow[cath] && npady[cath]) npady[cath]++;
1340 ylow[cath] = fXyq[1][j];
1341 }
1342 if (fXyq[1][j] > yhig[cath]+0.001) {
1343 if (fXyq[1][j]-fXyq[4][j] >= yhig[cath]) npady[cath]++;
1344 yhig[cath] = fXyq[1][j];
1345 }
1346 }
1347 //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]);
1348
0df3ca52 1349 // Exclude pads with overflows
1350 for (Int_t j=0; j<npad; j++) {
0627f609 1351 if (fXyq[2][j] > fgkSaturation-1) fPadIJ[1][j] = -5;
0df3ca52 1352 else fPadIJ[1][j] = 0;
1353 }
1354
1355 // Compute couplings of clusters to pads
c1aed84f 1356 TMatrixD *aijclupad = new TMatrixD(nclust,npad);
1357 *aijclupad = 0;
0df3ca52 1358 Int_t npxclu;
1359 for (Int_t iclust=0; iclust<nclust; iclust++) {
1360 pix = clusters[iclust];
1361 npxclu = pix->GetEntriesFast();
1362 for (Int_t i=0; i<npxclu; i++) {
1363 indx = fPixArray->IndexOf(pix->UncheckedAt(i));
1364 for (Int_t j=0; j<npad; j++) {
2b1e4f0e 1365 if (fPadIJ[1][j] < 0 && fPadIJ[1][j] != -5) continue;
343146bf 1366 if (coef[j*nPix+indx] < fgkCouplMin) continue;
c1aed84f 1367 (*aijclupad)(iclust,j) += coef[j*nPix+indx];
0df3ca52 1368 }
1369 }
1370 }
1371 // Compute couplings between clusters
c1aed84f 1372 TMatrixD *aijcluclu = new TMatrixD(nclust,nclust);
1373 *aijcluclu = 0;
0df3ca52 1374 for (Int_t iclust=0; iclust<nclust; iclust++) {
1375 for (Int_t j=0; j<npad; j++) {
1376 // Exclude overflows
1377 if (fPadIJ[1][j] < 0) continue;
343146bf 1378 if ((*aijclupad)(iclust,j) < fgkCouplMin) continue;
0df3ca52 1379 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
343146bf 1380 if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue;
c1aed84f 1381 (*aijcluclu)(iclust,iclust1) +=
1382 TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j));
0df3ca52 1383 }
1384 }
1385 }
1386 for (Int_t iclust=0; iclust<nclust; iclust++) {
1387 for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) {
c1aed84f 1388 (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1);
0df3ca52 1389 }
1390 }
1391
2b1e4f0e 1392 if (fDebug && nclust > 1) aijcluclu->Print();
0df3ca52 1393
1394 // Find groups of coupled clusters
1395 used = new Bool_t[nclust];
1396 for (Int_t i=0; i<nclust; i++) used[i] = kFALSE;
1397 Int_t *clustNumb = new Int_t[nclust];
1398 Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0;
1399 Double_t parOk[8];
1400
1401 for (Int_t igroup=0; igroup<nclust; igroup++) {
1402 if (used[igroup]) continue;
1403 used[igroup] = kTRUE;
1404 clustNumb[0] = igroup;
1405 nCoupled = 1;
1406 // Find group of coupled clusters
c1aed84f 1407 AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive
2b1e4f0e 1408 if (fDebug) {
1409 cout << " nCoupled: " << nCoupled << endl;
1410 for (Int_t i=0; i<nCoupled; i++) cout << clustNumb[i] << " "; cout << endl;
1411 }
1412 fnCoupled = nCoupled;
0df3ca52 1413
1414 while (nCoupled > 0) {
1415
1416 if (nCoupled < 4) {
1417 nForFit = nCoupled;
1418 for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i];
1419 } else {
1420 // Too many coupled clusters to fit - try to decouple them
1421 // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with
1422 // all the others in the group
1423 for (Int_t j=0; j<3; j++) minGroup[j] = -1;
c1aed84f 1424 Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup);
0df3ca52 1425
1426 // Flag clusters for fit
1427 nForFit = 0;
1428 while (minGroup[nForFit] >= 0 && nForFit < 3) {
2b1e4f0e 1429 if (fDebug) cout << clustNumb[minGroup[nForFit]] << " ";
0df3ca52 1430 clustFit[nForFit] = clustNumb[minGroup[nForFit]];
1431 clustNumb[minGroup[nForFit]] -= 999;
1432 nForFit++;
1433 }
2b1e4f0e 1434 if (fDebug) cout << nForFit << " " << coupl << endl;
0df3ca52 1435 } // else
1436
1437 // Select pads for fit.
c1aed84f 1438 if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) {
0df3ca52 1439 // Deselect pads
2b1e4f0e 1440 for (Int_t j=0; j<npad; j++) {
1441 if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0;
1442 if (TMath::Abs(fPadIJ[1][j]) == -9) fPadIJ[1][j] = -5;
1443 }
0df3ca52 1444 // Merge the failed cluster candidates (with too few pads to fit) with
1445 // the one with the strongest coupling
c1aed84f 1446 Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad);
0df3ca52 1447 } else {
1448 // Do the fit
0627f609 1449 nfit = Fit(0, nForFit, clustFit, clusters, parOk);
0df3ca52 1450 }
1451
1452 // Subtract the fitted charges from pads with strong coupling and/or
1453 // return pads for further use
1454 UpdatePads(nfit, parOk);
1455
1456 // Mark used pads
2b1e4f0e 1457 for (Int_t j=0; j<npad; j++) {
1458 if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;
1459 if (fPadIJ[1][j] == -9) fPadIJ[1][j] = -5;
1460 }
0df3ca52 1461
1462 // Sort the clusters (move to the right the used ones)
1463 Int_t beg = 0, end = nCoupled - 1;
1464 while (beg < end) {
1465 if (clustNumb[beg] >= 0) { beg++; continue; }
1466 for (Int_t j=end; j>beg; j--) {
1467 if (clustNumb[j] < 0) continue;
1468 end = j - 1;
1469 indx = clustNumb[beg];
1470 clustNumb[beg] = clustNumb[j];
1471 clustNumb[j] = indx;
1472 break;
1473 }
1474 beg++;
1475 }
1476
1477 nCoupled -= nForFit;
1478 if (nCoupled > 3) {
1479 // Remove couplings of used clusters
1480 for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) {
1481 indx = clustNumb[iclust] + 999;
1482 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1483 indx1 = clustNumb[iclust1];
c1aed84f 1484 (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0;
0df3ca52 1485 }
1486 }
1487
1488 // Update the remaining clusters couplings (exclude couplings from
1489 // the used pads)
1490 for (Int_t j=0; j<npad; j++) {
1491 if (fPadIJ[1][j] != -1) continue;
1492 for (Int_t iclust=0; iclust<nCoupled; iclust++) {
1493 indx = clustNumb[iclust];
343146bf 1494 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
0df3ca52 1495 for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) {
1496 indx1 = clustNumb[iclust1];
343146bf 1497 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
0df3ca52 1498 // Check this
c1aed84f 1499 (*aijcluclu)(indx,indx1) -=
1500 TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j));
1501 (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1);
0df3ca52 1502 }
1503 }
2b1e4f0e 1504 fPadIJ[1][j] = -8;
0df3ca52 1505 } // for (Int_t j=0; j<npad;
1506 } // if (nCoupled > 3)
1507 } // while (nCoupled > 0)
1508 } // for (Int_t igroup=0; igroup<nclust;
1509
c1aed84f 1510 aijcluclu->Delete(); aijclupad->Delete();
0df3ca52 1511 for (Int_t iclust=0; iclust<nclust; iclust++) {
1512 pix = clusters[iclust];
1513 pix->Clear();
1514 delete pix; pix = 0;
1515 }
1516 delete [] clustNumb; clustNumb = 0; delete [] used; used = 0;
1517}
1518
1519//_____________________________________________________________________________
1520void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix)
1521{
d19b6003 1522/// Add a bin to the cluster
0df3ca52 1523
1524 Int_t nx = mlem->GetNbinsX();
1525 Int_t ny = mlem->GetNbinsY();
1526 Double_t cont1, cont = mlem->GetCellContent(jc,ic);
1527 AliMUONPixel *pixPtr = 0;
1528
1529 for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) {
1530 for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) {
1531 if (i != ic && j != jc) continue;
1532 if (used[(i-1)*nx+j-1]) continue;
1533 cont1 = mlem->GetCellContent(j,i);
1534 if (mode && cont1 > cont) continue;
1535 used[(i-1)*nx+j-1] = kTRUE;
1536 if (cont1 < 0.5) continue;
1537 if (pix) pix->Add(BinToPix(mlem,j,i));
1538 else {
1539 pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j),
1540 mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
1541 fPixArray->Add((TObject*)pixPtr);
1542 }
1543 AddBin(mlem, i, j, mode, used, pix); // recursive call
1544 }
1545 }
1546}
1547
1548//_____________________________________________________________________________
1549TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic)
1550{
d19b6003 1551/// Translate histogram bin to pixel
0df3ca52 1552
1553 Double_t yc = mlem->GetYaxis()->GetBinCenter(ic);
1554 Double_t xc = mlem->GetXaxis()->GetBinCenter(jc);
1555
1556 Int_t nPix = fPixArray->GetEntriesFast();
0627f609 1557 AliMUONPixel *pixPtr = NULL;
0df3ca52 1558
1559 // Compare pixel and bin positions
1560 for (Int_t i=0; i<nPix; i++) {
1561 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
1562 if (pixPtr->Charge() < 0.5) continue;
1563 if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr;
1564 }
94eb555e 1565 AliError(Form(" Something wrong ??? %f %f ", xc, yc));
0df3ca52 1566 return NULL;
1567}
1568
1569//_____________________________________________________________________________
c1aed84f 1570void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled)
0df3ca52 1571{
d19b6003 1572/// Add a cluster to the group of coupled clusters
0df3ca52 1573
1574 for (Int_t i=0; i<nclust; i++) {
1575 if (used[i]) continue;
343146bf 1576 if ((*aijcluclu)(i,ic) < fgkCouplMin) continue;
0df3ca52 1577 used[i] = kTRUE;
1578 clustNumb[nCoupled++] = i;
c1aed84f 1579 AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled);
0df3ca52 1580 }
1581}
1582
1583//_____________________________________________________________________________
c1aed84f 1584Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup)
0df3ca52 1585{
d19b6003 1586/// Find group of clusters with minimum coupling to all the others
0df3ca52 1587
1588 Int_t i123max = TMath::Min(3,nCoupled/2);
1589 Int_t indx, indx1, indx2, indx3, nTot = 0;
1590 Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0;
1591
1592 for (Int_t i123=1; i123<=i123max; i123++) {
1593
1594 if (i123 == 1) {
1595 coupl1 = new Double_t [nCoupled];
1596 for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0;
1597 }
1598 else if (i123 == 2) {
1599 nTot = nCoupled*nCoupled;
1600 coupl2 = new Double_t [nTot];
1601 for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999;
1602 } else {
1603 nTot = nTot*nCoupled;
1604 coupl3 = new Double_t [nTot];
1605 for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999;
1606 } // else
1607
1608 for (Int_t i=0; i<nCoupled; i++) {
1609 indx1 = clustNumb[i];
1610 for (Int_t j=i+1; j<nCoupled; j++) {
1611 indx2 = clustNumb[j];
1612 if (i123 == 1) {
c1aed84f 1613 coupl1[i] += (*aijcluclu)(indx1,indx2);
1614 coupl1[j] += (*aijcluclu)(indx1,indx2);
0df3ca52 1615 }
1616 else if (i123 == 2) {
1617 indx = i*nCoupled + j;
1618 coupl2[indx] = coupl1[i] + coupl1[j];
c1aed84f 1619 coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2));
0df3ca52 1620 } else {
1621 for (Int_t k=j+1; k<nCoupled; k++) {
1622 indx3 = clustNumb[k];
1623 indx = i*nCoupled*nCoupled + j*nCoupled + k;
1624 coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k];
c1aed84f 1625 coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3));
0df3ca52 1626 }
1627 } // else
1628 } // for (Int_t j=i+1;
1629 } // for (Int_t i=0;
1630 } // for (Int_t i123=1;
1631
1632 // Find minimum coupling
1633 Double_t couplMin = 9999;
1634 Int_t locMin = 0;
1635
1636 for (Int_t i123=1; i123<=i123max; i123++) {
1637 if (i123 == 1) {
1638 locMin = TMath::LocMin(nCoupled, coupl1);
1639 couplMin = coupl1[locMin];
1640 minGroup[0] = locMin;
1641 delete [] coupl1; coupl1 = 0;
1642 }
1643 else if (i123 == 2) {
1644 locMin = TMath::LocMin(nCoupled*nCoupled, coupl2);
1645 if (coupl2[locMin] < couplMin) {
1646 couplMin = coupl2[locMin];
1647 minGroup[0] = locMin/nCoupled;
1648 minGroup[1] = locMin%nCoupled;
1649 }
1650 delete [] coupl2; coupl2 = 0;
1651 } else {
1652 locMin = TMath::LocMin(nTot, coupl3);
1653 if (coupl3[locMin] < couplMin) {
1654 couplMin = coupl3[locMin];
1655 minGroup[0] = locMin/nCoupled/nCoupled;
1656 minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled;
1657 minGroup[2] = locMin%nCoupled;
1658 }
1659 delete [] coupl3; coupl3 = 0;
1660 } // else
1661 } // for (Int_t i123=1;
1662 return couplMin;
1663}
1664
1665//_____________________________________________________________________________
c1aed84f 1666Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad)
0df3ca52 1667{
d19b6003 1668/// Select pads for fit. If too many coupled clusters, find pads giving
1669/// the strongest coupling with the rest of clusters and exclude them from the fit.
0df3ca52 1670
1671 Int_t npad = fnPads[0] + fnPads[1];
c1aed84f 1672 Double_t *padpix = 0;
0df3ca52 1673
1674 if (nCoupled > 3) {
c1aed84f 1675 padpix = new Double_t[npad];
1676 for (Int_t i=0; i<npad; i++) padpix[i] = 0;
0df3ca52 1677 }
1678
1679 Int_t nOK = 0, indx, indx1;
1680 for (Int_t iclust=0; iclust<nForFit; iclust++) {
1681 indx = clustFit[iclust];
1682 for (Int_t j=0; j<npad; j++) {
343146bf 1683 if ((*aijclupad)(indx,j) < fgkCouplMin) continue;
2b1e4f0e 1684 if (fPadIJ[1][j] == -5) fPadIJ[1][j] = -9; // flag overflow
1685 if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads
1686 if (!fPadIJ[1][j]) { fPadIJ[1][j] = 1; nOK++; } // pad to be used in fit
0df3ca52 1687 if (nCoupled > 3) {
1688 // Check other clusters
1689 for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) {
1690 indx1 = clustNumb[iclust1];
1691 if (indx1 < 0) continue;
343146bf 1692 if ((*aijclupad)(indx1,j) < fgkCouplMin) continue;
c1aed84f 1693 padpix[j] += (*aijclupad)(indx1,j);
0df3ca52 1694 }
1695 } // if (nCoupled > 3)
1696 } // for (Int_t j=0; j<npad;
1697 } // for (Int_t iclust=0; iclust<nForFit
1698 if (nCoupled < 4) return nOK;
1699
1700 Double_t aaa = 0;
1701 for (Int_t j=0; j<npad; j++) {
343146bf 1702 if (padpix[j] < fgkCouplMin) continue;
2b1e4f0e 1703 if (fDebug) cout << j << " " << padpix[j] << " " << fXyq[0][j] << " " << fXyq[1][j] << endl;
c1aed84f 1704 aaa += padpix[j];
0df3ca52 1705 fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters
1706 nOK--;
1707 }
c1aed84f 1708 delete [] padpix; padpix = 0;
0df3ca52 1709 return nOK;
1710}
1711
1712//_____________________________________________________________________________
c1aed84f 1713void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad)
0df3ca52 1714{
d19b6003 1715/// Merge the group of clusters with the one having the strongest coupling with them
0df3ca52 1716
1717 Int_t indx, indx1, npxclu, npxclu1, imax=0;
1718 TObjArray *pix, *pix1;
1719 Double_t couplMax;
1720
1721 for (Int_t icl=0; icl<nForFit; icl++) {
1722 indx = clustFit[icl];
1723 pix = clusters[indx];
1724 npxclu = pix->GetEntriesFast();
1725 couplMax = -1;
1726 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1727 indx1 = clustNumb[icl1];
1728 if (indx1 < 0) continue;
c1aed84f 1729 if ((*aijcluclu)(indx,indx1) > couplMax) {
1730 couplMax = (*aijcluclu)(indx,indx1);
0df3ca52 1731 imax = indx1;
1732 }
1733 } // for (Int_t icl1=0;
343146bf 1734 /*if (couplMax < fgkCouplMin) {
0df3ca52 1735 cout << " Oops " << couplMax << endl;
c1aed84f 1736 aijcluclu->Print();
0df3ca52 1737 cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl;
1738 ::exit(0);
1739 }*/
1740 // Add to it
1741 pix1 = clusters[imax];
1742 npxclu1 = pix1->GetEntriesFast();
1743 // Add pixels
1744 for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); }
2b1e4f0e 1745 if (fDebug) cout << " New number of pixels: " << npxclu1 << " " << pix1->GetEntriesFast() << endl;
0df3ca52 1746 //Add cluster-to-cluster couplings
c1aed84f 1747 //aijcluclu->Print();
0df3ca52 1748 for (Int_t icl1=0; icl1<nCoupled; icl1++) {
1749 indx1 = clustNumb[icl1];
1750 if (indx1 < 0 || indx1 == imax) continue;
c1aed84f 1751 (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1);
1752 (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax);
0df3ca52 1753 }
c1aed84f 1754 (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0;
1755 //aijcluclu->Print();
0df3ca52 1756 //Add cluster-to-pad couplings
1757 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2b1e4f0e 1758 if (fPadIJ[1][j] < 0 && fPadIJ[1][j] != -5) continue; // exclude used pads
c1aed84f 1759 (*aijclupad)(imax,j) += (*aijclupad)(indx,j);
1760 (*aijclupad)(indx,j) = 0;
0df3ca52 1761 }
1762 } // for (Int_t icl=0; icl<nForFit;
1763}
1764
1765//_____________________________________________________________________________
0627f609 1766Int_t AliMUONClusterFinderAZ::Fit(Int_t iSimple, Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk)
0df3ca52 1767{
d19b6003 1768/// Find selected clusters to selected pad charges
0df3ca52 1769
1770 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
0df3ca52 1771 Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1);
1772 Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1);
1773 Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1);
1774 Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1);
2b1e4f0e 1775 Double_t step[3]={0.01,0.002,0.02}, xPad = 0, yPad = 99999;
0df3ca52 1776
2b1e4f0e 1777 // Number of pads to use and number of virtual pads
0627f609 1778 Int_t npads = 0, nVirtual = 0, nfit0 = nfit;
2b1e4f0e 1779 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
2b1e4f0e 1780 if (fXyq[3][i] < 0) nVirtual++;
1781 if (fPadIJ[1][i] != 1) continue;
0627f609 1782 if (fXyq[3][i] > 0) {
1783 npads++;
1784 if (yPad > 9999) {
1785 xPad = fXyq[0][i];
1786 yPad = fXyq[1][i];
1787 } else {
1788 if (fXyq[4][i] < fXyq[3][i]) yPad = fXyq[1][i];
1789 else xPad = fXyq[0][i];
1790 }
1791 }
2b1e4f0e 1792 }
1793 if (fDebug) {
1794 for (Int_t i=0; i<nfit; i++) {cout << i+1 << " " << clustFit[i] << " ";}
1795 cout << nfit << endl;
1796 cout << " Number of pads to fit: " << npads << endl;
1797 }
0df3ca52 1798 fNpar = 0;
1799 fQtot = 0;
1800 if (npads < 2) return 0;
0627f609 1801
1802 Int_t digit = 0;
2b1e4f0e 1803 AliMUONDigit *mdig = 0;
1804 Int_t tracks[3] = {-1, -1, -1};
1805 for (Int_t cath=0; cath<2; cath++) {
1806 for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {
1807 if (fPadIJ[0][i] != cath) continue;
1808 if (fPadIJ[1][i] != 1) continue;
1809 if (fXyq[3][i] < 0) continue; // exclude virtual pads
1810 digit = TMath::Nint (fXyq[5][i]);
1811 if (digit >= 0) mdig = fInput->Digit(cath,digit);
1812 else mdig = fInput->Digit(TMath::Even(cath),-digit-1);
1813 //if (!mdig) mdig = fInput->Digit(TMath::Even(cath),digit);
1814 if (!mdig) continue; // protection for cluster display
1815 if (mdig->Hit() >= 0) {
1816 if (tracks[0] < 0) {
1817 tracks[0] = mdig->Hit();
1818 tracks[1] = mdig->Track(0);
1819 } else if (mdig->Track(0) < tracks[1]) {
1820 tracks[0] = mdig->Hit();
1821 tracks[1] = mdig->Track(0);
1822 }
1823 }
2b1e4f0e 1824 if (mdig->Track(1) >= 0 && mdig->Track(1) != tracks[1]) {
1825 if (tracks[2] < 0) tracks[2] = mdig->Track(1);
1826 else tracks[2] = TMath::Min (tracks[2], mdig->Track(1));
1827 }
1828 //if (!mdig) break;
1829 //cout << mdig->Hit() << " " << mdig->Track(0) << " " << mdig->Track(1) <<endl;
1830 } // for (Int_t i=0;
1831 } // for (Int_t cath=0;
1832 //cout << tracks[0] << " " << tracks[1] << " " << tracks[2] <<endl;
1833
1834 // Get number of pads in X and Y
1835 Int_t nInX = 0, nInY;
1836 PadsInXandY(nInX, nInY);
1af223d7 1837 //cout << " nInX and Y: " << nInX << " " << nInY << endl;
2b1e4f0e 1838
0627f609 1839 Int_t nfitMax = 3;
1840 nfitMax = TMath::Min (nfitMax, (npads + 1) / 3);
1841 if (nfitMax > 1) {
1842 if (nInX < 3 && nInY < 3 || nInX == 3 && nInY < 3 || nInX < 3 && nInY == 3) nfitMax = 1; // not enough pads in each direction
1843 }
1844 if (nfit > nfitMax) nfit = nfitMax;
1845
0df3ca52 1846 // Take cluster maxima as fitting seeds
2b1e4f0e 1847 TObjArray *pix;
0df3ca52 1848 AliMUONPixel *pixPtr;
2b1e4f0e 1849 Int_t npxclu;
1850 Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8], qq = 0;
f29ba3e1 1851 Double_t xyseed[3][2], qseed[3], xyCand[3][2] = {{0},{0}}, sigCand[3][2] = {{0},{0}};
2b1e4f0e 1852
0627f609 1853 for (Int_t ifit=1; ifit<=nfit0; ifit++) {
0df3ca52 1854 cmax = 0;
1855 pix = clusters[clustFit[ifit-1]];
1856 npxclu = pix->GetEntriesFast();
2b1e4f0e 1857 //qq = 0;
0df3ca52 1858 for (Int_t clu=0; clu<npxclu; clu++) {
1859 pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu);
1860 cont = pixPtr->Charge();
1861 fQtot += cont;
1862 if (cont > cmax) {
1863 cmax = cont;
1864 xseed = pixPtr->Coord(0);
1865 yseed = pixPtr->Coord(1);
1866 }
2b1e4f0e 1867 qq += cont;
1868 /*
f29ba3e1 1869 xyCand[ifit-1][0] += pixPtr->Coord(0) * cont;
1870 xyCand[ifit-1][1] += pixPtr->Coord(1) * cont;
1871 sigCand[ifit-1][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
1872 sigCand[ifit-1][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
2b1e4f0e 1873 */
f29ba3e1 1874 xyCand[0][0] += pixPtr->Coord(0) * cont;
1875 xyCand[0][1] += pixPtr->Coord(1) * cont;
1876 sigCand[0][0] += pixPtr->Coord(0) * pixPtr->Coord(0) * cont;
1877 sigCand[0][1] += pixPtr->Coord(1) * pixPtr->Coord(1) * cont;
0df3ca52 1878 }
1879 xyseed[ifit-1][0] = xseed;
1880 xyseed[ifit-1][1] = yseed;
1881 qseed[ifit-1] = cmax;
2b1e4f0e 1882 /*
f29ba3e1 1883 xyCand[ifit-1][0] /= qq; // <x>
1884 xyCand[ifit-1][1] /= qq; // <y>
1885 sigCand[ifit-1][0] = sigCand[ifit-1][0]/qq - xyCand[ifit-1][0]*xyCand[ifit-1][0]; // <x^2> - <x>^2
1886 sigCand[ifit-1][0] = sigCand[ifit-1][0] > 0 ? TMath::Sqrt (sigCand[ifit-1][0]) : 0;
1887 sigCand[ifit-1][1] = sigCand[ifit-1][1]/qq - xyCand[ifit-1][1]*xyCand[ifit-1][1]; // <y^2> - <y>^2
1888 sigCand[ifit-1][1] = sigCand[ifit-1][1] > 0 ? TMath::Sqrt (sigCand[ifit-1][1]) : 0;
1889 cout << xyCand[ifit-1][0] << " " << xyCand[ifit-1][1] << " " << sigCand[ifit-1][0] << " " << sigCand[ifit-1][1] << endl;
2b1e4f0e 1890 */
0df3ca52 1891 } // for (Int_t ifit=1;
1892
f29ba3e1 1893 xyCand[0][0] /= qq; // <x>
1894 xyCand[0][1] /= qq; // <y>
1895 sigCand[0][0] = sigCand[0][0]/qq - xyCand[0][0]*xyCand[0][0]; // <x^2> - <x>^2
1896 sigCand[0][0] = sigCand[0][0] > 0 ? TMath::Sqrt (sigCand[0][0]) : 0;
1897 sigCand[0][1] = sigCand[0][1]/qq - xyCand[0][1]*xyCand[0][1]; // <y^2> - <y>^2
1898 sigCand[0][1] = sigCand[0][1] > 0 ? TMath::Sqrt (sigCand[0][1]) : 0;
1899 if (fDebug) cout << xyCand[0][0] << " " << xyCand[0][1] << " " << sigCand[0][0] << " " << sigCand[0][1] << endl;
2b1e4f0e 1900
0627f609 1901 Int_t nDof, maxSeed[3], nMax = 0;
0df3ca52 1902 Double_t fmin, chi2o = 9999, chi2n;
1903
0627f609 1904 TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
1905 /*
1906 Int_t itmp[100], localMax[100];
1907 Double_t maxVal[100];
1908 if (!iSimple && nfit < nfitMax) {
1909 // Try to split pixel cluster according to local maxima
1910 Int_t nfit1 = nfit;
1911 for (Int_t iclus = 0; iclus < nfit1; iclus++) {
1912 nMax = FindLocalMaxima (clusters[clustFit[maxSeed[iclus]]], localMax, maxVal);
1913 TH2D *hist = (TH2D*) gROOT->FindObject("anode1");
1914 if (nMax == 1) { hist->Delete(); continue; }
1915 // Add extra fitting seeds from local maxima
1916 Int_t ixseed = hist->GetXaxis()->FindBin(xyseed[maxSeed[iclus]][0]);
1917 Int_t iyseed = hist->GetYaxis()->FindBin(xyseed[maxSeed[iclus]][1]);
1918 Int_t nx = hist->GetNbinsX();
1919 TMath::Sort(nMax, maxVal, itmp, kTRUE); // in decreasing order
1920 for (Int_t j = 0; j < nMax; j++) {
1921 Int_t iyc = localMax[itmp[j]] / nx + 1;
1922 Int_t ixc = localMax[itmp[j]] % nx + 1;
1923 if (ixc == ixseed && iyc == iyseed) continue; // local max already taken for seeding
1924 xyseed[nfit][0] = hist->GetXaxis()->GetBinCenter(ixc);
1925 xyseed[nfit][1] = hist->GetYaxis()->GetBinCenter(iyc);
1926 qseed[nfit] = maxVal[itmp[j]];
1927 maxSeed[nfit] = nfit++;
1928 if (nfit >= nfitMax) break;
1929 }
1930 hist->Delete();
1931 if (nfit >= nfitMax) break;
1932 } // for (Int_t iclus = 0;
1933 //nfit0 = nfit;
1934 //TMath::Sort(nfit0, qseed, maxSeed, kTRUE); // in decreasing order
1935 } //if (!iSimple && nfit < nfitMax)
1936 */
0df3ca52 1937
a952ddec 1938 Double_t *gin = 0, func0, func1, param[8], step0[8];
1939 Double_t param0[2][8]={{0},{0}}, deriv[2][8]={{0},{0}};
0df3ca52 1940 Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0;
1941 Double_t delta[8], scMax, dder[8], estim, shiftSave = 0;
1942 Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail;
2b1e4f0e 1943 Double_t rad, dist[3] = {0};
0df3ca52 1944
0627f609 1945 // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is
1946 // lower, try 3-track (if number of pads is sufficient).
0df3ca52 1947 for (Int_t iseed=0; iseed<nfit; iseed++) {
1948
2b1e4f0e 1949 if (iseed) { for (Int_t j=0; j<fNpar; j++) param[j] = parOk[j]; } // for bounded params
0df3ca52 1950 for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j];
0627f609 1951 if (nfit == 1) param[fNpar] = xyCand[0][0]; // take COG
1952 else param[fNpar] = xyseed[maxSeed[iseed]][0];
0df3ca52 1953 parmin[fNpar] = xmin;
1954 parmax[fNpar++] = xmax;
0627f609 1955 if (nfit == 1) param[fNpar] = xyCand[0][1]; // take COG
1956 else param[fNpar] = xyseed[maxSeed[iseed]][1];
0df3ca52 1957 parmin[fNpar] = ymin;
1958 parmax[fNpar++] = ymax;
1959 if (fNpar > 2) {
1960 param[fNpar] = fNpar == 4 ? 0.5 : 0.3;
1961 parmin[fNpar] = 0;
1962 parmax[fNpar++] = 1;
1963 }
5a051e34 1964 if (iseed) { for (Int_t j=0; j<fNpar; j++) param0[1][j] = 0; }
0df3ca52 1965
1966 // Try new algorithm
1967 min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0;
1968
1969 while (1) {
1970 max = !min;
30178c30 1971 Fcn1(fNpar, gin, func0, param, 1); nCall++;
0df3ca52 1972 //cout << " Func: " << func0 << endl;
1973
1974 func2[max] = func0;
1975 for (Int_t j=0; j<fNpar; j++) {
1976 param0[max][j] = param[j];
1977 delta[j] = step0[j];
1978 param[j] += delta[j] / 10;
1979 if (j > 0) param[j-1] -= delta[j-1] / 10;
30178c30 1980 Fcn1(fNpar, gin, func1, param, 1); nCall++;
0df3ca52 1981 deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative
1982 //cout << j << " " << deriv[max][j] << endl;
1983 dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) /
a952ddec 1984 (param0[0][j] - param0[1][j]) : 0; // second derivative
0df3ca52 1985 }
1986 param[fNpar-1] -= delta[fNpar-1] / 10;
2b1e4f0e 1987 if (nCall > 2000) break;
0df3ca52 1988
1989 min = func2[0] < func2[1] ? 0 : 1;
1990 nFail = min == max ? 0 : nFail + 1;
1991
1992 stepMax = derMax = estim = 0;
1993 for (Int_t j=0; j<fNpar; j++) {
1994 // Estimated distance to minimum
1995 shift0 = shift[j];
1996 if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step
1997 else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0;
1998 else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j])
2b1e4f0e 1999 //|| TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) {
2000 || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3 || TMath::Abs(dder[j]) < 1.e-6) {
0df3ca52 2001 shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j]));
2002 if (min == max) {
2003 if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; }
2004 memory[j]++;
2005 }
2006 } else {
2b1e4f0e 2007 shift[j] = dder[j] != 0 ? -deriv[min][j] / dder[j] : 0;
0df3ca52 2008 memory[j] = 0;
2009 }
2010 if (TMath::Abs(shift[j])/step0[j] > estim) {
2011 estim = TMath::Abs(shift[j])/step0[j];
2012 iestMax = j;
2013 }
2014
2015 // Too big step
2016 if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; //
2017
2018 // Failed to improve minimum
2019 if (min != max) {
2020 memory[j] = 0;
2021 param[j] = param0[min][j];
2022 if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2;
2023 else shift[j] /= -2;
2024 }
2025
2026 // Too big step
2027 if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min])
2028 shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]);
2029
2030 // Introduce step relaxation factor
2031 if (memory[j] < 3) {
2032 scMax = 1 + 4 / TMath::Max(nLoop/2.,1.);
2033 if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax)
2034 shift[j] = TMath::Sign (shift0*scMax, shift[j]);
2035 }
2036 param[j] += shift[j];
2b1e4f0e 2037 //AZ Check parameter limits 27-12-2004
2038 if (param[j] < parmin[j]) {
2039 shift[j] = parmin[j] - param[j];
2040 param[j] = parmin[j];
2041 } else if (param[j] > parmax[j]) {
2042 shift[j] = parmax[j] - param[j];
2043 param[j] = parmax[j];
2044 }
0df3ca52 2045 //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl;
2046 stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j]));
2047 if (TMath::Abs(deriv[min][j]) > derMax) {
2048 idMax = j;
2049 derMax = TMath::Abs (deriv[min][j]);
2050 }
2051 } // for (Int_t j=0; j<fNpar;
2052 //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl;
2b1e4f0e 2053 if (estim < 1 && derMax < 2 || nLoop > 150) break; // minimum was found
0df3ca52 2054
2055 nLoop++;
2056 // Check for small step
2057 if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; }
2058 if (!memory[idMax] && derMax > 0.5 && nLoop > 10) {
2059 //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl;
2060 if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) {
2061 if (min == max) dder[idMax] = -dder[idMax];
2062 shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10;
2063 param[idMax] += shift[idMax];
2064 stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]);
2065 //cout << shift[idMax] << " " << param[idMax] << endl;
2066 if (min == max) shiftSave = shift[idMax];
2067 }
2068 if (nFail > 10) {
2069 param[idMax] -= shift[idMax];
2070 shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5);
2071 param[idMax] += shift[idMax];
2072 //cout << shift[idMax] << endl;
2073 }
2074 }
2075 } // while (1)
2076 fmin = func2[min];
2077
2b1e4f0e 2078 nDof = npads - fNpar + nVirtual;
2079 if (!nDof) nDof++;
2080 chi2n = fmin / nDof;
2081 if (fDebug) cout << " Chi2 " << chi2n << " " << fNpar << endl;
0df3ca52 2082
2083 if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; }
2b1e4f0e 2084
0df3ca52 2085 // Save parameters and errors
2b1e4f0e 2086
0627f609 2087 if (nInX == 1) {
2b1e4f0e 2088 // One pad per direction
2b1e4f0e 2089 for (Int_t i=0; i<fNpar; i++) if (i == 0 || i == 2 || i == 5) param0[min][i] = xPad;
2090 }
0627f609 2091 if (nInY == 1) {
2b1e4f0e 2092 // One pad per direction
2b1e4f0e 2093 for (Int_t i=0; i<fNpar; i++) if (i == 1 || i == 3 || i == 6) param0[min][i] = yPad;
2094 }
2095
2096 /*
2097 if (iseed > 0) {
2098 // Find distance to the nearest neighbour
2099 dist[0] = dist[1] = TMath::Sqrt ((param0[min][0]-param0[min][2])*
2100 (param0[min][0]-param0[min][2])
2101 +(param0[min][1]-param0[min][3])*
2102 (param0[min][1]-param0[min][3]));
2103 if (iseed > 1) {
2104 dist[2] = TMath::Sqrt ((param0[min][0]-param0[min][5])*
2105 (param0[min][0]-param0[min][5])
2106 +(param0[min][1]-param0[min][6])*
2107 (param0[min][1]-param0[min][6]));
2108 rad = TMath::Sqrt ((param0[min][2]-param0[min][5])*
2109 (param0[min][2]-param0[min][5])
2110 +(param0[min][3]-param0[min][6])*
2111 (param0[min][3]-param0[min][6]));
2112 if (dist[2] < dist[0]) dist[0] = dist[2];
2113 if (rad < dist[1]) dist[1] = rad;
2114 if (rad < dist[2]) dist[2] = rad;
2115 }
2116 cout << dist[0] << " " << dist[1] << " " << dist[2] << endl;
2117 if (dist[TMath::LocMin(iseed+1,dist)] < 1.) { fNpar -= 3; break; }
2118 }
2119 */
2120
0df3ca52 2121 for (Int_t i=0; i<fNpar; i++) {
2122 parOk[i] = param0[min][i];
0627f609 2123 //errOk[i] = fmin;
2124 errOk[i] = chi2n;
2b1e4f0e 2125 // Bounded params
2126 parOk[i] = TMath::Max (parOk[i], parmin[i]);
2127 parOk[i] = TMath::Min (parOk[i], parmax[i]);
0df3ca52 2128 }
2129
0df3ca52 2130 chi2o = chi2n;
2131 if (fmin < 0.1) break; // !!!???
2132 } // for (Int_t iseed=0;
2133
2b1e4f0e 2134 if (fDebug) {
2135 for (Int_t i=0; i<fNpar; i++) {
2b1e4f0e 2136 if (i == 4 || i == 7) {
2137 if (i == 7 || i == 4 && fNpar < 7) cout << parOk[i] << endl;
2138 else cout << parOk[i] * (1-parOk[7]) << endl;
2139 continue;
2140 }
2141 cout << parOk[i] << " " << errOk[i] << endl;
2142 }
0df3ca52 2143 }
2144 nfit = (fNpar + 1) / 3;
2b1e4f0e 2145 dist[0] = dist[1] = dist[2] = 0;
2146
2147 if (nfit > 1) {
2148 // Find distance to the nearest neighbour
2149 dist[0] = dist[1] = TMath::Sqrt ((parOk[0]-parOk[2])*
2150 (parOk[0]-parOk[2])
2151 +(parOk[1]-parOk[3])*
2152 (parOk[1]-parOk[3]));
2153 if (nfit > 2) {
2154 dist[2] = TMath::Sqrt ((parOk[0]-parOk[5])*
2155 (parOk[0]-parOk[5])
2156 +(parOk[1]-parOk[6])*
2157 (parOk[1]-parOk[6]));
2158 rad = TMath::Sqrt ((parOk[2]-parOk[5])*
2159 (parOk[2]-parOk[5])
2160 +(parOk[3]-parOk[6])*
2161 (parOk[3]-parOk[6]));
2162 if (dist[2] < dist[0]) dist[0] = dist[2];
2163 if (rad < dist[1]) dist[1] = rad;
2164 if (rad < dist[2]) dist[2] = rad;
2165 }
2166 }
2167
1af223d7 2168 Int_t indx;
2b1e4f0e 2169 fnPads[1] -= nVirtual;
cc87ebcd 2170 if (!fDraw) {
2171 Double_t coef = 0;
0627f609 2172 if (iSimple) fnCoupled = 0;
cc87ebcd 2173 //for (Int_t j=0; j<nfit; j++) {
2174 for (Int_t j=nfit-1; j>=0; j--) {
2175 indx = j<2 ? j*2 : j*2+1;
2176 if (nfit == 1) coef = 1;
2177 else coef = j==nfit-1 ? parOk[indx+2] : 1-coef;
2178 coef = TMath::Max (coef, 0.);
2179 if (nfit == 3 && j < 2) coef = j==1 ? coef*parOk[indx+2] : coef - parOk[7];
2180 coef = TMath::Max (coef, 0.);
0627f609 2181 AddRawCluster (parOk[indx], parOk[indx+1], coef*fQtot, errOk[indx], nfit0+10*nfit+100*nMax+10000*fnCoupled, tracks,
cc87ebcd 2182 //sigCand[maxSeed[j]][0], sigCand[maxSeed[j]][1]);
2183 //sigCand[0][0], sigCand[0][1], dist[j]);
2184 sigCand[0][0], sigCand[0][1], dist[TMath::LocMin(nfit,dist)]);
2185 }
2186 } else fDraw->FillMuon(nfit, parOk, errOk);
0df3ca52 2187 return nfit;
2188}
2189
2190//_____________________________________________________________________________
30178c30 2191void AliMUONClusterFinderAZ::Fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/)
0df3ca52 2192{
d19b6003 2193/// Fit for one track
2194/// AZ for Muinuit AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder);
2195
2b1e4f0e 2196 AliMUONClusterFinderAZ& c = *this; //AZ
0df3ca52 2197
2198 Int_t cath, ix, iy, indx, npads=0;
2b1e4f0e 2199 Double_t charge, delta, coef=0, chi2=0, qTot = 0;
0df3ca52 2200 for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) {
2201 if (c.fPadIJ[1][j] != 1) continue;
2202 cath = c.fPadIJ[0][j];
2b1e4f0e 2203 if (c.fXyq[3][j] > 0) npads++; // exclude virtual pads
2204 qTot += c.fXyq[2][j];
0627f609 2205 ix = c.fPadIJ[2][j];
2206 iy = c.fPadIJ[3][j];
2207 c.fSegmentation[cath]->SetPad(ix, iy);
0df3ca52 2208 charge = 0;
2209 for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks
2210 indx = i<2 ? 2*i : 2*i+1;
0627f609 2211 c.fSegmentation[cath]->SetHit(par[indx], par[indx+1], c.fZpad);
0df3ca52 2212 if (c.fNpar == 2) coef = 1;
2213 else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef;
2b1e4f0e 2214 coef = TMath::Max (coef, 0.);
0df3ca52 2215 if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2b1e4f0e 2216 coef = TMath::Max (coef, 0.);
0627f609 2217 charge += c.fInput->Mathieson()->IntXY(fInput->DetElemId(), c.fInput->Segmentation2(cath))*coef;
0df3ca52 2218 }
2219 charge *= c.fQtot;
0df3ca52 2220 delta = charge - c.fXyq[2][j];
2b1e4f0e 2221 delta *= delta;
2222 delta /= c.fXyq[2][j];
2223 //if (cath) delta /= 5; // just for test
2224 chi2 += delta;
0df3ca52 2225 } // for (Int_t j=0;
2226 f = chi2;
2b1e4f0e 2227 Double_t qAver = qTot/npads; //(c.fnPads[0]+c.fnPads[1]);
0df3ca52 2228 f = chi2/qAver;
2229}
2230
2231//_____________________________________________________________________________
6aaf81e6 2232void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par)
0df3ca52 2233{
d19b6003 2234/// Subtract the fitted charges from pads with strong coupling
0df3ca52 2235
2236 Int_t cath, ix, iy, indx;
2237 Double_t charge, coef=0;
2238 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2239 if (fPadIJ[1][j] != -1) continue;
2240 if (fNpar != 0) {
2241 cath = fPadIJ[0][j];
0627f609 2242 ix = fPadIJ[2][j];
2243 iy = fPadIJ[3][j];
2244 fSegmentation[cath]->SetPad(ix, iy);
0df3ca52 2245 charge = 0;
2246 for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks
2247 indx = i<2 ? 2*i : 2*i+1;
0627f609 2248 fSegmentation[cath]->SetHit(par[indx], par[indx+1], fZpad);
0df3ca52 2249 if (fNpar == 2) coef = 1;
2250 else coef = i==fNpar/3 ? par[indx+2] : 1-coef;
2b1e4f0e 2251 coef = TMath::Max (coef, 0.);
0df3ca52 2252 if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7];
2b1e4f0e 2253 coef = TMath::Max (coef, 0.);
0627f609 2254 charge += fInput->Mathieson()->IntXY(fInput->DetElemId(),fInput->Segmentation2(cath))*coef;
0df3ca52 2255 }
2256 charge *= fQtot;
2257 fXyq[2][j] -= charge;
2258 } // if (fNpar != 0)
0627f609 2259 if (fXyq[2][j] > fgkZeroSuppression) fPadIJ[1][j] = 0; // return pad for further using
0df3ca52 2260 } // for (Int_t j=0;
2261}
2262
2263//_____________________________________________________________________________
d19b6003 2264Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) const
2265{
2266/// Test if track was user selected
2267
0df3ca52 2268 return kTRUE;
2269 /*
2270 if (fTrack[0]==-1 || fTrack[1]==-1) {
2271 return kTRUE;
2272 } else if (t==fTrack[0] || t==fTrack[1]) {
2273 return kTRUE;
2274 } else {
2275 return kFALSE;
2276 }
2277 */
2278}
2279
2280//_____________________________________________________________________________
2b1e4f0e 2281void 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*/)
0df3ca52 2282{
d19b6003 2283/// Add a raw cluster copy to the list
2284
2b1e4f0e 2285 if (qTot <= 0.501) return;
0df3ca52 2286 AliMUONRawCluster cnew;
0df3ca52 2287
2b1e4f0e 2288 Int_t cath, npads[2] = {0}, nover[2] = {0};
2289 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2290 cath = fPadIJ[0][j];
2291 // There was an overflow
2292 if (fPadIJ[1][j] == -9) nover[cath]++;
2293 if (fPadIJ[1][j] != 1 && fPadIJ[1][j] != -9) continue;
2294 cnew.SetMultiplicity(cath,cnew.GetMultiplicity(cath)+1);
2295 if (fXyq[2][j] > cnew.GetPeakSignal(cath)) cnew.SetPeakSignal(cath,TMath::Nint (fXyq[2][j]));
2296 //cnew.SetCharge(cath,cnew.GetCharge(cath) + TMath::Nint (fXyq[2][j]));
2297 cnew.SetContrib(npads[cath],cath,fXyq[2][j]);
5a051e34 2298 cnew.SetIndex(npads[cath],cath,TMath::Nint (fXyq[5][j]));
2299 cnew.SetDetElemId(fInput->DetElemId());
2b1e4f0e 2300 npads[cath]++;
2301 }
2302
2303 cnew.SetClusterType(nover[0] + nover[1] * 100);
2304 for (Int_t j=0; j<3; j++) cnew.SetTrack(j,tracks[j]);
2305
0627f609 2306 Double_t xg, yg, zg;
0df3ca52 2307 for (cath=0; cath<2; cath++) {
0627f609 2308 // Perform local-to-global transformation
2309 fInput->Segmentation2(cath)->GetTransformer()->Local2Global(fInput->DetElemId(), x, y, fZpad, xg, yg, zg);
2310 cnew.SetX(cath, xg);
2311 cnew.SetY(cath, yg);
2312 cnew.SetZ(cath, zg);
2b1e4f0e 2313 cnew.SetCharge(cath, TMath::Nint(qTot));
2314 //cnew.SetPeakSignal(cath,20);
2315 //cnew.SetMultiplicity(cath, 5);
2316 cnew.SetNcluster(cath, nfit);
2317 cnew.SetChi2(cath, fmin); //0.;1
0df3ca52 2318 }
2b1e4f0e 2319 // Evaluate measurement errors
2320 //AZ Errors(&cnew);
2321
2322 cnew.SetGhost(nfit); //cnew.SetX(1,sigx); cnew.SetY(1,sigy); cnew.SetZ(1,dist);
0df3ca52 2323 //cnew.fClusterType=cnew.PhysicsContribution();
0627f609 2324 new((*fRawClusters)[fNRawClusters++]) AliMUONRawCluster(cnew);
2325 if (fDebug) cout << fNRawClusters << " " << fInput->Chamber() << endl;
0df3ca52 2326 //fNPeaks++;
2327}
2328
2329//_____________________________________________________________________________
0627f609 2330Int_t AliMUONClusterFinderAZ::FindLocalMaxima(TObjArray *pixArray, Int_t *localMax, Double_t *maxVal)
0df3ca52 2331{
d19b6003 2332/// Find local maxima in pixel space for large preclusters in order to
2333/// try to split them into smaller pieces (to speed up the MLEM procedure)
2334/// or to find additional fitting seeds if clusters were not completely resolved
0df3ca52 2335
0627f609 2336 TH2D *hist = NULL;
2337 //if (pixArray == fPixArray) hist = (TH2D*) gROOT->FindObject("anode");
2338 //else { hist = (TH2D*) gROOT->FindObject("anode1"); cout << hist << endl; }
2339 //if (hist) hist->Delete();
0df3ca52 2340
cd747ddb 2341 Double_t xylim[4] = {999, 999, 999, 999};
0627f609 2342 Int_t nPix = pixArray->GetEntriesFast();
0df3ca52 2343 AliMUONPixel *pixPtr = 0;
2344 for (Int_t ipix=0; ipix<nPix; ipix++) {
0627f609 2345 pixPtr = (AliMUONPixel*) pixArray->UncheckedAt(ipix);
0df3ca52 2346 for (Int_t i=0; i<4; i++)
2347 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
2348 }
2349 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
2350
2351 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
2352 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
0627f609 2353 if (pixArray == fPixArray) hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2354 else hist = new TH2D("anode1","anode1",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
0df3ca52 2355 for (Int_t ipix=0; ipix<nPix; ipix++) {
0627f609 2356 pixPtr = (AliMUONPixel*) pixArray->UncheckedAt(ipix);
0df3ca52 2357 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
2358 }
0627f609 2359 if (fDraw && pixArray == fPixArray) fDraw->DrawHist("c2", hist);
0df3ca52 2360
2361 Int_t nMax = 0, indx;
2362 Int_t *isLocalMax = new Int_t[ny*nx];
2363 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
2364
2365 for (Int_t i=1; i<=ny; i++) {
2366 indx = (i-1) * nx;
2367 for (Int_t j=1; j<=nx; j++) {
2368 if (hist->GetCellContent(j,i) < 0.5) continue;
2369 //if (isLocalMax[indx+j-1] < 0) continue;
2370 if (isLocalMax[indx+j-1] != 0) continue;
2371 FlagLocalMax(hist, i, j, isLocalMax);
2372 }
2373 }
2374
2375 for (Int_t i=1; i<=ny; i++) {
2376 indx = (i-1) * nx;
2377 for (Int_t j=1; j<=nx; j++) {
2378 if (isLocalMax[indx+j-1] > 0) {
2379 localMax[nMax] = indx + j - 1;
2380 maxVal[nMax++] = hist->GetCellContent(j,i);
2b1e4f0e 2381 if (nMax > 99) AliFatal(" Too many local maxima !!!");
0df3ca52 2382 }
0df3ca52 2383 }
2384 }
2b1e4f0e 2385 if (fDebug) cout << " Local max: " << nMax << endl;
0df3ca52 2386 delete [] isLocalMax; isLocalMax = 0;
2387 return nMax;
2388}
2389
2390//_____________________________________________________________________________
2391void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
2392{
d19b6003 2393/// Flag pixels (whether or not local maxima)
0df3ca52 2394
2395 Int_t nx = hist->GetNbinsX();
2396 Int_t ny = hist->GetNbinsY();
2397 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
0627f609 2398 Int_t cont1 = 0, indx = (i-1)*nx+j-1, indx1 = 0, indx2 = 0;
0df3ca52 2399
2400 for (Int_t i1=i-1; i1<i+2; i1++) {
2401 if (i1 < 1 || i1 > ny) continue;
0627f609 2402 indx1 = (i1 - 1) * nx;
0df3ca52 2403 for (Int_t j1=j-1; j1<j+2; j1++) {
2404 if (j1 < 1 || j1 > nx) continue;
2405 if (i == i1 && j == j1) continue;
0627f609 2406 indx2 = indx1 + j1 - 1;
0df3ca52 2407 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
0627f609 2408 if (cont < cont1) { isLocalMax[indx] = -1; return; }
2409 else if (cont > cont1) isLocalMax[indx2] = -1;
0df3ca52 2410 else { // the same charge
0627f609 2411 isLocalMax[indx] = 1;
2412 if (isLocalMax[indx2] == 0) {
0df3ca52 2413 FlagLocalMax(hist, i1, j1, isLocalMax);
0627f609 2414 if (isLocalMax[indx2] < 0) { isLocalMax[indx] = -1; return; }
2415 else isLocalMax[indx2] = -1;
0df3ca52 2416 }
2417 }
2418 }
2419 }
0627f609 2420 isLocalMax[indx] = 1; // local maximum
0df3ca52 2421}
2422
2423//_____________________________________________________________________________
2424void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax)
2425{
13985652 2426/// Find pixel cluster around local maximum \a iMax and pick up pads
d19b6003 2427/// overlapping with it
0df3ca52 2428
2429 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
2430 Int_t nx = hist->GetNbinsX();
2431 Int_t ny = hist->GetNbinsY();
2432 Int_t ic = localMax[iMax] / nx + 1;
2433 Int_t jc = localMax[iMax] % nx + 1;
2434 Bool_t *used = new Bool_t[ny*nx];
2435 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
2436
2437 // Drop all pixels from the array - pick up only the ones from the cluster
2438 fPixArray->Delete();
2439
2440 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
2441 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
2442 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
2443 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
2444 Double_t cont = hist->GetCellContent(jc,ic);
2445 AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont);
2446 fPixArray->Add((TObject*)pixPtr);
2447 used[(ic-1)*nx+jc-1] = kTRUE;
2448 AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
2449
2450 Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1];
2451 for (Int_t i=0; i<nPix; i++) {
2452 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx);
2453 ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy);
2454 }
2b1e4f0e 2455 if (fDebug) cout << iMax << " " << nPix << endl;
0df3ca52 2456
2457 Float_t xy[4], xy12[4];
2458 // Pick up pads which overlap with found pixels
2459 for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1;
2460 for (Int_t i=0; i<nPix; i++) {
2461 pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i);
2462 for (Int_t j=0; j<4; j++)
2463 xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2);
2464 for (Int_t j=0; j<npad; j++)
2465 if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use
2466 }
2467
2468 delete [] used; used = 0;
2469}
30178c30 2470
2b1e4f0e 2471//_____________________________________________________________________________
2472void AliMUONClusterFinderAZ::AddVirtualPad()
2473{
d19b6003 2474/// Add virtual pad (with small charge) to improve fit for some
2475/// clusters (when pad with max charge is at the extreme of the cluster)
2b1e4f0e 2476
2477 // Get number of pads in X and Y-directions
2478 Int_t nInX = -1, nInY;
2479 PadsInXandY(nInX, nInY);
2480 //return;
2481
0627f609 2482 // Add virtual pad only if number of pads per direction == 2
2b1e4f0e 2483 if (nInX != 2 && nInY != 2) return;
2484
2485 // Find pads with max charge
2486 Int_t maxpad[2][2] = {{-1, -1}, {-1, -1}}, cath;
2487 Double_t sigmax[2] = {0}, aamax[2] = {0};
2488 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2489 if (fPadIJ[1][j] != 0) continue;
2490 cath = fPadIJ[0][j];
2491 if (fXyq[2][j] > sigmax[cath]) {
2492 maxpad[cath][1] = maxpad[cath][0];
2493 aamax[cath] = sigmax[cath];
2494 sigmax[cath] = fXyq[2][j];
2495 maxpad[cath][0] = j;
2496 }
2497 }
2498 if (maxpad[0][0] >= 0 && maxpad[0][1] < 0 || maxpad[1][0] >= 0 && maxpad[1][1] < 0) {
2499 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2500 if (fPadIJ[1][j] != 0) continue;
2501 cath = fPadIJ[0][j];
2502 if (j == maxpad[cath][0] || j == maxpad[cath][1]) continue;
2503 if (fXyq[2][j] > aamax[cath]) {
2504 aamax[cath] = fXyq[2][j];
2505 maxpad[cath][1] = j;
2506 }
2507 }
2508 }
2509 // Check for mirrors (side X on cathode 0)
2510 Bool_t mirror = kFALSE;
0627f609 2511 if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0) {
2b1e4f0e 2512 mirror = fXyq[3][maxpad[0][0]] < fXyq[4][maxpad[0][0]];
0627f609 2513 if (!mirror && TMath::Abs(fXyq[3][maxpad[0][0]]-fXyq[3][maxpad[1][0]]) < 0.001) {
2514 // Special case when pads on both cathodes have the same size
2515 Int_t yud[2] = {0};
2516 for (Int_t j = 0; j < fnPads[0]+fnPads[1]; j++) {
2517 cath = fPadIJ[0][j];
2518 if (j == maxpad[cath][0]) continue;
2519 if (fPadIJ[2][j] != fPadIJ[2][maxpad[cath][0]]) continue;
2520 if (fPadIJ[3][j] + 1 == fPadIJ[3][maxpad[cath][0]] ||
2521 fPadIJ[3][j] - 1 == fPadIJ[3][maxpad[cath][0]]) yud[cath]++;
2522 }
2523 if (!yud[0]) mirror = kTRUE; // take the other cathode
2524 } // if (!mirror &&...
2525 } // if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
2b1e4f0e 2526
2527 // Find neughbours of pads with max charges
2528 Int_t nn, xList[10], yList[10], ix0, iy0, ix, iy, neighb;
2529 for (cath=0; cath<2; cath++) {
2530 if (!cath && maxpad[0][0] < 0) continue; // one-sided cluster - cathode 1
2531 if (cath && maxpad[1][0] < 0) break; // one-sided cluster - cathode 0
2532 if (maxpad[1][0] >= 0) {
2533 if (!mirror) {
2534 if (!cath && nInY != 2) continue;
2b1e4f0e 2535 if (cath && nInX != 2 && (maxpad[0][0] >= 0 || nInY != 2)) continue;
2536 } else {
2537 if (!cath && nInX != 2) continue;
2538 if (cath && nInY != 2 && (maxpad[0][0] >= 0 || nInX != 2)) continue;
2539 }
2540 }
2541
1af223d7 2542 Int_t iAddX = 0, iAddY = 0, ix1 = 0, iy1 = 0, iPad = 0;
2b1e4f0e 2543 if (maxpad[0][0] < 0) iPad = 1;
2544
2b1e4f0e 2545 for (iPad=0; iPad<2; iPad++) {
94eb555e 2546 if (maxpad[cath][iPad] < 0) continue;
2b1e4f0e 2547 if (iPad && !iAddX && !iAddY) break;
2548 if (iPad && fXyq[2][maxpad[cath][1]] / sigmax[cath] < 0.5) break;
2549
2550 Int_t neighbx = 0, neighby = 0;
0627f609 2551 ix0 = fPadIJ[2][maxpad[cath][iPad]];
2552 iy0 = fPadIJ[3][maxpad[cath][iPad]];
2553 fSegmentation[cath]->Neighbours(ix0, iy0, &nn, xList, yList);
2554 Float_t zpad;
2b1e4f0e 2555 for (Int_t j=0; j<nn; j++) {
0627f609 2556 if (TMath::Abs(xList[j]-ix0) == 1 || xList[j]*ix0 == -1) neighbx++;
2557 if (TMath::Abs(yList[j]-iy0) == 1 || yList[j]*iy0 == -1) neighby++;
2b1e4f0e 2558 }
2559 if (!mirror) {
2560 if (cath) neighb = neighbx;
2561 else neighb = neighby;
2562 if (maxpad[0][0] < 0) neighb += neighby;
2563 else if (maxpad[1][0] < 0) neighb += neighbx;
2564 } else {
2565 if (!cath) neighb = neighbx;
2566 else neighb = neighby;
2567 if (maxpad[0][0] < 0) neighb += neighbx;
2568 else if (maxpad[1][0] < 0) neighb += neighby;
2569 }
2570
2571 for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) {
2572 if (fPadIJ[0][j] != cath) continue;
0627f609 2573 ix = fPadIJ[2][j];
2574 iy = fPadIJ[3][j];
2b1e4f0e 2575 if (iy == iy0 && ix == ix0) continue;
2576 for (Int_t k=0; k<nn; k++) {
2577 if (xList[k] != ix || yList[k] != iy) continue;
2578 if (!mirror) {
2579 if ((!cath || maxpad[0][0] < 0) &&
1af223d7 2580 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
0627f609 2581 if (!iPad && TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) ix1 = xList[k]; //19-12-05
2b1e4f0e 2582 xList[k] = yList[k] = 0;
2583 neighb--;
2584 break;
2585 }
2586 if ((cath || maxpad[1][0] < 0) &&
1af223d7 2587 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
0627f609 2588 if (!iPad) ix1 = xList[k]; //19-12-05
2b1e4f0e 2589 xList[k] = yList[k] = 0;
2590 neighb--;
2591 }
2592 } else {
2593 if ((!cath || maxpad[0][0] < 0) &&
1af223d7 2594 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
0627f609 2595 if (!iPad) ix1 = xList[k]; //19-12-05
2b1e4f0e 2596 xList[k] = yList[k] = 0;
2597 neighb--;
2598 break;
2599 }
2600 if ((cath || maxpad[1][0] < 0) &&
1af223d7 2601 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
2b1e4f0e 2602 xList[k] = yList[k] = 0;
2603 neighb--;
2604 }
2605 }
2606 break;
2607 } // for (Int_t k=0; k<nn;
2608 if (!neighb) break;
2609 } // for (Int_t j=0; j<fnPads[0]+fnPads[1];
2610 if (!neighb) continue;
2611
2612 // Add virtual pad
2613 Int_t npads, isec;
2614 isec = 0;
2615 for (Int_t j=0; j<nn; j++) {
2616 if (xList[j] == 0 && yList[j] == 0) continue;
2617 npads = fnPads[0] + fnPads[1];
2618 fPadIJ[0][npads] = cath;
2619 fPadIJ[1][npads] = 0;
2620 ix = xList[j];
2621 iy = yList[j];
1af223d7 2622 if (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) {
2b1e4f0e 2623 if (iy != iy0) continue; // new segmentation - check
2624 if (nInX != 2) continue; // new
2625 if (!mirror) {
2626 if (!cath && maxpad[1][0] >= 0) continue;
2b1e4f0e 2627 } else {
2628 if (cath && maxpad[0][0] >= 0) continue;
2b1e4f0e 2629 }
2630 if (iPad && !iAddX) continue;
0627f609 2631 fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
1af223d7 2632 if (fXyq[0][npads] > 1.e+5) continue; // temporary fix
0627f609 2633 if (ix == ix1) continue; //19-12-05
2b1e4f0e 2634 if (ix1 == ix0) continue;
2b1e4f0e 2635 if (maxpad[1][0] < 0 || mirror && maxpad[0][0] >= 0) {
2636 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/100, 5.);
2637 else fXyq[2][npads] = TMath::Min (aamax[0]/100, 5.);
2638 }
2639 else {
2640 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/100, 5.);
2641 else fXyq[2][npads] = TMath::Min (aamax[1]/100, 5.);
2642 }
2643 fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
2b1e4f0e 2644 fXyq[3][npads] = -2; // flag
0627f609 2645 fPadIJ[2][npads] = ix;
2646 fPadIJ[3][npads] = iy;
2b1e4f0e 2647 fnPads[1]++;
2648 iAddX = npads;
0627f609 2649 if (fDebug) printf(" ***** Add virtual pad in X ***** %f %f %f %3d %3d \n", fXyq[2][npads],
e8fb921b 2650 fXyq[0][npads], fXyq[1][npads], ix, iy);
2b1e4f0e 2651 ix1 = ix0;
2652 continue;
2653 }
2654 if (nInY != 2) continue;
2655 if (!mirror && cath && maxpad[0][0] >= 0) continue;
2656 if (mirror && !cath && maxpad[1][0] >= 0) continue;
2657 if (TMath::Abs(iy-iy0) == 1 || TMath::Abs(iy*iy0) == 1) {
2658 if (ix != ix0) continue; // new segmentation - check
2659 if (iPad && !iAddY) continue;
0627f609 2660 fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
2b1e4f0e 2661 if (iy1 == iy0) continue;
2662 //if (iPad && iy1 == iy0) continue;
2b1e4f0e 2663 if (maxpad[0][0] < 0 || mirror && maxpad[1][0] >= 0) {
0627f609 2664 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/15, fgkZeroSuppression);
2665 else fXyq[2][npads] = TMath::Min (aamax[1]/15, fgkZeroSuppression);
2b1e4f0e 2666 }
2667 else {
0627f609 2668 if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/15, fgkZeroSuppression);
2669 else fXyq[2][npads] = TMath::Min (aamax[0]/15, fgkZeroSuppression);
2b1e4f0e 2670 }
2671 fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
2b1e4f0e 2672 fXyq[3][npads] = -2; // flag
0627f609 2673 fPadIJ[2][npads] = ix;
2674 fPadIJ[3][npads] = iy;
2b1e4f0e 2675 fnPads[1]++;
2676 iAddY = npads;
0627f609 2677 if (fDebug) printf(" ***** Add virtual pad in Y ***** %f %f %f %3d %3d \n", fXyq[2][npads],
e8fb921b 2678 fXyq[0][npads], fXyq[1][npads], ix, iy);
2b1e4f0e 2679 iy1 = iy0;
2680 }
2681 } // for (Int_t j=0; j<nn;
2682 } // for (Int_t iPad=0;
2683 } // for (cath=0; cath<2;
2684 return;
2685}
2686
2687//_____________________________________________________________________________
2688void AliMUONClusterFinderAZ::PadsInXandY(Int_t &nInX, Int_t &nInY)
2689{
d19b6003 2690/// Find number of pads in X and Y-directions (excluding virtual ones and
2691/// overflows)
2b1e4f0e 2692
2693 static Int_t nXsaved = 0, nYsaved = 0;
2694 nXsaved = nYsaved = 0;
2695 //if (nInX >= 0) {nInX = nXsaved; nInY = nYsaved; return; }
5a051e34 2696 Float_t *xPad0 = NULL, *yPad0 = NULL, *xPad1 = NULL, *yPad1 = NULL;
1af223d7 2697 Float_t wMinX[2] = {99, 99}, wMinY[2] = {99, 99};
5a051e34 2698 Int_t *nPad0 = NULL, *nPad1 = NULL;
2699 Int_t nPads = fnPads[0] + fnPads[1];
2700 if (fnPads[0]) {
2701 xPad0 = new Float_t[nPads];
2702 yPad0 = new Float_t[nPads];
2703 nPad0 = new Int_t[nPads];
2704 }
2705 if (fnPads[1]) {
2706 xPad1 = new Float_t[nPads];
2707 yPad1 = new Float_t[nPads];
2708 nPad1 = new Int_t[nPads];
2709 }
2710 Int_t n0 = 0, n1 = 0, cath, npadx[2] = {1, 1}, npady[2] = {1, 1};
2711 for (Int_t j = 0; j < nPads; j++) {
2b1e4f0e 2712 if (nInX < 0 && fPadIJ[1][j] != 0) continue; // before fit
2713 else if (nInX == 0 && fPadIJ[1][j] != 1) continue; // fit - exclude overflows
2714 else if (nInX > 0 && fPadIJ[1][j] != 1 && fPadIJ[1][j] != -9) continue; // exclude non-marked
0627f609 2715 if (nInX <= 0 && fXyq[2][j] > fgkSaturation-1) continue; // skip overflows
2b1e4f0e 2716 cath = fPadIJ[0][j];
1af223d7 2717 if (fXyq[3][j] > 0) { // exclude virtual pads
2718 wMinX[cath] = TMath::Min (wMinX[cath], fXyq[3][j]);
2719 wMinY[cath] = TMath::Min (wMinY[cath], fXyq[4][j]);
0627f609 2720 //20-12-05 }
2721 if (cath) { xPad1[n1] = fXyq[0][j]; yPad1[n1++] = fXyq[1][j]; }
2722 else { xPad0[n0] = fXyq[0][j]; yPad0[n0++] = fXyq[1][j]; }
1af223d7 2723 }
2b1e4f0e 2724 }
5a051e34 2725
2726 // Sort
2727 if (n0) {
2728 TMath::Sort (n0, xPad0, nPad0); // in X
2729 for (Int_t i = 1; i < n0; i++)
2730 if (xPad0[nPad0[i]] - xPad0[nPad0[i-1]] < -0.01) npadx[0]++;
2731 TMath::Sort (n0, yPad0, nPad0); // in Y
2732 for (Int_t i = 1; i < n0; i++)
2733 if (yPad0[nPad0[i]] - yPad0[nPad0[i-1]] < -0.01) npady[0]++;
2734 }
2735
2736 if (n1) {
2737 TMath::Sort (n1, xPad1, nPad1); // in X
2738 for (Int_t i = 1; i < n1; i++)
2739 if (xPad1[nPad1[i]] - xPad1[nPad1[i-1]] < -0.01) npadx[1]++;
2740 TMath::Sort (n1, yPad1, nPad1); // in Y
2741 for (Int_t i = 1; i < n1; i++)
2742 if (yPad1[nPad1[i]] - yPad1[nPad1[i-1]] < -0.01) npady[1]++;
2743 }
2744 if (fnPads[0]) { delete [] xPad0; delete [] yPad0; delete [] nPad0; }
2745 if (fnPads[1]) { delete [] xPad1; delete [] yPad1; delete [] nPad1; }
cc87ebcd 2746 if (TMath::Abs (wMinY[0] - wMinY[1]) < 1.e-3) nInY = TMath::Max (npady[0], npady[1]);
2747 else nInY = wMinY[0] < wMinY[1] ? npady[0] : npady[1];
2748 if (TMath::Abs (wMinX[0] - wMinX[1]) < 1.e-3) nInX = TMath::Max (npadx[0], npadx[1]);
2749 else nInX = wMinX[0] < wMinX[1] ? npadx[0] : npadx[1];
2b1e4f0e 2750}
2751
2752//_____________________________________________________________________________
2753void AliMUONClusterFinderAZ::Simple()
2754{
d19b6003 2755/// Process simple cluster (small number of pads) without EM-procedure
2b1e4f0e 2756
ff7af159 2757 Int_t nForFit = 1, clustFit[1] = {0}, nfit;
2b1e4f0e 2758 Double_t parOk[3] = {0.};
2759 TObjArray *clusters[1];
ff7af159 2760 clusters[0] = fPixArray;
5a051e34 2761 for (Int_t i = 0; i < fnPads[0]+fnPads[1]; i++) {
0627f609 2762 if (fXyq[2][i] > fgkSaturation-1) fPadIJ[1][i] = -9;
5a051e34 2763 else fPadIJ[1][i] = 1;
2764 }
0627f609 2765 nfit = Fit(1, nForFit, clustFit, clusters, parOk);
2b1e4f0e 2766}
2767
2768//_____________________________________________________________________________
2769void AliMUONClusterFinderAZ::Errors(AliMUONRawCluster *clus)
2770{
d19b6003 2771/// Correct reconstructed coordinates for some clusters and evaluate errors
2b1e4f0e 2772
2773 Double_t qTot = clus->GetCharge(0), fmin = clus->GetChi2(0);
2774 Double_t xreco = clus->GetX(0), yreco = clus->GetY(0), zreco = clus->GetZ(0);
2775 Double_t sigmax[2] = {0};
2776
2777 Int_t nInX = 1, nInY, maxdig[2] ={-1, -1}, digit, cath1, isec;
2778 PadsInXandY(nInX, nInY);
2779
2780 // Find pad with maximum signal
2781 for (Int_t cath = 0; cath < 2; cath++) {
2782 for (Int_t j = 0; j < clus->GetMultiplicity(cath); j++) {
2783 cath1 = cath;
2784 digit = clus->GetIndex(j, cath);
2785 if (digit < 0) { cath1 = TMath::Even(cath); digit = -digit - 1; } // from the other cathode
2786
2787 if (clus->GetContrib(j,cath) > sigmax[cath1]) {
2788 sigmax[cath1] = clus->GetContrib(j,cath);
2789 maxdig[cath1] = digit;
2790 }
2791 }
2792 }
2793
2794 // Size of pad with maximum signal and reco coordinate distance from the pad center
2795 AliMUONDigit *mdig = 0;
2796 Double_t wx[2], wy[2], dxc[2], dyc[2];
2797 Float_t xpad, ypad, zpad;
2798 Int_t ix, iy;
2799 for (Int_t cath = 0; cath < 2; cath++) {
2800 if (maxdig[cath] < 0) continue;
2801 mdig = fInput->Digit(cath,maxdig[cath]);
0627f609 2802 isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
2803 wx[cath] = fSegmentation[cath]->Dpx(isec);
2804 wy[cath] = fSegmentation[cath]->Dpy(isec);
2805 fSegmentation[cath]->GetPadI(xreco, yreco, zreco, ix, iy);
2806 isec = fSegmentation[cath]->Sector(ix, iy);
2b1e4f0e 2807 if (isec > 0) {
0627f609 2808 fSegmentation[cath]->GetPadC(ix, iy, xpad, ypad, zpad);
2b1e4f0e 2809 dxc[cath] = xreco - xpad;
2810 dyc[cath] = yreco - ypad;
2811 }
2812 }
2813
2814 // Check if pad with max charge at the edge (number of neughbours)
2815 Int_t nn, xList[10], yList[10], neighbx[2][2] = {{0,0}, {0,0}}, neighby[2][2]= {{0,0}, {0,0}};
2816 for (Int_t cath = 0; cath < 2; cath++) {
2817 if (maxdig[cath] < 0) continue;
2818 mdig = fInput->Digit(cath,maxdig[cath]);
0627f609 2819 fSegmentation[cath]->Neighbours(mdig->PadX(), mdig->PadY(), &nn, xList, yList);
2820 isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY());
2821 /*??
f29ba3e1 2822 Float_t sprX = fResponse->SigmaIntegration() * fResponse->ChargeSpreadX();
2823 Float_t sprY = fResponse->SigmaIntegration() * fResponse->ChargeSpreadY();
2824 //fSegmentation[cath]->FirstPad(fInput->DetElemId(),muons[ihit][1], muons[ihit][2], muons[ihit][3], sprX, sprY);
0627f609 2825 //fSegmentation[cath]->FirstPad(fInput->DetElemId(),xreco, yreco, zreco, sprX, sprY);
2826 fSegmentation[cath]->FirstPad(xreco, yreco, zreco, sprX, sprY);
2b1e4f0e 2827 Int_t border = 0;
0627f609 2828 //if (fSegmentation[cath]->Sector(fInput->DetElemId(),fSegmentation[cath]->Ix(),fSegmentation[cath]->Iy()) <= 0) {
2829 if (fSegmentation[cath]->Sector(fSegmentation[cath]->Ix(), fSegmentation[cath]->Iy()) <= 0) {
2830 //fSegmentation[cath]->NextPad(fInput->DetElemId());
2831 fSegmentation[cath]->NextPad();
2b1e4f0e 2832 border = 1;
2833 }
0627f609 2834 */
2b1e4f0e 2835 for (Int_t j=0; j<nn; j++) {
0627f609 2836 //if (border && yList[j] < fSegmentation[cath]->Iy()) continue;
2837 fSegmentation[cath]->GetPadC(xList[j], yList[j], xpad, ypad, zpad);
2b1e4f0e 2838 //cout << ch << " " << xList[j] << " " << yList[j] << " " << border << " " << x << " " << y << " " << xpad << " " << ypad << endl;
2839 if (TMath::Abs(xpad) < 1 && TMath::Abs(ypad) < 1) continue;
2840 if (xList[j] == mdig->PadX()-1 || mdig->PadX() == 1 &&
2841 xList[j] == -1) neighbx[cath][0] = 1;
2842 else if (xList[j] == mdig->PadX()+1 || mdig->PadX() == -1 &&
2843 xList[j] == 1) neighbx[cath][1] = 1;
2844 if (yList[j] == mdig->PadY()-1 || mdig->PadY() == 1 &&
2845 yList[j] == -1) neighby[cath][0] = 1;
2846 else if (yList[j] == mdig->PadY()+1 || mdig->PadY() == -1 &&
2847 yList[j] == 1) neighby[cath][1] = 1;
2848 } // for (Int_t j=0; j<nn;
2849 if (neighbx[cath][0] && neighbx[cath][1]) neighbx[cath][0] = 0;
2850 else if (neighbx[cath][1]) neighbx[cath][0] = -1;
2851 else neighbx[cath][0] = 1;
2852 if (neighby[cath][0] && neighby[cath][1]) neighby[cath][0] = 0;
2853 else if (neighby[cath][1]) neighby[cath][0] = -1;
2854 else neighby[cath][0] = 1;
2855 }
2856
2857 Int_t iOver = clus->GetClusterType();
2858 // One-sided cluster
2859 if (!clus->GetMultiplicity(0)) {
2860 neighby[0][0] = neighby[1][0];
2861 wy[0] = wy[1];
2862 if (iOver < 99) iOver += 100 * iOver;
2863 dyc[0] = dyc[1];
2864 } else if (!clus->GetMultiplicity(1)) {
2865 neighbx[1][0] = neighbx[0][0];
2866 wx[1] = wx[0];
2867 if (iOver < 99) iOver += 100 * iOver;
2868 dxc[1] = dxc[0];
2869 }
2870
2871 // Apply corrections and evaluate errors
2872 Double_t errY, errX;
2873 Errors(nInY, nInX, neighby[0][0],neighbx[1][0], fmin, wy[0]*10, wx[1]*10, iOver,
2874 dyc[0], dxc[1], qTot, yreco, xreco, errY, errX);
2875 errY = TMath::Max (errY, 0.01);
2876 //errY = 0.01;
2877 //errX = TMath::Max (errX, 0.144);
2878 clus->SetX(0, xreco); clus->SetY(0, yreco);
1af223d7 2879 clus->SetErrX(errX); clus->SetErrY(errY);
2b1e4f0e 2880}
2881
2882//_____________________________________________________________________________
2883void AliMUONClusterFinderAZ::Errors(Int_t ny, Int_t nx, Int_t iby, Int_t ibx, Double_t fmin,
2884 Double_t wy, Double_t wx, Int_t iover,
2885 Double_t dyc, Double_t /*dxc*/, Double_t qtot,
2886 Double_t &yrec, Double_t &xrec, Double_t &erry, Double_t &errx)
2887{
d19b6003 2888/// Correct reconstructed coordinates for some clusters and evaluate errors
2b1e4f0e 2889
2890 erry = 0.01;
2891 errx = 0.144;
2892 Int_t iovery = iover % 100;
2893 Double_t corr = 0;
2894
2895/* ---> Ny = 1 */
2896 if (ny == 1) {
2897 if (iby != 0) {
2898 // edge effect
2899 yrec += iby * (0.1823+0.2008)/2;
2900 erry = 0.04587;
2901 } else {
2902 // Find "effective pad width"
2903 Double_t width = 0.218 / (1.31e-4 * TMath::Exp (2.688 * TMath::Log(qtot)) + 1) * 2;
2904 width = TMath::Min (width, 0.4);
2905 erry = width / TMath::Sqrt(12.);
2906 erry = TMath::Max (erry, 0.01293);
2907 }
2908 goto x; //return;
2909 }
2910
2911/* ---> "Bad" fit */
2912 if (fmin > 0.4) {
2913 erry = 0.1556;
2914 if (ny == 5) erry = 0.06481;
2915 goto x; //return;
2916 }
2917
2918/* ---> By != 0 */
2919 if (iby != 0) {
2920 if (ny > 2) {
2921 erry = 0.00417; //0.01010
2922 } else {
2923 // ny = 2
2924 if (dyc * iby > -0.05) {
2925 Double_t dyc2 = dyc * dyc;
2926 if (iby < 0) {
2927 corr = 0.019 - 0.602 * dyc + 8.739 * dyc2 - 44.209 * dyc2 * dyc;
2928 corr = TMath::Min (corr, TMath::Abs(-0.25-dyc));
2929 yrec -= corr;
2930 //dyc -= corr;
2931 erry = 0.00814;
2932 } else {
2933 corr = 0.006 + 0.300 * dyc + 6.147 * dyc2 + 42.039 * dyc2 * dyc;
2934 corr = TMath::Min (corr, 0.25-dyc);
2935 yrec += corr;
2936 //dyc += corr;
2937 erry = 0.01582;
2938 }
2939 } else {
2940 erry = (0.00303 + 0.00296) / 2;
2941 }
2942 }
2943 goto x; //return;
2944 }
2945
2946/* ---> Overflows */
2947 if (iovery != 0) {
2948 if (qtot < 3000) {
2949 erry = 0.0671;
2950 } else {
2951 if (iovery > 1) {
2952 erry = 0.09214;
2953 } else if (TMath::Abs(wy - 5) < 0.1) {
2954 erry = 0.061; //0.06622
2955 } else {
2956 erry = 0.00812; // 0.01073
2957 }
2958 }
2959 goto x; //return;
2960 }
2961
2962/* ---> "Good" but very high signal */
2963 if (qtot > 4000) {
2964 if (TMath::Abs(wy - 4) < 0.1) {
2965 erry = 0.00117;
2966 } else if (fmin < 0.03 && qtot < 6000) {
2967 erry = 0.01003;
2968 } else {
2969 erry = 0.1931;
2970 }
2971 goto x; //return;
2972 }
2973
2974/* ---> "Good" clusters */
2975 if (ny > 3) {
2976 if (TMath::Abs(wy - 5) < 0.1) {
2977 erry = 0.0011; //0.00304
2978 } else if (qtot < 400.) {
2979 erry = 0.0165;
2980 } else {
2981 erry = 0.00135; // 0.00358
2982 }
2983 } else if (ny == 3) {
2984 if (TMath::Abs(wy - 4) < 0.1) {
2985 erry = 35.407 / (1 + TMath::Exp(5.511*TMath::Log(qtot/265.51))) + 11.564;
2986 //erry = 83.512 / (1 + TMath::Exp(3.344*TMath::Log(qtot/211.58))) + 12.260;
2987 } else {
2988 erry = 147.03 / (1 + TMath::Exp(1.713*TMath::Log(qtot/73.151))) + 9.575;
2989 //erry = 91.743 / (1 + TMath::Exp(2.332*TMath::Log(qtot/151.67))) + 11.453;
2990 }
2991 erry *= 1.e-4;
2992 } else {
2993 // ny = 2
2994 if (TMath::Abs(wy - 4) < 0.1) {
2995 erry = 60.800 / (1 + TMath::Exp(3.305*TMath::Log(qtot/104.53))) + 11.702;
2996 //erry = 73.128 / (1 + TMath::Exp(5.676*TMath::Log(qtot/120.93))) + 17.839;
2997 } else {
2998 erry = 117.98 / (1 + TMath::Exp(2.005*TMath::Log(qtot/37.649))) + 21.431;
2999 //erry = 99.066 / (1 + TMath::Exp(4.900*TMath::Log(qtot/107.57))) + 25.315;
3000 }
3001 erry *= 1.e-4;
3002 }
3003 //return;
3004
3005 x:
3006/* ---> X-coordinate */
3007/* ---> Y-side */
3008 if (wx > 11) {
3009 errx = 0.0036;
3010 xrec -= 0.1385;
3011 return;
3012 }
3013/* ---> Nx = 1 */
3014 if (nx == 1) {
3015 if (TMath::Abs(wx - 6) < 0.1) {
3016 if (qtot < 40) errx = 0.1693;
3017 else errx = 0.06241;
3018 } else if (TMath::Abs(wx - 7.5) < 0.1) {
3019 if (qtot < 40) errx = 0.2173;
3020 else errx = 0.07703;
3021 } else if (TMath::Abs(wx - 10) < 0.1) {
3022 if (ibx == 0) {
3023 if (qtot < 40) errx = 0.2316;
3024 else errx = 0.1426;
3025 } else {
3026 xrec += (0.2115 + 0.1942) / 2 * ibx;
3027 errx = 0.1921;
3028 }
3029 }
3030 return;
3031 }
3032/* ---> "Bad" fit */
3033 if (fmin > 0.5) {
3034 errx = 0.1591;
3035 return;
3036 }
3037/* ---> Bx != 0 */
3038 if (ibx != 0) {
3039 if (ibx > 0) { errx = 0.06761; xrec -= 0.03832; }
3040 else { errx = 0.06653; xrec += 0.02581; }
3041 return;
3042 }
3043/* ---> Overflows */
3044 if (iover != 0) {
3045 if (TMath::Abs(wx - 6) < 0.1) errx = 0.06979;
3046 else if (TMath::Abs(wx - 7.5) < 0.1) errx = 0.1089;
3047 else if (TMath::Abs(wx - 10) < 0.1) errx = 0.09847;
3048 return;
3049 }
3050/* ---> Good */
3051 if (TMath::Abs(wx - 6) < 0.1) errx = 0.06022;
3052 else if (TMath::Abs(wx - 7.5) < 0.1) errx = 0.07247;
3053 else if (TMath::Abs(wx - 10) < 0.1) errx = 0.07359;
3054}
3055