#include "AliMpMotifType.h"
#include "AliMpExMapIterator.h"
#include "AliMpMotifTypePadIterator.h"
#include "AliMpConnection.h"
#include "AliMpConstants.h"
#include "AliMpFiles.h"
#include "AliMpEncodePair.h"
#include "AliLog.h"
#include <TSystem.h>
#include <Riostream.h>
#include <cstdlib>
using std::cout;
using std::endl;
using std::ofstream;
using std::setw;
ClassImp(AliMpMotifType)
const Int_t AliMpMotifType::fgkPadNumForA = 65;
AliMpMotifType::AliMpMotifType(const TString &id)
: TObject(),
fID(id),
fNofPadsX(0),
fNofPadsY(0),
fNofPads(0),
fMaxNofPads(AliMpConstants::ManuNofChannels()),
fConnectionsByLocalIndices(fMaxNofPads*fMaxNofPads),
fConnectionsByManuChannel(fMaxNofPads)
{
fConnectionsByLocalIndices.SetOwner(kTRUE);
fConnectionsByManuChannel.SetOwner(kFALSE);
AliDebug(1,Form("this=%p id=%s",this,id.Data()));
}
AliMpMotifType::AliMpMotifType(TRootIOCtor*)
: TObject(),
fID(""),
fNofPadsX(0),
fNofPadsY(0),
fNofPads(0),
fMaxNofPads(0),
fConnectionsByLocalIndices(),
fConnectionsByManuChannel()
{
AliDebug(1,Form("this=%p",this));
}
AliMpMotifType::AliMpMotifType(const AliMpMotifType& rhs)
: TObject(),
fID(""),
fNofPadsX(0),
fNofPadsY(0),
fNofPads(0),
fMaxNofPads(0),
fConnectionsByLocalIndices(),
fConnectionsByManuChannel()
{
AliDebug(1,Form("this=%p (copy ctor)",this));
rhs.Copy(*this);
}
AliMpMotifType&
AliMpMotifType::operator=(const AliMpMotifType& rhs)
{
TObject::operator=(rhs);
rhs.Copy(*this);
return *this;
}
TObject*
AliMpMotifType::Clone(const char* ) const
{
return new AliMpMotifType(*this);
}
void
AliMpMotifType::Copy(TObject& object) const
{
TObject::Copy(object);
AliMpMotifType& mt = static_cast<AliMpMotifType&>(object);
mt.fID = fID;
mt.fNofPadsX = fNofPadsX;
mt.fNofPadsY = fNofPadsY;
mt.fNofPads = fNofPads;
mt.fMaxNofPads = fMaxNofPads;
mt.fConnectionsByLocalIndices = fConnectionsByLocalIndices;
mt.fConnectionsByManuChannel = fConnectionsByManuChannel;
}
AliMpMotifType::~AliMpMotifType()
{
AliDebug(1,Form("this=%p",this));
}
AliMpVPadIterator* AliMpMotifType::CreateIterator() const
{
return new AliMpMotifTypePadIterator(this);
}
void AliMpMotifType::SetNofPads(Int_t nofPadsX, Int_t nofPadsY)
{
fNofPadsX = nofPadsX;
fNofPadsY = nofPadsY;
}
Int_t AliMpMotifType::PadNum(const TString &padName) const
{
if ( (padName[0]>='A') && (padName[0]<='Z') )
return fgkPadNumForA+padName[0]-'A';
else
return atoi(padName.Data());
}
TString AliMpMotifType::PadName(Int_t padNum) const
{
if (padNum<fgkPadNumForA)
return Form("%d",padNum);
else
return char('A'+padNum-fgkPadNumForA);
}
Bool_t
AliMpMotifType::AddConnection(AliMpConnection* connection)
{
if (!connection) return kFALSE;
Int_t ix = connection->GetLocalIx();
Int_t iy = connection->GetLocalIy();
Int_t manuChannel = connection->GetManuChannel();
if ( ix >=0 && ix < fMaxNofPads &&
iy >=0 && iy < fMaxNofPads &&
manuChannel >= 0 && manuChannel < AliMpConstants::ManuNofChannels())
{
Int_t index = ix + iy*AliMpConstants::ManuNofChannels();
AliMpConnection* c = FindConnectionByLocalIndices(
connection->GetLocalIndices());
if (c)
{
AliError(Form("Connection already exists for ix=%d iy=%d",ix,iy));
return kFALSE;
}
++fNofPads;
fConnectionsByLocalIndices[index] = connection;
fConnectionsByManuChannel[manuChannel] = connection;
connection->SetOwner(this);
return kTRUE;
}
return kFALSE;
}
AliMpConnection*
AliMpMotifType::FindConnectionByPadNum(Int_t padNum) const
{
TIter next(&fConnectionsByManuChannel);
AliMpConnection* connection;
while ( ( connection = static_cast<AliMpConnection*>(next()) ) )
{
if (connection->GetPadNum()==padNum) return connection;
}
return 0x0;
}
AliMpConnection*
AliMpMotifType::FindConnectionByLocalIndices(MpPair_t localIndices) const
{
return FindConnectionByLocalIndices(AliMp::PairFirst(localIndices),
AliMp::PairSecond(localIndices));
}
AliMpConnection*
AliMpMotifType::FindConnectionByLocalIndices(Int_t ix, Int_t iy) const
{
if ( ix < fNofPadsX && iy < fNofPadsY && ix >= 0 && iy >= 0 )
{
Int_t index = ix + iy*fMaxNofPads;
return static_cast<AliMpConnection*>(fConnectionsByLocalIndices.UncheckedAt(index));
}
else
{
return 0x0;
}
}
AliMpConnection*
AliMpMotifType::FindConnectionByGassiNum(Int_t gassiNum) const
{
if ( gassiNum >=0 && gassiNum < fMaxNofPads )
{
return static_cast<AliMpConnection*>(fConnectionsByManuChannel.UncheckedAt(gassiNum));
}
return 0x0;
}
AliMpConnection*
AliMpMotifType::FindConnectionByKaptonNum(Int_t kaptonNum) const
{
TIter next(&fConnectionsByManuChannel);
AliMpConnection* connection;
while ( ( connection = static_cast<AliMpConnection*>(next()) ) )
{
if ( connection && connection->GetKaptonNum()==kaptonNum) return connection;
}
return 0x0;
}
AliMpConnection*
AliMpMotifType::FindConnectionByBergNum(Int_t bergNum) const
{
TIter next(&fConnectionsByManuChannel);
AliMpConnection* connection;
while ( ( connection = static_cast<AliMpConnection*>(next()) ) )
{
if ( connection && connection->GetBergNum()==bergNum) return connection;
}
return 0x0;
}
MpPair_t AliMpMotifType::FindLocalIndicesByConnection(const AliMpConnection* connection) const
{
return connection->GetLocalIndices();
}
MpPair_t AliMpMotifType::FindLocalIndicesByPadNum(Int_t padNum) const
{
AliMpConnection* connection = FindConnectionByPadNum(padNum);
if ( ! connection) return -1;
return connection->GetLocalIndices();
}
MpPair_t AliMpMotifType::FindLocalIndicesByGassiNum(Int_t gassiNum) const
{
AliMpConnection* connection = FindConnectionByGassiNum(gassiNum);
if ( ! connection) return -1;
return connection->GetLocalIndices();
}
MpPair_t AliMpMotifType::FindLocalIndicesByKaptonNum(Int_t kaptonNum) const
{
AliMpConnection* connection = FindConnectionByKaptonNum(kaptonNum);
if ( ! connection) return -1;
return connection->GetLocalIndices();
}
MpPair_t AliMpMotifType::FindLocalIndicesByBergNum(Int_t bergNum) const
{
AliMpConnection* connection = FindConnectionByBergNum(bergNum);
if ( ! connection) return -1;
return connection->GetLocalIndices();
}
Bool_t
AliMpMotifType::HasPadByLocalIndices(MpPair_t localIndices) const
{
return ( FindConnectionByLocalIndices(localIndices) != 0x0 );
}
Bool_t
AliMpMotifType::HasPadByLocalIndices(Int_t localIx, Int_t localIy) const
{
return ( FindConnectionByLocalIndices(localIx, localIy) != 0x0 );
}
Bool_t
AliMpMotifType::HasPadByManuChannel(Int_t manuChannel) const
{
return ( FindConnectionByGassiNum(manuChannel) != 0x0 );
}
void AliMpMotifType::Print(Option_t *option) const
{
switch (option[0]){
case 'N':cout<<"Name mapping";
break;
case 'K':cout<<"Kapton mapping";
break;
case 'B':cout<<"Berg mapping";
break;
case 'G':cout<<"Gassiplex number mapping";
break;
default:cout<<"Pad mapping";
}
cout<<" in the motif "<<fID<<endl;
cout<<"-----------------------------------"<<endl;
for (Int_t j=fNofPadsY-1;j>=0;j--){
for (Int_t i=0;i<fNofPadsX;i++){
AliMpConnection *connexion = FindConnectionByLocalIndices(i,j);
TString str;
if (connexion){
AliDebug(1,Form("i,j=%2d,%2d connexion=%p",i,j,connexion));
switch (option[0]){
case 'N':str=PadName(connexion->GetPadNum());
break;
case 'K':str=Form("%d",connexion->GetKaptonNum());
break;
case 'B':str=Form("%d",connexion->GetBergNum());
break;
case 'G':str=Form("%d",connexion->GetManuChannel());
break;
default:str= Form("%d",connexion->GetPadNum());
}
cout<<setw(2)<<str;
} else cout<<setw(2)<<"--";
cout<<" ";
}
cout<<endl;
}
}
Bool_t
AliMpMotifType::Save() const
{
return Save(fID.Data());
}
Bool_t
AliMpMotifType::Save(const char* motifName) const
{
TString padPosFileName(AliMpFiles::PadPosFileName(motifName));
TString motifTypeFileName(AliMpFiles::MotifFileName(motifName));
Bool_t test = gSystem->AccessPathName(padPosFileName.Data());
if (test==kFALSE)
{
AliError("Cannot overwrite existing padPos file");
return kFALSE;
}
test = gSystem->AccessPathName(motifTypeFileName.Data());
if (test==kFALSE)
{
AliError("Cannot overwrite existing motifType file");
return kFALSE;
}
ofstream padPosFile(padPosFileName.Data());
ofstream motifFile(motifTypeFileName.Data());
motifFile << "# Motif " << motifName << endl
<< "#" << endl
<< "#connecteur_berg kapton padname not_used" << endl
<< "#for slats there's no kapton connector, so it's always 1"
<< " (zero make the reader" << endl
<< "#exit, so it's not a valid value here)." << endl
<< "#" << endl;
for ( Int_t ix = 0; ix < GetNofPadsX(); ++ix )
{
for ( Int_t iy = 0; iy < GetNofPadsY(); ++iy )
{
AliMpConnection* con = FindConnectionByLocalIndices(ix,iy);
if (con)
{
motifFile << con->GetBergNum() << "\t1\t" << con->GetPadNum() << "\t-" << endl;
padPosFile << con->GetPadNum() << "\t" << ix << "\t" << iy << endl;
}
}
}
padPosFile.close();
motifFile.close();
return kTRUE;
}