CMS 3D CMS Logo

MuLocalRecoBaseProducer.h
Go to the documentation of this file.
1 #ifndef MuonTools_MuRecObjBaseProducer_h
2 #define MuonTools_MuRecObjBaseProducer_h
3 
16 
20 
22 
27 
28 #include <algorithm>
29 #include <type_traits>
30 #include <list>
31 #include <string>
32 
33 template <class DETECTOR_T, class RECO_T, class GEOM_T>
35  : public SimpleFlatTableProducerBase<RECO_T, edm::RangeMap<DETECTOR_T, edm::OwnVector<RECO_T>>> {
37 
40 
45 
46  std::vector<std::unique_ptr<Variable<DETECTOR_T>>> detIdVars_;
47 
50 
51  std::vector<std::unique_ptr<Variable<GlobalPoint>>> globalPosVars_;
52  std::vector<std::unique_ptr<Variable<GlobalVector>>> globalDirVars_;
53 
54 public:
56  : SimpleFlatTableProducerBase<RECO_T, COLLECTION>(params), m_token{this->template esConsumes()} {
57  auto varCfgs = params.getParameter<edm::ParameterSet>("detIdVariables");
58  auto varNames = varCfgs.getParameterNamesForType<edm::ParameterSet>();
59 
60  std::transform(varNames.begin(), varNames.end(), std::back_inserter(detIdVars_), [&](const auto &name) {
61  const edm::ParameterSet &varCfg = varCfgs.getParameter<edm::ParameterSet>(name);
62  const std::string &type = varCfg.getParameter<std::string>("type");
63 
64  std::unique_ptr<Variable<DETECTOR_T>> detVarPtr;
65 
66  if (type == "int") {
67  detVarPtr = std::move(std::make_unique<IntDetVar>(name, varCfg)); // CB can improve?
68  } else if (type == "uint") {
69  detVarPtr = std::move(std::make_unique<UIntDetVar>(name, varCfg));
70  } else if (type == "int16") {
71  detVarPtr = std::move(std::make_unique<Int16DetVar>(name, varCfg));
72  } else if (type == "uint8") {
73  detVarPtr = std::move(std::make_unique<UInt8DetVar>(name, varCfg));
74  } else {
75  throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + name);
76  }
77 
78  return detVarPtr;
79  });
80 
81  varCfgs = params.getParameter<edm::ParameterSet>("globalPosVariables");
82  varNames = varCfgs.getParameterNamesForType<edm::ParameterSet>();
83 
84  std::transform(varNames.begin(), varNames.end(), std::back_inserter(globalPosVars_), [&](const auto &name) {
85  return std::make_unique<GlobalPosVar>(name, varCfgs.getParameter<edm::ParameterSet>(name));
86  });
87 
88  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
89  varCfgs = params.getParameter<edm::ParameterSet>("globalDirVariables");
90  varNames = varCfgs.getParameterNamesForType<edm::ParameterSet>();
91 
92  std::transform(varNames.begin(), varNames.end(), std::back_inserter(globalDirVars_), [&](const auto &name) {
93  return std::make_unique<GlobalDirVar>(name, varCfgs.getParameter<edm::ParameterSet>(name));
94  });
95  }
96  }
97 
98  ~MuRecObjBaseProducer() override {}
99 
102 
103  auto baseDescription = []() {
105 
106  varBase.add<std::string>("expr")->setComment("a function to define the content of the branch in the flat table");
107  varBase.add<std::string>("doc")->setComment("few words description of the branch content");
108 
109  return varBase;
110  };
111 
112  auto fullDescription = [](auto const &var, std::string const label) {
114 
116  fullDesc.setComment("a parameters set to define all " + label + " variables to the flat table");
117  fullDesc.addNode(detIdVarWildCard);
118 
119  return fullDesc;
120  };
121 
122  auto detIdVar{baseDescription()};
123  auto globalGeomVar{baseDescription()};
124 
125  edm::Comment comType{"the c++ type of the branch in the flat table"};
126  detIdVar.ifValue(edm::ParameterDescription<std::string>{"type", "int", true, comType},
127  edm::allowedValues<std::string>("int", "uint", "int16", "uint8"));
128 
129  edm::Comment comPrecision{"the precision with which to store the value in the flat table"};
130  globalGeomVar.addOptionalNode(edm::ParameterDescription<int>{"precision", true, comPrecision}, false);
131 
132  desc.add<edm::ParameterSetDescription>("detIdVariables", fullDescription(detIdVar, "DetId"));
133  desc.add<edm::ParameterSetDescription>("globalPosVariables", fullDescription(globalGeomVar, "Global Position"));
134 
135  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
136  desc.add<edm::ParameterSetDescription>("globalDirVariables", fullDescription(globalGeomVar, "Global Direction"));
137  }
138 
139  descriptions.addWithDefaultLabel(desc);
140  }
141 
142  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
143  const edm::Handle<COLLECTION> &product) const override {
144  std::vector<const RECO_T *> objs;
145  std::vector<const DETECTOR_T *> detIds;
146  std::vector<const GlobalPoint *> globalPositions;
147  std::vector<const GlobalVector *> globalDirections;
148 
149  // CB needed to store DetIds, global points and vectors (they are transient)
150  std::list<DETECTOR_T> detIdObjs;
151  std::list<GlobalPoint> globalPointObjs;
152  std::list<GlobalVector> globalVectorObjs;
153 
154  if (product.isValid()) {
155  auto detIdIt = product->id_begin();
156  const auto detIdEnd = product->id_end();
157 
158  for (; detIdIt != detIdEnd; ++detIdIt) {
159  const auto &range = product->get(*detIdIt);
160  const GeomDet *geomDet = m_geometry->idToDet(*detIdIt);
161 
162  detIdObjs.push_back(*detIdIt);
163  std::fill_n(std::back_inserter(detIds), range.second - range.first, &detIdObjs.back());
164 
165  for (auto objIt{range.first}; objIt != range.second; ++objIt) {
166  objs.push_back(&(*objIt));
167  globalPointObjs.push_back(geomDet->toGlobal(objIt->localPosition()));
168  globalPositions.push_back(&globalPointObjs.back());
169  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
170  globalVectorObjs.push_back(geomDet->toGlobal(objIt->localDirection()));
171  globalDirections.push_back(&globalVectorObjs.back());
172  }
173  }
174  }
175  }
176 
177  auto table = std::make_unique<nanoaod::FlatTable>(objs.size(), this->name_, false, this->extension_);
178 
179  for (const auto &var : this->vars_) {
180  var->fill(objs, *table);
181  }
182 
183  for (const auto &var : detIdVars_) {
184  var->fill(detIds, *table);
185  }
186 
187  for (const auto &var : globalPosVars_) {
188  var->fill(globalPositions, *table);
189  }
190 
191  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
192  for (const auto &var : globalDirVars_) {
193  var->fill(globalDirections, *table);
194  }
195  }
196 
197  return table;
198  }
199 
202  event.getByToken(this->src_, src);
203 
204  m_geometry = environment.getHandle(m_token);
205  std::unique_ptr<nanoaod::FlatTable> out = fillTable(event, src);
206  out->setDoc(this->doc_);
207 
208  event.put(std::move(out));
209  }
210 };
211 
212 #endif
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const edm::EDGetTokenT< edm::RangeMap< DETECTOR_T, edm::OwnVector< RECO_T > > > src_
MuRecObjBaseProducer(edm::ParameterSet const &params)
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
constexpr char const * varNames[]
char const * label
void setComment(std::string const &value)
void produce(edm::Event &event, const edm::EventSetup &environment) override
int iEvent
Definition: GenABIO.cc:224
edm::ESHandle< GEOM_T > m_geometry
std::vector< std::unique_ptr< Variable< DETECTOR_T > > > detIdVars_
id_iterator id_end() const
last identifier iterator
Definition: RangeMap.h:182
range get(ID id, CMP comparator) const
Definition: RangeMap.h:75
ParameterDescriptionBase * add(U const &iLabel, T const &value)
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
edm::ESGetToken< GEOM_T, MuonGeometryRecord > m_token
bool isValid() const
Definition: HandleBase.h:70
std::unique_ptr< nanoaod::FlatTable > fillTable(const edm::Event &iEvent, const edm::Handle< COLLECTION > &product) const override
std::vector< std::unique_ptr< Variable< GlobalPoint > > > globalPosVars_
id_iterator id_begin() const
first identifier iterator
Definition: RangeMap.h:180
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< std::unique_ptr< Variable< GlobalVector > > > globalDirVars_
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
static edm::ParameterSetDescription baseDescriptions()
unsigned transform(const HcalDetId &id, unsigned transformCode)