File indexing completed on 2023-06-05 22:16:07
0001 #ifndef MuonTools_MuDigiBaseProducer_h
0002 #define MuonTools_MuDigiBaseProducer_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "DataFormats/MuonData/interface/MuonDigiCollection.h"
0014 #include "PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016
0017 #include <algorithm>
0018 #include <list>
0019 #include <string>
0020
0021 template <class DETECTOR_T, class DIGI_T>
0022 class MuDigiBaseProducer : public SimpleFlatTableProducerBase<DIGI_T, MuonDigiCollection<DETECTOR_T, DIGI_T>> {
0023 using COLLECTION = MuonDigiCollection<DETECTOR_T, DIGI_T>;
0024
0025 using IntDetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, int>;
0026 using UIntDetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, unsigned int>;
0027 using Int8DetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, int8_t>;
0028 using UInt8DetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, uint8_t>;
0029
0030 std::vector<std::unique_ptr<Variable<DETECTOR_T>>> detIdVars_;
0031
0032 public:
0033 MuDigiBaseProducer(edm::ParameterSet const ¶ms) : SimpleFlatTableProducerBase<DIGI_T, COLLECTION>(params) {
0034 const auto &varCfgs = params.getParameter<edm::ParameterSet>("detIdVariables");
0035 const auto &varNames = varCfgs.getParameterNamesForType<edm::ParameterSet>();
0036
0037 std::transform(varNames.begin(), varNames.end(), std::back_inserter(detIdVars_), [&](const auto &name) {
0038 const edm::ParameterSet &varCfg = varCfgs.getParameter<edm::ParameterSet>(name);
0039 const std::string &type = varCfg.getParameter<std::string>("type");
0040
0041 std::unique_ptr<Variable<DETECTOR_T>> detVarPtr;
0042
0043 if (type == "int") {
0044 detVarPtr = std::move(std::make_unique<IntDetVar>(name, varCfg));
0045 } else if (type == "uint") {
0046 detVarPtr = std::move(std::make_unique<UIntDetVar>(name, varCfg));
0047 } else if (type == "int8") {
0048 detVarPtr = std::move(std::make_unique<Int8DetVar>(name, varCfg));
0049 } else if (type == "uint8") {
0050 detVarPtr = std::move(std::make_unique<UInt8DetVar>(name, varCfg));
0051 } else {
0052 throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + name);
0053 }
0054
0055 return detVarPtr;
0056 });
0057 }
0058
0059 ~MuDigiBaseProducer() override {}
0060
0061 static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0062 edm::ParameterSetDescription desc = SimpleFlatTableProducerBase<DIGI_T, COLLECTION>::baseDescriptions();
0063
0064 edm::ParameterSetDescription variable;
0065 edm::Comment comType{"the c++ type of the branch in the flat table"};
0066 edm::Comment comPrecision{"the precision with which to store the value in the flat table"};
0067
0068 variable.add<std::string>("expr")->setComment("a function to define the content of the branch in the flat table");
0069 variable.add<std::string>("doc")->setComment("few words description of the branch content");
0070
0071 variable.ifValue(edm::ParameterDescription<std::string>("type", "int", true, comType),
0072 edm::allowedValues<std::string>("int", "uint", "int8", "uint8"));
0073
0074 edm::ParameterSetDescription variables;
0075
0076 variables.setComment("a parameters set to define all variable taken form detId to fill the flat table");
0077
0078 edm::ParameterWildcard<edm::ParameterSetDescription> variableWildCard{"*", edm::RequireZeroOrMore, true, variable};
0079 variables.addNode(variableWildCard);
0080
0081 desc.add<edm::ParameterSetDescription>("detIdVariables", variables);
0082
0083 descriptions.addWithDefaultLabel(desc);
0084 }
0085
0086 std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
0087 const edm::Handle<COLLECTION> &prod) const override {
0088 std::vector<const DIGI_T *> digis;
0089 std::vector<const DETECTOR_T *> detIds;
0090 std::list<DETECTOR_T> detIdObjs;
0091
0092 if (prod.isValid()) {
0093 auto detIdIt = prod->begin();
0094 auto detIdEnd = prod->end();
0095
0096 for (; detIdIt != detIdEnd; ++detIdIt) {
0097 const auto &[detId, range] = (*detIdIt);
0098 detIdObjs.push_back(detId);
0099 std::fill_n(std::back_inserter(detIds), range.second - range.first, &detIdObjs.back());
0100 std::transform(range.first, range.second, std::back_inserter(digis), [](const auto &digi) { return &digi; });
0101 }
0102 }
0103
0104 auto table = std::make_unique<nanoaod::FlatTable>(digis.size(), this->name_, false, this->extension_);
0105
0106 for (const auto &var : this->vars_) {
0107 var->fill(digis, *table);
0108 }
0109
0110 for (const auto &var : detIdVars_) {
0111 var->fill(detIds, *table);
0112 }
0113
0114 return table;
0115 }
0116 };
0117
0118 #endif