mist-hep 0.1.0
ROOT-backed analysis helpers built on mist
Loading...
Searching...
No Matches
fill.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2//
3// mist/hep/histo/fill.h — fill histograms from graphs / functions / profiles.
4//
5// project_y returns a new owned histogram; the fill_* helpers add entries to
6// a histogram the caller already owns (they mutate the target and return
7// void — no allocation, no ownership transfer).
8//
9// Salvaged from the ePIC SiPM-characterisation laser/waveform utilities
10// (mist-hep:F-55, F-56).
11//
12#pragma once
13
14#include <TF1.h>
15#include <TGraph.h>
16#include <TH1.h>
17#include <TH1F.h>
18#include <TH2.h>
19#include <TProfile.h>
20
21#include <mist/hep/owned.h>
22
24
25namespace owned = ::mist::hep::owned;
26
27// ---------------------------------------------------------------------------
28// project_y: histogram the y-values of a graph into a new 1-D histogram.
29// Returns an owned, gDirectory-detached TH1F.
30// ---------------------------------------------------------------------------
31[[nodiscard]] inline owned::root_ptr<TH1F>
32project_y(const TGraph& source, int n_bins, double min, double max,
33 const char* name = "histo_project_y")
34{
35 auto h = owned::make<TH1F>(name, name, n_bins, min, max);
36 for (int i = 0; i < source.GetN(); ++i) h->Fill(source.GetPointY(i));
37 return h;
38}
39
40// ---------------------------------------------------------------------------
41// fill_profile: accumulate a graph's (x, y) points into a TProfile (e.g. to
42// build the mean curve of many overlaid graphs). Mutates `target`.
43// ---------------------------------------------------------------------------
44inline void fill_profile(TProfile& target, const TGraph& source)
45{
46 for (int i = 0; i < source.GetN(); ++i)
47 target.Fill(source.GetPointX(i), source.GetPointY(i));
48}
49
50// ---------------------------------------------------------------------------
51// fill_persistence: accumulate into a 2-D histogram (a "persistence" / heat
52// map). From a graph's points, or by sampling a function at each x-bin centre.
53// Mutates `target`.
54// ---------------------------------------------------------------------------
55inline void fill_persistence(TH2& target, const TGraph& source)
56{
57 for (int i = 0; i < source.GetN(); ++i)
58 target.Fill(source.GetPointX(i), source.GetPointY(i));
59}
60
61inline void fill_persistence(TH2& target, const TF1& f)
62{
63 auto& fn = const_cast<TF1&>(f); // TF1::Eval is non-const in older ROOT
64 for (int i = 1; i <= target.GetNbinsX(); ++i) {
65 const double x = target.GetXaxis()->GetBinCenter(i);
66 target.Fill(x, fn.Eval(x));
67 }
68}
69
70// ---------------------------------------------------------------------------
71// fill_from_interval: histogram the y-values (graph) or bin contents
72// (profile) whose x falls in [min, max] into a 1-D histogram. Mutates
73// `target`. The graph overload assumes ascending x and stops past max.
74// ---------------------------------------------------------------------------
75inline void fill_from_interval(TH1& target, const TGraph& source,
76 double min, double max)
77{
78 for (int i = 0; i < source.GetN(); ++i) {
79 const double x = source.GetPointX(i);
80 if (x < min) continue;
81 if (x > max) break;
82 target.Fill(source.GetPointY(i));
83 }
84}
85
86inline void fill_from_interval(TH1& target, const TProfile& source,
87 double min, double max)
88{
89 for (int i = 1; i <= source.GetNbinsX(); ++i) {
90 const double x = source.GetBinCenter(i);
91 if (x < min) continue;
92 if (x > max) break;
93 target.Fill(source.GetBinContent(i));
94 }
95}
96
97} // namespace mist::hep::histo
Definition fill.h:23
void fill_persistence(TH2 &target, const TGraph &source)
Definition fill.h:55
owned::root_ptr< TH1F > project_y(const TGraph &source, int n_bins, double min, double max, const char *name="histo_project_y")
Definition fill.h:32
void fill_from_interval(TH1 &target, const TGraph &source, double min, double max)
Definition fill.h:75
void fill_profile(TProfile &target, const TGraph &source)
Definition fill.h:44
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