Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DT vdrift calibration code to allow using "new" DB format #31808

Merged
merged 7 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CalibMuon/DTCalibration/plugins/DTVDriftMeanTimer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "CondFormats/DTObjects/interface/DTMtime.h"
#include "DataFormats/MuonDetId/interface/DTWireId.h"

#include "CalibMuon/DTCalibration/interface/DTMeanTimerFitter.h"
#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
Expand Down
39 changes: 30 additions & 9 deletions CalibMuon/DTCalibration/plugins/DTVDriftSegment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "CondFormats/DTObjects/interface/DTMtime.h"
#include "CondFormats/DataRecord/interface/DTMtimeRcd.h"
#include "CondFormats/DTObjects/interface/DTRecoConditions.h"
#include "CondFormats/DataRecord/interface/DTRecoConditionsVdriftRcd.h"

#include "CalibMuon/DTCalibration/interface/DTResidualFitter.h"
#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
Expand All @@ -32,14 +34,17 @@ using namespace edm;
namespace dtCalibration {

DTVDriftSegment::DTVDriftSegment(const ParameterSet& pset)
: nSigmas_(pset.getUntrackedParameter<unsigned int>("nSigmasFitRange", 1)) {
: nSigmas_(pset.getUntrackedParameter<unsigned int>("nSigmasFitRange", 1)),
mTimeMap_(nullptr),
vDriftMap_(nullptr) {
string rootFileName = pset.getParameter<string>("rootFileName");
rootFile_ = new TFile(rootFileName.c_str(), "READ");

bool debug = pset.getUntrackedParameter<bool>("debug", false);
fitter_ = new DTResidualFitter(debug);
//bool debug = pset.getUntrackedParameter<bool>("debug", false);
//if(debug) fitter_->setVerbosity(1);

readLegacyVDriftDB = pset.getParameter<bool>("readLegacyVDriftDB");
}

DTVDriftSegment::~DTVDriftSegment() {
Expand All @@ -49,18 +54,34 @@ namespace dtCalibration {

void DTVDriftSegment::setES(const edm::EventSetup& setup) {
// Get the map of vdrift from the setup
ESHandle<DTMtime> mTime;
setup.get<DTMtimeRcd>().get(mTime);
mTimeMap_ = &*mTime;
if (readLegacyVDriftDB) {
ESHandle<DTMtime> mTime;
setup.get<DTMtimeRcd>().get(mTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but this migration has to be done on these classes anyhow, it is not needed as a consequence of this PR.
Therefore it would make more sense to make it in its own PR, after this one is merged.
I can make a new PR for the migration right away after this one is merged; this is my preference given that this PR is by now tested and fully signed.

mTimeMap_ = &*mTime;
} else {
ESHandle<DTRecoConditions> hVdrift;
setup.get<DTRecoConditionsVdriftRcd>().get(hVdrift);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

vDriftMap_ = &*hVdrift;
// Consistency check: no parametrization is implemented for the time being
int version = vDriftMap_->version();
if (version != 1) {
throw cms::Exception("Configuration") << "only version 1 is presently supported for VDriftDB";
}
}
}

DTVDriftData DTVDriftSegment::compute(DTSuperLayerId const& slId) {
// Get original value from DB; vdrift is cm/ns , resolution is cm
float vDrift = 0., resolution = 0.;
int status = mTimeMap_->get(slId, vDrift, resolution, DTVelocityUnits::cm_per_ns);
// Note that resolution is irrelevant as it is no longer used anywhere in reconstruction.

if (status != 0)
throw cms::Exception("DTCalibration") << "Could not find vDrift entry in DB for" << slId << endl;
float vDrift = 0., resolution = 0.;
if (readLegacyVDriftDB) { // Legacy format
int status = mTimeMap_->get(slId, vDrift, resolution, DTVelocityUnits::cm_per_ns);
if (status != 0)
throw cms::Exception("DTCalibration") << "Could not find vDrift entry in DB for" << slId << endl;
} else { // New DB format
vDrift = vDriftMap_->get(DTWireId(slId.rawId()));
}

// For RZ superlayers use original value
if (slId.superLayer() == 2) {
Expand Down
5 changes: 4 additions & 1 deletion CalibMuon/DTCalibration/plugins/DTVDriftSegment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string>

class DTMtime;
class DTRecoConditions;
class DTResidualFitter;
class TH1F;
class TFile;
Expand All @@ -34,7 +35,9 @@ namespace dtCalibration {

unsigned int nSigmas_;

const DTMtime* mTimeMap_;
const DTMtime* mTimeMap_; // legacy DB object
const DTRecoConditions* vDriftMap_; // DB object in new format
bool readLegacyVDriftDB; // which one to use
TFile* rootFile_;
DTResidualFitter* fitter_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "Geometry/DTGeometry/interface/DTGeometry.h"
#include "Geometry/Records/interface/MuonGeometryRecord.h"
#include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
#include "CondFormats/DTObjects/interface/DTMtime.h"

#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
#include "CalibMuon/DTCalibration/interface/DTSegmentSelector.h"
Expand Down
86 changes: 65 additions & 21 deletions CalibMuon/DTCalibration/plugins/DTVDriftWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "CondFormats/DTObjects/interface/DTMtime.h"
#include "CondFormats/DataRecord/interface/DTMtimeRcd.h"
#include "CondFormats/DTObjects/interface/DTRecoConditions.h"
#include "CondFormats/DataRecord/interface/DTRecoConditionsVdriftRcd.h"

#include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
#include "CalibMuon/DTCalibration/interface/DTVDriftPluginFactory.h"
Expand All @@ -32,22 +34,38 @@ using namespace edm;

DTVDriftWriter::DTVDriftWriter(const ParameterSet& pset)
: granularity_(pset.getUntrackedParameter<string>("calibGranularity", "bySL")),
mTimeMap_(nullptr),
vDriftMap_(nullptr),
vDriftAlgo_{DTVDriftPluginFactory::get()->create(pset.getParameter<string>("vDriftAlgo"),
pset.getParameter<ParameterSet>("vDriftAlgoConfig"))} {
LogVerbatim("Calibration") << "[DTVDriftWriter]Constructor called!";

if (granularity_ != "bySL")
throw cms::Exception("Configuration")
<< "[DTVDriftWriter] Check parameter calibGranularity: " << granularity_ << " option not available.";

readLegacyVDriftDB = pset.getParameter<bool>("readLegacyVDriftDB");
writeLegacyVDriftDB = pset.getParameter<bool>("writeLegacyVDriftDB");
}

DTVDriftWriter::~DTVDriftWriter() { LogVerbatim("Calibration") << "[DTVDriftWriter]Destructor called!"; }

void DTVDriftWriter::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
// Get the map of ttrig from the Setup
ESHandle<DTMtime> mTime;
setup.get<DTMtimeRcd>().get(mTime);
mTimeMap_ = &*mTime;
// Get the map of vdrift from the Setup
if (readLegacyVDriftDB) {
ESHandle<DTMtime> mTime;
setup.get<DTMtimeRcd>().get(mTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

mTimeMap_ = &*mTime;
} else {
ESHandle<DTRecoConditions> hVdrift;
setup.get<DTRecoConditionsVdriftRcd>().get(hVdrift);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

vDriftMap_ = &*hVdrift;
// Consistency check: no parametrization is implemented for the time being
int version = vDriftMap_->version();
if (version != 1) {
throw cms::Exception("Configuration") << "only version 1 is presently supported for VDriftDB";
}
}

// Get geometry from Event Setup
setup.get<MuonGeometryRecord>().get(dtGeom_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

Copy link
Contributor Author

@namapane namapane Oct 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, the same migration would have to be done everywhere in the package; for example. also in
https://github.com/cms-sw/cmssw/blob/master/CalibMuon/DTCalibration/plugins/DTTTrigCorrection.cc#L49
an generally in each and every cc under plugins, not just in those interested by this PR.
IMO this is another reason why that migration deserves its own, separate PR.
@silviodonato can you please let me know if you agree.

Expand All @@ -57,7 +75,16 @@ void DTVDriftWriter::beginRun(const edm::Run& run, const edm::EventSetup& setup)

void DTVDriftWriter::endJob() {
// Create the object to be written to DB
DTMtime* mTimeNewMap = new DTMtime();
DTMtime* mTimeNewMap = nullptr;
DTRecoConditions* vDriftNewMap = nullptr;
if (writeLegacyVDriftDB) {
mTimeNewMap = new DTMtime();
} else {
vDriftNewMap = new DTRecoConditions();
vDriftNewMap->setFormulaExpr("[0]");
//vDriftNewMap->setFormulaExpr("[0]*(1-[1]*x)"); // add parametrization for dependency along Y
vDriftNewMap->setVersion(1);
}

if (granularity_ == "bySL") {
// Get all the sls from the geometry
Expand All @@ -66,34 +93,51 @@ void DTVDriftWriter::endJob() {
auto sl_end = superLayers.end();
for (; sl != sl_end; ++sl) {
DTSuperLayerId slId = (*sl)->id();
// Get original value from DB
float vDrift = 0., resolution = 0.;
// vdrift is cm/ns , resolution is cm
int status = mTimeMap_->get(slId, vDrift, resolution, DTVelocityUnits::cm_per_ns);

// Compute vDrift
float vDriftNew = -1.;
float resolutionNew = -1;
try {
dtCalibration::DTVDriftData vDriftData = vDriftAlgo_->compute(slId);
float vDriftNew = vDriftData.vdrift;
float resolutionNew = vDriftData.resolution;
// vdrift is cm/ns , resolution is cm
mTimeNewMap->set(slId, vDriftNew, resolutionNew, DTVelocityUnits::cm_per_ns);
vDriftNew = vDriftData.vdrift;
resolutionNew = vDriftData.resolution;
LogVerbatim("Calibration") << "vDrift for: " << slId << " Mean " << vDriftNew << " Resolution "
<< resolutionNew;
} catch (cms::Exception& e) {
} catch (cms::Exception& e) { // Failure to compute new value, fall back to old table
LogError("Calibration") << e.explainSelf();
// Go back to original value in case of error
if (!status) {
mTimeNewMap->set(slId, vDrift, resolution, DTVelocityUnits::cm_per_ns);
LogVerbatim("Calibration") << "Keep original vDrift for: " << slId << " Mean " << vDrift << " Resolution "
<< resolution;
if (readLegacyVDriftDB) { //...reading old db format...
int status = mTimeMap_->get(slId, vDriftNew, resolutionNew, DTVelocityUnits::cm_per_ns);
if (status == 0) { // not found; silently skip this SL
continue;
}
} else { //...reading new db format
try {
vDriftNew = vDriftMap_->get(DTWireId(slId.rawId()));
} catch (cms::Exception& e2) {
// not found; silently skip this SL
continue;
}
}
LogVerbatim("Calibration") << "Keep original vDrift for: " << slId << " Mean " << vDriftNew << " Resolution "
<< resolutionNew;
}

// Add value to the vdrift table
if (writeLegacyVDriftDB) {
mTimeNewMap->set(slId, vDriftNew, resolutionNew, DTVelocityUnits::cm_per_ns);
} else {
vector<double> params = {vDriftNew};
vDriftNewMap->set(DTWireId(slId.rawId()), params);
}
} // End of loop on superlayers
}

// Write the vDrift object to DB
LogVerbatim("Calibration") << "[DTVDriftWriter]Writing vdrift object to DB!";
string record = "DTMtimeRcd";
DTCalibDBUtils::writeToDB<DTMtime>(record, mTimeNewMap);
if (writeLegacyVDriftDB) {
string record = "DTMtimeRcd";
DTCalibDBUtils::writeToDB<DTMtime>(record, mTimeNewMap);
} else {
DTCalibDBUtils::writeToDB<DTRecoConditions>("DTRecoConditionsVdriftRcd", vDriftNewMap);
}
}
7 changes: 6 additions & 1 deletion CalibMuon/DTCalibration/plugins/DTVDriftWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string>

class DTMtime;
class DTRecoConditions;
class DTGeometry;
namespace dtCalibration {
class DTVDriftBaseAlgo;
Expand All @@ -34,7 +35,11 @@ class DTVDriftWriter : public edm::EDAnalyzer {
private:
std::string granularity_; // enforced by SL

const DTMtime* mTimeMap_;
const DTMtime* mTimeMap_; // legacy DB object
const DTRecoConditions* vDriftMap_; // DB object in new format
bool readLegacyVDriftDB; // which format to use to read old values
bool writeLegacyVDriftDB; // which format to be created

edm::ESHandle<DTGeometry> dtGeom_;

std::unique_ptr<dtCalibration::DTVDriftBaseAlgo> vDriftAlgo_;
Expand Down
18 changes: 15 additions & 3 deletions CalibMuon/DTCalibration/python/dtVDriftSegmentWriter_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

process = cms.Process("DTVDriftWriter")

### Set to true to switch to writing constants in the new DB format.
NEWDBFORMAT = False
###

process.load("CalibMuon.DTCalibration.messageLoggerDebug_cff")
process.MessageLogger.debugModules = cms.untracked.vstring('dtVDriftSegmentWriter')

Expand All @@ -12,6 +16,8 @@

process.load("CondCore.CondDB.CondDB_cfi")

process.load("CalibMuon.DTCalibration.dtVDriftSegmentWriter_cfi")

process.source = cms.Source("EmptySource",
numberEventsInRun = cms.untracked.uint32(1),
firstRun = cms.untracked.uint32(1)
Expand All @@ -21,16 +27,22 @@
input = cms.untracked.int32(1)
)

RECORD = 'DTMtimeRcd'
if NEWDBFORMAT :
RECORD = 'DTRecoConditionsVdriftRcd'
process.dtVDriftSegmentWriter.writeLegacyVDriftDB = False
# The following needs to be set as well if calibration should start use
# constants written in the new format as a starting point.
# process.dtVDriftSegmentWriter.vDriftAlgoConfig.readLegacyVDriftDB = False

process.PoolDBOutputService = cms.Service("PoolDBOutputService",
process.CondDB,
timetype = cms.untracked.string('runnumber'),
toPut = cms.VPSet(cms.PSet(
record = cms.string('DTMtimeRcd'),
record = cms.string(RECORD),
tag = cms.string('vDrift')
))
)
process.PoolDBOutputService.connect = cms.string('sqlite_file:vDrift.db')

process.load("CalibMuon.DTCalibration.dtVDriftSegmentWriter_cfi")

process.p = cms.Path(process.dtVDriftSegmentWriter)
3 changes: 3 additions & 0 deletions CalibMuon/DTCalibration/python/dtVDriftSegmentWriter_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

dtVDriftSegmentWriter = cms.EDAnalyzer("DTVDriftWriter",
vDriftAlgo = cms.string('DTVDriftSegment'),
readLegacyVDriftDB =cms.bool(True),
writeLegacyVDriftDB =cms.bool(True),
vDriftAlgoConfig = cms.PSet(
rootFileName = cms.string(''),
nSigmasFitRange = cms.untracked.uint32(1),
readLegacyVDriftDB =cms.bool(True),
debug = cms.untracked.bool(False)
)
)
Loading