From c47d8cb0deafaec22a666c93b52ae9ff21a134ad Mon Sep 17 00:00:00 2001
From: Marco Montella <marco.montella@cern.ch>
Date: Thu, 11 Nov 2021 19:06:43 +0100
Subject: [PATCH] Partial Modification to hypos

---
 .../src/TrigEgammaTLAPhotonHypoAlg.cxx        | 78 +++++--------------
 .../src/TrigEgammaTLAPhotonHypoTool.cxx       |  8 +-
 .../src/TrigEgammaTLAPhotonHypoTool.h         |  4 +-
 .../TrigHLTJetHypo/src/TrigJetTLAHypoAlg.cxx  |  8 +-
 .../TrigHLTJetHypo/src/TrigJetTLAHypoTool.cxx |  8 +-
 .../TrigHLTJetHypo/src/TrigJetTLAHypoTool.h   |  5 +-
 .../TrigMuonHypo/src/TrigMuonTLAHypoAlg.cxx   | 35 +++++----
 .../TrigMuonHypo/src/TrigMuonTLAHypoAlg.h     |  2 +-
 .../HLTMenuConfig/Muon/TLAMuonSequence.py     |  5 +-
 .../Photon/TLAPhotonMenuSequences.py          |  1 +
 10 files changed, 61 insertions(+), 93 deletions(-)

diff --git a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoAlg.cxx b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoAlg.cxx
index 128324f5bcc8..a94bb640c68d 100644
--- a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoAlg.cxx
+++ b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoAlg.cxx
@@ -63,9 +63,6 @@ StatusCode TrigEgammaTLAPhotonHypoAlg::execute( const EventContext& ctx) const
 SG::WriteHandle<DecisionContainer> outputHandle = createAndStore(decisionOutput(), ctx);
 DecisionContainer* outputDecisions = outputHandle.ptr();
 
-// the HypoTool needs a pair of <object*, decision*> as input
-std::vector<std::pair<const Decision*, Decision*> > photonHypoInputs;
-
 // loops over previous decisions
 int nDecision = 0;
 for (const auto previousDecision : *previousDecisionHandle)
@@ -75,68 +72,33 @@ for (const auto previousDecision : *previousDecisionHandle)
         const xAOD::Photon *photonPrev = nullptr;
         auto prevPhotons = TrigCompositeUtils::findLinks<xAOD::PhotonContainer>(previousDecision, TrigCompositeUtils::featureString(), TrigDefs::lastFeatureOfType);
         ATH_MSG_WARNING("This decision has " << prevPhotons.size() << " photons");
