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