-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph1D.cpp
96 lines (78 loc) · 2.48 KB
/
graph1D.cpp
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
/*
* Draw a simple graph from input files.
* Input files must contain at maximum 3 columns: x dx y dy
* Available formats:
* x y -> "xy"
* x y dy -> "xydy"
* x dx y -> "xdxy"
* x dx y dy -> "xdxydy"
* Empty lines and lines starting with # will ne ignored.
* Errors will be considered as absolute errors.
* If there is more than 1 input file, all graphs will be superimposed.
*/
#include "MODULE.h"
void graph1D (vector<string> input, vector<string> outputFormat = {"root"}, string format = "xy") {
const string blue("\033[1;34m"); // info
const string green("\033[1;32m"); // request
const string red("\033[1;31m"); // errors
const string yellow("\033[1;33m"); // warnings
Int_t colorID = 2;
string line, outputFile;
unsigned int point = 0;
Double_t x, y, dx(0), dy(0);
TCanvas * c = new TCanvas();
TLegend * legend = new TLegend();
legend->SetTextAlign(12);
TGraphErrors * graph;
for (unsigned int i = 0; i < input.size(); i++) {
graph = new TGraphErrors();
graph->SetMarkerSize(1);
graph->SetMarkerStyle(20);
if (legendItem.size() == input.size()) {
legend->AddEntry(graph, legendItem[i].c_str(), "lep");
} else {
legend->AddEntry(graph, input[i].c_str(), "lep");
}
ifstream fileInput (input[i].c_str());
if (!fileInput.good()) {
cerr << red << "ERROR :: file " << input[i] << " not found" << endl;
exit(1);
}
while (getline(fileInput, line)) {
if (line[0]=='#' || line[0]=='\0') continue;
if (format == "xdxydy")
stringstream(line) >> x >> dx >> y >> dy;
else if (format == "xydy")
stringstream(line) >> x >> y >> dy;
else if (format == "xdxy")
stringstream(line) >> x >> dx >> y;
else
stringstream(line) >> x >> y;
graph->SetPoint(point, x, y);
graph->SetPointError(point, dx, dy);
point++;
}
point = 0;
graph->SetTitle(tTitle.c_str());
graph->SetTitle(tTitle.c_str());
colorID += colorID == 10 ? 1 : 0;
graph->SetLineColor(colorID);
graph->SetLineWidth(2);
graph->SetMarkerColor(graph->GetLineColor());
graph->SetTitle((input[i] + ";x;y").c_str());
if (xMin != xMax) graph->GetXaxis()->SetRangeUser(xMin, xMax);
if (yMin != yMax) graph->GetYaxis()->SetRangeUser(yMin, yMax);
if (i > 0)
graph->Draw("PLE");
else
graph->Draw("APLE");
colorID++;
}
if (input.size() > 1)
legend->Draw();
for (unsigned int i = 0; i < outputFormat.size(); i++) {
outputFile = input[0] + "." + outputFormat[i];
c->SaveAs(outputFile.c_str());
}
return;
}