ATLAS Offline Software
Loading...
Searching...
No Matches
IndexMap.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
10
11
16#include "GaudiKernel/ServiceHandle.h"
17#include "GaudiKernel/IIncidentSvc.h"
18
19
20namespace D3PD {
21
22
31IndexMap::IndexMap (INamedInterface* parent,
32 const std::string& targetLabel,
33 ToolHandle<ICollectionGetterRegistryTool>& registry,
34 const bool& allowMissing)
35 : m_parent (parent),
36 m_targetLabel (&targetLabel),
38 m_registry (&registry),
39 m_getters (0),
40 m_allowMissing (allowMissing),
41 m_valid (false)
42{
43}
44
45
58IndexMap::IndexMap (INamedInterface* parent,
59 const std::string& targetLabel,
60 const std::vector<std::string>& targetLabels,
61 ToolHandle<ICollectionGetterRegistryTool>& registry,
62 const bool& allowMissing)
63 : m_parent (parent),
64 m_targetLabel (&targetLabel),
65 m_targetLabels (&targetLabels),
66 m_registry (&registry),
67 m_getters (0),
68 m_allowMissing (allowMissing),
69 m_valid (false)
70{
71}
72
73
80IndexMap::IndexMap (INamedInterface* parent,
81 ToolHandleArray<ICollectionGetterTool>& getters,
82 const bool& allowMissing)
83 : m_parent (parent),
84 m_targetLabel (0),
86 m_registry (0),
87 m_getters (&getters),
88 m_allowMissing (allowMissing),
89 m_valid (false)
90{
91}
92
93
98{
99 ServiceHandle<IIncidentSvc> incSvc ("IncidentSvc", "IndexMap");
100 CHECK( incSvc.retrieve() );
101 incSvc->addListener (this, "EndEvent");
102
103 if (m_registry) {
104 // Look up targets by label.
105 CHECK( m_registry->retrieve() );
106
107 if (!m_targetLabel->empty()) {
109 CHECK( (*m_registry)->get (*m_targetLabel, m_parent, target) );
110 m_targets.push_back (target);
111 }
112 if (m_targetLabels) {
113 for (size_t i = 0; i < m_targetLabels->size(); i++) {
115 CHECK( (*m_registry)->get ((*m_targetLabels)[i], m_parent, target) );
116 m_targets.push_back (target);
117 }
118 }
119 }
120 else {
121 // Use direct list of getters.
122 CHECK( m_getters->retrieve() );
123 for (size_t i = 0; i < m_getters->size(); i++)
124 m_targets.push_back (&*(*m_getters)[i]);
125 }
126
127 m_converters.resize (m_targets.size());
128 return StatusCode::SUCCESS;
129}
130
131
138{
140
141 // Configure the converters to expect the types of the
142 // target containers.
143 for (size_t i = 0; i < m_targets.size(); i++) {
144 if (m_targets[i])
145 CHECK( m_converters[i].init (m_targets[i]->elementTypeinfo(),
146 m_targets[i]->elementTypeinfo()) );
147 }
148 return StatusCode::SUCCESS;
149}
150
151
159StatusCode IndexMap::configureD3PD (const std::type_info& ti)
160{
162
163 // Configure the converters.
164 for (size_t i = 0; i < m_targets.size(); i++) {
165 if (m_targets[i])
166 CHECK( m_converters[i].init (m_targets[i]->elementTypeinfo(), ti) );
167 }
168 return StatusCode::SUCCESS;
169}
170
171
176{
177 for (size_t i = 0; i < m_targets.size(); i++) {
178 if (m_targets[i])
179 return m_targets[i];
180 }
181 return 0;
182}
183
184
189{
190 if (i >= m_targets.size())
191 return 0;
192 return m_targets[i];
193}
194
195
200{
201 int n = 0;
202 for (size_t i = 0; i < m_targets.size(); i++) {
203 if (m_targets[i])
204 ++n;
205 }
206 return n;
207}
208
209
214void IndexMap::handle (const Incident &inc)
215{
216 if (inc.type() == "EndEvent")
217 {
218 m_map.clear();
219 m_valid = false;
220 }
221}
222
223
227StatusCode IndexMap::reset()
228{
229 if (!m_valid) {
230 for (size_t i = 0; i < m_targets.size(); i++) {
232 if (target) {
233 CHECK( target->reset (m_allowMissing) );
234 int idx = 0;
235 while (const void* obj = target->nextUntyped() ) {
236 const void* cobj = m_converters[i].convertUntyped (obj);
237 if (cobj)
238 m_map.insert (std::make_pair (cobj, std::make_pair(idx++, (int)i)));
239 target->releaseObjectUntyped (obj);
240 }
241 }
242 }
243 m_valid = true;
244 }
245 return StatusCode::SUCCESS;
246}
247
248
255int IndexMap::findIndex (const void* p)
256{
257 return findIndexPair(p).first;
258}
259
260
261
270int IndexMap::findIndex (const void* p, unsigned int i)
271{
272 int ret = -1;
273 for (map_t::iterator it = m_map.find (p);
274 it != m_map.end() && it->first == p;
275 ++it)
276 {
277 if (it->second.second == static_cast<int>(i)) {
278 ret = it->second.first;
279 break;
280 }
281 }
282 return ret;
283}
284
285
286
293std::pair<int,int> IndexMap::findIndexPair (const void* p)
294{
295 map_t::iterator it = m_map.find (p);
296 std::pair<int, int> ret (-1, -1);
297 if (it == m_map.end())
298 return ret;
299
300 // In case of multiple matches, return the first (smallest index)
301 // matching container.
302 ret = it->second;
303 for (++it; it != m_map.end() && it->first == p; ++it) {
304 if (it->second.second < ret.second)
305 ret = it->second;
306 }
307
308 return ret;
309}
310
311
312
316bool IndexMap::valid() const
317{
318 return m_valid;
319}
320
321
325std::string IndexMap::formatLabels() const
326{
327 std::string out = *m_targetLabel;
328 if (m_targetLabels) {
329 for (const std::string& l : *m_targetLabels) {
330 if (!out.empty())
331 out += ",";
332 out += l;
333 }
334 }
335 return out;
336}
337
338} // namespace D3PD
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
Abstract interface to keep a registry of collection getter tools.
Abstract interface to get a collection of objects and iterate over it.
Cache pointer -> index mappings for a Getter used for index assocs.
Abstract interface to get a collection of objects and iterate over it.
ToolHandleArray< ICollectionGetterTool > * m_getters
Property for a direct list of getters.
Definition IndexMap.h:181
std::vector< ICollectionGetterTool * > m_targets
Getters defining the collections within which to index.
Definition IndexMap.h:187
const bool & m_allowMissing
Property for the allowMissing flag.
Definition IndexMap.h:184
std::vector< TypeConverter > m_converters
Converter for each target.
Definition IndexMap.h:197
ICollectionGetterTool * target()
Return the getter defining the first collection within which to index.
Definition IndexMap.cxx:175
int ntargets()
Return the number of valid targets.
Definition IndexMap.cxx:199
const std::string * m_targetLabel
Property for the label of the getter defining the target collection.
Definition IndexMap.h:171
std::string formatLabels() const
Return list of all configured targets as a comma-separated string.
Definition IndexMap.cxx:325
bool m_valid
Flag if the map is valid.
Definition IndexMap.h:194
std::pair< int, int > findIndexPair(const void *p)
Find the (index,container) pair corresponding to an element.
Definition IndexMap.cxx:293
virtual void handle(const Incident &inc)
Incident handler.
Definition IndexMap.cxx:214
StatusCode configureD3PD()
Configure during initialization: type-check.
Definition IndexMap.cxx:137
StatusCode reset()
Call before asking for an index. Rebuilds cache if needed.
Definition IndexMap.cxx:227
INamedInterface * m_parent
The parent tool.
Definition IndexMap.h:168
IndexMap(INamedInterface *parent, const std::string &targetLabel, ToolHandle< ICollectionGetterRegistryTool > &registry, const bool &allowMissing)
Constructor: for a single target.
Definition IndexMap.cxx:31
int findIndex(const void *p)
Find the index corresponding to an element.
Definition IndexMap.cxx:255
ToolHandle< ICollectionGetterRegistryTool > * m_registry
Property for the ICollectionGetterRegistryTool instance.
Definition IndexMap.h:178
bool valid() const
Return the valid flag.
Definition IndexMap.cxx:316
StatusCode configureCommon()
Common part of configuration.
Definition IndexMap.cxx:97
const std::vector< std::string > * m_targetLabels
Property for the label of the getter defining the list of target collections.
Definition IndexMap.h:175
Block filler tool for noisy FEB information.