ATLAS Offline Software
Loading...
Searching...
No Matches
ColumnarToolArray.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
12
18
19//
20// method implementations
21//
22
23namespace columnar
24{
25 ColumnarToolArray ::
26 ColumnarToolArray (ColumnarToolArray* val_parent)
27 {
28 if (val_parent == nullptr)
29 {
30 m_data = std::make_shared<ColumnarToolDataArray> ();
31 m_data->mainTool = this;
32 m_data->sharedTools.push_back (this);
33
34 setContainerUserName (eventContextCIName, eventRangeColumnName);
35 m_eventsData = std::make_unique<ColumnAccessorDataArray> (&m_eventsIndex, &m_eventsData, &typeid (ColumnarOffsetType), ColumnAccessMode::input);
36 ColumnInfo eventsInfo;
37 eventsInfo.isOffset = true;
39 addColumn (std::string (eventContextCIName), m_eventsData.get(), std::move (eventsInfo));
40 } else
41 {
42 m_data = val_parent->m_data;
43 m_data->sharedTools.push_back (this);
44
45 m_eventsData = std::make_unique<ColumnAccessorDataArray> (&m_eventsIndex, &m_eventsData, &typeid (ColumnarOffsetType), ColumnAccessMode::input);
46 ColumnInfo eventsInfo;
47 eventsInfo.isOffset = true;
49 addColumn (std::string (EventContextDef::idName), m_eventsData.get(), std::move (eventsInfo));
50 }
51 }
52
53
54
55 ColumnarToolArray ::
56 ~ColumnarToolArray ()
57 {
58 if (m_data->mainTool == this)
59 m_data->mainTool = nullptr;
60
61 auto iter = std::find (m_data->sharedTools.begin(), m_data->sharedTools.end(), this);
62 if (iter != m_data->sharedTools.end())
63 m_data->sharedTools.erase (iter);
64 }
65
66
67
68 void ColumnarToolArray ::
69 addSubtool (ColumnarToolArray& subtool)
70 {
71 // make a copy of the shared data for the subtool, so that I don't
72 // accidentally release it when resetting the shared data pointer on
73 // the subtool
74 auto subtoolData = subtool.m_data;
75
76 if (m_data == subtoolData)
77 return;
78
79 if (subtoolData->mainTool != &subtool)
80 throw std::runtime_error ("subtool already has a different parent tool");
81
82 for (auto& containerName : subtoolData->containerInternalToUserNames)
83 {
84 auto [iter,success] = m_data->containerInternalToUserNames.emplace (containerName.first, containerName.second);
85 if (!success && iter->second != containerName.second)
86 throw std::runtime_error ("container assigned different name in subtool: " + iter->second + " vs " + containerName.second);
87 if (success)
88 m_data->containerUserToInternalNames[containerName.second].push_back (containerName.first);
89 }
90 for (auto& columnName : subtoolData->columnInternalToUserNames)
91 {
92 auto [iter,success] = m_data->columnInternalToUserNames.emplace (columnName.first, columnName.second);
93 if (!success && iter->second != columnName.second)
94 throw std::runtime_error ("column assigned different name in subtool: " + iter->second + " vs " + columnName.second);
95 if (success)
96 m_data->columnUserToInternalNames[columnName.second].push_back (columnName.first);
97 }
98
99 for (auto& column : subtoolData->columns)
100 {
101 auto [iter,success] = m_data->columns.try_emplace (column.first, std::move (column.second));
102 if (!success)
103 {
104 if (iter->second.empty())
105 iter->second = std::move (column.second);
106 else if (!column.second.empty())
107 iter->second.mergeData (column.first, std::move (column.second));
108 }
109 }
110
111 for (auto& [ciName, watchers] : subtoolData->linkTargetNameWatchers)
112 {
113 auto& target = m_data->linkTargetNameWatchers[ciName];
114 target.insert (target.end(), std::make_move_iterator (watchers.begin()), std::make_move_iterator (watchers.end()));
115 }
116 subtoolData->linkTargetNameWatchers.clear();
117 m_data->linkTargetNames.insert (subtoolData->linkTargetNames.begin(), subtoolData->linkTargetNames.end());
118
119 m_data->sharedTools.insert (m_data->sharedTools.end(), subtoolData->sharedTools.begin(), subtoolData->sharedTools.end());
120 for (auto& tool : subtoolData->sharedTools)
121 tool->m_data = m_data;
122
123 m_data->notifyLinkTargetNameWatchers ();
124 }
125
126
127
128 StatusCode ColumnarToolArray ::
129 initializeColumns ()
130 {
131 return StatusCode::SUCCESS;
132 }
133
134
135
136 std::vector<ColumnInfo> ColumnarToolArray ::
137 getColumnInfo () const
138 {
139 for (auto& [ciName, watchers] : m_data->linkTargetNameWatchers)
140 {
141 if (!m_data->hasContainerUserName (ciName))
142 throw std::runtime_error ("no name registered for container: " + ciName + " (it needs an ObjectColumn accessor, e.g. as the target of a link)");
143 }
144
145 std::vector<std::pair<std::string,std::string>> names;
146 names.reserve (m_data->columns.size());
147 for (auto& [name, column] : m_data->columns)
148 {
149 if (!column.empty())
150 names.emplace_back (name, m_data->convertInternalToUserName (name));
151 }
152 std::sort (names.begin(), names.end(), [] (const auto& a, const auto& b) { return a.second < b.second; });
153
154 std::vector<ColumnInfo> result;
155 result.reserve (names.size());
156 for (auto& name : names)
157 {
158 auto& column = m_data->columns.at (name.first);
159 auto info = column.info();
160 info.name = name.second;
161 info.offsetName = m_data->convertInternalToUserName (info.offsetName);
162 info.replacesColumn = m_data->convertInternalToUserName (info.replacesColumn);
163 if (m_data->linkTargetNameWatchers.contains (name.first))
164 info.nameForLinks = m_data->linkTargetName (name.first);
165 if (result.empty() || result.back().name != info.name)
166 result.push_back (std::move(info));
167 else
168 mergeColumnInfo (result.back(), info);
169 }
170 return result;
171 }
172
173
174
175 void ColumnarToolArray ::
176 renameColumn (const std::string& from, const std::string& to)
177 {
178 if (auto iter = m_data->columnUserToInternalNames.find (from);
179 iter != m_data->columnUserToInternalNames.end())
180 {
181 for (auto& internalName : iter->second)
182 m_data->columnInternalToUserNames[internalName] = to;
183 auto internalNames = iter->second;
184 m_data->columnUserToInternalNames.erase (iter);
185 m_data->columnUserToInternalNames[to].insert (m_data->columnUserToInternalNames[to].end(), internalNames.begin(), internalNames.end());
186 return;
187 }
188
189 auto internalNames = m_data->convertUserToInternalNames (from);
190 if (internalNames.empty())
191 throw std::runtime_error ("column not found for rename: " + from);
192 m_data->columnUserToInternalNames[to].insert (m_data->columnUserToInternalNames[to].end(), internalNames.begin(), internalNames.end());
193 for (auto& internalName : internalNames)
194 m_data->columnInternalToUserNames[internalName] = to;
195 }
196
197
198
199 void ColumnarToolArray ::
200 setLinkTargetName (const std::string& column, const std::string& name)
201 {
202 auto internalNames = m_data->convertUserToInternalNames (column);
203 if (internalNames.empty())
204 throw std::runtime_error ("column not found for link target name: " + column);
205 for (auto& internalName : internalNames)
206 {
207 if (!m_data->hasContainerUserName (internalName))
208 throw std::runtime_error ("column for link target name is not the offset map of a container: " + column);
209 }
210 for (auto& internalName : internalNames)
211 m_data->linkTargetNames[internalName] = name;
212 m_data->notifyLinkTargetNameWatchers ();
213 }
214
215
216
217 void ColumnarToolArray ::
218 setColumnIndex (const std::string& name, std::size_t index)
219 {
220 auto internalNames = m_data->convertUserToInternalNames (name);
221 bool wasSet = false;
222 for (auto& internalName : internalNames)
223 {
224 if (auto column = m_data->columns.find (internalName);
225 column != m_data->columns.end() && !column->second.empty())
226 {
227 column->second.setIndex (index);
228 wasSet = true;
229 }
230 }
231 if (!wasSet)
232 throw std::runtime_error ("column not found: " + name);
233 }
234
235
236
237 void ColumnarToolArray ::
238 setContainerUserName (std::string_view container, const std::string& name)
239 {
240 auto [iter, success] = m_data->containerInternalToUserNames.emplace (container, name);
241 if (!success && iter->second != name)
242 throw std::runtime_error ("container already registered with different name: " + name + " vs " + iter->second);
243 m_data->containerUserToInternalNames[name].emplace_back (container);
244 m_data->notifyLinkTargetNameWatchers ();
245 }
246
247
248
249 void ColumnarToolArray ::
250 watchLinkTargetName (std::string_view ciName, std::function<void(const std::string&)> watcher)
251 {
252 if (m_data->hasContainerUserName (ciName))
253 watcher (m_data->linkTargetName (ciName));
254 auto iter = m_data->linkTargetNameWatchers.find (ciName);
255 if (iter == m_data->linkTargetNameWatchers.end())
256 iter = m_data->linkTargetNameWatchers.emplace (std::string (ciName), std::vector<std::function<void(const std::string&)>> ()).first;
257 iter->second.push_back (std::move (watcher));
258 }
259
260
261
262 void ColumnarToolArray ::
263 addColumn (const std::string& name, ColumnAccessorDataArray *accessorData, ColumnInfo&& info)
264 {
265 if (info.type == nullptr || info.sizeOfType == 0u)
266 throw std::logic_error ("missing type information for column: " + name);
267 if (*info.type != *accessorData->type)
268 throw std::logic_error ("inconsistent type information for column: " + name);
269 info.accessMode = accessorData->accessMode;
270
271 m_data->columns[name].addAccessor (name, info, accessorData);
272
273 // make sure all referenced columns also exist
274 if (!info.offsetName.empty())
275 m_data->columns[info.offsetName];
276 if (!info.replacesColumn.empty())
277 m_data->columns[info.replacesColumn];
278 }
279
280
281
282 ColumnDataArray ::
283 ColumnDataArray (ColumnDataArray&& other) noexcept
284 : m_info (std::move (other.m_info))
285 , m_accessors (std::move (other.m_accessors))
286 {
287 for (auto *ptr : m_accessors)
288 {
289 if (ptr->dataRef == &other)
290 ptr->dataRef = this;
291 }
292 }
293
294
295
296 ColumnDataArray ::
297 ~ColumnDataArray () noexcept
298 {
299 for (auto *ptr : m_accessors)
300 {
301 if (ptr->dataRef == this)
302 ptr->dataRef = nullptr;
303 }
304 }
305
306
307
308 bool ColumnDataArray ::
309 empty () const noexcept
310 {
311 return m_accessors.empty();
312 }
313
314
315
316 const ColumnInfo& ColumnDataArray ::
317 info () const noexcept
318 {
319 return m_info;
320 }
321
322
323
324 void ColumnDataArray ::
325 addAccessor (const std::string& name, const ColumnInfo& val_info, ColumnAccessorDataArray* val_accessorData)
326 {
327 if (m_accessors.empty())
328 {
329 m_info = val_info;
330
331 // ensure that any index is not zero. zero is reserved for a
332 // nullptr, so that if an index has never been initialized, we
333 // get a segfault on access.
334 m_info.index = 0;
335 } else
336 {
337 if (val_info.type != m_info.type)
338 throw std::runtime_error ("multiple types for column: " + name);
340 m_info.accessMode = ColumnAccessMode::update;
341 else if (m_info.accessMode == ColumnAccessMode::output || val_info.accessMode == ColumnAccessMode::output)
342 m_info.accessMode = ColumnAccessMode::output;
343 if (!val_info.isOptional)
344 m_info.isOptional = false;
345 // for the type matching the strictest accessor wins
346 m_info.typeCategory = mergeColumnTypeCategory (m_info.typeCategory, val_info.typeCategory);
347 if (val_info.offsetName != m_info.offsetName)
348 throw std::runtime_error ("inconsistent offset map for column: " + name);
349 }
350
351 if (val_accessorData)
352 {
353 m_accessors.push_back (val_accessorData);
354 val_accessorData->dataRef = this;
355 }
356 }
357
358
359
360 void ColumnDataArray ::
361 removeAccessor (ColumnAccessorDataArray& val_accessorData)
362 {
363 auto iter = std::find (m_accessors.begin(), m_accessors.end(), &val_accessorData);
364 if (iter == m_accessors.end())
365 return;
366 m_accessors.erase (iter);
367 val_accessorData.dataRef = nullptr;
368 }
369
370
371
372 void ColumnDataArray ::
373 mergeData (const std::string& name, ColumnDataArray&& other)
374 {
375 mergeColumnInfo (m_info, other.m_info);
376 addAccessor (name, other.m_info, nullptr);
377 m_accessors.insert (m_accessors.end(), other.m_accessors.begin(), other.m_accessors.end());
378 for (auto *ptr : other.m_accessors)
379 ptr->dataRef = this;
380 other.m_accessors.clear();
381 }
382
383
384
385 void ColumnDataArray ::
386 setIndex (unsigned index) noexcept
387 {
388 m_info.index = index;
389 for (auto *ptr : m_accessors)
390 *ptr->dataIndexPtr = index;
391 }
392
393
394
395 std::string ColumnarToolDataArray ::
396 convertInternalToUserName (std::string_view name) const
397 {
398 if (name.empty())
399 return std::string{};
400 if (auto iter = columnInternalToUserNames.find (name);
401 iter != columnInternalToUserNames.end())
402 return iter->second;
403 auto split = name.find ('.');
404 if (split == std::string::npos)
405 split = name.size();
406 auto containerName = name.substr (0, split);
407 auto iter = containerInternalToUserNames.find (containerName);
408 if (iter == containerInternalToUserNames.end())
409 return std::string (name);
410 return iter->second + std::string (name.substr (split));
411 }
412
413
414
415 std::vector<std::string> ColumnarToolDataArray ::
416 convertUserToInternalNames (std::string_view name) const
417 {
418 if (auto iter = columnUserToInternalNames.find (name);
419 iter != columnUserToInternalNames.end())
420 return iter->second;
421 auto split = name.find ('.');
422 if (split == std::string::npos)
423 split = name.size();
424 auto containerName = name.substr (0, split);
425 std::vector<std::string> result;
426 if (auto iter = containerUserToInternalNames.find (containerName);
427 iter != containerUserToInternalNames.end())
428 {
429 for (auto& internalContainerName : iter->second)
430 {
431 std::string subname = internalContainerName + std::string (name.substr (split));
432 if (!columnInternalToUserNames.contains (subname) && columns.contains (subname))
433 result.push_back (std::move (subname));
434 }
435 }
436 if (!containerInternalToUserNames.contains (containerName) && !columnInternalToUserNames.contains (name) && columns.contains (name))
437 result.push_back (std::string (name));
438 return result;
439 }
440
441
442
443 std::string ColumnarToolDataArray ::
444 linkTargetName (std::string_view ciName) const
445 {
446 if (auto iter = linkTargetNames.find (ciName); iter != linkTargetNames.end())
447 return iter->second;
448 return containerInternalToUserNames.find (ciName)->second;
449 }
450
451
452
453 bool ColumnarToolDataArray ::
454 hasContainerUserName (std::string_view ciName) const
455 {
456 return containerInternalToUserNames.contains (ciName);
457 }
458
459
460
461 void ColumnarToolDataArray ::
462 notifyLinkTargetNameWatchers () const
463 {
464 for (auto& [ciName, watchers] : linkTargetNameWatchers)
465 {
466 if (!hasContainerUserName (ciName))
467 continue;
468 const std::string name = linkTargetName (ciName);
469 for (auto& watcher : watchers)
470 watcher (name);
471 }
472 }
473}
helper functions for working with the type information in columnar::ColumnInfo
static Double_t a
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
::StatusCode StatusCode
StatusCode definition for legacy code.
void mergeColumnInfo(ColumnInfo &target, const ColumnInfo &source)
constexpr ColumnTypeCategory mergeColumnTypeCategory(ColumnTypeCategory a, ColumnTypeCategory b) noexcept
merge the type categories of two users of the same column (with the same type), the strictest one win...
constexpr std::string_view eventContextCIName
the name for the event context container id
@ update
an updateable column
Definition ColumnInfo.h:28
@ output
an output column
Definition ColumnInfo.h:25
@ input
an input column
Definition ColumnInfo.h:22
void setColumnType(ColumnInfo &info) noexcept
set the type information in the ColumnInfo for the given C++ type
Definition ColumnInfo.h:227
const std::string eventRangeColumnName
the default name for the column containing the event range
std::size_t ColumnarOffsetType
the type used for the size and offsets in the columnar data
Definition index.py:1
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
all the data about a column accessor that the ColumnarTool needs to know about
ColumnDataArray * dataRef
the ColumnDataArray object that holds us
ColumnDataArray() noexcept=default
standard constructor
std::vector< ColumnAccessorDataArray * > m_accessors
the data on all accessors for this column
ColumnInfo m_info
the column info
void addAccessor(const std::string &name, const ColumnInfo &val_info, ColumnAccessorDataArray *val_accessorData)
add an info to the column
a struct that contains meta-information about each column that's needed to interface the column with ...
Definition ColumnInfo.h:73
std::string offsetName
the name of the offset column used for this column (or empty string for none)
Definition ColumnInfo.h:120
ColumnTypeCategory typeCategory
the type category for the individual entries in the column
Definition ColumnInfo.h:100
bool isOptional
whether this column is optional
Definition ColumnInfo.h:167
bool isOffset
whether this is an offset column
Definition ColumnInfo.h:138
ColumnAccessMode accessMode
the access mode for the column
Definition ColumnInfo.h:104
const std::type_info * type
the type of the individual entries in the column
Definition ColumnInfo.h:92
std::string linkTargetName(std::string_view ciName) const
the name of the given container as a link target, i.e.
std::unordered_map< std::string, std::string, StringHash, std::equal_to<> > containerInternalToUserNames
the names associated with all container ids
std::unordered_map< std::string, std::vector< std::string >, StringHash, std::equal_to<> > columnUserToInternalNames
std::unordered_map< std::string, std::string, StringHash, std::equal_to<> > linkTargetNames
the names to use for link targets that were set explicitly, instead of the name set by the ObjectColu...
bool hasContainerUserName(std::string_view ciName) const
whether the given container has a user name assigned
std::unordered_map< std::string, std::vector< std::string >, StringHash, std::equal_to<> > containerUserToInternalNames
std::unordered_map< std::string, ColumnDataArray, StringHash, std::equal_to<> > columns
the name-column map
std::unordered_map< std::string, std::vector< std::function< void(const std::string &)> >, StringHash, std::equal_to<> > linkTargetNameWatchers
the watchers for the link target names of containers
std::unordered_map< std::string, std::string, StringHash, std::equal_to<> > columnInternalToUserNames
an extra map, for mapping individual column names
static constexpr std::string_view idName