AnalysisTree
Loading...
Searching...
No Matches
BranchConfig.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
5#ifndef ANALYSISTREE_BRANCHCONFIG_H
6#define ANALYSISTREE_BRANCHCONFIG_H
7
8#include <algorithm>
9#include <iomanip>
10#include <iostream>
11#include <map>
12#include <string>
13#include <utility>
14#include <vector>
15
16#include <TObject.h>
17
18#include "Constants.hpp"
19
20namespace AnalysisTree {
21
24 ConfigElement() = default;
25 virtual ~ConfigElement() = default;
26
27 explicit ConfigElement(ShortInt_t id, std::string title) : id_(id), title_(std::move(title)) {}
28 ShortInt_t id_{0};
29 std::string title_;
30
31 protected:
32 ClassDef(ConfigElement, 1)
33};
34
35typedef std::map<std::string, ConfigElement> MapType;
36
38template<typename T>
40 public:
41 VectorConfig() = default;
42 VectorConfig(const VectorConfig&) = default;
43 explicit VectorConfig(const MapType& map) : map_(map), size_(map.size()) {}
44 VectorConfig(VectorConfig&&) noexcept(std::is_nothrow_move_constructible<MapType>::value) = default;
45 VectorConfig& operator=(VectorConfig&&) noexcept(std::is_nothrow_move_assignable<MapType>::value) = default;
46 VectorConfig& operator=(const VectorConfig&) = default;
47 virtual ~VectorConfig() = default;
48
49 virtual void AddField(const std::string& name, const std::string& title) {
50 map_.insert(std::make_pair(name, ConfigElement(size_++, title)));
51 }
52 virtual void AddField(const std::string& name, ShortInt_t id, const std::string& title) {
53 map_.insert(std::make_pair(name, ConfigElement(id, title)));
54 }
55 void AddField(const std::string& name, const ConfigElement& field) {
56 map_.insert(std::make_pair(name, field));
57 }
58 virtual void AddFields(const std::vector<std::string>& names, const std::string& title) {
59 for (const auto& name : names) {
60 map_.insert(std::make_pair(name, ConfigElement(size_++, title)));
61 }
62 }
63
64 ANALYSISTREE_ATTR_NODISCARD ShortInt_t GetId(const std::string& sField) const {
65 auto search = map_.find(sField);
66 if (search != map_.end()) {
67 return search->second.id_;
68 } else {
69 return UndefValueShort;
70 }
71 }
72
73 ANALYSISTREE_ATTR_NODISCARD virtual const MapType& GetMap() const { return map_; }
74 ANALYSISTREE_ATTR_NODISCARD virtual ShortInt_t GetSize() const { return size_; }
75
76 virtual void Print() const {
77 if (map_.empty()) return;
78
79 auto print_row = [](const std::vector<std::pair<std::string, int>>& elements) {
80 for (const auto& el : elements) {
81 std::cout << std::left << std::setw(el.second) << std::setfill(' ') << el.first;
82 }
83 std::cout << std::endl;
84 };
85
86 int name_strlen{0};
87 for (const auto& entry : map_) {
88 name_strlen = std::max(name_strlen, (int) entry.first.length());
89 }
90 name_strlen += 4;
91
92 print_row({{"Id", 10}, {"Name", name_strlen}, {"Info", 50}});
93 for (const auto& entry : map_) {
94 if (entry.second.title_.find("\n") == std::string::npos) {
95 print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {entry.second.title_, 50}});
96 } else {
97 auto est = SplitString(entry.second.title_);
98 print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {est.at(0), 50}});
99 for (int iest = 1; iest < est.size(); ++iest) {
100 print_row({{"", 10}, {"", name_strlen}, {est.at(iest), 50}});
101 }
102 }
103 }
104 }
105
106 protected:
107 static std::vector<std::string> SplitString(const std::string& input);
108 void RemoveField(const std::string& name, int id);
109 MapType map_{};
110 ShortInt_t size_{0};
111 ClassDef(VectorConfig, 2)
112};
113
115
118class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, public VectorConfig<bool> {
119
120 public:
121 BranchConfig() = default;
122 BranchConfig(const BranchConfig&) = default;
123 BranchConfig(BranchConfig&&) = default;
124 BranchConfig& operator=(BranchConfig&&) = default;
125 BranchConfig& operator=(const BranchConfig&) = default;
126 ~BranchConfig() override = default;
127
128 BranchConfig(std::string name, DetType type, std::string title = "");
129
130 void Print() const override;
131
132 void PrintBranchId() const;
133
134 ANALYSISTREE_ATTR_NODISCARD Types GetFieldType(const std::string& sField) const;
135 ANALYSISTREE_ATTR_NODISCARD ShortInt_t GetFieldId(const std::string& sField) const;
136
137 // Setters
138 template<typename T>
139 void AddField(const std::string& name, const std::string& title = "") {
140 GuaranteeFieldNameVacancy(name);
141 VectorConfig<T>::AddField(name, title);
142 }
143 template<typename T>
144 void AddFields(const std::vector<std::string>& names, const std::string& title = "") {
145 for (auto& n : names) {
146 GuaranteeFieldNameVacancy(n);
147 }
148 VectorConfig<T>::AddFields(names, title);
149 }
150 template<typename T>
151 void AddField(const std::string& name, ShortInt_t id, const std::string& title = "") {
152 GuaranteeFieldNameVacancy(name);
153 VectorConfig<T>::AddField(name, id, title);
154 }
155
156 void RemoveField(const std::string& name);
157
158 void RemoveFields(const std::vector<std::string>& names) {
159 for (auto& n : names) {
160 RemoveField(n);
161 }
162 }
163
164 void SetTitle(std::string title) { title_ = std::move(title); }
165
166 // Getters
167 template<typename T>
168 ANALYSISTREE_ATTR_NODISCARD const MapType& GetMap() const { return VectorConfig<T>::GetMap(); }
169 template<typename T>
170 ANALYSISTREE_ATTR_NODISCARD ShortInt_t GetSize() const { return VectorConfig<T>::GetSize(); }
171
172 ANALYSISTREE_ATTR_NODISCARD std::string GetName() const { return name_; }
173 ANALYSISTREE_ATTR_NODISCARD std::string GetTitle() const { return title_; }
174
175 template<typename T>
176 ANALYSISTREE_ATTR_NODISCARD std::vector<std::string> GetFieldsNamesT() const {
177 std::vector<std::string> result;
178 std::transform(begin(GetMap<T>()), end(GetMap<T>()), back_inserter(result),
179 [](const typename MapType::value_type& elem) { return elem.first; });
180 return result;
181 }
182 ANALYSISTREE_ATTR_NODISCARD size_t GetId() const { return id_; }
183 ANALYSISTREE_ATTR_NODISCARD DetType GetType() const { return type_; }
184
190 ANALYSISTREE_ATTR_NODISCARD BranchConfig Clone(const std::string& name, DetType type) const;
191
192 ANALYSISTREE_ATTR_NODISCARD BranchConfig CloneAndMerge(const BranchConfig& attached) const;
193
194 bool HasField(const std::string& field) const { return GetFieldId(field) != UndefValueShort; }
195
196 protected:
197 void GenerateId();
198
199 void GuaranteeFieldNameVacancy(const std::string& name) const;
200
201 std::string name_;
202 std::string title_;
203 size_t id_{0};
204 DetType type_{DetType(UndefValueShort)};
205
206 ClassDefOverride(BranchConfig, 4);
207};
208
209// BranchConfig Merge(const BranchConfig& primary, const BranchConfig& secondary);
210
211}// namespace AnalysisTree
212#endif//ANALYSISTREE_BRANCHCONFIG_H
A class to store configuration of the Container.
Definition BranchConfig.hpp:118
ANALYSISTREE_ATTR_NODISCARD BranchConfig Clone(const std::string &name, DetType type) const
Creates a copy with different name and/or detector type.
Definition BranchConfig.cpp:90
Template class to store configuration, e. g. name and description of the vector element.
Definition BranchConfig.hpp:39
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10
Information to store about a data field in Configuration.
Definition BranchConfig.hpp:23