ATLAS Offline Software
TrigBjetHypoAlgBase.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "AthViews/ViewHelper.h"
6 
7 template < class CONTAINER >
8 StatusCode TrigBjetHypoAlgBase::retrieveCollectionFromView( const EventContext& context,
9  ElementLinkVector< CONTAINER >& EL,
10  const SG::ReadHandleKey< CONTAINER >& inputKey,
11  const TrigCompositeUtils::Decision* prevDecision ) const {
12 
13  ElementLink< ViewContainer > viewEL = prevDecision->objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
14  ATH_CHECK( viewEL.isValid() );
15  ATH_MSG_DEBUG( "Retrieved View" );
16 
17  SG::ReadHandle< CONTAINER > Handle = ViewHelper::makeHandle( *viewEL, inputKey, context );
18  ATH_CHECK( Handle.isValid() );
19  ATH_MSG_DEBUG ( "EventView " << (*viewEL)->name() << " has object's container of size: " << Handle->size() );
20 
21  for ( unsigned int index(0); index < Handle->size(); index++ ) {
22  ElementLink< CONTAINER > toAdd = ViewHelper::makeLink< CONTAINER >( *viewEL, Handle, index );
23  ATH_CHECK( toAdd.isValid() );
24  EL.push_back( toAdd );
25  }
26 
27  return StatusCode::SUCCESS;
28 }
29 
30 template < class CONTAINER >
31 StatusCode TrigBjetHypoAlgBase::retrieveObjectFromNavigation( const std::string& linkName,
32  ElementLink< CONTAINER >& EL,
33  const TrigCompositeUtils::Decision* prevDecision ) const {
34 
35  const std::vector< TrigCompositeUtils::LinkInfo< CONTAINER > > myObj =
36  TrigCompositeUtils::findLinks< CONTAINER >( prevDecision, linkName.c_str(), TrigDefs::lastFeatureOfType);
37 
38  if ( myObj.size() != 1 ) {
39  ATH_MSG_ERROR( "Did not find only 1 object for link `" << linkName << "` stored in navigation!" );
40  return StatusCode::FAILURE;
41  }
42 
43  ATH_CHECK( myObj.at(0).isValid() );
44  EL = myObj.at(0).link;
45 
46  return StatusCode::SUCCESS;
47 }
48 
49 template < class CONTAINER >
50 StatusCode TrigBjetHypoAlgBase::retrieveCollectionFromNavigation( const std::string& linkName,
51  ElementLinkVector< CONTAINER >& objELs,
52  const TrigCompositeUtils::DecisionContainer* prevDecisionContainer ) const {
53 
54  for ( const TrigCompositeUtils::Decision *prevDecision : *prevDecisionContainer ) {
55  ElementLink< CONTAINER > objEL;
56  CHECK( retrieveObjectFromNavigation( linkName, objEL, prevDecision ) );
57  objELs.push_back( objEL ) ;
58  }
59 
60  return StatusCode::SUCCESS;
61 }
62 
63 
64 
65 
66 
67 
68 
69 
70 template < class CONTAINER >
71 StatusCode TrigBjetHypoAlgBase::retrieveObjectFromStoreGate( const EventContext& context,
72  ElementLinkVector< CONTAINER >& ELs,
73  const SG::ReadHandleKey< CONTAINER >& inputKey ) const {
74 
75  ATH_MSG_DEBUG( "Retrieving object from StoreGate from " << inputKey.key() );
76 
77  SG::ReadHandle< CONTAINER > ContainerHandle = SG::makeHandle( inputKey,context );
78  CHECK( ContainerHandle.isValid() );
79  const CONTAINER *Collection = ContainerHandle.get();
80 
81  for (const auto* obj : *Collection) {
82  ElementLink< CONTAINER > Link = ElementLink< CONTAINER >(*Collection, obj->index());
83  ELs.push_back( Link );
84  }
85 
86  return StatusCode::SUCCESS;
87 }
88 
89 template < class CONTAINER >
90 StatusCode TrigBjetHypoAlgBase::retrieveObjectFromEventView( const EventContext& context,
91  ElementLinkVector< CONTAINER >& ELs,
92  const SG::ReadHandleKey< CONTAINER >& inputKey,
93  const TrigCompositeUtils::DecisionContainer* prevDevisionContainer ) const {
94 
95  ATH_MSG_DEBUG( "Retrieving object from Event View from " << inputKey.key() );
96 
97  // This vector is for checking we are not reading more than once from the same View, thus retrieving the same objects multiple times!
98  std::vector< ElementLink< ViewContainer > > readViews;
99 
100  for ( const TrigCompositeUtils::Decision* previousDecision: *prevDevisionContainer ) {
101  // get View
102  ElementLink< ViewContainer > viewEL = previousDecision->objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
103  ATH_CHECK( viewEL.isValid() );
104  ATH_MSG_DEBUG( "Retrieved View" );
105 
106  bool alreadyUsed = false;
107  for ( const ElementLink< ViewContainer >& storedViews : readViews ) {
108  if ( viewEL == storedViews ) {
109  ATH_MSG_DEBUG( "We have already used this view!" );
110  alreadyUsed = true;
111  break;
112  }
113  }
114  if ( alreadyUsed ) continue;
115 
116  readViews.push_back( viewEL );
117 
118  SG::ReadHandle< CONTAINER > Handle = ViewHelper::makeHandle( *viewEL, inputKey, context );
119  ATH_CHECK( Handle.isValid() );
120  ATH_MSG_DEBUG ( "EventView " << (*viewEL)->name() << " has object's container of size: " << Handle->size() );
121 
122  size_t Counter = 0;
123  for ( auto it = Handle->begin(); it != Handle->end(); ++it, ++Counter ) {
124  ElementLink< CONTAINER > EL = ViewHelper::makeLink< CONTAINER >( *viewEL, Handle, Counter );
125  ATH_CHECK( EL.isValid() );
126  ELs.push_back( EL );
127  }
128  }
129 
130  return StatusCode::SUCCESS;
131 }
132 
133 
134 
135 
136 template< class CONTAINER >
137 StatusCode TrigBjetHypoAlgBase::attachObjectLinkToDecisionFromStoreGate( TrigCompositeUtils::Decision& outputDecision,
138  const SG::ReadHandleKey< CONTAINER >& objectKey,
139  const std::string& objLink,
140  int objIndex ) const {
141 
142  // Do the linking
143  ATH_MSG_DEBUG( "Adding object with link name '" << objLink << "'" );
144  outputDecision.setObjectLink( objLink,ElementLink< CONTAINER >( objectKey.key(),objIndex ) );
145 
146  return StatusCode::SUCCESS;
147 }
148 
149 template< class CONTAINER >
150 StatusCode TrigBjetHypoAlgBase::attachObjectLinkToDecisionsFromStoreGate( std::vector< TrigCompositeUtils::Decision* >& outputDecisions,
151  const SG::ReadHandleKey< CONTAINER >& objectKey,
152  const std::string& objLink,
153  int forcedIndex ) const {
154 
155  for ( unsigned int index(0); index < outputDecisions.size(); index++ ) {
156 
157  int objIndex = index;
158  // This is used for primary vertex (one single obj attached to every output decisions)
159  if ( forcedIndex >= 0 )
160  objIndex = forcedIndex;
161 
162  // Get the correct decisions
163  TrigCompositeUtils::Decision* outputDecision = outputDecisions.at( index );
164 
165  CHECK( attachObjectLinkToDecisionFromStoreGate( *outputDecision,
166  objectKey,
167  objLink,
168  objIndex ) );
169  }
170 
171  return StatusCode::SUCCESS;
172 }
173 
174 
175 template< class CONTAINER >
176 StatusCode TrigBjetHypoAlgBase::attachObjectLinkToDecisionFromEventView( const EventContext& context,
177  TrigCompositeUtils::Decision& outputDecision,
178  const SG::ReadHandleKey< CONTAINER >& objectKey,
179  const std::string& objLink,
180  int objIndex ) const {
181 
182  // Check navigation is set properly
183  if ( not outputDecision.hasObjectCollectionLinks( TrigCompositeUtils::seedString() ) ) {
184  ATH_MSG_ERROR( "Trying to add object link with handle key '" << objectKey.key() << "' to output decision with link name '" << objLink <<"'" );
185  ATH_MSG_ERROR( "But output decision does not have link to seed collection!" );
186  return StatusCode::FAILURE;
187  }
188 
189  // Get parent decision
190  const std::vector<ElementLink< TrigCompositeUtils::DecisionContainer >> mySeeds = outputDecision.objectCollectionLinks< TrigCompositeUtils::DecisionContainer >( TrigCompositeUtils::seedString() );
191  const TrigCompositeUtils::Decision inputDecision = **mySeeds.back();
192 
193  // Get object link from view, taken from the input decision
194  ElementLink< ViewContainer > viewEL = inputDecision.objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
195  ATH_CHECK( viewEL.isValid() );
196 
197  SG::ReadHandle< CONTAINER > calObjectHandle = ViewHelper::makeHandle( *viewEL, objectKey, context );
198  ATH_CHECK( calObjectHandle.isValid() );
199 
200  ElementLink< CONTAINER > objEL = ViewHelper::makeLink( *viewEL, calObjectHandle, objIndex );
201  ATH_CHECK( objEL.isValid() );
202 
203  // Do the linking
204  ATH_MSG_DEBUG( "Adding object with link name '" << objLink << "'" );
205  outputDecision.setObjectLink( objLink,objEL );
206 
207  return StatusCode::SUCCESS;
208 }
209 
210 template< class CONTAINER >
211 StatusCode TrigBjetHypoAlgBase::attachObjectLinkToDecisionsFromEventView( const EventContext& context,
212  std::vector< TrigCompositeUtils::Decision* >& outputDecisions,
213  const SG::ReadHandleKey< CONTAINER >& objectKey,
214  const std::string& objLink,
215  int forcedIndex ) const {
216 
217  for ( unsigned int index(0); index<outputDecisions.size(); index++ ) {
218 
219  int objIndex = index;
220  // This is used for primary vertex (one single obj attached to every output decisions)
221  if ( forcedIndex >= 0 )
222  objIndex = forcedIndex;
223 
224  // Get the correct decision
225  TrigCompositeUtils::Decision* outputDecision = outputDecisions.at( index );
226 
227  CHECK( attachObjectLinkToDecisionFromEventView( context,
228  *outputDecision,
229  objectKey,
230  objLink,
231  objIndex ) );
232  }
233 
234  return StatusCode::SUCCESS;
235 }
236 
237 template < class CONTAINER >
238 StatusCode TrigBjetHypoAlgBase::attachObjectCollectionLinkToDecisionsFromEventView( const EventContext& context,
239  std::vector< TrigCompositeUtils::Decision* >& outputDecisions,
240  const SG::ReadHandleKey< CONTAINER >& objectKey,
241  const std::string& objLink ) const {
242 
243  for ( TrigCompositeUtils::Decision* outputDecision : outputDecisions ) {
244 
245  // Check navigation is set properly
246  if ( not outputDecision->hasObjectCollectionLinks( TrigCompositeUtils::seedString() ) ) {
247  ATH_MSG_ERROR( "Trying to add object collection link with handle key '" << objectKey.key() << "' to output decision with link name '" << objLink <<"'" );
248  ATH_MSG_ERROR( "But output decision does not have link to seed collection!" );
249  return StatusCode::FAILURE;
250  }
251 
252  // Get corresponding input decision
253  const std::vector<ElementLink< TrigCompositeUtils::DecisionContainer >> mySeeds = outputDecision->objectCollectionLinks< TrigCompositeUtils::DecisionContainer >( TrigCompositeUtils::seedString() );
254  const TrigCompositeUtils::Decision* inputDecision = *mySeeds.back();
255 
256  // Get object link from view, taken from the input decision
257  ElementLink< ViewContainer > viewEL = inputDecision->objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
258  ATH_CHECK( viewEL.isValid() );
259 
260  SG::ReadHandle< CONTAINER > calObjectHandle = ViewHelper::makeHandle( *viewEL,objectKey,context );
261  ATH_CHECK( calObjectHandle.isValid() );
262 
263  // Create Collection to be linked
264  std::vector<ElementLink< CONTAINER >> objELs;
265  for ( unsigned int i(0); i<calObjectHandle->size(); i++ ) {
266  ElementLink< CONTAINER > objEL = ViewHelper::makeLink( *viewEL, calObjectHandle, i );
267  ATH_CHECK( objEL.isValid() );
268  objELs.push_back( objEL );
269  }
270 
271  // Do the linking
272  ATH_MSG_DEBUG( "Adding object collection with link name '" << objLink << "' and size " << objELs.size() );
273  outputDecision->addObjectCollectionLinks( objLink,objELs );
274  }
275 
276  return StatusCode::SUCCESS;
277 }
278 
279 
280 
281