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.
155 /// We have to call the destructor of TObjArray to be sure that all
156 /// allocated memory is released.
160 //_____________________________________________________________________________
162 AliMUONCluster::Contains(const AliMUONPad& pad) const
164 /// Whether this cluster contains the pad
165 if (fPads.IsEmpty()) return kFALSE;
167 for ( Int_t i = 0; i < Multiplicity(); ++i )
169 AliMUONPad* p = Pad(i);
170 if ( pad.Compare(p) == 0 ) return kTRUE;
175 //_____________________________________________________________________________
177 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
179 /// Add all the pads for cluster to this one
180 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
182 AliMUONPad* p = cluster.Pad(i);
185 AliError("I already got this pad : ");
186 StdoutToAliError(p->Print(););
194 //_____________________________________________________________________________
196 AliMUONCluster::AddPad(const AliMUONPad& pad)
198 /// Add a pad to our pad array, and update some internal information
201 AliMUONPad* p = new AliMUONPad(pad);
203 p->SetClusterId(GetUniqueID());
204 Int_t cathode = p->Cathode();
205 ++(fMultiplicity[cathode]);
206 fRawCharge[cathode] += p->Charge();
207 if ( p->IsSaturated() )
209 fIsSaturated[p->Cathode()]=kTRUE;
214 //___________________________________________________________________________
216 AliMUONCluster::AsString() const
218 /// Return a string containing a compact form of the pad list
219 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
221 for (Int_t i = 0; i < Multiplicity(); ++i )
223 AliMUONPad* p = Pad(i);
224 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
230 //___________________________________________________________________________
232 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
234 /// Whether the two clusters overlap
236 static Double_t precision = 1E-4; // cm
237 static TVector2 precisionAdjustment(precision,precision);
239 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
241 AliMUONPad* p1 = c1.Pad(i1);
243 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
245 AliMUONPad* p2 = c2.Pad(i2);
246 // Note: we use negative precision numbers, meaning
247 // the area of the pads will be *increased* by these small numbers
248 // prior to check the overlap by the AreOverlapping method,
249 // so pads touching only by the corners will be considered as
251 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
260 //_____________________________________________________________________________
262 AliMUONCluster::Area() const
264 /// Return the geometrical area covered by this cluster
266 // Start by finding the (x,y) limits of this cluster
267 TVector2 lowerLeft(1E9,1E9);
268 TVector2 upperRight(-1E9,-1E9);
270 for ( Int_t i = 0; i < Multiplicity(); ++i )
272 AliMUONPad* pad = Pad(i);
273 TVector2 ll = pad->Position() - pad->Dimensions();
274 TVector2 ur = pad->Position() + pad->Dimensions();
275 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
276 TMath::Min(ll.Y(),lowerLeft.Y()) );
277 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
278 TMath::Max(ur.Y(),upperRight.Y()) );
281 // then construct the area from those limits
282 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
283 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
286 //_____________________________________________________________________________
288 AliMUONCluster::Area(Int_t cathode) const
290 /// Return the geometrical area covered by this cluster's pads on
293 // Start by finding the (x,y) limits of this cluster
294 TVector2 lowerLeft(1E9,1E9);
295 TVector2 upperRight(-1E9,-1E9);
297 for ( Int_t i = 0; i < Multiplicity(); ++i )
299 AliMUONPad* pad = Pad(i);
300 if ( pad->Cathode() == cathode )
302 TVector2 ll = pad->Position() - pad->Dimensions();
303 TVector2 ur = pad->Position() + pad->Dimensions();
304 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
305 TMath::Min(ll.Y(),lowerLeft.Y()) );
306 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
307 TMath::Max(ur.Y(),upperRight.Y()) );
311 // then construct the area from those limits
312 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
313 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
316 //_____________________________________________________________________________
318 AliMUONCluster::IsMonoCathode() const
320 /// Whether we have signals only in one of the two cathodes
321 return (Cathode()<2);
324 //_____________________________________________________________________________
326 AliMUONCluster::Cathode() const
328 /// Return the cathode "number" of this cluster :
329 /// 0 if all its pads are on cathode 0
330 /// 1 if all its pads are on cathode 1
331 /// 2 if some pads on cath 0 and some on cath 1
334 if (Multiplicity(0)>0 && Multiplicity(1)>0)
338 else if (Multiplicity(0)>0)
342 else if (Multiplicity(1)>0)
350 //_____________________________________________________________________________
352 AliMUONCluster::Copy(TObject& obj) const
355 /// Copy this cluster to (cluster&)obj
358 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
360 // dest.fPads.Delete();
363 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
365 AliMUONPad* p = static_cast<AliMUONPad*>(fPads.UncheckedAt(i));
366 dest.fPads.AddLast(new AliMUONPad(*p));
368 dest.fHasPosition = fHasPosition;
369 dest.fPosition = fPosition;
370 dest.fPositionError = fPositionError;
371 dest.fHasCharge = fHasCharge;
373 for ( Int_t i = 0; i < 2; ++i )
375 dest.fRawCharge[i] = fRawCharge[i];
376 dest.fCharge[i] = fCharge[i];
377 dest.fMultiplicity[i] = fMultiplicity[i];
378 dest.fIsSaturated[i] = fIsSaturated[i];
382 //_____________________________________________________________________________
384 AliMUONCluster::Charge() const
386 /// Return the average charge over both cathodes
388 if ( Multiplicity(0) && Multiplicity(1) )
390 return (Charge(0)+Charge(1))/2.0;
392 else if ( Multiplicity(0) )
396 else if ( Multiplicity(1) )
400 AliError("Should not be here ?!");
404 //_____________________________________________________________________________
406 AliMUONCluster::Charge(Int_t cathode) const
408 /// Returns the charge of a given cathode
409 if ( !fHasCharge ) return RawCharge(cathode);
411 if ( cathode == 0 || cathode == 1 )
413 return fCharge[cathode];
418 //_____________________________________________________________________________
420 AliMUONCluster::ChargeAsymmetry() const
422 /// Returns the charge asymmetry
425 return TMath::Abs(Charge(0)-Charge(1))/Charge();
430 //_____________________________________________________________________________
432 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
434 /// Returns the maximum pad dimensions (half sizes), only considering
435 /// pads matching (or not, depending matchMask) a given mask
437 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
438 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
440 return TVector2( TMath::Max(cath0.X(),cath1.X()),
441 TMath::Max(cath0.Y(),cath1.Y()) );
444 //_____________________________________________________________________________
446 AliMUONCluster::MaxPadDimensions(Int_t cathode,
447 Int_t statusMask, Bool_t matchMask) const
449 /// Returns the maximum pad dimensions (half sizes), only considering
450 /// pads matching (or not, depending matchMask) a given mask, within a
456 for ( Int_t i = 0; i < Multiplicity(); ++i )
458 AliMUONPad* pad = Pad(i);
459 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
461 xmax = TMath::Max(xmax,pad->DX());
462 ymax = TMath::Max(ymax,pad->DY());
465 return TVector2(xmax,ymax);
468 //_____________________________________________________________________________
470 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
472 /// Returns the minimum pad dimensions (half sizes), only considering
473 /// pads matching (or not, depending matchMask) a given mask
475 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
476 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
478 return TVector2( TMath::Min(cath0.X(),cath1.X()),
479 TMath::Min(cath0.Y(),cath1.Y()) );
482 //_____________________________________________________________________________
484 AliMUONCluster::MinPadDimensions(Int_t cathode,
485 Int_t statusMask, Bool_t matchMask) const
487 /// Returns the minimum pad dimensions (half sizes), only considering
488 /// pads matching (or not, depending matchMask) a given mask, within a
494 for ( Int_t i = 0; i < Multiplicity(); ++i )
496 AliMUONPad* pad = Pad(i);
497 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
499 xmin = TMath::Min(xmin,pad->DX());
500 ymin = TMath::Min(ymin,pad->DY());
503 return TVector2(xmin,ymin);
506 //_____________________________________________________________________________
508 AliMUONCluster::Multiplicity() const
510 /// Returns the total number of pads in this cluster
511 return Multiplicity(0)+Multiplicity(1);
514 //_____________________________________________________________________________
516 AliMUONCluster::Multiplicity(Int_t cathode) const
518 /// Returns the number of pads in this cluster, in the given cathode
519 if ( cathode == 0 || cathode == 1 )
521 return fMultiplicity[cathode];
526 //_____________________________________________________________________________
528 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
530 /// Number of pads satisfying (or not, depending matchMask) a
535 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
536 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
538 Long_t npad0(NofPads(0,statusMask,matchMask));
539 Long_t npad1(NofPads(1,statusMask,matchMask));
541 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
543 nx = TMath::Max( AliMp::PairFirst(npad0), AliMp::PairFirst(npad1) );
547 nx = dim0.X() < dim1.X() ? AliMp::PairFirst(npad0) : AliMp::PairFirst(npad1);
550 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
552 ny = TMath::Max( AliMp::PairSecond(npad0), AliMp::PairSecond(npad1) );
556 ny = dim0.Y() < dim1.Y() ? AliMp::PairSecond(npad0) : AliMp::PairSecond(npad1);
559 return AliMp::Pair(nx,ny);
562 //_____________________________________________________________________________
564 AliMUONCluster::NofPads(Int_t cathode,
565 Int_t statusMask, Bool_t matchMask) const
567 /// Number of pads of a given cathode, satisfying (or not,
568 /// depending matchMask) a given mask
570 Int_t n = Multiplicity(cathode);
575 Double_t* x = new Double_t[n];
576 Double_t* y = new Double_t[n];
579 for ( Int_t i = 0; i < Multiplicity(); ++i )
581 AliMUONPad* pad = Pad(i);
582 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
590 Int_t cx = Unique(np,x,0.01);
591 Int_t cy = Unique(np,y,0.01);
596 return AliMp::Pair(cx,cy);
599 //_____________________________________________________________________________
601 AliMUONCluster::Pad(Int_t index) const
603 /// Returns the index-th pad
605 if (fPads.IsEmpty()) return 0x0;
606 if ( index < fPads.GetLast()+1 )
608 return static_cast<AliMUONPad*>(fPads.At(index));
612 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
613 fPads.GetLast(),Multiplicity()));
620 //_____________________________________________________________________________
622 AliMUONCluster::Paint(Option_t*)
624 /// Paint this cluster
625 if (!Multiplicity()) return;
627 AliMpArea area(Area());
629 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
631 gVirtualX->SetFillStyle(0);
633 gVirtualX->SetLineColor(2);
634 gVirtualX->SetLineWidth(4);
635 for ( Int_t i = 0; i < Multiplicity(); ++i)
637 AliMUONPad* pad = Pad(i);
638 if ( pad->Cathode() == 0 ) pad->Paint();
641 gVirtualX->SetLineColor(4);
642 gVirtualX->SetLineWidth(2);
643 for ( Int_t i = 0; i < Multiplicity(); ++i)
645 AliMUONPad* pad = Pad(i);
646 if ( pad->Cathode() == 1 ) pad->Paint();
651 //_____________________________________________________________________________
653 AliMUONCluster::DumpMe() const
656 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
657 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
658 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
659 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
662 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
663 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
666 // cout << " " << Area() << endl;
667 for (Int_t i = 0; i < fPads.GetSize(); ++i)
669 cout << Form("fPads[%d]=%p",i,fPads.At(i)) << endl;
670 if ( fPads.At(i) ) fPads.At(i)->Print();
675 //_____________________________________________________________________________
677 AliMUONCluster::Print(Option_t* opt) const
680 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
681 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
682 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
683 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
686 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
687 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
689 cout << " " << Area();
691 TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
697 //_____________________________________________________________________________
699 //AliMUONCluster::IsEqual(const TObject* obj) const
701 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
702 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
704 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
706 // AliMUONPad* p = c->Pad(i);
707 // if ( p->Compare(Pad(i)) ) return kFALSE;
712 //_____________________________________________________________________________
714 AliMUONCluster::Compare(const TObject* obj) const
716 /// Compare two clusters. Comparison is made on position and rawcharge only.
718 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
720 AliMpArea carea(cluster->Area());
721 AliMpArea area(Area());
723 if ( carea.GetPositionX() > area.GetPositionX() )
727 else if ( carea.GetPositionX() < area.GetPositionX() )
733 if ( carea.GetPositionY() > area.GetPositionY() )
737 else if ( carea.GetPositionY() < area.GetPositionY() )
743 if ( cluster->RawCharge() > RawCharge() )
747 else if ( cluster->RawCharge() < RawCharge() )
756 //_____________________________________________________________________________
758 AliMUONCluster::RemovePad(AliMUONPad* pad)
761 /// As a consequence, some internal information must be updated
766 // update cluster's data
767 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
768 fMultiplicity[0]=fMultiplicity[1]=0;
769 fRawCharge[0]=fRawCharge[1]=0;
770 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
772 AliMUONPad* p = Pad(i);
773 if ( p->IsSaturated() )
775 fIsSaturated[p->Cathode()] = kTRUE;
777 ++fMultiplicity[p->Cathode()];
778 fRawCharge[p->Cathode()] += p->Charge();
782 //_____________________________________________________________________________
784 AliMUONCluster::RawCharge() const
786 /// Returns the raw average charge
787 return (RawCharge(0)+RawCharge(1))/2.0;
790 //_____________________________________________________________________________
792 AliMUONCluster::RawCharge(Int_t cathode) const
794 /// Returns the average charge of a given cathode
795 if ( cathode == 0 || cathode == 1 )
797 return fRawCharge[cathode];
802 //_____________________________________________________________________________
804 AliMUONCluster::RawChargeAsymmetry() const
806 /// Returns the raw charge asymmetry
807 if ( RawCharge() > 0 )
809 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();