From c756bbb4d4dd5bdbbde88a88eb148f70c2d3216a Mon Sep 17 00:00:00 2001
From: Tomasz Bold <tomasz.bold@gmail.com>
Date: Thu, 4 May 2017 20:21:00 +0200
Subject: [PATCH] Fixed small issues so that the EM +CTP unpacking works

---
 Trigger/TrigSteer/L1Decoder/share/decodeBS.py | 32 +++++++++++------
 .../L1Decoder/src/CTPUnpackingTool.cxx        | 36 +++++++++++++++++--
 .../L1Decoder/src/CTPUnpackingTool.h          | 17 ++++++---
 .../L1Decoder/src/EMRoIsUnpackingTool.cxx     | 12 ++++---
 .../L1Decoder/src/IRoIsUnpackingTool.cxx      | 19 ++++++++--
 .../L1Decoder/src/IRoIsUnpackingTool.h        |  4 +++
 Trigger/TrigSteer/L1Decoder/src/L1Decoder.cxx | 34 +++++++++++-------
 Trigger/TrigSteer/L1Decoder/src/L1Decoder.h   | 17 +++------
 .../src/components/L1Decoder_entries.cxx      |  9 +++++
 9 files changed, 129 insertions(+), 51 deletions(-)

diff --git a/Trigger/TrigSteer/L1Decoder/share/decodeBS.py b/Trigger/TrigSteer/L1Decoder/share/decodeBS.py
index d279b2cf02f..e439226f87c 100755
--- a/Trigger/TrigSteer/L1Decoder/share/decodeBS.py
+++ b/Trigger/TrigSteer/L1Decoder/share/decodeBS.py
@@ -7,6 +7,9 @@
 # ln -s /afs/cern.ch/atlas/project/trigger/pesa-sw/validation/atn-test/data15_13TeV.00266904.physics_EnhancedBias.merge.RAW._lb0452._SFO-1._0001.1 input.data
 # 
 
+import os.path
+assert os.path.isfile('input.data'), 'No input file: see the JO to see how to get it'
+
 ## @file L1Topo_ReadBS_test.py
 ## @brief Example job options file to read BS file to test a converter
 ## $Id: decodeBS.parallel.py 717359 2016-01-12 14:40:21Z bwynne $
@@ -80,18 +83,25 @@ if nThreads >= 1:
   topSequence += SGInputLoader( OutputLevel=INFO, ShowEventDump=False )
   topSequence.SGInputLoader.Load = [ ('ROIB::RoIBResult','RoIBResult') ]
 
-
+from L1Decoder.L1DecoderConf import *
+l1Decoder = L1Decoder()
+l1Decoder.ctpUnpacker = CTPUnpackingTool()
+l1Decoder.ctpUnpacker.CTPToChainMapping = ["1:HLT_e3", "2:HLT_e5"]
+emUnpacker = EMRoIsUnpackingTool()
+emUnpacker.ThresholdToChainMapping = ["EM3:HLT_e3", "EM3:HLT_e5"]
+l1Decoder.roiUnpackers = []
+topSequence += l1Decoder
 #Run calo decoder
-from L1Decoder.L1DecoderConf import L1CaloDecoder
-caloDecoder = L1CaloDecoder() # by default it is steered towards the RoIBResult of the name above
-caloDecoder.OutputLevel=VERBOSE
-topSequence += caloDecoder
-
-#Dumper
-from ViewAlgs.ViewAlgsConf import DumpDecisions
-dumper = DumpDecisions("L1CaloDecisions")
-dumper.OutputLevel=VERBOSE
-topSequence += dumper
+
+# caloDecoder = L1CaloDecoder() # by default it is steered towards the RoIBResult of the name above
+# caloDecoder.OutputLevel=VERBOSE
+# topSequence += caloDecoder
+
+# #Dumper
+# from ViewAlgs.ViewAlgsConf import DumpDecisions
+# dumper = DumpDecisions("L1CaloDecisions")
+# dumper.OutputLevel=VERBOSE
+# topSequence += dumper
 
 
 #--------------------------------------------------------------
