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  int cachedGeometryVersion_{-1};
79 
80  const bool debug_;
82  };
83 } // namespace magneticfield
84 
85 DD4hep_VolumeBasedMagneticFieldESProducerFromDB::DD4hep_VolumeBasedMagneticFieldESProducerFromDB(
86  const edm::ParameterSet& iConfig)
87  : debug_(iConfig.getUntrackedParameter<bool>("debugBuilder")),
88  useMergeFileIfAvailable_(iConfig.getParameter<bool>("useMergeFileIfAvailable")) {
89  std::string const myConfigLabel = "VBMFESChoice";
91 
92  //Based on configuration, pick algorithm to produce the proper MagFieldConfig with a specific label
93  const int current = iConfig.getParameter<int>("valueOverride");
94  if (current < 0) {
95  //We do not know what to get until we first read RunInfo
98  .setMayConsume(
100  [](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
101  auto const label = closerNominalLabel(iHandle->m_avg_current);
102  edm::LogInfo("MagneticField") << "Current :" << iHandle->m_avg_current
103  << " (from RunInfo DB); using map configuration with label: " << label;
104  return iGet("", label);
105  },
107 
108  } else {
109  //we know exactly what we are going to get
110  auto const label = closerNominalLabel(current);
111  edm::LogInfo("MagneticField") << "Current :" << current
112  << " (from valueOverride card); using map configuration with label: " << label;
113  auto cc = setWhatProduced(this,
115  edm::es::Label(myConfigLabel));
116 
118  }
119 
120  auto const label = iConfig.getUntrackedParameter<std::string>("label");
121  auto const myConfigTag = edm::ESInputTag(iConfig.getParameter<std::string>("@module_label"), myConfigLabel);
122 
123  //We use the MagFieldConfig created above to decide which FileBlob to use
124  auto cc = setWhatProduced(this, label);
125  cc.setMayConsume(
127  [](auto const& iGet, edm::ESTransientHandle<MagFieldConfig> iConfig) {
128  if (iConfig->version == "parametrizedMagneticField") {
129  return iGet.nothing();
130  }
131  return iGet("", std::to_string(iConfig->geometryVersion));
132  },
134  chosenConfigToken_ = cc.consumes(myConfigTag); //Use same tag as the choice
135 }
136 
138  delete detector_;
139 }
140 
142  IdealMagneticFieldRecord const& iRcd) {
144 
145  //just forward what we just got but do not take ownership
146  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
147 }
148 
150  const IdealMagneticFieldRecord& iRecord) {
152 
153  //just forward what we just got but do not take ownership
154  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
155 }
156 
157 // ------------ method called to produce the data ------------
159  const IdealMagneticFieldRecord& iRecord) {
160  auto const& conf = iRecord.getTransientHandle(chosenConfigToken_);
161 
162  std::unique_ptr<MagneticField> paramField =
163  ParametrizedMagneticFieldFactory::get(conf->slaveFieldVersion, conf->slaveFieldParameters);
164 
165  edm::LogInfo("MagneticField") << "(DD4hep) Version: " << conf->version
166  << " geometryVersion: " << conf->geometryVersion
167  << " slaveFieldVersion: " << conf->slaveFieldVersion;
168 
169  if (conf->version == "parametrizedMagneticField") {
170  // The map consist of only the parametrization in this case
171  return paramField;
172  }
173 
174  // Full VolumeBased map + parametrization
175  MagGeoBuilder builder(conf->version, conf->geometryVersion, debug_, useMergeFileIfAvailable_);
176 
177  // Set scaling factors
178  if (!conf->keys.empty()) {
179  builder.setScaling(conf->keys, conf->values);
180  }
181 
182  // Set specification for the grid tables to be used.
183  if (!conf->gridFiles.empty()) {
184  builder.setGridFiles(conf->gridFiles);
185  }
186 
187  // Build the geometry from the DB blob and cache it
188  if (cachedGeometryVersion_ != conf->geometryVersion) {
189  if (nullptr != detector_) {
190  edm::LogError("MagneticField") << "MF Geometry needs to be re-created since current changed (cached: "
191  << cachedGeometryVersion_ << " requested: " << conf->geometryVersion
192  << "), which is not supported by dd4hep" << endl;
193  }
194 
195  auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
196  std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();
197 
198  string sblob(tb->begin(), tb->end());
199  sblob.insert(sblob.rfind("</DDDefinition>"),
200  "<MaterialSection label=\"materials.xml\"><ElementaryMaterial name=\"materials:Vacuum\" "
201  "density=\"1e-13*mg/cm3\" "
202  "symbol=\" \" atomicWeight=\"1*g/mole\" atomicNumber=\"1\"/></MaterialSection>");
203 
204  detector_ = new cms::DDDetector("cmsMagneticField:MAGF", sblob, true);
205  cachedGeometryVersion_ = conf->geometryVersion;
206  }
207 
208  builder.build(detector_);
209 
210  // Build the VB map. Ownership of the parametrization is transferred to it
211  return std::make_unique<VolumeBasedMagneticField>(conf->geometryVersion,
212  builder.barrelLayers(),
213  builder.endcapSectors(),
214  builder.barrelVolumes(),
215  builder.endcapVolumes(),
216  builder.maxR(),
217  builder.maxZ(),
218  paramField.release(),
219  true);
220 }
221 
223  constexpr std::array<int, 7> nominalCurrents = {{-1, 0, 9558, 14416, 16819, 18268, 19262}};
224  constexpr std::array<std::string_view, 7> nominalLabels = {{"3.8T", "0T", "2T", "3T", "3.5T", "3.8T", "4T"}};
225 
226  int i = 0;
227  for (; i < (int)nominalLabels.size() - 1; i++) {
228  if (2 * current < nominalCurrents[i] + nominalCurrents[i + 1])
229  return nominalLabels[i];
230  }
231  return nominalLabels[i];
232 }
233 
236  desc.addUntracked<bool>("debugBuilder", false);
237  desc.add<bool>("useMergeFileIfAvailable", true);
238  desc.add<int>("valueOverride", -1)->setComment("Force value of current (in A); take the value from DB if < 0.");
239  desc.addUntracked<std::string>("label", "");
240 
241  descriptions.addDefault(desc);
242 }
243 
ESTransientHandle< ProductT > getTransientHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:166
edm::ESGetToken< MagFieldConfig, IdealMagneticFieldRecord > chosenConfigToken_
static const std::string kDD4hep
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
std::vector< MagBLayer * > barrelLayers() const
Get barrel layers.
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
std::unique_ptr< MagneticField > produce(const IdealMagneticFieldRecord &iRecord)
Definition: config.py:1
Log< level::Error, false > LogError
std::shared_ptr< MagFieldConfig const > chooseConfigAtRuntime(const IdealMagneticFieldRecord &iRecord)
static std::string to_string(const XMLCh *ch)
T getUntrackedParameter(std::string const &, T const &) const
void usesResources(std::vector< std::string > const &)
Definition: ESProducer.cc:86
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
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:61
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.
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