LoadEventNexus.h 29.5 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2010 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
#include "MantidAPI/IFileLoader.h"
William F Godoy's avatar
William F Godoy committed
10
#include "MantidAPI/NexusFileLoader.h"
Hahn, Steven's avatar
Hahn, Steven committed
11
#include "MantidAPI/WorkspaceGroup.h"
12
#include "MantidDataHandling/BankPulseTimes.h"
Hahn, Steven's avatar
Hahn, Steven committed
13
#include "MantidDataHandling/EventWorkspaceCollection.h"
14
#include "MantidDataHandling/LoadGeometry.h"
15
#include "MantidDataObjects/EventWorkspace.h"
16
#include "MantidDataObjects/Events.h"
17
18
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/ParameterMap.h"
William F Godoy's avatar
William F Godoy committed
19
#include "MantidKernel/NexusHDF5Descriptor.h"
Hahn, Steven's avatar
Hahn, Steven committed
20
#include "MantidKernel/OptionalBool.h"
21
#include "MantidKernel/TimeSeriesProperty.h"
Alex Buts's avatar
Alex Buts committed
22

23
24
#ifdef _WIN32 // fixing windows issue causing conflict between
// winnt char and nexus char
Alex Buts's avatar
Alex Buts committed
25
#undef CHAR
26
27
#endif

28
// clang-format off
29
#include <nexus/NeXusFile.hpp>
30
31
#include <nexus/NeXusException.hpp>
// clang-format on
Alex Buts's avatar
Alex Buts committed
32

33
34
35
#include <boost/lexical_cast.hpp>
#include <boost/scoped_array.hpp>
#include <functional>
Peterson, Peter's avatar
Peterson, Peter committed
36
#include <random>
37
#include <memory>
38
#include <mutex>
39
#include <numeric>
40

41
42
namespace Mantid {
namespace DataHandling {
43

44
45
46
47
48
49
50
51
52
53
54
/** @class InvalidLogPeriods
 * Custom exception extending std::invalid_argument
 * Thrown when nperiods does not match period_log
 * Custom exception so we can re-propagate this error and
 * handle all other errors.
 */
class InvalidLogPeriods : public std::invalid_argument {
public:
  InvalidLogPeriods(const std::string &msg) : std::invalid_argument(msg) {}
};

55
56
bool exists(::NeXus::File &file, const std::string &name);

57
58
59
bool exists(const std::map<std::string, std::string> &entries,
            const std::string &name);

60
/** @class LoadEventNexus LoadEventNexus.h Nexus/LoadEventNexus.h
61

62
  Load Event Nexus files.
63

64
65
66
67
68
  Required Properties:
  <UL>
  <LI> Filename - The name of and path to the input NeXus file </LI>
  <LI> Workspace - The name of the workspace to output</LI>
  </UL>
69

70
71
  @date Sep 27, 2010
  */
William F Godoy's avatar
William F Godoy committed
72
class DLLExport LoadEventNexus : public API::NexusFileLoader {
73
74
75
76

public:
  LoadEventNexus();

77
  const std::string name() const override { return "LoadEventNexus"; };
78
79

  /// Summary of algorithms purpose
80
  const std::string summary() const override {
Owen Arnold's avatar
Owen Arnold committed
81
    return "Loads an Event NeXus file and stores as an "
82
83
84
85
86
           "EventWorkspace. Optionally, you can filter out events falling "
           "outside a range of times-of-flight and/or a time interval.";
  }

  /// Version
87
  int version() const override { return 1; };
Nick Draper's avatar
Nick Draper committed
88
  const std::vector<std::string> seeAlso() const override {
Nick Draper's avatar
Nick Draper committed
89
    return {"LoadISISNexus", "LoadEventAndCompress"};
Nick Draper's avatar
Nick Draper committed
90
  }
91
92

