File: simple_weight.py

package info (click to toggle)
python-boost-histogram 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,232 kB
  • sloc: python: 7,745; cpp: 3,243; makefile: 22; sh: 1
file content (25 lines) | stat: -rwxr-xr-x 687 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3

from __future__ import annotations

import boost_histogram as bh

# Make 1-d histogram with 5 logarithmic bins from 1e0 to 1e5
h = bh.Histogram(
    bh.axis.Regular(5, 1e0, 1e5, metadata="x", transform=bh.axis.transform.log),
    storage=bh.storage.Weight(),
)

# Fill histogram with numbers
x = (2e0, 2e1, 2e2, 2e3, 2e4)

# Doing this several times so the variance is more interesting
h.fill(x, weight=2)
h.fill(x, weight=2)
h.fill(x, weight=2)
h.fill(x, weight=2)

# Iterate over bins and access bin counter
for idx, (lower, upper) in enumerate(h.axes[0]):
    val = h[idx]
    print(f"bin {idx} in [{lower:g}, {upper:g}): {val.value} +/- {val.variance**0.5}")