Skip to content

Commit

Permalink
Merge pull request #46465 from mmusich/mm_dev_AlcaBeamMonitor_redux_1…
Browse files Browse the repository at this point in the history
…4_1_X

[14.1.X] make `AlcaBeamMonitor` hoard less memory
  • Loading branch information
cmsbuild authored Oct 22, 2024
2 parents ab8e5f5 + 50e9dc0 commit 1dd928e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
45 changes: 24 additions & 21 deletions DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ std::shared_ptr<alcabeammonitor::BeamSpotInfo> 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);
}
}
Expand Down Expand Up @@ -296,7 +296,15 @@ void AlcaBeamMonitor::analyze(const Event& iEvent, const EventSetup& iSetup) {
//------ Primary Vertices
Handle<VertexCollection> PVCollection;
if (iEvent.getByToken(primaryVertexLabel_, PVCollection)) {
beamSpotInfo->vertices_.push_back(*PVCollection.product());
std::vector<alcabeammonitor::pvPosAndErr> 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()) {
Expand Down Expand Up @@ -363,24 +371,19 @@ void AlcaBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, con
}
}
vertexResults.clear();
for (vector<VertexCollection>::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<double, double>(pv->x(), pv->xError()));
} else if (*itV == "y") {
vertexResults.push_back(pair<double, double>(pv->y(), pv->yError()));
} else if (*itV == "z") {
vertexResults.push_back(pair<double, double>(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);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion DQM/BeamMonitor/plugins/AlcaBeamMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
*/
// C++
#include <map>
#include <array>
#include <vector>
#include <string>
#include <utility>

// CMS
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
Expand All @@ -28,8 +31,23 @@ class BeamFitter;
class PVFitter;

namespace alcabeammonitor {

struct pvPosAndErr {
// Array of pairs: (value, error) for x, y, z
std::array<std::pair<double, double>, 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<double, double> xWithError() const { return data[0]; }
std::pair<double, double> yWithError() const { return data[1]; }
std::pair<double, double> zWithError() const { return data[2]; }
};

struct BeamSpotInfo {
std::vector<reco::VertexCollection> vertices_;
std::vector<std::vector<pvPosAndErr>> vertices_;
typedef std::map<std::string, reco::BeamSpot> BeamSpotContainer;
BeamSpotContainer beamSpotMap_;
};
Expand Down

0 comments on commit 1dd928e

Please sign in to comment.