Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernized AlcaBeamMonitor #31354

Merged
merged 5 commits into from Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions DQM/BeamMonitor/plugins/AlcaBeamMonitor.cc
Expand Up @@ -8,8 +8,6 @@
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
#include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
#include "DataFormats/BeamSpot/interface/BeamSpot.h"
//#include "DataFormats/Scalers/interface/BeamSpotOnline.h"
#include "DQM/BeamMonitor/plugins/AlcaBeamMonitor.h"
Expand All @@ -31,24 +29,22 @@ using namespace reco;

//----------------------------------------------------------------------------------------------------------------------
AlcaBeamMonitor::AlcaBeamMonitor(const ParameterSet& ps)
: parameters_(ps),
monitorName_(parameters_.getUntrackedParameter<string>("MonitorName", "YourSubsystemName")),
primaryVertexLabel_(
consumes<VertexCollection>(parameters_.getUntrackedParameter<InputTag>("PrimaryVertexLabel"))),
trackLabel_(consumes<reco::TrackCollection>(parameters_.getUntrackedParameter<InputTag>("TrackLabel"))),
scalerLabel_(consumes<BeamSpot>(parameters_.getUntrackedParameter<InputTag>("ScalerLabel"))),
beamSpotLabel_(parameters_.getUntrackedParameter<InputTag>("BeamSpotLabel")),
: monitorName_(ps.getUntrackedParameter<string>("MonitorName")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why you have removed the second parameter "YourSubsystemName" here but not in AlcaBeamMonitor::fillDescriptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having it in both the getUntrackedParameter call and in fillDescriptions would be redundant. If the parameter is missing, the fillDescriptions call will inject the "MonitorName" string parameter into the module's PSet using the default "YourSubsystemName". Therefore during the constructor call the parameter "MonitorName" is guaranteed to exist so no default value is needed for the getUntrackedParameter. So the change I made has exactly the same ultimate behavior as the original code.

In general, the framework group highly encourages everyone to stop specifying default values in getUntrackedParameter and instead use fillDescriptions.

primaryVertexLabel_(consumes<VertexCollection>(ps.getUntrackedParameter<InputTag>("PrimaryVertexLabel"))),
trackLabel_(consumes<reco::TrackCollection>(ps.getUntrackedParameter<InputTag>("TrackLabel"))),
scalerLabel_(consumes<BeamSpot>(ps.getUntrackedParameter<InputTag>("ScalerLabel"))),
beamSpotToken_(esConsumes<edm::Transition::BeginLuminosityBlock>()),
numberOfValuesToSave_(0) {
if (!monitorName_.empty())
monitorName_ = monitorName_ + "/";

theBeamFitter_ = std::make_unique<BeamFitter>(parameters_, consumesCollector());
theBeamFitter_ = std::make_unique<BeamFitter>(ps, consumesCollector());
theBeamFitter_->resetTrkVector();
theBeamFitter_->resetLSRange();
theBeamFitter_->resetRefTime();
theBeamFitter_->resetPVFitter();

thePVFitter_ = std::make_unique<PVFitter>(parameters_, consumesCollector());
thePVFitter_ = std::make_unique<PVFitter>(ps, consumesCollector());

processedLumis_.clear();

Expand Down Expand Up @@ -85,6 +81,20 @@ AlcaBeamMonitor::AlcaBeamMonitor(const ParameterSet& ps)
}
}

void AlcaBeamMonitor::fillDescriptions(edm::ConfigurationDescriptions& iDesc) {
edm::ParameterSetDescription ps;

ps.addUntracked<std::string>("MonitorName", "YourSubsystemName");
ps.addUntracked<edm::InputTag>("PrimaryVertexLabel");
ps.addUntracked<edm::InputTag>("TrackLabel");
ps.addUntracked<edm::InputTag>("ScalerLabel");

BeamFitter::fillDescription(ps);
PVFitter::fillDescription(ps);

iDesc.addDefault(ps);
}