-        //copy all muons into the new TLA collection
-        for (auto photon : prevPhotons)
-        {
-            auto prevPhotonLink = photon.link;
-            ATH_CHECK(prevPhotonLink.isValid());
-            photonPrev = *prevPhotonLink;
-
-            xAOD::Photon *copiedPhoton = new xAOD::Photon();            
-            h_TLAPhotons->push_back(copiedPhoton);
-            *copiedPhoton = *photonPrev;
-
-              // now go on with the normal Hypo, linking new decision with previous one
-            auto newDecision = newDecisionIn( outputDecisions, hypoAlgNodeName() );
-            TrigCompositeUtils::linkToPrevious( newDecision, previousDecision, ctx );
-            // do we need to re-link the feature?
-            newDecision->setObjectLink(featureString(), prevPhotonLink); 
-            photonHypoInputs.push_back( std::make_pair(previousDecision, newDecision) );
-
-            ATH_MSG_WARNING("Copied photon with pT: " << copiedPhoton->pt() << " from decision " << nDecision);
+        
+        
+        if (prevPhotons.size() != 1) {
+          ATH_MSG_ERROR("Did not locate exactly one photon for this Decision Object, found " << prevPhotons.size());
+          return StatusCode::FAILURE;
         }
 
-      
 
-    // for (const xAOD::Photon* currPhoton : *hltPhotonsCollectionHandle)
-    // {
-      
+        auto prevPhotonLink = prevPhotons.at(0).link;
+        ATH_CHECK(prevPhotonLink.isValid());
+        photonPrev = *prevPhotonLink;
 
-    //   ATH_MSG_DEBUG("Original Photon Printout: " << currPhoton->p4().Pt() << " " << currPhoton->p4().Eta() << " " << currPhoton->p4().Phi() );
+        xAOD::Photon *copiedPhoton = new xAOD::Photon();            
+        h_TLAPhotons->push_back(copiedPhoton);
+        *copiedPhoton = *photonPrev;
 
-    //   // now loop over the TLAPhotons container and look for a photon matching the photon associated to the current decision
-    //   for (const xAOD::Photon* TLAPhoton : *TLAPhotons)
-    //   {
+          // now go on with the normal Hypo, linking new decision with previous one
+        auto newDecision = newDecisionIn( outputDecisions, hypoAlgNodeName() );
+        TrigCompositeUtils::linkToPrevious( newDecision, previousDecision, ctx );
+        // do we need to re-link the feature?
+        newDecision->setObjectLink(featureString(), ElementLink<xAOD::PhotonContainer>(*h_TLAPhotons, h_TLAPhotons->size() - 1, ctx)); 
         
-    //     ATH_MSG_DEBUG("TLA Photon Printout: " << TLAPhoton->p4().Pt() << " " << TLAPhoton->p4().Eta() << " " << TLAPhoton->p4().Phi() );
+
+        ATH_MSG_WARNING("Copied photon with pT: " << copiedPhoton->pt() << " from decision " << nDecision);
         
-    //     // verify match between TLAPhoton and currPhoton
-    //     if (TLAPhoton->p4().Pt() == currPhoton->p4().Pt() and TLAPhoton->p4().Eta() == currPhoton->p4().Eta()
-    //           and TLAPhoton->p4().Phi() == currPhoton->p4().Phi()) // they are the same photon 
-    //     {
-
-    //       ATH_MSG_DEBUG("Matched a decision to a TLA Photon!");
-
-          
-    //       atLeastOneDecision = true;
-
-    //       // now create a new Decision object, have it be linked to the current TLAPhoton, then create the pair with photon,decision
-    //       // to feed the HypoTool
-    //       Decision* newDecision = TrigCompositeUtils::newDecisionIn(outputDecisions, currDecision, hypoAlgNodeName(), ctx);
-          
-    //       // create the link
-    //       ElementLink<xAOD::PhotonContainer> photonLink = ElementLink<xAOD::PhotonContainer>(*TLAPhotons, TLAPhoton->index());
-    //       ATH_CHECK(photonLink.isValid());
-    //       newDecision->setObjectLink<xAOD::PhotonContainer>(featureString(), photonLink);
-
-    //       photonHypoInputs.push_back( std::make_pair(TLAPhoton, newDecision) );
-
-    //       break; // the Original Photon has been matched with a TLA Photon, no need to continue the TLA loop
-    //     }
-    //   }
-    // }
-}
 
+    }
 
 
 
@@ -149,7 +111,7 @@ for (const auto previousDecision : *previousDecisionHandle)
   for (const auto& tool : m_hypoTools)
   {
     ATH_MSG_DEBUG("Now computing decision for HypoTool: " << tool->name());
-    ATH_CHECK(tool->decide(photonHypoInputs));
+    ATH_CHECK(tool->decide(outputDecisions));
   }
 
   ATH_CHECK( hypoBaseOutputProcessing(outputHandle ));
diff --git a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.cxx b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.cxx
index 6f266f893afe..fbe3501e1257 100644
--- a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.cxx
+++ b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.cxx
@@ -31,22 +31,22 @@ StatusCode TrigEgammaTLAPhotonHypoTool::finalize()
   return StatusCode::SUCCESS;
 }
 
