sipm-characterisation 0.1.0
SiPM characterisation for ePIC — IV/DCR/gain, laser, readout, irradiation
Loading...
Searching...
No Matches
tree_database.h
Go to the documentation of this file.
1#pragma once
2
3#include "general_utility.h"
4
6{
7 std::unordered_map<int, std::string> name_database; // Mapping from node ID to display name
8 std::unordered_map<std::string, std::vector<int>> omonymes_database; // Mapping from display names to node IDs
9 std::map<int, std::vector<int>> database; // Tree structure with parent ID -> vector of child IDs
10 int current_id = 0; // To generate unique IDs
11
12public:
13 // Declaration
14 // --- I/O
15 int add_node(int parent_id, const std::string &child_name);
16 int inline add_node(const std::string &child_name) { return add_node(-1, child_name); };
17 template <typename... Args>
18 int add_node(const Args &...ancestors, const std::string &child_name);
19 int find_node(int parent_id, const std::string &name);
20 int find_node(int parent_id, int depth, const std::string &name);
21 int find_or_create_node(int parent_id, const std::string &name);
22 template <typename... Args>
23 int find_or_create_node(int parent_id, const Args &...ancestors);
24 // --- Getters
25 std::vector<int> get_children_ids(int parent_id);
26 std::vector<std::pair<int, std::string>> get_children(int parent_id) { return get_ids_name(get_children_ids(parent_id)); }
27 std::vector<int> get_children_ids(int parent_id, int depth);
28 std::vector<std::pair<int, std::string>> get_children(int parent_id, int depth) { return get_ids_name(get_children_ids(parent_id, depth)); }
29 std::vector<int> get_children_ids(int parent_id, int depth, std::vector<std::pair<int, std::string>> filters);
30 std::vector<std::pair<int, std::string>> get_children(int parent_id, int depth, std::vector<std::pair<int, std::string>> filters) { return get_ids_name(get_children_ids(parent_id, depth, filters)); }
31 std::vector<int> get_name_ids(std::string name) { return omonymes_database[name]; }
32 std::pair<int, std::string> get_id_name(int id) { return {id, name_database[id]}; }
33 std::vector<std::pair<int, std::string>> get_ids_name(std::vector<int> id_list);
34 int get_ancestor_id(int child_id, int depth);
35 std::pair<int, std::string> get_ancestor(int child_id, int depth) { return get_id_name(get_ancestor_id(child_id, depth)); }
36 // --- Helpers
37 bool is_name_unique(std::string name) { return get_name_ids(name).size() == 1; }
38 // --- Print function
39 void print_tree(int node_id, int depth);
40 void inline print_tree() { return print_tree(-1, -1); }
41};
42
43// Implementation
44// --- I/O
45int tree_database::add_node(int parent_id, const std::string &child_name)
46{
47 int child_id = current_id++; // Generate unique ID for the child
48 name_database[child_id] = child_name;
49 omonymes_database[child_name].push_back(child_id);
50 database[parent_id].push_back(child_id);
51 return child_id;
52}
53template <typename... Args>
54int tree_database::add_node(const Args &...ancestors, const std::string &child_name)
55{
56 // Check all ancestors are string
57 static_assert((std::is_same_v<Args, std::string> && ...), "[ERROR][database::tree_database::add_node] All ancestors must be of type std::string");
58
59 std::vector<std::string> nodes = {ancestors...};
60 int parent_id = -1;
61
62 // Traverse through ancestors, ensuring each exists or is created
63 for (const auto &node : nodes)
65
66 // Now add the final child node under the last parent
68}
69int tree_database::find_node(int parent_id, const std::string &name)
70{
72 for (auto id : node_children)
73 if (id.second == name)
74 return id.first; // Node exists, return its ID
75 return -1;
76}
77int tree_database::find_node(int parent_id, int depth, const std::string &name)
78{
80 for (auto id : node_children)
81 if (name_database[id.first] == name)
82 return id.first; // Node exists, return its ID
83 return -1;
84}
86{
88 if (found_node >= 0)
89 return found_node;
90
91 // Node doesn't exist, create a new one
92 return add_node(parent_id, name);
93}
94template <typename... Args>
96{
97 static_assert((std::is_same_v<Args, std::string> && ...), "[ERROR][tree_database::find_or_create_node] All ancestors must be of type std::string");
98
99 std::vector<std::string> nodes = {ancestors...};
100
101 // Traverse through ancestors, ensuring each exists or is created
102 for (const auto &node : nodes)
103 { // cout << node << endl;
105 }
106
107 return parent_id; // Return the last node ID created or found
108}
109// --- Getters
111{
112 std::vector<int> children;
113 for (auto current_child : database[parent_id])
114 children.push_back(current_child);
115 return children;
116}
118{
119 std::vector<int> result;
120 std::vector<int> prev_gen;
121 if (depth <= 0)
122 return result;
124 if (depth == 1)
127 for (auto i_depth = 1; i_depth < depth; i_depth++)
128 {
129 result.clear();
130 for (auto current_parent : prev_gen)
133 }
134 return result;
135}
136std::vector<int> tree_database::get_children_ids(int parent_id, int depth, std::vector<std::pair<int, std::string>> filters)
137{
138 std::vector<std::pair<int, std::string>> result;
139
140 // Trivial case, no reasonable filter is applicable
141 if (depth < 2)
143
144 // Filter out any filters with depths below 1 or greater than the specified depth
145 filters.erase(std::remove_if(filters.begin(), filters.end(), [depth](const std::pair<int, std::string> &filter)
146 { return filter.first < 1 || filter.first > depth; }),
147 filters.end());
148
149 if (filters.size() == 0)
151
152 // Map to group filters by depth
153 std::unordered_map<int, std::vector<std::string>> filters_names_per_depth;
154 std::unordered_map<int, std::vector<int>> filters_ids_per_depth;
155 for (const auto &filter : filters)
156 {
157 int filter_depth = filter.first;
158 std::string filter_name = filter.second;
160 }
161 std::vector<int> current_parent_ids = {parent_id};
162 std::vector<int> current_children_ids = {};
163 for (auto i_depth = 1; i_depth <= depth; i_depth++)
164 {
168 {
170 if (found_node >= 0)
172 }
173 else
176 current_children_ids.clear();
177 }
178 return current_parent_ids;
179}
180std::vector<std::pair<int, std::string>> tree_database::get_ids_name(std::vector<int> id_list)
181{
182 std::vector<std::pair<int, std::string>> result;
183 for (auto current_id : id_list)
184 result.push_back({current_id, name_database[current_id]});
185 return result;
186}
188{
189 if (depth == 0)
190 return child_id;
191 std::pair<int, std::string> result;
192 for (auto [parent_id, children_ids] : database)
193 {
194 auto current_child = std::find(children_ids.begin(), children_ids.end(), child_id);
195 if (current_child != children_ids.end())
196 return get_ancestor_id(parent_id, depth - 1);
197 }
198 return -1;
199}
200// --- Print function
202{
203 // Indent based on the depth of the node in the tree
204 std::cout << "|- ";
205 if (node_id == depth && depth == -1)
206 std::cout << "Printing database";
207 for (int i = 0; i < depth; ++i)
208 std::cout << " - "; // Characters per level of depth
209
210 // Print the current node's name
211 std::cout << name_database[node_id] << std::endl;
212
213 // If the node has children, recursively print them
214 if (database.find(node_id) != database.end())
215 for (int child_id : database[node_id])
216 print_tree(child_id, depth + 1); // Recursive call for each child
217}
Definition tree_database.h:6
int find_node(int parent_id, const std::string &name)
Definition tree_database.h:69
std::vector< std::pair< int, std::string > > get_children(int parent_id, int depth, std::vector< std::pair< int, std::string > > filters)
Definition tree_database.h:30
int add_node(const std::string &child_name)
Definition tree_database.h:16
std::vector< int > get_name_ids(std::string name)
Definition tree_database.h:31
void print_tree()
Definition tree_database.h:40
int get_ancestor_id(int child_id, int depth)
Definition tree_database.h:187
std::vector< std::pair< int, std::string > > get_children(int parent_id, int depth)
Definition tree_database.h:28
std::vector< std::pair< int, std::string > > get_ids_name(std::vector< int > id_list)
Definition tree_database.h:180
std::pair< int, std::string > get_ancestor(int child_id, int depth)
Definition tree_database.h:35
std::vector< int > get_children_ids(int parent_id)
Definition tree_database.h:110
int find_or_create_node(int parent_id, const std::string &name)
Definition tree_database.h:85
int add_node(int parent_id, const std::string &child_name)
Definition tree_database.h:45
bool is_name_unique(std::string name)
Definition tree_database.h:37
std::pair< int, std::string > get_id_name(int id)
Definition tree_database.h:32
std::vector< std::pair< int, std::string > > get_children(int parent_id)
Definition tree_database.h:26
TH2_Type * build_fine_tune_raw_histogram(std::vector< TString > kInputFileNames, TString kRunTag, TString kOutputFileName, bool kRecalculate)
Functions -------------------------------------------------------------------------------------------...
Definition fine_analysis.h:80
Definition database.C:26
std::vector< T > merge(const std::vector< T > &vec1, const std::vector< T > &vec2)
Definition general_utility.h:78