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 //-----------------------------------------------------------------------------
46 ClassImp(AliMUONCluster)
51 //___________________________________________________________________________
53 ShouldUsePad(const AliMUONPad& pad,
54 Int_t cathode, Int_t statusMask, Bool_t matchMask)
57 // FIXME : we should only use >=0 status, so we can fully
58 // use masking possibility ?
59 if ( pad.Status() < 0 ) return kFALSE;
61 if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
63 Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
66 test = ( pad.Status() == 0 );
68 if ( ( test && matchMask ) || ( !test && !matchMask ) )
76 //___________________________________________________________________________
77 Int_t Unique(Int_t n, Double_t* array, Double_t precision)
79 /// Return the number of *different* elements in array
80 /// where different is up to precision
81 /// Note that we assume that n is >= 1
85 Int_t* index = new Int_t[n];
87 TMath::Sort(n,array,index);
89 for ( Int_t i = 1; i < n; ++i )
91 if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
100 //_____________________________________________________________________________
101 AliMUONCluster::AliMUONCluster()
104 fHasPosition(kFALSE),
106 fPositionError(1E9,1E9),
111 fMultiplicity[0]=fMultiplicity[1]=0;
112 fRawCharge[0]=fRawCharge[1]=0;
113 fCharge[0]=fCharge[1]=0;
114 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
115 fPads.SetOwner(kTRUE);
118 //_____________________________________________________________________________
119 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
122 fHasPosition(kFALSE),
124 fPositionError(1E9,1E9),
129 fPads.SetOwner(kTRUE);
133 //_____________________________________________________________________________
135 AliMUONCluster::operator=(const AliMUONCluster& src)
137 /// assignement operator
145 //_____________________________________________________________________________
146 AliMUONCluster::~AliMUONCluster()
148 /// dtor : note that we're owner of our pads
152 //_____________________________________________________________________________
154 AliMUONCluster::Clear(Option_t*)
156 /// Clear our pad array
161 //_____________________________________________________________________________
163 AliMUONCluster::Contains(const AliMUONPad& pad) const
165 /// Whether this cluster contains the pad
166 if (fPads.IsEmpty()) return kFALSE;
168 for ( Int_t i = 0; i < Multiplicity(); ++i )
170 AliMUONPad* p = Pad(i);
171 if ( pad.Compare(p) == 0 ) return kTRUE;
176 //_____________________________________________________________________________
178 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
180 /// Add all the pads for cluster to this one
181 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
183 AliMUONPad* p = cluster.Pad(i);
186 AliError("I already got this pad : ");
187 StdoutToAliError(p->Print(););
195 //_____________________________________________________________________________
197 AliMUONCluster::AddPad(const AliMUONPad& pad)
199 /// Add a pad to our pad array, and update some internal information
202 AliMUONPad* p = new AliMUONPad(pad);
204 p->SetClusterId(GetUniqueID());
205 Int_t cathode = p->Cathode();
206 ++(fMultiplicity[cathode]);
207 fRawCharge[cathode] += p->Charge();
208 if ( p->IsSaturated() )
210 fIsSaturated[p->Cathode()]=kTRUE;
215 //___________________________________________________________________________
217 AliMUONCluster::AsString() const
219 /// Return a string containing a compact form of the pad list
220 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
222 for (Int_t i = 0; i < Multiplicity(); ++i )
224 AliMUONPad* p = Pad(i);
225 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
231 //___________________________________________________________________________
233 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
235 /// Whether the two clusters overlap
237 static Double_t precision = 1E-4; // cm
238 static TVector2 precisionAdjustment(precision,precision);
240 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
242 AliMUONPad* p1 = c1.Pad(i1);
244 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
246 AliMUONPad* p2 = c2.Pad(i2);
247 // Note: we use negative precision numbers, meaning
248 // the area of the pads will be *increased* by these small numbers
249 // prior to check the overlap by the AreOverlapping method,
250 // so pads touching only by the corners will be considered as
252 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
261 //_____________________________________________________________________________
263 AliMUONCluster::Area() const
265 /// Return the geometrical area covered by this cluster
267 // Start by finding the (x,y) limits of this cluster
268 TVector2 lowerLeft(1E9,1E9);
269 TVector2 upperRight(-1E9,-1E9);
271 for ( Int_t i = 0; i < Multiplicity(); ++i )
273 AliMUONPad* pad = Pad(i);
274 TVector2 ll = pad->Position() - pad->Dimensions();
275 TVector2 ur = pad->Position() + pad->Dimensions();
276 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
277 TMath::Min(ll.Y(),lowerLeft.Y()) );
278 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
279 TMath::Max(ur.Y(),upperRight.Y()) );
282 // then construct the area from those limits
283 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
284 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
287 //_____________________________________________________________________________
289 AliMUONCluster::Area(Int_t cathode) const
291 /// Return the geometrical area covered by this cluster's pads on
294 // Start by finding the (x,y) limits of this cluster
295 TVector2 lowerLeft(1E9,1E9);
296 TVector2 upperRight(-1E9,-1E9);
298 for ( Int_t i = 0; i < Multiplicity(); ++i )
300 AliMUONPad* pad = Pad(i);
301 if ( pad->Cathode() == cathode )
303 TVector2 ll = pad->Position() - pad->Dimensions();
304 TVector2 ur = pad->Position() + pad->Dimensions();
305 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
306 TMath::Min(ll.Y(),lowerLeft.Y()) );
307 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
308 TMath::Max(ur.Y(),upperRight.Y()) );
312 // then construct the area from those limits
313 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
314 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
317 //_____________________________________________________________________________
319 AliMUONCluster::IsMonoCathode() const
321 /// Whether we have signals only in one of the two cathodes
322 return (Cathode()<2);
325 //_____________________________________________________________________________
327 AliMUONCluster::Cathode() const
329 /// Return the cathode "number" of this cluster :
330 /// 0 if all its pads are on cathode 0
331 /// 1 if all its pads are on cathode 1
332 /// 2 if some pads on cath 0 and some on cath 1
335 if (Multiplicity(0)>0 && Multiplicity(1)>0)
339 else if (Multiplicity(0)>0)
343 else if (Multiplicity(1)>0)
351 //_____________________________________________________________________________
353 AliMUONCluster::Copy(TObject& obj) const
356 /// Copy this cluster to (cluster&)obj
359 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
361 // dest.fPads.Delete();
364 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
366 AliMUONPad* p = static_cast<AliMUONPad*>(fPads.UncheckedAt(i));
367 dest.fPads.AddLast(new AliMUONPad(*p));
369 dest.fHasPosition = fHasPosition;
370 dest.fPosition = fPosition;
371 dest.fPositionError = fPositionError;
372 dest.fHasCharge = fHasCharge;
374 for ( Int_t i = 0; i < 2; ++i )
376 dest.fRawCharge[i] = fRawCharge[i];
377 dest.fCharge[i] = fCharge[i];
378 dest.fMultiplicity[i] = fMultiplicity[i];
379 dest.fIsSaturated[i] = fIsSaturated[i];
383 //_____________________________________________________________________________
385 AliMUONCluster::Charge() const
387 /// Return the average charge over both cathodes
389 if ( Multiplicity(0) && Multiplicity(1) )
391 return (Charge(0)+Charge(1))/2.0;
393 else if ( Multiplicity(0) )
397 else if ( Multiplicity(1) )
401 AliError("Should not be here ?!");
405 //_____________________________________________________________________________
407 AliMUONCluster::Charge(Int_t cathode) const
409 /// Returns the charge of a given cathode
410 if ( !fHasCharge ) return RawCharge(cathode);
412 if ( cathode == 0 || cathode == 1 )
414 return fCharge[cathode];
419 //_____________________________________________________________________________
421 AliMUONCluster::ChargeAsymmetry() const
423 /// Returns the charge asymmetry
426 return TMath::Abs(Charge(0)-Charge(1))/Charge();
431 //_____________________________________________________________________________
433 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
435 /// Returns the maximum pad dimensions (half sizes), only considering
436 /// pads matching (or not, depending matchMask) a given mask
438 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
439 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
441 return TVector2( TMath::Max(cath0.X(),cath1.X()),
442 TMath::Max(cath0.Y(),cath1.Y()) );
445 //_____________________________________________________________________________
447 AliMUONCluster::MaxPadDimensions(Int_t cathode,
448 Int_t statusMask, Bool_t matchMask) const
450 /// Returns the maximum pad dimensions (half sizes), only considering
451 /// pads matching (or not, depending matchMask) a given mask, within a
457 for ( Int_t i = 0; i < Multiplicity(); ++i )
459 AliMUONPad* pad = Pad(i);
460 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
462 xmax = TMath::Max(xmax,pad->DX());
463 ymax = TMath::Max(ymax,pad->DY());
466 return TVector2(xmax,ymax);
469 //_____________________________________________________________________________
471 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
473 /// Returns the minimum pad dimensions (half sizes), only considering
474 /// pads matching (or not, depending matchMask) a given mask
476 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
477 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
479 return TVector2( TMath::Min(cath0.X(),cath1.X()),
480 TMath::Min(cath0.Y(),cath1.Y()) );
483 //_____________________________________________________________________________
485 AliMUONCluster::MinPadDimensions(Int_t cathode,
486 Int_t statusMask, Bool_t matchMask) const
488 /// Returns the minimum pad dimensions (half sizes), only considering
489 /// pads matching (or not, depending matchMask) a given mask, within a
495 for ( Int_t i = 0; i < Multiplicity(); ++i )
497 AliMUONPad* pad = Pad(i);
498 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
500 xmin = TMath::Min(xmin,pad->DX());
501 ymin = TMath::Min(ymin,pad->DY());
504 return TVector2(xmin,ymin);
507 //_____________________________________________________________________________
509 AliMUONCluster::Multiplicity() const
511 /// Returns the total number of pads in this cluster
512 return Multiplicity(0)+Multiplicity(1);
515 //_____________________________________________________________________________
517 AliMUONCluster::Multiplicity(Int_t cathode) const
519 /// Returns the number of pads in this cluster, in the given cathode
520 if ( cathode == 0 || cathode == 1 )
522 return fMultiplicity[cathode];
527 //_____________________________________________________________________________
529 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
531 /// Number of pads satisfying (or not, depending matchMask) a
536 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
537 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
539 Long_t npad0(NofPads(0,statusMask,matchMask));
540 Long_t npad1(NofPads(1,statusMask,matchMask));
542 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
544 nx = TMath::Max( AliMp::PairFirst(npad0), AliMp::PairFirst(npad1) );
548 nx = dim0.X() < dim1.X() ? AliMp::PairFirst(npad0) : AliMp::PairFirst(npad1);
551 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
553 ny = TMath::Max( AliMp::PairSecond(npad0), AliMp::PairSecond(npad1) );
557 ny = dim0.Y() < dim1.Y() ? AliMp::PairSecond(npad0) : AliMp::PairSecond(npad1);
560 return AliMp::Pair(nx,ny);
563 //_____________________________________________________________________________
565 AliMUONCluster::NofPads(Int_t cathode,
566 Int_t statusMask, Bool_t matchMask) const
568 /// Number of pads of a given cathode, satisfying (or not,
569 /// depending matchMask) a given mask
571 Int_t n = Multiplicity(cathode);
576 Double_t* x = new Double_t[n];
577 Double_t* y = new Double_t[n];
580 for ( Int_t i = 0; i < Multiplicity(); ++i )
582 AliMUONPad* pad = Pad(i);
583 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
591 Int_t cx = Unique(np,x,0.01);
592 Int_t cy = Unique(np,y,0.01);
597 return AliMp::Pair(cx,cy);
600 //_____________________________________________________________________________
602 AliMUONCluster::Pad(Int_t index) const
604 /// Returns the index-th pad
606 if (fPads.IsEmpty()) return 0x0;
607 if ( index < fPads.GetLast()+1 )
609 return static_cast<AliMUONPad*>(fPads.At(index));
613 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
614 fPads.GetLast(),Multiplicity()));
621 //_____________________________________________________________________________
623 AliMUONCluster::Paint(Option_t*)
625 /// Paint this cluster
626 if (!Multiplicity()) return;
628 AliMpArea area(Area());
630 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
632 gVirtualX->SetFillStyle(0);
634 gVirtualX->SetLineColor(2);
635 gVirtualX->SetLineWidth(4);
636 for ( Int_t i = 0; i < Multiplicity(); ++i)
638 AliMUONPad* pad = Pad(i);
639 if ( pad->Cathode() == 0 ) pad->Paint();
642 gVirtualX->SetLineColor(4);
643 gVirtualX->SetLineWidth(2);
644 for ( Int_t i = 0; i < Multiplicity(); ++i)
646 AliMUONPad* pad = Pad(i);
647 if ( pad->Cathode() == 1 ) pad->Paint();
652 //_____________________________________________________________________________
654 AliMUONCluster::DumpMe() const
657 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
658 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
659 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
660 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
663 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
664 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
667 // cout << " " << Area() << endl;
668 for (Int_t i = 0; i < fPads.GetSize(); ++i)
670 cout << Form("fPads[%d]=%p",i,fPads.At(i)) << endl;
671 if ( fPads.At(i) ) fPads.At(i)->Print();
676 //_____________________________________________________________________________
678 AliMUONCluster::Print(Option_t* opt) const
681 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
682 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
683 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
684 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
687 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
688 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
690 cout << " " << Area();
692 TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
698 //_____________________________________________________________________________
700 //AliMUONCluster::IsEqual(const TObject* obj) const
702 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
703 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
705 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
707 // AliMUONPad* p = c->Pad(i);
708 // if ( p->Compare(Pad(i)) ) return kFALSE;
713 //_____________________________________________________________________________
715 AliMUONCluster::Compare(const TObject* obj) const
717 /// Compare two clusters. Comparison is made on position and rawcharge only.
719 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
721 AliMpArea carea(cluster->Area());
722 AliMpArea area(Area());
724 if ( carea.GetPositionX() > area.GetPositionX() )
728 else if ( carea.GetPositionX() < area.GetPositionX() )
734 if ( carea.GetPositionY() > area.GetPositionY() )
738 else if ( carea.GetPositionY() < area.GetPositionY() )
744 if ( cluster->RawCharge() > RawCharge() )
748 else if ( cluster->RawCharge() < RawCharge() )
757 //_____________________________________________________________________________
759 AliMUONCluster::RemovePad(AliMUONPad* pad)
762 /// As a consequence, some internal information must be updated
767 // update cluster's data
768 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
769 fMultiplicity[0]=fMultiplicity[1]=0;
770 fRawCharge[0]=fRawCharge[1]=0;
771 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
773 AliMUONPad* p = Pad(i);
774 if ( p->IsSaturated() )
776 fIsSaturated[p->Cathode()] = kTRUE;
778 ++fMultiplicity[p->Cathode()];
779 fRawCharge[p->Cathode()] += p->Charge();
783 //_____________________________________________________________________________
785 AliMUONCluster::RawCharge() const
787 /// Returns the raw average charge
788 return (RawCharge(0)+RawCharge(1))/2.0;
791 //_____________________________________________________________________________
793 AliMUONCluster::RawCharge(Int_t cathode) const
795 /// Returns the average charge of a given cathode
796 if ( cathode == 0 || cathode == 1 )
798 return fRawCharge[cathode];
803 //_____________________________________________________________________________
805 AliMUONCluster::RawChargeAsymmetry() const
807 /// Returns the raw charge asymmetry
808 if ( RawCharge() > 0 )
810 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();