#include "AliMpRow.h"
#include "AliMpVRowSegment.h"
#include "AliMpVRowSegmentSpecial.h"
#include "AliMpRowSegmentRSpecial.h"
#include "AliMpVMotif.h"
#include "AliMpMotifType.h"
#include "AliMpMotifPosition.h"
#include "AliMpMotifMap.h"
#include "AliMpConstants.h"
#include "AliLog.h"
#include <TMath.h>
#include <Riostream.h>
using std::endl;
ClassImp(AliMpRow)
AliMpRow::AliMpRow(Int_t id, AliMpMotifMap* motifMap)
: AliMpVIndexed(),
fID(id),
fOffsetY(0.),
fSegments(),
fMotifMap(motifMap)
{
}
AliMpRow::AliMpRow()
: AliMpVIndexed(),
fID(0),
fOffsetY(0.),
fSegments(),
fMotifMap(0)
{
}
AliMpRow::~AliMpRow()
{
fSegments.Delete();
}
AliMpVRowSegment* AliMpRow::FindRowSegment(Int_t ix) const
{
for (Int_t i=0; i<GetNofRowSegments(); i++) {
AliMpVRowSegment* segment = GetRowSegment(i);
if (!dynamic_cast<AliMpVRowSegmentSpecial*>(segment) &&
segment->GetHighLimitIx() >= ix)
return segment;
}
return 0;
}
AliMpMotifPosition*
AliMpRow::FindMotifPosition(AliMpVRowSegment* segment, Int_t ix) const
{
if (!segment) return 0;
for (Int_t i=0; i<segment->GetNofMotifs(); i++){
AliMpMotifPosition* motifPosition
= GetMotifMap()->FindMotifPosition(segment->GetMotifPositionId(i));
if(!motifPosition) {
Fatal("FindMotifPosition", "Not found.");
return 0;
}
if (motifPosition->GetHighLimitIx()>=ix)
return motifPosition;
}
return 0;
}
void AliMpRow::SetHighIndicesLimits(Int_t iy)
{
for (Int_t j=0; j<GetNofRowSegments(); j++) {
AliMpVRowSegment* rowSegment = GetRowSegment(j);
rowSegment
->SetHighIndicesLimit(rowSegment->GetHighLimitIx(),iy);
for (Int_t k=0; k<rowSegment->GetNofMotifs(); k++) {
Int_t motifPositionId = rowSegment->GetMotifPositionId(k);
AliMpMotifPosition* motifPosition
= GetMotifMap()->FindMotifPosition(motifPositionId);
motifPosition
->SetHighIndicesLimit(motifPosition->GetHighLimitIx(), iy);
}
}
}
void AliMpRow::CheckEmpty() const
{
if (GetNofRowSegments() == 0)
Fatal("CheckEmpty", "Empty row");
}
void AliMpRow::AddRowSegment(AliMpVRowSegment* rowSegment)
{
fSegments.Add(rowSegment);
}
void AliMpRow::AddRowSegmentInFront(AliMpVRowSegment* rowSegment)
{
fSegments.AddFirst(rowSegment);
}
AliMpVRowSegment* AliMpRow::FindRowSegment(Double_t x) const
{
for (Int_t i=0; i<GetNofRowSegments(); i++) {
AliMpVRowSegment* rs = (AliMpVRowSegment*)fSegments.At(i);
if (x >= rs->LeftBorderX() && x <= rs->RightBorderX())
return rs;
}
return 0;
}
Double_t AliMpRow::LowBorderY() const
{
CheckEmpty();
return fOffsetY - GetRowSegment(0)->HalfSizeY();
}
Double_t AliMpRow::UpperBorderY() const
{
\
CheckEmpty();
return fOffsetY + GetRowSegment(0)->HalfSizeY();
}
AliMpVPadIterator* AliMpRow::CreateIterator() const
{
Fatal("CreateIterator", "Iterator is not implemented.");
return 0;
}
void AliMpRow::SetMotifPositions()
{
CheckEmpty();
for (Int_t j=0; j<GetNofRowSegments(); j++) {
AliMpVRowSegment* rowSegment = GetRowSegment(j);
for (Int_t k=0; k<rowSegment->GetNofMotifs(); k++) {
Int_t motifPositionId = rowSegment->GetMotifPositionId(k);
AliMpVMotif* motif = rowSegment->GetMotif(k);
Double_t posx, posy;
rowSegment->MotifCenter(motifPositionId, posx, posy);
AliMpMotifPosition* motifPosition
= new AliMpMotifPosition(motifPositionId, motif, posx, posy);
motifPosition->SetHighIndicesLimit(0, 0, false);
Bool_t warn = true;
if (dynamic_cast<AliMpVRowSegmentSpecial*>(rowSegment)) warn = false;
Bool_t added = GetMotifMap()->AddMotifPosition(motifPosition, warn);
if (!added) delete motifPosition;
}
}
}
void AliMpRow::SetGlobalIndices(AliMp::Direction constPadSizeDirection,
AliMpRow* rowBefore)
{
Int_t ix = AliMpConstants::StartPadIndex();
Int_t iy = AliMpConstants::StartPadIndex();
for (Int_t j=0; j<GetNofRowSegments(); j++) {
AliMpVRowSegment* rowSegment = GetRowSegment(j);
ix += rowSegment->GetLowLimitIx();
for (Int_t k=0; k<rowSegment->GetNofMotifs(); k++) {
if (rowBefore) {
if (constPadSizeDirection == AliMp::kY) {
iy = rowBefore->GetHighLimitIy()+1;
}
else {
AliMpVRowSegment* seg = rowBefore->FindRowSegment(ix);
AliMpMotifPosition* motPos = FindMotifPosition(seg, ix);
if (!dynamic_cast<AliMpRowSegmentRSpecial*>(rowSegment)) {
if (!motPos) {
Fatal("SetGlobalIndices", "Motif position in rowBefore not found.");
return;
}
iy = motPos->GetHighLimitIy()+1;
}
}
}
ix = rowSegment->SetIndicesToMotifPosition(k, AliMp::Pair(ix, iy));
}
rowSegment->SetGlobalIndices(rowBefore);
}
Int_t ixl = 9999;
Int_t iyl = 9999;
Int_t ixh = AliMpConstants::StartPadIndex();
Int_t iyh = AliMpConstants::StartPadIndex();
for (Int_t i=0; i<GetNofRowSegments(); i++) {
AliMpVRowSegment* rowSegment = GetRowSegment(i);
if ( rowSegment->GetLowLimitIx() < ixl )
ixl = rowSegment->GetLowLimitIx();
if ( rowSegment->GetLowLimitIy() < iyl )
iyl = rowSegment->GetLowLimitIy();
if ( rowSegment->GetHighLimitIx() > ixh )
ixh = rowSegment->GetHighLimitIx();
if ( rowSegment->GetHighLimitIy() > iyh )
iyh = rowSegment->GetHighLimitIy();
}
SetLowIndicesLimit(ixl, iyl);
SetHighIndicesLimit(ixh, iyh);
}
Double_t AliMpRow::GetPositionX() const
{
return ( GetRowSegment(0)->LeftBorderX() +
GetRowSegment(GetNofRowSegments()-1)->RightBorderX() )/2.;
}
Double_t AliMpRow::GetPositionY() const
{
return fOffsetY;
}
Double_t AliMpRow::GetDimensionX() const
{
return ( GetRowSegment(GetNofRowSegments()-1)->RightBorderX() -
GetRowSegment(0)->LeftBorderX() )/2.;
}
Double_t AliMpRow::GetDimensionY() const
{
return GetRowSegment(0)->HalfSizeY();
}
void AliMpRow::SetRowSegmentOffsets(Double_t offsetx)
{
CheckEmpty();
AliMpVRowSegment* previous = 0;
for (Int_t j=0; j<GetNofRowSegments(); j++) {
AliMpVRowSegment* rowSegment = GetRowSegment(j);
Double_t offsetX;
if (previous)
offsetX = previous->RightBorderX();
else
offsetX = offsetx;
rowSegment->SetOffset(offsetX, 0.);
previous = rowSegment;
}
}
Double_t AliMpRow::SetOffsetY(Double_t offsetY)
{
CheckEmpty();
AliMpVRowSegment* first = GetRowSegment(0);
Double_t rowSizeY = first->HalfSizeY();
for (Int_t i=1; i<GetNofRowSegments(); i++) {
Double_t sizeY = GetRowSegment(i)->HalfSizeY();
if (TMath::Abs(sizeY - rowSizeY) >= AliMpConstants::LengthTolerance()) {
Fatal("SetOffsetY", "Motif with different Y size in one row");
return 0.;
}
}
offsetY += rowSizeY ;
fOffsetY = offsetY;
return offsetY += rowSizeY;
}
Int_t AliMpRow::GetNofRowSegments() const
{
return fSegments.GetSize();
}
AliMpVRowSegment* AliMpRow::GetRowSegment(Int_t i) const
{
if (i<0 || i>=GetNofRowSegments()) {
AliWarningStream() << "Index outside range" << endl;
return 0;
}
return (AliMpVRowSegment*)fSegments.At(i);
}