Mantid
Loading...
Searching...
No Matches
Instrument.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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 +
7#pragma once
8
9#include "MantidGeometry/DllConfig.h"
15
16#include "MantidTypes/Core/DateAndTime.h"
17
18#include <map>
19#include <queue>
20#include <stdexcept>
21#include <string>
22#include <tuple>
23#include <unordered_set>
24#include <vector>
25
26namespace Mantid {
28using detid2det_map = std::map<detid_t, Geometry::IDetector_const_sptr>;
29
30namespace Geometry {
31class ComponentInfo;
32class DetectorInfo;
34class Parameter;
35class ParameterMap;
36class ReferenceFrame;
40 std::map<std::pair<std::string, const IComponent *>, std::shared_ptr<XMLInstrumentParameter>>;
41
50class MANTID_GEOMETRY_DLL Instrument : public CompAssembly {
51public:
53 std::string type() const override { return "Instrument"; }
54
55 Instrument(const std::shared_ptr<const Instrument> &instr, const std::shared_ptr<ParameterMap> &map);
56 Instrument();
57 Instrument(const std::string &name);
58 Instrument(const Instrument &);
59 Instrument(const Instrument &, bool copyCache);
60
61 Instrument *clone() const override;
62
63 bool hasSource() const;
64 bool hasSample() const;
65
66 IComponent_const_sptr getSource() const;
67 IComponent_const_sptr getSample() const;
68 Kernel::V3D getBeamDirection() const;
69
70 IDetector_const_sptr getDetector(const detid_t &detector_id) const;
71 const IDetector *getBaseDetector(const detid_t &detector_id) const;
72 bool isMonitor(const detid_t &detector_id) const;
73 bool isMonitor(const std::set<detid_t> &detector_ids) const;
74
76 IDetector_const_sptr getDetectorG(const std::set<detid_t> &det_ids) const;
77
79 std::vector<IDetector_const_sptr> getDetectors(const std::vector<detid_t> &det_ids) const;
80
82 std::vector<IDetector_const_sptr> getDetectors(const std::set<detid_t> &det_ids) const;
83
86 void markAsSamplePos(const IComponent *);
87
90 void markAsSource(const IComponent *);
91
94 void markAsDetector(const IDetector *);
95 void markAsDetectorIncomplete(const IDetector *);
96 void markAsDetectorFinalize();
97
100 void markAsMonitor(IDetector const *);
101 void markAsMonitorIncomplete(IDetector const *);
102
104 void removeDetector(IDetector *);
106 void removeDetectorIncomplete(IDetector const *);
107 void removeDetectorFinalize();
108
110 void getDetectors(detid2det_map &out_map) const;
111
113 std::vector<detid_t> getDetectorIDs(bool skipMonitors = false) const;
115 std::vector<detid_t> getMonitorIDs() const;
116
117 std::size_t getNumberDetectors(bool skipMonitors = false) const;
118
119 void getMinMaxDetectorIDs(detid_t &min, detid_t &max) const;
120
121 void getDetectorsInBank(std::vector<IDetector_const_sptr> &dets, const IComponent &comp) const;
122 void getDetectorsInBank(std::vector<IDetector_const_sptr> &dets, const std::string &bankName) const;
123 std::set<detid_t> getDetectorIDsInBank(const std::string &bankName) const;
124
126 void getBoundingBox(BoundingBox &assemblyBox) const override;
127
129 std::shared_ptr<const std::vector<IObjComponent_const_sptr>> getPlottable() const;
130
132 std::shared_ptr<const IComponent> getComponentByID(const IComponent *id) const;
133
135 std::vector<std::shared_ptr<const IComponent>> getAllComponentsWithName(const std::string &cname) const;
136
139 InstrumentParameterCache &getLogfileCache() { return m_logfileCache; }
140 const InstrumentParameterCache &getLogfileCache() const { return m_logfileCache; }
141
144 std::map<std::string, std::string> &getLogfileUnit() { return m_logfileUnit; }
145 const std::map<std::string, std::string> &getLogfileUnit() const { return m_logfileUnit; }
146
150 const std::string &getDefaultView() const { return m_defaultView; }
154 void setDefaultView(const std::string &type);
157 const std::string &getDefaultAxis() const { return m_defaultViewAxis; }
160 void setDefaultViewAxis(const std::string &axis) { m_defaultViewAxis = axis; }
161 // Allow access by index
162 using CompAssembly::getChild;
163
165 std::shared_ptr<const Instrument> baseInstrument() const;
166
169 std::shared_ptr<ParameterMap> getParameterMap() const;
170
172 Types::Core::DateAndTime getValidFromDate() const { return m_ValidFrom; }
173
175 Types::Core::DateAndTime getValidToDate() const { return m_ValidTo; }
176
178 void setValidFromDate(const Types::Core::DateAndTime &val);
179
182 void setValidToDate(const Types::Core::DateAndTime &val) { m_ValidTo = val; }
183
184 // Methods for use with indirect geometry instruments,
185 // where the physical instrument differs from the 'neutronic' one
186 std::shared_ptr<const Instrument> getPhysicalInstrument() const;
187 void setPhysicalInstrument(std::unique_ptr<Instrument>);
188
189 void getInstrumentParameters(double &l1, Kernel::V3D &beamline, double &beamline_norm, Kernel::V3D &samplePos) const;
190
191 void saveNexus(Nexus::File *file, const std::string &group) const;
192 void loadNexus(Nexus::File *file, const std::string &group);
193
194 void setFilename(const std::string &filename);
195 const std::string &getFilename() const;
196 void setXmlText(const std::string &XmlText);
197 const std::string &getXmlText() const;
198
200 void setReferenceFrame(std::shared_ptr<ReferenceFrame> frame);
202 std::shared_ptr<const ReferenceFrame> getReferenceFrame() const;
203
205 enum ContainsState { Full, Partial, None };
206
209 ContainsState containsRectDetectors() const;
210
211 std::vector<RectangularDetector_const_sptr> findRectDetectors() const {
212 return findDetectorsOfType<RectangularDetector>();
213 }
214 std::vector<GridDetector_const_sptr> findGridDetectors() const { return findDetectorsOfType<GridDetector>(); }
215
216 bool isMonitorViaIndex(const size_t index) const;
217 size_t detectorIndex(const detid_t detID) const;
219 std::vector<std::pair<size_t, std::shared_ptr<Parameter>>> legacyParameters() const;
220
221 bool isEmptyInstrument() const;
222
223 void parseTreeAndCacheBeamline();
224 std::pair<std::unique_ptr<ComponentInfo>, std::unique_ptr<DetectorInfo>>
225 makeBeamline(ParameterMap &pmap, const ParameterMap *source = nullptr) const;
226 std::pair<std::unique_ptr<ComponentInfo>, std::unique_ptr<DetectorInfo>> makeBeamlineNew(ParameterMap &pmap) const;
227
229 size_t getMemorySize() const;
230
232
233private:
235 void saveDetectorSetInfoToNexus(Nexus::File *file, const std::vector<detid_t> &detIDs) const;
236
237 bool validateComponentProperties(IComponent_const_sptr component) const;
238
239 void addInstrumentChildrenToQueue(std::queue<IComponent_const_sptr> &queue) const;
240 bool addAssemblyChildrenToQueue(std::queue<IComponent_const_sptr> &queue, IComponent_const_sptr component) const;
241 template <typename T> std::vector<std::shared_ptr<const T>> findDetectorsOfType() const {
242 std::queue<IComponent_const_sptr> compQueue; // Search queue
243 addInstrumentChildrenToQueue(compQueue);
244
245 std::vector<std::shared_ptr<const T>> detectors;
246
248
249 while (!compQueue.empty()) {
250 comp = compQueue.front();
251 compQueue.pop();
252
253 if (!validateComponentProperties(comp))
254 continue;
255
256 if (auto const detector = std::dynamic_pointer_cast<const T>(comp)) {
257 detectors.push_back(detector);
258 } else {
259 // If component is a ComponentAssembly, we add its children to the queue to check if they're type T
260 addAssemblyChildrenToQueue(compQueue, comp);
261 }
262 }
263 return detectors;
264 }
265
268
270 void appendPlottable(const CompAssembly &ca, std::vector<IObjComponent_const_sptr> &lst) const;
271
272 std::pair<std::unique_ptr<ComponentInfo>, std::unique_ptr<DetectorInfo>>
273 makeWrappers(ParameterMap &pmap, const ComponentInfo &componentInfo, const DetectorInfo &detectorInfo) const;
274
279 struct DetectorCacheEntry : public std::tuple<detid_t, IDetector_const_sptr, bool> {
281 : std::tuple<detid_t, IDetector_const_sptr, bool>(id, det, isMonitorFlag) {}
282 detid_t const &id() const { return std::get<0>(*this); }
283 IDetector_const_sptr const &detector() const { return std::get<1>(*this); }
284 bool const &isMonitor() const { return std::get<2>(*this); }
285 void setIsMonitor(bool flag) { std::get<2>(*this) = flag; }
286 };
287
293 struct DetectorCache : public std::vector<DetectorCacheEntry> {
294 bool m_isFinalized{true};
295 bool isFinalized() const { return m_isFinalized; }
296 void setIncomplete() { m_isFinalized = false; }
297 void setFinalized(bool const flag = true) { m_isFinalized = flag; }
298 detid_t minID() const {
299 if (isFinalized() && !empty()) {
300 return front().id();
301 } else {
302 throw std::runtime_error("minID() called on non-finalized or empty DetectorCache");
303 }
304 }
305 detid_t maxID() const {
306 if (isFinalized() && !empty()) {
307 return back().id();
308 } else {
309 throw std::runtime_error("maxID() called on non-finalized or empty DetectorCache");
310 }
311 }
312 DetectorCache::iterator lower_bound(detid_t id);
313 DetectorCache::const_iterator lower_bound(detid_t id) const;
314 DetectorCache::iterator find(detid_t id);
315 DetectorCache::const_iterator find(detid_t id) const;
316 std::unordered_set<IDetector const *> m_toRemove;
317 } m_detectorCache;
318
319 bool isFinalized() const {
320 if (m_map) {
321 return m_instr->isFinalized();
322 } else {
323 return m_detectorCache.isFinalized();
324 }
325 }
326
329 const IComponent *m_sourceCache = nullptr;
330
333 const IComponent *m_sampleCache = nullptr;
334
339
349 std::map<std::string, std::string> m_logfileUnit;
350
353 std::string m_defaultView;
356 std::string m_defaultViewAxis;
357
359 std::shared_ptr<const Instrument> m_instr;
360
362 std::shared_ptr<ParameterMap> m_map_nonconst;
363
365 Types::Core::DateAndTime m_ValidFrom;
367 Types::Core::DateAndTime m_ValidTo;
368
370 mutable std::string m_filename;
371
373 mutable std::string m_xmlText;
374
377 std::shared_ptr<const Instrument> m_physicalInstrument;
378
380 std::shared_ptr<ReferenceFrame> m_referenceFrame;
381
383 std::shared_ptr<const DetectorInfo> m_detectorInfo{nullptr};
384
386 std::shared_ptr<const ComponentInfo> m_componentInfo{nullptr};
387
389 bool m_isPhysicalInstrument{false};
390};
391namespace Conversion {
392
393MANTID_GEOMETRY_DLL double tofToDSpacingFactor(const double l1, const double l2, const double twoTheta,
394 const double offset);
395
396double MANTID_GEOMETRY_DLL tofToDSpacingFactor(const double l1, const double l2, const double twoTheta,
397 const std::vector<detid_t> &detectors,
398 const std::map<detid_t, double> &offsets);
399
400MANTID_GEOMETRY_DLL double calculateDIFCCorrection(const double l1, const double l2, const double twoTheta,
401 const double offset, const double binWidth);
402} // namespace Conversion
403
404} // namespace Geometry
405} // Namespace Mantid
std::string name
Definition Run.cpp:60
std::map< DeltaEMode::Type, std::string > index
IntArray detectorIndex
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition BoundingBox.h:33
Class for Assembly of geometric components.
ComponentInfo : Provides a component centric view on to the instrument.
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
base class for Geometric IComponent
Definition IComponent.h:53
Interface class for detector objects.
Definition IDetector.h:37
InstrumentVisitor : Visitor for components with access to Info wrapping features.
Base Instrument Class.
Definition Instrument.h:50
std::string m_filename
Path to the original IDF .xml file that was loaded for this instrument.
Definition Instrument.h:370
const InstrumentParameterCache & getLogfileCache() const
Definition Instrument.h:140
std::string m_defaultViewAxis
Stores from which side the instrument will be viewed from, initially in the instrument viewer,...
Definition Instrument.h:356
std::string m_xmlText
Contents of the IDF .xml file that was loaded for this instrument.
Definition Instrument.h:373
std::vector< GridDetector_const_sptr > findGridDetectors() const
Definition Instrument.h:214
ContainsState
To determine whether the instrument contains elements of some type.
Definition Instrument.h:205
std::shared_ptr< const Instrument > m_instr
Pointer to the "real" instrument, for parametrized Instrument.
Definition Instrument.h:359
std::string m_defaultView
Stores the default type of the instrument view: 3D or one of the "unwrapped".
Definition Instrument.h:353
std::map< std::string, std::string > m_logfileUnit
Store units used by users to specify angles in IDFs and associated parameter files.
Definition Instrument.h:349
Instrument & operator=(const Instrument &)
Private copy assignment operator.
std::vector< RectangularDetector_const_sptr > findRectDetectors() const
Definition Instrument.h:211
std::map< std::string, std::string > & getLogfileUnit()
Get information about the units used for parameters described in the IDF and associated parameter fil...
Definition Instrument.h:144
Types::Core::DateAndTime m_ValidTo
the date at which the instrument definition is no longer valid.
Definition Instrument.h:367
const std::map< std::string, std::string > & getLogfileUnit() const
Definition Instrument.h:145
const std::string & getDefaultView() const
Get the default type of the instrument view.
Definition Instrument.h:150
const std::string & getDefaultAxis() const
Retrieves from which side the instrument to be viewed from when the instrument viewer first starts,...
Definition Instrument.h:157
std::string type() const override
String description of the type of component.
Definition Instrument.h:53
Types::Core::DateAndTime getValidFromDate() const
Definition Instrument.h:172
std::shared_ptr< const Instrument > m_physicalInstrument
Pointer to the physical instrument, where this differs from the 'neutronic' one (indirect geometry)
Definition Instrument.h:377
std::shared_ptr< ParameterMap > m_map_nonconst
Non-const pointer to the parameter map.
Definition Instrument.h:362
InstrumentParameterCache m_logfileCache
To store info about the parameters defined in IDF.
Definition Instrument.h:338
void setValidToDate(const Types::Core::DateAndTime &val)
Set the date at which the instrument definition is no longer valid.
Definition Instrument.h:182
void setDefaultViewAxis(const std::string &axis)
Retrieves from which side the instrument to be viewed from when the instrument viewer first starts,...
Definition Instrument.h:160
std::vector< std::shared_ptr< const T > > findDetectorsOfType() const
Definition Instrument.h:241
std::shared_ptr< ReferenceFrame > m_referenceFrame
Pointer to the reference frame object.
Definition Instrument.h:380
InstrumentParameterCache & getLogfileCache()
Get information about the parameters described in the instrument definition file and associated param...
Definition Instrument.h:139
Types::Core::DateAndTime m_ValidFrom
the date from which the instrument definition begins to be valid.
Definition Instrument.h:365
Types::Core::DateAndTime getValidToDate() const
Definition Instrument.h:175
Parameter map iterator typedef.
Base class for parameters of an instrument.
Definition Parameter.h:42
ReferenceFrame : Holds reference frame information from the geometry description file.
This class is used to store information about parameters in XML instrument definition files and instr...
Class for 3D vectors.
Definition V3D.h:35
MANTID_GEOMETRY_DLL double calculateDIFCCorrection(const double l1, const double l2, const double twoTheta, const double offset, const double binWidth)
MANTID_GEOMETRY_DLL double tofToDSpacingFactor(const double l1, const double l2, const double twoTheta, const double offset)
Calculate and return conversion factor from tof to d-spacing.
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
Definition IComponent.h:165
std::map< std::pair< std::string, const IComponent * >, std::shared_ptr< XMLInstrumentParameter > > InstrumentParameterCache
Convenience typedef.
Definition Instrument.h:40
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
Definition IDetector.h:96
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
std::map< detid_t, Geometry::IDetector_const_sptr > detid2det_map
Typedef of a map from detector ID to detector shared pointer.
Definition Instrument.h:28
Generate a tableworkspace to store the calibration results.
STL namespace.
Tuple which holds detector-IDs and pointers to detector components, and monitor flags.
Definition Instrument.h:279
IDetector_const_sptr const & detector() const
Definition Instrument.h:283
DetectorCacheEntry(detid_t id, IDetector_const_sptr det, bool isMonitorFlag)
Definition Instrument.h:280
A vector of DetectorCacheEntry, which holds the detector cache This is implemented as a vector rather...
Definition Instrument.h:293
std::unordered_set< IDetector const * > m_toRemove
Definition Instrument.h:316
void setFinalized(bool const flag=true)
Definition Instrument.h:297