ATLAS Offline Software
ExampleMonitorAlgorithm.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 ExampleMonitorAlgorithm::ExampleMonitorAlgorithm( const std::string& name, ISvcLocator* pSvcLocator )
8 :AthMonitorAlgorithm(name,pSvcLocator)
9 ,m_doRandom(true)
10 {}
11 
12 
14 
15 
17  using namespace Monitored;
18  // initialize superclass
20 
21  m_abGroups1 = buildToolMap<int>(m_tools,"ExampleMonitor",2);
22  m_abGroups2 = buildToolMap<std::vector<int>>(m_tools,"ExampleMonitor",4,2);
23 
24  std::vector<std::string> layers = {"layer1","layer2"};
25  std::vector<std::string> clusters = {"clusterX","clusterB"};
26  m_cGroups1 = buildToolMap<int>(m_tools,"ExampleMonitor",layers);
27  m_cGroups2 = buildToolMap<std::map<std::string,int>>(m_tools,"ExampleMonitor",layers,clusters);
28  return StatusCode::SUCCESS;
29 }
30 
31 
32 StatusCode ExampleMonitorAlgorithm::fillHistograms( const EventContext& ctx ) const {
33  using namespace Monitored;
34 
35  // Declare the quantities which should be monitored
36  auto lumiPerBCID = Monitored::Scalar<float>("lumiPerBCID",0.0);
37  auto lb = Monitored::Scalar<int>("lb",0);
38  auto run = Monitored::Scalar<int>("run",0);
39  auto random = Monitored::Scalar<float>("random",0.0);
40  auto testweight = Monitored::Scalar<float>("testweight",1.0);
41 
42  // Two variables (value and passed) needed for TEfficiency
43  auto pT = Monitored::Scalar<float>("pT",0.0);
44  auto pT_passed = Monitored::Scalar<bool>("pT_passed",false);
45 
46  // a string vector variable
47  std::vector<std::string> strvec{"alpha", "beta"};
48  auto mon_strvec = Monitored::Collection("strvec", strvec);
49  // a plain string
50  std::string mystring = "gamma";
51  auto mon_str = Monitored::Scalar<std::string>("str", mystring);
52 
53  // Set the values of the monitored variables for the event
54  lumiPerBCID = lbAverageInteractionsPerCrossing(ctx);
55  lb = GetEventInfo(ctx)->lumiBlock();
56  run = GetEventInfo(ctx)->runNumber();
57  testweight = 2.0;
58 
59  // Event number as string
60  auto mon_evtstr = Monitored::Scalar<std::string>("evtstr", std::to_string(ctx.eventID().event_number()));
61 
62  TRandom3 r(ctx.eventID().event_number());
63  // Example of using flags
64  if (m_doRandom) {
65  random = r.Rndm();
66  }
67 
68  // Fake efficiency calculator
69  pT = r.Landau(15);
70  pT_passed = pT>r.Poisson(15);
71 
72  // Example of passing a collection
73  std::vector<double> pT_vec{pT};
74  while (pT_vec.back() > 5) {
75  pT_vec.push_back(pT_vec.back() - 5);
76  }
77  auto mon_pT_vec = Monitored::Collection("pT_vec", pT_vec);
78 
79  // Fill. First argument is the tool name, all others are the variables to be saved.
80  fill("ExampleMonitor",lumiPerBCID,lb,random,pT,pT_passed,mon_pT_vec,testweight,mon_str,mon_strvec,mon_evtstr);
81 
82  // Alternative fill method. Get the group yourself, and pass it to the fill function.
83  auto tool = getGroup("ExampleMonitor");
84  fill(tool,run);
85 
86  // Fill with a vector; useful in some circumstances.
87  std::vector<std::reference_wrapper<Monitored::IMonitoredVariable>> varVec = {lumiPerBCID,pT};
88  fill("ExampleMonitor",varVec);
89  fill(tool,varVec);
90 
91  // Filling using a pre-defined array of groups.
92  auto a = Scalar<float>("a",0.0);
93  auto b = Scalar<float>("b",1.0);
94  auto c = Scalar<float>("c",2.0);
95  for ( auto iEta : {0,1} ) {
96  // 1) Valid but inefficient fill
97  fill("ExampleMonitor_"+std::to_string(iEta),a,b,c);
98  // 2) Faster way to fill a vector of histograms
100  for ( auto iPhi : {0,1} ) {
101  // Same efficient method for 2D array
103  }
104  }
105 
106  // Filling using a pre-defined map of groups.
107  for ( auto& layer : std::vector<std::string>({"layer1","layer2"}) ) {
108  fill(m_tools[m_cGroups1.at(layer)],c);
109  for ( auto& cluster : std::vector<std::string>({"clusterX","clusterB"}) ) {
110  // Same efficient method for 2D map
111  fill(m_tools[m_cGroups2.at(layer).at(cluster)],c);
112  }
113  }
114 
115  return StatusCode::SUCCESS;
116 }
beamspotman.r
def r
Definition: beamspotman.py:676
CalculateHighPtTerm.pT
pT
Definition: ICHEP2016/CalculateHighPtTerm.py:57
module_driven_slicing.layers
layers
Definition: module_driven_slicing.py:114
ExampleMonitorAlgorithm::~ExampleMonitorAlgorithm
virtual ~ExampleMonitorAlgorithm()
Definition: ExampleMonitorAlgorithm.cxx:13
ExampleMonitorAlgorithm::m_abGroups2
std::vector< std::vector< int > > m_abGroups2
Definition: ExampleMonitorAlgorithm.h:22
ExampleMonitorAlgorithm::fillHistograms
virtual StatusCode fillHistograms(const EventContext &ctx) const override
adds event to the monitoring histograms
Definition: ExampleMonitorAlgorithm.cxx:32
Monitored::Collection
ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
Definition: MonitoredCollection.h:38
AthMonitorAlgorithm
Base class for Athena Monitoring Algorithms.
Definition: AthMonitorAlgorithm.h:36
python.BunchSpacingUtils.lb
lb
Definition: BunchSpacingUtils.py:88
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
ExampleMonitorAlgorithm::m_abGroups1
std::vector< int > m_abGroups1
Definition: ExampleMonitorAlgorithm.h:21
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
run
Definition: run.py:1
AthMonitorAlgorithm::fill
void fill(const ToolHandle< GenericMonitoringTool > &groupHandle, std::vector< std::reference_wrapper< Monitored::IMonitoredVariable >> &&variables) const
Fills a vector of variables to a group by reference.
ExampleMonitorAlgorithm::m_cGroups1
std::map< std::string, int > m_cGroups1
Definition: ExampleMonitorAlgorithm.h:23
AthMonitorAlgorithm::GetEventInfo
SG::ReadHandle< xAOD::EventInfo > GetEventInfo(const EventContext &) const
Return a ReadHandle for an EventInfo object (get run/event numbers, etc.)
Definition: AthMonitorAlgorithm.cxx:107
ExampleMonitorAlgorithm.h
AthMonitorAlgorithm::m_tools
ToolHandleArray< GenericMonitoringTool > m_tools
Array of Generic Monitoring Tools.
Definition: AthMonitorAlgorithm.h:338
ExampleMonitorAlgorithm::m_doRandom
Gaudi::Property< bool > m_doRandom
Definition: ExampleMonitorAlgorithm.h:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
AthMonitorAlgorithm::lbAverageInteractionsPerCrossing
virtual float lbAverageInteractionsPerCrossing(const EventContext &ctx=Gaudi::Hive::currentContext()) const
Calculate the average mu, i.e.
Definition: AthMonitorAlgorithm.cxx:222
Trk::iPhi
@ iPhi
Definition: ParamDefs.h:53
AthMonitorAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: AthMonitorAlgorithm.cxx:18
a
TList * a
Definition: liststreamerinfos.cxx:10
RunTileMonitoring.clusters
clusters
Definition: RunTileMonitoring.py:133
ExampleMonitorAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: ExampleMonitorAlgorithm.cxx:16
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
ExampleMonitorAlgorithm::m_cGroups2
std::map< std::string, std::map< std::string, int > > m_cGroups2
Definition: ExampleMonitorAlgorithm.h:24
ExampleMonitorAlgorithm::ExampleMonitorAlgorithm
ExampleMonitorAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ExampleMonitorAlgorithm.cxx:7
xAOD::iEta
setScale setgFexType iEta
Definition: gFexJetRoI_v1.cxx:74
python.compressB64.c
def c
Definition: compressB64.py:93
AthMonitorAlgorithm::getGroup
const ToolHandle< GenericMonitoringTool > & getGroup(const std::string &name) const
Get a specific monitoring tool from the tool handle array.
Definition: AthMonitorAlgorithm.cxx:164