The purpose of this note is to compare how pyFAI and ImageD11 treat the detector position. In particular, we derive how “PONI” detector parameters refined with pyFAI can be transformed into ImageD11 parameters.

In both packages, the transformation from pixel space to 3D laboratory coordinates is carried out in 4 steps:

  • Transformation from “pixel space” to the “detector coordinate system”. The detector coordinate system is a 3D coordinate system centered on the (0,0) pixel of the detector.

  • Correction for linear offsets, i.e. the position of the (0,0) pixel relative to the beam axis.

  • Correction for the origin/diffractometer-to-detector distance. The sample and diffractometer center of rotation are assumed to be located at the origin.

  • A series of rotations for the detector coordinate system relative to the laboratory coordinates.

Unfortunately, the conventions chosen by pyFAI and ImageD11 differ. For example, pyFAI applies the origin-to-detector distance correction before rotations, whereas ImageD11 applies it after rotations. Furthermore, they employ different coordinate systems.

Detector

We consider a pixelated 2D imaging detector. In “pixel space”, the position of a given pixel is given by the horizontal and vertical pixel numbers, dH and dV. We assume that looking along the beam axis into the detector, dH increases towards the right (towards the center of the synchrotron) and dV towards the top. For clarity, we assign the unit px to these coordinates.

The pixel numbers dH and dV are transformed into 3D “detector” coordinates by a function D:

p=D(dH,dV).

This function will account for the detector’s pixel size and the orientation and direction of pixel rows and columns relative to the detector coordinate system. Furthermore it may apply a distortion correction. This, however, is beyond the scope of this note.

Limiting ourselves to linear functions, D takes the form of a matrix with two columns and three rows. We will see below that the different choices of laboratory coordinate systems yield different D-matrices for pyFAI and ImageD11. We assume that the pixels have a constant horizontal and vertical size, pxsizeH and pxsizeV. Both are given in units of length per pixel. pyFAI specifically defines the unit of length as meter, we will therefore use pixel sizes in units of m/px throughout this note.

The position and orientation of this detector relative to the laboratory coordinates are described below.

Geometry definition of pyFAI

Coordinates

pyFAI uses a coordinate system where the first axis (1) is vertically up (y), the second axis (2) is horizontal (x) towards the ring center (starboard), and the third axis (3) along the beam (z). Note that in this order (1, 2, 3) is a right-handed coordinate system, which makes xyz in the usual order a left-handed coordinate system!

Units

All dimensions in pyFAI are in meter and all rotation are in radians.

Parameters

pyFAI describes the position and orientation of the detector by six variables, collectively called the PONI, for point of normal incidence. In addition, a detector calibration is provided in the PONI-file to convert pixel coordinates into real-space coordinates. Here we limit our discussion to the simplest case, i.e. a pixel size as discussed above.

Rotations:

θ1, θ2 and θ3 describe the detector’s orientation relative to the laboratory coordinate system.

Offsets:

poni1 and poni2 describe the offsets of pixel (0,0) relative to the “point of normal incidence”. In the absence of rotations the point of normal incidence is defined by the intersection of the direct beam beam axis with the detector.

Distance:

L describes the distance from the origin of the laboratory system to the point of normal incidence.

Detector

The transformation from pixel space to pyFAI detector coordinates is given by

[p1p2p3]=[0pxsizeVpxsizeH000][dHdV]DpyFAI=[0pxsizeVpxsizeH000].

Offsets

The PONI parameters are: a distance L, the vertical (y) and horizontal (x) coordinates of the point of normal incidence in meters, poni1 and poni2. The inversion of the x and y axes is due to the arrangement of the detector data, with x-rows being the “slow” axis and y-columns the “fast” axis. Extra care has to be taken with the signs of the rotations when converting form this coordinate system to another.

pyFAI applies both the offset correction and the origin-to-detector distance after the transformation from pixel space to the detector system, but before rotations,

Let L be the distance from the origin/sample/diffractometer center of rotation. In the absence of any detector rotations, L is taken along p3 (beam axis, z), p1 along the y-axis (vertical) and p2 along the x-axis (horizontal). Then the laboratory coordinates before rotation are

[p1p2p3]=DpyFAI[dHdV]+[poni1poni2L].

Rotations

The detector rotations are taken about the origin of the coordinate system (sample position). We define the following right-handed rotation matrices:

