Skip to content

Commit

Permalink
improve caching of MF geometry across runs
Browse files Browse the repository at this point in the history
  • Loading branch information
namapane committed Jul 14, 2022
1 parent 2c96f6e commit 9340531
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
39 changes: 24 additions & 15 deletions MagneticField/Engine/test/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
18000, #default value
VarParsing.VarParsing.multiplicity.singleton,
VarParsing.VarParsing.varType.float,
"Magnet current (nominal values: 18164=3.8T; 16730=3.5T; 14340=3T; 9500=2T; -1=loop in different IOV")
"Magnet current (nominal values: 18164=3.8T; 16730=3.5T; 14340=3T; 9500=2T; -1=loop in different IOVs, to test switching currents in the same job")

options.parseArguments()

Expand All @@ -37,12 +37,15 @@

REFERENCEFILE = 'none'

if options.current < 0 : # Test all currents, simulating different IOVs with different values runInfo
# FIXME: currents are set all equal for the time being because we need to set up a way to specify the correct
# reference file file to be picked for each at runtime.
if options.current < 0 : # Test switching of maps in the same job, simulating different IOVs with different currents in runInfo
# Note that this currently crashes with producerType='fromDB_DD4hep' when a different geometry has to be created in the switch, due a limitation of DD4hep.

# FIXME: Only build the map and print the field at IP - no regression is actually run. To do that we would need to set up a mechanism to specify the
# reference file file to be picked for each current at runtime.
REFERENCEFILE = ''

if options.producerType == 'static_DDD' or options.producerType == 'static_DD4hep' :
sys.exit('Invalid configuration: current=-1 mode is not supported with static prouducers')
process.maxEvents.input = 4
process.source.numberEventsInLuminosityBlock =cms.untracked.uint32(1)

if options.era=='RunI':
Expand All @@ -51,33 +54,39 @@
cms.LuminosityBlockID(20,2),
cms.LuminosityBlockID(30,3),
cms.LuminosityBlockID(40,4),
cms.LuminosityBlockID(50,5),
)

process.riSource = cms.ESSource("EmptyESSource", recordName = cms.string("RunInfoRcd"),
iovIsRunNotTime = cms.bool(True),
firstValid = cms.vuint32(10,20,30,40,50))

process.add_( cms.ESProducer("RunInfoTestESProducer",
runInfos = cms.VPSet(cms.PSet(run = cms.int32(10), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(20), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(30), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(40), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(20), avg_current = cms.double(16000.)),
cms.PSet(run = cms.int32(30), avg_current = cms.double(14000.)),
cms.PSet(run = cms.int32(40), avg_current = cms.double(10000.)),
cms.PSet(run = cms.int32(50), avg_current = cms.double(0.)),
) ) )
else :
process.source.firstLuminosityBlockForEachRun = cms.untracked.VLuminosityBlockID(
cms.LuminosityBlockID(300001,1),
cms.LuminosityBlockID(300002,2),
cms.LuminosityBlockID(300003,3),
cms.LuminosityBlockID(300004,4),
cms.LuminosityBlockID(300005,5),
)
process.add_( cms.ESProducer("RunInfoTestESProducer",
runInfos = cms.VPSet(cms.PSet(run = cms.int32(300001), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(300002), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(300003), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(300004), avg_current = cms.double(18000.)),
cms.PSet(run = cms.int32(300002), avg_current = cms.double(16000.)),
cms.PSet(run = cms.int32(300003), avg_current = cms.double(14000.)),
cms.PSet(run = cms.int32(300004), avg_current = cms.double(10000.)),
cms.PSet(run = cms.int32(300005), avg_current = cms.double(0.)),
) ) )


process.riSource = cms.ESSource("EmptyESSource", recordName = cms.string("RunInfoRcd"),
iovIsRunNotTime = cms.bool(True),
firstValid = cms.vuint32(10,20,30,40))
process.maxEvents.input = len(process.source.firstLuminosityBlockForEachRun)

REFERENCEFILE = 'MagneticField/Engine/data/Regression/referenceField_160812_RII_3_8T.bin' #FIXME cf. comment above.

if options.current > 18765 or (options.current <= 4779 and options.current>0) :
sys.exit('ERROR: invalid current value: ' + str(options.current))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace magneticfield {

edm::ESGetToken<FileBlob, MFGeometryFileRcd> mayConsumeBlobToken_;
cms::DDDetector* detector_{nullptr};
int cachedGeometryVersion_{-1};

const bool debug_;
const bool useMergeFileIfAvailable_;
Expand Down Expand Up @@ -183,19 +184,26 @@ std::unique_ptr<MagneticField> DD4hep_VolumeBasedMagneticFieldESProducerFromDB::
builder.setGridFiles(conf->gridFiles);
}

// Build the geometry from the DB blob
// Build the geometry from the DB blob and cache it
if (cachedGeometryVersion_ != conf->geometryVersion) {
if (nullptr != detector_) {
edm::LogError("MagneticField") << "MF Geometry needs to be re-created since current changed (cached: "
<< cachedGeometryVersion_ << " requested: " << conf->geometryVersion
<< "), which is not supported by dd4hep" << endl;
}

auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();
auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();

string sblob(tb->begin(), tb->end());
sblob.insert(
sblob.rfind("</DDDefinition>"),
"<MaterialSection label=\"materials.xml\"><ElementaryMaterial name=\"materials:Vacuum\" density=\"1e-13*mg/cm3\" "
"symbol=\" \" atomicWeight=\"1*g/mole\" atomicNumber=\"1\"/></MaterialSection>");
string sblob(tb->begin(), tb->end());
sblob.insert(sblob.rfind("</DDDefinition>"),
"<MaterialSection label=\"materials.xml\"><ElementaryMaterial name=\"materials:Vacuum\" "
"density=\"1e-13*mg/cm3\" "
"symbol=\" \" atomicWeight=\"1*g/mole\" atomicNumber=\"1\"/></MaterialSection>");

if (nullptr == detector_)
detector_ = new cms::DDDetector("cmsMagneticField:MAGF", sblob, true);
cachedGeometryVersion_ = conf->geometryVersion;
}

builder.build(detector_);

Expand Down

0 comments on commit 9340531

Please sign in to comment.