//----------------------------------------------------------------------------------------------------------------------
void AlcaBeamMonitor::bookHistograms(DQMStore::IBooker& ibooker, edm::Run const& iRun, edm::EventSetup const& iSetup) {
string name;
Expand Down Expand Up @@ -220,9 +230,9 @@ std::shared_ptr<alcabeammonitor::NoCache> AlcaBeamMonitor::globalBeginLuminosity
//Read BeamSpot from DB
ESHandle<BeamSpotObjects> bsDBHandle;
try {
iSetup.get<BeamSpotObjectsRcd>().get(bsDBHandle);
bsDBHandle = iSetup.getHandle(beamSpotToken_);
} catch (cms::Exception& exception) {
LogInfo("AlcaBeamMonitor") << exception.what();
LogError("AlcaBeamMonitor") << exception.what();
return nullptr;
}
if (bsDBHandle.isValid()) { // check the product
Expand Down Expand Up @@ -479,6 +489,10 @@ void AlcaBeamMonitor::globalEndLuminosityBlock(const LuminosityBlock& iLumi, con
}

void AlcaBeamMonitor::dqmEndRun(edm::Run const&, edm::EventSetup const&) {
if (processedLumis_.empty()) {
return;
}

const double bigNumber = 1000000.;
std::sort(processedLumis_.begin(), processedLumis_.end());
int firstLumi = *processedLumis_.begin();
Expand Down
15 changes: 9 additions & 6 deletions DQM/BeamMonitor/plugins/AlcaBeamMonitor.h
Expand Up @@ -14,12 +14,15 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/ESGetToken.h"
#include "DQMServices/Core/interface/DQMStore.h"
#include "DQMServices/Core/interface/DQMOneEDAnalyzer.h"
#include "DataFormats/Provenance/interface/LuminosityBlockID.h"
#include "DataFormats/BeamSpot/interface/BeamSpot.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
#include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"

class BeamFitter;
class PVFitter;
Expand All @@ -31,6 +34,7 @@ namespace alcabeammonitor {
class AlcaBeamMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<alcabeammonitor::NoCache>> {
public:
AlcaBeamMonitor(const edm::ParameterSet&);
static void fillDescriptions(edm::ConfigurationDescriptions&);

protected:
void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
Expand All @@ -50,12 +54,11 @@ class AlcaBeamMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<alcabe
typedef std::map<std::string, std::map<std::string, std::map<std::string, int>>> PositionContainer;

//Parameters
edm::ParameterSet parameters_;
std::string monitorName_;
edm::EDGetTokenT<reco::VertexCollection> primaryVertexLabel_;
edm::EDGetTokenT<reco::TrackCollection> trackLabel_;
edm::EDGetTokenT<reco::BeamSpot> scalerLabel_;
edm::InputTag beamSpotLabel_;
const edm::EDGetTokenT<reco::VertexCollection> primaryVertexLabel_;
const edm::EDGetTokenT<reco::TrackCollection> trackLabel_;
const edm::EDGetTokenT<reco::BeamSpot> scalerLabel_;
const edm::ESGetToken<BeamSpotObjects, BeamSpotObjectsRcd> beamSpotToken_;

//Service variables
int numberOfValuesToSave_;
Expand All @@ -78,4 +81,4 @@ class AlcaBeamMonitor : public DQMOneEDAnalyzer<edm::LuminosityBlockCache<alcabe
mutable std::vector<reco::VertexCollection> vertices_;
};

#endif
#endif
4 changes: 1 addition & 3 deletions DQM/BeamMonitor/python/AlcaBeamMonitor_cfi.py
Expand Up @@ -4,7 +4,6 @@
AlcaBeamMonitor = DQMEDAnalyzer('AlcaBeamMonitor',
MonitorName = cms.untracked.string('AlcaBeamMonitor'),
PrimaryVertexLabel = cms.untracked.InputTag('offlinePrimaryVertices'),
BeamSpotLabel = cms.untracked.InputTag('offlineBeamSpot'),
#TrackLabel = cms.untracked.InputTag('ALCARECOTkAlMinBias'),
TrackLabel = cms.untracked.InputTag('generalTracks'),
ScalerLabel = cms.untracked.InputTag('scalerBeamSpot'),
Expand Down Expand Up @@ -33,8 +32,7 @@
TrackQuality = cms.untracked.vstring(), ## loose, tight, highPurity...; for all qualities, leave it blank
InputBeamWidth = cms.untracked.double(0.0060), ## beam width used for Trk fitter, used only when result from PV is not available
FractionOfFittedTrks = cms.untracked.double(0.9),
MinimumInputTracks = cms.untracked.int32(150),
deltaSignificanceCut = cms.untracked.double(10)
MinimumInputTracks = cms.untracked.int32(150)
),
PVFitter = cms.PSet(
Debug = cms.untracked.bool(False),
Expand Down
4 changes: 4 additions & 0 deletions DQM/BeamMonitor/test/BuildFile.xml
@@ -0,0 +1,4 @@
<bin file="testAlcaBeamMonitor.cc">
<use name="FWCore/TestProcessor"/>
<use name="catch2"/>
</bin>
23 changes: 23 additions & 0 deletions DQM/BeamMonitor/test/testAlcaBeamMonitor.cc
@@ -0,0 +1,23 @@
#include "FWCore/TestProcessor/interface/TestProcessor.h"

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE("AlcaBeamMonitor tests", "[AlcaBeamMonitor]") {
//The python configuration
edm::test::TestProcessor::Config config{
R"_(from FWCore.TestProcessor.TestProcess import *
from DQM.BeamMonitor.AlcaBeamMonitor_cfi import AlcaBeamMonitor
process = TestProcess()
process.beamMonitor = AlcaBeamMonitor
process.moduleToTest(process.beamMonitor)
process.add_(cms.Service("DQMStore"))
)_"};

SECTION("Run with no Lumis") {
edm::test::TestProcessor tester{config};
tester.testRunWithNoLuminosityBlocks();
//get here without an exception or crashing
REQUIRE(true);
};
}
3 changes: 3 additions & 0 deletions RecoVertex/BeamSpotProducer/interface/BeamFitter.h
Expand Up @@ -16,6 +16,7 @@
#include "FWCore/Framework/interface/Event.h"
#include "DataFormats/Provenance/interface/Timestamp.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "DataFormats/BeamSpot/interface/BeamSpot.h"
#include "DataFormats/TrackReco/interface/TrackBase.h"
#include "DataFormats/TrackReco/interface/Track.h"
Expand Down Expand Up @@ -43,6 +44,8 @@ class BeamFitter {
BeamFitter(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iColl);
virtual ~BeamFitter();

static void fillDescription(edm::ParameterSetDescription &);

void readEvent(const edm::Event &iEvent);

bool runFitter();
Expand Down
3 changes: 3 additions & 0 deletions RecoVertex/BeamSpotProducer/interface/PVFitter.h
Expand Up @@ -16,6 +16,7 @@
#include "FWCore/Framework/interface/Event.h"
#include "DataFormats/Provenance/interface/Timestamp.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "DataFormats/BeamSpot/interface/BeamSpot.h"
#include "DataFormats/TrackReco/interface/TrackBase.h"
#include "RecoVertex/BeamSpotProducer/interface/BSTrkParameters.h"
Expand Down Expand Up @@ -48,6 +49,8 @@ class PVFitter {
PVFitter(const edm::ParameterSet& iConfig, edm::ConsumesCollector& iColl);
virtual ~PVFitter();

static void fillDescription(edm::ParameterSetDescription&);

void initialize(const edm::ParameterSet& iConfig, edm::ConsumesCollector& iColl);
void readEvent(const edm::Event& iEvent);
void setTree(TTree* tree);
Expand Down
37 changes: 37 additions & 0 deletions RecoVertex/BeamSpotProducer/src/BeamFitter.cc
Expand Up @@ -221,6 +221,43 @@ BeamFitter::~BeamFitter() {
delete MyPVFitter;
}

void BeamFitter::fillDescription(edm::ParameterSetDescription &iDesc) {
edm::ParameterSetDescription beamFitter;

beamFitter.addUntracked<bool>("Debug");
beamFitter.addUntracked<edm::InputTag>("TrackCollection");
iDesc.addUntracked<edm::InputTag>("primaryVertex", edm::InputTag("offlinePrimaryVertices"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the default values for these parameters are defined here, would it make sense to remove them from the constructor:

iConfig.getUntrackedParameter<edm::InputTag>("primaryVertex", edm::InputTag("offlinePrimaryVertices")));
?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the main python definition of BeamFitter is here [1] (with some alternate defitions in neighboring files) - do we want to reduce the duplication of the default parameters somewhat between the fillDescriptions and the cff?

[1]

BeamFitter = cms.PSet(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d prefer to have that done in a separate pull request so we can get the bug fix in.

iDesc.addUntracked<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
beamFitter.addUntracked<bool>("WriteAscii");
beamFitter.addUntracked<std::string>("AsciiFileName");
beamFitter.addUntracked<bool>("AppendRunToFileName");
beamFitter.addUntracked<bool>("WriteDIPAscii");
// Specify whether we want to write the DIP file even if the fit is failed.
beamFitter.addUntracked<bool>("WriteDIPOnBadFit", true);
beamFitter.addUntracked<std::string>("DIPFileName");
beamFitter.addUntracked<bool>("SaveNtuple");
beamFitter.addUntracked<bool>("SaveFitResults");
beamFitter.addUntracked<bool>("SavePVVertices");
beamFitter.addUntracked<bool>("IsMuonCollection");

beamFitter.addUntracked<double>("MinimumPt");
beamFitter.addUntracked<double>("MaximumEta");
beamFitter.addUntracked<double>("MaximumImpactParameter");
beamFitter.addUntracked<double>("MaximumZ");
beamFitter.addUntracked<int>("MinimumTotalLayers");
beamFitter.addUntracked<int>("MinimumPixelLayers");
beamFitter.addUntracked<double>("MaximumNormChi2");
beamFitter.addUntracked<std::vector<std::string> >("TrackAlgorithm");
beamFitter.addUntracked<std::vector<std::string> >("TrackQuality");
beamFitter.addUntracked<int>("MinimumInputTracks");
beamFitter.addUntracked<double>("FractionOfFittedTrks");
beamFitter.addUntracked<double>("InputBeamWidth", -1.);

beamFitter.addUntracked<std::string>("OutputFileName", "");

iDesc.add<edm::ParameterSetDescription>("BeamFitter", beamFitter);
}

void BeamFitter::readEvent(const edm::Event &iEvent) {
frun = iEvent.id().run();
const edm::TimeValue_t ftimestamp = iEvent.time().value();
Expand Down
23 changes: 23 additions & 0 deletions RecoVertex/BeamSpotProducer/src/PVFitter.cc
Expand Up @@ -94,6 +94,29 @@ void PVFitter::initialize(const edm::ParameterSet& iConfig, edm::ConsumesCollect

PVFitter::~PVFitter() {}

void PVFitter::fillDescription(edm::ParameterSetDescription& iDesc) {
edm::ParameterSetDescription pvFitter;

pvFitter.addUntracked<bool>("Debug");
pvFitter.addUntracked<edm::InputTag>("VertexCollection", edm::InputTag("offlinePrimaryVertices"));
pvFitter.addUntracked<bool>("Apply3DFit");
pvFitter.addUntracked<unsigned int>("maxNrStoredVertices");
pvFitter.addUntracked<unsigned int>("minNrVerticesForFit");
pvFitter.addUntracked<double>("minVertexNdf");
pvFitter.addUntracked<double>("maxVertexNormChi2");
pvFitter.addUntracked<unsigned int>("minVertexNTracks");
pvFitter.addUntracked<double>("minVertexMeanWeight");
pvFitter.addUntracked<double>("maxVertexR");
pvFitter.addUntracked<double>("maxVertexZ");
pvFitter.addUntracked<double>("errorScale");
pvFitter.addUntracked<double>("nSigmaCut");
pvFitter.addUntracked<bool>("FitPerBunchCrossing");
pvFitter.addUntracked<bool>("useOnlyFirstPV");
pvFitter.addUntracked<double>("minSumPt");

iDesc.add<edm::ParameterSetDescription>("PVFitter", pvFitter);
}

void PVFitter::readEvent(const edm::Event& iEvent) {
//------ Primary Vertices
edm::Handle<reco::VertexCollection> PVCollection;
Expand Down