Page History
This histogramming code is part of the world-reusable scikit-beam project project hosted by BNL. This goal of this python library is to provide low-level x-ray-science building-blocks that can be easily installed around the world enabling scientists to reuse familiar tools at different laboratories. The code supports 1D, 2D and n-dimensional histograms (n<10). This script can be found in /reg/g/psdm/tutorials/examplePythonexamplePython3/hist.py
This example demonstrates 1D and 2D histograms and the various ways to fill them.
Code Block |
---|
from psana import * from skbeam.core.accumulators.histogram import Histogram import numpy as np # 1D histogram: 11 bins going from -5.5 to 5.5 hist = Histogram((11,-5.5,5.5)) hist.fill(4) hist.fill(0,weights=2) x = np.array((-4,-4,-4)) weights = np.array((1,1,0.5)) hist.fill(x,weights=weights) import matplotlib.pyplot as plt plt.plot(hist.centers[0],hist.values) plt.title('1D Histogram Results') plt.show() # 2D histogram: 3 bins in x going from -0.5 to 2.5 # and 2 bins going from -0.5 to 1.5 hist2d = Histogram((3,-0.5,2.5),(2,-0.5,1.5)) hist2d.fill(1,1) x = np.array((0,1,2)) y = np.array((0,1,2)) weights = np.array((5.5,2.5,9999.)) hist2d.fill(x,y,weights=weights) print (f'2D histogram values:\n{hist2d.values}',hist2d.values) |
Phil Hart provides this example of how to add histograms:
Code Block |
---|
import numpy as np
from skbeam.core.accumulators.histogram import Histogram
bins_eLow_eHigh = (1000, -100, 1900) ## keV; guess
h0 = Histogram(bins_eLow_eHigh)
h1 = Histogram(bins_eLow_eHigh)
hSum = Histogram(bins_eLow_eHigh)
h0.fill(100)
h1.fill(200)
hSum.fill(h0.centers[0], weights=h0.values+h1.values) |
Overview
Content Tools