-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_root_objects.cpp
More file actions
205 lines (173 loc) · 6.42 KB
/
create_root_objects.cpp
File metadata and controls
205 lines (173 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "TFile.h"
#include "TGraph.h"
#include "TGraphErrors.h"
// --- UTILS FUNCTION -----------------------------------------------
// Read a file.txt of n_colums and create a vector<vector> in which each vector
// is a line
void Read_n_columns(std::string fileName, std::vector<std::vector<double>>& y,
size_t n_colums) {
double t;
y.clear();
y.resize(n_colums);
std::ifstream file(fileName.c_str());
if (!file.is_open()){
std::cout << "Error opening " << fileName << std::endl;
return;
}
while (true) {
std::vector<double> line;
for (size_t i=0; i<n_colums; i++){
if (!(file >> t)) {
// if (i==0){
// std::cout << "Error reading data from " << fileName << std::endl;
// return;
// }
// else {
// std::cerr << "Error reading data from " << fileName
// << ". The file may not have enough columns." << std::endl;
// }
std::cout << "Done" << fileName << std::endl;
return;
}
line.push_back(t);
}
for (size_t i=0; i<n_colums; i++){
y[i].push_back(line[i]);
if (i==0) std::cout << line[0] << std::endl;
}
}
return;
}
//**********************************************************
std::vector<std::pair<std::string, std::vector<double>>> read_vec_pair_CSV(const std::string& filename) {
//**********************************************************
std::ifstream infile(filename);
if (!infile.is_open()) {
throw std::runtime_error("Could not open file: " + filename);
}
std::vector<std::pair<std::string, std::vector<double>>> data;
std::string line;
// Read the header line
if (std::getline(infile, line)) {
std::stringstream ss(line);
std::string header;
while (std::getline(ss, header, ',')) {
if (!header.empty()) {
data.emplace_back(header, std::vector<double>{});
}
}
}
// Read the data lines
while (std::getline(infile, line)) {
std::stringstream ss(line);
std::string value;
size_t col_index = 0;
while (std::getline(ss, value, ',') && col_index < data.size()) {
try {
if (!value.empty()) {
data[col_index].second.push_back(std::stod(value)); // Convert to double
}
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid number at column " << col_index + 1 << std::endl;
data[col_index].second.push_back(0.0); // Default value for invalid numbers
}
col_index++;
}
}
infile.close();
return data;
}
// --- CREATE ROOT OBJECTS FUNCTIONS --------------------------------
void create_tgraph_from_txt(std::string txt_file_name,
std::string tgraph_name,
std::string folder = "") {
std::cout << "The file must be header free and only with two colums\n"
<< "x-column \t y-column" << std::endl;
std::vector<std::vector<double>> data;
Read_n_columns(txt_file_name, data, 2);
TGraph* graph = new TGraph(data[0].size(), &data[0][0], &data[1][0]);
graph->SetName(tgraph_name.c_str()); graph->SetTitle(tgraph_name.c_str());
TFile* file = TFile::Open((folder+tgraph_name+".root").c_str(), "RECREATE");
file->cd();
graph->Write();
file->Close();
return;
}
void create_tgrapherrors_from_txt(std::string txt_file_name,
std::string tgraph_name,
std::string folder = "") {
std::cout << "The file must be header free and with four colums\n"
<< "x-column \t y-column \t x-error \t y-error" << std::endl;
std::vector<std::vector<double>> data;
Read_n_columns(txt_file_name, data, 4);
TGraphErrors* graph_errors = new TGraphErrors(data[0].size(), &data[0][0], &data[1][0],
&data[2][0], &data[3][0]);
graph_errors->SetName(tgraph_name.c_str()); graph_errors->SetTitle(tgraph_name.c_str());
TFile* file = TFile::Open((folder+tgraph_name+".root").c_str(), "RECREATE");
file->cd();
graph_errors->Write();
file->Close();
return;
}
void create_tgraph_from_csv(std::string csv_file_name,
std::string tgraph_name,
std::string x_col_name = "",
std::string y_col_name = "",
std::string folder = "") {
std::vector<std::pair<std::string, std::vector<double>>> data;
std::cout << "d" << std::endl;
data = read_vec_pair_CSV(csv_file_name);
std::cout << "d" << std::endl;
if (x_col_name == "") {
x_col_name = data[0].first; // Default to the first column
}
if (y_col_name == "") {
y_col_name = data[1].first; // Default to the second column
}
std::cout << "d" << std::endl;
TGraph* graph = new TGraph(data[0].second.size(),
&data[0].second[0], &data[1].second[0]);
graph->SetName(tgraph_name.c_str());
graph->SetTitle(tgraph_name.c_str());
std::cout << "d" << std::endl;
TFile* file = TFile::Open((folder+tgraph_name+".root").c_str(), "RECREATE");
file->cd();
graph->Write();
file->Close();
return;
}
void create_tgrapherrors_from_csv(std::string csv_file_name,
std::string tgraph_name,
std::string x_col_name = "",
std::string y_col_name = "",
std::string err_x_col_name = "",
std::string err_y_col_name = "",
std::string folder = "") {
std::vector<std::pair<std::string, std::vector<double>>> data;
data = read_vec_pair_CSV(csv_file_name);
if (x_col_name == "") {
x_col_name = data[0].first; // Default to the first column
}
if (y_col_name == "") {
y_col_name = data[1].first; // Default to the second column
}
if (err_x_col_name == "") {
x_col_name = data[2].first;
}
if (err_y_col_name == "") {
y_col_name = data[3].first;
}
TGraphErrors* graph_errors = new TGraphErrors(data[0].second.size(), &data[0].second[0], &data[1].second[0],
&data[2].second[0], &data[3].second[0]);
graph_errors->SetName(tgraph_name.c_str()); graph_errors->SetTitle(tgraph_name.c_str());
TFile* file = TFile::Open((folder+tgraph_name+".root").c_str(), "RECREATE");
file->cd();
graph_errors->Write();
file->Close();
return;
}