#include "AliESDTOFCluster.h"
#include "TClonesArray.h"
#include "AliESDEvent.h"
ClassImp(AliESDTOFCluster)
AliESDTOFCluster::AliESDTOFCluster(Int_t clID) :
fID(clID),
fNTOFhits(0),
fStatus(0),
fNmatchableTracks(0)
{
for(Int_t i=kMaxHits;i--;) fHitIndex[i] = -1;
for(Int_t i=kMaxMatches;i--;) fMatchIndex[i] = -1;
}
AliESDTOFCluster::AliESDTOFCluster(Int_t ,Int_t ,Float_t ,Float_t ,Float_t ,Int_t*,Int_t ,Int_t ,
Bool_t ,Float_t ,Float_t ,Float_t ,
Int_t ,Float_t ,Float_t ,Float_t ,Float_t ,Double_t*) :
fID(-1),
fNTOFhits(0),
fStatus(1),
fNmatchableTracks(1)
{
for(Int_t i=kMaxHits;i--;) fHitIndex[i] = -1;
for(Int_t i=kMaxMatches;i--;) fMatchIndex[i] = -1;
}
AliESDTOFCluster::AliESDTOFCluster(Int_t ,Int_t ,Float_t ,Float_t ,Float_t ,Int_t*,Int_t ,Int_t ,
Bool_t ,Float_t ,Float_t ,Float_t ) :
fID(-1),
fNTOFhits(0),
fStatus(1),
fNmatchableTracks(0)
{
for(Int_t i=kMaxHits;i--;) fHitIndex[i] = -1;
for(Int_t i=kMaxMatches;i--;) fMatchIndex[i] = -1;
}
AliESDTOFCluster::AliESDTOFCluster(const AliESDTOFCluster & source) :
AliVTOFcluster(source),
fID(source.fID),
fNTOFhits(source.fNTOFhits),
fStatus(source.fStatus),
fNmatchableTracks(source.fNmatchableTracks)
{
SetEvent(source.GetEvent());
for(Int_t i=kMaxHits;i--;) fHitIndex[i] = source.fHitIndex[i];
for(Int_t i=kMaxMatches;i--;) fMatchIndex[i] = source.fMatchIndex[i];
}
AliESDTOFCluster & AliESDTOFCluster::operator=(const AliESDTOFCluster & source)
{
if (this == &source) return *this;
AliVTOFcluster::operator=(source);
fID = source.fID;
fNTOFhits = source.fNTOFhits;
fStatus = source.fStatus;
fNmatchableTracks = source.fNmatchableTracks;
for(Int_t i=kMaxHits;i--;) fHitIndex[i] = source.fHitIndex[i];
for(Int_t i=kMaxMatches;i--;) fMatchIndex[i] = source.fMatchIndex[i];
return *this;
}
Int_t AliESDTOFCluster::Update(Int_t trackIndex,Float_t dX,Float_t dY,Float_t dZ,
Float_t length,Double_t expTimes[AliPID::kSPECIESC])
{
if(fNmatchableTracks >= kMaxMatches) return 2;
for (Int_t ii=fNmatchableTracks; ii--;) if (trackIndex==GetTrackIndex(ii)) return 1;
const AliESDEvent *event = (AliESDEvent *) GetEvent();
TClonesArray *matchAr = event->GetESDTOFMatches();
int ntr = matchAr->GetEntriesFast();
new((*matchAr)[ntr]) AliESDTOFMatch(trackIndex,expTimes,dX,dY,dZ,length);
int nmt = fNmatchableTracks++;
fMatchIndex[nmt] = ntr;
return 0;
}
void AliESDTOFCluster::AddESDTOFHitIndex(Int_t hitID)
{
if(fNTOFhits >= kMaxHits) return;
int nth = fNTOFhits++;
fHitIndex[nth] = hitID;
}
void AliESDTOFCluster::AddTOFhit(AliESDTOFHit *hit)
{
if(fNTOFhits >= kMaxHits) return;
const AliESDEvent *event = (AliESDEvent *) GetEvent();
TClonesArray *hitAr = event->GetESDTOFHits();
int nh = hitAr->GetEntriesFast();
new((*hitAr)[nh]) AliESDTOFHit(*hit);
int nth = fNTOFhits++;
fHitIndex[nth] = nh;
}
void AliESDTOFCluster::AddTOFhit(Int_t ,Int_t ,Float_t ,Float_t ,Float_t ,Int_t*,Int_t ,Int_t , Bool_t ,Float_t ,Float_t ,Float_t )
{
}
Int_t AliESDTOFCluster::GetClusterIndex(int ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetClusterIndex() : -1;
}
Int_t AliESDTOFCluster::GetTOFchannel(int ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetTOFchannel() : -1;
}
Float_t AliESDTOFCluster::GetTime(int ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetTime() : 0;
}
Float_t AliESDTOFCluster::GetTimeRaw(Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetTimeRaw() : 0;
}
Float_t AliESDTOFCluster::GetTOT(Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetTOT() : 0;
}
Float_t AliESDTOFCluster::GetTOFsignalToT(Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetTOT() : 0;
}
Int_t AliESDTOFCluster::GetLabel(Int_t i,Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
if (!hit || i>=3) return -999;
return hit->GetTOFLabel(i);
}
Int_t AliESDTOFCluster::GetDeltaBC(Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetDeltaBC() : 0;
}
Int_t AliESDTOFCluster::GetL0L1Latency(Int_t ihit) const
{
AliESDTOFHit* hit = GetTOFHit(ihit);
return hit ? hit->GetL0L1Latency() : 0;
}
Bool_t AliESDTOFCluster::GetStatus() const
{
if(!fEvent){
AliInfo("No AliESDEvent available here!");
return 0;
}
return fStatus;
}
Float_t AliESDTOFCluster::GetZ() const
{
AliESDTOFHit* hit = GetTOFHit(0);
return hit ? hit->GetZ() : 0;
}
Float_t AliESDTOFCluster::GetPhi() const
{
AliESDTOFHit* hit = GetTOFHit(0);
return hit ? hit->GetPhi() : 0;
}
Float_t AliESDTOFCluster::GetR() const
{
AliESDTOFHit* hit = GetTOFHit(0);
return hit ? hit->GetR() : 0;
}
Int_t AliESDTOFCluster::GetNMatchableTracks() const
{
return fNmatchableTracks;
}
Int_t AliESDTOFCluster::GetNTOFhits() const
{
return fNTOFhits;
}
Int_t AliESDTOFCluster::GetTrackIndex(Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetTrackIndex() : -999;
}
Float_t AliESDTOFCluster::GetDistanceInStripPlane(Int_t i) const
{
AliESDTOFMatch* match = 0;
if (i>=fNmatchableTracks || !(match=GetTOFMatch(i))) return -999;
Double_t dz = match->GetDz();
Double_t dx = match->GetDx();
return TMath::Sqrt(dx*dx+dz*dz);
}
Float_t AliESDTOFCluster::GetDx(Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetDx() : -999;
}
Float_t AliESDTOFCluster::GetDy(Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetDy() : -999;
}
Float_t AliESDTOFCluster::GetDz(Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetDz() : -999;
}
Float_t AliESDTOFCluster::GetLength(Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetTrackLength() : -999.;
}
Double_t AliESDTOFCluster::GetIntegratedTime(Int_t iPart,Int_t i) const
{
AliESDTOFMatch* match = 0;
return (i<fNmatchableTracks && (match=GetTOFMatch(i))) ? match->GetIntegratedTimes(iPart) : -999.;
}
void AliESDTOFCluster::Print(const Option_t*) const
{
printf("#%5d TOF Cluster %5d of %d Hits, %d Matchable Tracks\n",GetESDID(),fID, fNTOFhits, fNmatchableTracks);
for (int ih=0;ih<fNTOFhits;ih++) {
AliESDTOFHit* ht = GetTOFHit(ih);
if (ht) {
printf("%d: ",ih);
ht->Print();
}
}
for (int it=0;it<fNmatchableTracks;it++) {
AliESDTOFMatch* mt = GetTOFMatch(it);
if (mt) {
printf("%d: ",it);
mt->Print();
}
}
}
AliESDTOFHit* AliESDTOFCluster::GetTOFHit(Int_t i) const
{
return fEvent ? ((AliESDTOFHit *) ((AliESDEvent *) GetEvent())->GetESDTOFHits()->At(fHitIndex[i])):0;
}
AliESDTOFMatch* AliESDTOFCluster::GetTOFMatch(Int_t i) const
{
return fEvent ? ((AliESDTOFMatch *) ((AliESDEvent *) GetEvent())->GetESDTOFMatches()->At(fMatchIndex[i])) : 0;
}
void AliESDTOFCluster::SuppressMatchedTrack(Int_t id)
{
for (int it=fNmatchableTracks;it--;) {
AliESDTOFMatch* mtc = GetTOFMatch(it);
if (mtc->GetTrackIndex()!=id) continue;
int rmID = fMatchIndex[it];
for (int jt=it+1;jt<fNmatchableTracks;jt++) fMatchIndex[jt-1] = fMatchIndex[jt];
fNmatchableTracks--;
TClonesArray* arrMatch = ((AliESDEvent *)GetEvent())->GetESDTOFMatches();
int last = arrMatch->GetEntriesFast()-1;
AliESDTOFMatch* mtcL = (AliESDTOFMatch*)arrMatch->At(last);
if (rmID!=last) {
*mtc = *mtcL;
int trID = mtc->GetTrackIndex();
AliESDtrack* trc = ((AliESDEvent *)GetEvent())->GetTrack(trID);
trc->ReplaceTOFMatchID(last,rmID);
}
arrMatch->RemoveAt(last);
break;
}
if (!fNmatchableTracks) {
TClonesArray* arrHits = ((AliESDEvent *)GetEvent())->GetESDTOFHits();
TClonesArray* arrClus = ((AliESDEvent *)GetEvent())->GetESDTOFClusters();
int last = arrHits->GetEntriesFast()-1;
for (;fNTOFhits--;) {
int hID = fHitIndex[int(fNTOFhits)];
AliESDTOFHit* hit = (AliESDTOFHit*)arrHits->At(hID);
AliESDTOFHit* hitL = (AliESDTOFHit*)arrHits->At(last);
if (hID!=last) {
*hit = *hitL;
int clID = hit->GetESDTOFClusterIndex();
AliESDTOFCluster* clusL = (AliESDTOFCluster*)arrClus->At(clID);
clusL->ReplaceHitID(last,hID);
}
arrHits->RemoveAt(last--);
}
}
}
void AliESDTOFCluster::ReplaceHitID(int oldID, int newID)
{
for (int it=fNTOFhits;it--;) {
if (fHitIndex[it]==oldID) {
fHitIndex[it]=newID;
return;
}
}
}
void AliESDTOFCluster::ReplaceMatchID(int oldID, int newID)
{
for (int it=fNmatchableTracks;it--;) {
if (fMatchIndex[it]==oldID) {
fMatchIndex[it]=newID;
return;
}
}
}
void AliESDTOFCluster::ReplaceMatchedTrackID(int oldID, int newID)
{
for (int it=fNmatchableTracks;it--;) {
AliESDTOFMatch* mtc = GetTOFMatch(it);
if (mtc->GetTrackIndex()!=oldID) continue;
mtc->SetTrackIndex(newID);
break;
}
}
void AliESDTOFCluster::FixSelfReferences(int oldID, int newID)
{
for (int it=fNmatchableTracks;it--;) {
int trID = GetTOFMatch(it)->GetTrackIndex();
AliESDtrack* trc = ((AliESDEvent *)GetEvent())->GetTrack(trID);
trc->ReplaceTOFClusterID(oldID,newID);
}
for (int it=fNTOFhits;it--;) {
AliESDTOFHit* hit = GetTOFHit(it);
if (hit) hit->SetESDTOFClusterIndex(newID);
}
}