]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONClusterFinderMLEM.cxx
centrality calibration files for lhc10g2a
[u/mrichter/AliRoot.git] / MUON / AliMUONClusterFinderMLEM.cxx
CommitLineData
c0a16418 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
3d1463c8 18//-----------------------------------------------------------------------------
c0a16418 19/// \class AliMUONClusterFinderMLEM
20///
21/// Clusterizer class based on the Expectation-Maximization algorithm
22///
23/// Pre-clustering is handled by AliMUONPreClusterFinder
24/// From a precluster a pixel array is built, and from this pixel array
25/// a list of clusters is output (using AliMUONClusterSplitterMLEM).
26///
27/// \author Laurent Aphecetche (for the "new" C++ structure) and
28/// Alexander Zinchenko, JINR Dubna, for the hardcore of it ;-)
3d1463c8 29//-----------------------------------------------------------------------------
c0a16418 30
9edefa04 31#include "AliMUONClusterFinderMLEM.h"
c0a16418 32#include "AliLog.h"
33#include "AliMUONCluster.h"
34#include "AliMUONClusterSplitterMLEM.h"
3887d11e 35#include "AliMUONVDigit.h"
c0a16418 36#include "AliMUONPad.h"
37#include "AliMUONPreClusterFinder.h"
38#include "AliMpPad.h"
39#include "AliMpVPadIterator.h"
40#include "AliMpVSegmentation.h"
41#include "AliRunLoader.h"
3887d11e 42#include "AliMUONVDigitStore.h"
a6435fb4 43#include <Riostream.h>
44#include <TH2.h>
45#include <TMinuit.h>
46#include <TCanvas.h>
a6435fb4 47#include <TMath.h>
f6c291ef 48#include "AliCodeTimer.h"
a6435fb4 49
c0a16418 50/// \cond CLASSIMP
51ClassImp(AliMUONClusterFinderMLEM)
52/// \endcond
53
2abdae6e 54const Double_t AliMUONClusterFinderMLEM::fgkDistancePrecision = 1e-3; // (cm) used to check overlaps and so on
c0a16418 55const TVector2 AliMUONClusterFinderMLEM::fgkIncreaseSize(-AliMUONClusterFinderMLEM::fgkDistancePrecision,-AliMUONClusterFinderMLEM::fgkDistancePrecision);
56const TVector2 AliMUONClusterFinderMLEM::fgkDecreaseSize(AliMUONClusterFinderMLEM::fgkDistancePrecision,AliMUONClusterFinderMLEM::fgkDistancePrecision);
57
8c718ff8 58// Status flags for pads
59const Int_t AliMUONClusterFinderMLEM::fgkZero = 0x0; ///< pad "basic" state
60const Int_t AliMUONClusterFinderMLEM::fgkMustKeep = 0x1; ///< do not kill (for pixels)
61const Int_t AliMUONClusterFinderMLEM::fgkUseForFit = 0x10; ///< should be used for fit
62const Int_t AliMUONClusterFinderMLEM::fgkOver = 0x100; ///< processing is over
63const Int_t AliMUONClusterFinderMLEM::fgkModified = 0x1000; ///< modified pad charge
64const Int_t AliMUONClusterFinderMLEM::fgkCoupled = 0x10000; ///< coupled pad
c0a16418 65
66//_____________________________________________________________________________
b1a19e07 67AliMUONClusterFinderMLEM::AliMUONClusterFinderMLEM(Bool_t plot, AliMUONVClusterFinder* clusterFinder)
c0a16418 68 : AliMUONVClusterFinder(),
b1a19e07 69fPreClusterFinder(clusterFinder),
c0a16418 70fPreCluster(0x0),
71fClusterList(),
72fEventNumber(0),
73fDetElemId(-1),
74fClusterNumber(0),
c80b29a4 75fHistMlem(0x0),
76fHistAnode(0x0),
c0a16418 77fPixArray(new TObjArray(20)),
9bbd7f60 78fDebug(0),
c0a16418 79fPlot(plot),
c0a16418 80fSplitter(0x0),
81fNClusters(0),
110edb51 82fNAddVirtualPads(0),
83fLowestPixelCharge(0),
84fLowestPadCharge(0),
85fLowestClusterCharge(0)
c0a16418 86{
87 /// Constructor
57f3728e 88
72dae9ff 89 fkSegmentation[1] = fkSegmentation[0] = 0x0;
c0a16418 90
9bbd7f60 91 if (fPlot) fDebug = 1;
c0a16418 92}
93
94//_____________________________________________________________________________
95AliMUONClusterFinderMLEM::~AliMUONClusterFinderMLEM()
96{
97/// Destructor
c80b29a4 98 delete fPixArray; fPixArray = 0;
c0a16418 99// delete fDraw;
100 delete fPreClusterFinder;
c0a16418 101 delete fSplitter;
102 AliInfo(Form("Total clusters %d AddVirtualPad needed %d",
103 fNClusters,fNAddVirtualPads));
104}
105
106//_____________________________________________________________________________
107Bool_t
24935e58 108AliMUONClusterFinderMLEM::Prepare(Int_t detElemId,
109 TClonesArray* pads[2],
110 const AliMpArea& area,
111 const AliMpVSegmentation* seg[2])
c0a16418 112{
113 /// Prepare for clustering
99c136e1 114// AliCodeTimerAuto("",0)
c0a16418 115
116 for ( Int_t i = 0; i < 2; ++i )
117 {
72dae9ff 118 fkSegmentation[i] = seg[i];
c0a16418 119 }
120
121 // Find out the DetElemId
24935e58 122 fDetElemId = detElemId;
c0a16418 123
124 delete fSplitter;
110edb51 125 fSplitter = new AliMUONClusterSplitterMLEM(fDetElemId,
126 fPixArray,
127 fLowestPixelCharge,
128 fLowestPadCharge,
129 fLowestClusterCharge);
9bbd7f60 130 fSplitter->SetDebug(fDebug);
c0a16418 131
132 // find out current event number, and reset the cluster number
33c3c91a 133 AliRunLoader *runLoader = AliRunLoader::Instance();
7deb8eb0 134 fEventNumber = runLoader ? runLoader->GetEventNumber() : 0;
c0a16418 135 fClusterNumber = -1;
136 fClusterList.Delete();
57f3728e 137 fPixArray->Delete();
138
b1a19e07 139 AliDebug(3,Form("EVT %d DE %d",fEventNumber,fDetElemId));
c0a16418 140
24935e58 141 if ( fPreClusterFinder->NeedSegmentation() )
142 {
143 return fPreClusterFinder->Prepare(detElemId,pads,area,seg);
144 }
145 else
146 {
147 return fPreClusterFinder->Prepare(detElemId,pads,area);
148 }
c0a16418 149}
150
151//_____________________________________________________________________________
152AliMUONCluster*
153AliMUONClusterFinderMLEM::NextCluster()
154{
155 /// Return next cluster
99c136e1 156// AliCodeTimerAuto("",0)
c0a16418 157
c0a16418 158 // if the list of clusters is not void, pick one from there
9bbd7f60 159 TObject* o = fClusterList.At(++fClusterNumber);
160 if ( o != 0x0 ) return static_cast<AliMUONCluster*>(o);
c0a16418 161
162 //FIXME : at this point, must check whether we've used all the digits
163 //from precluster : if not, let the preclustering know about those unused
164 //digits, so it can reuse them
165
166 // if the cluster list is exhausted, we need to go to the next
167 // pre-cluster and treat it
168
169 fPreCluster = fPreClusterFinder->NextCluster();
9bf6860b 170
57f3728e 171 fPixArray->Delete();
9bf6860b 172 fClusterList.Delete(); // reset the list of clusters for this pre-cluster
173 fClusterNumber = -1; //AZ
174
c0a16418 175 if (!fPreCluster)
176 {
177 // we are done
178 return 0x0;
179 }
180
c0a16418 181 WorkOnPreCluster();
182
183 // WorkOnPreCluster may have used only part of the pads, so we check that
184 // now, and let the unused pads be reused by the preclustering...
185
2abdae6e 186 Int_t mult = fPreCluster->Multiplicity();
187 for ( Int_t i = 0; i < mult; ++i )
c0a16418 188 {
189 AliMUONPad* pad = fPreCluster->Pad(i);
190 if ( !pad->IsUsed() )
191 {
192 fPreClusterFinder->UsePad(*pad);
193 }
194 }
195
196 return NextCluster();
197}
198
199//_____________________________________________________________________________
200Bool_t
201AliMUONClusterFinderMLEM::WorkOnPreCluster()
202{
203 /// Starting from a precluster, builds a pixel array, and then
204 /// extract clusters from this array
205
99c136e1 206 // AliCodeTimerAuto("",0)
9bbd7f60 207
208 if (fDebug) {
7deb8eb0 209 cout << " *** Event # " << fEventNumber
9bbd7f60 210 << " det. elem.: " << fDetElemId << endl;
2abdae6e 211 for (Int_t j = 0; j < fPreCluster->Multiplicity(); ++j) {
9bbd7f60 212 AliMUONPad* pad = fPreCluster->Pad(j);
213 printf(" bbb %3d %1d %8.4f %8.4f %8.4f %8.4f %6.1f %3d %3d %2d %1d %1d \n",
214 j, pad->Cathode(), pad->Coord(0), pad->Coord(1), pad->DX()*2, pad->DY()*2,
215 pad->Charge(), pad->Ix(), pad->Iy(), pad->Status(), pad->IsReal(), pad->IsSaturated());
216 }
217 }
218
c0a16418 219 AliMUONCluster* cluster = CheckPrecluster(*fPreCluster);
c0a16418 220 if (!cluster) return kFALSE;
9bbd7f60 221
c0a16418 222 BuildPixArray(*cluster);
223
224 if ( fPixArray->GetLast() < 0 )
225 {
226 AliDebug(1,"No pixel for the above cluster");
227 delete cluster;
228 return kFALSE;
229 }
230
231 // Use MLEM for cluster finder
232 Int_t nMax = 1, localMax[100], maxPos[100];
233 Double_t maxVal[100];
234
c0a16418 235 Int_t iSimple = 0, nInX = -1, nInY;
236
237 PadsInXandY(*cluster,nInX, nInY);
238
8c718ff8 239 if (nInX < 4 && nInY < 4)
240 {
241 iSimple = 1; // simple cluster
242 }
243 else
c0a16418 244 {
8c718ff8 245 nMax = FindLocalMaxima(fPixArray, localMax, maxVal); // for small clusters just to tag pixels
246 if (nMax > 1) {
247 if (cluster->Multiplicity() <= 50) nMax = 1; // for small clusters
248 if (nMax > 1) TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in descending order
249 }
c0a16418 250 }
251
2abdae6e 252 for (Int_t i = 0; i < nMax; ++i)
c0a16418 253 {
254 if (nMax > 1)
255 {
256 FindCluster(*cluster,localMax, maxPos[i]);
257 }
2abdae6e 258
c0a16418 259 MainLoop(*cluster,iSimple);
2abdae6e 260
c0a16418 261 if (i < nMax-1)
262 {
2abdae6e 263 Int_t mult = cluster->Multiplicity();
264 for (Int_t j = 0; j < mult; ++j)
c0a16418 265 {
266 AliMUONPad* pad = cluster->Pad(j);
8c718ff8 267 //if ( pad->Status() == 0 ) continue; // pad charge was not modified
268 if ( pad->Status() != fgkOver ) continue; // pad was not used
269 //pad->SetStatus(0);
270 pad->SetStatus(fgkZero);
c0a16418 271 pad->RevertCharge(); // use backup charge value
272 }
273 }
274 } // for (Int_t i=0; i<nMax;
c80b29a4 275 delete fHistMlem;
276 delete fHistAnode;
277 fHistMlem = fHistAnode = 0x0;
c0a16418 278 delete cluster;
279 return kTRUE;
280}
281
282//_____________________________________________________________________________
283Bool_t
284AliMUONClusterFinderMLEM::Overlap(const AliMUONPad& pad, const AliMUONPad& pixel)
285{
286 /// Check if the pad and the pixel overlaps
287
288 // make a fake pad from the pixel
289 AliMUONPad tmp(pad.DetElemId(),pad.Cathode(),pad.Ix(),pad.Iy(),
290 pixel.Coord(0),pixel.Coord(1),
291 pixel.Size(0),pixel.Size(1),0);
292
293 return AliMUONPad::AreOverlapping(pad,tmp,fgkDecreaseSize);
294}
295
296//_____________________________________________________________________________
297AliMUONCluster*
298AliMUONClusterFinderMLEM::CheckPrecluster(const AliMUONCluster& origCluster)
299{
300 /// Check precluster in order to attempt to simplify it (mostly for
301 /// two-cathode preclusters)
2abdae6e 302
99c136e1 303 AliCodeTimerAuto("",0)
f6c291ef 304
2abdae6e 305 // Disregard small clusters (leftovers from splitting or noise)
306 if ((origCluster.Multiplicity()==1 || origCluster.Multiplicity()==2) &&
110edb51 307 origCluster.Charge(0)+origCluster.Charge(1) < fLowestClusterCharge )
c0a16418 308 {
c0a16418 309 return 0x0;
310 }
311
8c718ff8 312 AliMUONCluster* cluster = new AliMUONCluster(origCluster);
c0a16418 313
c0a16418 314 AliDebug(2,"Start of CheckPreCluster=");
2abdae6e 315 //StdoutToAliDebug(2,cluster->Print("full"));
c0a16418 316
c0a16418 317 AliMUONCluster* rv(0x0);
318
2abdae6e 319 if (cluster->Multiplicity(0) && cluster->Multiplicity(1))
c0a16418 320 {
321 rv = CheckPreclusterTwoCathodes(cluster);
322 }
323 else
324 {
f6c291ef 325 rv = cluster;
c0a16418 326 }
c0a16418 327 return rv;
328}
329
c0a16418 330//_____________________________________________________________________________
331AliMUONCluster*
332AliMUONClusterFinderMLEM::CheckPreclusterTwoCathodes(AliMUONCluster* cluster)
333{
71a2d3aa 334 /// Check two-cathode cluster
c0a16418 335
c0a16418 336 Int_t npad = cluster->Multiplicity();
337 Int_t* flags = new Int_t[npad];
2abdae6e 338 for (Int_t j = 0; j < npad; ++j) flags[j] = 0;
c0a16418 339
340 // Check pad overlaps
2abdae6e 341 for ( Int_t i = 0; i < npad; ++i)
c0a16418 342 {
343 AliMUONPad* padi = cluster->Pad(i);
8c718ff8 344 if ( padi->Cathode() != 0 ) continue;
2abdae6e 345 for (Int_t j = i+1; j < npad; ++j)
c0a16418 346 {
347 AliMUONPad* padj = cluster->Pad(j);
8c718ff8 348 if ( padj->Cathode() != 1 ) continue;
c0a16418 349 if ( !AliMUONPad::AreOverlapping(*padi,*padj,fgkDecreaseSize) ) continue;
350 flags[i] = flags[j] = 1; // mark overlapped pads
351 }
352 }
353
354 // Check if all pads overlap
355 Int_t nFlags=0;
2abdae6e 356 for (Int_t i = 0; i < npad; ++i)
c0a16418 357 {
2abdae6e 358 if (!flags[i]) ++nFlags;
c0a16418 359 }
360
361 if (nFlags > 0)
362 {
363 // not all pads overlap.
9bbd7f60 364 if (fDebug) cout << " nFlags: " << nFlags << endl;
365 TObjArray toBeRemoved;
2abdae6e 366 for (Int_t i = 0; i < npad; ++i)
c0a16418 367 {
368 AliMUONPad* pad = cluster->Pad(i);
369 if (flags[i]) continue;
370 Int_t cath = pad->Cathode();
371 Int_t cath1 = TMath::Even(cath);
372 // Check for edge effect (missing pads on the _other_ cathode)
6e97fbb8 373 AliMpPad mpPad =
374 fkSegmentation[cath1]->PadByPosition(pad->Position().X(),
375 pad->Position().Y(),kFALSE);
c0a16418 376 if (!mpPad.IsValid()) continue;
110edb51 377 if (nFlags == 1 && pad->Charge() < fLowestPadCharge) continue;
c0a16418 378 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
379 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),pad->Charge()));
9bbd7f60 380 toBeRemoved.AddLast(pad);
c0a16418 381 fPreCluster->Pad(i)->Release();
9bbd7f60 382 }
2abdae6e 383 Int_t nRemove = toBeRemoved.GetEntriesFast();
384 for ( Int_t i = 0; i < nRemove; ++i )
9bbd7f60 385 {
2abdae6e 386 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.UncheckedAt(i)));
c0a16418 387 }
388 }
389
390 // Check correlations of cathode charges
9bbd7f60 391 if ( !cluster->IsSaturated() && cluster->ChargeAsymmetry() > 1 )
c0a16418 392 {
393 // big difference
394 Int_t cathode = cluster->MaxRawChargeCathode();
8c718ff8 395 Int_t imin(-1);
396 Int_t imax(-1);
c0a16418 397 Double_t cmax(0);
398 Double_t cmin(1E9);
399
400 // get min and max pad charges on the cathode opposite to the
401 // max pad (given by MaxRawChargeCathode())
402 //
2abdae6e 403 Int_t mult = cluster->Multiplicity();
404 for ( Int_t i = 0; i < mult; ++i )
c0a16418 405 {
406 AliMUONPad* pad = cluster->Pad(i);
407 if ( pad->Cathode() != cathode || !pad->IsReal() )
408 {
409 // only consider pads in the opposite cathode, and
2abdae6e 410 // only consider real pads (i.e. exclude the virtual ones)
c0a16418 411 continue;
412 }
413 if ( pad->Charge() < cmin )
414 {
415 cmin = pad->Charge();
416 imin = i;
8c718ff8 417 if (imax < 0) {
418 imax = imin;
419 cmax = cmin;
420 }
c0a16418 421 }
8c718ff8 422 else if ( pad->Charge() > cmax )
c0a16418 423 {
424 cmax = pad->Charge();
425 imax = i;
426 }
427 }
428 AliDebug(2,Form("Pad imin,imax %d,%d cmin,cmax %e,%e",
429 imin,imax,cmin,cmax));
430 //
431 // arrange pads according to their distance to the max, normalized
432 // to the pad size
2abdae6e 433 Double_t* dist = new Double_t[mult];
c0a16418 434 Double_t dxMin(1E9);
435 Double_t dyMin(1E9);
436 Double_t dmin(0);
437
438 AliMUONPad* padmax = cluster->Pad(imax);
439
2abdae6e 440 for ( Int_t i = 0; i < mult; ++i )
c0a16418 441 {
442 dist[i] = 0.0;
443 if ( i == imax) continue;
444 AliMUONPad* pad = cluster->Pad(i);
445 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
446 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
447 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
448 dist[i] = TMath::Sqrt(dx*dx+dy*dy);
449 if ( i == imin )
450 {
451 dmin = dist[i] + 1E-3; // distance to the pad with minimum charge
452 dxMin = dx;
453 dyMin = dy;
454 }
455 }
456
2abdae6e 457 TMath::Sort(mult,dist,flags,kFALSE); // in ascending order
458 Double_t xmax(-1), distPrev(999);
c0a16418 459 TObjArray toBeRemoved;
460
2abdae6e 461 for ( Int_t i = 0; i < mult; ++i )
c0a16418 462 {
463 Int_t indx = flags[i];
464 AliMUONPad* pad = cluster->Pad(indx);
465 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
466 if ( dist[indx] > dmin )
467 {
468 // farther than the minimum pad
469 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
470 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
471 dx *= dxMin;
472 dy *= dyMin;
473 if (dx >= 0 && dy >= 0) continue;
474 if (TMath::Abs(dx) > TMath::Abs(dy) && dx >= 0) continue;
475 if (TMath::Abs(dy) > TMath::Abs(dx) && dy >= 0) continue;
476 }
2abdae6e 477 if (dist[indx] > distPrev + 1) break; // overstepping empty pads
c0a16418 478 if ( pad->Charge() <= cmax || TMath::Abs(dist[indx]-xmax) < 1E-3 )
479 {
480 // release pad
481 if (TMath::Abs(dist[indx]-xmax) < 1.e-3)
482 {
483 cmax = TMath::Max(pad->Charge(),cmax);
484 }
485 else
486 {
487 cmax = pad->Charge();
488 }
489 xmax = dist[indx];
2abdae6e 490 distPrev = dist[indx];
c0a16418 491 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
492 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),
493 pad->Charge()));
494
495 toBeRemoved.AddLast(pad);
496 fPreCluster->Pad(indx)->Release();
497 }
498 }
2abdae6e 499 Int_t nRemove = toBeRemoved.GetEntriesFast();
500 for ( Int_t i = 0; i < nRemove; ++i )
c0a16418 501 {
2abdae6e 502 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.UncheckedAt(i)));
c0a16418 503 }
504 delete[] dist;
505 }
506
507 delete[] flags;
508
509 AliDebug(2,"End of CheckPreClusterTwoCathodes=");
2abdae6e 510 //StdoutToAliDebug(2,cluster->Print("full"));
c0a16418 511
512 return cluster;
513}
514
515//_____________________________________________________________________________
516void
517AliMUONClusterFinderMLEM::CheckOverlaps()
518{
519 /// For debug only : check if some pixels overlap...
520
521 Int_t nPix = fPixArray->GetLast()+1;
522 Int_t dummy(0);
523
524 for ( Int_t i = 0; i < nPix; ++i )
525 {
526 AliMUONPad* pixelI = Pixel(i);
527 AliMUONPad pi(dummy,dummy,dummy,dummy,
528 pixelI->Coord(0),pixelI->Coord(1),
529 pixelI->Size(0),pixelI->Size(1),0.0);
530
531 for ( Int_t j = i+1; j < nPix; ++j )
532 {
533 AliMUONPad* pixelJ = Pixel(j);
534 AliMUONPad pj(dummy,dummy,dummy,dummy,
535 pixelJ->Coord(0),pixelJ->Coord(1),
536 pixelJ->Size(0),pixelJ->Size(1),0.0);
537 AliMpArea area;
538
539 if ( AliMUONPad::AreOverlapping(pi,pj,fgkDecreaseSize,area) )
540 {
541 AliInfo(Form("The following 2 pixels (%d and %d) overlap !",i,j));
2abdae6e 542 /*
c0a16418 543 StdoutToAliInfo(pixelI->Print();
544 cout << " Surface = " << pixelI->Size(0)*pixelI->Size(1)*4 << endl;
545 pixelJ->Print();
546 cout << " Surface = " << pixelJ->Size(0)*pixelJ->Size(1)*4 << endl;
79f44b3b 547 cout << " Area surface = " << area.GetDimensionX()*area.GetDimensionY()*4 << endl;
c0a16418 548 cout << "-------" << endl;
549 );
2abdae6e 550 */
c0a16418 551 }
552 }
553 }
554}
555
556//_____________________________________________________________________________
557void AliMUONClusterFinderMLEM::BuildPixArray(AliMUONCluster& cluster)
558{
559 /// Build pixel array for MLEM method
560
561 Int_t npad = cluster.Multiplicity();
562 if (npad<=0)
563 {
564 AliWarning("Got no pad at all ?!");
565 }
566
567 fPixArray->Delete();
8c718ff8 568 BuildPixArrayOneCathode(cluster);
c0a16418 569
570 Int_t nPix = fPixArray->GetLast()+1;
571
2abdae6e 572// AliDebug(2,Form("nPix after BuildPixArray=%d",nPix));
c0a16418 573
2abdae6e 574 if ( nPix > npad )
c0a16418 575 {
2abdae6e 576// AliDebug(2,Form("Will trim number of pixels to number of pads"));
c0a16418 577
578 // Too many pixels - sort and remove pixels with the lowest signal
579 fPixArray->Sort();
8c718ff8 580 for ( Int_t i = npad; i < nPix; ++i )
c0a16418 581 {
582 RemovePixel(i);
583 }
584 fPixArray->Compress();
c0a16418 585 } // if (nPix > npad)
586
587// StdoutToAliDebug(2,cout << "End of BuildPixelArray:" << endl;
588// fPixArray->Print(););
2abdae6e 589 //CheckOverlaps();//FIXME : this is for debug only. Remove it.
c0a16418 590}
591
2abdae6e 592//_____________________________________________________________________________
593void AliMUONClusterFinderMLEM::BuildPixArrayOneCathode(AliMUONCluster& cluster)
594{
595 /// Build the pixel array
596
597// AliDebug(2,Form("cluster.Multiplicity=%d",cluster.Multiplicity()));
598
2abdae6e 599 TVector2 dim = cluster.MinPadDimensions (-1, kFALSE);
824fb5ed 600 Double_t width[2] = {dim.X(), dim.Y()}, xy0[2]={99999,99999};
601 Int_t found[2] = {0,0}, mult = cluster.Multiplicity();
2abdae6e 602
8c718ff8 603 for ( Int_t i = 0; i < mult; ++i) {
2abdae6e 604 AliMUONPad* pad = cluster.Pad(i);
2abdae6e 605 for (Int_t j = 0; j < 2; ++j) {
606 if (found[j] == 0 && TMath::Abs(pad->Size(j)-width[j]) < fgkDistancePrecision) {
607 xy0[j] = pad->Coord(j);
608 found[j] = 1;
609 }
610 }
8c718ff8 611 if (found[0] && found[1]) break;
2abdae6e 612 }
2abdae6e 613
2abdae6e 614 Double_t min[2], max[2];
615 Int_t cath0 = 0, cath1 = 1;
616 if (cluster.Multiplicity(0) == 0) cath0 = 1;
617 else if (cluster.Multiplicity(1) == 0) cath1 = 0;
8c718ff8 618
6e97fbb8 619
620 Double_t leftDownX, leftDownY;
621 cluster.Area(cath0).LeftDownCorner(leftDownX, leftDownY);
622 Double_t rightUpX, rightUpY;
623 cluster.Area(cath0).RightUpCorner(rightUpX, rightUpY);
624 min[0] = leftDownX;
625 min[1] = leftDownY;
626 max[0] = rightUpX;
627 max[1] = rightUpY;;
8c718ff8 628 if (cath1 != cath0) {
6e97fbb8 629 cluster.Area(cath1).LeftDownCorner(leftDownX, leftDownY);
630 cluster.Area(cath1).RightUpCorner(rightUpX, rightUpY);
631 min[0] = TMath::Max (min[0], leftDownX);
632 min[1] = TMath::Max (min[1], leftDownY);
633 max[0] = TMath::Min (max[0], rightUpX);
634 max[1] = TMath::Min (max[1], rightUpY);
8c718ff8 635 }
2abdae6e 636
637 // Adjust limits
638 //width[0] /= 2; width[1] /= 2; // just for check
824fb5ed 639 Int_t nbins[2]={0,0};
2abdae6e 640 for (Int_t i = 0; i < 2; ++i) {
641 Double_t dist = (min[i] - xy0[i]) / width[i] / 2;
8c718ff8 642 if (TMath::Abs(dist) < 1.e-6) dist = -1.e-6;
2abdae6e 643 min[i] = xy0[i] + (TMath::Nint(dist-TMath::Sign(1.e-6,dist))
644 + TMath::Sign(0.5,dist)) * width[i] * 2;
645 nbins[i] = TMath::Nint ((max[i] - min[i]) / width[i] / 2);
646 if (nbins[i] == 0) ++nbins[i];
647 max[i] = min[i] + nbins[i] * width[i] * 2;
648 //cout << dist << " " << min[i] << " " << max[i] << " " << nbins[i] << endl;
649 }
650
651 // Book histogram
652 TH2D *hist1 = new TH2D ("Grid", "", nbins[0], min[0], max[0], nbins[1], min[1], max[1]);
653 TH2D *hist2 = new TH2D ("Entries", "", nbins[0], min[0], max[0], nbins[1], min[1], max[1]);
654 TAxis *xaxis = hist1->GetXaxis();
655 TAxis *yaxis = hist1->GetYaxis();
656
657 // Fill histogram
2abdae6e 658 for ( Int_t i = 0; i < mult; ++i) {
659 AliMUONPad* pad = cluster.Pad(i);
660 Int_t ix0 = xaxis->FindBin(pad->X());
661 Int_t iy0 = yaxis->FindBin(pad->Y());
c80b29a4 662 PadOverHist(0, ix0, iy0, pad, hist1, hist2);
2abdae6e 663 }
664
665 // Store pixels
666 for (Int_t i = 1; i <= nbins[0]; ++i) {
667 Double_t x = xaxis->GetBinCenter(i);
668 for (Int_t j = 1; j <= nbins[1]; ++j) {
669 if (hist2->GetCellContent(i,j) < 0.1) continue;
8c718ff8 670 //if (hist2->GetCellContent(i,j) < 1.1 && cluster.Multiplicity(0) &&
671 // cluster.Multiplicity(1)) continue;
672 if (cath0 != cath1) {
673 // Two-sided cluster
674 Double_t cont = hist2->GetCellContent(i,j);
675 if (cont < 999.) continue;
676 if (cont-Int_t(cont/1000.)*1000. < 0.5) continue;
677 }
2abdae6e 678 Double_t y = yaxis->GetBinCenter(j);
679 Double_t charge = hist1->GetCellContent(i,j);
680 AliMUONPad* pixPtr = new AliMUONPad(x, y, width[0], width[1], charge);
681 fPixArray->Add(pixPtr);
682 }
683 }
8c718ff8 684 //*
685 if (fPixArray->GetEntriesFast() == 1) {
686 // Split pixel into 2
687 AliMUONPad* pixPtr = static_cast<AliMUONPad*> (fPixArray->UncheckedAt(0));
688 pixPtr->SetSize(0,width[0]/2.);
689 pixPtr->Shift(0,-width[0]/4.);
690 pixPtr = new AliMUONPad(pixPtr->X()+width[0], pixPtr->Y(), width[0]/2., width[1], pixPtr->Charge());
691 fPixArray->Add(pixPtr);
692 }
693 //*/
2abdae6e 694 //fPixArray->Print();
695 delete hist1;
696 delete hist2;
697}
698
699//_____________________________________________________________________________
c80b29a4 700void AliMUONClusterFinderMLEM::PadOverHist(Int_t idir, Int_t ix0, Int_t iy0, AliMUONPad *pad,
701 TH2D *hist1, TH2D *hist2)
2abdae6e 702{
703 /// "Span" pad over histogram in the direction idir
704
2abdae6e 705 TAxis *axis = idir == 0 ? hist1->GetXaxis() : hist1->GetYaxis();
8c718ff8 706 Int_t nbins = axis->GetNbins(), cath = pad->Cathode();
707 Double_t bin = axis->GetBinWidth(1), amask = TMath::Power(1000.,cath*1.);
2abdae6e 708
709 Int_t nbinPad = (Int_t)(pad->Size(idir)/bin*2+fgkDistancePrecision) + 1; // number of bins covered by pad
710
711 for (Int_t i = 0; i < nbinPad; ++i) {
712 Int_t ixy = idir == 0 ? ix0 + i : iy0 + i;
713 if (ixy > nbins) break;
714 Double_t lowEdge = axis->GetBinLowEdge(ixy);
715 if (lowEdge + fgkDistancePrecision > pad->Coord(idir) + pad->Size(idir)) break;
c80b29a4 716 if (idir == 0) PadOverHist(1, ixy, iy0, pad, hist1, hist2); // span in the other direction
2abdae6e 717 else {
718 // Fill histogram
719 Double_t cont = pad->Charge();
720 if (hist2->GetCellContent(ix0, ixy) > 0.1)
721 cont = TMath::Min (hist1->GetCellContent(ix0, ixy), cont);
722 hist1->SetCellContent(ix0, ixy, cont);
8c718ff8 723 //hist2->SetCellContent(ix0, ixy, hist2->GetCellContent(ix0, ixy)+1);
724 hist2->SetCellContent(ix0, ixy, hist2->GetCellContent(ix0, ixy)+amask);
2abdae6e 725 }
726 }
727
728 for (Int_t i = -1; i > -nbinPad; --i) {
729 Int_t ixy = idir == 0 ? ix0 + i : iy0 + i;
730 if (ixy < 1) break;
731 Double_t upEdge = axis->GetBinUpEdge(ixy);
732 if (upEdge - fgkDistancePrecision < pad->Coord(idir) - pad->Size(idir)) break;
c80b29a4 733 if (idir == 0) PadOverHist(1, ixy, iy0, pad, hist1, hist2); // span in the other direction
2abdae6e 734 else {
735 // Fill histogram
736 Double_t cont = pad->Charge();
737 if (hist2->GetCellContent(ix0, ixy) > 0.1)
738 cont = TMath::Min (hist1->GetCellContent(ix0, ixy), cont);
739 hist1->SetCellContent(ix0, ixy, cont);
8c718ff8 740 //hist2->SetCellContent(ix0, ixy, hist2->GetCellContent(ix0, ixy)+1);
741 hist2->SetCellContent(ix0, ixy, hist2->GetCellContent(ix0, ixy)+amask);
2abdae6e 742 }
743 }
744}
745
c0a16418 746//_____________________________________________________________________________
747void
748AliMUONClusterFinderMLEM::Plot(const char* basename)
749{
750 /// Make a plot and save it as png
751
9bbd7f60 752 return; //AZ
c0a16418 753 if (!fPlot) return;
754
755 TCanvas* c = new TCanvas("MLEM","MLEM",800,600);
756 c->Draw();
757 Draw();
758 c->Modified();
759 c->Update();
760 c->Print(Form("%s.EVT%d.DE%d.CLU%d.png",basename,fEventNumber,
761 fDetElemId,fClusterNumber));
762}
763
764//_____________________________________________________________________________
765void
766AliMUONClusterFinderMLEM::ComputeCoefficients(AliMUONCluster& cluster,
767 Double_t* coef,
768 Double_t* probi)
769{
770 /// Compute coefficients needed for MLEM algorithm
771
824fb5ed 772 Int_t npadTot = cluster.Multiplicity();
c0a16418 773 Int_t nPix = fPixArray->GetLast()+1;
774
2abdae6e 775 //memset(probi,0,nPix*sizeof(Double_t));
824fb5ed 776 for (Int_t j = 0; j < npadTot*nPix; ++j) coef[j] = 0.;
2abdae6e 777 for (Int_t j = 0; j < nPix; ++j) probi[j] = 0.;
c0a16418 778
2abdae6e 779 Int_t mult = cluster.Multiplicity();
780 for ( Int_t j = 0; j < mult; ++j )
c0a16418 781 {
782 AliMUONPad* pad = cluster.Pad(j);
783 Int_t indx = j*nPix;
784
2abdae6e 785 for ( Int_t ipix = 0; ipix < nPix; ++ipix )
c0a16418 786 {
787 Int_t indx1 = indx + ipix;
8c718ff8 788 //if (pad->Status() < 0)
789 if (pad->Status() != fgkZero)
c0a16418 790 {
791 coef[indx1] = 0;
792 continue;
793 }
794 AliMUONPad* pixPtr = Pixel(ipix);
795 // coef is the charge (given by Mathieson integral) on pad, assuming
796 // the Mathieson is center at pixel.
797 coef[indx1] = fSplitter->ChargeIntegration(pixPtr->Coord(0), pixPtr->Coord(1), *pad);
2abdae6e 798// AliDebug(2,Form("pad=(%d,%d,%e,%e,%e,%e) pix=(%e,%e,%e,%e) coef %e",
799// pad->Ix(),pad->Iy(),
800// pad->X(),pad->Y(),
801// pad->DX(),pad->DY(),
802// pixPtr->Coord(0),pixPtr->Coord(1),
803// pixPtr->Size(0),pixPtr->Size(1),
804// coef[indx1]));
c0a16418 805
806 probi[ipix] += coef[indx1];
807 }
808 }
809}
810
811//_____________________________________________________________________________
812Bool_t AliMUONClusterFinderMLEM::MainLoop(AliMUONCluster& cluster, Int_t iSimple)
813{
814 /// Repeat MLEM algorithm until pixel size becomes sufficiently small
815
99c136e1 816 // AliCodeTimerAuto("",0)
f6c291ef 817
c0a16418 818 Int_t nPix = fPixArray->GetLast()+1;
819
820 AliDebug(2,Form("nPix=%d iSimple=%d, precluster=",nPix,iSimple));
2abdae6e 821 //StdoutToAliDebug(2,cluster.Print("full"););
c0a16418 822
823 if ( nPix < 0 )
824 {
825 AliDebug(1,"No pixels, why am I here then ?");
826 }
827
828 AddVirtualPad(cluster); // add virtual pads if necessary
829
830 Int_t npadTot = cluster.Multiplicity();
831 Int_t npadOK = 0;
832 for (Int_t i = 0; i < npadTot; ++i)
833 {
8c718ff8 834 //if (cluster.Pad(i)->Status() == 0) ++npadOK;
835 if (cluster.Pad(i)->Status() == fgkZero) ++npadOK;
c0a16418 836 }
837
c0a16418 838 Double_t* coef(0x0);
839 Double_t* probi(0x0);
8c718ff8 840 Int_t lc(0); // loop counter
c0a16418 841
2abdae6e 842 //Plot("mlem.start");
8c718ff8 843 AliMUONPad* pixPtr = Pixel(0);
844 Double_t xylim[4] = {pixPtr->X(), -pixPtr->X(), pixPtr->Y(), -pixPtr->Y()};
845
c0a16418 846 while (1)
847 {
848 ++lc;
c80b29a4 849 delete fHistMlem;
110edb51 850 fHistMlem = 0x0;
c0a16418 851
852 AliDebug(2,Form("lc %d nPix %d(%d) npadTot %d npadOK %d",lc,nPix,fPixArray->GetLast()+1,npadTot,npadOK));
853 AliDebug(2,Form("EVT%d PixArray=",fEventNumber));
110edb51 854 //StdoutToAliDebug(2,fPixArray->Print("full"));
c0a16418 855
856 coef = new Double_t [npadTot*nPix];
857 probi = new Double_t [nPix];
858
859 // Calculate coefficients and pixel visibilities
860 ComputeCoefficients(cluster,coef,probi);
861
2abdae6e 862 for (Int_t ipix = 0; ipix < nPix; ++ipix)
c0a16418 863 {
864 if (probi[ipix] < 0.01)
865 {
866 AliMUONPad* pixel = Pixel(ipix);
867 AliDebug(2,Form("Setting the following pixel to invisible as its probi<0.01:"));
2abdae6e 868 //StdoutToAliDebug(2,cout << Form(" -- ipix %3d --- "); pixel->Print(););
c0a16418 869 pixel->SetCharge(0); // "invisible" pixel
870 }
871 }
872
873 // MLEM algorithm
874 Mlem(cluster,coef, probi, 15);
875
8c718ff8 876 // Find histogram limits for the 1'st pass only - for others computed below
877 if (lc == 1) {
878 for ( Int_t ipix = 1; ipix < nPix; ++ipix )
879 {
880 pixPtr = Pixel(ipix);
881 for ( Int_t i = 0; i < 2; ++i )
882 {
883 Int_t indx = i * 2;
884 if (pixPtr->Coord(i) < xylim[indx]) xylim[indx] = pixPtr->Coord(i);
885 else if (-pixPtr->Coord(i) < xylim[indx+1]) xylim[indx+1] = -pixPtr->Coord(i);
886 }
887 }
888 } else pixPtr = Pixel(0);
889
2abdae6e 890 for (Int_t i = 0; i < 4; i++)
c0a16418 891 {
892 xylim[i] -= pixPtr->Size(i/2);
893 }
c0a16418 894
895 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
896 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
897
2abdae6e 898 //StdoutToAliDebug(2,cout << "pixel used for nx,ny computation : "; pixPtr->Print(););
c0a16418 899 AliDebug(2,Form("lc %d pixPtr size = %e,%e nx,ny=%d,%d xylim=%e,%e,%e,%e",
900 lc,pixPtr->Size(0),pixPtr->Size(1),nx,ny,
901 xylim[0],-xylim[1],xylim[2],-xylim[3]
902 ));
903
110edb51 904 AliDebug(2,Form("LowestPadCharge=%e",fLowestPadCharge));
905
906 delete fHistMlem;
907
c80b29a4 908 fHistMlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
c0a16418 909
2abdae6e 910 for (Int_t ipix = 0; ipix < nPix; ++ipix)
c0a16418 911 {
bf0d3528 912 AliMUONPad* pixPtr2 = Pixel(ipix);
913 fHistMlem->Fill(pixPtr2->Coord(0),pixPtr2->Coord(1),pixPtr2->Charge());
c0a16418 914 }
915
916 // Check if the total charge of pixels is too low
917 Double_t qTot = 0;
2abdae6e 918 for ( Int_t i = 0; i < nPix; ++i)
c0a16418 919 {
920 qTot += Pixel(i)->Charge();
921 }
922
110edb51 923 if ( qTot < 1.e-4 || ( npadOK < 3 && qTot < fLowestClusterCharge ) )
c0a16418 924 {
925 AliDebug(1,Form("Deleting the above cluster (charge %e too low, npadOK=%d)",qTot,npadOK));
926 delete [] coef;
927 delete [] probi;
c0a16418 928 fPixArray->Delete();
2abdae6e 929 for ( Int_t i = 0; i < npadTot; ++i)
c0a16418 930 {
931 AliMUONPad* pad = cluster.Pad(i);
8c718ff8 932 //if ( pad->Status() == 0) pad->SetStatus(-1);
933 if ( pad->Status() == fgkZero) pad->SetStatus(fgkOver);
c0a16418 934 }
935 return kFALSE;
936 }
937
938 if (iSimple)
939 {
940 // Simple cluster - skip further passes thru EM-procedure
941 Simple(cluster);
942 delete [] coef;
943 delete [] probi;
c0a16418 944 fPixArray->Delete();
945 return kTRUE;
946 }
947
948 // Calculate position of the center-of-gravity around the maximum pixel
949 Double_t xyCOG[2];
c80b29a4 950 FindCOG(xyCOG);
c0a16418 951
952 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 &&
953 pixPtr->Size(0) > pixPtr->Size(1)) break;
954
955 // Sort pixels according to the charge
8c718ff8 956 MaskPeaks(1); // mask local maxima
c0a16418 957 fPixArray->Sort();
8c718ff8 958 MaskPeaks(0); // unmask local maxima
c0a16418 959 Double_t pixMin = 0.01*Pixel(0)->Charge();
110edb51 960 pixMin = TMath::Min(pixMin,100*fLowestPixelCharge);
c0a16418 961
962 // Decrease pixel size and shift pixels to make them centered at
963 // the maximum one
964 Int_t indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
965 Int_t ix(1);
966 Double_t width = 0;
967 Double_t shift[2] = { 0.0, 0.0 };
2abdae6e 968 for (Int_t i = 0; i < 4; ++i) xylim[i] = 999;
c0a16418 969 Int_t nPix1 = nPix;
970 nPix = 0;
2abdae6e 971 for (Int_t ipix = 0; ipix < nPix1; ++ipix)
c0a16418 972 {
bf0d3528 973 AliMUONPad* pixPtr2 = Pixel(ipix);
c0a16418 974 if ( nPix >= npadOK // too many pixels already
975 ||
94bf739c 976 ((pixPtr2->Charge() < pixMin) && (pixPtr2->Status() != fgkMustKeep)) // too low charge
c0a16418 977 )
978 {
979 RemovePixel(ipix);
980 continue;
981 }
2abdae6e 982 for (Int_t i = 0; i < 2; ++i)
c0a16418 983 {
984 if (!i)
985 {
110edb51 986 pixPtr2->SetCharge(fLowestPadCharge);
bf0d3528 987 pixPtr2->SetSize(indx, pixPtr2->Size(indx)/2);
988 width = -pixPtr2->Size(indx);
989 pixPtr2->Shift(indx, width);
c0a16418 990 // Shift pixel position
991 if (ix)
992 {
993 ix = 0;
2abdae6e 994 for (Int_t j = 0; j < 2; ++j)
c0a16418 995 {
bf0d3528 996 shift[j] = pixPtr2->Coord(j) - xyCOG[j];
997 shift[j] -= ((Int_t)(shift[j]/pixPtr2->Size(j)/2))*pixPtr2->Size(j)*2;
c0a16418 998 }
999 } // if (ix)
bf0d3528 1000 pixPtr2->Shift(0, -shift[0]);
1001 pixPtr2->Shift(1, -shift[1]);
8c718ff8 1002 ++nPix;
c0a16418 1003 }
8c718ff8 1004 else if (nPix < npadOK)
c0a16418 1005 {
bf0d3528 1006 pixPtr2 = new AliMUONPad(*pixPtr2);
1007 pixPtr2->Shift(indx, -2*width);
1008 pixPtr2->SetStatus(fgkZero);
1009 fPixArray->Add(pixPtr2);
8c718ff8 1010 ++nPix;
c0a16418 1011 }
8c718ff8 1012 else continue; // skip adjustment of histo limits
1013 for (Int_t j = 0; j < 4; ++j)
c0a16418 1014 {
bf0d3528 1015 xylim[j] = TMath::Min (xylim[j], (j%2 ? -1 : 1)*pixPtr2->Coord(j/2));
c0a16418 1016 }
1017 } // for (Int_t i=0; i<2;
c0a16418 1018 } // for (Int_t ipix=0;
1019
1020 fPixArray->Compress();
c0a16418 1021
1022 AliDebug(2,Form("After shift:"));
2abdae6e 1023 //StdoutToAliDebug(2,fPixArray->Print("","full"););
1024 //Plot(Form("mlem.lc%d",lc+1));
c0a16418 1025
1026 AliDebug(2,Form(" xyCOG=%9.6f %9.6f xylim=%9.6f,%9.6f,%9.6f,%9.6f",
1027 xyCOG[0],xyCOG[1],
1028 xylim[0],xylim[1],
1029 xylim[2],xylim[3]));
1030
8c718ff8 1031 if (nPix < npadOK)
c0a16418 1032 {
bf0d3528 1033 AliMUONPad* pixPtr2 = Pixel(0);
8c718ff8 1034 // add pixels if the maximum is at the limit of pixel area:
c0a16418 1035 // start from Y-direction
1036 Int_t j = 0;
2abdae6e 1037 for (Int_t i = 3; i > -1; --i)
c0a16418 1038 {
1039 if (nPix < npadOK &&
bf0d3528 1040 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr2->Size(i/2))
c0a16418 1041 {
8c718ff8 1042 //AliMUONPad* p = static_cast<AliMUONPad*>(pixPtr->Clone());
bf0d3528 1043 AliMUONPad* p = new AliMUONPad(*pixPtr2);
1044 p->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr2->Size(i/2));
8c718ff8 1045 xylim[i] = p->Coord(i/2) * (i%2 ? -1 : 1); // update histo limits
c0a16418 1046 j = TMath::Even (i/2);
1047 p->SetCoord(j, xyCOG[j]);
1048 AliDebug(2,Form("Adding pixel on the edge (i=%d) ",i));
2abdae6e 1049 //StdoutToAliDebug(2,cout << " ---- ";
1050 // p->Print("corners"););
c0a16418 1051 fPixArray->Add(p);
1052 ++nPix;
1053 }
1054 }
1055 }
c0a16418 1056 nPix = fPixArray->GetEntriesFast();
1057 delete [] coef;
1058 delete [] probi;
c0a16418 1059 } // while (1)
1060
1061 AliDebug(2,Form("At the end of while loop nPix=%d : ",fPixArray->GetLast()+1));
2abdae6e 1062 //StdoutToAliDebug(2,fPixArray->Print("","full"););
c0a16418 1063
1064 // remove pixels with low signal or low visibility
1065 // Cuts are empirical !!!
110edb51 1066 Double_t thresh = TMath::Max (fHistMlem->GetMaximum()/100.,2.0*fLowestPixelCharge);
1067 thresh = TMath::Min (thresh,100.0*fLowestPixelCharge);
c0a16418 1068 Double_t charge = 0;
1069
c0a16418 1070 // Mark pixels which should be removed
2abdae6e 1071 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1072 {
bf0d3528 1073 AliMUONPad* pixPtr2 = Pixel(i);
1074 charge = pixPtr2->Charge();
c0a16418 1075 if (charge < thresh)
1076 {
bf0d3528 1077 pixPtr2->SetCharge(-charge);
c0a16418 1078 }
1079 }
1080
1081 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1082 Int_t near = 0;
2abdae6e 1083 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1084 {
bf0d3528 1085 AliMUONPad* pixPtr2 = Pixel(i);
1086 charge = pixPtr2->Charge();
c0a16418 1087 if (charge > 0) continue;
bf0d3528 1088 near = FindNearest(pixPtr2);
1089 pixPtr2->SetCharge(0);
c0a16418 1090 probi[i] = 0; // make it "invisible"
1091 AliMUONPad* pnear = Pixel(near);
1092 pnear->SetCharge(pnear->Charge() + (-charge));
1093 }
1094 Mlem(cluster,coef,probi,2);
1095
1096 AliDebug(2,Form("Before splitting nPix=%d EVT %d DE %d",fPixArray->GetLast()+1,fEventNumber,fDetElemId));
2abdae6e 1097 //StdoutToAliDebug(2,fPixArray->Print("","full"););
1098 //Plot("mlem.beforesplit");
c0a16418 1099
8c718ff8 1100 // Update histogram
2abdae6e 1101 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1102 {
bf0d3528 1103 AliMUONPad* pixPtr2 = Pixel(i);
1104 Int_t ix = fHistMlem->GetXaxis()->FindBin(pixPtr2->Coord(0));
1105 Int_t iy = fHistMlem->GetYaxis()->FindBin(pixPtr2->Coord(1));
1106 fHistMlem->SetBinContent(ix, iy, pixPtr2->Charge());
c0a16418 1107 }
1108
1109 // Try to split into clusters
1110 Bool_t ok = kTRUE;
110edb51 1111 if (fHistMlem->GetSum() < 2.0*fLowestPixelCharge)
c0a16418 1112 {
1113 ok = kFALSE;
1114 }
1115 else
1116 {
c80b29a4 1117 fSplitter->Split(cluster,fHistMlem,coef,fClusterList);
c0a16418 1118 }
1119
1120 delete [] coef;
1121 delete [] probi;
c0a16418 1122 fPixArray->Delete();
1123
1124 return ok;
1125}
1126
8c718ff8 1127//_____________________________________________________________________________
1128void AliMUONClusterFinderMLEM::MaskPeaks(Int_t mask)
1129{
1130 /// Mask/unmask pixels corresponding to local maxima (add/subtract 10000 to their charge
1131 /// - to avoid loosing low charge pixels after sorting)
1132
1133 for (Int_t i = 0; i < fPixArray->GetEntriesFast(); ++i) {
1134 AliMUONPad* pix = Pixel(i);
1135 if (pix->Status() == fgkMustKeep) {
1136 if (mask == 1) pix->SetCharge(pix->Charge()+10000.);
1137 else pix->SetCharge(pix->Charge()-10000.);
1138 }
1139 }
1140}
1141
c0a16418 1142//_____________________________________________________________________________
1143void AliMUONClusterFinderMLEM::Mlem(AliMUONCluster& cluster,
72dae9ff 1144 const Double_t* coef, Double_t* probi,
c0a16418 1145 Int_t nIter)
1146{
1147 /// Use MLEM to find pixel charges
1148
1149 Int_t nPix = fPixArray->GetEntriesFast();
1150
1151 Int_t npad = cluster.Multiplicity();
1152
1153 Double_t* probi1 = new Double_t[nPix];
2abdae6e 1154 Double_t probMax = TMath::MaxElement(nPix,probi);
c0a16418 1155
2abdae6e 1156 for (Int_t iter = 0; iter < nIter; ++iter)
c0a16418 1157 {
1158 // Do iterations
2abdae6e 1159 for (Int_t ipix = 0; ipix < nPix; ++ipix)
c0a16418 1160 {
9bbd7f60 1161 Pixel(ipix)->SetChargeBackup(0);
c0a16418 1162 // Correct each pixel
824fb5ed 1163 probi1[ipix] = 0;
c0a16418 1164 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1165 Double_t sum = 0;
1166 probi1[ipix] = probMax;
2abdae6e 1167 for (Int_t j = 0; j < npad; ++j)
c0a16418 1168 {
1169 AliMUONPad* pad = cluster.Pad(j);
8c718ff8 1170 //if (pad->Status() < 0) continue;
1171 if (pad->Status() != fgkZero) continue;
c0a16418 1172 Double_t sum1 = 0;
1173 Int_t indx1 = j*nPix;
1174 Int_t indx = indx1 + ipix;
1175 // Calculate expectation
2abdae6e 1176 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1177 {
1178 sum1 += Pixel(i)->Charge()*coef[indx1+i];
2abdae6e 1179 //cout << i << " " << Pixel(i)->Charge() << " " << coef[indx1+i] << endl;
c0a16418 1180 }
2abdae6e 1181 if ( pad->IsSaturated() && sum1 > pad->Charge() )
c0a16418 1182 {
c0a16418 1183 // correct for pad charge overflows
1184 probi1[ipix] -= coef[indx];
1185 continue;
2abdae6e 1186 //sum1 = pad->Charge();
c0a16418 1187 }
1188
2abdae6e 1189 if (sum1 > 1.e-6) sum += pad->Charge()*coef[indx]/sum1;
c0a16418 1190 } // for (Int_t j=0;
1191 AliMUONPad* pixPtr = Pixel(ipix);
1192 if (probi1[ipix] > 1.e-6)
1193 {
9bbd7f60 1194 //AZ pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1195 pixPtr->SetChargeBackup(pixPtr->Charge()*sum/probi1[ipix]);
c0a16418 1196 }
2abdae6e 1197 //cout << " xxx " << ipix << " " << pixPtr->Charge() << " " << pixPtr->ChargeBackup() << " " << sum << " " << probi1[ipix] << endl;
c0a16418 1198 } // for (Int_t ipix=0;
2abdae6e 1199 Double_t qTot = 0;
9bbd7f60 1200 for (Int_t i = 0; i < nPix; ++i) {
1201 AliMUONPad* pixPtr = Pixel(i);
2abdae6e 1202 pixPtr->RevertCharge();
1203 qTot += pixPtr->Charge();
1204 }
1205 if (qTot < 1.e-6) {
1206 // Can happen in clusters with large number of overflows - speeding up
1207 delete [] probi1;
1208 return;
9bbd7f60 1209 }
c0a16418 1210 } // for (Int_t iter=0;
1211 delete [] probi1;
1212}
1213
1214//_____________________________________________________________________________
c80b29a4 1215void AliMUONClusterFinderMLEM::FindCOG(Double_t *xyc)
c0a16418 1216{
1217 /// Calculate position of the center-of-gravity around the maximum pixel
1218
1219 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1220 Int_t i1 = -9, j1 = -9;
c80b29a4 1221 fHistMlem->GetMaximumBin(ixmax,iymax,ix);
1222 Int_t nx = fHistMlem->GetNbinsX();
1223 Int_t ny = fHistMlem->GetNbinsY();
1224 Double_t thresh = fHistMlem->GetMaximum()/10;
c0a16418 1225 Double_t x, y, cont, xq=0, yq=0, qq=0;
1226
2abdae6e 1227 Int_t ie = TMath::Min(ny,iymax+1), je = TMath::Min(nx,ixmax+1);
1228 for (Int_t i = TMath::Max(1,iymax-1); i <= ie; ++i) {
c80b29a4 1229 y = fHistMlem->GetYaxis()->GetBinCenter(i);
2abdae6e 1230 for (Int_t j = TMath::Max(1,ixmax-1); j <= je; ++j) {
c80b29a4 1231 cont = fHistMlem->GetCellContent(j,i);
c0a16418 1232 if (cont < thresh) continue;
1233 if (i != i1) {i1 = i; nsumy++;}
1234 if (j != j1) {j1 = j; nsumx++;}
c80b29a4 1235 x = fHistMlem->GetXaxis()->GetBinCenter(j);
c0a16418 1236 xq += x*cont;
1237 yq += y*cont;
1238 qq += cont;
1239 nsum++;
1240 }
1241 }
1242
1243 Double_t cmax = 0;
1244 Int_t i2 = 0, j2 = 0;
1245 x = y = 0;
1246 if (nsumy == 1) {
1247 // one bin in Y - add one more (with the largest signal)
2abdae6e 1248 for (Int_t i = TMath::Max(1,iymax-1); i <= ie; ++i) {
c0a16418 1249 if (i == iymax) continue;
2abdae6e 1250 for (Int_t j = TMath::Max(1,ixmax-1); j <= je; ++j) {
c80b29a4 1251 cont = fHistMlem->GetCellContent(j,i);
c0a16418 1252 if (cont > cmax) {
1253 cmax = cont;
c80b29a4 1254 x = fHistMlem->GetXaxis()->GetBinCenter(j);
1255 y = fHistMlem->GetYaxis()->GetBinCenter(i);
c0a16418 1256 i2 = i;
1257 j2 = j;
1258 }
1259 }
1260 }
1261 xq += x*cmax;
1262 yq += y*cmax;
1263 qq += cmax;
1264 if (i2 != i1) nsumy++;
1265 if (j2 != j1) nsumx++;
1266 nsum++;
1267 } // if (nsumy == 1)
1268
1269 if (nsumx == 1) {
1270 // one bin in X - add one more (with the largest signal)
1271 cmax = x = y = 0;
2abdae6e 1272 for (Int_t j = TMath::Max(1,ixmax-1); j <= je; ++j) {
c0a16418 1273 if (j == ixmax) continue;
2abdae6e 1274 for (Int_t i = TMath::Max(1,iymax-1); i <= ie; ++i) {
c80b29a4 1275 cont = fHistMlem->GetCellContent(j,i);
c0a16418 1276 if (cont > cmax) {
1277 cmax = cont;
c80b29a4 1278 x = fHistMlem->GetXaxis()->GetBinCenter(j);
1279 y = fHistMlem->GetYaxis()->GetBinCenter(i);
c0a16418 1280 i2 = i;
1281 j2 = j;
1282 }
1283 }
1284 }
1285 xq += x*cmax;
1286 yq += y*cmax;
1287 qq += cmax;
1288 if (i2 != i1) nsumy++;
1289 if (j2 != j1) nsumx++;
1290 nsum++;
1291 } // if (nsumx == 1)
1292
1293 xyc[0] = xq/qq; xyc[1] = yq/qq;
1294 AliDebug(2,Form("x,y COG = %e,%e",xyc[0],xyc[1]));
1295}
1296
1297//_____________________________________________________________________________
72dae9ff 1298Int_t AliMUONClusterFinderMLEM::FindNearest(const AliMUONPad *pixPtr0)
c0a16418 1299{
1300/// Find the pixel nearest to the given one
1301/// (algorithm may be not very efficient)
1302
1303 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1304 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1305 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1306 AliMUONPad *pixPtr;
1307
2abdae6e 1308 for (Int_t i = 0; i < nPix; ++i) {
c0a16418 1309 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
110edb51 1310 if (pixPtr == pixPtr0 || pixPtr->Charge() < fLowestPixelCharge) continue;
c0a16418 1311 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1312 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1313 r = dx *dx + dy * dy;
1314 if (r < rmin) { rmin = r; imin = i; }
1315 }
1316 return imin;
1317}
1318
c0a16418 1319//_____________________________________________________________________________
1320void
1321AliMUONClusterFinderMLEM::Paint(Option_t*)
1322{
1323 /// Paint cluster and pixels
1324
1325 AliMpArea area(fPreCluster->Area());
1326
1327 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
1328
1329 gVirtualX->SetFillStyle(1001);
1330 gVirtualX->SetFillColor(3);
1331 gVirtualX->SetLineColor(3);
1332
1333 Double_t s(1.0);
1334
1335 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1336 {
1337 AliMUONPad* pixel = Pixel(i);
1338
1339 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0)*s,
1340 pixel->Coord(1)-pixel->Size(1)*s,
1341 pixel->Coord(0)+pixel->Size(0)*s,
1342 pixel->Coord(1)+pixel->Size(1)*s);
1343
1344// for ( Int_t sign = -1; sign < 2; sign +=2 )
1345// {
1346// gPad->PaintLine(pixel->Coord(0) - pixel->Size(0),
1347// pixel->Coord(1) + sign*pixel->Size(1),
1348// pixel->Coord(0) + pixel->Size(0),
1349// pixel->Coord(1) - sign*pixel->Size(1)
1350// );
1351// }
1352 }
1353
1354
1355 gVirtualX->SetFillStyle(0);
1356
1357 fPreCluster->Paint();
1358
1359 gVirtualX->SetLineColor(1);
1360 gVirtualX->SetLineWidth(2);
1361 gVirtualX->SetFillStyle(0);
1362 gVirtualX->SetTextColor(1);
1363 gVirtualX->SetTextAlign(22);
1364
1365 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1366 {
1367 AliMUONPad* pixel = Pixel(i);
1368 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0),
1369 pixel->Coord(1)-pixel->Size(1),
1370 pixel->Coord(0)+pixel->Size(0),
1371 pixel->Coord(1)+pixel->Size(1));
1372 gVirtualX->SetTextSize(pixel->Size(0)*60);
1373
1374 gPad->PaintText(pixel->Coord(0),pixel->Coord(1),Form("%d",(Int_t)(pixel->Charge())));
1375 }
1376}
1377
1378//_____________________________________________________________________________
1379Int_t AliMUONClusterFinderMLEM::FindLocalMaxima(TObjArray *pixArray, Int_t *localMax, Double_t *maxVal)
1380{
1381/// Find local maxima in pixel space for large preclusters in order to
1382/// try to split them into smaller pieces (to speed up the MLEM procedure)
1383/// or to find additional fitting seeds if clusters were not completely resolved
1384
1385 AliDebug(1,Form("nPix=%d",pixArray->GetLast()+1));
1386
c0a16418 1387 Double_t xylim[4] = {999, 999, 999, 999};
1388
1389 Int_t nPix = pixArray->GetEntriesFast();
1390 AliMUONPad *pixPtr = 0;
2abdae6e 1391 for (Int_t ipix = 0; ipix < nPix; ++ipix) {
c0a16418 1392 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
2abdae6e 1393 for (Int_t i = 0; i < 4; ++i)
c0a16418 1394 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1395 }
2abdae6e 1396 for (Int_t i = 0; i < 4; ++i) xylim[i] -= pixPtr->Size(i/2);
c0a16418 1397
1398 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1399 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
c80b29a4 1400 if (pixArray == fPixArray) fHistAnode = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1401 else fHistAnode = new TH2D("anode1","anode1",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
2abdae6e 1402 for (Int_t ipix = 0; ipix < nPix; ++ipix) {
c0a16418 1403 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
c80b29a4 1404 fHistAnode->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
c0a16418 1405 }
1406// if (fDraw && pixArray == fPixArray) fDraw->DrawHist("c2", hist);
1407
2abdae6e 1408 Int_t nMax = 0, indx, nxy = ny * nx;
1409 Int_t *isLocalMax = new Int_t[nxy];
1410 for (Int_t i = 0; i < nxy; ++i) isLocalMax[i] = 0;
c0a16418 1411
2abdae6e 1412 for (Int_t i = 1; i <= ny; ++i) {
c0a16418 1413 indx = (i-1) * nx;
2abdae6e 1414 for (Int_t j = 1; j <= nx; ++j) {
110edb51 1415 if (fHistAnode->GetCellContent(j,i) < fLowestPixelCharge) continue;
c0a16418 1416 //if (isLocalMax[indx+j-1] < 0) continue;
1417 if (isLocalMax[indx+j-1] != 0) continue;
c80b29a4 1418 FlagLocalMax(fHistAnode, i, j, isLocalMax);
c0a16418 1419 }
1420 }
1421
2abdae6e 1422 for (Int_t i = 1; i <= ny; ++i) {
c0a16418 1423 indx = (i-1) * nx;
2abdae6e 1424 for (Int_t j = 1; j <= nx; ++j) {
c0a16418 1425 if (isLocalMax[indx+j-1] > 0) {
1426 localMax[nMax] = indx + j - 1;
c80b29a4 1427 maxVal[nMax++] = fHistAnode->GetCellContent(j,i);
1428 ((AliMUONPad*)fSplitter->BinToPix(fHistAnode, j, i))->SetStatus(fgkMustKeep);
85a72902 1429 if (nMax > 99) break;
c0a16418 1430 }
1431 }
85a72902 1432 if (nMax > 99) {
1433 AliError(" Too many local maxima !!!");
1434 break;
1435 }
c0a16418 1436 }
1437 if (fDebug) cout << " Local max: " << nMax << endl;
2abdae6e 1438 delete [] isLocalMax;
c0a16418 1439 return nMax;
1440}
1441
1442//_____________________________________________________________________________
1443void AliMUONClusterFinderMLEM::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
1444{
1445/// Flag pixels (whether or not local maxima)
1446
1447 Int_t nx = hist->GetNbinsX();
1448 Int_t ny = hist->GetNbinsY();
1449 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
1450 Int_t cont1 = 0, indx = (i-1)*nx+j-1, indx1 = 0, indx2 = 0;
1451
2abdae6e 1452 Int_t ie = i + 2, je = j + 2;
1453 for (Int_t i1 = i-1; i1 < ie; ++i1) {
c0a16418 1454 if (i1 < 1 || i1 > ny) continue;
1455 indx1 = (i1 - 1) * nx;
2abdae6e 1456 for (Int_t j1 = j-1; j1 < je; ++j1) {
c0a16418 1457 if (j1 < 1 || j1 > nx) continue;
1458 if (i == i1 && j == j1) continue;
1459 indx2 = indx1 + j1 - 1;
1460 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
1461 if (cont < cont1) { isLocalMax[indx] = -1; return; }
1462 else if (cont > cont1) isLocalMax[indx2] = -1;
1463 else { // the same charge
1464 isLocalMax[indx] = 1;
1465 if (isLocalMax[indx2] == 0) {
1466 FlagLocalMax(hist, i1, j1, isLocalMax);
1467 if (isLocalMax[indx2] < 0) { isLocalMax[indx] = -1; return; }
1468 else isLocalMax[indx2] = -1;
1469 }
1470 }
1471 }
1472 }
1473 isLocalMax[indx] = 1; // local maximum
1474}
1475
1476//_____________________________________________________________________________
1477void AliMUONClusterFinderMLEM::FindCluster(AliMUONCluster& cluster,
72dae9ff 1478 const Int_t *localMax, Int_t iMax)
c0a16418 1479{
1480/// Find pixel cluster around local maximum \a iMax and pick up pads
1481/// overlapping with it
1482
2abdae6e 1483 /* Just for check
1484 TCanvas* c = new TCanvas("Anode","Anode",800,600);
1485 c->cd();
1486 hist->Draw("lego1Fb"); // debug
1487 c->Update();
1488 Int_t tmp;
1489 cin >> tmp;
1490 */
c80b29a4 1491 Int_t nx = fHistAnode->GetNbinsX();
1492 Int_t ny = fHistAnode->GetNbinsY();
c0a16418 1493 Int_t ic = localMax[iMax] / nx + 1;
1494 Int_t jc = localMax[iMax] % nx + 1;
2abdae6e 1495 Int_t nxy = ny * nx;
1496 Bool_t *used = new Bool_t[nxy];
1497 for (Int_t i = 0; i < nxy; ++i) used[i] = kFALSE;
c0a16418 1498
1499 // Drop all pixels from the array - pick up only the ones from the cluster
1500 fPixArray->Delete();
1501
c80b29a4 1502 Double_t wx = fHistAnode->GetXaxis()->GetBinWidth(1)/2;
1503 Double_t wy = fHistAnode->GetYaxis()->GetBinWidth(1)/2;
1504 Double_t yc = fHistAnode->GetYaxis()->GetBinCenter(ic);
1505 Double_t xc = fHistAnode->GetXaxis()->GetBinCenter(jc);
1506 Double_t cont = fHistAnode->GetCellContent(jc,ic);
c0a16418 1507 fPixArray->Add(new AliMUONPad (xc, yc, wx, wy, cont));
1508 used[(ic-1)*nx+jc-1] = kTRUE;
c80b29a4 1509 AddBinSimple(fHistAnode, ic, jc);
2abdae6e 1510 //fSplitter->AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
c0a16418 1511
1512 Int_t nPix = fPixArray->GetEntriesFast();
1513 Int_t npad = cluster.Multiplicity();
1514
2abdae6e 1515 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1516 {
1517 AliMUONPad* pixPtr = Pixel(i);
1518 pixPtr->SetSize(0,wx);
1519 pixPtr->SetSize(1,wy);
1520 }
1521
1522 // Pick up pads which overlap with found pixels
2abdae6e 1523 for (Int_t i = 0; i < npad; ++i)
c0a16418 1524 {
8c718ff8 1525 //cluster.Pad(i)->SetStatus(-1);
1526 cluster.Pad(i)->SetStatus(fgkOver); // just the dirty trick
c0a16418 1527 }
1528
2abdae6e 1529 for (Int_t i = 0; i < nPix; ++i)
c0a16418 1530 {
1531 AliMUONPad* pixPtr = Pixel(i);
2abdae6e 1532 for (Int_t j = 0; j < npad; ++j)
c0a16418 1533 {
1534 AliMUONPad* pad = cluster.Pad(j);
8c718ff8 1535 //if (pad->Status() == 0) continue;
1536 if (pad->Status() == fgkZero) continue;
c0a16418 1537 if ( Overlap(*pad,*pixPtr) )
1538 {
8c718ff8 1539 //pad->SetStatus(0);
1540 pad->SetStatus(fgkZero);
2abdae6e 1541 if (fDebug) { cout << j << " "; pad->Print("full"); }
c0a16418 1542 }
1543 }
1544 }
1545
2abdae6e 1546 delete [] used;
1547}
1548
1549//_____________________________________________________________________________
1550void
c80b29a4 1551AliMUONClusterFinderMLEM::AddBinSimple(TH2D *hist, Int_t ic, Int_t jc)
2abdae6e 1552{
1553 /// Add adjacent bins (+-1 in X and Y) to the cluster
1554
c80b29a4 1555 Int_t nx = hist->GetNbinsX();
1556 Int_t ny = hist->GetNbinsY();
1557 Double_t cont1, cont = hist->GetCellContent(jc,ic);
2abdae6e 1558 AliMUONPad *pixPtr = 0;
1559
1560 Int_t ie = TMath::Min(ic+1,ny), je = TMath::Min(jc+1,nx);
1561 for (Int_t i = TMath::Max(ic-1,1); i <= ie; ++i) {
1562 for (Int_t j = TMath::Max(jc-1,1); j <= je; ++j) {
c80b29a4 1563 cont1 = hist->GetCellContent(j,i);
2abdae6e 1564 if (cont1 > cont) continue;
110edb51 1565 if (cont1 < fLowestPixelCharge) continue;
c80b29a4 1566 pixPtr = new AliMUONPad (hist->GetXaxis()->GetBinCenter(j),
1567 hist->GetYaxis()->GetBinCenter(i), 0, 0, cont1);
2abdae6e 1568 fPixArray->Add(pixPtr);
1569 }
1570 }
c0a16418 1571}
1572
1573//_____________________________________________________________________________
1574AliMUONClusterFinderMLEM&
1575AliMUONClusterFinderMLEM::operator=(const AliMUONClusterFinderMLEM& rhs)
1576{
1577/// Protected assignement operator
1578
1579 if (this == &rhs) return *this;
1580
1581 AliFatal("Not implemented.");
1582
1583 return *this;
1584}
1585
c0a16418 1586//_____________________________________________________________________________
1587void AliMUONClusterFinderMLEM::AddVirtualPad(AliMUONCluster& cluster)
1588{
2abdae6e 1589 /// Add virtual pad (with small charge) to improve fit for clusters
1590 /// with number of pads == 2 per direction
c0a16418 1591
2abdae6e 1592 // Find out non-bending and bending planes
1593 Int_t nonb[2] = {1, 0}; // non-bending and bending cathodes
c0a16418 1594
2abdae6e 1595 TVector2 dim0 = cluster.MinPadDimensions(0, 0, kTRUE);
1596 TVector2 dim1 = cluster.MinPadDimensions(1, 0, kTRUE);
1597 if (dim0.X() < dim1.X() - fgkDistancePrecision) {
1598 nonb[0] = 0;
1599 nonb[1] = 1;
1600 }
1601
1602 Bool_t same = kFALSE;
1603 if (TMath::Abs(dim0.Y()-dim1.Y()) < fgkDistancePrecision) same = kTRUE; // the same pad size on both planes
1604
168e9c4d 1605 Long_t cn;
2abdae6e 1606 Bool_t check[2] = {kFALSE, kFALSE};
1607 Int_t nxy[2];
1608 nxy[0] = nxy[1] = 0;
1609 for (Int_t inb = 0; inb < 2; ++inb) {
1610 cn = cluster.NofPads(nonb[inb], 0, kTRUE);
168e9c4d 1611 if (inb == 0 && AliMp::PairFirst(cn) == 2) check[inb] = kTRUE; // check non-bending plane
1612 else if (inb == 1 && AliMp::PairSecond(cn) == 2) check[inb] = kTRUE; // check bending plane
2abdae6e 1613 if (same) {
168e9c4d 1614 nxy[0] = TMath::Max (nxy[0], AliMp::PairFirst(cn));
1615 nxy[1] = TMath::Max (nxy[1], AliMp::PairSecond(cn));
2abdae6e 1616 if (inb == 0 && nxy[0] < 2) nonb[inb] = !nonb[inb];
168e9c4d 1617 else if (inb == 1 && AliMp::PairSecond(cn) < 2) nonb[inb] = !nonb[inb];
c0a16418 1618 }
1619 }
2abdae6e 1620 if (same) {
1621 if (nxy[0] > 2) check[0] = kFALSE;
1622 if (nxy[1] > 2) check[1] = kFALSE;
1623 }
1624 if (!check[0] && !check[1]) return;
1625
1626 for (Int_t inb = 0; inb < 2; ++inb) {
1627 if (!check[inb]) continue;
1628
1629 // Find pads with maximum and next to maximum charges
1630 Int_t maxPads[2] = {-1, -1};
1631 Double_t amax[2] = {0};
1632 Int_t mult = cluster.Multiplicity();
1633 for (Int_t j = 0; j < mult; ++j) {
1634 AliMUONPad *pad = cluster.Pad(j);
1635 if (pad->Cathode() != nonb[inb]) continue;
1636 for (Int_t j2 = 0; j2 < 2; ++j2) {
1637 if (pad->Charge() > amax[j2]) {
1638 if (j2 == 0) { amax[1] = amax[0]; maxPads[1] = maxPads[0]; }
1639 amax[j2] = pad->Charge();
1640 maxPads[j2] = j;
1641 break;
1642 }
c0a16418 1643 }
1644 }
c0a16418 1645
2abdae6e 1646 // Find min and max dimensions of the cluster
1647 Double_t limits[2] = {9999, -9999};
1648 for (Int_t j = 0; j < mult; ++j) {
1649 AliMUONPad *pad = cluster.Pad(j);
1650 if (pad->Cathode() != nonb[inb]) continue;
1651 if (pad->Coord(inb) < limits[0]) limits[0] = pad->Coord(inb);
1652 if (pad->Coord(inb) > limits[1]) limits[1] = pad->Coord(inb);
c0a16418 1653 }
2abdae6e 1654
1655 // Loop over max and next to max pads
1656 Bool_t add = kFALSE;
1657 Int_t idirAdd = 0;
1658 for (Int_t j = 0; j < 2; ++j) {
1659 if (j == 1) {
1660 if (maxPads[j] < 0) continue;
1661 if (!add) break;
1662 if (amax[1] / amax[0] < 0.5) break;
c0a16418 1663 }
2abdae6e 1664 // Check if pad at the cluster limit
1665 AliMUONPad *pad = cluster.Pad(maxPads[j]);
1666 Int_t idir = 0;
1667 if (TMath::Abs(pad->Coord(inb)-limits[0]) < fgkDistancePrecision) idir = -1;
1668 else if (TMath::Abs(pad->Coord(inb)-limits[1]) < fgkDistancePrecision) idir = 1;
1669 else {
1670 //cout << " *** Pad not at the cluster limit: " << j << endl;
1671 break;
c0a16418 1672 }
2abdae6e 1673 if (j == 1 && idir == idirAdd) break; // this pad has been already added
1674
1675 // Add pad (if it exists)
1676 TVector2 pos;
1677 if (inb == 0) pos.Set (pad->X() + idir * (pad->DX()+fgkDistancePrecision), pad->Y());
1678 else pos.Set (pad->X(), pad->Y() + idir * (pad->DY()+fgkDistancePrecision));
72dae9ff 1679 //AliMpPad mppad = fkSegmentation[nonb[inb]]->PadByPosition(pos,kTRUE);
6e97fbb8 1680 AliMpPad mppad = fkSegmentation[nonb[inb]]->PadByPosition(pos.X(), pos.Y(),kFALSE);
2abdae6e 1681 if (!mppad.IsValid()) continue; // non-existing pad
168e9c4d 1682 AliMUONPad muonPad(fDetElemId, nonb[inb], mppad.GetIx(), mppad.GetIy(),
6e97fbb8 1683 mppad.GetPositionX(), mppad.GetPositionY(),
1684 mppad.GetDimensionX(), mppad.GetDimensionY(), 0);
110edb51 1685 if (inb == 0) muonPad.SetCharge(TMath::Min (amax[j]/100, fLowestPadCharge));
8c718ff8 1686 //else muonPad.SetCharge(TMath::Min (amax[j]/15, fgkZeroSuppression));
110edb51 1687 else muonPad.SetCharge(TMath::Min (amax[j]/15, fLowestPadCharge));
1688 if (muonPad.Charge() < 2.0*fLowestPixelCharge) muonPad.SetCharge(2.0*fLowestPixelCharge);
2abdae6e 1689 muonPad.SetReal(kFALSE);
1690 if (fDebug) printf(" ***** Add virtual pad in %d direction ***** %f %f %f %3d %3d %f %f \n",
1691 inb, muonPad.Charge(), muonPad.X(), muonPad.Y(), muonPad.Ix(),
1692 muonPad.Iy(), muonPad.DX(), muonPad.DY());
1693 cluster.AddPad(muonPad); // add pad to the cluster
1694 add = kTRUE;
1695 idirAdd = idir;
1696 }
1697 }
c0a16418 1698}
1699
1700//_____________________________________________________________________________
1701void AliMUONClusterFinderMLEM::PadsInXandY(AliMUONCluster& cluster,
1702 Int_t &nInX, Int_t &nInY) const
1703{
1704 /// Find number of pads in X and Y-directions (excluding virtual ones and
1705 /// overflows)
1706
8c718ff8 1707 //Int_t statusToTest = 1;
1708 Int_t statusToTest = fgkUseForFit;
c0a16418 1709
8c718ff8 1710 //if ( nInX < 0 ) statusToTest = 0;
1711 if ( nInX < 0 ) statusToTest = fgkZero;
c0a16418 1712
1713 Bool_t mustMatch(kTRUE);
1714
168e9c4d 1715 Long_t cn = cluster.NofPads(statusToTest,mustMatch);
c0a16418 1716
168e9c4d 1717 nInX = AliMp::PairFirst(cn);
1718 nInY = AliMp::PairSecond(cn);
c0a16418 1719}
1720
1721//_____________________________________________________________________________
1722void AliMUONClusterFinderMLEM::RemovePixel(Int_t i)
1723{
1724 /// Remove pixel at index i
1725 AliMUONPad* pixPtr = Pixel(i);
1726 fPixArray->RemoveAt(i);
1727 delete pixPtr;
1728}
1729
1730//_____________________________________________________________________________
1731void AliMUONClusterFinderMLEM::Simple(AliMUONCluster& cluster)
1732{
1733/// Process simple cluster (small number of pads) without EM-procedure
1734
1735 Int_t nForFit = 1, clustFit[1] = {0}, nfit;
1736 Double_t parOk[3] = {0.};
1737 TObjArray *clusters[1];
1738 clusters[0] = fPixArray;
1739
1740 AliDebug(1,Form("nPix=%d",fPixArray->GetLast()+1));
1741
2abdae6e 1742 Int_t mult = cluster.Multiplicity();
1743 for (Int_t i = 0; i < mult; ++i)
c0a16418 1744 {
1745 AliMUONPad* pad = cluster.Pad(i);
8c718ff8 1746 /*
9bbd7f60 1747 if ( pad->IsSaturated())
c0a16418 1748 {
1749 pad->SetStatus(-9);
1750 }
1751 else
1752 {
1753 pad->SetStatus(1);
1754 }
8c718ff8 1755 */
1756 if (!pad->IsSaturated()) pad->SetStatus(fgkUseForFit);
c0a16418 1757 }
c80b29a4 1758 nfit = fSplitter->Fit(cluster,1, nForFit, clustFit, clusters, parOk, fClusterList, fHistMlem);
c0a16418 1759}
1760
1761//_____________________________________________________________________________
1762AliMUONPad*
1763AliMUONClusterFinderMLEM::Pixel(Int_t i) const
1764{
1765 /// Returns pixel at index i
1766 return static_cast<AliMUONPad*>(fPixArray->UncheckedAt(i));
1767}
1768
1769//_____________________________________________________________________________
1770void
1771AliMUONClusterFinderMLEM::Print(Option_t* what) const
1772{
1773 /// printout
1774 TString swhat(what);
1775 swhat.ToLower();
1776 if ( swhat.Contains("precluster") )
1777 {
1778 if ( fPreCluster) fPreCluster->Print();
1779 }
1780}
1781
110edb51 1782//_____________________________________________________________________________
1783void
1784AliMUONClusterFinderMLEM::SetChargeHints(Double_t lowestPadCharge, Double_t lowestClusterCharge)
1785{
1786 /// Set some thresholds we use later on in the algorithm
1787 fLowestPadCharge=lowestPadCharge;
1788 fLowestClusterCharge=lowestClusterCharge;
1789 fLowestPixelCharge=fLowestPadCharge/12.0;
1790}
1791
c0a16418 1792