From 50e9dc01436854bb31ed1187dbfd5c8054b4e6c3 Mon Sep 17 00:00:00 2001 From: mmusich Date: Fri, 18 Oct 2024 22:58:53 +0200 Subject: [PATCH] make AlcaBeamMonitor hoard less memory, use a dedicated struct instead of storing raw vertices --- DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc | 45 ++++++++++++---------- DQM/BeamMonitor/plugins/AlcaBeamMonitor.h | 20 +++++++++- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc b/DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc index efdd24ab55848..511aa155b7bae 100644 --- a/DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc +++ b/DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc @@ -245,8 +245,8 @@ std::shared_ptr AlcaBeamMonitor::globalBeginLumin BeamSpot::Point apoint(spotDB->x(), spotDB->y(), spotDB->z()); BeamSpot::CovarianceMatrix matrix; - for (int i = 0; i < 7; ++i) { - for (int j = 0; j < 7; ++j) { + for (int i = 0; i < reco::BeamSpot::dimension; ++i) { + for (int j = 0; j < reco::BeamSpot::dimension; ++j) { matrix(i, j) = spotDB->covariance(i, j); } } @@ -296,7 +296,15 @@ void AlcaBeamMonitor::analyze(const Event& iEvent, const EventSetup& iSetup) { //------ Primary Vertices Handle PVCollection; if (iEvent.getByToken(primaryVertexLabel_, PVCollection)) { - beamSpotInfo->vertices_.push_back(*PVCollection.product()); + std::vector vertices; + vertices.reserve(PVCollection->size()); + for (const auto& pv : *PVCollection.product()) { + if (pv.isFake() || pv.tracksSize() < 10) + continue; + vertices.emplace_back(pv); + } + vertices.shrink_to_fit(); + beamSpotInfo->vertices_.emplace_back(std::move(vertices)); } if (beamSpotInfo->beamSpotMap_.find("SC") == beamSpotInfo->beamSpotMap_.end()) { @@ -363,24 +371,19 @@ void AlcaBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, con } } vertexResults.clear(); - for (vector::iterator itPV = beamSpotInfo->vertices_.begin(); - itPV != beamSpotInfo->vertices_.end(); - itPV++) { - if (!itPV->empty()) { - for (VertexCollection::const_iterator pv = itPV->begin(); pv != itPV->end(); pv++) { - if (pv->isFake() || pv->tracksSize() < 10) - continue; - if (*itV == "x") { - vertexResults.push_back(pair(pv->x(), pv->xError())); - } else if (*itV == "y") { - vertexResults.push_back(pair(pv->y(), pv->yError())); - } else if (*itV == "z") { - vertexResults.push_back(pair(pv->z(), pv->zError())); - } else if (*itV != "sigmaX" && *itV != "sigmaY" && *itV != "sigmaZ") { - LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV - << " that I can't recognize!"; - //assert(0); - } + + for (const auto& itPV : beamSpotInfo->vertices_) { + for (const auto& pv : itPV) { + if (*itV == "x") { + vertexResults.push_back(pv.xWithError()); + } else if (*itV == "y") { + vertexResults.push_back(pv.yWithError()); + } else if (*itV == "z") { + vertexResults.push_back(pv.zWithError()); + } else if (*itV != "sigmaX" && *itV != "sigmaY" && *itV != "sigmaZ") { + LogInfo("AlcaBeamMonitor") << "The histosMap_ has been built with the name " << *itV + << " that I can't recognize!"; + //assert(0); } } } diff --git a/DQM/BeamMonitor/plugins/AlcaBeamMonitor.h b/DQM/BeamMonitor/plugins/AlcaBeamMonitor.h index 56fcca62531d5..00fb67fbe0e9a 100644 --- a/DQM/BeamMonitor/plugins/AlcaBeamMonitor.h +++ b/DQM/BeamMonitor/plugins/AlcaBeamMonitor.h @@ -8,8 +8,11 @@ */ // C++ #include +#include #include #include +#include + // CMS #include "FWCore/Framework/interface/Frameworkfwd.h" #include "FWCore/Framework/interface/Event.h" @@ -28,8 +31,23 @@ class BeamFitter; class PVFitter; namespace alcabeammonitor { + + struct pvPosAndErr { + // Array of pairs: (value, error) for x, y, z + std::array, 3> data; + + // Constructor initializes the array with values and errors from a reco::Vertex + pvPosAndErr(const reco::Vertex& vertex) + : data{{{vertex.x(), vertex.xError()}, {vertex.y(), vertex.yError()}, {vertex.z(), vertex.zError()}}} {} + + // Accessor functions that return pairs (value, error) directly + std::pair xWithError() const { return data[0]; } + std::pair yWithError() const { return data[1]; } + std::pair zWithError() const { return data[2]; } + }; + struct BeamSpotInfo { - std::vector vertices_; + std::vector> vertices_; typedef std::map BeamSpotContainer; BeamSpotContainer beamSpotMap_; };