/cms:histfrom cms
Creates, fills, slices, rebins, normalises, and plots histograms in Python using scikit-hep hist / boost-histogram and mplhep, especially for CMS NanoAOD arrays. Covers weighted fills with genWeight, MC normalisation to cross section times luminosity, categorical axes, UHI indexing, and CMS-style plots with ratio panels.
Ships in cms. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
hist builds N-dimensional histograms (boost-histogram underneath) with
named axes, weighted storage and UHI indexing. mplhep plots them in the CMS
style. On CMS NanoAOD, values are in GeV and MC events carry genWeight,
which can be negative.
Typical flow: define axes and storage → fill with flattened arrays and weights → normalise MC → slice/project → plot with the CMS label → save.
| Piece | Use |
|---|---|
Hist.new.Reg(n, lo, hi, name=, label=) | Uniform bins |
.Var([edges], name=) | Variable bins (e.g. pT tails) |
.Integer(lo, hi) | Counts such as nJet, PV_npvsGood |
.StrCat([...], growth=True) | Regions, processes, systematics |
.Int64() / .Weight() | Exactly one storage. Weight keeps Σw and Σw² for correct errors |
| Flow bins | Under/overflow are kept. h.values(flow=True), h[hist.overflow] |
| UHI | h[5j] = bin containing 5, h[0.3j:], h[::2j] rebin, h[:, sum] project |
import awkward as ak, numpy as np, hist
from hist import Hist
h_mll = Hist.new.Reg(60, 60, 120, name="mll", label=r"$m_{\mu\mu}$ [GeV]").Weight()
h_mll.fill(mll=ak.to_numpy(mll), weight=ak.to_numpy(w)) # 1 value per event
h_pt = Hist.new.Reg(50, 0, 200, name="pt", label=r"Muon $p_{T}$ [GeV]").Weight()
pt_flat = ak.flatten(muons.pt, axis=1) # jagged -> one entry per muon
w_flat = ak.flatten(ak.broadcast_arrays(w, muons.pt)[0], axis=1)
h_pt.fill(pt=pt_flat, weight=w_flat)
Jagged arrays must be flattened. Broadcast the per-event weight to objects first, as above.
sumw = sum(float(ak.sum(uproot.open(f)["Runs"]["genEventSumw"].array()))
for f in all_files_of_the_dataset) # ALL files, before any cut
w = genWeight * xsec_pb * lumi_invpb / sumw # per-event weight, float64
Runs/genEventSumw equals the sum of genWeight over every generated event.
Using the event count, or the sum after cuts, gives the wrong normalisation for
samples with negative weights.
h = (Hist.new.Reg(60, 60, 120, name="mll", label=r"$m_{\mu\mu}$ [GeV]")
.StrCat(["SR", "CR"], name="region")
.StrCat([], name="process", growth=True)
.StrCat([], name="syst", growth=True)
.Weight())
h.fill(mll=x, region="SR", process="DY", syst="nominal", weight=w)
h[:, "SR", "DY", "nominal"] # 1D slice
h[:, "SR", sum, "nominal"] # sum over processes
import matplotlib.pyplot as plt, mplhep as hep
plt.style.use(hep.style.CMS)
fig, ax = plt.subplots()
h[:, "SR", "DY", "nominal"].plot(ax=ax, histtype="fill", label="DY")
hep.cms.label("Open Data", data=True, lumi=16.8, com=13, ax=ax) # "CMS Open Data"; MC only: data=False
ax.set_ylabel("Events / 1 GeV")
ax.legend()
fig.savefig("mll.png")
plt.close(fig)
Stacks, data/MC ratio panels and 2D plots are in references/plotting-cms.md. Suggested CMS ranges and binning are in references/cms-binning.md.
Int64 silently ignores the meaning of weights. Filling .Int64() with
weight= raises no error, but the storage is integer and no Σw² is kept.
Use .Weight() for any weighted fill..Int64().Weight() gives
AttributeError: 'Hist' object has no attribute 'Weight'.genWeights (16% of
events in the tested DY sample, all with the same magnitude). Bins can go
negative, so always use Weight storage and normalise with genEventSumw.cannot convert to RegularArray. Flatten with
ak.flatten(x, axis=1) and broadcast weights to the same shape.genWeight is float32. Cast to float64 before summing
(in CMSSW, a direct sum differed from the histogram's by 5,000 out of 1.1e8).histtype depends on the mplhep version. fill, step, errorbar, band
always work. bar, barstep exist only in newer mplhep (1.x), and fail in
CMSSW_15_0_21's 0.3.47 with Select 'histtype' from: [...].genWeight
(~25,000 per event) gives a 1e7 axis offset that mplhep 0.3.x draws under
the "CMS" text. Normalise to σ×L first..fill returns the histogram, not None, so h = Hist...().fill(...)
chains.h[hist.overflow] when totals don't
match.[GeV]), and use raw
strings for LaTeX (r"$p_{T}$"). In f-strings, double the braces.hep.cms.label options), stacked MC, data/MC ratio panels, 2D
plots, and differences between mplhep 0.3.x (CMSSW) and 1.x (pip).hist skill (BSD 3-Clause; see
plugins/cms/VENDORED-LICENSES.md):
https://github.com/iris-hep/marketplace/tree/main/iris-hep/skills/histWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub cms-ml/marketplace --plugin cms10 skills in this plugin
/analysis-spec-builderUse when a user wants to plan, scope or write down a CMS physics analysis or plotting task before coding it: creating or updating an analysis specification, turning a quick request ("plot the dimuon mass in 2016 data vs DY", "find the best trijet in ttbar") into a structured document, listing datasets, triggers, selections, histograms, corrections and statistical treatment, or producing a specification.md for later implementation./awkward-arrayUse when working with jagged or variable-length arrays in Python with Awkward Array 2 (import awkward as ak): reading CMS NanoAOD or other ROOT ntuples into arrays with uproot, building per-object records with ak.zip, selecting objects or events with masks, counting with ak.num or ak.count, pairing objects with ak.combinations or ak.cartesian (dilepton masses, deltaR, jet cleaning), taking the leading object or argmax per event, following NanoAOD index branches such as Jet_muonIdx1, flattening for histograms, writing Parquet or JSON, or debugging awkward errors such as "cannot slice", "index out of range" or None values appearing in results./cmsswUse when setting up or working inside a CMSSW release area (cmsrel, cmsenv, scram), choosing a CMSSW release or SCRAM_ARCH for a dataset, running an old release inside a cmssw-el7/el8 container, checking out or patching CMSSW packages with git cms-addpkg or git cms-merge-topic, writing or building an EDAnalyzer, EDProducer or EDFilter, writing or debugging a cmsRun Python configuration (_cfg.py, _cfi.py, _cff.py), inspecting EDM ROOT files (MiniAOD, AOD, RECO) with edm* tools, or installing extra Python packages on top of a CMSSW release.Other plugins' skills for the same job