Skip to content

Commit

Permalink
add SimBeamSpotObjects reader and use cfi files
Browse files Browse the repository at this point in the history
  • Loading branch information
francescobrivio committed Jun 13, 2023
1 parent ad63263 commit 03af6ab
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 29 deletions.
183 changes: 183 additions & 0 deletions CondTools/BeamSpot/plugins/BeamProfile2DBReader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// -*- C++ -*-
//
// Package: CondTools/BeamProfile2DBReader
// Class: BeamProfile2DBReader
//
/**\class BeamProfile2DBReader BeamProfile2DBReader.cc CondTools/BeamSpot/plugins/BeamProfile2DBReader.cc
Description: simple emd::one::EDAnalyzer to retrieve and ntuplize SimBeamSpot data from the conditions database
Implementation:
[Notes on implementation]
*/
//
// Original Author: Francesco Brivio
// Created: 11 June 2023
//

// system include files
#include <memory>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESWatcher.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "CondFormats/DataRecord/interface/SimBeamSpotObjectsRcd.h"
#include "CondFormats/BeamSpotObjects/interface/SimBeamSpotObjects.h"

// For ROOT
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CommonTools/UtilAlgos/interface/TFileService.h"
#include <TTree.h>

#include <sstream>
#include <fstream>

//
// class declaration
//

class BeamProfile2DBReader : public edm::one::EDAnalyzer<edm::one::SharedResources> {
public:
explicit BeamProfile2DBReader(const edm::ParameterSet&);
~BeamProfile2DBReader() override;

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
void beginJob() override;
void analyze(const edm::Event&, const edm::EventSetup&) override;

struct theBSfromDB {
int run;
int ls;
double fX0, fY0, fZ0;
double fSigmaZ;
double fbetastar, femittance;
double fPhi, fAlpha;
double fTimeOffset;
void init();
} theBSfromDB_;

const edm::ESGetToken<SimBeamSpotObjects, SimBeamSpotObjectsRcd> beamSpotToken_;
edm::Service<TFileService> tFileService;
TTree* bstree_;

// ----------member data ---------------------------
edm::ESWatcher<SimBeamSpotObjectsRcd> watcher_;
std::unique_ptr<std::ofstream> output_;
};

//
// constructors and destructor
//
BeamProfile2DBReader::BeamProfile2DBReader(const edm::ParameterSet& iConfig)
: beamSpotToken_(esConsumes()), bstree_(nullptr) {
//now do what ever initialization is needed
usesResource("TFileService");
std::string fileName(iConfig.getUntrackedParameter<std::string>("rawFileName"));
if (!fileName.empty()) {
output_ = std::make_unique<std::ofstream>(fileName.c_str());
if (!output_->good()) {
edm::LogError("IOproblem") << "Could not open output file " << fileName << ".";
output_.reset();
}
}
}

BeamProfile2DBReader::~BeamProfile2DBReader() = default;

//
// member functions
//

void BeamProfile2DBReader::theBSfromDB::init() {
float dummy_double = 0.0;
int dummy_int = 0;

run = dummy_int;
ls = dummy_int;
fX0 = dummy_double;
fY0 = dummy_double;
fZ0 = dummy_double;
fSigmaZ = dummy_double;
fbetastar = dummy_double;
femittance = dummy_double;
fPhi = dummy_double;
fAlpha = dummy_double;
fTimeOffset = dummy_double;
}

// ------------ method called for each event ------------
void BeamProfile2DBReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
using namespace edm;
std::ostringstream output;

// initialize the ntuple
theBSfromDB_.init();

if (watcher_.check(iSetup)) { // check for new IOV for this run / LS

output << " for runs: " << iEvent.id().run() << " - " << iEvent.id().luminosityBlock() << std::endl;

// Get SimBeamSpot from EventSetup:
const SimBeamSpotObjects* mybeamspot = &iSetup.getData(beamSpotToken_);

theBSfromDB_.run = iEvent.id().run();
theBSfromDB_.ls = iEvent.id().luminosityBlock();
theBSfromDB_.fX0 = mybeamspot->x();
theBSfromDB_.fY0 = mybeamspot->y();
theBSfromDB_.fZ0 = mybeamspot->z();
theBSfromDB_.fSigmaZ = mybeamspot->sigmaZ();
theBSfromDB_.fbetastar = mybeamspot->betaStar();
theBSfromDB_.femittance = mybeamspot->emittance();
theBSfromDB_.fPhi = mybeamspot->phi();
theBSfromDB_.fAlpha = mybeamspot->alpha();
theBSfromDB_.fTimeOffset = mybeamspot->timeOffset();
bstree_->Fill();
output << *mybeamspot << std::endl;
}

// Final output - either message logger or output file:
if (output_.get())
*output_ << output.str();
else
edm::LogInfo("") << output.str();
}

