Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ if(PROJECT_LINK_DIRECTORIES)
link_directories(${PROJECT_LINK_DIRECTORIES})
endif()

add_executable(example example.cpp)
add_dependencies(example AnalysisTreeQA)
target_link_libraries(example ${ROOT_LIBRARIES} AnalysisTreeQA AnalysisTreeBase AnalysisTreeInfra)
set(EXECUTABLES example
)

install (TARGETS example RUNTIME DESTINATION bin)
foreach(EXE ${EXECUTABLES})
add_executable(${EXE} ${EXE}.cpp)
add_dependencies(${EXE} AnalysisTreeQA)
target_link_libraries(${EXE} ${ROOT_LIBRARIES} AnalysisTreeQA AnalysisTreeBase AnalysisTreeInfra)
install (TARGETS ${EXE} RUNTIME DESTINATION bin)
install (FILES ${EXE}.cpp DESTINATION share)
endforeach()
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void example(const std::string& filelist){
//Integral plot 1D
SimpleCut vtx_chi2_track_cut = RangeCut({"VtxTracks.vtx_chi2"}, 0, 3);
SimpleCut nhits_cut = RangeCut({"VtxTracks.nhits"}, 4, 100);
SimpleCut chi2_cut({"VtxTracks.chi2", "VtxTracks.ndf"}, [](std::vector<double> par){ return par[0]/par[1] < 10; });
SimpleCut chi2_cut((std::vector<std::string>){"VtxTracks.chi2", "VtxTracks.ndf"}, [](std::vector<double> par){ return par[0]/par[1] < 10; });
SimpleCut eta_cut = RangeCut({"VtxTracks.eta"}, 0, 3);
Cuts* vertex_tracks_cuts = new Cuts("GoodCentralityTracks", {vtx_chi2_track_cut, nhits_cut, chi2_cut, eta_cut});
task->AddIntegral({"Multiplicity", Variable::FromString("VtxTracks.ones"), {1000, 0, 1000}}, vertex_tracks_cuts);
Expand Down
24 changes: 4 additions & 20 deletions src/EntryConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ using TH1FD = TH1F;
using TH2FD = TH2F;
#endif

namespace AnalysisTree {
namespace QA {
namespace AnalysisTree::QA {

struct fill2_struct : public Utils::Visitor<void> {
fill2_struct(double val1, double val2) : val1_(val1), val2_(val2) {}
Expand All @@ -32,13 +31,6 @@ struct fill3_struct : public Utils::Visitor<void> {
double val1_, val2_, val3_;
};

struct write_struct : public Utils::Visitor<void> {
explicit write_struct(std::string n) : name_(std::move(n)) {}
template<class PlotType>
void operator()(PlotType* p) const { p->Write(name_.c_str()); }
std::string name_;
};

EntryConfig::EntryConfig(const Axis& axis, Variable& weight, const std::string& name, Cuts* cuts, bool is_integral)
: name_(name == "" ? axis.GetName() : name),
type_(is_integral ? PlotType::kIntegral1D : PlotType::kHisto1D),
Expand Down Expand Up @@ -138,8 +130,8 @@ void EntryConfig::InitPlot() {
}

void EntryConfig::Set2DName(const std::string& name) {
name_ = name == "" ? Form("%s_%s", axes_[0].GetName(), axes_[1].GetName()) : name;
if (name == "") {
name_ = name.empty() ? Form("%s_%s", axes_[0].GetName(), axes_[1].GetName()) : name;
if (name.empty()) {
if (entry_cuts_ != nullptr)
name_ += "_" + entry_cuts_->GetName();

Expand All @@ -161,12 +153,6 @@ void EntryConfig::Fill(double value1, double value2, double value3) {
ANALYSISTREE_UTILS_VISIT(fill3_struct(value1, value2, value3), plot_);
}

void EntryConfig::Write() const {
assert(out_dir_);
out_dir_->cd();
ANALYSISTREE_UTILS_VISIT(write_struct(name_), plot_);
}

void EntryConfig::Fill(double value) { ANALYSISTREE_UTILS_GET<TH1*>(plot_)->Fill(value); }

std::string EntryConfig::GetDirectoryName() const {
Expand All @@ -186,9 +172,7 @@ std::string EntryConfig::GetDirectoryName() const {
if (!var4weight_.GetName().empty() && var4weight_.GetFields().at(0).GetName() != "ones") {
name += "_weight_" + var4weight_.GetName();
}
if (!toplevel_dir_name_.empty()) name.insert(0, toplevel_dir_name_ + "/");
return name;
}

}// namespace QA
}// namespace AnalysisTree
} // namespace AnalysisTree::QA
27 changes: 8 additions & 19 deletions src/EntryConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
#include <vector>

#include <TAxis.h>
#include <TH1.h>
#include <TH2.h>
#include <TProfile.h>

#include "AnalysisTree/Cuts.hpp"
#include "AnalysisTree/Utils.hpp"

class TH1;
class TH2;
class TProfile;

namespace AnalysisTree {
namespace QA {
namespace AnalysisTree::QA {

class Axis : public Variable, public TAxis {
public:
Expand Down Expand Up @@ -59,12 +57,8 @@ class EntryConfig {
void Fill(double value);
void Fill(double value1, double value2);
void Fill(double value1, double value2, double value3);
void Write() const;

ANALYSISTREE_ATTR_NODISCARD const std::vector<Axis>& GetAxes() const { return axes_; }
std::vector<Axis>& Axes() { return axes_; }

ANALYSISTREE_ATTR_NODISCARD unsigned int GetNdimentions() const { return axes_.size(); }
ANALYSISTREE_ATTR_NODISCARD unsigned int GetNdimensions() const { return axes_.size(); }
ANALYSISTREE_ATTR_NODISCARD Cuts* GetEntryCuts() const { return entry_cuts_; }
ANALYSISTREE_ATTR_NODISCARD PlotType GetType() const { return type_; }

Expand All @@ -85,12 +79,10 @@ class EntryConfig {

ANALYSISTREE_ATTR_NODISCARD std::string GetDirectoryName() const;

void SetOutDir(TDirectory* out_dir) { out_dir_ = out_dir; }

void SetTopLevelDirName(const std::string& name) { toplevel_dir_name_ = name; }

PlotPointer GetPlot() { return plot_; }

const std::string& GetName() const { return name_; }

protected:
void InitPlot();
ANALYSISTREE_ATTR_NODISCARD TH1* CreateHisto1D() const;
Expand All @@ -107,12 +99,9 @@ class EntryConfig {
Variable var4weight_{};
Cuts* entry_cuts_{nullptr};
std::vector<std::pair<int, int>> vars_id_{};
std::string toplevel_dir_name_{""};

TDirectory* out_dir_{nullptr};
ClassDef(EntryConfig, 1);
};

}// namespace QA
}// namespace AnalysisTree
}// namespace AnalysisTree::QA
#endif//ANALYSISTREE_QA_ENTRYCONFIG_H
141 changes: 99 additions & 42 deletions src/Task.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,106 @@
#include "Task.hpp"

namespace AnalysisTree {
namespace QA {
#include <utility>

namespace AnalysisTree::QA {

size_t Task::AddH1(const std::string& name, const Axis& x, Cuts* cuts, Variable weight) {
CreateOutputFileIfNotYet();
weight.IfEmptyVariableConvertToOnes(x);
entries_.emplace_back(x, weight, name, cuts, false);
TDirectory* dir = MkMultiLevelDir(out_file_, toplevel_dir_name_ + "/" + entries_.back().GetDirectoryName());
ANALYSISTREE_UTILS_VISIT(setdirectory_struct(dir), entries_.back().GetPlot());
ANALYSISTREE_UTILS_VISIT(setname_struct(entries_.back().GetName()), entries_.back().GetPlot());
auto var_id = AddEntry(AnalysisEntry(entries_.back().GetVariables(), entries_.back().GetEntryCuts(), entries_.back().GetVariableForWeight()));
entries_.back().SetVariablesId({{var_id.first, var_id.second.at(0)}});
return entries_.size() - 1;
}

void Task::FillIntegral(EntryConfig& plot) {
size_t Task::AddH1(const Axis& x, Cuts* cuts, Variable weight) {
return AddH1("", x, cuts, std::move(weight));
}

size_t Task::AddH2(const std::string& name, const Axis& x, const Axis& y, Cuts* cuts, Variable weight) {
CreateOutputFileIfNotYet();
weight.IfEmptyVariableConvertToOnes(x);
entries_.emplace_back(x, y, weight, name, cuts);
TDirectory* dir = MkMultiLevelDir(out_file_, toplevel_dir_name_ + "/" + entries_.back().GetDirectoryName());
ANALYSISTREE_UTILS_VISIT(setdirectory_struct(dir), entries_.back().GetPlot());
ANALYSISTREE_UTILS_VISIT(setname_struct(entries_.back().GetName()), entries_.back().GetPlot());
auto var_id = AddEntry(AnalysisEntry(entries_.back().GetVariables(), entries_.back().GetEntryCuts(), entries_.back().GetVariableForWeight()));
entries_.back().SetVariablesId({{var_id.first, var_id.second.at(0)}, {var_id.first, var_id.second.at(1)}});
return entries_.size() - 1;
}

size_t Task::AddH2(const Axis& x, const Axis& y, Cuts* cuts, Variable weight) {
return AddH2("", x, y, cuts, std::move(weight));
}

size_t Task::AddProfile(const std::string& name, const Axis& x, const Axis& y, Cuts* cuts, Variable weight) {
CreateOutputFileIfNotYet();
weight.IfEmptyVariableConvertToOnes(x);
entries_.emplace_back(x, y, weight, name, cuts, true);
TDirectory* dir = MkMultiLevelDir(out_file_, toplevel_dir_name_ + "/" + entries_.back().GetDirectoryName());
ANALYSISTREE_UTILS_VISIT(setdirectory_struct(dir), entries_.back().GetPlot());
ANALYSISTREE_UTILS_VISIT(setname_struct(entries_.back().GetName()), entries_.back().GetPlot());
auto var_id = AddEntry(AnalysisEntry(entries_.back().GetVariables(), entries_.back().GetEntryCuts(), entries_.back().GetVariableForWeight()));
entries_.back().SetVariablesId({{var_id.first, var_id.second.at(0)}, {var_id.first, var_id.second.at(1)}});
return entries_.size() - 1;
}

size_t Task::AddProfile(const Axis& x, const Axis& y, Cuts* cuts, Variable weight) {
return AddProfile("", x, y, cuts, std::move(weight));
}

size_t Task::AddIntegral(const std::string& name, const Axis& x, Cuts* cuts, Variable weight) {
CreateOutputFileIfNotYet();
weight.IfEmptyVariableConvertToOnes(x);
entries_.emplace_back(x, weight, name, cuts, true);
TDirectory* dir = MkMultiLevelDir(out_file_, toplevel_dir_name_ + "/" + entries_.back().GetDirectoryName());
ANALYSISTREE_UTILS_VISIT(setdirectory_struct(dir), entries_.back().GetPlot());
ANALYSISTREE_UTILS_VISIT(setname_struct(entries_.back().GetName()), entries_.back().GetPlot());
auto var_id = AddEntry(AnalysisEntry(entries_.back().GetVariables(), entries_.back().GetEntryCuts(), entries_.back().GetVariableForWeight()));
entries_.back().SetVariablesId({{var_id.first, var_id.second.at(0)}});
return entries_.size() - 1;
}

size_t Task::AddIntegral(const Axis& x, Cuts* cuts, Variable weight) {
return AddIntegral("", x, cuts, std::move(weight));
}

size_t Task::AddIntegral(const Axis& x, const Axis& y, Cuts* cuts_x, Cuts* cuts_y) {
CreateOutputFileIfNotYet();
entries_.emplace_back(x, cuts_x, y, cuts_y);
TDirectory* dir = MkMultiLevelDir(out_file_, toplevel_dir_name_ + "/" + entries_.back().GetDirectoryName());
ANALYSISTREE_UTILS_VISIT(setdirectory_struct(dir), entries_.back().GetPlot());
ANALYSISTREE_UTILS_VISIT(setname_struct(entries_.back().GetName()), entries_.back().GetPlot());
auto var_id_x = AddEntry(AnalysisEntry({entries_.back().GetVariables()[0]}, cuts_x));
auto var_id_y = AddEntry(AnalysisEntry({entries_.back().GetVariables()[1]}, cuts_y));
entries_.back().SetVariablesId({{var_id_x.first, var_id_x.second.at(0)}, {var_id_y.first, var_id_y.second.at(0)}});
return entries_.size() - 1;
}

void Task::FillIntegral(EntryConfig& plot) {
double integral_x{0.};
double integral_y{0.};
auto var_ids = plot.GetVariablesId();

for (const auto& var : this->GetValues(var_ids.at(0).first)) {
integral_x += var[var_ids.at(0).second];
}
if (plot.GetNdimentions() > 1) {
if (plot.GetNdimensions() > 1) {
for (const auto& var : this->GetValues(var_ids.at(1).first)) {
integral_y += var[var_ids.at(1).second];
}
}
if (plot.GetNdimentions() == 1) {
if (plot.GetNdimensions() == 1) {
plot.Fill(integral_x);
} else {
plot.Fill(integral_x, integral_y);
}
}

void Task::Exec() {

AnalysisTask::Exec();

for (auto& plot : entries_) {
Expand All @@ -41,7 +116,7 @@ void Task::Exec() {
++ivw;
auto weight = weights.at(ivw);
if (std::fabs(weight) < 1e-6) continue;
switch (plot.GetNdimentions()) {
switch (plot.GetNdimensions()) {
case 1: {
if (std::fabs(weight - 1) < 1e-4) plot.Fill(var[var_ids.at(0).second]);
else
Expand All @@ -56,37 +131,28 @@ void Task::Exec() {
}
}
}

}// plots
}

void Task::Finish() {
out_file_->cd();
for (auto& plot : entries_) {
plot.Write();
}
out_file_->Write();
out_file_->Close();
}

void Task::Init() {
TDirectory* Task::MkMultiLevelDir(TFile* file, const std::string& name) const {
auto splitBySlash = [] (const std::string& str) {
std::vector<std::string> result;
std::stringstream ss(str);
std::string item;

// Split the string by slashes
while (std::getline(ss, item, '/')) {
result.push_back(item);
}

AnalysisTask::Init();
std::set<std::string> dirs{};
return result;
};

for (auto& entry : entries_) {
dirs.insert(entry.GetDirectoryName());
}
out_file_ = new TFile(out_file_name_.c_str(), "recreate");
for (const auto& dir : dirs) {
out_file_->cd();
dir_map_.insert(std::make_pair(dir, MkMultiLevelDir(out_file_, dir)));
}
for (auto& entry : entries_) {
entry.SetOutDir(dir_map_.find(entry.GetDirectoryName())->second);
}
}

TDirectory* Task::MkMultiLevelDir(TFile* file, const std::string& name) const {
auto vDirs = splitBySlash(name);
TDirectory* result;
for (int iDir = 0; iDir < vDirs.size(); iDir++) {
Expand All @@ -97,18 +163,9 @@ TDirectory* Task::MkMultiLevelDir(TFile* file, const std::string& name) const {
return result;
}

std::vector<std::string> Task::splitBySlash(const std::string& str) {
std::vector<std::string> result;
std::stringstream ss(str);
std::string item;

// Split the string by slashes
while (std::getline(ss, item, '/')) {
result.push_back(item);
}

return result;
void Task::CreateOutputFileIfNotYet() {
if (out_file_ == nullptr) out_file_ = new TFile(out_file_name_.c_str(), "recreate");
}

}// namespace QA
}// namespace AnalysisTree
} // namespace AnalysisTree::QA

Loading