From 145fddb88e42869b19d84642244e28430eaf2752 Mon Sep 17 00:00:00 2001
From: Kenji Hamano <kenji.hamano@cern.ch>
Date: Tue, 11 Jun 2019 10:42:12 -0700
Subject: [PATCH] METMon MT initial files

---
 .../TrigMETMonitoring/CMakeLists.txt          |  12 +-
 .../python/TrigMETMonitorAlgorithm.py         | 143 ++++++++++++++++++
 .../TrigMETMonitorAlgorithm_jobOptions.py     |  41 +++++
 .../src/TrigMETMonitorAlgorithm.cxx           |  47 ++++++
 .../src/TrigMETMonitorAlgorithm.h             |  19 +++
 .../components/TrigMETMonitoring_entries.cxx  |   2 +
 6 files changed, 261 insertions(+), 3 deletions(-)
 create mode 100644 Trigger/TrigMonitoring/TrigMETMonitoring/python/TrigMETMonitorAlgorithm.py
 create mode 100644 Trigger/TrigMonitoring/TrigMETMonitoring/share/TrigMETMonitorAlgorithm_jobOptions.py
 create mode 100644 Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.cxx
 create mode 100644 Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.h

diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/CMakeLists.txt b/Trigger/TrigMonitoring/TrigMETMonitoring/CMakeLists.txt
index 077e38eed02f..b6f9a4eba7f8 100644
--- a/Trigger/TrigMonitoring/TrigMETMonitoring/CMakeLists.txt
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/CMakeLists.txt
@@ -17,6 +17,7 @@ atlas_depends_on_subdirs( PUBLIC
                           Trigger/TrigEvent/TrigSteeringEvent
                           Trigger/TrigMonitoring/TrigHLTMonitoring
                           PRIVATE
+			  Control/AthenaMonitoring
                           Control/StoreGate
                           GaudiKernel
                           Trigger/TrigConfiguration/TrigConfL1Data )
