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(); ++i )
295 AliMUONPad* pad = Pad(i);
296 if ( pad->Cathode() == cathode )
298 TVector2 ll = pad->Position() - pad->Dimensions();
299 TVector2 ur = pad->Position() + pad->Dimensions();
300 lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
301 TMath::Min(ll.Y(),lowerLeft.Y()) );
302 upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
303 TMath::Max(ur.Y(),upperRight.Y()) );
307 // then construct the area from those limits
308 return AliMpArea((lowerLeft+upperRight)/2,
309 (upperRight-lowerLeft)/2);
312 //_____________________________________________________________________________
314 AliMUONCluster::Cathode() const
316 /// Return the cathode "number" of this cluster :
317 /// 0 if all its pads are on cathode 0
318 /// 1 if all its pads are on cathode 1
319 /// 2 if some pads on cath 0 and some on cath 1
322 if (Multiplicity(0)>0 && Multiplicity(1)>0)
326 else if (Multiplicity(0)>0)
330 else if (Multiplicity(1)>0)
338 //_____________________________________________________________________________
340 AliMUONCluster::Copy(TObject& obj) const
343 /// Copy this cluster to (cluster&)obj
346 AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
350 dest.fPads = static_cast<TObjArray*>(fPads->Clone());
352 dest.fHasPosition = fHasPosition;
353 dest.fPosition = fPosition;
354 dest.fPositionError = fPositionError;
355 dest.fHasCharge = fHasCharge;
357 for ( Int_t i = 0; i < 2; ++i )
359 dest.fRawCharge[i] = fRawCharge[i];
360 dest.fCharge[i] = fCharge[i];
361 dest.fMultiplicity[i] = fMultiplicity[i];
362 dest.fIsSaturated[i] = fIsSaturated[i];
366 //_____________________________________________________________________________
368 AliMUONCluster::Charge() const
370 /// Return the average charge over both cathodes
372 if ( Multiplicity(0) && Multiplicity(1) )
374 return (Charge(0)+Charge(1))/2.0;
376 else if ( Multiplicity(0) )
380 else if ( Multiplicity(1) )
384 AliError("Should not be here ?!");
388 //_____________________________________________________________________________
390 AliMUONCluster::Charge(Int_t cathode) const
392 /// Returns the charge of a given cathode
393 if ( !fHasCharge ) return RawCharge(cathode);
395 if ( cathode == 0 || cathode == 1 )
397 return fCharge[cathode];
402 //_____________________________________________________________________________
404 AliMUONCluster::ChargeAsymmetry() const
406 /// Returns the charge asymmetry
409 return TMath::Abs(Charge(0)-Charge(1))/Charge();
414 //_____________________________________________________________________________
416 AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
418 /// Returns the maximum pad dimensions (half sizes), only considering
419 /// pads matching (or not, depending matchMask) a given mask
421 TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
422 TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
424 return TVector2( TMath::Max(cath0.X(),cath1.X()),
425 TMath::Max(cath0.Y(),cath1.Y()) );
428 //_____________________________________________________________________________
430 AliMUONCluster::MaxPadDimensions(Int_t cathode,
431 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, within a
440 for ( Int_t i = 0; i < Multiplicity(); ++i )
442 AliMUONPad* pad = Pad(i);
443 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
445 xmax = TMath::Max(xmax,pad->DX());
446 ymax = TMath::Max(ymax,pad->DY());
449 return TVector2(xmax,ymax);
452 //_____________________________________________________________________________
454 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
456 /// Returns the minimum pad dimensions (half sizes), only considering
457 /// pads matching (or not, depending matchMask) a given mask
459 TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
460 TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
462 return TVector2( TMath::Min(cath0.X(),cath1.X()),
463 TMath::Min(cath0.Y(),cath1.Y()) );
466 //_____________________________________________________________________________
468 AliMUONCluster::MinPadDimensions(Int_t cathode,
469 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, within a
478 for ( Int_t i = 0; i < Multiplicity(); ++i )
480 AliMUONPad* pad = Pad(i);
481 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
483 xmin = TMath::Min(xmin,pad->DX());
484 ymin = TMath::Min(ymin,pad->DY());
487 return TVector2(xmin,ymin);
490 //_____________________________________________________________________________
492 AliMUONCluster::Multiplicity() const
494 /// Returns the total number of pads in this cluster
495 return Multiplicity(0)+Multiplicity(1);
498 //_____________________________________________________________________________
500 AliMUONCluster::Multiplicity(Int_t cathode) const
502 /// Returns the number of pads in this cluster, in the given cathode
503 if ( cathode == 0 || cathode == 1 )
505 return fMultiplicity[cathode];
510 //_____________________________________________________________________________
512 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
514 /// Number of pads satisfying (or not, depending matchMask) a
519 TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
520 TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
522 AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
523 AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
525 if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
527 nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
531 nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
534 if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
536 ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
540 ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
543 return AliMpIntPair(nx,ny);
546 //_____________________________________________________________________________
548 AliMUONCluster::NofPads(Int_t cathode,
549 Int_t statusMask, Bool_t matchMask) const
551 /// Number of pads of a given cathode, satisfying (or not,
552 /// depending matchMask) a given mask
554 Int_t n = Multiplicity(cathode);
557 return AliMpIntPair(0,0);
559 Double_t* x = new Double_t[n];
560 Double_t* y = new Double_t[n];
563 for ( Int_t i = 0; i < Multiplicity(); ++i )
565 AliMUONPad* pad = Pad(i);
566 if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
574 Int_t cx = Unique(np,x,0.01);
575 Int_t cy = Unique(np,y,0.01);
580 return AliMpIntPair(cx,cy);
583 //_____________________________________________________________________________
585 AliMUONCluster::Pad(Int_t index) const
587 /// Returns the index-th pad
589 if (!fPads) return 0x0;
590 if ( index < fPads->GetLast()+1 )
592 return static_cast<AliMUONPad*>(fPads->At(index));
596 AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
597 fPads->GetLast(),Multiplicity()));
604 //_____________________________________________________________________________
606 AliMUONCluster::Paint(Option_t*)
608 /// Paint this cluster
609 if (!Multiplicity()) return;
611 AliMpArea area(Area());
613 gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
615 gVirtualX->SetFillStyle(0);
617 gVirtualX->SetLineColor(2);
618 gVirtualX->SetLineWidth(4);
619 for ( Int_t i = 0; i < Multiplicity(); ++i)
621 AliMUONPad* pad = Pad(i);
622 if ( pad->Cathode() == 0 ) pad->Paint();
625 gVirtualX->SetLineColor(4);
626 gVirtualX->SetLineWidth(2);
627 for ( Int_t i = 0; i < Multiplicity(); ++i)
629 AliMUONPad* pad = Pad(i);
630 if ( pad->Cathode() == 1 ) pad->Paint();
635 //_____________________________________________________________________________
637 AliMUONCluster::DumpMe() const
640 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
641 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
642 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
643 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
646 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
647 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
650 // cout << " " << Area() << endl;
653 for (Int_t i = 0; i < fPads->GetSize(); ++i)
655 cout << Form("fPads[%d]=%x",i,fPads->At(i)) << endl;
656 if ( fPads->At(i) ) fPads->At(i)->Print();
662 //_____________________________________________________________________________
664 AliMUONCluster::Print(Option_t* opt) const
667 cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
668 << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
669 << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
670 << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
673 cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
674 cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
676 cout << " " << Area();
679 TObjArray* a = static_cast<TObjArray*>(fPads->Clone());
686 //_____________________________________________________________________________
688 //AliMUONCluster::IsEqual(const TObject* obj) const
690 // const AliMUONCluster* c = static_cast<const AliMUONCluster*>(obj);
691 // if ( c->Multiplicity() != Multiplicity() ) return kFALSE;
693 // for ( Int_t i = 0; i < c->Multiplicity(); ++i )
695 // AliMUONPad* p = c->Pad(i);
696 // if ( p->Compare(Pad(i)) ) return kFALSE;
701 //_____________________________________________________________________________
703 AliMUONCluster::Compare(const TObject* obj) const
705 /// Compare two clusters. Comparison is made on position and rawcharge only.
707 const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
709 AliMpArea carea(cluster->Area());
710 AliMpArea area(Area());
712 if ( carea.Position().X() > area.Position().X() )
716 else if ( carea.Position().X() < area.Position().X() )
722 if ( carea.Position().Y() > area.Position().Y() )
726 else if ( carea.Position().Y() < area.Position().Y() )
732 if ( cluster->RawCharge() > RawCharge() )
736 else if ( cluster->RawCharge() < RawCharge() )
745 //_____________________________________________________________________________
747 AliMUONCluster::RemovePad(AliMUONPad* pad)
750 /// As a consequence, some internal information must be updated
754 // update cluster's data
755 fIsSaturated[0]=fIsSaturated[1]=kFALSE;
756 fMultiplicity[0]=fMultiplicity[1]=0;
757 fRawCharge[0]=fRawCharge[1]=0;
758 for ( Int_t i = 0; i <= fPads->GetLast(); ++i )
760 AliMUONPad* p = Pad(i);
761 if ( p->IsSaturated() )
763 fIsSaturated[p->Cathode()] = kTRUE;
765 ++fMultiplicity[p->Cathode()];
766 fRawCharge[p->Cathode()] += p->Charge();
770 //_____________________________________________________________________________
772 AliMUONCluster::RawCharge() const
774 /// Returns the raw average charge
775 return (RawCharge(0)+RawCharge(1))/2.0;
778 //_____________________________________________________________________________
780 AliMUONCluster::RawCharge(Int_t cathode) const
782 /// Returns the average charge of a given cathode
783 if ( cathode == 0 || cathode == 1 )
785 return fRawCharge[cathode];
790 //_____________________________________________________________________________
792 AliMUONCluster::RawChargeAsymmetry() const
794 /// Returns the raw charge asymmetry
795 if ( RawCharge() > 0 )
797 return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();