CMS 3D CMS Logo

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