mist-hep 0.1.0
ROOT-backed analysis helpers built on mist
Loading...
Searching...
No Matches
binning.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2//
3// mist/hep/graph/binning.h — non-overlapping block reductions over a TGraph.
4//
5// Thin adapters over the ROOT-free primitives in <mist/algo/binning.h>:
6// the X and Y coordinate arrays are pulled from the TGraph, reduced by
7// mist::algo, and zipped back into a new TGraph.
8//
9// Header-only. Each function returns an owning
10// mist::hep::owned::root_ptr<TGraph>.
11//
12// Ports / rewrites of:
13// - graphutils `average` -> block_average (F-14)
14// - graphutils `rms` -> block_rms (F-15)
15//
16#pragma once
17
18#include <vector>
19
20#include <TGraph.h>
21
22#include <mist/algo/binning.h>
23#include <mist/hep/owned.h>
24
25namespace mist::hep::graph {
26
27namespace owned = ::mist::hep::owned;
28
29namespace detail {
30
31inline std::vector<double> graph_x(const TGraph& g)
32{
33 std::vector<double> xs;
34 xs.reserve(g.GetN());
35 for (int i = 0; i < g.GetN(); ++i) xs.push_back(g.GetPointX(i));
36 return xs;
37}
38
39inline std::vector<double> graph_y(const TGraph& g)
40{
41 std::vector<double> ys;
42 ys.reserve(g.GetN());
43 for (int i = 0; i < g.GetN(); ++i) ys.push_back(g.GetPointY(i));
44 return ys;
45}
46
48zip(const std::vector<double>& xs, const std::vector<double>& ys)
49{
50 auto out = owned::make<TGraph>();
51 const std::size_t n = std::min(xs.size(), ys.size());
52 for (std::size_t i = 0; i < n; ++i)
53 out->SetPoint(static_cast<int>(i), xs[i], ys[i]);
54 return out;
55}
56
57} // namespace detail
58
59// ---------------------------------------------------------------------------
60// block_average: each non-overlapping block of `block_size` consecutive
61// points becomes one point at (mean x, mean y) of that block.
62//
63// `drop_partial = false` (default) keeps the final short block; pass `true`
64// to reproduce the graphutils-original behaviour of discarding it.
65// ---------------------------------------------------------------------------
66[[nodiscard]] inline owned::root_ptr<TGraph>
67block_average(const TGraph& source, std::size_t block_size,
68 bool drop_partial = false)
69{
70 const auto mean_x = mist::algo::block_mean(detail::graph_x(source),
71 block_size, drop_partial);
72 const auto mean_y = mist::algo::block_mean(detail::graph_y(source),
73 block_size, drop_partial);
74 return detail::zip(mean_x, mean_y);
75}
76
77// ---------------------------------------------------------------------------
78// block_rms: each non-overlapping block becomes one point at (mean x of the
79// block, population standard deviation of the block's y-values).
80// ---------------------------------------------------------------------------
81[[nodiscard]] inline owned::root_ptr<TGraph>
82block_rms(const TGraph& source, std::size_t block_size,
83 bool drop_partial = false)
84{
85 const auto mean_x = mist::algo::block_mean(detail::graph_x(source),
86 block_size, drop_partial);
87 const auto rms_y = mist::algo::block_rms(detail::graph_y(source),
88 block_size, drop_partial);
89 return detail::zip(mean_x, rms_y);
90}
91
92} // namespace mist::hep::graph
std::vector< double > graph_x(const TGraph &g)
Definition binning.h:31
owned::root_ptr< TGraph > zip(const std::vector< double > &xs, const std::vector< double > &ys)
Definition binning.h:48
std::vector< double > graph_y(const TGraph &g)
Definition binning.h:39
Definition algebra.h:32
owned::root_ptr< TGraph > block_average(const TGraph &source, std::size_t block_size, bool drop_partial=false)
Definition binning.h:67
owned::root_ptr< TGraph > block_rms(const TGraph &source, std::size_t block_size, bool drop_partial=false)
Definition binning.h:82
Definition owned.h:33
std::unique_ptr< T, root_deleter > root_ptr
Definition owned.h:60
root_ptr< T > make(Args &&... args)
Definition owned.h:67