1 // Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
8 #ifndef TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC
9 #define TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC
14 #include "TrigSteeringEvent/TrigRoiDescriptorCollection.h"
16 /************************************/
17 /* Public Functions */
18 /************************************/
20 template< typename trigType >
21 std::vector< const trigType* >
22 TrigMatchToolCore::getTriggerObjects( const std::string& chainName,
23 bool onlyPassedFeatures ) const
25 std::vector< const trigType* > trigObjects;
26 this->getObjects( trigObjects, chainNameToIndex (chainName),
31 template< typename trigType >
32 std::vector< const trigType* >
33 TrigMatchToolCore::getTriggerObjects( size_t chainIndex,
34 bool onlyPassedFeatures ) const {
36 std::vector< const trigType* > trigObjects;
38 // grab the features of type trigType from the chain
39 this->getObjects( trigObjects, chainIndex, onlyPassedFeatures );
44 template< typename trigType, typename baseType >
45 std::vector< const trigType* >
46 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
47 const std::string& chainName,
49 bool onlyPassedFeatures,
50 const DistanceFunctor< trigType,
51 baseType > *metric ) const
53 return matchToTriggerObjects (baseObject,
54 chainNameToIndex (chainName),
60 template< typename trigType, typename baseType >
61 std::vector< const trigType* >
62 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
65 bool onlyPassedFeatures,
66 const DistanceFunctor< trigType,
67 baseType > *metric ) const {
70 return std::vector<const trigType*>();
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 );
79 template< typename trigType, typename baseType >
80 std::vector< const trigType* >
81 TrigMatchToolCore::matchToTriggerObjects( const baseType* baseObject,
82 const std::string& chainName,
84 bool onlyPassedFeatures ) const {
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 >() );
91 template< typename trigType, typename baseType >
93 TrigMatchToolCore::matchToTriggerObject( const baseType* baseObject,
94 const std::string& chainName,
96 bool onlyPassedFeatures,
97 const DistanceFunctor< trigType, baseType >* metric) const {
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
102 std::vector< const trigType* > passedObjects =
103 this->matchToTriggerObjects< trigType, baseType >( baseObject, chainName,
104 maxDistance, onlyPassedFeatures, metric );
106 const trigType *best = 0;
108 if( passedObjects.size() ) {
109 best = passedObjects[ 0 ];
115 template< typename trigType, typename baseType >
117 TrigMatchToolCore::matchToTriggerObject( const baseType* baseObject,
118 const std::string& chainName,
120 bool onlyPassedFeatures ) const {
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 >() );
127 template< typename trigType, typename baseType >
128 std::vector< const trigType* >
129 TrigMatchToolCore::matchedTriggerObjects( const DataVector<baseType>& baseObjects,
130 const std::string& chainName,
133 bool onlyPassedFeatures,
134 const DistanceFunctor< trigType, baseType >* metric ) const {
136 std::vector< const baseType* > obs;
137 obs.insert( obs.begin(), baseObjects.begin(), baseObjects.end() );
139 return this->matchedTriggerObjects< trigType, baseType >( obs, chainName,
140 maxDistance, onlyBestMatch, onlyPassedFeatures, metric );
143 template< typename trigType, typename baseType >
144 std::vector< const trigType* >
145 TrigMatchToolCore::matchedTriggerObjects( const DataVector<baseType>& baseObjects,
146 const std::string& chainName,
149 bool onlyPassedFeatures ) const {
151 std::vector< const baseType* > obs;
152 obs.insert( obs.begin(), baseObjects.begin(), baseObjects.end() );
154 return this->matchedTriggerObjects< trigType, baseType >( obs, chainName,
155 maxDistance, onlyBestMatch, onlyPassedFeatures );
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,
164 bool onlyPassedFeatures,
165 const DistanceFunctor< trigType, baseType >* metric ) const {
167 std::set< const trigType* > matches;
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,
178 matches.insert(match);
181 std::vector< const trigType* > match =
182 this->matchToTriggerObjects< trigType >( *iter, chainName,
183 maxDistance, onlyPassedFeatures,
185 matches.insert( match.begin(), match.end() );
189 std::vector< const trigType* > result;
190 result.insert( result.begin(), matches.begin(), matches.end() );
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,
203 bool onlyPassedFeatures ) const {
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 >() );
211 template< typename trigType, typename baseType >
212 std::vector< const trigType* >
213 TrigMatchToolCore::unmatchedTriggerObjects( const DataVector<baseType>& baseObjects,
214 const std::string& chainName,
217 bool onlyPassedFeatures,
218 const DistanceFunctor< trigType, baseType >* metric) const {
220 std::vector< const baseType* > objs;
221 objs.insert( objs.begin(), baseObjects.begin(), baseObjects.end() );
223 return this->unmatchedTriggerObjects< trigType,baseType >( objs, chainName,
224 maxDistance, onlyBestMatch, onlyPassedFeatures, metric );
227 template< typename trigType, typename baseType >
228 std::vector< const trigType* >
229 TrigMatchToolCore::unmatchedTriggerObjects( const DataVector< baseType >& baseObjects,
230 const std::string& chainName,
233 bool onlyPassedFeatures ) const {
235 std::vector< const baseType* > objs;
236 objs.insert( objs.begin(), baseObjects.begin(), baseObjects.end() );
238 return this->unmatchedTriggerObjects< trigType, baseType >( objs, chainName,
239 maxDistance, onlyBestMatch, onlyPassedFeatures );
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,
248 bool onlyPassedFeatures,
249 const DistanceFunctor< trigType, baseType >* metric ) const {
251 // grab all the features
252 const std::vector< const trigType* > trigObjects =
253 this->getTriggerObjects< trigType >( chainName, onlyPassedFeatures );
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 );
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;
269 if( std::find( matched.begin(), matched.end(), *iter ) == matched.end() ) {
270 results.push_back( *iter );
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,
285 bool onlyPassedFeatures ) const {
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 >() );
293 template< typename trigType, typename baseType >
294 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
295 const std::string& chainName,
297 const DistanceFunctor< trigType, baseType >* metric ) const
299 return chainPassedByObject (baseObject,
300 chainNameToIndex (chainName),
305 template< typename trigType, typename baseType >
306 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
309 const DistanceFunctor< trigType, baseType >* metric ) const
311 if (!baseObject) return false;
313 std::vector<const trigType*> v =
314 this->getTriggerObjects<trigType> (chainIndex, true);
315 return this->anyMatch (baseObject, v, maxDistance, metric);
318 template< typename trigType, typename baseType >
319 bool TrigMatchToolCore::chainPassedByObject( const baseType* baseObject,
320 const std::string& chainName,
321 float maxDistance ) const {
323 // prepare the default metric, and offload it to the main version
325 return this->chainPassedByObject( baseObject, chainName, maxDistance,
326 prepareMetric< trigType, baseType >() );
329 template< typename trigType, typename baseType >
330 std::vector< std::string >
331 TrigMatchToolCore::chainsPassedByObject( const baseType* baseObject,
333 const DistanceFunctor< trigType, baseType >* metric) const {
335 // loop through all chains and check if there are any features
336 // matching the passed object
337 std::vector< std::string > passedChains;
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,
343 maxDistance, metric ) ) {
344 passedChains.push_back( configuredChains[i] );
351 template <typename trigType, typename baseType>
352 std::vector< std::string >
353 TrigMatchToolCore::chainsPassedByObject( const baseType* baseObject,
354 float maxDistance ) const {
356 // prepare the default metric and ship the work off to the main version
358 return this->chainsPassedByObject( baseObject, maxDistance,
359 prepareMetric< trigType, baseType >() );
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,
368 const DistanceFunctor< trigType, baseType >* metric ) const {
370 // loop through reco objects supplied to see if any match objects
371 // in the specified chain
372 std::vector< const baseType* > matchedBaseObjects;
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 );
385 return matchedBaseObjects;
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 {
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 >() );
401 template< typename trigType, typename baseType >
402 std::map< const trigType*, std::vector< std::string > >
403 TrigMatchToolCore::matchToAllTriggerObjects( const baseType* baseObject,
405 bool onlyPassedFeatures,
406 const DistanceFunctor< trigType, baseType >* metric ) const {
408 // build map for storing matching trigger objects
409 std::map< const trigType*, std::vector< std::string > > matchMap;
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 );
428 template< typename trigType, typename baseType >
429 std::map< const trigType*, std::vector< std::string > >
430 TrigMatchToolCore::matchToAllTriggerObjects( const baseType* baseObject,
432 bool onlyPassedFeatures ) const {
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 >() );
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
447 this->getObjects (objects, chainNameToIndex (chainName), onlyPassedFeatures);
451 template<typename trigType>
452 void TrigMatchToolCore::getObjects( std::vector< const trigType* >& objects,
454 bool onlyPassedFeatures ) const {
456 SlotCache& slotCache = *m_slotCache;
457 SlotCache::lock_t lock (slotCache.m_mutex);
459 // check if we are able to use the cache
460 if( this->changedDecisionAware() ) {
462 static const int type_key = m_typeMap.key (&typeid(trigType));
463 TrigFeatureCache< trigType >& cache = this->getCache<trigType> (type_key,
467 // check if its already in the cache - if not, add it
468 if (cache.get (chainIndex, onlyPassedFeatures, objects)) {
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;
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 );
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 );
493 cache.add( chainIndex, onlyPassedFeatures, objects );
498 // can't use the cache, so just run it normally
499 std::string chainName = m_chainNameIndex.chainName(chainIndex);
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 );
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 {
517 const std::vector< Trig::Feature< trigType > > trigFeatures =
518 featureContainer.get< trigType >( featureLabel,
519 onlyPassedFeatures ? TrigDefs::Physics :
520 TrigDefs::alsoDeactivateTEs );
522 // convert the Trig::Feature objects to trigType objects
523 // Additionally, if onlyPassedFeatures is true only take those
525 typename std::vector< Trig::Feature< trigType > >::const_iterator iter =
526 trigFeatures.begin();
527 typename std::vector< Trig::Feature< trigType > >::const_iterator end =
529 for( ; iter != end; ++iter ) {
530 if( iter->te() != 0 ) {
531 objects.push_back( *iter );
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 {
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,
551 // flatten them out - we need to check that we don't get duplicates...
552 typename std::vector< const contType* >::const_iterator iter =
554 typename std::vector< const contType* >::const_iterator end =
556 for( ; iter != end; ++iter ) {
557 objects.insert( objects.end(), ( *iter )->begin(),
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 {
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 );
575 if( objects.size() > 0 ) {
579 // if we get here, we failed to grab them from the TDT, so try
580 // to get them propagating from the TrigRoiDescriptor
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 );
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 =
593 for( ; iter != end; ++iter ) {
595 const trigType* feature =
596 m_trigDecisionToolCore->ancestor< trigType >( iter->te(), "" );
597 objects.push_back( feature );
604 template <typename trigType>
605 TrigMatchToolCore::TrigFeatureCache<trigType>&
606 TrigMatchToolCore::getCache (int type_key,
607 SlotCache& slotCache,
608 const SlotCache::lock_t& lock) const
610 TrigFeatureCacheBase*& cache = this->getCache1 (&typeid(trigType), type_key,
613 cache = new TrigFeatureCache<trigType>;
614 return *static_cast<TrigFeatureCache<trigType>*> (cache);
618 #endif // TRIGOBJECTMATCHING_TRIGMATCHTOOLCORE_ICC