Mantid
Loading...
Searching...
No Matches
InstrumentCreationHelper.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include "MantidAPI/Axis.h"
13
14using namespace Mantid;
15using namespace Mantid::Kernel;
16using namespace Mantid::Geometry;
17using namespace Mantid::API;
18
20
21void addFullInstrumentToWorkspace(MatrixWorkspace &workspace, bool includeMonitors, bool startYNegative,
22 const std::string &instrumentName) {
23 auto instrument = std::make_shared<Instrument>(instrumentName);
24 instrument->setReferenceFrame(std::make_shared<ReferenceFrame>(Y, Z, Right, ""));
25
26 const double pixelRadius(0.05);
27 auto pixelShape =
28 ComponentCreationHelper::createCappedCylinder(pixelRadius, 0.02, V3D(0.0, 0.0, 0.0), V3D(0., 1.0, 0.), "tube");
29
30 const double detZPos(5.0);
31 // Careful! Do not use size_t or auto, the unsigned will break the -=2 below.
32 auto ndets = static_cast<int>(workspace.getNumberHistograms());
33 if (includeMonitors)
34 ndets -= 2;
35 for (int i = 0; i < ndets; ++i) {
36 std::ostringstream lexer;
37 lexer << "pixel-" << i << ")";
38 Detector *physicalPixel =
39 new Detector(lexer.str(), workspace.getAxis(1)->spectraNo(i), pixelShape, instrument.get());
40 int ycount(i);
41 if (startYNegative)
42 ycount -= 1;
43 const double ypos = ycount * 2.0 * pixelRadius;
44 physicalPixel->setPos(0.0, ypos, detZPos);
45 instrument->add(physicalPixel);
46 instrument->markAsDetector(physicalPixel);
47 workspace.getSpectrum(i).setDetectorID(physicalPixel->getID());
48 }
49
50 // Monitors last
51 if (includeMonitors) // These occupy the last 2 spectra
52 {
53 Detector *monitor1 = new Detector("mon1", workspace.getAxis(1)->spectraNo(ndets), IObject_sptr(), instrument.get());
54 monitor1->setPos(0.0, 0.0, -9.0);
55 instrument->add(monitor1);
56 instrument->markAsMonitor(monitor1);
57 workspace.getSpectrum(ndets).setDetectorID(ndets + 1);
58
59 Detector *monitor2 =
60 new Detector("mon2", workspace.getAxis(1)->spectraNo(ndets) + 1, IObject_sptr(), instrument.get());
61 monitor2->setPos(0.0, 0.0, -2.0);
62 instrument->add(monitor2);
63 instrument->markAsMonitor(monitor2);
64 workspace.getSpectrum(ndets + 1).setDetectorID(ndets + 2);
65 }
66
67 // Define a source and sample position
68 // Define a source component
69 ObjComponent *source =
70 new ObjComponent("moderator", ComponentCreationHelper::createSphere(0.1, V3D(0, 0, 0), "1"), instrument.get());
71 source->setPos(V3D(0.0, 0.0, -20.0));
72 instrument->add(source);
73 instrument->markAsSource(source);
74
75 // Define a sample position
76 Component *sample = new Component("samplePos", instrument.get());
77 instrument->setPos(0.0, 0.0, 0.0);
78 instrument->add(sample);
79 instrument->markAsSamplePos(sample);
80 // chopper position
81 Component *chop_pos = new Component("chopper-position", Kernel::V3D(0, 0, -10), instrument.get());
82 instrument->add(chop_pos);
83 workspace.setInstrument(instrument);
84}
85
87 const int nlong, const double anginc,
88 const std::string &instrumentName) {
89 V3D samplePosition(0., 0., 0.);
90 V3D sourcePosition(0., 0., -14.);
91
92 Instrument_sptr instrument = std::make_shared<Instrument>(instrumentName);
93 instrument->setReferenceFrame(
94 std::make_shared<ReferenceFrame>(Mantid::Geometry::Y, Mantid::Geometry::Z, Right, "0,0,0"));
95
96 InstrumentCreationHelper::addSource(instrument, sourcePosition, "source");
97 InstrumentCreationHelper::addSample(instrument, samplePosition, "sample");
98
99 auto compAss = new ObjCompAssembly("detector-stage");
100 // set up detectors with even spacing in latitude and longitude (to match geographical angles
101 // approach used in the spatial interpolation\sparse instrument functionality)
102 int i = 0;
103 constexpr double deg2rad = M_PI / 180.0;
104 const double angincRad = anginc * deg2rad;
105 constexpr double R = 1.0;
106 for (int lat = 0; lat < nlat; ++lat) {
107 for (int lng = 0; lng < nlong; ++lng) {
108 std::stringstream buffer;
109 buffer << "detector_" << i;
110 V3D detPos;
111 auto latrad = lat * angincRad;
112 auto longrad = lng * angincRad;
113 detPos[1] = R * sin(latrad);
114 const double ct = R * cos(latrad);
115 detPos[2] = ct * cos(longrad);
116 detPos[0] = ct * sin(longrad);
117
118 InstrumentCreationHelper::addDetector(instrument, detPos, i, buffer.str(), compAss);
119 // Link it to the workspace
120 workspace.getSpectrum(i).addDetectorID(i);
121 i++;
122 }
123 }
124 instrument->add(compAss);
125 workspace.setInstrument(instrument);
126}
127
136 const std::string &name) {
137 auto *component = new Component(name);
138 component->setPos(position);
139 instrument->add(component);
140 return component;
141}
142
150 const std::string &name) {
151 auto sample = addComponent(instrument, position, name);
152 instrument->markAsSamplePos(sample);
153}
154
162 const std::string &name) {
163 auto source = addComponent(instrument, position, name);
164 instrument->markAsSource(source);
165}
166
175 const std::string &name) {
176 auto *monitor = new Detector(name, ID, nullptr);
177 monitor->setPos(position);
178 instrument->add(monitor);
179 instrument->markAsMonitor(monitor);
180}
181
191 const std::string &name, Mantid::Geometry::ObjCompAssembly *compAss) {
192 // Where 0.01 is half detector width etc.
193 Detector *detector = new Detector(name, ID, ComponentCreationHelper::createCuboid(0.01, 0.02, 0.03), nullptr);
194 detector->setPos(position);
195 instrument->markAsDetector(detector);
196 if (compAss) {
197 compAss->add(detector);
198 } else {
199 instrument->add(detector);
200 }
201}
202} // namespace InstrumentCreationHelper
double position
Definition: GetAllEi.cpp:154
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
constexpr double deg2rad
Definition: Q1DWeighted.cpp:35
Base MatrixWorkspace Abstract Class.
Component is a wrapper for a Component which can modify some of its parameters, e....
Definition: Component.h:41
void setPos(double, double, double) override
Set the IComponent position, x, y, z respective to parent (if present)
Definition: Component.cpp:204
This class represents a detector - i.e.
Definition: Detector.h:30
detid_t getID() const override
Gets the detector id.
Definition: Detector.cpp:51
Class for Assembly of geometric components.
int add(IComponent *) override
Add a component to the assembly.
Object Component class, this class brings together the physical attributes of the component to the po...
Definition: ObjComponent.h:33
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< Mantid::Geometry::CSGObject > createSphere(double radius, const Mantid::Kernel::V3D &centre=Mantid::Kernel::V3D(), const std::string &id="sp-1")
Create a sphere object.
std::shared_ptr< Mantid::Geometry::CSGObject > createCuboid(double xHalfLength, double yHalfLength=-1.0, double zHalfLength=-1.0, const Mantid::Kernel::V3D &centre={0.0, 0.0, 0.0}, const std::string &id="detector-shape")
Create a cuboid shape.
std::shared_ptr< Mantid::Geometry::CSGObject > createCappedCylinder(double radius, double height, const Mantid::Kernel::V3D &baseCentre, const Mantid::Kernel::V3D &axis, const std::string &id)
Create a capped cylinder object.
void addMonitor(Mantid::Geometry::Instrument_sptr &instrument, const Mantid::Kernel::V3D &position, const int ID, const std::string &name)
Adds a monitor to an instrument.
void addSource(Mantid::Geometry::Instrument_sptr &instrument, const Mantid::Kernel::V3D &position, const std::string &name)
Adds a source to an instrument.
void addFullInstrumentToWorkspace(Mantid::API::MatrixWorkspace &workspace, bool includeMonitors, bool startYNegative, const std::string &instrumentName)
void addInstrumentWithGeographicalDetectorsToWorkspace(Mantid::API::MatrixWorkspace &workspace, const int nlat, const int nlong, const double anginc, const std::string &instrumentName)
void addSample(Mantid::Geometry::Instrument_sptr &instrument, const Mantid::Kernel::V3D &position, const std::string &name)
Adds a sample to an instrument.
Mantid::Geometry::Component * addComponent(Mantid::Geometry::Instrument_sptr &instrument, const Mantid::Kernel::V3D &position, const std::string &name)
Adds a component to an instrument.
void addDetector(Mantid::Geometry::Instrument_sptr &instrument, const Mantid::Kernel::V3D &position, const int ID, const std::string &name, Mantid::Geometry::ObjCompAssembly *compAss=nullptr)
Adds a detector to an instrument.
std::shared_ptr< Instrument > Instrument_sptr
Shared pointer to an instrument object.
std::shared_ptr< IObject > IObject_sptr
Typdef for a shared pointer.
Definition: IObject.h:92
Helper class which provides the Collimation Length for SANS instruments.