  /// Category
93
  const std::string category() const override { return "DataHandling\\Nexus"; }
94

William F Godoy's avatar
William F Godoy committed
95
96
  int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

97
  template <typename T>
98
  static std::shared_ptr<BankPulseTimes> runLoadNexusLogs(
Tom Titcombe's avatar
Tom Titcombe committed
99
100
101
102
      const std::string &nexusfilename, T localWorkspace, Algorithm &alg,
      bool returnpulsetimes, int &nPeriods,
      std::unique_ptr<const Kernel::TimeSeriesProperty<int>> &periodLog);

103
104
105
106
107
108
109
110
  template <typename T>
  static std::shared_ptr<BankPulseTimes> runLoadNexusLogs(
      const std::string &nexusfilename, T localWorkspace, Algorithm &alg,
      bool returnpulsetimes, int &nPeriods,
      std::unique_ptr<const Kernel::TimeSeriesProperty<int>> &periodLog,
      const std::vector<std::string> &allow_list,
      const std::vector<std::string> &block_list);

Tom Titcombe's avatar
Tom Titcombe committed
111
112
113
114
  static void checkForCorruptedPeriods(
      std::unique_ptr<Kernel::TimeSeriesProperty<int>> tempPeriodLog,
      std::unique_ptr<const Kernel::TimeSeriesProperty<int>> &periodLog,
      const int &nPeriods, const std::string &nexusfilename);
115
116
117

  template <typename T>
  static void loadEntryMetadata(const std::string &nexusfilename, T WS,
William F Godoy's avatar
William F Godoy committed
118
119
                                const std::string &entry_name,
                                const Kernel::NexusHDF5Descriptor &descriptor);
120

William F Godoy's avatar
William F Godoy committed
121
122
123
124
125
126
127
128
129
130
  /**
   * Load instrument from Nexus file if possible, else from IDF spacified by
   * Nexus file
   * @param nexusfilename input nexus file name
   * @param localWorkspace input
   * @param top_entry_name e.g. /entry
   * @param alg input algorithm executing this task
   * @param descriptor input descriptor
   * @return true: success, false: failure
   */
131
  template <typename T>
132
133
134
135
  static bool
  loadInstrument(const std::string &nexusfilename, T localWorkspace,
                 const std::string &top_entry_name, Algorithm *alg,
                 const Kernel::NexusHDF5Descriptor *descriptor = nullptr);
136
137

  /// Load instrument for Nexus file
138
139
140
141
  template <typename T>
  static bool
  runLoadIDFFromNexus(const std::string &nexusfilename, T localWorkspace,
                      const std::string &top_entry_name, Algorithm *alg);
142

William F Godoy's avatar
William F Godoy committed
143
144
145
146
147
148
149
150
151
  /**
   * Load instrument from IDF file specified by Nexus file
   * @param nexusfilename input nexus file name
   * @param localWorkspace input
   * @param top_entry_name e.g. /entry
   * @param alg input algorithm executing this task
   * @param descriptor input descriptor
   * @return true: success, false: failure
   */
152
  template <typename T>
153
154
155
156
  static bool
  runLoadInstrument(const std::string &nexusfilename, T localWorkspace,
                    const std::string &top_entry_name, Algorithm *alg,
                    const Kernel::NexusHDF5Descriptor *descriptor = nullptr);
157

158
  static void loadSampleDataISIScompatibility(::NeXus::File &file,
159
                                              EventWorkspaceCollection &WS);
160
161
162
163
164
165
166
167
168
169

  /// method used to return instrument name for some old ISIS files where it is
  /// not written properly within the instrument
  static std::string readInstrumentFromISIS_VMSCompat(::NeXus::File &hFile);

public:
  /// The name and path of the input file
  std::string m_filename;

  /// The workspace being filled out
170
  std::shared_ptr<EventWorkspaceCollection> m_ws;
171
172
173
174
175
176
177
178
179
180
181
182

  /// Filter by a minimum time-of-flight
  double filter_tof_min;
  /// Filter by a maximum time-of-flight
  double filter_tof_max;

  /// Minimum spectrum to load
  int32_t m_specMin;
  /// Maximum spectrum to load
  int32_t m_specMax;

  /// Filter by start time
183
  Mantid::Types::Core::DateAndTime filter_time_start;
184
  /// Filter by stop time
185
  Mantid::Types::Core::DateAndTime filter_time_stop;
186
187

  /// Mutex protecting tof limits
188
  std::mutex m_tofMutex;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

  /// Limits found to tof
  double longest_tof;
  /// Limits found to tof
  double shortest_tof;
  /// Count of all the "bad" tofs found. These are events with TOF > 2e8
  /// microsec
  size_t bad_tofs;
  /// A count of events discarded because they came from a pixel that's not in
  /// the IDF
  size_t discarded_events;

  /// Tolerance for CompressEvents; use -1 to mean don't compress.
  double compressTolerance;

