-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadEventNexus.cpp
1661 lines (1483 loc) · 68.6 KB
/
LoadEventNexus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
296
297
298
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/LoadEventNexus.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/Sample.h"
#include "MantidDataHandling/DefaultEventLoader.h"
#include "MantidDataHandling/EventWorkspaceCollection.h"
#include "MantidDataHandling/LoadEventNexusIndexSetup.h"
#include "MantidDataHandling/LoadHelper.h"
#include "MantidDataHandling/ParallelEventLoader.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Goniometer.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidIndexing/IndexInfo.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/DateAndTimeHelpers.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MultiThreaded.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VisibleWhenProperty.h"
#include "MantidNexus/NexusIOHelper.h"
#include <H5Cpp.h>
#include <memory>
#include <regex>
using Mantid::Types::Core::DateAndTime;
using std::map;
using std::string;
using std::vector;
namespace Mantid::DataHandling {
DECLARE_NEXUS_HDF5_FILELOADER_ALGORITHM(LoadEventNexus)
using namespace Kernel;
using namespace DateAndTimeHelpers;
using namespace Geometry;
using namespace API;
using namespace DataObjects;
using Types::Core::DateAndTime;
namespace {
// detnotes the end of iteration for NeXus::getNextEntry
const std::string NULL_STR("NULL");
} // namespace
/**
* Based on the current group in the file, does the named sub-entry exist?
* @param file : File handle. This is not modified, but cannot be const
* @param name : sub entry name to look for
* @return true only if it exists
*/
bool exists(::NeXus::File &file, const std::string &name) {
const auto entries = file.getEntries();
return exists(entries, name);
}
bool exists(const std::map<std::string, std::string> &entries, const std::string &name) {
return entries.find(name) != entries.end();
}
//----------------------------------------------------------------------------------------------
/** Empty default constructor
*/
LoadEventNexus::LoadEventNexus()
: filter_tof_min(0), filter_tof_max(0), m_specMin(0), m_specMax(0), longest_tof(0), shortest_tof(0), bad_tofs(0),
discarded_events(0), compressTolerance(0), m_instrument_loaded_correctly(false), loadlogs(false),
event_id_is_spec(false) {}
//----------------------------------------------------------------------------------------------
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not
* be used
*/
int LoadEventNexus::confidence(Kernel::NexusHDF5Descriptor &descriptor) const {
int confidence = 0;
const std::map<std::string, std::set<std::string>> &allEntries = descriptor.getAllEntries();
if (allEntries.count("NXevent_data") == 1) {
if (descriptor.isEntry("/entry", "NXentry") || descriptor.isEntry("/raw_data_1", "NXentry")) {
confidence = 80;
}
}
return confidence;
}
//----------------------------------------------------------------------------------------------
/** Initialisation method.
*/
void LoadEventNexus::init() {
const std::vector<std::string> exts{".nxs.h5", ".nxs", "_event.nxs"};
this->declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
"The name of the Event NeXus file to read, including its full or "
"relative path. "
"The file name is typically of the form INST_####_event.nxs (N.B. case "
"sensitive if running on Linux).");
this->declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
"The name of the output EventWorkspace or WorkspaceGroup in which to "
"load the EventNexus file.");
declareProperty(std::make_unique<PropertyWithValue<string>>("NXentryName", "", Direction::Input),
"Optional: Name of the NXentry to load if it's not the default.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterByTofMin", EMPTY_DBL(), Direction::Input),
"Optional: To exclude events that do not fall within a range "
"of times-of-flight. "
"This is the minimum accepted value in microseconds. Keep "
"blank to load all events.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterByTofMax", EMPTY_DBL(), Direction::Input),
"Optional: To exclude events that do not fall within a range "
"of times-of-flight. "
"This is the maximum accepted value in microseconds. Keep "
"blank to load all events.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterByTimeStart", EMPTY_DBL(), Direction::Input),
"Optional: To only include events after the provided start "
"time, in seconds (relative to the start of the run).");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterByTimeStop", EMPTY_DBL(), Direction::Input),
"Optional: To only include events before the provided stop "
"time, in seconds (relative to the start of the run).");
std::string grp1 = "Filter Events";
setPropertyGroup("FilterByTofMin", grp1);
setPropertyGroup("FilterByTofMax", grp1);
setPropertyGroup("FilterByTimeStart", grp1);
setPropertyGroup("FilterByTimeStop", grp1);
declareProperty(std::make_unique<ArrayProperty<string>>("BankName", Direction::Input),
"Optional: To only include events from one bank. Any bank "
"whose name does not match the given string will have no "
"events.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("SingleBankPixelsOnly", true, Direction::Input),
"Optional: Only applies if you specified a single bank to "
"load with BankName. "
"Only pixels in the specified bank will be created if true; "
"all of the instrument's pixels will be created otherwise.");
setPropertySettings("SingleBankPixelsOnly", std::make_unique<VisibleWhenProperty>("BankName", IS_NOT_DEFAULT));
std::string grp2 = "Loading a Single Bank";
setPropertyGroup("BankName", grp2);
setPropertyGroup("SingleBankPixelsOnly", grp2);
declareProperty(std::make_unique<PropertyWithValue<bool>>("Precount", true, Direction::Input),
"Pre-count the number of events in each pixel before allocating memory "
"(optional, default True). "
"This can significantly reduce memory use and memory fragmentation; it "
"may also speed up loading.");
declareProperty(std::make_unique<PropertyWithValue<double>>("CompressTolerance", -1.0, Direction::Input),
"Run CompressEvents while loading (optional, leave blank or "
"negative to not do). "
"This specified the tolerance to use (in microseconds) when "
"compressing.");
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty("ChunkNumber", EMPTY_INT(), mustBePositive,
"If loading the file by sections ('chunks'), this is the "
"section number of this execution of the algorithm.");
declareProperty("TotalChunks", EMPTY_INT(), mustBePositive,
"If loading the file by sections ('chunks'), this is the "
"total number of sections.");
// TotalChunks is only meaningful if ChunkNumber is set
// Would be nice to be able to restrict ChunkNumber to be <= TotalChunks at
// validation
setPropertySettings("TotalChunks", std::make_unique<VisibleWhenProperty>("ChunkNumber", IS_NOT_DEFAULT));
std::string grp3 = "Reduce Memory Use";
setPropertyGroup("Precount", grp3);
setPropertyGroup("CompressTolerance", grp3);
setPropertyGroup("ChunkNumber", grp3);
setPropertyGroup("TotalChunks", grp3);
declareProperty(std::make_unique<PropertyWithValue<bool>>("LoadMonitors", false, Direction::Input),
"Load the monitors from the file (optional, default False).");
std::vector<std::string> options{"", "Events", "Histogram"};
declareProperty("MonitorsLoadOnly", "", std::make_shared<Kernel::StringListValidator>(options),
"If multiple repesentations exist, which one to load. "
"Default is to load the one that is present.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterMonByTofMin", EMPTY_DBL(), Direction::Input),
"Optional: To exclude events from monitors that do not fall "
"within a range of times-of-flight. "
"This is the minimum accepted value in microseconds.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterMonByTofMax", EMPTY_DBL(), Direction::Input),
"Optional: To exclude events from monitors that do not fall "
"within a range of times-of-flight. "
"This is the maximum accepted value in microseconds.");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterMonByTimeStart", EMPTY_DBL(), Direction::Input),
"Optional: To only include events from monitors after the "
"provided start time, in seconds (relative to the start of "
"the run).");
declareProperty(std::make_unique<PropertyWithValue<double>>("FilterMonByTimeStop", EMPTY_DBL(), Direction::Input),
"Optional: To only include events from monitors before the "
"provided stop time, in seconds (relative to the start of "
"the run).");
setPropertySettings("MonitorsLoadOnly", std::make_unique<VisibleWhenProperty>("LoadMonitors", IS_EQUAL_TO, "1"));
auto asEventsIsOn = [] {
std::unique_ptr<IPropertySettings> prop = std::make_unique<VisibleWhenProperty>("LoadMonitors", IS_EQUAL_TO, "1");
return prop;
};
setPropertySettings("FilterMonByTofMin", asEventsIsOn());
setPropertySettings("FilterMonByTofMax", asEventsIsOn());
setPropertySettings("FilterMonByTimeStart", asEventsIsOn());
setPropertySettings("FilterMonByTimeStop", asEventsIsOn());
std::string grp4 = "Monitors";
setPropertyGroup("LoadMonitors", grp4);
setPropertyGroup("MonitorsLoadOnly", grp4);
setPropertyGroup("FilterMonByTofMin", grp4);
setPropertyGroup("FilterMonByTofMax", grp4);
setPropertyGroup("FilterMonByTimeStart", grp4);
setPropertyGroup("FilterMonByTimeStop", grp4);
declareProperty("SpectrumMin", EMPTY_INT(), mustBePositive, "The number of the first spectrum to read.");
declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive, "The number of the last spectrum to read.");
declareProperty(std::make_unique<ArrayProperty<int32_t>>("SpectrumList"),
"A comma-separated list of individual spectra to read.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("MetaDataOnly", false, Direction::Input),
"If true, only the meta data and sample logs will be loaded.");
declareProperty(std::make_unique<PropertyWithValue<bool>>("LoadLogs", true, Direction::Input),
"Load only the Sample/DAS logs from the file (default True).");
declareProperty(std::make_unique<PropertyWithValue<bool>>("LoadAllLogs", false, Direction::Input),
"Load all the logs from the nxs, without checking or processing them; if checked, LoadLogs will be "
"ignored; use with caution");
std::vector<std::string> loadType{"Default"};
#ifndef _WIN32
loadType.emplace_back("Multiprocess (experimental)");
#endif // _WIN32
#ifdef MPI_EXPERIMENTAL
loadType.emplace_back("MPI");
#endif // MPI_EXPERIMENTAL
auto loadTypeValidator = std::make_shared<StringListValidator>(loadType);
declareProperty("LoadType", "Default", loadTypeValidator,
"Set type of loader. 2 options {Default, Multiproceess},"
"'Multiprocess' should work faster for big files and it is "
"experimental, available only in Linux");
declareProperty(std::make_unique<PropertyWithValue<bool>>("LoadNexusInstrumentXML", true, Direction::Input),
"Reads the embedded Instrument XML from the NeXus file "
"(optional, default True). ");
declareProperty("NumberOfBins", 500, mustBePositive,
"The number of bins intially defined. Use Rebin to change "
"the binning later. If there is no data loaded, or you "
"select meta data only you will only get 1 bin.");
// Flexible log loading
declareProperty(std::make_unique<PropertyWithValue<std::vector<std::string>>>("AllowList", std::vector<std::string>(),
Direction::Input),
"If specified, only these logs will be loaded from the file (each "
"separated by a space).");
declareProperty(std::make_unique<PropertyWithValue<std::vector<std::string>>>("BlockList", std::vector<std::string>(),
Direction::Input),
"If specified, these logs will NOT be loaded from the file (each "
"separated by a space).");
}
//----------------------------------------------------------------------------------------------
/** set the name of the top level NXentry m_top_entry_name
*/
void LoadEventNexus::setTopEntryName() {
std::string nxentryProperty = getProperty("NXentryName");
if (!nxentryProperty.empty()) {
m_top_entry_name = nxentryProperty;
return;
}
try {
while (true) {
const auto entry = m_file->getNextEntry();
if (entry.second == "NXentry") {
if ((entry.first == "entry") || (entry.first == "raw_data_1")) {
m_top_entry_name = entry.first;
break;
}
} else if (entry.first == NULL_STR && entry.second == NULL_STR) {
g_log.error() << "Unable to determine name of top level NXentry - assuming "
"\"entry\".\n";
m_top_entry_name = "entry";
break;
}
}
} catch (const std::exception &) {
g_log.error() << "Unable to determine name of top level NXentry - assuming "
"\"entry\".\n";
m_top_entry_name = "entry";
}
}
template <typename T> void LoadEventNexus::filterDuringPause(T workspace) {
try {
if ((!ConfigService::Instance().hasProperty("loadeventnexus.keeppausedevents")) &&
(m_ws->run().getLogData("pause")->size() > 1)) {
g_log.notice("Filtering out events when the run was marked as paused. "
"Set the loadeventnexus.keeppausedevents configuration "
"property to override this.");
auto filter = createChildAlgorithm("FilterByLogValue");
filter->setProperty("InputWorkspace", workspace);
filter->setProperty("OutputWorkspace", workspace);
filter->setProperty("LogName", "pause");
// The log value is set to 1 when the run is paused, 0 otherwise.
filter->setProperty("MinimumValue", 0.0);
filter->setProperty("MaximumValue", 0.0);
filter->setProperty("LogBoundary", "Left");
filter->execute();
}
} catch (Exception::NotFoundError &) {
// No "pause" log, just carry on
}
}
template <>
void LoadEventNexus::filterDuringPause<EventWorkspaceCollection_sptr>(EventWorkspaceCollection_sptr workspace) {
// We provide a function pointer to the filter method of the object
using std::placeholders::_1;
auto func = std::bind(&LoadEventNexus::filterDuringPause<MatrixWorkspace_sptr>, this, _1);
workspace->applyFilterInPlace(func);
}
//-----------------------------------------------------------------------------
/**
* Filter the events by pulse time - no in place version so have to return workspace
*
*/
template <typename T>
T LoadEventNexus::filterEventsByTime(T workspace, Mantid::Types::Core::DateAndTime &startTime,
Mantid::Types::Core::DateAndTime &stopTime) {
auto filterByTime = createChildAlgorithm("FilterByTime");
g_log.information("Filtering events by time...");
filterByTime->setProperty("InputWorkspace", workspace);
// sample log already filtered by time so use absolute times to be safe
filterByTime->setProperty("AbsoluteStartTime", startTime.toISO8601String());
filterByTime->setProperty("AbsoluteStopTime", stopTime.toISO8601String());
filterByTime->execute();
return filterByTime->getProperty("OutputWorkspace");
}
template <>
EventWorkspaceCollection_sptr
LoadEventNexus::filterEventsByTime<EventWorkspaceCollection_sptr>(EventWorkspaceCollection_sptr workspace,
Mantid::Types::Core::DateAndTime &startTime,
Mantid::Types::Core::DateAndTime &stopTime) {
// We provide a function pointer to the filter method of the object
using std::placeholders::_1;
auto func = std::bind(&LoadEventNexus::filterEventsByTime<EventWorkspace_sptr>, this, _1, startTime, stopTime);
workspace->applyFilter(func);
return workspace;
}
//------------------------------------------------------------------------------------------------
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*/
void LoadEventNexus::execLoader() {
// Retrieve the filename from the properties
m_filename = getPropertyValue("Filename");
compressTolerance = getProperty("CompressTolerance");
loadlogs = getProperty("LoadLogs");
// Check to see if the monitors need to be loaded later
bool load_monitors = this->getProperty("LoadMonitors");
// this must make absolutely sure that m_file is a valid (and open)
// NeXus::File object
safeOpenFile(m_filename);
setTopEntryName();
// Initialize progress reporting.
int reports = 3;
if (load_monitors)
reports++;
Progress prog(this, 0.0, 0.3, reports);
// Load the detector events
m_ws = std::make_shared<EventWorkspaceCollection>(); // Algorithm currently
// relies on an
// object-level workspace ptr
loadEvents(&prog, false); // Do not load monitor blocks
if (discarded_events > 0) {
g_log.information() << discarded_events
<< " events were encountered coming from pixels which "
"are not in the Instrument Definition File."
"These events were discarded.\n";
}
// If the run was paused at any point, filter out those events (SNS only, I
// think)
filterDuringPause(m_ws->getSingleHeldWorkspace());
// add filename
m_ws->mutableRun().addProperty("Filename", m_filename);
// Save output
this->setProperty("OutputWorkspace", m_ws->combinedWorkspace());
// close the file since LoadNexusMonitors will take care of its own file
// handle
m_file->close();
// Load the monitors with child algorithm 'LoadNexusMonitors'
if (load_monitors) {
prog.report("Loading monitors");
this->runLoadMonitors();
}
}
std::pair<DateAndTime, DateAndTime> firstLastPulseTimes(::NeXus::File &file, Kernel::Logger &logger) {
file.openData("event_time_zero");
std::string isooffset; // ISO8601 offset
DateAndTime offset;
// According to the Nexus standard, if the offset is not present, it implies
// the offset is and absolute timestamp, which is relative to the start of
// Unix epoch (https://manual.nexusformat.org/classes/base_classes/NXlog.html)
if (!file.hasAttr("offset")) {
offset = DateAndTime("1970-01-01T00:00:00Z");
logger.warning("In firstLastPulseTimes: no ISO8601 offset attribute "
"provided for event_time_zero, using UNIX epoch instead");
} else {
file.getAttr("offset", isooffset);
offset = DateAndTime(isooffset);
}
std::string units; // time units
if (file.hasAttr("units"))
file.getAttr("units", units);
// Read in the pulse times
auto pulse_times = Mantid::NeXus::NeXusIOHelper::readNexusVector<double>(file, "event_time_zero");
// Remember to close the entry
file.closeData();
if (pulse_times.empty()) {
throw std::invalid_argument("Cannot find run start; event_time_zero contains no pulse times");
}
// Convert to seconds
auto conv = Kernel::Units::timeConversionValue(units, "s");
return std::make_pair(DateAndTime(pulse_times.front() * conv, 0.0) + offset.totalNanoseconds(),
DateAndTime(pulse_times.back() * conv, 0.0) + offset.totalNanoseconds());
} // namespace DataHandling
/**
* Get the number of events in the currently opened group.
*
* @param file The handle to the nexus file opened to the group to look at.
* @param hasTotalCounts Whether to try looking at the total_counts field.
* This variable will be changed if the field is not there.
* @param oldNeXusFileNames Whether to try using old names. This variable will
* be changed if it is determined that old names are being used.
* @param prefix current entry name prefix (e.g. /entry)
* @param descriptor input containing metadata information
* @return The number of events.
*/
std::size_t numEvents(::NeXus::File &file, bool &hasTotalCounts, bool &oldNeXusFileNames, const std::string &prefix,
const NexusHDF5Descriptor &descriptor) {
// try getting the value of total_counts
if (hasTotalCounts) {
hasTotalCounts = false;
if (descriptor.isEntry(prefix + "/total_counts")) {
try {
file.openData("total_counts");
auto info = file.getInfo();
file.closeData();
if (info.type == ::NeXus::UINT64) {
uint64_t eventCount;
file.readData("total_counts", eventCount);
hasTotalCounts = true;
return eventCount;
}
} catch (::NeXus::Exception &) {
}
}
}
// just get the length of the event pixel ids
try {
if (oldNeXusFileNames)
file.openData("event_pixel_id");
else
file.openData("event_id");
} catch (::NeXus::Exception &) {
// Older files (before Nov 5, 2010) used this field.
try {
file.openData("event_pixel_id");
oldNeXusFileNames = true;
} catch (::NeXus::Exception &) {
// Some groups have neither indicating there are not events here
return 0;
}
}
size_t numEvents = static_cast<std::size_t>(file.getInfo().dims[0]);
file.closeData();
return numEvents;
}
/** Load the log from the nexus file
*
* @param nexusfilename :: The name of the nexus file being loaded
* @param localWorkspace :: Templated workspace in which to put the instrument
*geometry
* @param alg :: Handle of the algorithm
* @param returnpulsetimes :: flag to return shared pointer for
*BankPulseTimes, otherwise NULL.
* @param nPeriods : Number of periods (write to)
* @param periodLog : Period logs DateAndTime to int map.
*
* @return Pulse times given in the DAS logs
*/
template <typename T>
std::shared_ptr<BankPulseTimes>
LoadEventNexus::runLoadNexusLogs(const std::string &nexusfilename, T localWorkspace, API::Algorithm &alg,
bool returnpulsetimes, int &nPeriods,
std::unique_ptr<const TimeSeriesProperty<int>> &periodLog) {
// --------------------- Load DAS Logs -----------------
// The pulse times will be empty if not specified in the DAS logs.
// BankPulseTimes * out = NULL;
std::shared_ptr<BankPulseTimes> out;
auto loadLogs = alg.createChildAlgorithm("LoadNexusLogs");
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
try {
alg.getLogger().information() << "Loading logs from NeXus file..."
<< "\n";
loadLogs->setPropertyValue("Filename", nexusfilename);
loadLogs->setProperty<API::MatrixWorkspace_sptr>("Workspace", localWorkspace);
try {
loadLogs->setPropertyValue("NXentryName", alg.getPropertyValue("NXentryName"));
} catch (...) {
}
loadLogs->execute();
const Run &run = localWorkspace->run();
// Get the number of periods
if (run.hasProperty("nperiods")) {
nPeriods = run.getPropertyValueAsType<int>("nperiods");
}
// Get the period log. Map of DateAndTime to Period int values.
if (run.hasProperty("period_log")) {
auto *temp = run.getProperty("period_log");
// Check for corrupted period logs
std::unique_ptr<TimeSeriesProperty<int>> tempPeriodLog(dynamic_cast<TimeSeriesProperty<int> *>(temp->clone()));
checkForCorruptedPeriods(std::move(tempPeriodLog), periodLog, nPeriods, nexusfilename);
}
// If successful, we can try to load the pulse times
std::vector<Types::Core::DateAndTime> temp;
if (localWorkspace->run().hasProperty("proton_charge")) {
auto *log =
dynamic_cast<Kernel::TimeSeriesProperty<double> *>(localWorkspace->mutableRun().getProperty("proton_charge"));
if (log)
temp = log->timesAsVector();
}
if (returnpulsetimes)
out = std::make_shared<BankPulseTimes>(temp);
// Use the first pulse as the run_start time.
if (!temp.empty()) {
if (temp[0] < Types::Core::DateAndTime("1991-01-01T00:00:00"))
alg.getLogger().warning() << "Found entries in the proton_charge "
"sample log with invalid pulse time!\n";
Types::Core::DateAndTime run_start = localWorkspace->getFirstPulseTime();
// add the start of the run as a ISO8601 date/time string. The start =
// first non-zero time.
// (this is used in LoadInstrument to find the right instrument file to
// use).
localWorkspace->mutableRun().addProperty("run_start", run_start.toISO8601String(), true);
} else if (run.hasProperty("start_time")) {
localWorkspace->mutableRun().addProperty("run_start", run.getProperty("start_time")->value(), true);
} else {
alg.getLogger().warning() << "Empty proton_charge sample log. You will "
"not be able to filter by time.\n";
}
/// Attempt to make a gonoimeter from the logs
try {
Geometry::Goniometer gm;
gm.makeUniversalGoniometer();
localWorkspace->mutableRun().setGoniometer(gm, true);
} catch (std::runtime_error &) {
}
} catch (const InvalidLogPeriods &) {
// Rethrow so LoadEventNexus fails.
// If we don't, Mantid will crash.
throw;
} catch (...) {
alg.getLogger().error() << "Error while loading Logs from SNS Nexus. Some "
"sample logs may be missing."
<< "\n";
return out;
}
return out;
}
/** Load the log from the nexus file
*
* @param nexusfilename :: The name of the nexus file being loaded
* @param localWorkspace :: Templated workspace in which to put the instrument
*geometry
* @param alg :: Handle of the algorithm
* @param returnpulsetimes :: flag to return shared pointer for
*BankPulseTimes, otherwise NULL.
* @param nPeriods : Number of periods (write to)
* @param periodLog : Period logs DateAndTime to int map.
* @param allow_list: list of properties that will be loaded
* @param block_list: list of properties that will be excluded from loading
*
* @return Pulse times given in the DAS logs
*/
template <typename T>
std::shared_ptr<BankPulseTimes> LoadEventNexus::runLoadNexusLogs(
const std::string &nexusfilename, T localWorkspace, API::Algorithm &alg, bool returnpulsetimes, int &nPeriods,
std::unique_ptr<const TimeSeriesProperty<int>> &periodLog, const std::vector<std::string> &allow_list,
const std::vector<std::string> &block_list) {
// --------------------- Load DAS Logs -----------------
// The pulse times will be empty if not specified in the DAS logs.
// BankPulseTimes * out = NULL;
std::shared_ptr<BankPulseTimes> out;
auto loadLogs = alg.createChildAlgorithm("LoadNexusLogs");
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
try {
alg.getLogger().information() << "Loading logs from NeXus file..."
<< "\n";
loadLogs->setPropertyValue("Filename", nexusfilename);
loadLogs->setProperty<API::MatrixWorkspace_sptr>("Workspace", localWorkspace);
loadLogs->setProperty<std::vector<std::string>>("AllowList", allow_list);
loadLogs->setProperty<std::vector<std::string>>("BlockList", block_list);
try {
loadLogs->setPropertyValue("NXentryName", alg.getPropertyValue("NXentryName"));
} catch (...) {
}
loadLogs->execute();
const Run &run = localWorkspace->run();
// Get the number of periods
if (run.hasProperty("nperiods")) {
nPeriods = run.getPropertyValueAsType<int>("nperiods");
}
// Get the period log. Map of DateAndTime to Period int values.
if (run.hasProperty("period_log")) {
auto *temp = run.getProperty("period_log");
// Check for corrupted period logs
std::unique_ptr<TimeSeriesProperty<int>> tempPeriodLog(dynamic_cast<TimeSeriesProperty<int> *>(temp->clone()));
checkForCorruptedPeriods(std::move(tempPeriodLog), periodLog, nPeriods, nexusfilename);
}
// If successful, we can try to load the pulse times
std::vector<Types::Core::DateAndTime> temp;
if (localWorkspace->run().hasProperty("proton_charge")) {
auto *log =
dynamic_cast<Kernel::TimeSeriesProperty<double> *>(localWorkspace->mutableRun().getProperty("proton_charge"));
if (log)
temp = log->timesAsVector();
}
if (returnpulsetimes)
out = std::make_shared<BankPulseTimes>(temp);
// Use the first pulse as the run_start time.
if (!temp.empty()) {
if (temp[0] < Types::Core::DateAndTime("1991-01-01T00:00:00"))
alg.getLogger().warning() << "Found entries in the proton_charge "
"sample log with invalid pulse time!\n";
Types::Core::DateAndTime run_start = localWorkspace->getFirstPulseTime();
// add the start of the run as a ISO8601 date/time string. The start =
// first non-zero time.
// (this is used in LoadInstrument to find the right instrument file to
// use).
localWorkspace->mutableRun().addProperty("run_start", run_start.toISO8601String(), true);
} else {
alg.getLogger().warning() << "Empty proton_charge sample log. You will "
"not be able to filter by time.\n";
}
/// Attempt to make a gonoimeter from the logs
try {
Geometry::Goniometer gm;
gm.makeUniversalGoniometer();
localWorkspace->mutableRun().setGoniometer(gm, true);
} catch (std::runtime_error &) {
}
} catch (const InvalidLogPeriods &) {
// Rethrow so LoadEventNexus fails.
// If we don't, Mantid will crash.
throw;
} catch (...) {
alg.getLogger().error() << "Error while loading Logs from SNS Nexus. Some "
"sample logs may be missing."
<< "\n";
return out;
}
return out;
}
/** Check for corrupted period logs
* If data is historical (1 periods, period is labelled 0) then change period
* labels to 1 If number of periods does not match expected number of periods
* then throw an error
* @param tempPeriodLog :: a temporary local copy of period logs, which will
* change
* @param periodLog :: unique pointer which will point to period logs once they
* have been changed
* @param nPeriods :: the value in the nperiods log of the run. Number of
* expected periods
* @param nexusfilename :: the filename of the run to load
*/
void LoadEventNexus::checkForCorruptedPeriods(std::unique_ptr<TimeSeriesProperty<int>> tempPeriodLog,
std::unique_ptr<const TimeSeriesProperty<int>> &periodLog,
const int &nPeriods, const std::string &nexusfilename) {
const auto valuesAsVector = tempPeriodLog->valuesAsVector();
const auto nPeriodsInLog = *std::max_element(valuesAsVector.begin(), valuesAsVector.end());
// Check for historic files
if (nPeriodsInLog == 0 && nPeriods == 1) {
// "modernize" the local copy here by making period_log
// a vector of 1s
const std::vector<int> newValues(tempPeriodLog->realSize(), 1);
const auto times = tempPeriodLog->timesAsVector();
periodLog.reset(new const TimeSeriesProperty<int>("period_log", times, newValues));
} else if (nPeriodsInLog != nPeriods) {
// Sanity check here that period_log only contains period numbers up to
// nperiods. These values can be different due to instrument noise, and
// cause undescriptive crashes if not caught.
// We throw here to make it clear
// that the file is corrupted and must be manually assessed.
const auto msg = "File " + nexusfilename +
" has been corrupted. The log framelog/period_log/value "
"contains " +
std::to_string(nPeriodsInLog) + " periods, but periods/number contains " +
std::to_string(nPeriods) + ". This file should be manually inspected and corrected.";
throw InvalidLogPeriods(msg);
} else {
// periodLog should point to a copy of the period logs
periodLog = std::make_unique<const TimeSeriesProperty<int>>(*tempPeriodLog);
tempPeriodLog.reset();
}
}
/** Load the instrument from the nexus file
*
* @param nexusfilename :: The name of the nexus file being loaded
* @param localWorkspace :: EventWorkspaceCollection in which to put the
*instrument
*geometry
* @param alg :: Handle of the algorithm
* @param returnpulsetimes :: flag to return shared pointer for
*BankPulseTimes, otherwise NULL.
* @param nPeriods : Number of periods (write to)
* @param periodLog : Period logs DateAndTime to int map.
*
* @return Pulse times given in the DAS logs
*/
template <>
std::shared_ptr<BankPulseTimes> LoadEventNexus::runLoadNexusLogs<EventWorkspaceCollection_sptr>(
const std::string &nexusfilename, EventWorkspaceCollection_sptr localWorkspace, API::Algorithm &alg,
bool returnpulsetimes, int &nPeriods, std::unique_ptr<const TimeSeriesProperty<int>> &periodLog) {
auto ws = localWorkspace->getSingleHeldWorkspace();
auto ret = runLoadNexusLogs<MatrixWorkspace_sptr>(nexusfilename, ws, alg, returnpulsetimes, nPeriods, periodLog);
return ret;
}
/** Load the instrument from the nexus file
*
* @param nexusfilename :: The name of the nexus file being loaded
* @param localWorkspace :: EventWorkspaceCollection in which to put the
*instrument
*geometry
* @param alg :: Handle of the algorithm
* @param returnpulsetimes :: flag to return shared pointer for
*BankPulseTimes, otherwise NULL.
* @param nPeriods : Number of periods (write to)
* @param periodLog : Period logs DateAndTime to int map.
* @param allow_list: log entry that will be loaded
* @param block_list: log entry that will be excluded
*
* @return Pulse times given in the DAS logs
*/
template <>
std::shared_ptr<BankPulseTimes> LoadEventNexus::runLoadNexusLogs<EventWorkspaceCollection_sptr>(
const std::string &nexusfilename, EventWorkspaceCollection_sptr localWorkspace, API::Algorithm &alg,
bool returnpulsetimes, int &nPeriods, std::unique_ptr<const TimeSeriesProperty<int>> &periodLog,
const std::vector<std::string> &allow_list, const std::vector<std::string> &block_list) {
auto ws = localWorkspace->getSingleHeldWorkspace();
auto ret = runLoadNexusLogs<MatrixWorkspace_sptr>(nexusfilename, ws, alg, returnpulsetimes, nPeriods, periodLog,
allow_list, block_list);
return ret;
}
enum class LoadEventNexus::LoaderType { MPI, MULTIPROCESS, DEFAULT };
//-----------------------------------------------------------------------------
/**
* Load events from the file.
* @param prog :: A pointer to the progress reporting object
* @param monitors :: If true the events from the monitors are loaded and not
*the main banks
*
* This also loads the instrument, but only if it has not been set in the
*workspace
* being used as input (m_ws data member). Same applies to the logs.
*/
void LoadEventNexus::loadEvents(API::Progress *const prog, const bool monitors) {
bool metaDataOnly = getProperty("MetaDataOnly");
// Get the time filters
setTimeFilters(monitors);
// Get the log filter if provided
std::vector<std::string> allow_list = getProperty("AllowList");
std::vector<std::string> block_list = getProperty("BlockList");
// The run_start will be loaded from the pulse times.
DateAndTime run_start(0, 0);
bool takeTimesFromEvents = false;
// Initialize the counter of bad TOFs
bad_tofs = 0;
int nPeriods = 1;
auto periodLog = std::make_unique<const TimeSeriesProperty<int>>("period_log");
bool loadAllLogs = getProperty("LoadAllLogs");
if (loadlogs) {
if (!loadAllLogs) {
prog->doReport("Loading DAS logs");
if (allow_list.empty() && block_list.empty()) {
m_allBanksPulseTimes =
runLoadNexusLogs<EventWorkspaceCollection_sptr>(m_filename, m_ws, *this, true, nPeriods, periodLog);
} else {
m_allBanksPulseTimes = runLoadNexusLogs<EventWorkspaceCollection_sptr>(m_filename, m_ws, *this, true, nPeriods,
periodLog, allow_list, block_list);
}
try {
run_start = m_ws->getFirstPulseTime();
} catch (Kernel::Exception::NotFoundError &) {
/*
This is added to (a) support legacy behaviour of continuing to take
times from the proto_charge log, but (b) allowing a fall back of
getting run start and end from actual pulse times within the
NXevent_data group. Note that the latter is better Nexus compliant.
*/
takeTimesFromEvents = true;
}
} else {
prog->doReport("Loading all logs");
// Open NeXus file
NXhandle nxHandle;
NXstatus nxStat = NXopen(m_filename.c_str(), NXACC_READ, &nxHandle);
if (nxStat != NX_ERROR) {
LoadHelper loadHelper;
loadHelper.addNexusFieldsToWsRun(nxHandle, m_ws->mutableRun(), "", true);
NXclose(&nxHandle);
}
}
} else {
g_log.information() << "Skipping the loading of sample logs!\n"
<< "Reading the start time directly from /" << m_top_entry_name << "/start_time\n";
// start_time is read and set
m_file->openPath("/");
m_file->openGroup(m_top_entry_name, "NXentry");
std::string tmp;
m_file->readData("start_time", tmp);
m_file->closeGroup();
run_start = createFromSanitizedISO8601(tmp);
m_ws->mutableRun().addProperty("run_start", run_start.toISO8601String(), true);
}
// set more properties on the workspace
const std::shared_ptr<NexusHDF5Descriptor> descriptor = getFileInfo();
try {
// this is a static method that is why it is passing the
// file object and the file path
loadEntryMetadata<EventWorkspaceCollection_sptr>(m_filename, m_ws, m_top_entry_name, *descriptor);
} catch (std::runtime_error &e) {
// Missing metadata is not a fatal error. Log and go on with your life
g_log.error() << "Error loading metadata: " << e.what() << '\n';
}
m_ws->setNPeriods(static_cast<size_t>(nPeriods),
periodLog); // This is how many workspaces we are going to make.
// Make sure you have a non-NULL m_allBanksPulseTimes
if (m_allBanksPulseTimes == nullptr) {
std::vector<DateAndTime> temp;
m_allBanksPulseTimes = std::make_shared<BankPulseTimes>(temp);
}
if (!m_ws->getInstrument() || !m_instrument_loaded_correctly) {
// Load the instrument (if not loaded before)
prog->report("Loading instrument");
// Note that closing an re-opening the file is needed here for loading
// instruments directly from the nexus file containing the event data.
// This may not be needed in the future if both LoadEventNexus and
// LoadInstrument are made to use the same Nexus/HDF5 library
m_file->close();
m_instrument_loaded_correctly = loadInstrument(m_filename, m_ws, m_top_entry_name, this, descriptor.get());
if (!m_instrument_loaded_correctly)
throw std::runtime_error("Instrument was not initialized correctly! "
"Loading cannot continue.");
// reopen file
safeOpenFile(m_filename);
}
// top level file information
m_file->openPath("/");
// Start with the base entry
m_file->openGroup(m_top_entry_name, "NXentry");
// Now we want to go through all the bankN_event entries
vector<string> bankNames;
vector<std::size_t> bankNumEvents;
std::string classType = monitors ? "NXmonitor" : "NXevent_data";
::NeXus::Info info;
bool oldNeXusFileNames(false);
bool haveWeights = false;
auto firstPulseT = DateAndTime::maximum();
const std::map<std::string, std::set<std::string>> &allEntries = descriptor->getAllEntries();
auto itClassEntries = allEntries.find(classType);
if (itClassEntries != allEntries.end()) {
const std::set<std::string> &classEntries = itClassEntries->second;
const std::regex classRegex("(/" + m_top_entry_name + "/)([^/]*)");
std::smatch groups;
for (const std::string &classEntry : classEntries) {
if (std::regex_match(classEntry, groups, classRegex)) {
const std::string entry_name(groups[2].str());
// skip entries with junk data
if (entry_name == "bank_error_events" || entry_name == "bank_unmapped_events")
continue;
m_file->openGroup(entry_name, classType);
// get the number of events
const std::string prefix = "/" + m_top_entry_name + "/" + entry_name;
bool hasTotalCounts = true;
std::size_t num = numEvents(*m_file, hasTotalCounts, oldNeXusFileNames, prefix, *descriptor);
bankNames.emplace_back(entry_name);
bankNumEvents.emplace_back(num);
if (takeTimesFromEvents && num > 0) {
/* If we are here, we are loading logs, but have failed to establish
* the run_start from the proton_charge log. We are going to get this
* from our event_time_zero instead
*/
auto localFirstLast = firstLastPulseTimes(*m_file, this->g_log);
firstPulseT = std::min(firstPulseT, localFirstLast.first);
}
// Look for weights in simulated file
const std::string absoluteEventWeightName = prefix + "/event_weight";
haveWeights = descriptor->isEntry(absoluteEventWeightName);