R1(θ1)=[1000cos(θ1)sin(θ1)0sin(θ1)cos(θ1)]R2(θ2)=[cos(θ2)0sin(θ2)010sin(θ2)0cos(θ2)]R3(θ3)=[cos(θ3)sin(θ3)0sin(θ3)cos(θ3)0001].

The rotations 1 and 2 in pyFAI are left handed, i.e. the sign of θ1 and θ2 is inverted.

The combined pyFAI rotation matrix is then

RpyFAI(θ1,θ2,θ3)=R3(θ3)R2(θ2)R1(θ1)

which yields the final laboratory coordinates after rotation

[t1t2t3]=RpyFAI(θ1,θ2,θ3)[p1p2p3]=RpyFAI(θ1,θ2,θ3)(DpyFAI[dHdV]+[poni1poni2L])=RpyFAI(θ1,θ2,θ3)([0pxsizeVpxsizeH000][dHdV]+[poni1poni2L]).

Inversion: Finding where a scattered beam hits the detector

For a 3DXRD-type simulation, we have to determine the pixel where a scattered ray intercepts the detector. Let A be the scattering center of a ray within a sample volume (grain, sub-grain or voxel). The Bragg condition and grain orientation pre-define the direction of the scattered beam, k. The coordinates A1,2,3 and k1,2,3 are specified in the laboratory system.

The inversion eq. [eq-tpyFAI] is straight-forward:

R1(θ1)R2(θ2)R3(θ3)[t1t2t3]=[p1p2L][t1t2t3]=[A1A2A3]+α[k1k2k3].

The third line (=L) of eq. [eq-find-alpha] is then used to determine the free parameter α, which in turn is used in the first and second lines to find p1,2 and thus d1,2.

As the most trivial example we consider the case of no rotations, θ1=θ2=θ3=0. Then

A3+αk3=Lα=LA3k3p1=A1+(LA3)k1k3p2=A2+(LA3)k2k3.

We see also that when all rotations are zero, (poni1,poni2) are the real space coordinates of the direct beam (A1,2,3=k1,2=0) .

Geometry definition of ImageD11

For maximum convenience, ImageD11 defines almost everything differently than pyFAI.

Coordinates

ImageD11 uses the ID06 coordinate system with x along the beam, y to port (away from the ring center), and z up.

Units

As the problem is somewhat scale-invariant, ImageD11 allows a free choice of the unit of length, which we will call X here. The same unit has to be used for all translations, and for the pixel size of the detector. The default used in the code appears to be X=1μm, but it might as well be Planck lengths, millimeters, inches, meters, tlalcuahuitl, furlongs, nautical miles, light years, kparsec, or whatever else floats your boat. The only requirement is that you can actually measure and express the detector pixel size and COR-to-detector distance in your units of choice. Since we want to compare to pyFAI, we choose X=1m.

Rotations are given in radians.

Parameters

ImageD11 defines the detector geometry via the following parameters:

Beam center:

ycenter and zcenter define the position of the direct beam on the detector. Contrary to pyFAI, the beam center is given in pixel space, in units of px.

Pixel size:

The horizontal and vertical pixel size are defined by ysize and zsize in X/px. With the right choice of the unit of length X, these corresponds directly to the pixel sizes pxsizeH and pxsizeV defined above.

Detector flip matrix:

O=[o11o12o21o22]. This matrix takes care ofcorrecting typical problems with the way pixel coordinates are arranged on the detector. If, e.g., the detector is rotated by 90, then O=[0110]. If left and right (or up and down) are inverted on the detector, then o22=1 (o11=1).

Rotations:

Detector tilts tx, ty, and tz, in rad. The center of rotation is the point where the direct beam intersects the detector.

Distance:

Δ, in units X, is the distance between the origin to the point where the direct beam intersects the detector. Note that this is again different from the definition of pyFAI.

It appears that these conventions where defined under the assumption that the detector is more or less centered in the direct beam, and that the detector tilts are small.

Transformation

The implementation in the code transform.py is using the following equations:

RImageD11(θx,θy,θz)=R1(θx)R2(θy)R3(θz)[pzpy]=[o11o12o21o22][(dzzcenter)zsize(dyycenter)ysize][txtytz]=RImageD11(θx,θy,θz)[0pypz]+[Δ00]

Note that the order of y and z is not the same in eqs. [eq-p] and [eq-tImageD11].

