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
23struct ConfigElement {
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>
39class VectorConfig {
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 void Print() const;
77
78 protected:
79 void RemoveField(const std::string& name, int id);
80 MapType map_{};
81 ShortInt_t size_{0};
82 ClassDef(VectorConfig, 2)
83};
84
86
89class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, public VectorConfig<bool> {
90
91 public:
92 BranchConfig() = default;
93 BranchConfig(const BranchConfig&) = default;
94 BranchConfig(BranchConfig&&) = default;
95 BranchConfig& operator=(BranchConfig&&) = default;
96 BranchConfig& operator=(const BranchConfig&) = default;
97 ~BranchConfig() override = default;
98
99 BranchConfig(std::string name, DetType type, std::string title = "");
100
101 void Print() const;
102
103 void PrintBranchId() const;
104
105 ANALYSISTREE_ATTR_NODISCARD Types GetFieldType(const std::string& sField) const;
106 ANALYSISTREE_ATTR_NODISCARD ShortInt_t GetFieldId(const std::string& sField) const;
107
108 // Setters
109 template<typename T>
110 void AddField(const std::string& name, const std::string& title = "") {
111 GuaranteeFieldNameVacancy(name);
112 VectorConfig<T>::AddField(name, title);
113 }
114 template<typename T>
115 void AddFields(const std::vector<std::string>& names, const std::string& title = "") {
116 for (auto& n : names) {
117 GuaranteeFieldNameVacancy(n);
118 }
119 VectorConfig<T>::AddFields(names, title);
120 }
121 template<typename T>
122 void AddField(const std::string& name, ShortInt_t id, const std::string& title = "") {
123 GuaranteeFieldNameVacancy(name);
124 VectorConfig<T>::AddField(name, id, title);
125 }
126
127 void RemoveField(const std::string& name);
128
129 void RemoveFields(const std::vector<std::string>& names) {
130 for (auto& n : names) {
131 RemoveField(n);
132 }
133 }
134
135 void SetTitle(std::string title) { title_ = std::move(title); }
136
137 // Getters
138 template<typename T>
139 ANALYSISTREE_ATTR_NODISCARD const MapType& GetMap() const { return VectorConfig<T>::GetMap(); }
140 template<typename T>
141 ANALYSISTREE_ATTR_NODISCARD ShortInt_t GetSize() const { return VectorConfig<T>::GetSize(); }
142
143 ANALYSISTREE_ATTR_NODISCARD std::string GetName() const { return name_; }
144 ANALYSISTREE_ATTR_NODISCARD std::string GetTitle() const { return title_; }
145
146 template<typename T>
147 ANALYSISTREE_ATTR_NODISCARD std::vector<std::string> GetFieldsNamesT() const {
148 std::vector<std::string> result;
149 std::transform(begin(GetMap<T>()), end(GetMap<T>()), back_inserter(result),
150 [](const typename MapType::value_type& elem) { return elem.first; });
151 return result;
152 }
153 ANALYSISTREE_ATTR_NODISCARD size_t GetId() const { return id_; }
154 ANALYSISTREE_ATTR_NODISCARD DetType GetType() const { return type_; }
155
161 ANALYSISTREE_ATTR_NODISCARD BranchConfig Clone(const std::string& name, DetType type) const;
162
163 ANALYSISTREE_ATTR_NODISCARD BranchConfig CloneAndMerge(const BranchConfig& attached) const;
164
165 bool HasField(const std::string& field) const { return GetFieldId(field) != UndefValueShort; }
166
167 protected:
168 void GenerateId();
169
170 void GuaranteeFieldNameVacancy(const std::string& name) const;
171
172 std::string name_;
173 std::string title_;
174 size_t id_{0};
175 DetType type_{DetType(UndefValueShort)};
176
177 ClassDefOverride(BranchConfig, 4);
178};
179
180// BranchConfig Merge(const BranchConfig& primary, const BranchConfig& secondary);
181
182}// namespace AnalysisTree
183#endif//ANALYSISTREE_BRANCHCONFIG_H
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
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