12 std::string
ToText(
const TH1 *h, std::string outfile, std::string delimiter)
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";
25 if (!outfile.empty()) {
26 std::ofstream file(outfile);
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)
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";
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";
59 if (!outfile.empty()) {
60 std::ofstream file(outfile);
68 std::string
ToText(
const TGraphErrors *g, std::string outfile, std::string delimiter)
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) {
80 ss << g->GetErrorY(i) <<
"\n";
83 if (!outfile.empty()) {
84 std::ofstream file(outfile);
92 std::string
ToText(
const TGraphAsymmErrors *g, std::string outfile, std::string delimiter)
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) {
102 ss << x << delimiter;
103 ss << y << delimiter;
104 ss << g->GetErrorYlow(i) << delimiter;
105 ss << g->GetErrorYhigh(i) <<
"\n";
108 if (!outfile.empty()) {
109 std::ofstream file(outfile);
117 std::string
ToText(
const TGraph *g, std::string outfile, std::string delimiter)
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) {
127 ss << x << delimiter;
128 ss << y << std::endl;
131 if (!outfile.empty()) {
132 std::ofstream file(outfile);
140 std::string
ToText(
const TGraph *g,
int ID, std::string outfile, std::string delimiter)
142 std::stringstream
ss;
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) {
151 ss << x << delimiter;
152 ss << y << std::endl;
155 if (!outfile.empty()) {
156 std::ofstream file(outfile, std::ios_base::app);
164 std::string
ToText(
const TF1* f, std::string outfile, std::string delimiter)
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";
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;
178 if (!outfile.empty()) {
179 std::ofstream file(outfile);
187 void ToBin(
const TH2 *h, std::string outfile,
int padx,
int pady)
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) {
193 if (i <=h->GetNbinsX() && j <= h->GetNbinsY()) {
194 conts = (
short int)h->GetBinContent(i,j);
196 fwrite(&conts,
sizeof(
short int), 1, file);
202 void ToBin(
const TH1 *h, std::string outfile)
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);
212 void ToSPE(
const TH1 *h, std::string outfile)
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);
225 int idim = h->GetNbinsX();
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);
231 fwrite(&rl, 4, 1, file);
232 fwrite(buf, rl, 1, file);
233 fwrite(&rl, 4, 1, file);
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);
240 fwrite(&idim, 4, 1, file);
245 TH1D *
FromText(std::string s, std::string name,
bool fill) {
246 FILE *file = fopen(s.c_str(),
"ra");
247 std::stringstream
ss;
250 std::vector<double> x_data;
251 std::vector<double> y_data;
252 std::vector<double> y_err;
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; }
272 y_err.push_back(err);
275 int n = x_data.size();
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;
283 TH1D *hist =
new TH1D(name.c_str(), name.c_str(), n, start, stop);
284 for (
int i=0; i<n; ++i) {
286 for (
int j=0; j<y_data[i]; ++j) {
287 hist->Fill(x_data[i]);
291 hist->SetBinContent(i+1, y_data[i]);
292 hist->SetBinError(i+1, y_err[i]);
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; }
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; }
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]));
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,
323 FILE *
fp = fopen(s.c_str(),
"r");
324 if (
fp == NULL) { std::cerr <<
"Error, cannot open file " << s << std::endl;
return NULL; }
326 fseek(
fp, 0L, SEEK_END);
327 int ydim = (ftell(
fp) / size ) / nx;
329 if (ydim != ny) { std::cerr <<
"Error, wrong file size " << s << std::endl; fclose(
fp);
return NULL; }
333 if (ydim * size * nx != ftell(
fp)) {
334 std::cerr <<
"Error, wrong file size, not evenly divisible " << s << std::endl; fclose(
fp);
return NULL;
336 std::cout <<
"Detected " << ydim <<
" rows" << std::endl;
343 TH2D *hist =
new TH2D(name.c_str(), title.c_str(), nx, xlow, xhigh, ny, ylow, yhigh);
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]));
360 FILE *file = fopen(s.c_str(),
"ra");
361 std::stringstream
ss;
364 std::vector<double> x_data;
365 std::vector<double> y_data;
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; }
385 int n = x_data.size();
388 TCutG *cut =
new TCutG();
389 cut->SetName(name.c_str());
391 for (
int i=0; i<n; ++i) {
392 cut->SetPoint(i, x_data[i], y_data[i]);
TH1D * FromBin(std::string s, std::string name, std::string title, int nx, double low, double high, int size)