Skip to content

Commit

Permalink
store store as IComponent. re #9133
Browse files Browse the repository at this point in the history
and added unit test to test ok
  • Loading branch information
Anders-Markvardsen committed Mar 28, 2014
1 parent 3c6f26c commit 2db727f
Show file tree
Hide file tree
Showing 31 changed files with 74 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Expand Up @@ -750,7 +750,7 @@ namespace Mantid
{
Instrument_const_sptr instrument = getInstrument();

Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr sample = instrument->getSample();
if ( source == NULL || sample == NULL )
{
Expand All @@ -776,7 +776,7 @@ namespace Mantid
*/
double MatrixWorkspace::detectorTwoTheta(Geometry::IDetector_const_sptr det) const
{
Geometry::IObjComponent_const_sptr source = getInstrument()->getSource();
Geometry::IComponent_const_sptr source = getInstrument()->getSource();
Geometry::IComponent_const_sptr sample = getInstrument()->getSample();
if ( source == NULL || sample == NULL )
{
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Algorithms/src/AddPeak.cpp
Expand Up @@ -75,7 +75,7 @@ namespace Algorithms
const double count = getProperty("BinCount");

Mantid::Geometry::Instrument_const_sptr instr = runWS->getInstrument();
Mantid::Geometry::IObjComponent_const_sptr source = instr->getSource();
Mantid::Geometry::IComponent_const_sptr source = instr->getSource();
Mantid::Geometry::IComponent_const_sptr sample = instr->getSample();
Mantid::Geometry::IDetector_const_sptr det = instr->getDetector(detID);

Expand Down
Expand Up @@ -86,7 +86,7 @@ namespace Algorithms
{
Kernel::Unit_sptr fromUnit = inputWS->getAxis(0)->unit();
Kernel::Unit_sptr toUnit = UnitFactory::Instance().create(unitTarget);
IObjComponent_const_sptr source = inputWS->getInstrument()->getSource();
IComponent_const_sptr source = inputWS->getInstrument()->getSource();
IComponent_const_sptr sample = inputWS->getInstrument()->getSample();
std::vector<double> emptyVector;
const double l1 = source->getDistance(*sample);
Expand Down
Expand Up @@ -134,7 +134,7 @@ namespace Algorithms

void ConvertSpectrumAxis2::createElasticQMap(const std::string & targetUnit)
{
IObjComponent_const_sptr source = m_inputWS->getInstrument()->getSource();
IComponent_const_sptr source = m_inputWS->getInstrument()->getSource();
IComponent_const_sptr sample = m_inputWS->getInstrument()->getSample();

const std::string emodeStr = getProperty("EMode");
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Algorithms/src/ConvertUnits.cpp
Expand Up @@ -394,7 +394,7 @@ void ConvertUnits::convertViaTOF(Kernel::Unit_const_sptr fromUnit, API::MatrixWo
Kernel::Unit_const_sptr outputUnit = outputWS->getAxis(0)->unit();

// Get the distance between the source and the sample (assume in metres)
IObjComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr sample = instrument->getSample();
if ( source == NULL || sample == NULL )
{
Expand Down
Expand Up @@ -124,7 +124,7 @@ namespace Algorithms
}
V3D samplepos = sample->getPos();

IObjComponent_const_sptr source = m_instrument->getSource();
IComponent_const_sptr source = m_instrument->getSource();
if (!source)
{
throw runtime_error("No source has been set.");
Expand Down
Expand Up @@ -409,7 +409,7 @@ namespace Algorithms
outfile << "# "<< DateAndTime::getCurrentTime().toFormattedString("%c") <<"\n";
outfile << "#\n";
outfile << "6 L1 T0_SHIFT\n";
IObjComponent_const_sptr source = inst->getSource();
IComponent_const_sptr source = inst->getSource();
IComponent_const_sptr sample = inst->getSample();
outfile << "7 "<<source->getDistance(*sample)*100<<" 0\n";
outfile << "4 DETNUM NROWS NCOLS WIDTH HEIGHT DEPTH DETD CenterX CenterY CenterZ BaseX BaseY BaseZ UpX UpY UpZ\n";
Expand Down
Expand Up @@ -207,7 +207,7 @@ namespace Algorithms
g_log.error(errmsg);
throw std::runtime_error(errmsg);
}
Geometry::IObjComponent_const_sptr source = originstrument->getSource();
Geometry::IComponent_const_sptr source = originstrument->getSource();
Geometry::IComponent_const_sptr sample = originstrument->getSample();
l1 = source->getDistance(*sample);
g_log.information() << "Retrieve L1 from input data workspace. \n";
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Algorithms/src/GetEi.cpp
Expand Up @@ -151,7 +151,7 @@ void GetEi::exec()
*/
void GetEi::getGeometry(API::MatrixWorkspace_const_sptr WS, specid_t mon0Spec, specid_t mon1Spec, double &monitor0Dist, double &monitor1Dist) const
{
const IObjComponent_const_sptr source = WS->getInstrument()->getSource();
const IComponent_const_sptr source = WS->getInstrument()->getSource();

// retrieve a pointer to the first detector and get its distance
size_t monWI = 0;
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Algorithms/src/GetEi2.cpp
Expand Up @@ -295,7 +295,7 @@ double GetEi2::getDistanceFromSource(size_t ws_index) const
{
g_log.debug() << "Computing distance between spectrum at index '" << ws_index << "' and the source\n";

const IObjComponent_const_sptr source = m_input_ws->getInstrument()->getSource();
const IComponent_const_sptr source = m_input_ws->getInstrument()->getSource();
// Retrieve a pointer detector
IDetector_const_sptr det = m_input_ws->getDetector(ws_index);
if( !det )
Expand Down
Expand Up @@ -144,7 +144,7 @@ void MultipleScatteringCylinderAbsorption::exec()
Instrument_const_sptr instrument = in_WS->getInstrument();
if (instrument == NULL)
throw std::runtime_error("Failed to find instrument attached to InputWorkspace");
IObjComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr sample = instrument->getSample();
if (source == NULL)
throw std::runtime_error("Failed to find source in the instrument for InputWorkspace");
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Algorithms/src/SofQW.cpp
Expand Up @@ -114,7 +114,7 @@ void SofQW::exec()
Instrument_const_sptr instrument = inputWorkspace->getInstrument();

// Get the distance between the source and the sample (assume in metres)
IObjComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr sample = instrument->getSample();
V3D beamDir = sample->getPos() - source->getPos();
beamDir.normalize();
Expand Down
Expand Up @@ -197,7 +197,7 @@ namespace Crystal
*
*/
static void updateSourceParams(
boost::shared_ptr<const Geometry::IObjComponent> bank_const,
boost::shared_ptr<const Geometry::IComponent> bank_const,
boost::shared_ptr<Geometry::ParameterMap> pmap,
boost::shared_ptr<const Geometry::ParameterMap> pmapSv);

Expand Down
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/Crystal/src/SCDCalibratePanels.cpp
Expand Up @@ -1145,7 +1145,7 @@ namespace Mantid
double mL1;
stringstream(line) >> count >> mL1 >> T0;
double scaleL0= .01*mL1/beamlineLen;
const IObjComponent_const_sptr source=instrument->getSource();
const IComponent_const_sptr source=instrument->getSource();
V3D NewSourcePos= samplePos-beamline*scaleL0*2.0;//beamLine is 2*length.
L0=beamline.norm()*scaleL0*2.0;
V3D RelSourcePos = source->getRelativePos()+NewSourcePos-source->getPos();
Expand Down Expand Up @@ -1617,7 +1617,7 @@ namespace Mantid
}


void SCDCalibratePanels::updateSourceParams(boost::shared_ptr<const Geometry::IObjComponent> bank_const,
void SCDCalibratePanels::updateSourceParams(boost::shared_ptr<const Geometry::IComponent> bank_const,
boost::shared_ptr<Geometry::ParameterMap> pmap, boost::shared_ptr<const Geometry::ParameterMap> pmapSv)
{
vector< V3D > posv = pmapSv->getV3D(bank_const->getName(), "pos");
Expand All @@ -1642,7 +1642,7 @@ namespace Mantid
boost::shared_ptr<const ParameterMap> const pmapOld)
{
boost::shared_ptr<ParameterMap> pmap = NewInstrument->getParameterMap();
IObjComponent_const_sptr source = NewInstrument->getSource();
IComponent_const_sptr source = NewInstrument->getSource();
updateSourceParams(source, pmap, pmapOld);

IComponent_const_sptr sample = NewInstrument->getSample();
Expand Down Expand Up @@ -1801,7 +1801,7 @@ namespace Mantid
} // for each group

// write out the source
IObjComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr source = instrument->getSource();

oss3 << "<component-link name=\"" << source->getName() << "\">" << endl;
IComponent_const_sptr sample = instrument->getSample();
Expand Down
Expand Up @@ -194,7 +194,7 @@ namespace DataHandling
// Get the sample (needed to calculate distances)
Geometry::IComponent_const_sptr sample = instrument->getSample();
// Get the source (moderator)
Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();

// Open the NeXus file
::NeXus::File nxfile(m_filename, NXACC_RDWR);
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/src/SaveFocusedXYE.cpp
Expand Up @@ -333,7 +333,7 @@ void SaveFocusedXYE::getFocusedPos(Mantid::API::MatrixWorkspace_const_sptr wksp,
tth = 0.;
return;
}
Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr sample = instrument->getSample();
if (source == NULL || sample == NULL)
{
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/DataHandling/src/SaveGSS.cpp
Expand Up @@ -93,7 +93,7 @@ namespace Mantid
tth = 0.;
return;
}
Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr sample = instrument->getSample();
if (source == NULL || sample == NULL)
{
Expand Down Expand Up @@ -143,7 +143,7 @@ namespace Mantid
Progress p(this, 0.0, 1.0, nHist);
double l1, l2, tth;
Geometry::Instrument_const_sptr instrument = inputWS->getInstrument();
Geometry::IObjComponent_const_sptr source;
Geometry::IComponent_const_sptr source;
Geometry::IComponent_const_sptr sample;
if (instrument != NULL)
{
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/DataHandling/test/LoadInstrumentTest.h
Expand Up @@ -178,7 +178,7 @@ class LoadInstrumentTest : public CxxTest::TestSuite
void evaluate_GEM(MatrixWorkspace_sptr output)
{
boost::shared_ptr<const Instrument> i = output->getInstrument();
boost::shared_ptr<const IObjComponent> source = i->getSource();
boost::shared_ptr<const IComponent> source = i->getSource();
TS_ASSERT_EQUALS( source->getName(), "undulator");
TS_ASSERT_DELTA( source->getPos().Z(), -17.0,0.01);

Expand Down Expand Up @@ -292,7 +292,7 @@ class LoadInstrumentTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName));

boost::shared_ptr<const Instrument> i = output->getInstrument();
boost::shared_ptr<const IObjComponent> source = i->getSource();
boost::shared_ptr<const IComponent> source = i->getSource();
TS_ASSERT_EQUALS( source->getName(), "undulator");
TS_ASSERT_DELTA( source->getPos().Z(), -11.016,0.01);

Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataObjects/src/Peak.cpp
Expand Up @@ -319,7 +319,7 @@ namespace DataObjects
if (!inst) throw std::runtime_error("Peak::setInstrument(): No instrument is set!");

// Cache some positions
const Geometry::IObjComponent_const_sptr sourceObj = m_inst->getSource();
const Geometry::IComponent_const_sptr sourceObj = m_inst->getSource();
if (sourceObj == NULL)
throw Exception::InstrumentDefinitionError("Peak::setInstrument(): Failed to get source component from instrument");
const Geometry::IComponent_const_sptr sampleObj = m_inst->getSample();
Expand Down
Expand Up @@ -75,7 +75,7 @@ namespace Mantid

Instrument* clone() const;

IObjComponent_const_sptr getSource() const;
IComponent_const_sptr getSource() const;
IObjComponent_const_sptr getChopperPoint(const size_t index = 0) const;
size_t getNumberOfChopperPoints() const;
IComponent_const_sptr getSample() const;
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace Mantid
/// mark a Component which has already been added to the Instrument (as a child comp.)
/// to be 'the' source Component. For now it is assumed that we have
/// at most one of these.
void markAsSource(const ObjComponent*);
void markAsSource(const IComponent*);

/// mark a Component which has already been added to the Instrument (as a child comp.)
/// to be a Detector component by adding it to _detectorCache
Expand Down Expand Up @@ -245,7 +245,7 @@ namespace Mantid
std::map<detid_t, IDetector_const_sptr > m_detectorCache;

/// Purpose to hold copy of source component. For now assumed to be just one component
const ObjComponent* m_sourceCache;
const IComponent* m_sourceCache;

/// Hold a list of places where a chopper can be situated
/// A pointer so that parameterized intruments are still fast to create.
Expand Down
26 changes: 19 additions & 7 deletions Code/Mantid/Framework/Geometry/src/Instrument.cpp
Expand Up @@ -327,20 +327,32 @@ namespace Mantid
/** Gets a pointer to the source
* @returns a pointer to the source
*/
IObjComponent_const_sptr Instrument::getSource() const
IComponent_const_sptr Instrument::getSource() const
{
if ( !m_sourceCache )
{
g_log.warning("In Instrument::getSource(). No source has been set.");
return IObjComponent_const_sptr(m_sourceCache,NoDeleting());
return IComponent_const_sptr(m_sourceCache,NoDeleting());
}
else if (m_isParametrized)
{
return IObjComponent_const_sptr(new ObjComponent(static_cast<const Instrument*>(m_base)->m_sourceCache,m_map));
auto sourceCache = static_cast<const Instrument*>(m_base)->m_sourceCache;
if ( dynamic_cast<const ObjComponent*>(sourceCache) )
return IComponent_const_sptr(new ObjComponent(sourceCache,m_map));
else if ( dynamic_cast<const CompAssembly*>(sourceCache) )
return IComponent_const_sptr(new CompAssembly(sourceCache,m_map));
else if ( dynamic_cast<const Component*>(sourceCache) )
return IComponent_const_sptr(new Component(sourceCache,m_map));
else
{
g_log.error("In Instrument::getSource(). Source is not a recognised component type.");
g_log.error("Try to assume it is a Component.");
return IComponent_const_sptr(new ObjComponent(sourceCache,m_map));
}
}
else
{
return IObjComponent_const_sptr(m_sourceCache,NoDeleting());
return IComponent_const_sptr(m_sourceCache,NoDeleting());
}
}

Expand Down Expand Up @@ -651,7 +663,7 @@ namespace Mantid
{
throw std::invalid_argument("Instrument::markAsChopper - Chopper component must have a name");
}
IObjComponent_const_sptr source = getSource();
IComponent_const_sptr source = getSource();
if(!source)
{
throw Exception::InstrumentDefinitionError("Instrument::markAsChopper - No source is set, cannot defined chopper positions.");
Expand Down Expand Up @@ -702,7 +714,7 @@ namespace Mantid
*
* @param comp :: Component to be marked (stored for later retrieval) as a "source" Component
*/
void Instrument::markAsSource(const ObjComponent* comp)
void Instrument::markAsSource(const IComponent* comp)
{
if (m_isParametrized)
throw std::runtime_error("Instrument::markAsSource() called on a parametrized Instrument object.");
Expand Down Expand Up @@ -1022,7 +1034,7 @@ namespace Mantid
double & beamline_norm, Kernel::V3D & samplePos) const
{
// Get some positions
const IObjComponent_const_sptr sourceObj = this->getSource();
const IComponent_const_sptr sourceObj = this->getSource();
if (sourceObj == NULL)
{
throw Exception::InstrumentDefinitionError("Failed to get source component from instrument");
Expand Down
Expand Up @@ -1015,6 +1015,10 @@ namespace Geometry
{
m_instrument->markAsSamplePos(ass);
}
if ( category.compare("Source") == 0 || category.compare("source") == 0 )
{
m_instrument->markAsSource(ass);
}

// If enabled, check for a 'neutronic position' tag and add to cache if found
if ( m_indirectPositions )
Expand Down
Expand Up @@ -159,7 +159,7 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite
TS_FAIL("Cannot find expected .vtp file next to " + filename);
}

boost::shared_ptr<const IObjComponent> source = i->getSource();
boost::shared_ptr<const IObjComponent> source = boost::dynamic_pointer_cast<const IObjComponent>(i->getSource());
TS_ASSERT_EQUALS( source->getName(), "undulator");
TS_ASSERT_DELTA( source->getPos().Z(), -17.0,0.01);

Expand Down Expand Up @@ -445,6 +445,11 @@ class InstrumentDefinitionParserTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS( sample->getName(), "nickel-holder");
TS_ASSERT_DELTA( sample->getPos().X(), 2.0,0.01);

// test source
boost::shared_ptr<const IComponent> source = i->getSource();
TS_ASSERT_EQUALS( source->getName(), "undulator");
TS_ASSERT_DELTA( source->getPos().Z(), -95.0,0.01);

// Test of monitor shape
boost::shared_ptr<const IDetector> ptrMonShape = i->getDetector(1001);
TS_ASSERT( ptrMonShape->isValid(V3D(0.002,0.0,0.0)+ptrMonShape->getPos()) );
Expand Down
Expand Up @@ -253,7 +253,7 @@ namespace Mantid
Geometry::Instrument_const_sptr instrument = inputWS->getInstrument();
//this->pBaseInstr = instrument->baseInstrument();
//
Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr sample = instrument->getSample();
if ((!source) || (!sample))
{
Expand Down
Expand Up @@ -154,7 +154,7 @@ namespace Mantid
m_up = refFrame->pointingUp();
m_horiz = refFrame->pointingHorizontal();

IObjComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr source = instrument->getSource();
IComponent_const_sptr sample = instrument->getSample();
IComponent_const_sptr aperture = instrument->getComponentByName("aperture", 1);
if(!aperture)
Expand Down
Expand Up @@ -12,6 +12,7 @@ namespace Mantid
{
using Geometry::Instrument_const_sptr;
using Geometry::IObjComponent_const_sptr;
using Geometry::IComponent_const_sptr;
using Geometry::IDetector_const_sptr;

/**
Expand Down Expand Up @@ -78,7 +79,7 @@ namespace Mantid
void ModeratorChopperResolution::initCaches()
{
Instrument_const_sptr instr = m_observation.experimentInfo().getInstrument();
IObjComponent_const_sptr source = instr->getSource();
IComponent_const_sptr source = instr->getSource();

m_modChopDist = m_observation.moderatorToFirstChopperDistance();
m_chopSampleDist = m_observation.firstChopperToSampleDistance();
Expand Down
Expand Up @@ -1162,7 +1162,7 @@ namespace WorkspaceCreationHelper
{
Geometry::Instrument_const_sptr instrument = inputWS->getInstrument();
//
Geometry::IObjComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr source = instrument->getSource();
Geometry::IComponent_const_sptr sample = instrument->getSample();
if ((!source) || (!sample))
{
Expand Down

0 comments on commit 2db727f

Please sign in to comment.