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
159 //_____________________________________________________________________________
161 AliMUONCluster::Contains(const AliMUONPad& pad) const
163 /// Whether this cluster contains the pad
164 if (fPads.IsEmpty()) return kFALSE;
166 for ( Int_t i = 0; i < Multiplicity(); ++i )
168 AliMUONPad* p = Pad(i);
169 if ( pad.Compare(p) == 0 ) return kTRUE;
174 //_____________________________________________________________________________
176 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
178 /// Add all the pads for cluster to this one
179 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
181 AliMUONPad* p = cluster.Pad(i);
184 AliError("I already got this pad : ");
185 StdoutToAliError(p->Print(););
193 //_____________________________________________________________________________
195 AliMUONCluster::AddPad(const AliMUONPad& pad)
197 /// Add a pad to our pad array, and update some internal information
200 AliMUONPad* p = new AliMUONPad(pad);
202 p->SetClusterId(GetUniqueID());
203 Int_t cathode = p->Cathode();
204 ++(fMultiplicity[cathode]);
205 fRawCharge[cathode] += p->Charge();
206 if ( p->IsSaturated() )
208 fIsSaturated[p->Cathode()]=kTRUE;
213 //___________________________________________________________________________
215 AliMUONCluster::AsString() const
217 /// Return a string containing a compact form of the pad list
218 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
220 for (Int_t i = 0; i < Multiplicity(); ++i )
222 AliMUONPad* p = Pad(i);
223 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
229 //___________________________________________________________________________
231 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
233 /// Whether the two clusters overlap
235 static Double_t precision = 1E-4; // cm
236 static TVector2 precisionAdjustment(precision,precision);
238 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
240 AliMUONPad* p1 = c1.Pad(i1);
242 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
244 AliMUONPad* p2 = c2.Pad(i2);
245 // Note: we use negative precision numbers, meaning
246 // the area of the pads will be *increased* by these small numbers
247 // prior to check the overlap by the AreOverlapping method,
248 // so pads touching only by the corners will be considered as
250 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
259 //_____________________________________________________________________________
261 AliMUONCluster::Area() const
263 /// Return the geometrical area covered by this cluster
265 // Start by finding the (x,y) limits of this cluster
266 TVector2 lowerLeft(1E9,1E9);
267 TVector2 upperRight(-1E9,-1E9);
269 for ( Int_t i = 0; i < Multiplicity(); ++i )
271 AliMUONPad* pad = Pad(i);
272 TVector2 ll = pad->Position() - pad->Dimensions();
273 TVector2 ur = pad->Position() + pad->Dimensions();
274 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
275 TMath::Min(ll.Y(),lowerLeft.Y()) );
276 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
277 TMath::Max(ur.Y(),upperRight.Y()) );
280 // then construct the area from those limits
281 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
282 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
285 //_____________________________________________________________________________
287 AliMUONCluster::Area(Int_t cathode) const
289 /// Return the geometrical area covered by this cluster's pads on
292 // Start by finding the (x,y) limits of this cluster
293 TVector2 lowerLeft(1E9,1E9);
294 TVector2 upperRight(-1E9,-1E9);
296 for ( Int_t i = 0; i < Multiplicity(); ++i )
298 AliMUONPad* pad = Pad(i);
299 if ( pad->Cathode() == cathode )
301 TVector2 ll = pad->Position() - pad->Dimensions();
302 TVector2 ur = pad->Position() + pad->Dimensions();
303 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
304 TMath::Min(ll.Y(),lowerLeft.Y()) );
305 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
306 TMath::Max(ur.Y(),upperRight.Y()) );
310 // then construct the area from those limits
311 return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
312 (upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
315 //_____________________________________________________________________________
317 AliMUONCluster::IsMonoCathode() const
319 /// Whether we have signals only in one of the two cathodes
320 return (Cathode()<2);
323 //_____________________________________________________________________________
325 AliMUONCluster::Cathode() const
327 /// Return the cathode "number" of this cluster :
328 /// 0 if all its pads are on cathode 0
329 /// 1 if all its pads are on cathode 1
330 /// 2 if some pads on cath 0 and some on cath 1
333 if (Multiplicity(0)>0 && Multiplicity(1)>0)
337 else if (Multiplicity(0)>0)
341 else if (Multiplicity(1)>0)
349 //_____________________________________________________________________________
351 AliMUONCluster::Copy(TObject& obj) const
354 /// Copy this cluster to (cluster&)obj
357 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
359 // dest.fPads.Delete();
362 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
364 AliMUONPad* p = static_cast<AliMUONPad*>(fPads.UncheckedAt(i));
365 dest.fPads.AddLast(new AliMUONPad(*p));
367 dest.fHasPosition = fHasPosition;
368 dest.fPosition = fPosition;
369 dest.fPositionError = fPositionError;
370 dest.fHasCharge = fHasCharge;
372 for ( Int_t i = 0; i < 2; ++i )
374 dest.fRawCharge[i] = fRawCharge[i];
375 dest.fCharge[i] = fCharge[i];
376 dest.fMultiplicity[i] = fMultiplicity[i];
377 dest.fIsSaturated[i] = fIsSaturated[i];
381 //_____________________________________________________________________________
383 AliMUONCluster::Charge() const
385 /// Return the average charge over both cathodes
387 if ( Multiplicity(0) && Multiplicity(1) )
389 return (Charge(0)+Charge(1))/2.0;
391 else if ( Multiplicity(0) )
395 else if ( Multiplicity(1) )
399 AliError("Should not be here ?!");
403 //_____________________________________________________________________________
405 AliMUONCluster::Charge(Int_t cathode) const
407 /// Returns the charge of a given cathode
408 if ( !fHasCharge ) return RawCharge(cathode);
410 if ( cathode == 0 || cathode == 1 )
412 return fCharge[cathode];
417 //_____________________________________________________________________________
419 AliMUONCluster::ChargeAsymmetry() const
421 /// Returns the charge asymmetry
424 return TMath::Abs(Charge(0)-Charge(1))/Charge();
429 //_____________________________________________________________________________
431 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
433 /// Returns the maximum pad dimensions (half sizes), only considering
434 /// pads matching (or not, depending matchMask) a given mask
436 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
437 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
439 return TVector2( TMath::Max(cath0.X(),cath1.X()),
440 TMath::Max(cath0.Y(),cath1.Y()) );
443 //_____________________________________________________________________________
445 AliMUONCluster::MaxPadDimensions(Int_t cathode,
446 Int_t statusMask, Bool_t matchMask) const
448 /// Returns the maximum pad dimensions (half sizes), only considering
449 /// pads matching (or not, depending matchMask) a given mask, within a
455 for ( Int_t i = 0; i < Multiplicity(); ++i )
457 AliMUONPad* pad = Pad(i);
458 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
460 xmax = TMath::Max(xmax,pad->DX());
461 ymax = TMath::Max(ymax,pad->DY());
464 return TVector2(xmax,ymax);
467 //_____________________________________________________________________________
469 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
471 /// Returns the minimum pad dimensions (half sizes), only considering
472 /// pads matching (or not, depending matchMask) a given mask
474 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
475 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
477 return TVector2( TMath::Min(cath0.X(),cath1.X()),
478 TMath::Min(cath0.Y(),cath1.Y()) );
481 //_____________________________________________________________________________
483 AliMUONCluster::MinPadDimensions(Int_t cathode,
484 Int_t statusMask, Bool_t matchMask) const
486 /// Returns the minimum pad dimensions (half sizes), only considering
487 /// pads matching (or not, depending matchMask) a given mask, within a
493 for ( Int_t i = 0; i < Multiplicity(); ++i )
495 AliMUONPad* pad = Pad(i);
496 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
498 xmin = TMath::Min(xmin,pad->DX());
499 ymin = TMath::Min(ymin,pad->DY());
502 return TVector2(xmin,ymin);
505 //_____________________________________________________________________________
507 AliMUONCluster::Multiplicity() const
509 /// Returns the total number of pads in this cluster
510 return Multiplicity(0)+Multiplicity(1);
513 //_____________________________________________________________________________
515 AliMUONCluster::Multiplicity(Int_t cathode) const
517 /// Returns the number of pads in this cluster, in the given cathode
518 if ( cathode == 0 || cathode == 1 )
520 return fMultiplicity[cathode];
525 //_____________________________________________________________________________
527 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
529 /// Number of pads satisfying (or not, depending matchMask) a
534 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
535 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
537 Long_t npad0(NofPads(0,statusMask,matchMask));
538 Long_t npad1(NofPads(1,statusMask,matchMask));
540 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
542 nx = TMath::Max( AliMp::PairFirst(npad0), AliMp::PairFirst(npad1) );
546 nx = dim0.X() < dim1.X() ? AliMp::PairFirst(npad0) : AliMp::PairFirst(npad1);
549 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
551 ny = TMath::Max( AliMp::PairSecond(npad0), AliMp::PairSecond(npad1) );
555 ny = dim0.Y() < dim1.Y() ? AliMp::PairSecond(npad0) : AliMp::PairSecond(npad1);
558 return AliMp::Pair(nx,ny);
561 //_____________________________________________________________________________
563 AliMUONCluster::NofPads(Int_t cathode,
564 Int_t statusMask, Bool_t matchMask) const
566 /// Number of pads of a given cathode, satisfying (or not,
567 /// depending matchMask) a given mask
569 Int_t n = Multiplicity(cathode);
574 Double_t* x = new Double_t[n];
575 Double_t* y = new Double_t[n];
578 for ( Int_t i = 0; i < Multiplicity(); ++i )
580 AliMUONPad* pad = Pad(i);
581 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
589 Int_t cx = Unique(np,x,0.01);
590 Int_t cy = Unique(np,y,0.01);
595 return AliMp::Pair(cx,cy);
598 //_____________________________________________________________________________
600 AliMUONCluster::Pad(Int_t index) const
602 /// Returns the index-th pad
604 if (fPads.IsEmpty()) return 0x0;
605 if ( index < fPads.GetLast()+1 )
607 return static_cast<AliMUONPad*>(fPads.At(index));
611 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
612 fPads.GetLast(),Multiplicity()));
619 //_____________________________________________________________________________
621 AliMUONCluster::Paint(Option_t*)
623 /// Paint this cluster
624 if (!Multiplicity()) return;
626 AliMpArea area(Area());
628 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
630 gVirtualX->SetFillStyle(0);
632 gVirtualX->SetLineColor(2);
633 gVirtualX->SetLineWidth(4);
634 for ( Int_t i = 0; i < Multiplicity(); ++i)
636 AliMUONPad* pad = Pad(i);
637 if ( pad->Cathode() == 0 ) pad->Paint();
640 gVirtualX->SetLineColor(4);
641 gVirtualX->SetLineWidth(2);
642 for ( Int_t i = 0; i < Multiplicity(); ++i)
644 AliMUONPad* pad = Pad(i);
645 if ( pad->Cathode() == 1 ) pad->Paint();
650 //_____________________________________________________________________________
652 AliMUONCluster::DumpMe() const
655 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
656 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
657 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
658 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
661 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
662 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
665 // cout << " " << Area() << endl;
666 for (Int_t i = 0; i < fPads.GetSize(); ++i)
668 cout << Form("fPads[%d]=%x",i,fPads.At(i)) << endl;
669 if ( fPads.At(i) ) fPads.At(i)->Print();
674 //_____________________________________________________________________________
676 AliMUONCluster::Print(Option_t* opt) const
679 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
680 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
681 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
682 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
685 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
686 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
688 cout << " " << Area();
690 TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
696 //_____________________________________________________________________________
698 //AliMUONCluster::IsEqual(const TObject* obj) const
700 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
701 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
703 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
705 // AliMUONPad* p = c->Pad(i);
706 // if ( p->Compare(Pad(i)) ) return kFALSE;
711 //_____________________________________________________________________________
713 AliMUONCluster::Compare(const TObject* obj) const
715 /// Compare two clusters. Comparison is made on position and rawcharge only.
717 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
719 AliMpArea carea(cluster->Area());
720 AliMpArea area(Area());
722 if ( carea.GetPositionX() > area.GetPositionX() )
726 else if ( carea.GetPositionX() < area.GetPositionX() )
732 if ( carea.GetPositionY() > area.GetPositionY() )
736 else if ( carea.GetPositionY() < area.GetPositionY() )
742 if ( cluster->RawCharge() > RawCharge() )
746 else if ( cluster->RawCharge() < RawCharge() )
755 //_____________________________________________________________________________
757 AliMUONCluster::RemovePad(AliMUONPad* pad)
760 /// As a consequence, some internal information must be updated
765 // update cluster's data
766 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
767 fMultiplicity[0]=fMultiplicity[1]=0;
768 fRawCharge[0]=fRawCharge[1]=0;
769 for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
771 AliMUONPad* p = Pad(i);
772 if ( p->IsSaturated() )
774 fIsSaturated[p->Cathode()] = kTRUE;
776 ++fMultiplicity[p->Cathode()];
777 fRawCharge[p->Cathode()] += p->Charge();
781 //_____________________________________________________________________________
783 AliMUONCluster::RawCharge() const
785 /// Returns the raw average charge
786 return (RawCharge(0)+RawCharge(1))/2.0;
789 //_____________________________________________________________________________
791 AliMUONCluster::RawCharge(Int_t cathode) const
793 /// Returns the average charge of a given cathode
794 if ( cathode == 0 || cathode == 1 )
796 return fRawCharge[cathode];
801 //_____________________________________________________________________________
803 AliMUONCluster::RawChargeAsymmetry() const
805 /// Returns the raw charge asymmetry
806 if ( RawCharge() > 0 )
808 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();