AnalysisTree
Loading...
Searching...
No Matches
Branch.hpp
1/* Copyright (C) 2019-2021 GSI, MEPhI, Universität Tübingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Eugeny Kashirin, Viktor Klochkov, Ilya Selyuzhenkov */
4#ifndef ANALYSISTREE_INFRA_BRANCH_HPP_
5#define ANALYSISTREE_INFRA_BRANCH_HPP_
6
7#include <cassert>
8#include <map>
9#include <utility>
10#include <vector>
11
12#include "BranchChannel.hpp"
13#include "BranchConfig.hpp"
14#include "Configuration.hpp"
15#include "EventHeader.hpp"
16#include "Field.hpp"
17#include "VariantMagic.hpp"
18
19class TTree;
20
21namespace AnalysisTree {
22
23class Branch {
24 public:
25 /* c-tors */
26 Branch() = default;
27 Branch(const Branch&) = default;
28 Branch(Branch&&) = default;
29 Branch& operator=(Branch&&) = default;
30 Branch& operator=(const Branch&) = default;
31
32 explicit Branch(BranchConfig config) : config_(std::move(config)) {
33 InitDataPtr();
34 UpdateConfigHash();
35 }
36
37 template<class T>
38 Branch(BranchConfig config, T* data) : config_(std::move(config)), data_(data) {
39 UpdateConfigHash();
40 }
41
42 Branch(BranchConfig config, BranchPointer ptr) : config_(std::move(config)), data_(std::move(ptr)) {
43 UpdateConfigHash();
44 }
45
46 ~Branch();
47
49 std::vector<std::pair<Field /* src */, Field /* dst */>> field_pairs;
50 };
51
52 /* Accessors to branch' main parameters, used very often */
53 [[nodiscard]] std::string GetBranchName() const { return config_.GetName(); }
54 [[nodiscard]] DetType GetBranchType() const { return config_.GetType(); }
55 [[nodiscard]] const BranchConfig& GetConfig() const { return config_; }
56
57 void InitDataPtr();
58
59 BranchChannel NewChannel();
60 BranchChannel NullChannel();
61 void ClearChannels();
62 Field NewVariable(const std::string& field_name, const std::string& title, AnalysisTree::Types type);
63 void CloneVariables(const AnalysisTree::BranchConfig& other);
64
65 // /* iterating */
66 [[nodiscard]] size_t size() const;
67
68 BranchChannel operator[](size_t i_channel) const {
69 return BranchChannel(this, i_channel);
70 }
71
72 std::vector<std::string> GetFieldNames() const;
73
74 [[nodiscard]] size_t GetId() const {
75 return ANALYSISTREE_UTILS_VISIT(get_id_struct(), data_);
76 }
77
78 [[nodiscard]] BranchPointer GetData() const { return data_; }
79 template<class T>
80 [[nodiscard]] T& GetDataRaw() { return ANALYSISTREE_UTILS_GET<T>(data_); }
81
82 [[nodiscard]] Field GetField(std::string field_name) const {
83 Field field(config_.GetName(), std::move(field_name));
84 field.Init(config_);
85 return field;
86 }
87
88 private:
90 BranchPointer data_;
91 bool is_mutable_{false};
92 bool is_frozen_{false};
93 std::size_t config_hash_{0};
94
95 public:
96 AnalysisTree::Configuration* parent_config{nullptr};
97 mutable std::map<const Branch* /* other branch */, FieldsMapping> copy_fields_mapping;
98
99 /* Modification */
100 void Freeze(bool freeze = true) { is_frozen_ = freeze; };
101 void SetMutable(bool is_mutable = true) { Branch::is_mutable_ = is_mutable; }
102
103 /* Checks are used very often */
104 inline void CheckFrozen(bool expected = true) const {
105 if (is_frozen_ != expected)
106 throw std::runtime_error("Branch is frozen");
107 }
108 inline void CheckMutable(bool expected = true) const {
109 if (is_mutable_ != expected)
110 throw std::runtime_error("Branch is not mutable");
111 }
119#ifdef cpp17
120 template<typename... Args>
121 auto GetVars(Args... field_name) {
122 return GetVarsImpl(std::array<std::string, sizeof...(Args)>{{std::string(field_name)...}},
123 std::make_index_sequence<sizeof...(Args)>());
124 }
129 void UseFields(std::vector<std::pair<std::string, std::reference_wrapper<Field>>>&& vars, bool ignore_missing = false);
130 // [[nodiscard]] std::vector<std::string> GetFieldNames() const;
131
136 template<typename EntityPtr>
137 constexpr static const bool is_event_header_v =
138 std::is_same_v<AnalysisTree::EventHeader, std::remove_const_t<std::remove_pointer_t<EntityPtr>>>;
139#endif
140
141 void CopyContentsRaw(Branch* other);
142
143 void CreateMapping(const Branch* other, std::string branch_name_prefix = "") const;
144
145 void UpdateConfigHash();
146
147 [[nodiscard]] AnalysisTree::ShortInt_t Hash() const {
148 const auto hasher = std::hash<std::string>();
149 return AnalysisTree::ShortInt_t(hasher(config_.GetName()));
150 }
151
152 private:
153 // template<size_t... Idx>
154 // auto GetVarsImpl(std::array<std::string, sizeof...(Idx)>&& field_names, std::index_sequence<Idx...>) {
155 // return std::make_tuple(GetFieldVar(field_names[Idx])...);
156 // }
157};
158
159}// namespace AnalysisTree
160
161#endif//ANALYSISTREE_INFRA_BRANCH_HPP_
A class to store configuration of the Container.
Definition BranchConfig.hpp:118
Definition Branch.hpp:23
void CopyContentsRaw(Branch *other)
Gets variables according to variable names specified in the arguments. Returns tuple of variables whi...
Definition Branch.cpp:132
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
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10
Definition Branch.hpp:48