Skip to content

Commit 90c760d

Browse files
add yaml interface
1 parent e3feeef commit 90c760d

13 files changed

Lines changed: 341 additions & 46 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(AnalysisTreeQA_BUILD_TASKS OFF CACHE BOOL "Build user' AnalysisTreeQA tasks
1313
set(AnalysisTreeQA_BUILD_EXAMPLES ON CACHE BOOL "Build AnalysisTreeQA examples (examples/)")
1414
set(AnalysisTreeQA_BUILD_TESTS OFF CACHE BOOL "Build tests for AnalysisTreeQA")
1515
set(AnalysisTreeQA_BUNDLED_AT ON CACHE BOOL "Get and build AnalysisTree")
16-
set(AnalysisTreeQA_BUNDLED_AT_VERSION "v2.2.5" CACHE STRING "Bundled AnalysisTree version")
16+
set(AnalysisTreeQA_BUNDLED_AT_VERSION "v2.2.6" CACHE STRING "Bundled AnalysisTree version")
1717
set(AnalysisTreeQA_BUNDLED_CUTS ON CACHE BOOL "Get and build AnalysisTreeCuts")
1818
set(AnalysisTreeQA_BUNDLED_CUTS_VERSION "v0.0.1" CACHE STRING "Bundled AnalysisTreeCuts version")
1919

@@ -46,7 +46,9 @@ message(STATUS "Using CXX flags for ${CMAKE_BUILD_TYPE}: ${CMAKE_CXX_FLAGS_${CMA
4646

4747
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
4848
list(APPEND CMAKE_PREFIX_PATH ${ROOTSYS})
49+
4950
find_package(ROOT REQUIRED RIO)
51+
find_package(yaml-cpp REQUIRED)
5052

5153
message(STATUS "Using ROOT: ${ROOT_VERSION} <${ROOT_CONFIG}>")
5254
include_directories(${CMAKE_SOURCE_DIR} ${ROOT_INCLUDE_DIR} ${ROOT_INCLUDE_DIRS})
@@ -55,7 +57,7 @@ include(${ROOT_USE_FILE})
5557
set(EXTERNAL_DIR ${CMAKE_BINARY_DIR}/external)
5658
set(EXTERNAL_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/external)
5759

58-
include_directories(${CMAKE_SOURCE_DIR} ${AnalysisTree_INCLUDE_DIR} ${PROJECT_INCLUDE_DIRECTORIES})
60+
include_directories(${CMAKE_SOURCE_DIR} ${AnalysisTree_INCLUDE_DIR} ${YAML_CPP_INCLUDE_DIR} ${PROJECT_INCLUDE_DIRECTORIES})
5961

6062
add_subdirectory(src)
6163

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ To apply the flag use -D{Name}={value}, for example, if you want to compile usin
3131
cmake -DCMAKE_CXX_STANDARD=11 ../
3232

3333
| Name | Default value | Possible values |
34-
| ------------- | ------------- | ---------- |
35-
| CMAKE_BUILD_TYPE | RELEASE | RELEASE/DEBUG |
36-
| CMAKE_CXX_STANDARD | 17 | 11/14/17 |
37-
| AnalysisTreeQA_BUILD_TESTS | ON | ON/OFF |
38-
| AnalysisTreeQA_BUILD_TASKS | OFF | ON/OFF |
39-
| AnalysisTreeQA_BUNDLED_AT | ON | ON/OFF |
40-
| AnalysisTreeQA_BUNDLED_AT_VERSION | master | master/v2.0.1/... |
34+
| ------------- |---------------| ---------- |
35+
| CMAKE_BUILD_TYPE | RELEASE | RELEASE/DEBUG |
36+
| CMAKE_CXX_STANDARD | 17 | 11/14/17 |
37+
| AnalysisTreeQA_BUILD_TESTS | OFF | ON/OFF |
38+
| AnalysisTreeQA_BUILD_TASKS | OFF | ON/OFF |
39+
| AnalysisTreeQA_BUNDLED_AT | ON | ON/OFF |
40+
| AnalysisTreeQA_BUNDLED_AT_VERSION | master | master/v2.0.1/... |
4141

4242
## Usage
4343

cmake_modules/YamlCpp.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
if (TRUE)
2+
message("-- Building bundled yaml-cpp")
3+
include(FetchContent)
4+
5+
FetchContent_Declare(
6+
YamlCpp
7+
GIT_REPOSITORY "https://github.com/jbeder/yaml-cpp.git"
8+
GIT_TAG "yaml-cpp-0.7.0"
9+
GIT_SHALLOW ON
10+
)
11+
FetchContent_MakeAvailable(YamlCpp)
12+
else()
13+
list(APPEND CMAKE_PREFIX_PATH ${ANALYSISTREE_HOME})
14+
list(APPEND CMAKE_PREFIX_PATH $ENV{ANALYSISTREE_HOME})
15+
find_package(AnalysisTree REQUIRED)
16+
list(APPEND PROJECT_INCLUDE_DIRECTORIES ${YAML_CPP_INCLUDE_DIR})
17+
endif()

examples/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ add_executable(example example.cpp)
1717
add_dependencies(example AnalysisTreeQA)
1818
target_link_libraries(example ${ROOT_LIBRARIES} AnalysisTreeQA AnalysisTreeBase AnalysisTreeInfra)
1919

20+
add_executable(test_yaml test_yaml.cpp)
21+
add_dependencies(test_yaml AnalysisTreeQA)
22+
target_link_libraries(test_yaml ${ROOT_LIBRARIES} AnalysisTreeQA AnalysisTreeBase AnalysisTreeInfra ${YAML_CPP_LIBRARIES})
23+
24+
2025
install (TARGETS example RUNTIME DESTINATION bin)

examples/example.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
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"]
14+
bins: [100, -3, 3]
15+
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+
23+
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+
31+
Integral:
32+
-
33+
name: "px_integral"
34+
field: ["p_{x}, GeV/c", "SimParticles.px"]
35+
bins: [100, -3, 3]
36+
cuts: ""
37+
38+
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]

examples/test_yaml.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <string>
2+
3+
#include "AnalysisTree/TaskManager.hpp"
4+
#include "AnalysisTree/Variable.hpp"
5+
6+
#include "Task.hpp"
7+
#include "YamlReader.hpp"
8+
9+
using namespace AnalysisTree;
10+
11+
void example(const std::string& filelist){
12+
auto* man = TaskManager::GetInstance();
13+
14+
auto* task = new QA::Task;
15+
task->SetOutputFileName("cbm_qa.root");
16+
17+
Cuts* pT_cut = new Cuts("pT_cut", {RangeCut("RecTracks.pT", 1, 1.5)});
18+
Variable pxpy("pxpy", {{"RecTracks", "px"}, {"RecTracks", "py"}}, []( std::vector<double>& var ) { return var.at(0)*var.at(1); });
19+
20+
task->AddCut(pT_cut);
21+
task->AddVariable(pxpy);
22+
23+
QA::YamlReader r("/Users/viktor/Soft/AnalysisTreeQA/examples/example.yaml");
24+
r.AddPlotsFromYaml(task);
25+
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+
57+
man->AddTask(task);
58+
59+
man->Init({filelist}, {"tTree"});
60+
man->Run(-1);
61+
man->Finish();
62+
}
63+
64+
int main(int argc, char* argv[]){
65+
if (argc < 2) {
66+
std::cout << "Error! Please use " << std::endl;
67+
std::cout << " ./example filename" << std::endl;
68+
exit(EXIT_FAILURE);
69+
}
70+
71+
const std::string filename = argv[1];
72+
example(filename);
73+
74+
return 0;
75+
}

src/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
include(AnalysisTree)
2+
#include(YamlCpp)
23

34
set(SOURCES
45
EntryConfig.cpp
56
Task.cpp
6-
)
7+
YamlReader.cpp)
78

