Skip to content

Conversation

@jokasimr
Copy link
Contributor

Addresses part of #217 by binning the McStas data by "pixel" after loading and adapting the workflow to handle the pixel-binned data structure.

@jokasimr
Copy link
Contributor Author

Questions that remain answering

  • what is the number of pixels in the detectors should be divided into?
  • probably we should load the detectors separately, with a type parameter determining which detector to load, like in other instruments with multiple detectors

Copy link
Member

@jl-wynen jl-wynen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don not see any pixel binning here, only binning by position. Is the detector aligned with the instrument coord system? And does the mcstas file not contain pixel ids?

sc.midpoints(da.coords['y']),
sc.midpoints(-da.coords['x'] if bank == 1 else da.coords['x']),
)
).transpose(da.dims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining what is going on here?
Why are you using 'x' as the z component of the vector? (You should drop the 'x' coord to avoid confusions down the line.)

Copy link
Contributor Author

@jokasimr jokasimr Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x is a detector local coordinate, after the detectors are rotated it points in the global z direction.

But this is changed. Now the rotation is applied explicitly.

da.coords['Ltotal'] = L1 + L2
da.coords['two_theta'] = sc.acos(
(-da.coords['x'] if bank == 1 else da.coords['x']) / L2
(da.coords['position'] - da.coords['sample_position']).fields.z / L2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right because it doesn't account for the source position. Why don't you us tuse the predefined coord transform graph?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Implicit assumption in the old code was that incident beam is along z.)

The code now explicitly defines the incident beam direction, and uses the coordinate transformation graphs.

@jokasimr
Copy link
Contributor Author

does the mcstas file not contain pixel ids?

It does contain pixel ids, but the positions they are associated with in the detector component are wrong :/

I did try quite a bit to get binning by pixel id to work, but in the end the conclusion I reached is that the McStas "Nexus" files are not good enough to be amenable to automatic processing. There are just too many problems and missing information in the files.

A major issue is that they don't use the NeXus classes correctly, so it's impossible to automate finding the correct component in the component list... Finding components by name break because the names of components can change between different versions of the model...

So the less we use instrument component data the better.

@jokasimr jokasimr requested a review from jl-wynen January 23, 2026 09:21
else sc.spatial.rotation(
value=[
0.0,
np.sin(-detector_rotation_angle / 2),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the - into the definition of the angle to make this more readable? It took me a moment to see the difference between the banks.

'''Load beer McStas data from a file to a
data group with one data array for each bank.
'''
if isinstance(f, str | Path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use ess.reduce.nexus.open_nexus_file here. It handles both paths and existing file objects. And it bypasses locking on read only filesystems. But it uses ScippNexus objects, not h5py. So you would have to get the h5.Group out of that first. Your call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. But given how nexus-incompatible these files are, I suspect scippnexus might be confused by that, and that in the end we don't gain much by using it here, for now I'll just keep it as it is. We won't need to read McStas simulation files from a read only filesystem anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening a file with scippneux doesn't really do anything on top of what h5py does. But fair point

@jokasimr jokasimr enabled auto-merge (squash) January 23, 2026 12:19
@jokasimr jokasimr merged commit c2e1240 into main Jan 23, 2026
7 of 8 checks passed
@jokasimr jokasimr deleted the bin-by-pixel branch January 23, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants