Skip to content

Commit

Permalink
Merge pull request #41674 from mmusich/13_0_X_useFakeBSasFallBackInOn…
Browse files Browse the repository at this point in the history
…lineProducer2

[13.0.X] Produce a fake `BeamSpot` object in `BeamSpotOnlineProducer` when using transient record logic and `OnlineBeamSpotESProducer` returned a fake
  • Loading branch information
cmsbuild authored May 16, 2023
2 parents 1f37da9 + 991186b commit 6e9ea6c
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class BeamSpotOnlineObjects : public BeamSpotObjects {
enum TimeParamIndex { CREATE_TIME = 0, START_TIMESTAMP = 1, END_TIMESTAMP = 2, TSIZE = 3 };

/// Setters Methods
// copy all copiable members from BeamSpotObjects
void copyFromBeamSpotObject(const BeamSpotObjects& bs);

// set lastAnalyzedLumi_, last analyzed lumisection
void setLastAnalyzedLumi(int val) { lastAnalyzedLumi_ = val; }

Expand Down Expand Up @@ -164,4 +167,4 @@ class BeamSpotOnlineObjects : public BeamSpotObjects {

std::ostream& operator<<(std::ostream&, BeamSpotOnlineObjects beam);

#endif
#endif
23 changes: 22 additions & 1 deletion CondFormats/BeamSpotObjects/src/BeamSpotOnlineObjects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ cond::Time_t BeamSpotOnlineObjects::endTimeStamp() const {
}

// setters
void BeamSpotOnlineObjects::copyFromBeamSpotObject(const BeamSpotObjects& bs) {
setType(bs.beamType());
setPosition(bs.x(), bs.y(), bs.z());
setSigmaZ(bs.sigmaZ());
setdxdz(bs.dxdz());
setdydz(bs.dydz());
setBeamWidthX(bs.beamWidthX());
setBeamWidthY(bs.beamWidthY());
setBeamWidthXError(bs.beamWidthXError());
setBeamWidthYError(bs.beamWidthYError());
setEmittanceX(bs.emittanceX());
setEmittanceY(bs.emittanceY());
setBetaStar(bs.betaStar());

for (int i = 0; i < 7; ++i) {
for (int j = 0; j < 7; ++j) {
setCovariance(i, j, bs.covariance(i, j));
}
}
}

void BeamSpotOnlineObjects::setNumTracks(int nTracks) {
BeamSpotOnlineObjectsImpl::setOneParam(intParams_, NUM_TRACKS, nTracks);
}
Expand Down Expand Up @@ -173,4 +194,4 @@ std::ostream& operator<<(std::ostream& os, BeamSpotOnlineObjects beam) {
beam.print(ss);
os << ss.str();
return os;
}
}
194 changes: 194 additions & 0 deletions CondTools/BeamSpot/plugins/BeamSpotOnlineFromOfflineConverter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// -*- 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 <memory>
#include <string>
#include <fstream>
#include <iostream>
#include <ctime>

// 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<BeamSpotObjects, BeamSpotObjectsRcd> bsToken_;

// parameters that can't be copied from the BeamSpotObject
const int lastAnalyzedLumi_, lastAnalyzedRun_, lastAnalyzedFill_;
const int numTracks_, numPVs_, numUsedEvents_, numMaxPVs_;
const float meanPVs_, meanPVError_, rmsPV_, rmsPVError_;
const std::string startTime_, endTime_, lumiRange_;

// IoV-structure
const bool fIsHLT_;
uint32_t fIOVStartRun_;
uint32_t fIOVStartLumi_;
cond::Time_t fnewSince_;
bool fuseNewSince_;
};

//
// constructors and destructor
//
BeamSpotOnlineFromOfflineConverter::BeamSpotOnlineFromOfflineConverter(const edm::ParameterSet& iConfig)
: bsToken_(esConsumes()),
lastAnalyzedLumi_(iConfig.getParameter<double>("lastAnalyzedLumi")),
lastAnalyzedRun_(iConfig.getParameter<double>("lastAnalyzedRun")),
lastAnalyzedFill_(iConfig.getParameter<double>("lastAnalyzedFill")),
numTracks_(iConfig.getParameter<int>("numTracks")),
numPVs_(iConfig.getParameter<int>("numPVs")),
numUsedEvents_(iConfig.getParameter<int>("numUsedEvents")),
numMaxPVs_(iConfig.getParameter<int>("numMaxPVs")),
meanPVs_(iConfig.getParameter<double>("meanPVs")),
meanPVError_(iConfig.getParameter<double>("meanPVError")),
rmsPV_(iConfig.getParameter<double>("rmsPVs")),
rmsPVError_(iConfig.getParameter<double>("rmsPVError")),
startTime_(iConfig.getParameter<std::string>("startTime")),
endTime_(iConfig.getParameter<std::string>("endTime")),
lumiRange_(iConfig.getParameter<std::string>("lumiRange")),
fIsHLT_(iConfig.getParameter<bool>("isHLT")) {
if (iConfig.exists("IOVStartRun") && iConfig.exists("IOVStartLumi")) {
fIOVStartRun_ = iConfig.getUntrackedParameter<uint32_t>("IOVStartRun");
fIOVStartLumi_ = iConfig.getUntrackedParameter<uint32_t>("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.copyFromBeamSpotObject(*inputSpot);
abeam.setLastAnalyzedLumi(lastAnalyzedLumi_);
abeam.setLastAnalyzedRun(lastAnalyzedRun_);
abeam.setLastAnalyzedFill(lastAnalyzedFill_);
abeam.setStartTimeStamp(std::time(nullptr));
abeam.setEndTimeStamp(std::time(nullptr));
abeam.setNumTracks(numTracks_);
abeam.setNumPVs(numPVs_);
abeam.setUsedEvents(numUsedEvents_);
abeam.setMaxPVs(numMaxPVs_);
abeam.setMeanPV(meanPVs_);
abeam.setMeanErrorPV(meanPVError_);
abeam.setRmsPV(rmsPV_);
abeam.setRmsErrorPV(rmsPVError_);
abeam.setStartTime(startTime_);
abeam.setEndTime(endTime_);
abeam.setLumiRange(lumiRange_);

// Set the creation time of the payload to the current time
auto creationTime =
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
abeam.setCreationTime(creationTime);

edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << " Writing results to DB...";
edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << abeam;

edm::Service<cond::service::PoolDBOutputService> 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<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel);
} else
poolDbService->createOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->beginOfTime(), fLabel);
} else {
edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "no new tag requested";
if (fuseNewSince_) {
edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "Using a new Since: " << fnewSince_;
poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, fnewSince_, fLabel);
} else
poolDbService->appendOneIOV<BeamSpotOnlineObjects>(abeam, poolDbService->currentTime(), fLabel);
}
}
edm::LogPrint("BeamSpotOnlineFromOfflineConverter") << "[BeamSpotOnlineFromOfflineConverter] analyze done \n";
}

// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
void BeamSpotOnlineFromOfflineConverter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<bool>("isHLT", true);
desc.addOptionalUntracked<uint32_t>("IOVStartRun", 1);
desc.addOptionalUntracked<uint32_t>("IOVStartLumi", 1);
desc.add<double>("lastAnalyzedLumi", 1000);
desc.add<double>("lastAnalyzedRun", 1);
desc.add<double>("lastAnalyzedFill", -999);
desc.add<int>("numTracks", 0);
desc.add<int>("numPVs", 0);
desc.add<int>("numUsedEvents", 0);
desc.add<int>("numMaxPVs", 0);
desc.add<double>("meanPVs", 0.);
desc.add<double>("meanPVError", 0.);
desc.add<double>("rmsPVs", 0.);
desc.add<double>("rmsPVError", 0.);
desc.add<std::string>("startTime", std::string(""));
desc.add<std::string>("endTime", std::string(""));
desc.add<std::string>("lumiRange", std::string(""));
descriptions.addWithDefaultLabel(desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(BeamSpotOnlineFromOfflineConverter);
101 changes: 101 additions & 0 deletions CondTools/BeamSpot/test/BeamSpotOnlineFromOfflineConverter_cfg.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 5 additions & 1 deletion CondTools/BeamSpot/test/testReadWriteOnlineBSFromDB.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ cmsRun ${LOCAL_TEST_DIR}/BeamSpotOnlineRecordsReader_cfg.py unitTest=True inputR
printf "TESTING Reading BeamSpotOnlineHLTObjectsRcd DB object ...\n\n"
cmsRun ${LOCAL_TEST_DIR}/BeamSpotOnlineRecordsReader_cfg.py unitTest=True inputRecord=BeamSpotOnlineHLTObjectsRcd || die "Failure reading payload for BeamSpotOnlineHLTObjectsRcd" $?

echo "TESTING reading BeamSpotObjectRcd DB object ...\n\n"
printf "TESTING reading BeamSpotObjectRcd DB object ...\n\n"
cmsRun ${LOCAL_TEST_DIR}/BeamSpotRcdPrinter_cfg.py || die "Failure running BeamSpotRcdPrinter" $?

printf "TESTING converting BeamSpotOnlineObjects from BeamSpotObjects ...\n\n"
cmsRun ${LOCAL_TEST_DIR}/BeamSpotOnlineFromOfflineConverter_cfg.py unitTest=True || die "Failure running BeamSpotRcdPrinter" $?

Loading

0 comments on commit 6e9ea6c

Please sign in to comment.