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 #include <Riostream.h>
20 #include <TObjArray.h>
21 #include <TVirtualPad.h>
22 #include <TVirtualX.h>
24 #include "AliMUONCluster.h"
25 #include "AliMUONPad.h"
27 #include "AliMpEncodePair.h"
31 //-----------------------------------------------------------------------------
32 /// \class AliMUONCluster
34 /// A group of adjacent pads
36 /// Besides holding an internal array of AliMUONPads, this object
37 /// also computes some global characteristics for that pad sets.
39 /// \author Laurent Aphecetche
41 //-----------------------------------------------------------------------------
44 ClassImp(AliMUONCluster)
49 //___________________________________________________________________________
51 ShouldUsePad(const AliMUONPad& pad,
52 Int_t cathode, Int_t statusMask, Bool_t matchMask)
55 // FIXME : we should only use >=0 status, so we can fully
56 // use masking possibility ?
57 if ( pad.Status() < 0 ) return kFALSE;
59 if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
61 Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
64 test = ( pad.Status() == 0 );
66 if ( ( test && matchMask ) || ( !test && !matchMask ) )
74 //___________________________________________________________________________
75 Int_t Unique(Int_t n, Double_t* array, Double_t precision)
77 /// Return the number of *different* elements in array
78 /// where different is up to precision
79 /// Note that we assume that n is >= 1
83 Int_t* index = new Int_t[n];
85 TMath::Sort(n,array,index);
87 for ( Int_t i = 1; i < n; ++i )
89 if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
98 //_____________________________________________________________________________
99 AliMUONCluster::AliMUONCluster()
102 fHasPosition(kFALSE),
104 fPositionError(1E9,1E9),
109 fMultiplicity[0]=fMultiplicity[1]=0;
110 fRawCharge[0]=fRawCharge[1]=0;
111 fCharge[0]=fCharge[1]=0;
112 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
113 fPads.SetOwner(kTRUE);
116 //_____________________________________________________________________________
117 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
120 fHasPosition(kFALSE),
122 fPositionError(1E9,1E9),
127 fPads.SetOwner(kTRUE);
131 //_____________________________________________________________________________
133 AliMUONCluster::operator=(const AliMUONCluster& src)
135 /// assignement operator
143 //_____________________________________________________________________________
144 AliMUONCluster::~AliMUONCluster()
146 /// dtor : note that we're owner of our pads
150 //_____________________________________________________________________________
152 AliMUONCluster::Clear(Option_t*)
154 /// Clear our pad array
159 //_____________________________________________________________________________
161 AliMUONCluster::Contains(const AliMUONPad& pad) const
163 /// Whether this cluster contains the pad
164 if (fPads.IsEmpty()) return kFALSE;
166 for ( Int_t i = 0; i < Multiplicity(); ++i )
168 AliMUONPad* p = Pad(i);
169 if ( pad.Compare(p) == 0 ) return kTRUE;
174 //_____________________________________________________________________________
176 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
178 /// Add all the pads for cluster to this one
179 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
181 AliMUONPad* p = cluster.Pad(i);
184 AliError("I already got this pad : ");
185 StdoutToAliError(p->Print(););
193 //_____________________________________________________________________________
195 AliMUONCluster::AddPad(const AliMUONPad& pad)
197 /// Add a pad to our pad array, and update some internal information
200 AliMUONPad* p = new AliMUONPad(pad);
202 p->SetClusterId(GetUniqueID());
203 Int_t cathode = p->Cathode();
204 ++(fMultiplicity[cathode]);
205 fRawCharge[cathode] += p->Charge();
206 if ( p->IsSaturated() )
208 fIsSaturated[p->Cathode()]=kTRUE;
213 //___________________________________________________________________________
215 AliMUONCluster::AsString() const
217 /// Return a string containing a compact form of the pad list
218 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
220 for (Int_t i = 0; i < Multiplicity(); ++i )
222 AliMUONPad* p = Pad(i);
223 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
229 //___________________________________________________________________________
231 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
233 /// Whether the two clusters overlap
235 static Double_t precision = 1E-4; // cm
236 static TVector2 precisionAdjustment(precision,precision);
238 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
240 AliMUONPad* p1 = c1.Pad(i1);
242 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
244 AliMUONPad* p2 = c2.Pad(i2);
245 // Note: we use negative precision numbers, meaning
246 // the area of the pads will be *increased* by these small numbers
247 // prior to check the overlap by the AreOverlapping method,
248 // so pads touching only by the corners will be considered as
250 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
259 //_____________________________________________________________________________
261 AliMUONCluster::Area() const
263 /// Return the geometrical area covered by this cluster
265 // Start by finding the (x,y) limits of this cluster
266 TVector2 lowerLeft(1E9,1E9);
267 TVector2 upperRight(-1E9,-1E9);
269 for ( Int_t i = 0; i < Multiplicity(); ++i )
271 AliMUONPad* pad = Pad(i);
272 TVector2 ll = pad->Position() - pad->Dimensions();
273 TVector2 ur = pad->Position() + pad->Dimensions();
274 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
275 TMath::Min(ll.Y(),lowerLeft.Y()) );
276 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
277 TMath::Max(ur.Y(),upperRight.Y()) );
280 // then construct the area from those limits
281 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
282 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
285 //_____________________________________________________________________________
287 AliMUONCluster::Area(Int_t cathode) const
289 /// Return the geometrical area covered by this cluster's pads on
292 // Start by finding the (x,y) limits of this cluster
293 TVector2 lowerLeft(1E9,1E9);
294 TVector2 upperRight(-1E9,-1E9);
296 for ( Int_t i = 0; i < Multiplicity(); ++i )
298 AliMUONPad* pad = Pad(i);
299 if ( pad->Cathode() == cathode )
301 TVector2 ll = pad->Position() - pad->Dimensions();
302 TVector2 ur = pad->Position() + pad->Dimensions();
303 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
304 TMath::Min(ll.Y(),lowerLeft.Y()) );
305 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
306 TMath::Max(ur.Y(),upperRight.Y()) );
310 // then construct the area from those limits
311 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
312 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
315 //_____________________________________________________________________________
317 AliMUONCluster::Cathode() const
319 /// Return the cathode "number" of this cluster :
320 /// 0 if all its pads are on cathode 0
321 /// 1 if all its pads are on cathode 1
322 /// 2 if some pads on cath 0 and some on cath 1
325 if (Multiplicity(0)>0 && Multiplicity(1)>0)
329 else if (Multiplicity(0)>0)
333 else if (Multiplicity(1)>0)
341 //_____________________________________________________________________________
343 AliMUONCluster::Copy(TObject& obj) const
346 /// Copy this cluster to (cluster&)obj
349 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
351 // dest.fPads.Delete();
354 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
356 AliMUONPad* p = static_cast<AliMUONPad*>(fPads.UncheckedAt(i));
357 dest.fPads.AddLast(new AliMUONPad(*p));
359 dest.fHasPosition = fHasPosition;
360 dest.fPosition = fPosition;
361 dest.fPositionError = fPositionError;
362 dest.fHasCharge = fHasCharge;
364 for ( Int_t i = 0; i < 2; ++i )
366 dest.fRawCharge[i] = fRawCharge[i];
367 dest.fCharge[i] = fCharge[i];
368 dest.fMultiplicity[i] = fMultiplicity[i];
369 dest.fIsSaturated[i] = fIsSaturated[i];
373 //_____________________________________________________________________________
375 AliMUONCluster::Charge() const
377 /// Return the average charge over both cathodes
379 if ( Multiplicity(0) && Multiplicity(1) )
381 return (Charge(0)+Charge(1))/2.0;
383 else if ( Multiplicity(0) )
387 else if ( Multiplicity(1) )
391 AliError("Should not be here ?!");
395 //_____________________________________________________________________________
397 AliMUONCluster::Charge(Int_t cathode) const
399 /// Returns the charge of a given cathode
400 if ( !fHasCharge ) return RawCharge(cathode);
402 if ( cathode == 0 || cathode == 1 )
404 return fCharge[cathode];
409 //_____________________________________________________________________________
411 AliMUONCluster::ChargeAsymmetry() const
413 /// Returns the charge asymmetry
416 return TMath::Abs(Charge(0)-Charge(1))/Charge();
421 //_____________________________________________________________________________
423 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
425 /// Returns the maximum pad dimensions (half sizes), only considering
426 /// pads matching (or not, depending matchMask) a given mask
428 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
429 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
431 return TVector2( TMath::Max(cath0.X(),cath1.X()),
432 TMath::Max(cath0.Y(),cath1.Y()) );
435 //_____________________________________________________________________________
437 AliMUONCluster::MaxPadDimensions(Int_t cathode,
438 Int_t statusMask, Bool_t matchMask) const
440 /// Returns the maximum pad dimensions (half sizes), only considering
441 /// pads matching (or not, depending matchMask) a given mask, within a
447 for ( Int_t i = 0; i < Multiplicity(); ++i )
449 AliMUONPad* pad = Pad(i);
450 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
452 xmax = TMath::Max(xmax,pad->DX());
453 ymax = TMath::Max(ymax,pad->DY());
456 return TVector2(xmax,ymax);
459 //_____________________________________________________________________________
461 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
463 /// Returns the minimum pad dimensions (half sizes), only considering
464 /// pads matching (or not, depending matchMask) a given mask
466 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
467 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
469 return TVector2( TMath::Min(cath0.X(),cath1.X()),
470 TMath::Min(cath0.Y(),cath1.Y()) );
473 //_____________________________________________________________________________
475 AliMUONCluster::MinPadDimensions(Int_t cathode,
476 Int_t statusMask, Bool_t matchMask) const
478 /// Returns the minimum pad dimensions (half sizes), only considering
479 /// pads matching (or not, depending matchMask) a given mask, within a
485 for ( Int_t i = 0; i < Multiplicity(); ++i )
487 AliMUONPad* pad = Pad(i);
488 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
490 xmin = TMath::Min(xmin,pad->DX());
491 ymin = TMath::Min(ymin,pad->DY());
494 return TVector2(xmin,ymin);
497 //_____________________________________________________________________________
499 AliMUONCluster::Multiplicity() const
501 /// Returns the total number of pads in this cluster
502 return Multiplicity(0)+Multiplicity(1);
505 //_____________________________________________________________________________
507 AliMUONCluster::Multiplicity(Int_t cathode) const
509 /// Returns the number of pads in this cluster, in the given cathode
510 if ( cathode == 0 || cathode == 1 )
512 return fMultiplicity[cathode];
517 //_____________________________________________________________________________
519 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
521 /// Number of pads satisfying (or not, depending matchMask) a
526 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
527 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
529 Long_t npad0(NofPads(0,statusMask,matchMask));
530 Long_t npad1(NofPads(1,statusMask,matchMask));
532 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
534 nx = TMath::Max( AliMp::PairFirst(npad0), AliMp::PairFirst(npad1) );
538 nx = dim0.X() < dim1.X() ? AliMp::PairFirst(npad0) : AliMp::PairFirst(npad1);
541 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
543 ny = TMath::Max( AliMp::PairSecond(npad0), AliMp::PairSecond(npad1) );
547 ny = dim0.Y() < dim1.Y() ? AliMp::PairSecond(npad0) : AliMp::PairSecond(npad1);
550 return AliMp::Pair(nx,ny);
553 //_____________________________________________________________________________
555 AliMUONCluster::NofPads(Int_t cathode,
556 Int_t statusMask, Bool_t matchMask) const
558 /// Number of pads of a given cathode, satisfying (or not,
559 /// depending matchMask) a given mask
561 Int_t n = Multiplicity(cathode);
566 Double_t* x = new Double_t[n];
567 Double_t* y = new Double_t[n];
570 for ( Int_t i = 0; i < Multiplicity(); ++i )
572 AliMUONPad* pad = Pad(i);
573 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
581 Int_t cx = Unique(np,x,0.01);
582 Int_t cy = Unique(np,y,0.01);
587 return AliMp::Pair(cx,cy);
590 //_____________________________________________________________________________
592 AliMUONCluster::Pad(Int_t index) const
594 /// Returns the index-th pad
596 if (fPads.IsEmpty()) return 0x0;
597 if ( index < fPads.GetLast()+1 )
599 return static_cast<AliMUONPad*>(fPads.At(index));
603 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
604 fPads.GetLast(),Multiplicity()));
611 //_____________________________________________________________________________
613 AliMUONCluster::Paint(Option_t*)
615 /// Paint this cluster
616 if (!Multiplicity()) return;
618 AliMpArea area(Area());
620 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
622 gVirtualX->SetFillStyle(0);
624 gVirtualX->SetLineColor(2);
625 gVirtualX->SetLineWidth(4);
626 for ( Int_t i = 0; i < Multiplicity(); ++i)
628 AliMUONPad* pad = Pad(i);
629 if ( pad->Cathode() == 0 ) pad->Paint();
632 gVirtualX->SetLineColor(4);
633 gVirtualX->SetLineWidth(2);
634 for ( Int_t i = 0; i < Multiplicity(); ++i)
636 AliMUONPad* pad = Pad(i);
637 if ( pad->Cathode() == 1 ) pad->Paint();
642 //_____________________________________________________________________________
644 AliMUONCluster::DumpMe() const
647 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
648 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
649 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
650 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
653 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
654 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
657 // cout << " " << Area() << endl;
658 for (Int_t i = 0; i < fPads.GetSize(); ++i)
660 cout << Form("fPads[%d]=%x",i,fPads.At(i)) << endl;
661 if ( fPads.At(i) ) fPads.At(i)->Print();
666 //_____________________________________________________________________________
668 AliMUONCluster::Print(Option_t* opt) const
671 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
672 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
673 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
674 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
677 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
678 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
680 cout << " " << Area();
682 TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
688 //_____________________________________________________________________________
690 //AliMUONCluster::IsEqual(const TObject* obj) const
692 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
693 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
695 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
697 // AliMUONPad* p = c->Pad(i);
698 // if ( p->Compare(Pad(i)) ) return kFALSE;
703 //_____________________________________________________________________________
705 AliMUONCluster::Compare(const TObject* obj) const
707 /// Compare two clusters. Comparison is made on position and rawcharge only.
709 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
711 AliMpArea carea(cluster->Area());
712 AliMpArea area(Area());
714 if ( carea.GetPositionX() > area.GetPositionX() )
718 else if ( carea.GetPositionX() < area.GetPositionX() )
724 if ( carea.GetPositionY() > area.GetPositionY() )
728 else if ( carea.GetPositionY() < area.GetPositionY() )
734 if ( cluster->RawCharge() > RawCharge() )
738 else if ( cluster->RawCharge() < RawCharge() )
747 //_____________________________________________________________________________
749 AliMUONCluster::RemovePad(AliMUONPad* pad)
752 /// As a consequence, some internal information must be updated
757 // update cluster's data
758 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
759 fMultiplicity[0]=fMultiplicity[1]=0;
760 fRawCharge[0]=fRawCharge[1]=0;
761 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
763 AliMUONPad* p = Pad(i);
764 if ( p->IsSaturated() )
766 fIsSaturated[p->Cathode()] = kTRUE;
768 ++fMultiplicity[p->Cathode()];
769 fRawCharge[p->Cathode()] += p->Charge();
773 //_____________________________________________________________________________
775 AliMUONCluster::RawCharge() const
777 /// Returns the raw average charge
778 return (RawCharge(0)+RawCharge(1))/2.0;
781 //_____________________________________________________________________________
783 AliMUONCluster::RawCharge(Int_t cathode) const
785 /// Returns the average charge of a given cathode
786 if ( cathode == 0 || cathode == 1 )
788 return fRawCharge[cathode];
793 //_____________________________________________________________________________
795 AliMUONCluster::RawChargeAsymmetry() const
797 /// Returns the raw charge asymmetry
798 if ( RawCharge() > 0 )
800 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();