Skip to content

Commit

Permalink
Merge pull request cms-sw#29556 from namapane/MF_optimization
Browse files Browse the repository at this point in the history
Magnetic Field optimization
  • Loading branch information
cmsbuild authored May 1, 2020
2 parents f786372 + 8850003 commit ae6f5ca
Show file tree
Hide file tree
Showing 49 changed files with 322 additions and 393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
version = cms.string('grid_160812_3_8t'),
geometryVersion = cms.int32(160812),
debugBuilder = cms.untracked.bool(False),
cacheLastVolume = cms.untracked.bool(True),
scalingVolumes = cms.vint32(),
scalingFactors = cms.vdouble(),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
version = cms.string('grid_1103l_071212_2t'),
geometryVersion = cms.int32(71212),
debugBuilder = cms.untracked.bool(False),
cacheLastVolume = cms.untracked.bool(True),
scalingVolumes = cms.vint32(),
scalingFactors = cms.vdouble(),
gridFiles = cms.VPSet(
Expand Down
4 changes: 0 additions & 4 deletions MagneticField/Engine/test/MakePlots.sh

This file was deleted.

18 changes: 0 additions & 18 deletions MagneticField/Engine/test/README

This file was deleted.

14 changes: 0 additions & 14 deletions MagneticField/Engine/test/plot3d.gnuplot

This file was deleted.

40 changes: 23 additions & 17 deletions MagneticField/Engine/test/queryField.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,30 @@
#process.VolumeBasedMagneticFieldESProducer.valueOverride = 18000


# process.MessageLogger = cms.Service("MessageLogger",
# categories = cms.untracked.vstring("MagneticField"),
# destinations = cms.untracked.vstring("cout"),
# cout = cms.untracked.PSet(
# noLineBreaks = cms.untracked.bool(True),
# threshold = cms.untracked.string("INFO"),
# INFO = cms.untracked.PSet(
# limit = cms.untracked.int32(0)
# ),
# WARNING = cms.untracked.PSet(
# limit = cms.untracked.int32(0)
# ),
# MagneticField = cms.untracked.PSet(
# limit = cms.untracked.int32(10000000)
# )
# )
# )
process.MessageLogger = cms.Service("MessageLogger",
destinations = cms.untracked.vstring('cerr'),
categories = cms.untracked.vstring("MagneticField", # messages on MF configuration, field query
"MagGeoBuilder", # Debug of MF geometry building (debugBuilder flag also needed for full output)
"MagGeometry", # Debug of MF geometry search
"MagGeometry_cache"), # Volume cache debug
cerr = cms.untracked.PSet(
threshold = cms.untracked.string('WARNING'), # DEBUG + set limits below
INFO = cms.untracked.PSet(limit=cms.untracked.int32(0)),
DEBUG = cms.untracked.PSet(limit=cms.untracked.int32(0)),
WARNING = cms.untracked.PSet(limit=cms.untracked.int32(0)),
MagneticField = cms.untracked.PSet(limit=cms.untracked.int32(10000000)),
MagGeoBuilder = cms.untracked.PSet(limit=cms.untracked.int32(0)),
MagGeometry = cms.untracked.PSet(limit=cms.untracked.int32(0)),
MagGeometry_cache = cms.untracked.PSet(limit=cms.untracked.int32(0)),
),

debugModules = cms.untracked.vstring('queryField')
)


process.queryField = cms.EDAnalyzer("queryField")
process.p1 = cms.Path(process.queryField)

### Activate verbose mode of geometry building as well as additional
### consistency checks on geometry
#process.VolumeBasedMagneticFieldESProducer.debugBuilder = True
3 changes: 2 additions & 1 deletion MagneticField/Engine/test/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@
## Size of testing volume (cm):
InnerRadius = cms.untracked.double(0), # default: 0
OuterRadius = cms.untracked.double(900), # default: 900
HalfLength = cms.untracked.double(2400) # default: 2400
minZ = cms.untracked.double(-2400), # default: -2400
maxZ = cms.untracked.double(2400) # default: 2400

)

Expand Down
34 changes: 17 additions & 17 deletions MagneticField/Engine/test/testMagneticField.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* A set of tests for regression and validation of the field map.
*
* outputTable: generate txt file with values to be used for regression. Points are generated
* according to InnerRadius, OuterRadius, HalfLength
* according to innerRadius, outerRadius, minZ, maxZ
*
* inputTable: file with input values to be checked against, format depends on inputFileType:
* xyz = cartesian coordinates in cm (default)
Expand Down Expand Up @@ -53,8 +53,6 @@ using namespace edm;
using namespace Geom;
using namespace std;

// #include "MagneticField/Layers/interface/MagVerbosity.h"

