File indexing completed on 2024-11-28 03:56:19
0001
0002
0003
0004
0005
0006
0007 #include <atomic>
0008 #include <fstream>
0009
0010 #include <fmt/printf.h>
0011
0012 #include "CondFormats/DataRecord/interface/L1TUtmTriggerMenuRcd.h"
0013 #include "CondFormats/L1TObjects/interface/L1TUtmAlgorithm.h"
0014 #include "CondFormats/L1TObjects/interface/L1TUtmTriggerMenu.h"
0015 #include "DataFormats/Common/interface/Handle.h"
0016 #include "DataFormats/L1TGlobal/interface/GlobalAlgBlk.h"
0017 #include "DataFormats/L1TGlobal/interface/GlobalExtBlk.h"
0018 #include "EventFilter/Utilities/interface/EvFDaqDirector.h"
0019 #include "EventFilter/Utilities/interface/FastMonitor.h"
0020 #include "EventFilter/Utilities/interface/FastMonitoringService.h"
0021 #include "EventFilter/Utilities/interface/JSONSerializer.h"
0022 #include "EventFilter/Utilities/interface/JsonMonitorable.h"
0023 #include "FWCore/Framework/interface/Event.h"
0024 #include "FWCore/Framework/interface/EventSetup.h"
0025 #include "FWCore/Framework/interface/LuminosityBlock.h"
0026 #include "FWCore/Framework/interface/Run.h"
0027 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0031 #include "FWCore/ServiceRegistry/interface/Service.h"
0032 #include "FWCore/Utilities/interface/Adler32Calculator.h"
0033
0034 using namespace jsoncollector;
0035
0036 struct L1TriggerJSONMonitoringData {
0037
0038 static constexpr const int kPrescaleUndefined = -2;
0039 static constexpr const int kPrescaleConflict = -1;
0040
0041 struct stream {
0042 unsigned int processed = 0;
0043 std::vector<unsigned int> l1tAccept;
0044 std::vector<unsigned int> l1tAcceptPhysics;
0045 std::vector<unsigned int> l1tAcceptCalibration;
0046 std::vector<unsigned int> l1tAcceptRandom;
0047 std::vector<unsigned int> tcdsAccept;
0048 int prescaleIndex = kPrescaleUndefined;
0049 };
0050
0051
0052 struct run {
0053 std::string streamDestination;
0054 std::string streamMergeType;
0055 std::string baseRunDir;
0056 std::string jsdFileName;
0057 };
0058
0059
0060 struct lumisection {
0061 jsoncollector::HistoJ<unsigned int> processed;
0062 jsoncollector::HistoJ<unsigned int> l1tAccept;
0063 jsoncollector::HistoJ<unsigned int> l1tAcceptPhysics;
0064 jsoncollector::HistoJ<unsigned int>
0065 l1tAcceptCalibration;
0066 jsoncollector::HistoJ<unsigned int> l1tAcceptRandom;
0067 jsoncollector::HistoJ<unsigned int> tcdsAccept;
0068 int prescaleIndex = kPrescaleUndefined;
0069 };
0070 };
0071
0072 class L1TriggerJSONMonitoring : public edm::global::EDAnalyzer<
0073
0074 edm::StreamCache<L1TriggerJSONMonitoringData::stream>,
0075
0076 edm::RunCache<L1TriggerJSONMonitoringData::run>,
0077
0078 edm::LuminosityBlockSummaryCache<L1TriggerJSONMonitoringData::lumisection> > {
0079 public:
0080
0081 explicit L1TriggerJSONMonitoring(const edm::ParameterSet&);
0082
0083
0084 ~L1TriggerJSONMonitoring() override = default;
0085
0086
0087 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0088
0089
0090 void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0091
0092
0093
0094
0095 std::unique_ptr<L1TriggerJSONMonitoringData::stream> beginStream(edm::StreamID) const override;
0096
0097
0098 void streamBeginLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const override;
0099
0100
0101
0102
0103 std::shared_ptr<L1TriggerJSONMonitoringData::run> globalBeginRun(edm::Run const&,
0104 edm::EventSetup const&) const override;
0105
0106
0107 void globalEndRun(edm::Run const&, edm::EventSetup const&) const override;
0108
0109
0110
0111
0112 std::shared_ptr<L1TriggerJSONMonitoringData::lumisection> globalBeginLuminosityBlockSummary(
0113 edm::LuminosityBlock const&, edm::EventSetup const&) const override;
0114
0115
0116 void streamEndLuminosityBlockSummary(edm::StreamID,
0117 edm::LuminosityBlock const&,
0118 edm::EventSetup const&,
0119 L1TriggerJSONMonitoringData::lumisection*) const override;
0120
0121
0122 void globalEndLuminosityBlockSummary(edm::LuminosityBlock const&,
0123 edm::EventSetup const&,
0124 L1TriggerJSONMonitoringData::lumisection*) const override;
0125
0126 private:
0127
0128
0129 static constexpr const std::array<const char*, 16> tcdsTriggerTypes_ = {{
0130 "Error",
0131 "Physics",
0132 "Calibration",
0133 "Random",
0134 "Auxiliary",
0135 "",
0136 "",
0137 "",
0138 "Cyclic",
0139 "Bunch-pattern",
0140 "Software",
0141 "TTS",
0142 "",
0143 "",
0144 "",
0145 ""
0146 }};
0147
0148 static constexpr const char* streamName_ = "streamL1Rates";
0149
0150 static void writeJsdFile(L1TriggerJSONMonitoringData::run const&);
0151 static void writeIniFile(L1TriggerJSONMonitoringData::run const&, unsigned int, std::vector<std::string> const&);
0152
0153
0154 const edm::InputTag level1Results_;
0155 const edm::EDGetTokenT<GlobalAlgBlkBxCollection> level1ResultsToken_;
0156 const edm::ESGetToken<L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd> l1tUtmTriggerMenuRcdToken_;
0157 };
0158
0159
0160 constexpr const std::array<const char*, 16> L1TriggerJSONMonitoring::tcdsTriggerTypes_;
0161
0162
0163 L1TriggerJSONMonitoring::L1TriggerJSONMonitoring(edm::ParameterSet const& config)
0164 : level1Results_(config.getParameter<edm::InputTag>("L1Results")),
0165 level1ResultsToken_(consumes<GlobalAlgBlkBxCollection>(level1Results_)),
0166 l1tUtmTriggerMenuRcdToken_(esConsumes<edm::Transition::BeginRun>()) {
0167 if (edm::Service<evf::EvFDaqDirector>().isAvailable()) {
0168
0169 std::string initFileName = edm::Service<evf::EvFDaqDirector>()->getInitTempFilePath("streamL1Rates");
0170 std::ofstream file(initFileName);
0171 if (!file)
0172 throw cms::Exception("L1TriggerJsonMonitoring")
0173 << "Cannot create INITEMP file: " << initFileName << " error: " << strerror(errno);
0174 file.close();
0175 }
0176 }
0177
0178
0179 void L1TriggerJSONMonitoring::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0180 edm::ParameterSetDescription desc;
0181 desc.add<edm::InputTag>("L1Results", edm::InputTag("hltGtStage2Digis"));
0182 descriptions.add("L1TriggerJSONMonitoring", desc);
0183 }
0184
0185
0186 std::unique_ptr<L1TriggerJSONMonitoringData::stream> L1TriggerJSONMonitoring::beginStream(edm::StreamID) const {
0187 return std::make_unique<L1TriggerJSONMonitoringData::stream>();
0188 }
0189
0190
0191 std::shared_ptr<L1TriggerJSONMonitoringData::run> L1TriggerJSONMonitoring::globalBeginRun(
0192 edm::Run const& run, edm::EventSetup const& setup) const {
0193 auto rundata = std::make_shared<L1TriggerJSONMonitoringData::run>();
0194
0195
0196 if (edm::Service<evf::EvFDaqDirector>().isAvailable()) {
0197 rundata->streamDestination = edm::Service<evf::EvFDaqDirector>()->getStreamDestinations(streamName_);
0198 rundata->streamMergeType =
0199 edm::Service<evf::EvFDaqDirector>()->getStreamMergeType(streamName_, evf::MergeTypeJSNDATA);
0200 rundata->baseRunDir = edm::Service<evf::EvFDaqDirector>()->baseRunDir();
0201 } else {
0202 rundata->streamDestination = "";
0203 rundata->streamMergeType = "";
0204 rundata->baseRunDir = ".";
0205 }
0206
0207
0208 std::vector<std::string> triggerNames(GlobalAlgBlk::maxPhysicsTriggers, ""s);
0209 auto const& menuHandle = setup.getHandle(l1tUtmTriggerMenuRcdToken_);
0210 if (menuHandle.isValid()) {
0211 for (auto const& algo : menuHandle->getAlgorithmMap())
0212 triggerNames[algo.second.getIndex()] = algo.first;
0213 } else {
0214 edm::LogWarning("L1TriggerJSONMonitoring") << "L1TUtmTriggerMenu not found in the EventSetup.\nThe Level 1 Trigger "
0215 "rate monitoring will not include the trigger names.";
0216 }
0217
0218
0219 rundata->jsdFileName = fmt::sprintf("run%06d_ls0000_streamL1Rates_pid%05d.jsd", run.run(), getpid());
0220 writeJsdFile(*rundata);
0221
0222
0223
0224 writeIniFile(*rundata, run.run(), triggerNames);
0225
0226 return rundata;
0227 }
0228
0229
0230 void L1TriggerJSONMonitoring::globalEndRun(edm::Run const&, edm::EventSetup const&) const {}
0231
0232
0233 void L1TriggerJSONMonitoring::analyze(edm::StreamID sid, edm::Event const& event, edm::EventSetup const&) const {
0234 auto& stream = *streamCache(sid);
0235
0236 ++stream.processed;
0237 unsigned int eventType = event.experimentType();
0238 if (eventType < tcdsTriggerTypes_.size())
0239 ++stream.tcdsAccept[eventType];
0240 else
0241 edm::LogWarning("L1TriggerJSONMonitoring") << "Unexpected event type " << eventType;
0242
0243
0244 edm::Handle<GlobalAlgBlkBxCollection> handle;
0245 if (not event.getByToken(level1ResultsToken_, handle) or not handle.isValid() or handle->isEmpty(0)) {
0246 edm::LogError("L1TriggerJSONMonitoring")
0247 << "L1 trigger results with label [" + level1Results_.encode() + "] not present or invalid";
0248 return;
0249 }
0250
0251
0252
0253
0254 auto const& results = handle->at(0, 0);
0255 auto const& decision = results.getAlgoDecisionFinal();
0256 assert(decision.size() == GlobalAlgBlk::maxPhysicsTriggers);
0257
0258
0259 for (unsigned int i = 0; i < decision.size(); ++i) {
0260 if (decision[i]) {
0261 ++stream.l1tAccept[i];
0262 switch (eventType) {
0263 case edm::EventAuxiliary::PhysicsTrigger:
0264 ++stream.l1tAcceptPhysics[i];
0265 break;
0266 case edm::EventAuxiliary::CalibrationTrigger:
0267 ++stream.l1tAcceptCalibration[i];
0268 break;
0269 case edm::EventAuxiliary::RandomTrigger:
0270 ++stream.l1tAcceptRandom[i];
0271 break;
0272 default:
0273 break;
0274 }
0275 }
0276 }
0277
0278
0279 int prescaleIndex = results.getPreScColumn();
0280 if (stream.prescaleIndex == L1TriggerJSONMonitoringData::kPrescaleUndefined) {
0281 stream.prescaleIndex = prescaleIndex;
0282 } else if (stream.prescaleIndex == L1TriggerJSONMonitoringData::kPrescaleConflict) {
0283
0284 } else if (stream.prescaleIndex != prescaleIndex) {
0285 edm::LogWarning("L1TriggerJSONMonitoring") << "Prescale index changed from " << stream.prescaleIndex << " to "
0286 << prescaleIndex << " inside lumisection " << event.luminosityBlock();
0287 stream.prescaleIndex = L1TriggerJSONMonitoringData::kPrescaleConflict;
0288 }
0289 }
0290
0291
0292 std::shared_ptr<L1TriggerJSONMonitoringData::lumisection> L1TriggerJSONMonitoring::globalBeginLuminosityBlockSummary(
0293 edm::LuminosityBlock const& lumi, edm::EventSetup const&) const {
0294
0295
0296 auto lumidata = std::make_shared<L1TriggerJSONMonitoringData::lumisection>(L1TriggerJSONMonitoringData::lumisection{
0297 jsoncollector::HistoJ<unsigned int>(1),
0298 jsoncollector::HistoJ<unsigned int>(GlobalAlgBlk::maxPhysicsTriggers),
0299 jsoncollector::HistoJ<unsigned int>(GlobalAlgBlk::maxPhysicsTriggers),
0300 jsoncollector::HistoJ<unsigned int>(GlobalAlgBlk::maxPhysicsTriggers),
0301 jsoncollector::HistoJ<unsigned int>(GlobalAlgBlk::maxPhysicsTriggers),
0302 jsoncollector::HistoJ<unsigned int>(tcdsTriggerTypes_.size())
0303 });
0304
0305 lumidata->processed.update(0);
0306 for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i)
0307 lumidata->l1tAccept.update(0);
0308 for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i)
0309 lumidata->l1tAcceptPhysics.update(0);
0310 for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i)
0311 lumidata->l1tAcceptCalibration.update(0);
0312 for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i)
0313 lumidata->l1tAcceptRandom.update(0);
0314 for (unsigned int i = 0; i < tcdsTriggerTypes_.size(); ++i)
0315 lumidata->tcdsAccept.update(0);
0316 lumidata->prescaleIndex = L1TriggerJSONMonitoringData::kPrescaleUndefined;
0317
0318 return lumidata;
0319 }
0320
0321
0322 void L1TriggerJSONMonitoring::streamBeginLuminosityBlock(edm::StreamID sid,
0323 edm::LuminosityBlock const& lumi,
0324 edm::EventSetup const&) const {
0325 auto& stream = *streamCache(sid);
0326
0327
0328 stream.processed = 0;
0329 stream.l1tAccept.assign(GlobalAlgBlk::maxPhysicsTriggers, 0);
0330 stream.l1tAcceptPhysics.assign(GlobalAlgBlk::maxPhysicsTriggers, 0);
0331 stream.l1tAcceptCalibration.assign(GlobalAlgBlk::maxPhysicsTriggers, 0);
0332 stream.l1tAcceptRandom.assign(GlobalAlgBlk::maxPhysicsTriggers, 0);
0333 stream.tcdsAccept.assign(tcdsTriggerTypes_.size(), 0);
0334 stream.prescaleIndex = L1TriggerJSONMonitoringData::kPrescaleUndefined;
0335 }
0336
0337
0338 void L1TriggerJSONMonitoring::streamEndLuminosityBlockSummary(edm::StreamID sid,
0339 edm::LuminosityBlock const& lumi,
0340 edm::EventSetup const&,
0341 L1TriggerJSONMonitoringData::lumisection* lumidata) const {
0342 auto const& stream = *streamCache(sid);
0343 lumidata->processed.value()[0] += stream.processed;
0344
0345 for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i) {
0346 lumidata->l1tAccept.value()[i] += stream.l1tAccept[i];
0347 lumidata->l1tAcceptPhysics.value()[i] += stream.l1tAcceptPhysics[i];
0348 lumidata->l1tAcceptCalibration.value()[i] += stream.l1tAcceptCalibration[i];
0349 lumidata->l1tAcceptRandom.value()[i] += stream.l1tAcceptRandom[i];
0350 }
0351 for (unsigned int i = 0; i < tcdsTriggerTypes_.size(); ++i)
0352 lumidata->tcdsAccept.value()[i] += stream.tcdsAccept[i];
0353
0354
0355 if (lumidata->prescaleIndex == L1TriggerJSONMonitoringData::kPrescaleUndefined)
0356 lumidata->prescaleIndex = stream.prescaleIndex;
0357 else if (lumidata->prescaleIndex != stream.prescaleIndex)
0358 lumidata->prescaleIndex = L1TriggerJSONMonitoringData::kPrescaleConflict;
0359 }
0360
0361
0362 void L1TriggerJSONMonitoring::globalEndLuminosityBlockSummary(edm::LuminosityBlock const& lumi,
0363 edm::EventSetup const&,
0364 L1TriggerJSONMonitoringData::lumisection* lumidata) const {
0365 unsigned int ls = lumi.luminosityBlock();
0366 unsigned int run = lumi.run();
0367
0368 if (edm::Service<evf::FastMonitoringService>().isAvailable()) {
0369 if (!edm::Service<evf::FastMonitoringService>()->shouldWriteFiles(ls))
0370 return;
0371 }
0372
0373 unsigned int processed = lumidata->processed.value().at(0);
0374 auto const& rundata = *runCache(lumi.getRun().index());
0375 Json::StyledWriter writer;
0376
0377
0378 char hostname[33];
0379 gethostname(hostname, 32);
0380 std::string sourceHost(hostname);
0381
0382
0383 std::stringstream sOutDef;
0384 sOutDef << rundata.baseRunDir << "/"
0385 << "output_" << getpid() << ".jsd";
0386
0387 std::string jsndataFileList = "";
0388 unsigned int jsndataSize = 0;
0389 unsigned int jsndataAdler32 = 1;
0390
0391 if (processed) {
0392
0393 Json::Value jsndata;
0394 jsndata[jsoncollector::DataPoint::SOURCE] = sourceHost;
0395 jsndata[jsoncollector::DataPoint::DEFINITION] = rundata.jsdFileName;
0396 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->processed.toJsonValue());
0397 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->l1tAccept.toJsonValue());
0398 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->l1tAcceptPhysics.toJsonValue());
0399 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->l1tAcceptCalibration.toJsonValue());
0400 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->l1tAcceptRandom.toJsonValue());
0401
0402
0403 jsoncollector::HistoJ<unsigned int> tcdsAccept;
0404 tcdsAccept.update(lumidata->tcdsAccept.value()[edm::EventAuxiliary::PhysicsTrigger]);
0405 tcdsAccept.update(lumidata->tcdsAccept.value()[edm::EventAuxiliary::CalibrationTrigger]);
0406 tcdsAccept.update(lumidata->tcdsAccept.value()[edm::EventAuxiliary::RandomTrigger]);
0407 jsndata[jsoncollector::DataPoint::DATA].append(tcdsAccept.toJsonValue());
0408
0409
0410
0411 jsndata[jsoncollector::DataPoint::DATA].append(lumidata->prescaleIndex);
0412
0413 auto jsndataFileName = fmt::sprintf("run%06d_ls%04d_streamL1Rates_pid%05d.jsndata", run, ls, getpid());
0414
0415 std::string result = writer.write(jsndata);
0416 std::ofstream jsndataFile(rundata.baseRunDir + "/" + jsndataFileName);
0417 jsndataFile << result;
0418 jsndataFile.close();
0419
0420 jsndataFileList = jsndataFileName;
0421 jsndataSize = result.size();
0422 jsndataAdler32 = cms::Adler32(result.c_str(), result.size());
0423 }
0424
0425
0426 unsigned int jsnProcessed = processed;
0427 unsigned int jsnAccepted = processed;
0428 unsigned int jsnErrorEvents = 0;
0429 unsigned int jsnRetCodeMask = 0;
0430 std::string jsnInputFiles = "";
0431 unsigned int jsnHLTErrorEvents = 0;
0432
0433 Json::Value jsn;
0434 jsn[jsoncollector::DataPoint::SOURCE] = sourceHost;
0435 jsn[jsoncollector::DataPoint::DEFINITION] = sOutDef.str();
0436 jsn[jsoncollector::DataPoint::DATA].append(jsnProcessed);
0437 jsn[jsoncollector::DataPoint::DATA].append(jsnAccepted);
0438 jsn[jsoncollector::DataPoint::DATA].append(jsnErrorEvents);
0439 jsn[jsoncollector::DataPoint::DATA].append(jsnRetCodeMask);
0440 jsn[jsoncollector::DataPoint::DATA].append(jsndataFileList);
0441 jsn[jsoncollector::DataPoint::DATA].append(jsndataSize);
0442 jsn[jsoncollector::DataPoint::DATA].append(jsnInputFiles);
0443 jsn[jsoncollector::DataPoint::DATA].append(jsndataAdler32);
0444 jsn[jsoncollector::DataPoint::DATA].append(rundata.streamDestination);
0445 jsn[jsoncollector::DataPoint::DATA].append(rundata.streamMergeType);
0446 jsn[jsoncollector::DataPoint::DATA].append(jsnHLTErrorEvents);
0447
0448 auto jsnFileName = fmt::sprintf("run%06d_ls%04d_streamL1Rates_pid%05d.jsn", run, ls, getpid());
0449 std::ofstream jsnFile(rundata.baseRunDir + "/" + jsnFileName);
0450 jsnFile << writer.write(jsn);
0451 jsnFile.close();
0452 }
0453
0454 void L1TriggerJSONMonitoring::writeJsdFile(L1TriggerJSONMonitoringData::run const& rundata) {
0455 std::ofstream file(rundata.baseRunDir + "/" + rundata.jsdFileName);
0456 file << R"""({
0457 "data" : [
0458 { "name" : "Processed", "type" : "integer", "operation" : "histo"},
0459 { "name" : "L1-AlgoAccepted", "type" : "integer", "operation" : "histo"},
0460 { "name" : "L1-AlgoAccepted-Physics", "type" : "integer", "operation" : "histo"},
0461 { "name" : "L1-AlgoAccepted-Calibration", "type" : "integer", "operation" : "histo"},
0462 { "name" : "L1-AlgoAccepted-Random", "type" : "integer", "operation" : "histo"},
0463 { "name" : "L1-Global", "type" : "integer", "operation" : "histo"},
0464 { "name" : "Prescale-Index", "type" : "integer", "operation" : "sample"}
0465 ]
0466 })""";
0467 file.close();
0468 }
0469
0470 void L1TriggerJSONMonitoring::writeIniFile(L1TriggerJSONMonitoringData::run const& rundata,
0471 unsigned int run,
0472 std::vector<std::string> const& l1TriggerNames) {
0473 Json::Value content;
0474
0475 Json::Value triggerNames(Json::arrayValue);
0476 for (auto const& name : l1TriggerNames)
0477 triggerNames.append(name);
0478 content["L1-Algo-Names"] = triggerNames;
0479
0480 Json::Value eventTypes(Json::arrayValue);
0481 eventTypes.append(tcdsTriggerTypes_[edm::EventAuxiliary::PhysicsTrigger]);
0482 eventTypes.append(tcdsTriggerTypes_[edm::EventAuxiliary::CalibrationTrigger]);
0483 eventTypes.append(tcdsTriggerTypes_[edm::EventAuxiliary::RandomTrigger]);
0484
0485
0486
0487
0488 content["Event-Type"] = eventTypes;
0489
0490 std::string iniFileName = fmt::sprintf("run%06d_ls0000_streamL1Rates_pid%05d.ini", run, getpid());
0491 std::ofstream file(rundata.baseRunDir + "/" + iniFileName);
0492 Json::StyledWriter writer;
0493 file << writer.write(content);
0494 file.close();
0495 }
0496
0497
0498 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
0499 #include "FWCore/Framework/interface/MakerMacros.h"
0500 DEFINE_FWK_MODULE(L1TriggerJSONMonitoring);