|
| 1 | +#include "DataFormats/NanoAOD/interface/FlatTable.h" |
| 2 | +#include "DataFormats/TauReco/interface/PFTauTransverseImpactParameterAssociation.h" |
| 3 | +#include "DataFormats/TauReco/interface/TauDiscriminatorContainer.h" |
| 4 | +#include "FWCore/Framework/interface/Event.h" |
| 5 | +#include "FWCore/Framework/interface/Frameworkfwd.h" |
| 6 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 7 | +#include "FWCore/Framework/interface/global/EDProducer.h" |
| 8 | +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" |
| 9 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 10 | + |
| 11 | +class HLTTauTableProducer : public edm::global::EDProducer<> { |
| 12 | +public: |
| 13 | + using TauCollection = edm::View<reco::BaseTau>; |
| 14 | + using TauIPVector = edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::PFTauTransverseImpactParameterRef>>; |
| 15 | + using TauDiscrMap = reco::TauDiscriminatorContainer; |
| 16 | + // TauCollection = deeptau.TauCollection; |
| 17 | + // using TauDeepTauVector = edm::AssociationVector<reco::PFTauRefProd, std::vector<reco::TauDiscriminatorContainer>>; |
| 18 | + HLTTauTableProducer(const edm::ParameterSet& cfg) |
| 19 | + : tableName_(cfg.getParameter<std::string>("tableName")), |
| 20 | + skipNonExistingSrc_(cfg.getParameter<bool>("skipNonExistingSrc")), |
| 21 | + tauToken_(mayConsume<TauCollection>(cfg.getParameter<edm::InputTag>("taus"))), |
| 22 | + tauIPToken_(mayConsume<TauIPVector>(cfg.getParameter<edm::InputTag>("tauTransverseImpactParameters"))), |
| 23 | + deepTauVSeToken_(mayConsume<TauDiscrMap>(cfg.getParameter<edm::InputTag>("deepTauVSe"))), |
| 24 | + deepTauVSmuToken_(mayConsume<TauDiscrMap>(cfg.getParameter<edm::InputTag>("deepTauVSmu"))), |
| 25 | + deepTauVSjetToken_(mayConsume<TauDiscrMap>(cfg.getParameter<edm::InputTag>("deepTauVSjet"))), |
| 26 | + precision_(cfg.getParameter<int>("precision")) { |
| 27 | + produces<nanoaod::FlatTable>(tableName_); |
| 28 | + } |
| 29 | + |
| 30 | + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) { |
| 31 | + edm::ParameterSetDescription desc; |
| 32 | + desc.add<std::string>("tableName", "hltHpsPFTau") |
| 33 | + ->setComment("Table name, needs to be the same as the main Tau table"); |
| 34 | + desc.add<bool>("skipNonExistingSrc", false) |
| 35 | + ->setComment("whether or not to skip producing the table on absent input product"); |
| 36 | + desc.add<edm::InputTag>("taus", edm::InputTag("")); |
| 37 | + desc.add<edm::InputTag>("tauTransverseImpactParameters", edm::InputTag("")); |
| 38 | + desc.add<edm::InputTag>("deepTauVSe", edm::InputTag("")); |
| 39 | + desc.add<edm::InputTag>("deepTauVSmu", edm::InputTag("")); |
| 40 | + desc.add<edm::InputTag>("deepTauVSjet", edm::InputTag("")); |
| 41 | + desc.add<int>("precision", 7); |
| 42 | + descriptions.addWithDefaultLabel(desc); |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + void produce(edm::StreamID id, edm::Event& event, const edm::EventSetup& setup) const override { |
| 47 | + const auto tausHandle = event.getHandle(tauToken_); |
| 48 | + const size_t nTaus = tausHandle.isValid() ? (*tausHandle).size() : 0; |
| 49 | + |
| 50 | + // resize all output vectors |
| 51 | + static constexpr float default_value = std::numeric_limits<float>::quiet_NaN(); |
| 52 | + |
| 53 | + std::vector<float> deepTauVSe(nTaus, default_value); |
| 54 | + std::vector<float> deepTauVSmu(nTaus, default_value); |
| 55 | + std::vector<float> deepTauVSjet(nTaus, default_value); |
| 56 | + |
| 57 | + // source: RecoTauTag/RecoTau/plugins/PFTauTransverseImpactParameters.cc |
| 58 | + std::vector<float> dxy(nTaus, default_value); |
| 59 | + std::vector<float> dxy_error(nTaus, default_value); |
| 60 | + std::vector<float> ip3d(nTaus, default_value); |
| 61 | + std::vector<float> ip3d_error(nTaus, default_value); |
| 62 | + std::vector<float> hasSecondaryVertex(nTaus, default_value); |
| 63 | + std::vector<float> flightLength_x(nTaus, default_value); |
| 64 | + std::vector<float> flightLength_y(nTaus, default_value); |
| 65 | + std::vector<float> flightLength_z(nTaus, default_value); |
| 66 | + std::vector<float> flightLengthSig(nTaus, default_value); |
| 67 | + std::vector<float> secondaryVertex_x(nTaus, default_value); |
| 68 | + std::vector<float> secondaryVertex_y(nTaus, default_value); |
| 69 | + std::vector<float> secondaryVertex_z(nTaus, default_value); |
| 70 | + |
| 71 | + if (tausHandle.isValid() || !(this->skipNonExistingSrc_)) { |
| 72 | + const auto& tausProductId = tausHandle.id(); |
| 73 | + const auto& tausIPHandle = event.getHandle(tauIPToken_); |
| 74 | + const auto& deepTauVSeMapHandle = event.getHandle(deepTauVSeToken_); |
| 75 | + const auto& deepTauVSmuMapHandle = event.getHandle(deepTauVSmuToken_); |
| 76 | + const auto& deepTauVSjetMapHandle = event.getHandle(deepTauVSjetToken_); |
| 77 | + |
| 78 | + for (size_t tau_index = 0; tau_index < nTaus; ++tau_index) { |
| 79 | + if (deepTauVSeMapHandle.isValid() || !(this->skipNonExistingSrc_)) { |
| 80 | + deepTauVSe[tau_index] = deepTauVSeMapHandle->get(tausProductId, tau_index).rawValues.at(0); |
| 81 | + } else { |
| 82 | + edm::LogWarning("HLTTauTableProducer") << " Invalid handle for DeeTauVse score input collection"; |
| 83 | + } |
| 84 | + |
| 85 | + if (deepTauVSmuMapHandle.isValid() || !(this->skipNonExistingSrc_)) { |
| 86 | + deepTauVSmu[tau_index] = deepTauVSmuMapHandle->get(tausProductId, tau_index).rawValues.at(0); |
| 87 | + } else { |
| 88 | + edm::LogWarning("HLTTauTableProducer") << " Invalid handle for DeeTauVsMu score input collection"; |
| 89 | + } |
| 90 | + |
| 91 | + if (deepTauVSjetMapHandle.isValid() || !(this->skipNonExistingSrc_)) { |
| 92 | + deepTauVSjet[tau_index] = deepTauVSjetMapHandle->get(tausProductId, tau_index).rawValues.at(0); |
| 93 | + } else { |
| 94 | + edm::LogWarning("HLTTauTableProducer") << " Invalid handle for DeeTauVsJet score input collection"; |
| 95 | + } |
| 96 | + |
| 97 | + if (tausIPHandle.isValid() || !(this->skipNonExistingSrc_)) { |
| 98 | + dxy[tau_index] = tausIPHandle->value(tau_index)->dxy(); |
| 99 | + dxy_error[tau_index] = tausIPHandle->value(tau_index)->dxy_error(); |
| 100 | + ip3d[tau_index] = tausIPHandle->value(tau_index)->ip3d(); |
| 101 | + ip3d_error[tau_index] = tausIPHandle->value(tau_index)->ip3d_error(); |
| 102 | + hasSecondaryVertex[tau_index] = tausIPHandle->value(tau_index)->hasSecondaryVertex(); |
| 103 | + flightLength_x[tau_index] = tausIPHandle->value(tau_index)->flightLength().x(); |
| 104 | + flightLength_y[tau_index] = tausIPHandle->value(tau_index)->flightLength().y(); |
| 105 | + flightLength_z[tau_index] = tausIPHandle->value(tau_index)->flightLength().z(); |
| 106 | + flightLengthSig[tau_index] = tausIPHandle->value(tau_index)->flightLengthSig(); |
| 107 | + |
| 108 | + if (hasSecondaryVertex[tau_index] > 0) { |
| 109 | + secondaryVertex_x[tau_index] = tausIPHandle->value(tau_index)->secondaryVertex()->x(); |
| 110 | + secondaryVertex_y[tau_index] = tausIPHandle->value(tau_index)->secondaryVertex()->y(); |
| 111 | + secondaryVertex_z[tau_index] = tausIPHandle->value(tau_index)->secondaryVertex()->z(); |
| 112 | + } |
| 113 | + } else { |
| 114 | + edm::LogWarning("HLTTauTableProducer") << " Invalid handle for Tau IP input collection"; |
| 115 | + } |
| 116 | + } |
| 117 | + } else { |
| 118 | + edm::LogWarning("HLTTauTableProducer") << " Invalid handle for PFTau candidate input collection"; |
| 119 | + } |
| 120 | + |
| 121 | + auto tauTable = std::make_unique<nanoaod::FlatTable>(nTaus, tableName_, /*singleton*/ false, /*extension*/ true); |
| 122 | + tauTable->addColumn<float>("dxy", dxy, "tau transverse impact parameter", precision_); |
| 123 | + tauTable->addColumn<float>("dxy_error", dxy_error, " dxy_error ", precision_); |
| 124 | + tauTable->addColumn<float>("ip3d", ip3d, " ip3d ", precision_); |
| 125 | + tauTable->addColumn<float>("ip3d_error", ip3d_error, " ip3d_error ", precision_); |
| 126 | + tauTable->addColumn<float>("hasSecondaryVertex", hasSecondaryVertex, " hasSecondaryVertex ", precision_); |
| 127 | + tauTable->addColumn<float>("flightLength_x", flightLength_x, "flightLength_x", precision_); |
| 128 | + tauTable->addColumn<float>("flightLength_y", flightLength_y, "flightLength_y", precision_); |
| 129 | + tauTable->addColumn<float>("flightLength_z", flightLength_z, "flightLength_z", precision_); |
| 130 | + tauTable->addColumn<float>("flightLengthSig", flightLengthSig, "flightLengthSig", precision_); |
| 131 | + tauTable->addColumn<float>("secondaryVertex_x", secondaryVertex_x, "secondaryVertex_x", precision_); |
| 132 | + tauTable->addColumn<float>("secondaryVertex_y", secondaryVertex_y, "secondaryVertex_y", precision_); |
| 133 | + tauTable->addColumn<float>("secondaryVertex_z", secondaryVertex_z, "secondaryVertex_z", precision_); |
| 134 | + tauTable->addColumn<float>("deepTauVSe", deepTauVSe, "tau vs electron discriminator", precision_); |
| 135 | + tauTable->addColumn<float>("deepTauVSmu", deepTauVSmu, "tau vs muon discriminator", precision_); |
| 136 | + tauTable->addColumn<float>("deepTauVSjet", deepTauVSjet, "tau vs jet discriminator", precision_); |
| 137 | + |
| 138 | + event.put(std::move(tauTable), tableName_); |
| 139 | + } |
| 140 | + |
| 141 | +private: |
| 142 | + const std::string tableName_; |
| 143 | + const bool skipNonExistingSrc_; |
| 144 | + const edm::EDGetTokenT<TauCollection> tauToken_; |
| 145 | + const edm::EDGetTokenT<TauIPVector> tauIPToken_; |
| 146 | + const edm::EDGetTokenT<TauDiscrMap> deepTauVSeToken_, deepTauVSmuToken_, deepTauVSjetToken_; |
| 147 | + const unsigned int precision_; |
| 148 | +}; |
| 149 | + |
| 150 | +#include "FWCore/Framework/interface/MakerMacros.h" |
| 151 | +DEFINE_FWK_MODULE(HLTTauTableProducer); |
0 commit comments