Skip to content

Commit

Permalink
Merge pull request #34295 from MilanoBicocca-pix/BSOnlineProducer_12_X
Browse files Browse the repository at this point in the history
modified the various (ES)producers for express stream and HLT
  • Loading branch information
cmsbuild authored Jul 5, 2021
2 parents 661d5c1 + b1d5e95 commit dbeaf36
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 132 deletions.
176 changes: 118 additions & 58 deletions RecoVertex/BeamSpotProducer/plugins/BeamSpotOnlineProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
author: Francisco Yumiceva, Fermilab ([email protected])
modified by: Simone Gennai, INFN MIB
________________________________________________________________**/
Expand All @@ -15,6 +16,9 @@ ________________________________________________________________**/
#include "DataFormats/Scalers/interface/BeamSpotOnline.h"
#include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h"
#include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h"
#include "CondFormats/DataRecord/interface/BeamSpotTransientObjectsRcd.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
Expand All @@ -31,13 +35,18 @@ class BeamSpotOnlineProducer : public edm::stream::EDProducer<> {
/// produce a beam spot class
void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;

///Fill descriptor
static void fillDescriptions(edm::ConfigurationDescriptions& iDesc);

private:
const bool changeFrame_;
const double theMaxZ, theSetSigmaZ;
double theMaxR2;
const bool useTransientRecord_;
const edm::EDGetTokenT<BeamSpotOnlineCollection> scalerToken_;
const edm::EDGetTokenT<L1GlobalTriggerEvmReadoutRecord> l1GtEvmReadoutRecordToken_;
const edm::ESGetToken<BeamSpotObjects, BeamSpotObjectsRcd> beamToken_;
const edm::ESGetToken<BeamSpotObjects, BeamSpotTransientObjectsRcd> beamTransientToken_;

const unsigned int theBeamShoutMode;
};
Expand All @@ -48,17 +57,35 @@ BeamSpotOnlineProducer::BeamSpotOnlineProducer(const ParameterSet& iconf)
: changeFrame_(iconf.getParameter<bool>("changeToCMSCoordinates")),
theMaxZ(iconf.getParameter<double>("maxZ")),
theSetSigmaZ(iconf.getParameter<double>("setSigmaZ")),
useTransientRecord_(iconf.getParameter<bool>("useTransientRecord")),
scalerToken_(consumes<BeamSpotOnlineCollection>(iconf.getParameter<InputTag>("src"))),
l1GtEvmReadoutRecordToken_(consumes<L1GlobalTriggerEvmReadoutRecord>(iconf.getParameter<InputTag>("gtEvmLabel"))),
beamToken_(esConsumes<BeamSpotObjects, BeamSpotObjectsRcd>()),
beamTransientToken_(esConsumes<BeamSpotObjects, BeamSpotTransientObjectsRcd>()),
theBeamShoutMode(iconf.getUntrackedParameter<unsigned int>("beamMode", 11)) {
theMaxR2 = iconf.getParameter<double>("maxRadius");
theMaxR2 *= theMaxR2;

produces<reco::BeamSpot>();
}

void BeamSpotOnlineProducer::fillDescriptions(edm::ConfigurationDescriptions& iDesc) {
edm::ParameterSetDescription ps;
ps.add<bool>("changeToCMSCoordinates", false);
ps.add<double>("maxZ", 40.);
ps.add<double>("setSigmaZ", -1.);
ps.addUntracked<unsigned int>("beamMode", 11);
ps.add<InputTag>("src", InputTag("hltScalersRawToDigi"));
ps.add<InputTag>("gtEvmLabel", InputTag(""));
ps.add<double>("maxRadius", 2.0);
ps.add<bool>("useTransientRecord", false);
iDesc.addWithDefaultLabel(ps);
}

