AnalysisTree
Loading...
Searching...
No Matches
Variable.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_SRC_VARIABLE_H_
5#define ANALYSISTREE_SRC_VARIABLE_H_
6
7#include <cassert>
8#include <functional>
9#include <set>
10#include <stdexcept>
11#include <string>
12#include <utility>
13
14#include "BranchChannel.hpp"
15#include "Field.hpp"
16
17namespace AnalysisTree {
18
23class Variable {
24 public:
25 Variable() = default;
26 Variable(const Variable&) = default;
27 Variable(Variable&&) = default;
28 Variable& operator=(Variable&&) = default;
29 Variable& operator=(const Variable&) = default;
30 virtual ~Variable() = default;
31
32 explicit Variable(const Field& field)
33 : name_(field.GetBranchName() + "." + field.GetName()),
34 fields_({field}),
35 n_branches_(1){};
36
43 Variable(std::string name, std::vector<Field> fields, std::function<double(std::vector<double>&)> lambda);
44
45 Variable(const std::string& branch, const std::string& field) : Variable(Field(branch, field)) {}
46
52 static Variable FromString(const std::string& full_name);
53
54 friend bool operator==(const Variable& that, const Variable& other);
55 friend bool operator>(const Variable& that, const Variable& other);
56 friend bool operator<(const Variable& that, const Variable& other);
57
58 void Init(const Configuration& conf);
59
60 ANALYSISTREE_ATTR_NODISCARD const std::string& GetName() const { return name_; }
61 ANALYSISTREE_ATTR_NODISCARD const std::vector<Field>& GetFields() const { return fields_; }
62 ANALYSISTREE_ATTR_NODISCARD short GetNumberOfBranches() const { return n_branches_; }
63 ANALYSISTREE_ATTR_NODISCARD std::set<std::string> GetBranches() const;
64 ANALYSISTREE_ATTR_NODISCARD std::string GetBranchName() const;
65
66 double GetValue(std::vector<const BranchChannel*>& bch, std::vector<size_t>& id) const;
67 [[deprecated]] double GetValue(const BranchChannel& a, size_t a_id, const BranchChannel& b, size_t b_id) const;
68
69 template<class T>
70 double GetValue(const T& object) const {
71 assert(is_init_ && n_branches_ == 1);
72 vars_.clear();
73 for (const auto& field : fields_) {
74 vars_.emplace_back(field.template GetValue(object));
75 }
76 return lambda_(vars_);
77 }
78
79 double GetValue(const BranchChannel& object) const;
80
81 void SetName(std::string name) { name_ = std::move(name); }
82
83 void Print() const;
84
85 void IfEmptyVariableConvertToOnes(const Variable& var);
86
87 protected:
88 std::string name_;
89 std::vector<Field> fields_{};
90 mutable std::vector<double> vars_{};
91 std::function<double(std::vector<double>&)> lambda_{[](std::vector<double>& var) { return var.at(0); }};
92 short n_branches_{0};
93 bool is_init_{false};
94 ClassDef(Variable, 0);
95};
96
97}// namespace AnalysisTree
98
99#endif
Definition BranchChannel.hpp:19
A class to store configuration of the whole AnalysisTree object.
Definition Configuration.hpp:58
Field is a pointer in a branch/field structure.
Definition Field.hpp:22
Variable is a wrapper object for Field. In contrary to Field Variable is not bound to the data....
Definition Variable.hpp:23
static Variable FromString(const std::string &full_name)
Definition Variable.cpp:53
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10