diff --git a/CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc b/CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc new file mode 100644 index 0000000000000..18fc03ba9246a --- /dev/null +++ b/CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc @@ -0,0 +1,174 @@ +// -*- C++ -*- +// +// Package: CondTools/BeamSpot +// Class: BeamSpotOnlineFromOfflineConverter +// +/**\class BeamSpotOnlineFromOfflineConverter BeamSpotOnlineFromOfflineConverter.cc CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc + + Description: EDAnalyzer to create a BeamSpotOnlineHLTObjectsRcd from a BeamSpotObjectsRcd (inserting some parameters manually) + + Implementation: + [Notes on implementation] +*/ +// +// Original Author: Marco Musich +// Created: Sat, 06 May 2023 21:10:00 GMT +// +// + +// system include files +#include +#include +#include +#include +#include + +// user include files +#include "CondCore/DBOutputService/interface/PoolDBOutputService.h" +#include "CondFormats/BeamSpotObjects/interface/BeamSpotObjects.h" +#include "CondFormats/BeamSpotObjects/interface/BeamSpotOnlineObjects.h" +#include "CondFormats/DataRecord/interface/BeamSpotObjectsRcd.h" +#include "CondFormats/DataRecord/interface/BeamSpotOnlineHLTObjectsRcd.h" +#include "CondFormats/DataRecord/interface/BeamSpotOnlineLegacyObjectsRcd.h" +#include "FWCore/Framework/interface/ESHandle.h" +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/EventSetup.h" +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/Framework/interface/one/EDAnalyzer.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/ServiceRegistry/interface/Service.h" +#include "FWCore/Utilities/interface/InputTag.h" + +// +// class declaration +// + +class BeamSpotOnlineFromOfflineConverter : public edm::one::EDAnalyzer<> { +public: + explicit BeamSpotOnlineFromOfflineConverter(const edm::ParameterSet&); + ~BeamSpotOnlineFromOfflineConverter() override = default; + + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + cond::Time_t pack(uint32_t, uint32_t); + +private: + void analyze(const edm::Event&, const edm::EventSetup&) override; + + // ----------member data --------------------------- + const edm::ESGetToken bsToken_; + + // IoV-structure + bool fIsHLT_; + uint32_t fIOVStartRun_; + uint32_t fIOVStartLumi_; + cond::Time_t fnewSince_; + bool fuseNewSince_; + + // parameters that can't be copied from the BeamSpotObject + int lastAnalyzedLumi_, lastAnalyzedRun_, lastAnalyzedFill_; +}; + +// +// constructors and destructor +// +BeamSpotOnlineFromOfflineConverter::BeamSpotOnlineFromOfflineConverter(const edm::ParameterSet& iConfig) + : bsToken_(esConsumes()) { + lastAnalyzedLumi_ = iConfig.getParameter("lastAnalyzedLumi"); + lastAnalyzedRun_ = iConfig.getParameter("lastAnalyzedRun"); + lastAnalyzedFill_ = iConfig.getParameter("lastAnalyzedFill"); + + fIsHLT_ = iConfig.getParameter("isHLT"); + if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) { + fIOVStartRun_ = iConfig.getUntrackedParameter("IOVStartRun"); + fIOVStartLumi_ = iConfig.getUntrackedParameter("IOVStartLumi"); + fnewSince_ = BeamSpotOnlineFromOfflineConverter::pack(fIOVStartRun_, fIOVStartLumi_); + fuseNewSince_ = true; + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = True"; + } else { + fuseNewSince_ = false; + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "useNewSince = False"; + } +} + +// +// member functions +// + +// ------------ Create a since object (cond::Time_t) by packing Run and LS (both uint32_t) ------------ +cond::Time_t BeamSpotOnlineFromOfflineConverter::pack(uint32_t fIOVStartRun, uint32_t fIOVStartLumi) { + return ((uint64_t)fIOVStartRun << 32 | fIOVStartLumi); +} + +// ------------ method called for each event ------------ +void BeamSpotOnlineFromOfflineConverter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) { + const std::string fLabel = (fIsHLT_) ? "BeamSpotOnlineHLTObjectsRcd" : "BeamSpotOnlineLegacyObjectsRcd"; + const BeamSpotObjects* inputSpot = &iSetup.getData(bsToken_); + + BeamSpotOnlineObjects abeam; + + abeam.setLastAnalyzedLumi(lastAnalyzedLumi_); + abeam.setLastAnalyzedRun(lastAnalyzedRun_); + abeam.setLastAnalyzedFill(lastAnalyzedFill_); + abeam.setStartTimeStamp(std::time(nullptr)); + abeam.setEndTimeStamp(std::time(nullptr)); + abeam.setType(inputSpot->beamType()); + abeam.setPosition(inputSpot->x(), inputSpot->y(), inputSpot->z()); + abeam.setSigmaZ(inputSpot->sigmaZ()); + abeam.setdxdz(inputSpot->dxdz()); + abeam.setdydz(inputSpot->dydz()); + abeam.setBeamWidthX(inputSpot->beamWidthX()); + abeam.setBeamWidthY(inputSpot->beamWidthY()); + abeam.setEmittanceX(inputSpot->emittanceX()); + abeam.setEmittanceY(inputSpot->emittanceY()); + abeam.setBetaStar(inputSpot->betaStar()); + + for (int i = 0; i < 7; ++i) { + for (int j = 0; j < 7; ++j) { + abeam.setCovariance(i, j, inputSpot->covariance(i, j)); + } + } + + // Set the creation time of the payload to the current time + auto creationTime = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + abeam.setCreationTime(creationTime); + + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << " Writing results to DB..."; + + edm::Service poolDbService; + if (poolDbService.isAvailable()) { + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "poolDBService available"; + if (poolDbService->isNewTagRequest(fLabel)) { + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "new tag requested"; + if (fuseNewSince_) { + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_; + poolDbService->createOneIOV(abeam, fnewSince_, fLabel); + } else + poolDbService->createOneIOV(abeam, poolDbService->beginOfTime(), fLabel); + } else { + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "no new tag requested"; + if (fuseNewSince_) { + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_; + poolDbService->appendOneIOV(abeam, fnewSince_, fLabel); + } else + poolDbService->appendOneIOV(abeam, poolDbService->currentTime(), fLabel); + } + } + edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "[BeamSpotOnlineFromOfflineConverter] endJob done \n"; +} + +// ------------ method fills 'descriptions' with the allowed parameters for the module ------------ +void BeamSpotOnlineFromOfflineConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { + edm::ParameterSetDescription desc; + desc.add("isHLT", true); + desc.addOptionalUntracked("IOVStartRun", 1); + desc.addOptionalUntracked("IOVStartLumi", 1); + desc.add("lastAnalyzedLumi", 1000); + desc.add("lastAnalyzedRun", 1); + desc.add("lastAnalyzedFill", -999); + descriptions.addWithDefaultLabel(desc); +} + +//define this as a plug-in +DEFINE_FWK_MODULE(BeamSpotOnlineFromOfflineConverter); diff --git a/CondTools/BeamSpot/test/BeamSpotOnlineFromOfflineConverter_cfg.py b/CondTools/BeamSpot/test/BeamSpotOnlineFromOfflineConverter_cfg.py new file mode 100644 index 0000000000000..84361db367bbd --- /dev/null +++ b/CondTools/BeamSpot/test/BeamSpotOnlineFromOfflineConverter_cfg.py @@ -0,0 +1,101 @@ +import FWCore.ParameterSet.Config as cms +import FWCore.ParameterSet.VarParsing as VarParsing + +process = cms.Process("READ") + +options = VarParsing.VarParsing() +options.register('unitTest', + False, # default value + VarParsing.VarParsing.multiplicity.singleton, # singleton or list + VarParsing.VarParsing.varType.bool, # string, int, or float + "are we running the unit test?") +options.register('inputTag', + "myTagName", # default value + VarParsing.VarParsing.multiplicity.singleton, # singleton or list + VarParsing.VarParsing.varType.string, # string, int, or float + "output tag name") +options.register('inputRecord', + "BeamSpotOnlineLegacyObjectsRcd", # default value + VarParsing.VarParsing.multiplicity.singleton, # singleton or list + VarParsing.VarParsing.varType.string, # string, int, or float + "type of record") +options.register('startRun', + 1, # default value + VarParsing.VarParsing.multiplicity.singleton, # singleton or list + VarParsing.VarParsing.varType.int, # string, int, or float + "location of the input data") +options.register('startLumi', + 1, # default value + VarParsing.VarParsing.multiplicity.singleton, # singleton or list + VarParsing.VarParsing.varType.int, # string, int, or float + "IOV Start Lumi") +options.parseArguments() + +process.load("FWCore.MessageService.MessageLogger_cfi") +process.MessageLogger.cerr.FwkReport.reportEvery = 100000 # do not clog output with IO + +process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1)) + +#################################################################### +# Empty source +#################################################################### +process.source = cms.Source("EmptySource", + firstRun = cms.untracked.uint32(options.startRun), + firstLuminosityBlock = cms.untracked.uint32(options.startLumi), + numberEventsInLuminosityBlock = cms.untracked.uint32(1), + numberEventsInRun = cms.untracked.uint32(1)) + +#################################################################### +# Connect to conditions DB +#################################################################### + +# either from Global Tag +process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff") +from Configuration.AlCa.GlobalTag import GlobalTag +process.GlobalTag = GlobalTag(process.GlobalTag,"auto:phase2_realistic") + +# ...or specify database connection and tag: +#from CondCore.CondDB.CondDB_cfi import * +#CondDBBeamSpotObjects = CondDB.clone(connect = cms.string('frontier://FrontierProd/CMS_CONDITIONS')) +#process.dbInput = cms.ESSource("PoolDBESSource", +# CondDBBeamSpotObjects, +# toGet = cms.VPSet(cms.PSet(record = cms.string('BeamSpotOnlineLegacyObjectsRcd'), # BeamSpotOnlineLegacy record +# tag = cms.string('BSLegacy_tag') # choose your favourite tag +# ) +# ) +# ) +# ...or from a local db file +# input database (in this case the local sqlite file) + +#################################################################### +# Load and configure outputservice +#################################################################### +if options.unitTest : + if options.inputRecord == "BeamSpotOnlineLegacyObjectsRcd" : + tag_name = 'BSLegacy_tag' + else: + tag_name = 'BSHLT_tag' +else: + tag_name = options.inputTag + +from CondCore.CondDB.CondDB_cfi import * +CondDBBeamSpotObjects = CondDB.clone(connect = cms.string('sqlite_file:test_%s.db' % tag_name)) # choose an output name +process.PoolDBOutputService = cms.Service("PoolDBOutputService", + CondDBBeamSpotObjects, + timetype = cms.untracked.string('lumiid'), #('lumiid'), #('runnumber') + toPut = cms.VPSet(cms.PSet(record = cms.string(options.inputRecord), # BeamSpotOnline record + tag = cms.string(tag_name))), # choose your favourite tag + loadBlobStreamer = cms.untracked.bool(False) + ) + +isForHLT = (options.inputRecord == "BeamSpotOnlineHLTObjectsRcd") +print("isForHLT: ",isForHLT) + +#################################################################### +# Load and configure analyzer +#################################################################### +from CondTools.BeamSpot.beamSpotOnlineFromOfflineConverter_cfi import beamSpotOnlineFromOfflineConverter +process.BeamSpotOnlineFromOfflineConverter = beamSpotOnlineFromOfflineConverter.clone(isHLT = isForHLT) + +# Put module in path: +process.p = cms.Path(process.BeamSpotOnlineFromOfflineConverter)