Advanced Computing Platform for Theoretical Physics

Commit c4430352 authored by Yu-Chen Ding's avatar Yu-Chen Ding
Browse files

custom style/support band

parent 416d86a7
build
deprecated
*.disabled
# config
.ycm_extra_conf.py
.cache
# Editor #
.*.sw?
......
......@@ -371,3 +371,14 @@ std::shared_ptr<const CRData> CRDataset::find_exact(const std::string& quantity,
}
return found;
}
CRData CRData::from_crd(const std::string& filename) {
_CRUTIL::FileParser fp(filename);
fp.next_line(fp.skip::both);
return *_CRDATASET::parse_data_crd(fp);
}
CRData CRData::from_usine(const std::string& filename) {
_CRUTIL::FileParser fp(filename);
fp.next_line(fp.skip::both);
return *_CRDATASET::parse_data_usine(fp);
}
......@@ -14,10 +14,14 @@ struct CRCurve {
};
struct CRBand {
std::valarray<double> x, y_lower, y_upper;
std::valarray<double> x, y, y_lower, y_upper;
};
class CRData {
public:
static CRData from_crd(const std::string& filename);
static CRData from_usine(const std::string& filename);
public:
friend class CRDataset;
......@@ -109,14 +113,23 @@ class CRDataset : public std::vector<std::shared_ptr<CRData>> {
public:
std::shared_ptr<const CRData> find_exact(const std::string& quantity,
const std::string& exp) const;
const std::string& exp) const;
};
class CRPlot {
public:
std::map<std::string, CRData> _egraphs;
std::map<std::string, CRCurve> _graphs;
std::map<std::string, CRBand> _band;
struct Style {
short color = 1;
short style = 1;
short size = -1; // lines:width data:marker_size
short fill_style = 1030;
short fill_color = -1;
};
public:
std::map<std::string, std::pair<CRData, Style>> _egraphs;
std::map<std::string, std::pair<CRCurve, Style>> _graphs;
std::map<std::string, std::pair<CRBand, Style>> _bands;
public:
std::string title;
......@@ -127,25 +140,29 @@ class CRPlot {
int legend_phase;
bool log_x, log_y;
bool grid_x, grid_y;
// todo: normalized comparisons
public:
CRPlot(const std::string& title = "Untitled Plot");
void AddData(const CRData& data, const std::string& label = "");
// void AddData(const std::string& filename, const std::string& label,
void AddData(const CRData&, const Style&, const std::string& label = "");
// void AddData(const std::string& filename, const Style&,
// const std::string& label,
// const std::array<size_t, 3> columns = {0});
void DropData(const std::string& label = "");
void AddCurve(const CRCurve& curve, const std::string& label);
void AddCurve(const std::string& filename, const std::string& label,
void AddCurve(const CRCurve&, const Style&, const std::string& label);
void AddCurve(const std::string& filename, const Style&,
const std::string& label,
const std::array<size_t, 2>& columns = {0});
void DropCurve(const std::string& label = "");
void AddBand(const CRBand& band, const std::string& label);
void AddBand(const std::string& filename, const std::string& label,
const std::array<size_t, 3>& columns = {0});
void AddBand(const CRBand&, const Style&, const std::string& label);
void AddBand(const std::string& filename, const Style&,
const std::string& label,
const std::array<size_t, 4>& columns = {0});
void DropBand(const std::string& label = "");
public:
......
......@@ -69,13 +69,20 @@ read_columns(const std::string& filename,
}; // namespace _CRPLOT
CRPlot::CRPlot(const std::string& title)
: title(title), x_min(NAN), x_max(NAN), y_min(NAN), y_max(NAN),
scale_index(0), legend_phase(4), log_x(true), log_y(true) {}
: title(title), //
x_min(NAN), x_max(NAN), //
y_min(NAN), y_max(NAN), //
log_x(true), log_y(true), //
grid_x(true), grid_y(true), //
scale_index(0), legend_phase(4) //
{}
void CRPlot::AddData(const CRData& data, const std::string& label) {
using Style = CRPlot::Style;
void CRPlot::AddData(const CRData& data, const Style& style,
const std::string& label) {
std::string l = label.empty() ? data.label : label;
_egraphs.erase(l);
_egraphs.insert({l, data});
_egraphs.insert({l, {data, style}});
}
// void CRPlot::AddData(const std::string& filename, const std::string& label,
// const std::array<size_t, 3> columns) {
......@@ -90,18 +97,20 @@ void CRPlot::DropData(const std::string& label) {
}
}
void CRPlot::AddCurve(const CRCurve& curve, const std::string& label) {
void CRPlot::AddCurve(const CRCurve& curve, const Style& style,
const std::string& label) {
_graphs.erase(label);
_graphs.insert({label, curve});
_graphs.insert({label, {curve, style}});
}
void CRPlot::AddCurve(const std::string& filename, const std::string& label,
void CRPlot::AddCurve(const std::string& filename, const Style& style,
const std::string& label,
const std::array<size_t, 2>& columns) {
std::string l = label.empty() ? filename : label;
const auto ret = _CRPLOT::read_columns(filename, columns);
const size_t n = ret[0].size();
AddCurve({std::valarray<double>(ret[0].data(), n),
std::valarray<double>(ret[1].data(), n)},
l);
style, l);
}
void CRPlot::DropCurve(const std::string& label) {
if (label.empty()) {
......@@ -111,32 +120,35 @@ void CRPlot::DropCurve(const std::string& label) {
}
}
void CRPlot::AddBand(const CRBand& band, const std::string& label) {
_band.erase(label);
_band.insert({label, band});
void CRPlot::AddBand(const CRBand& band, const Style& style,
const std::string& label) {
_bands.erase(label);
_bands.insert({label, {band, style}});
}
void CRPlot::AddBand(const std::string& filename, const std::string& label,
const std::array<size_t, 3>& columns) {
void CRPlot::AddBand(const std::string& filename, const Style& style,
const std::string& label,
const std::array<size_t, 4>& columns) {
std::string l = label.empty() ? filename : label;
const auto ret = _CRPLOT::read_columns(filename, columns);
const size_t n = ret[0].size();
AddBand({std::valarray<double>(ret[0].data(), n),
std::valarray<double>(ret[1].data(), n),
std::valarray<double>(ret[2].data(), n)},
l);
std::valarray<double>(ret[2].data(), n),
std::valarray<double>(ret[3].data(), n)},
style, l);
}
void CRPlot::DropBand(const std::string& label) {
if (label.empty()) {
_band.clear();
_bands.clear();
} else {
_band.erase(label);
_bands.erase(label);
}
}
double AdjustXMin(const CRPlot& plot) {
double x = NAN;
for (const auto& d : plot._egraphs) {
double xmin = d.second.x_min();
double xmin = d.second.first.x_min();
if (!(x <= xmin)) { // including nan
x = xmin;
}
......@@ -146,7 +158,7 @@ double AdjustXMin(const CRPlot& plot) {
double AdjustXMax(const CRPlot& plot) {
double x = NAN;
for (const auto& d : plot._egraphs) {
double xmax = d.second.x_max();
double xmax = d.second.first.x_max();
if (!(x >= xmax)) { // including nan
x = xmax;
}
......@@ -156,7 +168,7 @@ double AdjustXMax(const CRPlot& plot) {
double AdjustYMin(const CRPlot& plot) {
double y = NAN;
for (const auto& d : plot._egraphs) {
for (const auto& dp : d.second) {
for (const auto& dp : d.second.first) {
double scaled_y = std::pow(dp.x, plot.scale_index) *
(plot.log_y ? std::pow(dp.y - dp.ey1, 2) / dp.y
: (dp.y - 2 * dp.ey1));
......@@ -170,7 +182,7 @@ double AdjustYMin(const CRPlot& plot) {
double AdjustYMax(const CRPlot& plot) {
double y = NAN;
for (const auto& d : plot._egraphs) {
for (const auto& dp : d.second) {
for (const auto& dp : d.second.first) {
double scaled_y = std::pow(dp.x, plot.scale_index) *
(plot.log_y ? std::pow(dp.y + dp.ey2, 2) / dp.y
: (dp.y + 2 * dp.ey2));
......@@ -198,13 +210,14 @@ std::shared_ptr<TCanvas> CRPlot::Plot(void) const {
canv.SetBorderMode(0);
canv.SetLogx(log_x);
canv.SetLogy(log_y);
canv.SetGrid();
canv.SetGrid(grid_x, grid_y);
canv.SetTickx(1);
canv.SetTicky(1);
canv.SetFrameBorderMode(0);
// TH1D hframe("hframe", title.c_str(), 1, x_min_plot, x_max_plot);
TH1D* phframe = new TH1D("hframe", title.c_str(), 1, x_min_plot, x_max_plot);
TH1D* phframe =
new TH1D("hframe", title.c_str(), 1, x_min_plot, x_max_plot);
phframe->SetBit(kCanDelete); // prevent leak
auto& hframe = *phframe;
hframe.SetMinimum(y_min_plot);
......@@ -218,31 +231,82 @@ std::shared_ptr<TCanvas> CRPlot::Plot(void) const {
std::vector<TGraph*> graphs;
std::vector<TGraphAsymmErrors*> egraphs;
int iColor = 1;
for (const auto& d : _bands) {
const auto& band = d.second.first;
const auto style = d.second.second;
const size_t n = band.x.size();
auto g = new TGraphAsymmErrors(n);
g->SetBit(kCanDelete); // prevent leak
g->SetTitle(d.first.c_str());
for (size_t i = 0; i < n; ++i) {
double x = band.x[i];
double y = band.y[i];
double y_upper = band.y_upper[i];
double y_lower = band.y_lower[i];
if (y_upper < y_lower) { // swap
y_upper += y_lower;
y_lower = y_upper - y_lower;
y_upper -= y_lower;
}
double el = y - y_lower;
double eu = y_upper - y;
double fac = std::pow(x, scale_index);
g->SetPoint(i, x, y * fac);
g->SetPointError(i, 0, 0, el * fac, eu * fac);
}
if (style.fill_style / 1000 == 1) {
double alpha = (style.fill_style % 1000 % 101) * .01;
g->SetFillColorAlpha(
style.fill_color < 0 ? style.color : style.fill_color, alpha);
g->SetFillStyle(1001);
} else {
g->SetFillColor(style.fill_color < 0 ? style.color
: style.fill_color);
g->SetFillStyle(style.fill_style);
}
if (style.size > 0) {
// draw center line
g->SetLineWidth(style.size);
g->SetLineColor(style.color);
g->SetLineStyle(style.style);
g->Draw("same CE3");
} else {
g->SetLineWidth(0);
g->Draw("same E3");
}
egraphs.push_back(g);
}
// plot curves
for (const auto& d : _graphs) {
auto g = new TGraph(d.second.x.size());
const auto& curve = d.second.first;
const auto style = d.second.second;
auto g = new TGraph(curve.x.size());
g->SetBit(kCanDelete); // prevent leak
g->SetTitle(d.first.c_str());
for (size_t i = 0; i < d.second.x.size(); ++i) {
double x = d.second.x[i], y = d.second.y[i],
for (size_t i = 0; i < curve.x.size(); ++i) {
double x = curve.x[i], y = curve.y[i],
fac = std::pow(x, scale_index);
g->SetPoint(i, x, y * fac);
}
if (iColor == 3 || iColor == 5 || iColor == 7) {
++iColor;
}
g->SetLineColor(iColor++);
g->Draw("l same");
g->SetLineColor(style.color);
g->SetLineStyle(style.style);
g->SetLineWidth(style.size < 0 ? 2 : style.size);
g->Draw("same l");
graphs.push_back(g);
}
iColor = 2;
// plot data
for (const auto& d : _egraphs) {
auto g = new TGraphAsymmErrors(d.second.size());
const auto data = d.second.first;
const auto style = d.second.second;
auto g = new TGraphAsymmErrors(data.size());
g->SetBit(kCanDelete); // prevent leak
g->SetTitle(d.first.c_str());
for (size_t i = 0; i < d.second.size(); ++i) {
auto dp = d.second[i];
for (size_t i = 0; i < data.size(); ++i) {
auto dp = data[i];
double fac = std::pow(dp.x, scale_index);
// TODO: support upper-limit
......@@ -254,12 +318,11 @@ std::shared_ptr<TCanvas> CRPlot::Plot(void) const {
dp.ey1 * fac, dp.ey2 * fac);
}
g->SetFillColor(0);
if (iColor == 3 || iColor == 5 || iColor == 7) {
++iColor;
}
g->SetLineColor(iColor);
g->SetMarkerColor(iColor++);
g->SetMarkerStyle(2);
g->SetLineColor(style.color);
g->SetMarkerColor(style.color);
g->SetMarkerStyle(style.style);
g->SetLineWidth(style.size < 2 ? 1 : (style.size - 1));
g->SetMarkerSize(style.size < 0 ? 2 : style.size);
g->Draw("same ep");
egraphs.push_back(g);
}
......@@ -294,5 +357,5 @@ std::shared_ptr<TCanvas> CRPlot::Plot(void) const {
// for (auto p : graphs) {
// delete p;
// }
// XXX According to ROOT community,
// XXX According to ROOT community,
}
......@@ -18,4 +18,4 @@ target_link_libraries(test_plot crdb ${ROOT_LIBRARIES})
target_include_directories(test_plot PUBLIC ${CMAKE_SOURCE_DIR}/src)
add_test(
NAME test:plot
COMMAND test_plot "${Test_DATA}/BC_EKN.usine" "${Test_DATA}/fitted_BC.tsv")
COMMAND test_plot "${Test_DATA}/BC_EKN.usine" "${Test_DATA}/fitted_BC.tsv" "${Test_DATA}/fitted_BC_band.tsv")
1.000000e-03 2.238076e-01 1.721597e-01 2.909499e-01
1.300000e-03 2.238831e-01 1.722178e-01 2.910480e-01
1.690000e-03 2.239812e-01 1.722932e-01 2.911756e-01
2.197000e-03 2.241086e-01 1.723912e-01 2.913412e-01
2.856100e-03 2.242742e-01 1.725186e-01 2.915565e-01
3.712930e-03 2.244892e-01 1.726840e-01 2.918360e-01
4.826809e-03 2.247684e-01 1.728988e-01 2.921989e-01
6.274852e-03 2.251308e-01 1.731775e-01 2.926700e-01
8.157307e-03 2.256008e-01 1.735391e-01 2.932810e-01
1.060450e-02 2.262102e-01 1.740078e-01 2.940733e-01
1.378585e-02 2.271372e-01 1.747209e-01 2.952784e-01
1.792160e-02 2.283374e-01 1.756442e-01 2.968386e-01
2.329809e-02 2.298882e-01 1.768371e-01 2.988547e-01
3.028751e-02 2.318888e-01 1.783760e-01 3.014554e-01
3.937376e-02 2.344654e-01 1.803580e-01 3.048050e-01
5.118589e-02 2.366230e-01 1.820177e-01 3.076099e-01
6.654166e-02 2.393856e-01 1.841428e-01 3.112013e-01
8.650416e-02 2.429065e-01 1.868512e-01 3.157784e-01
1.124554e-01 2.475866e-01 1.904512e-01 3.218626e-01
1.461920e-01 2.535742e-01 1.950571e-01 3.296465e-01
1.900496e-01 2.584054e-01 1.987734e-01 3.359270e-01
2.470645e-01 2.648823e-01 2.037556e-01 3.443470e-01
3.211839e-01 2.708271e-01 2.083285e-01 3.520752e-01
4.175391e-01 2.775099e-01 2.134692e-01 3.607629e-01
5.428008e-01 2.830075e-01 2.176981e-01 3.679098e-01
7.056410e-01 2.885664e-01 2.219742e-01 3.751363e-01
9.173333e-01 2.930755e-01 2.254427e-01 3.809981e-01
1.192533e+00 2.943380e-01 2.264138e-01 3.826394e-01
1.550293e+00 2.938009e-01 2.260007e-01 3.819412e-01
2.015381e+00 2.905116e-01 2.234705e-01 3.776651e-01
2.619996e+00 2.839525e-01 2.184250e-01 3.691383e-01
3.405994e+00 2.737889e-01 2.106068e-01 3.559256e-01
4.427793e+00 2.600977e-01 2.000752e-01 3.381270e-01
5.756130e+00 2.434150e-01 1.872423e-01 3.164395e-01
7.482970e+00 2.244913e-01 1.726856e-01 2.918387e-01
9.727860e+00 2.049328e-01 1.576406e-01 2.664126e-01
1.264622e+01 1.873100e-01 1.440846e-01 2.435030e-01
1.644008e+01 1.729401e-01 1.330308e-01 2.248221e-01
2.137211e+01 1.588627e-01 1.222021e-01 2.065215e-01
2.778374e+01 1.453048e-01 1.117729e-01 1.888962e-01
3.611886e+01 1.327060e-01 1.020815e-01 1.725178e-01
4.695452e+01 1.213117e-01 9.331669e-02 1.577052e-01
6.104088e+01 1.111191e-01 8.547623e-02 1.444548e-01
7.935315e+01 1.020187e-01 7.847592e-02 1.326243e-01
1.031591e+02 9.386136e-02 7.220105e-02 1.220198e-01
1.341068e+02 8.650805e-02 6.654465e-02 1.124605e-01
1.743389e+02 7.982223e-02 6.140172e-02 1.037689e-01
2.266405e+02 7.370569e-02 5.669668e-02 9.581740e-02
2.946327e+02 6.808534e-02 5.237334e-02 8.851094e-02
3.830225e+02 6.290489e-02 4.838838e-02 8.177636e-02
4.979292e+02 5.812012e-02 4.470778e-02 7.555616e-02
6.473080e+02 5.371883e-02 4.132218e-02 6.983448e-02
8.415004e+02 4.964768e-02 3.819052e-02 6.454198e-02
1.093951e+03 4.588772e-02 3.529825e-02 5.965404e-02
1.422136e+03 4.242578e-02 3.263522e-02 5.515351e-02
1.848776e+03 3.921875e-02 3.016827e-02 5.098437e-02
2.403409e+03 3.624644e-02 2.788188e-02 4.712037e-02
3.124432e+03 3.349189e-02 2.576299e-02 4.353946e-02
4.061762e+03 3.093962e-02 2.379971e-02 4.022151e-02
5.280290e+03 2.857533e-02 2.198102e-02 3.714793e-02
6.864377e+03 2.638577e-02 2.029675e-02 3.430150e-02
8.923690e+03 2.435862e-02 1.873740e-02 3.166621e-02
1.160080e+04 2.248240e-02 1.729415e-02 2.922712e-02
1.508104e+04 2.074643e-02 1.595879e-02 2.697036e-02
1.960535e+04 1.914072e-02 1.472363e-02 2.488294e-02
2.548695e+04 1.765595e-02 1.358150e-02 2.295274e-02
3.313304e+04 1.628344e-02 1.252572e-02 2.116847e-02
4.307295e+04 1.501507e-02 1.155005e-02 1.951959e-02
5.599483e+04 1.384328e-02 1.064868e-02 1.799626e-02
7.279328e+04 1.276101e-02 9.816162e-03 1.658931e-02
9.463127e+04 1.176166e-02 9.047431e-03 1.529016e-02
1.230206e+05 1.083581e-02 8.335238e-03 1.408655e-02
......@@ -4,8 +4,8 @@
int main(int argc, char* argv[]) {
if (argc != 3) {
throw std::runtime_error(std::string(argv[0]) + " <data file> <curve file>");
if (argc != 4) {
throw std::runtime_error(std::string(argv[0]) + " <data file> <curve file> <band file>");
}
CRPlot plot("B/C Test Plot");
......@@ -13,9 +13,10 @@ int main(int argc, char* argv[]) {
CRDataset ds;
ds.parse_file(argv[1]);
auto data = ds.find_exact("B/C", "AMS02");
plot.AddData(*data);
plot.AddData(*data, {2, 20, 1});
plot.AddCurve(argv[2], "fitted B/C");
// plot.AddCurve(argv[2], {3, 2, 2}, "fitted B/C");
plot.AddBand(argv[3], {4, 2, 2, 1020}, "fitted B/C");
plot.log_y = false;
plot.x_label = "E_{kin}/nuc [GeV/n]";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment