CMS 3D CMS Logo

VolumeBasedMagneticFieldESProducerFromDB.cc
Go to the documentation of this file.
1 
9 
12 
17 
19 
24 
28 
37 
38 #include <string>
39 #include <vector>
40 #include <iostream>
41 #include <memory>
42 
43 #include <boost/algorithm/string/split.hpp>
44 #include <boost/algorithm/string/classification.hpp>
45 #include <boost/lexical_cast.hpp>
46 
47 using namespace std;
48 using namespace magneticfield;
49 using namespace edm;
50 
51 namespace magneticfield {
53  public:
55  // 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  const bool debug_;
78  };
79 } // namespace magneticfield
80 
81 VolumeBasedMagneticFieldESProducerFromDB::VolumeBasedMagneticFieldESProducerFromDB(const edm::ParameterSet& iConfig)
82  : debug_(iConfig.getUntrackedParameter<bool>("debugBuilder")) {
83  std::string const myConfigLabel = "VBMFESChoice";
84 
85  //Based on configuration, pick algorithm to produce the proper MagFieldConfig with a specific label
86  const int current = iConfig.getParameter<int>("valueOverride");
87  if (current < 0) {
88  //We do not know what to get until we first read RunInfo
91  .setMayConsume(
93  [](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
94  auto const label = closerNominalLabel(iHandle->m_avg_current);
95  edm::LogInfo("MagneticField|AutoMagneticField")
96  << "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|AutoMagneticField")
106  << "Current :" << current << " (from valueOverride card); using map configuration with label: " << label;
109  .setConsumes(knownFromParamConfigToken_, edm::ESInputTag(""s, std::string(label)));
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  setWhatProduced(this, label)
117  .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  .setConsumes(chosenConfigToken_, 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|AutoMagneticField")
154  << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
155  << " slaveFieldVersion: " << conf->slaveFieldVersion;
156 
157  if (conf->version == "parametrizedMagneticField") {
158  // The map consist of only the parametrization in this case
159  return paramField;
160  }
161 
162  // Full VolumeBased map + parametrization
163  MagGeoBuilderFromDDD builder(conf->version, conf->geometryVersion, debug_);
164 
165  // Set scaling factors
166  if (!conf->keys.empty()) {
167  builder.setScaling(conf->keys, conf->values);
168  }
169 
170  // Set specification for the grid tables to be used.
171  if (!conf->gridFiles.empty()) {
172  builder.setGridFiles(conf->gridFiles);
173  }
174 
175  // Build the geometry (DDDCompactView) from the DB blob
176  // (code taken from GeometryReaders/XMLIdealGeometryESSource/src/XMLIdealMagneticFieldGeometryESProducer.cc)
177 
178  auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
179 
180  DDCompactView cpv{DDName("cmsMagneticField:MAGF")};
181  DDLParser parser(cpv);
182  parser.getDDLSAX2FileHandler()->setUserNS(true);
183  parser.clearFiles();
184  std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();
185  parser.parse(*tb, tb->size());
186  cpv.lockdown();
187 
188  builder.build(cpv);
189 
190  // Build the VB map. Ownership of the parametrization is transferred to it
191  return std::make_unique<VolumeBasedMagneticField>(conf->geometryVersion,
192  builder.barrelLayers(),
193  builder.endcapSectors(),
194  builder.barrelVolumes(),
195  builder.endcapVolumes(),
196  builder.maxR(),
197  builder.maxZ(),
198  paramField.release(),
199  true);
200 }
201 
203  constexpr std::array<int, 7> nominalCurrents = {{-1, 0, 9558, 14416, 16819, 18268, 19262}};
204  constexpr std::array<std::string_view, 7> nominalLabels = {{"3.8T", "0T", "2T", "3T", "3.5T", "3.8T", "4T"}};
205 
206  int i = 0;
207  for (; i < (int)nominalLabels.size() - 1; i++) {
208  if (2 * current < nominalCurrents[i] + nominalCurrents[i + 1])
209  return nominalLabels[i];
210  }
211  return nominalLabels[i];
212 }
213 
216  desc.addUntracked<bool>("debugBuilder", false);
217  desc.add<int>("valueOverride", -1)->setComment("Force value of current (in A); take the value from DB if < 0.");
218  desc.addUntracked<std::string>("label", "");
219 
220  descriptions.addDefault(desc);
221 }
222 
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T getParameter(std::string const &) const
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:138
T getUntrackedParameter(std::string const &, T const &) const
int parse(const DDLDocumentProvider &dp)
Parse all files. Return is meaningless.
Definition: DDLParser.cc:123
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
ESHandle< ProductT > getHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
std::vector< MagBLayer * > barrelLayers() const
Get barrel layers.
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
ESTransientHandle< ProductT > getTransientHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
Definition: config.py:1
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
std::vector< MagVolume6Faces * > barrelVolumes() const
char const * label
static std::unique_ptr< MagneticField > get(std::string version, const edm::ParameterSet &parameters)
virtual void build(const DDCompactView &cpv)
void addDefault(ParameterSetDescription const &psetDescription)
virtual void setUserNS(bool userns)
std::array< int, 7 > nominalCurrents
std::shared_ptr< MagFieldConfig const > chooseConfigAtRuntime(const IdealMagneticFieldRecord &iRecord)
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd > mayGetConfigToken_
std::vector< MagESector * > endcapSectors() const
Get endcap layers.
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void clearFiles()
Clear the file list - see Warning!
Definition: DDLParser.cc:208
DDLSAX2FileHandler * getDDLSAX2FileHandler()
To get the parent this class allows access to the handler.
Definition: DDLParser.cc:53
DDLParser is the main class of Detector Description Language Parser.
Definition: DDLParser.h:63
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::ESGetToken< MagFieldConfig, IdealMagneticFieldRecord > chosenConfigToken_
std::string version
Version of the data tables to be used.
void setScaling(const std::vector< int > &keys, const std::vector< double > &values)
std::shared_ptr< MagFieldConfig const > chooseConfigViaParameter(const IdealMagneticFieldRecord &iRecord)
float m_avg_current
Definition: RunInfo.h:28
HLT enums.
std::vector< MagVolume6Faces * > endcapVolumes() const
int geometryVersion
Version of the geometry to be used.
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd > knownFromParamConfigToken_
std::unique_ptr< MagneticField > produce(const IdealMagneticFieldRecord &iRecord)
T const * product() const
Definition: ESHandle.h:86
#define constexpr
void setGridFiles(const magneticfield::TableFileMap &gridFiles)