CMS 3D CMS Logo

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