wavecatcher-analysis
Loading...
Searching...
No Matches
Helpers.cc
Go to the documentation of this file.
1#include "Helpers.h"
2
7string Helpers::ListFiles(const char* dirname, const char* ext) {
8 stringstream ss;
9 TSystemDirectory dir(dirname, dirname);
10 TList* files = dir.GetListOfFiles();
11
12 TIter next(files);
13 TObjString* objString;
14 while ((objString = (TObjString*)next())) {
15 const char* fileName = objString->GetString().Data();
16 // skip hidden/temporary files from backup etc.
17 if (fileName[0] == '.') continue;
18 // Check if the filename or extension contains ".bin"
19 if (strstr(fileName, ext) != nullptr) ss << fileName << "\n";
20 }
21 if (ss.str().empty()) throw runtime_error("Error: No .bin files found in " + static_cast<string>(dirname) + "\n");
22 return ss.str();
23}
24
28void Helpers::PrintProgressBar(int index, int length) {
29 static int lastProgress = -1;
30 int progress = (10 * (index + 1)) / length;
31 if (progress != lastProgress) {
32 lastProgress = progress;
33 cout << "\r[" << string(progress * 5, '#')
34 << string(50 - progress * 5, ' ')
35 << "] " << progress * 10 << "%";
36 cout.flush();
37 }
38 if (index + 1 == length) cout << endl << endl;
39}
40
44int Helpers::rcolor(unsigned int i) {
45 const int nclrs = 17;
46 int rclrs[nclrs] = { 1, 2, 3, 4, 6, 8, 9, 13, 20, 28, 30, 34, 38, 40, 31, 46, 49 };
47 return rclrs[i - static_cast<int>(floor(i / nclrs)) * nclrs];
48}
49
59void Helpers::SetRangeCanvas(TCanvas*& c, double x_range_min, double x_range_max, double y_range_min, double y_range_max) {
60
61 // Lambda function to set the axis ranges
62 auto setAxisRanges = [&](TH1* his) {
63 if (x_range_min != x_range_max) his->GetXaxis()->SetRangeUser(x_range_min, x_range_max);
64 if (y_range_min != y_range_max) his->GetYaxis()->SetRangeUser(y_range_min, y_range_max);
65 };
66
67 // Get the list of pads on the canvas and loop over pads
68 TList* pads = c->GetListOfPrimitives();
69 TIter nextPad(pads);
70 TObject* object;
71 while ((object = nextPad())) {
72 if (object->InheritsFrom(TPad::Class())) {
73 TPad* pad = static_cast<TPad*>(object);
74 // Set the axis ranges on the current pad
75 pad->cd();
76 TList* primitives = pad->GetListOfPrimitives();
77 TIter nextPrimitive(primitives);
78 TObject* primitive;
79 while ((primitive = nextPrimitive())) {
80 if (primitive->InheritsFrom(TH1::Class())) {
81 setAxisRanges(static_cast<TH1*>(primitive));
82 }
83 }
84 }
85 else if (object->InheritsFrom(TH1::Class())) {
86 setAxisRanges(static_cast<TH1*>(object));
87 }
88 }
89 c->Modified();
90 c->Update();
91}
92
93
97void Helpers::filterChannelUserInput(vector<int>& user_channels, const vector<int> active_channels) {
98 vector<int> rmv;
99
100 for (int channel : user_channels) {
101 if (!Helpers::Contains(active_channels, channel)) {
102 cout << "\n ------------ WARNING ------------\n"
103 << "YOUR SELECTED CHANNEL " << channel << " DOES NOT EXIST IN DATA" << endl;
104 rmv.push_back(channel);
105 }
106 }
107
108 for (int channel : rmv) {
109 auto it = find(user_channels.begin(), user_channels.end(), channel);
110 if (it != user_channels.end()) {
111 user_channels.erase(it);
112 }
113 }
114}
115
116
121void Helpers::SplitCanvas(TCanvas*& c, vector<int> active_channels, vector<int> plot_active_channels) {
122 // cross check if user input exists in data
123 filterChannelUserInput(plot_active_channels, active_channels);
124
125 if (plot_active_channels.empty()) {
126 c->Divide(TMath::Min(static_cast<double>(active_channels.size()), 4.), TMath::Max(TMath::Ceil(static_cast<double>(active_channels.size()) / 4.), 1.), 0, 0);
127 }
128 else if (static_cast<int>(plot_active_channels.size()) > 1) {
129 c->Divide(TMath::Min(static_cast<double>(plot_active_channels.size()), 4.), TMath::Max(ceil(static_cast<double>(plot_active_channels.size()) / 4.), 1.), 0, 0);
130 }
131}
132
137template <typename HistType>
138double* Helpers::gety(HistType* his) {
139 static_assert(is_base_of<TH1, HistType>::value, "ERROR in Helpers::gety():\n Argument must be ROOT TH1 histogram.");
140 double* yvals = new double[his->GetNbinsX()];
141 for (int i = 0; i < his->GetNbinsX(); ++i) {
142 yvals[i] = static_cast<double>(his->GetBinContent(i + 1));
143 }
144 return yvals;
145}
146template double* Helpers::gety<TH1F>(TH1F*);
147template double* Helpers::gety<TH1D>(TH1D*);
148template double* Helpers::gety<TH1I>(TH1I*);
149
156template <typename HistType>
157double* Helpers::gety(HistType* his, int start_at, int end_at) {
158 static_assert(is_base_of<TH1, HistType>::value, "ERROR in Helpers::gety():\n Argument must be ROOT TH1 histogram.");
159
160 if (start_at < 0 || end_at > his->GetNbinsX() || end_at <= start_at) {
161 cout << "\nError: Helpers::gety out of range" << endl;
162 return nullptr;
163 }
164 const int n_bins_new = end_at - start_at;
165 double* yvals = new double[n_bins_new];
166 for (int i = start_at; i < end_at; i++) {
167 yvals[i - start_at] = his->GetBinContent(i + 1);
168 }
169 return yvals;
170}
171template double* Helpers::gety<TH1F>(TH1F*, int, int);
172template double* Helpers::gety<TH1D>(TH1D*, int, int);
173template double* Helpers::gety<TH1I>(TH1I*, int, int);
174
180double* Helpers::gety(const vector<float>& waveform, int start_at, int end_at) {
181 if (start_at < 0 || end_at > static_cast<int>(waveform.size()) || end_at <= start_at) {
182 cout << "\nError: Helpers::gety out of range" << endl;
183 return nullptr;
184 }
185 const int n_bins_new = end_at - start_at;
186 double* yvals = new double[n_bins_new];
187 for (int i = start_at; i < end_at; i++) {
188 yvals[i - start_at] = static_cast<double>(waveform[i]);
189 }
190 return yvals;
191}
192
198template <typename HistType>
199void Helpers::ShiftTH1(HistType*& his, int shift_bins) {
200 static_assert(is_base_of<TH1, HistType>::value, "ERROR in Helpers::gety():\n Argument must be ROOT TH1 histogram.");
201 int nbins = his->GetNbinsX();
202 shift_bins = shift_bins % nbins;
203 double* yvals = Helpers::gety(his);
204
205 for (int i = 0; i < nbins; i++) {
206 int new_bin = (i + shift_bins) % nbins;
207 if (new_bin < 0) new_bin += nbins;
208 his->SetBinContent(i + 1, yvals[new_bin]);
209 }
210 delete[] yvals;
211}
212template void Helpers::ShiftTH1<TH1F>(TH1F*&, int);
213template void Helpers::ShiftTH1<TH1D>(TH1D*&, int);
214template void Helpers::ShiftTH1<TH1I>(TH1I*&, int);
static void SplitCanvas(TCanvas *&, vector< int >, vector< int >)
Helper to split canvas according to the number of channels to be plotted.
Definition Helpers.cc:121
static double * gety(HistType *his)
Get array of y values for a histogram.
Definition Helpers.cc:138
static void SetRangeCanvas(TCanvas *&, double, double, double=-999, double=-999)
Set consistent x-axis and y-axis range for all TH1 histograms on a canvas.
Definition Helpers.cc:59
static int rcolor(unsigned int)
Translate a random number into a useful root color https://root.cern.ch/doc/master/classTColor....
Definition Helpers.cc:44
static void filterChannelUserInput(vector< int > &, const vector< int >)
Check if user input exists in data and remove channels that are not there.
Definition Helpers.cc:97
static bool Contains(const vector< T > &vec, const T &val)
Returns true if vector vec contains value val.
Definition Helpers.h:55
static void ShiftTH1(HistType *&, int)
Shift a histogram in x The histogram will be cycled, so bins at the end will be attached at the fron...
Definition Helpers.cc:199
static void PrintProgressBar(int, int)
Print progress bar for a loop in steps of 10 percent.
Definition Helpers.cc:28
static string ListFiles(const char *, const char *)
Helper. Creates a list of .bin data files in data folder to be read in.
Definition Helpers.cc:7