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 /// \class AliMUONCluster
31 /// A group of adjacent pads
33 /// Besides holding an internal array of AliMUONPads, this object
34 /// also computes some global characteristics for that pad sets.
36 /// \author Laurent Aphecetche
40 ClassImp(AliMUONCluster)
45 //___________________________________________________________________________
47 ShouldUsePad(const AliMUONPad& pad,
48 Int_t cathode, Int_t statusMask, Bool_t matchMask)
51 // FIXME : we should only use >=0 status, so we can fully
52 // use masking possibility ?
53 if ( pad.Status() < 0 ) return kFALSE;
55 if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
57 Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
60 test = ( pad.Status() == 0 );
62 if ( ( test && matchMask ) || ( !test && !matchMask ) )
70 //___________________________________________________________________________
71 Int_t Unique(Int_t n, Double_t* array, Double_t precision)
73 /// Return the number of *different* elements in array
74 /// where different is up to precision
75 /// Note that we assume that n is >= 1
79 Int_t* index = new Int_t[n];
81 TMath::Sort(n,array,index);
83 for ( Int_t i = 1; i < n; ++i )
85 if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
94 //_____________________________________________________________________________
95 AliMUONCluster::AliMUONCluster()
100 fPositionError(1E9,1E9),
105 fMultiplicity[0]=fMultiplicity[1]=0;
106 fRawCharge[0]=fRawCharge[1]=0;
107 fCharge[0]=fCharge[1]=0;
108 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
111 //_____________________________________________________________________________
112 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
115 fHasPosition(kFALSE),
117 fPositionError(1E9,1E9),
125 //_____________________________________________________________________________
127 AliMUONCluster::operator=(const AliMUONCluster& src)
129 /// assignement operator
130 AliMUONCluster c(src);
135 //_____________________________________________________________________________
136 AliMUONCluster::~AliMUONCluster()
138 /// dtor : note that we're owner of our pads
142 //_____________________________________________________________________________
144 AliMUONCluster::Clear(Option_t*)
146 /// Clear our pad array
147 if (fPads) fPads->Clear("C");
150 //_____________________________________________________________________________
152 AliMUONCluster::Contains(const AliMUONPad& pad) const
154 /// Whether this cluster contains the pad
155 if (!fPads) return kFALSE;
156 for ( Int_t i = 0; i < Multiplicity(); ++i )
158 AliMUONPad* p = Pad(i);
159 if ( pad.Compare(p) == 0 ) return kTRUE;
164 //_____________________________________________________________________________
166 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
168 /// Add all the pads for cluster to this one
169 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
171 AliMUONPad* p = cluster.Pad(i);
174 AliError("I already got this pad : ");
175 StdoutToAliError(p->Print(););
183 //_____________________________________________________________________________
185 AliMUONCluster::AddPad(const AliMUONPad& pad)
187 /// Add a pad to our pad array, and update some internal information
192 fPads = new TObjArray(10);
193 fPads->SetOwner(kTRUE);
195 AliMUONPad* p = new AliMUONPad(pad);
197 p->SetClusterId(GetUniqueID());
198 Int_t cathode = p->Cathode();
199 ++(fMultiplicity[cathode]);
200 fRawCharge[cathode] += p->Charge();
201 if ( p->IsSaturated() )
203 fIsSaturated[p->Cathode()]=kTRUE;
208 //___________________________________________________________________________
210 AliMUONCluster::AsString() const
212 /// Return a string containing a compact form of the pad list
213 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
215 for (Int_t i = 0; i < Multiplicity(); ++i )
217 AliMUONPad* p = Pad(i);
218 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
224 //___________________________________________________________________________
226 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
228 /// Whether the two clusters overlap
230 static Double_t precision = 1E-4; // cm
231 static TVector2 precisionAdjustment(precision,precision);
233 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
235 AliMUONPad* p1 = c1.Pad(i1);
237 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
239 AliMUONPad* p2 = c2.Pad(i2);
240 // Note: we use negative precision numbers, meaning
241 // the area of the pads will be *increased* by these small numbers
242 // prior to check the overlap by the AreOverlapping method,
243 // so pads touching only by the corners will be considered as
245 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
254 //_____________________________________________________________________________
256 AliMUONCluster::Area() const
258 /// Return the geometrical area covered by this cluster
260 // Start by finding the (x,y) limits of this cluster
261 TVector2 lowerLeft(1E9,1E9);
262 TVector2 upperRight(-1E9,-1E9);
264 for ( Int_t i = 0; i < Multiplicity(); ++i )
266 AliMUONPad* pad = Pad(i);
267 TVector2 ll = pad->Position() - pad->Dimensions();
268 TVector2 ur = pad->Position() + pad->Dimensions();
269 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
270 TMath::Min(ll.Y(),lowerLeft.Y()) );
271 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
272 TMath::Max(ur.Y(),upperRight.Y()) );
275 // then construct the area from those limits
276 return AliMpArea((lowerLeft+upperRight)/2,
277 (upperRight-lowerLeft)/2);
280 //_____________________________________________________________________________
282 AliMUONCluster::Area(Int_t cathode) const
284 /// Return the geometrical area covered by this cluster's pads on
287 // Start by finding the (x,y) limits of this cluster
288 TVector2 lowerLeft(1E9,1E9);
289 TVector2 upperRight(-1E9,-1E9);
291 for ( Int_t i = 0; i < Multiplicity(cathode); ++i )
293 AliMUONPad* pad = Pad(i);
294 TVector2 ll = pad->Position() - pad->Dimensions();
295 TVector2 ur = pad->Position() + pad->Dimensions();
296 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
297 TMath::Min(ll.Y(),lowerLeft.Y()) );
298 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
299 TMath::Max(ur.Y(),upperRight.Y()) );
302 // then construct the area from those limits
303 return AliMpArea((lowerLeft+upperRight)/2,
304 (upperRight-lowerLeft)/2);
307 //_____________________________________________________________________________
309 AliMUONCluster::Cathode() const
311 /// Return the cathode "number" of this cluster :
312 /// 0 if all its pads are on cathode 0
313 /// 1 if all its pads are on cathode 1
314 /// 2 if some pads on cath 0 and some on cath 1
317 if (Multiplicity(0)>0 && Multiplicity(1)>0)
321 else if (Multiplicity(0)>0)
325 else if (Multiplicity(1)>0)
333 //_____________________________________________________________________________
335 AliMUONCluster::Copy(TObject& obj) const
338 /// Copy this cluster to (cluster&)obj
341 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
345 dest.fPads = static_cast<TObjArray*>(fPads->Clone());
347 dest.fHasPosition = fHasPosition;
348 dest.fPosition = fPosition;
349 dest.fPositionError = fPositionError;
350 dest.fHasCharge = fHasCharge;
352 for ( Int_t i = 0; i < 2; ++i )
354 dest.fRawCharge[i] = fRawCharge[i];
355 dest.fCharge[i] = fCharge[i];
356 dest.fMultiplicity[i] = fMultiplicity[i];
357 dest.fIsSaturated[i] = fIsSaturated[i];
361 //_____________________________________________________________________________
363 AliMUONCluster::Charge() const
365 /// Return the average charge over both cathodes
366 return (Charge(0)+Charge(1))/2.0;
369 //_____________________________________________________________________________
371 AliMUONCluster::Charge(Int_t cathode) const
373 /// Returns the charge of a given cathode
374 if ( !fHasCharge ) return RawCharge(cathode);
376 if ( cathode == 0 || cathode == 1 )
378 return fCharge[cathode];
383 //_____________________________________________________________________________
385 AliMUONCluster::ChargeAsymmetry() const
387 /// Returns the charge asymmetry
390 return TMath::Abs(Charge(0)-Charge(1))/Charge();
395 //_____________________________________________________________________________
397 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
399 /// Returns the maximum pad dimensions (half sizes), only considering
400 /// pads matching (or not, depending matchMask) a given mask
402 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
403 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
405 return TVector2( TMath::Max(cath0.X(),cath1.X()),
406 TMath::Max(cath0.Y(),cath1.Y()) );
409 //_____________________________________________________________________________
411 AliMUONCluster::MaxPadDimensions(Int_t cathode,
412 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, within a
421 for ( Int_t i = 0; i < Multiplicity(); ++i )
423 AliMUONPad* pad = Pad(i);
424 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
426 xmax = TMath::Max(xmax,pad->DX());
427 ymax = TMath::Max(ymax,pad->DY());
430 return TVector2(xmax,ymax);
433 //_____________________________________________________________________________
435 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
437 /// Returns the minimum pad dimensions (half sizes), only considering
438 /// pads matching (or not, depending matchMask) a given mask
440 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
441 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
443 return TVector2( TMath::Min(cath0.X(),cath1.X()),
444 TMath::Min(cath0.Y(),cath1.Y()) );
447 //_____________________________________________________________________________
449 AliMUONCluster::MinPadDimensions(Int_t cathode,
450 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, within a
459 for ( Int_t i = 0; i < Multiplicity(); ++i )
461 AliMUONPad* pad = Pad(i);
462 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
464 xmin = TMath::Min(xmin,pad->DX());
465 ymin = TMath::Min(ymin,pad->DY());
468 return TVector2(xmin,ymin);
471 //_____________________________________________________________________________
473 AliMUONCluster::Multiplicity() const
475 /// Returns the total number of pads in this cluster
476 return Multiplicity(0)+Multiplicity(1);
479 //_____________________________________________________________________________
481 AliMUONCluster::Multiplicity(Int_t cathode) const
483 /// Returns the number of pads in this cluster, in the given cathode
484 if ( cathode == 0 || cathode == 1 )
486 return fMultiplicity[cathode];
491 //_____________________________________________________________________________
493 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
495 /// Number of pads satisfying (or not, depending matchMask) a
500 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
501 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
503 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
504 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
506 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
508 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
512 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
515 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
517 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
521 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
524 return AliMpIntPair(nx,ny);
527 //_____________________________________________________________________________
529 AliMUONCluster::NofPads(Int_t cathode,
530 Int_t statusMask, Bool_t matchMask) const
532 /// Number of pads of a given cathode, satisfying (or not,
533 /// depending matchMask) a given mask
535 Int_t n = Multiplicity(cathode);
538 return AliMpIntPair(0,0);
540 Double_t* x = new Double_t[n];
541 Double_t* y = new Double_t[n];
544 for ( Int_t i = 0; i < Multiplicity(); ++i )
546 AliMUONPad* pad = Pad(i);
547 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
555 Int_t cx = Unique(np,x,0.01);
556 Int_t cy = Unique(np,y,0.01);
561 return AliMpIntPair(cx,cy);
564 //_____________________________________________________________________________
566 AliMUONCluster::Pad(Int_t index) const
568 /// Returns the index-th pad
570 if (!fPads) return 0x0;
571 if ( index < fPads->GetLast()+1 )
573 return static_cast<AliMUONPad*>(fPads->At(index));
577 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
578 fPads->GetLast(),Multiplicity()));
585 //_____________________________________________________________________________
587 AliMUONCluster::Paint(Option_t*)
589 /// Paint this cluster
590 if (!Multiplicity()) return;
592 AliMpArea area(Area());
594 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
596 gVirtualX->SetFillStyle(0);
598 gVirtualX->SetLineColor(2);
599 gVirtualX->SetLineWidth(4);
600 for ( Int_t i = 0; i < Multiplicity(); ++i)
602 AliMUONPad* pad = Pad(i);
603 if ( pad->Cathode() == 0 ) pad->Paint();
606 gVirtualX->SetLineColor(4);
607 gVirtualX->SetLineWidth(2);
608 for ( Int_t i = 0; i < Multiplicity(); ++i)
610 AliMUONPad* pad = Pad(i);
611 if ( pad->Cathode() == 1 ) pad->Paint();
616 //_____________________________________________________________________________
618 AliMUONCluster::DumpMe() const
621 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
622 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
623 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
624 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
627 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
628 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
631 // cout << " " << Area() << endl;
634 for (Int_t i = 0; i < fPads->GetSize(); ++i)
636 cout << Form("fPads[%d]=%x",i,fPads->At(i)) << endl;
637 if ( fPads->At(i) ) fPads->At(i)->Print();
643 //_____________________________________________________________________________
645 AliMUONCluster::Print(Option_t* opt) const
648 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
649 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
650 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
651 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
654 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
655 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
657 cout << " " << Area();
660 TObjArray* a = static_cast<TObjArray*>(fPads->Clone());
667 //_____________________________________________________________________________
669 //AliMUONCluster::IsEqual(const TObject* obj) const
671 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
672 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
674 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
676 // AliMUONPad* p = c->Pad(i);
677 // if ( p->Compare(Pad(i)) ) return kFALSE;
682 //_____________________________________________________________________________
684 AliMUONCluster::Compare(const TObject* obj) const
686 /// Compare two clusters. Comparison is made on position and rawcharge only.
688 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
690 AliMpArea carea(cluster->Area());
691 AliMpArea area(Area());
693 if ( carea.Position().X() > area.Position().X() )
697 else if ( carea.Position().X() < area.Position().X() )
703 if ( carea.Position().Y() > area.Position().Y() )
707 else if ( carea.Position().Y() < area.Position().Y() )
713 if ( cluster->RawCharge() > RawCharge() )
717 else if ( cluster->RawCharge() < RawCharge() )
726 //_____________________________________________________________________________
728 AliMUONCluster::RemovePad(AliMUONPad* pad)
731 /// As a consequence, some internal information must be updated
735 // update cluster's data
736 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
737 fMultiplicity[0]=fMultiplicity[1]=0;
738 fRawCharge[0]=fRawCharge[1]=0;
739 for ( Int_t i = 0; i <= fPads->GetLast(); ++i )
741 AliMUONPad* p = Pad(i);
742 if ( p->IsSaturated() )
744 fIsSaturated[p->Cathode()] = kTRUE;
746 ++fMultiplicity[p->Cathode()];
747 fRawCharge[p->Cathode()] += p->Charge();
751 //_____________________________________________________________________________
753 AliMUONCluster::RawCharge() const
755 /// Returns the raw average charge
756 return (RawCharge(0)+RawCharge(1))/2.0;
759 //_____________________________________________________________________________
761 AliMUONCluster::RawCharge(Int_t cathode) const
763 /// Returns the average charge of a given cathode
764 if ( cathode == 0 || cathode == 1 )
766 return fRawCharge[cathode];
771 //_____________________________________________________________________________
773 AliMUONCluster::RawChargeAsymmetry() const
775 /// Returns the raw charge asymmetry
776 if ( RawCharge() > 0 )
778 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();