ATLAS Offline Software
Loading...
Searching...
No Matches
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,std::move(clusters));
20 return StatusCode::SUCCESS;
21}
22
23
24StatusCode 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 const 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
91 fill(m_tools[m_abGroups1[iEta]],a,b,c);
92 for ( auto iPhi : {0,1} ) {
93 // Same efficient method for 2D array
94 fill(m_tools[m_abGroups2[iEta][iPhi]],a,b);
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
static Double_t a
const ToolHandle< GenericMonitoringTool > & getGroup(const std::string &name) const
Get a specific monitoring tool from the tool handle array.
virtual StatusCode initialize() override
initialize
SG::ReadHandle< xAOD::EventInfo > GetEventInfo(const EventContext &) const
Return a ReadHandle for an EventInfo object (get run/event numbers, etc.)
ToolHandleArray< GenericMonitoringTool > m_tools
Array of Generic Monitoring Tools.
std::map< std::string, std::map< std::string, int > > m_cGroups2
virtual StatusCode initialize() override
initialize
std::vector< std::vector< int > > m_abGroups2
Gaudi::Property< bool > m_doRandom
std::map< std::string, int > m_cGroups1
virtual StatusCode fillHistograms(const EventContext &ctx) const override
adds event to the monitoring histograms
Declare a monitored scalar variable.
int lb
Definition globals.cxx:23
int r
Definition globals.cxx:22
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.
virtual float lbAverageInteractionsPerCrossing(const EventContext &ctx=Gaudi::Hive::currentContext()) const
Calculate the average mu, i.e.
Generic monitoring tool for athena components.
std::vector< V > buildToolMap(const ToolHandleArray< GenericMonitoringTool > &tools, const std::string &baseName, int nHist)
Builds an array of indices (base case)
ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
Definition run.py:1