  /// Pulse times for ALL banks, taken from proton_charge log.
205
  std::shared_ptr<BankPulseTimes> m_allBanksPulseTimes;
206

207
208
  /// name of top level NXentry to use
  std::string m_top_entry_name;
209
  std::unique_ptr<::NeXus::File> m_file;
210

211
212
213
214
215
protected:
  Parallel::ExecutionMode getParallelExecutionMode(
      const std::map<std::string, Parallel::StorageMode> &storageModes)
      const override;

216
private:
Igor Gudich's avatar
Igor Gudich committed
217
  /// Possible loaders types
218
  enum class LoaderType;
Igor Gudich's avatar
Igor Gudich committed
219

220
  /// Intialisation code
221
  void init() override;
222
223

  /// Execution code
William F Godoy's avatar
William F Godoy committed
224
  void execLoader() override;
225

226
227
228
  LoadEventNexus::LoaderType
  defineLoaderType(const bool haveWeights, const bool oldNeXusFileNames,
                   const std::string &classType) const;
229

230
231
232
233
234
235
  DataObjects::EventWorkspace_sptr createEmptyEventWorkspace();

  void loadEvents(API::Progress *const prog, const bool monitors);
  void createSpectraMapping(
      const std::string &nxsfile, const bool monitorsOnly,
      const std::vector<std::string> &bankNames = std::vector<std::string>());
David Fairbrother's avatar
David Fairbrother committed
236
237
  void deleteBanks(const EventWorkspaceCollection_sptr &workspace,
                   const std::vector<std::string> &bankNames);
238
239
240
241
242
  void runLoadMonitors();
  /// Set the filters on TOF.
  void setTimeFilters(const bool monitors);

  /// Load a spectra mapping from the given file
243
  std::unique_ptr<std::pair<std::vector<int32_t>, std::vector<int32_t>>>
244
  loadISISVMSSpectraMapping(const std::string &entry_name);
245

246
  template <typename T> void filterDuringPause(T workspace);
247
248
249

  /// Set the top entry field name
  void setTopEntryName();
250

251
  /// to open the nexus file with specific exception handling/message
David Fairbrother's avatar
David Fairbrother committed
252
  void safeOpenFile(const std::string &fname);
253

254
255
256
257
258
259
260
  /// Was the instrument loaded?
  bool m_instrument_loaded_correctly;

