ATLAS Offline Software
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>
10 #include "TrigNavStructure/Types.h"
12 
13 using namespace std;
14 using namespace HLT;
15 TriggerElement::TriggerElement()
16  : m_state(activeState), m_id(0), m_transient(0) {
17 }
18 
19 TriggerElement::TriggerElement(te_id_type id, bool ghost, bool nofwd)
20  : m_state(activeState), m_id(id), m_transient(0) {
21  if ( ghost )
23  if ( nofwd )
25 }
26 
28 }
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 */
37 void TriggerElement::addFeature( class_id_type clid, const ObjectIndex& index, bool forget) {
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 
55 void TriggerElement::setActiveState( bool state ) {
56  if ( m_state & nofwdState ) // this TE is immune to state changes
57  return;
58 
59  state ? m_state |= activeState : m_state &= ~activeState;
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 
82 void TriggerElement::setGhostState( bool state) {
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 
95 void 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 
111 unsigned int TriggerElement::enquireId( std::vector<uint32_t>::const_iterator& inputIt ) {
112  return unsigned(*inputIt);
113 }
114 
115 void 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 
125 void 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 
134 void 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 
182 void 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++;
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 
235  : m_subTypeIndex(invalid_sub_index), m_objIndexBegin(invalid_index), m_objIndexEnd(invalid_index) {}
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 
248  return m_subTypeIndex;
249 }
250 /*
251 uint32_t TriggerElement::ObjectIndex::objectIndex() const {
252  return m_objIndexBegin;
253 }
254 */
255 
257  return m_objIndexBegin;
258 }
259 
261  return m_objIndexEnd;
262 }
263 
265  m_objIndexBegin = begin;
266  m_objIndexEnd = end;
267 }
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 
278 void 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 
306 void 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 
343  if ( m_subTypeIndex == invalid_sub_index && m_objIndexBegin == invalid_index && m_objIndexEnd == invalid_index )
344  return false;
345  return true;
346 }
347 
348 /*
349 MsgStream& 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 
355 MsgStream& operator<< ( MsgStream& m, const HLT::TriggerElement& te ) {
356  m << "TE id: " << te.getId() << " ac: " << te.getActiveState() << " err: " << te.getErrorState();
357  return m;
358 }
359 */
HLT::TriggerElement::ghostState
@ ghostState
this TE is of type ghost/evaporating/one can't attach features to this TE
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:138
HLT::TriggerElement::ObjectIndex
Helper class for conversion from/to int stored in TE and pair of ints used in Navigation Object point...
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:75
beamspotman.r
def r
Definition: beamspotman.py:676
HLT::TriggerElement::ObjectIndex::serialize
void serialize(std::vector< uint32_t > &output) const
Definition: TrigNavStructure/Root/TriggerElement.cxx:278
HLT::TriggerElement::addFeature
void addFeature(class_id_type clid, const ObjectIndex &index, bool forget=false)
add helper for new feature
Definition: TrigNavStructure/Root/TriggerElement.cxx:37
HLT::TriggerElement::ObjectIndex::deserialize
void deserialize(std::vector< uint32_t >::const_iterator &inputIt)
Definition: TrigNavStructure/Root/TriggerElement.cxx:306
HLT::TriggerElement::Relation
Relation
possible relations enumeration (Note: uses no longer here)
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:116
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
HLT::TriggerElement::seedsRelation
@ seedsRelation
what TEs I am seeding
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:118
index
Definition: index.py:1
insertUint16ToUint32Vector
void insertUint16ToUint32Vector(std::vector< uint32_t > &v, uint16_t val, unsigned count)
Definition: TrigNavStructure/Root/TriggerElement.cxx:115
extractUint16FromUint32Vector
void extractUint16FromUint32Vector(std::vector< uint32_t >::const_iterator &it, uint16_t &val, unsigned count)
Definition: TrigNavStructure/Root/TriggerElement.cxx:125
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
skel.it
it
Definition: skel.GENtoEVGEN.py:396
NSWL1::coordinate
float coordinate(const Vertex &v)
Definition: GeoUtils.h:56
empty
bool empty(TH1 *h)
Definition: computils.cxx:295
xAOD::unsigned
unsigned
Definition: RingSetConf_v1.cxx:662
HLT::TriggerElement::seededByRelation
@ seededByRelation
what TEs are seeding me
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:119
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
HLT::TriggerElement::setActiveState
void setActiveState(bool state)
set state of the TriggerElement
Definition: TrigNavStructure/Root/TriggerElement.cxx:55
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
HLT::TriggerElement::getRelated
const std::vector< TriggerElement * > & getRelated(Relation rel) const
returns reference to the likns to other TriggerElements related by relation r
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:127
HLT::TriggerElement::setErrorState
void setErrorState(bool state=true)
Definition: TrigNavStructure/Root/TriggerElement.cxx:78
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
HLT::TriggerElement::ObjectIndex::subTypeIndex
sub_index_type subTypeIndex() const
to get collection index
Definition: TrigNavStructure/Root/TriggerElement.cxx:247
HLT::TriggerElement::ObjectIndex::objectsBegin
index_type objectsBegin() const
to get object number in th ecollection
Definition: TrigNavStructure/Root/TriggerElement.cxx:256
HLT::TriggerElement::m_state
unsigned int m_state
one word for all states, info in bits (saving space in serialized object)
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:256
HLT::TriggerElement::ObjectIndex::valid
bool valid() const
Definition: TrigNavStructure/Root/TriggerElement.cxx:342
HLT::TriggerElement::m_uses
std::vector< FeatureAccessHelper > m_uses
all features attached are in this storage
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:259
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
HLT
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
Definition: HLTResultReader.h:26
lumiFormat.i
int i
Definition: lumiFormat.py:85
HLT::TriggerElement::serialize
void serialize(std::vector< uint32_t > &output, const std::map< TriggerElement *, uint16_t > &keys, const TriggerElement *previous) const
serializes TE
Definition: TrigNavStructure/Root/TriggerElement.cxx:134
HLT::TriggerElement
TriggerElement is the basic ingreedient of the interface between HLT algorithms and the navigation It...
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:27
Types.h
TriggerElement.h
HLT::TriggerElement::relate
void relate(TriggerElement *te, Relation r)
reates given TE to other TE
Definition: TrigNavStructure/Root/TriggerElement.cxx:86
hist_file_dump.f
f
Definition: hist_file_dump.py:135
HLT::class_id_type
uint32_t class_id_type
Definition: Trigger/TrigEvent/TrigNavStructure/Root/Types.h:11
HLT::TriggerElement::enquireId
static unsigned int enquireId(std::vector< uint32_t >::const_iterator &inputIt)
Definition: TrigNavStructure/Root/TriggerElement.cxx:111
HLT::sub_index_type
uint16_t sub_index_type
Definition: Trigger/TrigEvent/TrigNavStructure/Root/Types.h:9
HLT::TriggerElement::ObjectIndex::updateBeginAndEnd
void updateBeginAndEnd(index_type begin, index_type end)
Definition: TrigNavStructure/Root/TriggerElement.cxx:264
HLT::TriggerElement::m_relations
std::map< Relation, std::vector< TriggerElement * > > m_relations
relations holder (features outside)
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:143
merge.output
output
Definition: merge.py:17
HLT::TriggerElement::m_prev
std::vector< FeatureAccessHelper > m_prev
all features attached are in this TE and seeding TEs
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:260
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
HLT::TriggerElement::~TriggerElement
~TriggerElement()
Definition: TrigNavStructure/Root/TriggerElement.cxx:27
HLT::TriggerElement::ObjectIndex::operator<
bool operator<(const ObjectIndex &obj) const
Definition: TrigNavStructure/Root/TriggerElement.cxx:269
HLT::TriggerElement::ObjectIndex::ObjectIndex
ObjectIndex()
Definition: TrigNavStructure/Root/TriggerElement.cxx:234
python.PyAthena.v
v
Definition: PyAthena.py:154
HLT::TriggerElement::ghost
bool ghost() const
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:55
HLT::TriggerElement::ObjectIndex::objectsEnd
index_type objectsEnd() const
to get object number in th ecollection
Definition: TrigNavStructure/Root/TriggerElement.cxx:260
HLT::TriggerElement::setGhostState
void setGhostState(bool t=true)
Definition: TrigNavStructure/Root/TriggerElement.cxx:82
HLT::TriggerElement::FeatureAccessHelper
the FeatureAccessHelper is a class used to keep track of features attached to this TE.
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:192
HLT::TriggerElement::TriggerElement
TriggerElement()
Definition: TrigNavStructure/Root/TriggerElement.cxx:15
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
HLT::TriggerElement::nofwdState
@ nofwdState
this TE is of type which stops fwd deactivation
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:139
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
HLT::TriggerElement::deserialize
void deserialize(std::vector< uint32_t >::const_iterator &inputIt, const std::map< uint16_t, TriggerElement * > &keys, const TriggerElement *previous)
deserialization
Definition: TrigNavStructure/Root/TriggerElement.cxx:182
HLT::TriggerElement::errorState
@ errorState
somewhere in the algorithms dealing with this TE there was an error
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:140
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
python.PyAthena.obj
obj
Definition: PyAthena.py:132
HLT::TriggerElement::activeState
@ activeState
this bit is keeping active/inactive state of TE
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:137
HLT::index_type
uint32_t index_type
Definition: Trigger/TrigEvent/TrigNavStructure/Root/Types.h:10
HLT::TriggerElement::ObjectIndex::isSameOrWithin
bool isSameOrWithin(const ObjectIndex *idx) const
check if idx is the same as this or is within this index
Definition: TrigNavStructure/Root/TriggerElement.cxx:332
HLT::TriggerElement::getPreviousFeatures
const std::vector< FeatureAccessHelper > & getPreviousFeatures() const
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:251
HLT::TriggerElement::m_id
te_id_type m_id
id
Definition: TrigNavStructure/TrigNavStructure/TriggerElement.h:257
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
HLT::te_id_type
uint32_t te_id_type
Definition: Trigger/TrigEvent/TrigNavStructure/TrigNavStructure/Types.h:11