-StatusCode TrigEgammaTLAPhotonHypoTool::decide(std::vector<PhotonDecision>& photonHypoInputs) const
+StatusCode TrigEgammaTLAPhotonHypoTool::decide(TrigCompositeUtils::DecisionContainer* outputDecisions) const
 {
 
-    for (auto& hypoPair: photonHypoInputs )
+    for (auto decision : *outputDecisions )
     {
       // creates container of Decision identifiers that will be filled with the previous decisions attached to the object
       DecisionIDContainer previousDecisionIDs;
       // not sure whtat this does
-      const auto previousDecisionEL = TrigCompositeUtils::getLinkToPrevious(hypoPair.second).at(0);
+      const auto previousDecisionEL = TrigCompositeUtils::getLinkToPrevious(decision).at(0);
       decisionIDs(*previousDecisionEL, previousDecisionIDs);
       // if we are sure that there is only one decision added by the parent HypoAlg (it should be), then checking that count > 0
       // is equivalent to checking if the previous decision was positive-NO
       // checks that the decision corresponding to the chain name is positive? uff
       if (previousDecisionIDs.count( m_decisionId.numeric()) > 0)
       {
-        TrigCompositeUtils::addDecisionID(getId().numeric(), hypoPair.second);
+        TrigCompositeUtils::addDecisionID(getId().numeric(), decision );
       }
     }
   return StatusCode::SUCCESS;
diff --git a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.h b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.h
index 32247a79539f..1b91746d1ffa 100644
--- a/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.h
+++ b/Trigger/TrigHypothesis/TrigEgammaHypo/src/TrigEgammaTLAPhotonHypoTool.h
@@ -24,8 +24,8 @@ class TrigEgammaTLAPhotonHypoTool: public AthAlgTool{
     StatusCode finalize();
 
     //using PhotonDecision = std::pair<const xAOD::Photon*, TrigCompositeUtils::Decision*>;
-    using PhotonDecision = std::pair<const TrigCompositeUtils::Decision*, TrigCompositeUtils::Decision*>;
-    StatusCode decide(std::vector<PhotonDecision>& photonHypoInputs) const;
+   
+    StatusCode decide(TrigCompositeUtils::DecisionContainer* decisionContainer) const;
 
     const HLT::Identifier& getId() const;
 
diff --git a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoAlg.cxx b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoAlg.cxx
index 5929950d4039..21996d523486 100644
--- a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoAlg.cxx
+++ b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoAlg.cxx
@@ -50,10 +50,7 @@ StatusCode TrigJetTLAHypoAlg::execute( const EventContext& context ) const {
   SG::WriteHandle<DecisionContainer> outputHandle = createAndStore(decisionOutput(), context);
   DecisionContainer* outputDecisions = outputHandle.ptr();
 
-  //information to pass to hypoTool: jet pointer and decision
-  std::vector<std::pair<const xAOD::Jet*,Decision*>> jetHypoInputs;
 
-  /// NEW
 
   int nDecision = 0;
     for (const auto previousDecision : *previousDecisionHandle)
@@ -87,7 +84,7 @@ StatusCode TrigJetTLAHypoAlg::execute( const EventContext& context ) const {
             // do we need to re-link the feature?
             newDecision->setObjectLink<xAOD::JetContainer>(featureString(), prevJetLink);
 
-            jetHypoInputs.push_back( std::make_pair(copiedJet, newDecision) );
+            
 
 
         }
@@ -96,13 +93,14 @@ StatusCode TrigJetTLAHypoAlg::execute( const EventContext& context ) const {
 
       nDecision++;
     }
+  
 
 
 
   for (const auto& tool: m_hypoTools) {
 
     ATH_MSG_DEBUG("Now computing decision for " << tool->name());
-    CHECK(tool->decide(jetHypoInputs));
+    CHECK(tool->decide(outputDecisions));
 
   }//end loop on hypoTools
 
diff --git a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.cxx b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.cxx
index 5721ca6073dc..acf6e9c57a3c 100644
--- a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.cxx
+++ b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.cxx
@@ -62,7 +62,7 @@ StatusCode TrigJetTLAHypoTool::finalize(){
 }
 
 StatusCode
-TrigJetTLAHypoTool::decide(std::vector<JetDecision>& jetHypoInputs) const {
+TrigJetTLAHypoTool::decide(TrigCompositeUtils::DecisionContainer* outputDecisions) const {
 
   int decision_count=0;
 
@@ -71,15 +71,15 @@ TrigJetTLAHypoTool::decide(std::vector<JetDecision>& jetHypoInputs) const {
   // jet hypo inputs:
   // pairs of const xAOD::Jet* (first) and mutable Decision* (second)
 
-  for (auto& pair : jetHypoInputs) { 
+  for (auto decision : *outputDecisions) { 
       
       //check that the previous decision comes from the appropriate chain
       DecisionIDContainer previousDecisionIDs;
-      const auto previousDecisionEL = TrigCompositeUtils::getLinkToPrevious(pair.second).at(0); // We know that the parent HypoAlg added exactly one parent.
+      const auto previousDecisionEL = TrigCompositeUtils::getLinkToPrevious(decision).at(0); // We know that the parent HypoAlg added exactly one parent.
       decisionIDs(*previousDecisionEL, previousDecisionIDs);
       //check that the previous decision corresponding to this decisionId was positive
       if ( previousDecisionIDs.count( m_decisionId.numeric() ) > 0) {
-          TrigCompositeUtils::addDecisionID(getId().numeric(), pair.second);
+          TrigCompositeUtils::addDecisionID(getId().numeric(), decision);
           ++decision_count;
       }
   }
diff --git a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.h b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.h
index 4b5c4e95e3fa..454b21a48c74 100644
--- a/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.h
+++ b/Trigger/TrigHypothesis/TrigHLTJetHypo/src/TrigJetTLAHypoTool.h
@@ -43,11 +43,8 @@ class TrigJetTLAHypoTool: public AthAlgTool{
     StatusCode initialize();
     StatusCode finalize();
 
-    // typedef for passing jet - decision object assocation
-    using JetDecision = std::pair<const xAOD::Jet*,TrigCompositeUtils::Decision*>;
-
     StatusCode
-      decide(std::vector<JetDecision>& jetHypoInputs) const;
+      decide(TrigCompositeUtils::DecisionContainer* outputDecisions) const;
 
     const HLT::Identifier& getId() const; 
 
diff --git a/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.cxx b/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.cxx
index 21edba55afd0..77d56f2916a6 100644
--- a/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.cxx
+++ b/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.cxx
@@ -58,25 +58,32 @@ StatusCode TrigMuonTLAHypoAlg::execute(const EventContext &ctx) const
         const xAOD::Muon *muonPrev = nullptr;
         auto prevMuons = TrigCompositeUtils::findLinks<xAOD::MuonContainer>(previousDecision, TrigCompositeUtils::featureString(), TrigDefs::lastFeatureOfType);
         ATH_MSG_DEBUG("This decision has " << prevMuons.size() << " decisions");
-        //copy all muons into the new TLA collection
-        for (auto muon : prevMuons)
-        {
-            auto prevMuLink = muon.link;
-            ATH_CHECK(prevMuLink.isValid());
-            muonPrev = *prevMuLink;
-
-            xAOD::Muon *copiedMuon = new xAOD::Muon();            
-            h_TLAMuons->push_back(copiedMuon);
-            *copiedMuon = *muonPrev;
-
-            ATH_MSG_DEBUG("Copied muon with pT: " << copiedMuon->pt() << " from decision " << nDecision);
+
+        // verify that only one object is found per decision
+        if (prevMuons.size() != 1) {
+            ATH_MSG_ERROR("Did not locate exactly one muon for this Decision Object, found " << prevMuons.size());
+            return StatusCode::FAILURE;
         }
+     
+
+        auto prevMuLink = prevMuons.at(0).link;
+        ATH_CHECK(prevMuLink.isValid());
+        muonPrev = *prevMuLink;
+
+        xAOD::Muon *copiedMuon = new xAOD::Muon();            
+        h_TLAMuons->push_back(copiedMuon);
+        *copiedMuon = *muonPrev;
 
         // now go on with the normal Hypo, linking new decision with previous one
         auto newDecision = newDecisionIn( outputDecisions, hypoAlgNodeName() );
         TrigCompositeUtils::linkToPrevious( newDecision, previousDecision, ctx );
-        // do we need to re-link the feature?
-        //newDecision->setObjectLink(featureString(), prevMuons);
+        newDecision->setObjectLink(featureString(), ElementLink<xAOD::MuonContainer>(*h_TLAMuons, h_TLAMuons->size() - 1, ctx));
+
+        ATH_MSG_DEBUG("Copied muon with pT: " << copiedMuon->pt() << " from decision " << nDecision);
+    
+
+      
+      
 
         HypoInputs.push_back( std::make_pair(newDecision, previousDecision) );
         nDecision++;
diff --git a/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.h b/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.h
index 4baab1a7b8ad..0fc69299b6a0 100644
--- a/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.h
+++ b/Trigger/TrigHypothesis/TrigMuonHypo/src/TrigMuonTLAHypoAlg.h
@@ -29,7 +29,7 @@ class TrigMuonTLAHypoAlg : public HypoBase {
 
     
     SG::WriteHandleKey< xAOD::MuonContainer > m_TLAMuonsKey {
-      this, "TLAMuons", "Muons", "TLA Muon container key"
+      this, "TLAOutputName", "Muons", "TLA Muon container key"
     };
 
 };
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Muon/TLAMuonSequence.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Muon/TLAMuonSequence.py
index 622dbc0aa54f..b73696f9a608 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Muon/TLAMuonSequence.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Muon/TLAMuonSequence.py
@@ -11,6 +11,7 @@ def TLAMuonSequence (flags, muons):
     ## add the InputMaker (event context)    
     tlaMuonInputMakerAlg = CompFactory.InputMakerForRoI("IMTLAMuons", RoIsLink="initialRoI")
     tlaMuonInputMakerAlg.mergeUsingFeature = True
+    tlaMuonInputMakerAlg.RoITool = CompFactory.ViewCreatorPreviousROITool()
     
     tlaMuonAthSequence = seqAND( "TLAMuonAthSequence", [tlaMuonInputMakerAlg] )
     sequenceOut = muons+"_TLA"
@@ -23,7 +24,9 @@ def TLAMuonMenuSequence( flags, muonsIn):
      #  add the hypo
     from TrigMuonHypo.TrigMuonHypoConf import TrigMuonTLAHypoAlg
     from TrigMuonHypo.TrigMuonHypoConfig import TrigMuonEFMSonlyHypoToolFromDict
-    hypo = TrigMuonTLAHypoAlg("TrigMuonTLAHypoAlg")  #+muonsIn)    
+    hypo = TrigMuonTLAHypoAlg("TrigMuonTLAHypoAlg")  #+muonsIn)  
+
+    hypo.TLAOutputName = sequenceOut  
 
     return MenuSequence( Sequence  = tlaMuonAthSequence,
                          Maker       = tlaMuonInputMakerAlg,
diff --git a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Photon/TLAPhotonMenuSequences.py b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Photon/TLAPhotonMenuSequences.py
index e0fd4dbc00be..03fc1c074786 100644
--- a/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Photon/TLAPhotonMenuSequences.py
+++ b/Trigger/TriggerCommon/TriggerMenuMT/python/HLTMenuConfig/Photon/TLAPhotonMenuSequences.py
@@ -12,6 +12,7 @@ def TLAPhotonSequence (flags, photonsIn):
     
     ## add the InputMaker (event context)    
     tlaPhotonInputMakerAlg = CompFactory.InputMakerForRoI("IMTLAPhotons", RoIsLink="initialRoI")
+    tlaPhotonInputMakerAlg.RoITool =  CompFactory.ViewCreatorPreviousROITool()
     tlaPhotonInputMakerAlg.mergeUsingFeature = True
     
     tlaPhotonAthSequence = seqAND( "TLAPhotonAthSequence", [tlaPhotonInputMakerAlg] )
-- 
GitLab