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;
113 //_____________________________________________________________________________
114 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
117 fHasPosition(kFALSE),
119 fPositionError(1E9,1E9),
127 //_____________________________________________________________________________
129 AliMUONCluster::operator=(const AliMUONCluster& src)
131 /// assignement operator
139 //_____________________________________________________________________________
140 AliMUONCluster::~AliMUONCluster()
142 /// dtor : note that we're owner of our pads
146 //_____________________________________________________________________________
148 AliMUONCluster::Clear(Option_t*)
150 /// Clear our pad array
151 if (fPads) fPads->Clear("C");
154 //_____________________________________________________________________________
156 AliMUONCluster::Contains(const AliMUONPad& pad) const
158 /// Whether this cluster contains the pad
159 if (!fPads) return kFALSE;
160 for ( Int_t i = 0; i < Multiplicity(); ++i )
162 AliMUONPad* p = Pad(i);
163 if ( pad.Compare(p) == 0 ) return kTRUE;
168 //_____________________________________________________________________________
170 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
172 /// Add all the pads for cluster to this one
173 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
175 AliMUONPad* p = cluster.Pad(i);
178 AliError("I already got this pad : ");
179 StdoutToAliError(p->Print(););
187 //_____________________________________________________________________________
189 AliMUONCluster::AddPad(const AliMUONPad& pad)
191 /// Add a pad to our pad array, and update some internal information
196 fPads = new TObjArray(10);
197 fPads->SetOwner(kTRUE);
199 AliMUONPad* p = new AliMUONPad(pad);
201 p->SetClusterId(GetUniqueID());
202 Int_t cathode = p->Cathode();
203 ++(fMultiplicity[cathode]);
204 fRawCharge[cathode] += p->Charge();
205 if ( p->IsSaturated() )
207 fIsSaturated[p->Cathode()]=kTRUE;
212 //___________________________________________________________________________
214 AliMUONCluster::AsString() const
216 /// Return a string containing a compact form of the pad list
217 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
219 for (Int_t i = 0; i < Multiplicity(); ++i )
221 AliMUONPad* p = Pad(i);
222 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
228 //___________________________________________________________________________
230 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
232 /// Whether the two clusters overlap
234 static Double_t precision = 1E-4; // cm
235 static TVector2 precisionAdjustment(precision,precision);
237 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
239 AliMUONPad* p1 = c1.Pad(i1);
241 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
243 AliMUONPad* p2 = c2.Pad(i2);
244 // Note: we use negative precision numbers, meaning
245 // the area of the pads will be *increased* by these small numbers
246 // prior to check the overlap by the AreOverlapping method,
247 // so pads touching only by the corners will be considered as
249 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
258 //_____________________________________________________________________________
260 AliMUONCluster::Area() const
262 /// Return the geometrical area covered by this cluster
264 // Start by finding the (x,y) limits of this cluster
265 TVector2 lowerLeft(1E9,1E9);
266 TVector2 upperRight(-1E9,-1E9);
268 for ( Int_t i = 0; i < Multiplicity(); ++i )
270 AliMUONPad* pad = Pad(i);
271 TVector2 ll = pad->Position() - pad->Dimensions();
272 TVector2 ur = pad->Position() + pad->Dimensions();
273 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
274 TMath::Min(ll.Y(),lowerLeft.Y()) );
275 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
276 TMath::Max(ur.Y(),upperRight.Y()) );
279 // then construct the area from those limits
280 return AliMpArea((lowerLeft+upperRight)/2,
281 (upperRight-lowerLeft)/2);
284 //_____________________________________________________________________________
286 AliMUONCluster::Area(Int_t cathode) const
288 /// Return the geometrical area covered by this cluster's pads on
291 // Start by finding the (x,y) limits of this cluster
292 TVector2 lowerLeft(1E9,1E9);
293 TVector2 upperRight(-1E9,-1E9);
295 for ( Int_t i = 0; i < Multiplicity(); ++i )
297 AliMUONPad* pad = Pad(i);
298 if ( pad->Cathode() == cathode )
300 TVector2 ll = pad->Position() - pad->Dimensions();
301 TVector2 ur = pad->Position() + pad->Dimensions();
302 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
303 TMath::Min(ll.Y(),lowerLeft.Y()) );
304 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
305 TMath::Max(ur.Y(),upperRight.Y()) );
309 // then construct the area from those limits
310 return AliMpArea((lowerLeft+upperRight)/2,
311 (upperRight-lowerLeft)/2);
314 //_____________________________________________________________________________
316 AliMUONCluster::Cathode() const
318 /// Return the cathode "number" of this cluster :
319 /// 0 if all its pads are on cathode 0
320 /// 1 if all its pads are on cathode 1
321 /// 2 if some pads on cath 0 and some on cath 1
324 if (Multiplicity(0)>0 && Multiplicity(1)>0)
328 else if (Multiplicity(0)>0)
332 else if (Multiplicity(1)>0)
340 //_____________________________________________________________________________
342 AliMUONCluster::Copy(TObject& obj) const
345 /// Copy this cluster to (cluster&)obj
348 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
353 dest.fPads = static_cast<TObjArray*>(fPads->Clone());
355 dest.fHasPosition = fHasPosition;
356 dest.fPosition = fPosition;
357 dest.fPositionError = fPositionError;
358 dest.fHasCharge = fHasCharge;
360 for ( Int_t i = 0; i < 2; ++i )
362 dest.fRawCharge[i] = fRawCharge[i];
363 dest.fCharge[i] = fCharge[i];
364 dest.fMultiplicity[i] = fMultiplicity[i];
365 dest.fIsSaturated[i] = fIsSaturated[i];
369 //_____________________________________________________________________________
371 AliMUONCluster::Charge() const
373 /// Return the average charge over both cathodes
375 if ( Multiplicity(0) && Multiplicity(1) )
377 return (Charge(0)+Charge(1))/2.0;
379 else if ( Multiplicity(0) )
383 else if ( Multiplicity(1) )
387 AliError("Should not be here ?!");
391 //_____________________________________________________________________________
393 AliMUONCluster::Charge(Int_t cathode) const
395 /// Returns the charge of a given cathode
396 if ( !fHasCharge ) return RawCharge(cathode);
398 if ( cathode == 0 || cathode == 1 )
400 return fCharge[cathode];
405 //_____________________________________________________________________________
407 AliMUONCluster::ChargeAsymmetry() const
409 /// Returns the charge asymmetry
412 return TMath::Abs(Charge(0)-Charge(1))/Charge();
417 //_____________________________________________________________________________
419 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
421 /// Returns the maximum pad dimensions (half sizes), only considering
422 /// pads matching (or not, depending matchMask) a given mask
424 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
425 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
427 return TVector2( TMath::Max(cath0.X(),cath1.X()),
428 TMath::Max(cath0.Y(),cath1.Y()) );
431 //_____________________________________________________________________________
433 AliMUONCluster::MaxPadDimensions(Int_t cathode,
434 Int_t statusMask, Bool_t matchMask) const
436 /// Returns the maximum pad dimensions (half sizes), only considering
437 /// pads matching (or not, depending matchMask) a given mask, within a
443 for ( Int_t i = 0; i < Multiplicity(); ++i )
445 AliMUONPad* pad = Pad(i);
446 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
448 xmax = TMath::Max(xmax,pad->DX());
449 ymax = TMath::Max(ymax,pad->DY());
452 return TVector2(xmax,ymax);
455 //_____________________________________________________________________________
457 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
459 /// Returns the minimum pad dimensions (half sizes), only considering
460 /// pads matching (or not, depending matchMask) a given mask
462 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
463 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
465 return TVector2( TMath::Min(cath0.X(),cath1.X()),
466 TMath::Min(cath0.Y(),cath1.Y()) );
469 //_____________________________________________________________________________
471 AliMUONCluster::MinPadDimensions(Int_t cathode,
472 Int_t statusMask, Bool_t matchMask) const
474 /// Returns the minimum pad dimensions (half sizes), only considering
475 /// pads matching (or not, depending matchMask) a given mask, within a
481 for ( Int_t i = 0; i < Multiplicity(); ++i )
483 AliMUONPad* pad = Pad(i);
484 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
486 xmin = TMath::Min(xmin,pad->DX());
487 ymin = TMath::Min(ymin,pad->DY());
490 return TVector2(xmin,ymin);
493 //_____________________________________________________________________________
495 AliMUONCluster::Multiplicity() const
497 /// Returns the total number of pads in this cluster
498 return Multiplicity(0)+Multiplicity(1);
501 //_____________________________________________________________________________
503 AliMUONCluster::Multiplicity(Int_t cathode) const
505 /// Returns the number of pads in this cluster, in the given cathode
506 if ( cathode == 0 || cathode == 1 )
508 return fMultiplicity[cathode];
513 //_____________________________________________________________________________
515 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
517 /// Number of pads satisfying (or not, depending matchMask) a
522 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
523 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
525 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
526 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
528 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
530 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
534 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
537 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
539 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
543 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
546 return AliMpIntPair(nx,ny);
549 //_____________________________________________________________________________
551 AliMUONCluster::NofPads(Int_t cathode,
552 Int_t statusMask, Bool_t matchMask) const
554 /// Number of pads of a given cathode, satisfying (or not,
555 /// depending matchMask) a given mask
557 Int_t n = Multiplicity(cathode);
560 return AliMpIntPair(0,0);
562 Double_t* x = new Double_t[n];
563 Double_t* y = new Double_t[n];
566 for ( Int_t i = 0; i < Multiplicity(); ++i )
568 AliMUONPad* pad = Pad(i);
569 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
577 Int_t cx = Unique(np,x,0.01);
578 Int_t cy = Unique(np,y,0.01);
583 return AliMpIntPair(cx,cy);
586 //_____________________________________________________________________________
588 AliMUONCluster::Pad(Int_t index) const
590 /// Returns the index-th pad
592 if (!fPads) return 0x0;
593 if ( index < fPads->GetLast()+1 )
595 return static_cast<AliMUONPad*>(fPads->At(index));
599 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
600 fPads->GetLast(),Multiplicity()));
607 //_____________________________________________________________________________
609 AliMUONCluster::Paint(Option_t*)
611 /// Paint this cluster
612 if (!Multiplicity()) return;
614 AliMpArea area(Area());
616 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
618 gVirtualX->SetFillStyle(0);
620 gVirtualX->SetLineColor(2);
621 gVirtualX->SetLineWidth(4);
622 for ( Int_t i = 0; i < Multiplicity(); ++i)
624 AliMUONPad* pad = Pad(i);
625 if ( pad->Cathode() == 0 ) pad->Paint();
628 gVirtualX->SetLineColor(4);
629 gVirtualX->SetLineWidth(2);
630 for ( Int_t i = 0; i < Multiplicity(); ++i)
632 AliMUONPad* pad = Pad(i);
633 if ( pad->Cathode() == 1 ) pad->Paint();
638 //_____________________________________________________________________________
640 AliMUONCluster::DumpMe() const
643 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
644 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
645 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
646 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
649 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
650 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
653 // cout << " " << Area() << endl;
656 for (Int_t i = 0; i < fPads->GetSize(); ++i)
658 cout << Form("fPads[%d]=%x",i,fPads->At(i)) << endl;
659 if ( fPads->At(i) ) fPads->At(i)->Print();
665 //_____________________________________________________________________________
667 AliMUONCluster::Print(Option_t* opt) const
670 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
671 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
672 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
673 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
676 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
677 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
679 cout << " " << Area();
682 TObjArray* a = static_cast<TObjArray*>(fPads->Clone());
689 //_____________________________________________________________________________
691 //AliMUONCluster::IsEqual(const TObject* obj) const
693 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
694 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
696 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
698 // AliMUONPad* p = c->Pad(i);
699 // if ( p->Compare(Pad(i)) ) return kFALSE;
704 //_____________________________________________________________________________
706 AliMUONCluster::Compare(const TObject* obj) const
708 /// Compare two clusters. Comparison is made on position and rawcharge only.
710 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
712 AliMpArea carea(cluster->Area());
713 AliMpArea area(Area());
715 if ( carea.Position().X() > area.Position().X() )
719 else if ( carea.Position().X() < area.Position().X() )
725 if ( carea.Position().Y() > area.Position().Y() )
729 else if ( carea.Position().Y() < area.Position().Y() )
735 if ( cluster->RawCharge() > RawCharge() )
739 else if ( cluster->RawCharge() < RawCharge() )
748 //_____________________________________________________________________________
750 AliMUONCluster::RemovePad(AliMUONPad* pad)
753 /// 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();