Skip to content

Commit 457609a

Browse files
improve interface
1 parent 90c760d commit 457609a

4 files changed

Lines changed: 65 additions & 103 deletions

File tree

examples/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void example(const std::string& filelist){
1717
task->AddH1({"p_{T}, GeV/c", Variable::FromString("VtxTracks.pT"), {100, 0, 3}});
1818

1919
// 1D histo with cut
20-
Cuts* pT_cut = new Cuts("pT_cut", {RangeCut("VtxTracks.pT", 1, 1.5)});
20+
Cuts* pT_cut = new Cuts("pT_cut", {RangeCut("VtxTracks.pT", 0, 1.5)});
2121
task->AddH1({"p_{T}, GeV/c", Variable::FromString("VtxTracks.pT"), {100, 0, 3}}, pT_cut);
2222

2323
// AnalysisTree::Variable in case of more complicated plot

examples/example.yaml

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
1D:
2-
-
3-
name: "px"
4-
field: ["p_{x}, GeV/c", "SimParticles.px"]
5-
bins: [100, -3., 3.]
6-
cuts: "pT_cut"
7-
-
8-
name: "py"
9-
field: ["p_{y}, GeV/c", "RecTracks.py"]
10-
bins: [100, -3, 3]
11-
-
12-
name: "pxpy"
13-
field: ["p_{x} p_{y}, (GeV/c)ˆ2", "pxpy"]
2+
- name: "px"
3+
x_axis: &sim_px
4+
title: "p_{x}, GeV/c"
5+
field: "SimParticles.px"
6+
bins: [100, -3., 3.]
7+
cuts: "rec_pT_cut"
8+
- name: "py"
9+
x_axis:
10+
field: "RecTracks.py"
11+
bins_edges: [0, 0.2, 0.5, 1, 2]
12+
- name: "pxpy"
13+
x_axis:
14+
field: "pxpy"
1415
bins: [100, -3, 3]
1516
2D:
16-
-
17-
name: "px_py"
18-
field_x: ["p_{x}, GeV/c", "SimParticles.px"]
19-
bins_x: [100, -2, 2]
20-
field_y: ["p_{y}, GeV/c", "RecTracks.py"]
21-
bins_y: [100, -2, 2]
22-
17+
- name: "px_py"
18+
x_axis: *sim_px
19+
y_axis: &sim_py
20+
title: "p_{y}, GeV/c"
21+
field: "SimParticles.py"
22+
bins: [100, -2, 2]
2323
Profile:
24-
-
25-
name: "px_py_profile"
26-
field_x: ["p_{x}, GeV/c", "SimParticles.px"]
27-
bins_x: [100, -2, 2]
28-
field_y: ["p_{y}, GeV/c", "RecTracks.py"]
29-
cuts: ""
30-
24+
- name: "px_py_profile"
25+
x_axis: *sim_px
26+
y_axis: *sim_py
3127
Integral:
32-
-
33-
name: "px_integral"
34-
field: ["p_{x}, GeV/c", "SimParticles.px"]
35-
bins: [100, -3, 3]
36-
cuts: ""
37-
28+
- name: "pT_integral"
29+
x_axis: &sim_pT
30+
title: "#Sum{p_{T}, GeV/c}"
31+
field: "SimParticles.pT"
32+
bins: [100, 0, 100]
33+
cuts: "rec_pT_cut"
3834
Integral2D:
39-
-
40-
name: "px_py_integral"
41-
field_x: ["p_{x}, GeV/c", "SimParticles.px"]
42-
bins_x: [100, -2, 2]
43-
field_y: ["p_{y}, GeV/c", "RecTracks.py"]
44-
bins_y: [100, -2, 2]
35+
- name: "pT_M_integral"
36+
x_axis:
37+
title: "M"
38+
field: "SimParticles.ones"
39+
bins: [100, 0, 2000]
40+
y_axis: *sim_pT
41+
y_cuts: "rec_pT_cut"
42+

examples/test_yaml.cpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void example(const std::string& filelist){
1414
auto* task = new QA::Task;
1515
task->SetOutputFileName("cbm_qa.root");
1616

17-
Cuts* pT_cut = new Cuts("pT_cut", {RangeCut("RecTracks.pT", 1, 1.5)});
17+
Cuts* pT_cut = new Cuts("rec_pT_cut", {RangeCut("RecTracks.pT", 1, 1.5)});
1818
Variable pxpy("pxpy", {{"RecTracks", "px"}, {"RecTracks", "py"}}, []( std::vector<double>& var ) { return var.at(0)*var.at(1); });
1919

2020
task->AddCut(pT_cut);
@@ -23,37 +23,6 @@ void example(const std::string& filelist){
2323
QA::YamlReader r("/Users/viktor/Soft/AnalysisTreeQA/examples/example.yaml");
2424
r.AddPlotsFromYaml(task);
2525

26-
// // 1D histo
27-
// task->AddH1({"p_{T}, GeV/c", Variable::FromString("VtxTracks.pT"), {100, 0, 3}});
28-
//
29-
// // 1D histo with cut
30-
// Cuts* pT_cut = new Cuts("pT_cut", {RangeCut("VtxTracks.pT", 1, 1.5)});
31-
// task->AddH1({"p_{T}, GeV/c", Variable::FromString("VtxTracks.pT"), {100, 0, 3}}, pT_cut);
32-
//
33-
// // AnalysisTree::Variable in case of more complicated plot
34-
// Variable chi2_over_ndf("#chi^{2}/NDF", {{"VtxTracks", "chi2"}, {"VtxTracks", "ndf"}}, []( std::vector<double>& var ) { return var.at(0)/var.at(1); });
35-
// task->AddH1({"#chi^{2}/NDF", chi2_over_ndf, {100, 0, 10}});
36-
//
37-
// // 2D histo
38-
// task->AddH2({"#eta", Variable::FromString("VtxTracks.eta"), {100, -1, 4}}, {"p_{T}, GeV/c", Variable::FromString("VtxTracks.pT"), {100, 0, 3}});
39-
//
40-
// Variable qp_sts("qp_reco", {{"VtxTracks", "q"}, {"VtxTracks", "p"}}, [](std::vector<double>& qp) { return qp.at(0) * qp.at(1); });
41-
// task->AddH2({"sign(q)*p, GeV/c", qp_sts, {500, -10, 10}},{"m^{2}, GeV^{2}/c^{2}", {"TofHits", "mass2"}, {500, -1, 2}});
42-
//
43-
// // Histo with additional cuts:
44-
// Cuts* mc_protons = new Cuts("McProtons", {EqualsCut("SimParticles.pid", 2212)});
45-
// Cuts* mc_pions = new Cuts("McPions", {EqualsCut("SimParticles.pid", 211)});
46-
// task->AddH1({"MC-protons #chi^{2}/NDF", chi2_over_ndf, {100, 0, 10}}, mc_protons);
47-
// task->AddH1({"MC-pions #chi^{2}/NDF", chi2_over_ndf, {100, 0, 10}}, mc_pions);
48-
//
49-
// // TProfiles
50-
// const Field psi_RP = Field("SimEventHeader", "psi_RP");
51-
// const Field mc_phi = Field("SimParticles", "phi");
52-
// Variable v1("v1", {mc_phi, psi_RP}, [](std::vector<double> phi) { return cos(phi[0] - phi[1]); });
53-
// Variable v2("v2", {mc_phi, psi_RP}, [](std::vector<double> phi) { return cos(2 * (phi[0] - phi[1])); });
54-
// task->AddProfile({"#it{y}", Variable::FromString("SimParticles.rapidity"), {20, 0.5, 2.5}}, {"MC-protons v_{1}", v1, {}}, mc_protons);
55-
// task->AddProfile({"#it{y}", Variable::FromString("SimParticles.rapidity"), {20, 0.5, 2.5}}, {"MC-protons v_{1}", v1, {}}, mc_pions);
56-
5726
man->AddTask(task);
5827

5928
man->Init({filelist}, {"tTree"});

src/YamlReader.hpp

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ class YamlReader {
1515
public:
1616
explicit YamlReader(std::string filename) : filename_(std::move(filename)) {}
1717

18+
static Axis ConstructAxis(const YAML::Node& yaml_axis, Task* task){
19+
auto field = yaml_axis["field"].as<std::string>();
20+
auto title = yaml_axis["title"] ? yaml_axis["title"].as<std::string>() : field;
21+
if(yaml_axis["bins"]){
22+
auto bins = yaml_axis["bins"].as<std::vector<double>>();
23+
return {title, task->GetVariable(field),
24+
{static_cast<Int_t>(bins.at(0)), bins.at(1), bins.at(2)}};
25+
}
26+
else if(yaml_axis["bins_edges"]){
27+
auto bins = yaml_axis["bins_edges"].as<std::vector<double>>();
28+
return {title, task->GetVariable(field),
29+
TAxis(bins.size()-1, &(bins[0]))};
30+
}
31+
throw std::runtime_error("No axis bins are specified: " + title);
32+
}
33+
1834
void AddPlotsFromYaml(Task* task){
1935
assert(!filename_.empty());
2036
YAML::Node config = YAML::LoadFile(filename_);
@@ -29,49 +45,28 @@ class YamlReader {
2945
}
3046

3147
static void Add1DPlot(const YAML::Node& node, Task* task, bool is_integral = false) {
32-
assert(task);
33-
if(!node) {
34-
return;
35-
}
48+
assert(task && node);
49+
3650
for(auto plot : node){
3751
auto name = plot["name"] ? plot["name"].as<std::string>() : "";
38-
auto axis = plot["field"].as<std::vector<std::string>>();
39-
auto bins = plot["bins"].as<std::vector<double>>();
4052
auto cuts = plot["cuts"] ? plot["cuts"].as<std::string>() : "";
41-
42-
Axis a(axis.at(0), task->GetVariable(axis.at(1)),
43-
{static_cast<Int_t>(bins.at(0)), bins.at(1), bins.at(1)});
44-
53+
auto a = ConstructAxis(plot["x_axis"], task);
4554
Cuts* c = !cuts.empty() ? task->GetCut(cuts) : nullptr;
46-
47-
if(!is_integral){
48-
task->AddH1(a, c, name);
49-
}
50-
else{
51-
task->AddIntegral(a, c, name);
52-
}
55+
is_integral ? task->AddIntegral(a, c, name) : task->AddH1(a, c, name);
5356
}
5457
}
5558

5659
static void Add2DPlot(const YAML::Node& node, Task* task, EntryConfig::PlotType type) {
57-
assert(task);
58-
if(!node) {
59-
return;
60-
}
60+
assert(task && node);
61+
6162
for(auto plot : node) {
6263
auto name = plot["name"] ? plot["name"].as<std::string>() : "";
63-
auto axis_x = plot["field_x"].as<std::vector<std::string>>();
64-
auto bins_x = plot["bins_x"].as<std::vector<double>>();
65-
auto axis_y = plot["field_y"].as<std::vector<std::string>>();
66-
auto bins_y = plot["bins_y"] ? plot["bins_y"].as<std::vector<double>>() : std::vector<double>({0,0,0});
67-
auto cuts = plot["cuts"] ? plot["cuts"].as<std::string>() : "";
68-
auto cuts_x = plot["cuts_x"] ? plot["cuts_x"].as<std::string>() : "";
69-
auto cuts_y = plot["cuts_y"] ? plot["cuts_y"].as<std::string>() : "";
64+
auto x = ConstructAxis(plot["x_axis"], task);
65+
auto y = ConstructAxis(plot["y_axis"], task);
7066

71-
Axis x(axis_x.at(0), task->GetVariable(axis_x.at(1)),
72-
{static_cast<Int_t>(bins_x.at(0)), bins_x.at(1), bins_x.at(1)});
73-
Axis y(axis_y.at(0), task->GetVariable(axis_y.at(1)),
74-
{static_cast<Int_t>(bins_y.at(0)), bins_y.at(1), bins_y.at(1)});
67+
auto cuts = plot["cuts"] ? plot["cuts"].as<std::string>() : "";
68+
auto cuts_x = plot["x_cuts"] ? plot["x_cuts"].as<std::string>() : "";
69+
auto cuts_y = plot["y_cuts"] ? plot["y_cuts"].as<std::string>() : "";
7570

7671
Cuts* c = !cuts.empty() ? task->GetCut(cuts) : nullptr;
7772
Cuts* c_x = !cuts_x.empty() ? task->GetCut(cuts_x) : nullptr;

0 commit comments

Comments
 (0)