#include <Riostream.h>
#include <TMath.h>
#include <TObjArray.h>
#include <TVirtualPad.h>
#include <TVirtualX.h>
#include "AliMUONCluster.h"
#include "AliMUONPad.h"
#include "AliMpEncodePair.h"
#include "AliLog.h"
using std::cout;
using std::endl;
ClassImp(AliMUONCluster)
namespace
{
Bool_t
ShouldUsePad(const AliMUONPad& pad,
Int_t cathode, Int_t statusMask, Bool_t matchMask)
{
if ( pad.Status() < 0 ) return kFALSE;
if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
{
Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
if ( !statusMask )
{
test = ( pad.Status() == 0 );
}
if ( ( test && matchMask ) || ( !test && !matchMask ) )
{
return kTRUE;
}
}
return kFALSE;
}
Int_t Unique(Int_t n, Double_t* array, Double_t precision)
{
Int_t count(1);
Int_t* index = new Int_t[n];
TMath::Sort(n,array,index);
for ( Int_t i = 1; i < n; ++i )
{
if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
}
delete[] index;
return count;
}
}
AliMUONCluster::AliMUONCluster()
: TObject(),
fPads(),
fHasPosition(kFALSE),
fPosition(1E9,1E9),
fPositionError(1E9,1E9),
fHasCharge(kFALSE),
fChi2(0)
{
fMultiplicity[0]=fMultiplicity[1]=0;
fRawCharge[0]=fRawCharge[1]=0;
fCharge[0]=fCharge[1]=0;
fIsSaturated[0]=fIsSaturated[1]=kFALSE;
fPads.SetOwner(kTRUE);
}
AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
: TObject(src),
fPads(),
fHasPosition(kFALSE),
fPosition(1E9,1E9),
fPositionError(1E9,1E9),
fHasCharge(kFALSE),
fChi2(0)
{
fPads.SetOwner(kTRUE);
src.Copy(*this);
}
AliMUONCluster&
AliMUONCluster::operator=(const AliMUONCluster& src)
{
if ( this != &src )
{
src.Copy(*this);
}
return *this;
}
AliMUONCluster::~AliMUONCluster()
{
}
void
AliMUONCluster::Clear(Option_t*)
{
fPads.Clear();
}
Bool_t
AliMUONCluster::Contains(const AliMUONPad& pad) const
{
if (fPads.IsEmpty()) return kFALSE;
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* p = Pad(i);
if ( pad.Compare(p) == 0 ) return kTRUE;
}
return kFALSE;
}
void
AliMUONCluster::AddCluster(const AliMUONCluster& cluster)
{
for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
{
AliMUONPad* p = cluster.Pad(i);
if ( Contains(*p) )
{
AliError("I already got this pad : ");
StdoutToAliError(p->Print(););
AliFatal("");
}
AddPad(*p);
}
}
AliMUONPad*
AliMUONCluster::AddPad(const AliMUONPad& pad)
{
AliMUONPad* p = new AliMUONPad(pad);
fPads.AddLast(p);
p->SetClusterId(GetUniqueID());
Int_t cathode = p->Cathode();
++(fMultiplicity[cathode]);
fRawCharge[cathode] += p->Charge();
if ( p->IsSaturated() )
{
fIsSaturated[p->Cathode()]=kTRUE;
}
return p;
}
TString
AliMUONCluster::AsString() const
{
TString s(Form("NPADS(%d,%d)",Multiplicity(0),Multiplicity(1)));
for (Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* p = Pad(i);
s += Form(" (%d,%d,%d) ",p->Cathode(),p->Ix(),p->Iy());
}
return s;
}
Bool_t
AliMUONCluster::AreOverlapping(const AliMUONCluster& c1, const AliMUONCluster& c2)
{
static Double_t precision = 1E-4;
static TVector2 precisionAdjustment(precision,precision);
for ( Int_t i1 = 0; i1 < c1.Multiplicity(); ++i1 )
{
AliMUONPad* p1 = c1.Pad(i1);
for ( Int_t i2 = 0; i2 < c2.Multiplicity(); ++i2 )
{
AliMUONPad* p2 = c2.Pad(i2);
if ( AliMUONPad::AreOverlapping(*p1,*p2,precisionAdjustment) )
{
return kTRUE;
}
}
}
return kFALSE;
}
AliMpArea
AliMUONCluster::Area() const
{
TVector2 lowerLeft(1E9,1E9);
TVector2 upperRight(-1E9,-1E9);
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* pad = Pad(i);
TVector2 ll = pad->Position() - pad->Dimensions();
TVector2 ur = pad->Position() + pad->Dimensions();
lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
TMath::Min(ll.Y(),lowerLeft.Y()) );
upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
TMath::Max(ur.Y(),upperRight.Y()) );
}
return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
(upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
}
AliMpArea
AliMUONCluster::Area(Int_t cathode) const
{
TVector2 lowerLeft(1E9,1E9);
TVector2 upperRight(-1E9,-1E9);
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* pad = Pad(i);
if ( pad->Cathode() == cathode )
{
TVector2 ll = pad->Position() - pad->Dimensions();
TVector2 ur = pad->Position() + pad->Dimensions();
lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
TMath::Min(ll.Y(),lowerLeft.Y()) );
upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
TMath::Max(ur.Y(),upperRight.Y()) );
}
}
return AliMpArea((lowerLeft+upperRight).X()/2,(lowerLeft+upperRight).Y()/2,
(upperRight-lowerLeft).X()/2, (upperRight-lowerLeft).Y()/2);
}
Bool_t
AliMUONCluster::IsMonoCathode() const
{
return (Cathode()<2);
}
Int_t
AliMUONCluster::Cathode() const
{
Int_t cathode(-1);
if (Multiplicity(0)>0 && Multiplicity(1)>0)
{
cathode=2;
}
else if (Multiplicity(0)>0)
{
cathode=0;
}
else if (Multiplicity(1)>0)
{
cathode=1;
}
return cathode;
}
void
AliMUONCluster::Copy(TObject& obj) const
{
TObject::Copy(obj);
AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
dest.fPads.Clear();
for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
{
AliMUONPad* p = static_cast<AliMUONPad*>(fPads.UncheckedAt(i));
dest.fPads.AddLast(new AliMUONPad(*p));
}
dest.fHasPosition = fHasPosition;
dest.fPosition = fPosition;
dest.fPositionError = fPositionError;
dest.fHasCharge = fHasCharge;
dest.fChi2 = fChi2;
for ( Int_t i = 0; i < 2; ++i )
{
dest.fRawCharge[i] = fRawCharge[i];
dest.fCharge[i] = fCharge[i];
dest.fMultiplicity[i] = fMultiplicity[i];
dest.fIsSaturated[i] = fIsSaturated[i];
}
}
Float_t
AliMUONCluster::Charge() const
{
if ( Multiplicity(0) && Multiplicity(1) )
{
return (Charge(0)+Charge(1))/2.0;
}
else if ( Multiplicity(0) )
{
return Charge(0);
}
else if ( Multiplicity(1) )
{
return Charge(1);
}
AliError("Should not be here ?!");
return -1.0;
}
Float_t
AliMUONCluster::Charge(Int_t cathode) const
{
if ( !fHasCharge ) return RawCharge(cathode);
if ( cathode == 0 || cathode == 1 )
{
return fCharge[cathode];
}
return 0;
}
Float_t
AliMUONCluster::ChargeAsymmetry() const
{
if ( Charge() > 0 )
{
return TMath::Abs(Charge(0)-Charge(1))/Charge();
}
return 0;
}
TVector2
AliMUONCluster::MaxPadDimensions(Int_t statusMask, Bool_t matchMask) const
{
TVector2 cath0(MaxPadDimensions(0,statusMask,matchMask));
TVector2 cath1(MaxPadDimensions(1,statusMask,matchMask));
return TVector2( TMath::Max(cath0.X(),cath1.X()),
TMath::Max(cath0.Y(),cath1.Y()) );
}
TVector2
AliMUONCluster::MaxPadDimensions(Int_t cathode,
Int_t statusMask, Bool_t matchMask) const
{
Double_t xmax(0);
Double_t ymax(0);
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* pad = Pad(i);
if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
{
xmax = TMath::Max(xmax,pad->DX());
ymax = TMath::Max(ymax,pad->DY());
}
}
return TVector2(xmax,ymax);
}
TVector2
AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
{
TVector2 cath0(MinPadDimensions(0,statusMask,matchMask));
TVector2 cath1(MinPadDimensions(1,statusMask,matchMask));
return TVector2( TMath::Min(cath0.X(),cath1.X()),
TMath::Min(cath0.Y(),cath1.Y()) );
}
TVector2
AliMUONCluster::MinPadDimensions(Int_t cathode,
Int_t statusMask, Bool_t matchMask) const
{
Double_t xmin(1E9);
Double_t ymin(1E9);
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* pad = Pad(i);
if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
{
xmin = TMath::Min(xmin,pad->DX());
ymin = TMath::Min(ymin,pad->DY());
}
}
return TVector2(xmin,ymin);
}
Int_t
AliMUONCluster::Multiplicity() const
{
return Multiplicity(0)+Multiplicity(1);
}
Int_t
AliMUONCluster::Multiplicity(Int_t cathode) const
{
if ( cathode == 0 || cathode == 1 )
{
return fMultiplicity[cathode];
}
return 0;
}
Long_t
AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
{
Int_t nx, ny;
TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
Long_t npad0(NofPads(0,statusMask,matchMask));
Long_t npad1(NofPads(1,statusMask,matchMask));
if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
{
nx = TMath::Max( AliMp::PairFirst(npad0), AliMp::PairFirst(npad1) );
}
else
{
nx = dim0.X() < dim1.X() ? AliMp::PairFirst(npad0) : AliMp::PairFirst(npad1);
}
if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
{
ny = TMath::Max( AliMp::PairSecond(npad0), AliMp::PairSecond(npad1) );
}
else
{
ny = dim0.Y() < dim1.Y() ? AliMp::PairSecond(npad0) : AliMp::PairSecond(npad1);
}
return AliMp::Pair(nx,ny);
}
Long_t
AliMUONCluster::NofPads(Int_t cathode,
Int_t statusMask, Bool_t matchMask) const
{
Int_t n = Multiplicity(cathode);
if (!n)
{
return 0;
}
Double_t* x = new Double_t[n];
Double_t* y = new Double_t[n];
Int_t np(0);
for ( Int_t i = 0; i < Multiplicity(); ++i )
{
AliMUONPad* pad = Pad(i);
if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
{
x[np] = pad->X();
y[np] = pad->Y();
++np;
}
}
Int_t cx = Unique(np,x,0.01);
Int_t cy = Unique(np,y,0.01);
delete[] x;
delete[] y;
return AliMp::Pair(cx,cy);
}
AliMUONPad*
AliMUONCluster::Pad(Int_t index) const
{
if (fPads.IsEmpty()) return 0x0;
if ( index < fPads.GetLast()+1 )
{
return static_cast<AliMUONPad*>(fPads.At(index));
}
else
{
AliError(Form("Requested index %d out of bounds (%d) Mult is %d",index,
fPads.GetLast(),Multiplicity()));
DumpMe();
}
return 0x0;
}
void
AliMUONCluster::Paint(Option_t*)
{
if (!Multiplicity()) return;
AliMpArea area(Area());
gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
gVirtualX->SetFillStyle(0);
gVirtualX->SetLineColor(2);
gVirtualX->SetLineWidth(4);
for ( Int_t i = 0; i < Multiplicity(); ++i)
{
AliMUONPad* pad = Pad(i);
if ( pad->Cathode() == 0 ) pad->Paint();
}
gVirtualX->SetLineColor(4);
gVirtualX->SetLineWidth(2);
for ( Int_t i = 0; i < Multiplicity(); ++i)
{
AliMUONPad* pad = Pad(i);
if ( pad->Cathode() == 1 ) pad->Paint();
}
}
void
AliMUONCluster::DumpMe() const
{
cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
<< "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
<< RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
<< ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
if ( HasPosition() )
{
cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
}
cout << endl;
for (Int_t i = 0; i < fPads.GetSize(); ++i)
{
cout << Form("fPads[%d]=%p",i,fPads.At(i)) << endl;
if ( fPads.At(i) ) fPads.At(i)->Print();
}
}
void
AliMUONCluster::Print(Option_t* opt) const
{
cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity()
<< "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge="
<< RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
<< ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
if ( HasPosition() )
{
cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
}
cout << " " << Area();
TObjArray* a = static_cast<TObjArray*>(fPads.Clone());
a->Sort();
a->Print("",opt);
delete a;
}
Int_t
AliMUONCluster::Compare(const TObject* obj) const
{
const AliMUONCluster* cluster = static_cast<const AliMUONCluster*>(obj);
AliMpArea carea(cluster->Area());
AliMpArea area(Area());
if ( carea.GetPositionX() > area.GetPositionX() )
{
return 1;
}
else if ( carea.GetPositionX() < area.GetPositionX() )
{
return -1;
}
else
{
if ( carea.GetPositionY() > area.GetPositionY() )
{
return 1;
}
else if ( carea.GetPositionY() < area.GetPositionY() )
{
return -1;
}
else
{
if ( cluster->RawCharge() > RawCharge() )
{
return 1;
}
else if ( cluster->RawCharge() < RawCharge() )
{
return -1;
}
}
}
return 0;
}
void
AliMUONCluster::RemovePad(AliMUONPad* pad)
{
fPads.Remove(pad);
fPads.Compress();
delete pad;
fIsSaturated[0]=fIsSaturated[1]=kFALSE;
fMultiplicity[0]=fMultiplicity[1]=0;
fRawCharge[0]=fRawCharge[1]=0;
for ( Int_t i = 0; i <= fPads.GetLast(); ++i )
{
AliMUONPad* p = Pad(i);
if ( p->IsSaturated() )
{
fIsSaturated[p->Cathode()] = kTRUE;
}
++fMultiplicity[p->Cathode()];
fRawCharge[p->Cathode()] += p->Charge();
}
}
Float_t
AliMUONCluster::RawCharge() const
{
return (RawCharge(0)+RawCharge(1))/2.0;
}
Float_t
AliMUONCluster::RawCharge(Int_t cathode) const
{
if ( cathode == 0 || cathode == 1 )
{
return fRawCharge[cathode];
}
return 0;
}
Float_t
AliMUONCluster::RawChargeAsymmetry() const
{
if ( RawCharge() > 0 )
{
return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();
}
return 0;
}