  /// Do we load the sample logs?
  bool loadlogs;
  /// True if the event_id is spectrum no not pixel ID
  bool event_id_is_spec;
261
};
262

Peterson, Peter's avatar
Peterson, Peter committed
263
264
265
//-----------------------------------------------------------------------------
//               ISIS event corrections
//-----------------------------------------------------------------------------
266
267

/**
LamarMoore's avatar
LamarMoore committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
 * Load the time of flight data. file must have open the group containing
 * "time_of_flight" data set. This will add a offset to all of the
 * time-of-flight values or a random number to each time-of-flight. It
 * should only ever be called on event files that have a "detector_1_events"
 * group inside the "NXentry". It is an old ISIS requirement that is rarely
 * used now.
 *
 * Due to hardware issues with retro-fitting event mode to old electronics,
 * ISIS event mode is really a very fine histogram with between 1 and 2
 * microseconds bins.
 *
 * If we just took "middle of bin" as the true event time here then WISH
 * observed strange ripples when they added spectra. The solution was to
 * randomise the probability of an event within the bin.
 *
 * This randomisation is now performed in the control program which also writes
 * the "event_time_offset_shift" dataset (with a single value of "random") when
 * it has been performed. If this dataset is present in an event file then no
 * randomisation is performed in LoadEventNexus.
 *
 * This code should remain for loading older ISIS event datasets.
 *
 * @param file :: The nexus file to read from.
 * @param localWorkspace :: The event workspace collection to write to.
 * @param binsName :: bins name
 * @param start_wi :: First workspace index to process
 * @param end_wi :: Last workspace index to process
 */
Peterson, Peter's avatar
Peterson, Peter committed
296
297
template <typename T>
void makeTimeOfFlightDataFuzzy(::NeXus::File &file, T localWorkspace,
298
                               const std::string &binsName, size_t start_wi = 0,
Peterson, Peter's avatar
Peterson, Peter committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
                               size_t end_wi = 0) {
  const std::string EVENT_TIME_SHIFT_TAG("event_time_offset_shift");
  // first check if the data is already randomized
  const auto entries = file.getEntries();
  if (entries.find(EVENT_TIME_SHIFT_TAG) != entries.end()) {
    std::string event_shift_type;
    file.readData(EVENT_TIME_SHIFT_TAG, event_shift_type);
    if (event_shift_type == "random") {
      return;
    }
  }

  // if the data is not randomized randomize it uniformly within each bin
  file.openData(binsName);
  // time of flights of events
  std::vector<float> tofsFile;
  file.getData(tofsFile);
  file.closeData();

  // todo: try to find if tof can be reduced to just 3 numbers: start, end and
  // dt
  if (end_wi <= start_wi) {
    end_wi = localWorkspace->getNumberHistograms();
  }

  // random number generator
  std::mt19937 rng;

  // loop over spectra
  for (size_t wi = start_wi; wi < end_wi; ++wi) {
    DataObjects::EventList &event_list = localWorkspace->getSpectrum(wi);
    if (event_list.empty())
      continue;
    // sort the events
    event_list.sortTof();
    auto tofsEventList = event_list.getTofs();

    size_t n = tofsFile.size();
    // iterate over the events and time bins
    auto ev = tofsEventList.begin();
    auto ev_end = tofsEventList.end();
    for (size_t i = 1; i < n; ++i) {
      double right = double(tofsFile[i]);
      // find the right boundary for the current event
      if ((ev != ev_end) && (right < *ev)) {
        continue;
      }
      // count events which have the same right boundary
      size_t m = 0;
      while ((ev != ev_end) && (*ev < right)) {
        ++ev;
        ++m; // count events in the i-th bin
      }

      if (m > 0) { // m events in this bin
        double left = double(tofsFile[i - 1]);
        // spread the events uniformly inside the bin
        std::uniform_real_distribution<double> flat(left, right);
        std::vector<double> random_numbers(m);
        for (double &random_number : random_numbers) {
          random_number = flat(rng);
        }
        std::sort(random_numbers.begin(), random_numbers.end());
        auto it = random_numbers.begin();
        for (auto ev1 = ev - m; ev1 != ev; ++ev1, ++it) {
          *ev1 = *it;
        }
      }

    } // for i
    event_list.setTofs(tofsEventList);

    event_list.sortTof();
  } // for wi
}

/**
LamarMoore's avatar
LamarMoore committed
376
377
378
379
380
381
382
383
384
385
 * ISIS specific method for dealing with wide events. Check if time_of_flight
 * can be found in the file and load it.
 *
 * THIS ONLY APPLIES TO ISIS FILES WITH "detector_1_events" IN THE "NXentry."
 *
 * @param file :: The nexus file to read from.
 * @param localWorkspace :: The event workspace collection which events will be
 *modified.
 * @param entry_name :: An NXentry tag in the file
 * @param classType :: The type of the events: either detector or monitor
William F Godoy's avatar
William F Godoy committed
386
 * @param descriptor :: input descriptor carrying metadata information
LamarMoore's avatar
LamarMoore committed
387
 */
Peterson, Peter's avatar
Peterson, Peter committed
388
template <typename T>
389
390
391
void adjustTimeOfFlightISISLegacy(
    ::NeXus::File &file, T localWorkspace, const std::string &entry_name,
    const std::string &classType,
William F Godoy's avatar
William F Godoy committed
392
    const Kernel::NexusHDF5Descriptor *descriptor = nullptr) {
Peterson, Peter's avatar
Peterson, Peter committed
393
394
395
396
397
  bool done = false;
  // Go to the root, and then top entry
  file.openPath("/");
  file.openGroup(entry_name, "NXentry");

398
  // NexusHDF5Descriptor
William F Godoy's avatar
William F Godoy committed
399
  if (descriptor != nullptr) {
400
401
402
403
404
405
    // not an ISIS file
    if (!descriptor->isEntry("/" + entry_name + "/detector_1_events")) {
      return;
    }
  }

Peterson, Peter's avatar
Peterson, Peter committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
  using string_map_t = std::map<std::string, std::string>;
  string_map_t entries = file.getEntries();

  if (entries.find("detector_1_events") == entries.end()) { // not an ISIS file
    return;
  }

  // try if monitors have their own bins
  if (classType == "NXmonitor") {
    std::vector<std::string> bankNames;
    for (string_map_t::const_iterator it = entries.begin(); it != entries.end();
         ++it) {
      std::string entry_name(it->first);
      std::string entry_class(it->second);
      if (entry_class == classType) {
421
        bankNames.emplace_back(entry_name);
Peterson, Peter's avatar
Peterson, Peter committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
      }
    }
    for (size_t i = 0; i < bankNames.size(); ++i) {
      const std::string &mon = bankNames[i];
      file.openGroup(mon, classType);
      entries = file.getEntries();
      if (entries.find("event_time_bins") == entries.end()) {
        // bins = entries.find("time_of_flight"); // I think time_of_flight
        // doesn't work here
        // if (bins == entries.end())
        //{
        done = false;
        file.closeGroup();
        break; // done == false => use bins from the detectors
               //}
      }
      done = true;
      makeTimeOfFlightDataFuzzy(file, localWorkspace, "event_time_bins", i,
                                i + 1);
      file.closeGroup();
    }
  }

  if (!done) {
    // first check detector_1_events
    file.openGroup("detector_1_events", "NXevent_data");
    entries = file.getEntries();
    for (string_map_t::const_iterator it = entries.begin(); it != entries.end();
         ++it) {
      if (it->first == "time_of_flight" || it->first == "event_time_bins") {
        makeTimeOfFlightDataFuzzy(file, localWorkspace, it->first);
        done = true;
      }
    }
    file.closeGroup(); // detector_1_events

    if (!done) { // if time_of_flight was not found try
                 // instrument/dae/time_channels_#
      file.openGroup("instrument", "NXinstrument");
      file.openGroup("dae", "IXdae");
      entries = file.getEntries();
      size_t time_channels_number = 0;
      for (string_map_t::const_iterator it = entries.begin();
           it != entries.end(); ++it) {
        // check if there are groups with names "time_channels_#" and select the
        // one with the highest number
        if (it->first.size() > 14 &&
            it->first.substr(0, 14) == "time_channels_") {
          size_t n = boost::lexical_cast<size_t>(it->first.substr(14));
          if (n > time_channels_number) {
            time_channels_number = n;
          }
        }
      }
      if (time_channels_number > 0) // the numbers start with 1
      {
        file.openGroup("time_channels_" + std::to_string(time_channels_number),
                       "IXtime_channels");
        entries = file.getEntries();
        for (string_map_t::const_iterator it = entries.begin();
             it != entries.end(); ++it) {
          if (it->first == "time_of_flight" || it->first == "event_time_bins") {
            makeTimeOfFlightDataFuzzy(file, localWorkspace, it->first);
          }
        }
        file.closeGroup();
      }
      file.closeGroup(); // dae
      file.closeGroup(); // instrument
    }
  }

  // close top entry (or entry given in entry_name)
  file.closeGroup();
}
497

498
499
//-----------------------------------------------------------------------------
/** Load the instrument definition file specified by info in the NXS file.
LamarMoore's avatar
LamarMoore committed
500
501
502
503
504
505
506
507
 *
 *  @param nexusfilename :: Used to pick the instrument.
 *  @param localWorkspace :: Templated workspace in which to put the instrument
 *geometry
 *  @param top_entry_name :: entry name at the top of the NXS file
 *  @param alg :: Handle of the algorithm
 *  @return true if successful
 */
508
template <typename T>
William F Godoy's avatar
William F Godoy committed
509
510
511
bool LoadEventNexus::runLoadInstrument(
    const std::string &nexusfilename, T localWorkspace,
    const std::string &top_entry_name, Algorithm *alg,
512
    const Kernel::NexusHDF5Descriptor *descriptor) {
513
  std::string instrument;
514
515
  std::string instFilename;

William F Godoy's avatar
William F Godoy committed
516
  const bool isNexus =
517
518
519
      (descriptor == nullptr)
          ? LoadGeometry::isNexus(nexusfilename)
          : LoadGeometry::isNexus(nexusfilename, descriptor->getAllEntries());
William F Godoy's avatar
William F Godoy committed
520

521
  // Check if the geometry can be loaded directly from the Nexus file
William F Godoy's avatar
William F Godoy committed
522
  if (isNexus) {
523
524
525
526
527
528
529
530
531
532
    instFilename = nexusfilename;
  } else {
    // Get the instrument name
    ::NeXus::File nxfile(nexusfilename);
    // Start with the base entry
    nxfile.openGroup(top_entry_name, "NXentry");
    // Open the instrument
    nxfile.openGroup("instrument", "NXinstrument");
    try {
      nxfile.openData("name");
533
      instrument = nxfile.getStrData();
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
      alg->getLogger().debug()
          << "Instrument name read from NeXus file is " << instrument << '\n';
    } catch (::NeXus::Exception &) {
      // Try to fall back to isis compatibility options
      nxfile.closeGroup();
      instrument = readInstrumentFromISIS_VMSCompat(nxfile);
      if (instrument.empty()) {
        // Get the instrument name from the file instead
        size_t n = nexusfilename.rfind('/');
        if (n != std::string::npos) {
          std::string temp =
              nexusfilename.substr(n + 1, nexusfilename.size() - n - 1);
          n = temp.find('_');
          if (n != std::string::npos && n > 0) {
            instrument = temp.substr(0, n);
          }
550
551
552
        }
      }
    }
553
554
555
556
557
558
559
560
561
562
563
564
565
    if (instrument == "POWGEN3") // hack for powgen b/c of bad long name
      instrument = "POWGEN";
    if (instrument == "NOM") // hack for nomad
      instrument = "NOMAD";

    if (instrument.empty())
      throw std::runtime_error("Could not find the instrument name in the NXS "
                               "file or using the filename. Cannot load "
                               "instrument!");

    // Now let's close the file as we don't need it anymore to load the
    // instrument.
    nxfile.close();
566
567
568
  }

  // do the actual work
569
570
  Mantid::API::IAlgorithm_sptr loadInst =
      alg->createChildAlgorithm("LoadInstrument");
571
572
573
574

  // Now execute the Child Algorithm. Catch and log any error, but don't stop.
  bool executionSuccessful(true);
  try {
575
    loadInst->setPropertyValue("Filename", instFilename);
576
    loadInst->setPropertyValue("InstrumentName", instrument);
577
578
    loadInst->setProperty<Mantid::API::MatrixWorkspace_sptr>("Workspace",
                                                             localWorkspace);
579
    loadInst->setProperty("RewriteSpectraMap",
580
                          Mantid::Kernel::OptionalBool(false));
581
582
583
584
585
586
587
588
    loadInst->execute();

    // Populate the instrument parameters in this workspace - this works around
    // a bug
    localWorkspace->populateInstrumentParameters();
  } catch (std::invalid_argument &e) {
    alg->getLogger().information()
        << "Invalid argument to LoadInstrument Child Algorithm : " << e.what()
589
        << '\n';
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
    executionSuccessful = false;
  } catch (std::runtime_error &e) {
    alg->getLogger().information(
        "Unable to successfully run LoadInstrument Child Algorithm");
    alg->getLogger().information(e.what());
    executionSuccessful = false;
  }

  // If loading instrument definition file fails
  if (!executionSuccessful) {
    alg->getLogger().error() << "Error loading Instrument definition file\n";
    return false;
  }

  // Ticket #2049: Cleanup all loadinstrument members to a single instance
  // If requested update the instrument to positions in the data file
606
  const auto &pmap = localWorkspace->constInstrumentParameters();
607
608
609
610
  if (!pmap.contains(localWorkspace->getInstrument()->getComponentID(),
                     "det-pos-source"))
    return executionSuccessful;

611
  std::shared_ptr<Geometry::Parameter> updateDets = pmap.get(
612
613
614
615
616
      localWorkspace->getInstrument()->getComponentID(), "det-pos-source");
  std::string value = updateDets->value<std::string>();
  if (value.substr(0, 8) == "datafile") {
    Mantid::API::IAlgorithm_sptr updateInst =
        alg->createChildAlgorithm("UpdateInstrumentFromFile");
617
618
    updateInst->setProperty<Mantid::API::MatrixWorkspace_sptr>("Workspace",
                                                               localWorkspace);
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
    updateInst->setPropertyValue("Filename", nexusfilename);
    if (value == "datafile-ignore-phi") {
      updateInst->setProperty("IgnorePhi", true);
      alg->getLogger().information("Detector positions in IDF updated with "
                                   "positions in the data file except for the "
                                   "phi values");
    } else {
      alg->getLogger().information(
          "Detector positions in IDF updated with positions in the data file");
    }
    // We want this to throw if it fails to warn the user that the information
    // is not correct.
    updateInst->execute();
  }

  return executionSuccessful;
}

//-----------------------------------------------------------------------------
/** Load the run number and other meta data from the given bank */
639
template <typename T>
William F Godoy's avatar
William F Godoy committed
640
641
642
void LoadEventNexus::loadEntryMetadata(
    const std::string &nexusfilename, T WS, const std::string &entry_name,
    const Kernel::NexusHDF5Descriptor &descriptor) {
643
644
645
646
647
  // Open the file
  ::NeXus::File file(nexusfilename);
  file.openGroup(entry_name, "NXentry");

  // get the title
William F Godoy's avatar
William F Godoy committed
648
  if (descriptor.isEntry("/" + entry_name + "/title", "SDS")) {
649
650
651
652
653
654
655
    file.openData("title");
    if (file.getInfo().type == ::NeXus::CHAR) {
      std::string title = file.getStrData();
      if (!title.empty())
        WS->setTitle(title);
    }
    file.closeData();
656
657
658
  }

  // get the notes
William F Godoy's avatar
William F Godoy committed
659
  if (descriptor.isEntry("/" + entry_name + "/notes", "SDS")) {
660
661
662
663
664
665
666
667
668
669
    file.openData("notes");
    if (file.getInfo().type == ::NeXus::CHAR) {
      std::string notes = file.getStrData();
      if (!notes.empty())
        WS->mutableRun().addProperty("file_notes", notes);
    }
    file.closeData();
  }

  // Get the run number
William F Godoy's avatar
William F Godoy committed
670
  if (descriptor.isEntry("/" + entry_name + "/run_number", "SDS")) {
Lamar Moore's avatar
Lamar Moore committed
671
    file.openData("run_number");
672
    std::string run;
Lamar Moore's avatar
Lamar Moore committed
673
674
675
676
677
678
679
680
681
    if (file.getInfo().type == ::NeXus::CHAR) {
      run = file.getStrData();
    } else if (file.isDataInt()) {
      // inside ISIS the run_number type is int32
      std::vector<int> value;
      file.getData(value);
      if (!value.empty())
        run = std::to_string(value[0]);
    }
682
683
684
    if (!run.empty()) {
      WS->mutableRun().addProperty("run_number", run);
    }
Lamar Moore's avatar
Lamar Moore committed
685
    file.closeData();
686
687
688
  }

  // get the experiment identifier
William F Godoy's avatar
William F Godoy committed
689
  if (descriptor.isEntry("/" + entry_name + "/experiment_identifier", "SDS")) {
690
    file.openData("experiment_identifier");
691
    std::string expId;
692
693
694
695
696
697
698
699
700
    if (file.getInfo().type == ::NeXus::CHAR) {
      expId = file.getStrData();
    }
    if (!expId.empty()) {
      WS->mutableRun().addProperty("experiment_identifier", expId);
    }
    file.closeData();
  }

701
702
  // get the sample name - nested try/catch to leave the handle in an
  // appropriate state
William F Godoy's avatar
William F Godoy committed
703
  if (descriptor.isEntry("/" + entry_name + "/sample", "NXsample")) {
704
    file.openGroup("sample", "NXsample");
705
    try {
William F Godoy's avatar
William F Godoy committed
706
      if (descriptor.isEntry("/" + entry_name + "/sample/name", "SDS")) {
707
708
709
710
711
712
713
714
715
716
717
718
719
720
        file.openData("name");
        const auto info = file.getInfo();
        std::string name;
        if (info.type == ::NeXus::CHAR) {
          if (info.dims.size() == 1) {
            name = file.getStrData();
          } else { // something special for 2-d array
            const int64_t total_length = std::accumulate(
                info.dims.begin(), info.dims.end(), static_cast<int64_t>(1),
                std::multiplies<int64_t>());
            boost::scoped_array<char> val_array(new char[total_length]);
            file.getData(val_array.get());
            name = std::string(val_array.get(), total_length);
          }
721
        }
722
723
724
        file.closeData();
        if (!name.empty()) {
          WS->mutableSample().setName(name);
725
726
727
        }
      }
    } catch (::NeXus::Exception &) {
728
      // let it drop on floor if an exception occurs while reading sample
729
730
731
732
733
    }
    file.closeGroup();
  }

  // get the duration
William F Godoy's avatar
William F Godoy committed
734
  if (descriptor.isEntry("/" + entry_name + "/duration", "SDS")) {
735
736
737
738
739
740
    file.openData("duration");
    std::vector<double> duration;
    file.getDataCoerce(duration);
    if (duration.size() == 1) {
      // get the units
      // clang-format off
741
    std::vector< ::NeXus::AttrInfo> infos = file.getAttrInfos();
742
    std::string units;
743
    for (std::vector< ::NeXus::AttrInfo>::const_iterator it = infos.begin();
744
         it != infos.end(); ++it) {
745
      if (it->name == "units") {
746
747
748
749
        units = file.getStrAttr(*it);
        break;
      }
    }
750
      // clang-format on
751

752
753
754
755
      // set the property
      WS->mutableRun().addProperty("duration", duration[0], units);
    }
    file.closeData();
756
757
758
759
760
761
762
  }

  // close the file
  file.close();
}

//-----------------------------------------------------------------------------
763
764
765
/** Load the instrument from the nexus file if property LoadNexusInstrumentXML
 *  is set to true. If instrument XML not found from the IDF file
 *  (specified by the info in the Nexus file) load the IDF.
LamarMoore's avatar
LamarMoore committed
766
767
 *
 *  @param nexusfilename :: The Nexus file name
768
769
 *  @param localWorkspace :: templated workspace in which to put the
 *instrument geometry
LamarMoore's avatar
LamarMoore committed
770
771
772
773
 *  @param top_entry_name :: entry name at the top of the Nexus file
 *  @param alg :: Handle of the algorithm
 *  @return true if successful
 */
774
template <typename T>
William F Godoy's avatar
William F Godoy committed
775
776
777
bool LoadEventNexus::loadInstrument(
    const std::string &nexusfilename, T localWorkspace,
    const std::string &top_entry_name, Algorithm *alg,
778
    const Kernel::NexusHDF5Descriptor *descriptor) {
779

780
781
782
  bool loadNexusInstrumentXML = true;
  if (alg->existsProperty("LoadNexusInstrumentXML"))
    loadNexusInstrumentXML = alg->getProperty("LoadNexusInstrumentXML");
783

784
  bool foundInstrument = false;
785
786
787
  if (loadNexusInstrumentXML)
    foundInstrument = runLoadIDFFromNexus<T>(nexusfilename, localWorkspace,
                                             top_entry_name, alg);
788
  if (!foundInstrument)
789
    foundInstrument = runLoadInstrument<T>(nexusfilename, localWorkspace,
William F Godoy's avatar
William F Godoy committed
790
                                           top_entry_name, alg, descriptor);
791
792
793
794
795
  return foundInstrument;
}

//-----------------------------------------------------------------------------
/** Load the instrument from the nexus file
LamarMoore's avatar
LamarMoore committed
796
797
 *
 *  @param nexusfilename :: The name of the nexus file being loaded
798
799
 *  @param localWorkspace :: templated workspace in which to put the
 *instrument geometry
LamarMoore's avatar
LamarMoore committed
800
801
802
803
 *  @param top_entry_name :: entry name at the top of the Nexus file
 *  @param alg :: Handle of the algorithm
 *  @return true if successful
 */
804
805
806
807
808
template <typename T>
bool LoadEventNexus::runLoadIDFFromNexus(const std::string &nexusfilename,
                                         T localWorkspace,
                                         const std::string &top_entry_name,
                                         Algorithm *alg) {
809
810
811
812
813
  // Test if IDF exists in file, move on quickly if not
  try {
    ::NeXus::File nxsfile(nexusfilename);
    nxsfile.openPath(top_entry_name + "/instrument/instrument_xml");
  } catch (::NeXus::Exception &) {
814
    alg->getLogger().information("No instrument XML definition found in " +
815
816
817
818
819
                                 nexusfilename + " at " + top_entry_name +
                                 "/instrument");
    return false;
  }

820
821
  Mantid::API::IAlgorithm_sptr loadInst =
      alg->createChildAlgorithm("LoadIDFFromNexus");
822
823
824
825

  // Now execute the Child Algorithm. Catch and log any error, but don't stop.
  try {
    loadInst->setPropertyValue("Filename", nexusfilename);
826
827
    loadInst->setProperty<Mantid::API::MatrixWorkspace_sptr>("Workspace",
                                                             localWorkspace);
828
829
830
831
832
833
    loadInst->setPropertyValue("InstrumentParentPath", top_entry_name);
    loadInst->execute();
  } catch (std::invalid_argument &) {
    alg->getLogger().error(
        "Invalid argument to LoadIDFFromNexus Child Algorithm ");
  } catch (std::runtime_error &) {
834
835
836
    alg->getLogger().debug(
        "No instrument definition found by LoadIDFFromNexus in " +
        nexusfilename + " at " + top_entry_name + "/instrument");
837
838
839
840
841
842
  }

  if (!loadInst->isExecuted())
    alg->getLogger().information("No IDF loaded from Nexus file.");
  return loadInst->isExecuted();
}
843
} // namespace DataHandling
844
} // namespace Mantid