class testMagneticField : public edm::EDAnalyzer {
public:
testMagneticField(const edm::ParameterSet& pset) {
Expand All @@ -68,11 +66,11 @@ class testMagneticField : public edm::EDAnalyzer {
// number of random points to try
numberOfPoints = pset.getUntrackedParameter<int>("numberOfPoints", 10000);
// outer radius of test cylinder
InnerRadius = pset.getUntrackedParameter<double>("InnerRadius", 0.);
// half length of test cylinder
OuterRadius = pset.getUntrackedParameter<double>("OuterRadius", 900);
// half length of test cylinder
HalfLength = pset.getUntrackedParameter<double>("HalfLength", 2400);
innerRadius = pset.getUntrackedParameter<double>("InnerRadius", 0.);
outerRadius = pset.getUntrackedParameter<double>("OuterRadius", 900);
// Z extent of test cylinder
minZ = pset.getUntrackedParameter<double>("minZ", -2400);
maxZ = pset.getUntrackedParameter<double>("maxZ", 2400);
}

~testMagneticField() {}
Expand All @@ -91,6 +89,7 @@ class testMagneticField : public edm::EDAnalyzer {

if (outputFile != "") {
writeValidationTable(numberOfPoints, outputFile);
return;
}

if (inputFileType == "TOSCA") {
Expand Down Expand Up @@ -139,15 +138,17 @@ class testMagneticField : public edm::EDAnalyzer {
string outputFile;
double reso;
int numberOfPoints;
double OuterRadius;
double InnerRadius;
double HalfLength;
double outerRadius;
double innerRadius;
double minZ;
double maxZ;
};

void testMagneticField::writeValidationTable(int npoints, string filename) {
GlobalPointProvider p(InnerRadius, OuterRadius, -Geom::pi(), Geom::pi(), -HalfLength, HalfLength);
GlobalPointProvider p(innerRadius, outerRadius, -Geom::pi(), Geom::pi(), minZ, maxZ);

if (filename.substr(filename.rfind(".")) == ".txt") {
std::string::size_type ps = filename.rfind(".");
if (ps != std::string::npos && filename.substr(ps) == ".txt") {
ofstream file(filename.c_str());

for (int i = 0; i < npoints; ++i) {
Expand Down Expand Up @@ -188,7 +189,8 @@ void testMagneticField::validate(string filename, string type) {
edm::FileInPath mydata(filename);
fullPath = mydata.fullPath();

if (filename.substr(filename.rfind(".")) == ".txt") {
std::string::size_type ps = filename.rfind(".");
if (ps != std::string::npos && filename.substr(filename.rfind(".")) == ".txt") {
binary = false;
file.open(fullPath.c_str());
} else {
Expand Down Expand Up @@ -230,7 +232,7 @@ void testMagneticField::validate(string filename, string type) {
}
}

if (gp.perp() < InnerRadius || gp.perp() > OuterRadius || fabs(gp.z()) > HalfLength)
if (gp.perp() < innerRadius || gp.perp() > outerRadius || gp.z() < minZ || gp.z() > maxZ)
continue;

GlobalVector oldB(bx, by, bz);
Expand Down Expand Up @@ -346,8 +348,6 @@ void testMagneticField::validateVsTOSCATable(string filename) {
return;
}

// if (gp.perp() < InnerRadius || gp.perp() > OuterRadius || fabs(gp.z()) > HalfLength) continue;

GlobalVector oldB(bx, by, bz);
if (vol->inside(gp, 0.03)) {
GlobalVector newB = vol->inTesla(gp);
Expand Down
18 changes: 0 additions & 18 deletions MagneticField/Engine/test/testMagneticField_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,6 @@
process.load("MagneticField.Engine.volumeBasedMagneticField_160812_cfi")


process.MessageLogger = cms.Service("MessageLogger",
categories = cms.untracked.vstring("MagneticField"),
destinations = cms.untracked.vstring("cout"),
cout = cms.untracked.PSet(
noLineBreaks = cms.untracked.bool(True),
threshold = cms.untracked.string("WARNING"),
WARNING = cms.untracked.PSet(
limit = cms.untracked.int32(0)
),
MagneticField = cms.untracked.PSet(
limit = cms.untracked.int32(1)
)
)
)

process.testField = cms.EDAnalyzer("testMagneticField")
process.p1 = cms.Path(process.testField)


### Activate the check of finding volumes at random points.
### This test is useful during developlment of new geometries to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ namespace magneticfield {

std::unique_ptr<MagneticField> produce(const IdealMagneticFieldRecord& iRecord);

private:
// forbid copy ctor and assignment op.
VolumeBasedMagneticFieldESProducer(const VolumeBasedMagneticFieldESProducer&) = delete;
const VolumeBasedMagneticFieldESProducer& operator=(const VolumeBasedMagneticFieldESProducer&) = delete;

private:
const bool debug_;
const bool useParametrizedTrackerField_;
const MagFieldConfig conf_;
Expand All @@ -60,9 +60,7 @@ VolumeBasedMagneticFieldESProducer::VolumeBasedMagneticFieldESProducer(const edm

// ------------ method called to produce the data ------------
std::unique_ptr<MagneticField> VolumeBasedMagneticFieldESProducer::produce(const IdealMagneticFieldRecord& iRecord) {
if (debug_) {
edm::LogPrint("VolumeBasedMagneticFieldESProducer") << "VolumeBasedMagneticFieldESProducer::produce() " << version_;
}
LogTrace("MagGeoBuilder") << "VolumeBasedMagneticFieldESProducer::produce() " << version_;

auto cpv = iRecord.getTransientHandle(cpvToken_);
MagGeoBuilderFromDDD builder(conf_.version, conf_.geometryVersion, debug_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ VolumeBasedMagneticFieldESProducerFromDB::VolumeBasedMagneticFieldESProducerFrom
mayGetConfigToken_,
[](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
auto const label = closerNominalLabel(iHandle->m_avg_current);
edm::LogInfo("MagneticFieldDB") << "Current :" << iHandle->m_avg_current
<< " (from RunInfo DB); using map configuration with label: " << label;
edm::LogInfo("MagneticField") << "Current :" << iHandle->m_avg_current
<< " (from RunInfo DB); using map configuration with label: " << label;
return iGet("", label);
},
edm::ESProductTag<RunInfo, RunInfoRcd>("", ""));

} else {
//we know exactly what we are going to get
auto const label = closerNominalLabel(current);
edm::LogInfo("MagneticFieldDB") << "Current :" << current
<< " (from valueOverride card); using map configuration with label: " << label;
edm::LogInfo("MagneticField") << "Current :" << current
<< " (from valueOverride card); using map configuration with label: " << label;
setWhatProduced(
this, &VolumeBasedMagneticFieldESProducerFromDB::chooseConfigViaParameter, edm::es::Label(myConfigLabel))
.setConsumes(knownFromParamConfigToken_, edm::ESInputTag(""s, std::string(label)));
Expand Down Expand Up @@ -144,8 +144,8 @@ std::unique_ptr<MagneticField> VolumeBasedMagneticFieldESProducerFromDB::produce
std::unique_ptr<MagneticField> paramField =
ParametrizedMagneticFieldFactory::get(conf->slaveFieldVersion, conf->slaveFieldParameters);

edm::LogInfo("MagneticFieldDB") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
<< " slaveFieldVersion: " << conf->slaveFieldVersion;
edm::LogInfo("MagneticField") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
<< " slaveFieldVersion: " << conf->slaveFieldVersion;

if (conf->version == "parametrizedMagneticField") {
// The map consist of only the parametrization in this case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ DD4hep_VolumeBasedMagneticFieldESProducer::DD4hep_VolumeBasedMagneticFieldESProd
useParametrizedTrackerField_{iConfig.getParameter<bool>("useParametrizedTrackerField")},
conf_{iConfig, debug_},
version_{iConfig.getParameter<std::string>("version")} {
// LogVerbatim used because LogTrace messages don't seem to appear even when fully enabled.
edm::LogVerbatim("DD4hep_VolumeBasedMagneticFieldESProducer")
<< "info:Constructing a DD4hep_VolumeBasedMagneticFieldESProducer";
LogTrace("MagGeoBuilder") << "info:Constructing a DD4hep_VolumeBasedMagneticFieldESProducer";

auto cc = setWhatProduced(this, iConfig.getUntrackedParameter<std::string>("label", ""));
cc.setConsumes(cpvToken_, edm::ESInputTag{"", "magfield"});
Expand All @@ -70,8 +68,7 @@ DD4hep_VolumeBasedMagneticFieldESProducer::DD4hep_VolumeBasedMagneticFieldESProd
std::unique_ptr<MagneticField> DD4hep_VolumeBasedMagneticFieldESProducer::produce(
const IdealMagneticFieldRecord& iRecord) {
if (debug_) {
edm::LogVerbatim("DD4hep_VolumeBasedMagneticFieldESProducer")
<< "DD4hep_VolumeBasedMagneticFieldESProducer::produce() " << version_;
LogTrace("MagGeoBuilder") << "DD4hep_VolumeBasedMagneticFieldESProducer::produce() " << version_;
}

MagGeoBuilder builder(conf_.version, conf_.geometryVersion, debug_);
Expand All @@ -90,12 +87,12 @@ std::unique_ptr<MagneticField> DD4hep_VolumeBasedMagneticFieldESProducer::produc
const cms::DDCompactView* cpvPtr = cpv.product();
const cms::DDDetector* det = cpvPtr->detector();
builder.build(det);
edm::LogVerbatim("DD4hep_VolumeBasedMagneticFieldESProducer") << "produce() finished build";
LogTrace("MagGeoBuilder") << "produce() finished build";

// Get slave field (from ES)
const MagneticField* paramField = nullptr;
if (useParametrizedTrackerField_) {
edm::LogVerbatim("DD4hep_VolumeBasedMagneticFieldESProducer") << "Getting MF for parametrized field";
LogTrace("MagGeoBuilder") << "Getting MF for parametrized field";
paramField = &iRecord.get(paramFieldToken_);
}
return std::make_unique<VolumeBasedMagneticField>(conf_.geometryVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ DD4hep_VolumeBasedMagneticFieldESProducerFromDB::DD4hep_VolumeBasedMagneticField
mayGetConfigToken_,
[](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
auto const label = closerNominalLabel(iHandle->m_avg_current);
edm::LogInfo("MagneticFieldDB") << "Current :" << iHandle->m_avg_current
<< " (from RunInfo DB); using map configuration with label: " << label;
edm::LogInfo("MagneticField") << "Current :" << iHandle->m_avg_current
<< " (from RunInfo DB); using map configuration with label: " << label;
return iGet("", label);
},
edm::ESProductTag<RunInfo, RunInfoRcd>("", ""));

} else {
//we know exactly what we are going to get
auto const label = closerNominalLabel(current);
edm::LogInfo("MagneticFieldDB") << "Current :" << current
<< " (from valueOverride card); using map configuration with label: " << label;
edm::LogInfo("MagneticField") << "Current :" << current
<< " (from valueOverride card); using map configuration with label: " << label;
setWhatProduced(
this, &DD4hep_VolumeBasedMagneticFieldESProducerFromDB::chooseConfigViaParameter, edm::es::Label(myConfigLabel))
.setConsumes(knownFromParamConfigToken_, edm::ESInputTag(""s, std::string(label)));
Expand Down Expand Up @@ -148,8 +148,8 @@ std::unique_ptr<MagneticField> DD4hep_VolumeBasedMagneticFieldESProducerFromDB::
std::unique_ptr<MagneticField> paramField =
ParametrizedMagneticFieldFactory::get(conf->slaveFieldVersion, conf->slaveFieldParameters);

edm::LogInfo("MagneticFieldDB") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
<< " slaveFieldVersion: " << conf->slaveFieldVersion;
edm::LogInfo("MagneticField") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
<< " slaveFieldVersion: " << conf->slaveFieldVersion;

if (conf->version == "parametrizedMagneticField") {
// The map consist of only the parametrization in this case
Expand Down
2 changes: 0 additions & 2 deletions MagneticField/GeomBuilder/src/BaseVolumeHandle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "DataFormats/GeometrySurface/interface/Cone.h"
#include "DataFormats/GeometryVector/interface/CoordinateSets.h"

#include "MagneticField/Layers/interface/MagVerbosity.h"

#include <cassert>
#include <string>
#include <iterator>
Expand Down
11 changes: 5 additions & 6 deletions MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "MagneticField/VolumeGeometry/interface/MagVolume6Faces.h"
#include "MagneticField/VolumeGeometry/interface/MagExceptions.h"
#include "MagneticField/Layers/interface/MagVerbosity.h"

#include "DataFormats/Math/interface/deltaPhi.h"

Expand Down Expand Up @@ -392,9 +391,9 @@ void MagGeoBuilder::buildMagVolumes(const handles& volumes, map<string, MagProvi
if (interpolators.find(vol->magFile) != interpolators.end()) {
mp = interpolators[vol->magFile];
} else {
edm::LogError("MagGeoBuilder|buildMagVolumes")
<< "No interpolator found for file " << vol->magFile << " vol: " << vol->volumeno << "\n"
<< interpolators.size();
edm::LogError("MagGeoBuilder") << "No interpolator found for file " << vol->magFile << " vol: " << vol->volumeno
<< "\n"
<< interpolators.size();
}

// Search for [volume,sector] in the list of scaling factors; sector = 0 handled as wildcard
Expand All @@ -410,8 +409,8 @@ void MagGeoBuilder::buildMagVolumes(const handles& volumes, map<string, MagProvi
if (isf != theScalingFactors_.end()) {
sf = (*isf).second;

LogTrace("MagGeoBuilder|buildMagVolumes") << "Applying scaling factor " << sf << " to " << vol->volumeno << "["
<< vol->copyno << "] (key:" << key << ")";
LogTrace("MagGeoBuilder") << "Applying scaling factor " << sf << " to " << vol->volumeno << "[" << vol->copyno
<< "] (key:" << key << ")";
}

const GloballyPositioned<float>* gpos = vol->placement();
Expand Down
Loading

0 comments on commit ae6f5ca

Please sign in to comment.