]>
Commit | Line | Data |
---|---|---|
0df3ca52 | 1 | #include "AliMUONClusterFinderAZ.h" |
2 | ||
1f9a65c4 | 3 | #include <stdlib.h> |
0df3ca52 | 4 | #include <fcntl.h> |
5 | #include <Riostream.h> | |
6 | #include <TROOT.h> | |
7 | #include <TCanvas.h> | |
8 | #include <TLine.h> | |
9 | #include <TTree.h> | |
10 | #include <TH2.h> | |
11 | #include <TView.h> | |
12 | #include <TStyle.h> | |
13 | #include <TMinuit.h> | |
14 | #include <TMatrixD.h> | |
15 | ||
16 | #include "AliHeader.h" | |
17 | #include "AliRun.h" | |
18 | #include "AliMUON.h" | |
19 | #include "AliMUONChamber.h" | |
20 | #include "AliMUONDigit.h" | |
21 | #include "AliMUONHit.h" | |
22 | #include "AliMUONChamber.h" | |
23 | #include "AliMUONRawCluster.h" | |
24 | #include "AliMUONClusterInput.h" | |
25 | #include "AliMUONPixel.h" | |
5d12ce38 | 26 | #include "AliMC.h" |
0df3ca52 | 27 | |
c1aed84f | 28 | // Clusterizer class developped by Zitchenko (Dubna) |
29 | // | |
30 | // | |
31 | // | |
9e993f2a | 32 | |
0df3ca52 | 33 | |
34 | ClassImp(AliMUONClusterFinderAZ) | |
0558a292 | 35 | |
343146bf | 36 | const Double_t AliMUONClusterFinderAZ::fgkCouplMin = 1.e-3; // threshold on coupling |
0558a292 | 37 | AliMUONClusterFinderAZ* AliMUONClusterFinderAZ::fgClusterFinder = 0x0; |
38 | TMinuit* AliMUONClusterFinderAZ::fgMinuit = 0x0; | |
0df3ca52 | 39 | |
0df3ca52 | 40 | |
41 | //_____________________________________________________________________________ | |
42 | AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(Bool_t draw=0, Int_t iReco=0) | |
74f7bbc5 | 43 | : AliMUONClusterFinderVS() |
0df3ca52 | 44 | { |
45 | // Constructor | |
46 | for (Int_t i=0; i<4; i++) {fHist[i] = 0;} | |
47 | fMuonDigits = 0; | |
48 | fSegmentation[1] = fSegmentation[0] = 0; | |
0558a292 | 49 | fgClusterFinder = 0x0; |
50 | fgMinuit = 0x0; | |
0df3ca52 | 51 | if (!fgClusterFinder) fgClusterFinder = this; |
52 | if (!fgMinuit) fgMinuit = new TMinuit(8); | |
53 | fDraw = draw; | |
54 | fReco = iReco; | |
55 | fPixArray = new TObjArray(20); | |
56 | /* | |
57 | fPoints = 0; | |
58 | fPhits = 0; | |
59 | fRpoints = 0; | |
60 | fCanvas = 0; | |
61 | fNextCathode = kFALSE; | |
62 | fColPad = 0; | |
63 | */ | |
64 | } | |
65 | ||
74f7bbc5 | 66 | //_____________________________________________________________________________ |
67 | AliMUONClusterFinderAZ::AliMUONClusterFinderAZ(const AliMUONClusterFinderAZ& rhs) | |
68 | : AliMUONClusterFinderVS(rhs) | |
69 | { | |
70 | // Protected copy constructor | |
71 | ||
72 | Fatal("AliMUONClusterFinderAZModule", "Not implemented."); | |
73 | } | |
74 | ||
0df3ca52 | 75 | //_____________________________________________________________________________ |
76 | AliMUONClusterFinderAZ::~AliMUONClusterFinderAZ() | |
77 | { | |
78 | // Destructor | |
79 | delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0; | |
80 | /* | |
81 | // Delete space point structure | |
82 | if (fPoints) fPoints->Delete(); | |
83 | delete fPoints; | |
84 | fPoints = 0; | |
85 | // | |
86 | if (fPhits) fPhits->Delete(); | |
87 | delete fPhits; | |
88 | fPhits = 0; | |
89 | // | |
90 | if (fRpoints) fRpoints->Delete(); | |
91 | delete fRpoints; | |
92 | fRpoints = 0; | |
93 | */ | |
94 | } | |
95 | ||
74f7bbc5 | 96 | //_____________________________________________________________________________ |
97 | AliMUONClusterFinderAZ& | |
98 | AliMUONClusterFinderAZ::operator=(const AliMUONClusterFinderAZ& rhs) | |
99 | { | |
100 | // Protected assignement operator | |
101 | ||
102 | if (this == &rhs) return *this; | |
103 | ||
104 | Fatal("operator=", "Not implemented."); | |
105 | ||
106 | return *this; | |
107 | } | |
108 | ||
0df3ca52 | 109 | //_____________________________________________________________________________ |
110 | void AliMUONClusterFinderAZ::FindRawClusters() | |
111 | { | |
112 | // To provide the same interface as in AliMUONClusterFinderVS | |
113 | ||
114 | EventLoop (gAlice->GetHeader()->GetEvent(), AliMUONClusterInput::Instance()->Chamber()); | |
115 | } | |
116 | ||
117 | //_____________________________________________________________________________ | |
118 | void AliMUONClusterFinderAZ::EventLoop(Int_t nev=0, Int_t ch=0) | |
119 | { | |
120 | // Loop over events | |
121 | ||
122 | FILE *lun = 0; | |
123 | TCanvas *c1 = 0; | |
124 | TView *view = 0; | |
125 | TH2F *hist = 0; | |
126 | Double_t p1[3]={0}, p2[3]; | |
c1aed84f | 127 | TTree *treeR = 0; |
0df3ca52 | 128 | if (fDraw) { |
129 | // File | |
130 | lun = fopen("pool.dat","w"); | |
131 | c1 = new TCanvas("c1","Clusters",0,0,600,700); | |
132 | c1->Divide(1,2); | |
133 | new TCanvas("c2","Mlem",700,0,600,350); | |
134 | } | |
135 | ||
136 | newev: | |
137 | Int_t nparticles = 0, nent; | |
88cb7938 | 138 | |
139 | //Loaders | |
140 | AliRunLoader * rl = AliRunLoader::GetRunLoader(); | |
141 | AliLoader * gime = rl->GetLoader("MUONLoader"); | |
142 | ||
143 | if (!fReco) nparticles = rl->GetEvent(nev); | |
5d12ce38 | 144 | else nparticles = gAlice->GetMCApp()->GetNtrack(); |
0df3ca52 | 145 | cout << "nev " << nev <<endl; |
146 | cout << "nparticles " << nparticles <<endl; | |
147 | if (nparticles <= 0) return; | |
148 | ||
c1aed84f | 149 | TTree *treeH = gime->TreeH(); |
150 | Int_t ntracks = (Int_t) treeH->GetEntries(); | |
0df3ca52 | 151 | cout<<"ntracks "<<ntracks<<endl; |
152 | ||
153 | // Get pointers to Alice detectors and Digits containers | |
c1aed84f | 154 | AliMUON *muon = (AliMUON*) gAlice->GetModule("MUON"); |
155 | if (!muon) return; | |
0df3ca52 | 156 | // TClonesArray *Particles = gAlice->Particles(); |
157 | if (!fReco) { | |
c1aed84f | 158 | treeR = gime->TreeR(); |
159 | if (treeR) { | |
160 | muon->ResetRawClusters(); | |
161 | nent = (Int_t) treeR->GetEntries(); | |
0df3ca52 | 162 | if (nent != 1) { |
163 | cout << "Error in MUONdrawClust" << endl; | |
164 | cout << " nent = " << nent << " not equal to 1" << endl; | |
165 | //exit(0); | |
166 | } | |
c1aed84f | 167 | } // if (treeR) |
0df3ca52 | 168 | } // if (!fReco) |
169 | ||
c1aed84f | 170 | TTree *treeD = gime->TreeD(); |
171 | //muon->ResetDigits(); | |
0df3ca52 | 172 | |
c1aed84f | 173 | TClonesArray *listMUONrawclust ; |
0df3ca52 | 174 | AliMUONChamber* iChamber = 0; |
175 | ||
176 | // As default draw the first cluster of the chamber #0 | |
177 | ||
178 | newchamber: | |
179 | if (ch > 9) {if (fReco) return; nev++; ch = 0; goto newev;} | |
180 | //gAlice->ResetDigits(); | |
c1aed84f | 181 | fMuonDigits = muon->GetMUONData()->Digits(ch); |
0df3ca52 | 182 | if (fMuonDigits == 0) return; |
c1aed84f | 183 | iChamber = &(muon->Chamber(ch)); |
0df3ca52 | 184 | fSegmentation[0] = iChamber->SegmentationModel(1); |
185 | fSegmentation[1] = iChamber->SegmentationModel(2); | |
186 | fResponse = iChamber->ResponseModel(); | |
187 | ||
188 | nent = 0; | |
189 | ||
c1aed84f | 190 | if (treeD) { |
191 | nent = (Int_t) treeD->GetEntries(); | |
0df3ca52 | 192 | //printf(" entries %d \n", nent); |
193 | } | |
194 | ||
195 | Int_t ndigits[2]={9,9}, nShown[2]={0}; | |
196 | for (Int_t i=0; i<2; i++) { | |
343146bf | 197 | for (Int_t j=0; j<fgkDim; j++) {fUsed[i][j]=kFALSE;} |
0df3ca52 | 198 | } |
199 | ||
200 | next: | |
201 | if (ndigits[0] == nShown[0] && ndigits[1] == nShown[1]) { | |
202 | // No more clusters | |
203 | if (fReco) return; | |
204 | ch++; | |
205 | goto newchamber; // next chamber | |
206 | } | |
207 | Float_t xpad, ypad, zpad, zpad0; | |
208 | TLine *line[99]={0}; | |
209 | Int_t nLine = 0; | |
210 | Bool_t first = kTRUE; | |
211 | cout << " *** Event # " << nev << " chamber: " << ch << endl; | |
212 | fnPads[0] = fnPads[1] = 0; | |
343146bf | 213 | for (Int_t i=0; i<fgkDim; i++) {fPadIJ[1][i] = 0;} |
0df3ca52 | 214 | //for (Int_t iii = 0; iii<999; iii++) { |
215 | for (Int_t iii = 0; iii<2; iii++) { | |
216 | Int_t cath = TMath::Odd(iii); | |
217 | gAlice->ResetDigits(); | |
c1aed84f | 218 | treeD->GetEvent(cath); |
219 | fMuonDigits = muon->GetMUONData()->Digits(ch); | |
0df3ca52 | 220 | |
221 | ndigits[cath] = fMuonDigits->GetEntriesFast(); | |
222 | if (!ndigits[0] && !ndigits[1]) {if (fReco) return; ch++; goto newchamber;} | |
223 | if (ndigits[cath] == 0) continue; | |
224 | cout << " ndigits: " << ndigits[cath] << " " << cath << endl; | |
225 | ||
226 | AliMUONDigit *mdig; | |
227 | Int_t digit; | |
228 | ||
c1aed84f | 229 | Bool_t eEOC = kTRUE; // end-of-cluster |
0df3ca52 | 230 | for (digit = 0; digit < ndigits[cath]; digit++) { |
231 | mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit); | |
232 | if (mdig->Cathode() != cath) continue; | |
233 | if (first) { | |
234 | // Find first unused pad | |
235 | if (fUsed[cath][digit]) continue; | |
236 | fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad0); | |
237 | } else { | |
238 | if (fUsed[cath][digit]) continue; | |
239 | fSegmentation[cath]->GetPadC(mdig->PadX(),mdig->PadY(),xpad,ypad,zpad); | |
240 | if (TMath::Abs(zpad-zpad0)>0.1) continue; // different slats | |
241 | // Find a pad overlapping with the cluster | |
242 | if (!Overlap(cath,mdig)) continue; | |
243 | } | |
244 | // Add pad - recursive call | |
245 | AddPad(cath,digit); | |
c1aed84f | 246 | eEOC = kFALSE; |
0df3ca52 | 247 | if (digit >= 0) break; |
248 | } | |
c1aed84f | 249 | if (first && eEOC) { |
0df3ca52 | 250 | // No more unused pads |
251 | if (cath == 0) continue; // on cathode #0 - check #1 | |
252 | else { | |
253 | // No more clusters | |
254 | if (fReco) return; | |
255 | ch++; | |
256 | goto newchamber; // next chamber | |
257 | } | |
258 | } | |
c1aed84f | 259 | if (eEOC) break; // cluster found |
0df3ca52 | 260 | first = kFALSE; |
261 | cout << " nPads: " << fnPads[cath] << " " << nShown[cath]+fnPads[cath] << " " << cath << endl; | |
262 | } // for (Int_t iii = 0; | |
263 | ||
264 | ||
265 | if (fReco) goto skip; | |
266 | char hName[4]; | |
267 | for (Int_t cath = 0; cath<2; cath++) { | |
268 | // Build histograms | |
269 | if (fHist[cath*2]) {fHist[cath*2]->Delete(); fHist[cath*2] = 0;} | |
270 | if (fHist[cath*2+1]) {fHist[cath*2+1]->Delete(); fHist[cath*2+1] = 0;} | |
271 | if (fnPads[cath] == 0) continue; // cluster on one cathode only | |
272 | Float_t wxMin=999, wxMax=0, wyMin=999, wyMax=0; | |
273 | Int_t minDx=0, maxDx=0, minDy=0, maxDy=0; | |
274 | for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) { | |
275 | if (fPadIJ[0][i] != cath) continue; | |
276 | if (fXyq[3][i] < wxMin) {wxMin = fXyq[3][i]; minDx = i;} | |
277 | if (fXyq[3][i] > wxMax) {wxMax = fXyq[3][i]; maxDx = i;} | |
278 | if (fXyq[4][i] < wyMin) {wyMin = fXyq[4][i]; minDy = i;} | |
279 | if (fXyq[4][i] > wyMax) {wyMax = fXyq[4][i]; maxDy = i;} | |
280 | } | |
281 | cout << minDx << maxDx << minDy << maxDy << endl; | |
282 | Int_t nx, ny, padSize; | |
283 | Float_t xmin=9999, xmax=-9999, ymin=9999, ymax=-9999; | |
284 | if (TMath::Nint(fXyq[3][minDx]*1000) == TMath::Nint(fXyq[3][maxDx]*1000) && | |
285 | TMath::Nint(fXyq[4][minDy]*1000) == TMath::Nint(fXyq[4][maxDy]*1000)) { | |
286 | // the same segmentation | |
287 | cout << " Same" << endl; | |
288 | cout << fXyq[3][minDx] << " " << fXyq[3][maxDx] << " " << fXyq[4][minDy] << " " << fXyq[4][maxDy] << endl; | |
289 | for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) { | |
290 | if (fPadIJ[0][i] != cath) continue; | |
291 | if (fXyq[0][i] < xmin) xmin = fXyq[0][i]; | |
292 | if (fXyq[0][i] > xmax) xmax = fXyq[0][i]; | |
293 | if (fXyq[1][i] < ymin) ymin = fXyq[1][i]; | |
294 | if (fXyq[1][i] > ymax) ymax = fXyq[1][i]; | |
295 | } | |
296 | xmin -= fXyq[3][minDx]; xmax += fXyq[3][minDx]; | |
297 | ymin -= fXyq[4][minDy]; ymax += fXyq[4][minDy]; | |
298 | nx = TMath::Nint ((xmax-xmin)/wxMin/2); | |
299 | ny = TMath::Nint ((ymax-ymin)/wyMin/2); | |
300 | sprintf(hName,"h%d",cath*2); | |
301 | fHist[cath*2] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax); | |
302 | cout << fHist[cath*2] << " " << fnPads[cath] << endl; | |
303 | for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) { | |
304 | if (fPadIJ[0][i] != cath) continue; | |
305 | fHist[cath*2]->Fill(fXyq[0][i],fXyq[1][i],fXyq[2][i]); | |
306 | //cout << fXyq[0][i] << fXyq[1][i] << fXyq[2][i] << endl; | |
307 | } | |
308 | } else { | |
309 | // different segmentation in the cluster | |
310 | cout << " Different" << endl; | |
311 | cout << fXyq[3][minDx] << " " << fXyq[3][maxDx] << " " << fXyq[4][minDy] << " " << fXyq[4][maxDy] << endl; | |
312 | Int_t nOK = 0; | |
313 | Int_t indx, locMin, locMax; | |
314 | if (TMath::Nint(fXyq[3][minDx]*1000) != TMath::Nint(fXyq[3][maxDx]*1000)) { | |
315 | // different segmentation along x | |
316 | indx = 0; | |
317 | locMin = minDx; | |
318 | locMax = maxDx; | |
319 | } else { | |
320 | // different segmentation along y | |
321 | indx = 1; | |
322 | locMin = minDy; | |
323 | locMax = maxDy; | |
324 | } | |
325 | Int_t loc = locMin; | |
326 | for (Int_t i=0; i<2; i++) { | |
327 | // loop over different pad sizes | |
328 | if (i>0) loc = locMax; | |
329 | padSize = TMath::Nint(fXyq[indx+3][loc]*1000); | |
330 | xmin = 9999; xmax = -9999; ymin = 9999; ymax = -9999; | |
331 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
332 | if (fPadIJ[0][j] != cath) continue; | |
333 | if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue; | |
334 | nOK++; | |
335 | xmin = TMath::Min (xmin,fXyq[0][j]); | |
336 | xmax = TMath::Max (xmax,fXyq[0][j]); | |
337 | ymin = TMath::Min (ymin,fXyq[1][j]); | |
338 | ymax = TMath::Max (ymax,fXyq[1][j]); | |
339 | } | |
340 | xmin -= fXyq[3][loc]; xmax += fXyq[3][loc]; | |
341 | ymin -= fXyq[4][loc]; ymax += fXyq[4][loc]; | |
342 | nx = TMath::Nint ((xmax-xmin)/fXyq[3][loc]/2); | |
343 | ny = TMath::Nint ((ymax-ymin)/fXyq[4][loc]/2); | |
344 | sprintf(hName,"h%d",cath*2+i); | |
345 | fHist[cath*2+i] = new TH2F(hName,"cluster",nx,xmin,xmax,ny,ymin,ymax); | |
346 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
347 | if (fPadIJ[0][j] != cath) continue; | |
348 | if (TMath::Nint(fXyq[indx+3][j]*1000) != padSize) continue; | |
349 | fHist[cath*2+i]->Fill(fXyq[0][j],fXyq[1][j],fXyq[2][j]); | |
350 | } | |
351 | } // for (Int_t i=0; | |
352 | if (nOK != fnPads[cath]) cout << " *** Too many segmentations: nPads, nOK " << fnPads[cath] << " " << nOK << endl; | |
353 | } // if (TMath::Nint(fXyq[3][minDx]*1000) | |
354 | } // for (Int_t cath = 0; | |
355 | ||
356 | // Draw histograms and coordinates | |
357 | for (Int_t cath=0; cath<2; cath++) { | |
358 | if (cath == 0) ModifyHistos(); | |
359 | if (fnPads[cath] == 0) continue; // cluster on one cathode only | |
360 | if (fDraw) { | |
361 | c1->cd(cath+1); | |
362 | gPad->SetTheta(55); | |
363 | gPad->SetPhi(30); | |
cd747ddb | 364 | Double_t x, y, x0, y0, r1=999, r2=0; |
0df3ca52 | 365 | if (fHist[cath*2+1]) { |
366 | // | |
367 | x0 = fHist[cath*2]->GetXaxis()->GetXmin() - 1000*TMath::Cos(30*TMath::Pi()/180); | |
368 | y0 = fHist[cath*2]->GetYaxis()->GetXmin() - 1000*TMath::Sin(30*TMath::Pi()/180); | |
369 | r1 = 0; | |
370 | Int_t ihist=cath*2; | |
371 | for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) { | |
372 | y = fHist[ihist]->GetYaxis()->GetBinCenter(iy) | |
373 | + fHist[ihist]->GetYaxis()->GetBinWidth(iy); | |
374 | for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) { | |
375 | if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) { | |
376 | x = fHist[ihist]->GetXaxis()->GetBinCenter(ix) | |
377 | + fHist[ihist]->GetXaxis()->GetBinWidth(ix); | |
378 | r1 = TMath::Max (r1,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0))); | |
379 | } | |
380 | } | |
381 | } | |
382 | ihist = cath*2 + 1 ; | |
383 | for (Int_t iy=1; iy<=fHist[ihist]->GetNbinsY(); iy++) { | |
384 | y = fHist[ihist]->GetYaxis()->GetBinCenter(iy) | |
385 | + fHist[ihist]->GetYaxis()->GetBinWidth(iy); | |
386 | for (Int_t ix=1; ix<=fHist[ihist]->GetNbinsX(); ix++) { | |
387 | if (fHist[ihist]->GetCellContent(ix,iy) > 0.1) { | |
388 | x = fHist[ihist]->GetXaxis()->GetBinCenter(ix) | |
389 | + fHist[ihist]->GetXaxis()->GetBinWidth(ix); | |
390 | r2 = TMath::Max (r2,TMath::Sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0))); | |
391 | } | |
392 | } | |
393 | } | |
394 | cout << r1 << " " << r2 << endl; | |
395 | } // if (fHist[cath*2+1]) | |
396 | if (r1 > r2) { | |
397 | //fHist[cath*2]->Draw("lego1"); | |
398 | fHist[cath*2]->Draw("lego1Fb"); | |
399 | //if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBb"); | |
400 | if (fHist[cath*2+1]) fHist[cath*2+1]->Draw("lego1SameAxisBbFb"); | |
401 | } else { | |
402 | //fHist[cath*2+1]->Draw("lego1"); | |
403 | fHist[cath*2+1]->Draw("lego1Fb"); | |
404 | //fHist[cath*2]->Draw("lego1SameAxisBb"); | |
405 | fHist[cath*2]->Draw("lego1SameAxisFbBb"); | |
406 | } | |
407 | c1->Update(); | |
408 | } // if (fDraw) | |
409 | } // for (Int_t cath = 0; | |
410 | ||
411 | // Draw generated hits | |
412 | Double_t xNDC[6]; | |
413 | hist = fHist[0] ? fHist[0] : fHist[2]; | |
414 | p2[2] = hist->GetMaximum(); | |
415 | view = 0; | |
416 | if (c1) view = c1->Pad()->GetView(); | |
417 | cout << " *** GEANT hits *** " << endl; | |
418 | fnMu = 0; | |
419 | Int_t ix, iy, iok; | |
420 | for (Int_t i=0; i<ntracks; i++) { | |
c1aed84f | 421 | treeH->GetEvent(i); |
422 | for (AliMUONHit* mHit=(AliMUONHit*)muon->FirstHit(-1); | |
0df3ca52 | 423 | mHit; |
c1aed84f | 424 | mHit=(AliMUONHit*)muon->NextHit()) { |
0df3ca52 | 425 | if (mHit->Chamber() != ch+1) continue; // chamber number |
426 | if (TMath::Abs(mHit->Z()-zpad0) > 1) continue; // different slat | |
427 | p2[0] = p1[0] = mHit->X(); // x-pos of hit | |
428 | p2[1] = p1[1] = mHit->Y(); // y-pos | |
429 | if (p1[0] < hist->GetXaxis()->GetXmin() || | |
430 | p1[0] > hist->GetXaxis()->GetXmax()) continue; | |
431 | if (p1[1] < hist->GetYaxis()->GetXmin() || | |
432 | p1[1] > hist->GetYaxis()->GetXmax()) continue; | |
433 | // Check if track comes thru pads with signal | |
434 | iok = 0; | |
435 | for (Int_t ihist=0; ihist<4; ihist++) { | |
436 | if (!fHist[ihist]) continue; | |
437 | ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]); | |
438 | iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]); | |
439 | if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;} | |
440 | } | |
441 | if (!iok) continue; | |
442 | gStyle->SetLineColor(1); | |
443 | if (TMath::Abs((Int_t)mHit->Particle()) == 13) { | |
444 | gStyle->SetLineColor(4); | |
445 | fnMu++; | |
446 | if (fnMu <= 2) { | |
447 | fxyMu[fnMu-1][0] = p1[0]; | |
448 | fxyMu[fnMu-1][1] = p1[1]; | |
449 | } | |
450 | } | |
451 | printf(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mHit->Z()); | |
452 | if (view) { | |
453 | view->WCtoNDC(p1, &xNDC[0]); | |
454 | view->WCtoNDC(p2, &xNDC[3]); | |
455 | for (Int_t ipad=1; ipad<3; ipad++) { | |
456 | c1->cd(ipad); | |
457 | //c1->DrawLine(xpad[0],xpad[1],xpad[3],xpad[4]); | |
458 | line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]); | |
459 | line[nLine++]->Draw(); | |
460 | } | |
461 | } | |
462 | } // for (AliMUONHit* mHit= | |
463 | } // for (Int_t i=0; i<ntracks; | |
464 | ||
465 | // Draw reconstructed coordinates | |
c1aed84f | 466 | listMUONrawclust = muon->GetMUONData()->RawClusters(ch); |
467 | treeR->GetEvent(ch); | |
468 | //cout << listMUONrawclust << " " << listMUONrawclust ->GetEntries() << endl; | |
0df3ca52 | 469 | AliMUONRawCluster *mRaw; |
470 | gStyle->SetLineColor(3); | |
471 | cout << " *** Reconstructed hits *** " << endl; | |
c1aed84f | 472 | for (Int_t i=0; i<listMUONrawclust ->GetEntries(); i++) { |
473 | mRaw = (AliMUONRawCluster*)listMUONrawclust ->UncheckedAt(i); | |
ba12c242 | 474 | if (TMath::Abs(mRaw->GetZ(0)-zpad0) > 1) continue; // different slat |
475 | p2[0] = p1[0] = mRaw->GetX(0); // x-pos of hit | |
476 | p2[1] = p1[1] = mRaw->GetY(0); // y-pos | |
0df3ca52 | 477 | if (p1[0] < hist->GetXaxis()->GetXmin() || |
478 | p1[0] > hist->GetXaxis()->GetXmax()) continue; | |
479 | if (p1[1] < hist->GetYaxis()->GetXmin() || | |
480 | p1[1] > hist->GetYaxis()->GetXmax()) continue; | |
481 | /* | |
c1aed84f | 482 | treeD->GetEvent(cath); |
0df3ca52 | 483 | cout << mRaw->fMultiplicity[0] << mRaw->fMultiplicity[1] << endl; |
484 | for (Int_t j=0; j<mRaw->fMultiplicity[cath]; j++) { | |
485 | Int_t digit = mRaw->fIndexMap[j][cath]; | |
486 | cout << ((AliMUONDigit*)fMuonDigits->UncheckedAt(digit))->Signal() << endl; | |
487 | } | |
488 | */ | |
489 | // Check if track comes thru pads with signal | |
490 | iok = 0; | |
491 | for (Int_t ihist=0; ihist<4; ihist++) { | |
492 | if (!fHist[ihist]) continue; | |
493 | ix = fHist[ihist]->GetXaxis()->FindBin(p1[0]); | |
494 | iy = fHist[ihist]->GetYaxis()->FindBin(p1[1]); | |
495 | if (fHist[ihist]->GetCellContent(ix,iy) > 0.5) {iok = 1; break;} | |
496 | } | |
497 | if (!iok) continue; | |
ba12c242 | 498 | printf(" X=%10.4f, Y=%10.4f, Z=%10.4f\n",p1[0],p1[1],mRaw->GetZ(0)); |
0df3ca52 | 499 | if (view) { |
500 | view->WCtoNDC(p1, &xNDC[0]); | |
501 | view->WCtoNDC(p2, &xNDC[3]); | |
502 | for (Int_t ipad=1; ipad<3; ipad++) { | |
503 | c1->cd(ipad); | |
504 | line[nLine] = new TLine(xNDC[0],xNDC[1],xNDC[3],xNDC[4]); | |
505 | line[nLine++]->Draw(); | |
506 | } | |
507 | } | |
c1aed84f | 508 | } // for (Int_t i=0; i<listMUONrawclust ->GetEntries(); |
0df3ca52 | 509 | if (fDraw) c1->Update(); |
510 | ||
511 | skip: | |
512 | // Use MLEM for cluster finder | |
513 | fZpad = zpad0; | |
514 | Int_t nMax = 1, localMax[100], maxPos[100]; | |
515 | Double_t maxVal[100]; | |
516 | ||
517 | if (CheckPrecluster(nShown)) { | |
518 | BuildPixArray(); | |
519 | if (fnPads[0]+fnPads[1] > 50) nMax = FindLocalMaxima(localMax, maxVal); | |
520 | if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order | |
521 | for (Int_t i=0; i<nMax; i++) { | |
522 | if (nMax > 1) FindCluster(localMax, maxPos[i]); | |
523 | if (!MainLoop()) cout << " MainLoop failed " << endl; | |
524 | if (i < nMax-1) { | |
525 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
526 | if (fPadIJ[1][j] == 0) continue; // pad charge was not modified | |
527 | fPadIJ[1][j] = 0; | |
528 | fXyq[2][j] = fXyq[5][j]; // use backup charge value | |
529 | } | |
530 | } | |
531 | } | |
532 | } | |
533 | if (fReco) goto next; | |
534 | ||
535 | for (Int_t i=0; i<fnMu; i++) { | |
536 | // Check again if muon come thru the used pads (due to extra splitting) | |
537 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
538 | if (TMath::Abs(fxyMu[i][0]-fXyq[0][j])<fXyq[3][j] && | |
539 | TMath::Abs(fxyMu[i][1]-fXyq[1][j])<fXyq[4][j]) { | |
540 | printf("%12.3e %12.3e %12.3e %12.3e\n",fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]); | |
541 | if (lun) fprintf(lun,"%4d %2d %12.3e %12.3e %12.3e %12.3e\n",nev,ch,fxyMu[i][2],fxyMu[i][3],fxyMu[i][4],fxyMu[i][5]); | |
542 | break; | |
543 | } | |
544 | } | |
545 | } // for (Int_t i=0; i<fnMu; | |
546 | ||
547 | // What's next? | |
548 | char command[8]; | |
549 | cout << " What is next? " << endl; | |
550 | command[0] = ' '; | |
551 | if (fDraw) gets(command); | |
552 | if (command[0] == 'n' || command[0] == 'N') {nev++; goto newev;} // next event | |
553 | else if (command[0] == 'q' || command[0] == 'Q') {fclose(lun); return;} // exit display | |
554 | //else if (command[0] == 'r' || command[0] == 'R') goto redraw; // redraw points | |
555 | else if (command[0] == 'c' || command[0] == 'C') { | |
556 | // new chamber | |
557 | sscanf(command+1,"%d",&ch); | |
558 | goto newchamber; | |
559 | } | |
560 | else if (command[0] == 'e' || command[0] == 'E') { | |
561 | // new event | |
562 | sscanf(command+1,"%d",&nev); | |
563 | goto newev; | |
564 | } | |
565 | else goto next; // Next cluster | |
566 | } | |
567 | ||
568 | //_____________________________________________________________________________ | |
569 | void AliMUONClusterFinderAZ::ModifyHistos(void) | |
570 | { | |
571 | // Modify histograms to bring them to the same size | |
572 | Int_t nhist = 0; | |
573 | Float_t hlim[4][4], hbin[4][4]; // first index - xmin, xmax, ymin, ymax | |
574 | Float_t binMin[4] = {999,999,999,999}; | |
575 | ||
576 | for (Int_t i=0; i<4; i++) { | |
577 | if (!fHist[i]) continue; | |
578 | hlim[0][nhist] = fHist[i]->GetXaxis()->GetXmin(); // xmin | |
579 | hlim[1][nhist] = fHist[i]->GetXaxis()->GetXmax(); // xmax | |
580 | hlim[2][nhist] = fHist[i]->GetYaxis()->GetXmin(); // ymin | |
581 | hlim[3][nhist] = fHist[i]->GetYaxis()->GetXmax(); // ymax | |
582 | hbin[0][nhist] = hbin[1][nhist] = fHist[i]->GetXaxis()->GetBinWidth(1); | |
583 | hbin[2][nhist] = hbin[3][nhist] = fHist[i]->GetYaxis()->GetBinWidth(1); | |
584 | binMin[0] = TMath::Min(binMin[0],hbin[0][nhist]); | |
585 | binMin[2] = TMath::Min(binMin[2],hbin[2][nhist]); | |
586 | nhist++; | |
587 | } | |
588 | binMin[1] = binMin[0]; | |
589 | binMin[3] = binMin[2]; | |
590 | cout << " Nhist: " << nhist << endl; | |
591 | ||
592 | Int_t imin, imax; | |
593 | for (Int_t lim=0; lim<4; lim++) { | |
594 | while (1) { | |
595 | imin = TMath::LocMin(nhist,hlim[lim]); | |
596 | imax = TMath::LocMax(nhist,hlim[lim]); | |
597 | if (TMath::Abs(hlim[lim][imin]-hlim[lim][imax])<0.01*binMin[lim]) break; | |
598 | if (lim == 0 || lim == 2) { | |
599 | // find lower limit | |
600 | hlim[lim][imax] -= hbin[lim][imax]; | |
601 | } else { | |
602 | // find upper limit | |
603 | hlim[lim][imin] += hbin[lim][imin]; | |
604 | } | |
605 | } // while (1) | |
606 | } | |
607 | ||
608 | // Rebuild histograms | |
609 | nhist = 0; | |
610 | TH2F *hist = 0; | |
611 | Int_t nx, ny; | |
cd747ddb | 612 | Double_t x, y, cont, cmax=0; |
0df3ca52 | 613 | char hName[4]; |
614 | for (Int_t ihist=0; ihist<4; ihist++) { | |
615 | if (!fHist[ihist]) continue; | |
616 | nx = TMath::Nint((hlim[1][nhist]-hlim[0][nhist])/hbin[0][nhist]); | |
617 | ny = TMath::Nint((hlim[3][nhist]-hlim[2][nhist])/hbin[2][nhist]); | |
618 | //hist = new TH2F("h","hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]); | |
619 | sprintf(hName,"hh%d",ihist); | |
620 | hist = new TH2F(hName,"hist",nx,hlim[0][nhist],hlim[1][nhist],ny,hlim[2][nhist],hlim[3][nhist]); | |
621 | for (Int_t i=1; i<=fHist[ihist]->GetNbinsX(); i++) { | |
622 | x = fHist[ihist]->GetXaxis()->GetBinCenter(i); | |
623 | for (Int_t j=1; j<=fHist[ihist]->GetNbinsY(); j++) { | |
624 | y = fHist[ihist]->GetYaxis()->GetBinCenter(j); | |
625 | cont = fHist[ihist]->GetCellContent(i,j); | |
626 | hist->Fill(x,y,cont); | |
627 | } | |
628 | } | |
629 | cmax = TMath::Max (cmax,hist->GetMaximum()); | |
630 | fHist[ihist]->Delete(); | |
631 | fHist[ihist] = new TH2F(*hist); | |
632 | hist->Delete(); | |
633 | nhist++; | |
634 | } | |
635 | printf("%f \n",cmax); | |
636 | ||
637 | for (Int_t ihist=0; ihist<4; ihist++) { | |
638 | if (!fHist[ihist]) continue; | |
639 | fHist[ihist]->SetMaximum(cmax); | |
640 | } | |
641 | } | |
642 | ||
643 | //_____________________________________________________________________________ | |
644 | void AliMUONClusterFinderAZ::AddPad(Int_t cath, Int_t digit) | |
645 | { | |
646 | // Add pad to the cluster | |
647 | AliMUONDigit *mdig = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit); | |
648 | ||
649 | Int_t charge = mdig->Signal(); | |
650 | // get the center of the pad | |
651 | Float_t xpad, ypad, zpad; | |
652 | fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad); | |
653 | ||
654 | Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY()); | |
655 | Int_t nPads = fnPads[0] + fnPads[1]; | |
656 | fXyq[0][nPads] = xpad; | |
657 | fXyq[1][nPads] = ypad; | |
658 | fXyq[2][nPads] = charge; | |
659 | fXyq[3][nPads] = fSegmentation[cath]->Dpx(isec)/2; | |
660 | fXyq[4][nPads] = fSegmentation[cath]->Dpy(isec)/2; | |
661 | fXyq[5][nPads] = digit; | |
662 | fPadIJ[0][nPads] = cath; | |
663 | fPadIJ[1][nPads] = 0; | |
664 | fUsed[cath][digit] = kTRUE; | |
665 | //cout << " bbb " << fXyq[cath][2][nPads] << " " << fXyq[cath][0][nPads] << " " << fXyq[cath][1][nPads] << " " << fXyq[cath][3][nPads] << " " << fXyq[cath][4][nPads] << " " << zpad << " " << nPads << endl; | |
666 | fnPads[cath]++; | |
667 | ||
668 | // Check neighbours | |
669 | Int_t nn, ix, iy, xList[10], yList[10]; | |
670 | AliMUONDigit *mdig1; | |
671 | ||
672 | Int_t ndigits = fMuonDigits->GetEntriesFast(); | |
673 | fSegmentation[cath]->Neighbours(mdig->PadX(),mdig->PadY(),&nn,xList,yList); | |
674 | for (Int_t in=0; in<nn; in++) { | |
675 | ix=xList[in]; | |
676 | iy=yList[in]; | |
677 | for (Int_t digit1 = 0; digit1 < ndigits; digit1++) { | |
678 | if (digit1 == digit) continue; | |
679 | mdig1 = (AliMUONDigit*)fMuonDigits->UncheckedAt(digit1); | |
680 | if (mdig1->Cathode() != cath) continue; | |
681 | if (!fUsed[cath][digit1] && mdig1->PadX() == ix && mdig1->PadY() == iy) { | |
682 | fUsed[cath][digit1] = kTRUE; | |
683 | // Add pad - recursive call | |
684 | AddPad(cath,digit1); | |
685 | } | |
686 | } //for (Int_t digit1 = 0; | |
687 | } // for (Int_t in=0; | |
688 | } | |
689 | ||
690 | //_____________________________________________________________________________ | |
691 | Bool_t AliMUONClusterFinderAZ::Overlap(Int_t cath, TObject *dig) | |
692 | { | |
693 | // Check if the pad from one cathode overlaps with a pad | |
694 | // in the precluster on the other cathode | |
695 | ||
696 | AliMUONDigit *mdig = (AliMUONDigit*) dig; | |
697 | ||
698 | Float_t xpad, ypad, zpad; | |
699 | fSegmentation[cath]->GetPadC(mdig->PadX(), mdig->PadY(), xpad, ypad, zpad); | |
700 | Int_t isec = fSegmentation[cath]->Sector(mdig->PadX(), mdig->PadY()); | |
701 | ||
702 | Float_t xy1[4], xy12[4]; | |
703 | xy1[0] = xpad - fSegmentation[cath]->Dpx(isec)/2; | |
704 | xy1[1] = xy1[0] + fSegmentation[cath]->Dpx(isec); | |
705 | xy1[2] = ypad - fSegmentation[cath]->Dpy(isec)/2; | |
706 | xy1[3] = xy1[2] + fSegmentation[cath]->Dpy(isec); | |
707 | //cout << " ok " << fnPads[0]+fnPads[1] << xy1[0] << xy1[1] << xy1[2] << xy1[3] << endl; | |
708 | ||
709 | Int_t cath1 = TMath::Even(cath); | |
710 | for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) { | |
711 | if (fPadIJ[0][i] != cath1) continue; | |
712 | if (Overlap(xy1, i, xy12, 0)) return kTRUE; | |
713 | } | |
714 | return kFALSE; | |
715 | } | |
716 | ||
717 | //_____________________________________________________________________________ | |
718 | Bool_t AliMUONClusterFinderAZ::Overlap(Float_t *xy1, Int_t iPad, Float_t *xy12, Int_t iSkip) | |
719 | { | |
720 | // Check if the pads xy1 and iPad overlap and return overlap area | |
721 | ||
722 | Float_t xy2[4]; | |
723 | xy2[0] = fXyq[0][iPad] - fXyq[3][iPad]; | |
724 | xy2[1] = fXyq[0][iPad] + fXyq[3][iPad]; | |
725 | if (xy1[0] > xy2[1]-1.e-4 || xy1[1] < xy2[0]+1.e-4) return kFALSE; | |
726 | xy2[2] = fXyq[1][iPad] - fXyq[4][iPad]; | |
727 | xy2[3] = fXyq[1][iPad] + fXyq[4][iPad]; | |
728 | if (xy1[2] > xy2[3]-1.e-4 || xy1[3] < xy2[2]+1.e-4) return kFALSE; | |
729 | if (!iSkip) return kTRUE; // just check overlap (w/out computing the area) | |
730 | xy12[0] = TMath::Max (xy1[0],xy2[0]); | |
731 | xy12[1] = TMath::Min (xy1[1],xy2[1]); | |
732 | xy12[2] = TMath::Max (xy1[2],xy2[2]); | |
733 | xy12[3] = TMath::Min (xy1[3],xy2[3]); | |
734 | return kTRUE; | |
735 | } | |
736 | ||
737 | //_____________________________________________________________________________ | |
738 | /* | |
739 | Bool_t AliMUONClusterFinderAZ::Overlap(Int_t i, Int_t j, Float_t *xy12, Int_t iSkip) | |
740 | { | |
741 | // Check if the pads i and j overlap and return overlap area | |
742 | ||
743 | Float_t xy1[4], xy2[4]; | |
744 | return Overlap(xy1, xy2, xy12, iSkip); | |
745 | } | |
746 | */ | |
747 | //_____________________________________________________________________________ | |
748 | Bool_t AliMUONClusterFinderAZ::CheckPrecluster(Int_t *nShown) | |
749 | { | |
750 | // Check precluster in order to attempt to simplify it (mostly for | |
751 | // two-cathode preclusters) | |
752 | ||
753 | Int_t i1, i2; | |
754 | Float_t xy1[4], xy12[4]; | |
755 | ||
756 | Int_t npad = fnPads[0] + fnPads[1]; | |
757 | ||
758 | // If pads have the same size take average of pads on both cathodes | |
759 | Int_t sameSize = (fnPads[0] && fnPads[1]) ? 1 : 0; | |
760 | if (sameSize) { | |
761 | Double_t xSize = -1, ySize = 0; | |
762 | for (Int_t i=0; i<npad; i++) { | |
763 | if (fXyq[2][i] < 0) continue; | |
764 | if (xSize < 0) { xSize = fXyq[3][i]; ySize = fXyq[4][i]; } | |
765 | if (TMath::Abs(xSize-fXyq[3][i]) > 1.e-4 || TMath::Abs(ySize-fXyq[4][i]) > 1.e-4) { sameSize = 0; break; } | |
766 | } | |
767 | } // if (sameSize) | |
768 | if (sameSize && (fnPads[0] > 2 || fnPads[1] > 2)) { | |
769 | nShown[0] += fnPads[0]; | |
770 | nShown[1] += fnPads[1]; | |
771 | fnPads[0] = fnPads[1] = 0; | |
772 | Int_t div; | |
773 | for (Int_t i=0; i<npad; i++) { | |
774 | if (fXyq[2][i] < 0) continue; // used pad | |
775 | fXyq[2][fnPads[0]] = fXyq[2][i]; | |
776 | div = 1; | |
777 | for (Int_t j=i+1; j<npad; j++) { | |
778 | if (fPadIJ[0][j] == fPadIJ[0][i]) continue; // same cathode | |
779 | if (TMath::Abs(fXyq[0][j]-fXyq[0][i]) > 1.e-4) continue; | |
780 | if (TMath::Abs(fXyq[1][j]-fXyq[1][i]) > 1.e-4) continue; | |
781 | fXyq[2][fnPads[0]] += fXyq[2][j]; | |
782 | div = 2; | |
783 | fXyq[2][j] = -2; | |
784 | break; | |
785 | } | |
786 | fXyq[2][fnPads[0]] /= div; | |
787 | fXyq[0][fnPads[0]] = fXyq[0][i]; | |
788 | fXyq[1][fnPads[0]] = fXyq[1][i]; | |
789 | fPadIJ[0][fnPads[0]++] = 0; | |
790 | } | |
791 | } // if (sameSize) | |
792 | ||
793 | // Check if one-cathode precluster | |
794 | i1 = fnPads[0]!=0 ? 0 : 1; | |
795 | i2 = fnPads[1]!=0 ? 1 : 0; | |
796 | ||
797 | if (i1 != i2) { // two-cathode | |
798 | ||
799 | Int_t *flags = new Int_t[npad]; | |
800 | for (Int_t i=0; i<npad; i++) { flags[i] = 0; } | |
801 | ||
802 | // Check pad overlaps | |
803 | for (Int_t i=0; i<npad; i++) { | |
804 | if (fPadIJ[0][i] != i1) continue; | |
805 | xy1[0] = fXyq[0][i] - fXyq[3][i]; | |
806 | xy1[1] = fXyq[0][i] + fXyq[3][i]; | |
807 | xy1[2] = fXyq[1][i] - fXyq[4][i]; | |
808 | xy1[3] = fXyq[1][i] + fXyq[4][i]; | |
809 | for (Int_t j=0; j<npad; j++) { | |
810 | if (fPadIJ[0][j] != i2) continue; | |
811 | if (!Overlap(xy1, j, xy12, 0)) continue; | |
812 | flags[i] = flags[j] = 1; // mark overlapped pads | |
813 | } // for (Int_t j=0; | |
814 | } // for (Int_t i=0; | |
815 | ||
816 | // Check if all pads overlap | |
817 | Int_t digit=0, cath, nFlags=0; | |
818 | for (Int_t i=0; i<npad; i++) {nFlags += !flags[i];} | |
819 | if (nFlags) cout << " nFlags = " << nFlags << endl; | |
820 | //if (nFlags > 2 || (Float_t)nFlags / npad > 0.2) { // why 2 ??? - empirical choice | |
821 | if (nFlags > 0) { | |
822 | for (Int_t i=0; i<npad; i++) { | |
823 | if (flags[i]) continue; | |
824 | digit = TMath::Nint (fXyq[5][i]); | |
825 | cath = fPadIJ[0][i]; | |
826 | fUsed[cath][digit] = kFALSE; // release pad | |
827 | fXyq[2][i] = -2; | |
828 | fnPads[cath]--; | |
829 | } | |
830 | } // if (nFlags > 2) | |
831 | ||
832 | // Check correlations of cathode charges | |
833 | if (fnPads[0] && fnPads[1]) { // two-cathode | |
834 | Double_t sum[2]={0}; | |
835 | Int_t over[2] = {1, 1}; | |
836 | for (Int_t i=0; i<npad; i++) { | |
837 | cath = fPadIJ[0][i]; | |
838 | if (fXyq[2][i] > 0) sum[cath] += fXyq[2][i]; | |
839 | if (fXyq[2][i] > fResponse->MaxAdc()-1) over[cath] = 0; | |
840 | } | |
841 | cout << " Total charge: " << sum[0] << " " << sum[1] << endl; | |
842 | if ((over[0] || over[1]) && TMath::Abs(sum[0]-sum[1])/(sum[0]+sum[1])*2 > 1) { // 3 times difference | |
843 | cout << " Release " << endl; | |
844 | // Big difference | |
845 | cath = sum[0]>sum[1] ? 0 : 1; | |
846 | Int_t imax = 0; | |
847 | Double_t cmax=-1; | |
848 | Double_t *dist = new Double_t[npad]; | |
849 | for (Int_t i=0; i<npad; i++) { | |
850 | if (fPadIJ[0][i] != cath) continue; | |
851 | if (fXyq[2][i] < cmax) continue; | |
852 | cmax = fXyq[2][i]; | |
853 | imax = i; | |
854 | } | |
855 | // Arrange pads according to their distance to the max, | |
856 | // normalized to the pad size | |
857 | for (Int_t i=0; i<npad; i++) { | |
858 | dist[i] = 0; | |
859 | if (fPadIJ[0][i] != cath) continue; | |
860 | if (i == imax) continue; | |
861 | if (fXyq[2][i] < 0) continue; | |
862 | dist[i] = (fXyq[0][i]-fXyq[0][imax])*(fXyq[0][i]-fXyq[0][imax])/ | |
863 | fXyq[3][imax]/fXyq[3][imax]/4; | |
864 | dist[i] += (fXyq[1][i]-fXyq[1][imax])*(fXyq[1][i]-fXyq[1][imax])/ | |
865 | fXyq[4][imax]/fXyq[4][imax]/4; | |
866 | dist[i] = TMath::Sqrt (dist[i]); | |
867 | } | |
868 | TMath::Sort(npad, dist, flags, kFALSE); // in increasing order | |
869 | Int_t indx; | |
870 | Double_t xmax = -1; | |
871 | for (Int_t i=0; i<npad; i++) { | |
872 | indx = flags[i]; | |
873 | if (fPadIJ[0][indx] != cath) continue; | |
874 | if (fXyq[2][indx] < 0) continue; | |
875 | if (fXyq[2][indx] <= cmax || TMath::Abs(dist[indx]-xmax)<1.e-3) { | |
876 | // Release pads | |
877 | if (TMath::Abs(dist[indx]-xmax)<1.e-3) | |
cd747ddb | 878 | cmax = TMath::Max((Double_t)(fXyq[2][indx]),cmax); |
0df3ca52 | 879 | else cmax = fXyq[2][indx]; |
880 | xmax = dist[indx]; | |
881 | digit = TMath::Nint (fXyq[5][indx]); | |
882 | fUsed[cath][digit] = kFALSE; | |
883 | fXyq[2][indx] = -2; | |
884 | fnPads[cath]--; | |
885 | // xmax = dist[i]; // Bug? | |
886 | } | |
887 | else break; | |
888 | } | |
889 | delete [] dist; dist = 0; | |
890 | } // TMath::Abs(sum[0]-sum[1])... | |
891 | } // if (fnPads[0] && fnPads[1]) | |
892 | delete [] flags; flags = 0; | |
893 | } // if (i1 != i2) | |
894 | ||
895 | if (!sameSize) { nShown[0] += fnPads[0]; nShown[1] += fnPads[1]; } | |
896 | ||
897 | // Move released pads to the right | |
898 | Int_t beg = 0, end = npad-1, padij; | |
899 | Double_t xyq; | |
900 | while (beg < end) { | |
901 | if (fXyq[2][beg] > 0) { beg++; continue; } | |
902 | for (Int_t j=end; j>beg; j--) { | |
903 | if (fXyq[2][j] < 0) continue; | |
904 | end = j - 1; | |
905 | for (Int_t j1=0; j1<2; j1++) { | |
906 | padij = fPadIJ[j1][beg]; | |
907 | fPadIJ[j1][beg] = fPadIJ[j1][j]; | |
908 | fPadIJ[j1][j] = padij; | |
909 | } | |
910 | for (Int_t j1=0; j1<6; j1++) { | |
911 | xyq = fXyq[j1][beg]; | |
912 | fXyq[j1][beg] = fXyq[j1][j]; | |
913 | fXyq[j1][j] = xyq; | |
914 | } | |
915 | break; | |
916 | } // for (Int_t j=end; | |
917 | beg++; | |
918 | } // while | |
919 | npad = fnPads[0] + fnPads[1]; | |
920 | if (npad > 500) { cout << " ***** Too large cluster. Give up. " << npad << endl; return kFALSE; } | |
921 | // Back up charge value | |
922 | for (Int_t j=0; j<npad; j++) fXyq[5][j] = fXyq[2][j]; | |
923 | ||
924 | return kTRUE; | |
925 | } | |
926 | ||
927 | //_____________________________________________________________________________ | |
928 | void AliMUONClusterFinderAZ::BuildPixArray() | |
929 | { | |
930 | // Build pixel array for MLEM method | |
931 | ||
932 | Int_t nPix=0, i1, i2; | |
933 | Float_t xy1[4], xy12[4]; | |
934 | AliMUONPixel *pixPtr=0; | |
935 | ||
936 | Int_t npad = fnPads[0] + fnPads[1]; | |
937 | ||
938 | // One cathode is empty | |
939 | i1 = fnPads[0]!=0 ? 0 : 1; | |
940 | i2 = fnPads[1]!=0 ? 1 : 0; | |
941 | ||
942 | // Build array of pixels on anode plane | |
943 | if (i1 == i2) { // one-cathode precluster | |
944 | for (Int_t j=0; j<npad; j++) { | |
945 | pixPtr = new AliMUONPixel(); | |
946 | for (Int_t i=0; i<2; i++) { | |
947 | pixPtr->SetCoord(i, fXyq[i][j]); // pixel coordinates | |
948 | pixPtr->SetSize(i, fXyq[i+3][j]); // pixel size | |
949 | } | |
950 | pixPtr->SetCharge(fXyq[2][j]); // charge | |
951 | fPixArray->Add((TObject*)pixPtr); | |
952 | nPix++; | |
953 | } | |
954 | } else { // two-cathode precluster | |
955 | for (Int_t i=0; i<npad; i++) { | |
956 | if (fPadIJ[0][i] != i1) continue; | |
957 | xy1[0] = fXyq[0][i] - fXyq[3][i]; | |
958 | xy1[1] = fXyq[0][i] + fXyq[3][i]; | |
959 | xy1[2] = fXyq[1][i] - fXyq[4][i]; | |
960 | xy1[3] = fXyq[1][i] + fXyq[4][i]; | |
961 | for (Int_t j=0; j<npad; j++) { | |
962 | if (fPadIJ[0][j] != i2) continue; | |
963 | if (!Overlap(xy1, j, xy12, 1)) continue; | |
964 | pixPtr = new AliMUONPixel(); | |
965 | for (Int_t k=0; k<2; k++) { | |
966 | pixPtr->SetCoord(k, (xy12[2*k]+xy12[2*k+1])/2); // pixel coordinates | |
967 | pixPtr->SetSize(k, xy12[2*k+1]-pixPtr->Coord(k)); // size | |
968 | } | |
969 | pixPtr->SetCharge(TMath::Min (fXyq[2][i],fXyq[2][j])); //charge | |
970 | fPixArray->Add((TObject*)pixPtr); | |
971 | nPix++; | |
972 | } // for (Int_t j=0; | |
973 | } // for (Int_t i=0; | |
974 | } // else | |
975 | ||
976 | Float_t wxmin=999, wymin=999; | |
977 | for (Int_t i=0; i<npad; i++) { | |
978 | if (fPadIJ[0][i] == i1) wymin = TMath::Min (wymin,fXyq[4][i]); | |
979 | if (fPadIJ[0][i] == i2) wxmin = TMath::Min (wxmin,fXyq[3][i]); | |
980 | } | |
981 | cout << wxmin << " " << wymin << endl; | |
982 | ||
983 | // Check if small pixel X-size | |
984 | AjustPixel(wxmin, 0); | |
985 | // Check if small pixel Y-size | |
986 | AjustPixel(wymin, 1); | |
987 | // Check if large pixel size | |
988 | AjustPixel(wxmin, wymin); | |
989 | ||
990 | // Remove discarded pixels | |
991 | for (Int_t i=0; i<nPix; i++) { | |
992 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
993 | //pixPtr->Print(); | |
994 | if (pixPtr->Charge() < 1) { fPixArray->RemoveAt(i); delete pixPtr; }// discarded pixel | |
995 | } | |
996 | fPixArray->Compress(); | |
997 | nPix = fPixArray->GetEntriesFast(); | |
998 | ||
999 | if (nPix > npad) { | |
1000 | cout << nPix << endl; | |
1001 | // Too many pixels - sort and remove pixels with the lowest signal | |
1002 | fPixArray->Sort(); | |
1003 | for (Int_t i=npad; i<nPix; i++) { | |
1004 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1005 | //pixPtr->Print(); | |
1006 | fPixArray->RemoveAt(i); | |
1007 | delete pixPtr; | |
1008 | } | |
1009 | nPix = npad; | |
1010 | } // if (nPix > npad) | |
1011 | ||
1012 | // Set pixel charges to the same value (for MLEM) | |
1013 | for (Int_t i=0; i<nPix; i++) { | |
1014 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1015 | //pixPtr->SetCharge(10); | |
1016 | cout << i+1 << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << " " << pixPtr->Size(0) << " " << pixPtr->Size(1) << endl; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | //_____________________________________________________________________________ | |
1021 | void AliMUONClusterFinderAZ::AjustPixel(Float_t width, Int_t ixy) | |
1022 | { | |
1023 | // Check if some pixels have small size (ajust if necessary) | |
1024 | ||
1025 | AliMUONPixel *pixPtr, *pixPtr1 = 0; | |
1026 | Int_t ixy1 = TMath::Even(ixy); | |
1027 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1028 | ||
1029 | for (Int_t i=0; i<nPix; i++) { | |
1030 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1031 | if (pixPtr->Charge() < 1) continue; // discarded pixel | |
1032 | if (pixPtr->Size(ixy)-width < -1.e-4) { | |
1033 | // try to merge | |
1034 | cout << " Small X or Y: " << ixy << " " << pixPtr->Size(ixy) << " " << width << " " << pixPtr->Coord(0) << " " << pixPtr->Coord(1) << endl; | |
1035 | for (Int_t j=i+1; j<nPix; j++) { | |
1036 | pixPtr1 = (AliMUONPixel*) fPixArray->UncheckedAt(j); | |
1037 | if (pixPtr1->Charge() < 1) continue; // discarded pixel | |
1038 | if (TMath::Abs(pixPtr1->Size(ixy)-width) < 1.e-4) continue; // right size | |
1039 | if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > 1.e-4) continue; // different rows/columns | |
1040 | if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width) { | |
1041 | // merge | |
1042 | pixPtr->SetSize(ixy, width); | |
1043 | pixPtr->SetCoord(ixy, (pixPtr->Coord(ixy)+pixPtr1->Coord(ixy))/2); | |
1044 | pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge())); | |
1045 | pixPtr1->SetCharge(0); | |
1046 | pixPtr1 = 0; | |
1047 | break; | |
1048 | } | |
1049 | } // for (Int_t j=i+1; | |
1050 | //if (!pixPtr1) { cout << " I am here!" << endl; pixPtr->SetSize(ixy, width); } // ??? | |
1051 | //else if (pixPtr1->Charge() > 0.5 || i == nPix-1) { | |
1052 | if (pixPtr1 || i == nPix-1) { | |
1053 | // edge pixel - just increase its size | |
1054 | cout << " Edge ..." << endl; | |
1055 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
1056 | // ???if (fPadIJ[0][j] != i1) continue; | |
1057 | if (TMath::Abs(pixPtr->Coord(ixy1)-fXyq[ixy1][j]) > 1.e-4) continue; | |
1058 | if (pixPtr->Coord(ixy) < fXyq[ixy][j]) | |
1059 | pixPtr->Shift(ixy, -pixPtr->Size(ixy)); | |
1060 | else pixPtr->Shift(ixy, pixPtr->Size(ixy)); | |
1061 | pixPtr->SetSize(ixy, width); | |
1062 | break; | |
1063 | } | |
1064 | } | |
1065 | } // if (pixPtr->Size(ixy)-width < -1.e-4) | |
1066 | } // for (Int_t i=0; i<nPix; | |
1067 | return; | |
1068 | } | |
1069 | ||
1070 | //_____________________________________________________________________________ | |
1071 | void AliMUONClusterFinderAZ::AjustPixel(Float_t wxmin, Float_t wymin) | |
1072 | { | |
1073 | // Check if some pixels have large size (ajust if necessary) | |
1074 | ||
1075 | Int_t nx, ny; | |
1076 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1077 | AliMUONPixel *pixPtr, *pixPtr1, pix; | |
1078 | ||
1079 | // Check if large pixel size | |
1080 | for (Int_t i=0; i<nPix; i++) { | |
1081 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1082 | if (pixPtr->Charge() < 1) continue; // discarded pixel | |
1083 | if (pixPtr->Size(0)-wxmin > 1.e-4 || pixPtr->Size(1)-wymin > 1.e-4) { | |
1084 | cout << " Different " << pixPtr->Size(0) << " " << wxmin << " " << pixPtr->Size(1) << " " << wymin << endl; | |
1085 | pix = *pixPtr; | |
1086 | nx = TMath::Nint (pix.Size(0)/wxmin); | |
1087 | ny = TMath::Nint (pix.Size(1)/wymin); | |
1088 | pix.Shift(0, -pix.Size(0)-wxmin); | |
1089 | pix.Shift(1, -pix.Size(1)-wymin); | |
1090 | pix.SetSize(0, wxmin); | |
1091 | pix.SetSize(1, wymin); | |
1092 | for (Int_t ii=0; ii<nx; ii++) { | |
1093 | pix.Shift(0, wxmin*2); | |
1094 | for (Int_t jj=0; jj<ny; jj++) { | |
1095 | pix.Shift(1, wymin*2); | |
1096 | pixPtr1 = new AliMUONPixel(pix); | |
1097 | fPixArray->Add((TObject*)pixPtr1); | |
1098 | } | |
1099 | } | |
1100 | pixPtr->SetCharge(0); | |
1101 | } | |
1102 | } // for (Int_t i=0; i<nPix; | |
1103 | return; | |
1104 | } | |
1105 | ||
1106 | //_____________________________________________________________________________ | |
1107 | Bool_t AliMUONClusterFinderAZ::MainLoop() | |
1108 | { | |
1109 | // Repeat MLEM algorithm until pixel size becomes sufficiently small | |
1110 | ||
1111 | TH2D *mlem; | |
1112 | ||
1113 | Int_t ix, iy; | |
1114 | //Int_t nn, xList[10], yList[10]; | |
1115 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1116 | Int_t npadTot = fnPads[0] + fnPads[1], npadOK = 0; | |
1117 | AliMUONPixel *pixPtr = 0; | |
1118 | Double_t *coef = 0, *probi = 0; | |
1119 | for (Int_t i=0; i<npadTot; i++) if (fPadIJ[1][i] == 0) npadOK++; | |
1120 | ||
1121 | while (1) { | |
1122 | ||
1123 | mlem = (TH2D*) gROOT->FindObject("mlem"); | |
1124 | if (mlem) mlem->Delete(); | |
1125 | // Calculate coefficients | |
1126 | cout << " nPix, npadTot, npadOK " << nPix << " " << npadTot << " " << npadOK << endl; | |
1127 | ||
1128 | // Calculate coefficients and pixel visibilities | |
1129 | coef = new Double_t [npadTot*nPix]; | |
1130 | probi = new Double_t [nPix]; | |
1131 | Int_t indx = 0, cath; | |
1132 | for (Int_t ipix=0; ipix<nPix; ipix++) { | |
1133 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1134 | probi[ipix] = 0; | |
1135 | for (Int_t j=0; j<npadTot; j++) { | |
1136 | if (fPadIJ[1][j] < 0) { coef[j*nPix+ipix] = 0; continue; } | |
1137 | cath = fPadIJ[0][j]; | |
1138 | fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy); | |
1139 | fSegmentation[cath]->SetPad(ix,iy); | |
1140 | /* | |
1141 | fSegmentation[cath]->Neighbours(ix,iy,&nn,xList,yList); | |
1142 | if (nn != 4) { | |
1143 | cout << nn << ": "; | |
1144 | for (Int_t i=0; i<nn; i++) {cout << xList[i] << " " << yList[i] << ", ";} | |
1145 | cout << endl; | |
1146 | } | |
1147 | */ | |
1148 | Double_t sum = 0; | |
1149 | fSegmentation[cath]->SetHit(pixPtr->Coord(0),pixPtr->Coord(1),fZpad); | |
1150 | sum += fResponse->IntXY(fSegmentation[cath]); | |
1151 | indx = j*nPix + ipix; | |
1152 | coef[indx] = sum; | |
1153 | probi[ipix] += coef[indx]; | |
1154 | //cout << j << " " << ipix << " " << coef[indx] << endl; | |
1155 | } // for (Int_t j=0; | |
1156 | //cout << " prob: " << probi[ipix] << endl; | |
1157 | if (probi[ipix] < 0.01) pixPtr->SetCharge(0); // "invisible" pixel | |
1158 | } // for (Int_t ipix=0; | |
1159 | ||
1160 | // MLEM algorithm | |
1161 | Mlem(coef, probi); | |
1162 | ||
cd747ddb | 1163 | Double_t xylim[4] = {999, 999, 999, 999}; |
0df3ca52 | 1164 | for (Int_t ipix=0; ipix<nPix; ipix++) { |
1165 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1166 | for (Int_t i=0; i<4; i++) | |
1167 | xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2)); | |
1168 | //cout << ipix+1; pixPtr->Print(); | |
1169 | } | |
1170 | for (Int_t i=0; i<4; i++) { | |
1171 | xylim[i] -= pixPtr->Size(i/2); cout << (i%2 ? -1 : 1)*xylim[i] << " "; } | |
1172 | cout << endl; | |
1173 | ||
1174 | // Ajust histogram to approximately the same limits as for the pads | |
1175 | // (for good presentation) | |
1176 | //* | |
1177 | Float_t xypads[4]; | |
1178 | if (fHist[0]) { | |
1179 | xypads[0] = fHist[0]->GetXaxis()->GetXmin(); | |
1180 | xypads[1] = -fHist[0]->GetXaxis()->GetXmax(); | |
1181 | xypads[2] = fHist[0]->GetYaxis()->GetXmin(); | |
1182 | xypads[3] = -fHist[0]->GetYaxis()->GetXmax(); | |
1183 | for (Int_t i=0; i<4; i++) { | |
1184 | while(1) { | |
1185 | if (xylim[i] < xypads[i]) break; | |
1186 | xylim[i] -= 2*pixPtr->Size(i/2); | |
1187 | } | |
1188 | } | |
1189 | } // if (fHist[0]) | |
1190 | //*/ | |
1191 | ||
1192 | Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2); | |
1193 | Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2); | |
1194 | mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]); | |
1195 | for (Int_t ipix=0; ipix<nPix; ipix++) { | |
1196 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1197 | mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge()); | |
1198 | } | |
1199 | //gPad->GetCanvas()->cd(3); | |
1200 | if (fDraw) { | |
1201 | ((TCanvas*)gROOT->FindObject("c2"))->cd(); | |
1202 | gPad->SetTheta(55); | |
1203 | gPad->SetPhi(30); | |
1204 | mlem->Draw("lego1Fb"); | |
1205 | gPad->Update(); | |
1206 | gets((char*)&ix); | |
1207 | } | |
1208 | ||
1209 | // Check if the total charge of pixels is too low | |
1210 | Double_t qTot = 0; | |
1211 | for (Int_t i=0; i<nPix; i++) { | |
1212 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1213 | qTot += pixPtr->Charge(); | |
1214 | } | |
1215 | if (qTot < 1.e-4 || npadOK < 3 && qTot < 50) { | |
1216 | delete [] coef; delete [] probi; coef = 0; probi = 0; | |
1217 | fPixArray->Delete(); | |
1218 | return kFALSE; | |
1219 | } | |
1220 | ||
1221 | // Plot data - expectation | |
1222 | /* | |
1223 | Double_t x, y, cont; | |
1224 | for (Int_t j=0; j<npadTot; j++) { | |
1225 | Double_t sum1 = 0; | |
1226 | for (Int_t i=0; i<nPix; i++) { | |
1227 | // Caculate expectation | |
1228 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1229 | sum1 += pixPtr->Charge()*coef[j*nPix+i]; | |
1230 | } | |
1231 | sum1 = TMath::Min (sum1,(Double_t)fResponse->MaxAdc()); | |
1232 | x = fXyq[0][j]; | |
1233 | y = fXyq[1][j]; | |
1234 | cath = fPadIJ[0][j]; | |
1235 | Int_t ihist = cath*2; | |
1236 | ix = fHist[ihist]->GetXaxis()->FindBin(x); | |
1237 | iy = fHist[ihist]->GetYaxis()->FindBin(y); | |
1238 | cont = fHist[ihist]->GetCellContent(ix,iy); | |
1239 | if (cont == 0 && fHist[ihist+1]) { | |
1240 | ihist += 1; | |
1241 | ix = fHist[ihist]->GetXaxis()->FindBin(x); | |
1242 | iy = fHist[ihist]->GetYaxis()->FindBin(y); | |
1243 | } | |
1244 | fHist[ihist]->SetBinContent(ix,iy,fXyq[2][j]-sum1); | |
1245 | } | |
1246 | ((TCanvas*)gROOT->FindObject("c1"))->cd(1); | |
1247 | //gPad->SetTheta(55); | |
1248 | //gPad->SetPhi(30); | |
1249 | //mlem->Draw("lego1"); | |
1250 | gPad->Modified(); | |
1251 | ((TCanvas*)gROOT->FindObject("c1"))->cd(2); | |
1252 | gPad->Modified(); | |
1253 | */ | |
1254 | ||
1255 | // Calculate position of the center-of-gravity around the maximum pixel | |
1256 | Double_t xyCOG[2]; | |
1257 | FindCOG(mlem, xyCOG); | |
1258 | ||
1259 | if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 && pixPtr->Size(0) > pixPtr->Size(1)) break; | |
1260 | //if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) >= 0.07 || pixPtr->Size(0) < pixPtr->Size(1)) { | |
1261 | // Sort pixels according to the charge | |
1262 | fPixArray->Sort(); | |
1263 | /* | |
1264 | for (Int_t i=0; i<nPix; i++) { | |
1265 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1266 | cout << i+1; pixPtr->Print(); | |
1267 | } | |
1268 | */ | |
1269 | Double_t pixMin = 0.01*((AliMUONPixel*)fPixArray->UncheckedAt(0))->Charge(); | |
1270 | pixMin = TMath::Min (pixMin,50.); | |
1271 | ||
1272 | // Decrease pixel size and shift pixels to make them centered at | |
1273 | // the maximum one | |
1274 | indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1; | |
1275 | Double_t width = 0, shift[2]={0}; | |
1276 | ix = 1; | |
1277 | for (Int_t i=0; i<4; i++) xylim[i] = 999; | |
1278 | Int_t nPix1 = nPix; nPix = 0; | |
1279 | for (Int_t ipix=0; ipix<nPix1; ipix++) { | |
1280 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1281 | if (nPix >= npadOK) { // too many pixels already | |
1282 | fPixArray->RemoveAt(ipix); | |
1283 | delete pixPtr; | |
1284 | continue; | |
1285 | } | |
1286 | if (pixPtr->Charge() < pixMin) { // low charge | |
1287 | fPixArray->RemoveAt(ipix); | |
1288 | delete pixPtr; | |
1289 | continue; | |
1290 | } | |
1291 | for (Int_t i=0; i<2; i++) { | |
1292 | if (!i) { | |
1293 | pixPtr->SetCharge(10); | |
1294 | pixPtr->SetSize(indx, pixPtr->Size(indx)/2); | |
1295 | width = -pixPtr->Size(indx); | |
1296 | pixPtr->Shift(indx, width); | |
1297 | // Shift pixel position | |
1298 | if (ix) { | |
1299 | ix = 0; | |
1300 | for (Int_t j=0; j<2; j++) { | |
1301 | shift[j] = pixPtr->Coord(j) - xyCOG[j]; | |
1302 | shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2; | |
1303 | } | |
1304 | //cout << ipix << " " << i << " " << shift[0] << " " << shift[1] << endl; | |
1305 | } // if (ix) | |
1306 | pixPtr->Shift(0, -shift[0]); | |
1307 | pixPtr->Shift(1, -shift[1]); | |
1308 | } else { | |
1309 | pixPtr = new AliMUONPixel(*pixPtr); | |
1310 | pixPtr->Shift(indx, -2*width); | |
1311 | fPixArray->Add((TObject*)pixPtr); | |
1312 | } // else | |
1313 | //pixPtr->Print(); | |
1314 | for (Int_t i=0; i<4; i++) | |
1315 | xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2)); | |
1316 | } // for (Int_t i=0; i<2; | |
1317 | nPix += 2; | |
1318 | } // for (Int_t ipix=0; | |
1319 | ||
1320 | fPixArray->Compress(); | |
1321 | nPix = fPixArray->GetEntriesFast(); | |
1322 | ||
1323 | // Remove excessive pixels | |
1324 | if (nPix > npadOK) { | |
1325 | for (Int_t ipix=npadOK; ipix<nPix; ipix++) { | |
1326 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1327 | fPixArray->RemoveAt(ipix); | |
1328 | delete pixPtr; | |
1329 | } | |
1330 | } else { | |
1331 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(0); | |
1332 | // add pixels if the maximum is at the limit of pixel area | |
1333 | // start from Y-direction | |
1334 | Int_t j = 0; | |
1335 | for (Int_t i=3; i>-1; i--) { | |
1336 | if (nPix < npadOK && | |
1337 | TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2)) { | |
1338 | pixPtr = new AliMUONPixel(*pixPtr); | |
1339 | pixPtr->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2)); | |
1340 | j = TMath::Even (i/2); | |
1341 | pixPtr->SetCoord(j, xyCOG[j]); | |
1342 | fPixArray->Add((TObject*)pixPtr); | |
1343 | nPix++; | |
1344 | } | |
1345 | } | |
1346 | } // else | |
1347 | ||
1348 | fPixArray->Compress(); | |
1349 | nPix = fPixArray->GetEntriesFast(); | |
1350 | delete [] coef; delete [] probi; coef = 0; probi = 0; | |
1351 | } // while (1) | |
1352 | ||
1353 | // remove pixels with low signal or low visibility | |
1354 | // Cuts are empirical !!! | |
1355 | Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.); | |
1356 | thresh = TMath::Min (thresh,50.); | |
1357 | Double_t cmax = -1, charge = 0; | |
1358 | for (Int_t i=0; i<nPix; i++) cmax = TMath::Max (cmax,probi[i]); | |
1359 | // Mark pixels which should be removed | |
1360 | for (Int_t i=0; i<nPix; i++) { | |
1361 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1362 | charge = pixPtr->Charge(); | |
1363 | if (charge < thresh) pixPtr->SetCharge(-charge); | |
1364 | else if (cmax > 1.91) { | |
1365 | if (probi[i] < 1.9) pixPtr->SetCharge(-charge); | |
1366 | } | |
1367 | else if (probi[i] < cmax*0.9) pixPtr->SetCharge(-charge); | |
1368 | } | |
1369 | // Move charge of removed pixels to their nearest neighbour (to keep total charge the same) | |
1370 | Int_t near = 0; | |
1371 | for (Int_t i=0; i<nPix; i++) { | |
1372 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1373 | charge = pixPtr->Charge(); | |
1374 | if (charge > 0) continue; | |
1375 | near = FindNearest(pixPtr); | |
1376 | pixPtr->SetCharge(0); | |
1377 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(near); | |
1378 | pixPtr->SetCharge(pixPtr->Charge() - charge); | |
1379 | } | |
1380 | // Update histogram | |
1381 | for (Int_t i=0; i<nPix; i++) { | |
1382 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1383 | ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0)); | |
1384 | iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1)); | |
1385 | mlem->SetBinContent(ix, iy, pixPtr->Charge()); | |
1386 | } | |
1387 | if (fDraw) { | |
1388 | ((TCanvas*)gROOT->FindObject("c2"))->cd(); | |
1389 | gPad->SetTheta(55); | |
1390 | gPad->SetPhi(30); | |
1391 | mlem->Draw("lego1Fb"); | |
1392 | gPad->Update(); | |
1393 | } | |
1394 | ||
1395 | fxyMu[0][6] = fxyMu[1][6] = 9999; | |
1396 | // Try to split into clusters | |
1397 | Bool_t ok = kTRUE; | |
1398 | if (mlem->GetSum() < 1) ok = kFALSE; | |
1399 | else Split(mlem, coef); | |
1400 | delete [] coef; delete [] probi; coef = 0; probi = 0; | |
1401 | fPixArray->Delete(); | |
1402 | return ok; | |
1403 | } | |
1404 | ||
1405 | //_____________________________________________________________________________ | |
1406 | void AliMUONClusterFinderAZ::Mlem(Double_t *coef, Double_t *probi) | |
1407 | { | |
1408 | // Use MLEM to find pixel charges | |
1409 | ||
1410 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1411 | Int_t npad = fnPads[0] + fnPads[1]; | |
1412 | Double_t *probi1 = new Double_t [nPix]; | |
1413 | Int_t indx, indx1; | |
1414 | AliMUONPixel *pixPtr; | |
1415 | ||
1416 | for (Int_t iter=0; iter<15; iter++) { | |
1417 | // Do iterations | |
1418 | for (Int_t ipix=0; ipix<nPix; ipix++) { | |
1419 | // Correct each pixel | |
1420 | if (probi[ipix] < 0.01) continue; // skip "invisible" pixel | |
1421 | Double_t sum = 0; | |
1422 | probi1[ipix] = probi[ipix]; | |
1423 | for (Int_t j=0; j<npad; j++) { | |
1424 | if (fPadIJ[1][j] < 0) continue; | |
1425 | Double_t sum1 = 0; | |
1426 | indx1 = j*nPix; | |
1427 | indx = indx1 + ipix; | |
1428 | for (Int_t i=0; i<nPix; i++) { | |
1429 | // Caculate expectation | |
1430 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1431 | sum1 += pixPtr->Charge()*coef[indx1+i]; | |
1432 | } // for (Int_t i=0; | |
1433 | if (fXyq[2][j] > fResponse->MaxAdc()-1 && sum1 > fResponse->MaxAdc()) { probi1[ipix] -= coef[indx]; continue; } // correct for pad charge overflows | |
1434 | //cout << sum1 << " " << fXyq[2][j] << " " << coef[j*nPix+ipix] << endl; | |
1435 | if (coef[indx] > 1.e-6) sum += fXyq[2][j]*coef[indx]/sum1; | |
1436 | } // for (Int_t j=0; | |
1437 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
1438 | if (probi1[ipix] > 1.e-6) pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]); | |
1439 | } // for (Int_t ipix=0; | |
1440 | } // for (Int_t iter=0; | |
1441 | delete [] probi1; | |
1442 | return; | |
1443 | } | |
1444 | ||
1445 | //_____________________________________________________________________________ | |
1446 | void AliMUONClusterFinderAZ::FindCOG(TH2D *mlem, Double_t *xyc) | |
1447 | { | |
1448 | // Calculate position of the center-of-gravity around the maximum pixel | |
1449 | ||
1450 | Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0; | |
1451 | Int_t i1 = -9, j1 = -9; | |
1452 | mlem->GetMaximumBin(ixmax,iymax,ix); | |
1453 | Int_t nx = mlem->GetNbinsX(); | |
1454 | Int_t ny = mlem->GetNbinsY(); | |
1455 | Double_t thresh = mlem->GetMaximum()/10; | |
1456 | Double_t x, y, cont, xq=0, yq=0, qq=0; | |
1457 | ||
1458 | for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) { | |
1459 | y = mlem->GetYaxis()->GetBinCenter(i); | |
1460 | for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) { | |
1461 | cont = mlem->GetCellContent(j,i); | |
1462 | if (cont < thresh) continue; | |
1463 | if (i != i1) {i1 = i; nsumy++;} | |
1464 | if (j != j1) {j1 = j; nsumx++;} | |
1465 | x = mlem->GetXaxis()->GetBinCenter(j); | |
1466 | xq += x*cont; | |
1467 | yq += y*cont; | |
1468 | qq += cont; | |
1469 | nsum++; | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | Double_t cmax = 0; | |
1474 | Int_t i2 = 0, j2 = 0; | |
1475 | x = y = 0; | |
1476 | if (nsumy == 1) { | |
1477 | // one bin in Y - add one more (with the largest signal) | |
1478 | for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) { | |
1479 | if (i == iymax) continue; | |
1480 | for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) { | |
1481 | cont = mlem->GetCellContent(j,i); | |
1482 | if (cont > cmax) { | |
1483 | cmax = cont; | |
1484 | x = mlem->GetXaxis()->GetBinCenter(j); | |
1485 | y = mlem->GetYaxis()->GetBinCenter(i); | |
1486 | i2 = i; | |
1487 | j2 = j; | |
1488 | } | |
1489 | } | |
1490 | } | |
1491 | xq += x*cmax; | |
1492 | yq += y*cmax; | |
1493 | qq += cmax; | |
1494 | if (i2 != i1) nsumy++; | |
1495 | if (j2 != j1) nsumx++; | |
1496 | nsum++; | |
1497 | } // if (nsumy == 1) | |
1498 | ||
1499 | if (nsumx == 1) { | |
1500 | // one bin in X - add one more (with the largest signal) | |
1501 | cmax = x = y = 0; | |
1502 | for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) { | |
1503 | if (j == ixmax) continue; | |
1504 | for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) { | |
1505 | cont = mlem->GetCellContent(j,i); | |
1506 | if (cont > cmax) { | |
1507 | cmax = cont; | |
1508 | x = mlem->GetXaxis()->GetBinCenter(j); | |
1509 | y = mlem->GetYaxis()->GetBinCenter(i); | |
1510 | i2 = i; | |
1511 | j2 = j; | |
1512 | } | |
1513 | } | |
1514 | } | |
1515 | xq += x*cmax; | |
1516 | yq += y*cmax; | |
1517 | qq += cmax; | |
1518 | if (i2 != i1) nsumy++; | |
1519 | if (j2 != j1) nsumx++; | |
1520 | nsum++; | |
1521 | } // if (nsumx == 1) | |
1522 | ||
1523 | xyc[0] = xq/qq; xyc[1] = yq/qq; | |
1524 | cout << xyc[0] << " " << xyc[1] << " " << qq << " " << nsum << " " << nsumx << " " << nsumy << endl; | |
1525 | return; | |
1526 | } | |
1527 | ||
1528 | //_____________________________________________________________________________ | |
1529 | Int_t AliMUONClusterFinderAZ::FindNearest(AliMUONPixel *pixPtr0) | |
1530 | { | |
1531 | // Find the pixel nearest to the given one | |
1532 | // (algorithm may be not very efficient) | |
1533 | ||
1534 | Int_t nPix = fPixArray->GetEntriesFast(), imin = 0; | |
1535 | Double_t rmin = 99999, dx = 0, dy = 0, r = 0; | |
1536 | Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1); | |
1537 | AliMUONPixel *pixPtr; | |
1538 | ||
1539 | for (Int_t i=0; i<nPix; i++) { | |
1540 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1541 | if (pixPtr->Charge() < 0.5) continue; | |
1542 | dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0); | |
1543 | dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1); | |
1544 | r = dx *dx + dy * dy; | |
1545 | if (r < rmin) { rmin = r; imin = i; } | |
1546 | } | |
1547 | return imin; | |
1548 | } | |
1549 | ||
1550 | //_____________________________________________________________________________ | |
1551 | void AliMUONClusterFinderAZ::Split(TH2D *mlem, Double_t *coef) | |
1552 | { | |
1553 | // The main steering function to work with clusters of pixels in anode | |
1554 | // plane (find clusters, decouple them from each other, merge them (if | |
1555 | // necessary), pick up coupled pads, call the fitting function) | |
1556 | ||
1557 | Int_t nx = mlem->GetNbinsX(); | |
1558 | Int_t ny = mlem->GetNbinsY(); | |
1559 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1560 | ||
1561 | Bool_t *used = new Bool_t[ny*nx]; | |
1562 | Double_t cont; | |
1563 | Int_t nclust = 0, indx, indx1; | |
1564 | ||
1565 | for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE; | |
1566 | ||
1567 | TObjArray *clusters[200]={0}; | |
1568 | TObjArray *pix; | |
1569 | ||
1570 | // Find clusters of histogram bins (easier to work in 2-D space) | |
1571 | for (Int_t i=1; i<=ny; i++) { | |
1572 | for (Int_t j=1; j<=nx; j++) { | |
1573 | indx = (i-1)*nx + j - 1; | |
1574 | if (used[indx]) continue; | |
1575 | cont = mlem->GetCellContent(j,i); | |
1576 | if (cont < 0.5) continue; | |
1577 | pix = new TObjArray(20); | |
1578 | used[indx] = 1; | |
1579 | pix->Add(BinToPix(mlem,j,i)); | |
1580 | AddBin(mlem, i, j, 0, used, pix); // recursive call | |
1581 | clusters[nclust++] = pix; | |
1582 | if (nclust > 200) { cout << " Too many clusters " << endl; ::exit(0); } | |
1583 | } // for (Int_t j=1; j<=nx; j++) { | |
1584 | } // for (Int_t i=1; i<=ny; | |
1585 | cout << nclust << endl; | |
1586 | delete [] used; used = 0; | |
1587 | ||
1588 | // Compute couplings between clusters and clusters to pads | |
1589 | Int_t npad = fnPads[0] + fnPads[1]; | |
1590 | ||
1591 | // Exclude pads with overflows | |
1592 | for (Int_t j=0; j<npad; j++) { | |
1593 | if (fXyq[2][j] > fResponse->MaxAdc()-1) fPadIJ[1][j] = -9; | |
1594 | else fPadIJ[1][j] = 0; | |
1595 | } | |
1596 | ||
1597 | // Compute couplings of clusters to pads | |
c1aed84f | 1598 | TMatrixD *aijclupad = new TMatrixD(nclust,npad); |
1599 | *aijclupad = 0; | |
0df3ca52 | 1600 | Int_t npxclu; |
1601 | for (Int_t iclust=0; iclust<nclust; iclust++) { | |
1602 | pix = clusters[iclust]; | |
1603 | npxclu = pix->GetEntriesFast(); | |
1604 | for (Int_t i=0; i<npxclu; i++) { | |
1605 | indx = fPixArray->IndexOf(pix->UncheckedAt(i)); | |
1606 | for (Int_t j=0; j<npad; j++) { | |
1607 | // Exclude overflows | |
1608 | if (fPadIJ[1][j] < 0) continue; | |
343146bf | 1609 | if (coef[j*nPix+indx] < fgkCouplMin) continue; |
c1aed84f | 1610 | (*aijclupad)(iclust,j) += coef[j*nPix+indx]; |
0df3ca52 | 1611 | } |
1612 | } | |
1613 | } | |
1614 | // Compute couplings between clusters | |
c1aed84f | 1615 | TMatrixD *aijcluclu = new TMatrixD(nclust,nclust); |
1616 | *aijcluclu = 0; | |
0df3ca52 | 1617 | for (Int_t iclust=0; iclust<nclust; iclust++) { |
1618 | for (Int_t j=0; j<npad; j++) { | |
1619 | // Exclude overflows | |
1620 | if (fPadIJ[1][j] < 0) continue; | |
343146bf | 1621 | if ((*aijclupad)(iclust,j) < fgkCouplMin) continue; |
0df3ca52 | 1622 | for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) { |
343146bf | 1623 | if ((*aijclupad)(iclust1,j) < fgkCouplMin) continue; |
c1aed84f | 1624 | (*aijcluclu)(iclust,iclust1) += |
1625 | TMath::Sqrt ((*aijclupad)(iclust,j)*(*aijclupad)(iclust1,j)); | |
0df3ca52 | 1626 | } |
1627 | } | |
1628 | } | |
1629 | for (Int_t iclust=0; iclust<nclust; iclust++) { | |
1630 | for (Int_t iclust1=iclust+1; iclust1<nclust; iclust1++) { | |
c1aed84f | 1631 | (*aijcluclu)(iclust1,iclust) = (*aijcluclu)(iclust,iclust1); |
0df3ca52 | 1632 | } |
1633 | } | |
1634 | ||
c1aed84f | 1635 | if (nclust > 1) aijcluclu->Print(); |
0df3ca52 | 1636 | |
1637 | // Find groups of coupled clusters | |
1638 | used = new Bool_t[nclust]; | |
1639 | for (Int_t i=0; i<nclust; i++) used[i] = kFALSE; | |
1640 | Int_t *clustNumb = new Int_t[nclust]; | |
1641 | Int_t nCoupled, nForFit, minGroup[3], clustFit[3], nfit = 0; | |
1642 | Double_t parOk[8]; | |
1643 | ||
1644 | for (Int_t igroup=0; igroup<nclust; igroup++) { | |
1645 | if (used[igroup]) continue; | |
1646 | used[igroup] = kTRUE; | |
1647 | clustNumb[0] = igroup; | |
1648 | nCoupled = 1; | |
1649 | // Find group of coupled clusters | |
c1aed84f | 1650 | AddCluster(igroup, nclust, aijcluclu, used, clustNumb, nCoupled); // recursive |
0df3ca52 | 1651 | cout << " nCoupled: " << nCoupled << endl; |
1652 | for (Int_t i=0; i<nCoupled; i++) cout << clustNumb[i] << " "; cout << endl; | |
1653 | ||
1654 | while (nCoupled > 0) { | |
1655 | ||
1656 | if (nCoupled < 4) { | |
1657 | nForFit = nCoupled; | |
1658 | for (Int_t i=0; i<nCoupled; i++) clustFit[i] = clustNumb[i]; | |
1659 | } else { | |
1660 | // Too many coupled clusters to fit - try to decouple them | |
1661 | // Find the lowest coupling of 1, 2, min(3,nLinks/2) pixels with | |
1662 | // all the others in the group | |
1663 | for (Int_t j=0; j<3; j++) minGroup[j] = -1; | |
c1aed84f | 1664 | Double_t coupl = MinGroupCoupl(nCoupled, clustNumb, aijcluclu, minGroup); |
0df3ca52 | 1665 | |
1666 | // Flag clusters for fit | |
1667 | nForFit = 0; | |
1668 | while (minGroup[nForFit] >= 0 && nForFit < 3) { | |
1669 | cout << clustNumb[minGroup[nForFit]] << " "; | |
1670 | clustFit[nForFit] = clustNumb[minGroup[nForFit]]; | |
1671 | clustNumb[minGroup[nForFit]] -= 999; | |
1672 | nForFit++; | |
1673 | } | |
1674 | cout << nForFit << " " << coupl << endl; | |
1675 | } // else | |
1676 | ||
1677 | // Select pads for fit. | |
c1aed84f | 1678 | if (SelectPad(nCoupled, nForFit, clustNumb, clustFit, aijclupad) < 3 && nCoupled > 1) { |
0df3ca52 | 1679 | // Deselect pads |
1680 | for (Int_t j=0; j<npad; j++) if (TMath::Abs(fPadIJ[1][j]) == 1) fPadIJ[1][j] = 0; | |
1681 | // Merge the failed cluster candidates (with too few pads to fit) with | |
1682 | // the one with the strongest coupling | |
c1aed84f | 1683 | Merge(nForFit, nCoupled, clustNumb, clustFit, clusters, aijcluclu, aijclupad); |
0df3ca52 | 1684 | } else { |
1685 | // Do the fit | |
1686 | nfit = Fit(nForFit, clustFit, clusters, parOk); | |
1687 | } | |
1688 | ||
1689 | // Subtract the fitted charges from pads with strong coupling and/or | |
1690 | // return pads for further use | |
1691 | UpdatePads(nfit, parOk); | |
1692 | ||
1693 | // Mark used pads | |
1694 | for (Int_t j=0; j<npad; j++) {if (fPadIJ[1][j] == 1) fPadIJ[1][j] = -1;} | |
1695 | ||
1696 | // Sort the clusters (move to the right the used ones) | |
1697 | Int_t beg = 0, end = nCoupled - 1; | |
1698 | while (beg < end) { | |
1699 | if (clustNumb[beg] >= 0) { beg++; continue; } | |
1700 | for (Int_t j=end; j>beg; j--) { | |
1701 | if (clustNumb[j] < 0) continue; | |
1702 | end = j - 1; | |
1703 | indx = clustNumb[beg]; | |
1704 | clustNumb[beg] = clustNumb[j]; | |
1705 | clustNumb[j] = indx; | |
1706 | break; | |
1707 | } | |
1708 | beg++; | |
1709 | } | |
1710 | ||
1711 | nCoupled -= nForFit; | |
1712 | if (nCoupled > 3) { | |
1713 | // Remove couplings of used clusters | |
1714 | for (Int_t iclust=nCoupled; iclust<nCoupled+nForFit; iclust++) { | |
1715 | indx = clustNumb[iclust] + 999; | |
1716 | for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) { | |
1717 | indx1 = clustNumb[iclust1]; | |
c1aed84f | 1718 | (*aijcluclu)(indx,indx1) = (*aijcluclu)(indx1,indx) = 0; |
0df3ca52 | 1719 | } |
1720 | } | |
1721 | ||
1722 | // Update the remaining clusters couplings (exclude couplings from | |
1723 | // the used pads) | |
1724 | for (Int_t j=0; j<npad; j++) { | |
1725 | if (fPadIJ[1][j] != -1) continue; | |
1726 | for (Int_t iclust=0; iclust<nCoupled; iclust++) { | |
1727 | indx = clustNumb[iclust]; | |
343146bf | 1728 | if ((*aijclupad)(indx,j) < fgkCouplMin) continue; |
0df3ca52 | 1729 | for (Int_t iclust1=iclust+1; iclust1<nCoupled; iclust1++) { |
1730 | indx1 = clustNumb[iclust1]; | |
343146bf | 1731 | if ((*aijclupad)(indx1,j) < fgkCouplMin) continue; |
0df3ca52 | 1732 | // Check this |
c1aed84f | 1733 | (*aijcluclu)(indx,indx1) -= |
1734 | TMath::Sqrt ((*aijclupad)(indx,j)*(*aijclupad)(indx1,j)); | |
1735 | (*aijcluclu)(indx1,indx) = (*aijcluclu)(indx,indx1); | |
0df3ca52 | 1736 | } |
1737 | } | |
1738 | fPadIJ[1][j] = -9; | |
1739 | } // for (Int_t j=0; j<npad; | |
1740 | } // if (nCoupled > 3) | |
1741 | } // while (nCoupled > 0) | |
1742 | } // for (Int_t igroup=0; igroup<nclust; | |
1743 | ||
c1aed84f | 1744 | //delete aij_clu; aij_clu = 0; delete aijclupad; aijclupad = 0; |
1745 | aijcluclu->Delete(); aijclupad->Delete(); | |
0df3ca52 | 1746 | for (Int_t iclust=0; iclust<nclust; iclust++) { |
1747 | pix = clusters[iclust]; | |
1748 | pix->Clear(); | |
1749 | delete pix; pix = 0; | |
1750 | } | |
1751 | delete [] clustNumb; clustNumb = 0; delete [] used; used = 0; | |
1752 | } | |
1753 | ||
1754 | //_____________________________________________________________________________ | |
1755 | void AliMUONClusterFinderAZ::AddBin(TH2D *mlem, Int_t ic, Int_t jc, Int_t mode, Bool_t *used, TObjArray *pix) | |
1756 | { | |
1757 | // Add a bin to the cluster | |
1758 | ||
1759 | Int_t nx = mlem->GetNbinsX(); | |
1760 | Int_t ny = mlem->GetNbinsY(); | |
1761 | Double_t cont1, cont = mlem->GetCellContent(jc,ic); | |
1762 | AliMUONPixel *pixPtr = 0; | |
1763 | ||
1764 | for (Int_t i=TMath::Max(ic-1,1); i<=TMath::Min(ic+1,ny); i++) { | |
1765 | for (Int_t j=TMath::Max(jc-1,1); j<=TMath::Min(jc+1,nx); j++) { | |
1766 | if (i != ic && j != jc) continue; | |
1767 | if (used[(i-1)*nx+j-1]) continue; | |
1768 | cont1 = mlem->GetCellContent(j,i); | |
1769 | if (mode && cont1 > cont) continue; | |
1770 | used[(i-1)*nx+j-1] = kTRUE; | |
1771 | if (cont1 < 0.5) continue; | |
1772 | if (pix) pix->Add(BinToPix(mlem,j,i)); | |
1773 | else { | |
1774 | pixPtr = new AliMUONPixel (mlem->GetXaxis()->GetBinCenter(j), | |
1775 | mlem->GetYaxis()->GetBinCenter(i), 0, 0, cont1); | |
1776 | fPixArray->Add((TObject*)pixPtr); | |
1777 | } | |
1778 | AddBin(mlem, i, j, mode, used, pix); // recursive call | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | //_____________________________________________________________________________ | |
1784 | TObject* AliMUONClusterFinderAZ::BinToPix(TH2D *mlem, Int_t jc, Int_t ic) | |
1785 | { | |
1786 | // Translate histogram bin to pixel | |
1787 | ||
1788 | Double_t yc = mlem->GetYaxis()->GetBinCenter(ic); | |
1789 | Double_t xc = mlem->GetXaxis()->GetBinCenter(jc); | |
1790 | ||
1791 | Int_t nPix = fPixArray->GetEntriesFast(); | |
1792 | AliMUONPixel *pixPtr; | |
1793 | ||
1794 | // Compare pixel and bin positions | |
1795 | for (Int_t i=0; i<nPix; i++) { | |
1796 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
1797 | if (pixPtr->Charge() < 0.5) continue; | |
1798 | if (TMath::Abs(pixPtr->Coord(0)-xc)<1.e-4 && TMath::Abs(pixPtr->Coord(1)-yc)<1.e-4) return (TObject*) pixPtr; | |
1799 | } | |
1800 | cout << " Something wrong ??? " << endl; | |
1801 | return NULL; | |
1802 | } | |
1803 | ||
1804 | //_____________________________________________________________________________ | |
c1aed84f | 1805 | void AliMUONClusterFinderAZ::AddCluster(Int_t ic, Int_t nclust, TMatrixD *aijcluclu, Bool_t *used, Int_t *clustNumb, Int_t &nCoupled) |
0df3ca52 | 1806 | { |
1807 | // Add a cluster to the group of coupled clusters | |
1808 | ||
1809 | for (Int_t i=0; i<nclust; i++) { | |
1810 | if (used[i]) continue; | |
343146bf | 1811 | if ((*aijcluclu)(i,ic) < fgkCouplMin) continue; |
0df3ca52 | 1812 | used[i] = kTRUE; |
1813 | clustNumb[nCoupled++] = i; | |
c1aed84f | 1814 | AddCluster(i, nclust, aijcluclu, used, clustNumb, nCoupled); |
0df3ca52 | 1815 | } |
1816 | } | |
1817 | ||
1818 | //_____________________________________________________________________________ | |
c1aed84f | 1819 | Double_t AliMUONClusterFinderAZ::MinGroupCoupl(Int_t nCoupled, Int_t *clustNumb, TMatrixD *aijcluclu, Int_t *minGroup) |
0df3ca52 | 1820 | { |
1821 | // Find group of clusters with minimum coupling to all the others | |
1822 | ||
1823 | Int_t i123max = TMath::Min(3,nCoupled/2); | |
1824 | Int_t indx, indx1, indx2, indx3, nTot = 0; | |
1825 | Double_t *coupl1 = 0, *coupl2 = 0, *coupl3 = 0; | |
1826 | ||
1827 | for (Int_t i123=1; i123<=i123max; i123++) { | |
1828 | ||
1829 | if (i123 == 1) { | |
1830 | coupl1 = new Double_t [nCoupled]; | |
1831 | for (Int_t i=0; i<nCoupled; i++) coupl1[i] = 0; | |
1832 | } | |
1833 | else if (i123 == 2) { | |
1834 | nTot = nCoupled*nCoupled; | |
1835 | coupl2 = new Double_t [nTot]; | |
1836 | for (Int_t i=0; i<nTot; i++) coupl2[i] = 9999; | |
1837 | } else { | |
1838 | nTot = nTot*nCoupled; | |
1839 | coupl3 = new Double_t [nTot]; | |
1840 | for (Int_t i=0; i<nTot; i++) coupl3[i] = 9999; | |
1841 | } // else | |
1842 | ||
1843 | for (Int_t i=0; i<nCoupled; i++) { | |
1844 | indx1 = clustNumb[i]; | |
1845 | for (Int_t j=i+1; j<nCoupled; j++) { | |
1846 | indx2 = clustNumb[j]; | |
1847 | if (i123 == 1) { | |
c1aed84f | 1848 | coupl1[i] += (*aijcluclu)(indx1,indx2); |
1849 | coupl1[j] += (*aijcluclu)(indx1,indx2); | |
0df3ca52 | 1850 | } |
1851 | else if (i123 == 2) { | |
1852 | indx = i*nCoupled + j; | |
1853 | coupl2[indx] = coupl1[i] + coupl1[j]; | |
c1aed84f | 1854 | coupl2[indx] -= 2 * ((*aijcluclu)(indx1,indx2)); |
0df3ca52 | 1855 | } else { |
1856 | for (Int_t k=j+1; k<nCoupled; k++) { | |
1857 | indx3 = clustNumb[k]; | |
1858 | indx = i*nCoupled*nCoupled + j*nCoupled + k; | |
1859 | coupl3[indx] = coupl2[i*nCoupled+j] + coupl1[k]; | |
c1aed84f | 1860 | coupl3[indx] -= 2 * ((*aijcluclu)(indx1,indx3)+(*aijcluclu)(indx2,indx3)); |
0df3ca52 | 1861 | } |
1862 | } // else | |
1863 | } // for (Int_t j=i+1; | |
1864 | } // for (Int_t i=0; | |
1865 | } // for (Int_t i123=1; | |
1866 | ||
1867 | // Find minimum coupling | |
1868 | Double_t couplMin = 9999; | |
1869 | Int_t locMin = 0; | |
1870 | ||
1871 | for (Int_t i123=1; i123<=i123max; i123++) { | |
1872 | if (i123 == 1) { | |
1873 | locMin = TMath::LocMin(nCoupled, coupl1); | |
1874 | couplMin = coupl1[locMin]; | |
1875 | minGroup[0] = locMin; | |
1876 | delete [] coupl1; coupl1 = 0; | |
1877 | } | |
1878 | else if (i123 == 2) { | |
1879 | locMin = TMath::LocMin(nCoupled*nCoupled, coupl2); | |
1880 | if (coupl2[locMin] < couplMin) { | |
1881 | couplMin = coupl2[locMin]; | |
1882 | minGroup[0] = locMin/nCoupled; | |
1883 | minGroup[1] = locMin%nCoupled; | |
1884 | } | |
1885 | delete [] coupl2; coupl2 = 0; | |
1886 | } else { | |
1887 | locMin = TMath::LocMin(nTot, coupl3); | |
1888 | if (coupl3[locMin] < couplMin) { | |
1889 | couplMin = coupl3[locMin]; | |
1890 | minGroup[0] = locMin/nCoupled/nCoupled; | |
1891 | minGroup[1] = locMin%(nCoupled*nCoupled)/nCoupled; | |
1892 | minGroup[2] = locMin%nCoupled; | |
1893 | } | |
1894 | delete [] coupl3; coupl3 = 0; | |
1895 | } // else | |
1896 | } // for (Int_t i123=1; | |
1897 | return couplMin; | |
1898 | } | |
1899 | ||
1900 | //_____________________________________________________________________________ | |
c1aed84f | 1901 | Int_t AliMUONClusterFinderAZ::SelectPad(Int_t nCoupled, Int_t nForFit, Int_t *clustNumb, Int_t *clustFit, TMatrixD *aijclupad) |
0df3ca52 | 1902 | { |
1903 | // Select pads for fit. If too many coupled clusters, find pads giving | |
1904 | // the strongest coupling with the rest of clusters and exclude them from the fit. | |
1905 | ||
1906 | Int_t npad = fnPads[0] + fnPads[1]; | |
c1aed84f | 1907 | Double_t *padpix = 0; |
0df3ca52 | 1908 | |
1909 | if (nCoupled > 3) { | |
c1aed84f | 1910 | padpix = new Double_t[npad]; |
1911 | for (Int_t i=0; i<npad; i++) padpix[i] = 0; | |
0df3ca52 | 1912 | } |
1913 | ||
1914 | Int_t nOK = 0, indx, indx1; | |
1915 | for (Int_t iclust=0; iclust<nForFit; iclust++) { | |
1916 | indx = clustFit[iclust]; | |
1917 | for (Int_t j=0; j<npad; j++) { | |
1918 | if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads | |
343146bf | 1919 | if ((*aijclupad)(indx,j) < fgkCouplMin) continue; |
0df3ca52 | 1920 | fPadIJ[1][j] = 1; // pad to be used in fit |
1921 | nOK++; | |
1922 | if (nCoupled > 3) { | |
1923 | // Check other clusters | |
1924 | for (Int_t iclust1=0; iclust1<nCoupled; iclust1++) { | |
1925 | indx1 = clustNumb[iclust1]; | |
1926 | if (indx1 < 0) continue; | |
343146bf | 1927 | if ((*aijclupad)(indx1,j) < fgkCouplMin) continue; |
c1aed84f | 1928 | padpix[j] += (*aijclupad)(indx1,j); |
0df3ca52 | 1929 | } |
1930 | } // if (nCoupled > 3) | |
1931 | } // for (Int_t j=0; j<npad; | |
1932 | } // for (Int_t iclust=0; iclust<nForFit | |
1933 | if (nCoupled < 4) return nOK; | |
1934 | ||
1935 | Double_t aaa = 0; | |
1936 | for (Int_t j=0; j<npad; j++) { | |
343146bf | 1937 | if (padpix[j] < fgkCouplMin) continue; |
c1aed84f | 1938 | cout << j << " " << padpix[j] << " "; |
0df3ca52 | 1939 | cout << fXyq[0][j] << " " << fXyq[1][j] << endl; |
c1aed84f | 1940 | aaa += padpix[j]; |
0df3ca52 | 1941 | fPadIJ[1][j] = -1; // exclude pads with strong coupling to the other clusters |
1942 | nOK--; | |
1943 | } | |
c1aed84f | 1944 | delete [] padpix; padpix = 0; |
0df3ca52 | 1945 | return nOK; |
1946 | } | |
1947 | ||
1948 | //_____________________________________________________________________________ | |
c1aed84f | 1949 | void AliMUONClusterFinderAZ::Merge(Int_t nForFit, Int_t nCoupled, Int_t *clustNumb, Int_t *clustFit, TObjArray **clusters, TMatrixD *aijcluclu, TMatrixD *aijclupad) |
0df3ca52 | 1950 | { |
1951 | // Merge the group of clusters with the one having the strongest coupling with them | |
1952 | ||
1953 | Int_t indx, indx1, npxclu, npxclu1, imax=0; | |
1954 | TObjArray *pix, *pix1; | |
1955 | Double_t couplMax; | |
1956 | ||
1957 | for (Int_t icl=0; icl<nForFit; icl++) { | |
1958 | indx = clustFit[icl]; | |
1959 | pix = clusters[indx]; | |
1960 | npxclu = pix->GetEntriesFast(); | |
1961 | couplMax = -1; | |
1962 | for (Int_t icl1=0; icl1<nCoupled; icl1++) { | |
1963 | indx1 = clustNumb[icl1]; | |
1964 | if (indx1 < 0) continue; | |
c1aed84f | 1965 | if ((*aijcluclu)(indx,indx1) > couplMax) { |
1966 | couplMax = (*aijcluclu)(indx,indx1); | |
0df3ca52 | 1967 | imax = indx1; |
1968 | } | |
1969 | } // for (Int_t icl1=0; | |
343146bf | 1970 | /*if (couplMax < fgkCouplMin) { |
0df3ca52 | 1971 | cout << " Oops " << couplMax << endl; |
c1aed84f | 1972 | aijcluclu->Print(); |
0df3ca52 | 1973 | cout << icl << " " << indx << " " << npxclu << " " << nLinks << endl; |
1974 | ::exit(0); | |
1975 | }*/ | |
1976 | // Add to it | |
1977 | pix1 = clusters[imax]; | |
1978 | npxclu1 = pix1->GetEntriesFast(); | |
1979 | // Add pixels | |
1980 | for (Int_t i=0; i<npxclu; i++) { pix1->Add(pix->UncheckedAt(i)); pix->RemoveAt(i); } | |
1981 | cout << " New number of pixels: " << npxclu1 << " " << pix1->GetEntriesFast() << endl; | |
1982 | //Add cluster-to-cluster couplings | |
c1aed84f | 1983 | //aijcluclu->Print(); |
0df3ca52 | 1984 | for (Int_t icl1=0; icl1<nCoupled; icl1++) { |
1985 | indx1 = clustNumb[icl1]; | |
1986 | if (indx1 < 0 || indx1 == imax) continue; | |
c1aed84f | 1987 | (*aijcluclu)(indx1,imax) += (*aijcluclu)(indx,indx1); |
1988 | (*aijcluclu)(imax,indx1) = (*aijcluclu)(indx1,imax); | |
0df3ca52 | 1989 | } |
c1aed84f | 1990 | (*aijcluclu)(indx,imax) = (*aijcluclu)(imax,indx) = 0; |
1991 | //aijcluclu->Print(); | |
0df3ca52 | 1992 | //Add cluster-to-pad couplings |
1993 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
1994 | if (fPadIJ[1][j] < 0) continue; // exclude overflows and used pads | |
c1aed84f | 1995 | (*aijclupad)(imax,j) += (*aijclupad)(indx,j); |
1996 | (*aijclupad)(indx,j) = 0; | |
0df3ca52 | 1997 | } |
1998 | } // for (Int_t icl=0; icl<nForFit; | |
1999 | } | |
2000 | ||
2001 | //_____________________________________________________________________________ | |
2002 | Int_t AliMUONClusterFinderAZ::Fit(Int_t nfit, Int_t *clustFit, TObjArray **clusters, Double_t *parOk) | |
2003 | { | |
2004 | // Find selected clusters to selected pad charges | |
2005 | ||
2006 | TH2D *mlem = (TH2D*) gROOT->FindObject("mlem"); | |
2007 | //Int_t nx = mlem->GetNbinsX(); | |
2008 | //Int_t ny = mlem->GetNbinsY(); | |
2009 | Double_t xmin = mlem->GetXaxis()->GetXmin() - mlem->GetXaxis()->GetBinWidth(1); | |
2010 | Double_t xmax = mlem->GetXaxis()->GetXmax() + mlem->GetXaxis()->GetBinWidth(1); | |
2011 | Double_t ymin = mlem->GetYaxis()->GetXmin() - mlem->GetYaxis()->GetBinWidth(1); | |
2012 | Double_t ymax = mlem->GetYaxis()->GetXmax() + mlem->GetYaxis()->GetBinWidth(1); | |
2013 | //Double_t qmin = 0, qmax = 1; | |
2014 | Double_t step[3]={0.01,0.002,0.02}; | |
2015 | ||
2016 | Double_t cont, cmax = 0, xseed = 0, yseed = 0, errOk[8]; | |
2017 | TObjArray *pix; | |
2018 | Int_t npxclu; | |
2019 | ||
2020 | // Number of pads to use | |
2021 | Int_t npads = 0; | |
2022 | for (Int_t i=0; i<fnPads[0]+fnPads[1]; i++) {if (fPadIJ[1][i] == 1) npads++;} | |
2023 | for (Int_t i=0; i<nfit; i++) {cout << i+1 << " " << clustFit[i] << " ";} | |
2024 | cout << nfit << endl; | |
2025 | cout << " Number of pads to fit: " << npads << endl; | |
2026 | fNpar = 0; | |
2027 | fQtot = 0; | |
2028 | if (npads < 2) return 0; | |
2029 | ||
2030 | // Take cluster maxima as fitting seeds | |
2031 | AliMUONPixel *pixPtr; | |
2032 | Double_t xyseed[3][2], qseed[3]; | |
2033 | for (Int_t ifit=1; ifit<=nfit; ifit++) { | |
2034 | cmax = 0; | |
2035 | pix = clusters[clustFit[ifit-1]]; | |
2036 | npxclu = pix->GetEntriesFast(); | |
2037 | for (Int_t clu=0; clu<npxclu; clu++) { | |
2038 | pixPtr = (AliMUONPixel*) pix->UncheckedAt(clu); | |
2039 | cont = pixPtr->Charge(); | |
2040 | fQtot += cont; | |
2041 | if (cont > cmax) { | |
2042 | cmax = cont; | |
2043 | xseed = pixPtr->Coord(0); | |
2044 | yseed = pixPtr->Coord(1); | |
2045 | } | |
2046 | } | |
2047 | xyseed[ifit-1][0] = xseed; | |
2048 | xyseed[ifit-1][1] = yseed; | |
2049 | qseed[ifit-1] = cmax; | |
2050 | } // for (Int_t ifit=1; | |
2051 | ||
2052 | Int_t nDof, maxSeed[3]; | |
2053 | Double_t fmin, chi2o = 9999, chi2n; | |
2054 | ||
2055 | // Try to fit with one-track hypothesis, then 2-track. If chi2/dof is | |
2056 | // lower, try 3-track (if number of pads is sufficient). | |
2057 | ||
2058 | TMath::Sort(nfit, qseed, maxSeed, kTRUE); // in decreasing order | |
2059 | nfit = TMath::Min (nfit, (npads + 1) / 3); | |
2060 | ||
2061 | Double_t *gin = 0, func0, func1, param[8], param0[2][8], deriv[2][8], step0[8]; | |
2062 | Double_t shift[8], stepMax, derMax, parmin[8], parmax[8], func2[2], shift0; | |
2063 | Double_t delta[8], scMax, dder[8], estim, shiftSave = 0; | |
2064 | Int_t min, max, nCall = 0, memory[8] = {0}, nLoop, idMax = 0, iestMax = 0, nFail; | |
2065 | ||
2066 | for (Int_t iseed=0; iseed<nfit; iseed++) { | |
2067 | ||
2068 | for (Int_t j=0; j<3; j++) step0[fNpar+j] = shift[fNpar+j] = step[j]; | |
2069 | param[fNpar] = xyseed[maxSeed[iseed]][0]; | |
2070 | parmin[fNpar] = xmin; | |
2071 | parmax[fNpar++] = xmax; | |
2072 | param[fNpar] = xyseed[maxSeed[iseed]][1]; | |
2073 | parmin[fNpar] = ymin; | |
2074 | parmax[fNpar++] = ymax; | |
2075 | if (fNpar > 2) { | |
2076 | param[fNpar] = fNpar == 4 ? 0.5 : 0.3; | |
2077 | parmin[fNpar] = 0; | |
2078 | parmax[fNpar++] = 1; | |
2079 | } | |
2080 | ||
2081 | // Try new algorithm | |
2082 | min = nLoop = 1; stepMax = func2[1] = derMax = 999999; nFail = 0; | |
2083 | ||
2084 | while (1) { | |
2085 | max = !min; | |
2086 | fcn1(fNpar, gin, func0, param, 1); nCall++; | |
2087 | //cout << " Func: " << func0 << endl; | |
2088 | ||
2089 | func2[max] = func0; | |
2090 | for (Int_t j=0; j<fNpar; j++) { | |
2091 | param0[max][j] = param[j]; | |
2092 | delta[j] = step0[j]; | |
2093 | param[j] += delta[j] / 10; | |
2094 | if (j > 0) param[j-1] -= delta[j-1] / 10; | |
2095 | fcn1(fNpar, gin, func1, param, 1); nCall++; | |
2096 | deriv[max][j] = (func1 - func0) / delta[j] * 10; // first derivative | |
2097 | //cout << j << " " << deriv[max][j] << endl; | |
2098 | dder[j] = param0[0][j] != param0[1][j] ? (deriv[0][j] - deriv[1][j]) / | |
2099 | (param0[0][j] - param0[1][j]) : 0; // second derivative | |
2100 | } | |
2101 | param[fNpar-1] -= delta[fNpar-1] / 10; | |
2102 | if (nCall > 2000) ::exit(0); | |
2103 | ||
2104 | min = func2[0] < func2[1] ? 0 : 1; | |
2105 | nFail = min == max ? 0 : nFail + 1; | |
2106 | ||
2107 | stepMax = derMax = estim = 0; | |
2108 | for (Int_t j=0; j<fNpar; j++) { | |
2109 | // Estimated distance to minimum | |
2110 | shift0 = shift[j]; | |
2111 | if (nLoop == 1) shift[j] = TMath::Sign (step0[j], -deriv[max][j]); // first step | |
2112 | else if (TMath::Abs(deriv[0][j]) < 1.e-3 && TMath::Abs(deriv[1][j]) < 1.e-3) shift[j] = 0; | |
2113 | else if (deriv[min][j]*deriv[!min][j] > 0 && TMath::Abs(deriv[min][j]) > TMath::Abs(deriv[!min][j]) | |
2114 | || TMath::Abs(deriv[0][j]-deriv[1][j]) < 1.e-3) { | |
2115 | shift[j] = -TMath::Sign (shift[j], (func2[0]-func2[1]) * (param0[0][j]-param0[1][j])); | |
2116 | if (min == max) { | |
2117 | if (memory[j] > 1) { shift[j] *= 2; } //cout << " Memory " << memory[j] << " " << shift[j] << endl; } | |
2118 | memory[j]++; | |
2119 | } | |
2120 | } else { | |
2121 | shift[j] = -deriv[min][j] / dder[j]; | |
2122 | memory[j] = 0; | |
2123 | } | |
2124 | if (TMath::Abs(shift[j])/step0[j] > estim) { | |
2125 | estim = TMath::Abs(shift[j])/step0[j]; | |
2126 | iestMax = j; | |
2127 | } | |
2128 | ||
2129 | // Too big step | |
2130 | if (TMath::Abs(shift[j])/step0[j] > 10) shift[j] = TMath::Sign(10.,shift[j]) * step0[j]; // | |
2131 | ||
2132 | // Failed to improve minimum | |
2133 | if (min != max) { | |
2134 | memory[j] = 0; | |
2135 | param[j] = param0[min][j]; | |
2136 | if (TMath::Abs(shift[j]+shift0) > 0.1*step0[j]) shift[j] = (shift[j] + shift0) / 2; | |
2137 | else shift[j] /= -2; | |
2138 | } | |
2139 | ||
2140 | // Too big step | |
2141 | if (TMath::Abs(shift[j]*deriv[min][j]) > func2[min]) | |
2142 | shift[j] = TMath::Sign (func2[min]/deriv[min][j], shift[j]); | |
2143 | ||
2144 | // Introduce step relaxation factor | |
2145 | if (memory[j] < 3) { | |
2146 | scMax = 1 + 4 / TMath::Max(nLoop/2.,1.); | |
2147 | if (TMath::Abs(shift0) > 0 && TMath::Abs(shift[j]/shift0) > scMax) | |
2148 | shift[j] = TMath::Sign (shift0*scMax, shift[j]); | |
2149 | } | |
2150 | param[j] += shift[j]; | |
2151 | ||
2152 | //cout << " xxx " << j << " " << shift[j] << " " << param[j] << endl; | |
2153 | stepMax = TMath::Max (stepMax, TMath::Abs(shift[j]/step0[j])); | |
2154 | if (TMath::Abs(deriv[min][j]) > derMax) { | |
2155 | idMax = j; | |
2156 | derMax = TMath::Abs (deriv[min][j]); | |
2157 | } | |
2158 | } // for (Int_t j=0; j<fNpar; | |
2159 | //cout << max << " " << func2[min] << " " << derMax << " " << stepMax << " " << estim << " " << iestMax << " " << nCall << endl; | |
2160 | if (estim < 1 && derMax < 2 || nLoop > 100) break; // minimum was found | |
2161 | ||
2162 | nLoop++; | |
2163 | // Check for small step | |
2164 | if (shift[idMax] == 0) { shift[idMax] = step0[idMax]/10; param[idMax] += shift[idMax]; continue; } | |
2165 | if (!memory[idMax] && derMax > 0.5 && nLoop > 10) { | |
2166 | //cout << " ok " << deriv[min][idMax] << " " << deriv[!min][idMax] << " " << dder[idMax]*shift[idMax] << " " << shift[idMax] << endl; | |
2167 | if (dder[idMax] != 0 && TMath::Abs(deriv[min][idMax]/dder[idMax]/shift[idMax]) > 10) { | |
2168 | if (min == max) dder[idMax] = -dder[idMax]; | |
2169 | shift[idMax] = -deriv[min][idMax] / dder[idMax] / 10; | |
2170 | param[idMax] += shift[idMax]; | |
2171 | stepMax = TMath::Max (stepMax, TMath::Abs(shift[idMax])/step0[idMax]); | |
2172 | //cout << shift[idMax] << " " << param[idMax] << endl; | |
2173 | if (min == max) shiftSave = shift[idMax]; | |
2174 | } | |
2175 | if (nFail > 10) { | |
2176 | param[idMax] -= shift[idMax]; | |
2177 | shift[idMax] = 4 * shiftSave * (gRandom->Rndm(0) - 0.5); | |
2178 | param[idMax] += shift[idMax]; | |
2179 | //cout << shift[idMax] << endl; | |
2180 | } | |
2181 | } | |
2182 | } // while (1) | |
2183 | fmin = func2[min]; | |
2184 | ||
2185 | nDof = npads - fNpar; | |
2186 | chi2n = nDof ? fmin/nDof : 0; | |
2187 | ||
2188 | if (chi2n*1.2+1.e-6 > chi2o ) { fNpar -= 3; break; } | |
2189 | // Save parameters and errors | |
2190 | for (Int_t i=0; i<fNpar; i++) { | |
2191 | parOk[i] = param0[min][i]; | |
2192 | errOk[i] = fmin; | |
2193 | } | |
2194 | ||
2195 | cout << chi2o << " " << chi2n << endl; | |
2196 | chi2o = chi2n; | |
2197 | if (fmin < 0.1) break; // !!!??? | |
2198 | } // for (Int_t iseed=0; | |
2199 | ||
2200 | for (Int_t i=0; i<fNpar; i++) { | |
2201 | if (i == 4 || i == 7) continue; | |
2202 | cout << parOk[i] << " " << errOk[i] << endl; | |
2203 | } | |
2204 | nfit = (fNpar + 1) / 3; | |
2205 | Double_t rad; | |
2206 | Int_t indx, imax; | |
2207 | if (fReco) { | |
2208 | for (Int_t j=0; j<nfit; j++) { | |
2209 | indx = j<2 ? j*2 : j*2+1; | |
2210 | AddRawCluster (parOk[indx], parOk[indx+1], errOk[indx]); | |
2211 | } | |
2212 | return nfit; | |
2213 | } | |
2214 | for (Int_t i=0; i<fnMu; i++) { | |
2215 | cmax = fxyMu[i][6]; | |
2216 | for (Int_t j=0; j<nfit; j++) { | |
2217 | indx = j<2 ? j*2 : j*2+1; | |
2218 | rad = (fxyMu[i][0]-parOk[indx])*(fxyMu[i][0]-parOk[indx]) + | |
2219 | (fxyMu[i][1]-parOk[indx+1])*(fxyMu[i][1]-parOk[indx+1]); | |
2220 | if (rad < cmax) { | |
2221 | cmax = rad; | |
2222 | imax = indx; | |
2223 | fxyMu[i][6] = cmax; | |
2224 | fxyMu[i][2] = parOk[imax] - fxyMu[i][0]; | |
2225 | fxyMu[i][4] = parOk[imax+1] - fxyMu[i][1]; | |
2226 | fxyMu[i][3] = errOk[imax]; | |
2227 | fxyMu[i][5] = errOk[imax+1]; | |
2228 | } | |
2229 | } | |
2230 | } | |
2231 | return nfit; | |
2232 | } | |
2233 | ||
2234 | //_____________________________________________________________________________ | |
9e993f2a | 2235 | void AliMUONClusterFinderAZ::fcn1(Int_t & /*npar*/, Double_t * /*gin*/, Double_t &f, Double_t *par, Int_t /*iflag*/) |
0df3ca52 | 2236 | { |
2237 | // Fit for one track | |
2238 | AliMUONClusterFinderAZ& c = *(AliMUONClusterFinderAZ::fgClusterFinder); | |
2239 | ||
2240 | Int_t cath, ix, iy, indx, npads=0; | |
2241 | Double_t charge, delta, coef=0, chi2=0; | |
2242 | for (Int_t j=0; j<c.fnPads[0]+c.fnPads[1]; j++) { | |
2243 | if (c.fPadIJ[1][j] != 1) continue; | |
2244 | cath = c.fPadIJ[0][j]; | |
2245 | npads++; | |
2246 | c.fSegmentation[cath]->GetPadI(c.fXyq[0][j],c.fXyq[1][j],c.fZpad,ix,iy); | |
2247 | c.fSegmentation[cath]->SetPad(ix,iy); | |
2248 | charge = 0; | |
2249 | for (Int_t i=c.fNpar/3; i>=0; i--) { // sum over tracks | |
2250 | indx = i<2 ? 2*i : 2*i+1; | |
2251 | c.fSegmentation[cath]->SetHit(par[indx],par[indx+1],c.fZpad); | |
2252 | //charge += c.fResponse->IntXY(c.fSegmentation[cath])*par[icl*3+2]; | |
2253 | if (c.fNpar == 2) coef = 1; | |
2254 | else coef = i==c.fNpar/3 ? par[indx+2] : 1-coef; | |
2255 | //coef = TMath::Max (coef, 0.); | |
2256 | if (c.fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7]; | |
2257 | //coef = TMath::Max (coef, 0.); | |
2258 | charge += c.fResponse->IntXY(c.fSegmentation[cath])*coef; | |
2259 | } | |
2260 | charge *= c.fQtot; | |
2261 | //if (c.fXyq[2][j] > c.fResponse->MaxAdc()-1 && charge > | |
2262 | // c.fResponse->MaxAdc()) charge = c.fResponse->MaxAdc(); | |
2263 | delta = charge - c.fXyq[2][j]; | |
2264 | delta /= TMath::Sqrt ((Double_t)c.fXyq[2][j]); | |
2265 | //chi2 += TMath::Abs(delta); | |
2266 | chi2 += delta*delta; | |
2267 | } // for (Int_t j=0; | |
2268 | f = chi2; | |
2269 | Double_t qAver = c.fQtot/npads; //(c.fnPads[0]+c.fnPads[1]); | |
2270 | f = chi2/qAver; | |
2271 | } | |
2272 | ||
2273 | //_____________________________________________________________________________ | |
6aaf81e6 | 2274 | void AliMUONClusterFinderAZ::UpdatePads(Int_t /*nfit*/, Double_t *par) |
0df3ca52 | 2275 | { |
2276 | // Subtract the fitted charges from pads with strong coupling | |
2277 | ||
2278 | Int_t cath, ix, iy, indx; | |
2279 | Double_t charge, coef=0; | |
2280 | for (Int_t j=0; j<fnPads[0]+fnPads[1]; j++) { | |
2281 | if (fPadIJ[1][j] != -1) continue; | |
2282 | if (fNpar != 0) { | |
2283 | cath = fPadIJ[0][j]; | |
2284 | fSegmentation[cath]->GetPadI(fXyq[0][j],fXyq[1][j],fZpad,ix,iy); | |
2285 | fSegmentation[cath]->SetPad(ix,iy); | |
2286 | charge = 0; | |
2287 | for (Int_t i=fNpar/3; i>=0; i--) { // sum over tracks | |
2288 | indx = i<2 ? 2*i : 2*i+1; | |
2289 | fSegmentation[cath]->SetHit(par[indx],par[indx+1],fZpad); | |
2290 | if (fNpar == 2) coef = 1; | |
2291 | else coef = i==fNpar/3 ? par[indx+2] : 1-coef; | |
2292 | if (fNpar == 8 && i < 2) coef = i==1 ? coef*par[indx+2] : coef - par[7]; | |
2293 | charge += fResponse->IntXY(fSegmentation[cath])*coef; | |
2294 | } | |
2295 | charge *= fQtot; | |
2296 | fXyq[2][j] -= charge; | |
2297 | } // if (fNpar != 0) | |
2298 | if (fXyq[2][j] > fResponse->ZeroSuppression()) fPadIJ[1][j] = 0; // return pad for further using | |
2299 | } // for (Int_t j=0; | |
2300 | } | |
2301 | ||
2302 | //_____________________________________________________________________________ | |
6aaf81e6 | 2303 | Bool_t AliMUONClusterFinderAZ::TestTrack(Int_t /*t*/) { |
0df3ca52 | 2304 | // Test if track was user selected |
2305 | return kTRUE; | |
2306 | /* | |
2307 | if (fTrack[0]==-1 || fTrack[1]==-1) { | |
2308 | return kTRUE; | |
2309 | } else if (t==fTrack[0] || t==fTrack[1]) { | |
2310 | return kTRUE; | |
2311 | } else { | |
2312 | return kFALSE; | |
2313 | } | |
2314 | */ | |
2315 | } | |
2316 | ||
2317 | //_____________________________________________________________________________ | |
2318 | void AliMUONClusterFinderAZ::AddRawCluster(Double_t x, Double_t y, Double_t fmin) | |
2319 | { | |
2320 | // | |
2321 | // Add a raw cluster copy to the list | |
2322 | // | |
2323 | AliMUONRawCluster cnew; | |
2324 | AliMUON *pMUON=(AliMUON*)gAlice->GetModule("MUON"); | |
2325 | //pMUON->AddRawCluster(fInput->Chamber(),c); | |
2326 | ||
2327 | Int_t cath; | |
2328 | for (cath=0; cath<2; cath++) { | |
ba12c242 | 2329 | cnew.SetX(cath, x); |
2330 | cnew.SetY(cath, y); | |
2331 | cnew.SetZ(cath, fZpad); | |
2332 | cnew.SetCharge(cath, 100); | |
9e993f2a | 2333 | cnew.SetPeakSignal(cath,20); |
2334 | cnew.SetMultiplicity(cath, 5); | |
3b5272e3 | 2335 | cnew.SetNcluster(cath, 1); |
2336 | cnew.SetChi2(cath, fmin); //0.1; | |
0df3ca52 | 2337 | /* |
2338 | cnew.fMultiplicity[cath]=c->fMultiplicity[cath]; | |
2339 | for (i=0; i<fMul[cath]; i++) { | |
2340 | cnew.fIndexMap[i][cath]=c->fIndexMap[i][cath]; | |
2341 | fSeg[cath]->SetPad(fIx[i][cath], fIy[i][cath]); | |
2342 | } | |
2343 | fprintf(stderr,"\nRawCluster %d cath %d\n",ico,cath); | |
2344 | fprintf(stderr,"mult_av %d\n",c->fMultiplicity[cath]); | |
2345 | FillCluster(&cnew,cath); | |
2346 | */ | |
2347 | } | |
2348 | //cnew.fClusterType=cnew.PhysicsContribution(); | |
ce3f5e87 | 2349 | pMUON->GetMUONData()->AddRawCluster(AliMUONClusterInput::Instance()->Chamber(),cnew); |
0df3ca52 | 2350 | //fNPeaks++; |
2351 | } | |
2352 | ||
2353 | //_____________________________________________________________________________ | |
2354 | Int_t AliMUONClusterFinderAZ::FindLocalMaxima(Int_t *localMax, Double_t *maxVal) | |
2355 | { | |
2356 | // Find local maxima in pixel space for large preclusters in order to | |
2357 | // try to split them into smaller pieces (to speed up the MLEM procedure) | |
2358 | ||
2359 | TH2D *hist = (TH2D*) gROOT->FindObject("anode"); | |
2360 | if (hist) hist->Delete(); | |
2361 | ||
cd747ddb | 2362 | Double_t xylim[4] = {999, 999, 999, 999}; |
0df3ca52 | 2363 | Int_t nPix = fPixArray->GetEntriesFast(); |
2364 | AliMUONPixel *pixPtr = 0; | |
2365 | for (Int_t ipix=0; ipix<nPix; ipix++) { | |
2366 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
2367 | for (Int_t i=0; i<4; i++) | |
2368 | xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2)); | |
2369 | } | |
2370 | for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2); | |
2371 | ||
2372 | Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2); | |
2373 | Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2); | |
2374 | hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]); | |
2375 | for (Int_t ipix=0; ipix<nPix; ipix++) { | |
2376 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(ipix); | |
2377 | hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge()); | |
2378 | } | |
2379 | if (fDraw) { | |
2380 | ((TCanvas*)gROOT->FindObject("c2"))->cd(); | |
2381 | gPad->SetTheta(55); | |
2382 | gPad->SetPhi(30); | |
2383 | hist->Draw("lego1Fb"); | |
2384 | gPad->Update(); | |
2385 | int ia; | |
2386 | cin >> ia; | |
2387 | } | |
2388 | ||
2389 | Int_t nMax = 0, indx; | |
2390 | Int_t *isLocalMax = new Int_t[ny*nx]; | |
2391 | for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0; | |
2392 | ||
2393 | for (Int_t i=1; i<=ny; i++) { | |
2394 | indx = (i-1) * nx; | |
2395 | for (Int_t j=1; j<=nx; j++) { | |
2396 | if (hist->GetCellContent(j,i) < 0.5) continue; | |
2397 | //if (isLocalMax[indx+j-1] < 0) continue; | |
2398 | if (isLocalMax[indx+j-1] != 0) continue; | |
2399 | FlagLocalMax(hist, i, j, isLocalMax); | |
2400 | } | |
2401 | } | |
2402 | ||
2403 | for (Int_t i=1; i<=ny; i++) { | |
2404 | indx = (i-1) * nx; | |
2405 | for (Int_t j=1; j<=nx; j++) { | |
2406 | if (isLocalMax[indx+j-1] > 0) { | |
2407 | localMax[nMax] = indx + j - 1; | |
2408 | maxVal[nMax++] = hist->GetCellContent(j,i); | |
2409 | } | |
2410 | if (nMax > 99) { cout << " Too many local maxima !!!" << endl; ::exit(0); } | |
2411 | } | |
2412 | } | |
2413 | cout << " Local max: " << nMax << endl; | |
2414 | delete [] isLocalMax; isLocalMax = 0; | |
2415 | return nMax; | |
2416 | } | |
2417 | ||
2418 | //_____________________________________________________________________________ | |
2419 | void AliMUONClusterFinderAZ::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax) | |
2420 | { | |
2421 | // Flag pixels (whether or not local maxima) | |
2422 | ||
2423 | Int_t nx = hist->GetNbinsX(); | |
2424 | Int_t ny = hist->GetNbinsY(); | |
2425 | Int_t cont = TMath::Nint (hist->GetCellContent(j,i)); | |
2426 | Int_t cont1 = 0; | |
2427 | ||
2428 | for (Int_t i1=i-1; i1<i+2; i1++) { | |
2429 | if (i1 < 1 || i1 > ny) continue; | |
2430 | for (Int_t j1=j-1; j1<j+2; j1++) { | |
2431 | if (j1 < 1 || j1 > nx) continue; | |
2432 | if (i == i1 && j == j1) continue; | |
2433 | cont1 = TMath::Nint (hist->GetCellContent(j1,i1)); | |
2434 | if (cont < cont1) { isLocalMax[(i-1)*nx+j-1] = -1; return; } | |
2435 | else if (cont > cont1) isLocalMax[(i1-1)*nx+j1-1] = -1; | |
2436 | else { // the same charge | |
2437 | isLocalMax[(i-1)*nx+j-1] = 1; | |
2438 | if (isLocalMax[(i1-1)*nx+j1-1] == 0) { | |
2439 | FlagLocalMax(hist, i1, j1, isLocalMax); | |
2440 | if (isLocalMax[(i1-1)*nx+j1-1] < 0) { isLocalMax[(i-1)*nx+j-1] = -1; return; } | |
2441 | else isLocalMax[(i1-1)*nx+j1-1] = -1; | |
2442 | } | |
2443 | } | |
2444 | } | |
2445 | } | |
2446 | isLocalMax[(i-1)*nx+j-1] = 1; // local maximum | |
2447 | } | |
2448 | ||
2449 | //_____________________________________________________________________________ | |
2450 | void AliMUONClusterFinderAZ::FindCluster(Int_t *localMax, Int_t iMax) | |
2451 | { | |
2452 | // Find pixel cluster around local maximum #iMax and pick up pads | |
2453 | // overlapping with it | |
2454 | ||
2455 | TH2D *hist = (TH2D*) gROOT->FindObject("anode"); | |
2456 | Int_t nx = hist->GetNbinsX(); | |
2457 | Int_t ny = hist->GetNbinsY(); | |
2458 | Int_t ic = localMax[iMax] / nx + 1; | |
2459 | Int_t jc = localMax[iMax] % nx + 1; | |
2460 | Bool_t *used = new Bool_t[ny*nx]; | |
2461 | for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE; | |
2462 | ||
2463 | // Drop all pixels from the array - pick up only the ones from the cluster | |
2464 | fPixArray->Delete(); | |
2465 | ||
2466 | Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2; | |
2467 | Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2; | |
2468 | Double_t yc = hist->GetYaxis()->GetBinCenter(ic); | |
2469 | Double_t xc = hist->GetXaxis()->GetBinCenter(jc); | |
2470 | Double_t cont = hist->GetCellContent(jc,ic); | |
2471 | AliMUONPixel *pixPtr = new AliMUONPixel (xc, yc, wx, wy, cont); | |
2472 | fPixArray->Add((TObject*)pixPtr); | |
2473 | used[(ic-1)*nx+jc-1] = kTRUE; | |
2474 | AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call | |
2475 | ||
2476 | Int_t nPix = fPixArray->GetEntriesFast(), npad = fnPads[0] + fnPads[1]; | |
2477 | for (Int_t i=0; i<nPix; i++) { | |
2478 | ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(0,wx); | |
2479 | ((AliMUONPixel*)fPixArray->UncheckedAt(i))->SetSize(1,wy); | |
2480 | } | |
2481 | cout << iMax << " " << nPix << endl; | |
2482 | ||
2483 | Float_t xy[4], xy12[4]; | |
2484 | // Pick up pads which overlap with found pixels | |
2485 | for (Int_t i=0; i<npad; i++) fPadIJ[1][i] = -1; | |
2486 | for (Int_t i=0; i<nPix; i++) { | |
2487 | pixPtr = (AliMUONPixel*) fPixArray->UncheckedAt(i); | |
2488 | for (Int_t j=0; j<4; j++) | |
2489 | xy[j] = pixPtr->Coord(j/2) + (j%2 ? 1 : -1)*pixPtr->Size(j/2); | |
2490 | for (Int_t j=0; j<npad; j++) | |
2491 | if (Overlap(xy, j, xy12, 0)) fPadIJ[1][j] = 0; // flag for use | |
2492 | } | |
2493 | ||
2494 | delete [] used; used = 0; | |
2495 | } |