Advanced Computing Platform for Theoretical Physics

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

apps/crdb won't plot

parent c14358a5
......@@ -31,125 +31,131 @@ int main(int argc, char* argv[]) {
yuc::argument arg;
arg.add_flag('g', "get", "get data from CRDB", false);
arg.add_option('d', "data", "use input data file", "");
arg.add_option('t', "etype", "energy type [EKN],EK,R,ETOT,ETOTN", "EKN");
arg.add_option('A', "accuracy", "conversion accuracy 0-2", "1");
arg.add_option('s', "subexp",
"name (and time intervals) of sub-experiments", "");
arg.add_option('o', "output", "output filename", "");
arg.add_flag('a', "append", "append to output file", false);
arg.add_usage("[crplot script...]");
arg.add_usage(
"-g [-t ETYPE] [-s SUBEXP] [-o FILE] <numerator> [denominator]");
arg.add_usage("[-t ETYPE] [-s SUBEXP] [-o FILE] <numerator> [denominator]");
_YUC_ARGUMENT_PARSE_ASSERTION(arg, argc, argv);
if (arg["get"] || arg["data"]) {
std::string energy_type = crdb_bin::toupper(arg["etype"].str());
std::string subexp = arg["subexp"];
std::string ofn = arg["output"];
if (ofn == "-") {
ofn = "/dev/stdout";
}
std::string energy_type = crdb_bin::toupper(arg["etype"].str());
std::string subexp = arg["subexp"];
std::string ofn = arg["output"];
if (ofn == "-") {
ofn = "/dev/stdout";
}
std::string num = "", den = "";
switch (arg.size()) {
case 3:
den = arg[2];
case 2:
num = arg[1];
case 1:
break;
default:
std::cerr << arg[0] << ": too many CR quantities provied" << std::endl;
arg.help();
return EXIT_FAILURE;
}
std::string num = "", den = "";
switch (arg.size()) {
case 3:
den = arg[2];
case 2:
num = arg[1];
case 1:
break;
default:
std::cerr << arg[0] << ": too many CR quantities provied"
std::string y_quan = ""; // for filter datafile
std::string acc_str = arg["accuracy"];
auto acc = CRRequest::exact;
switch (acc_str.size() == 1 ? acc_str[0] : ' ') {
case '0':
acc = CRRequest::none;
break;
case '1':
acc = CRRequest::exact;
break;
case '2':
acc = CRRequest::approx;
break;
default:
std::cerr << arg[0] << ": accuracy should be 0, 1 or 2" << std::endl;
return EXIT_FAILURE;
}
CRRequest req{
.num = num, .den = den, .energy_type = energy_type, .accuracy = acc};
if (arg["data"]) {
if (num.size()) {
y_quan = num;
if (den.size()) {
y_quan += "/" + den;
}
}
} else { // fetch online data or use cached data, must provide cr name
if (num.empty()) {
std::cerr << arg[0] << " must provicde numerator [denominator]"
<< std::endl;
arg.help();
return EXIT_FAILURE;
}
}
std::string y_quan = ""; // for filter datafile
CRRequest req{.num = num, .den = den, .energy_type = energy_type};
if (ofn.empty()) { // list experiments
CRDataset dataset;
if (arg["data"]) {
if (num.size()) {
y_quan = num;
if (den.size()) {
y_quan += "/" + den;
}
}
} else { //if (arg["get"]) {
if (num.empty()) {
std::cerr << arg[0] << " must provicde numerator [denominator]"
<< std::endl;
}
dataset.parse_file(arg["data"].str());
} else {
dataset = req.fetch(cache_dir);
}
if (ofn.empty()) { // list experiments
CRDataset dataset;
size_t len_name = 0, len_exp = 0;
std::vector<size_t> idxs;
for (size_t i = 0; i < dataset.size(); ++i) {
const auto& d = *dataset[i];
if (arg["data"]) {
dataset.parse_file(arg["data"].str());
} else {
dataset = req.fetch(cache_dir);
if (energy_type != crdb_bin::toupper(d.x_quantity) ||
y_quan.size() && y_quan != d.y_quantity) {
continue;
}
}
size_t len_name = 0, len_exp = 0;
std::vector<size_t> idxs;
for (size_t i = 0; i < dataset.size(); ++i) {
const auto& d = *dataset[i];
if (arg["data"]) {
if (energy_type != crdb_bin::toupper(d.x_quantity) ||
y_quan.size() && y_quan != d.y_quantity) {
continue;
}
if (subexp.empty() ||
d.experiment.substr(0, subexp.size()) == subexp) {
if (len_name < d.y_quantity.size()) {
len_name = d.y_quantity.size();
}
if (subexp.empty() ||
d.experiment.substr(0, subexp.size()) == subexp) {
if (len_name < d.y_quantity.size()) {
len_name = d.y_quantity.size();
}
if (len_exp < d.experiment.size()) {
len_exp = d.experiment.size();
}
idxs.push_back(i);
if (len_exp < d.experiment.size()) {
len_exp = d.experiment.size();
}
idxs.push_back(i);
}
for (auto i : idxs) {
const auto& d = *dataset[i];
std::printf(("%-" + std::to_string(len_name + 3) +
"s %s %.1e ~ %.1e %2ld %-" +
std::to_string(len_exp + 3) + "s %s\n")
.c_str(), //
d.y_quantity.c_str(), d.x_quantity.c_str(),
d.x().min(), d.x().max(), d.size(),
d.experiment.c_str(), d.cite.c_str());
}
}
for (auto i : idxs) {
const auto& d = *dataset[i];
std::printf(("%-" + std::to_string(len_name + 3) +
"s %s %.1e ~ %.1e %2ld %-" +
std::to_string(len_exp + 3) + "s %s\n")
.c_str(), //
d.y_quantity.c_str(), d.x_quantity.c_str(), d.x().min(),
d.x().max(), d.size(), d.experiment.c_str(),
d.cite.c_str());
}
} else {
// save expriments
std::stringstream ss;
if (arg["data"]) {
ss << std::ifstream(arg["data"].str()).rdbuf();
} else {
// save expriments
std::stringstream ss;
if (arg["data"]) {
ss << std::ifstream(arg["data"].str()).rdbuf();
} else {
req.fetch(ss, cache_dir);
}
std::string line;
bool appending = arg["append"];
std::ofstream ofs(ofn, appending ? (ofs.out | ofs.app) : ofs.out);
while (std::getline(ss, line)) {
std::stringstream ssl(line);
std::string field;
ssl >> field;
if (field.size() &&
((field[0] == '#') || subexp.empty() ||
((ssl >> field) &&
field.substr(0, subexp.size()) == subexp))) {
ofs << line << std::endl;
}
req.fetch(ss, cache_dir);
}
std::string line;
bool appending = arg["append"];
std::ofstream ofs(ofn, appending ? (ofs.out | ofs.app) : ofs.out);
while (std::getline(ss, line)) {
std::stringstream ssl(line);
std::string field;
ssl >> field;
if (field.size() && ((field[0] == '#') || subexp.empty() ||
((ssl >> field) &&
field.substr(0, subexp.size()) == subexp))) {
ofs << line << std::endl;
}
}
} else {
std::cerr << arg[0] << ": script plotting in development" << std::endl;
return EXIT_FAILURE;
}
return 0;
......
......@@ -116,6 +116,10 @@ class CRData {
double chi_squared(const std::valarray<double>& y_aligned) const;
bool is_from_experiment(const std::string& exp) const;
bool is_quantity(const std::string& quan) const;
public:
static void list(const std::vector<std::shared_ptr<CRData>>&,
std::ostream&);
};
class CRDataset : public std::vector<std::shared_ptr<CRData>> {
......
......@@ -56,3 +56,28 @@ double CRData::x_max(void) const {
}
}
}
void CRData::list(const std::vector<std::shared_ptr<CRData>>& lst,
std::ostream& os) {
size_t len_name(0), len_exp(0);
for (const auto& pd : lst) {
if (len_name < pd->y_quantity.size()) {
len_name = pd->y_quantity.size();
}
if (len_exp < pd->experiment.size()) {
len_exp = pd->experiment.size();
}
}
for (const auto& pd : lst) {
char buf[1024];
std::sprintf(buf, //
("%-" + std::to_string(len_name + 3) +
"s %-5s %.1e ~ %.1e %2ld %-" +
std::to_string(len_exp + 3) + "s %s")
.c_str(),
pd->y_quantity.c_str(), pd->x_quantity.c_str(),
pd->x().min(), pd->x().max(), pd->size(),
pd->experiment.c_str(), pd->cite.c_str());
os << std::string(buf) << std::endl;
}
}
......@@ -15,9 +15,12 @@
#include <TH1D.h>
#include <TLegend.h>
#include <TPad.h>
#include <TError.h>
#include "loginterp"
int gErrorIgnoreLevel = kWarning;
namespace _CRPLOT {
template <size_t N>
std::array<double, N> line_columns(const std::string& line,
......@@ -83,7 +86,8 @@ CRPlot::CRPlot(const std::string& title, int canv_w, int canv_h)
ratio_min(NAN), ratio_max(NAN), //
canvas_width(canv_w), //
canvas_height(canv_h), //
divide_ratio(0.3) {}
divide_ratio(0.3) {
}
using Style = CRPlot::Style;
void CRPlot::AddData(const CRData& data, const Style& style,
......
......@@ -104,7 +104,7 @@ std::ostream& CRRequest::fetch(std::ostream& os,
std::string(
"https://lpsc.in2p3.fr/crdb/_dialog_result.php?format=usine&num=") +
num_used + "&den=" + den_used + "&energy_type=" + energy_type +
"&combo_level=" + static_cast<char>(accuracy);
"&energy_convert_level=" + static_cast<char>(accuracy);
// allocate curl instance
CURL* curl = curl_easy_init();
......
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