-
Notifications
You must be signed in to change notification settings - Fork 122
/
FilterEventsByLogValuePreNexus.h
275 lines (217 loc) · 8.72 KB
/
FilterEventsByLogValuePreNexus.h
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2010 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 +
#pragma once
#include "MantidAPI/DeprecatedAlgorithm.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/Events.h"
#include "MantidKernel/BinaryFile.h"
#include "MantidKernel/FileDescriptor.h"
#include <fstream>
#include <string>
#include <vector>
namespace Mantid {
namespace DataHandling {
/** @class Mantid::DataHandling::FilterEventsByLogValuePreNexus
A data loading and splitting routine for SNS pre-nexus event files
*/
/// This define is used to quickly turn parallel code on or off.
#undef LOADEVENTPRENEXUS_ALLOW_PARALLEL
/// Make the code clearer by having this an explicit type
using PixelType = int;
/// Type for the DAS time of flight (data file)
using DasTofType = int;
/// Structure that matches the form in the binary event list.
#pragma pack(push, 4) // Make sure the structure is 8 bytes.
struct DasEvent {
/// Time of flight.
DasTofType tof;
/// Pixel identifier as published by the DAS/DAE/DAQ.
PixelType pid;
};
#pragma pack(pop)
/// Structure used as an intermediate for parallel processing of events
#pragma pack(push, 4) // Make sure the structure is 8 bytes.
struct IntermediateEvent {
/// Time of flight.
DasTofType tof;
/// Pixel identifier as published by the DAS/DAE/DAQ.
PixelType pid;
/// Frame index (pulse # of this event)
size_t frame_index;
/// Period of the event (not really used at this time)
uint32_t period;
};
#pragma pack(pop)
/// Structure that matches the form in the new pulseid files.
#pragma pack(push, 4) // Make sure the structure is 16 bytes.
struct Pulse {
/// The number of nanoseconds since the seconds field. This is not necessarily
/// less than one second.
uint32_t nanoseconds;
/// The number of seconds since January 1, 1990.
uint32_t seconds;
/// The index of the first event for this pulse.
uint64_t event_index;
/// The proton charge for the pulse.
double pCurrent;
};
#pragma pack(pop)
class DLLExport FilterEventsByLogValuePreNexus
: public API::IFileLoader<Kernel::FileDescriptor>,
public API::DeprecatedAlgorithm {
public:
/// Constructor
FilterEventsByLogValuePreNexus();
/// Virtual destructor
~FilterEventsByLogValuePreNexus() override;
/// Algorithm's name
const std::string name() const override {
return "FilterEventsByLogValuePreNexus";
}
/// Algorithm's version
int version() const override { return (2); }
/// Algorithm's category for identification
const std::string category() const override {
return "DataHandling\\PreNexus";
}
/// Algorithm's aliases
const std::string alias() const override { return "LoadEventPreNeXus2"; }
/// Summary of algorithms purpose
const std::string summary() const override {
return "Load and split SNS raw neutron event data format and stores it in "
"a workspace";
}
/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::FileDescriptor &descriptor) const override;
private:
/// Initialisation code
void init() override;
/// Execution code
void exec() override;
/// Process properties
void processProperties();
/// Create, initialize and set up output EventWrokspace
DataObjects::EventWorkspace_sptr setupOutputEventWorkspace();
void loadPixelMap(const std::string &filename);
void openEventFile(const std::string &filename);
void readPulseidFile(const std::string &filename, const bool throwError);
void runLoadInstrument(const std::string &eventfilename,
const API::MatrixWorkspace_sptr &localWorkspace);
void procEvents(DataObjects::EventWorkspace_sptr &workspace);
void procEventsLinear(DataObjects::EventWorkspace_sptr &workspace,
std::vector<Types::Event::TofEvent> **arrayOfVectors,
DasEvent *event_buffer,
size_t current_event_buffer_size, size_t fileOffset);
void setProtonCharge(DataObjects::EventWorkspace_sptr &workspace);
void addToWorkspaceLog(const std::string &logtitle, size_t mindex);
void processEventLogs();
/// Pad out empty pixel
size_t padOutEmptyPixels(const DataObjects::EventWorkspace_sptr &eventws);
/// Set up spectrum/detector ID map inside a workspace
void setupPixelSpectrumMap(const DataObjects::EventWorkspace_sptr &eventws);
///
void filterEvents();
///
void filterEventsLinear(DataObjects::EventWorkspace_sptr &workspace,
std::vector<Types::Event::TofEvent> **arrayOfVectors,
DasEvent *event_buffer,
size_t current_event_buffer_size, size_t fileOffset);
/// Correct wrong event indexes with pulse
void unmaskVetoEventIndexes();
/// Use pulse index/ event index to find out the frequency of instrument
/// running
int findRunFrequency();
void debugOutput(bool doit, size_t mindex);
/// Perform statistics to event (wrong pixel ID) logs
void doStatToEventLog(size_t mindex);
std::unique_ptr<Mantid::API::Progress> m_progress = nullptr;
DataObjects::EventWorkspace_sptr m_localWorkspace; //< Output EventWorkspace
std::vector<int64_t> m_spectraList; ///< the list of Spectra
/// The times for each pulse.
std::vector<Types::Core::DateAndTime> pulsetimes;
/// The index of the first event in each pulse.
std::vector<uint64_t> m_vecEventIndex;
/// The proton charge on a pulse by pulse basis.
std::vector<double> m_protonCharge;
/// The total proton charge for the run.
double m_protonChargeTot;
/// The value of the vector is the workspace index. The index into it is the
/// pixel ID from DAS
std::vector<std::size_t> m_pixelToWkspindex;
/// Map between the DAS pixel IDs and our pixel IDs, used while loading.
std::vector<PixelType> m_pixelmap;
/// The maximum detector ID possible
Mantid::detid_t m_detid_max;
/// Handles loading from the event file
std::unique_ptr<Mantid::Kernel::BinaryFile<DasEvent>> m_eventFile;
std::size_t m_numEvents; ///< The number of events in the file
std::size_t m_numPulses; ///< the number of pulses
uint32_t m_numPixel; ///< the number of pixels
std::size_t m_numGoodEvents; ///< The number of good events loaded
std::size_t m_numErrorEvents; ///< The number of error events encountered
std::size_t m_numBadEvents; ///< The number of bad events. Part of error
///< events
std::size_t m_numWrongdetidEvents; ///< The number of events with wrong
/// detector IDs. Part of error events.
std::set<PixelType> wrongdetids; ///< set of all wrong detector IDs
std::map<PixelType, size_t> wrongdetidmap;
std::vector<std::vector<Types::Core::DateAndTime>> wrongdetid_pulsetimes;
std::vector<std::vector<double>> wrongdetid_tofs;
/// the number of events that were ignored (not loaded) because, e.g. of only
/// loading some spectra.
std::size_t m_numIgnoredEvents;
std::size_t m_firstEvent; ///< The first event to load (count from zero)
std::size_t m_maxNumEvents; ///< Number of events to load
/// Set to true if a valid Mapping file was provided.
bool m_usingMappingFile;
/// For loading only some spectra
bool m_loadOnlySomeSpectra;
/// Handle to the loaded spectra map
std::map<int64_t, bool> spectraLoadMap;
/// Longest TOF limit
double m_longestTof;
/// Shortest TOF limit
double m_shortestTof;
/// Flag to allow for parallel loading
bool m_parallelProcessing;
/// Whether or not the pulse times are sorted in increasing order.
bool m_pulseTimesIncreasing;
/// sample environment event
std::vector<detid_t> mSEids;
std::map<size_t, detid_t> mSEmap;
std::vector<std::vector<int64_t>> mSEpulseids;
std::vector<std::vector<double>> mSEtofs;
/// Event file
std::string m_eventFileName;
/// Pulse ID file
std::string m_pulseIDFileName;
/// Throw error with bad pulse ID
bool m_throwError;
/// Function mode
std::string m_functionMode;
/// Flag for examine event (log)
bool m_examEventLog;
/// Pixel ID to exam
int m_pixelid2exam;
/// Number of events to write out
int m_numevents2write;
/// Log pixel IDs for filtering
std::vector<int> m_vecLogPixelID;
/// Log pixel Tags for filtering
std::vector<std::string> m_vecLogPixelTag;
/// Output EventWorkspace for filtered event B->A
DataObjects::EventWorkspace_sptr m_localWorkspaceBA;
/// Accelerator operation frequency
int m_freqHz;
int64_t m_istep;
int64_t m_dbPixelID;
bool m_useDBOutput;
bool m_corretctTOF;
};
} // namespace DataHandling
} // namespace Mantid