diff --git a/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.cxx b/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.cxx
index 35106039eec..3a350e07476 100644
--- a/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.cxx
+++ b/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.cxx
@@ -7,11 +7,41 @@
 
 using namespace HLT;
 
+
+CTPUnpackingTool::CTPUnpackingTool( const std::string& type,
+				    const std::string& name, 
+				    const IInterface* parent ) 
+  : AthAlgTool(type, name, parent) {
+  declareProperty("CTPToChainMapping", m_ctpToChainProperty, "Mapping of the form: '34:HLT_x', '35:HLT_y', ..., both CTP ID and chain may appear many times");
+}
+
+
 CTPUnpackingTool::~CTPUnpackingTool()
 {}
 
+StatusCode CTPUnpackingTool::decodeCTPToChainMapping() {
+  std::istringstream input;
+  for ( auto entry: m_ctpToChainProperty) {
+    input.clear();
+    input.str(entry);
+    size_t ctpId;
+    input >> ctpId;
+    char delim;
+    input >> delim;    
+    if ( delim != ':' ) {
+      ATH_MSG_ERROR("Error in conf. entry: " << entry << " missing ':'");
+      return StatusCode::FAILURE;
+    }
+    std::string chainName;
+    input >> chainName;
+    ATH_MSG_DEBUG("Chain " << chainName << " seeded from CTP item of ID " << ctpId);
+    m_ctpToChain[ctpId].push_back(HLT::Identifier(chainName));
+  }
+  return StatusCode::SUCCESS;
+}
+
 
-StatusCode CTPUnpackingTool::decode(const ROIB::RoIBResult& roib, const IndexToIdentifiers& ctpToChain, HLT::IDVec& enabledChains) const {
+StatusCode CTPUnpackingTool::decode(const ROIB::RoIBResult& roib,  HLT::IDVec& enabledChains) const {
   size_t numberPfActivatedBits= 0;
   
   auto tav = roib.cTPResult().TAV();
@@ -23,8 +53,8 @@ StatusCode CTPUnpackingTool::decode(const ROIB::RoIBResult& roib, const IndexToI
       const bool decision = (tav[wordCounter].roIWord() & (1 << bitCounter)) > 0;
       if ( decision == true ) {
 	numberPfActivatedBits++;
-	auto itr = ctpToChain.find(ctpIndex);
-	if ( itr != ctpToChain.end() ) 
+	auto itr = m_ctpToChain.find(ctpIndex);
+	if ( itr != m_ctpToChain.end() ) 
 	  enabledChains.insert(enabledChains.end(), itr->second.begin(), itr->second.end());
       }
     }    
diff --git a/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.h b/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.h
index 63ad1bbf28f..5b54a800980 100644
--- a/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.h
+++ b/Trigger/TrigSteer/L1Decoder/src/CTPUnpackingTool.h
@@ -1,3 +1,4 @@
+// -*- c++ -*-
 /*
   Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
 */
@@ -28,7 +29,7 @@ public:
 		    const std::string& name, 
 		    const IInterface* parent );
   virtual ~CTPUnpackingTool();
-  typedef std::map<size_t, HLT::IDVec> IndexToIdentifiers;
+
 
   /*
     @brief The method decodes CTP bits content of the RoIBResult and fills the list of chains which are activated by those bits
@@ -36,11 +37,17 @@ public:
     @warning if the mapping is empty it means an empty menu. This condition is NOT checked and not reported.
     @warning if none of CTP bits is set this is also an error condition, this is the event should not have been passed to HLT
    */
-  StatusCode decode(const ROIB::RoIBResult&, const IndexToIdentifiers& ctpToChain, HLT::IDVec& enabledChains) const;
-
-  StatusCode initialize(){ return StatusCode::SUCCESS; }
+  StatusCode decode(const ROIB::RoIBResult& roib, HLT::IDVec& enabledChains) const;
 
-protected: 
+  StatusCode initialize(){ return decodeCTPToChainMapping(); }
+  
+  
+protected:
+  StatusCode decodeCTPToChainMapping();
+private:
+  typedef std::map<size_t, HLT::IDVec> IndexToIdentifiers;
+  IndexToIdentifiers       m_ctpToChain;
+  std::vector<std::string> m_ctpToChainProperty;
 
 }; 
 