// ------------ method called once each job just before starting event loop ------------
void BeamProfile2DBReader::beginJob() {
bstree_ = tFileService->make<TTree>("BSNtuple", "SimBeamSpot analyzer ntuple");

//Tree Branches
bstree_->Branch("run", &theBSfromDB_.run, "run/I");
bstree_->Branch("ls", &theBSfromDB_.ls, "ls/I");
bstree_->Branch("BSx0", &theBSfromDB_.fX0, "BSx0/F");
bstree_->Branch("BSy0", &theBSfromDB_.fY0, "BSy0/F");
bstree_->Branch("BSz0", &theBSfromDB_.fZ0, "BSz0/F");
bstree_->Branch("Beamsigmaz", &theBSfromDB_.fSigmaZ, "Beamsigmaz/F");
bstree_->Branch("BetaStar", &theBSfromDB_.fbetastar, "BetaStar/F");
bstree_->Branch("Emittance", &theBSfromDB_.femittance, "Emittance/F");
bstree_->Branch("Phi", &theBSfromDB_.fPhi, "Phi/F");
bstree_->Branch("Alpha", &theBSfromDB_.fAlpha, "Alpha/F");
bstree_->Branch("TimeOffset", &theBSfromDB_.fTimeOffset, "TimeOffset/F");
}

// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
void BeamProfile2DBReader::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
//The following says we do not know what parameters are allowed so do no validation
// Please change this to state exactly what you do use, even if it is no parameters
edm::ParameterSetDescription desc;
desc.setUnknown();
descriptions.addDefault(desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(BeamProfile2DBReader);
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// -*- C++ -*-
//
// Package: BeamProfile2DB
// Class: BeamProfile2DB
// Package: BeamProfile2DBWriter
// Class: BeamProfile2DBWriter
//
/**\class BeamProfile2DB BeamProfile2DB.cc IOMC/BeamProfile2DB/src/BeamProfile2DB.cc
/**\class BeamProfile2DBWriter BeamProfile2DBWriter.cc CondTools/BeamSpot/plugins/BeamProfile2DBWriter.cc
Description: [one line class summary]
Expand All @@ -14,6 +14,7 @@
// Original Author: Jean-Roch Vlimant,40 3-A28,+41227671209,
// Created: Fri Jan 6 14:49:42 CET 2012
//
// Updated; Francesco Brivio, June 11, 2023
//

// system include files
Expand All @@ -39,11 +40,10 @@
//
// class declaration
//

class BeamProfile2DB : public edm::global::EDAnalyzer<> {
class BeamProfile2DBWriter : public edm::global::EDAnalyzer<> {
public:
explicit BeamProfile2DB(const edm::ParameterSet&);
~BeamProfile2DB() override;
explicit BeamProfile2DBWriter(const edm::ParameterSet&);
~BeamProfile2DBWriter() override;

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

Expand All @@ -58,7 +58,7 @@ class BeamProfile2DB : public edm::global::EDAnalyzer<> {
//
// constructors and destructor
//
BeamProfile2DB::BeamProfile2DB(const edm::ParameterSet& iConfig) {
BeamProfile2DBWriter::BeamProfile2DBWriter(const edm::ParameterSet& iConfig) {
beamSpot_.setX(iConfig.getParameter<double>("X0"));
beamSpot_.setY(iConfig.getParameter<double>("Y0"));
beamSpot_.setZ(iConfig.getParameter<double>("Z0"));
Expand All @@ -70,23 +70,23 @@ BeamProfile2DB::BeamProfile2DB(const edm::ParameterSet& iConfig) {
beamSpot_.setTimeOffset(iConfig.getParameter<double>("TimeOffset"));
}

BeamProfile2DB::~BeamProfile2DB() = default;
BeamProfile2DBWriter::~BeamProfile2DBWriter() = default;

//
// member functions
//

// ------------ method called for each event ------------
void BeamProfile2DB::analyze(edm::StreamID, const edm::Event& iEvent, const edm::EventSetup& iSetup) const {}
void BeamProfile2DBWriter::analyze(edm::StreamID, const edm::Event& iEvent, const edm::EventSetup& iSetup) const {}

// ------------ method called once each job just after ending the event loop ------------
void BeamProfile2DB::endJob() {
void BeamProfile2DBWriter::endJob() {
edm::Service<cond::service::PoolDBOutputService> poolDbService;
poolDbService->createOneIOV<SimBeamSpotObjects>(beamSpot_, poolDbService->beginOfTime(), "SimBeamSpotObjectsRcd");
}

// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
void BeamProfile2DB::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
void BeamProfile2DBWriter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
//The following says we do not know what parameters are allowed so do no validation
// Please change this to state exactly what you do use, even if it is no parameters
edm::ParameterSetDescription desc;
Expand All @@ -103,4 +103,4 @@ void BeamProfile2DB::fillDescriptions(edm::ConfigurationDescriptions& descriptio
}

//define this as a plug-in
DEFINE_FWK_MODULE(BeamProfile2DB);
DEFINE_FWK_MODULE(BeamProfile2DBWriter);
5 changes: 5 additions & 0 deletions CondTools/BeamSpot/python/BeamProfile2DBRead_cfi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import FWCore.ParameterSet.Config as cms

BeamProfile2DBRead = cms.EDAnalyzer("BeamProfile2DBReader",
rawFileName = cms.untracked.string("")
)
68 changes: 68 additions & 0 deletions CondTools/BeamSpot/test/BeamProfile2DBReader_cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import FWCore.ParameterSet.Config as cms

process = cms.Process("READ")

process.load("FWCore.MessageService.MessageLogger_cfi")
#process.MessageLogger.cerr.FwkReport.reportEvery = 1000000 # do not clog output with IO

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(1) ) # large number of events is needed since we probe 5000LS for run (see below)

####################################################################
# Empty source
####################################################################

process.source = cms.Source("EmptySource",
firstRun = cms.untracked.uint32(1),
firstLuminosityBlock = cms.untracked.uint32(1), # probe one LS after the other
numberEventsInLuminosityBlock = cms.untracked.uint32(1), # probe one event per LS
numberEventsInRun = cms.untracked.uint32(1), # a number of events > the number of LS possible in a real run (5000 s ~ 32 h)
)

####################################################################
# Connect to conditions DB
####################################################################

# either from Global Tag
# process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cfi")
# from Configuration.AlCa.GlobalTag import GlobalTag
# process.GlobalTag = GlobalTag(process.GlobalTag,"auto:phase1_2023_realistic")

# ...or specify database connection and tag...
# from CondCore.CondDB.CondDB_cfi import *
# CondDBSimBeamSpot = CondDB.clone(connect = cms.string('frontier://FrontierProd/CMS_CONDITIONS'))
# process.dbInput = cms.ESSource("PoolDBESSource",
# CondDBSimBeamSpot,
# toGet = cms.VPSet(cms.PSet(record = cms.string('SimBeamSpotObjectsRcd'),
# tag = cms.string('your_tag_name') # customize with input tag name
# )
# )
# )

# ...or specify local db file:
from CondCore.CondDB.CondDB_cfi import *
CondDBSimBeamSpot = CondDB.clone(connect = cms.string("sqlite_file:your_db_file.db")) # customize with input db file
process.PoolDBESSource = cms.ESSource("PoolDBESSource",
CondDBSimBeamSpot,
DumpStat=cms.untracked.bool(True),
toGet = cms.VPSet(cms.PSet(
record = cms.string('SimBeamSpotObjectsRcd'),
tag = cms.string('your_tag_name') # customize with input tag name
))
)


####################################################################
# Load and configure analyzer
####################################################################
process.load("CondTools.BeamSpot.BeamProfile2DBRead_cfi")
process.BeamProfile2DBRead.rawFileName = 'reference_SimBeamSpotObjects.txt'

####################################################################
# Output file
####################################################################
process.TFileService = cms.Service("TFileService",
fileName=cms.string("reference_SimBeamSpotObjects.root")
)

# Put module in path:
process.p = cms.Path(process.BeamProfile2DBRead)
25 changes: 9 additions & 16 deletions CondTools/BeamSpot/test/BeamProfile2DBWriter_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,12 @@

process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(1))

process.simbeamspotwriter = cms.EDAnalyzer("BeamProfile2DB",
X0 = cms.double(0.0458532), # in cm # Early 2023
Y0 = cms.double(-0.016966), # in cm
Z0 = cms.double(-0.074992), # in cm
SigmaZ = cms.double(3.6), # in cm
BetaStar = cms.double(30.0), # in cm
Emittance = cms.double(3.931e-8) # in cm
#X0 = cms.double(0.1027975), # in cm # EOY 2022
#Y0 = cms.double(-0.016762), # in cm
#Z0 = cms.double(0.101756), # in cm
#SigmaZ = cms.double(3.4), # in cm
#BetaStar = cms.double(30.0), # in cm
#Emittance = cms.double(4.276e-8) # in cm
)

process.p = cms.Path(process.simbeamspotwriter)
process.load("CondTools.BeamSpot.beamProfile2DBWriter_cfi")
process.beamProfile2DBWriter.X0 = 0.0458532
process.beamProfile2DBWriter.Y0 = -0.016966
process.beamProfile2DBWriter.Z0 = -0.074992
process.beamProfile2DBWriter.SigmaZ = 3.6
process.beamProfile2DBWriter.BetaStar = 30.0
process.beamProfile2DBWriter.Emittance = 3.931e-8

process.p = cms.Path(process.beamProfile2DBWriter)

0 comments on commit 03af6ab

Please sign in to comment.