ATLAS Offline Software
Loading...
Searching...
No Matches
TrigNavStructure/Root/TriggerElement.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <iostream>
6#include <stdexcept>
7#include <iterator>
8#include <functional>
9#include <algorithm>
12
13using namespace std;
14using namespace HLT;
18
21 if ( ghost )
23 if ( nofwd )
25}
26
29/*
30 void TriggerElement::reset() {
31 //
32 m_uses.clear();
33 m_prev.clear();
34 m_relations.clear(); //< do not worry! map is clearing recursivelly lists also
35 }
36*/
38 addFeature(FeatureAccessHelper(clid, index, forget));
39}
40
42 const static size_t max_features_per_te = 0xfff;
43 if ( m_uses.size() == max_features_per_te-1 ) {
44 std::cout << "ERROR - count of features per TE exceeds the limitation, further attach operations are ignored" << std::endl;
45 }
46 if ( m_uses.size() == max_features_per_te ) {
47 return;
48 }
49 // if ( m_uses.size() > 100 )
50 // std::cout << "INFO - many features per TE " << f.getCLID() << std::endl;
51 m_uses.push_back( f );
52 m_prev.push_back( f );
53}
54
56 if ( m_state & nofwdState ) // this TE is immune to state changes
57 return;
58
60 // std::cerr << "Deactivating " << m_id << std::endl;
61 // propagate forward the state of this TE if deactivated
62 // if activated then that attempt is illogical, theredore not done
63
64 if ( state == true )
65 return;
66
67 // this node is terminal or it is special TE type which is holding fwd deactivation
69 return;
70
71 // std::cerr << "Propagating deactivation " << std::endl;
72 std::vector<TriggerElement*>::const_iterator it;
73 for ( it = getRelated(seedsRelation).begin(); it != getRelated(seedsRelation).end(); ++it ) {
74 (*it)->setActiveState(false);
75 }
76}
77
79 state ? m_state |= errorState : m_state &= ~errorState;
80}
81
83 state ? m_state |= ghostState : m_state &= ~ghostState;
84}
85
87 // secure form multiple relations of the same type
88 // silently ignoring if already related
89 // his helps if one wants the Topo TE to merge several RoIs
90 if ( find(m_relations[r].begin(), m_relations[r].end(), te) == m_relations[r].end() )
91 m_relations[r].push_back(te);
92}
93
94
95void TriggerElement::relate( const std::vector<TriggerElement*>& tes, Relation r ) {
96 // secure form multiple relations of the same type
97 std::vector<TriggerElement*>::const_iterator it;
98 for ( it = tes.begin(); it != tes.end(); ++it ) {
99 relate(*it, r);
100 }
101}
102
103
104
105/*****************************************************************************
106 *
107 * SERIALIZATION
108 *
109 *****************************************************************************/
110
111unsigned int TriggerElement::enquireId( std::vector<uint32_t>::const_iterator& inputIt ) {
112 return unsigned(*inputIt);
113}
114
115void insertUint16ToUint32Vector(std::vector<uint32_t>& v, uint16_t val, unsigned count) {
116 uint32_t s=0;
117 if (count % 2 == 0 ) {
118 s = (uint32_t(val) << 16) & 0xffff0000;
119 v.push_back(s);
120 } else {
121 v.back() |= uint32_t(val & 0xffff);
122 }
123}
124
125void extractUint16FromUint32Vector( std::vector<uint32_t>::const_iterator& it, uint16_t& val, unsigned count) {
126 if ( count % 2 == 0 ) { // do need to increment iterator
127 val = ((*it) >> 16 ) & 0xffff;
128 } else {
129 val = (*it) & 0xffff;
130 ++it;
131 }
132}
133
134void TriggerElement::serialize( std::vector<uint32_t>& output, const std::map<TriggerElement*, uint16_t>& keys,
135 const TriggerElement* /*previous*/ ) const {
136
137 output.push_back(m_id); // record the TE id first
138 unsigned summaryIndex = output.size(); // reserve space for vaious caounters and state
139 output.push_back(0);
140
141 auto it = m_relations.find (seededByRelation);
142 std::vector<TriggerElement*> dumvec;
143 const std::vector<TriggerElement*>& relvec =
144 (it != m_relations.end() ? it->second : dumvec);
145
146 std::vector<TriggerElement*>::const_iterator teIt;
147 unsigned relationsCount = 0;
148 // go over all TE which seed me and record link to them
149 for ( teIt = relvec.begin(); teIt != relvec.end(); ++teIt ) {
150 std::map<TriggerElement*, uint16_t>::const_iterator key = keys.find(*teIt);
151 if ( key != keys.end() ) {
152 insertUint16ToUint32Vector(output, key->second, relationsCount);
153 relationsCount++;
154 }
155 // other are forgotten silently .... joke, there will be none
156 // due to fact that the to create TE one needs to know all the TEs seeding it
157 // so they are before on the list and in the "keys" map
158 }
159
160 // here one should put only this features which are not to to be forget (we do not use this now)
161 std::vector< FeatureAccessHelper >::const_iterator featuresIt;
162 unsigned featuresCount = 0;
163
164 for ( featuresIt = m_uses.begin(); featuresIt != m_uses.end(); ++featuresIt ) {
165 if ( ! featuresIt->forget() ) {
166 output.push_back ( featuresIt->getCLID() );
167 featuresIt->getIndex().serialize(output);
168 featuresCount++;
169 }
170 }
171 uint32_t seedingUsesStateWord = ((relvec.size() << 20)) | (featuresCount << 8) | (m_state & 0xf);
172 output[summaryIndex] = seedingUsesStateWord;
173 /*
174 std::cerr << "Serialized TE id: " << m_id << " fea: " << featuresCount
175 << " forg: " << m_uses.size() - featuresCount
176 << " rel: "<< m_relations[seededByRelation].size() << " raw: " << seedingUsesStateWord<< std::endl;
177 */
178}
179
180
181
182void TriggerElement::deserialize( std::vector<uint32_t>::const_iterator& inputIt,
183 const std::map<uint16_t, TriggerElement*>& keys, const TriggerElement* /*previous*/) {
184
185 m_id = *inputIt++;
186 uint32_t seedingUsesStateWord = *inputIt++;
187 m_state = seedingUsesStateWord & 0xf;
188 unsigned int size = (seedingUsesStateWord & 0xfff00000) >> 20;
189 unsigned int featuresAttached = (seedingUsesStateWord & 0x000fff00) >> 8;
190 // std::cerr << "Try DeSerialize TE id: " << m_id << " fea: " << featuresAttached << " rel: "<< size << " raw " << seedingUsesStateWord << std::endl;
191
192
193
194 std::map<uint16_t, TriggerElement*>::const_iterator id;
195 unsigned relationsCount = 0;
196 uint16_t key{0};
197 for ( unsigned int i = 0 ; i < size; ++i ) {
198 extractUint16FromUint32Vector(inputIt, key, relationsCount); // extract the key from the payload
199 ++relationsCount;
200 // look for that key
201 id = keys.find(key);
202 if ( id != keys.end() ) {
203 TriggerElement* te = id->second;
204 // relate ( te, seededByRelation );
205 te->relate ( this, seedsRelation );
206 this->relate ( te, seededByRelation ); // seededBy and seeds are reflexive ...
207 m_prev.reserve(m_prev.size() + te->getPreviousFeatures().size());
208 m_prev.insert(m_prev.end(), te->getPreviousFeatures().begin(), te->getPreviousFeatures().end() ); // rebuild previous features list
209 }
210 }
211 if ( relationsCount %2 == 1 ) inputIt++; // we need to do this because if number of features is odd the payload iterator is not incremented by extract...
212
213 // unpack features holders info
214 m_uses.reserve(m_uses.size() + featuresAttached);
215 m_prev.reserve(m_prev.size() + featuresAttached);
216 for ( unsigned int i = 0 ; i < featuresAttached; ++i ) {
217 unsigned int clid = *inputIt++;
218 ObjectIndex coordinate;
219 coordinate.deserialize(inputIt);
220 addFeature(clid, coordinate);
221 }
222}
223
224/*
225 void TriggerElement::appendFeaturesKeyPrint(const std::vector<FeatureAccessHelper>& k) {
226 m_keyPrint.insert(m_keyPrint.end(), k.begin(), k.end());
227 }
228*/
229
231//
232// Object index
233
236
237
239 : m_subTypeIndex(subType), m_objIndexBegin(begin), m_objIndexEnd(end)
240{
241 if (subType >= invalid_sub_index)
242 throw std::runtime_error("To many collections of one type: " + std::to_string(subType)+" >= 0xffff" );
243}
244
245
246
250/*
251uint32_t TriggerElement::ObjectIndex::objectIndex() const {
252 return m_objIndexBegin;
253}
254*/
255
259
261 return m_objIndexEnd;
262}
263
268
270 if ( m_subTypeIndex != obj.m_subTypeIndex )
271 return m_subTypeIndex < obj.m_subTypeIndex;
272 if ( m_objIndexBegin != obj.m_objIndexBegin )
273 return m_objIndexBegin < obj.m_objIndexBegin;
274
275 return m_objIndexEnd < obj.m_objIndexEnd;
276}
277
278void TriggerElement::ObjectIndex::serialize(std::vector<uint32_t>& output) const {
279 // here comec the compression
280 // check if we can fit into one int
281 // ie. subtypeIndex fits into the 4 bits (0-8)
282 // if the begin indexes fit into the 13 bits (0-8192 )
283 // if the end indexes fit into the 14 bits (0-16383 )
284
285 if ( (0 == (m_subTypeIndex & ~0xf)) && (0 == (objectsBegin() & ~0x1fff)) && ( 0 == (objectsEnd() & ~0x3fff)) ) {
286 output.push_back(0x80000000 | m_subTypeIndex | objectsEnd()<< 4 | objectsBegin() << 18);
287 // std::cerr << "feature idx ser: " << std::hex << output.back() << std::dec << std::endl;
288
289 } else if ( (0 == (objectsBegin() & ~0xffff)) && (0 == (objectsEnd() & ~0xffff)) ) {
290 // check if we can fit into two ints
291 // ie. subtypeIndex fits into the 16 bits
292 // if the begin indexes fit into the 16 bits (0-65536 )
293 // if the end indexes fit into the 16 bits (0-65536)
294
295 // std::cerr << "feature idx2: " << std::hex << m_subTypeIndex << " " << m_objIndex << std::dec << std::endl;
296 output.push_back(m_subTypeIndex);
297 output.push_back((objectsBegin() << 16) | (objectsEnd()) );
298 } else {
299 // we have to use 3 words
300 output.push_back(m_subTypeIndex | 0x40000000); // second most significant bit marks that
301 output.push_back(objectsBegin());
302 output.push_back(objectsEnd());
303 }
304}
305
306void TriggerElement::ObjectIndex::deserialize( std::vector<uint32_t>::const_iterator& inputIt) {
307 // check if compressed reference
308 uint32_t w = *inputIt++; // pickup and advance
309 if ( w & 0x80000000 ) {
310 // small numbers (compressed to one word)
311 m_subTypeIndex = w & 0xf;
312 m_objIndexBegin = ( (w>>18) & 0x1fff );
313 m_objIndexEnd = (w>>4) & 0x3fff;
314 // std::cerr << "feature idx deser: " << std::hex << w << std::dec << " " << std::endl;
315 } else if ( (w & 0x40000000) == 0 ) {
316 // medium numbers (compressed to 2 words)
317 m_subTypeIndex = w & 0xffff;
318 uint32_t w2 = *inputIt++;
319 m_objIndexBegin = w2 >> 16;
320 m_objIndexEnd = w2 & 0xffff;
321 // std::cerr << "feature idx2: " << std::hex << m_subTypeIndex << " " << m_objIndex << std::dec << std::endl;
322 } else {
323 // huge numbers (3 word involved)
324 m_subTypeIndex = w & 0xffff;
325 m_objIndexBegin = *inputIt++;
326 m_objIndexEnd = *inputIt++;
327
328 }
329 // at the end the inputIt is pointing outside the ObjectIndex
330}
331
333 if ( this->subTypeIndex() != idx->subTypeIndex() )
334 return false;
335 if ( this->objectsBegin() > idx->objectsBegin() )
336 return false;
337 if ( this->objectsEnd() < idx->objectsEnd() )
338 return false;
339 return true;
340}
341
344 return false;
345 return true;
346}
347
348/*
349MsgStream& operator<< ( MsgStream& m, const HLT::TriggerElement::ObjectIndex& i ) {
350 m << "SubTypeIdx: " << i.subTypeIndex() << " begin: " << i.objectsBegin() << " end: " << i.objectsEnd();
351 return m;
352
353}
354
355MsgStream& operator<< ( MsgStream& m, const HLT::TriggerElement& te ) {
356 m << "TE id: " << te.getId() << " ac: " << te.getActiveState() << " err: " << te.getErrorState();
357 return m;
358}
359*/
void insertUint16ToUint32Vector(std::vector< uint32_t > &v, uint16_t val, unsigned count)
void extractUint16FromUint32Vector(std::vector< uint32_t >::const_iterator &it, uint16_t &val, unsigned count)
static const Attributes_t empty
the FeatureAccessHelper is a class used to keep track of features attached to this TE.
Helper class for conversion from/to int stored in TE and pair of ints used in Navigation Object point...
void serialize(std::vector< uint32_t > &output) const
sub_index_type subTypeIndex() const
to get collection index
index_type objectsEnd() const
to get object number in th ecollection
index_type m_objIndexEnd
auxiliary index word for big features
void updateBeginAndEnd(index_type begin, index_type end)
index_type objectsBegin() const
to get object number in th ecollection
void deserialize(std::vector< uint32_t >::const_iterator &inputIt)
index_type m_objIndexBegin
auxiliary index word for big features
bool isSameOrWithin(const ObjectIndex *idx) const
check if idx is the same as this or is within this index
std::vector< FeatureAccessHelper > m_uses
all features attached are in this storage
@ nofwdState
this TE is of type which stops fwd deactivation
@ ghostState
this TE is of type ghost/evaporating/one can't attach features to this TE
@ activeState
this bit is keeping active/inactive state of TE
@ errorState
somewhere in the algorithms dealing with this TE there was an error
unsigned int m_state
one word for all states, info in bits (saving space in serialized object)
static unsigned int enquireId(std::vector< uint32_t >::const_iterator &inputIt)
Relation
possible relations enumeration (Note: uses no longer here)
void serialize(std::vector< uint32_t > &output, const std::map< TriggerElement *, uint16_t > &keys, const TriggerElement *previous) const
serializes TE
const std::vector< TriggerElement * > & getRelated(Relation rel) const
returns reference to the likns to other TriggerElements related by relation r
bool m_transient
marker that TE should not be serialized
void addFeature(class_id_type clid, const ObjectIndex &index, bool forget=false)
add helper for new feature
std::vector< FeatureAccessHelper > m_prev
all features attached are in this TE and seeding TEs
const std::vector< FeatureAccessHelper > & getPreviousFeatures() const
void deserialize(std::vector< uint32_t >::const_iterator &inputIt, const std::map< uint16_t, TriggerElement * > &keys, const TriggerElement *previous)
deserialization
void setActiveState(bool state)
set state of the TriggerElement
void relate(TriggerElement *te, Relation r)
reates given TE to other TE
std::map< Relation, std::vector< TriggerElement * > > m_relations
relations holder (features outside)
int r
Definition globals.cxx:22
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
static const index_type invalid_index
const FeatureContainerInit< FEATURE, CONTAINER > RegisterFeatureContainerTypes< FEATURE, CONTAINER >::s
static const index_type invalid_sub_index
Definition index.py:1
STL namespace.