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
132 AliMUONCluster c(src);
137 //_____________________________________________________________________________
138 AliMUONCluster::~AliMUONCluster()
140 /// dtor : note that we're owner of our pads
144 //_____________________________________________________________________________
146 AliMUONCluster::Clear(Option_t*)
148 /// Clear our pad array
149 if (fPads) fPads->Clear("C");
152 //_____________________________________________________________________________
154 AliMUONCluster::Contains(const AliMUONPad& pad) const
156 /// Whether this cluster contains the pad
157 if (!fPads) return kFALSE;
158 for ( Int_t i = 0; i < Multiplicity(); ++i )
160 AliMUONPad* p = Pad(i);
161 if ( pad.Compare(p) == 0 ) return kTRUE;
166 //_____________________________________________________________________________
168 AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
170 /// Add all the pads for cluster to this one
171 for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
173 AliMUONPad* p = cluster.Pad(i);
176 AliError("I already got this pad : ");
177 StdoutToAliError(p->Print(););
185 //_____________________________________________________________________________
187 AliMUONCluster::AddPad(const AliMUONPad& pad)
189 /// Add a pad to our pad array, and update some internal information
194 fPads = new TObjArray(10);
195 fPads->SetOwner(kTRUE);
197 AliMUONPad* p = new AliMUONPad(pad);
199 p->SetClusterId(GetUniqueID());
200 Int_t cathode = p->Cathode();
201 ++(fMultiplicity[cathode]);
202 fRawCharge[cathode] += p->Charge();
203 if ( p->IsSaturated() )
205 fIsSaturated[p->Cathode()]=kTRUE;
210 //___________________________________________________________________________
212 AliMUONCluster::AsString() const
214 /// Return a string containing a compact form of the pad list
215 TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
217 for (Int_t i = 0; i < Multiplicity(); ++i )
219 AliMUONPad* p = Pad(i);
220 s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
226 //___________________________________________________________________________
228 AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
230 /// Whether the two clusters overlap
232 static Double_t precision = 1E-4; // cm
233 static TVector2 precisionAdjustment(precision,precision);
235 for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
237 AliMUONPad* p1 = c1.Pad(i1);
239 for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
241 AliMUONPad* p2 = c2.Pad(i2);
242 // Note: we use negative precision numbers, meaning
243 // the area of the pads will be *increased* by these small numbers
244 // prior to check the overlap by the AreOverlapping method,
245 // so pads touching only by the corners will be considered as
247 if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
256 //_____________________________________________________________________________
258 AliMUONCluster::Area() const
260 /// Return the geometrical area covered by this cluster
262 // Start by finding the (x,y) limits of this cluster
263 TVector2 lowerLeft(1E9,1E9);
264 TVector2 upperRight(-1E9,-1E9);
266 for ( Int_t i = 0; i < Multiplicity(); ++i )
268 AliMUONPad* pad = Pad(i);
269 TVector2 ll = pad->Position() - pad->Dimensions();
270 TVector2 ur = pad->Position() + pad->Dimensions();
271 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
272 TMath::Min(ll.Y(),lowerLeft.Y()) );
273 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
274 TMath::Max(ur.Y(),upperRight.Y()) );
277 // then construct the area from those limits
278 return AliMpArea((lowerLeft+upperRight)/2,
279 (upperRight-lowerLeft)/2);
282 //_____________________________________________________________________________
284 AliMUONCluster::Area(Int_t cathode) const
286 /// Return the geometrical area covered by this cluster's pads on
289 // Start by finding the (x,y) limits of this cluster
290 TVector2 lowerLeft(1E9,1E9);
291 TVector2 upperRight(-1E9,-1E9);
293 for ( Int_t i = 0; i < Multiplicity(cathode); ++i )
295 AliMUONPad* pad = Pad(i);
296 TVector2 ll = pad->Position() - pad->Dimensions();
297 TVector2 ur = pad->Position() + pad->Dimensions();
298 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
299 TMath::Min(ll.Y(),lowerLeft.Y()) );
300 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
301 TMath::Max(ur.Y(),upperRight.Y()) );
304 // then construct the area from those limits
305 return AliMpArea((lowerLeft+upperRight)/2,
306 (upperRight-lowerLeft)/2);
309 //_____________________________________________________________________________
311 AliMUONCluster::Cathode() const
313 /// Return the cathode "number" of this cluster :
314 /// 0 if all its pads are on cathode 0
315 /// 1 if all its pads are on cathode 1
316 /// 2 if some pads on cath 0 and some on cath 1
319 if (Multiplicity(0)>0 && Multiplicity(1)>0)
323 else if (Multiplicity(0)>0)
327 else if (Multiplicity(1)>0)
335 //_____________________________________________________________________________
337 AliMUONCluster::Copy(TObject& obj) const
340 /// Copy this cluster to (cluster&)obj
343 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
347 dest.fPads = static_cast<TObjArray*>(fPads->Clone());
349 dest.fHasPosition = fHasPosition;
350 dest.fPosition = fPosition;
351 dest.fPositionError = fPositionError;
352 dest.fHasCharge = fHasCharge;
354 for ( Int_t i = 0; i < 2; ++i )
356 dest.fRawCharge[i] = fRawCharge[i];
357 dest.fCharge[i] = fCharge[i];
358 dest.fMultiplicity[i] = fMultiplicity[i];
359 dest.fIsSaturated[i] = fIsSaturated[i];
363 //_____________________________________________________________________________
365 AliMUONCluster::Charge() const
367 /// Return the average charge over both cathodes
368 return (Charge(0)+Charge(1))/2.0;
371 //_____________________________________________________________________________
373 AliMUONCluster::Charge(Int_t cathode) const
375 /// Returns the charge of a given cathode
376 if ( !fHasCharge ) return RawCharge(cathode);
378 if ( cathode == 0 || cathode == 1 )
380 return fCharge[cathode];
385 //_____________________________________________________________________________
387 AliMUONCluster::ChargeAsymmetry() const
389 /// Returns the charge asymmetry
392 return TMath::Abs(Charge(0)-Charge(1))/Charge();
397 //_____________________________________________________________________________
399 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
401 /// Returns the maximum pad dimensions (half sizes), only considering
402 /// pads matching (or not, depending matchMask) a given mask
404 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
405 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
407 return TVector2( TMath::Max(cath0.X(),cath1.X()),
408 TMath::Max(cath0.Y(),cath1.Y()) );
411 //_____________________________________________________________________________
413 AliMUONCluster::MaxPadDimensions(Int_t cathode,
414 Int_t statusMask, Bool_t matchMask) const
416 /// Returns the maximum pad dimensions (half sizes), only considering
417 /// pads matching (or not, depending matchMask) a given mask, within a
423 for ( Int_t i = 0; i < Multiplicity(); ++i )
425 AliMUONPad* pad = Pad(i);
426 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
428 xmax = TMath::Max(xmax,pad->DX());
429 ymax = TMath::Max(ymax,pad->DY());
432 return TVector2(xmax,ymax);
435 //_____________________________________________________________________________
437 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
439 /// Returns the minimum pad dimensions (half sizes), only considering
440 /// pads matching (or not, depending matchMask) a given mask
442 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
443 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
445 return TVector2( TMath::Min(cath0.X(),cath1.X()),
446 TMath::Min(cath0.Y(),cath1.Y()) );
449 //_____________________________________________________________________________
451 AliMUONCluster::MinPadDimensions(Int_t cathode,
452 Int_t statusMask, Bool_t matchMask) const
454 /// Returns the minimum pad dimensions (half sizes), only considering
455 /// pads matching (or not, depending matchMask) a given mask, within a
461 for ( Int_t i = 0; i < Multiplicity(); ++i )
463 AliMUONPad* pad = Pad(i);
464 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
466 xmin = TMath::Min(xmin,pad->DX());
467 ymin = TMath::Min(ymin,pad->DY());
470 return TVector2(xmin,ymin);
473 //_____________________________________________________________________________
475 AliMUONCluster::Multiplicity() const
477 /// Returns the total number of pads in this cluster
478 return Multiplicity(0)+Multiplicity(1);
481 //_____________________________________________________________________________
483 AliMUONCluster::Multiplicity(Int_t cathode) const
485 /// Returns the number of pads in this cluster, in the given cathode
486 if ( cathode == 0 || cathode == 1 )
488 return fMultiplicity[cathode];
493 //_____________________________________________________________________________
495 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
497 /// Number of pads satisfying (or not, depending matchMask) a
502 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
503 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
505 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
506 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
508 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
510 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
514 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
517 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
519 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
523 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
526 return AliMpIntPair(nx,ny);
529 //_____________________________________________________________________________
531 AliMUONCluster::NofPads(Int_t cathode,
532 Int_t statusMask, Bool_t matchMask) const
534 /// Number of pads of a given cathode, satisfying (or not,
535 /// depending matchMask) a given mask
537 Int_t n = Multiplicity(cathode);
540 return AliMpIntPair(0,0);
542 Double_t* x = new Double_t[n];
543 Double_t* y = new Double_t[n];
546 for ( Int_t i = 0; i < Multiplicity(); ++i )
548 AliMUONPad* pad = Pad(i);
549 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
557 Int_t cx = Unique(np,x,0.01);
558 Int_t cy = Unique(np,y,0.01);
563 return AliMpIntPair(cx,cy);
566 //_____________________________________________________________________________
568 AliMUONCluster::Pad(Int_t index) const
570 /// Returns the index-th pad
572 if (!fPads) return 0x0;
573 if ( index < fPads->GetLast()+1 )
575 return static_cast<AliMUONPad*>(fPads->At(index));
579 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
580 fPads->GetLast(),Multiplicity()));
587 //_____________________________________________________________________________
589 AliMUONCluster::Paint(Option_t*)
591 /// Paint this cluster
592 if (!Multiplicity()) return;
594 AliMpArea area(Area());
596 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
598 gVirtualX->SetFillStyle(0);
600 gVirtualX->SetLineColor(2);
601 gVirtualX->SetLineWidth(4);
602 for ( Int_t i = 0; i < Multiplicity(); ++i)
604 AliMUONPad* pad = Pad(i);
605 if ( pad->Cathode() == 0 ) pad->Paint();
608 gVirtualX->SetLineColor(4);
609 gVirtualX->SetLineWidth(2);
610 for ( Int_t i = 0; i < Multiplicity(); ++i)
612 AliMUONPad* pad = Pad(i);
613 if ( pad->Cathode() == 1 ) pad->Paint();
618 //_____________________________________________________________________________
620 AliMUONCluster::DumpMe() const
623 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
624 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
625 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
626 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
629 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
630 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
633 // cout << " " << Area() << endl;
636 for (Int_t i = 0; i < fPads->GetSize(); ++i)
638 cout << Form("fPads[%d]=%x",i,fPads->At(i)) << endl;
639 if ( fPads->At(i) ) fPads->At(i)->Print();
645 //_____________________________________________________________________________
647 AliMUONCluster::Print(Option_t* opt) const
650 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
651 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
652 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
653 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
656 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
657 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
659 cout << " " << Area();
662 TObjArray* a = static_cast<TObjArray*>(fPads->Clone());
669 //_____________________________________________________________________________
671 //AliMUONCluster::IsEqual(const TObject* obj) const
673 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
674 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
676 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
678 // AliMUONPad* p = c->Pad(i);
679 // if ( p->Compare(Pad(i)) ) return kFALSE;
684 //_____________________________________________________________________________
686 AliMUONCluster::Compare(const TObject* obj) const
688 /// Compare two clusters. Comparison is made on position and rawcharge only.
690 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
692 AliMpArea carea(cluster->Area());
693 AliMpArea area(Area());
695 if ( carea.Position().X() > area.Position().X() )
699 else if ( carea.Position().X() < area.Position().X() )
705 if ( carea.Position().Y() > area.Position().Y() )
709 else if ( carea.Position().Y() < area.Position().Y() )
715 if ( cluster->RawCharge() > RawCharge() )
719 else if ( cluster->RawCharge() < RawCharge() )
728 //_____________________________________________________________________________
730 AliMUONCluster::RemovePad(AliMUONPad* pad)
733 /// As a consequence, some internal information must be updated
737 // update cluster's data
738 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
739 fMultiplicity[0]=fMultiplicity[1]=0;
740 fRawCharge[0]=fRawCharge[1]=0;
741 for ( Int_t i = 0; i <= fPads->GetLast(); ++i )
743 AliMUONPad* p = Pad(i);
744 if ( p->IsSaturated() )
746 fIsSaturated[p->Cathode()] = kTRUE;
748 ++fMultiplicity[p->Cathode()];
749 fRawCharge[p->Cathode()] += p->Charge();
753 //_____________________________________________________________________________
755 AliMUONCluster::RawCharge() const
757 /// Returns the raw average charge
758 return (RawCharge(0)+RawCharge(1))/2.0;
761 //_____________________________________________________________________________
763 AliMUONCluster::RawCharge(Int_t cathode) const
765 /// Returns the average charge of a given cathode
766 if ( cathode == 0 || cathode == 1 )
768 return fRawCharge[cathode];
773 //_____________________________________________________________________________
775 AliMUONCluster::RawChargeAsymmetry() const
777 /// Returns the raw charge asymmetry
778 if ( RawCharge() > 0 )
780 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();