89
string(REPLACE ".cpp" ".hpp" HEADERS "${SOURCES}")
910
list(APPEND HEADERS "BasicQA.hpp")
@@ -18,7 +19,7 @@ else ()
1819
link_directories(${PROJECT_LINK_DIRECTORIES})
1920
endif ()
2021

21-
#add_dependencies(AnalysisTreeQA ${PROJECT_DEPENDENCIES})
22+
add_dependencies(AnalysisTreeQA AnalysisTreeInfra AnalysisTreeBase)
2223

2324
ROOT_GENERATE_DICTIONARY(G__AnalysisTreeQA
2425
${HEADERS}
@@ -29,10 +30,12 @@ target_link_libraries(AnalysisTreeQA
2930
PUBLIC
3031
AnalysisTreeBase
3132
AnalysisTreeInfra
33+
# ${YAML_CPP_LIBRARIES}
3234
)
3335
target_include_directories(AnalysisTreeQA
3436
PUBLIC
3537
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
38+
${YAML_CPP_INCLUDE_DIR}
3639
)
3740

3841
install(TARGETS AnalysisTreeQA EXPORT AnalysisTreeQATargets

src/EntryConfig.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,34 @@ struct write_struct : public Utils::Visitor<void> {
2323
std::string name_;
2424
};
2525

26-
EntryConfig::EntryConfig(const Axis& axis, Cuts* cuts, bool is_integral)
27-
: name_(axis.GetName()),
26+
EntryConfig::EntryConfig(const Axis& axis, Cuts* cuts, bool is_integral, const std::string& name)
27+
: name_(name.empty() ? axis.GetName() : name),
2828
type_(is_integral ? PlotType::kIntegral1D : PlotType::kHisto1D),
2929
axes_({axis}),
3030
entry_cuts_(cuts) {
31-
if (cuts)
31+
if (cuts && name.empty())
3232
name_ += "_" + cuts->GetName();
33-
if(is_integral){
33+
if(is_integral && name.empty()){
3434
name_ += "_integral";
3535
}
3636
InitPlot();
3737
}
3838

39-
EntryConfig::EntryConfig(const Axis& x, const Axis& y, Cuts* cuts, bool is_profile) : type_(is_profile ? PlotType::kProfile : PlotType::kHisto2D),
40-
axes_({x, y}),
41-
entry_cuts_(cuts) {
42-
Set2DName();
39+
EntryConfig::EntryConfig(const Axis& x, const Axis& y, Cuts* cuts, bool is_profile, const std::string& name)
40+
: type_(is_profile ? PlotType::kProfile : PlotType::kHisto2D),
41+
axes_({x, y}),
42+
entry_cuts_(cuts)
43+
{
44+
name_ = name.empty() ? Construct2DName() : name;
4345
InitPlot();
4446
}
4547

46-
EntryConfig::EntryConfig(const Axis& x, Cuts* cuts_x, const Axis& y, Cuts* cuts_y) : type_(PlotType::kIntegral2D),
47-
axes_({x, y}),
48-
entry_cuts_(cuts_x) {
49-
Set2DName();
48+
EntryConfig::EntryConfig(const Axis& x, Cuts* cuts_x, const Axis& y, Cuts* cuts_y, const std::string& name)
49+
: type_(PlotType::kIntegral2D),
50+
axes_({x, y}),
51+
entry_cuts_(cuts_x)
52+
{
53+
name_ = name.empty() ? Construct2DName() : name;
5054
InitPlot();
5155
}
5256

@@ -116,14 +120,15 @@ void EntryConfig::InitPlot() {
116120
}
117121
}
118122

119-
void EntryConfig::Set2DName() {
120-
name_ = Form("%s_%s", axes_[0].GetName(), axes_[1].GetName());
123+
std::string EntryConfig::Construct2DName() {
124+
std::string name = Form("%s_%s", axes_[0].GetName(), axes_[1].GetName());
121125
if (entry_cuts_ != nullptr)
122-
name_ += "_" + entry_cuts_->GetName();
126+
name += "_" + entry_cuts_->GetName();
123127

124128
if (type_ == PlotType::kProfile) {
125-
name_ += "_profile";
129+
name += "_profile";
126130
}
131+
return name;
127132
}
128133

129134
void EntryConfig::Fill(double value1, double value2) {

src/EntryConfig.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class Axis : public Variable, public TAxis {
2222
Axis(const std::string& title, const Variable& var, const TAxis& a) : Variable(var), TAxis(a) {
2323
this->SetTitle(title.c_str());
2424
if(this->GetFields().size() == 1 && this->GetFields().at(0).GetName() == "ones"){
25-
// fields_[0] = Field(fields_[0].GetBranchName(), "ones");
26-
// fields_.clear();
2725
this->lambda_ = [](const std::vector<double>& ){ return 1; };
2826
this->name_ = "Ones";
2927
}
@@ -48,9 +46,9 @@ class EntryConfig {
4846
};
4947

5048
EntryConfig() = default;
51-
explicit EntryConfig(const Axis& axis, Cuts* cuts = nullptr, bool is_integral = false);
52-
EntryConfig(const Axis& x, const Axis& y, Cuts* cuts = nullptr, bool is_profile = false);
53-
EntryConfig(const Axis& x, Cuts* cuts_x, const Axis& y, Cuts* cuts_y);
49+
explicit EntryConfig(const Axis& axis, Cuts* cuts = nullptr, bool is_integral = false, const std::string& name="");
50+
EntryConfig(const Axis& x, const Axis& y, Cuts* cuts = nullptr, bool is_profile = false, const std::string& name="");
51+
EntryConfig(const Axis& x, Cuts* cuts_x, const Axis& y, Cuts* cuts_y, const std::string& name="");
5452

5553
EntryConfig(const EntryConfig&) = default;
5654
EntryConfig(EntryConfig&&) = default;
@@ -91,7 +89,7 @@ class EntryConfig {
9189
ANALYSISTREE_ATTR_NODISCARD TH1* CreateHisto1D() const;
9290
ANALYSISTREE_ATTR_NODISCARD TH2* CreateHisto2D() const;
9391
ANALYSISTREE_ATTR_NODISCARD TProfile* CreateProfile() const;
94-
void Set2DName();
92+
std::string Construct2DName();
9593

9694
PlotPointer plot_;
9795
std::string name_;///< Name of the plot

0 commit comments

Comments
 (0)