void BeamSpotOnlineProducer::produce(Event& iEvent, const EventSetup& iSetup) {
// product is a reco::BeamSpot object
auto result = std::make_unique<reco::BeamSpot>();
reco::BeamSpot aSpot;
//shout MODE only in stable beam
bool shoutMODE = false;
edm::Handle<L1GlobalTriggerEvmReadoutRecord> gtEvmReadoutRecord;
Expand All @@ -68,74 +95,107 @@ void BeamSpotOnlineProducer::produce(Event& iEvent, const EventSetup& iSetup) {
} else {
shoutMODE = true;
}

// get scalar collection
Handle<BeamSpotOnlineCollection> handleScaler;
iEvent.getByToken(scalerToken_, handleScaler);

// beam spot scalar object
BeamSpotOnline spotOnline;

// product is a reco::BeamSpot object
auto result = std::make_unique<reco::BeamSpot>();

reco::BeamSpot aSpot;

bool fallBackToDB = false;
if (!handleScaler->empty()) {
// get one element
spotOnline = *(handleScaler->begin());

// in case we need to switch to LHC reference frame
// ignore for the moment rotations, and translations
double f = 1.;
if (changeFrame_)
f = -1.;

reco::BeamSpot::Point apoint(f * spotOnline.x(), spotOnline.y(), f * spotOnline.z());

reco::BeamSpot::CovarianceMatrix matrix;
matrix(0, 0) = spotOnline.err_x() * spotOnline.err_x();
matrix(1, 1) = spotOnline.err_y() * spotOnline.err_y();
matrix(2, 2) = spotOnline.err_z() * spotOnline.err_z();
matrix(3, 3) = spotOnline.err_sigma_z() * spotOnline.err_sigma_z();

double sigmaZ = spotOnline.sigma_z();
if (theSetSigmaZ > 0)
sigmaZ = theSetSigmaZ;

aSpot = reco::BeamSpot(apoint, sigmaZ, spotOnline.dxdz(), f * spotOnline.dydz(), spotOnline.width_x(), matrix);

aSpot.setBeamWidthY(spotOnline.width_y());
aSpot.setEmittanceX(0.);
aSpot.setEmittanceY(0.);
aSpot.setbetaStar(0.);
aSpot.setType(reco::BeamSpot::LHC); // flag value from scalars

// check if we have a valid beam spot fit result from online DQM
if (spotOnline.x() == 0 && spotOnline.y() == 0 && spotOnline.z() == 0 && spotOnline.width_x() == 0 &&
spotOnline.width_y() == 0) {
if (useTransientRecord_) {
auto const& spotDB = iSetup.getData(beamTransientToken_);
if (spotDB.GetBeamType() != 2) {
if (shoutMODE) {
edm::LogWarning("BeamSpotFromDB")
<< "Online Beam Spot producer falls back to DB value because the scaler values are zero ";
<< "Online Beam Spot producer falls back to DB value because the ESProducer returned a fake beamspot ";
}
fallBackToDB = true;
} else {
// translate from BeamSpotObjects to reco::BeamSpot
// in case we need to switch to LHC reference frame
// ignore for the moment rotations, and translations
double f = 1.;
if (changeFrame_)
f = -1.;
reco::BeamSpot::Point apoint(f * spotDB.GetX(), f * spotDB.GetY(), f * spotDB.GetZ());

reco::BeamSpot::CovarianceMatrix matrix;
for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 7; ++j) {
matrix(i, j) = spotDB.GetCovariance(i, j);
}
}
double sigmaZ = spotDB.GetSigmaZ();
if (theSetSigmaZ > 0)
sigmaZ = theSetSigmaZ;

// this assume beam width same in x and y
aSpot = reco::BeamSpot(apoint, sigmaZ, spotDB.Getdxdz(), spotDB.Getdydz(), spotDB.GetBeamWidthX(), matrix);
aSpot.setBeamWidthY(spotDB.GetBeamWidthY());
aSpot.setEmittanceX(spotDB.GetEmittanceX());
aSpot.setEmittanceY(spotDB.GetEmittanceY());
aSpot.setbetaStar(spotDB.GetBetaStar());
aSpot.setType(reco::BeamSpot::Tracker);
}
double r2 = spotOnline.x() * spotOnline.x() + spotOnline.y() * spotOnline.y();
if (fabs(spotOnline.z()) >= theMaxZ || r2 >= theMaxR2) {
if (shoutMODE) {
edm::LogError("BeamSpotFromDB")
<< "Online Beam Spot producer falls back to DB value because the scaler values are too big to be true :"
<< spotOnline.x() << " " << spotOnline.y() << " " << spotOnline.z();
} else {
// get scalar collection
Handle<BeamSpotOnlineCollection> handleScaler;
iEvent.getByToken(scalerToken_, handleScaler);

// beam spot scalar object
BeamSpotOnline spotOnline;

// product is a reco::BeamSpot object
auto result = std::make_unique<reco::BeamSpot>();

if (!handleScaler->empty()) {
// get one element
spotOnline = *(handleScaler->begin());

// in case we need to switch to LHC reference frame
// ignore for the moment rotations, and translations
double f = 1.;
if (changeFrame_)
f = -1.;

reco::BeamSpot::Point apoint(f * spotOnline.x(), spotOnline.y(), f * spotOnline.z());

reco::BeamSpot::CovarianceMatrix matrix;
matrix(0, 0) = spotOnline.err_x() * spotOnline.err_x();
matrix(1, 1) = spotOnline.err_y() * spotOnline.err_y();
matrix(2, 2) = spotOnline.err_z() * spotOnline.err_z();
matrix(3, 3) = spotOnline.err_sigma_z() * spotOnline.err_sigma_z();

double sigmaZ = spotOnline.sigma_z();
if (theSetSigmaZ > 0)
sigmaZ = theSetSigmaZ;

aSpot = reco::BeamSpot(apoint, sigmaZ, spotOnline.dxdz(), f * spotOnline.dydz(), spotOnline.width_x(), matrix);

aSpot.setBeamWidthY(spotOnline.width_y());
aSpot.setEmittanceX(0.);
aSpot.setEmittanceY(0.);
aSpot.setbetaStar(0.);
aSpot.setType(reco::BeamSpot::LHC); // flag value from scalars

// check if we have a valid beam spot fit result from online DQM
if (spotOnline.x() == 0 && spotOnline.y() == 0 && spotOnline.z() == 0 && spotOnline.width_x() == 0 &&
spotOnline.width_y() == 0) {
if (shoutMODE) {
edm::LogWarning("BeamSpotFromDB")
<< "Online Beam Spot producer falls back to DB value because the scaler values are zero ";
}
fallBackToDB = true;
}
double r2 = spotOnline.x() * spotOnline.x() + spotOnline.y() * spotOnline.y();
if (std::abs(spotOnline.z()) >= theMaxZ || r2 >= theMaxR2) {
if (shoutMODE) {
edm::LogError("BeamSpotFromDB")
<< "Online Beam Spot producer falls back to DB value because the scaler values are too big to be true :"
<< spotOnline.x() << " " << spotOnline.y() << " " << spotOnline.z();
}
fallBackToDB = true;
}
} else {
//empty online beamspot collection: FED data was empty
//the error should probably have been send at unpacker level
fallBackToDB = true;
}
} else {
//empty online beamspot collection: FED data was empty
//the error should probably have been send at unpacker level
fallBackToDB = true;
}

if (fallBackToDB) {
edm::ESHandle<BeamSpotObjects> beamhandle = iSetup.getHandle(beamToken_);
const BeamSpotObjects* spotDB = beamhandle.product();
Expand Down
18 changes: 11 additions & 7 deletions RecoVertex/BeamSpotProducer/plugins/OnlineBeamSpotESProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ OnlineBeamSpotESProducer::OnlineBeamSpotESProducer(const edm::ParameterSet& p) {
fakeBS_.SetBeamWidthX(0.1);
fakeBS_.SetBeamWidthY(0.1);
fakeBS_.SetSigmaZ(15.);
fakeBS_.SetPosition(0., 0., 0.);
fakeBS_.SetPosition(0.0001, 0.0001, 0.0001);
fakeBS_.SetType(-1);

bsHLTToken_ = cc.consumesFrom<BeamSpotOnlineObjects, BeamSpotOnlineHLTObjectsRcd>();
Expand All @@ -57,18 +57,18 @@ void OnlineBeamSpotESProducer::fillDescriptions(edm::ConfigurationDescriptions&
const BeamSpotOnlineObjects* OnlineBeamSpotESProducer::compareBS(const BeamSpotOnlineObjects* bs1,
const BeamSpotOnlineObjects* bs2) {
//Random logic so far ...
if (bs1->GetSigmaZ() - 0.0001 < bs2->GetSigmaZ()) { //just temporary for debugging
if (bs1->GetSigmaZ() > 5.) {
if (bs1->GetSigmaZ() - 0.0001 > bs2->GetSigmaZ()) { //just temporary for debugging
if (bs1->GetSigmaZ() > 2.5) {
return bs1;
} else {
return bs2;
return nullptr;
}

} else {
if (bs2->GetSigmaZ() > 5.) {
if (bs2->GetSigmaZ() > 2.5) {
return bs2;
} else {
return bs1;
return nullptr;
}
}
}
Expand All @@ -88,7 +88,11 @@ std::shared_ptr<const BeamSpotObjects> OnlineBeamSpotESProducer::produce(const B
} else {
best = &hltRec->get(bsHLTToken_);
}
return std::shared_ptr<const BeamSpotObjects>(best, edm::do_nothing_deleter());
if (best) {
return std::shared_ptr<const BeamSpotObjects>(best, edm::do_nothing_deleter());
} else {
return std::shared_ptr<const BeamSpotObjects>(&fakeBS_, edm::do_nothing_deleter());
}
};

DEFINE_FWK_EVENTSETUP_MODULE(OnlineBeamSpotESProducer);
4 changes: 4 additions & 0 deletions RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from RecoVertex.BeamSpotProducer.BeamSpotOnline_cfi import *

#scalers = cms.EDProducer('ScalersRawToDigi')
BeamSpotESProducer = cms.ESProducer("OnlineBeamSpotESProducer")

from Configuration.Eras.Modifier_run3_common_cff import run3_common
run3_common.toModify(onlineBeamSpotProducer, useTransientRecord = True)

onlineBeamSpot = cms.Sequence( onlineBeamSpotProducer )

3 changes: 2 additions & 1 deletion RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
maxZ = cms.double(40),
maxRadius = cms.double(2),
setSigmaZ = cms.double(-1), #negative value disables it.
gtEvmLabel = cms.InputTag('gtEvmDigis')
gtEvmLabel = cms.InputTag('gtEvmDigis'),
useTransientRecord = cms.bool(False)
)

Loading

0 comments on commit dbeaf36

Please sign in to comment.