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