-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist1D.cpp
93 lines (74 loc) · 2.66 KB
/
hist1D.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
/*
* Draw a simple hist from input files.
* Input files must contain at maximum 3 columns: x y dy or x y ey
* 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 hists will be superimposed.
*/
#include "MODULE.h"
void hist1D (vector<string> input, vector<string> outputFormat = {"root"}, string format = "xydy") {
Double_t xf, yf, dyf(0);
vector<Double_t> x, y, dy;
string line, output, outputFile;
Int_t point = 0;
Int_t colorID = 2;
TCanvas * c = new TCanvas("c1", "canvas", canvasWidth, canvasHeight);
c->SetWindowSize(canvasWidth + (canvasWidth - c->GetWw()), canvasHeight + (canvasHeight - c->GetWh()));
makeLegend();
TLegend * legend = new TLegend(legendCoordinates.x1, legendCoordinates.y1, legendCoordinates.x2, legendCoordinates.y2);
legend->SetTextAlign(12);
TH1D * hist;
gStyle->SetOptStat(false);
if (xLog) gPad->SetLogx();
if (yLog) gPad->SetLogy();
output = "integral";
std::ofstream fileOutput (output, ios::app);
fileOutput << std::setw(34) << std::setfill(' ') << std::left << "# Input file" << "Integral" << std::endl;
for (unsigned int i = 0; i < input.size(); i++) {
std::ifstream fileInput (input[i].c_str());
if (!fileInput.good()) {
std::cerr << "ERROR :: file " << input[i] << " not found" << std::endl;
exit(1);
}
while (getline(fileInput, line)) {
if (line[0] == '#' || line[0] == '\0') continue;
stringstream(line) >> xf >> yf >> dyf;
x.push_back(xf);
y.push_back(yf);
dy.push_back(dyf);
}
x.insert(x.begin(), 0);
hist = new TH1D(input[i].c_str(), "", y.size(), (Double_t * )&x[0]);
for (unsigned int l = 0; l < y.size(); l++) {
hist->SetBinContent(l + 1, y[l]);
if (format == "xyey")
hist->SetBinError(l + 1, y[1]*dy[l]);
else
hist->SetBinError(l + 1, dy[l]);
}
if (legendItem.size() == input.size()) {
legend->AddEntry(hist, legendItem[i].c_str(), "lep");
} else {
legend->AddEntry(hist, input[i].c_str(), "lep");
}
hist->SetTitle(tTitle.c_str());
colorID += colorID == 10 ? 1 : 0;
hist->SetLineColor(colorID);
hist->SetLineWidth(2);
if (xMin != xMax) hist->GetXaxis()->SetRangeUser(xMin, xMax);
if (yMin != yMax) hist->GetYaxis()->SetRangeUser(yMin, yMax);
hist->Draw("HIST E0 same");
fileOutput << std::setw(34) << std::setfill(' ') << std::left << input[i] << hist->Integral() << std::endl;
colorID++;
x.clear();
y.clear();
dy.clear();
}
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;
}