CMS 3D CMS Logo

DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc
Go to the documentation of this file.
1 
10 
13 
18 
20 
25 
28 
30 
36 
38 
39 #include <string>
40 #include <vector>
41 #include <iostream>
42 #include <memory>
43 
44 using namespace std;
45 using namespace magneticfield;
46 using namespace edm;
47 
48 namespace magneticfield {
50  public:
52  // forbid copy ctor and assignment op.
56 
57  std::shared_ptr<MagFieldConfig const> chooseConfigViaParameter(const IdealMagneticFieldRecord& iRecord);
58  std::shared_ptr<MagFieldConfig const> chooseConfigAtRuntime(const IdealMagneticFieldRecord& iRecord);
59 
60  std::unique_ptr<MagneticField> produce(const IdealMagneticFieldRecord& iRecord);
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64  private:
65  static std::string_view closerNominalLabel(float current);
66 
69 
70  //NOTE: change of record since this MagFieldConfig was chosen based on data
71  // from the record RunInfoRcd so therefore has a dependency upon that record
73 
75  const bool debug_;
76  };
77 } // namespace magneticfield
78 
79 DD4hep_VolumeBasedMagneticFieldESProducerFromDB::DD4hep_VolumeBasedMagneticFieldESProducerFromDB(
80  const edm::ParameterSet& iConfig)
81  : debug_(iConfig.getUntrackedParameter<bool>("debugBuilder")) {
82  std::string const myConfigLabel = "VBMFESChoice";
83 
84  //Based on configuration, pick algorithm to produce the proper MagFieldConfig with a specific label
85  const int current = iConfig.getParameter<int>("valueOverride");
86  if (current < 0) {
87  //We do not know what to get until we first read RunInfo
90  .setMayConsume(
92  [](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
93  auto const label = closerNominalLabel(iHandle->m_avg_current);
94  edm::LogInfo("MagneticField") << "Current :" << iHandle->m_avg_current
95  << " (from RunInfo DB); using map configuration with label: " << label;
96  return iGet("", label);
97  },
99 
100  } else {
101  //we know exactly what we are going to get
102  auto const label = closerNominalLabel(current);
103  edm::LogInfo("MagneticField") << "Current :" << current
104  << " (from valueOverride card); using map configuration with label: " << label;
105  auto cc = setWhatProduced(this,
107  edm::es::Label(myConfigLabel));
108 
110  }
111 
112  auto const label = iConfig.getUntrackedParameter<std::string>("label");
113  auto const myConfigTag = edm::ESInputTag(iConfig.getParameter<std::string>("@module_label"), myConfigLabel);
114 
115  //We use the MagFieldConfig created above to decide which FileBlob to use
116  auto cc = setWhatProduced(this, label);
117  cc.setMayConsume(
119  [](auto const& iGet, edm::ESTransientHandle<MagFieldConfig> iConfig) {
120  if (iConfig->version == "parametrizedMagneticField") {
121  return iGet.nothing();
122  }
123  return iGet("", std::to_string(iConfig->geometryVersion));
124  },
126  chosenConfigToken_ = cc.consumes(myConfigTag); //Use same tag as the choice
127 }
128 
130  IdealMagneticFieldRecord const& iRcd) {
132 
133  //just forward what we just got but do not take ownership
134  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
135 }
136 
138  const IdealMagneticFieldRecord& iRecord) {
140 
141  //just forward what we just got but do not take ownership
142  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
143 }
144 
145 // ------------ method called to produce the data ------------
147  const IdealMagneticFieldRecord& iRecord) {
148  auto const& conf = iRecord.getTransientHandle(chosenConfigToken_);
149 
150  std::unique_ptr<MagneticField> paramField =
151  ParametrizedMagneticFieldFactory::get(conf->slaveFieldVersion, conf->slaveFieldParameters);
152 
153  edm::LogInfo("MagneticField") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
154  << " slaveFieldVersion: " << conf->slaveFieldVersion;
155 
156  if (conf->version == "parametrizedMagneticField") {
157  // The map consist of only the parametrization in this case
158  return paramField;
159  }
160 
161  // Full VolumeBased map + parametrization
162  MagGeoBuilder builder(conf->version, conf->geometryVersion, debug_);
163 
164  // Set scaling factors
165  if (!conf->keys.empty()) {
166  builder.setScaling(conf->keys, conf->values);
167  }
168 
169  // Set specification for the grid tables to be used.
170  if (!conf->gridFiles.empty()) {
171  builder.setGridFiles(conf->gridFiles);
172  }
173 
174  // Build the geometry from the DB blob
175 
176  auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
177  std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();
178 
179  string sblob(tb->begin(), tb->end());
180  sblob.insert(
181  sblob.rfind("</DDDefinition>"),
182  "<MaterialSection label=\"materials.xml\"><ElementaryMaterial name=\"materials:Vacuum\" density=\"1e-13*mg/cm3\" "
183  "symbol=\" \" atomicWeight=\"1*g/mole\" atomicNumber=\"1\"/></MaterialSection>");
184 
185  auto ddet = make_unique<cms::DDDetector>("", sblob, true);
186 
187  builder.build(ddet.get());
188 
189  // Build the VB map. Ownership of the parametrization is transferred to it
190  return std::make_unique<VolumeBasedMagneticField>(conf->geometryVersion,
191  builder.barrelLayers(),
192  builder.endcapSectors(),
193  builder.barrelVolumes(),
194  builder.endcapVolumes(),
195  builder.maxR(),
196  builder.maxZ(),
197  paramField.release(),
198  true);
199 }
200 
202  constexpr std::array<int, 7> nominalCurrents = {{-1, 0, 9558, 14416, 16819, 18268, 19262}};
203  constexpr std::array<std::string_view, 7> nominalLabels = {{"3.8T", "0T", "2T", "3T", "3.5T", "3.8T", "4T"}};
204 
205  int i = 0;
206  for (; i < (int)nominalLabels.size() - 1; i++) {
207  if (2 * current < nominalCurrents[i] + nominalCurrents[i + 1])
208  return nominalLabels[i];
209  }
210  return nominalLabels[i];
211 }
212 
215  desc.addUntracked<bool>("debugBuilder", false);
216  desc.add<int>("valueOverride", -1)->setComment("Force value of current (in A); take the value from DB if < 0.");
217  desc.addUntracked<std::string>("label", "");
218 
219  descriptions.addDefault(desc);
220 }
221 
magneticfield::MagGeoBuilder::build
void build(const cms::DDDetector *det)
Definition: DD4hep_MagGeoBuilder.cc:104
electrons_cff.bool
bool
Definition: electrons_cff.py:393
mps_fire.i
i
Definition: mps_fire.py:428
edm::ESProductTag
Definition: ESProductTag.h:29
edm::ESInputTag
Definition: ESInputTag.h:87
ESTransientHandle.h
MessageLogger.h
RunInfoPI::nominalCurrents
constexpr std::array< int, 7 > nominalCurrents
Definition: RunInfoPayloadInspectoHelper.h:19
RunSummaryRcd.h
ParametrizedMagneticFieldFactory::get
static std::unique_ptr< MagneticField > get(std::string version, const edm::ParameterSet &parameters)
MFGeometryFileRcd.h
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::chosenConfigToken_
edm::ESGetToken< MagFieldConfig, IdealMagneticFieldRecord > chosenConfigToken_
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:72
edm::eventsetup::DependentRecordImplementation::getHandle
ESHandle< ProductT > getHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
Definition: DependentRecordImplementation.h:81
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ESProducer::setWhatProduced
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
magneticfield
Definition: MagFieldConfig.h:22
MagFieldConfig::version
std::string version
Version of the data tables to be used.
Definition: MagFieldConfig.h:41
ESProducer.h
magneticfield::MagGeoBuilder
Definition: DD4hep_MagGeoBuilder.h:35
MagFieldConfig::geometryVersion
int geometryVersion
Version of the geometry to be used.
Definition: MagFieldConfig.h:38
DD4hep_MagGeoBuilder.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
magneticfield::MagGeoBuilder::endcapSectors
std::vector< MagESector * > endcapSectors() const
Get endcap layers.
Definition: DD4hep_MagGeoBuilder.cc:557
DDDetector.h
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::chooseConfigAtRuntime
std::shared_ptr< MagFieldConfig const > chooseConfigAtRuntime(const IdealMagneticFieldRecord &iRecord)
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:129
FileBlob.h
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::closerNominalLabel
static std::string_view closerNominalLabel(float current)
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:201
cc
IdealMagneticFieldRecord
Definition: IdealMagneticFieldRecord.h:11
config
Definition: config.py:1
magneticfield::MagGeoBuilder::barrelLayers
std::vector< MagBLayer * > barrelLayers() const
Get barrel layers.
Definition: DD4hep_MagGeoBuilder.cc:555
alignCSCRings.s
s
Definition: alignCSCRings.py:92
fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::eventsetup::DependentRecordImplementation::getTransientHandle
ESTransientHandle< ProductT > getTransientHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
Definition: DependentRecordImplementation.h:92
VolumeBasedMagneticField.h
IdealMagneticFieldRecord.h
edm::ESHandle
Definition: DTSurvey.h:22
edm::es::Label
Definition: es_Label.h:56
magneticfield::MagGeoBuilder::maxZ
float maxZ() const
Definition: DD4hep_MagGeoBuilder.cc:582
EventSetupRecordIntervalFinder.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:49
edm::ParameterSet
Definition: ParameterSet.h:47
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::debug_
const bool debug_
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:75
magneticfield::MagGeoBuilder::maxR
float maxR() const
Definition: DD4hep_MagGeoBuilder.cc:577
createfilelist.int
int
Definition: createfilelist.py:10
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::chooseConfigViaParameter
std::shared_ptr< MagFieldConfig const > chooseConfigViaParameter(const IdealMagneticFieldRecord &iRecord)
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:137
MagFieldConfig.h
MagneticField.h
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::mayGetConfigToken_
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd > mayGetConfigToken_
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:67
magneticfield::MagGeoBuilder::barrelVolumes
std::vector< MagVolume6Faces * > barrelVolumes() const
Definition: DD4hep_MagGeoBuilder.cc:559
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd >
edm::ESTransientHandle
Definition: ESTransientHandle.h:41
RunInfo.h
ModuleFactory.h
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::mayConsumeBlobToken_
edm::ESGetToken< FileBlob, MFGeometryFileRcd > mayConsumeBlobToken_
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:74
ESProductTag.h
magneticfield::MagGeoBuilder::endcapVolumes
std::vector< MagVolume6Faces * > endcapVolumes() const
Definition: DD4hep_MagGeoBuilder.cc:568
MagFieldConfigRcd.h
DEFINE_FWK_EVENTSETUP_MODULE
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::ESProducer
Definition: ESProducer.h:104
ParameterSet.h
magneticfield::MagGeoBuilder::setScaling
void setScaling(const std::vector< int > &keys, const std::vector< double > &values)
Definition: DD4hep_MagGeoBuilder.cc:592
RunInfo::m_avg_current
float m_avg_current
Definition: RunInfo.h:28
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
magneticfield::MagGeoBuilder::setGridFiles
void setGridFiles(const TableFileMap &gridFiles)
Definition: DD4hep_MagGeoBuilder.cc:602
label
const char * label
Definition: PFTauDecayModeTools.cc:11
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::produce
std::unique_ptr< MagneticField > produce(const IdealMagneticFieldRecord &iRecord)
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:146
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::knownFromParamConfigToken_
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd > knownFromParamConfigToken_
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:68
ParametrizedMagneticFieldFactory.h
magneticfield::DD4hep_VolumeBasedMagneticFieldESProducerFromDB::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: DD4hep_VolumeBasedMagneticFieldESProducerFromDB.cc:213