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);
352 dest.fPads = static_cast<TObjArray*>(fPads->Clone());
354 dest.fHasPosition = fHasPosition;
355 dest.fPosition = fPosition;
356 dest.fPositionError = fPositionError;
357 dest.fHasCharge = fHasCharge;
359 for ( Int_t i = 0; i < 2; ++i )
361 dest.fRawCharge[i] = fRawCharge[i];
362 dest.fCharge[i] = fCharge[i];
363 dest.fMultiplicity[i] = fMultiplicity[i];
364 dest.fIsSaturated[i] = fIsSaturated[i];
368 //_____________________________________________________________________________
370 AliMUONCluster::Charge() const
372 /// Return the average charge over both cathodes
374 if ( Multiplicity(0) && Multiplicity(1) )
376 return (Charge(0)+Charge(1))/2.0;
378 else if ( Multiplicity(0) )
382 else if ( Multiplicity(1) )
386 AliError("Should not be here ?!");
390 //_____________________________________________________________________________
392 AliMUONCluster::Charge(Int_t cathode) const
394 /// Returns the charge of a given cathode
395 if ( !fHasCharge ) return RawCharge(cathode);
397 if ( cathode == 0 || cathode == 1 )
399 return fCharge[cathode];
404 //_____________________________________________________________________________
406 AliMUONCluster::ChargeAsymmetry() const
408 /// Returns the charge asymmetry
411 return TMath::Abs(Charge(0)-Charge(1))/Charge();
416 //_____________________________________________________________________________
418 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
420 /// Returns the maximum pad dimensions (half sizes), only considering
421 /// pads matching (or not, depending matchMask) a given mask
423 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
424 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
426 return TVector2( TMath::Max(cath0.X(),cath1.X()),
427 TMath::Max(cath0.Y(),cath1.Y()) );
430 //_____________________________________________________________________________
432 AliMUONCluster::MaxPadDimensions(Int_t cathode,
433 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, within a
442 for ( Int_t i = 0; i < Multiplicity(); ++i )
444 AliMUONPad* pad = Pad(i);
445 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
447 xmax = TMath::Max(xmax,pad->DX());
448 ymax = TMath::Max(ymax,pad->DY());
451 return TVector2(xmax,ymax);
454 //_____________________________________________________________________________
456 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
458 /// Returns the minimum pad dimensions (half sizes), only considering
459 /// pads matching (or not, depending matchMask) a given mask
461 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
462 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
464 return TVector2( TMath::Min(cath0.X(),cath1.X()),
465 TMath::Min(cath0.Y(),cath1.Y()) );
468 //_____________________________________________________________________________
470 AliMUONCluster::MinPadDimensions(Int_t cathode,
471 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, within a
480 for ( Int_t i = 0; i < Multiplicity(); ++i )
482 AliMUONPad* pad = Pad(i);
483 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
485 xmin = TMath::Min(xmin,pad->DX());
486 ymin = TMath::Min(ymin,pad->DY());
489 return TVector2(xmin,ymin);
492 //_____________________________________________________________________________
494 AliMUONCluster::Multiplicity() const
496 /// Returns the total number of pads in this cluster
497 return Multiplicity(0)+Multiplicity(1);
500 //_____________________________________________________________________________
502 AliMUONCluster::Multiplicity(Int_t cathode) const
504 /// Returns the number of pads in this cluster, in the given cathode
505 if ( cathode == 0 || cathode == 1 )
507 return fMultiplicity[cathode];
512 //_____________________________________________________________________________
514 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
516 /// Number of pads satisfying (or not, depending matchMask) a
521 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
522 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
524 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
525 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
527 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
529 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
533 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
536 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
538 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
542 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
545 return AliMpIntPair(nx,ny);
548 //_____________________________________________________________________________
550 AliMUONCluster::NofPads(Int_t cathode,
551 Int_t statusMask, Bool_t matchMask) const
553 /// Number of pads of a given cathode, satisfying (or not,
554 /// depending matchMask) a given mask
556 Int_t n = Multiplicity(cathode);
559 return AliMpIntPair(0,0);
561 Double_t* x = new Double_t[n];
562 Double_t* y = new Double_t[n];
565 for ( Int_t i = 0; i < Multiplicity(); ++i )
567 AliMUONPad* pad = Pad(i);
568 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
576 Int_t cx = Unique(np,x,0.01);
577 Int_t cy = Unique(np,y,0.01);
582 return AliMpIntPair(cx,cy);
585 //_____________________________________________________________________________
587 AliMUONCluster::Pad(Int_t index) const
589 /// Returns the index-th pad
591 if (!fPads) return 0x0;
592 if ( index < fPads->GetLast()+1 )
594 return static_cast<AliMUONPad*>(fPads->At(index));
598 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
599 fPads->GetLast(),Multiplicity()));
606 //_____________________________________________________________________________
608 AliMUONCluster::Paint(Option_t*)
610 /// Paint this cluster
611 if (!Multiplicity()) return;
613 AliMpArea area(Area());
615 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
617 gVirtualX->SetFillStyle(0);
619 gVirtualX->SetLineColor(2);
620 gVirtualX->SetLineWidth(4);
621 for ( Int_t i = 0; i < Multiplicity(); ++i)
623 AliMUONPad* pad = Pad(i);
624 if ( pad->Cathode() == 0 ) pad->Paint();
627 gVirtualX->SetLineColor(4);
628 gVirtualX->SetLineWidth(2);
629 for ( Int_t i = 0; i < Multiplicity(); ++i)
631 AliMUONPad* pad = Pad(i);
632 if ( pad->Cathode() == 1 ) pad->Paint();
637 //_____________________________________________________________________________
639 AliMUONCluster::DumpMe() const
642 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
643 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
644 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
645 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
648 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
649 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
652 // cout << " " << Area() << endl;
655 for (Int_t i = 0; i < fPads->GetSize(); ++i)
657 cout << Form("fPads[%d]=%x",i,fPads->At(i)) << endl;
658 if ( fPads->At(i) ) fPads->At(i)->Print();
664 //_____________________________________________________________________________
666 AliMUONCluster::Print(Option_t* opt) const
669 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
670 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
671 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
672 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
675 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
676 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
678 cout << " " << Area();
681 TObjArray* a = static_cast<TObjArray*>(fPads->Clone());
688 //_____________________________________________________________________________
690 //AliMUONCluster::IsEqual(const TObject* obj) const
692 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
693 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
695 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
697 // AliMUONPad* p = c->Pad(i);
698 // if ( p->Compare(Pad(i)) ) return kFALSE;
703 //_____________________________________________________________________________
705 AliMUONCluster::Compare(const TObject* obj) const
707 /// Compare two clusters. Comparison is made on position and rawcharge only.
709 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
711 AliMpArea carea(cluster->Area());
712 AliMpArea area(Area());
714 if ( carea.Position().X() > area.Position().X() )
718 else if ( carea.Position().X() < area.Position().X() )
724 if ( carea.Position().Y() > area.Position().Y() )
728 else if ( carea.Position().Y() < area.Position().Y() )
734 if ( cluster->RawCharge() > RawCharge() )
738 else if ( cluster->RawCharge() < RawCharge() )
747 //_____________________________________________________________________________
749 AliMUONCluster::RemovePad(AliMUONPad* pad)
752 /// As a consequence, some internal information must be updated
756 // update cluster's data
757 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
758 fMultiplicity[0]=fMultiplicity[1]=0;
759 fRawCharge[0]=fRawCharge[1]=0;
760 for ( Int_t i = 0; i <= fPads->GetLast(); ++i )
762 AliMUONPad* p = Pad(i);
763 if ( p->IsSaturated() )
765 fIsSaturated[p->Cathode()] = kTRUE;
767 ++fMultiplicity[p->Cathode()];
768 fRawCharge[p->Cathode()] += p->Charge();
772 //_____________________________________________________________________________
774 AliMUONCluster::RawCharge() const
776 /// Returns the raw average charge
777 return (RawCharge(0)+RawCharge(1))/2.0;
780 //_____________________________________________________________________________
782 AliMUONCluster::RawCharge(Int_t cathode) const
784 /// Returns the average charge of a given cathode
785 if ( cathode == 0 || cathode == 1 )
787 return fRawCharge[cathode];
792 //_____________________________________________________________________________
794 AliMUONCluster::RawChargeAsymmetry() const
796 /// Returns the raw charge asymmetry
797 if ( RawCharge() > 0 )
799 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();