MuDigiBaseProducer

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#ifndef MuonTools_MuDigiBaseProducer_h
#define MuonTools_MuDigiBaseProducer_h

/** \class MuDigiBaseProducer MuDigiBaseProducer.h DPGAnalysis/MuonTools/src/MuDigiBaseProducer.h
 *  
 * Helper class defining the generic interface of a muon digi Producer
 *
 * \author C. Battilana (INFN BO)
 *
 *
 */

#include "DataFormats/MuonData/interface/MuonDigiCollection.h"
#include "PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include <algorithm>
#include <list>
#include <string>

template <class DETECTOR_T, class DIGI_T>
class MuDigiBaseProducer : public SimpleFlatTableProducerBase<DIGI_T, MuonDigiCollection<DETECTOR_T, DIGI_T>> {
  using COLLECTION = MuonDigiCollection<DETECTOR_T, DIGI_T>;

  using IntDetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, int>;
  using UIntDetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, unsigned int>;
  using Int16DetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, int16_t>;
  using UInt8DetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, uint8_t>;

  std::vector<std::unique_ptr<Variable<DETECTOR_T>>> detIdVars_;

public:
  MuDigiBaseProducer(edm::ParameterSet const &params) : SimpleFlatTableProducerBase<DIGI_T, COLLECTION>(params) {
    const auto &varCfgs = params.getParameter<edm::ParameterSet>("detIdVariables");
    const auto &varNames = varCfgs.getParameterNamesForType<edm::ParameterSet>();

    std::transform(varNames.begin(), varNames.end(), std::back_inserter(detIdVars_), [&](const auto &name) {
      const edm::ParameterSet &varCfg = varCfgs.getParameter<edm::ParameterSet>(name);
      const std::string &type = varCfg.getParameter<std::string>("type");

      std::unique_ptr<Variable<DETECTOR_T>> detVarPtr;

      if (type == "int") {
        detVarPtr = std::move(std::make_unique<IntDetVar>(name, varCfg));
      } else if (type == "uint") {
        detVarPtr = std::move(std::make_unique<UIntDetVar>(name, varCfg));
      } else if (type == "int16") {
        detVarPtr = std::move(std::make_unique<Int16DetVar>(name, varCfg));
      } else if (type == "uint8") {
        detVarPtr = std::move(std::make_unique<UInt8DetVar>(name, varCfg));
      } else {
        throw cms::Exception("Configuration", "unsupported type " + type + " for variable " + name);
      }

      return detVarPtr;
    });
  }

  ~MuDigiBaseProducer() override {}

  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
    edm::ParameterSetDescription desc = SimpleFlatTableProducerBase<DIGI_T, COLLECTION>::baseDescriptions();

    edm::ParameterSetDescription variable;
    edm::Comment comType{"the c++ type of the branch in the flat table"};
    edm::Comment comPrecision{"the precision with which to store the value in the flat table"};

    variable.add<std::string>("expr")->setComment("a function to define the content of the branch in the flat table");
    variable.add<std::string>("doc")->setComment("few words description of the branch content");
    variable.addUntracked<bool>("lazyEval")->setComment("set to True if the type read from the Event is unknown");

    variable.ifValue(edm::ParameterDescription<std::string>("type", "int", true, comType),
                     edm::allowedValues<std::string>("int", "uint", "int16", "uint8"));

    edm::ParameterSetDescription variables;

    variables.setComment("a parameters set to define all variable taken form detId to fill the flat table");

    edm::ParameterWildcard<edm::ParameterSetDescription> variableWildCard{"*", edm::RequireZeroOrMore, true, variable};
    variables.addNode(variableWildCard);

    desc.add<edm::ParameterSetDescription>("detIdVariables", variables);

    descriptions.addWithDefaultLabel(desc);
  }

  std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
                                                const edm::Handle<COLLECTION> &prod) const override {
    std::vector<const DIGI_T *> digis;
    std::vector<const DETECTOR_T *> detIds;
    std::list<DETECTOR_T> detIdObjs;  // CB needed to store DetIds (they are transient)

    if (prod.isValid()) {
      auto detIdIt = prod->begin();
      auto detIdEnd = prod->end();

      for (; detIdIt != detIdEnd; ++detIdIt) {
        const auto &[detId, range] = (*detIdIt);
        detIdObjs.push_back(detId);
        std::fill_n(std::back_inserter(detIds), range.second - range.first, &detIdObjs.back());
        std::transform(range.first, range.second, std::back_inserter(digis), [](const auto &digi) { return &digi; });
      }
    }

    auto table = std::make_unique<nanoaod::FlatTable>(digis.size(), this->name_, false, this->extension_);

    for (const auto &var : this->vars_) {
      var->fill(digis, *table);
    }

    for (const auto &var : detIdVars_) {
      var->fill(detIds, *table);
    }

    return table;
  }
};

#endif