Skip to content

Commit 82fc593

Browse files
authored
Fields removal (#133)
* enable fields removing; bugfix PR #125: don't create matching_case var multiple times * apply clang format
1 parent 53759d9 commit 82fc593

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

core/BranchConfig.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ BranchConfig BranchConfig::CloneAndMerge(const BranchConfig& attached) const {
128128
result.AddField<bool>(name2 + "_" + field.first, name2 + ": " + field.second.title_);
129129
}
130130

131-
result.AddField<int>("matching_case", "0 - both present, 1 - only first present, 2 - only second present");
131+
if (type1 != DetType::kEventHeader && attached.GetType() != DetType::kEventHeader) {
132+
result.AddField<int>("matching_case", "0 - both present, 1 - only first present, 2 - only second present");
133+
}
132134

133135
return result;
134136
}
@@ -150,6 +152,31 @@ std::vector<std::string> VectorConfig<T>::SplitString(const std::string& input)
150152
return result;
151153
}
152154

155+
template<typename T>
156+
void VectorConfig<T>::RemoveField(const std::string& name, int id) {
157+
auto iter = map_.find(name);
158+
map_.erase(iter);
159+
for (auto& m : map_) {
160+
if (m.second.id_ > id) {
161+
m.second.id_--;
162+
}
163+
}
164+
}
165+
166+
void BranchConfig::RemoveField(const std::string& name) {
167+
if (!HasField(name)) {
168+
throw std::runtime_error("BranchConfig::RemoveField(): no field " + name + " to be removed");
169+
}
170+
auto field_type = GetFieldType(name);
171+
auto field_id = GetFieldId(name);
172+
if (field_id < 0) {
173+
throw std::runtime_error("BranchConfig::RemoveField(): default field " + name + " cannot be removed");
174+
}
175+
if (field_type == Types::kInteger) VectorConfig<int>::RemoveField(name, field_id);
176+
if (field_type == Types::kFloat) VectorConfig<float>::RemoveField(name, field_id);
177+
if (field_type == Types::kBool) VectorConfig<bool>::RemoveField(name, field_id);
178+
}
179+
153180
void BranchConfig::GuaranteeFieldNameVacancy(const std::string& name) const {
154181
if (HasField(name)) {
155182
throw std::runtime_error("BranchConfig::GuaranteeFieldNameVacancy(): field " + name + " already exists");

core/BranchConfig.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class VectorConfig {
105105

106106
protected:
107107
static std::vector<std::string> SplitString(const std::string& input);
108+
void RemoveField(const std::string& name, int id);
108109
MapType map_{};
109110
ShortInt_t size_{0};
110111
ClassDef(VectorConfig, 2)
@@ -150,6 +151,14 @@ class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, publi
150151
VectorConfig<T>::AddField(name, id, title);
151152
}
152153

154+
void RemoveField(const std::string& name);
155+
156+
void RemoveFields(const std::vector<std::string>& names) {
157+
for (auto& n : names) {
158+
RemoveField(n);
159+
}
160+
}
161+
153162
// Getters
154163
template<typename T>
155164
ANALYSISTREE_ATTR_NODISCARD const MapType& GetMap() const { return VectorConfig<T>::GetMap(); }

0 commit comments

Comments
 (0)