Table of Contents
Name | Direction | Type | Default | Description |
---|---|---|---|---|
InputWorkspace | Input | MatrixWorkspace | Mandatory | A workspace containing the input histograms |
X | Input | number | 0 | The x value of the bin to use. |
Rounding | Input | string | None | Bin width rounding. Allowed values: [‘None’, ‘10^n’] |
BinWidth | Output | number | The averaged bin width |
This algorithm takes the bin width at X of each histogram in InputWorkspace and calculates the average. This value is then placed in the BinWidth output property.
If the Rounding property is set to 10^n, the bin width will be rounded down to the nearest power of 10. For example, 0.11 and 0.99 will be rounded to 0.1, while 0.011 and 0.099 will be rounded to 0.01.
The InputWorkspace has to contain histogram data. For point data, ConvertToHistogram v1 can be used first, but care should be taken if the points are not equally spaced.
Example: rebin a workspace to equidistant bin boundaries.
import numpy
# Create non-equidistant bin boundaries.
xs = [x * x for x in numpy.arange(0.0, 10.0, 0.05)]
# Convert xs to numpy array.
xs = numpy.array(xs)
# There is one less bin than the number of boundaries.
ys = numpy.zeros(len(xs) - 1)
ws = CreateWorkspace(DataX=xs, DataY=ys)
newWidth = BinWidthAtX(InputWorkspace=ws, X=1.0, Rounding='10^n')
print('New bin width: {0}'.format(newWidth))
# Rebin to equidistant grid
rebinned = Rebin(InputWorkspace=ws, Params=[newWidth], FullBinsOnly=True)
def firstAndLastBinWidths(workspace):
first = workspace.readX(0)[1] - workspace.readX(0)[0]
last = workspace.readX(0)[-1] - workspace.readX(0)[-2]
return (first, last)
first, last = firstAndLastBinWidths(ws)
print('Bin widths before rebinning, first: {0:.4f}, last: {1:.4f}'.format(first, last))
first, last = firstAndLastBinWidths(rebinned)
print('Bin widths after rebinning, first: {0:.2f}, last: {1:.2f}'.format(first, last))
Output:
New bin width: 0.01
Bin widths before rebinning, first: 0.0025, last: 0.9925
Bin widths after rebinning, first: 0.01, last: 0.01
Categories: Algorithms | Utility\Calculation
Python: BinWidthAtX.py (last modified: 2017-09-06)