diff --git a/Trigger/TrigSteer/L1Decoder/src/EMRoIsUnpackingTool.cxx b/Trigger/TrigSteer/L1Decoder/src/EMRoIsUnpackingTool.cxx
index 82b36f826c5..ac5d2a62a5f 100644
--- a/Trigger/TrigSteer/L1Decoder/src/EMRoIsUnpackingTool.cxx
+++ b/Trigger/TrigSteer/L1Decoder/src/EMRoIsUnpackingTool.cxx
@@ -16,9 +16,10 @@ EMRoIsUnpackingTool::EMRoIsUnpackingTool( const std::string& type,
   : AthAlgTool ( type, name, parent ),
     m_configSvc("TrigConf::LVL1ConfigSvc/LVL1ConfigSvc", name) {
 
-  declareProperty( "Decisions", m_decisionsKey, "Decisions for each RoI" );
-  declareProperty("OutputRoIs", m_trigRoIsKey, "Name of the RoIs object produced by the unpacker");
-  declareProperty("OutputRecEMTauRoIs", m_recEMTauRoIsKey, "Name of the RoIs object produced by the unpacker");
+  declareProperty( "Decisions", m_decisionsKey="RoIDecisions", "Decisions for each RoI" );
+  declareProperty("ThresholdToChainMapping", m_thresholdToChainProperty, "Mapping from the threshold name to chain in the form: 'EM3:HLT_e5', 'EM3:HLT_e5tight', ...");
+  declareProperty("OutputRoIs", m_trigRoIsKey="EMRoIs", "Name of the RoIs object produced by the unpacker");
+  declareProperty("OutputRecEMTauRoIs", m_recEMTauRoIsKey="RecEMRoIs", "Name of the RoIs object produced by the unpacker");
   declareProperty("RoIWidth", m_roIWidth = 0.1, "Size of RoI in eta/ phi");
 }
 
@@ -28,8 +29,11 @@ EMRoIsUnpackingTool::~EMRoIsUnpackingTool(){
 
 
 StatusCode EMRoIsUnpackingTool::initialize() {  
-  CHECK(m_configSvc.retrieve());
+  CHECK( m_configSvc.retrieve() );
+  CHECK( m_trigRoIsKey.initialize() );
+  CHECK( m_recEMTauRoIsKey.initialize() );
   //TODO add mapping retrieval
+  CHECK( decodeMapping() );
   return StatusCode::SUCCESS;
 }
 
diff --git a/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.cxx b/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.cxx
index b255eacb50d..45c93b66981 100644
--- a/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.cxx
+++ b/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.cxx
@@ -2,7 +2,7 @@
   Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
 */
 
-
+#include <iostream>
 // L1Decoder includes
 #include "IRoIsUnpackingTool.h"
 
@@ -13,7 +13,22 @@ IRoIsUnpackingTool::~IRoIsUnpackingTool()
 
 
 
-
+StatusCode IRoIsUnpackingTool::decodeMapping() {
+  std::istringstream input;
+  for ( auto entry: m_thresholdToChainProperty ) {
+    input.clear();
+    input.str(entry);
+    std::string thresholdName;
+    char delim;
+    std::string chainName;
+    input >> thresholdName >> delim >> chainName;
+    if ( delim != ':' ) {
+      return StatusCode::FAILURE;
+    }
+    m_thresholdToChainMapping[HLT::Identifier(thresholdName)].push_back(HLT::Identifier(chainName));
+  }
+  return StatusCode::SUCCESS;
+}
 
 
 
diff --git a/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.h b/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.h
index 57daa3d4775..1e0f38049bf 100644
--- a/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.h
+++ b/Trigger/TrigSteer/L1Decoder/src/IRoIsUnpackingTool.h
@@ -38,8 +38,12 @@ class IRoIsUnpackingTool
 			    const ROIB::RoIBResult& roib,
 			    const HLT::IDVec& activeChains) const = 0;
 
+  StatusCode decodeMapping();
+  
 protected:
   SG::WriteHandleKey<  TrigCompositeUtils::DecisionContainer> m_decisionsKey;
+  std::vector<std::string>              m_thresholdToChainProperty;
+  std::map<HLT::Identifier, HLT::IDVec> m_thresholdToChainMapping;
 }; 
 
 inline const InterfaceID& IRoIsUnpackingTool::interfaceID() { 
diff --git a/Trigger/TrigSteer/L1Decoder/src/L1Decoder.cxx b/Trigger/TrigSteer/L1Decoder/src/L1Decoder.cxx
index 0b7432694e2..d61183ab481 100644
--- a/Trigger/TrigSteer/L1Decoder/src/L1Decoder.cxx
+++ b/Trigger/TrigSteer/L1Decoder/src/L1Decoder.cxx
@@ -5,20 +5,26 @@
 #include "./L1Decoder.h"
 
 L1Decoder::L1Decoder(const std::string& name, ISvcLocator* pSvcLocator)
-  : AthReentrantAlgorithm(name, pSvcLocator) {
+  : AthReentrantAlgorithm(name, pSvcLocator),
+    m_ctpUnpacker("CTPUnpackingTool/CTPUnpackingTool", this),
+    m_roiUnpackers(this) {
+  
   declareProperty("RoIBResult", m_RoIBResultKey="RoIBResult", "Name of RoIBResult");
   declareProperty("Chains", m_chainsKey="HLTChains", "Chains status after L1 and prescaling");
+  declareProperty("ctpUnpacker", m_ctpUnpacker, "Tool used to unpack the CTP info");
+  declareProperty("roiUnpackers", m_roiUnpackers, "Tools unpacking RoIs");
 }
 
 StatusCode L1Decoder::initialize() {
   CHECK( m_RoIBResultKey.initialize() );
   CHECK( m_chainsKey.initialize() );
+
   CHECK( m_ctpUnpacker.retrieve() );
-  //  CHECK( m_roisUnpacker.retrieve() );
+  CHECK( m_roiUnpackers.retrieve() );
   //  CHECK( m_prescaler.retrieve() );
 
   
-  CHECK(readConfiguration());
+
   return StatusCode::SUCCESS;
 }
 
@@ -28,6 +34,7 @@ StatusCode L1Decoder::beginRun() {
   return StatusCode::SUCCESS;
 }
 
+
 StatusCode L1Decoder::readConfiguration() {
   return StatusCode::SUCCESS;
 }
@@ -42,7 +49,7 @@ StatusCode L1Decoder::execute_r (const EventContext& ctx) const {
   DecisionOutput chainsInfo;
   
   HLT::IDVec l1SeededChains;
-  CHECK( m_ctpUnpacker->decode(*roibH, m_ctpIDToChain, l1SeededChains) );
+  CHECK( m_ctpUnpacker->decode(*roibH, l1SeededChains) );
   sort(l1SeededChains.begin(), l1SeededChains.end()); // do so that following scaling is reproducable
 
   HLT::IDVec activeChains;
@@ -81,16 +88,17 @@ StatusCode L1Decoder::prescaleChains(const HLT::IDVec& active,
   for ( auto c: active ) {
     auto psInfo = m_prescalingInfo.find(c);
     if ( psInfo == m_prescalingInfo.end() )  {
-      ATH_MSG_ERROR("No prescaling information for the chain " << c);
-      return StatusCode::FAILURE;
-    }
+      ATH_MSG_INFO("No prescaling information for the chain, enabling it " << c);
+      notPrescaled.push_back(c);
+    } else {
     
-    // this code should then work
-    //    if ( scaler.decision( state, psInfo.second ) ) {
-    //      notPrescaled.push_back(c);
-    //      ATH_MSG_DEBUG("Chain " << c << " remained active after the HTL prescaling");
-    // but for now
-    notPrescaled.push_back(c);
+      // this code should then work
+      //    if ( scaler.decision( state, psInfo.second ) ) {
+      //      notPrescaled.push_back(c);
+      //      ATH_MSG_DEBUG("Chain " << c << " remained active after the HTL prescaling");
+      // but for now
+      notPrescaled.push_back(c);
+    }
   }
   return StatusCode::SUCCESS;
 }
diff --git a/Trigger/TrigSteer/L1Decoder/src/L1Decoder.h b/Trigger/TrigSteer/L1Decoder/src/L1Decoder.h
index c2938a2e432..d58deb74b46 100644
--- a/Trigger/TrigSteer/L1Decoder/src/L1Decoder.h
+++ b/Trigger/TrigSteer/L1Decoder/src/L1Decoder.h
@@ -40,23 +40,14 @@ protected: // protected to support unit testing
   StatusCode saveChainsInfo(const HLT::IDVec& chains,
 			    xAOD::TrigCompositeContainer* storage,
 			    const std::string& type) const;
-
   
 private:
   SG::ReadHandleKey<ROIB::RoIBResult> m_RoIBResultKey;
   SG::WriteHandleKey< TrigCompositeUtils::DecisionContainer > m_chainsKey;
 
-  ToolHandle<CTPUnpackingTool> m_ctpUnpacker;  
-  //  ToolHandle<PrescalingTool> m_prescaler;
-  ToolHandleArray<IRoIsUnpackingTool> m_roiUnpackers;
-  
-  CTPUnpackingTool::IndexToIdentifiers m_ctpIDToChain;
-  std::map<HLT::Identifier, float> m_prescalingInfo;
-
-
-  
+  ToolHandle<CTPUnpackingTool> m_ctpUnpacker; // = ToolHandle<CTPUnpackingTool>("CTPUnpackingTool/CTPUnpackingTool", this); // last arg makes it private tool    
+  //  ToolHandle<PrescalingTool> m_prescaler = ToolHandle<PrescalingTool>("PrescalingTool/PrescalingTool", this); 
+  ToolHandleArray<IRoIsUnpackingTool> m_roiUnpackers;  // = ToolHandleArray<IRoIsUnpackingTool>(this);    
+  std::map<HLT::Identifier, float> m_prescalingInfo;  
 };
-
-
-
 #endif
diff --git a/Trigger/TrigSteer/L1Decoder/src/components/L1Decoder_entries.cxx b/Trigger/TrigSteer/L1Decoder/src/components/L1Decoder_entries.cxx
index fa1df1eaa63..29aa6658482 100644
--- a/Trigger/TrigSteer/L1Decoder/src/components/L1Decoder_entries.cxx
+++ b/Trigger/TrigSteer/L1Decoder/src/components/L1Decoder_entries.cxx
@@ -4,13 +4,19 @@
 #include "../FakeRoI.h"
 #include "../FakeRoIView.h"
 //#include "../RoIGraph.h"
+#include "../L1Decoder.h"
 #include "../FakeCTP.h"
+#include "../CTPUnpackingTool.h"
+#include "../EMRoIsUnpackingTool.h"
 
 DECLARE_ALGORITHM_FACTORY(L1CaloDecoder)
 DECLARE_ALGORITHM_FACTORY(FakeRoI)
 //DECLARE_ALGORITHM_FACTORY(RoIGraph)
 DECLARE_ALGORITHM_FACTORY(FakeCTP)
+DECLARE_ALGORITHM_FACTORY(L1Decoder)
 DECLARE_NAMESPACE_ALGORITHM_FACTORY( AthViews, FakeRoIView )
+DECLARE_TOOL_FACTORY(CTPUnpackingTool)
+DECLARE_TOOL_FACTORY(EMRoIsUnpackingTool)
 
 DECLARE_FACTORY_ENTRIES( L1Decoder )
 {
@@ -18,5 +24,8 @@ DECLARE_FACTORY_ENTRIES( L1Decoder )
     DECLARE_ALGORITHM(FakeRoI)
       //  DECLARE_ALGORITHM(RoIGraph)
     DECLARE_ALGORITHM(FakeCTP)
+      DECLARE_ALGORITHM(L1Decoder)
     DECLARE_NAMESPACE_ALGORITHM( AthViews, FakeRoIView )
+      DECLARE_TOOL(CTPUnpackingTool)
+      DECLARE_TOOL(EMRoIsUnpackingTool)
 }
-- 
GitLab