1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 /// \class AliMUONClusterFinderMLEM
20 /// Clusterizer class based on the Expectation-Maximization algorithm
22 /// Pre-clustering is handled by AliMUONPreClusterFinder
23 /// From a precluster a pixel array is built, and from this pixel array
24 /// a list of clusters is output (using AliMUONClusterSplitterMLEM).
26 /// \author Laurent Aphecetche (for the "new" C++ structure) and
27 /// Alexander Zinchenko, JINR Dubna, for the hardcore of it ;-)
29 #include "AliMUONClusterFinderMLEM.h"
31 #include "AliMUONCluster.h"
32 #include "AliMUONClusterSplitterMLEM.h"
33 #include "AliMUONVDigit.h"
34 #include "AliMUONPad.h"
35 #include "AliMUONPreClusterFinder.h"
37 #include "AliMpVPadIterator.h"
38 #include "AliMpVSegmentation.h"
39 #include "AliRunLoader.h"
40 #include "AliMUONVDigitStore.h"
41 #include <Riostream.h>
45 #include <TStopwatch.h>
48 //#include "AliCodeTimer.h"
51 ClassImp(AliMUONClusterFinderMLEM)
54 const Double_t AliMUONClusterFinderMLEM::fgkZeroSuppression = 6; // average zero suppression value
55 const Double_t AliMUONClusterFinderMLEM::fgkSaturation = 3000; // average saturation level
56 const Double_t AliMUONClusterFinderMLEM::fgkDistancePrecision = 1e-6; // (cm) used to check overlaps and so on
57 const TVector2 AliMUONClusterFinderMLEM::fgkIncreaseSize(-AliMUONClusterFinderMLEM::fgkDistancePrecision,-AliMUONClusterFinderMLEM::fgkDistancePrecision);
58 const TVector2 AliMUONClusterFinderMLEM::fgkDecreaseSize(AliMUONClusterFinderMLEM::fgkDistancePrecision,AliMUONClusterFinderMLEM::fgkDistancePrecision);
60 TMinuit* AliMUONClusterFinderMLEM::fgMinuit = 0x0;
62 //_____________________________________________________________________________
63 AliMUONClusterFinderMLEM::AliMUONClusterFinderMLEM(Bool_t plot, AliMUONVClusterFinder* clusterFinder)
64 : AliMUONVClusterFinder(),
65 fPreClusterFinder(clusterFinder),
74 fPixArray(new TObjArray(20)),
77 fTimers(new TObjArray(kLast)),
84 fSegmentation[1] = fSegmentation[0] = 0x0;
86 fPadBeg[0] = fPadBeg[1] = fCathBeg;
88 if (!fgMinuit) fgMinuit = new TMinuit(8);
90 fTimers->SetOwner(kTRUE);
92 for ( Int_t i = 0; i < kLast; ++i )
94 TStopwatch* t = new TStopwatch;
95 fTimers->AddLast(new TStopwatch);
99 if (fPlot) fDebug = 1;
102 //_____________________________________________________________________________
103 AliMUONClusterFinderMLEM::~AliMUONClusterFinderMLEM()
106 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
108 delete fPreClusterFinder;
109 for ( Int_t i = 0; i < kLast; ++i )
111 AliInfo(Form("Timer %d",i));
116 AliInfo(Form("Total clusters %d AddVirtualPad needed %d",
117 fNClusters,fNAddVirtualPads));
120 //_____________________________________________________________________________
122 AliMUONClusterFinderMLEM::Prepare(const AliMpVSegmentation* segmentations[2],
123 const AliMUONVDigitStore& digitStore)
125 /// Prepare for clustering
127 for ( Int_t i = 0; i < 2; ++i )
129 fSegmentation[i] = segmentations[i];
132 // Find out the DetElemId
135 TIter next(digitStore.CreateIterator());
136 AliMUONVDigit* d = static_cast<AliMUONVDigit*>(next());
140 fDetElemId = d->DetElemId();
143 if ( fDetElemId < 0 )
145 AliWarning("Could not find DE. Probably no digits at all ?");
150 fSplitter = new AliMUONClusterSplitterMLEM(fDetElemId,fPixArray);
151 fSplitter->SetDebug(fDebug);
153 // find out current event number, and reset the cluster number
154 fEventNumber = AliRunLoader::GetRunLoader()->GetEventNumber();
156 fClusterList.Delete();
158 AliDebug(3,Form("EVT %d DE %d",fEventNumber,fDetElemId));
160 return fPreClusterFinder->Prepare(segmentations,digitStore);
163 //_____________________________________________________________________________
165 AliMUONClusterFinderMLEM::NextCluster()
167 /// Return next cluster
169 // if the list of clusters is not void, pick one from there
170 TObject* o = fClusterList.At(++fClusterNumber);
171 if ( o != 0x0 ) return static_cast<AliMUONCluster*>(o);
173 //FIXME : at this point, must check whether we've used all the digits
174 //from precluster : if not, let the preclustering know about those unused
175 //digits, so it can reuse them
177 // if the cluster list is exhausted, we need to go to the next
178 // pre-cluster and treat it
180 fPreCluster = fPreClusterFinder->NextCluster();
188 // AliCodeTimerAuto("Excluding preclustering")
191 fClusterList.Delete(); // reset the list of clusters for this pre-cluster
192 fClusterNumber = -1; //AZ
196 // WorkOnPreCluster may have used only part of the pads, so we check that
197 // now, and let the unused pads be reused by the preclustering...
199 for ( Int_t i = 0; i < fPreCluster->Multiplicity(); ++i )
201 AliMUONPad* pad = fPreCluster->Pad(i);
202 if ( !pad->IsUsed() )
204 fPreClusterFinder->UsePad(*pad);
208 return NextCluster();
211 //_____________________________________________________________________________
213 AliMUONClusterFinderMLEM::WorkOnPreCluster()
215 /// Starting from a precluster, builds a pixel array, and then
216 /// extract clusters from this array
218 // Set saturation flag - it is not set if working directly with MC digits (w/out
219 // creating raw data) !!!
220 for (Int_t j = 0; j < fPreCluster->Multiplicity(); ++j) {
221 AliMUONPad* pad = fPreCluster->Pad(j);
222 if (pad->IsSaturated()) break;
223 if (pad->Charge() > fgkSaturation-1) pad->SetSaturated(kTRUE); //FIXME : remove usage of fgkSaturation
227 cout << " *** Event # " << AliRunLoader::GetRunLoader()->GetEventNumber()
228 << " det. elem.: " << fDetElemId << endl;
229 for (Int_t j=0; j<fPreCluster->Multiplicity(); ++j) {
230 AliMUONPad* pad = fPreCluster->Pad(j);
231 printf(" bbb %3d %1d %8.4f %8.4f %8.4f %8.4f %6.1f %3d %3d %2d %1d %1d \n",
232 j, pad->Cathode(), pad->Coord(0), pad->Coord(1), pad->DX()*2, pad->DY()*2,
233 pad->Charge(), pad->Ix(), pad->Iy(), pad->Status(), pad->IsReal(), pad->IsSaturated());
237 AliMUONCluster* cluster = CheckPrecluster(*fPreCluster);
238 if (!cluster) return kFALSE;
240 BuildPixArray(*cluster);
242 if ( fPixArray->GetLast() < 0 )
244 AliDebug(1,"No pixel for the above cluster");
249 // Use MLEM for cluster finder
250 Int_t nMax = 1, localMax[100], maxPos[100];
251 Double_t maxVal[100];
253 if (cluster->Multiplicity() > 50)
255 nMax = FindLocalMaxima(fPixArray, localMax, maxVal);
260 TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
263 Int_t iSimple = 0, nInX = -1, nInY;
265 PadsInXandY(*cluster,nInX, nInY);
267 if (nMax == 1 && nInX < 4 && nInY < 4)
269 iSimple = 1; //1; // simple cluster
272 for (Int_t i=0; i<nMax; ++i)
276 FindCluster(*cluster,localMax, maxPos[i]);
278 Timer(kMainLoop)->Start(kFALSE);
279 MainLoop(*cluster,iSimple);
280 Timer(kMainLoop)->Stop();
283 for (Int_t j=0; j<cluster->Multiplicity(); ++j)
285 AliMUONPad* pad = cluster->Pad(j);
286 if ( pad->Status() == 0 ) continue; // pad charge was not modified
288 pad->RevertCharge(); // use backup charge value
291 } // for (Int_t i=0; i<nMax;
292 if (nMax > 1) ((TH2D*) gROOT->FindObject("anode"))->Delete();
293 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
294 if (mlem) mlem->Delete();
299 //_____________________________________________________________________________
301 AliMUONClusterFinderMLEM::Overlap(const AliMUONPad& pad, const AliMUONPad& pixel)
303 /// Check if the pad and the pixel overlaps
305 // make a fake pad from the pixel
306 AliMUONPad tmp(pad.DetElemId(),pad.Cathode(),pad.Ix(),pad.Iy(),
307 pixel.Coord(0),pixel.Coord(1),
308 pixel.Size(0),pixel.Size(1),0);
310 return AliMUONPad::AreOverlapping(pad,tmp,fgkDecreaseSize);
313 //_____________________________________________________________________________
315 AliMUONClusterFinderMLEM::CheckPrecluster(const AliMUONCluster& origCluster)
317 /// Check precluster in order to attempt to simplify it (mostly for
318 /// two-cathode preclusters)
320 if (origCluster.Multiplicity()==1)
322 // Disregard one-pad clusters (leftovers from splitting)
326 Timer(kCheckPreCluster)->Start(kFALSE);
329 AliMUONCluster* cluster = static_cast<AliMUONCluster*>(origCluster.Clone());
331 AliDebug(2,"Start of CheckPreCluster=");
332 // StdoutToAliDebug(2,cluster->Print("full"));
334 // Check if one-cathode precluster
335 Int_t i1 = cluster->Multiplicity(0) ? 0 : 1;
336 Int_t i2 = cluster->Multiplicity(1) ? 1 : 0;
338 AliMUONCluster* rv(0x0);
342 rv = CheckPreclusterTwoCathodes(cluster);
346 rv = CheckPreclusterOneCathode(cluster);
348 Timer(kCheckPreCluster)->Stop();
352 //_____________________________________________________________________________
354 AliMUONClusterFinderMLEM::CheckPreclusterOneCathode(AliMUONCluster* cluster)
356 /// Check single-cathode precluster
357 AliWarning("Reimplement me!");
358 AliDebug(2,"End of CheckPreClusterOneCathode=");
359 // StdoutToAliDebug(2,cluster->Print("full"));
364 //_____________________________________________________________________________
366 AliMUONClusterFinderMLEM::CheckPreclusterTwoCathodes(AliMUONCluster* cluster)
368 /// Check two-cathode cluster
370 Int_t i1 = cluster->Multiplicity(0) ? 0 : 1;
371 Int_t i2 = cluster->Multiplicity(1) ? 1 : 0;
373 Int_t npad = cluster->Multiplicity();
374 Int_t* flags = new Int_t[npad];
375 memset(flags,0,npad*sizeof(Int_t));
377 // Check pad overlaps
378 for ( Int_t i=0; i<npad; ++i)
380 AliMUONPad* padi = cluster->Pad(i);
381 if ( padi->Cathode() != i1 ) continue;
382 for (Int_t j=i+1; j<npad; ++j)
384 AliMUONPad* padj = cluster->Pad(j);
385 if ( padj->Cathode() != i2 ) continue;
386 if ( !AliMUONPad::AreOverlapping(*padi,*padj,fgkDecreaseSize) ) continue;
387 flags[i] = flags[j] = 1; // mark overlapped pads
391 // Check if all pads overlap
393 for (Int_t i=0; i<npad; ++i)
395 if (flags[i]) continue;
401 // not all pads overlap.
402 if (fDebug) cout << " nFlags: " << nFlags << endl;
403 TObjArray toBeRemoved;
404 for (Int_t i=0; i<npad; ++i)
406 AliMUONPad* pad = cluster->Pad(i);
407 if (flags[i]) continue;
408 Int_t cath = pad->Cathode();
409 Int_t cath1 = TMath::Even(cath);
410 // Check for edge effect (missing pads on the _other_ cathode)
411 AliMpPad mpPad = fSegmentation[cath1]->PadByPosition(pad->Position(),kFALSE);
412 if (!mpPad.IsValid()) continue;
413 if (nFlags == 1 && pad->Charge() < fgkZeroSuppression * 3) continue;
414 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
415 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),pad->Charge()));
416 toBeRemoved.AddLast(pad);
417 //AZ cluster->RemovePad(pad);
418 fPreCluster->Pad(i)->Release();
421 for ( Int_t i = 0; i <= toBeRemoved.GetLast(); ++i )
423 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.At(i)));
427 // Check correlations of cathode charges
428 if ( !cluster->IsSaturated() && cluster->ChargeAsymmetry() > 1 )
431 Int_t cathode = cluster->MaxRawChargeCathode();
437 // get min and max pad charges on the cathode opposite to the
438 // max pad (given by MaxRawChargeCathode())
440 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
442 AliMUONPad* pad = cluster->Pad(i);
443 if ( pad->Cathode() != cathode || !pad->IsReal() )
445 // only consider pads in the opposite cathode, and
446 // onyl consider real pads (i.e. exclude the virtual ones)
449 if ( pad->Charge() < cmin )
451 cmin = pad->Charge();
454 if ( pad->Charge() > cmax )
456 cmax = pad->Charge();
460 AliDebug(2,Form("Pad imin,imax %d,%d cmin,cmax %e,%e",
461 imin,imax,cmin,cmax));
463 // arrange pads according to their distance to the max, normalized
465 Double_t* dist = new Double_t[cluster->Multiplicity()];
470 AliMUONPad* padmax = cluster->Pad(imax);
472 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
475 if ( i == imax) continue;
476 AliMUONPad* pad = cluster->Pad(i);
477 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
478 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
479 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
480 dist[i] = TMath::Sqrt(dx*dx+dy*dy);
483 dmin = dist[i] + 1E-3; // distance to the pad with minimum charge
489 TMath::Sort(cluster->Multiplicity(),dist,flags,kFALSE); // in ascending order
491 TObjArray toBeRemoved;
493 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
495 Int_t indx = flags[i];
496 AliMUONPad* pad = cluster->Pad(indx);
497 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
498 if ( dist[indx] > dmin )
500 // farther than the minimum pad
501 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
502 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
505 if (dx >= 0 && dy >= 0) continue;
506 if (TMath::Abs(dx) > TMath::Abs(dy) && dx >= 0) continue;
507 if (TMath::Abs(dy) > TMath::Abs(dx) && dy >= 0) continue;
509 if ( pad->Charge() <= cmax || TMath::Abs(dist[indx]-xmax) < 1E-3 )
512 if (TMath::Abs(dist[indx]-xmax) < 1.e-3)
514 cmax = TMath::Max(pad->Charge(),cmax);
518 cmax = pad->Charge();
521 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
522 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),
525 toBeRemoved.AddLast(pad);
526 fPreCluster->Pad(indx)->Release();
529 for ( Int_t i = 0; i <= toBeRemoved.GetLast(); ++i )
531 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.At(i)));
538 AliDebug(2,"End of CheckPreClusterTwoCathodes=");
539 // StdoutToAliDebug(2,cluster->Print("full"));
544 //_____________________________________________________________________________
546 AliMUONClusterFinderMLEM::CheckOverlaps()
548 /// For debug only : check if some pixels overlap...
550 Int_t nPix = fPixArray->GetLast()+1;
553 for ( Int_t i = 0; i < nPix; ++i )
555 AliMUONPad* pixelI = Pixel(i);
556 AliMUONPad pi(dummy,dummy,dummy,dummy,
557 pixelI->Coord(0),pixelI->Coord(1),
558 pixelI->Size(0),pixelI->Size(1),0.0);
560 for ( Int_t j = i+1; j < nPix; ++j )
562 AliMUONPad* pixelJ = Pixel(j);
563 AliMUONPad pj(dummy,dummy,dummy,dummy,
564 pixelJ->Coord(0),pixelJ->Coord(1),
565 pixelJ->Size(0),pixelJ->Size(1),0.0);
568 if ( AliMUONPad::AreOverlapping(pi,pj,fgkDecreaseSize,area) )
570 AliInfo(Form("The following 2 pixels (%d and %d) overlap !",i,j));
571 StdoutToAliInfo(pixelI->Print();
572 cout << " Surface = " << pixelI->Size(0)*pixelI->Size(1)*4 << endl;
574 cout << " Surface = " << pixelJ->Size(0)*pixelJ->Size(1)*4 << endl;
575 cout << " Area surface = " << area.Dimensions().X()*area.Dimensions().Y()*4 << endl;
576 cout << "-------" << endl;
584 //_____________________________________________________________________________
585 void AliMUONClusterFinderMLEM::BuildPixArray(AliMUONCluster& cluster)
587 /// Build pixel array for MLEM method
589 Int_t npad = cluster.Multiplicity();
592 AliWarning("Got no pad at all ?!");
597 if ( !cluster.Multiplicity(0) || !cluster.Multiplicity(1) )
599 BuildPixArrayOneCathode(cluster);
603 BuildPixArrayTwoCathodes(cluster);
606 fPixArray->Sort(); // FIXME : not really needed, only to compare with ClusterFinderAZ
608 Int_t nPix = fPixArray->GetLast()+1;
610 AliDebug(2,Form("nPix after BuildPixArray=%d",nPix));
612 Double_t xPadMin(1E9);
613 Double_t yPadMin(1E9);
615 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
617 AliMUONPad* pad = cluster.Pad(i);
618 xPadMin = TMath::Min (xPadMin, pad->DX());
619 yPadMin = TMath::Min (yPadMin, pad->DY());
625 for ( Int_t i = 0; i < nPix; ++i )
627 AliMUONPad* pixPtr = Pixel(i);
628 wxmin = TMath::Min(wxmin, pixPtr->Size(0));
629 wymin = TMath::Min(wymin, pixPtr->Size(1));
632 wxmin = TMath::Abs (wxmin - xPadMin/2) > 0.001 ? xPadMin : xPadMin / 2;
633 wymin = TMath::Abs (wymin - yPadMin/2) > 0.001 ? yPadMin : yPadMin / 2;
635 // Check if small pixel X-size
636 AdjustPixel(cluster,wxmin, 0);
637 // Check if small pixel Y-size
638 AdjustPixel(cluster,wymin, 1);
639 // Check if large pixel size
640 AdjustPixel(wxmin, wymin);
642 // Remove discarded pixels
643 for (Int_t i=0; i<nPix; ++i)
645 AliMUONPad* pixPtr = Pixel(i);
646 if (pixPtr->Charge() < 1)
648 AliDebug(2,Form("Removing pixel %d with charge<1 : ",i));
649 // StdoutToAliDebug(2,pixPtr->Print());
654 fPixArray->Compress();
655 nPix = fPixArray->GetEntriesFast();
657 AliDebug(2,Form("nPix after AdjustPixel=%d",nPix));
659 if ( nPix > cluster.Multiplicity() )
661 AliDebug(2,Form("Will trim number of pixels to number of pads"));
663 // Too many pixels - sort and remove pixels with the lowest signal
665 for ( Int_t i = cluster.Multiplicity(); i<nPix; ++i )
669 fPixArray->Compress();
670 nPix = fPixArray->GetEntriesFast();
671 } // if (nPix > npad)
673 // StdoutToAliDebug(2,cout << "End of BuildPixelArray:" << endl;
674 // fPixArray->Print(););
675 // CheckOverlaps();//FIXME : this is for debug only. Remove it.
678 //_____________________________________________________________________________
679 void AliMUONClusterFinderMLEM::BuildPixArrayOneCathode(AliMUONCluster& cluster)
681 /// From a single-cathode cluster, build the pixel array
683 AliDebug(2,Form("cluster.Multiplicity=%d",cluster.Multiplicity()));
685 for ( Int_t j=0; j<cluster.Multiplicity(); ++j)
687 AliMUONPad* pad = cluster.Pad(j);
688 AliMUONPad* pixPtr = new AliMUONPad(pad->Position(),pad->Dimensions(),
690 fPixArray->Add(pixPtr);
694 //_____________________________________________________________________________
695 void AliMUONClusterFinderMLEM::BuildPixArrayTwoCathodes(AliMUONCluster& cluster)
697 /// From a two-cathodes cluster, build the pixel array
699 AliDebug(2,Form("cluster.Multiplicity=%d",cluster.Multiplicity()));
701 Int_t i1 = cluster.Pad(0)->Cathode();
702 Int_t i2 = TMath::Even(i1);
704 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i)
706 AliMUONPad* padi = cluster.Pad(i);
707 if (padi->Cathode() != i1) continue;
709 for ( Int_t j = 1; j < cluster.Multiplicity(); ++j)
711 AliMUONPad* padj = cluster.Pad(j);
712 if (padj->Cathode() != i2) continue;
716 if ( AliMUONPad::AreOverlapping(*padi,*padj,fgkDecreaseSize,overlap) )
718 AliMUONPad* pixPtr = new AliMUONPad(overlap.Position(),
719 overlap.Dimensions(),
720 TMath::Min(padi->Charge(),padj->Charge()));
721 if ( ( padi->Charge() <= padj->Charge() && padi->IsSaturated() ) ||
722 ( padj->Charge() < padi->Charge() && padj->IsSaturated() ) )
724 // if smallest charge of the 2 pads is already above saturation, then
725 // the pixel is saturated...
726 pixPtr->SetSaturated(kTRUE);
728 pixPtr->SetReal(kFALSE);
729 fPixArray->Add(pixPtr);
735 //_____________________________________________________________________________
736 void AliMUONClusterFinderMLEM::AdjustPixel(AliMUONCluster& cluster,
737 Float_t width, Int_t ixy)
739 /// Check if some pixels have small size (adjust if necessary)
741 AliDebug(2,Form("width=%e ixy=%d",width,ixy));
743 AliMUONPad *pixPtr, *pixPtr1 = 0;
744 Int_t ixy1 = TMath::Even(ixy);
745 Int_t nPix = fPixArray->GetEntriesFast();
747 for (Int_t i=0; i<nPix; i++)
750 if (pixPtr->Charge() < 1) continue; // discarded pixel
751 if (pixPtr->Size(ixy)-width < -1.e-4)
754 for (Int_t j=i+1; j<nPix; j++)
757 if (pixPtr1->Charge() < 1) continue; // discarded pixel
758 if (TMath::Abs(pixPtr1->Size(ixy)-width) < fgkDistancePrecision) continue; // right size
759 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > fgkDistancePrecision) continue; // different rows/columns
760 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width)
763 Double_t tmp = pixPtr->Coord(ixy) + pixPtr1->Size(ixy)*
764 TMath::Sign (1., pixPtr1->Coord(ixy) - pixPtr->Coord(ixy));
765 pixPtr->SetCoord(ixy, tmp);
766 pixPtr->SetSize(ixy, width);
767 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
768 pixPtr1->SetCharge(0);
772 } // for (Int_t j=i+1;
773 if (pixPtr1 || i == nPix-1)
775 // edge pixel - just increase its size
776 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
778 AliMUONPad* pad = cluster.Pad(j);
779 Double_t d = ( ixy == 0 ) ? pad->X() : ( ixy == 1 ) ? pad->Y() : -1E9;
781 if (pixPtr->Coord(ixy) < d)
783 pixPtr->Shift(ixy, pixPtr->Size(ixy)-width);
787 pixPtr->Shift(ixy, -pixPtr->Size(ixy)+width);
789 pixPtr->SetSize(ixy, width);
793 } // if (pixPtr->Size(ixy)-width < -1.e-4)
794 } // for (Int_t i=0; i<nPix;
797 //_____________________________________________________________________________
798 void AliMUONClusterFinderMLEM::AdjustPixel(Double_t wxmin, Double_t wymin)
800 /// Check if some pixels have large size (adjust if necessary)
802 AliDebug(2,Form("wxmin=%e wymin=%e",wxmin,wymin));
804 Int_t n1[2], n2[2], iOK = 1, nPix = fPixArray->GetEntriesFast();
805 AliMUONPad *pixPtr, pix;
806 Double_t xy0[2] = {9999, 9999}, wxy[2], dist[2] = {0};
808 // Check if large pixel size
809 for (Int_t i = 0; i < nPix; i++) {
810 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
811 if (pixPtr->Charge() < 1) continue; // discarded pixel
812 if (pixPtr->Size(0) - wxmin < 1.e-4) {
813 if (xy0[0] > 9998) xy0[0] = pixPtr->Coord(0); // position of a "normal" pixel
814 if (pixPtr->Size(1) - wymin < 1.e-4) {
815 if (xy0[1] > 9998) xy0[1] = pixPtr->Coord(1); // position of a "normal" pixel
817 } else iOK = 0; // large pixel
819 iOK = 0; // large pixel
820 if (xy0[1] > 9998 && pixPtr->Size(1) - wymin < 1.e-4) xy0[1] = pixPtr->Coord(1); // "normal" pixel
822 if (xy0[0] < 9998 && xy0[1] < 9998) break;
828 //cout << xy0[0] << " " << xy0[1] << endl;
829 for (Int_t i = 0; i < nPix; i++) {
830 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
831 if (pixPtr->Charge() < 1) continue; // discarded pixel
834 for (Int_t j = 0; j < 2; j++) {
835 if (pixPtr->Size(j) - wxy[j] < 1.e-4) continue;
836 dist[j] = (pixPtr->Coord(j) - xy0[j]) / wxy[j] / 2; // normalized distance to "normal" pixel
837 n2[j] = TMath::Nint (pixPtr->Size(j) / wxy[j]);
838 n1[j] = n2[j] == 1 ? TMath::Nint(dist[j]) : (Int_t)dist[j];
840 if (n1[0] > 998 && n1[1] > 998) continue;
841 if (fDebug) cout << " Different " << pixPtr->Size(0) << " " << wxy[0] << " "
842 << pixPtr->Size(1) << " " << wxy[1] <<endl;
844 if (n2[0] > 2 || n2[1] > 2) {
845 //cout << n2[0] << " " << n2[1] << endl;
846 if (n2[0] > 2 && n1[0] < 999) n1[0]--;
847 if (n2[1] > 2 && n1[1] < 999) n1[1]--;
849 //cout << n1[0] << " " << n2[0] << " " << n1[1] << " " << n2[1] << endl;
851 pix.SetSize(0, wxy[0]); pix.SetSize(1, wxy[1]);
853 for (Int_t ii = 0; ii < n2[0]; ii++) {
854 if (n1[0] < 999) pix.SetCoord(0, xy0[0] + (n1[0] + TMath::Sign(1.,dist[0]) * ii) * 2 * wxy[0]);
855 for (Int_t jj = 0; jj < n2[1]; jj++) {
856 if (n1[1] < 999) pix.SetCoord(1, xy0[1] + (n1[1] + TMath::Sign(1.,dist[1]) * jj) * 2 * wxy[1]);
857 fPixArray->Add(new AliMUONPad(pix));
861 pixPtr->SetCharge(0);
862 } // for (Int_t i = 0; i < nPix;
865 //_____________________________________________________________________________
867 AliMUONClusterFinderMLEM::Plot(const char* basename)
869 /// Make a plot and save it as png
874 TCanvas* c = new TCanvas("MLEM","MLEM",800,600);
879 c->Print(Form("%s.EVT%d.DE%d.CLU%d.png",basename,fEventNumber,
880 fDetElemId,fClusterNumber));
883 //_____________________________________________________________________________
885 AliMUONClusterFinderMLEM::ComputeCoefficients(AliMUONCluster& cluster,
889 /// Compute coefficients needed for MLEM algorithm
891 Int_t nPix = fPixArray->GetLast()+1;
893 memset(probi,0,nPix*sizeof(Double_t));
895 for ( Int_t j=0; j<cluster.Multiplicity(); ++j )
897 AliMUONPad* pad = cluster.Pad(j);
900 for ( Int_t ipix=0; ipix<nPix; ++ipix )
902 Int_t indx1 = indx + ipix;
903 if (pad->Status() < 0)
908 AliMUONPad* pixPtr = Pixel(ipix);
909 // coef is the charge (given by Mathieson integral) on pad, assuming
910 // the Mathieson is center at pixel.
911 coef[indx1] = fSplitter->ChargeIntegration(pixPtr->Coord(0), pixPtr->Coord(1), *pad);
912 AliDebug(2,Form("pad=(%d,%d,%e,%e,%e,%e) pix=(%e,%e,%e,%e) coef %e",
916 pixPtr->Coord(0),pixPtr->Coord(1),
917 pixPtr->Size(0),pixPtr->Size(1),
920 probi[ipix] += coef[indx1];
925 //_____________________________________________________________________________
926 Bool_t AliMUONClusterFinderMLEM::MainLoop(AliMUONCluster& cluster, Int_t iSimple)
928 /// Repeat MLEM algorithm until pixel size becomes sufficiently small
930 Int_t nPix = fPixArray->GetLast()+1;
932 AliDebug(2,Form("nPix=%d iSimple=%d, precluster=",nPix,iSimple));
933 // StdoutToAliDebug(2,cluster.Print("full"););
937 AliDebug(1,"No pixels, why am I here then ?");
940 AddVirtualPad(cluster); // add virtual pads if necessary
942 Int_t npadTot = cluster.Multiplicity();
944 for (Int_t i = 0; i < npadTot; ++i)
946 if (cluster.Pad(i)->Status() == 0) ++npadOK;
951 Double_t* probi(0x0);
952 Int_t lc(0); // loop counter (for debug)
954 // Plot("mlem.start");
959 mlem = (TH2D*) gROOT->FindObject("mlem");
962 AliDebug(2,Form("lc %d nPix %d(%d) npadTot %d npadOK %d",lc,nPix,fPixArray->GetLast()+1,npadTot,npadOK));
963 AliDebug(2,Form("EVT%d PixArray=",fEventNumber));
964 // StdoutToAliDebug(2,fPixArray->Print("","full"));
966 coef = new Double_t [npadTot*nPix];
967 probi = new Double_t [nPix];
969 // Calculate coefficients and pixel visibilities
970 ComputeCoefficients(cluster,coef,probi);
972 for (Int_t ipix=0; ipix<nPix; ++ipix)
974 if (probi[ipix] < 0.01)
976 AliMUONPad* pixel = Pixel(ipix);
977 AliDebug(2,Form("Setting the following pixel to invisible as its probi<0.01:"));
978 // StdoutToAliDebug(2,cout << Form(" -- ipix %3d --- "); pixel->Print(););
979 pixel->SetCharge(0); // "invisible" pixel
984 Mlem(cluster,coef, probi, 15);
986 Double_t xylim[4] = {999, 999, 999, 999};
987 AliMUONPad* pixPtr(0x0);
989 for ( Int_t ipix=0; ipix<nPix; ++ipix )
991 pixPtr = Pixel(ipix);
992 for ( Int_t i=0; i<4; ++i )
994 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
997 for (Int_t i=0; i<4; i++)
999 xylim[i] -= pixPtr->Size(i/2);
1003 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1004 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1006 // StdoutToAliDebug(2,cout << "pixel used for nx,ny computation : "; pixPtr->Print(););
1007 AliDebug(2,Form("lc %d pixPtr size = %e,%e nx,ny=%d,%d xylim=%e,%e,%e,%e",
1008 lc,pixPtr->Size(0),pixPtr->Size(1),nx,ny,
1009 xylim[0],-xylim[1],xylim[2],-xylim[3]
1012 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1014 for (Int_t ipix=0; ipix<nPix; ++ipix)
1016 AliMUONPad* pixPtr = Pixel(ipix);
1017 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
1020 // Check if the total charge of pixels is too low
1022 for ( Int_t i=0; i<nPix; ++i)
1024 qTot += Pixel(i)->Charge();
1027 if ( qTot < 1.e-4 || ( npadOK < 3 && qTot < 7 ) )
1029 AliDebug(1,Form("Deleting the above cluster (charge %e too low, npadOK=%d)",qTot,npadOK));
1034 fPixArray->Delete();
1035 for ( Int_t i=0; i<npadTot; ++i)
1037 AliMUONPad* pad = cluster.Pad(i);
1038 if ( pad->Status() == 0) pad->SetStatus(-1);
1045 // Simple cluster - skip further passes thru EM-procedure
1051 fPixArray->Delete();
1055 // Calculate position of the center-of-gravity around the maximum pixel
1057 FindCOG(mlem, xyCOG);
1059 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 &&
1060 pixPtr->Size(0) > pixPtr->Size(1)) break;
1062 // Sort pixels according to the charge
1064 Double_t pixMin = 0.01*Pixel(0)->Charge();
1065 pixMin = TMath::Min(pixMin,50.);
1067 // Decrease pixel size and shift pixels to make them centered at
1069 Int_t indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1072 Double_t shift[2] = { 0.0, 0.0 };
1073 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1076 for (Int_t ipix=0; ipix<nPix1; ++ipix)
1078 AliMUONPad* pixPtr = Pixel(ipix);
1079 if ( nPix >= npadOK // too many pixels already
1081 pixPtr->Charge() < pixMin // too low charge
1087 for (Int_t i=0; i<2; ++i)
1091 pixPtr->SetCharge(10);
1092 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1093 width = -pixPtr->Size(indx);
1094 pixPtr->Shift(indx, width);
1095 // Shift pixel position
1099 for (Int_t j=0; j<2; ++j)
1101 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1102 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1105 pixPtr->Shift(0, -shift[0]);
1106 pixPtr->Shift(1, -shift[1]);
1110 pixPtr = new AliMUONPad(*pixPtr);
1111 pixPtr->Shift(indx, -2*width);
1112 fPixArray->Add(pixPtr);
1114 for (Int_t i=0; i<4; ++i)
1116 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1118 } // for (Int_t i=0; i<2;
1120 } // for (Int_t ipix=0;
1122 fPixArray->Compress();
1123 nPix = fPixArray->GetEntriesFast();
1125 AliDebug(2,Form("After shift:"));
1126 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1127 // Plot(Form("mlem.lc%d",lc+1));
1129 AliDebug(2,Form(" xyCOG=%9.6f %9.6f xylim=%9.6f,%9.6f,%9.6f,%9.6f",
1132 xylim[2],xylim[3]));
1134 // Remove excessive pixels
1137 for (Int_t ipix=npadOK; ipix<nPix; ++ipix)
1144 AliMUONPad* pixPtr = Pixel(0);
1145 // add pixels if the maximum is at the limit of pixel area
1146 // start from Y-direction
1148 for (Int_t i=3; i>-1; --i)
1150 if (nPix < npadOK &&
1151 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2))
1153 AliMUONPad* p = static_cast<AliMUONPad*>(pixPtr->Clone());
1154 p->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1155 j = TMath::Even (i/2);
1156 p->SetCoord(j, xyCOG[j]);
1157 AliDebug(2,Form("Adding pixel on the edge (i=%d) ",i));
1158 // StdoutToAliDebug(2,cout << " ---- ";
1159 // p->Print("corners"););
1165 fPixArray->Compress();
1166 nPix = fPixArray->GetEntriesFast();
1173 AliDebug(2,Form("At the end of while loop nPix=%d : ",fPixArray->GetLast()+1));
1174 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1176 // remove pixels with low signal or low visibility
1177 // Cuts are empirical !!!
1178 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1179 thresh = TMath::Min (thresh,50.);
1181 Double_t charge = 0;
1183 for ( Int_t i=0; i<nPix; ++i)
1185 cmax = TMath::Max (cmax,probi[i]);
1188 // Mark pixels which should be removed
1189 for (Int_t i=0; i<nPix; ++i)
1191 AliMUONPad* pixPtr = Pixel(i);
1192 charge = pixPtr->Charge();
1193 if (charge < thresh)
1195 pixPtr->SetCharge(-charge);
1199 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1201 for (Int_t i=0; i<nPix; ++i)
1203 AliMUONPad* pixPtr = Pixel(i);
1204 charge = pixPtr->Charge();
1205 if (charge > 0) continue;
1206 near = FindNearest(pixPtr);
1207 pixPtr->SetCharge(0);
1208 probi[i] = 0; // make it "invisible"
1209 AliMUONPad* pnear = Pixel(near);
1210 pnear->SetCharge(pnear->Charge() + (-charge));
1212 Mlem(cluster,coef,probi,2);
1214 AliDebug(2,Form("Before splitting nPix=%d EVT %d DE %d",fPixArray->GetLast()+1,fEventNumber,fDetElemId));
1215 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1216 // Plot("mlem.beforesplit");
1219 for (Int_t i=0; i<nPix; ++i)
1221 AliMUONPad* pixPtr = Pixel(i);
1222 Int_t ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1223 Int_t iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1224 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1227 // Try to split into clusters
1229 if (mlem->GetSum() < 1)
1235 fSplitter->Split(cluster,mlem,coef,fClusterList);
1242 fPixArray->Delete();
1247 //_____________________________________________________________________________
1248 void AliMUONClusterFinderMLEM::Mlem(AliMUONCluster& cluster,
1249 Double_t* coef, Double_t* probi,
1252 /// Use MLEM to find pixel charges
1254 Int_t nPix = fPixArray->GetEntriesFast();
1256 Int_t npad = cluster.Multiplicity();
1258 Double_t* probi1 = new Double_t[nPix];
1259 Double_t probMax = 0;
1260 Double_t tmp = TMath::MaxElement(nPix,probi);
1262 for (Int_t ipix=0; ipix<nPix; ++ipix)
1264 probMax = TMath::Max(probMax,probi[ipix]);
1267 if (probMax!=tmp) { AliWarning(Form("probMax=%e tmp=%e",probMax,tmp)); }
1269 for (Int_t iter=0; iter<nIter; ++iter)
1272 for (Int_t ipix=0; ipix<nPix; ++ipix)
1274 Pixel(ipix)->SetChargeBackup(0);
1275 // Correct each pixel
1276 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1278 probi1[ipix] = probMax;
1279 for (Int_t j=0; j<npad; j++)
1281 AliMUONPad* pad = cluster.Pad(j);
1282 if (pad->Status() < 0) continue;
1284 Int_t indx1 = j*nPix;
1285 Int_t indx = indx1 + ipix;
1286 // Calculate expectation
1287 for (Int_t i=0; i<nPix; i++)
1289 sum1 += Pixel(i)->Charge()*coef[indx1+i];
1291 if ( pad->Charge() > fgkSaturation-1 && sum1 > pad->Charge() ) //FIXME : remove usage of fgkSaturation
1293 if ( !pad->IsSaturated() )
1295 AliWarning("Got a pad charge above saturation not backed-up by pad->IsSaturated() function : ");
1296 StdoutToAliWarning(pad->Print("full"));
1298 // correct for pad charge overflows
1299 probi1[ipix] -= coef[indx];
1303 if (coef[indx] > 1.e-6)
1305 sum += pad->Charge()*coef[indx]/sum1;
1307 } // for (Int_t j=0;
1308 AliMUONPad* pixPtr = Pixel(ipix);
1309 if (probi1[ipix] > 1.e-6)
1311 //AZ pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1312 pixPtr->SetChargeBackup(pixPtr->Charge()*sum/probi1[ipix]);
1314 } // for (Int_t ipix=0;
1315 for (Int_t i = 0; i < nPix; ++i) {
1316 AliMUONPad* pixPtr = Pixel(i);
1317 pixPtr->SetCharge(pixPtr->ChargeBackup());
1319 } // for (Int_t iter=0;
1323 //_____________________________________________________________________________
1324 void AliMUONClusterFinderMLEM::FindCOG(TH2D *mlem, Double_t *xyc)
1326 /// Calculate position of the center-of-gravity around the maximum pixel
1328 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1329 Int_t i1 = -9, j1 = -9;
1330 mlem->GetMaximumBin(ixmax,iymax,ix);
1331 Int_t nx = mlem->GetNbinsX();
1332 Int_t ny = mlem->GetNbinsY();
1333 Double_t thresh = mlem->GetMaximum()/10;
1334 Double_t x, y, cont, xq=0, yq=0, qq=0;
1336 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1337 y = mlem->GetYaxis()->GetBinCenter(i);
1338 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1339 cont = mlem->GetCellContent(j,i);
1340 if (cont < thresh) continue;
1341 if (i != i1) {i1 = i; nsumy++;}
1342 if (j != j1) {j1 = j; nsumx++;}
1343 x = mlem->GetXaxis()->GetBinCenter(j);
1352 Int_t i2 = 0, j2 = 0;
1355 // one bin in Y - add one more (with the largest signal)
1356 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1357 if (i == iymax) continue;
1358 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1359 cont = mlem->GetCellContent(j,i);
1362 x = mlem->GetXaxis()->GetBinCenter(j);
1363 y = mlem->GetYaxis()->GetBinCenter(i);
1372 if (i2 != i1) nsumy++;
1373 if (j2 != j1) nsumx++;
1375 } // if (nsumy == 1)
1378 // one bin in X - add one more (with the largest signal)
1380 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1381 if (j == ixmax) continue;
1382 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1383 cont = mlem->GetCellContent(j,i);
1386 x = mlem->GetXaxis()->GetBinCenter(j);
1387 y = mlem->GetYaxis()->GetBinCenter(i);
1396 if (i2 != i1) nsumy++;
1397 if (j2 != j1) nsumx++;
1399 } // if (nsumx == 1)
1401 xyc[0] = xq/qq; xyc[1] = yq/qq;
1402 AliDebug(2,Form("x,y COG = %e,%e",xyc[0],xyc[1]));
1405 //_____________________________________________________________________________
1406 Int_t AliMUONClusterFinderMLEM::FindNearest(AliMUONPad *pixPtr0)
1408 /// Find the pixel nearest to the given one
1409 /// (algorithm may be not very efficient)
1411 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1412 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1413 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1416 for (Int_t i=0; i<nPix; i++) {
1417 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
1418 if (pixPtr->Charge() < 0.5) continue;
1419 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1420 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1421 r = dx *dx + dy * dy;
1422 if (r < rmin) { rmin = r; imin = i; }
1428 //_____________________________________________________________________________
1430 AliMUONClusterFinderMLEM::Timer(Int_t i) const
1432 /// Return timer at index i
1433 return static_cast<TStopwatch*>(fTimers->At(i));
1436 //_____________________________________________________________________________
1438 AliMUONClusterFinderMLEM::Paint(Option_t*)
1440 /// Paint cluster and pixels
1442 AliMpArea area(fPreCluster->Area());
1444 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
1446 gVirtualX->SetFillStyle(1001);
1447 gVirtualX->SetFillColor(3);
1448 gVirtualX->SetLineColor(3);
1452 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1454 AliMUONPad* pixel = Pixel(i);
1456 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0)*s,
1457 pixel->Coord(1)-pixel->Size(1)*s,
1458 pixel->Coord(0)+pixel->Size(0)*s,
1459 pixel->Coord(1)+pixel->Size(1)*s);
1461 // for ( Int_t sign = -1; sign < 2; sign +=2 )
1463 // gPad->PaintLine(pixel->Coord(0) - pixel->Size(0),
1464 // pixel->Coord(1) + sign*pixel->Size(1),
1465 // pixel->Coord(0) + pixel->Size(0),
1466 // pixel->Coord(1) - sign*pixel->Size(1)
1472 gVirtualX->SetFillStyle(0);
1474 fPreCluster->Paint();
1476 gVirtualX->SetLineColor(1);
1477 gVirtualX->SetLineWidth(2);
1478 gVirtualX->SetFillStyle(0);
1479 gVirtualX->SetTextColor(1);
1480 gVirtualX->SetTextAlign(22);
1482 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1484 AliMUONPad* pixel = Pixel(i);
1485 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0),
1486 pixel->Coord(1)-pixel->Size(1),
1487 pixel->Coord(0)+pixel->Size(0),
1488 pixel->Coord(1)+pixel->Size(1));
1489 gVirtualX->SetTextSize(pixel->Size(0)*60);
1491 gPad->PaintText(pixel->Coord(0),pixel->Coord(1),Form("%d",(Int_t)(pixel->Charge())));
1495 //_____________________________________________________________________________
1496 Int_t AliMUONClusterFinderMLEM::FindLocalMaxima(TObjArray *pixArray, Int_t *localMax, Double_t *maxVal)
1498 /// Find local maxima in pixel space for large preclusters in order to
1499 /// try to split them into smaller pieces (to speed up the MLEM procedure)
1500 /// or to find additional fitting seeds if clusters were not completely resolved
1502 AliDebug(1,Form("nPix=%d",pixArray->GetLast()+1));
1505 //if (pixArray == fPixArray) hist = (TH2D*) gROOT->FindObject("anode");
1506 //else { hist = (TH2D*) gROOT->FindObject("anode1"); cout << hist << endl; }
1507 //if (hist) hist->Delete();
1509 Double_t xylim[4] = {999, 999, 999, 999};
1511 Int_t nPix = pixArray->GetEntriesFast();
1512 AliMUONPad *pixPtr = 0;
1513 for (Int_t ipix=0; ipix<nPix; ipix++) {
1514 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
1515 for (Int_t i=0; i<4; i++)
1516 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1518 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
1520 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1521 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1522 if (pixArray == fPixArray) hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1523 else hist = new TH2D("anode1","anode1",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1524 for (Int_t ipix=0; ipix<nPix; ipix++) {
1525 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
1526 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
1528 // if (fDraw && pixArray == fPixArray) fDraw->DrawHist("c2", hist);
1530 Int_t nMax = 0, indx;
1531 Int_t *isLocalMax = new Int_t[ny*nx];
1532 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
1534 for (Int_t i=1; i<=ny; i++) {
1536 for (Int_t j=1; j<=nx; j++) {
1537 if (hist->GetCellContent(j,i) < 0.5) continue;
1538 //if (isLocalMax[indx+j-1] < 0) continue;
1539 if (isLocalMax[indx+j-1] != 0) continue;
1540 FlagLocalMax(hist, i, j, isLocalMax);
1544 for (Int_t i=1; i<=ny; i++) {
1546 for (Int_t j=1; j<=nx; j++) {
1547 if (isLocalMax[indx+j-1] > 0) {
1548 localMax[nMax] = indx + j - 1;
1549 maxVal[nMax++] = hist->GetCellContent(j,i);
1550 if (nMax > 99) AliFatal(" Too many local maxima !!!");
1554 if (fDebug) cout << " Local max: " << nMax << endl;
1555 delete [] isLocalMax; isLocalMax = 0;
1559 //_____________________________________________________________________________
1560 void AliMUONClusterFinderMLEM::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
1562 /// Flag pixels (whether or not local maxima)
1564 Int_t nx = hist->GetNbinsX();
1565 Int_t ny = hist->GetNbinsY();
1566 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
1567 Int_t cont1 = 0, indx = (i-1)*nx+j-1, indx1 = 0, indx2 = 0;
1569 for (Int_t i1=i-1; i1<i+2; i1++) {
1570 if (i1 < 1 || i1 > ny) continue;
1571 indx1 = (i1 - 1) * nx;
1572 for (Int_t j1=j-1; j1<j+2; j1++) {
1573 if (j1 < 1 || j1 > nx) continue;
1574 if (i == i1 && j == j1) continue;
1575 indx2 = indx1 + j1 - 1;
1576 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
1577 if (cont < cont1) { isLocalMax[indx] = -1; return; }
1578 else if (cont > cont1) isLocalMax[indx2] = -1;
1579 else { // the same charge
1580 isLocalMax[indx] = 1;
1581 if (isLocalMax[indx2] == 0) {
1582 FlagLocalMax(hist, i1, j1, isLocalMax);
1583 if (isLocalMax[indx2] < 0) { isLocalMax[indx] = -1; return; }
1584 else isLocalMax[indx2] = -1;
1589 isLocalMax[indx] = 1; // local maximum
1592 //_____________________________________________________________________________
1593 void AliMUONClusterFinderMLEM::FindCluster(AliMUONCluster& cluster,
1594 Int_t *localMax, Int_t iMax)
1596 /// Find pixel cluster around local maximum \a iMax and pick up pads
1597 /// overlapping with it
1599 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
1600 Int_t nx = hist->GetNbinsX();
1601 Int_t ny = hist->GetNbinsY();
1602 Int_t ic = localMax[iMax] / nx + 1;
1603 Int_t jc = localMax[iMax] % nx + 1;
1604 Bool_t *used = new Bool_t[ny*nx];
1605 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1607 // Drop all pixels from the array - pick up only the ones from the cluster
1608 fPixArray->Delete();
1610 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
1611 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
1612 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
1613 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
1614 Double_t cont = hist->GetCellContent(jc,ic);
1615 fPixArray->Add(new AliMUONPad (xc, yc, wx, wy, cont));
1616 used[(ic-1)*nx+jc-1] = kTRUE;
1617 fSplitter->AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
1619 Int_t nPix = fPixArray->GetEntriesFast();
1620 Int_t npad = cluster.Multiplicity();
1622 for (Int_t i=0; i<nPix; ++i)
1624 AliMUONPad* pixPtr = Pixel(i);
1625 pixPtr->SetSize(0,wx);
1626 pixPtr->SetSize(1,wy);
1629 // Pick up pads which overlap with found pixels
1630 for (Int_t i=0; i<npad; i++)
1632 cluster.Pad(i)->SetStatus(-1);
1635 for (Int_t i=0; i<nPix; i++)
1637 AliMUONPad* pixPtr = Pixel(i);
1638 for (Int_t j=0; j<npad; ++j)
1640 AliMUONPad* pad = cluster.Pad(j);
1641 if ( Overlap(*pad,*pixPtr) )
1648 delete [] used; used = 0;
1651 //_____________________________________________________________________________
1652 AliMUONClusterFinderMLEM&
1653 AliMUONClusterFinderMLEM::operator=(const AliMUONClusterFinderMLEM& rhs)
1655 /// Protected assignement operator
1657 if (this == &rhs) return *this;
1659 AliFatal("Not implemented.");
1664 //_____________________________________________________________________________
1666 AliMUONClusterFinderMLEM::Neighbours(Int_t cathode, Int_t ix, Int_t iy,
1667 Int_t& n, Int_t* xList, Int_t* yList)
1669 /// Get the list of neighbours of pad at (cathode,ix,iy)
1672 const AliMpVSegmentation* seg = fSegmentation[cathode];
1674 AliMpPad pad = seg->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1676 // Define the region to look into : a region slightly bigger
1677 // than the pad itself (5% bigger), in order to catch first neighbours.
1679 AliMpArea area(pad.Position(),pad.Dimensions()*1.05);
1681 AliMpVPadIterator* it = seg->CreateIterator(area);
1683 while ( !it->IsDone() && n < 10 )
1685 AliMpPad p = it->CurrentItem();
1686 if ( p != pad ) // skip self
1688 xList[n] = p.GetIndices().GetFirst();
1689 yList[n] = p.GetIndices().GetSecond();
1697 //_____________________________________________________________________________
1698 void AliMUONClusterFinderMLEM::AddVirtualPad(AliMUONCluster& cluster)
1700 /// Add virtual pad (with small charge) to improve fit for some
1701 /// clusters (when pad with max charge is at the extreme of the cluster)
1703 // Get number of pads in X and Y-directions
1704 Int_t nInX = -1, nInY;
1705 PadsInXandY(cluster,nInX, nInY);
1708 // Add virtual pad only if number of pads per direction == 2
1709 if (nInX != 2 && nInY != 2) return;
1713 // Find pads with max charge
1714 Int_t maxpad[2][2] = {{-1, -1}, {-1, -1}}, cath;
1715 Double_t sigmax[2] = {0}, aamax[2] = {0};
1716 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1718 AliMUONPad* pad = cluster.Pad(j);
1719 if (pad->Status() != 0) continue;
1720 cath = pad->Cathode();
1721 if (pad->Charge() > sigmax[cath])
1723 maxpad[cath][1] = maxpad[cath][0];
1724 aamax[cath] = sigmax[cath];
1725 sigmax[cath] = pad->Charge();
1726 maxpad[cath][0] = j;
1730 if (maxpad[0][0] >= 0 && maxpad[0][1] < 0 || maxpad[1][0] >= 0 && maxpad[1][1] < 0)
1732 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1734 AliMUONPad* pad = cluster.Pad(j);
1735 if (pad->Status() != 0) continue;
1736 cath = pad->Cathode();
1737 if (j == maxpad[cath][0] || j == maxpad[cath][1]) continue;
1738 if ( pad->Charge() > aamax[cath])
1740 aamax[cath] = pad->Charge();
1741 maxpad[cath][1] = j;
1746 // cout << "-------AddVirtualPad" << endl;
1747 // cout << Form("nInX %2d nInY %2d",nInX,nInY) << endl;
1749 // cluster.Print("full");
1751 // for ( Int_t i = 0; i < 2; ++i )
1753 // for ( Int_t j = 0; j < 2; ++j )
1755 // cout << Form("maxpad[%d][%d]=%d",i,j,maxpad[i][j]) << endl;
1759 // Check for mirrors (side X on cathode 0)
1760 Bool_t mirror = kFALSE;
1761 if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
1763 AliMUONPad* maxPadCath[] = { cluster.Pad(maxpad[0][0]), cluster.Pad(maxpad[1][0]) };
1764 mirror = maxPadCath[0]->DX() < maxPadCath[0]->DY();
1765 if (!mirror && TMath::Abs( maxPadCath[0]->DX() - maxPadCath[1]->DX()) < 0.001)
1767 // Special case when pads on both cathodes have the same size
1769 for (Int_t j = 0; j < cluster.Multiplicity(); ++j)
1771 AliMUONPad* pad = cluster.Pad(j);
1772 cath = pad->Cathode();
1773 if (j == maxpad[cath][0]) continue;
1774 if ( pad->Ix() != maxPadCath[cath]->Ix() ) continue;
1775 if ( TMath::Abs(pad->Iy() - maxPadCath[cath]->Iy()) == 1 )
1780 if (!yud[0]) mirror = kTRUE; // take the other cathode
1781 } // if (!mirror &&...
1782 } // if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
1784 // // Find neughbours of pads with max charges
1785 Int_t nn, xList[10], yList[10], ix0, iy0, ix, iy, neighb;
1786 for (cath=0; cath<2; cath++)
1788 if (!cath && maxpad[0][0] < 0) continue; // one-sided cluster - cathode 1
1789 if (cath && maxpad[1][0] < 0) break; // one-sided cluster - cathode 0
1790 if (maxpad[1][0] >= 0)
1794 if (!cath && nInY != 2) continue;
1795 if (cath && nInX != 2 && (maxpad[0][0] >= 0 || nInY != 2)) continue;
1799 if (!cath && nInX != 2) continue;
1800 if (cath && nInY != 2 && (maxpad[0][0] >= 0 || nInX != 2)) continue;
1804 Int_t iAddX = 0, iAddY = 0, ix1 = 0, iy1 = 0, iPad = 0;
1805 if (maxpad[0][0] < 0) iPad = 1;
1807 for (iPad=0; iPad<2; iPad++)
1809 if (maxpad[cath][iPad] < 0) continue;
1810 if (iPad && !iAddX && !iAddY) break;
1811 if (iPad && cluster.Pad(maxpad[cath][1])->Charge() / sigmax[cath] < 0.5) break;
1813 Int_t neighbx = 0, neighby = 0;
1814 ix0 = cluster.Pad(maxpad[cath][iPad])->Ix();
1815 iy0 = cluster.Pad(maxpad[cath][iPad])->Iy();
1816 Neighbours(cath,ix0,iy0,nn,xList,yList);
1818 for (Int_t j=0; j<nn; j++) {
1819 if (TMath::Abs(xList[j]-ix0) == 1 || xList[j]*ix0 == -1) neighbx++;
1820 if (TMath::Abs(yList[j]-iy0) == 1 || yList[j]*iy0 == -1) neighby++;
1823 if (cath) neighb = neighbx;
1824 else neighb = neighby;
1825 if (maxpad[0][0] < 0) neighb += neighby;
1826 else if (maxpad[1][0] < 0) neighb += neighbx;
1828 if (!cath) neighb = neighbx;
1829 else neighb = neighby;
1830 if (maxpad[0][0] < 0) neighb += neighbx;
1831 else if (maxpad[1][0] < 0) neighb += neighby;
1834 for (Int_t j=0; j< cluster.Multiplicity(); ++j)
1836 AliMUONPad* pad = cluster.Pad(j);
1837 if ( pad->Cathode() != cath) continue;
1840 if (iy == iy0 && ix == ix0) continue;
1841 for (Int_t k=0; k<nn; ++k)
1843 if (xList[k] != ix || yList[k] != iy) continue;
1846 if ((!cath || maxpad[0][0] < 0) &&
1847 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
1848 if (!iPad && TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) ix1 = xList[k]; //19-12-05
1849 xList[k] = yList[k] = 0;
1853 if ((cath || maxpad[1][0] < 0) &&
1854 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
1855 if (!iPad) ix1 = xList[k]; //19-12-05
1856 xList[k] = yList[k] = 0;
1860 if ((!cath || maxpad[0][0] < 0) &&
1861 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
1862 if (!iPad) ix1 = xList[k]; //19-12-05
1863 xList[k] = yList[k] = 0;
1867 if ((cath || maxpad[1][0] < 0) &&
1868 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
1869 xList[k] = yList[k] = 0;
1874 } // for (Int_t k=0; k<nn;
1876 } // for (Int_t j=0; j< cluster.Multiplicity();
1877 if (!neighb) continue;
1882 for (Int_t j=0; j<nn; j++)
1884 if (xList[j] == 0 && yList[j] == 0) continue;
1885 // npads = fnPads[0] + fnPads[1];
1886 // fPadIJ[0][npads] = cath;
1887 // fPadIJ[1][npads] = 0;
1890 if (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) {
1891 if (iy != iy0) continue; // new segmentation - check
1892 if (nInX != 2) continue; // new
1894 if (!cath && maxpad[1][0] >= 0) continue;
1896 if (cath && maxpad[0][0] >= 0) continue;
1898 if (iPad && !iAddX) continue;
1899 AliMpPad pad = fSegmentation[cath]->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1900 // fXyq[0][npads] = pad.Position().X();
1901 // fXyq[1][npads] = pad.Position().Y();
1902 AliMUONPad muonPad(fDetElemId, cath, ix, iy, pad.Position().X(), pad.Position().Y(),
1903 pad.Dimensions().X(), pad.Dimensions().Y(), 0);
1904 // fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
1905 // if (fXyq[0][npads] > 1.e+5) continue; // temporary fix
1906 if (muonPad.Coord(0) > 1.e+5) continue; // temporary fix
1907 if (ix == ix1) continue; //19-12-05
1908 if (ix1 == ix0) continue;
1909 if (maxpad[1][0] < 0 || mirror && maxpad[0][0] >= 0) {
1910 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/100, 5.);
1911 // else fXyq[2][npads] = TMath::Min (aamax[0]/100, 5.);
1912 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[0]/100, 5.));
1913 else muonPad.SetCharge(TMath::Min (aamax[0]/100, 5.));
1916 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/100, 5.);
1917 // else fXyq[2][npads] = TMath::Min (aamax[1]/100, 5.);
1918 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[1]/100, 5.));
1919 else muonPad.SetCharge(TMath::Min (aamax[1]/100, 5.));
1921 // fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
1922 if (muonPad.Charge() < 1.) muonPad.SetCharge(1.);
1923 // fXyq[3][npads] = -2; // flag
1924 // fPadIJ[2][npads] = ix;
1925 // fPadIJ[3][npads] = iy;
1926 muonPad.SetReal(kFALSE);
1930 if (fDebug) printf(" ***** Add virtual pad in X ***** %f %f %f %3d %3d %f %f \n",
1931 muonPad.Charge(), muonPad.Coord(0), muonPad.Coord(1), ix, iy,
1932 muonPad.DX(), muonPad.DY());
1933 cluster.AddPad(muonPad); // add pad to the cluster
1937 if (nInY != 2) continue;
1938 if (!mirror && cath && maxpad[0][0] >= 0) continue;
1939 if (mirror && !cath && maxpad[1][0] >= 0) continue;
1940 if (TMath::Abs(iy-iy0) == 1 || TMath::Abs(iy*iy0) == 1) {
1941 if (ix != ix0) continue; // new segmentation - check
1942 if (iPad && !iAddY) continue;
1943 AliMpPad pad = fSegmentation[cath]->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1944 // fXyq[0][npads] = pad.Position().X();
1945 // fXyq[1][npads] = pad.Position().Y();
1946 // fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
1947 AliMUONPad muonPad(fDetElemId, cath, ix, iy, pad.Position().X(), pad.Position().Y(),
1948 pad.Dimensions().X(), pad.Dimensions().Y(), 0);
1949 if (iy1 == iy0) continue;
1950 //if (iPad && iy1 == iy0) continue;
1951 if (maxpad[0][0] < 0 || mirror && maxpad[1][0] >= 0) {
1952 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/15, fgkZeroSuppression);
1953 // else fXyq[2][npads] = TMath::Min (aamax[1]/15, fgkZeroSuppression);
1954 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[1]/15, fgkZeroSuppression));
1955 else muonPad.SetCharge(TMath::Min (aamax[1]/15, fgkZeroSuppression));
1958 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/15, fgkZeroSuppression);
1959 // else fXyq[2][npads] = TMath::Min (aamax[0]/15, fgkZeroSuppression);
1960 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[0]/15, fgkZeroSuppression));
1961 else muonPad.SetCharge(TMath::Min (aamax[0]/15, fgkZeroSuppression));
1963 // fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
1964 if (muonPad.Charge() < 1.) muonPad.SetCharge(1.);
1965 // fXyq[3][npads] = -2; // flag
1966 // fPadIJ[2][npads] = ix;
1967 // fPadIJ[3][npads] = iy;
1968 muonPad.SetReal(kFALSE);
1972 if (fDebug) printf(" ***** Add virtual pad in Y ***** %f %f %f %3d %3d %f %f \n",
1973 muonPad.Charge(), muonPad.Coord(0), muonPad.Coord(1), ix, iy,
1974 muonPad.DX(), muonPad.DY());
1975 cluster.AddPad(muonPad); // add pad to the cluster
1978 } // for (Int_t j=0; j<nn;
1979 } // for (Int_t iPad=0;
1980 } // for (cath=0; cath<2;
1983 //_____________________________________________________________________________
1984 void AliMUONClusterFinderMLEM::PadsInXandY(AliMUONCluster& cluster,
1985 Int_t &nInX, Int_t &nInY) const
1987 /// Find number of pads in X and Y-directions (excluding virtual ones and
1990 Int_t statusToTest = 1;
1992 if ( nInX < 0 ) statusToTest = 0;
1994 Bool_t mustMatch(kTRUE);
1996 AliMpIntPair cn = cluster.NofPads(statusToTest,mustMatch);
1998 nInX = cn.GetFirst();
1999 nInY = cn.GetSecond();
2002 //_____________________________________________________________________________
2003 void AliMUONClusterFinderMLEM::RemovePixel(Int_t i)
2005 /// Remove pixel at index i
2006 AliMUONPad* pixPtr = Pixel(i);
2007 fPixArray->RemoveAt(i);
2011 //_____________________________________________________________________________
2012 void AliMUONClusterFinderMLEM::Simple(AliMUONCluster& cluster)
2014 /// Process simple cluster (small number of pads) without EM-procedure
2016 Int_t nForFit = 1, clustFit[1] = {0}, nfit;
2017 Double_t parOk[3] = {0.};
2018 TObjArray *clusters[1];
2019 clusters[0] = fPixArray;
2021 AliDebug(1,Form("nPix=%d",fPixArray->GetLast()+1));
2023 for (Int_t i = 0; i < cluster.Multiplicity(); ++i)
2025 AliMUONPad* pad = cluster.Pad(i);
2026 if ( pad->IsSaturated())
2035 nfit = fSplitter->Fit(cluster,1, nForFit, clustFit, clusters, parOk, fClusterList);
2038 //_____________________________________________________________________________
2040 AliMUONClusterFinderMLEM::Pixel(Int_t i) const
2042 /// Returns pixel at index i
2043 return static_cast<AliMUONPad*>(fPixArray->UncheckedAt(i));
2046 //_____________________________________________________________________________
2048 AliMUONClusterFinderMLEM::Print(Option_t* what) const
2051 TString swhat(what);
2053 if ( swhat.Contains("precluster") )
2055 if ( fPreCluster) fPreCluster->Print();