src
DQMServices
StreamerIO
plugins
DQMMonitoringService.cc
Go to the documentation of this file.
1
#include "
DQMMonitoringService.h
"
2
3
#include "
DataFormats/Provenance/interface/Timestamp.h
"
4
#include "
FWCore/MessageLogger/interface/MessageLogger.h
"
5
#include "
FWCore/ParameterSet/interface/ParameterSet.h
"
6
#include "
FWCore/ServiceRegistry/interface/ActivityRegistry.h
"
7
#include "
FWCore/ServiceRegistry/interface/GlobalContext.h
"
8
#include "
FWCore/Utilities/interface/StreamID.h
"
9
10
#include <cstdlib>
11
#include <ctime>
12
#include <exception>
13
#include <iostream>
14
15
#include <boost/property_tree/json_parser.hpp>
16
17
#include <fmt/printf.h>
18
19
/*
20
* This service is very similar to the FastMonitoringService in the HLT,
21
* except that it is used for monitoring online DQM applications
22
*/
23
24
namespace
dqmservices
{
25
26
DQMMonitoringService::DQMMonitoringService
(
const
edm::ParameterSet
&
pset
,
edm::ActivityRegistry
& ar) {
27
const
char
*
x
= std::getenv(
"DQM2_SOCKET"
);
28
if
(
x
) {
29
std::cerr
<<
"Monitoring pipe: "
<<
x
<< std::endl;
30
mstream_
.connect(boost::asio::local::stream_protocol::endpoint(
x
));
31
}
else
{
32
std::cerr
<<
"Monitoring file not found, disabling."
<< std::endl;
33
}
34
35
// init counters
36
nevents_
= 0;
37
38
last_lumi_
= 0;
39
last_lumi_nevents_
= 0;
40
last_lumi_time_
=
std::chrono::high_resolution_clock::now
();
41
42
run_
= 0;
43
lumi_
= 0;
44
45
ar.
watchPreGlobalBeginLumi
(
this
, &
DQMMonitoringService::evLumi
);
46
ar.
watchPreSourceEvent
(
this
, &
DQMMonitoringService::evEvent
);
47
}
48
49
void
DQMMonitoringService::outputLumiUpdate
() {
50
auto
now
=
std::chrono::high_resolution_clock::now
();
51
52
boost::property_tree::ptree
doc
;
53
54
// these might be different than the numbers we want to report
55
// rate/stats per lumi are calculated from last_*_ fields
56
doc
.put(
"cmssw_run"
,
run_
);
57
doc
.put(
"cmssw_lumi"
,
lumi_
);
58
doc
.put(
"events_total"
,
nevents_
);
59
60
// do statistics for the last (elapsed) ls
61
auto
lumi_millis = std::chrono::duration_cast<std::chrono::milliseconds>(
now
-
last_lumi_time_
).
count
();
62
auto
lumi_events =
nevents_
-
last_lumi_nevents_
;
63
auto
lumi_last =
last_lumi_
;
64
65
if
((lumi_last > 0) && (lumi_millis > 0)) {
66
doc
.put(
"lumi_number"
, lumi_last);
67
doc
.put(
"lumi_events"
, lumi_events);
68
doc
.put(
"lumi_duration_ms"
, lumi_millis);
69
70
float
rate
= ((
float
)lumi_events * 1000) / lumi_millis;
71
doc
.put(
"events_rate"
,
rate
);
72
73
// also save the history entry
74
boost::property_tree::ptree plumi;
75
plumi.put(
"n"
, lumi_last);
76
plumi.put(
"nevents"
, lumi_events);
77
plumi.put(
"nmillis"
, lumi_millis);
78
plumi.put(
"rate"
,
rate
);
79
80
std::time_t hkey =
std::time
(
nullptr
);
81
doc
.add_child(fmt::sprintf(
"extra.lumi_stats.%d"
, hkey), plumi);
82
}
83
84
outputUpdate
(
doc
);
85
}
86
87
void
DQMMonitoringService::evLumi
(
edm::GlobalContext
const
& iContext) {
88
// these might be different than the numbers we want to report
89
// rate/stats per lumi are calculated from last_*_ fields
90
run_
= iContext.
luminosityBlockID
().
run
();
91
lumi_
= iContext.
luminosityBlockID
().
luminosityBlock
();
92
93
outputLumiUpdate
();
94
95
last_lumi_time_
=
std::chrono::high_resolution_clock::now
();
96
last_lumi_nevents_
=
nevents_
;
97
last_lumi_
=
lumi_
;
98
}
99
100
void
DQMMonitoringService::evEvent
(
edm::StreamID
const
& iContext) {
101
nevents_
+= 1;
102
tryUpdate
();
103
}
104
105
void
DQMMonitoringService::outputUpdate
(boost::property_tree::ptree&
doc
) {
106
if
(!
mstream_
)
107
return
;
108
109
try
{
110
last_update_time_
=
std::chrono::high_resolution_clock::now
();
111
doc
.put(
"update_timestamp"
,
std::time
(
nullptr
));
112
113
write_json(
mstream_
,
doc
,
false
);
114
mstream_
.flush();
115
}
catch
(
std::exception
const
& exc) {
116
LogDebug
(
"DQMMonitoringService"
) <<
"Exception thrown in outputUpdate method: "
<< exc.what();
117
}
118
}
119
120
void
DQMMonitoringService::keepAlive
() {
121
if
(!
mstream_
)
122
return
;
123
124
mstream_
<<
"\n"
;
125
mstream_
.flush();
126
127
tryUpdate
();
128
}
129
130
void
DQMMonitoringService::tryUpdate
() {
131
if
(!
mstream_
)
132
return
;
133
134
// sometimes we don't see any transition for a very long time
135
// but we still want updates
136
// luckily, keepAlive is called rather often by the input source
137
auto
const
now
=
std::chrono::high_resolution_clock::now
();
138
auto
const
millis = std::chrono::duration_cast<std::chrono::milliseconds>(
now
-
last_update_time_
).
count
();
139
if
(millis >= (25 * 1000)) {
140
outputLumiUpdate
();
141
}
142
}
143
144
}
// namespace dqmservices
145
146
#include "
FWCore/ServiceRegistry/interface/ServiceMaker.h
"
147
148
using
dqmservices::DQMMonitoringService
;
149
DEFINE_FWK_SERVICE
(
DQMMonitoringService
);
dqmservices::DQMMonitoringService::evEvent
void evEvent(edm::StreamID const &)
Definition:
DQMMonitoringService.cc:100
dqmservices::DQMMonitoringService::last_lumi_
unsigned long last_lumi_
Definition:
DQMMonitoringService.h:50
edm::LuminosityBlockID::luminosityBlock
LuminosityBlockNumber_t luminosityBlock() const
Definition:
LuminosityBlockID.h:32
DQMMonitoringService.h
edm::GlobalContext
Definition:
GlobalContext.h:30
MessageLogger.h
dqmservices::DQMMonitoringService::keepAlive
void keepAlive()
Definition:
DQMMonitoringService.cc:120
dqmservices
Definition:
DQMFileIterator.cc:19
edm::StreamID
Definition:
StreamID.h:30
dqmservices::DQMMonitoringService::lumi_
unsigned long lumi_
Definition:
DQMMonitoringService.h:53
dqmservices::DQMMonitoringService::evLumi
void evLumi(edm::GlobalContext const &)
Definition:
DQMMonitoringService.cc:87
edm::ActivityRegistry::watchPreGlobalBeginLumi
void watchPreGlobalBeginLumi(PreGlobalBeginLumi::slot_type const &iSlot)
Definition:
ActivityRegistry.h:445
muonDTDigis_cfi.pset
pset
Definition:
muonDTDigis_cfi.py:27
hcalRecHitTable_cff.time
time
Definition:
hcalRecHitTable_cff.py:14
dqmservices::DQMMonitoringService::last_lumi_nevents_
long last_lumi_nevents_
Definition:
DQMMonitoringService.h:48
dqmservices::DQMMonitoringService::outputLumiUpdate
void outputLumiUpdate()
Definition:
DQMMonitoringService.cc:49
dqmservices::DQMMonitoringService::run_
unsigned long run_
Definition:
DQMMonitoringService.h:52
ParameterSet.h
cppFunctionSkipper.exception
exception
Definition:
cppFunctionSkipper.py:10
ServiceMaker.h
dqmservices::DQMMonitoringService
Definition:
DQMMonitoringService.h:23
nano_mu_digi_cff.float
float
Definition:
nano_mu_digi_cff.py:14
beamspotdip_dqm_sourceclient-live_cfg.DQMMonitoringService
DQMMonitoringService
Definition:
beamspotdip_dqm_sourceclient-live_cfg.py:57
StreamID.h
Timestamp.h
edm::GlobalContext::luminosityBlockID
LuminosityBlockID const & luminosityBlockID() const
Definition:
GlobalContext.h:62
submitPVResolutionJobs.count
count
Definition:
submitPVResolutionJobs.py:359
edm::LuminosityBlockID::run
RunNumber_t run() const
Definition:
LuminosityBlockID.h:31
dqmservices::DQMMonitoringService::mstream_
boost::asio::local::stream_protocol::iostream mstream_
Definition:
DQMMonitoringService.h:40
DEFINE_FWK_SERVICE
#define DEFINE_FWK_SERVICE(type)
Definition:
ServiceMaker.h:97
GlobalContext.h
dqmservices::DQMMonitoringService::last_lumi_time_
std::chrono::high_resolution_clock::time_point last_lumi_time_
Definition:
DQMMonitoringService.h:46
DMR_cfg.cerr
cerr
Definition:
DMR_cfg.py:78
ActivityRegistry.h
RPCpg::rate
double rate(double x)
Definition:
Constants.cc:3
hcalRecHitTable_cff.doc
doc
Definition:
hcalRecHitTable_cff.py:8
dqmservices::DQMMonitoringService::outputUpdate
void outputUpdate(boost::property_tree::ptree &doc)
Definition:
DQMMonitoringService.cc:105
dqmservices::DQMMonitoringService::DQMMonitoringService
DQMMonitoringService(const edm::ParameterSet &, edm::ActivityRegistry &)
Definition:
DQMMonitoringService.cc:26
x
float x
Definition:
beamSpotDipStandalone.cc:55
edm::ParameterSet
Definition:
ParameterSet.h:48
edm::ActivityRegistry::watchPreSourceEvent
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
Definition:
ActivityRegistry.h:211
submitPVValidationJobs.now
now
Definition:
submitPVValidationJobs.py:644
dqmservices::DQMMonitoringService::last_update_time_
std::chrono::high_resolution_clock::time_point last_update_time_
Definition:
DQMMonitoringService.h:47
dqmservices::DQMMonitoringService::nevents_
long nevents_
Definition:
DQMMonitoringService.h:43
dqmservices::DQMMonitoringService::tryUpdate
void tryUpdate()
Definition:
DQMMonitoringService.cc:130
edm::ActivityRegistry
Definition:
ActivityRegistry.h:136
LogDebug
#define LogDebug(id)
Definition:
MessageLogger.h:241
Generated for CMSSW Reference Manual by
1.8.14