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"
27 #include "AliMUONPad.h"
29 //-----------------------------------------------------------------------------
30 /// \class AliMUONCluster
32 /// A group of adjacent pads
34 /// Besides holding an internal array of AliMUONPads, this object
35 /// also computes some global characteristics for that pad sets.
37 /// \author Laurent Aphecetche
39 //-----------------------------------------------------------------------------
42 ClassImp(AliMUONCluster)
47 //___________________________________________________________________________
49 ShouldUsePad(const AliMUONPad& pad,
50 Int_t cathode, Int_t statusMask, Bool_t matchMask)
53 // FIXME : we should only use >=0 status, so we can fully
54 // use masking possibility ?
55 if ( pad.Status() < 0 ) return kFALSE;
57 if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
59 Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
62 test = ( pad.Status() == 0 );
64 if ( ( test && matchMask ) || ( !test && !matchMask ) )
72 //___________________________________________________________________________
73 Int_t Unique(Int_t n, Double_t* array, Double_t precision)
75 /// Return the number of *different* elements in array
76 /// where different is up to precision
77 /// Note that we assume that n is >= 1
81 Int_t* index = new Int_t[n];
83 TMath::Sort(n,array,index);
85 for ( Int_t i = 1; i < n; ++i )
87 if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
96 //_____________________________________________________________________________
97 AliMUONCluster::AliMUONCluster()
100 fHasPosition(kFALSE),
102 fPositionError(1E9,1E9),
107 fMultiplicity[0]=fMultiplicity[1]=0;
108 fRawCharge[0]=fRawCharge[1]=0;
109 fCharge[0]=fCharge[1]=0;
110 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
111 fPads.SetOwner(kTRUE);
114 //_____________________________________________________________________________
115 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
118 fHasPosition(kFALSE),
120 fPositionError(1E9,1E9),
125 fPads.SetOwner(kTRUE);
129 //_____________________________________________________________________________
131 AliMUONCluster::operator=(const AliMUONCluster& src)
133 /// assignement operator
141 //_____________________________________________________________________________
142 AliMUONCluster::~AliMUONCluster()
144 /// dtor : note that we're owner of our pads
147 //_____________________________________________________________________________
149 AliMUONCluster::Clear(Option_t*)
151 /// Clear our pad array
155 //_____________________________________________________________________________
157 AliMUONCluster::Contains(const AliMUONPad& pad) const
159 /// Whether this cluster contains the pad
160 if (fPads.IsEmpty()) return kFALSE;
162 for ( Int_t i = 0; i < Multiplicity(); ++i )
164 AliMUONPad* p = Pad(i);
165 if ( pad.Compare(p) == 0 ) return kTRUE;
170 //_____________________________________________________________________________
172 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
174 /// Add all the pads for cluster to this one
175 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
177 AliMUONPad* p = cluster.Pad(i);
180 AliError("I already got this pad : ");
181 StdoutToAliError(p->Print(););
189 //_____________________________________________________________________________
191 AliMUONCluster::AddPad(const AliMUONPad& pad)
193 /// Add a pad to our pad array, and update some internal information
196 AliMUONPad* p = new AliMUONPad(pad);
198 p->SetClusterId(GetUniqueID());
199 Int_t cathode = p->Cathode();
200 ++(fMultiplicity[cathode]);
201 fRawCharge[cathode] += p->Charge();
202 if ( p->IsSaturated() )
204 fIsSaturated[p->Cathode()]=kTRUE;
209 //___________________________________________________________________________
211 AliMUONCluster::AsString() const
213 /// Return a string containing a compact form of the pad list
214 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
216 for (Int_t i = 0; i < Multiplicity(); ++i )
218 AliMUONPad* p = Pad(i);
219 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
225 //___________________________________________________________________________
227 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
229 /// Whether the two clusters overlap
231 static Double_t precision = 1E-4; // cm
232 static TVector2 precisionAdjustment(precision,precision);
234 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
236 AliMUONPad* p1 = c1.Pad(i1);
238 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
240 AliMUONPad* p2 = c2.Pad(i2);
241 // Note: we use negative precision numbers, meaning
242 // the area of the pads will be *increased* by these small numbers
243 // prior to check the overlap by the AreOverlapping method,
244 // so pads touching only by the corners will be considered as
246 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
255 //_____________________________________________________________________________
257 AliMUONCluster::Area() const
259 /// Return the geometrical area covered by this cluster
261 // Start by finding the (x,y) limits of this cluster
262 TVector2 lowerLeft(1E9,1E9);
263 TVector2 upperRight(-1E9,-1E9);
265 for ( Int_t i = 0; i < Multiplicity(); ++i )
267 AliMUONPad* pad = Pad(i);
268 TVector2 ll = pad->Position() - pad->Dimensions();
269 TVector2 ur = pad->Position() + pad->Dimensions();
270 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
271 TMath::Min(ll.Y(),lowerLeft.Y()) );
272 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
273 TMath::Max(ur.Y(),upperRight.Y()) );
276 // then construct the area from those limits
277 return AliMpArea((lowerLeft+upperRight)/2,
278 (upperRight-lowerLeft)/2);
281 //_____________________________________________________________________________
283 AliMUONCluster::Area(Int_t cathode) const
285 /// Return the geometrical area covered by this cluster's pads on
288 // Start by finding the (x,y) limits of this cluster
289 TVector2 lowerLeft(1E9,1E9);
290 TVector2 upperRight(-1E9,-1E9);
292 for ( Int_t i = 0; i < Multiplicity(); ++i )
294 AliMUONPad* pad = Pad(i);
295 if ( pad->Cathode() == cathode )
297 TVector2 ll = pad->Position() - pad->Dimensions();
298 TVector2 ur = pad->Position() + pad->Dimensions();
299 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
300 TMath::Min(ll.Y(),lowerLeft.Y()) );
301 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
302 TMath::Max(ur.Y(),upperRight.Y()) );
306 // then construct the area from those limits
307 return AliMpArea((lowerLeft+upperRight)/2,
308 (upperRight-lowerLeft)/2);
311 //_____________________________________________________________________________
313 AliMUONCluster::Cathode() const
315 /// Return the cathode "number" of this cluster :
316 /// 0 if all its pads are on cathode 0
317 /// 1 if all its pads are on cathode 1
318 /// 2 if some pads on cath 0 and some on cath 1
321 if (Multiplicity(0)>0 && Multiplicity(1)>0)
325 else if (Multiplicity(0)>0)
329 else if (Multiplicity(1)>0)
337 //_____________________________________________________________________________
339 AliMUONCluster::Copy(TObject& obj) const
342 /// Copy this cluster to (cluster&)obj
345 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
348 dest.fHasPosition = fHasPosition;
349 dest.fPosition = fPosition;
350 dest.fPositionError = fPositionError;
351 dest.fHasCharge = fHasCharge;
353 for ( Int_t i = 0; i < 2; ++i )
355 dest.fRawCharge[i] = fRawCharge[i];
356 dest.fCharge[i] = fCharge[i];
357 dest.fMultiplicity[i] = fMultiplicity[i];
358 dest.fIsSaturated[i] = fIsSaturated[i];
362 //_____________________________________________________________________________
364 AliMUONCluster::Charge() const
366 /// Return the average charge over both cathodes
368 if ( Multiplicity(0) && Multiplicity(1) )
370 return (Charge(0)+Charge(1))/2.0;
372 else if ( Multiplicity(0) )
376 else if ( Multiplicity(1) )
380 AliError("Should not be here ?!");
384 //_____________________________________________________________________________
386 AliMUONCluster::Charge(Int_t cathode) const
388 /// Returns the charge of a given cathode
389 if ( !fHasCharge ) return RawCharge(cathode);
391 if ( cathode == 0 || cathode == 1 )
393 return fCharge[cathode];
398 //_____________________________________________________________________________
400 AliMUONCluster::ChargeAsymmetry() const
402 /// Returns the charge asymmetry
405 return TMath::Abs(Charge(0)-Charge(1))/Charge();
410 //_____________________________________________________________________________
412 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
414 /// Returns the maximum pad dimensions (half sizes), only considering
415 /// pads matching (or not, depending matchMask) a given mask
417 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
418 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
420 return TVector2( TMath::Max(cath0.X(),cath1.X()),
421 TMath::Max(cath0.Y(),cath1.Y()) );
424 //_____________________________________________________________________________
426 AliMUONCluster::MaxPadDimensions(Int_t cathode,
427 Int_t statusMask, Bool_t matchMask) const
429 /// Returns the maximum pad dimensions (half sizes), only considering
430 /// pads matching (or not, depending matchMask) a given mask, within a
436 for ( Int_t i = 0; i < Multiplicity(); ++i )
438 AliMUONPad* pad = Pad(i);
439 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
441 xmax = TMath::Max(xmax,pad->DX());
442 ymax = TMath::Max(ymax,pad->DY());
445 return TVector2(xmax,ymax);
448 //_____________________________________________________________________________
450 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
452 /// Returns the minimum pad dimensions (half sizes), only considering
453 /// pads matching (or not, depending matchMask) a given mask
455 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
456 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
458 return TVector2( TMath::Min(cath0.X(),cath1.X()),
459 TMath::Min(cath0.Y(),cath1.Y()) );
462 //_____________________________________________________________________________
464 AliMUONCluster::MinPadDimensions(Int_t cathode,
465 Int_t statusMask, Bool_t matchMask) const
467 /// Returns the minimum pad dimensions (half sizes), only considering
468 /// pads matching (or not, depending matchMask) a given mask, within a
474 for ( Int_t i = 0; i < Multiplicity(); ++i )
476 AliMUONPad* pad = Pad(i);
477 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
479 xmin = TMath::Min(xmin,pad->DX());
480 ymin = TMath::Min(ymin,pad->DY());
483 return TVector2(xmin,ymin);
486 //_____________________________________________________________________________
488 AliMUONCluster::Multiplicity() const
490 /// Returns the total number of pads in this cluster
491 return Multiplicity(0)+Multiplicity(1);
494 //_____________________________________________________________________________
496 AliMUONCluster::Multiplicity(Int_t cathode) const
498 /// Returns the number of pads in this cluster, in the given cathode
499 if ( cathode == 0 || cathode == 1 )
501 return fMultiplicity[cathode];
506 //_____________________________________________________________________________
508 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
510 /// Number of pads satisfying (or not, depending matchMask) a
515 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
516 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
518 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
519 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
521 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
523 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
527 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
530 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
532 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
536 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
539 return AliMpIntPair(nx,ny);
542 //_____________________________________________________________________________
544 AliMUONCluster::NofPads(Int_t cathode,
545 Int_t statusMask, Bool_t matchMask) const
547 /// Number of pads of a given cathode, satisfying (or not,
548 /// depending matchMask) a given mask
550 Int_t n = Multiplicity(cathode);
553 return AliMpIntPair(0,0);
555 Double_t* x = new Double_t[n];
556 Double_t* y = new Double_t[n];
559 for ( Int_t i = 0; i < Multiplicity(); ++i )
561 AliMUONPad* pad = Pad(i);
562 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
570 Int_t cx = Unique(np,x,0.01);
571 Int_t cy = Unique(np,y,0.01);
576 return AliMpIntPair(cx,cy);
579 //_____________________________________________________________________________
581 AliMUONCluster::Pad(Int_t index) const
583 /// Returns the index-th pad
585 if (fPads.IsEmpty()) return 0x0;
586 if ( index < fPads.GetLast()+1 )
588 return static_cast<AliMUONPad*>(fPads.At(index));
592 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
593 fPads.GetLast(),Multiplicity()));
600 //_____________________________________________________________________________
602 AliMUONCluster::Paint(Option_t*)
604 /// Paint this cluster
605 if (!Multiplicity()) return;
607 AliMpArea area(Area());
609 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
611 gVirtualX->SetFillStyle(0);
613 gVirtualX->SetLineColor(2);
614 gVirtualX->SetLineWidth(4);
615 for ( Int_t i = 0; i < Multiplicity(); ++i)
617 AliMUONPad* pad = Pad(i);
618 if ( pad->Cathode() == 0 ) pad->Paint();
621 gVirtualX->SetLineColor(4);
622 gVirtualX->SetLineWidth(2);
623 for ( Int_t i = 0; i < Multiplicity(); ++i)
625 AliMUONPad* pad = Pad(i);
626 if ( pad->Cathode() == 1 ) pad->Paint();
631 //_____________________________________________________________________________
633 AliMUONCluster::DumpMe() const
636 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
637 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
638 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
639 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
642 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
643 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
646 // cout << " " << Area() << endl;
647 for (Int_t i = 0; i < fPads.GetSize(); ++i)
649 cout << Form("fPads[%d]=%x",i,fPads.At(i)) << endl;
650 if ( fPads.At(i) ) fPads.At(i)->Print();
655 //_____________________________________________________________________________
657 AliMUONCluster::Print(Option_t* opt) const
660 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
661 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
662 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
663 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
666 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
667 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
669 cout << " " << Area();
671 TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
677 //_____________________________________________________________________________
679 //AliMUONCluster::IsEqual(const TObject* obj) const
681 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
682 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
684 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
686 // AliMUONPad* p = c->Pad(i);
687 // if ( p->Compare(Pad(i)) ) return kFALSE;
692 //_____________________________________________________________________________
694 AliMUONCluster::Compare(const TObject* obj) const
696 /// Compare two clusters. Comparison is made on position and rawcharge only.
698 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
700 AliMpArea carea(cluster->Area());
701 AliMpArea area(Area());
703 if ( carea.Position().X() > area.Position().X() )
707 else if ( carea.Position().X() < area.Position().X() )
713 if ( carea.Position().Y() > area.Position().Y() )
717 else if ( carea.Position().Y() < area.Position().Y() )
723 if ( cluster->RawCharge() > RawCharge() )
727 else if ( cluster->RawCharge() < RawCharge() )
736 //_____________________________________________________________________________
738 AliMUONCluster::RemovePad(AliMUONPad* pad)
741 /// As a consequence, some internal information must be updated
745 // update cluster's data
746 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
747 fMultiplicity[0]=fMultiplicity[1]=0;
748 fRawCharge[0]=fRawCharge[1]=0;
749 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
751 AliMUONPad* p = Pad(i);
752 if ( p->IsSaturated() )
754 fIsSaturated[p->Cathode()] = kTRUE;
756 ++fMultiplicity[p->Cathode()];
757 fRawCharge[p->Cathode()] += p->Charge();
761 //_____________________________________________________________________________
763 AliMUONCluster::RawCharge() const
765 /// Returns the raw average charge
766 return (RawCharge(0)+RawCharge(1))/2.0;
769 //_____________________________________________________________________________
771 AliMUONCluster::RawCharge(Int_t cathode) const
773 /// Returns the average charge of a given cathode
774 if ( cathode == 0 || cathode == 1 )
776 return fRawCharge[cathode];
781 //_____________________________________________________________________________
783 AliMUONCluster::RawChargeAsymmetry() const
785 /// Returns the raw charge asymmetry
786 if ( RawCharge() > 0 )
788 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();