GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
IO.cc
Go to the documentation of this file.
1/* STD */
2#include <fstream>
3#include <iostream>
4#include <sstream>
5
6/* GAMR */
7#include "IO.hh"
8
9namespace GamR {
10 namespace Spect {
11
12 std::string ToText(const TH1 *h, std::string outfile, std::string delimiter)
13 {
14 std::stringstream ss;
15 ss << "#ROOT TH1 in ASCII format" <<std::endl;
16 ss << "#name : " << h->GetName() << " title : " << h->GetTitle() << std::endl;
17 ss << "#x-axis : " << h->GetXaxis()->GetTitle() << " y-axis : " << h->GetYaxis()->GetTitle() << std::endl;
18 ss << "#x" << delimiter << "count" << delimiter << "error\n";
19 for (int i = 0; i < h->GetNbinsX(); ++i) {
20 ss << h->GetBinCenter(i) << delimiter;
21 ss << h->GetBinContent(i) << delimiter;
22 ss << h->GetBinError(i) << "\n";
23 }
24
25 if (!outfile.empty()) {
26 std::ofstream file(outfile);
27 file << ss.str();
28 file.close();
29 }
30
31 return ss.str();
32 }
33
34 std::string ToText(const TH2 *h, std::string outfile, std::string delimiter, int binx_lo, int binx_hi, int biny_lo, int biny_hi)
35 {
36 std::stringstream ss;
37 ss << "#ROOT TH2 in ASCII format" <<std::endl;
38 ss << "#name : " << h->GetName() << " title : " << h->GetTitle() << std::endl;
39 ss << "#x-axis : " << h->GetXaxis()->GetTitle() << " y-axis : " << h->GetYaxis()->GetTitle() << std::endl;
40 ss << "#x" << delimiter << "y" << delimiter << "count" << delimiter << "error\n";
41 int minx = 1;
42 int miny = 1;
43 int maxx = h->GetNbinsX();
44 int maxy = h->GetNbinsY();
45 if (binx_lo > 0) { minx = binx_lo; }
46 if (biny_lo > 0) { miny = biny_lo; }
47 if (binx_hi > 0) { maxx = binx_hi; }
48 if (biny_hi > 0) { maxy = biny_hi; }
49 for (int i = minx; i < maxx; ++i) {
50 for (int j = miny; j < maxy; ++j) {
51 ss << h->GetXaxis()->GetBinCenter(i) << delimiter;
52 ss << h->GetYaxis()->GetBinCenter(j) << delimiter;
53 ss << h->GetBinContent(i,j) << delimiter;
54 ss << h->GetBinError(i,j) << "\n";
55 }
56 ss << "\n";
57 }
58
59 if (!outfile.empty()) {
60 std::ofstream file(outfile);
61 file << ss.str();
62 file.close();
63 }
64
65 return ss.str();
66 }
67
68 std::string ToText(const TGraphErrors *g, std::string outfile, std::string delimiter)
69 {
70 std::stringstream ss;
71 ss << "#ROOT TGraphErrors in ASCII format" <<std::endl;
72 ss << "#name : " << g->GetName() << " title : " << g->GetTitle() << std::endl;
73 ss << "#x" << delimiter << "y" << delimiter << "yerror\n";
74 for (int i = 0; i < g->GetN(); ++i) {
75 double x;
76 double y;
77 g->GetPoint(i,x,y);
78 ss << x << delimiter;
79 ss << y << delimiter;
80 ss << g->GetErrorY(i) << "\n";
81 }
82
83 if (!outfile.empty()) {
84 std::ofstream file(outfile);
85 file << ss.str();
86 file.close();
87 }
88
89 return ss.str();
90 }
91
92 std::string ToText(const TGraphAsymmErrors *g, std::string outfile, std::string delimiter)
93 {
94 std::stringstream ss;
95 ss << "#ROOT TGraphAsymmErrors in ASCII format" <<std::endl;
96 ss << "#name : " << g->GetName() << " title : " << g->GetTitle() << std::endl;
97 ss << "#x" << delimiter << "y" << delimiter << "yerror\n";
98 for (int i = 0; i < g->GetN(); ++i) {
99 double x;
100 double y;
101 g->GetPoint(i,x,y);
102 ss << x << delimiter;
103 ss << y << delimiter;
104 ss << g->GetErrorYlow(i) << delimiter;
105 ss << g->GetErrorYhigh(i) << "\n";
106 }
107
108 if (!outfile.empty()) {
109 std::ofstream file(outfile);
110 file << ss.str();
111 file.close();
112 }
113
114 return ss.str();
115 }
116
117 std::string ToText(const TGraph *g, std::string outfile, std::string delimiter)
118 {
119 std::stringstream ss;
120 ss << "#ROOT TGraph in ASCII format" <<std::endl;
121 ss << "#name : " << g->GetName() << " title : " << g->GetTitle() << std::endl;
122 ss << "#x" << delimiter << "y\n";
123 for (int i = 0; i < g->GetN(); ++i) {
124 double x;
125 double y;
126 g->GetPoint(i,x,y);
127 ss << x << delimiter;
128 ss << y << std::endl;
129 }
130
131 if (!outfile.empty()) {
132 std::ofstream file(outfile);
133 file << ss.str();
134 file.close();
135 }
136
137 return ss.str();
138 }
139
140 std::string ToText(const TGraph *g, int ID, std::string outfile, std::string delimiter)
141 {
142 std::stringstream ss;
143 //ss << "#x" << delimiter << "y\n";
144 ss << "#ROOT TGraph in ASCII format" <<std::endl;
145 ss << "#name : " << g->GetName() << " title : " << g->GetTitle() << std::endl;
146 ss << ID << delimiter << g->GetN() << std::endl;
147 for (int i = 0; i < g->GetN(); ++i) {
148 double x;
149 double y;
150 g->GetPoint(i,x,y);
151 ss << x << delimiter;
152 ss << y << std::endl;
153 }
154
155 if (!outfile.empty()) {
156 std::ofstream file(outfile, std::ios_base::app);
157 file << ss.str();
158 file.close();
159 }
160
161 return ss.str();
162 }
163
164 std::string ToText(const TF1* f, std::string outfile, std::string delimiter)
165 {
166 std::stringstream ss;
167 ss << "#ROOT TF1 in ASCII format" <<std::endl;
168 ss << "#name : " << f->GetName() << " title : " << f->GetTitle() << std::endl;;
169 ss << "#x" << delimiter << "y\n";
170
171 double xlow, xhigh;
172 f->GetRange(xlow,xhigh);
173 for (int i = 0; i < f->GetNpx(); ++i) {
174 double x = xlow + i*(xhigh-xlow)/(f->GetNpx()-1.0);
175 ss << x << delimiter << f->Eval(x) << std::endl;
176 }
177
178 if (!outfile.empty()) {
179 std::ofstream file(outfile);
180 file << ss.str();
181 file.close();
182 }
183
184 return ss.str();
185 }
186
187 void ToBin(const TH2 *h, std::string outfile, int padx, int pady)
188 {
189 FILE *file = fopen(outfile.c_str(), "wb");
190 for (int i=1; i<=std::max(h->GetNbinsX(), padx); ++i) {
191 for (int j=1; j<=std::max(h->GetNbinsY(), pady); ++j) {
192 short int conts = 0;
193 if (i <=h->GetNbinsX() && j <= h->GetNbinsY()) {
194 conts = (short int)h->GetBinContent(i,j);
195 }
196 fwrite(&conts, sizeof(short int), 1, file);
197 }
198 }
199 fclose(file);
200 }
201
202 void ToBin(const TH1 *h, std::string outfile)
203 {
204 FILE *file = fopen(outfile.c_str(), "wb");
205 for (int i=0; i<h->GetNbinsX(); ++i) {
206 int conts = (int)h->GetBinContent(i);
207 fwrite(&conts, sizeof(int), 1, file);
208 }
209 fclose(file);
210 }
211
212 void ToSPE(const TH1 *h, std::string outfile)
213 {
214 /* write spectra in gf3 format */
215
216 char buf[32];
217 int c1 = 1, rl = 0;
218 char namesp[8];
219
220 int j=outfile.size();
221 FILE *file = fopen(outfile.c_str(), "wb");
222 strncpy(namesp, outfile.c_str(), 8);
223 if (j < 8) memset(&namesp[j], ' ', 8-j);
224
225 int idim = h->GetNbinsX();
226
227#define W(a,b) { memcpy(buf + rl, a, b); rl += b; }
228 W(namesp,8); W(&idim,4); W(&c1,4); W(&c1,4); W(&c1,4);
229#undef W
230
231 fwrite(&rl, 4, 1, file);
232 fwrite(buf, rl, 1, file);
233 fwrite(&rl, 4, 1, file);
234
235 fwrite(&idim, 4, 1, file);
236 for (int i=0; i<h->GetNbinsX(); ++i) {
237 int conts = (int)h->GetBinContent(i);
238 fwrite(&conts, sizeof(int), 1, file);
239 }
240 fwrite(&idim, 4, 1, file);
241
242 fclose(file);
243 }
244
245 TH1D *FromText(std::string s, std::string name, bool fill) {
246 FILE *file = fopen(s.c_str(), "ra");
247 std::stringstream ss;
248 char cline[2048];
249
250 std::vector<double> x_data;
251 std::vector<double> y_data;
252 std::vector<double> y_err;
253
254 while(std::fgets(cline, sizeof cline, file)!=NULL) {
255 std::string line(cline);
256 if (line.size() == 0) { continue; }
257 if (line[0] == '#') { continue; }
258 if (line[0] == ';') { continue; }
259 if (line[0] == '!') { continue; }
260
261 ss.clear();
262 ss.str(line);
263
264 double x,y,err;
265
266 ss >> x;
267 ss >> y;
268 ss >> err;
269
270 x_data.push_back(x);
271 y_data.push_back(y);
272 y_err.push_back(err);
273 }
274
275 int n = x_data.size();
276 //assuming constant bin width;
277 double bin_width = x_data[1]-x_data[0];
278 std::cout << bin_width << std::endl;
279 std::cout << x_data[0] << " " << x_data[1] << std::endl;
280 double start = x_data[0]-bin_width/2.0;
281 double stop = x_data[x_data.size()-1]+bin_width/2.0;
282
283 TH1D *hist = new TH1D(name.c_str(), name.c_str(), n, start, stop);
284 for (int i=0; i<n; ++i) {
285 if (fill) {
286 for (int j=0; j<y_data[i]; ++j) {
287 hist->Fill(x_data[i]);
288 }
289 }
290 else {
291 hist->SetBinContent(i+1, y_data[i]);
292 hist->SetBinError(i+1, y_err[i]);
293 }
294 }
295 return hist;
296 }
297
298 TH1D *FromBin(std::string s, std::string name, std::string title, int nx, double low, double high, int size) {
299 TH1D *hist = new TH1D(name.c_str(), title.c_str(), nx, low, high);
300 FILE *fp = fopen(s.c_str(), "r");
301 if (fp == NULL) { std::cerr << "Error, cannot open file " << s << std::endl; return NULL; }
302
303 fseek(fp, 0L, SEEK_END);
304 int ydim = (ftell(fp) / size ) / nx;
305 if (ydim != 1) { std::cerr << "Error, wrong file size " << s << std::endl; fclose(fp); return NULL; }
306
307 int data[nx];
308 rewind(fp);
309 if (fread(data, nx*size, 1, fp) != 1) { std::cerr << "Error reading file" << std::endl; fclose(fp); return NULL; }
310 for (int i=1; i<=nx; ++i) {
311 hist->SetBinContent(i,data[i-1]);
312 hist->SetBinError(i,std::sqrt(data[i-1]));
313 }
314 fclose(fp);
315 return hist;
316 }
317
318 TH2D *FromBin(std::string s, std::string name, std::string title,
319 int nx, double xlow, double xhigh,
320 int ny, double ylow, double yhigh,
321 int size) {
322
323 FILE *fp = fopen(s.c_str(), "r");
324 if (fp == NULL) { std::cerr << "Error, cannot open file " << s << std::endl; return NULL; }
325
326 fseek(fp, 0L, SEEK_END);
327 int ydim = (ftell(fp) / size ) / nx;
328 if (ny > 0) {
329 if (ydim != ny) { std::cerr << "Error, wrong file size " << s << std::endl; fclose(fp); return NULL; }
330 }
331 else {
332 //check if integer
333 if (ydim * size * nx != ftell(fp)) {
334 std::cerr << "Error, wrong file size, not evenly divisible " << s << std::endl; fclose(fp); return NULL;
335 }
336 std::cout << "Detected " << ydim << " rows" << std::endl;
337 ny = ydim;
338 }
339 if (ylow == yhigh) {
340 ylow = 0;
341 yhigh = ny;
342 }
343 TH2D *hist = new TH2D(name.c_str(), title.c_str(), nx, xlow, xhigh, ny, ylow, yhigh);
344
345 int data[nx];
346 rewind(fp);
347
348 for (int j=1; j<=ny; ++j) {
349 if (fread(data, nx*size, 1, fp) != 1) { std::cerr << "Error reading file" << std::endl; fclose(fp); return NULL; }
350 for (int i=1; i<=nx; ++i) {
351 hist->SetBinContent(i,j,data[i-1]);
352 hist->SetBinError(i,j,std::sqrt(data[i-1]));
353 }
354 }
355 fclose(fp);
356 return hist;
357 }
358
359 TCutG* CutFromText(std::string s, std::string name) {
360 FILE *file = fopen(s.c_str(), "ra");
361 std::stringstream ss;
362 char cline[2048];
363
364 std::vector<double> x_data;
365 std::vector<double> y_data;
366
367 while(std::fgets(cline, sizeof cline, file)!=NULL) {
368 std::string line(cline);
369 if (line.size() == 0) { continue; }
370 if (line[0] == '#') { continue; }
371 if (line[0] == ';') { continue; }
372
373 ss.clear();
374 ss.str(line);
375
376 double x,y,err;
377
378 ss >> x;
379 ss >> y;
380
381 x_data.push_back(x);
382 y_data.push_back(y);
383 }
384
385 int n = x_data.size();
386 //assuming constant bin width;
387
388 TCutG *cut = new TCutG();
389 cut->SetName(name.c_str());
390
391 for (int i=0; i<n; ++i) {
392 cut->SetPoint(i, x_data[i], y_data[i]);
393 }
394 return cut;
395 }
396
397 } // namespace Spect
398} // namespace GamR
TSpectrum * fp(TCanvas *canvas, double sigma, Option_t *option, double threshold)
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
#define W(a, b)
std::string ToText(const TH1 *h, std::string outfile, std::string delimiter)
Definition IO.cc:12
TH1D * FromText(std::string s, std::string name, bool fill)
Definition IO.cc:245
void ToBin(const TH2 *h, std::string outfile, int padx, int pady)
Definition IO.cc:187
void ToSPE(const TH1 *h, std::string outfile)
Definition IO.cc:212
TH1D * FromBin(std::string s, std::string name, std::string title, int nx, double low, double high, int size)
Definition IO.cc:298
TCutG * CutFromText(std::string s, std::string name)
Definition IO.cc:359
Definition Gain.cc:19