ATLAS Offline Software
TrigMatchToolCore.icc
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // $Id$
8 #ifndef TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC
9 #define TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC
10 
11 // STL include(s):
12 #include <memory>
13 
14 #include "TrigSteeringEvent/TrigRoiDescriptorCollection.h"
15 
16 /************************************/
17 /* Public Functions */
18 /************************************/
19 
20 template< typename trigType >
21 std::vector< const trigType* >
22 TrigMatchToolCore::getTriggerObjects( const std::string& chainName,
23  bool onlyPassedFeatures ) const
24 {
25  std::vector< const trigType* > trigObjects;
26  this->getObjects( trigObjects, chainNameToIndex (chainName),
27  onlyPassedFeatures );
28  return trigObjects;
29 }
30 
31 template< typename trigType >
32 std::vector< const trigType* >
33 TrigMatchToolCore::getTriggerObjects( size_t chainIndex,
34  bool onlyPassedFeatures ) const {
35 
36  std::vector< const trigType* > trigObjects;
37 
38  // grab the features of type trigType from the chain
39  this->getObjects( trigObjects, chainIndex, onlyPassedFeatures );
40 
41  return trigObjects;
42 }
43 
44 template< typename trigType, typename baseType >
45 std::vector< const trigType* >
46 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
47  const std::string& chainName,
48  float maxDistance,
49  bool onlyPassedFeatures,
50  const DistanceFunctor< trigType,
51  baseType > *metric ) const
52 {
53  return matchToTriggerObjects (baseObject,
54  chainNameToIndex (chainName),
55  maxDistance,
56  onlyPassedFeatures,
57  metric);
58 }
59 
60 template< typename trigType, typename baseType >
61 std::vector< const trigType* >
62 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
63  size_t chainIndex,
64  float maxDistance,
65  bool onlyPassedFeatures,
66  const DistanceFunctor< trigType,
67  baseType > *metric ) const {
68 
69  if( ! baseObject ) {
70  return std::vector<const trigType*>();
71  }
72 
73  // match the collected trigger objects to the match object
74  return this->matchToObjects< trigType, baseType >( baseObject,
75  this->getTriggerObjects< trigType >( chainIndex, onlyPassedFeatures ),
76  maxDistance, metric );
77 }
78 
79 template< typename trigType, typename baseType >
80 std::vector< const trigType* >
81 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
82  const std::string& chainName,
83  float maxDistance,
84  bool onlyPassedFeatures ) const {
85 
86  //prepare the default metric and ship work to the main version of function
87  return this->matchToTriggerObjects< trigType, baseType >( baseObject, chainName,
88  maxDistance, onlyPassedFeatures, prepareMetric< trigType,baseType >() );
89 }
90 
91 template< typename trigType, typename baseType >
92 const trigType*
93 TrigMatchToolCore::matchToTriggerObject( const baseType* baseObject,
94  const std::string& chainName,
95  float maxDistance,
96  bool onlyPassedFeatures,
97  const DistanceFunctor< trigType, baseType >* metric) const {
98 
99  // grab all the matches, and if you had 1 or more, return
100  // the best one. Note that the matches will be sorted by
101  // distance.
102  std::vector< const trigType* > passedObjects =
103  this->matchToTriggerObjects< trigType, baseType >( baseObject, chainName,
104  maxDistance, onlyPassedFeatures, metric );
105 
106  const trigType *best = 0;
107 
108  if( passedObjects.size() ) {
109  best = passedObjects[ 0 ];
110  }
111 
112  return best;
113 }
114 
115 template< typename trigType, typename baseType >
116 const trigType*
117 TrigMatchToolCore::matchToTriggerObject( const baseType* baseObject,
118  const std::string& chainName,
119  float maxDistance,
120  bool onlyPassedFeatures ) const {
121 
122  //prepare the default metric and ship work to the main version of function
123  return this->matchToTriggerObject( baseObject, chainName, maxDistance,
124  onlyPassedFeatures, prepareMetric< trigType,baseType >() );
125 }
126 
127 template< typename trigType, typename baseType >
128 std::vector< const trigType* >
129 TrigMatchToolCore::matchedTriggerObjects( const DataVector<baseType>& baseObjects,
130  const std::string& chainName,
131  float maxDistance,
132  bool onlyBestMatch,
133  bool onlyPassedFeatures,
134  const DistanceFunctor< trigType, baseType >* metric ) const {
135 
136  std::vector< const baseType* > obs;
137  obs.insert( obs.begin(), baseObjects.begin(), baseObjects.end() );
138 
139  return this->matchedTriggerObjects< trigType, baseType >( obs, chainName,
140  maxDistance, onlyBestMatch, onlyPassedFeatures, metric );
141 }
142 
143 template< typename trigType, typename baseType >
144 std::vector< const trigType* >
145 TrigMatchToolCore::matchedTriggerObjects( const DataVector<baseType>& baseObjects,
146  const std::string& chainName,
147  float maxDistance,
148  bool onlyBestMatch,
149  bool onlyPassedFeatures ) const {
150 
151  std::vector< const baseType* > obs;
152  obs.insert( obs.begin(), baseObjects.begin(), baseObjects.end() );
153 
154  return this->matchedTriggerObjects< trigType, baseType >( obs, chainName,
155  maxDistance, onlyBestMatch, onlyPassedFeatures );
156 }
157 
158 template < typename trigType, typename baseType >
159 std::vector< const trigType* >
160 TrigMatchToolCore::matchedTriggerObjects( const std::vector< const baseType* >& baseObjects,
161  const std::string& chainName,
162  float maxDistance,
163  bool onlyBestMatch,
164  bool onlyPassedFeatures,
165  const DistanceFunctor< trigType, baseType >* metric ) const {
166 
167  std::set< const trigType* > matches;
168 
169  typename std::vector< const baseType* >::const_iterator iter = baseObjects.begin();
170  typename std::vector< const baseType* >::const_iterator end = baseObjects.end();
171  for( ; iter != end; ++iter ) {
172  if( onlyBestMatch ) {
173  const trigType* match =
174  this->matchToTriggerObject< trigType >( *iter, chainName,
175  maxDistance, onlyPassedFeatures,
176  metric );
177  if( match ) {
178  matches.insert(match);
179  }
180  } else {
181  std::vector< const trigType* > match =
182  this->matchToTriggerObjects< trigType >( *iter, chainName,
183  maxDistance, onlyPassedFeatures,
184  metric );
185  matches.insert( match.begin(), match.end() );
186  }
187  }
188 
189  std::vector< const trigType* > result;
190  result.insert( result.begin(), matches.begin(), matches.end() );
191 
192  return result;
193 }
194 
195 // matching using the default metric - same as above except
196 // will use match using deltaR, so no metric is needed
197 template< typename trigType, typename baseType >
198 std::vector< const trigType* >
199 TrigMatchToolCore::matchedTriggerObjects( const std::vector< const baseType* >& baseObjects,
200  const std::string& chainName,
201  float maxDistance,
202  bool onlyBestMatch,
203  bool onlyPassedFeatures ) const {
204 
205  //prepare the default metric and ship work to the main version of function
206  return this->matchedTriggerObjects< trigType, baseType>( baseObjects, chainName,
207  maxDistance, onlyBestMatch, onlyPassedFeatures,
208  prepareMetric< trigType, baseType >() );
209 }
210 
211 template< typename trigType, typename baseType >
212 std::vector< const trigType* >
213 TrigMatchToolCore::unmatchedTriggerObjects( const DataVector<baseType>& baseObjects,
214  const std::string& chainName,
215  float maxDistance,
216  bool onlyBestMatch,
217  bool onlyPassedFeatures,
218  const DistanceFunctor< trigType, baseType >* metric) const {
219 
220  std::vector< const baseType* > objs;
221  objs.insert( objs.begin(), baseObjects.begin(), baseObjects.end() );
222 
223  return this->unmatchedTriggerObjects< trigType,baseType >( objs, chainName,
224  maxDistance, onlyBestMatch, onlyPassedFeatures, metric );
225 }
226 
227 template< typename trigType, typename baseType >
228 std::vector< const trigType* >
229 TrigMatchToolCore::unmatchedTriggerObjects( const DataVector< baseType >& baseObjects,
230  const std::string& chainName,
231  float maxDistance,
232  bool onlyBestMatch,
233  bool onlyPassedFeatures ) const {
234 
235  std::vector< const baseType* > objs;
236  objs.insert( objs.begin(), baseObjects.begin(), baseObjects.end() );
237 
238  return this->unmatchedTriggerObjects< trigType, baseType >( objs, chainName,
239  maxDistance, onlyBestMatch, onlyPassedFeatures );
240 }
241 
242 template< typename trigType, typename baseType >
243 std::vector< const trigType* >
244 TrigMatchToolCore::unmatchedTriggerObjects( const std::vector< const baseType* >& baseObjects,
245  const std::string& chainName,
246  float maxDistance,
247  bool onlyBestMatch,
248  bool onlyPassedFeatures,
249  const DistanceFunctor< trigType, baseType >* metric ) const {
250 
251  // grab all the features
252  const std::vector< const trigType* > trigObjects =
253  this->getTriggerObjects< trigType >( chainName, onlyPassedFeatures );
254 
255  // grab the matched objects
256  const std::vector< const trigType* > matched =
257  this->matchedTriggerObjects< trigType,baseType >( baseObjects, chainName,
258  maxDistance, onlyBestMatch,
259  onlyPassedFeatures, metric );
260 
261  // build the result;
262  std::vector< const trigType* > results;
263  typename std::vector< const trigType* >::const_iterator iter = trigObjects.begin();
264  typename std::vector< const trigType* >::const_iterator end = trigObjects.end();
265  for( ; iter != end; ++iter ) {
266  // clean out the bad pointers
267  if( ! ( *iter ) ) continue;
268 
269  if( std::find( matched.begin(), matched.end(), *iter ) == matched.end() ) {
270  results.push_back( *iter );
271  }
272  }
273 
274  return results;
275 }
276 
277 // matching using the default metric - same as above except
278 // will use match using deltaR, so no metric is needed
279 template< typename trigType, typename baseType >
280 std::vector< const trigType* >
281 TrigMatchToolCore::unmatchedTriggerObjects( const std::vector< const baseType* >& baseObjects,
282  const std::string& chainName,
283  float maxDistance,
284  bool onlyBestMatch,
285  bool onlyPassedFeatures ) const {
286 
287  //prepare the default metric and ship work to the main version of function
288  return this->unmatchedTriggerObjects< trigType, baseType >( baseObjects, chainName,
289  maxDistance, onlyBestMatch,
290  onlyPassedFeatures, prepareMetric< trigType, baseType >() );
291 }
292 
293 template< typename trigType, typename baseType >
294 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
295  const std::string& chainName,
296  float maxDistance,
297  const DistanceFunctor< trigType, baseType >* metric ) const
298 {
299  return chainPassedByObject (baseObject,
300  chainNameToIndex (chainName),
301  maxDistance,
302  metric);
303 }
304 
305 template< typename trigType, typename baseType >
306 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
307  size_t chainIndex,
308  float maxDistance,
309  const DistanceFunctor< trigType, baseType >* metric ) const
310 {
311  if (!baseObject) return false;
312 
313  std::vector<const trigType*> v =
314  this->getTriggerObjects<trigType> (chainIndex, true);
315  return this->anyMatch (baseObject, v, maxDistance, metric);
316 }
317 
318 template< typename trigType, typename baseType >
319 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
320  const std::string& chainName,
321  float maxDistance ) const {
322 
323  // prepare the default metric, and offload it to the main version
324  // of the function
325  return this->chainPassedByObject( baseObject, chainName, maxDistance,
326  prepareMetric< trigType, baseType >() );
327 }
328 
329 template< typename trigType, typename baseType >
330 std::vector< std::string >
331 TrigMatchToolCore::chainsPassedByObject( const baseType* baseObject,
332  float maxDistance,
333  const DistanceFunctor< trigType, baseType >* metric) const {
334 
335  // loop through all chains and check if there are any features
336  // matching the passed object
337  std::vector< std::string > passedChains;
338 
339  std::vector< std::string > configuredChains = m_chainNameIndex.configuredChainNames();
340  for (size_t i = 0; i < configuredChains.size(); i++) {
341  if( this->chainPassedByObject< trigType, baseType >( baseObject,
342  i,
343  maxDistance, metric ) ) {
344  passedChains.push_back( configuredChains[i] );
345  }
346  }
347 
348  return passedChains;
349 }
350 
351 template <typename trigType, typename baseType>
352 std::vector< std::string >
353 TrigMatchToolCore::chainsPassedByObject( const baseType* baseObject,
354  float maxDistance ) const {
355 
356  // prepare the default metric and ship the work off to the main version
357  // of the function
358  return this->chainsPassedByObject( baseObject, maxDistance,
359  prepareMetric< trigType, baseType >() );
360 }
361 
362 template< typename trigType, typename baseType >
363 std::vector< const baseType* >
364 TrigMatchToolCore::objectsInChain( const std::vector< const baseType* >& baseObjects,
365  const std::string& chainName,
366  bool onlyPassedFeatures,
367  float maxDistance,
368  const DistanceFunctor< trigType, baseType >* metric ) const {
369 
370  // loop through reco objects supplied to see if any match objects
371  // in the specified chain
372  std::vector< const baseType* > matchedBaseObjects;
373 
374  typename std::vector< const baseType* >::const_iterator iter = baseObjects.begin();
375  typename std::vector< const baseType* >::const_iterator end = baseObjects.end();
376  for( ; iter != end; ++iter ) {
377  const std::vector< const trigType* > matches =
378  this->matchToTriggerObjects( *iter, chainName, maxDistance,
379  onlyPassedFeatures, metric );
380  if( matches.size() ) {
381  matchedBaseObjects.push_back( *iter );
382  }
383  }
384 
385  return matchedBaseObjects;
386 }
387 
388 template< typename trigType, typename baseType >
389 std::vector< const baseType* >
390 TrigMatchToolCore::objectsInChain( const std::vector< const baseType* >& baseObjects,
391  const std::string& chainName,
392  bool onlyPassedFeatures,
393  float maxDistance ) const {
394 
395  // prepare the default metric and ship the work off to the main
396  // version of the function
397  return this->objectsInChain( baseObjects, chainName, onlyPassedFeatures,
398  maxDistance, prepareMetric< trigType, baseType >() );
399 }
400 
401 template< typename trigType, typename baseType >
402 std::map< const trigType*, std::vector< std::string > >
403 TrigMatchToolCore::matchToAllTriggerObjects( const baseType* baseObject,
404  float maxDistance,
405  bool onlyPassedFeatures,
406  const DistanceFunctor< trigType, baseType >* metric ) const {
407 
408  // build map for storing matching trigger objects
409  std::map< const trigType*, std::vector< std::string > > matchMap;
410 
411  // loop through all configured chains
412  for (const std::string& chainName : m_chainNameIndex.configuredChainNames()){
413  // grab the matching objects from this chain
414  std::vector< const trigType* > matchedObjects =
415  this->matchToTriggerObjects< trigType, baseType >(
416  baseObject, chainName, maxDistance, onlyPassedFeatures, metric );
417  // add them to the map
418  typename std::vector< const trigType* >::const_iterator matchIter = matchedObjects.begin();
419  typename std::vector< const trigType* >::const_iterator matchEnd = matchedObjects.end();
420  for( ; matchIter != matchEnd; ++matchIter ) {
421  matchMap[ *matchIter ].push_back( chainName );
422  }
423  }
424 
425  return matchMap;
426 }
427 
428 template< typename trigType, typename baseType >
429 std::map< const trigType*, std::vector< std::string > >
430 TrigMatchToolCore::matchToAllTriggerObjects( const baseType* baseObject,
431  float maxDistance,
432  bool onlyPassedFeatures ) const {
433 
434  // prepare the default metric and ship the work off to the main function
435  return this->matchToAllTriggerObjects( baseObject, maxDistance, onlyPassedFeatures,
436  prepareMetric< trigType, baseType >() );
437 }
438 
439 /************************************/
440 /* Private Functions */
441 /************************************/
442 template<typename trigType>
443 void TrigMatchToolCore::getObjects( std::vector< const trigType* >& objects,
444  const std::string& chainName,
445  bool onlyPassedFeatures) const
446 {
447  this->getObjects (objects, chainNameToIndex (chainName), onlyPassedFeatures);
448 }
449 
450 
451 template<typename trigType>
452 void TrigMatchToolCore::getObjects( std::vector< const trigType* >& objects,
453  size_t chainIndex,
454  bool onlyPassedFeatures ) const {
455 
456  SlotCache& slotCache = *m_slotCache;
457  SlotCache::lock_t lock (slotCache.m_mutex);
458 
459  // check if we are able to use the cache
460  if( this->changedDecisionAware() ) {
461 
462  static const int type_key = m_typeMap.key (&typeid(trigType));
463  TrigFeatureCache< trigType >& cache = this->getCache<trigType> (type_key,
464  slotCache,
465  lock);
466 
467  // check if its already in the cache - if not, add it
468  if (cache.get (chainIndex, onlyPassedFeatures, objects)) {
469  return;
470  }
471  else {
472  // its not in the cache, so build it normally and add it to the cache
473  const typename TrigMatch::ClassTraits<trigType>::type* traits = 0;
474 
475  // try it directly first
476  const Trig::FeatureContainer& trigContainer =
477  this->getCachedFeatureContainer( chainIndex, slotCache, lock );
478  this->collectObjects( slotCache.m_featureLabel,
479  objects, trigContainer, onlyPassedFeatures, traits );
480 
481  // if the chain is propagatable, and nothing has been found, try that
482  if( objects.size() == 0 ) {
483  std::string chainName = m_chainNameIndex.chainName(chainIndex);
484  const std::string propagatedName = this->propagateChainNames( chainName, traits );
485  if( propagatedName != chainName ) {
486  size_t propagatedIndex = this->chainNameToIndex (propagatedName);
487  const Trig::FeatureContainer& trigPropContainer =
488  this->getCachedFeatureContainer( propagatedIndex, slotCache, lock );
489  this->collectObjects( slotCache.m_featureLabel,
490  objects, trigPropContainer, onlyPassedFeatures, traits );
491  }
492  }
493  cache.add( chainIndex, onlyPassedFeatures, objects );
494  return;
495  }
496  }
497 
498  // can't use the cache, so just run it normally
499  std::string chainName = m_chainNameIndex.chainName(chainIndex);
500 
501  const typename TrigMatch::ClassTraits<trigType>::type* traits = 0;
502  const Trig::FeatureContainer trigContainer =
503  this->getFeatureContainer( chainName, TrigDefs::alsoDeactivateTEs );
504  this->collectObjects( slotCache.m_featureLabel,
505  objects, trigContainer, onlyPassedFeatures, traits );
506 
507  return;
508 }
509 
510 template< typename trigType >
511 void TrigMatchToolCore::collectObjects( const std::string& featureLabel,
512  std::vector< const trigType* >& objects,
513  const Trig::FeatureContainer& featureContainer,
514  bool onlyPassedFeatures,
515  const TrigMatch::DirectAttached* ) const {
516 
517  const std::vector< Trig::Feature< trigType > > trigFeatures =
518  featureContainer.get< trigType >( featureLabel,
519  onlyPassedFeatures ? TrigDefs::Physics :
520  TrigDefs::alsoDeactivateTEs );
521 
522  // convert the Trig::Feature objects to trigType objects
523  // Additionally, if onlyPassedFeatures is true only take those
524  // from active te's
525  typename std::vector< Trig::Feature< trigType > >::const_iterator iter =
526  trigFeatures.begin();
527  typename std::vector< Trig::Feature< trigType > >::const_iterator end =
528  trigFeatures.end();
529  for( ; iter != end; ++iter ) {
530  if( iter->te() != 0 ) {
531  objects.push_back( *iter );
532  }
533  }
534 
535  return;
536 }
537 
538 template< typename trigType, typename contType >
539 void TrigMatchToolCore::collectObjects( const std::string& featureLabel,
540  std::vector< const trigType* >& objects,
541  const Trig::FeatureContainer& featureContainer,
542  bool onlyPassedFeatures,
543  const contType* ) const {
544 
545  // collect the container features from the navigation
546  std::vector< const contType* > contObjects;
547  const typename TrigMatch::DirectAttached* directAttached = 0;
548  this->collectObjects( featureLabel, contObjects, featureContainer, onlyPassedFeatures,
549  directAttached );
550 
551  // flatten them out - we need to check that we don't get duplicates...
552  typename std::vector< const contType* >::const_iterator iter =
553  contObjects.begin();
554  typename std::vector< const contType* >::const_iterator end =
555  contObjects.end();
556  for( ; iter != end; ++iter ) {
557  objects.insert( objects.end(), ( *iter )->begin(),
558  ( *iter )->end() );
559  }
560 
561  return;
562 }
563 
564 template< typename trigType >
565 void TrigMatchToolCore::collectObjects( const std::string& featureLabel,
566  std::vector< const trigType* >& objects,
567  const Trig::FeatureContainer& featureContainer,
568  bool onlyPassedFeatures,
569  const TrigMatch::AncestorAttached* ) const {
570 
571  // first, try to grab them straight from the TDT
572  const typename TrigMatch::DirectAttached* directAttached = 0;
573  this->collectObjects( featureLabel, objects, featureContainer, onlyPassedFeatures, directAttached );
574 
575  if( objects.size() > 0 ) {
576  return;
577  }
578 
579  // if we get here, we failed to grab them from the TDT, so try
580  // to get them propagating from the TrigRoiDescriptor
581 
582  // grab the TrigRoiDescriptors from the trig decision tool
583  std::vector< Trig::Feature<TrigRoiDescriptor > > trigFeatures =
584  featureContainer.get< TrigRoiDescriptor >( featureLabel,
585  onlyPassedFeatures ? TrigDefs::Physics :
586  TrigDefs::alsoDeactivateTEs );
587 
588  // now, we turn this into the L1 trig type we really want
589  typename std::vector< Trig::Feature< TrigRoiDescriptor > >::const_iterator iter =
590  trigFeatures.begin();
591  typename std::vector< Trig::Feature< TrigRoiDescriptor > >::const_iterator end =
592  trigFeatures.end();
593  for( ; iter != end; ++iter ) {
594  if( iter->te() ) {
595  const trigType* feature =
596  m_trigDecisionToolCore->ancestor< trigType >( iter->te(), "" );
597  objects.push_back( feature );
598  }
599  }
600 
601  return;
602 }
603 
604 template <typename trigType>
605 TrigMatchToolCore::TrigFeatureCache<trigType>&
606 TrigMatchToolCore::getCache (int type_key,
607  SlotCache& slotCache,
608  const SlotCache::lock_t& lock) const
609 {
610  TrigFeatureCacheBase*& cache = this->getCache1 (&typeid(trigType), type_key,
611  slotCache, lock);
612  if (!cache)
613  cache = new TrigFeatureCache<trigType>;
614  return *static_cast<TrigFeatureCache<trigType>*> (cache);
615 }
616 
617 
618 #endif // TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC