|
SND@LHC Software
|

Public Member Functions | |
| __init__ (self, config_path="config.yaml") | |
| read_files (self, path) | |
| base_cut (self, df) | |
| compute_ratio (self) | |
| fit (self, degree) | |
| run (self) | |
Static Public Member Functions | |
| fit_poly_weighted (x, y, sigma, degree=1) | |
Public Attributes | |
| cfg | |
| bins_E | |
| E | |
| H_DPM | |
| s_DPM | |
| H_LC | |
| s_LC | |
| w | |
| sig | |
Static Public Attributes | |
| dict | PID = {"nue": 12, "numu": 14, "nutau": 16} |
| list | COLS |
Protected Member Functions | |
| _load_config (self, yaml_path) | |
| _report_selection (self, name, df, pid) | |
| _hist (self, df, mask, use_weight=True) | |
| _fit_mask (self, verbose=False) | |
| _make_plot (self, deg, poly, chi2red, fit_mask) | |
Static Protected Member Functions | |
| _require (d, key, section) | |
| _adaptive_edges (fine, dsw, dsw2, lsw, lsw2, min_neff) | |
| _poly_equation_text (poly, deg) | |
Derive the reweighting function w(E) = fast-sim / DPMJET from flux files.
The class histograms the four input samples in neutrino energy, forms the
ratio bin by bin with propagated errors, and fits a polynomial to it. All
settings come from a YAML config passed to the constructor.
Typical use
-----------
rw = FluxReweighter("Config_epos.yaml")
rw.run() # diagnostic plot per degree
poly, chi2red = rw.fit(2) # or just the deg-2 weight function
Attributes populated by :meth:`compute_ratio`
---------------------------------------------
E : bin centres [GeV]
w : the ratio fast-sim / DPMJET per bin (this is w(E) sampled)
sig : uncertainty on the ratio per bin
H_DPM : summed DPMJET histogram (light + charm), s_DPM its error
H_LC : summed fast-sim histogram (light + charm), s_LC its error
Definition at line 104 of file reweighting_function.py.
| reweighting_function.FluxReweighter.__init__ | ( | self, | |
config_path = "config.yaml" |
|||
| ) |
Load the YAML config and prepare the default (uniform) bin edges. Adaptive edges, if requested, are computed later in :meth:`compute_ratio` because they depend on the selected events.
Definition at line 141 of file reweighting_function.py.
|
staticprotected |
Build variable-width bin edges with a minimum of effective entries.
Starting from a fine uniform grid, fine bins are accumulated left to
right and a bin is closed as soon as *both* the DPMJET and the fast-sim
side reach
N_eff = (sum w)^2 / sum w^2 >= min_neff
N_eff is the number of unweighted events that would give the same
statistical power, so this guarantees no bin is dominated by a handful
of large-weight events -- the usual failure mode at high energy, where
a single charm event with a huge weight otherwise makes the ratio jump
and its error explode.
Parameters
----------
fine : ndarray
Edges of the fine starting grid.
dsw, dsw2 : ndarray
Per-fine-bin sum of weights and sum of weights squared, DPMJET side.
lsw, lsw2 : ndarray
The same for the fast-sim side.
min_neff : float
Required effective entries per output bin.
Returns
-------
ndarray
The merged bin edges (always closed at ``fine[-1]``).
Definition at line 163 of file reweighting_function.py.
|
protected |
Boolean mask selecting which ratio bins enter the polynomial fit. A bin is kept when it lies inside ``fit_range``, has a non-zero ratio and error, passes the relative-error cap, and falls inside the optional ratio window. With ``verbose`` the number of bins removed by the ratio window is printed.
Definition at line 454 of file reweighting_function.py.
|
protected |
Weighted energy histogram of the selected rows, with its error. Each event contributes its own ``weight in pb`` (or 1.0 when ``use_weight`` is false). The error is the standard weighted-histogram one, sqrt(sum w^2) per bin. Returns ``(counts, sigma)``.
Definition at line 347 of file reweighting_function.py.
|
protected |
Parse the reweighter YAML into a flat dict of settings. Only the four input paths are mandatory; every other key has a default so a minimal config stays valid. See Config_epos.yaml for the meaning of each field.
Definition at line 207 of file reweighting_function.py.
|
protected |
Write the diagnostic ratio plot for one polynomial degree. Shows every ratio bin faintly, the subset entering the fit with error bars, the fitted curve inside ``fit_range`` and its extrapolation outside, plus a statistics box. Comparing the two point sets shows at a glance what the quality gates removed. Returns the path of the saved PDF.
Definition at line 532 of file reweighting_function.py.
|
staticprotected |
Format w(E) as a LaTeX string for the plot's statistics box.
Definition at line 520 of file reweighting_function.py.
|
protected |
Print how many rows survive each selection stage for one file. Printed before the ratio is built. If a stage removes everything the actual x/y data ranges are shown next to the cut bounds, which makes a unit mismatch (cut in cm, data in metres) immediately obvious. Returns the number of surviving rows.
Definition at line 316 of file reweighting_function.py.
|
staticprotected |
Return a mandatory config value, or raise naming the missing field.
Definition at line 156 of file reweighting_function.py.
| reweighting_function.FluxReweighter.base_cut | ( | self, | |
| df | |||
| ) |
Boolean mask for the fiducial (detector acceptance) selection. Applies a minimum energy and an x/y box, with the x/y columns scaled by ``cuts.unit_scale`` first. Returns all-true when ``cuts.enabled`` is false, i.e. when the inputs already went through the cut-based analysis.
Definition at line 298 of file reweighting_function.py.
| reweighting_function.FluxReweighter.compute_ratio | ( | self | ) |
Build the fast-sim / DPMJET ratio versus energy. Steps: read the four files, print the selection report, optionally derive adaptive bin edges, histogram each sample, sum light + charm on each side, and divide. Light and charm are histogrammed separately and the histograms added, which is equivalent to weighting every event individually and filling one histogram -- each event always keeps its own ``weight in pb``. The errors are combined in quadrature, sqrt(s_light^2 + s_charm^2). Populates ``E``, ``w``, ``sig``, ``H_DPM``, ``s_DPM``, ``H_LC``, ``s_LC`` and returns ``self`` so calls can be chained.
Definition at line 365 of file reweighting_function.py.
| reweighting_function.FluxReweighter.fit | ( | self, | |
| degree | |||
| ) |
Return the reweighting polynomial w(E) for one degree. Computes the ratio on first use. Evaluate the result per event as ``poly(E)``; it is only meaningful inside ``fit_range``. Returns ``(poly, chi2red)``. Raises ``ValueError`` if the selection leaves fewer points than the degree requires.
Definition at line 495 of file reweighting_function.py.
|
static |
Least-squares polynomial fit weighted by 1/sigma. Returns ``(poly, chi2, chi2/ndf)`` where ``poly`` is a ``np.poly1d`` with coefficients ordered highest power first.
Definition at line 484 of file reweighting_function.py.
| reweighting_function.FluxReweighter.read_files | ( | self, | |
| path | |||
| ) |
Read one flux text file into a DataFrame with named columns. POWHEG samples carry an extra trailing ``iEvent`` column; this is detected from the file name so the column names line up either way.
Definition at line 289 of file reweighting_function.py.
| reweighting_function.FluxReweighter.run | ( | self | ) |
Fit every degree in ``polynomial_degrees`` and save one plot each. This is the "reweight" mode entry point: it is the diagnostic pass used to choose a degree and check the binning and quality gates. Degrees with too few surviving points are skipped with a message. Returns ``self``.
Definition at line 586 of file reweighting_function.py.
| reweighting_function.FluxReweighter.bins_E |
Definition at line 148 of file reweighting_function.py.
| reweighting_function.FluxReweighter.cfg |
Definition at line 147 of file reweighting_function.py.
|
static |
Definition at line 131 of file reweighting_function.py.
| reweighting_function.FluxReweighter.E |
Definition at line 152 of file reweighting_function.py.
| reweighting_function.FluxReweighter.H_DPM |
Definition at line 153 of file reweighting_function.py.
| reweighting_function.FluxReweighter.H_LC |
Definition at line 436 of file reweighting_function.py.
|
static |
Definition at line 127 of file reweighting_function.py.
| reweighting_function.FluxReweighter.s_DPM |
Definition at line 431 of file reweighting_function.py.
| reweighting_function.FluxReweighter.s_LC |
Definition at line 437 of file reweighting_function.py.
| reweighting_function.FluxReweighter.sig |
Definition at line 451 of file reweighting_function.py.
| reweighting_function.FluxReweighter.w |
Definition at line 451 of file reweighting_function.py.