From 13aecbd5fc06f10ecdc62037b5574e596156cbe7 Mon Sep 17 00:00:00 2001 From: francescobrivio Date: Sun, 19 Sep 2021 11:30:04 +0200 Subject: [PATCH] add real OnlineBeamSpotESProducer logic --- .../plugins/OnlineBeamSpotESProducer.cc | 74 +++++++++++++++++-- .../python/BeamSpotOnline_cff.py | 5 +- .../BeamSpotProducer/test/test_scalars.py | 36 ++++----- 3 files changed, 84 insertions(+), 31 deletions(-) diff --git a/RecoVertex/BeamSpotProducer/plugins/OnlineBeamSpotESProducer.cc b/RecoVertex/BeamSpotProducer/plugins/OnlineBeamSpotESProducer.cc index 3bfe70aea6910..2ec8643b7dfcb 100644 --- a/RecoVertex/BeamSpotProducer/plugins/OnlineBeamSpotESProducer.cc +++ b/RecoVertex/BeamSpotProducer/plugins/OnlineBeamSpotESProducer.cc @@ -30,13 +30,22 @@ class OnlineBeamSpotESProducer : public edm::ESProducer { private: const BeamSpotOnlineObjects* compareBS(const BeamSpotOnlineObjects* bs1, const BeamSpotOnlineObjects* bs2); - BeamSpotObjects fakeBS_; + const BeamSpotOnlineObjects* checkSingleBS(const BeamSpotOnlineObjects* bs1); edm::ESGetToken const bsToken_; edm::ESGetToken bsHLTToken_; edm::ESGetToken bsLegacyToken_; + + BeamSpotObjects fakeBS_; + int timeThreshold_; + double sigmaZThreshold_; }; + OnlineBeamSpotESProducer::OnlineBeamSpotESProducer(const edm::ParameterSet& p) { + // get parameters + timeThreshold_ = p.getParameter("timeThreshold"); + sigmaZThreshold_ = p.getParameter("sigmaZThreshold"); + auto cc = setWhatProduced(this); fakeBS_.SetBeamWidthX(0.1); @@ -51,21 +60,50 @@ OnlineBeamSpotESProducer::OnlineBeamSpotESProducer(const edm::ParameterSet& p) { void OnlineBeamSpotESProducer::fillDescriptions(edm::ConfigurationDescriptions& desc) { edm::ParameterSetDescription dsc; + dsc.add("timeThreshold", 48); // hours + dsc.add("sigmaZThreshold", 2.); // cm desc.addWithDefaultLabel(dsc); } 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() > 2.5) { + // Current time to be compared with the one read from the payload + auto currentTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); + + // Get two beamspot creation times and compute the time difference wrt currentTime + auto bs1time = std::chrono::microseconds(bs1->GetCreationTime()); + auto diffBStime1 = (currentTime - bs1time).count(); + auto bs2time = std::chrono::microseconds(bs2->GetCreationTime()); + auto diffBStime2 = (currentTime - bs2time).count(); + + // Convert timeThreshold_ from hours to microseconds for comparison + auto limitTime = std::chrono::microseconds((std::chrono::hours)timeThreshold_).count(); + + // Logic to choose between the two BeamSpots: + // 1. If both BS are older than limitTime retun fake BS + // 2. If only one BS is newer than limitTime return it only if it has + // sigmaZ larger than sigmaZthreshold_ and the fit converged (BeamType 2) + // 3. If both are newer than the limit threshold return + // the BS that converged and has larger sigmaZ + if (diffBStime1 > limitTime && diffBStime2 > limitTime) { + return nullptr; + } else if (diffBStime2 > limitTime) { + if (bs1->GetSigmaZ() > sigmaZThreshold_ && bs1->GetBeamType() == 2) { return bs1; } else { return nullptr; } - + } else if (diffBStime1 > limitTime) { + if (bs2->GetSigmaZ() > sigmaZThreshold_ && bs2->GetBeamType() == 2) { + return bs2; + } else { + return nullptr; + } } else { - if (bs2->GetSigmaZ() > 2.5) { + if (bs1->GetSigmaZ() > bs2->GetSigmaZ() && bs1->GetBeamType() == 2) { + return bs1; + } else if (bs2->GetSigmaZ() > bs1->GetSigmaZ() && bs2->GetBeamType() == 2) { return bs2; } else { return nullptr; @@ -73,6 +111,26 @@ const BeamSpotOnlineObjects* OnlineBeamSpotESProducer::compareBS(const BeamSpotO } } +const BeamSpotOnlineObjects* OnlineBeamSpotESProducer::checkSingleBS(const BeamSpotOnlineObjects* bs1) { + // Current time to be compared with the one read from the payload + auto currentTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); + + // Get the beamspot creation time and compute the time difference wrt currentTime + auto bs1time = std::chrono::microseconds(bs1->GetCreationTime()); + auto diffBStime1 = (currentTime - bs1time).count(); + + // Convert timeThreshold_ from hours to microseconds for comparison + auto limitTime = std::chrono::microseconds((std::chrono::hours)timeThreshold_).count(); + + // Check that the BS is within the timeThreshold, converges and passes the sigmaZthreshold + if (diffBStime1 < limitTime && bs1->GetSigmaZ() > sigmaZThreshold_ && bs1->GetBeamType() == 2) { + return bs1; + } else { + return nullptr; + } +} + std::shared_ptr OnlineBeamSpotESProducer::produce(const BeamSpotTransientObjectsRcd& iRecord) { auto legacyRec = iRecord.tryToGetRecord(); auto hltRec = iRecord.tryToGetRecord(); @@ -84,9 +142,9 @@ std::shared_ptr OnlineBeamSpotESProducer::produce(const B if (legacyRec and hltRec) { best = compareBS(&legacyRec->get(bsLegacyToken_), &hltRec->get(bsHLTToken_)); } else if (legacyRec) { - best = &legacyRec->get(bsLegacyToken_); + best = checkSingleBS(&legacyRec->get(bsLegacyToken_)); } else { - best = &hltRec->get(bsHLTToken_); + best = checkSingleBS(&hltRec->get(bsHLTToken_)); } if (best) { return std::shared_ptr(best, edm::do_nothing_deleter()); diff --git a/RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cff.py b/RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cff.py index afd9bdb7ab8ef..1fc9d98c4a1b5 100644 --- a/RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cff.py +++ b/RecoVertex/BeamSpotProducer/python/BeamSpotOnline_cff.py @@ -3,7 +3,10 @@ from RecoVertex.BeamSpotProducer.BeamSpotOnline_cfi import * #scalers = cms.EDProducer('ScalersRawToDigi') -BeamSpotESProducer = cms.ESProducer("OnlineBeamSpotESProducer") +BeamSpotESProducer = cms.ESProducer("OnlineBeamSpotESProducer", + timeThreshold = cms.int32(48), + sigmaZThreshold = cms.double(2.0) + ) from Configuration.Eras.Modifier_run3_common_cff import run3_common run3_common.toModify(onlineBeamSpotProducer, useTransientRecord = True) diff --git a/RecoVertex/BeamSpotProducer/test/test_scalars.py b/RecoVertex/BeamSpotProducer/test/test_scalars.py index 58e3908bc8162..8b3dae3876766 100644 --- a/RecoVertex/BeamSpotProducer/test/test_scalars.py +++ b/RecoVertex/BeamSpotProducer/test/test_scalars.py @@ -5,8 +5,8 @@ process.source = cms.Source("PoolSource", fileNames = cms.untracked.vstring( - "file:/eos/cms/store/data/Commissioning2021/Cosmics/RAW/v1/000/342/218/00000/fdaf9009-dfd8-4774-9246-556088e65e9b.root", - "file:/eos/cms/store/data/Commissioning2021/Cosmics/RAW/v1/000/342/094/00000/7e88d2e8-6632-40f0-a1ca-4350adf60182.root" + "file:/eos/cms/store/data/Commissioning2021/Cosmics/RAW/v1/000/344/518/00000/00130ffc-3e69-4106-8151-c69dd735ee2e.root", + "file:/eos/cms/store/data/Commissioning2021/Cosmics/RAW/v1/000/344/518/00000/001d10b6-a19e-4889-ba07-88f8f6b17bc7.root" ), skipEvents = cms.untracked.uint32(0) ) @@ -17,31 +17,23 @@ process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) -) -process.load("CondCore.DBCommon.CondDBSetup_cfi") +) +process.load("CondCore.CondDB.CondDB_cfi") from Configuration.AlCa.GlobalTag import GlobalTag as customiseGlobalTag process.GlobalTag = customiseGlobalTag(globaltag = "auto:run3_hlt_GRun") - -process.BeamSpotDBSource = cms.ESSource("PoolDBESSource", - process.CondDBSetup, - toGet = cms.VPSet( - cms.PSet( - record = cms.string('BeamSpotOnlineLegacyObjectsRcd'), - tag = cms.string("BeamSpotOnlineTestLegacy"), - refreshTime = cms.uint64(1) - - ), - cms.PSet( - record = cms.string('BeamSpotOnlineHLTObjectsRcd'), - tag = cms.string('BeamSpotOnlineTestHLT'), - refreshTime = cms.uint64(1) - ) - ), - #connect = cms.string('oracle://cms_orcon_prod/CMS_CONDITIONS') - connect = cms.string('frontier://FrontierProd/CMS_CONDITIONS') +process.GlobalTag.toGet = cms.VPSet( + cms.PSet( + record = cms.string("BeamSpotOnlineLegacyObjectsRcd"), + refreshTime = cms.uint64(1) + ), + cms.PSet( + record = cms.string("BeamSpotOnlineHLTObjectsRcd"), + refreshTime = cms.uint64(1) + ) ) + process.MessageLogger = cms.Service("MessageLogger", cerr = cms.untracked.PSet( enable = cms.untracked.bool(False)