REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestHaloEvent.cxx
1#include "TRestHaloEvent.h"
2#include <algorithm>
3#include <numeric>
4#include <cmath>
5#include <TGraph.h>
6#include <TCanvas.h>
7#include <TString.h>
8#include <TBrowser.h>
9
10ClassImp(TRestHaloEvent);
11
12TRestHaloEvent::TRestHaloEvent() {}
13TRestHaloEvent::~TRestHaloEvent() {}
14
15// Initialize is intentionally a no-op; events are simple containers.
16// Required to satisfy TRestEvent pure virtual interface.
18
19void TRestHaloEvent::SetSpectrum(const std::vector<double>& freq,
20 const std::vector<double>& values,
21 TRestHaloMetadata::EValueUnit unit,
22 const std::vector<double>& uncertainties) {
23 fFrequency = freq;
24 fValues = values;
25 fMetadata.SetValueUnit(static_cast<int>(unit));
26
27 if (!fFrequency.empty()) {
28 fStartFrequency = fFrequency.front();
29 fStopFrequency = fFrequency.back();
30 if (fFrequency.size() > 1)
31 fResolution = fFrequency[1] - fFrequency[0];
32 else
33 fResolution = 0;
34 } else {
35 fStartFrequency = fStopFrequency = fResolution = 0;
36 }
37
38 // Set uncertainties if provided, otherwise leave empty
39 if (!uncertainties.empty()) {
40 if (uncertainties.size() != values.size()) {
41 std::cerr << "TRestHaloEvent::SetSpectrum() - Error: Uncertainties size ("
42 << uncertainties.size() << ") does not match values size ("
43 << values.size() << ")" << std::endl;
44 fUncertainties.clear();
45 return;
46 }
47 fUncertainties = uncertainties;
48 } else {
49 fUncertainties.clear();
50 }
51}
52
53void TRestHaloEvent::SetUncertainties(const std::vector<double>& uncertainties) {
54 if (uncertainties.size() != fValues.size()) {
55 std::cerr << "TRestHaloEvent::SetUncertainties() - Error: Uncertainties size ("
56 << uncertainties.size() << ") does not match values size ("
57 << fValues.size() << ")" << std::endl;
58 return;
59 }
60 fUncertainties = uncertainties;
61}
62
63double TRestHaloEvent::GetValueAtFrequency(double f, TRestHaloMetadata::EValueUnit outUnit) const {
64 if (fFrequency.empty() || fValues.empty()) return 0.0;
65 auto it = std::lower_bound(fFrequency.begin(), fFrequency.end(), f);
66 size_t idx = 0;
67 if (it == fFrequency.end()) idx = fFrequency.size() - 1;
68 else idx = std::distance(fFrequency.begin(), it);
69
70 double raw = fValues[idx];
71 auto inUnit = GetStoredUnit();
72 // If units match, return raw (or converted to requested unit)
73 if (inUnit == outUnit) return raw;
74
75 if (inUnit == TRestHaloMetadata::V_RMS && outUnit == TRestHaloMetadata::W) {
76 return VoltsToWatts(raw);
77 }
78 if (inUnit == TRestHaloMetadata::W && outUnit == TRestHaloMetadata::V_RMS) {
79 return WattsToVolts(raw);
80 }
81 // fallback: no conversion known
82 return raw;
83}
84
85
87 printf("TRestHaloEvent: bins=%zu res=%.3f\n",
88 fFrequency.size(), fResolution);
89 const_cast<TRestHaloMetadata&>(fMetadata).PrintMetadata();
90}
91
92
93TPad* TRestHaloEvent::DrawEvent(const TString& option) {
94 UNUSED(option);
95 if (fFrequency.empty() || fValues.empty()) return nullptr;
96
97 // Filter out non-finite points (NaN/Inf) to avoid plotting issues
98 std::vector<double> xf; xf.reserve(fFrequency.size());
99 std::vector<double> yf; yf.reserve(fValues.size());
100 for (size_t i = 0; i < fFrequency.size() && i < fValues.size(); ++i) {
101 double x = fFrequency[i];
102 double y = fValues[i];
103 if (std::isfinite(x) && std::isfinite(y)) {
104 xf.push_back(x);
105 yf.push_back(y);
106 }
107 }
108 if (xf.empty() || yf.empty()) return nullptr;
109
110 const int n = static_cast<int>(yf.size());
111 TGraph* g = new TGraph(n, xf.data(), yf.data());
112
113 TString title = GetName();
114 if (title.IsNull()) title = "TRestHaloEvent";
115 g->SetTitle(Form("%s;Frequency (Hz);Value", title.Data()));
116 // use an explicit name for the graph so it can be found in the pad
117 TString gname = TString("Spectrum_") + title;
118 g->SetName(gname);
119 g->SetLineWidth(2);
120 g->SetMarkerStyle(20);
121
122 // Create an explicit canvas (avoid implicit default-canvas edge cases)
123 TString cname = TString("c_") + title;
124 TCanvas* c = new TCanvas(cname, title, 800, 600);
125 c->cd();
126 g->Draw("AL");
127 c->Modified();
128 c->Update();
129 return c;
130}
131
132
133void TRestHaloEvent::Browse(TBrowser* b) {
134 if (!b) return;
135 if (fFrequency.empty() || fValues.empty()) return;
136
137 const int n = static_cast<int>(fValues.size());
138 TGraph* g = nullptr;
139 try {
140 g = new TGraph(n, fFrequency.data(), fValues.data());
141 } catch (...) {
142 return;
143 }
144 TString gname = TString("Spectrum_") + GetName();
145 if (gname.IsNull()) gname = "Spectrum";
146 g->SetName(gname);
147 g->SetTitle(Form("%s;Frequency (Hz);Value", gname.Data()));
148
149 b->Add(g, gname.Data());
150}
virtual TPad * DrawEvent(const TString &option="") override
Draw the event.
void Initialize() override
void PrintEvent() const override
Dedicated metadata for halo/acquisition settings.