-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflows.py
More file actions
278 lines (239 loc) · 8.61 KB
/
workflows.py
File metadata and controls
278 lines (239 loc) · 8.61 KB
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
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Scipp contributors (https://github.com/scipp)
import itertools
import sciline
import scipp as sc
import scippnexus as snx
from scippneutron.metadata import Software
from ess.powder import providers as powder_providers
from ess.powder import with_pixel_mask_filenames
from ess.powder.correction import RunNormalization, insert_run_normalization
from ess.powder.types import (
AccumulatedProtonCharge,
BunkerMonitor,
CaveMonitor,
CaveMonitorPosition, # Should this be a DREAM-only parameter?
EmptyCanRun,
KeepEvents,
LookupTableRelativeErrorThreshold,
Measurement,
PixelMaskFilename,
Position,
ReducerSoftware,
SampleRun,
TimeOfFlightLookupTableFilename,
TofMask,
TwoThetaMask,
VanadiumRun,
WavelengthMask,
)
from ess.reduce.nexus.types import DetectorBankSizes, NeXusName
from ess.reduce.parameter import parameter_mappers
from ess.reduce.time_of_flight import GenericTofWorkflow
from ess.reduce.workflow import register_workflow
from .beamline import InstrumentConfiguration
from .io.cif import (
CIFAuthors,
prepare_reduced_empty_can_subtracted_tof_cif,
prepare_reduced_tof_cif,
)
from .io.geant4 import providers as geant4_providers
from .parameters import typical_outputs
DETECTOR_BANK_SIZES = {
"endcap_backward_detector": {
"strip": 16,
"wire": 16,
"module": 11,
"segment": 28,
"counter": 2,
},
"endcap_forward_detector": {
"strip": 16,
"wire": 16,
"module": 5,
"segment": 28,
"counter": 2,
},
"mantle_detector": {
"wire": 32,
"module": 5,
"segment": 6,
"strip": 256,
"counter": 2,
},
"high_resolution_detector": {"strip": 32, "other": -1},
"sans_detector": {"strip": 32, "other": -1},
}
def _get_lookup_table_filename_from_configuration(
configuration: InstrumentConfiguration,
) -> TimeOfFlightLookupTableFilename:
from .data import tof_lookup_table_high_flux
match configuration:
case InstrumentConfiguration.high_flux_BC215:
out = tof_lookup_table_high_flux(bc=215)
case InstrumentConfiguration.high_flux_BC240:
out = tof_lookup_table_high_flux(bc=240)
case InstrumentConfiguration.high_resolution:
raise NotImplementedError("High resolution configuration not yet supported")
return TimeOfFlightLookupTableFilename(out)
def _collect_reducer_software() -> ReducerSoftware:
return ReducerSoftware(
[
Software.from_package_metadata('essdiffraction'),
Software.from_package_metadata('scippneutron'),
Software.from_package_metadata('scipp'),
]
)
def DreamWorkflow(**kwargs) -> sciline.Pipeline:
"""
Dream generic workflow with default parameters.
The workflow is based on the GenericTofWorkflow.
It can load data from a NeXus file recorded on the DREAM instrument, and can
compute time-of-flight for the neutron events.
It can be used as is, or as a base for more specific workflows, such as
``DreamPowderWorkflow``.
Parameters
----------
kwargs:
Additional keyword arguments are forwarded to the base
:func:`GenericTofWorkflow`.
"""
wf = GenericTofWorkflow(
run_types=[SampleRun, VanadiumRun, EmptyCanRun],
monitor_types=[BunkerMonitor, CaveMonitor],
**kwargs,
)
wf[DetectorBankSizes] = DETECTOR_BANK_SIZES
wf[NeXusName[BunkerMonitor]] = "monitor_bunker"
wf[NeXusName[CaveMonitor]] = "monitor_cave"
wf.insert(_get_lookup_table_filename_from_configuration)
wf[ReducerSoftware] = _collect_reducer_software()
wf[LookupTableRelativeErrorThreshold] = {
"endcap_backward_detector": float('inf'),
"endcap_forward_detector": float('inf'),
"mantle_detector": float('inf'),
"high_resolution_detector": float('inf'),
"sans_detector": float('inf'),
"monitor_bunker": float('inf'),
"monitor_cave": float('inf'),
}
return wf
_cif_providers = (
prepare_reduced_tof_cif,
prepare_reduced_empty_can_subtracted_tof_cif,
)
parameter_mappers[PixelMaskFilename] = with_pixel_mask_filenames
def default_parameters() -> dict:
return {
KeepEvents[SampleRun]: KeepEvents[SampleRun](True),
KeepEvents[VanadiumRun]: KeepEvents[VanadiumRun](True),
KeepEvents[EmptyCanRun]: KeepEvents[EmptyCanRun](True),
TofMask: None,
WavelengthMask: None,
TwoThetaMask: None,
CIFAuthors: CIFAuthors([]),
}
def DreamPowderWorkflow(*, run_norm: RunNormalization, **kwargs) -> sciline.Pipeline:
"""
Dream powder workflow with default parameters.
Parameters
----------
run_norm:
Select how to normalize each run (sample, vanadium, etc.).
kwargs:
Additional keyword arguments are forwarded to the base :func:`DreamWorkflow`.
Returns
-------
:
A workflow object for DREAM.
"""
wf = DreamWorkflow(**kwargs)
for provider in itertools.chain(powder_providers, _cif_providers):
wf.insert(provider)
insert_run_normalization(wf, run_norm)
for key, value in default_parameters().items():
wf[key] = value
wf.typical_outputs = typical_outputs
return wf
def DreamGeant4Workflow(*, run_norm: RunNormalization, **kwargs) -> sciline.Pipeline:
"""
Workflow with default parameters for the Dream Geant4 simulation.
Parameters
----------
run_norm:
Select how to normalize each run (sample, vanadium, etc.).
kwargs:
Additional keyword arguments are forwarded to the base :func:`DreamWorkflow`.
Returns
-------
:
A workflow object for DREAM.
"""
wf = DreamWorkflow(**kwargs)
for provider in itertools.chain(geant4_providers, powder_providers, _cif_providers):
wf.insert(provider)
insert_run_normalization(wf, run_norm)
for key, value in default_parameters().items():
wf[key] = value
# Quantities not available in the simulated data
sample_position = sc.vector([0.0, 0.0, 0.0], unit="mm")
source_position = sc.vector([-3.478, 0.0, -76550], unit="mm")
charge = sc.scalar(1.0, unit="µAh")
additional_parameters = {
Position[snx.NXsample, SampleRun]: sample_position,
Position[snx.NXsample, VanadiumRun]: sample_position,
Position[snx.NXsample, EmptyCanRun]: sample_position,
Position[snx.NXsource, SampleRun]: source_position,
Position[snx.NXsource, VanadiumRun]: source_position,
Position[snx.NXsource, EmptyCanRun]: source_position,
AccumulatedProtonCharge[SampleRun]: charge,
AccumulatedProtonCharge[VanadiumRun]: charge,
AccumulatedProtonCharge[EmptyCanRun]: charge,
CaveMonitorPosition: sc.vector([0.0, 0.0, -4220.0], unit='mm'),
LookupTableRelativeErrorThreshold: {
"mantle": 0.02,
"endcap_forward": 0.02,
"endcap_backward": 0.02,
"high_resolution": 0.02,
"monitor_bunker": float("inf"),
"monitor_cave": float("inf"),
},
# The GEANT4 files do not encode measurement information
Measurement[SampleRun]: Measurement[SampleRun](title=None),
Measurement[VanadiumRun]: Measurement[VanadiumRun](title=None),
Measurement[EmptyCanRun]: Measurement[EmptyCanRun](title=None),
}
for key, value in additional_parameters.items():
wf[key] = value
wf.typical_outputs = typical_outputs
return wf
@register_workflow
def DreamGeant4MonitorHistogramWorkflow() -> sciline.Pipeline:
"""
Workflow with default parameters for the Dream Geant4 simulation, using a
histogrammed monitor for the normalization.
"""
return DreamGeant4Workflow(run_norm=RunNormalization.monitor_histogram)
@register_workflow
def DreamGeant4MonitorIntegratedWorkflow() -> sciline.Pipeline:
"""
Workflow with default parameters for the Dream Geant4 simulation, using
integrated counts of the monitor for the normalization.
"""
return DreamGeant4Workflow(run_norm=RunNormalization.monitor_integrated)
@register_workflow
def DreamGeant4ProtonChargeWorkflow() -> sciline.Pipeline:
"""
Workflow with default parameters for the Dream Geant4 simulation, using
proton charge for the normalization.
"""
return DreamGeant4Workflow(run_norm=RunNormalization.proton_charge)
__all__ = [
'DreamGeant4MonitorHistogramWorkflow',
'DreamGeant4MonitorIntegratedWorkflow',
'DreamGeant4ProtonChargeWorkflow',
'DreamGeant4Workflow',
'DreamPowderWorkflow',
'DreamWorkflow',
'default_parameters',
]