Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-25 02:13:43

0001 #ifndef MuonTools_MuDigiBaseProducer_h
0002 #define MuonTools_MuDigiBaseProducer_h
0003 
0004 /** \class MuDigiBaseProducer MuDigiBaseProducer.h DPGAnalysis/MuonTools/src/MuDigiBaseProducer.h
0005  *  
0006  * Helper class defining the generic interface of a muon digi Producer
0007  *
0008  * \author C. Battilana (INFN BO)
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 Int16DetVar = FuncVariable<DETECTOR_T, StringObjectFunction<DETECTOR_T>, int16_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 &params) : 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 == "int16") {
0048         detVarPtr = std::move(std::make_unique<Int16DetVar>(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     variable.addUntracked<bool>("lazyEval")->setComment("set to True if the type read from the Event is unknown");
0071 
0072     variable.ifValue(edm::ParameterDescription<std::string>("type", "int", true, comType),
0073                      edm::allowedValues<std::string>("int", "uint", "int16", "uint8"));
0074 
0075     edm::ParameterSetDescription variables;
0076 
0077     variables.setComment("a parameters set to define all variable taken form detId to fill the flat table");
0078 
0079     edm::ParameterWildcard<edm::ParameterSetDescription> variableWildCard{"*", edm::RequireZeroOrMore, true, variable};
0080     variables.addNode(variableWildCard);
0081 
0082     desc.add<edm::ParameterSetDescription>("detIdVariables", variables);
0083 
0084     descriptions.addWithDefaultLabel(desc);
0085   }
0086 
0087   std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event &iEvent,
0088                                                 const edm::Handle<COLLECTION> &prod) const override {
0089     std::vector<const DIGI_T *> digis;
0090     std::vector<const DETECTOR_T *> detIds;
0091     std::list<DETECTOR_T> detIdObjs;  // CB needed to store DetIds (they are transient)
0092 
0093     if (prod.isValid()) {
0094       auto detIdIt = prod->begin();
0095       auto detIdEnd = prod->end();
0096 
0097       for (; detIdIt != detIdEnd; ++detIdIt) {
0098         const auto &[detId, range] = (*detIdIt);
0099         detIdObjs.push_back(detId);
0100         std::fill_n(std::back_inserter(detIds), range.second - range.first, &detIdObjs.back());
0101         std::transform(range.first, range.second, std::back_inserter(digis), [](const auto &digi) { return &digi; });
0102       }
0103     }
0104 
0105     auto table = std::make_unique<nanoaod::FlatTable>(digis.size(), this->name_, false, this->extension_);
0106 
0107     for (const auto &var : this->vars_) {
0108       var->fill(digis, *table);
0109     }
0110 
0111     for (const auto &var : detIdVars_) {
0112       var->fill(detIds, *table);
0113     }
0114 
0115     return table;
0116   }
0117 };
0118 
0119 #endif