[docs]defsimulate_beamline(choppers:Mapping[str,DiskChopper],neutrons:int=1_000_000,pulses:int=1,seed:int|None=None,facility:str='ess',)->SimulationResults:""" Simulate a pulse of neutrons propagating through a chopper cascade using the ``tof`` package (https://tof.readthedocs.io). Parameters ---------- choppers: A dict of DiskChopper objects representing the choppers in the beamline. See https://scipp.github.io/scippneutron/user-guide/chopper/processing-nexus-choppers.html#Build-DiskChopper for more information. neutrons: Number of neutrons to simulate. pulses: Number of pulses to simulate. seed: Seed for the random number generator used in the simulation. facility: Facility where the experiment is performed. """importtoftof_choppers=[tof.Chopper(frequency=abs(ch.frequency),direction=tof.AntiClockwiseif(ch.frequency.value>0.0)elsetof.Clockwise,open=ch.slit_begin,close=ch.slit_end,phase=abs(ch.phase),distance=ch.axle_position.fields.z,name=name,)forname,chinchoppers.items()]source=tof.Source(facility=facility,neutrons=neutrons,pulses=pulses,seed=seed)ifnottof_choppers:events=source.data.squeeze().flatten(to='event')returnSimulationResults(time_of_arrival=events.coords["time"],speed=events.coords["speed"],wavelength=events.coords["wavelength"],weight=events.data,distance=0.0*sc.units.m,)model=tof.Model(source=source,choppers=tof_choppers)results=model.run()# Find name of the furthest chopper in tof_choppersfurthest_chopper=max(tof_choppers,key=lambdac:c.distance)events=results[furthest_chopper.name].data.squeeze().flatten(to='event')events=events[~(events.masks["blocked_by_others"]|events.masks["blocked_by_me"])]returnSimulationResults(time_of_arrival=events.coords["toa"],speed=events.coords["speed"],wavelength=events.coords["wavelength"],weight=events.data,distance=furthest_chopper.distance,)