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
97void Helpers::SplitCanvas(TCanvas*& c, vector<int> active_channels, vector<int> plot_active_channels) {
98 // cross check if user input exists in data
99 vector<int> rmv;
100 for (int i = 0; i < static_cast<int>(plot_active_channels.size()); i++) {
101 if (find(active_channels.begin(), active_channels.end(), plot_active_channels[i]) == active_channels.end()) {
102 cout << "\n\n\n ------------ WARNING ------------\n";
103 cout << "YOUR SELECTED CHANNEL " << plot_active_channels[i] << " DOES NOT EXIST IN DATA\n";
104 cout << "PLEASE CHANGE plot_active_channels\n\n\n";
105 rmv.push_back(plot_active_channels[i]);
106 }
107 }
108
109 for (int i = 0; i < static_cast<int>(rmv.size()); i++) {
110 auto it = find(plot_active_channels.begin(), plot_active_channels.end(), rmv[i]);
111 if (it != plot_active_channels.end()) plot_active_channels.erase(it);
112 }
113
114 if (plot_active_channels.empty()) {
115 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);
116 }
117 else if (static_cast<int>(plot_active_channels.size()) > 1) {
118 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);
119 }
120}
121
125double* Helpers::gety(TH1F* his) {
126 double* yvals = new double[his->GetNbinsX()];
127 for (int i = 0; i < his->GetNbinsX(); i++) {
128 yvals[i] = his->GetBinContent(i + 1);
129 }
130 return yvals;
131}
132
138double* Helpers::gety(TH1F* his, int start_at, int end_at) {
139 if (start_at < 0 || start_at >= his->GetNbinsX() || end_at >= his->GetNbinsX() || end_at - start_at < 1) {
140 cout << "\nError: Helpers::gety out of range" << endl;
141 return 0;
142 }
143 const int n_bins_new = end_at - start_at;
144 double* yvals = new double[n_bins_new];
145 for (int i = start_at; i < end_at; i++) {
146 yvals[i - start_at] = his->GetBinContent(i + 1);
147 }
148 return yvals;
149}
150
157void Helpers::ShiftTH1F(TH1F*& his, int shift_bins) {
158 int nbins = his->GetNbinsX();
159 shift_bins = shift_bins % nbins;
160 double* yvals = Helpers::gety(his);
161
162 for (int i = 0; i < nbins; i++) {
163 int icycle = 0;
164 if (i + shift_bins >= nbins) icycle = -1 * nbins;
165 else if (i + shift_bins < 0) icycle = nbins;
166 his->SetBinContent(i + 1, yvals[i + shift_bins + icycle]);
167 }
168 delete[] yvals;
169}
static void SplitCanvas(TCanvas *&, vector< int >, vector< int >)
Helper to split canvas according to the number of channels to be plotted.
Definition Helpers.cc:97
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 void ShiftTH1F(TH1F *&, int=0)
Shift a histogram in x.
Definition Helpers.cc:157
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 PrintProgressBar(int, int)
Print progress bar for a loop in steps of 10 percent.
Definition Helpers.cc:28
static double * gety(TH1F *)
Get array of y values for a histogram.
Definition Helpers.cc:125
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