By combining the detector flip matrix O and the pixel size into a detector D matrix, this can be written as

DImageD11=[00ysizeo22zsizeo21ysizeo12zsizeo11][pxpypz]=DImageD11[dHycenterdVzcenter]

Conversion

Assume that the same detector geometry is described by the two notations. How can the parameters be converted from one to the other?

Detector D-matrix

The pixel size is the same in both notations, ysize=pxsizeH and zsize=pxsizeV.

As pyFAI does not allow for detector flipping, o11=1, o22=1 (because the sign of the horizontal axis is inverted between ImageD11 and pyFAI) and o12=o21=0. For the detector setup described above, with dV increasing to the top and dH increasing towards the center of the synchrotron (i.e. opposite to the positive y-direction), eq. [eq-DImageD11] becomes

DImageD11=[00pxsizeH00pxsizeV].

Coordinates

Both notations use the same sign for the vertical and beam axes. The sign of the horizontal transverse axis, however, is inverted.

The transformation between the different coordinate systems is then achieved by:

G=[001010100]tImageD11=GtpyFAI,

where tImageD11 is given by eq. [eq-tImageD11], and tpyFAI is given by eq. [eq-tpyFAI]. The matrix G performs the change of axes (xz, yy) and has the convenient property G2=1.

Substituting these equations into eq. [eq-coordconv], one can them attempt to convert pyFAI parameters into ImageD11 parameters and vice versa.

RImageD11DImageD11[dHycenterdVzcenter]+[Δ00]=GRpyFAI(DpyFAI[dHdV]+[poni1poni2L])

Rotations

Take an arbitrary vector d with dImageD11=[abc]. We first transform this into the pyFAI coordinate system by multiplication with G, and then apply an arbitrary rotation matrix, once in before (in pyFAI coordinates, RpyFAI) and once after the transformation (in ImageD11 coordinates, RImageD11).

dpyFAI=GdImageD11=[cba]RpyFAIdpyFAI=RpyFAIGdImageD11=GRImageD11dImageD11.

Comparing the last two lines, we find that with

RpyFAIG=GRImageD11

the transformation is applicable for each and any vector d. Because G1=G this transformation can also be applied to a series of rotations: GRR=(GRG)(GRG)G.

Applying this to the rotations matrices defined in eqs. [eq-rot1]–[eq-rot3] shows, unsurprisingly, that this coordinate transformation is an exchange of rotation axes x and y, and a change of sign for y.

GR1(θ)G=R3(θ)GR2(θ)G=R2(θ)GR3(θ)G=R1(θ)

Applying this transformation to the pyFAI rotation matrix can comparing to the ImageD11 rotation matrix, we see

GRpyFAI(θ1,θ2,θ3)G=GR3(θ3)R2(θ2)R1(θ1)G=R1(θ3)R2(θ2)R3(θ1)=RImageD11(θx,θy,θz)=R1(θx)R2(θy)R3(θz)

We find that, by divine intervention [1] and despite all the efforts to choose incompatible conventions, the effective order of rotations is actually the same between ``ImageD11`` and ``pyFAI``. Consequently, there is a direct correspondence with only a change of sign between θz and θ1:

θx=θ3θy=θ2θz=θ1

Translations and offsets

Inserting eqs. [eq-thetax]–[eq-thetaz] into [eq-transformation], we find

[Δ00]=GRpyFAI(DpyFAI[dHdV]+[poni1poni2L])RImageD11DImageD11[dHycenterdVzcenter]=RImageD11G([pxsizeVdVpxsizeHdH0]+[poni1poni2L])RImageD11[0pxsizeH(dHycenter)pxsizeV(dVzcenter)]=RImageD11([Lponi2pxsizeHdHponi1+pxsizeVdV][0pxsizeH(dHycenter)pxsizeV(dVzcenter)])=RImageD11[Lponi2pxsizeHycenterponi1+pxsizeVzcenter].

With a little help from our friend Mathematica, we find for the conversion from pyFAI to ImageD11

Δ=Lcos(θ1)cos(θ2)ycenter=1pxsizeH(poni2Ltan(θ1))zcenter=1pxsizeV(poni1+Ltan(θ2)cos(θ1)),

and for the conversion from ImageD11 to pyFAI

L=Δcos(θy)cos(θz)poni1=Δsin(θy)+pxsizeVzcenterponi2=Δcos(θy)sin(θz)+pxsizeHycenter.