ExperimentInfo.h 9.25 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
10
#include "MantidAPI/DllConfig.h"

11
#include "MantidAPI/SpectraDetectorTypes.h"
12
#include "MantidGeometry/Instrument_fwd.h"
13

14
#include "MantidKernel/DeltaEMode.h"
15
#include "MantidKernel/V3D.h"
16
#include "MantidKernel/cow_ptr.h"
17

18
#include <mutex>
19

20
namespace Mantid {
21
class SpectrumDefinition;
22
23
24
namespace Kernel {
class Property;
}
25
namespace Beamline {
Owen Arnold's avatar
Owen Arnold committed
26
class ComponentInfo;
27
class DetectorInfo;
28
class SpectrumInfo;
LamarMoore's avatar
LamarMoore committed
29
} // namespace Beamline
30
namespace Geometry {
Owen Arnold's avatar
Owen Arnold committed
31
class ComponentInfo;
32
class DetectorInfo;
33
class IDetector;
34
class ParameterMap;
35
class XMLInstrumentParameter;
LamarMoore's avatar
LamarMoore committed
36
} // namespace Geometry
37
38

namespace API {
39
40
class Run;
class Sample;
41
class SpectrumInfo;
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/** This class is shared by a few Workspace types
 * and holds information related to a particular experiment/run:
 *
 * - Instrument (with parameter map)
 * - Run object (sample logs)
 * - Sample object (sample info)
 *
 */
class MANTID_API_DLL ExperimentInfo {
public:
  /// Default constructor
  ExperimentInfo();
  /// Virtual destructor
56
  virtual ~ExperimentInfo();
57
58
59
60
61
  /// Copy constructor
  ExperimentInfo(const ExperimentInfo &);
  /// Copy everything from the given experiment object
  void copyExperimentInfoFrom(const ExperimentInfo *other);
  /// Clone us
62
  virtual ExperimentInfo *cloneExperimentInfo() const;
63
64

  /// Returns a string description of the object
65
  const std::string toString() const;
66
67

  /// Instrument accessors
68
  void setInstrument(const Geometry::Instrument_const_sptr &instr);
69
  /// Returns the parameterized instrument
70
  Geometry::Instrument_const_sptr getInstrument() const;
71
72
73

  /// Returns the set of parameters modifying the base instrument
  /// (const-version)
74
  const Geometry::ParameterMap &instrumentParameters() const;
75
  /// Returns a modifiable set of instrument parameters
76
  Geometry::ParameterMap &instrumentParameters();
77
  /// Const version
78
  const Geometry::ParameterMap &constInstrumentParameters() const;
79
  // Add parameters to the instrument parameter map
80
  void populateInstrumentParameters();
81

82
  void setNumberOfDetectorGroups(const size_t count) const;
83
84
  void setDetectorGrouping(const size_t index,
                           const std::set<detid_t> &detIDs) const;
85

86
  /// Sample accessors
87
  const Sample &sample() const;
88
  /// Writable version of the sample object
89
  Sample &mutableSample();
90
91

  /// Run details object access
92
  const Run &run() const;
93
  /// Writable version of the run object
94
  Run &mutableRun();
95
  void setSharedRun(Kernel::cow_ptr<Run> run);
96
  Kernel::cow_ptr<Run> sharedRun();
97

98
  /// Access a log for this experiment.
99
  Kernel::Property *getLog(const std::string &log) const;
100
  /// Access a single value from a log for this experiment.
101
  double getLogAsSingleValue(const std::string &log) const;
102
103

  /// Utility method to get the run number
104
  int getRunNumber() const;
105
  /// Returns the emode for this run
106
  Kernel::DeltaEMode::Type getEMode() const;
107
  /// Easy access to the efixed value for this run & detector ID
108
  double getEFixed(const detid_t detID) const;
109
  /// Easy access to the efixed value for this run & optional detector
110
111
112
  double getEFixed(const std::shared_ptr<const Geometry::IDetector> &detector =
                       std::shared_ptr<const Geometry::IDetector>{
                           nullptr}) const;
113
  /// Set the efixed value for a given detector ID
114
  void setEFixed(const detid_t detID, const double value);
115
116

  /// Saves this experiment description to the open NeXus file
Owen Arnold's avatar
Owen Arnold committed
117
118
  void saveExperimentInfoNexus(::NeXus::File *file,
                               bool saveLegacyInstrument = true) const;
119
120
  /// Saves this experiment description to the open NeXus file
  void saveExperimentInfoNexus(::NeXus::File *file, bool saveInstrument,
Owen Arnold's avatar
Owen Arnold committed
121
                               bool saveSample, bool saveLogs) const;
122
  /// Loads an experiment description from the open NeXus file
123
124
  void loadExperimentInfoNexus(const std::string &nxFilename,
                               ::NeXus::File *file, std::string &parameterStr);
125
  /// Load the instrument from an open NeXus file.
126
127
  void loadInstrumentInfoNexus(const std::string &nxFilename,
                               ::NeXus::File *file, std::string &parameterStr);
128
  /// Load the instrument from an open NeXus file without reading any parameters
129
130
  void loadInstrumentInfoNexus(const std::string &nxFilename,
                               ::NeXus::File *file);
131
132
  /// Load instrument parameters from an open Nexus file in Instument group if
  /// found there
133
134
  void loadInstrumentParametersNexus(::NeXus::File *file,
                                     std::string &parameterStr);
Nick Draper's avatar
Nick Draper committed
135

136
  /// Load the sample and log info from an open NeXus file.
137
  void loadSampleAndLogInfoNexus(::NeXus::File *file);
138
  /// Populate the parameter map given a string
139
  void readParameterMap(const std::string &parameterStr);
140
141
142

