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 }
45
52 friend SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const std::string& title);
53
59 friend SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title);
60
67 friend SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title);
68
74 friend SimpleCut EqualsCut(const Variable& var, int value, const std::string& title);
75
82 template<class T>
83 bool Apply(const T& object) const {
84 std::vector<double> variables;
85 variables.reserve(vars_.size());
86 for (const auto& var : vars_) {
87 variables.emplace_back(var.GetValue(object));
88 }
89 return lambda_(variables);
90 }
91 bool Apply(const BranchChannel& object) const;
92 bool Apply(std::vector<const BranchChannel*>& bch, std::vector<size_t>& id) const;
93 [[deprecated]] bool Apply(const BranchChannel& a, size_t a_id, const BranchChannel& b, size_t b_id) const;
94
95 void Print() const;
96
97 std::vector<Variable>& Variables() { return vars_; }
98 ANALYSISTREE_ATTR_NODISCARD const std::vector<Variable>& GetVariables() const { return vars_; }
99 ANALYSISTREE_ATTR_NODISCARD const std::set<std::string>& GetBranches() const { return branch_names_; }
100
101 friend bool operator==(const SimpleCut& that, const SimpleCut& other);
102
103 protected:
104 SimpleCut(const Variable& var, double min, double max, std::string title = "");
105 SimpleCut(const Variable& var, int value, std::string title = "");
106
107 void FillBranchNames();
108
109 std::string title_;
110 std::vector<Variable> vars_{};
111 std::set<std::string> branch_names_{};
112 std::function<bool(std::vector<double>&)> lambda_;
113
114 ClassDef(SimpleCut, 1);
115};
116SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const std::string& title = "");
117SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title = "");
118SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title = "");
119SimpleCut EqualsCut(const Variable& var, int value, const std::string& title = "");
120
121}// namespace AnalysisTree
122#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:112
friend SimpleCut RangeCut(const std::string &variable_name, double lo, double hi, const std::string &title)
Definition SimpleCut.cpp:28
bool Apply(const T &object) const
Evaluates cut.
Definition SimpleCut.hpp:83
friend SimpleCut EqualsCut(const std::string &variable_name, int value, const std::string &title)
Definition SimpleCut.cpp:32
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:32
SimpleCut RangeCut(const std::string &variable_name, double lo, double hi, const std::string &title)
Definition SimpleCut.cpp:28