AnalysisTree
Loading...
Searching...
No Matches
SimpleCut.hpp
1/* Copyright (C) 2019-2021 GSI, Universität Tübingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Viktor Klochkov, Ilya Selyuzhenkov */
4#ifndef ANALYSISTREE_SIMPLECUT_H
5#define ANALYSISTREE_SIMPLECUT_H
6
7#include <algorithm>
8#include <functional>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "Constants.hpp"
14#include "Utils.hpp"
15#include "Variable.hpp"
16
17namespace AnalysisTree {
18
22class SimpleCut {
23
24 public:
25 SimpleCut() = default;
26 SimpleCut(const SimpleCut& cut) = default;
27 SimpleCut(SimpleCut&& cut) = default;
28 SimpleCut& operator=(SimpleCut&&) = default;
29 SimpleCut& operator=(const SimpleCut& cut) = default;
30 virtual ~SimpleCut() = default;
31
37 SimpleCut(std::vector<std::string> vars, std::function<bool(std::vector<double>&)> lambda, std::string title = "") : title_(std::move(title)),
38 lambda_(std::move(lambda)) {
39 std::transform(vars.begin(), vars.end(),
40 std::back_inserter(vars_),
41 [](const std::string& arg_name) { return Variable::FromString(arg_name); });
42
43 FillBranchNames();
44 hash_ = 1; // Impossible to calculate for lambda_
45 }
46
47 SimpleCut(const std::vector<Variable>& vars, std::function<bool(std::vector<double>&)> lambda, std::string title = "") : title_(std::move(title)),
48 lambda_(std::move(lambda)) {
49 for (auto& var : vars) {
50 vars_.emplace_back(var);
51 }
52 FillBranchNames();
53 hash_ = 1; // Impossible to calculate for lambda_
54 }
55
62 friend SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const std::string& title);
63
69 friend SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title);
70
77 friend SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title);
78
84 friend SimpleCut EqualsCut(const Variable& var, int value, const std::string& title);
85
92 template<class T>
93 bool Apply(const T& object) const {
94 std::vector<double> variables;
95 variables.reserve(vars_.size());
96 for (const auto& var : vars_) {
97 variables.emplace_back(var.GetValue(object));
98 }
99 return lambda_(variables);
100 }
101 bool Apply(const BranchChannel& object) const;
102 bool Apply(std::vector<const BranchChannel*>& bch, std::vector<size_t>& id) const;
103 [[deprecated]] bool Apply(const BranchChannel& a, size_t a_id, const BranchChannel& b, size_t b_id) const;
104
105 void Print() const;
106
107 void SetTitle(const std::string& title) { title_ = title; }
108 const std::string& GetTitle() const { return title_; }
109
110 std::vector<Variable>& Variables() { return vars_; }
111 ANALYSISTREE_ATTR_NODISCARD const std::vector<Variable>& GetVariables() const { return vars_; }
112 ANALYSISTREE_ATTR_NODISCARD const std::set<std::string>& GetBranches() const { return branch_names_; }
113
114 friend bool operator==(const SimpleCut& that, const SimpleCut& other);
115
116 protected:
117 SimpleCut(const Variable& var, double min, double max, std::string title = "");
118 SimpleCut(const Variable& var, int value, std::string title = "");
119
120 void FillBranchNames();
121
122 std::string title_;
123 std::vector<Variable> vars_{};
124 std::set<std::string> branch_names_{};
125 std::function<bool(std::vector<double>&)> lambda_;
126 size_t hash_;
127
128 ClassDef(SimpleCut, 1);
129};
130SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const std::string& title = "");
131SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title = "");
132SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title = "");
133SimpleCut EqualsCut(const Variable& var, int value, const std::string& title = "");
134
135}// namespace AnalysisTree
136#endif//ANALYSISTREE_SIMPLECUT_H
Definition BranchChannel.hpp:19
SimpleCut keeps predicate (lambda function with vector of arguments) and list of Variables.
Definition SimpleCut.hpp:22
SimpleCut(std::vector< std::string > vars, std::function< bool(std::vector< double > &)> lambda, std::string title="")
Definition SimpleCut.hpp:37
std::function< bool(std::vector< double > &)> lambda_
function used to evaluate the cut.
Definition SimpleCut.hpp:125
friend SimpleCut RangeCut(const std::string &variable_name, double lo, double hi, const std::string &title)
Definition SimpleCut.cpp:32
bool Apply(const T &object) const
Evaluates cut.
Definition SimpleCut.hpp:93
friend SimpleCut EqualsCut(const std::string &variable_name, int value, const std::string &title)
Definition SimpleCut.cpp:36
Variable is a wrapper object for Field. In contrary to Field Variable is not bound to the data....
Definition Variable.hpp:23
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10
SimpleCut EqualsCut(const std::string &variable_name, int value, const std::string &title)
Definition SimpleCut.cpp:36
SimpleCut RangeCut(const std::string &variable_name, double lo, double hi, const std::string &title)
Definition SimpleCut.cpp:32