@@ -29,11 +30,16 @@ find_package( ROOT COMPONENTS Core Tree MathCore Hist RIO pthread MathMore Minui
 atlas_add_component( TrigMETMonitoring
                      src/*.cxx src/components/*.cxx
                      INCLUDE_DIRS ${ROOT_INCLUDE_DIRS} ${CLHEP_INCLUDE_DIRS}
-                     LINK_LIBRARIES ${ROOT_LIBRARIES} ${CLHEP_LIBRARIES} GaudiKernel TrigConfL1Data 
-                     xAODEgamma xAODMissingET xAODMuon xAODTrigMissingET xAODTrigger MissingETEvent 
-                     TrigMissingEtEvent TrigSteeringEvent TrigHLTMonitoringLib StoreGateLib SGtests )
+                     LINK_LIBRARIES ${ROOT_LIBRARIES} ${CLHEP_LIBRARIES} GaudiKernel 
+		     AthenaMonitoringLib
+		     TrigConfL1Data 
+                     xAODEgamma xAODMissingET xAODMuon xAODTrigMissingET xAODTrigger 
+		     MissingETEvent 
+                     TrigMissingEtEvent TrigSteeringEvent TrigHLTMonitoringLib 
+		     StoreGateLib SGtests )
 
 # Install files from the package:
+atlas_install_headers( TrigMETMonitoring )
 atlas_install_python_modules( python/*.py )
 atlas_install_joboptions( share/*.py )
 
diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/python/TrigMETMonitorAlgorithm.py b/Trigger/TrigMonitoring/TrigMETMonitoring/python/TrigMETMonitorAlgorithm.py
new file mode 100644
index 000000000000..969a9ee3c1c8
--- /dev/null
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/python/TrigMETMonitorAlgorithm.py
@@ -0,0 +1,143 @@
+#
+#  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+#
+
+'''@file TrigMETMonitoringAlgorithm.py
+@author K. Hamano
+'''
+
+
+def TrigMETMonitoringConfig(inputFlags):
+    '''Function to configures some algorithms in the monitoring system.'''
+
+    ### STEP 1 ###
+    # Define one top-level monitoring algorithm. The new configuration 
+    # framework uses a component accumulator.
+    from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
+    result = ComponentAccumulator()
+
+    # The following class will make a sequence, configure algorithms, and link
+    # them to GenericMonitoringTools
+    from AthenaMonitoring import AthMonitorCfgHelper
+    helper = AthMonitorCfgHelper(inputFlags,'TrigMETAthMonitorCfg')
+
+
+    ### STEP 2 ###
+    # Adding an algorithm to the helper. Here, we will use the example 
+    # algorithm in the AthenaMonitoring package. Just pass the type to the 
+    # helper. Then, the helper will instantiate an instance and set up the 
+    # base class configuration following the inputFlags. The returned object 
+    # is the algorithm.
+    #The added algorithm must exist as a .h file 
+
+    from TrigMETMonitoring.TrigMETMonitoringConfig import TrigMETMonitorAlgorithm
+    trigMETMonAlg = helper.addAlgorithm(TrigMETMonitorAlgorithm,'TrigMETMonAlg')
+
+    # You can actually make multiple instances of the same algorithm and give 
+    # them different configurations
+    shifterTrigMETMonAlg = helper.addAlgorithm(TrigMETMonitorAlgorithm,'ShifterTrigMETMonAlg')
+
+    # # If for some really obscure reason you need to instantiate an algorithm
+    # # yourself, the AddAlgorithm method will still configure the base 
+    # # properties and add the algorithm to the monitoring sequence.
+    # helper.AddAlgorithm(myExistingAlg)
+
+
+    ### STEP 3 ###
+    # Edit properties of a algorithm
+    # some generic property
+    # trigMETMonAlg.RandomHist = True
+    # to enable a trigger filter, for example:
+    #trigMETMonAlg.TriggerChain = 'HLT_mu26_ivarmedium'
+    trigMETMonAlg.TriggerChain = 'HLT_e24_lhtight_nod0'
+
+    ### STEP 4 ###
+    # Add some tools. N.B. Do not use your own trigger decion tool. Use the
+    # standard one that is included with AthMonitorAlgorithm.
+
+    # # First, add a tool that's set up by a different configuration function. 
+    # # In this case, CaloNoiseToolCfg returns its own component accumulator, 
+    # # which must be merged with the one from this function.
+    # from CaloTools.CaloNoiseToolConfig import CaloNoiseToolCfg
+    # caloNoiseAcc, caloNoiseTool = CaloNoiseToolCfg(inputFlags)
+    # result.merge(caloNoiseAcc)
+    # trigMETMonAlg.CaloNoiseTool = caloNoiseTool
+
+    # # Then, add a tool that doesn't have its own configuration function. In
+    # # this example, no accumulator is returned, so no merge is necessary.
+    # from MyDomainPackage.MyDomainPackageConf import MyDomainTool
+    # trigMETMonAlg.MyDomainTool = MyDomainTool()
+
+    # Add a generic monitoring tool (a "group" in old language). The returned 
+    # object here is the standard GenericMonitoringTool.
+    myGroup = helper.addGroup(
+        trigMETMonAlg,
+        'TrigMETMonitor',
+        'HLT/METMon/Expert/'
+    )
+
+    # Add a GMT for the other example monitor algorithm
+    shifterGroup = helper.addGroup(shifterTrigMETMonAlg,'TrigMETMonitor','HLT/METMon/Shifter/')
+
+    ### STEP 5 ###
+    # Configure histograms
+    #NB! The histograms defined here must match the ones in the cxx file exactly
+    myGroup.defineHistogram('lumiPerBCID',title='Luminosity;L/BCID;Events',
+                            path='ToRuleThemAll',xbins=10,xmin=0.0,xmax=10.0)
+    myGroup.defineHistogram('lb', title='Luminosity Block;lb;Events',
+                            path='ToFindThem',xbins=1000,xmin=-0.5,xmax=999.5)
+    #myGroup.defineHistogram('random', title='LB;x;Events',
+    #                        path='ToBringThemAll',xbins=30,xmin=0,xmax=1,opt='kLBNHistoryDepth=10')
+    #myGroup.defineHistogram('pT_passed,pT',type='TEfficiency',title='Test TEfficiency;x;Eff',
+    #                        path='AndInTheDarkness',xbins=100,xmin=0.0,xmax=50.0)
+
+    shifterGroup.defineHistogram('run',title='Run Number;run;Events',
+                                  path='SomePath',xbins=1000000,xmin=-0.5,xmax=999999.5)
+
+    ### STEP 6 ###
+    # Finalize. The return value should be a tuple of the ComponentAccumulator
+    # and the sequence containing the created algorithms. If we haven't called
+    # any configuration other than the AthMonitorCfgHelper here, then we can 
+    # just return directly (and not create "result" above)
+    return helper.result()
+    
+    # # Otherwise, merge with result object and return
+    # acc = helper.result()
+    # result.merge(acc)
+    # return result
+
+if __name__=='__main__':
+    # Setup the Run III behavior
+    from AthenaCommon.Configurable import Configurable
+    Configurable.configurableRun3Behavior = 1
+
+    # Setup logs
+    from AthenaCommon.Logging import log
+    from AthenaCommon.Constants import DEBUG,INFO
+    log.setLevel(DEBUG)
+
+    # Set the Athena configuration flags
+    from AthenaConfiguration.AllConfigFlags import ConfigFlags
+    nightly = '/cvmfs/atlas-nightlies.cern.ch/repo/data/data-art/CommonInputs/'
+    file = 'data16_13TeV.00311321.physics_Main.recon.AOD.r9264/AOD.11038520._000001.pool.root.1'
+    ConfigFlags.Input.Files = [nightly+file]
+    ConfigFlags.Input.isMC = False
+    ConfigFlags.Output.HISTFileName = 'TrigMETMonitorOutput.root'
+    
+    ConfigFlags.lock()
+
+    # Initialize configuration object, add accumulator, merge, and run.
+    from AthenaConfiguration.MainServicesConfig import MainServicesSerialCfg 
+    from AthenaPoolCnvSvc.PoolReadConfig import PoolReadCfg
+    cfg = MainServicesSerialCfg()
+    cfg.merge(PoolReadCfg(ConfigFlags))
+
+    trigMETMonitorAcc = TrigMETMonConfig(ConfigFlags)
+    cfg.merge(trigMETMonitorAcc)
+
+    # If you want to turn on more detailed messages ...
+    #trigMETMonitorAcc.getEventAlgo('TrigMETMonAlg').OutputLevel = 2 # DEBUG
+    cfg.printConfig(withDetails=True) # set True for exhaustive info
+
+    cfg.run() #use cfg.run(20) to only run on first 20 events
+
diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/share/TrigMETMonitorAlgorithm_jobOptions.py b/Trigger/TrigMonitoring/TrigMETMonitoring/share/TrigMETMonitorAlgorithm_jobOptions.py
new file mode 100644
index 000000000000..686f0fe6e8df
--- /dev/null
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/share/TrigMETMonitorAlgorithm_jobOptions.py
@@ -0,0 +1,41 @@
+#
+#  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+#
+
+'''@file TrigMETMonitorAlgorithm_jobOptions.py
+@author E. Bergeaas Kuutmann
+@date 2019-04-16
+@brief Example trigger python configuration for the Run III AthenaMonitoring package (old jobOptions), based on the example by C Burton and P Onyisi
+'''
+
+# The following class will make a sequence, configure algorithms, and link
+# them to GenericMonitoringTools
+from AthenaMonitoring import AthMonitorCfgHelperOld
+helper = AthMonitorCfgHelperOld(DQMonFlags, "TrigMETMonitor")
+
+### STEP 2 ###
+# Adding an algorithm to the helper. Here, we will use the example 
+# algorithm in the AthenaMonitoring package. Just pass the type to the 
+# helper. Then, the helper will instantiate an instance and set up the 
+# base class configuration following the inputFlags. The returned object 
+# is the algorithm.
+from TrigMETMonitoring.TrigMETMonitoringConf import TrigMETMonitorAlgorithm
+trigMETMonAlg = helper.addAlgorithm(TrigMETMonitorAlgorithm,'TrigMETMonAlg')
+
+# Examples of setting a trigger, or of running with debug messages on
+#trigMETMonAlg.TriggerChain = 'HLT_mu26_ivarmedium'
+#trigMETMonAlg.OutputLevel = DEBUG
+
+myGroup = helper.addGroup( trigMETMonAlg,
+        "TrigMETMonitor",
+        "HLT/METMon/Expert"
+    )
+
+myGroup.defineHistogram("lumiPerBCID;lumiPerBCID", title="Luminosity;L/BCID;Events",
+                        path='lumi',xbins=10,xmin=0.0,xmax=10.0)
+myGroup.defineHistogram("lb;lb", title="Luminosity Block;lb;Events",
+                        path='lbpath',xbins=1000,xmin=-0.5,xmax=999.5)
+myGroup.defineHistogram("random;random", title="LB;x;Events",
+                        path='randompath',xbins=30,xmin=0,xmax=1)
+
+topSequence += helper.result()
diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.cxx b/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.cxx
new file mode 100644
index 000000000000..45aac3edbf7c
--- /dev/null
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.cxx
@@ -0,0 +1,47 @@
+/*
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+*/
+
+#include "TrigMETMonitorAlgorithm.h"
+
+TrigMETMonitorAlgorithm::TrigMETMonitorAlgorithm( const std::string& name, ISvcLocator* pSvcLocator )
+  : AthMonitorAlgorithm(name,pSvcLocator)
+{}
+
+
+TrigMETMonitorAlgorithm::~TrigMETMonitorAlgorithm() {}
+
+
+StatusCode TrigMETMonitorAlgorithm::initialize() {
+    return AthMonitorAlgorithm::initialize();
+}
+
+
+StatusCode TrigMETMonitorAlgorithm::fillHistograms( const EventContext& ctx ) const {
+    using namespace Monitored;
+
+    // Declare the quantities which should be monitored
+    //NB! The variables and histograms defined here must match the ones in the py file exactly!
+    auto lumiPerBCID = Monitored::Scalar<float>("lumiPerBCID",0.0);
+    auto lb = Monitored::Scalar<int>("lb",0);
+    auto run = Monitored::Scalar<int>("run",0);
+
+    // Two variables (value and passed) needed for TEfficiency
+    auto pT = Monitored::Scalar<float>("pT",0.0);
+    auto pT_passed = Monitored::Scalar<float>("pT_passed",false);
+
+    //// Set the values of the monitored variables for the event		
+    lumiPerBCID = lbAverageInteractionsPerCrossing();
+    lb = GetEventInfo(ctx)->lumiBlock();
+    run = GetEventInfo(ctx)->runNumber();
+    
+    // Fill. First argument is the tool (GMT) name as defined in the py file, 
+    // all others are the variables to be saved.
+    fill("TrigMETMonitor",lumiPerBCID,lb);
+
+    // Alternative fill method. Get the group yourself, and pass it to the fill function.
+    auto tool = getGroup("TrigMETMonitor");
+    fill(tool,run);
+
+    return StatusCode::SUCCESS;
+}
diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.h b/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.h
new file mode 100644
index 000000000000..e9098ec5aa94
--- /dev/null
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/src/TrigMETMonitorAlgorithm.h
@@ -0,0 +1,19 @@
+/*
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+*/
+
+#ifndef TRIGMETMONITORING_TRIGMETMONITORALGORITHM_H
+#define TRIGMETMONITORING_TRIGMETMONITORALGORITHM_H
+
+#include "AthenaMonitoring/AthMonitorAlgorithm.h"
+#include "AthenaMonitoring/Monitored.h"
+
+
+class TrigMETMonitorAlgorithm : public AthMonitorAlgorithm {
+ public:
+  TrigMETMonitorAlgorithm( const std::string& name, ISvcLocator* pSvcLocator );
+  virtual ~TrigMETMonitorAlgorithm();
+  virtual StatusCode initialize() override;
+  virtual StatusCode fillHistograms( const EventContext& ctx ) const override;
+};
+#endif
diff --git a/Trigger/TrigMonitoring/TrigMETMonitoring/src/components/TrigMETMonitoring_entries.cxx b/Trigger/TrigMonitoring/TrigMETMonitoring/src/components/TrigMETMonitoring_entries.cxx
index b098b6722fd5..f5ad5fdf365e 100644
--- a/Trigger/TrigMonitoring/TrigMETMonitoring/src/components/TrigMETMonitoring_entries.cxx
+++ b/Trigger/TrigMonitoring/TrigMETMonitoring/src/components/TrigMETMonitoring_entries.cxx
@@ -1,6 +1,8 @@
 
 #include "TrigMETMonitoring/HLTMETMonTool.h"
+#include "../TrigMETMonitorAlgorithm.h"
 
 
 DECLARE_COMPONENT( HLTMETMonTool )
+DECLARE_COMPONENT( TrigMETMonitorAlgorithm )
 
-- 
GitLab