CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
VolumeBasedMagneticFieldESProducerFromDB.cc
Go to the documentation of this file.
1 
9 
12 
17 
19 
24 
27 
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.
55 
56  std::shared_ptr<MagFieldConfig const> chooseConfigViaParameter(const IdealMagneticFieldRecord& iRecord);
57  std::shared_ptr<MagFieldConfig const> chooseConfigAtRuntime(const IdealMagneticFieldRecord& iRecord);
58 
59  std::unique_ptr<MagneticField> produce(const IdealMagneticFieldRecord& iRecord);
60 
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63  private:
64  static std::string_view closerNominalLabel(float current);
65 
68 
69  //NOTE: change of record since this MagFieldConfig was chosen based on data
70  // from the record RunInfoRcd so therefore has a dependency upon that record
72 
74  const bool debug_;
75  };
76 } // namespace magneticfield
77 
78 VolumeBasedMagneticFieldESProducerFromDB::VolumeBasedMagneticFieldESProducerFromDB(const edm::ParameterSet& iConfig)
79  : debug_(iConfig.getUntrackedParameter<bool>("debugBuilder")) {
80  std::string const myConfigLabel = "VBMFESChoice";
81 
83  //Based on configuration, pick algorithm to produce the proper MagFieldConfig with a specific label
84  const int current = iConfig.getParameter<int>("valueOverride");
85  if (current < 0) {
86  //We do not know what to get until we first read RunInfo
89  .setMayConsume(
91  [](auto const& iGet, edm::ESTransientHandle<RunInfo> iHandle) {
92  auto const label = closerNominalLabel(iHandle->m_avg_current);
93  edm::LogInfo("MagneticField") << "Current :" << iHandle->m_avg_current
94  << " (from RunInfo DB); using map configuration with label: " << label;
95  return iGet("", label);
96  },
98 
99  } else {
100  //we know exactly what we are going to get
101  auto const label = closerNominalLabel(current);
102  edm::LogInfo("MagneticField") << "Current :" << current
103  << " (from valueOverride card); using map configuration with label: " << label;
104  auto cc = setWhatProduced(
106  knownFromParamConfigToken_ = cc.consumes(edm::ESInputTag(""s, std::string(label)));
107  }
108 
109  auto const label = iConfig.getUntrackedParameter<std::string>("label");
110  auto const myConfigTag = edm::ESInputTag(iConfig.getParameter<std::string>("@module_label"), myConfigLabel);
111 
112  //We use the MagFieldConfig created above to decide which FileBlob to use
113  auto cc = setWhatProduced(this, label);
114  cc.setMayConsume(
116  [](auto const& iGet, edm::ESTransientHandle<MagFieldConfig> iConfig) {
117  if (iConfig->version == "parametrizedMagneticField") {
118  return iGet.nothing();
119  }
120  return iGet("", std::to_string(iConfig->geometryVersion));
121  },
123  chosenConfigToken_ = cc.consumes(myConfigTag); //Use same tag as the choice
124 }
125 
126 std::shared_ptr<MagFieldConfig const> VolumeBasedMagneticFieldESProducerFromDB::chooseConfigAtRuntime(
127  IdealMagneticFieldRecord const& iRcd) {
129 
130  //just forward what we just got but do not take ownership
131  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
132 }
133 
135  const IdealMagneticFieldRecord& iRecord) {
137 
138  //just forward what we just got but do not take ownership
139  return std::shared_ptr<MagFieldConfig const>(config.product(), [](auto*) {});
140 }
141 
142 // ------------ method called to produce the data ------------
144  const IdealMagneticFieldRecord& iRecord) {
145  auto const& conf = iRecord.getTransientHandle(chosenConfigToken_);
146 
147  std::unique_ptr<MagneticField> paramField =
148  ParametrizedMagneticFieldFactory::get(conf->slaveFieldVersion, conf->slaveFieldParameters);
149 
150  edm::LogInfo("MagneticField") << "Version: " << conf->version << " geometryVersion: " << conf->geometryVersion
151  << " slaveFieldVersion: " << conf->slaveFieldVersion;
152 
153  if (conf->version == "parametrizedMagneticField") {
154  // The map consist of only the parametrization in this case
155  return paramField;
156  }
157 
158  // Full VolumeBased map + parametrization
159  MagGeoBuilderFromDDD builder(conf->version, conf->geometryVersion, debug_);
160 
161  // Set scaling factors
162  if (!conf->keys.empty()) {
163  builder.setScaling(conf->keys, conf->values);
164  }
165 
166  // Set specification for the grid tables to be used.
167  if (!conf->gridFiles.empty()) {
168  builder.setGridFiles(conf->gridFiles);
169  }
170 
171  // Build the geometry (DDDCompactView) from the DB blob
172  // (code taken from GeometryReaders/XMLIdealGeometryESSource/src/XMLIdealMagneticFieldGeometryESProducer.cc)
173 
174  auto const& blob = iRecord.getTransientHandle(mayConsumeBlobToken_);
175 
176  DDCompactView cpv{DDName("cmsMagneticField:MAGF")};
177  DDLParser parser(cpv);
178  parser.getDDLSAX2FileHandler()->setUserNS(true);
179  parser.clearFiles();
180  std::unique_ptr<std::vector<unsigned char> > tb = blob->getUncompressedBlob();
181  parser.parse(*tb, tb->size());
182  cpv.lockdown();
183 
184  builder.build(cpv);
185 
186  // Build the VB map. Ownership of the parametrization is transferred to it
187  return std::make_unique<VolumeBasedMagneticField>(conf->geometryVersion,
188  builder.barrelLayers(),
189  builder.endcapSectors(),
190  builder.barrelVolumes(),
191  builder.endcapVolumes(),
192  builder.maxR(),
193  builder.maxZ(),
194  paramField.release(),
195  true);
196 }
197 
199  constexpr std::array<int, 7> nominalCurrents = {{-1, 0, 9558, 14416, 16819, 18268, 19262}};
200  constexpr std::array<std::string_view, 7> nominalLabels = {{"3.8T", "0T", "2T", "3T", "3.5T", "3.8T", "4T"}};
201 
202  int i = 0;
203  for (; i < (int)nominalLabels.size() - 1; i++) {
204  if (2 * current < nominalCurrents[i] + nominalCurrents[i + 1])
205  return nominalLabels[i];
206  }
207  return nominalLabels[i];
208 }
209 
212  desc.addUntracked<bool>("debugBuilder", false);
213  desc.add<int>("valueOverride", -1)->setComment("Force value of current (in A); take the value from DB if < 0.");
214  desc.addUntracked<std::string>("label", "");
215 
216  descriptions.addDefault(desc);
217 }
218 
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
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:17
ESTransientHandle< ProductT > getTransientHandle(ESGetToken< ProductT, DepRecordT > const &iToken) const
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
std::vector< MagVolume6Faces * > barrelVolumes() const
void usesResources(std::vector< std::string > 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)
static const std::string kDDGeometry
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
Log< level::Info, false > LogInfo
DDLParser is the main class of Detector Description Language Parser.
Definition: DDLParser.h:63
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::ESGetToken< MagFieldConfig, IdealMagneticFieldRecord > chosenConfigToken_
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
void setScaling(const std::vector< int > &keys, const std::vector< double > &values)
std::shared_ptr< MagFieldConfig const > chooseConfigViaParameter(const IdealMagneticFieldRecord &iRecord)
tuple config
parse the configuration file
std::vector< MagVolume6Faces * > endcapVolumes() const
edm::ESGetToken< MagFieldConfig, MagFieldConfigRcd > knownFromParamConfigToken_
std::unique_ptr< MagneticField > produce(const IdealMagneticFieldRecord &iRecord)
constexpr std::array< int, 7 > nominalCurrents
void setGridFiles(const magneticfield::TableFileMap &gridFiles)