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 //-----------------------------------------------------------------------------
19 /// \class AliMUONClusterFinderMLEM
21 /// Clusterizer class based on the Expectation-Maximization algorithm
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).
27 /// \author Laurent Aphecetche (for the "new" C++ structure) and
28 /// Alexander Zinchenko, JINR Dubna, for the hardcore of it ;-)
29 //-----------------------------------------------------------------------------
31 #include "AliMUONClusterFinderMLEM.h"
33 #include "AliMUONCluster.h"
34 #include "AliMUONClusterSplitterMLEM.h"
35 #include "AliMUONVDigit.h"
36 #include "AliMUONPad.h"
37 #include "AliMUONPreClusterFinder.h"
39 #include "AliMpVPadIterator.h"
40 #include "AliMpVSegmentation.h"
41 #include "AliRunLoader.h"
42 #include "AliMUONVDigitStore.h"
43 #include <Riostream.h>
47 #include <TStopwatch.h>
50 //#include "AliCodeTimer.h"
53 ClassImp(AliMUONClusterFinderMLEM)
56 const Double_t AliMUONClusterFinderMLEM::fgkZeroSuppression = 6; // average zero suppression value
57 const Double_t AliMUONClusterFinderMLEM::fgkSaturation = 3000; // average saturation level
58 const Double_t AliMUONClusterFinderMLEM::fgkDistancePrecision = 1e-6; // (cm) used to check overlaps and so on
59 const TVector2 AliMUONClusterFinderMLEM::fgkIncreaseSize(-AliMUONClusterFinderMLEM::fgkDistancePrecision,-AliMUONClusterFinderMLEM::fgkDistancePrecision);
60 const TVector2 AliMUONClusterFinderMLEM::fgkDecreaseSize(AliMUONClusterFinderMLEM::fgkDistancePrecision,AliMUONClusterFinderMLEM::fgkDistancePrecision);
62 TMinuit* AliMUONClusterFinderMLEM::fgMinuit = 0x0;
64 //_____________________________________________________________________________
65 AliMUONClusterFinderMLEM::AliMUONClusterFinderMLEM(Bool_t plot, AliMUONVClusterFinder* clusterFinder)
66 : AliMUONVClusterFinder(),
67 fPreClusterFinder(clusterFinder),
76 fPixArray(new TObjArray(20)),
79 fTimers(new TObjArray(kLast)),
86 fSegmentation[1] = fSegmentation[0] = 0x0;
88 fPadBeg[0] = fPadBeg[1] = fCathBeg;
90 if (!fgMinuit) fgMinuit = new TMinuit(8);
92 fTimers->SetOwner(kTRUE);
94 for ( Int_t i = 0; i < kLast; ++i )
96 TStopwatch* t = new TStopwatch;
97 fTimers->AddLast(new TStopwatch);
101 if (fPlot) fDebug = 1;
104 //_____________________________________________________________________________
105 AliMUONClusterFinderMLEM::~AliMUONClusterFinderMLEM()
108 delete fgMinuit; fgMinuit = 0; delete fPixArray; fPixArray = 0;
110 delete fPreClusterFinder;
111 for ( Int_t i = 0; i < kLast; ++i )
113 AliInfo(Form("Timer %d",i));
118 AliInfo(Form("Total clusters %d AddVirtualPad needed %d",
119 fNClusters,fNAddVirtualPads));
122 //_____________________________________________________________________________
124 AliMUONClusterFinderMLEM::Prepare(const AliMpVSegmentation* segmentations[2],
125 const AliMUONVDigitStore& digitStore)
127 /// Prepare for clustering
128 // AliCodeTimerAuto("")
130 for ( Int_t i = 0; i < 2; ++i )
132 fSegmentation[i] = segmentations[i];
135 // Find out the DetElemId
138 TIter next(digitStore.CreateIterator());
139 AliMUONVDigit* d = static_cast<AliMUONVDigit*>(next());
143 fDetElemId = d->DetElemId();
146 if ( fDetElemId < 0 )
148 AliWarning("Could not find DE. Probably no digits at all ?");
153 fSplitter = new AliMUONClusterSplitterMLEM(fDetElemId,fPixArray);
154 fSplitter->SetDebug(fDebug);
156 // find out current event number, and reset the cluster number
157 fEventNumber = AliRunLoader::GetRunLoader()->GetEventNumber();
159 fClusterList.Delete();
161 AliDebug(3,Form("EVT %d DE %d",fEventNumber,fDetElemId));
163 return fPreClusterFinder->Prepare(segmentations,digitStore);
166 //_____________________________________________________________________________
168 AliMUONClusterFinderMLEM::NextCluster()
170 /// Return next cluster
171 // AliCodeTimerAuto("clustering + pre-clustering")
173 // if the list of clusters is not void, pick one from there
174 TObject* o = fClusterList.At(++fClusterNumber);
175 if ( o != 0x0 ) return static_cast<AliMUONCluster*>(o);
177 //FIXME : at this point, must check whether we've used all the digits
178 //from precluster : if not, let the preclustering know about those unused
179 //digits, so it can reuse them
181 // if the cluster list is exhausted, we need to go to the next
182 // pre-cluster and treat it
184 fPreCluster = fPreClusterFinder->NextCluster();
192 fClusterList.Delete(); // reset the list of clusters for this pre-cluster
193 fClusterNumber = -1; //AZ
197 // WorkOnPreCluster may have used only part of the pads, so we check that
198 // now, and let the unused pads be reused by the preclustering...
200 for ( Int_t i = 0; i < fPreCluster->Multiplicity(); ++i )
202 AliMUONPad* pad = fPreCluster->Pad(i);
203 if ( !pad->IsUsed() )
205 fPreClusterFinder->UsePad(*pad);
209 return NextCluster();
212 //_____________________________________________________________________________
214 AliMUONClusterFinderMLEM::WorkOnPreCluster()
216 /// Starting from a precluster, builds a pixel array, and then
217 /// extract clusters from this array
219 // Set saturation flag - it is not set if working directly with MC digits (w/out
220 // creating raw data) !!!
221 for (Int_t j = 0; j < fPreCluster->Multiplicity(); ++j) {
222 AliMUONPad* pad = fPreCluster->Pad(j);
223 if (pad->IsSaturated()) break;
224 if (pad->Charge() > fgkSaturation-1) pad->SetSaturated(kTRUE); //FIXME : remove usage of fgkSaturation
228 cout << " *** Event # " << AliRunLoader::GetRunLoader()->GetEventNumber()
229 << " det. elem.: " << fDetElemId << endl;
230 for (Int_t j=0; j<fPreCluster->Multiplicity(); ++j) {
231 AliMUONPad* pad = fPreCluster->Pad(j);
232 printf(" bbb %3d %1d %8.4f %8.4f %8.4f %8.4f %6.1f %3d %3d %2d %1d %1d \n",
233 j, pad->Cathode(), pad->Coord(0), pad->Coord(1), pad->DX()*2, pad->DY()*2,
234 pad->Charge(), pad->Ix(), pad->Iy(), pad->Status(), pad->IsReal(), pad->IsSaturated());
238 AliMUONCluster* cluster = CheckPrecluster(*fPreCluster);
239 if (!cluster) return kFALSE;
241 BuildPixArray(*cluster);
243 if ( fPixArray->GetLast() < 0 )
245 AliDebug(1,"No pixel for the above cluster");
250 // Use MLEM for cluster finder
251 Int_t nMax = 1, localMax[100], maxPos[100];
252 Double_t maxVal[100];
254 if (cluster->Multiplicity() > 50)
256 nMax = FindLocalMaxima(fPixArray, localMax, maxVal);
261 TMath::Sort(nMax, maxVal, maxPos, kTRUE); // in decreasing order
264 Int_t iSimple = 0, nInX = -1, nInY;
266 PadsInXandY(*cluster,nInX, nInY);
268 if (nMax == 1 && nInX < 4 && nInY < 4)
270 iSimple = 1; //1; // simple cluster
273 for (Int_t i=0; i<nMax; ++i)
277 FindCluster(*cluster,localMax, maxPos[i]);
279 Timer(kMainLoop)->Start(kFALSE);
280 MainLoop(*cluster,iSimple);
281 Timer(kMainLoop)->Stop();
284 for (Int_t j=0; j<cluster->Multiplicity(); ++j)
286 AliMUONPad* pad = cluster->Pad(j);
287 if ( pad->Status() == 0 ) continue; // pad charge was not modified
289 pad->RevertCharge(); // use backup charge value
292 } // for (Int_t i=0; i<nMax;
293 if (nMax > 1) ((TH2D*) gROOT->FindObject("anode"))->Delete();
294 TH2D *mlem = (TH2D*) gROOT->FindObject("mlem");
295 if (mlem) mlem->Delete();
300 //_____________________________________________________________________________
302 AliMUONClusterFinderMLEM::Overlap(const AliMUONPad& pad, const AliMUONPad& pixel)
304 /// Check if the pad and the pixel overlaps
306 // make a fake pad from the pixel
307 AliMUONPad tmp(pad.DetElemId(),pad.Cathode(),pad.Ix(),pad.Iy(),
308 pixel.Coord(0),pixel.Coord(1),
309 pixel.Size(0),pixel.Size(1),0);
311 return AliMUONPad::AreOverlapping(pad,tmp,fgkDecreaseSize);
314 //_____________________________________________________________________________
316 AliMUONClusterFinderMLEM::CheckPrecluster(const AliMUONCluster& origCluster)
318 /// Check precluster in order to attempt to simplify it (mostly for
319 /// two-cathode preclusters)
321 if (origCluster.Multiplicity()==1)
323 // Disregard one-pad clusters (leftovers from splitting)
327 Timer(kCheckPreCluster)->Start(kFALSE);
330 AliMUONCluster* cluster = static_cast<AliMUONCluster*>(origCluster.Clone());
332 AliDebug(2,"Start of CheckPreCluster=");
333 // StdoutToAliDebug(2,cluster->Print("full"));
335 // Check if one-cathode precluster
336 Int_t i1 = cluster->Multiplicity(0) ? 0 : 1;
337 Int_t i2 = cluster->Multiplicity(1) ? 1 : 0;
339 AliMUONCluster* rv(0x0);
343 rv = CheckPreclusterTwoCathodes(cluster);
347 rv = CheckPreclusterOneCathode(cluster);
349 Timer(kCheckPreCluster)->Stop();
353 //_____________________________________________________________________________
355 AliMUONClusterFinderMLEM::CheckPreclusterOneCathode(AliMUONCluster* cluster)
357 /// Check single-cathode precluster
358 AliWarning("Reimplement me!");
359 AliDebug(2,"End of CheckPreClusterOneCathode=");
360 // StdoutToAliDebug(2,cluster->Print("full"));
365 //_____________________________________________________________________________
367 AliMUONClusterFinderMLEM::CheckPreclusterTwoCathodes(AliMUONCluster* cluster)
369 /// Check two-cathode cluster
371 Int_t i1 = cluster->Multiplicity(0) ? 0 : 1;
372 Int_t i2 = cluster->Multiplicity(1) ? 1 : 0;
374 Int_t npad = cluster->Multiplicity();
375 Int_t* flags = new Int_t[npad];
376 memset(flags,0,npad*sizeof(Int_t));
378 // Check pad overlaps
379 for ( Int_t i=0; i<npad; ++i)
381 AliMUONPad* padi = cluster->Pad(i);
382 if ( padi->Cathode() != i1 ) continue;
383 for (Int_t j=i+1; j<npad; ++j)
385 AliMUONPad* padj = cluster->Pad(j);
386 if ( padj->Cathode() != i2 ) continue;
387 if ( !AliMUONPad::AreOverlapping(*padi,*padj,fgkDecreaseSize) ) continue;
388 flags[i] = flags[j] = 1; // mark overlapped pads
392 // Check if all pads overlap
394 for (Int_t i=0; i<npad; ++i)
396 if (flags[i]) continue;
402 // not all pads overlap.
403 if (fDebug) cout << " nFlags: " << nFlags << endl;
404 TObjArray toBeRemoved;
405 for (Int_t i=0; i<npad; ++i)
407 AliMUONPad* pad = cluster->Pad(i);
408 if (flags[i]) continue;
409 Int_t cath = pad->Cathode();
410 Int_t cath1 = TMath::Even(cath);
411 // Check for edge effect (missing pads on the _other_ cathode)
412 AliMpPad mpPad = fSegmentation[cath1]->PadByPosition(pad->Position(),kFALSE);
413 if (!mpPad.IsValid()) continue;
414 if (nFlags == 1 && pad->Charge() < fgkZeroSuppression * 3) continue;
415 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
416 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),pad->Charge()));
417 toBeRemoved.AddLast(pad);
418 //AZ cluster->RemovePad(pad);
419 fPreCluster->Pad(i)->Release();
422 for ( Int_t i = 0; i <= toBeRemoved.GetLast(); ++i )
424 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.At(i)));
428 // Check correlations of cathode charges
429 if ( !cluster->IsSaturated() && cluster->ChargeAsymmetry() > 1 )
432 Int_t cathode = cluster->MaxRawChargeCathode();
438 // get min and max pad charges on the cathode opposite to the
439 // max pad (given by MaxRawChargeCathode())
441 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
443 AliMUONPad* pad = cluster->Pad(i);
444 if ( pad->Cathode() != cathode || !pad->IsReal() )
446 // only consider pads in the opposite cathode, and
447 // onyl consider real pads (i.e. exclude the virtual ones)
450 if ( pad->Charge() < cmin )
452 cmin = pad->Charge();
455 if ( pad->Charge() > cmax )
457 cmax = pad->Charge();
461 AliDebug(2,Form("Pad imin,imax %d,%d cmin,cmax %e,%e",
462 imin,imax,cmin,cmax));
464 // arrange pads according to their distance to the max, normalized
466 Double_t* dist = new Double_t[cluster->Multiplicity()];
471 AliMUONPad* padmax = cluster->Pad(imax);
473 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
476 if ( i == imax) continue;
477 AliMUONPad* pad = cluster->Pad(i);
478 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
479 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
480 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
481 dist[i] = TMath::Sqrt(dx*dx+dy*dy);
484 dmin = dist[i] + 1E-3; // distance to the pad with minimum charge
490 TMath::Sort(cluster->Multiplicity(),dist,flags,kFALSE); // in ascending order
492 TObjArray toBeRemoved;
494 for ( Int_t i = 0; i < cluster->Multiplicity(); ++i )
496 Int_t indx = flags[i];
497 AliMUONPad* pad = cluster->Pad(indx);
498 if ( pad->Cathode() != cathode || !pad->IsReal() ) continue;
499 if ( dist[indx] > dmin )
501 // farther than the minimum pad
502 Double_t dx = (pad->X()-padmax->X())/padmax->DX()/2.0;
503 Double_t dy = (pad->Y()-padmax->Y())/padmax->DY()/2.0;
506 if (dx >= 0 && dy >= 0) continue;
507 if (TMath::Abs(dx) > TMath::Abs(dy) && dx >= 0) continue;
508 if (TMath::Abs(dy) > TMath::Abs(dx) && dy >= 0) continue;
510 if ( pad->Charge() <= cmax || TMath::Abs(dist[indx]-xmax) < 1E-3 )
513 if (TMath::Abs(dist[indx]-xmax) < 1.e-3)
515 cmax = TMath::Max(pad->Charge(),cmax);
519 cmax = pad->Charge();
522 AliDebug(2,Form("Releasing the following pad : de,cath,ix,iy %d,%d,%d,%d charge %e",
523 fDetElemId,pad->Cathode(),pad->Ix(),pad->Iy(),
526 toBeRemoved.AddLast(pad);
527 fPreCluster->Pad(indx)->Release();
530 for ( Int_t i = 0; i <= toBeRemoved.GetLast(); ++i )
532 cluster->RemovePad(static_cast<AliMUONPad*>(toBeRemoved.At(i)));
539 AliDebug(2,"End of CheckPreClusterTwoCathodes=");
540 // StdoutToAliDebug(2,cluster->Print("full"));
545 //_____________________________________________________________________________
547 AliMUONClusterFinderMLEM::CheckOverlaps()
549 /// For debug only : check if some pixels overlap...
551 Int_t nPix = fPixArray->GetLast()+1;
554 for ( Int_t i = 0; i < nPix; ++i )
556 AliMUONPad* pixelI = Pixel(i);
557 AliMUONPad pi(dummy,dummy,dummy,dummy,
558 pixelI->Coord(0),pixelI->Coord(1),
559 pixelI->Size(0),pixelI->Size(1),0.0);
561 for ( Int_t j = i+1; j < nPix; ++j )
563 AliMUONPad* pixelJ = Pixel(j);
564 AliMUONPad pj(dummy,dummy,dummy,dummy,
565 pixelJ->Coord(0),pixelJ->Coord(1),
566 pixelJ->Size(0),pixelJ->Size(1),0.0);
569 if ( AliMUONPad::AreOverlapping(pi,pj,fgkDecreaseSize,area) )
571 AliInfo(Form("The following 2 pixels (%d and %d) overlap !",i,j));
572 StdoutToAliInfo(pixelI->Print();
573 cout << " Surface = " << pixelI->Size(0)*pixelI->Size(1)*4 << endl;
575 cout << " Surface = " << pixelJ->Size(0)*pixelJ->Size(1)*4 << endl;
576 cout << " Area surface = " << area.Dimensions().X()*area.Dimensions().Y()*4 << endl;
577 cout << "-------" << endl;
585 //_____________________________________________________________________________
586 void AliMUONClusterFinderMLEM::BuildPixArray(AliMUONCluster& cluster)
588 /// Build pixel array for MLEM method
590 Int_t npad = cluster.Multiplicity();
593 AliWarning("Got no pad at all ?!");
598 if ( !cluster.Multiplicity(0) || !cluster.Multiplicity(1) )
600 BuildPixArrayOneCathode(cluster);
604 BuildPixArrayTwoCathodes(cluster);
607 fPixArray->Sort(); // FIXME : not really needed, only to compare with ClusterFinderAZ
609 Int_t nPix = fPixArray->GetLast()+1;
611 AliDebug(2,Form("nPix after BuildPixArray=%d",nPix));
613 Double_t xPadMin(1E9);
614 Double_t yPadMin(1E9);
616 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
618 AliMUONPad* pad = cluster.Pad(i);
619 xPadMin = TMath::Min (xPadMin, pad->DX());
620 yPadMin = TMath::Min (yPadMin, pad->DY());
626 for ( Int_t i = 0; i < nPix; ++i )
628 AliMUONPad* pixPtr = Pixel(i);
629 wxmin = TMath::Min(wxmin, pixPtr->Size(0));
630 wymin = TMath::Min(wymin, pixPtr->Size(1));
633 wxmin = TMath::Abs (wxmin - xPadMin/2) > 0.001 ? xPadMin : xPadMin / 2;
634 wymin = TMath::Abs (wymin - yPadMin/2) > 0.001 ? yPadMin : yPadMin / 2;
636 // Check if small pixel X-size
637 AdjustPixel(cluster,wxmin, 0);
638 // Check if small pixel Y-size
639 AdjustPixel(cluster,wymin, 1);
640 // Check if large pixel size
641 AdjustPixel(wxmin, wymin);
643 // Remove discarded pixels
644 for (Int_t i=0; i<nPix; ++i)
646 AliMUONPad* pixPtr = Pixel(i);
647 if (pixPtr->Charge() < 1)
649 AliDebug(2,Form("Removing pixel %d with charge<1 : ",i));
650 // StdoutToAliDebug(2,pixPtr->Print());
655 fPixArray->Compress();
656 nPix = fPixArray->GetEntriesFast();
658 AliDebug(2,Form("nPix after AdjustPixel=%d",nPix));
660 if ( nPix > cluster.Multiplicity() )
662 AliDebug(2,Form("Will trim number of pixels to number of pads"));
664 // Too many pixels - sort and remove pixels with the lowest signal
666 for ( Int_t i = cluster.Multiplicity(); i<nPix; ++i )
670 fPixArray->Compress();
671 nPix = fPixArray->GetEntriesFast();
672 } // if (nPix > npad)
674 // StdoutToAliDebug(2,cout << "End of BuildPixelArray:" << endl;
675 // fPixArray->Print(););
676 // CheckOverlaps();//FIXME : this is for debug only. Remove it.
679 //_____________________________________________________________________________
680 void AliMUONClusterFinderMLEM::BuildPixArrayOneCathode(AliMUONCluster& cluster)
682 /// From a single-cathode cluster, build the pixel array
684 AliDebug(2,Form("cluster.Multiplicity=%d",cluster.Multiplicity()));
686 for ( Int_t j=0; j<cluster.Multiplicity(); ++j)
688 AliMUONPad* pad = cluster.Pad(j);
689 AliMUONPad* pixPtr = new AliMUONPad(pad->Position(),pad->Dimensions(),
691 fPixArray->Add(pixPtr);
695 //_____________________________________________________________________________
696 void AliMUONClusterFinderMLEM::BuildPixArrayTwoCathodes(AliMUONCluster& cluster)
698 /// From a two-cathodes cluster, build the pixel array
700 AliDebug(2,Form("cluster.Multiplicity=%d",cluster.Multiplicity()));
702 Int_t i1 = cluster.Pad(0)->Cathode();
703 Int_t i2 = TMath::Even(i1);
705 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i)
707 AliMUONPad* padi = cluster.Pad(i);
708 if (padi->Cathode() != i1) continue;
710 for ( Int_t j = 1; j < cluster.Multiplicity(); ++j)
712 AliMUONPad* padj = cluster.Pad(j);
713 if (padj->Cathode() != i2) continue;
717 if ( AliMUONPad::AreOverlapping(*padi,*padj,fgkDecreaseSize,overlap) )
719 AliMUONPad* pixPtr = new AliMUONPad(overlap.Position(),
720 overlap.Dimensions(),
721 TMath::Min(padi->Charge(),padj->Charge()));
722 if ( ( padi->Charge() <= padj->Charge() && padi->IsSaturated() ) ||
723 ( padj->Charge() < padi->Charge() && padj->IsSaturated() ) )
725 // if smallest charge of the 2 pads is already above saturation, then
726 // the pixel is saturated...
727 pixPtr->SetSaturated(kTRUE);
729 pixPtr->SetReal(kFALSE);
730 fPixArray->Add(pixPtr);
736 //_____________________________________________________________________________
737 void AliMUONClusterFinderMLEM::AdjustPixel(AliMUONCluster& cluster,
738 Float_t width, Int_t ixy)
740 /// Check if some pixels have small size (adjust if necessary)
742 AliDebug(2,Form("width=%e ixy=%d",width,ixy));
744 AliMUONPad *pixPtr, *pixPtr1 = 0;
745 Int_t ixy1 = TMath::Even(ixy);
746 Int_t nPix = fPixArray->GetEntriesFast();
748 for (Int_t i=0; i<nPix; i++)
751 if (pixPtr->Charge() < 1) continue; // discarded pixel
752 if (pixPtr->Size(ixy)-width < -1.e-4)
755 for (Int_t j=i+1; j<nPix; j++)
758 if (pixPtr1->Charge() < 1) continue; // discarded pixel
759 if (TMath::Abs(pixPtr1->Size(ixy)-width) < fgkDistancePrecision) continue; // right size
760 if (TMath::Abs(pixPtr1->Coord(ixy1)-pixPtr->Coord(ixy1)) > fgkDistancePrecision) continue; // different rows/columns
761 if (TMath::Abs(pixPtr1->Coord(ixy)-pixPtr->Coord(ixy)) < 2*width)
764 Double_t tmp = pixPtr->Coord(ixy) + pixPtr1->Size(ixy)*
765 TMath::Sign (1., pixPtr1->Coord(ixy) - pixPtr->Coord(ixy));
766 pixPtr->SetCoord(ixy, tmp);
767 pixPtr->SetSize(ixy, width);
768 pixPtr->SetCharge(TMath::Min (pixPtr->Charge(),pixPtr1->Charge()));
769 pixPtr1->SetCharge(0);
773 } // for (Int_t j=i+1;
774 if (pixPtr1 || i == nPix-1)
776 // edge pixel - just increase its size
777 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
779 AliMUONPad* pad = cluster.Pad(j);
780 Double_t d = ( ixy == 0 ) ? pad->X() : ( ixy == 1 ) ? pad->Y() : -1E9;
782 if (pixPtr->Coord(ixy) < d)
784 pixPtr->Shift(ixy, pixPtr->Size(ixy)-width);
788 pixPtr->Shift(ixy, -pixPtr->Size(ixy)+width);
790 pixPtr->SetSize(ixy, width);
794 } // if (pixPtr->Size(ixy)-width < -1.e-4)
795 } // for (Int_t i=0; i<nPix;
798 //_____________________________________________________________________________
799 void AliMUONClusterFinderMLEM::AdjustPixel(Double_t wxmin, Double_t wymin)
801 /// Check if some pixels have large size (adjust if necessary)
803 AliDebug(2,Form("wxmin=%e wymin=%e",wxmin,wymin));
805 Int_t n1[2], n2[2], iOK = 1, nPix = fPixArray->GetEntriesFast();
806 AliMUONPad *pixPtr, pix;
807 Double_t xy0[2] = {9999, 9999}, wxy[2], dist[2] = {0};
809 // Check if large pixel size
810 for (Int_t i = 0; i < nPix; i++) {
811 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
812 if (pixPtr->Charge() < 1) continue; // discarded pixel
813 if (pixPtr->Size(0) - wxmin < 1.e-4) {
814 if (xy0[0] > 9998) xy0[0] = pixPtr->Coord(0); // position of a "normal" pixel
815 if (pixPtr->Size(1) - wymin < 1.e-4) {
816 if (xy0[1] > 9998) xy0[1] = pixPtr->Coord(1); // position of a "normal" pixel
818 } else iOK = 0; // large pixel
820 iOK = 0; // large pixel
821 if (xy0[1] > 9998 && pixPtr->Size(1) - wymin < 1.e-4) xy0[1] = pixPtr->Coord(1); // "normal" pixel
823 if (xy0[0] < 9998 && xy0[1] < 9998) break;
829 //cout << xy0[0] << " " << xy0[1] << endl;
830 for (Int_t i = 0; i < nPix; i++) {
831 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
832 if (pixPtr->Charge() < 1) continue; // discarded pixel
835 for (Int_t j = 0; j < 2; j++) {
836 if (pixPtr->Size(j) - wxy[j] < 1.e-4) continue;
837 dist[j] = (pixPtr->Coord(j) - xy0[j]) / wxy[j] / 2; // normalized distance to "normal" pixel
838 n2[j] = TMath::Nint (pixPtr->Size(j) / wxy[j]);
839 n1[j] = n2[j] == 1 ? TMath::Nint(dist[j]) : (Int_t)dist[j];
841 if (n1[0] > 998 && n1[1] > 998) continue;
842 if (fDebug) cout << " Different " << pixPtr->Size(0) << " " << wxy[0] << " "
843 << pixPtr->Size(1) << " " << wxy[1] <<endl;
845 if (n2[0] > 2 || n2[1] > 2) {
846 //cout << n2[0] << " " << n2[1] << endl;
847 if (n2[0] > 2 && n1[0] < 999) n1[0]--;
848 if (n2[1] > 2 && n1[1] < 999) n1[1]--;
850 //cout << n1[0] << " " << n2[0] << " " << n1[1] << " " << n2[1] << endl;
852 pix.SetSize(0, wxy[0]); pix.SetSize(1, wxy[1]);
854 for (Int_t ii = 0; ii < n2[0]; ii++) {
855 if (n1[0] < 999) pix.SetCoord(0, xy0[0] + (n1[0] + TMath::Sign(1.,dist[0]) * ii) * 2 * wxy[0]);
856 for (Int_t jj = 0; jj < n2[1]; jj++) {
857 if (n1[1] < 999) pix.SetCoord(1, xy0[1] + (n1[1] + TMath::Sign(1.,dist[1]) * jj) * 2 * wxy[1]);
858 fPixArray->Add(new AliMUONPad(pix));
862 pixPtr->SetCharge(0);
863 } // for (Int_t i = 0; i < nPix;
866 //_____________________________________________________________________________
868 AliMUONClusterFinderMLEM::Plot(const char* basename)
870 /// Make a plot and save it as png
875 TCanvas* c = new TCanvas("MLEM","MLEM",800,600);
880 c->Print(Form("%s.EVT%d.DE%d.CLU%d.png",basename,fEventNumber,
881 fDetElemId,fClusterNumber));
884 //_____________________________________________________________________________
886 AliMUONClusterFinderMLEM::ComputeCoefficients(AliMUONCluster& cluster,
890 /// Compute coefficients needed for MLEM algorithm
892 Int_t nPix = fPixArray->GetLast()+1;
894 memset(probi,0,nPix*sizeof(Double_t));
896 for ( Int_t j=0; j<cluster.Multiplicity(); ++j )
898 AliMUONPad* pad = cluster.Pad(j);
901 for ( Int_t ipix=0; ipix<nPix; ++ipix )
903 Int_t indx1 = indx + ipix;
904 if (pad->Status() < 0)
909 AliMUONPad* pixPtr = Pixel(ipix);
910 // coef is the charge (given by Mathieson integral) on pad, assuming
911 // the Mathieson is center at pixel.
912 coef[indx1] = fSplitter->ChargeIntegration(pixPtr->Coord(0), pixPtr->Coord(1), *pad);
913 AliDebug(2,Form("pad=(%d,%d,%e,%e,%e,%e) pix=(%e,%e,%e,%e) coef %e",
917 pixPtr->Coord(0),pixPtr->Coord(1),
918 pixPtr->Size(0),pixPtr->Size(1),
921 probi[ipix] += coef[indx1];
926 //_____________________________________________________________________________
927 Bool_t AliMUONClusterFinderMLEM::MainLoop(AliMUONCluster& cluster, Int_t iSimple)
929 /// Repeat MLEM algorithm until pixel size becomes sufficiently small
931 Int_t nPix = fPixArray->GetLast()+1;
933 AliDebug(2,Form("nPix=%d iSimple=%d, precluster=",nPix,iSimple));
934 // StdoutToAliDebug(2,cluster.Print("full"););
938 AliDebug(1,"No pixels, why am I here then ?");
941 AddVirtualPad(cluster); // add virtual pads if necessary
943 Int_t npadTot = cluster.Multiplicity();
945 for (Int_t i = 0; i < npadTot; ++i)
947 if (cluster.Pad(i)->Status() == 0) ++npadOK;
952 Double_t* probi(0x0);
953 Int_t lc(0); // loop counter (for debug)
955 // Plot("mlem.start");
960 mlem = (TH2D*) gROOT->FindObject("mlem");
963 AliDebug(2,Form("lc %d nPix %d(%d) npadTot %d npadOK %d",lc,nPix,fPixArray->GetLast()+1,npadTot,npadOK));
964 AliDebug(2,Form("EVT%d PixArray=",fEventNumber));
965 // StdoutToAliDebug(2,fPixArray->Print("","full"));
967 coef = new Double_t [npadTot*nPix];
968 probi = new Double_t [nPix];
970 // Calculate coefficients and pixel visibilities
971 ComputeCoefficients(cluster,coef,probi);
973 for (Int_t ipix=0; ipix<nPix; ++ipix)
975 if (probi[ipix] < 0.01)
977 AliMUONPad* pixel = Pixel(ipix);
978 AliDebug(2,Form("Setting the following pixel to invisible as its probi<0.01:"));
979 // StdoutToAliDebug(2,cout << Form(" -- ipix %3d --- "); pixel->Print(););
980 pixel->SetCharge(0); // "invisible" pixel
985 Mlem(cluster,coef, probi, 15);
987 Double_t xylim[4] = {999, 999, 999, 999};
988 AliMUONPad* pixPtr(0x0);
990 for ( Int_t ipix=0; ipix<nPix; ++ipix )
992 pixPtr = Pixel(ipix);
993 for ( Int_t i=0; i<4; ++i )
995 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
998 for (Int_t i=0; i<4; i++)
1000 xylim[i] -= pixPtr->Size(i/2);
1004 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1005 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1007 // StdoutToAliDebug(2,cout << "pixel used for nx,ny computation : "; pixPtr->Print(););
1008 AliDebug(2,Form("lc %d pixPtr size = %e,%e nx,ny=%d,%d xylim=%e,%e,%e,%e",
1009 lc,pixPtr->Size(0),pixPtr->Size(1),nx,ny,
1010 xylim[0],-xylim[1],xylim[2],-xylim[3]
1013 mlem = new TH2D("mlem","mlem",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1015 for (Int_t ipix=0; ipix<nPix; ++ipix)
1017 AliMUONPad* pixPtr = Pixel(ipix);
1018 mlem->Fill(pixPtr->Coord(0),pixPtr->Coord(1),pixPtr->Charge());
1021 // Check if the total charge of pixels is too low
1023 for ( Int_t i=0; i<nPix; ++i)
1025 qTot += Pixel(i)->Charge();
1028 if ( qTot < 1.e-4 || ( npadOK < 3 && qTot < 7 ) )
1030 AliDebug(1,Form("Deleting the above cluster (charge %e too low, npadOK=%d)",qTot,npadOK));
1035 fPixArray->Delete();
1036 for ( Int_t i=0; i<npadTot; ++i)
1038 AliMUONPad* pad = cluster.Pad(i);
1039 if ( pad->Status() == 0) pad->SetStatus(-1);
1046 // Simple cluster - skip further passes thru EM-procedure
1052 fPixArray->Delete();
1056 // Calculate position of the center-of-gravity around the maximum pixel
1058 FindCOG(mlem, xyCOG);
1060 if (TMath::Min(pixPtr->Size(0),pixPtr->Size(1)) < 0.07 &&
1061 pixPtr->Size(0) > pixPtr->Size(1)) break;
1063 // Sort pixels according to the charge
1065 Double_t pixMin = 0.01*Pixel(0)->Charge();
1066 pixMin = TMath::Min(pixMin,50.);
1068 // Decrease pixel size and shift pixels to make them centered at
1070 Int_t indx = (pixPtr->Size(0)>pixPtr->Size(1)) ? 0 : 1;
1073 Double_t shift[2] = { 0.0, 0.0 };
1074 for (Int_t i=0; i<4; i++) xylim[i] = 999;
1077 for (Int_t ipix=0; ipix<nPix1; ++ipix)
1079 AliMUONPad* pixPtr = Pixel(ipix);
1080 if ( nPix >= npadOK // too many pixels already
1082 pixPtr->Charge() < pixMin // too low charge
1088 for (Int_t i=0; i<2; ++i)
1092 pixPtr->SetCharge(10);
1093 pixPtr->SetSize(indx, pixPtr->Size(indx)/2);
1094 width = -pixPtr->Size(indx);
1095 pixPtr->Shift(indx, width);
1096 // Shift pixel position
1100 for (Int_t j=0; j<2; ++j)
1102 shift[j] = pixPtr->Coord(j) - xyCOG[j];
1103 shift[j] -= ((Int_t)(shift[j]/pixPtr->Size(j)/2))*pixPtr->Size(j)*2;
1106 pixPtr->Shift(0, -shift[0]);
1107 pixPtr->Shift(1, -shift[1]);
1111 pixPtr = new AliMUONPad(*pixPtr);
1112 pixPtr->Shift(indx, -2*width);
1113 fPixArray->Add(pixPtr);
1115 for (Int_t i=0; i<4; ++i)
1117 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1119 } // for (Int_t i=0; i<2;
1121 } // for (Int_t ipix=0;
1123 fPixArray->Compress();
1124 nPix = fPixArray->GetEntriesFast();
1126 AliDebug(2,Form("After shift:"));
1127 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1128 // Plot(Form("mlem.lc%d",lc+1));
1130 AliDebug(2,Form(" xyCOG=%9.6f %9.6f xylim=%9.6f,%9.6f,%9.6f,%9.6f",
1133 xylim[2],xylim[3]));
1135 // Remove excessive pixels
1138 for (Int_t ipix=npadOK; ipix<nPix; ++ipix)
1145 AliMUONPad* pixPtr = Pixel(0);
1146 // add pixels if the maximum is at the limit of pixel area
1147 // start from Y-direction
1149 for (Int_t i=3; i>-1; --i)
1151 if (nPix < npadOK &&
1152 TMath::Abs((i%2 ? -1 : 1)*xylim[i]-xyCOG[i/2]) < pixPtr->Size(i/2))
1154 AliMUONPad* p = static_cast<AliMUONPad*>(pixPtr->Clone());
1155 p->SetCoord(i/2, xyCOG[i/2]+(i%2 ? 2:-2)*pixPtr->Size(i/2));
1156 j = TMath::Even (i/2);
1157 p->SetCoord(j, xyCOG[j]);
1158 AliDebug(2,Form("Adding pixel on the edge (i=%d) ",i));
1159 // StdoutToAliDebug(2,cout << " ---- ";
1160 // p->Print("corners"););
1166 fPixArray->Compress();
1167 nPix = fPixArray->GetEntriesFast();
1174 AliDebug(2,Form("At the end of while loop nPix=%d : ",fPixArray->GetLast()+1));
1175 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1177 // remove pixels with low signal or low visibility
1178 // Cuts are empirical !!!
1179 Double_t thresh = TMath::Max (mlem->GetMaximum()/100.,1.);
1180 thresh = TMath::Min (thresh,50.);
1182 Double_t charge = 0;
1184 for ( Int_t i=0; i<nPix; ++i)
1186 cmax = TMath::Max (cmax,probi[i]);
1189 // Mark pixels which should be removed
1190 for (Int_t i=0; i<nPix; ++i)
1192 AliMUONPad* pixPtr = Pixel(i);
1193 charge = pixPtr->Charge();
1194 if (charge < thresh)
1196 pixPtr->SetCharge(-charge);
1200 // Move charge of removed pixels to their nearest neighbour (to keep total charge the same)
1202 for (Int_t i=0; i<nPix; ++i)
1204 AliMUONPad* pixPtr = Pixel(i);
1205 charge = pixPtr->Charge();
1206 if (charge > 0) continue;
1207 near = FindNearest(pixPtr);
1208 pixPtr->SetCharge(0);
1209 probi[i] = 0; // make it "invisible"
1210 AliMUONPad* pnear = Pixel(near);
1211 pnear->SetCharge(pnear->Charge() + (-charge));
1213 Mlem(cluster,coef,probi,2);
1215 AliDebug(2,Form("Before splitting nPix=%d EVT %d DE %d",fPixArray->GetLast()+1,fEventNumber,fDetElemId));
1216 // StdoutToAliDebug(2,fPixArray->Print("","full"););
1217 // Plot("mlem.beforesplit");
1220 for (Int_t i=0; i<nPix; ++i)
1222 AliMUONPad* pixPtr = Pixel(i);
1223 Int_t ix = mlem->GetXaxis()->FindBin(pixPtr->Coord(0));
1224 Int_t iy = mlem->GetYaxis()->FindBin(pixPtr->Coord(1));
1225 mlem->SetBinContent(ix, iy, pixPtr->Charge());
1228 // Try to split into clusters
1230 if (mlem->GetSum() < 1)
1236 fSplitter->Split(cluster,mlem,coef,fClusterList);
1243 fPixArray->Delete();
1248 //_____________________________________________________________________________
1249 void AliMUONClusterFinderMLEM::Mlem(AliMUONCluster& cluster,
1250 Double_t* coef, Double_t* probi,
1253 /// Use MLEM to find pixel charges
1255 Int_t nPix = fPixArray->GetEntriesFast();
1257 Int_t npad = cluster.Multiplicity();
1259 Double_t* probi1 = new Double_t[nPix];
1260 Double_t probMax = 0;
1261 Double_t tmp = TMath::MaxElement(nPix,probi);
1263 for (Int_t ipix=0; ipix<nPix; ++ipix)
1265 probMax = TMath::Max(probMax,probi[ipix]);
1268 if (probMax!=tmp) { AliWarning(Form("probMax=%e tmp=%e",probMax,tmp)); }
1270 for (Int_t iter=0; iter<nIter; ++iter)
1273 for (Int_t ipix=0; ipix<nPix; ++ipix)
1275 Pixel(ipix)->SetChargeBackup(0);
1276 // Correct each pixel
1277 if (probi[ipix] < 0.01) continue; // skip "invisible" pixel
1279 probi1[ipix] = probMax;
1280 for (Int_t j=0; j<npad; j++)
1282 AliMUONPad* pad = cluster.Pad(j);
1283 if (pad->Status() < 0) continue;
1285 Int_t indx1 = j*nPix;
1286 Int_t indx = indx1 + ipix;
1287 // Calculate expectation
1288 for (Int_t i=0; i<nPix; i++)
1290 sum1 += Pixel(i)->Charge()*coef[indx1+i];
1292 if ( pad->Charge() > fgkSaturation-1 && sum1 > pad->Charge() ) //FIXME : remove usage of fgkSaturation
1294 if ( !pad->IsSaturated() )
1296 AliWarning("Got a pad charge above saturation not backed-up by pad->IsSaturated() function : ");
1297 StdoutToAliWarning(pad->Print("full"));
1299 // correct for pad charge overflows
1300 probi1[ipix] -= coef[indx];
1304 if (coef[indx] > 1.e-6)
1306 sum += pad->Charge()*coef[indx]/sum1;
1308 } // for (Int_t j=0;
1309 AliMUONPad* pixPtr = Pixel(ipix);
1310 if (probi1[ipix] > 1.e-6)
1312 //AZ pixPtr->SetCharge(pixPtr->Charge()*sum/probi1[ipix]);
1313 pixPtr->SetChargeBackup(pixPtr->Charge()*sum/probi1[ipix]);
1315 } // for (Int_t ipix=0;
1316 for (Int_t i = 0; i < nPix; ++i) {
1317 AliMUONPad* pixPtr = Pixel(i);
1318 pixPtr->SetCharge(pixPtr->ChargeBackup());
1320 } // for (Int_t iter=0;
1324 //_____________________________________________________________________________
1325 void AliMUONClusterFinderMLEM::FindCOG(TH2D *mlem, Double_t *xyc)
1327 /// Calculate position of the center-of-gravity around the maximum pixel
1329 Int_t ixmax, iymax, ix, nsumx=0, nsumy=0, nsum=0;
1330 Int_t i1 = -9, j1 = -9;
1331 mlem->GetMaximumBin(ixmax,iymax,ix);
1332 Int_t nx = mlem->GetNbinsX();
1333 Int_t ny = mlem->GetNbinsY();
1334 Double_t thresh = mlem->GetMaximum()/10;
1335 Double_t x, y, cont, xq=0, yq=0, qq=0;
1337 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1338 y = mlem->GetYaxis()->GetBinCenter(i);
1339 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1340 cont = mlem->GetCellContent(j,i);
1341 if (cont < thresh) continue;
1342 if (i != i1) {i1 = i; nsumy++;}
1343 if (j != j1) {j1 = j; nsumx++;}
1344 x = mlem->GetXaxis()->GetBinCenter(j);
1353 Int_t i2 = 0, j2 = 0;
1356 // one bin in Y - add one more (with the largest signal)
1357 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1358 if (i == iymax) continue;
1359 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1360 cont = mlem->GetCellContent(j,i);
1363 x = mlem->GetXaxis()->GetBinCenter(j);
1364 y = mlem->GetYaxis()->GetBinCenter(i);
1373 if (i2 != i1) nsumy++;
1374 if (j2 != j1) nsumx++;
1376 } // if (nsumy == 1)
1379 // one bin in X - add one more (with the largest signal)
1381 for (Int_t j=TMath::Max(1,ixmax-1); j<=TMath::Min(nx,ixmax+1); j++) {
1382 if (j == ixmax) continue;
1383 for (Int_t i=TMath::Max(1,iymax-1); i<=TMath::Min(ny,iymax+1); i++) {
1384 cont = mlem->GetCellContent(j,i);
1387 x = mlem->GetXaxis()->GetBinCenter(j);
1388 y = mlem->GetYaxis()->GetBinCenter(i);
1397 if (i2 != i1) nsumy++;
1398 if (j2 != j1) nsumx++;
1400 } // if (nsumx == 1)
1402 xyc[0] = xq/qq; xyc[1] = yq/qq;
1403 AliDebug(2,Form("x,y COG = %e,%e",xyc[0],xyc[1]));
1406 //_____________________________________________________________________________
1407 Int_t AliMUONClusterFinderMLEM::FindNearest(AliMUONPad *pixPtr0)
1409 /// Find the pixel nearest to the given one
1410 /// (algorithm may be not very efficient)
1412 Int_t nPix = fPixArray->GetEntriesFast(), imin = 0;
1413 Double_t rmin = 99999, dx = 0, dy = 0, r = 0;
1414 Double_t xc = pixPtr0->Coord(0), yc = pixPtr0->Coord(1);
1417 for (Int_t i=0; i<nPix; i++) {
1418 pixPtr = (AliMUONPad*) fPixArray->UncheckedAt(i);
1419 if (pixPtr->Charge() < 0.5) continue;
1420 dx = (xc - pixPtr->Coord(0)) / pixPtr->Size(0);
1421 dy = (yc - pixPtr->Coord(1)) / pixPtr->Size(1);
1422 r = dx *dx + dy * dy;
1423 if (r < rmin) { rmin = r; imin = i; }
1429 //_____________________________________________________________________________
1431 AliMUONClusterFinderMLEM::Timer(Int_t i) const
1433 /// Return timer at index i
1434 return static_cast<TStopwatch*>(fTimers->At(i));
1437 //_____________________________________________________________________________
1439 AliMUONClusterFinderMLEM::Paint(Option_t*)
1441 /// Paint cluster and pixels
1443 AliMpArea area(fPreCluster->Area());
1445 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
1447 gVirtualX->SetFillStyle(1001);
1448 gVirtualX->SetFillColor(3);
1449 gVirtualX->SetLineColor(3);
1453 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1455 AliMUONPad* pixel = Pixel(i);
1457 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0)*s,
1458 pixel->Coord(1)-pixel->Size(1)*s,
1459 pixel->Coord(0)+pixel->Size(0)*s,
1460 pixel->Coord(1)+pixel->Size(1)*s);
1462 // for ( Int_t sign = -1; sign < 2; sign +=2 )
1464 // gPad->PaintLine(pixel->Coord(0) - pixel->Size(0),
1465 // pixel->Coord(1) + sign*pixel->Size(1),
1466 // pixel->Coord(0) + pixel->Size(0),
1467 // pixel->Coord(1) - sign*pixel->Size(1)
1473 gVirtualX->SetFillStyle(0);
1475 fPreCluster->Paint();
1477 gVirtualX->SetLineColor(1);
1478 gVirtualX->SetLineWidth(2);
1479 gVirtualX->SetFillStyle(0);
1480 gVirtualX->SetTextColor(1);
1481 gVirtualX->SetTextAlign(22);
1483 for ( Int_t i = 0; i <= fPixArray->GetLast(); ++i)
1485 AliMUONPad* pixel = Pixel(i);
1486 gPad->PaintBox(pixel->Coord(0)-pixel->Size(0),
1487 pixel->Coord(1)-pixel->Size(1),
1488 pixel->Coord(0)+pixel->Size(0),
1489 pixel->Coord(1)+pixel->Size(1));
1490 gVirtualX->SetTextSize(pixel->Size(0)*60);
1492 gPad->PaintText(pixel->Coord(0),pixel->Coord(1),Form("%d",(Int_t)(pixel->Charge())));
1496 //_____________________________________________________________________________
1497 Int_t AliMUONClusterFinderMLEM::FindLocalMaxima(TObjArray *pixArray, Int_t *localMax, Double_t *maxVal)
1499 /// Find local maxima in pixel space for large preclusters in order to
1500 /// try to split them into smaller pieces (to speed up the MLEM procedure)
1501 /// or to find additional fitting seeds if clusters were not completely resolved
1503 AliDebug(1,Form("nPix=%d",pixArray->GetLast()+1));
1506 //if (pixArray == fPixArray) hist = (TH2D*) gROOT->FindObject("anode");
1507 //else { hist = (TH2D*) gROOT->FindObject("anode1"); cout << hist << endl; }
1508 //if (hist) hist->Delete();
1510 Double_t xylim[4] = {999, 999, 999, 999};
1512 Int_t nPix = pixArray->GetEntriesFast();
1513 AliMUONPad *pixPtr = 0;
1514 for (Int_t ipix=0; ipix<nPix; ipix++) {
1515 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
1516 for (Int_t i=0; i<4; i++)
1517 xylim[i] = TMath::Min (xylim[i], (i%2 ? -1 : 1)*pixPtr->Coord(i/2));
1519 for (Int_t i=0; i<4; i++) xylim[i] -= pixPtr->Size(i/2);
1521 Int_t nx = TMath::Nint ((-xylim[1]-xylim[0])/pixPtr->Size(0)/2);
1522 Int_t ny = TMath::Nint ((-xylim[3]-xylim[2])/pixPtr->Size(1)/2);
1523 if (pixArray == fPixArray) hist = new TH2D("anode","anode",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1524 else hist = new TH2D("anode1","anode1",nx,xylim[0],-xylim[1],ny,xylim[2],-xylim[3]);
1525 for (Int_t ipix=0; ipix<nPix; ipix++) {
1526 pixPtr = (AliMUONPad*) pixArray->UncheckedAt(ipix);
1527 hist->Fill(pixPtr->Coord(0), pixPtr->Coord(1), pixPtr->Charge());
1529 // if (fDraw && pixArray == fPixArray) fDraw->DrawHist("c2", hist);
1531 Int_t nMax = 0, indx;
1532 Int_t *isLocalMax = new Int_t[ny*nx];
1533 for (Int_t i=0; i<ny*nx; i++) isLocalMax[i] = 0;
1535 for (Int_t i=1; i<=ny; i++) {
1537 for (Int_t j=1; j<=nx; j++) {
1538 if (hist->GetCellContent(j,i) < 0.5) continue;
1539 //if (isLocalMax[indx+j-1] < 0) continue;
1540 if (isLocalMax[indx+j-1] != 0) continue;
1541 FlagLocalMax(hist, i, j, isLocalMax);
1545 for (Int_t i=1; i<=ny; i++) {
1547 for (Int_t j=1; j<=nx; j++) {
1548 if (isLocalMax[indx+j-1] > 0) {
1549 localMax[nMax] = indx + j - 1;
1550 maxVal[nMax++] = hist->GetCellContent(j,i);
1551 if (nMax > 99) AliFatal(" Too many local maxima !!!");
1555 if (fDebug) cout << " Local max: " << nMax << endl;
1556 delete [] isLocalMax; isLocalMax = 0;
1560 //_____________________________________________________________________________
1561 void AliMUONClusterFinderMLEM::FlagLocalMax(TH2D *hist, Int_t i, Int_t j, Int_t *isLocalMax)
1563 /// Flag pixels (whether or not local maxima)
1565 Int_t nx = hist->GetNbinsX();
1566 Int_t ny = hist->GetNbinsY();
1567 Int_t cont = TMath::Nint (hist->GetCellContent(j,i));
1568 Int_t cont1 = 0, indx = (i-1)*nx+j-1, indx1 = 0, indx2 = 0;
1570 for (Int_t i1=i-1; i1<i+2; i1++) {
1571 if (i1 < 1 || i1 > ny) continue;
1572 indx1 = (i1 - 1) * nx;
1573 for (Int_t j1=j-1; j1<j+2; j1++) {
1574 if (j1 < 1 || j1 > nx) continue;
1575 if (i == i1 && j == j1) continue;
1576 indx2 = indx1 + j1 - 1;
1577 cont1 = TMath::Nint (hist->GetCellContent(j1,i1));
1578 if (cont < cont1) { isLocalMax[indx] = -1; return; }
1579 else if (cont > cont1) isLocalMax[indx2] = -1;
1580 else { // the same charge
1581 isLocalMax[indx] = 1;
1582 if (isLocalMax[indx2] == 0) {
1583 FlagLocalMax(hist, i1, j1, isLocalMax);
1584 if (isLocalMax[indx2] < 0) { isLocalMax[indx] = -1; return; }
1585 else isLocalMax[indx2] = -1;
1590 isLocalMax[indx] = 1; // local maximum
1593 //_____________________________________________________________________________
1594 void AliMUONClusterFinderMLEM::FindCluster(AliMUONCluster& cluster,
1595 Int_t *localMax, Int_t iMax)
1597 /// Find pixel cluster around local maximum \a iMax and pick up pads
1598 /// overlapping with it
1600 TH2D *hist = (TH2D*) gROOT->FindObject("anode");
1601 Int_t nx = hist->GetNbinsX();
1602 Int_t ny = hist->GetNbinsY();
1603 Int_t ic = localMax[iMax] / nx + 1;
1604 Int_t jc = localMax[iMax] % nx + 1;
1605 Bool_t *used = new Bool_t[ny*nx];
1606 for (Int_t i=0; i<ny*nx; i++) used[i] = kFALSE;
1608 // Drop all pixels from the array - pick up only the ones from the cluster
1609 fPixArray->Delete();
1611 Double_t wx = hist->GetXaxis()->GetBinWidth(1)/2;
1612 Double_t wy = hist->GetYaxis()->GetBinWidth(1)/2;
1613 Double_t yc = hist->GetYaxis()->GetBinCenter(ic);
1614 Double_t xc = hist->GetXaxis()->GetBinCenter(jc);
1615 Double_t cont = hist->GetCellContent(jc,ic);
1616 fPixArray->Add(new AliMUONPad (xc, yc, wx, wy, cont));
1617 used[(ic-1)*nx+jc-1] = kTRUE;
1618 fSplitter->AddBin(hist, ic, jc, 1, used, (TObjArray*)0); // recursive call
1620 Int_t nPix = fPixArray->GetEntriesFast();
1621 Int_t npad = cluster.Multiplicity();
1623 for (Int_t i=0; i<nPix; ++i)
1625 AliMUONPad* pixPtr = Pixel(i);
1626 pixPtr->SetSize(0,wx);
1627 pixPtr->SetSize(1,wy);
1630 // Pick up pads which overlap with found pixels
1631 for (Int_t i=0; i<npad; i++)
1633 cluster.Pad(i)->SetStatus(-1);
1636 for (Int_t i=0; i<nPix; i++)
1638 AliMUONPad* pixPtr = Pixel(i);
1639 for (Int_t j=0; j<npad; ++j)
1641 AliMUONPad* pad = cluster.Pad(j);
1642 if ( Overlap(*pad,*pixPtr) )
1649 delete [] used; used = 0;
1652 //_____________________________________________________________________________
1653 AliMUONClusterFinderMLEM&
1654 AliMUONClusterFinderMLEM::operator=(const AliMUONClusterFinderMLEM& rhs)
1656 /// Protected assignement operator
1658 if (this == &rhs) return *this;
1660 AliFatal("Not implemented.");
1665 //_____________________________________________________________________________
1667 AliMUONClusterFinderMLEM::Neighbours(Int_t cathode, Int_t ix, Int_t iy,
1668 Int_t& n, Int_t* xList, Int_t* yList)
1670 /// Get the list of neighbours of pad at (cathode,ix,iy)
1673 const AliMpVSegmentation* seg = fSegmentation[cathode];
1675 AliMpPad pad = seg->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1677 // Define the region to look into : a region slightly bigger
1678 // than the pad itself (5% bigger), in order to catch first neighbours.
1680 AliMpArea area(pad.Position(),pad.Dimensions()*1.05);
1682 AliMpVPadIterator* it = seg->CreateIterator(area);
1684 while ( !it->IsDone() && n < 10 )
1686 AliMpPad p = it->CurrentItem();
1687 if ( p != pad ) // skip self
1689 xList[n] = p.GetIndices().GetFirst();
1690 yList[n] = p.GetIndices().GetSecond();
1698 //_____________________________________________________________________________
1699 void AliMUONClusterFinderMLEM::AddVirtualPad(AliMUONCluster& cluster)
1701 /// Add virtual pad (with small charge) to improve fit for some
1702 /// clusters (when pad with max charge is at the extreme of the cluster)
1704 // Get number of pads in X and Y-directions
1705 Int_t nInX = -1, nInY;
1706 PadsInXandY(cluster,nInX, nInY);
1709 // Add virtual pad only if number of pads per direction == 2
1710 if (nInX != 2 && nInY != 2) return;
1714 // Find pads with max charge
1715 Int_t maxpad[2][2] = {{-1, -1}, {-1, -1}}, cath;
1716 Double_t sigmax[2] = {0}, aamax[2] = {0};
1717 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1719 AliMUONPad* pad = cluster.Pad(j);
1720 if (pad->Status() != 0) continue;
1721 cath = pad->Cathode();
1722 if (pad->Charge() > sigmax[cath])
1724 maxpad[cath][1] = maxpad[cath][0];
1725 aamax[cath] = sigmax[cath];
1726 sigmax[cath] = pad->Charge();
1727 maxpad[cath][0] = j;
1731 if (maxpad[0][0] >= 0 && maxpad[0][1] < 0 || maxpad[1][0] >= 0 && maxpad[1][1] < 0)
1733 for (Int_t j=0; j<cluster.Multiplicity(); ++j)
1735 AliMUONPad* pad = cluster.Pad(j);
1736 if (pad->Status() != 0) continue;
1737 cath = pad->Cathode();
1738 if (j == maxpad[cath][0] || j == maxpad[cath][1]) continue;
1739 if ( pad->Charge() > aamax[cath])
1741 aamax[cath] = pad->Charge();
1742 maxpad[cath][1] = j;
1747 // cout << "-------AddVirtualPad" << endl;
1748 // cout << Form("nInX %2d nInY %2d",nInX,nInY) << endl;
1750 // cluster.Print("full");
1752 // for ( Int_t i = 0; i < 2; ++i )
1754 // for ( Int_t j = 0; j < 2; ++j )
1756 // cout << Form("maxpad[%d][%d]=%d",i,j,maxpad[i][j]) << endl;
1760 // Check for mirrors (side X on cathode 0)
1761 Bool_t mirror = kFALSE;
1762 if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
1764 AliMUONPad* maxPadCath[] = { cluster.Pad(maxpad[0][0]), cluster.Pad(maxpad[1][0]) };
1765 mirror = maxPadCath[0]->DX() < maxPadCath[0]->DY();
1766 if (!mirror && TMath::Abs( maxPadCath[0]->DX() - maxPadCath[1]->DX()) < 0.001)
1768 // Special case when pads on both cathodes have the same size
1770 for (Int_t j = 0; j < cluster.Multiplicity(); ++j)
1772 AliMUONPad* pad = cluster.Pad(j);
1773 cath = pad->Cathode();
1774 if (j == maxpad[cath][0]) continue;
1775 if ( pad->Ix() != maxPadCath[cath]->Ix() ) continue;
1776 if ( TMath::Abs(pad->Iy() - maxPadCath[cath]->Iy()) == 1 )
1781 if (!yud[0]) mirror = kTRUE; // take the other cathode
1782 } // if (!mirror &&...
1783 } // if (maxpad[0][0] >= 0 && maxpad[1][0] >= 0)
1785 // // Find neughbours of pads with max charges
1786 Int_t nn, xList[10], yList[10], ix0, iy0, ix, iy, neighb;
1787 for (cath=0; cath<2; cath++)
1789 if (!cath && maxpad[0][0] < 0) continue; // one-sided cluster - cathode 1
1790 if (cath && maxpad[1][0] < 0) break; // one-sided cluster - cathode 0
1791 if (maxpad[1][0] >= 0)
1795 if (!cath && nInY != 2) continue;
1796 if (cath && nInX != 2 && (maxpad[0][0] >= 0 || nInY != 2)) continue;
1800 if (!cath && nInX != 2) continue;
1801 if (cath && nInY != 2 && (maxpad[0][0] >= 0 || nInX != 2)) continue;
1805 Int_t iAddX = 0, iAddY = 0, ix1 = 0, iy1 = 0, iPad = 0;
1806 if (maxpad[0][0] < 0) iPad = 1;
1808 for (iPad=0; iPad<2; iPad++)
1810 if (maxpad[cath][iPad] < 0) continue;
1811 if (iPad && !iAddX && !iAddY) break;
1812 if (iPad && cluster.Pad(maxpad[cath][1])->Charge() / sigmax[cath] < 0.5) break;
1814 Int_t neighbx = 0, neighby = 0;
1815 ix0 = cluster.Pad(maxpad[cath][iPad])->Ix();
1816 iy0 = cluster.Pad(maxpad[cath][iPad])->Iy();
1817 Neighbours(cath,ix0,iy0,nn,xList,yList);
1819 for (Int_t j=0; j<nn; j++) {
1820 if (TMath::Abs(xList[j]-ix0) == 1 || xList[j]*ix0 == -1) neighbx++;
1821 if (TMath::Abs(yList[j]-iy0) == 1 || yList[j]*iy0 == -1) neighby++;
1824 if (cath) neighb = neighbx;
1825 else neighb = neighby;
1826 if (maxpad[0][0] < 0) neighb += neighby;
1827 else if (maxpad[1][0] < 0) neighb += neighbx;
1829 if (!cath) neighb = neighbx;
1830 else neighb = neighby;
1831 if (maxpad[0][0] < 0) neighb += neighbx;
1832 else if (maxpad[1][0] < 0) neighb += neighby;
1835 for (Int_t j=0; j< cluster.Multiplicity(); ++j)
1837 AliMUONPad* pad = cluster.Pad(j);
1838 if ( pad->Cathode() != cath) continue;
1841 if (iy == iy0 && ix == ix0) continue;
1842 for (Int_t k=0; k<nn; ++k)
1844 if (xList[k] != ix || yList[k] != iy) continue;
1847 if ((!cath || maxpad[0][0] < 0) &&
1848 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
1849 if (!iPad && TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) ix1 = xList[k]; //19-12-05
1850 xList[k] = yList[k] = 0;
1854 if ((cath || maxpad[1][0] < 0) &&
1855 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
1856 if (!iPad) ix1 = xList[k]; //19-12-05
1857 xList[k] = yList[k] = 0;
1861 if ((!cath || maxpad[0][0] < 0) &&
1862 (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1)) {
1863 if (!iPad) ix1 = xList[k]; //19-12-05
1864 xList[k] = yList[k] = 0;
1868 if ((cath || maxpad[1][0] < 0) &&
1869 (TMath::Abs(iy-iy0) == 1 || iy*iy0 == -1)) {
1870 xList[k] = yList[k] = 0;
1875 } // for (Int_t k=0; k<nn;
1877 } // for (Int_t j=0; j< cluster.Multiplicity();
1878 if (!neighb) continue;
1883 for (Int_t j=0; j<nn; j++)
1885 if (xList[j] == 0 && yList[j] == 0) continue;
1886 // npads = fnPads[0] + fnPads[1];
1887 // fPadIJ[0][npads] = cath;
1888 // fPadIJ[1][npads] = 0;
1891 if (TMath::Abs(ix-ix0) == 1 || ix*ix0 == -1) {
1892 if (iy != iy0) continue; // new segmentation - check
1893 if (nInX != 2) continue; // new
1895 if (!cath && maxpad[1][0] >= 0) continue;
1897 if (cath && maxpad[0][0] >= 0) continue;
1899 if (iPad && !iAddX) continue;
1900 AliMpPad pad = fSegmentation[cath]->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1901 // fXyq[0][npads] = pad.Position().X();
1902 // fXyq[1][npads] = pad.Position().Y();
1903 AliMUONPad muonPad(fDetElemId, cath, ix, iy, pad.Position().X(), pad.Position().Y(),
1904 pad.Dimensions().X(), pad.Dimensions().Y(), 0);
1905 // fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
1906 // if (fXyq[0][npads] > 1.e+5) continue; // temporary fix
1907 if (muonPad.Coord(0) > 1.e+5) continue; // temporary fix
1908 if (ix == ix1) continue; //19-12-05
1909 if (ix1 == ix0) continue;
1910 if (maxpad[1][0] < 0 || mirror && maxpad[0][0] >= 0) {
1911 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/100, 5.);
1912 // else fXyq[2][npads] = TMath::Min (aamax[0]/100, 5.);
1913 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[0]/100, 5.));
1914 else muonPad.SetCharge(TMath::Min (aamax[0]/100, 5.));
1917 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/100, 5.);
1918 // else fXyq[2][npads] = TMath::Min (aamax[1]/100, 5.);
1919 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[1]/100, 5.));
1920 else muonPad.SetCharge(TMath::Min (aamax[1]/100, 5.));
1922 // fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
1923 if (muonPad.Charge() < 1.) muonPad.SetCharge(1.);
1924 // fXyq[3][npads] = -2; // flag
1925 // fPadIJ[2][npads] = ix;
1926 // fPadIJ[3][npads] = iy;
1927 muonPad.SetReal(kFALSE);
1931 if (fDebug) printf(" ***** Add virtual pad in X ***** %f %f %f %3d %3d %f %f \n",
1932 muonPad.Charge(), muonPad.Coord(0), muonPad.Coord(1), ix, iy,
1933 muonPad.DX(), muonPad.DY());
1934 cluster.AddPad(muonPad); // add pad to the cluster
1938 if (nInY != 2) continue;
1939 if (!mirror && cath && maxpad[0][0] >= 0) continue;
1940 if (mirror && !cath && maxpad[1][0] >= 0) continue;
1941 if (TMath::Abs(iy-iy0) == 1 || TMath::Abs(iy*iy0) == 1) {
1942 if (ix != ix0) continue; // new segmentation - check
1943 if (iPad && !iAddY) continue;
1944 AliMpPad pad = fSegmentation[cath]->PadByIndices(AliMpIntPair(ix,iy),kTRUE);
1945 // fXyq[0][npads] = pad.Position().X();
1946 // fXyq[1][npads] = pad.Position().Y();
1947 // fSegmentation[cath]->GetPadC(ix, iy, fXyq[0][npads], fXyq[1][npads], zpad);
1948 AliMUONPad muonPad(fDetElemId, cath, ix, iy, pad.Position().X(), pad.Position().Y(),
1949 pad.Dimensions().X(), pad.Dimensions().Y(), 0);
1950 if (iy1 == iy0) continue;
1951 //if (iPad && iy1 == iy0) continue;
1952 if (maxpad[0][0] < 0 || mirror && maxpad[1][0] >= 0) {
1953 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[1]/15, fgkZeroSuppression);
1954 // else fXyq[2][npads] = TMath::Min (aamax[1]/15, fgkZeroSuppression);
1955 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[1]/15, fgkZeroSuppression));
1956 else muonPad.SetCharge(TMath::Min (aamax[1]/15, fgkZeroSuppression));
1959 // if (!iPad) fXyq[2][npads] = TMath::Min (sigmax[0]/15, fgkZeroSuppression);
1960 // else fXyq[2][npads] = TMath::Min (aamax[0]/15, fgkZeroSuppression);
1961 if (!iPad) muonPad.SetCharge(TMath::Min (sigmax[0]/15, fgkZeroSuppression));
1962 else muonPad.SetCharge(TMath::Min (aamax[0]/15, fgkZeroSuppression));
1964 // fXyq[2][npads] = TMath::Max (fXyq[2][npads], (float)1);
1965 if (muonPad.Charge() < 1.) muonPad.SetCharge(1.);
1966 // fXyq[3][npads] = -2; // flag
1967 // fPadIJ[2][npads] = ix;
1968 // fPadIJ[3][npads] = iy;
1969 muonPad.SetReal(kFALSE);
1973 if (fDebug) printf(" ***** Add virtual pad in Y ***** %f %f %f %3d %3d %f %f \n",
1974 muonPad.Charge(), muonPad.Coord(0), muonPad.Coord(1), ix, iy,
1975 muonPad.DX(), muonPad.DY());
1976 cluster.AddPad(muonPad); // add pad to the cluster
1979 } // for (Int_t j=0; j<nn;
1980 } // for (Int_t iPad=0;
1981 } // for (cath=0; cath<2;
1984 //_____________________________________________________________________________
1985 void AliMUONClusterFinderMLEM::PadsInXandY(AliMUONCluster& cluster,
1986 Int_t &nInX, Int_t &nInY) const
1988 /// Find number of pads in X and Y-directions (excluding virtual ones and
1991 Int_t statusToTest = 1;
1993 if ( nInX < 0 ) statusToTest = 0;
1995 Bool_t mustMatch(kTRUE);
1997 AliMpIntPair cn = cluster.NofPads(statusToTest,mustMatch);
1999 nInX = cn.GetFirst();
2000 nInY = cn.GetSecond();
2003 //_____________________________________________________________________________
2004 void AliMUONClusterFinderMLEM::RemovePixel(Int_t i)
2006 /// Remove pixel at index i
2007 AliMUONPad* pixPtr = Pixel(i);
2008 fPixArray->RemoveAt(i);
2012 //_____________________________________________________________________________
2013 void AliMUONClusterFinderMLEM::Simple(AliMUONCluster& cluster)
2015 /// Process simple cluster (small number of pads) without EM-procedure
2017 Int_t nForFit = 1, clustFit[1] = {0}, nfit;
2018 Double_t parOk[3] = {0.};
2019 TObjArray *clusters[1];
2020 clusters[0] = fPixArray;
2022 AliDebug(1,Form("nPix=%d",fPixArray->GetLast()+1));
2024 for (Int_t i = 0; i < cluster.Multiplicity(); ++i)
2026 AliMUONPad* pad = cluster.Pad(i);
2027 if ( pad->IsSaturated())
2036 nfit = fSplitter->Fit(cluster,1, nForFit, clustFit, clusters, parOk, fClusterList);
2039 //_____________________________________________________________________________
2041 AliMUONClusterFinderMLEM::Pixel(Int_t i) const
2043 /// Returns pixel at index i
2044 return static_cast<AliMUONPad*>(fPixArray->UncheckedAt(i));
2047 //_____________________________________________________________________________
2049 AliMUONClusterFinderMLEM::Print(Option_t* what) const
2052 TString swhat(what);
2054 if ( swhat.Contains("precluster") )
2056 if ( fPreCluster) fPreCluster->Print();