Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-07-15 22:06:31

0001 #ifndef HLTrigger_HLTUpgradeNano_TICLCandidateExtraTableProducer_h
0002 #define HLTrigger_HLTUpgradeNano_TICLCandidateExtraTableProducer_h
0003 
0004 #include "PhysicsTools/NanoAOD/interface/SimpleFlatTableProducer.h"
0005 #include "DataFormats/HGCalReco/interface/TICLCandidate.h"
0006 #include "DataFormats/HGCalReco/interface/Trackster.h"
0007 
0008 //
0009 // One-to-many: TICLCandidate -> linked Tracksters
0010 // Or SimTICLCandidate --> SimTracksters
0011 //
0012 
0013 class TICLCandidateExtraTableProducer : public SimpleFlatTableProducerBase<TICLCandidate, std::vector<TICLCandidate>> {
0014 public:
0015   using TProd = edm::Ptr<ticl::Trackster>;
0016 
0017   TICLCandidateExtraTableProducer(edm::ParameterSet const& params)
0018       : SimpleFlatTableProducerBase<TICLCandidate, std::vector<TICLCandidate>>(params) {
0019     if (params.existsAs<edm::ParameterSet>("collectionVariables")) {
0020       edm::ParameterSet const& collectionVarsPSet = params.getParameter<edm::ParameterSet>("collectionVariables");
0021       for (const auto& coltablename : collectionVarsPSet.getParameterNamesForType<edm::ParameterSet>()) {
0022         const auto& coltablePSet = collectionVarsPSet.getParameter<edm::ParameterSet>(coltablename);
0023 
0024         CollectionVariableTableInfo coltable;
0025         coltable.name =
0026             coltablePSet.existsAs<std::string>("name") ? coltablePSet.getParameter<std::string>("name") : coltablename;
0027         coltable.doc = coltablePSet.getParameter<std::string>("doc");
0028         coltable.useCount = coltablePSet.getParameter<bool>("useCount");
0029         coltable.useOffset = coltablePSet.getParameter<bool>("useOffset");
0030 
0031         this->coltables_.push_back(std::move(coltable));
0032         produces<nanoaod::FlatTable>(coltables_.back().name + "Table");
0033       }
0034     }
0035   }
0036 
0037   void produce(edm::Event& iEvent, const edm::EventSetup&) override {
0038     const auto& prod = iEvent.getHandle(this->src_);
0039 
0040     const auto& candidates = *prod;
0041     const size_t table_size = candidates.size();
0042 
0043     auto out = std::make_unique<nanoaod::FlatTable>(table_size, this->name_, /*singleton*/ false, /*extension*/ false);
0044 
0045     unsigned int coltablesize = 0;
0046     std::vector<unsigned int> counts;
0047     counts.reserve(table_size);
0048 
0049     std::vector<uint32_t> tracksterKeys;
0050 
0051     for (const auto& cand : candidates) {
0052       const auto& children = cand.tracksters();
0053       counts.push_back(children.size());
0054       coltablesize += children.size();
0055       for (const auto& t : children) {
0056         tracksterKeys.push_back(t.key());
0057       }
0058     }
0059 
0060     for (const auto& coltable : this->coltables_) {
0061       if (coltable.useCount) {
0062         out->addColumn<uint16_t>("n" + coltable.name, counts, "Count for " + coltable.name);
0063       }
0064       if (coltable.useOffset) {
0065         std::vector<unsigned int> offsets;
0066         offsets.reserve(counts.size());
0067         unsigned int offset = 0;
0068         for (auto c : counts) {
0069           offsets.push_back(offset);
0070           offset += c;
0071         }
0072         out->addColumn<uint16_t>("o" + coltable.name, offsets, "Offset for " + coltable.name);
0073       }
0074 
0075       auto outcoltable = std::make_unique<nanoaod::FlatTable>(coltablesize, coltable.name, false, false);
0076 
0077       outcoltable->addColumn<uint32_t>("tracksterIndex", tracksterKeys, "Index of associated Trackster");
0078 
0079       outcoltable->setDoc(coltable.doc);
0080       iEvent.put(std::move(outcoltable), coltable.name + "Table");
0081     }
0082 
0083     if (out->nColumns() > 0) {
0084       out->setDoc(this->doc_);
0085       iEvent.put(std::move(out));
0086     }
0087   }
0088 
0089   std::unique_ptr<nanoaod::FlatTable> fillTable(const edm::Event&,
0090                                                 const edm::Handle<std::vector<TICLCandidate>>&) const override {
0091     return std::make_unique<nanoaod::FlatTable>();
0092   }
0093 
0094   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0095     edm::ParameterSetDescription desc =
0096         SimpleFlatTableProducerBase<TICLCandidate, std::vector<TICLCandidate>>::baseDescriptions();
0097 
0098     edm::ParameterSetDescription coltable;
0099     coltable.add<std::string>("name", "hltTiclTrackstersMerge");
0100     coltable.add<std::string>("doc", "TICL Candidates");
0101     coltable.add<bool>("useCount", true);
0102     coltable.add<bool>("useOffset", false);
0103     edm::ParameterSetDescription colvariables;  // unused here
0104     coltable.add<edm::ParameterSetDescription>("variables", colvariables);
0105 
0106     edm::ParameterSetDescription coltables;
0107     coltables.addOptionalNode(
0108         edm::ParameterWildcard<edm::ParameterSetDescription>("*", edm::RequireZeroOrMore, true, coltable), false);
0109 
0110     desc.addOptional<edm::ParameterSetDescription>("collectionVariables", coltables);
0111     descriptions.addWithDefaultLabel(desc);
0112   }
0113 
0114 protected:
0115   struct CollectionVariableTableInfo {
0116     std::string name;
0117     std::string doc;
0118     bool useCount;
0119     bool useOffset;
0120   };
0121   std::vector<CollectionVariableTableInfo> coltables_;
0122 };
0123 
0124 #include "FWCore/Framework/interface/MakerMacros.h"
0125 DEFINE_FWK_MODULE(TICLCandidateExtraTableProducer);
0126 
0127 #endif