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  varBase.addUntracked<bool>("lazyEval")->setComment("if True, check object type during Event processing.");
109 
110  return varBase;
111  };
112 
113  auto fullDescription = [](auto const &var, std::string const label) {
115 
117  fullDesc.setComment("a parameters set to define all " + label + " variables to the flat table");
118  fullDesc.addNode(detIdVarWildCard);
119 
120  return fullDesc;
121  };
122 
123  auto detIdVar{baseDescription()};
124  auto globalGeomVar{baseDescription()};
125 
126  edm::Comment comType{"the c++ type of the branch in the flat table"};
127  detIdVar.ifValue(edm::ParameterDescription<std::string>{"type", "int", true, comType},
128  edm::allowedValues<std::string>("int", "uint", "int16", "uint8"));
129 
130  edm::Comment comPrecision{"the precision with which to store the value in the flat table"};
131  globalGeomVar.addOptionalNode(edm::ParameterDescription<int>{"precision", true, comPrecision}, false);
132 
133  desc.add<edm::ParameterSetDescription>("detIdVariables", fullDescription(detIdVar, "DetId"));
134  desc.add<edm::ParameterSetDescription>("globalPosVariables", fullDescription(globalGeomVar, "Global Position"));
135 
136  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
137  desc.add<edm::ParameterSetDescription>("globalDirVariables", fullDescription(globalGeomVar, "Global Direction"));
138  }
139 
140  descriptions.addWithDefaultLabel(desc);
141  }
142 
143  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
144  const edm::Handle<COLLECTION> &product) const override {
145  std::vector<const RECO_T *> objs;
146  std::vector<const DETECTOR_T *> detIds;
147  std::vector<const GlobalPoint *> globalPositions;
148  std::vector<const GlobalVector *> globalDirections;
149 
150  // CB needed to store DetIds, global points and vectors (they are transient)
151  std::list<DETECTOR_T> detIdObjs;
152  std::list<GlobalPoint> globalPointObjs;
153  std::list<GlobalVector> globalVectorObjs;
154 
155  if (product.isValid()) {
156  auto detIdIt = product->id_begin();
157  const auto detIdEnd = product->id_end();
158 
159  for (; detIdIt != detIdEnd; ++detIdIt) {
160  const auto &range = product->get(*detIdIt);
161  const GeomDet *geomDet = m_geometry->idToDet(*detIdIt);
162 
163  detIdObjs.push_back(*detIdIt);
164  std::fill_n(std::back_inserter(detIds), range.second - range.first, &detIdObjs.back());
165 
166  for (auto objIt{range.first}; objIt != range.second; ++objIt) {
167  objs.push_back(&(*objIt));
168  globalPointObjs.push_back(geomDet->toGlobal(objIt->localPosition()));
169  globalPositions.push_back(&globalPointObjs.back());
170  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
171  globalVectorObjs.push_back(geomDet->toGlobal(objIt->localDirection()));
172  globalDirections.push_back(&globalVectorObjs.back());
173  }
174  }
175  }
176  }
177 
178  auto table = std::make_unique<nanoaod::FlatTable>(objs.size(), this->name_, false, this->extension_);
179 
180  for (const auto &var : this->vars_) {
181  var->fill(objs, *table);
182  }
183 
184  for (const auto &var : detIdVars_) {
185  var->fill(detIds, *table);
186  }
187 
188  for (const auto &var : globalPosVars_) {
189  var->fill(globalPositions, *table);
190  }
191 
192  if constexpr (std::is_base_of_v<RecSegment, RECO_T>) {
193  for (const auto &var : globalDirVars_) {
194  var->fill(globalDirections, *table);
195  }
196  }
197 
198  return table;
199  }
200 
203  event.getByToken(this->src_, src);
204 
205  m_geometry = environment.getHandle(m_token);
206  std::unique_ptr<nanoaod::FlatTable> out = fillTable(event, src);
207  out->setDoc(this->doc_);
208 
209  event.put(std::move(out));
210  }
211 };
212 
213 #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
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
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)