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