2   Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
 
    5 #include "AthViews/ViewHelper.h"
 
    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 {
 
   13   ElementLink< ViewContainer > viewEL = prevDecision->objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
 
   14   ATH_CHECK( viewEL.isValid() );
 
   15   ATH_MSG_DEBUG( "Retrieved View" );
 
   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() );
 
   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 );
 
   27   return StatusCode::SUCCESS;
 
   30 template < class CONTAINER >
 
   31 StatusCode TrigBjetHypoAlgBase::retrieveObjectFromNavigation( const std::string& linkName,
 
   32                                 ElementLink< CONTAINER >& EL,
 
   33                                 const TrigCompositeUtils::Decision* prevDecision )  const {
 
   35     const std::vector< TrigCompositeUtils::LinkInfo< CONTAINER > > myObj = 
 
   36           TrigCompositeUtils::findLinks< CONTAINER >( prevDecision, linkName.c_str(), TrigDefs::lastFeatureOfType);
 
   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;
 
   43     ATH_CHECK( myObj.at(0).isValid() );
 
   44     EL = myObj.at(0).link;       
 
   46     return StatusCode::SUCCESS;
 
   49 template < class CONTAINER >
 
   50 StatusCode TrigBjetHypoAlgBase::retrieveCollectionFromNavigation( const std::string& linkName,
 
   51                                                                 ElementLinkVector< CONTAINER >& objELs,
 
   52                                     const TrigCompositeUtils::DecisionContainer* prevDecisionContainer )  const {
 
   54   for ( const TrigCompositeUtils::Decision *prevDecision : *prevDecisionContainer ) {
 
   55     ElementLink< CONTAINER > objEL;
 
   56     CHECK( retrieveObjectFromNavigation( linkName, objEL, prevDecision ) );
 
   57     objELs.push_back( objEL ) ;
 
   60   return StatusCode::SUCCESS;
 
   70 template < class CONTAINER >
 
   71 StatusCode TrigBjetHypoAlgBase::retrieveObjectFromStoreGate( const EventContext& context,
 
   72                                    ElementLinkVector< CONTAINER >& ELs,
 
   73                                    const SG::ReadHandleKey< CONTAINER >& inputKey ) const {
 
   75   ATH_MSG_DEBUG( "Retrieving object from StoreGate from " << inputKey.key() );
 
   77   SG::ReadHandle< CONTAINER  > ContainerHandle = SG::makeHandle( inputKey,context );
 
   78   CHECK( ContainerHandle.isValid() );
 
   79   const CONTAINER *Collection = ContainerHandle.get();
 
   81   for (const auto* obj : *Collection) {
 
   82     ElementLink< CONTAINER > Link = ElementLink< CONTAINER  >(*Collection, obj->index());
 
   83     ELs.push_back( Link );
 
   86   return StatusCode::SUCCESS;
 
   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 {
 
   95   ATH_MSG_DEBUG( "Retrieving object from Event View from " << inputKey.key() );
 
   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;
 
  100   for ( const TrigCompositeUtils::Decision* previousDecision: *prevDevisionContainer ) {
 
  102     ElementLink< ViewContainer > viewEL = previousDecision->objectLink< ViewContainer >( TrigCompositeUtils::viewString() );
 
  103     ATH_CHECK( viewEL.isValid() );
 
  104     ATH_MSG_DEBUG( "Retrieved View" );
 
  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!" );
 
  114     if ( alreadyUsed ) continue;
 
  116     readViews.push_back( viewEL );
 
  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() );
 
  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() );
 
  130   return StatusCode::SUCCESS;
 
  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 {
 
  143   ATH_MSG_DEBUG( "Adding object with link name '" << objLink << "'" );
 
  144   outputDecision.setObjectLink( objLink,ElementLink< CONTAINER >( objectKey.key(),objIndex ) );
 
  146   return StatusCode::SUCCESS;
 
  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 {
 
  155   for ( unsigned int index(0); index < outputDecisions.size(); index++ ) {
 
  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;
 
  162     // Get the correct decisions  
 
  163     TrigCompositeUtils::Decision* outputDecision = outputDecisions.at( index );
 
  165     CHECK( attachObjectLinkToDecisionFromStoreGate( *outputDecision,
 
  171   return StatusCode::SUCCESS;
 
  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 {
 
  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;
 
  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();
 
  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() );
 
  197   SG::ReadHandle< CONTAINER > calObjectHandle = ViewHelper::makeHandle( *viewEL, objectKey, context );
 
  198   ATH_CHECK( calObjectHandle.isValid() );
 
  200   ElementLink< CONTAINER > objEL = ViewHelper::makeLink( *viewEL, calObjectHandle, objIndex );
 
  201   ATH_CHECK( objEL.isValid() );
 
  204   ATH_MSG_DEBUG( "Adding object with link name '" << objLink << "'" );
 
  205   outputDecision.setObjectLink( objLink,objEL );
 
  207   return StatusCode::SUCCESS;
 
  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 {
 
  217   for ( unsigned int index(0); index<outputDecisions.size(); index++ ) {
 
  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;
 
  224     // Get the correct decision
 
  225     TrigCompositeUtils::Decision* outputDecision = outputDecisions.at( index );
 
  227     CHECK( attachObjectLinkToDecisionFromEventView( context,
 
  234   return StatusCode::SUCCESS;
 
  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 {
 
  243   for ( TrigCompositeUtils::Decision* outputDecision : outputDecisions ) {
 
  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;
 
  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();
 
  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() );
 
  260     SG::ReadHandle< CONTAINER > calObjectHandle = ViewHelper::makeHandle( *viewEL,objectKey,context );
 
  261     ATH_CHECK( calObjectHandle.isValid() );
 
  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 );
 
  272     ATH_MSG_DEBUG( "Adding object collection with link name '" << objLink << "' and size " << objELs.size() );
 
  273     outputDecision->addObjectCollectionLinks( objLink,objELs );
 
  276   return StatusCode::SUCCESS;