  /// Returns the start date for this experiment (or current time if no info
  /// available)
143
  std::string getWorkspaceStartDate() const;
144
145

  // run/experiment stat time if available, empty otherwise
146
  std::string getAvailableWorkspaceStartDate() const;
147
  // run end time if available, empty otherwise
148
  std::string getAvailableWorkspaceEndDate() const;
149
150
151
152
153

  /// Utility to retrieve the validity dates for the given IDF
  static void getValidFromTo(const std::string &IDFfilename,
                             std::string &outValidFrom,
                             std::string &outValidTo);
154
  /// Utility to retrieve a resource file (IDF, Parameters, ..)
155
156
157
  static std::vector<std::string> getResourceFilenames(
      const std::string &prefix, const std::vector<std::string> &fileFormats,
      const std::vector<std::string> &directoryNames, const std::string &date);
158
159
160
  /// Get the IDF using the instrument name and date
  static std::string getInstrumentFilename(const std::string &instrumentName,
                                           const std::string &date = "");
161
162
163
164
165
166

  /// Search instrument directories for Parameter file,
  /// return full path name if found, else "".
  static std::string getFullPathParamIDF(std::string instName,
                                         const std::string &dirHint = "");

167
168
  const Geometry::DetectorInfo &detectorInfo() const;
  Geometry::DetectorInfo &mutableDetectorInfo();
169

170
171
172
  const SpectrumInfo &spectrumInfo() const;
  SpectrumInfo &mutableSpectrumInfo();

Owen Arnold's avatar
Owen Arnold committed
173
174
  const Geometry::ComponentInfo &componentInfo() const;
  Geometry::ComponentInfo &mutableComponentInfo();
Owen Arnold's avatar
Owen Arnold committed
175

176
177
  void invalidateSpectrumDefinition(const size_t index);
  void updateSpectrumDefinitionIfNecessary(const size_t index) const;
178

179
protected:
180
  size_t numberOfDetectorGroups() const;
181
182
183
  /// Called as the first operation of most public methods.
  virtual void populateIfNotLoaded() const;

184
185
  void setSpectrumDefinitions(
      Kernel::cow_ptr<std::vector<SpectrumDefinition>> spectrumDefinitions);
186

187
  virtual void updateCachedDetectorGrouping(const size_t index) const;
188
  /// Parameters modifying the base instrument
189
  std::shared_ptr<Geometry::ParameterMap> m_parmap;
190
191
192
193
194
195
  /// The base (unparametrized) instrument
  Geometry::Instrument_const_sptr sptr_instrument;

private:
  /// Fill with given instrument parameter
  void populateWithParameter(Geometry::ParameterMap &paramMap,
196
                             Geometry::ParameterMap &paramMapForPosAndRot,
197
198
199
200
                             const std::string &name,
                             const Geometry::XMLInstrumentParameter &paramInfo,
                             const Run &runData);

201
202
203
204
  /// Attempt to load instrument embedded in Nexus file. *file must have
  /// instrument group open.
  void loadEmbeddedInstrumentInfoNexus(::NeXus::File *file,
                                       std::string &instrumentName,
205
                                       std::string &instrumentXml);
206

207
208
209
210
  /// Set the instrument given the name and XML leading from IDF file if XML
  /// string is empty
  void setInstumentFromXML(const std::string &nxFilename,
                           std::string &instrumentName,
211
                           std::string &instrumentXml);
212

213
214
  // Loads the xml from an instrument file with some basic error handling
  std::string loadInstrumentXML(const std::string &filename);
215
216
217
218
219
220

  /// The information on the sample environment
  Kernel::cow_ptr<Sample> m_sample;
  /// The run information
  Kernel::cow_ptr<Run> m_run;

221
  /// Detector grouping information
222
  mutable std::unordered_map<detid_t, size_t> m_det2group;
223
  void cacheDefaultDetectorGrouping() const; // Not thread-safe
224
  void invalidateAllSpectrumDefinitions();
225
226
  mutable std::once_flag m_defaultDetectorGroupingCached;

227
  mutable std::unique_ptr<Beamline::SpectrumInfo> m_spectrumInfo;
228
229
  mutable std::unique_ptr<SpectrumInfo> m_spectrumInfoWrapper;
  mutable std::mutex m_spectrumInfoMutex;
230
231
232
  // This vector stores boolean flags but uses char to do so since
  // std::vector<bool> is not thread-safe.
  mutable std::vector<char> m_spectrumDefinitionNeedsUpdate;
233
234
235
};

/// Shared pointer to ExperimentInfo
236
using ExperimentInfo_sptr = std::shared_ptr<ExperimentInfo>;
237
238

/// Shared pointer to const ExperimentInfo
239
using ExperimentInfo_const_sptr = std::shared_ptr<const ExperimentInfo>;
240
241

} // namespace API
LamarMoore's avatar
LamarMoore committed
242
} // namespace Mantid