From cdfb9a5206a76195ca6a8c80961e13790ac37e2b Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 4 Jul 2018 01:29:45 +0200 Subject: [PATCH 01/31] Add MXNet predictor based on the C API. --- PhysicsTools/MXNet/BuildFile.xml | 7 + PhysicsTools/MXNet/interface/MXNetPredictor.h | 51 ++++++++ PhysicsTools/MXNet/src/MXNetPredictor.cc | 123 ++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 PhysicsTools/MXNet/BuildFile.xml create mode 100644 PhysicsTools/MXNet/interface/MXNetPredictor.h create mode 100644 PhysicsTools/MXNet/src/MXNetPredictor.cc diff --git a/PhysicsTools/MXNet/BuildFile.xml b/PhysicsTools/MXNet/BuildFile.xml new file mode 100644 index 0000000000000..6c7a5743d6a53 --- /dev/null +++ b/PhysicsTools/MXNet/BuildFile.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/PhysicsTools/MXNet/interface/MXNetPredictor.h b/PhysicsTools/MXNet/interface/MXNetPredictor.h new file mode 100644 index 0000000000000..3f54a967b405f --- /dev/null +++ b/PhysicsTools/MXNet/interface/MXNetPredictor.h @@ -0,0 +1,51 @@ +#ifndef PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H +#define PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H + +#include +#include +#include +#include "mxnet/c_predict_api.h" + +namespace mxnet{ +// Read file to buffer +class BufferFile { +public: + + explicit BufferFile(std::string file_path); + ~BufferFile() {} + + int GetLength() const { + return buffer_.length(); + } + const char* GetBuffer() const { + return buffer_.c_str(); + } + +private: + std::string file_path_; + std::string buffer_; +}; + +class MXNetPredictor { +public: + MXNetPredictor(); + virtual ~MXNetPredictor(); + + void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); + void load_model(const BufferFile* model_data, const BufferFile* param_data); + std::vector predict(const std::vector>& input_data, mx_uint output_index = 0); + +private: + mx_uint num_input_nodes_ = 0; + std::vector input_names_; + std::vector input_keys_; + + std::vector input_shapes_indices_; + std::vector input_shapes_data_; + + PredictorHandle pred_hnd_ = nullptr; +}; + +} + +#endif /* PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H */ diff --git a/PhysicsTools/MXNet/src/MXNetPredictor.cc b/PhysicsTools/MXNet/src/MXNetPredictor.cc new file mode 100644 index 0000000000000..61b0a4f3dd410 --- /dev/null +++ b/PhysicsTools/MXNet/src/MXNetPredictor.cc @@ -0,0 +1,123 @@ +#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" + +#include +#include +#include +#include +#include +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "FWCore/Utilities/interface/Exception.h" + +using namespace mxnet; + +BufferFile::BufferFile(std::string file_path) : file_path_(file_path) { + std::ifstream ifs(file_path.c_str(), std::ios::in | std::ios::binary); + if (!ifs) { + throw cms::Exception("InvalidArgument") << "Can't open the file. Please check " << file_path << ". \n"; + return; + } + + buffer_.assign(std::istreambuf_iterator(ifs), std::istreambuf_iterator()); + edm::LogInfo("NNKit") << file_path.c_str() << " ... "<< buffer_.length() << " bytes"; + ifs.close(); +} + + +MXNetPredictor::MXNetPredictor() { +} + +MXNetPredictor::~MXNetPredictor() { + if (pred_hnd_){ + // Release Predictor + MXPredFree(pred_hnd_); + } +} + +void MXNetPredictor::set_input_shapes(const std::vector& input_names, const std::vector >& input_shapes) { + assert(input_names.size() == input_shapes.size()); + + input_names_ = input_names; + num_input_nodes_ = input_names_.size(); + for (const auto &name : input_names_){ + input_keys_.push_back(name.c_str()); + } + + input_shapes_data_.clear(); + input_shapes_indices_ = {0}; + unsigned pos = 0; + for (const auto &shape : input_shapes){ + pos += shape.size(); + input_shapes_indices_.push_back(pos); + input_shapes_data_.insert(input_shapes_data_.end(), shape.begin(), shape.end()); + } +} + +void MXNetPredictor::load_model(const BufferFile* model_data, const BufferFile* param_data) { + if (model_data->GetLength() == 0 || + param_data->GetLength() == 0) { + throw cms::Exception("InvalidArgument") << "Invalid input"; + } + + std::stringstream msg; + msg << "input_shapes_indices_:\n"; + for (const auto &d : input_shapes_indices_) { + msg << d << ","; + } + msg << "\ninput_shapes_data_:\n"; + for (const auto &d : input_shapes_data_) { + msg << d << ","; + } + edm::LogInfo("NNKit") << msg.str(); + + // Create Predictor + int dev_type = 1; // 1: cpu, 2: gpu + int dev_id = 0; // arbitrary. + int status = MXPredCreate(model_data->GetBuffer(), + param_data->GetBuffer(), + param_data->GetLength(), + dev_type, + dev_id, + num_input_nodes_, + (const char**) input_keys_.data(), + input_shapes_indices_.data(), + input_shapes_data_.data(), + &pred_hnd_); + if (status != 0) + throw cms::Exception("RuntimeError") << "Cannot create predictor!"; +} + +std::vector MXNetPredictor::predict(const std::vector >& input_data, mx_uint output_index) { + assert(input_data.size() == input_names_.size()); + assert(pred_hnd_); + + // set input data + for (unsigned i=0; i prediction(size); + + if (MXPredGetOutput(pred_hnd_, output_index, &(prediction[0]), size) != 0){ + throw cms::Exception("RuntimeError") << "Error getting output values!"; + } + + return prediction; +} From 678c743e919c3c06250c02013c1b5090fc253e84 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 4 Jul 2018 01:35:03 +0200 Subject: [PATCH 02/31] DataFormats change for DeepAK8. --- .../interface/DeepBoostedJetFeatures.h | 52 +++++++++++++++++++ .../interface/DeepBoostedJetTagInfo.h | 15 ++++++ .../BTauReco/interface/DeepDoubleBTagInfo.h | 6 +-- .../BTauReco/interface/DeepFlavourTagInfo.h | 32 +----------- .../BTauReco/interface/FeaturesTagInfo.h | 40 ++++++++++++++ DataFormats/BTauReco/src/classes.h | 11 +++- DataFormats/BTauReco/src/classes_def.xml | 12 +++++ 7 files changed, 131 insertions(+), 37 deletions(-) create mode 100644 DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h create mode 100644 DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h create mode 100644 DataFormats/BTauReco/interface/FeaturesTagInfo.h diff --git a/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h new file mode 100644 index 0000000000000..e7173b563c297 --- /dev/null +++ b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h @@ -0,0 +1,52 @@ +#ifndef DataFormats_BTauReco_DeepBoostedJetFeatures_h +#define DataFormats_BTauReco_DeepBoostedJetFeatures_h + +#include +#include +#include +#include "FWCore/Utilities/interface/Exception.h" + +namespace btagbtvdeep { + +class DeepBoostedJetFeatures { + +public: + + bool empty() const { + return is_empty_; + } + + void add(const std::string& name){ + feature_map_[name]; + } + + void fill(const std::string& name, float value){ + try { + feature_map_.at(name).push_back(value); + is_empty_ = false; + }catch (const std::out_of_range& e) { + throw cms::Exception("InvalidArgument") << "[DeepBoostedJetFeatures::fill()] Feature " << name << " has not been registered"; + } + } + + void set(const std::string& name, const std::vector& vec){ + feature_map_[name] = vec; + } + + const std::vector& get(const std::string& name) const { + try { + return feature_map_.at(name); + }catch (const std::out_of_range& e) { + throw cms::Exception("InvalidArgument") << "[DeepBoostedJetFeatures::get()] Feature " << name << " does not exist!"; + } + } + +private: + bool is_empty_ = true; + std::unordered_map> feature_map_; + +}; + +} + +#endif // DataFormats_BTauReco_DeepBoostedJetFeatures_h diff --git a/DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h b/DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h new file mode 100644 index 0000000000000..09130f569de6d --- /dev/null +++ b/DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h @@ -0,0 +1,15 @@ +#ifndef DataFormats_BTauReco_DeepBoostedJetTagInfo_h +#define DataFormats_BTauReco_DeepBoostedJetTagInfo_h + +#include "DataFormats/BTauReco/interface/FeaturesTagInfo.h" +#include "DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h" + +namespace reco { + +typedef FeaturesTagInfo DeepBoostedJetTagInfo; + +DECLARE_EDM_REFS( DeepBoostedJetTagInfo ) + +} + +#endif // DataFormats_BTauReco_DeepBoostedJetTagInfo_h diff --git a/DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h b/DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h index 43b5ea81a5d1d..70ec4aa9941f0 100644 --- a/DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h +++ b/DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h @@ -1,13 +1,9 @@ #ifndef DataFormats_BTauReco_DeepDoubleBTagInfo_h #define DataFormats_BTauReco_DeepDoubleBTagInfo_h -#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h" -#include "DataFormats/BTauReco/interface/BaseTagInfo.h" -#include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" +#include "DataFormats/BTauReco/interface/FeaturesTagInfo.h" #include "DataFormats/BTauReco/interface/DeepDoubleBFeatures.h" -#include "DataFormats/PatCandidates/interface/Jet.h" - namespace reco { typedef FeaturesTagInfo DeepDoubleBTagInfo; diff --git a/DataFormats/BTauReco/interface/DeepFlavourTagInfo.h b/DataFormats/BTauReco/interface/DeepFlavourTagInfo.h index 5d148669f77ea..abd853ac702e8 100644 --- a/DataFormats/BTauReco/interface/DeepFlavourTagInfo.h +++ b/DataFormats/BTauReco/interface/DeepFlavourTagInfo.h @@ -1,41 +1,11 @@ #ifndef DataFormats_BTauReco_DeepFlavourTagInfo_h #define DataFormats_BTauReco_DeepFlavourTagInfo_h -#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h" -#include "DataFormats/BTauReco/interface/BaseTagInfo.h" #include "DataFormats/BTauReco/interface/DeepFlavourFeatures.h" - -#include "DataFormats/PatCandidates/interface/Jet.h" +#include "DataFormats/BTauReco/interface/FeaturesTagInfo.h" namespace reco { -template class FeaturesTagInfo : public BaseTagInfo { - - public: - - FeaturesTagInfo() {} - - FeaturesTagInfo(const Features & features, - const edm::RefToBase & jet_ref) : - features_(features), - jet_ref_(jet_ref) {} - - edm::RefToBase jet() const override { return jet_ref_; } - - const Features & features() const { return features_; } - - ~FeaturesTagInfo() override {} - // without overidding clone from base class will be store/retrieved - FeaturesTagInfo* clone(void) const override { return new FeaturesTagInfo(*this); } - - - CMS_CLASS_VERSION(3) - - private: - Features features_; - edm::RefToBase jet_ref_; -}; - typedef FeaturesTagInfo DeepFlavourTagInfo; DECLARE_EDM_REFS( DeepFlavourTagInfo ) diff --git a/DataFormats/BTauReco/interface/FeaturesTagInfo.h b/DataFormats/BTauReco/interface/FeaturesTagInfo.h new file mode 100644 index 0000000000000..152705920fe61 --- /dev/null +++ b/DataFormats/BTauReco/interface/FeaturesTagInfo.h @@ -0,0 +1,40 @@ +#ifndef DataFormats_BTauReco_FeaturesTagInfo_h +#define DataFormats_BTauReco_FeaturesTagInfo_h + +#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h" +#include "DataFormats/BTauReco/interface/BaseTagInfo.h" + +#include "DataFormats/PatCandidates/interface/Jet.h" + +namespace reco { + +template class FeaturesTagInfo : public BaseTagInfo { + + public: + + FeaturesTagInfo() {} + + FeaturesTagInfo(const Features & features, + const edm::RefToBase & jet_ref) : + features_(features), + jet_ref_(jet_ref) {} + + edm::RefToBase jet() const override { return jet_ref_; } + + const Features & features() const { return features_; } + + ~FeaturesTagInfo() override {} + // without overidding clone from base class will be store/retrieved + FeaturesTagInfo* clone(void) const override { return new FeaturesTagInfo(*this); } + + + CMS_CLASS_VERSION(3) + + private: + Features features_; + edm::RefToBase jet_ref_; +}; + +} + +#endif // DataFormats_BTauReco_FeaturesTagInfo_h diff --git a/DataFormats/BTauReco/src/classes.h b/DataFormats/BTauReco/src/classes.h index 4b61dbdefd843..2d35f90389e99 100755 --- a/DataFormats/BTauReco/src/classes.h +++ b/DataFormats/BTauReco/src/classes.h @@ -56,6 +56,7 @@ #include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" #include "DataFormats/BTauReco/interface/DeepDoubleBFeatures.h" #include "DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h" +#include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" namespace reco { @@ -289,7 +290,7 @@ namespace DataFormats_BTauReco { reco::HTTTopJetTagInfoRefVector htttopjet_rv; edm::Wrapper htttopjet_wc; edm::reftobase::Holder rb_htttopjet; - edm::reftobase::RefHolder rbh_htttopjet; + edm::reftobase::RefHolder rbh_htttopjet; std::vector vm1d; @@ -427,5 +428,13 @@ namespace DataFormats_BTauReco { reco::DeepDoubleBTagInfoRefVector deep_doubleb_tag_info_collection_ref_vector; edm::Wrapper deep_doubleb_tag_info_collection_edm_wrapper; + btagbtvdeep::DeepBoostedJetFeatures deep_boosted_jet_tag_info_features; + reco::DeepBoostedJetTagInfo deep_boosted_jet_tag_info; + reco::DeepBoostedJetTagInfoCollection deep_boosted_jet_tag_info_collection; + reco::DeepBoostedJetTagInfoRef deep_boosted_jet_tag_info_collection_ref; + reco::DeepBoostedJetTagInfoFwdRef deep_boosted_jet_tag_info_collection_fwd_ref; + reco::DeepBoostedJetTagInfoRefProd deep_boosted_jet_tag_info_collection_ref_prod; + reco::DeepBoostedJetTagInfoRefVector deep_boosted_jet_tag_info_collection_ref_vector; + edm::Wrapper deep_boosted_jet_tag_info_collection_edm_wrapper; }; } diff --git a/DataFormats/BTauReco/src/classes_def.xml b/DataFormats/BTauReco/src/classes_def.xml index 7104ce5b8b978..dd4e72bdcade0 100644 --- a/DataFormats/BTauReco/src/classes_def.xml +++ b/DataFormats/BTauReco/src/classes_def.xml @@ -471,4 +471,16 @@ + + + + + + + + + + + + From 55b6cd7c3d82d1d218f83f76329004e042c88f6e Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 4 Jul 2018 01:50:35 +0200 Subject: [PATCH 03/31] Add DeepAK8 tagger. - Both the nominal and the decorrelated versions are included, as well as a few meta taggers (aggregating the scores). - Currently it supports only updating a jet collection. The implementation to run on RECO exists but not tested. --- .../python/recoLayer0/bTagging_cff.py | 50 +- .../python/slimming/applyDeepBtagging_cff.py | 61 +- .../PatAlgos/python/tools/jetTools.py | 25 + RecoBTag/Configuration/python/RecoBTag_cff.py | 1 + RecoBTag/DeepBoostedJet/BuildFile.xml | 4 + RecoBTag/DeepBoostedJet/plugins/BuildFile.xml | 12 + .../plugins/DeepBoostedJetTagInfoProducer.cc | 539 ++++++++++++++++++ .../plugins/DeepBoostedJetTagsProducer.cc | 332 +++++++++++ .../pfDeepBoostedDiscriminatorsJetTags_cfi.py | 39 ++ .../pfDeepBoostedJetPreprocessParams_cfi.py | 309 ++++++++++ .../python/pfDeepBoostedJetTags_cfi.py | 27 + .../python/pfDeepBoostedJet_cff.py | 31 + ...tedDeepBoostedDiscriminatorsJetTags_cfi.py | 136 +++++ .../test/test_deep_boosted_jet_cfg.py | 125 ++++ RecoBTag/TensorFlow/interface/deep_helpers.h | 37 +- .../src/ChargedCandidateConverter.cc | 65 +-- RecoBTag/TensorFlow/src/deep_helpers.cc | 85 ++- 17 files changed, 1791 insertions(+), 87 deletions(-) create mode 100644 RecoBTag/DeepBoostedJet/BuildFile.xml create mode 100644 RecoBTag/DeepBoostedJet/plugins/BuildFile.xml create mode 100644 RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc create mode 100644 RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc create mode 100644 RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py create mode 100644 RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py create mode 100644 RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetTags_cfi.py create mode 100644 RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py create mode 100644 RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py create mode 100644 RecoBTag/DeepBoostedJet/test/test_deep_boosted_jet_cfg.py diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index b00a257acbee6..83c74f738b306 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -43,6 +43,8 @@ , 'pfNegativeDeepFlavourTagInfos' # DeepDoubleB tag infos , 'pfDeepDoubleBTagInfos' + # DeepBoostedJet tag infos + , 'pfDeepBoostedJetTagInfos' ] # extend for "internal use" in PAT/MINIAOD (renaming) supportedBtagInfos.append( 'caTopTagInfosPAT' ) @@ -189,6 +191,42 @@ , 'pfNegativeDeepFlavourJetTags:probg' : [["pfNegativeDeepFlavourTagInfos"], ['pfDeepCSVNegativeTagInfos', "pfImpactParameterTagInfos", 'pfInclusiveSecondaryVertexFinderNegativeTagInfos']] , 'pfDeepDoubleBJetTags:probQ' : [["pfDeepDoubleBTagInfos"], ['pfBoostedDoubleSVAK8TagInfos', "pfImpactParameterAK8TagInfos", 'pfInclusiveSecondaryVertexFinderAK8TagInfos']] , 'pfDeepDoubleBJetTags:probH' : [["pfDeepDoubleBTagInfos"], ['pfBoostedDoubleSVAK8TagInfos', "pfImpactParameterAK8TagInfos", 'pfInclusiveSecondaryVertexFinderAK8TagInfos']] + # DeepBoostedJetTagging + , 'pfDeepBoostedJetTags:probTbcq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probTbqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probTbc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probTbq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probWcq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probWqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probZbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probZcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probZqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probHbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probHcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probHqqqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probQCDbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probQCDcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probQCDb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probQCDc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfDeepBoostedJetTags:probQCDothers' : [["pfDeepBoostedJetTagInfos"]] + # DeepBoostedJetTagging - Mass-decorrelated + , 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probTbc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probTbq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probWcq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probWqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probZbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probZcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probZqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probHbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probHcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc' : [["pfDeepBoostedJetTagInfos"]] + , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers' : [["pfDeepBoostedJetTagInfos"]] } #meta-taggers are simple arithmetic on top of other taggers, they are stored here @@ -199,6 +237,16 @@ 'pfDeepCSVDiscriminatorsJetTags:CvsL' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], 'pfDeepCMVADiscriminatorsJetTags:BvsAll' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], 'pfDeepCMVADiscriminatorsJetTags:CvsB' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], - 'pfDeepCMVADiscriminatorsJetTags:CvsL' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], + 'pfDeepCMVADiscriminatorsJetTags:CvsL' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], + 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD' : ['pfDeepBoostedJetTags:probTbcq', 'pfDeepBoostedJetTags:probTbqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'], + 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD' : ['pfDeepBoostedJetTags:probWcq', 'pfDeepBoostedJetTags:probWqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probTbcq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probWcq', 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvscc' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc'], + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsZHcc': ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc'], } diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index 35f5827a958d1..b21352852ba5f 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -46,10 +46,10 @@ def applyDeepBtagging( process, postfix="" ) : # update slimmed jets to include DeepFlavour (keep same name) # make clone for DeepDoubleB-less slimmed AK8 jets, so output name is preserved - addToProcessAndTask('slimmedJetsAK8NoDeepDoubleB', process.slimmedJetsAK8.clone(), process, task) + addToProcessAndTask('slimmedJetsAK8NoDeepTags', process.slimmedJetsAK8.clone(), process, task) updateJetCollection( process, - jetSource = cms.InputTag('slimmedJetsAK8NoDeepDoubleB'), + jetSource = cms.InputTag('slimmedJetsAK8NoDeepTags'), # updateJetCollection defaults to MiniAOD inputs but # here it is made explicit (as in training or MINIAOD redoing) pvSource = cms.InputTag('offlineSlimmedPrimaryVertices'), @@ -58,19 +58,66 @@ def applyDeepBtagging( process, postfix="" ) : muSource = cms.InputTag('slimmedMuons'), elSource = cms.InputTag('slimmedElectrons'), rParam = 0.8, - jetCorrections = ('AK8PFPuppi', cms.vstring(['L1FastJet', 'L2Relative', 'L3Absolute']), 'None'), + jetCorrections = ('AK8PFPuppi', cms.vstring(['L2Relative', 'L3Absolute']), 'None'), btagDiscriminators = [ 'pfDeepDoubleBJetTags:probQ', 'pfDeepDoubleBJetTags:probH', + + # DeepBoostedJet (Nominal) + 'pfDeepBoostedJetTags:probTbcq', + 'pfDeepBoostedJetTags:probTbqq', + 'pfDeepBoostedJetTags:probTbc', + 'pfDeepBoostedJetTags:probTbq', + 'pfDeepBoostedJetTags:probWcq', + 'pfDeepBoostedJetTags:probWqq', + 'pfDeepBoostedJetTags:probZbb', + 'pfDeepBoostedJetTags:probZcc', + 'pfDeepBoostedJetTags:probZqq', + 'pfDeepBoostedJetTags:probHbb', + 'pfDeepBoostedJetTags:probHcc', + 'pfDeepBoostedJetTags:probHqqqq', + 'pfDeepBoostedJetTags:probQCDbb', + 'pfDeepBoostedJetTags:probQCDcc', + 'pfDeepBoostedJetTags:probQCDb', + 'pfDeepBoostedJetTags:probQCDc', + 'pfDeepBoostedJetTags:probQCDothers', + # meta taggers + 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD', + 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD', + + # DeepBoostedJet (mass decorrelated) + 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', + 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', + 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers', + # meta taggers + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD', + ], - postfix = 'SlimmedAK8DeepDoubleB'+postfix, + postfix = 'SlimmedAK8DeepTags'+postfix, printWarning = False ) # slimmedJetsAK8 with DeepDoubleB (remove DeepDoubleB-less) delattr(process, 'slimmedJetsAK8') - addToProcessAndTask('slimmedJetsAK8', getattr(process,'selectedUpdatedPatJetsSlimmedAK8DeepDoubleB'+postfix).clone(), process, task) + addToProcessAndTask('slimmedJetsAK8', getattr(process,'selectedUpdatedPatJetsSlimmedAK8DeepTags'+postfix).clone(), process, task) # delete module not used anymore (slimmedJetsAK8 substitutes) - delattr(process, 'selectedUpdatedPatJetsSlimmedAK8DeepDoubleB'+postfix) - + delattr(process, 'selectedUpdatedPatJetsSlimmedAK8DeepTags'+postfix) + diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index 6e01f81f45b6b..acf4d2dd20e62 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -630,6 +630,31 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou ), process, task) + if btagInfo == 'pfDeepBoostedJetTagInfos': + jetSrcName = jetSource.value().lower() + if 'slimmed' in jetSrcName or 'updated' in jetSrcName: + # running on slimmedJetsAK8 or re-applying btag on MiniAOD + updateJetCollection = cms.bool(True) + puppi_value_map = cms.InputTag("") + vertex_associator = cms.InputTag("") + subjets = cms.InputTag("") + else: + updateJetCollection = cms.bool(False) + puppi_value_map = cms.InputTag("puppi") + vertex_associator = cms.InputTag("primaryVertexAssociation","original") + subjets = cms.InputTag(jetSource.value().replace("Puppi", "PFPuppiSoftDrop")) + addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix, + btag.pfDeepBoostedJetTagInfos.clone( + jets = jetSource, + subjets = subjets, + vertices = pvSource, + secondary_vertices = svSource, + updateJetCollection = updateJetCollection, + puppi_value_map = puppi_value_map, + vertex_associator = vertex_associator, + ), + process, task) + acceptedTagInfos.append(btagInfo) elif hasattr(toptag, btagInfo) : acceptedTagInfos.append(btagInfo) diff --git a/RecoBTag/Configuration/python/RecoBTag_cff.py b/RecoBTag/Configuration/python/RecoBTag_cff.py index ed49d72c279ef..98ffb4e043ba0 100644 --- a/RecoBTag/Configuration/python/RecoBTag_cff.py +++ b/RecoBTag/Configuration/python/RecoBTag_cff.py @@ -8,6 +8,7 @@ from RecoBTag.CTagging.RecoCTagging_cff import * from RecoBTag.Combined.deepFlavour_cff import * from RecoBTag.TensorFlow.pfDeepFlavour_cff import * +from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import * from RecoVertex.AdaptiveVertexFinder.inclusiveVertexing_cff import * legacyBTaggingTask = cms.Task( diff --git a/RecoBTag/DeepBoostedJet/BuildFile.xml b/RecoBTag/DeepBoostedJet/BuildFile.xml new file mode 100644 index 0000000000000..37051249a2261 --- /dev/null +++ b/RecoBTag/DeepBoostedJet/BuildFile.xml @@ -0,0 +1,4 @@ + + + + diff --git a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml new file mode 100644 index 0000000000000..1a293383576fd --- /dev/null +++ b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc new file mode 100644 index 0000000000000..6e174bc2d3c76 --- /dev/null +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -0,0 +1,539 @@ +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/stream/EDProducer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/StreamID.h" + +#include "DataFormats/Candidate/interface/Candidate.h" + +#include "DataFormats/PatCandidates/interface/Jet.h" +#include "DataFormats/PatCandidates/interface/PackedCandidate.h" + +#include "TrackingTools/Records/interface/TransientTrackRecord.h" +#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" +#include "RecoBTag/TensorFlow/interface/deep_helpers.h" + +#include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" +#include "DataFormats/VertexReco/interface/VertexFwd.h" + +#include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" + +using namespace btagbtvdeep; + +class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> +{ + +public: + explicit DeepBoostedJetTagInfoProducer(const edm::ParameterSet&); + ~DeepBoostedJetTagInfoProducer() override; + + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); + +private: + typedef std::vector DeepBoostedJetTagInfoCollection; + typedef reco::VertexCompositePtrCandidateCollection SVCollection; + typedef reco::VertexCollection VertexCollection; + + void beginStream(edm::StreamID) override {} + void produce(edm::Event&, const edm::EventSetup&) override; + void endStream() override{} + + void fillParticleFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet); + void fillSVFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet); + + const bool update_jets_; + const double jet_radius_; + const double min_jet_pt_; + const double min_pt_for_track_properties_; + + edm::EDGetTokenT> jet_token_; + edm::EDGetTokenT> sdjet_token_; + edm::EDGetTokenT vtx_token_; + edm::EDGetTokenT sv_token_; + + bool use_puppi_value_map_; + bool use_pvasq_value_map_; + bool use_subjet_collection_; + + edm::EDGetTokenT> puppi_value_map_token_; + edm::EDGetTokenT> pvasq_value_map_token_; + edm::EDGetTokenT> pvas_token_; + + edm::Handle> sdjets; + edm::Handle vtxs; + edm::Handle svs; + edm::ESHandle track_builder; + edm::Handle> puppi_value_map; + edm::Handle> pvasq_value_map; + edm::Handle> pvas; + + std::vector feature_names_; + const reco::Vertex *pv_ = nullptr; +}; + +DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::ParameterSet& iConfig) +: update_jets_(iConfig.getParameter("updateJetCollection")) +, jet_radius_(iConfig.getParameter("jet_radius")) +, min_jet_pt_(iConfig.getParameter("min_jet_pt")) +, min_pt_for_track_properties_(iConfig.getParameter("minPtForTrackProperties")) +, jet_token_(consumes>(iConfig.getParameter("jets"))) +, vtx_token_(consumes(iConfig.getParameter("vertices"))) +, sv_token_(consumes(iConfig.getParameter("secondary_vertices"))) +, use_puppi_value_map_(false) +, use_pvasq_value_map_(false) +, use_subjet_collection_(false) +, feature_names_(iConfig.getParameter>("feature_names")) +{ + + const auto & puppi_value_map_tag = iConfig.getParameter("puppi_value_map"); + if (!puppi_value_map_tag.label().empty()) { + puppi_value_map_token_ = consumes>(puppi_value_map_tag); + use_puppi_value_map_ = true; + } + + const auto & pvas_tag = iConfig.getParameter("vertex_associator"); + if (!pvas_tag.label().empty()) { + pvasq_value_map_token_ = consumes>(pvas_tag); + pvas_token_ = consumes>(pvas_tag); + use_pvasq_value_map_ = true; + } + + const auto & subjet_tag = iConfig.getParameter("subjets"); + if (!subjet_tag.label().empty()) { + sdjet_token_ = consumes>(subjet_tag); + use_subjet_collection_ = true; + } + + produces(); + +} + +DeepBoostedJetTagInfoProducer::~DeepBoostedJetTagInfoProducer() +{ +} + +void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) +{ + // pfDeepBoostedJetTagInfos + edm::ParameterSetDescription desc; + // updateJetCollection + // set to true if applying on existing jet collections (whose daughters are *not* puppi weighted) + // set to false if the jet collection is (re)clustered (whose daughters are puppi weighted) + desc.add("updateJetCollection", true); + desc.add("jet_radius", 0.8); + desc.add("min_jet_pt", 170); + desc.add("minPtForTrackProperties", -1); + desc.add("vertices", edm::InputTag("offlinePrimaryVertices")); + desc.add("secondary_vertices", edm::InputTag("inclusiveCandidateSecondaryVertices")); + desc.add("jets", edm::InputTag("slimmedJetsAK8")); + desc.add("subjets", edm::InputTag("slimmedJetsAK8PFPuppiSoftDropPacked")); + desc.add("puppi_value_map", edm::InputTag("puppi")); + desc.add("vertex_associator", edm::InputTag("primaryVertexAssociation","original")); + desc.add>("feature_names", std::vector{ + "pfcand_puppiw", + "pfcand_hcalFrac", + "pfcand_VTX_ass", + "pfcand_lostInnerHits", + "pfcand_quality", + "pfcand_charge", + "pfcand_isEl", + "pfcand_isMu", + "pfcand_isChargedHad", + "pfcand_isGamma", + "pfcand_isNeutralHad", + "pfcand_pt", + "pfcand_ptrel", + "pfcand_erel", + "pfcand_phirel", + "pfcand_etarel", + "pfcand_deltaR", + "pfcand_abseta", + "pfcand_ptrel_log", + "pfcand_erel_log", + "pfcand_pt_log", + "pfcand_drminsv", + "pfcand_drsubjet1", + "pfcand_drsubjet2", + "pfcand_normchi2", + "pfcand_dz", + "pfcand_dzsig", + "pfcand_dxy", + "pfcand_dxysig", + "pfcand_dptdpt", + "pfcand_detadeta", + "pfcand_dphidphi", + "pfcand_dxydxy", + "pfcand_dzdz", + "pfcand_dxydz", + "pfcand_dphidxy", + "pfcand_dlambdadz", + "pfcand_btagMomentum", + "pfcand_btagEta", + "pfcand_btagEtaRel", + "pfcand_btagPtRel", + "pfcand_btagPPar", + "pfcand_btagDeltaR", + "pfcand_btagPtRatio", + "pfcand_btagPParRatio", + "pfcand_btagSip2dVal", + "pfcand_btagSip2dSig", + "pfcand_btagSip3dVal", + "pfcand_btagSip3dSig", + "pfcand_btagJetDistVal", + "sv_ptrel", + "sv_erel", + "sv_phirel", + "sv_etarel", + "sv_deltaR", + "sv_pt", + "sv_abseta", + "sv_mass", + "sv_ptrel_log", + "sv_erel_log", + "sv_pt_log", + "sv_ntracks", + "sv_chi2", + "sv_ndf", + "sv_normchi2", + "sv_dxy", + "sv_dxyerr", + "sv_dxysig", + "sv_d3d", + "sv_d3derr", + "sv_d3dsig", + "sv_costhetasvpv", + }); + descriptions.add("pfDeepBoostedJetTagInfos", desc); +} + +void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) +{ + + auto output_tag_infos = std::make_unique(); + + edm::Handle> jets; + iEvent.getByToken(jet_token_, jets); + + iEvent.getByToken(vtx_token_, vtxs); + if (vtxs->empty()){ + // produce empty TagInfos in case no primary vertex + iEvent.put(std::move(output_tag_infos)); + return; // exit event + } + // primary vertex + pv_ = &vtxs->at(0); + + iEvent.getByToken(sv_token_, svs); + + iSetup.get().get("TransientTrackBuilder", track_builder); + + if (use_puppi_value_map_) { + iEvent.getByToken(puppi_value_map_token_, puppi_value_map); + } + + if (use_pvasq_value_map_) { + iEvent.getByToken(pvasq_value_map_token_, pvasq_value_map); + iEvent.getByToken(pvas_token_, pvas); + } + + if (use_subjet_collection_) { + iEvent.getByToken(sdjet_token_, sdjets); + } + + for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ + + // reco jet reference (use as much as possible) + const auto& jet = jets->at(jet_n); + edm::RefToBase jet_ref(jets, jet_n); + + // create jet features + DeepBoostedJetFeatures features; + // declare all the feature variables (init as empty vector) + for (const auto &name : feature_names_) features.add(name); + // fill only if above pt threshold + if (jet.pt() > min_jet_pt_){ + fillParticleFeatures(features, jet); + fillSVFeatures(features, jet); + } + + output_tag_infos->emplace_back(features, jet_ref); + } + + iEvent.put(std::move(output_tag_infos)); +} + +void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet){ + + // stuff required for dealing with pf candidates + math::XYZVector jet_dir = jet.momentum().Unit(); + GlobalVector jet_ref_track_dir(jet.px(), jet.py(), jet.pz()); + const float etasign = jet.eta()>0 ? 1 : -1; + + auto puppiWgt = [&](reco::CandidatePtr cand){ + float puppiw = 1; // fall back value + const edm::Ptr packed_cand(cand); + if (packed_cand.isNonnull()) { puppiw = packed_cand->puppiWeight(); } + else{ + if (use_puppi_value_map_){ puppiw = (*puppi_value_map)[cand]; } + else { throw edm::Exception(edm::errors::InvalidReference) << "PUPPI value map is missing"; } + } + return puppiw; + }; + + std::vector daughters; + for (auto cand : jet.daughterPtrVector()){ + // remove particles w/ extremely low puppi weights + if ((puppiWgt(cand)) < 0.01) continue; + daughters.push_back(cand); + } + // sort by (Puppi-weighted) pt + if (update_jets_) { + // updating jet collection: + // linked daughters here are the original PackedCandidates + // need to scale the p4 with their puppi weights + std::sort(daughters.begin(), daughters.end(), [&](const reco::CandidatePtr a, const reco::CandidatePtr b){ return puppiWgt(a)*a->pt() > puppiWgt(b)*b->pt(); }); + }else{ + std::sort(daughters.begin(), daughters.end(), [](const reco::CandidatePtr a, const reco::CandidatePtr b){ return a->pt() > b->pt(); }); + } + + auto useTrackProperties = [&](const edm::Ptr reco_cand) { + if (reco_cand.isNull()) return false; + const auto* trk = reco_cand->bestTrack(); + return trk!=nullptr && trk->pt()>min_pt_for_track_properties_; + }; + + for (const auto cand : daughters){ + const edm::Ptr packed_cand(cand); + const edm::Ptr reco_cand(cand); + + auto puppiP4 = cand->p4(); + if (packed_cand.isNonnull()){ + if (update_jets_) { + // updating jet collection: + // linked daughters here are the original PackedCandidates + // need to scale the p4 with their puppi weights + puppiP4 *= packed_cand->puppiWeight(); + } + + fts.fill("pfcand_puppiw", packed_cand->puppiWeight()); + fts.fill("pfcand_hcalFrac", packed_cand->hcalFraction()); + fts.fill("pfcand_VTX_ass", packed_cand->pvAssociationQuality()); + fts.fill("pfcand_lostInnerHits", packed_cand->lostInnerHits()); + fts.fill("pfcand_quality", packed_cand->bestTrack() ? packed_cand->bestTrack()->qualityMask() : 0); + + fts.fill("pfcand_charge", packed_cand->charge()); + fts.fill("pfcand_isEl", std::abs(packed_cand->pdgId())==11); + fts.fill("pfcand_isMu", std::abs(packed_cand->pdgId())==13); + fts.fill("pfcand_isChargedHad", std::abs(packed_cand->pdgId())==211); + fts.fill("pfcand_isGamma", std::abs(packed_cand->pdgId())==22); + fts.fill("pfcand_isNeutralHad", std::abs(packed_cand->pdgId())==130); + + // impact parameters + fts.fill("pfcand_dz", catch_infs(packed_cand->dz())); + fts.fill("pfcand_dzsig", packed_cand->bestTrack() ? catch_infs(packed_cand->dz()/packed_cand->dzError()) : 0); + fts.fill("pfcand_dxy", catch_infs(packed_cand->dxy())); + fts.fill("pfcand_dxysig", packed_cand->bestTrack() ? catch_infs(packed_cand->dxy()/packed_cand->dxyError()) : 0); + + } else if(reco_cand.isNonnull()) { + // get vertex association quality + int pv_ass_quality = 0; // fallback value + float vtx_ass = 0; + if (use_pvasq_value_map_) { + pv_ass_quality = (*pvasq_value_map)[reco_cand]; + const reco::VertexRef & PV_orig = (*pvas)[reco_cand]; + vtx_ass = vtx_ass_from_pfcand(*reco_cand, pv_ass_quality, PV_orig); + } else { + throw edm::Exception(edm::errors::InvalidReference) << "Vertex association missing"; + } + + fts.fill("pfcand_puppiw", puppiWgt(cand)); + fts.fill("pfcand_hcalFrac", reco_cand->hcalEnergy()/(reco_cand->ecalEnergy()+reco_cand->hcalEnergy())); + fts.fill("pfcand_VTX_ass", vtx_ass); + fts.fill("pfcand_lostInnerHits", useTrackProperties(reco_cand) ? lost_inner_hits_from_pfcand(*reco_cand) : 0); + fts.fill("pfcand_quality", useTrackProperties(reco_cand) ? quality_from_pfcand(*reco_cand) : 0); + + fts.fill("pfcand_charge", reco_cand->charge()); + fts.fill("pfcand_isEl", std::abs(reco_cand->pdgId())==11); + fts.fill("pfcand_isMu", std::abs(reco_cand->pdgId())==13); + fts.fill("pfcand_isChargedHad", std::abs(reco_cand->pdgId())==211); + fts.fill("pfcand_isGamma", std::abs(reco_cand->pdgId())==22); + fts.fill("pfcand_isNeutralHad", std::abs(reco_cand->pdgId())==130); + + // impact parameters + const auto* trk = reco_cand->bestTrack(); + float dz = trk ? trk->dz(pv_->position()) : 0; + float dxy = trk ? trk->dxy(pv_->position()) : 0; + fts.fill("pfcand_dz", catch_infs(dz)); + fts.fill("pfcand_dzsig", trk ? catch_infs(dz/trk->dzError()) : 0); + fts.fill("pfcand_dxy", catch_infs(dxy)); + fts.fill("pfcand_dxysig", trk ? catch_infs(dxy/trk->dxyError()) : 0); + + }else { + throw edm::Exception(edm::errors::InvalidReference) << "Cannot convert reco::Candidate to either pat::PackedCandidate or reco::PFCandidate"; + } + + // basic kinematics, valid for both charged and neutral + fts.fill("pfcand_pt", puppiP4.pt()); + fts.fill("pfcand_ptrel", puppiP4.pt()/jet.pt()); + fts.fill("pfcand_erel", puppiP4.energy()/jet.energy()); + fts.fill("pfcand_phirel", reco::deltaPhi(puppiP4, jet)); + fts.fill("pfcand_etarel", etasign * (puppiP4.eta() - jet.eta())); + fts.fill("pfcand_deltaR", reco::deltaR(puppiP4, jet)); + fts.fill("pfcand_abseta", std::abs(puppiP4.eta())); + + fts.fill("pfcand_ptrel_log", catch_infs(std::log(puppiP4.pt()/jet.pt()), -99)); + fts.fill("pfcand_erel_log", catch_infs(std::log(puppiP4.energy()/jet.energy()), -99)); + fts.fill("pfcand_pt_log", catch_infs(std::log(puppiP4.pt()), -99)); + + double minDR = 999; + for (const auto &sv : *svs){ + double dr = reco::deltaR(*cand, sv); + if (dr < minDR) minDR = dr; + } + fts.fill("pfcand_drminsv", minDR==999 ? -1 : minDR); + + std::vector> subjets; + if (use_subjet_collection_){ + for (const auto &sj : *sdjets) { + // sdjets stores the soft-drop AK8 jets, with the actual subjets stored as daughters + // PhysicsTools/PatAlgos/python/slimming/applySubstructure_cff.py + // PhysicsTools/PatUtils/plugins/JetSubstructurePacker.cc + if (reco::deltaR(sj, jet) < jet_radius_) { + for ( size_t ida = 0; ida < sj.numberOfDaughters(); ++ida ) { + auto candPtr = sj.daughterPtr(ida); + subjets.emplace_back(candPtr); + } + break; + } + } + }else { + try { + const auto &patJet = dynamic_cast(jet); + for (auto sj : patJet.subjets()) { subjets.emplace_back(sj); } + }catch (const std::bad_cast &e) { + throw edm::Exception(edm::errors::InvalidReference) << "Cannot access subjets because this is not a pat::Jet."; + } + } + + fts.fill("pfcand_drsubjet1", subjets.size()>0 ? reco::deltaR(*cand, *subjets.at(0)) : -1); + fts.fill("pfcand_drsubjet2", subjets.size()>1 ? reco::deltaR(*cand, *subjets.at(1)) : -1); + + const reco::Track *trk = nullptr; + if (packed_cand.isNonnull()) { trk = packed_cand->bestTrack(); } + else if (useTrackProperties(reco_cand)) { trk= reco_cand->bestTrack(); } + if (trk){ + fts.fill("pfcand_normchi2", catch_infs(std::floor(trk->normalizedChi2()))); + + // track covariance + auto cov = [&](unsigned i, unsigned j) { + return catch_infs(trk->covariance(i, j)); + }; + fts.fill("pfcand_dptdpt", cov(0,0)); + fts.fill("pfcand_detadeta", cov(1,1)); + fts.fill("pfcand_dphidphi", cov(2,2)); + fts.fill("pfcand_dxydxy", cov(3,3)); + fts.fill("pfcand_dzdz", cov(4,4)); + fts.fill("pfcand_dxydz", cov(3,4)); + fts.fill("pfcand_dphidxy", cov(2,3)); + fts.fill("pfcand_dlambdadz", cov(1,4)); + + TrackInfoBuilder trkinfo(track_builder); + trkinfo.buildTrackInfo(&(*cand), jet_dir, jet_ref_track_dir, *pv_); + fts.fill("pfcand_btagMomentum", trkinfo.getTrackMomentum()); + fts.fill("pfcand_btagEta", trkinfo.getTrackEta()); + fts.fill("pfcand_btagEtaRel", trkinfo.getTrackEtaRel()); + fts.fill("pfcand_btagPtRel", trkinfo.getTrackPtRel()); + fts.fill("pfcand_btagPPar", trkinfo.getTrackPPar()); + fts.fill("pfcand_btagDeltaR", trkinfo.getTrackDeltaR()); + fts.fill("pfcand_btagPtRatio", trkinfo.getTrackPtRatio()); + fts.fill("pfcand_btagPParRatio", trkinfo.getTrackPParRatio()); + fts.fill("pfcand_btagSip2dVal", trkinfo.getTrackSip2dVal()); + fts.fill("pfcand_btagSip2dSig", trkinfo.getTrackSip2dSig()); + fts.fill("pfcand_btagSip3dVal", trkinfo.getTrackSip3dVal()); + fts.fill("pfcand_btagSip3dSig", trkinfo.getTrackSip3dSig()); + fts.fill("pfcand_btagJetDistVal", trkinfo.getTrackJetDistVal()); + }else{ + fts.fill("pfcand_normchi2", 999); + + fts.fill("pfcand_dptdpt", 0); + fts.fill("pfcand_detadeta", 0); + fts.fill("pfcand_dphidphi", 0); + fts.fill("pfcand_dxydxy", 0); + fts.fill("pfcand_dzdz", 0); + fts.fill("pfcand_dxydz", 0); + fts.fill("pfcand_dphidxy", 0); + fts.fill("pfcand_dlambdadz", 0); + + fts.fill("pfcand_btagMomentum", 0); + fts.fill("pfcand_btagEta", 0); + fts.fill("pfcand_btagEtaRel", 0); + fts.fill("pfcand_btagPtRel", 0); + fts.fill("pfcand_btagPPar", 0); + fts.fill("pfcand_btagDeltaR", 0); + fts.fill("pfcand_btagPtRatio", 0); + fts.fill("pfcand_btagPParRatio", 0); + fts.fill("pfcand_btagSip2dVal", 0); + fts.fill("pfcand_btagSip2dSig", 0); + fts.fill("pfcand_btagSip3dVal", 0); + fts.fill("pfcand_btagSip3dSig", 0); + fts.fill("pfcand_btagJetDistVal", 0); + } + + } + +} + +void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet){ + std::vector jetSVs; + for (const auto &sv : *svs){ + if (reco::deltaR(sv, jet) < jet_radius_) { + jetSVs.push_back(&sv); + } + } + // sort by dxy significance + std::sort(jetSVs.begin(), jetSVs.end(), [&](const reco::VertexCompositePtrCandidate *sva, const reco::VertexCompositePtrCandidate *svb){ + return sv_vertex_comparator(*sva, *svb, *pv_); + }); + + const float etasign = jet.eta()>0 ? 1 : -1; + + for (const auto *sv : jetSVs){ + // basic kinematics + fts.fill("sv_ptrel", sv->pt() / jet.pt()); + fts.fill("sv_erel", sv->energy() / jet.energy()); + fts.fill("sv_phirel", reco::deltaPhi(*sv, jet)); + fts.fill("sv_etarel", etasign * (sv->eta() - jet.eta())); + fts.fill("sv_deltaR", reco::deltaR(*sv, jet)); + fts.fill("sv_pt", sv->pt()); + fts.fill("sv_abseta", std::abs(sv->eta())); + fts.fill("sv_mass", sv->mass()); + + fts.fill("sv_ptrel_log", catch_infs(std::log(sv->pt()/jet.pt()), -99)); + fts.fill("sv_erel_log", catch_infs(std::log(sv->energy()/jet.energy()), -99)); + fts.fill("sv_pt_log", catch_infs(std::log(sv->pt()), -99)); + + // sv properties + fts.fill("sv_ntracks", sv->numberOfDaughters()); + fts.fill("sv_chi2", sv->vertexChi2()); + fts.fill("sv_ndf", sv->vertexNdof()); + fts.fill("sv_normchi2", catch_infs(sv->vertexNormalizedChi2())); + + const auto &dxy = vertexDxy(*sv, *pv_); + fts.fill("sv_dxy", dxy.value()); + fts.fill("sv_dxyerr", dxy.error()); + fts.fill("sv_dxysig", dxy.significance()); + + const auto &d3d = vertexD3d(*sv, *pv_); + fts.fill("sv_d3d", d3d.value()); + fts.fill("sv_d3derr", d3d.error()); + fts.fill("sv_d3dsig", d3d.significance()); + fts.fill("sv_costhetasvpv", vertexDdotP(*sv, *pv_)); + } + +} + +// define this as a plug-in +DEFINE_FWK_MODULE(DeepBoostedJetTagInfoProducer); diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc new file mode 100644 index 0000000000000..bd0121c7b521f --- /dev/null +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -0,0 +1,332 @@ +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/stream/EDProducer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/Framework/interface/makeRefToBaseProdFrom.h" + +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/StreamID.h" + +#include "DataFormats/BTauReco/interface/JetTag.h" + +#include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" + +#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" + +#include +#include + +// TO BE IMPROVED +// Declaration of the data structure that is hold by the edm::GlobalCache. +// In TensorFlow, the computational graph is stored in a stateless graph object which can be shared +// by multiple session instances which handle the initialization of variables related to the graph. +// Following this approach in CMSSW, a graph should be stored in a GlobalCache which can be accessed +// by sessions owned by multiple stream module copies. Instead of using only the plain graph, we +// make use of a cache struct that can be extended in the future if nedded. In addition, the graph +// is protected via std::atomic, which should not affect the performance as it is only accessed in +// the module constructor and not in the actual produce loop. +struct MXBufferFileCache { + MXBufferFileCache() : model_data(nullptr), param_data(nullptr) { + } + + std::atomic model_data; + std::atomic param_data; +}; + +// struct to hold preprocessing parameters +struct PreprocessParams { + struct VarInfo { + VarInfo() {} + VarInfo(float median, float upper) : center(median), scale(upper-median) { + if (scale==0) scale=1; + } + float center = 0; + float scale = 1; + }; + + unsigned var_length = 0; + std::vector var_names; + std::unordered_map var_info_map; + + VarInfo get_info(const std::string &name) const { + try { + return var_info_map.at(name); + }catch (const std::out_of_range &e) { + throw cms::Exception("InvalidArgument") << "Cannot find variable info for " << name; + } + } +}; + +class DeepBoostedJetTagsProducer : public edm::stream::EDProducer> { + + public: + explicit DeepBoostedJetTagsProducer(const edm::ParameterSet&, const MXBufferFileCache*); + ~DeepBoostedJetTagsProducer() override; + + static void fillDescriptions(edm::ConfigurationDescriptions&); + + static std::unique_ptr initializeGlobalCache(const edm::ParameterSet&); + static void globalEndJob(const MXBufferFileCache*); + + private: + typedef std::vector TagInfoCollection; + typedef reco::JetTagCollection JetTagCollection; + + void beginStream(edm::StreamID) override {} + void produce(edm::Event&, const edm::EventSetup&) override; + void endStream() override {} + + std::vector center_norm_pad(const std::vector& input, + float center, float scale, + unsigned target_length, float pad_value=0, + float min=0, float max=-1); + void make_inputs(const reco::DeepBoostedJetTagInfo &taginfo); + + const edm::EDGetTokenT< TagInfoCollection > src_; + std::vector>> flav_pairs_; + std::vector input_names_; // names of each input group :: the ordering is important! + std::vector> input_shapes_; // shapes of each input group + std::unordered_map prep_info_map_; // preprocessing info for each input group + + std::vector> data_; + std::unique_ptr predictor_; + + bool debug_ = false; +}; + +DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& iConfig, const MXBufferFileCache* cache) +: src_(consumes(iConfig.getParameter("src"))) +, debug_(iConfig.getUntrackedParameter("debugMode", false)) +{ + + // load preprocessing info + input_shapes_.clear(); + prep_info_map_.clear(); + const auto &prep_pset = iConfig.getParameterSet("preprocessParams"); + input_names_ = prep_pset.getParameter>("input_names"); + for (const auto &group_name : input_names_){ + const auto &group_pset = prep_pset.getParameterSet(group_name); + input_shapes_.push_back(group_pset.getParameter>("input_shape")); + auto& prep_params = prep_info_map_[group_name]; + prep_params.var_length = group_pset.getParameter("var_length"); + prep_params.var_names = group_pset.getParameter>("var_names"); + auto &var_info_pset = group_pset.getParameterSet("var_infos"); + for (const auto &var_name : prep_params.var_names){ + const auto &var_pset = var_info_pset.getParameterSet(var_name); + double median = var_pset.getParameter("median"); + double upper = var_pset.getParameter("upper"); + prep_params.var_info_map[var_name] = PreprocessParams::VarInfo(median, upper); + } + } + + if (debug_) { + for (unsigned i=0; i(); + predictor_->set_input_shapes(input_names_, input_shapes_); + predictor_->load_model(cache->model_data, cache->param_data); + + // get output names from flav_table + const auto & flav_pset = iConfig.getParameter("flav_table"); + for (const auto flav_pair : flav_pset.tbl()) { + const auto & flav_name = flav_pair.first; + flav_pairs_.emplace_back(flav_name, + flav_pset.getParameter>(flav_name)); + } + + for (const auto & flav_pair : flav_pairs_) { + produces(flav_pair.first); + } + +} + +DeepBoostedJetTagsProducer::~DeepBoostedJetTagsProducer(){ +} + +void DeepBoostedJetTagsProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) +{ + + // pfDeepBoostedJetTags + edm::ParameterSetDescription desc; + desc.add("src", edm::InputTag("pfDeepBoostedJetTagInfos")); + edm::ParameterSetDescription preprocessParams; + preprocessParams.setAllowAnything(); + desc.add("preprocessParams", preprocessParams); + desc.add("model_path", + edm::FileInPath("RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-symbol.json")); + desc.add("param_path", + edm::FileInPath("RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-0000.params")); + { + edm::ParameterSetDescription psd0; + psd0.add>("probTbcq", {0}); + psd0.add>("probTbqq", {1}); + psd0.add>("probTbc", {2}); + psd0.add>("probTbq", {3}); + psd0.add>("probWcq", {4}); + psd0.add>("probWqq", {5}); + psd0.add>("probZbb", {6}); + psd0.add>("probZcc", {7}); + psd0.add>("probZqq", {8}); + psd0.add>("probHbb", {9}); + psd0.add>("probHcc", {10}); + psd0.add>("probHqqqq", {11}); + psd0.add>("probQCDbb", {12}); + psd0.add>("probQCDcc", {13}); + psd0.add>("probQCDb", {14}); + psd0.add>("probQCDc", {15}); + psd0.add>("probQCDothers", {16}); + desc.add("flav_table", psd0); + } + desc.addOptionalUntracked("debugMode", false); + + descriptions.add("pfDeepBoostedJetTags", desc); +} + +std::unique_ptr DeepBoostedJetTagsProducer::initializeGlobalCache( + const edm::ParameterSet& iConfig) +{ + // get the model files + std::string model_file = iConfig.getParameter("model_path").fullPath(); + std::string param_file = iConfig.getParameter("param_path").fullPath(); + + // load the model and params and save it in the cache + MXBufferFileCache* cache = new MXBufferFileCache(); + cache->model_data = new mxnet::BufferFile(model_file); + cache->param_data = new mxnet::BufferFile(param_file); + return std::unique_ptr(cache); +} + +void DeepBoostedJetTagsProducer::globalEndJob(const MXBufferFileCache* cache) +{ + if (cache->model_data != nullptr) { + delete cache->model_data; + } + if (cache->param_data != nullptr) { + delete cache->param_data; + } +} + +void DeepBoostedJetTagsProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) +{ + + edm::Handle tag_infos; + iEvent.getByToken(src_, tag_infos); + + // initialize output collection + std::vector> output_tags; + for (std::size_t i=0; i < flav_pairs_.size(); i++) { + if (!tag_infos->empty()) { + auto jet_ref = tag_infos->begin()->jet(); + output_tags.emplace_back(std::make_unique( + edm::makeRefToBaseProdFrom(jet_ref, iEvent))); + } else { + output_tags.emplace_back(std::make_unique()); + } + } + + for (unsigned jet_n=0; jet_nsize(); ++jet_n){ + + const auto& taginfo = tag_infos->at(jet_n); + std::vector outputs(flav_pairs_.size(), 0); + + if (!taginfo.features().empty()){ + // convert inputs + make_inputs(taginfo); + // run prediction and get outputs + outputs = predictor_->predict(data_); + } + + const auto & jet_ref = tag_infos->at(jet_n).jet(); + for (std::size_t flav_n=0; flav_n < flav_pairs_.size(); flav_n++) { + const auto & flav_pair = flav_pairs_.at(flav_n); + float o_sum = 0.; + for (const unsigned int & ind : flav_pair.second) { + o_sum += outputs.at(ind); + } + (*(output_tags.at(flav_n)))[jet_ref] = o_sum; + } + + } + + if (debug_){ + std::cerr << "=== " << iEvent.id().run() << ":" << iEvent.id().luminosityBlock() << ":" << iEvent.id().event() << " ===" << std::endl; + for (unsigned jet_n=0; jet_nsize(); ++jet_n){ + const auto & jet_ref = tag_infos->at(jet_n).jet(); + std::cerr << " - Jet #" << jet_n << ", pt=" << jet_ref->pt() << ", eta=" << jet_ref->eta() << ", phi=" << jet_ref->phi() << std::endl; + for (std::size_t flav_n=0; flav_n < flav_pairs_.size(); ++flav_n) { + std::cerr << " " << flav_pairs_.at(flav_n).first << " = " << (*(output_tags.at(flav_n)))[jet_ref] << std::endl; + } + } + } + + // put into the event + for (std::size_t i=0; i < flav_pairs_.size(); i++) { + iEvent.put(std::move(output_tags[i]), flav_pairs_.at(i).first); + } + +} + +std::vector DeepBoostedJetTagsProducer::center_norm_pad( + const std::vector& input, float center, float scale, + unsigned target_length, float pad_value, float min, float max) { + + // do variable shifting/scaling/padding/clipping in one go + auto clip = [](float value, float low, float high){ + if (low >= high) throw cms::Exception("InvalidArgument") << "Error in clip: low >= high!"; + if (value < low) return low; + if (value > high) return high; + return value; + }; + + pad_value = clip(pad_value, min, max); + std::vector out(target_length, pad_value); + for (unsigned i=0; i bool sv_vertex_comparator(const SVType & sva, const SVType & svb, const PVType & pv) { - auto adxy = vertexDxy(sva,pv); - auto bdxy = vertexDxy(svb,pv); + auto adxy = vertexDxy(sva,pv); + auto bdxy = vertexDxy(svb,pv); float aval= adxy.value(); float bval= bdxy.value(); float aerr= adxy.error(); float berr= bdxy.error(); - + float asig= catch_infs(aval/aerr,0.); float bsig= catch_infs(bval/berr,0.); return bsig int dump_vector(reco::TaggingVariableList& from, T* to, @@ -63,9 +67,14 @@ namespace btagbtvdeep { } return size; } - + // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic) - float mindrsvpfcand(const std::vector & svs, + float mindrsvpfcand(const std::vector & svs, const reco::Candidate* cand, float mindr=0.4); + + // mimic the calculation in PackedCandidate + float vtx_ass_from_pfcand(const reco::PFCandidate &pfcand, int pv_ass_quality, const reco::VertexRef & pv); + float quality_from_pfcand(const reco::PFCandidate &pfcand); + float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand); } #endif //RecoBTag_TensorFlow_deep_helpers_h diff --git a/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc b/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc index 2f35d08513b64..28660ecd30ee2 100644 --- a/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc +++ b/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc @@ -2,34 +2,20 @@ namespace btagbtvdeep { - - constexpr static int qualityMap[8] = {1,0,1,1,4,4,5,6}; - - - enum qualityFlagsShiftsAndMasks { - assignmentQualityMask = 0x7, - assignmentQualityShift = 0, - trackHighPurityMask = 0x8, - trackHighPurityShift=3, - lostInnerHitsMask = 0x30, - lostInnerHitsShift=4, - muonFlagsMask = 0x0600, - muonFlagsShift=9 - }; - + void packedCandidateToFeatures(const pat::PackedCandidate * c_pf, const pat::Jet & jet, const TrackInfoBuilder & track_info, const float drminpfcandsv, const float jetR, ChargedCandidateFeatures & c_pf_features, const bool flip) { - + commonCandidateToFeatures(c_pf, jet, track_info, drminpfcandsv, jetR, c_pf_features, flip); - + c_pf_features.vtx_ass = c_pf->pvAssociationQuality(); - + c_pf_features.puppiw = c_pf->puppiWeight(); - + // if PackedCandidate does not have TrackDetails this gives an Exception // because unpackCovariance might be called for pseudoTrack/bestTrack if (c_pf->hasTrackDetails()) { @@ -42,45 +28,28 @@ namespace btagbtvdeep { c_pf_features.chi2 = catch_infs_and_bound(-1,300,-1,300); c_pf_features.quality =(1 << reco::TrackBase::loose); } - - } - + + } + void recoCandidateToFeatures(const reco::PFCandidate * c_pf, const reco::Jet & jet, const TrackInfoBuilder & track_info, const float drminpfcandsv, const float jetR, const float puppiw, const int pv_ass_quality, - const reco::VertexRef & pv, + const reco::VertexRef & pv, ChargedCandidateFeatures & c_pf_features, const bool flip) { - + commonCandidateToFeatures(c_pf, jet, track_info, drminpfcandsv, jetR, c_pf_features, flip); - - c_pf_features.vtx_ass = (float) pat::PackedCandidate::PVAssociationQuality(qualityMap[pv_ass_quality]); - if (c_pf->trackRef().isNonnull() && - pv->trackWeight(c_pf->trackRef()) > 0.5 && - pv_ass_quality == 7) { - c_pf_features.vtx_ass = (float) pat::PackedCandidate::UsedInFitTight; - } - + + c_pf_features.vtx_ass = vtx_ass_from_pfcand(*c_pf, pv_ass_quality, pv); c_pf_features.puppiw = puppiw; - + const auto & pseudo_track = (c_pf->bestTrack()) ? *c_pf->bestTrack() : reco::Track(); c_pf_features.chi2 = catch_infs_and_bound(std::floor(pseudo_track.normalizedChi2()),300,-1,300); - // conditions from PackedCandidate producer - bool highPurity = c_pf->trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity); - // do same bit operations than in PackedCandidate - uint16_t qualityFlags = 0; - qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask); - bool isHighPurity = (qualityFlags & trackHighPurityMask)>>trackHighPurityShift; - // to do as in TrackBase - uint8_t quality = (1 << reco::TrackBase::loose); - if (isHighPurity) { - quality |= (1 << reco::TrackBase::highPurity); - } - c_pf_features.quality = quality; - - } - + c_pf_features.quality = quality_from_pfcand(*c_pf); + + } + } diff --git a/RecoBTag/TensorFlow/src/deep_helpers.cc b/RecoBTag/TensorFlow/src/deep_helpers.cc index 6d508a72c4903..f1493633fa83a 100644 --- a/RecoBTag/TensorFlow/src/deep_helpers.cc +++ b/RecoBTag/TensorFlow/src/deep_helpers.cc @@ -1,11 +1,25 @@ #include "RecoBTag/TensorFlow/interface/deep_helpers.h" namespace btagbtvdeep { - + + constexpr static int qualityMap[8] = {1,0,1,1,4,4,5,6}; + + enum qualityFlagsShiftsAndMasks { + assignmentQualityMask = 0x7, + assignmentQualityShift = 0, + trackHighPurityMask = 0x8, + trackHighPurityShift=3, + lostInnerHitsMask = 0x30, + lostInnerHitsShift=4, + muonFlagsMask = 0x0600, + muonFlagsShift=9 + }; + + // remove infs and NaNs with value (adapted from DeepNTuples) const float catch_infs(const float in, const float replace_value) { - if(in==in){ // check if NaN + if(in==in){ // check if NaN if(std::isinf(in)) return replace_value; else if(in < -1e32 || in > 1e32) @@ -13,8 +27,8 @@ namespace btagbtvdeep { return in; } return replace_value; - } - + } + // remove infs/NaN and bound (adapted from DeepNTuples) const float catch_infs_and_bound(const float in, const float replace_value, @@ -29,8 +43,8 @@ namespace btagbtvdeep { withoutinfs+=offset; return withoutinfs; } - - + + // 2D distance between SV and PV (adapted from DeepNTuples) Measurement1D vertexDxy(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv) { VertexDistanceXY dist; @@ -38,7 +52,7 @@ namespace btagbtvdeep { reco::Vertex svtx(svcand.vertex(), csv); return dist.distance(svtx, pv); } - + //3D distance between SV and PV (adapted from DeepNTuples) Measurement1D vertexD3d(const reco::VertexCompositePtrCandidate &svcand, const reco::Vertex &pv) { VertexDistance3D dist; @@ -46,30 +60,67 @@ namespace btagbtvdeep { reco::Vertex svtx(svcand.vertex(), csv); return dist.distance(svtx, pv); } - + // dot product between SV and PV (adapted from DeepNTuples) float vertexDdotP(const reco::VertexCompositePtrCandidate &sv, const reco::Vertex &pv) { reco::Candidate::Vector p = sv.momentum(); reco::Candidate::Vector d(sv.vx() - pv.x(), sv.vy() - pv.y(), sv.vz() - pv.z()); return p.Unit().Dot(d.Unit()); } - + // compute minimum dr between SVs and a candidate (from DeepNTuples, now polymorphic) - float mindrsvpfcand(const std::vector & svs, + float mindrsvpfcand(const std::vector & svs, const reco::Candidate* cand, float mindr) { - + for (unsigned int i0=0; i0(const reco::VertexCompositePtrCandidate&, - const reco::VertexCompositePtrCandidate&, + template bool sv_vertex_comparator(const reco::VertexCompositePtrCandidate&, + const reco::VertexCompositePtrCandidate&, const reco::Vertex&); - + + float vtx_ass_from_pfcand(const reco::PFCandidate& pfcand, int pv_ass_quality, const reco::VertexRef & pv) { + float vtx_ass = pat::PackedCandidate::PVAssociationQuality(qualityMap[pv_ass_quality]); + if (pfcand.trackRef().isNonnull() && + pv->trackWeight(pfcand.trackRef()) > 0.5 && + pv_ass_quality == 7) { + vtx_ass = pat::PackedCandidate::UsedInFitTight; + } + return vtx_ass; + } + + float quality_from_pfcand(const reco::PFCandidate& pfcand) { + const auto & pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track(); + // conditions from PackedCandidate producer + bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity); + // do same bit operations than in PackedCandidate + uint16_t qualityFlags = 0; + qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask); + bool isHighPurity = (qualityFlags & trackHighPurityMask)>>trackHighPurityShift; + // to do as in TrackBase + uint8_t quality = (1 << reco::TrackBase::loose); + if (isHighPurity) { + quality |= (1 << reco::TrackBase::highPurity); + } + return quality; + } + + float lost_inner_hits_from_pfcand(const reco::PFCandidate& pfcand) { + const auto & pseudo_track = (pfcand.bestTrack()) ? *pfcand.bestTrack() : reco::Track(); + // conditions from PackedCandidate producer + bool highPurity = pfcand.trackRef().isNonnull() && pseudo_track.quality(reco::Track::highPurity); + // do same bit operations than in PackedCandidate + uint16_t qualityFlags = 0; + qualityFlags = (qualityFlags & ~trackHighPurityMask) | ((highPurity << trackHighPurityShift) & trackHighPurityMask); + return int16_t((qualityFlags & lostInnerHitsMask)>>lostInnerHitsShift)-1; + } + } + From 6aa6cca7fac39bb19a49fc88225f2f0260ee3014 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 4 Jul 2018 03:11:00 +0200 Subject: [PATCH 04/31] Apply code-checks. --- PhysicsTools/MXNet/src/MXNetPredictor.cc | 2 +- .../DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PhysicsTools/MXNet/src/MXNetPredictor.cc b/PhysicsTools/MXNet/src/MXNetPredictor.cc index 61b0a4f3dd410..29604decfa6c2 100644 --- a/PhysicsTools/MXNet/src/MXNetPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetPredictor.cc @@ -102,7 +102,7 @@ std::vector MXNetPredictor::predict(const std::vector0 ? reco::deltaR(*cand, *subjets.at(0)) : -1); + fts.fill("pfcand_drsubjet1", !subjets.empty() ? reco::deltaR(*cand, *subjets.at(0)) : -1); fts.fill("pfcand_drsubjet2", subjets.size()>1 ? reco::deltaR(*cand, *subjets.at(1)) : -1); const reco::Track *trk = nullptr; From fc1fd127390e78d98e64bd5488df4b22958e0db1 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 4 Jul 2018 14:57:28 +0200 Subject: [PATCH 05/31] Fix dependency of meta taggers. --- .../python/recoLayer0/bTagging_cff.py | 22 ++++++++++--------- .../python/pfDeepBoostedJet_cff.py | 3 ++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index 83c74f738b306..b3d42c808091b 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -231,6 +231,8 @@ #meta-taggers are simple arithmetic on top of other taggers, they are stored here #such that in case you want them re-run also the parent tagger is re-run as well +_pfDeepBoostedJetTagsProbs = ['pfDeepBoostedJetTags:probTbcq', 'pfDeepBoostedJetTags:probTbqq', 'pfDeepBoostedJetTags:probTbc', 'pfDeepBoostedJetTags:probTbq', 'pfDeepBoostedJetTags:probWcq', 'pfDeepBoostedJetTags:probWqq', 'pfDeepBoostedJetTags:probZbb', 'pfDeepBoostedJetTags:probZcc', 'pfDeepBoostedJetTags:probZqq', 'pfDeepBoostedJetTags:probHbb', 'pfDeepBoostedJetTags:probHcc', 'pfDeepBoostedJetTags:probHqqqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'] +_pfMassDecorrelatedDeepBoostedJetTagsProbs = ['pfMassDecorrelatedDeepBoostedJetTags:probTbcq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'] supportedMetaDiscr = { 'pfDeepCSVDiscriminatorsJetTags:BvsAll' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], 'pfDeepCSVDiscriminatorsJetTags:CvsB' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], @@ -238,15 +240,15 @@ 'pfDeepCMVADiscriminatorsJetTags:BvsAll' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], 'pfDeepCMVADiscriminatorsJetTags:CvsB' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], 'pfDeepCMVADiscriminatorsJetTags:CvsL' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], - 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD' : ['pfDeepBoostedJetTags:probTbcq', 'pfDeepBoostedJetTags:probTbqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'], - 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD' : ['pfDeepBoostedJetTags:probWcq', 'pfDeepBoostedJetTags:probWqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probTbcq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probWcq', 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD' : ['pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvscc' : ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc'], - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsZHcc': ['pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc'], + 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvscc' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsZHcc': _pfMassDecorrelatedDeepBoostedJetTagsProbs, } diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py index dcccbc19e6cae..02f945be20409 100644 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py +++ b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py @@ -28,4 +28,5 @@ # This task is not used, useful only if we run DeepFlavour from RECO # jets (RECO/AOD) pfDeepBoostedJetTask = cms.Task(puppi, primaryVertexAssociation, - pfDeepBoostedJetTagInfos, pfDeepBoostedJetTags, pfMassDecorrelatedDeepBoostedJetTags) + pfDeepBoostedJetTagInfos, pfDeepBoostedJetTags, pfMassDecorrelatedDeepBoostedJetTags, + pfDeepBoostedDiscriminatorsJetTags, pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags) From 20b9aec3e4b8cea00507ced76577baa61408127c Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Thu, 5 Jul 2018 19:05:43 +0200 Subject: [PATCH 06/31] Fix misuse of edm::Ptr and some clean-ups. --- .../PatAlgos/python/tools/jetTools.py | 10 +- RecoBTag/DeepBoostedJet/BuildFile.xml | 4 - .../plugins/DeepBoostedJetTagInfoProducer.cc | 96 +++++++------------ RecoBTag/TensorFlow/interface/deep_helpers.h | 1 - RecoBTag/TensorFlow/src/deep_helpers.cc | 1 + 5 files changed, 39 insertions(+), 73 deletions(-) delete mode 100644 RecoBTag/DeepBoostedJet/BuildFile.xml diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index acf4d2dd20e62..0e6a4ae879f4c 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -634,22 +634,20 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou jetSrcName = jetSource.value().lower() if 'slimmed' in jetSrcName or 'updated' in jetSrcName: # running on slimmedJetsAK8 or re-applying btag on MiniAOD - updateJetCollection = cms.bool(True) + update_jets = cms.bool(True) puppi_value_map = cms.InputTag("") vertex_associator = cms.InputTag("") - subjets = cms.InputTag("") else: - updateJetCollection = cms.bool(False) + sys.stderr.write("Warning: running pfDeepBoostedJetTagInfos on %s is not supported yet.\n"%jetSource) + update_jets = cms.bool(False) puppi_value_map = cms.InputTag("puppi") vertex_associator = cms.InputTag("primaryVertexAssociation","original") - subjets = cms.InputTag(jetSource.value().replace("Puppi", "PFPuppiSoftDrop")) addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix, btag.pfDeepBoostedJetTagInfos.clone( jets = jetSource, - subjets = subjets, vertices = pvSource, secondary_vertices = svSource, - updateJetCollection = updateJetCollection, + update_jets = update_jets, puppi_value_map = puppi_value_map, vertex_associator = vertex_associator, ), diff --git a/RecoBTag/DeepBoostedJet/BuildFile.xml b/RecoBTag/DeepBoostedJet/BuildFile.xml deleted file mode 100644 index 37051249a2261..0000000000000 --- a/RecoBTag/DeepBoostedJet/BuildFile.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index dc21e0ac1813c..a84abf05b5334 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -50,19 +50,16 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> const double min_pt_for_track_properties_; edm::EDGetTokenT> jet_token_; - edm::EDGetTokenT> sdjet_token_; edm::EDGetTokenT vtx_token_; edm::EDGetTokenT sv_token_; bool use_puppi_value_map_; bool use_pvasq_value_map_; - bool use_subjet_collection_; edm::EDGetTokenT> puppi_value_map_token_; edm::EDGetTokenT> pvasq_value_map_token_; edm::EDGetTokenT> pvas_token_; - edm::Handle> sdjets; edm::Handle vtxs; edm::Handle svs; edm::ESHandle track_builder; @@ -75,7 +72,7 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> }; DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::ParameterSet& iConfig) -: update_jets_(iConfig.getParameter("updateJetCollection")) +: update_jets_(iConfig.getParameter("update_jets")) , jet_radius_(iConfig.getParameter("jet_radius")) , min_jet_pt_(iConfig.getParameter("min_jet_pt")) , min_pt_for_track_properties_(iConfig.getParameter("minPtForTrackProperties")) @@ -84,7 +81,6 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete , sv_token_(consumes(iConfig.getParameter("secondary_vertices"))) , use_puppi_value_map_(false) , use_pvasq_value_map_(false) -, use_subjet_collection_(false) , feature_names_(iConfig.getParameter>("feature_names")) { @@ -101,12 +97,6 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete use_pvasq_value_map_ = true; } - const auto & subjet_tag = iConfig.getParameter("subjets"); - if (!subjet_tag.label().empty()) { - sdjet_token_ = consumes>(subjet_tag); - use_subjet_collection_ = true; - } - produces(); } @@ -119,17 +109,16 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti { // pfDeepBoostedJetTagInfos edm::ParameterSetDescription desc; - // updateJetCollection + // update_jets: // set to true if applying on existing jet collections (whose daughters are *not* puppi weighted) // set to false if the jet collection is (re)clustered (whose daughters are puppi weighted) - desc.add("updateJetCollection", true); + desc.add("update_jets", true); desc.add("jet_radius", 0.8); desc.add("min_jet_pt", 170); desc.add("minPtForTrackProperties", -1); desc.add("vertices", edm::InputTag("offlinePrimaryVertices")); desc.add("secondary_vertices", edm::InputTag("inclusiveCandidateSecondaryVertices")); desc.add("jets", edm::InputTag("slimmedJetsAK8")); - desc.add("subjets", edm::InputTag("slimmedJetsAK8PFPuppiSoftDropPacked")); desc.add("puppi_value_map", edm::InputTag("puppi")); desc.add("vertex_associator", edm::InputTag("primaryVertexAssociation","original")); desc.add>("feature_names", std::vector{ @@ -239,10 +228,6 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event iEvent.getByToken(pvas_token_, pvas); } - if (use_subjet_collection_) { - iEvent.getByToken(sdjet_token_, sdjets); - } - for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ // reco jet reference (use as much as possible) @@ -267,24 +252,29 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet){ - // stuff required for dealing with pf candidates + // do nothing if jet does not have constituents + if (jet.numberOfDaughters()==0) return; + + // some jet properties math::XYZVector jet_dir = jet.momentum().Unit(); GlobalVector jet_ref_track_dir(jet.px(), jet.py(), jet.pz()); const float etasign = jet.eta()>0 ? 1 : -1; - auto puppiWgt = [&](reco::CandidatePtr cand){ - float puppiw = 1; // fall back value - const edm::Ptr packed_cand(cand); - if (packed_cand.isNonnull()) { puppiw = packed_cand->puppiWeight(); } - else{ - if (use_puppi_value_map_){ puppiw = (*puppi_value_map)[cand]; } + auto puppiWgt = [&](const reco::CandidatePtr& cand){ + const auto* pack_cand = dynamic_cast(&(*cand)); + const auto* reco_cand = dynamic_cast(&(*cand)); + if (pack_cand) { + return pack_cand->puppiWeight(); + } else if (reco_cand) { + if (use_puppi_value_map_){ return (*puppi_value_map)[cand]; } else { throw edm::Exception(edm::errors::InvalidReference) << "PUPPI value map is missing"; } + } else { + throw edm::Exception(edm::errors::InvalidReference) << "Cannot convert to either pat::PackedCandidate or reco::PFCandidate"; } - return puppiw; }; std::vector daughters; - for (auto cand : jet.daughterPtrVector()){ + for (const auto& cand : jet.daughterPtrVector()){ // remove particles w/ extremely low puppi weights if ((puppiWgt(cand)) < 0.01) continue; daughters.push_back(cand); @@ -294,23 +284,22 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures // updating jet collection: // linked daughters here are the original PackedCandidates // need to scale the p4 with their puppi weights - std::sort(daughters.begin(), daughters.end(), [&](const reco::CandidatePtr a, const reco::CandidatePtr b){ return puppiWgt(a)*a->pt() > puppiWgt(b)*b->pt(); }); + std::sort(daughters.begin(), daughters.end(), [&](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return puppiWgt(a)*a->pt() > puppiWgt(b)*b->pt(); }); }else{ - std::sort(daughters.begin(), daughters.end(), [](const reco::CandidatePtr a, const reco::CandidatePtr b){ return a->pt() > b->pt(); }); + std::sort(daughters.begin(), daughters.end(), [](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return a->pt() > b->pt(); }); } - auto useTrackProperties = [&](const edm::Ptr reco_cand) { - if (reco_cand.isNull()) return false; + auto useTrackProperties = [&](const reco::PFCandidate* reco_cand) { const auto* trk = reco_cand->bestTrack(); return trk!=nullptr && trk->pt()>min_pt_for_track_properties_; }; - for (const auto cand : daughters){ - const edm::Ptr packed_cand(cand); - const edm::Ptr reco_cand(cand); + for (const auto& cand : daughters){ + const auto* packed_cand = dynamic_cast(&(*cand)); + const auto* reco_cand = dynamic_cast(&(*cand)); auto puppiP4 = cand->p4(); - if (packed_cand.isNonnull()){ + if (packed_cand){ if (update_jets_) { // updating jet collection: // linked daughters here are the original PackedCandidates @@ -337,13 +326,13 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures fts.fill("pfcand_dxy", catch_infs(packed_cand->dxy())); fts.fill("pfcand_dxysig", packed_cand->bestTrack() ? catch_infs(packed_cand->dxy()/packed_cand->dxyError()) : 0); - } else if(reco_cand.isNonnull()) { + } else if (reco_cand) { // get vertex association quality int pv_ass_quality = 0; // fallback value float vtx_ass = 0; if (use_pvasq_value_map_) { - pv_ass_quality = (*pvasq_value_map)[reco_cand]; - const reco::VertexRef & PV_orig = (*pvas)[reco_cand]; + pv_ass_quality = (*pvasq_value_map)[cand]; + const reco::VertexRef & PV_orig = (*pvas)[cand]; vtx_ass = vtx_ass_from_pfcand(*reco_cand, pv_ass_quality, PV_orig); } else { throw edm::Exception(edm::errors::InvalidReference) << "Vertex association missing"; @@ -371,8 +360,6 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures fts.fill("pfcand_dxy", catch_infs(dxy)); fts.fill("pfcand_dxysig", trk ? catch_infs(dxy/trk->dxyError()) : 0); - }else { - throw edm::Exception(edm::errors::InvalidReference) << "Cannot convert reco::Candidate to either pat::PackedCandidate or reco::PFCandidate"; } // basic kinematics, valid for both charged and neutral @@ -395,34 +382,19 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures } fts.fill("pfcand_drminsv", minDR==999 ? -1 : minDR); - std::vector> subjets; - if (use_subjet_collection_){ - for (const auto &sj : *sdjets) { - // sdjets stores the soft-drop AK8 jets, with the actual subjets stored as daughters - // PhysicsTools/PatAlgos/python/slimming/applySubstructure_cff.py - // PhysicsTools/PatUtils/plugins/JetSubstructurePacker.cc - if (reco::deltaR(sj, jet) < jet_radius_) { - for ( size_t ida = 0; ida < sj.numberOfDaughters(); ++ida ) { - auto candPtr = sj.daughterPtr(ida); - subjets.emplace_back(candPtr); - } - break; - } - } - }else { - try { - const auto &patJet = dynamic_cast(jet); - for (auto sj : patJet.subjets()) { subjets.emplace_back(sj); } - }catch (const std::bad_cast &e) { - throw edm::Exception(edm::errors::InvalidReference) << "Cannot access subjets because this is not a pat::Jet."; - } + pat::JetPtrCollection subjets; + try { + const auto &patJet = dynamic_cast(jet); + subjets = patJet.subjets(); + }catch (const std::bad_cast &e) { + throw edm::Exception(edm::errors::InvalidReference) << "Cannot access subjets because this is not a pat::Jet."; } fts.fill("pfcand_drsubjet1", !subjets.empty() ? reco::deltaR(*cand, *subjets.at(0)) : -1); fts.fill("pfcand_drsubjet2", subjets.size()>1 ? reco::deltaR(*cand, *subjets.at(1)) : -1); const reco::Track *trk = nullptr; - if (packed_cand.isNonnull()) { trk = packed_cand->bestTrack(); } + if (packed_cand) { trk = packed_cand->bestTrack(); } else if (useTrackProperties(reco_cand)) { trk= reco_cand->bestTrack(); } if (trk){ fts.fill("pfcand_normchi2", catch_infs(std::floor(trk->normalizedChi2()))); diff --git a/RecoBTag/TensorFlow/interface/deep_helpers.h b/RecoBTag/TensorFlow/interface/deep_helpers.h index b2bff604a49b0..e26a5d8c90233 100644 --- a/RecoBTag/TensorFlow/interface/deep_helpers.h +++ b/RecoBTag/TensorFlow/interface/deep_helpers.h @@ -12,7 +12,6 @@ #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" //#include "RecoVertex/VertexTools/interface/VertexDistanceXY.h" -#include "DataFormats/PatCandidates/interface/PackedCandidate.h" #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" #include "DataFormats/PatCandidates/interface/Jet.h" diff --git a/RecoBTag/TensorFlow/src/deep_helpers.cc b/RecoBTag/TensorFlow/src/deep_helpers.cc index f1493633fa83a..25657ce7611c3 100644 --- a/RecoBTag/TensorFlow/src/deep_helpers.cc +++ b/RecoBTag/TensorFlow/src/deep_helpers.cc @@ -1,4 +1,5 @@ #include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "DataFormats/PatCandidates/interface/PackedCandidate.h" namespace btagbtvdeep { From d7a1c269f02aa60185adf4de15abbf337ee7c563 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Thu, 5 Jul 2018 19:41:30 +0200 Subject: [PATCH 07/31] Override clearDaughters in pat::Jet to reset the daughter cache too. --- .../Candidate/interface/CompositePtrCandidate.h | 2 +- DataFormats/PatCandidates/interface/Jet.h | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/DataFormats/Candidate/interface/CompositePtrCandidate.h b/DataFormats/Candidate/interface/CompositePtrCandidate.h index ba84830a61c51..f13b9089c3567 100755 --- a/DataFormats/Candidate/interface/CompositePtrCandidate.h +++ b/DataFormats/Candidate/interface/CompositePtrCandidate.h @@ -48,7 +48,7 @@ namespace reco { /// add a daughter via a reference void addDaughter( const CandidatePtr & ); /// clear daughter references - void clearDaughters() { dau.clear(); } + virtual void clearDaughters() { dau.clear(); } /// reference to daughter at given position virtual CandidatePtr daughterPtr( size_type i ) const { return dau[ i ]; } /// references to daughtes diff --git a/DataFormats/PatCandidates/interface/Jet.h b/DataFormats/PatCandidates/interface/Jet.h index fdaa76e1465ec..9b3d2c94b98c4 100644 --- a/DataFormats/PatCandidates/interface/Jet.h +++ b/DataFormats/PatCandidates/interface/Jet.h @@ -452,6 +452,12 @@ namespace pat { /// Else return the reco Jet number of constituents size_t numberOfDaughters() const override; + /// clear daughter references + void clearDaughters() override { + reco::CompositePtrCandidate::clearDaughters(); + daughtersTemp_.reset(); // need to reset daughtersTemp_ as well + } + /// accessing Jet ID information reco::JetID const & jetID () const { return jetID_;} @@ -510,10 +516,10 @@ namespace pat { /// Check to see if the subjet collection exists bool hasSubjets( std::string const & label ) const { return find( subjetLabels_.begin(), subjetLabels_.end(), label) != subjetLabels_.end(); } - + /// Number of subjet collections unsigned int nSubjetCollections( ) const { return subjetCollections_.size(); } - + /// Subjet collection names std::vector const & subjetCollectionNames() const { return subjetLabels_; } From d0e4e1a82fbd75ef6279ebe76f952d3059437821 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 6 Jul 2018 16:17:25 +0200 Subject: [PATCH 08/31] Some improvements and clean-ups. --- .../python/slimming/applyDeepBtagging_cff.py | 6 +- .../PatAlgos/python/tools/jetTools.py | 20 +++-- RecoBTag/DeepBoostedJet/plugins/BuildFile.xml | 2 - .../plugins/DeepBoostedJetTagInfoProducer.cc | 84 +++++-------------- .../plugins/DeepBoostedJetTagsProducer.cc | 35 ++++---- 5 files changed, 56 insertions(+), 91 deletions(-) diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index b21352852ba5f..d9a7b76cf8b71 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -44,8 +44,8 @@ def applyDeepBtagging( process, postfix="" ) : - # update slimmed jets to include DeepFlavour (keep same name) - # make clone for DeepDoubleB-less slimmed AK8 jets, so output name is preserved + # update slimmed jets to include particle-based deep taggers (keep same name) + # make clone for DeepTags-less slimmed AK8 jets, so output name is preserved addToProcessAndTask('slimmedJetsAK8NoDeepTags', process.slimmedJetsAK8.clone(), process, task) updateJetCollection( process, @@ -114,7 +114,7 @@ def applyDeepBtagging( process, postfix="" ) : printWarning = False ) - # slimmedJetsAK8 with DeepDoubleB (remove DeepDoubleB-less) + # slimmedJetsAK8 with DeepTags (remove DeepTags-less) delattr(process, 'slimmedJetsAK8') addToProcessAndTask('slimmedJetsAK8', getattr(process,'selectedUpdatedPatJetsSlimmedAK8DeepTags'+postfix).clone(), process, task) # delete module not used anymore (slimmedJetsAK8 substitutes) diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index 0e6a4ae879f4c..ddb3e48f68bd8 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -633,21 +633,29 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou if btagInfo == 'pfDeepBoostedJetTagInfos': jetSrcName = jetSource.value().lower() if 'slimmed' in jetSrcName or 'updated' in jetSrcName: - # running on slimmedJetsAK8 or re-applying btag on MiniAOD - update_jets = cms.bool(True) + # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8 + # daughters are links to original PackedCandidates, so NOT scaled by their puppi weights + has_puppi_weighted_daughters = cms.bool(False) puppi_value_map = cms.InputTag("") vertex_associator = cms.InputTag("") else: sys.stderr.write("Warning: running pfDeepBoostedJetTagInfos on %s is not supported yet.\n"%jetSource) - update_jets = cms.bool(False) - puppi_value_map = cms.InputTag("puppi") - vertex_associator = cms.InputTag("primaryVertexAssociation","original") + has_puppi_weighted_daughters = cms.bool(True) + # daughters are the particles used in jet clustering, so already scaled by their puppi weights + if pfCandidates.value() == 'packedPFCandidates': + # case 2: running on new jet collection whose daughters are PackedCandidates (e.g., recluster jets from MiniAOD files) + puppi_value_map = cms.InputTag("") + vertex_associator = cms.InputTag("") + elif pfCandidates.value() == 'particleFlow': + # case 3: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD) + puppi_value_map = cms.InputTag("puppi") + vertex_associator = cms.InputTag("primaryVertexAssociation","original") addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix, btag.pfDeepBoostedJetTagInfos.clone( jets = jetSource, vertices = pvSource, secondary_vertices = svSource, - update_jets = update_jets, + has_puppi_weighted_daughters = has_puppi_weighted_daughters, puppi_value_map = puppi_value_map, vertex_associator = vertex_associator, ), diff --git a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml index 1a293383576fd..fa7a7cf8bcc61 100644 --- a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml +++ b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml @@ -3,10 +3,8 @@ - - diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index a84abf05b5334..59da6b14529d2 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -44,7 +44,7 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> void fillParticleFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet); void fillSVFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet); - const bool update_jets_; + const bool has_puppi_weighted_daughters_; const double jet_radius_; const double min_jet_pt_; const double min_pt_for_track_properties_; @@ -72,10 +72,10 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> }; DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::ParameterSet& iConfig) -: update_jets_(iConfig.getParameter("update_jets")) +: has_puppi_weighted_daughters_(iConfig.getParameter("has_puppi_weighted_daughters")) , jet_radius_(iConfig.getParameter("jet_radius")) , min_jet_pt_(iConfig.getParameter("min_jet_pt")) -, min_pt_for_track_properties_(iConfig.getParameter("minPtForTrackProperties")) +, min_pt_for_track_properties_(iConfig.getParameter("min_pt_for_track_properties")) , jet_token_(consumes>(iConfig.getParameter("jets"))) , vtx_token_(consumes(iConfig.getParameter("vertices"))) , sv_token_(consumes(iConfig.getParameter("secondary_vertices"))) @@ -109,16 +109,13 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti { // pfDeepBoostedJetTagInfos edm::ParameterSetDescription desc; - // update_jets: - // set to true if applying on existing jet collections (whose daughters are *not* puppi weighted) - // set to false if the jet collection is (re)clustered (whose daughters are puppi weighted) - desc.add("update_jets", true); + desc.add("has_puppi_weighted_daughters", true); desc.add("jet_radius", 0.8); - desc.add("min_jet_pt", 170); - desc.add("minPtForTrackProperties", -1); + desc.add("min_jet_pt", 150); + desc.add("min_pt_for_track_properties", -1); desc.add("vertices", edm::InputTag("offlinePrimaryVertices")); desc.add("secondary_vertices", edm::InputTag("inclusiveCandidateSecondaryVertices")); - desc.add("jets", edm::InputTag("slimmedJetsAK8")); + desc.add("jets", edm::InputTag("ak8PFJetsPuppi")); desc.add("puppi_value_map", edm::InputTag("puppi")); desc.add("vertex_associator", edm::InputTag("primaryVertexAssociation","original")); desc.add>("feature_names", std::vector{ @@ -133,9 +130,6 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti "pfcand_isChargedHad", "pfcand_isGamma", "pfcand_isNeutralHad", - "pfcand_pt", - "pfcand_ptrel", - "pfcand_erel", "pfcand_phirel", "pfcand_etarel", "pfcand_deltaR", @@ -159,39 +153,27 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti "pfcand_dxydz", "pfcand_dphidxy", "pfcand_dlambdadz", - "pfcand_btagMomentum", - "pfcand_btagEta", "pfcand_btagEtaRel", "pfcand_btagPtRel", - "pfcand_btagPPar", - "pfcand_btagDeltaR", - "pfcand_btagPtRatio", "pfcand_btagPParRatio", "pfcand_btagSip2dVal", "pfcand_btagSip2dSig", "pfcand_btagSip3dVal", "pfcand_btagSip3dSig", "pfcand_btagJetDistVal", - "sv_ptrel", - "sv_erel", "sv_phirel", "sv_etarel", "sv_deltaR", - "sv_pt", "sv_abseta", "sv_mass", "sv_ptrel_log", "sv_erel_log", "sv_pt_log", "sv_ntracks", - "sv_chi2", - "sv_ndf", "sv_normchi2", "sv_dxy", - "sv_dxyerr", "sv_dxysig", "sv_d3d", - "sv_d3derr", "sv_d3dsig", "sv_costhetasvpv", }); @@ -230,19 +212,20 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ - // reco jet reference (use as much as possible) const auto& jet = jets->at(jet_n); edm::RefToBase jet_ref(jets, jet_n); // create jet features DeepBoostedJetFeatures features; // declare all the feature variables (init as empty vector) - for (const auto &name : feature_names_) features.add(name); - // fill only if above pt threshold - if (jet.pt() > min_jet_pt_){ - fillParticleFeatures(features, jet); - fillSVFeatures(features, jet); - } + for (const auto &name : feature_names_) { features.add(name); } + + // fill values only if above pt threshold and has daughters, otherwise left empty + if (jet.pt() < min_jet_pt_) continue; + if (jet.numberOfDaughters() == 0) continue; + + fillParticleFeatures(features, jet); + fillSVFeatures(features, jet); output_tag_infos->emplace_back(features, jet_ref); } @@ -267,7 +250,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures return pack_cand->puppiWeight(); } else if (reco_cand) { if (use_puppi_value_map_){ return (*puppi_value_map)[cand]; } - else { throw edm::Exception(edm::errors::InvalidReference) << "PUPPI value map is missing"; } + else { throw edm::Exception(edm::errors::InvalidReference) << "Puppi value map is missing"; } } else { throw edm::Exception(edm::errors::InvalidReference) << "Cannot convert to either pat::PackedCandidate or reco::PFCandidate"; } @@ -280,10 +263,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures daughters.push_back(cand); } // sort by (Puppi-weighted) pt - if (update_jets_) { - // updating jet collection: - // linked daughters here are the original PackedCandidates - // need to scale the p4 with their puppi weights + if (!has_puppi_weighted_daughters_) { std::sort(daughters.begin(), daughters.end(), [&](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return puppiWgt(a)*a->pt() > puppiWgt(b)*b->pt(); }); }else{ std::sort(daughters.begin(), daughters.end(), [](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return a->pt() > b->pt(); }); @@ -300,10 +280,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures auto puppiP4 = cand->p4(); if (packed_cand){ - if (update_jets_) { - // updating jet collection: - // linked daughters here are the original PackedCandidates - // need to scale the p4 with their puppi weights + if (!has_puppi_weighted_daughters_) { puppiP4 *= packed_cand->puppiWeight(); } @@ -362,10 +339,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures } - // basic kinematics, valid for both charged and neutral - fts.fill("pfcand_pt", puppiP4.pt()); - fts.fill("pfcand_ptrel", puppiP4.pt()/jet.pt()); - fts.fill("pfcand_erel", puppiP4.energy()/jet.energy()); + // basic kinematics fts.fill("pfcand_phirel", reco::deltaPhi(puppiP4, jet)); fts.fill("pfcand_etarel", etasign * (puppiP4.eta() - jet.eta())); fts.fill("pfcand_deltaR", reco::deltaR(puppiP4, jet)); @@ -395,7 +369,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures const reco::Track *trk = nullptr; if (packed_cand) { trk = packed_cand->bestTrack(); } - else if (useTrackProperties(reco_cand)) { trk= reco_cand->bestTrack(); } + else if (reco_cand && useTrackProperties(reco_cand)) { trk= reco_cand->bestTrack(); } if (trk){ fts.fill("pfcand_normchi2", catch_infs(std::floor(trk->normalizedChi2()))); @@ -414,12 +388,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures TrackInfoBuilder trkinfo(track_builder); trkinfo.buildTrackInfo(&(*cand), jet_dir, jet_ref_track_dir, *pv_); - fts.fill("pfcand_btagMomentum", trkinfo.getTrackMomentum()); - fts.fill("pfcand_btagEta", trkinfo.getTrackEta()); fts.fill("pfcand_btagEtaRel", trkinfo.getTrackEtaRel()); - fts.fill("pfcand_btagPtRel", trkinfo.getTrackPtRel()); - fts.fill("pfcand_btagPPar", trkinfo.getTrackPPar()); - fts.fill("pfcand_btagDeltaR", trkinfo.getTrackDeltaR()); fts.fill("pfcand_btagPtRatio", trkinfo.getTrackPtRatio()); fts.fill("pfcand_btagPParRatio", trkinfo.getTrackPParRatio()); fts.fill("pfcand_btagSip2dVal", trkinfo.getTrackSip2dVal()); @@ -439,12 +408,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures fts.fill("pfcand_dphidxy", 0); fts.fill("pfcand_dlambdadz", 0); - fts.fill("pfcand_btagMomentum", 0); - fts.fill("pfcand_btagEta", 0); fts.fill("pfcand_btagEtaRel", 0); - fts.fill("pfcand_btagPtRel", 0); - fts.fill("pfcand_btagPPar", 0); - fts.fill("pfcand_btagDeltaR", 0); fts.fill("pfcand_btagPtRatio", 0); fts.fill("pfcand_btagPParRatio", 0); fts.fill("pfcand_btagSip2dVal", 0); @@ -474,12 +438,9 @@ void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts, for (const auto *sv : jetSVs){ // basic kinematics - fts.fill("sv_ptrel", sv->pt() / jet.pt()); - fts.fill("sv_erel", sv->energy() / jet.energy()); fts.fill("sv_phirel", reco::deltaPhi(*sv, jet)); fts.fill("sv_etarel", etasign * (sv->eta() - jet.eta())); fts.fill("sv_deltaR", reco::deltaR(*sv, jet)); - fts.fill("sv_pt", sv->pt()); fts.fill("sv_abseta", std::abs(sv->eta())); fts.fill("sv_mass", sv->mass()); @@ -489,19 +450,16 @@ void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts, // sv properties fts.fill("sv_ntracks", sv->numberOfDaughters()); - fts.fill("sv_chi2", sv->vertexChi2()); - fts.fill("sv_ndf", sv->vertexNdof()); fts.fill("sv_normchi2", catch_infs(sv->vertexNormalizedChi2())); const auto &dxy = vertexDxy(*sv, *pv_); fts.fill("sv_dxy", dxy.value()); - fts.fill("sv_dxyerr", dxy.error()); fts.fill("sv_dxysig", dxy.significance()); const auto &d3d = vertexD3d(*sv, *pv_); fts.fill("sv_d3d", d3d.value()); - fts.fill("sv_d3derr", d3d.error()); fts.fill("sv_d3dsig", d3d.significance()); + fts.fill("sv_costhetasvpv", vertexDdotP(*sv, *pv_)); } diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc index bd0121c7b521f..d4a393cc3e81d 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -18,15 +18,9 @@ #include #include -// TO BE IMPROVED -// Declaration of the data structure that is hold by the edm::GlobalCache. -// In TensorFlow, the computational graph is stored in a stateless graph object which can be shared -// by multiple session instances which handle the initialization of variables related to the graph. -// Following this approach in CMSSW, a graph should be stored in a GlobalCache which can be accessed -// by sessions owned by multiple stream module copies. Instead of using only the plain graph, we -// make use of a cache struct that can be extended in the future if nedded. In addition, the graph -// is protected via std::atomic, which should not affect the performance as it is only accessed in -// the module constructor and not in the actual produce loop. +// Currently hold the raw model/param data in the edm::GlobalCache. +// Can be improved by holding the mxnet Symbol/NDArrays in the cache +// when moving to the MXNet C++ API. struct MXBufferFileCache { MXBufferFileCache() : model_data(nullptr), param_data(nullptr) { } @@ -39,9 +33,8 @@ struct MXBufferFileCache { struct PreprocessParams { struct VarInfo { VarInfo() {} - VarInfo(float median, float upper) : center(median), scale(upper-median) { - if (scale==0) scale=1; - } + VarInfo(float median, float upper) : + center(median), scale(upper==median ? 1 : upper-median) {} float center = 0; float scale = 1; }; @@ -86,7 +79,7 @@ class DeepBoostedJetTagsProducer : public edm::stream::EDProducer src_; std::vector>> flav_pairs_; - std::vector input_names_; // names of each input group :: the ordering is important! + std::vector input_names_; // names of each input group - the ordering is important! std::vector> input_shapes_; // shapes of each input group std::unordered_map prep_info_map_; // preprocessing info for each input group @@ -112,7 +105,7 @@ DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& auto& prep_params = prep_info_map_[group_name]; prep_params.var_length = group_pset.getParameter("var_length"); prep_params.var_names = group_pset.getParameter>("var_names"); - auto &var_info_pset = group_pset.getParameterSet("var_infos"); + const auto &var_info_pset = group_pset.getParameterSet("var_infos"); for (const auto &var_name : prep_params.var_names){ const auto &var_pset = var_info_pset.getParameterSet(var_name); double median = var_pset.getParameter("median"); @@ -242,7 +235,7 @@ void DeepBoostedJetTagsProducer::produce(edm::Event& iEvent, const edm::EventSet for (unsigned jet_n=0; jet_nsize(); ++jet_n){ const auto& taginfo = tag_infos->at(jet_n); - std::vector outputs(flav_pairs_.size(), 0); + std::vector outputs(flav_pairs_.size(), 0); // init as all zeros if (!taginfo.features().empty()){ // convert inputs @@ -284,8 +277,8 @@ void DeepBoostedJetTagsProducer::produce(edm::Event& iEvent, const edm::EventSet std::vector DeepBoostedJetTagsProducer::center_norm_pad( const std::vector& input, float center, float scale, unsigned target_length, float pad_value, float min, float max) { - // do variable shifting/scaling/padding/clipping in one go + auto clip = [](float value, float low, float high){ if (low >= high) throw cms::Exception("InvalidArgument") << "Error in clip: low >= high!"; if (value < low) return low; @@ -307,12 +300,20 @@ void DeepBoostedJetTagsProducer::make_inputs(const reco::DeepBoostedJetTagInfo& data_.clear(); for (const auto &group_name : input_names_) { // initiate with an empty vector - data_.push_back({}); + data_.emplace_back(); auto &group_values = data_.back(); const auto& prep_params = prep_info_map_.at(group_name); // transform/pad + int var_ref_len = -1; for (const auto &varname : prep_params.var_names){ const auto &raw_value = taginfo.features().get(varname); + // check consistency of the variable length + if (var_ref_len == -1) { + var_ref_len = raw_value.size(); + } else { + if (static_cast(raw_value.size()) != var_ref_len) + throw cms::Exception("InvalidArgument") << "Inconsistent variable length " << raw_value.size() << " for " << varname << ", should be " << var_ref_len; + } const auto &info = prep_params.get_info(varname); float pad = 0; // pad w/ zero auto val = center_norm_pad(raw_value, info.center, info.scale, prep_params.var_length, pad, -5, 5); From 794b95333ac48548ad182bebce27b44e0046425e Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 6 Jul 2018 18:17:12 +0200 Subject: [PATCH 09/31] Add a test unit for MXNetPredictor. --- PhysicsTools/MXNet/test/BuildFile.xml | 8 ++ .../MXNet/test/data/testmxnet-0000.params | Bin 0 -> 480 bytes .../MXNet/test/data/testmxnet-symbol.json | 79 ++++++++++++++++++ PhysicsTools/MXNet/test/testMXNetPredictor.cc | 58 +++++++++++++ PhysicsTools/MXNet/test/testRunner.cpp | 1 + 5 files changed, 146 insertions(+) create mode 100644 PhysicsTools/MXNet/test/BuildFile.xml create mode 100644 PhysicsTools/MXNet/test/data/testmxnet-0000.params create mode 100644 PhysicsTools/MXNet/test/data/testmxnet-symbol.json create mode 100644 PhysicsTools/MXNet/test/testMXNetPredictor.cc create mode 100644 PhysicsTools/MXNet/test/testRunner.cpp diff --git a/PhysicsTools/MXNet/test/BuildFile.xml b/PhysicsTools/MXNet/test/BuildFile.xml new file mode 100644 index 0000000000000..fc4718f406f6f --- /dev/null +++ b/PhysicsTools/MXNet/test/BuildFile.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/PhysicsTools/MXNet/test/data/testmxnet-0000.params b/PhysicsTools/MXNet/test/data/testmxnet-0000.params new file mode 100644 index 0000000000000000000000000000000000000000..3c6a87622c52104480fc6c12b765af6374e00e3f GIT binary patch literal 480 zcmWe)WIzEdP~OR3lYfGkOhC*G6=R3es9GBA85pRGVRnP;f!U2}C)6Q83Rx|7_aW4x zvtW8beuU}8ZXb312MRA(I154@l~|N+m6DoQoN5?fo|>7SQNoWYk(8NOj3I0g4-&>O I#sF#z07SM$t^fc4 literal 0 HcmV?d00001 diff --git a/PhysicsTools/MXNet/test/data/testmxnet-symbol.json b/PhysicsTools/MXNet/test/data/testmxnet-symbol.json new file mode 100644 index 0000000000000..c423651756d8e --- /dev/null +++ b/PhysicsTools/MXNet/test/data/testmxnet-symbol.json @@ -0,0 +1,79 @@ +{ + "nodes": [ + { + "op": "null", + "name": "data", + "inputs": [] + }, + { + "op": "null", + "name": "dense0_weight", + "attrs": { + "__dtype__": "0", + "__lr_mult__": "1.0", + "__shape__": "(7, 0)", + "__wd_mult__": "1.0" + }, + "inputs": [] + }, + { + "op": "null", + "name": "dense0_bias", + "attrs": { + "__dtype__": "0", + "__init__": "zeros", + "__lr_mult__": "1.0", + "__shape__": "(7,)", + "__wd_mult__": "1.0" + }, + "inputs": [] + }, + { + "op": "FullyConnected", + "name": "dense0_fwd", + "attrs": { + "flatten": "True", + "no_bias": "False", + "num_hidden": "7" + }, + "inputs": [[0, 0, 0], [1, 0, 0], [2, 0, 0]] + }, + { + "op": "null", + "name": "dense1_weight", + "attrs": { + "__dtype__": "0", + "__lr_mult__": "1.0", + "__shape__": "(3, 0)", + "__wd_mult__": "1.0" + }, + "inputs": [] + }, + { + "op": "null", + "name": "dense1_bias", + "attrs": { + "__dtype__": "0", + "__init__": "zeros", + "__lr_mult__": "1.0", + "__shape__": "(3,)", + "__wd_mult__": "1.0" + }, + "inputs": [] + }, + { + "op": "FullyConnected", + "name": "dense1_fwd", + "attrs": { + "flatten": "True", + "no_bias": "False", + "num_hidden": "3" + }, + "inputs": [[3, 0, 0], [4, 0, 0], [5, 0, 0]] + } + ], + "arg_nodes": [0, 1, 2, 4, 5], + "node_row_ptr": [0, 1, 2, 3, 4, 5, 6, 7], + "heads": [[6, 0, 0]], + "attrs": {"mxnet_version": ["int", 10200]} +} \ No newline at end of file diff --git a/PhysicsTools/MXNet/test/testMXNetPredictor.cc b/PhysicsTools/MXNet/test/testMXNetPredictor.cc new file mode 100644 index 0000000000000..23067fac668d5 --- /dev/null +++ b/PhysicsTools/MXNet/test/testMXNetPredictor.cc @@ -0,0 +1,58 @@ +#include + +#include "FWCore/ParameterSet/interface/FileInPath.h" +#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" + +class testMXNetPredictor : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(testMXNetPredictor); + CPPUNIT_TEST(checkAll); + CPPUNIT_TEST_SUITE_END(); + +public: + void checkAll(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(testMXNetPredictor); + +void testMXNetPredictor::checkAll() +{ + + // load model and params into BufferFile + std::string model_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-symbol.json").fullPath(); + std::string param_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-0000.params").fullPath(); + + mxnet::BufferFile *model_file = new mxnet::BufferFile(model_path); + CPPUNIT_ASSERT(model_file != nullptr); + CPPUNIT_ASSERT(model_file->GetLength() > 0); + + mxnet::BufferFile *param_file = new mxnet::BufferFile(param_path); + CPPUNIT_ASSERT(param_file != nullptr); + CPPUNIT_ASSERT(param_file->GetLength() > 0); + + // create predictor + mxnet::MXNetPredictor predictor; + + // set input shape + std::vector input_names {"data"}; + std::vector> input_shapes {{1, 3}}; + CPPUNIT_ASSERT_NO_THROW( predictor.set_input_shapes(input_names, input_shapes) ); + + // load model from BufferFile + CPPUNIT_ASSERT_NO_THROW( predictor.load_model(model_file, param_file) ); + + // run predictor + std::vector> data {{1, 2, 3,}}; + std::vector outputs; + CPPUNIT_ASSERT_NO_THROW( outputs = predictor.predict(data) ); + + // check outputs + CPPUNIT_ASSERT(outputs.size() == 3); + CPPUNIT_ASSERT(outputs.at(0) == 42); + CPPUNIT_ASSERT(outputs.at(1) == 42); + CPPUNIT_ASSERT(outputs.at(2) == 42); + + delete model_file; + delete param_file; + +} diff --git a/PhysicsTools/MXNet/test/testRunner.cpp b/PhysicsTools/MXNet/test/testRunner.cpp new file mode 100644 index 0000000000000..1482cf9a9ce85 --- /dev/null +++ b/PhysicsTools/MXNet/test/testRunner.cpp @@ -0,0 +1 @@ +#include From e1e9e1a80d51c961170943ba98a4eb6f713e3b88 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 6 Jul 2018 19:40:03 +0200 Subject: [PATCH 10/31] Fix DeepBoostedJetTagInfoProducer. --- .../plugins/DeepBoostedJetTagInfoProducer.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index 59da6b14529d2..83991c9bfda0a 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -154,7 +154,7 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti "pfcand_dphidxy", "pfcand_dlambdadz", "pfcand_btagEtaRel", - "pfcand_btagPtRel", + "pfcand_btagPtRatio", "pfcand_btagPParRatio", "pfcand_btagSip2dVal", "pfcand_btagSip2dSig", @@ -221,12 +221,16 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event for (const auto &name : feature_names_) { features.add(name); } // fill values only if above pt threshold and has daughters, otherwise left empty - if (jet.pt() < min_jet_pt_) continue; - if (jet.numberOfDaughters() == 0) continue; + bool fill_vars = true; + if (jet.pt() < min_jet_pt_) fill_vars = false; + if (jet.numberOfDaughters() == 0) fill_vars = false; - fillParticleFeatures(features, jet); - fillSVFeatures(features, jet); + if (fill_vars){ + fillParticleFeatures(features, jet); + fillSVFeatures(features, jet); + } + // this should always be done even if features are not filled output_tag_infos->emplace_back(features, jet_ref); } From 70477ac3aa62a4a3b3242cbf06b8faf66d01026e Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Sun, 8 Jul 2018 23:04:16 +0200 Subject: [PATCH 11/31] Need to sort subjets by pt. --- .../DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index 83991c9bfda0a..3188032661ee8 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -364,6 +364,8 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures try { const auto &patJet = dynamic_cast(jet); subjets = patJet.subjets(); + // sort by pt + std::sort(subjets.begin(), subjets.end(), [](const edm::Ptr& p1, const edm::Ptr& p2){ return p1->pt()>p2->pt(); }); }catch (const std::bad_cast &e) { throw edm::Exception(edm::errors::InvalidReference) << "Cannot access subjets because this is not a pat::Jet."; } From 957cf9dec319b7490a1236cf52bfeaec45a160df Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 20 Jul 2018 10:14:25 +0200 Subject: [PATCH 12/31] Thread-safety and better logging in MXNetPredictor. --- PhysicsTools/MXNet/interface/MXNetPredictor.h | 3 ++ PhysicsTools/MXNet/src/MXNetPredictor.cc | 38 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/PhysicsTools/MXNet/interface/MXNetPredictor.h b/PhysicsTools/MXNet/interface/MXNetPredictor.h index 3f54a967b405f..763d46181c12d 100644 --- a/PhysicsTools/MXNet/interface/MXNetPredictor.h +++ b/PhysicsTools/MXNet/interface/MXNetPredictor.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "mxnet/c_predict_api.h" namespace mxnet{ @@ -44,6 +45,8 @@ class MXNetPredictor { std::vector input_shapes_data_; PredictorHandle pred_hnd_ = nullptr; + + static std::mutex mutex_; }; } diff --git a/PhysicsTools/MXNet/src/MXNetPredictor.cc b/PhysicsTools/MXNet/src/MXNetPredictor.cc index 29604decfa6c2..854a34f54be83 100644 --- a/PhysicsTools/MXNet/src/MXNetPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetPredictor.cc @@ -22,6 +22,7 @@ BufferFile::BufferFile(std::string file_path) : file_path_(file_path) { ifs.close(); } +std::mutex MXNetPredictor::mutex_; MXNetPredictor::MXNetPredictor() { } @@ -72,18 +73,23 @@ void MXNetPredictor::load_model(const BufferFile* model_data, const BufferFile* // Create Predictor int dev_type = 1; // 1: cpu, 2: gpu int dev_id = 0; // arbitrary. - int status = MXPredCreate(model_data->GetBuffer(), - param_data->GetBuffer(), - param_data->GetLength(), - dev_type, - dev_id, - num_input_nodes_, - (const char**) input_keys_.data(), - input_shapes_indices_.data(), - input_shapes_data_.data(), - &pred_hnd_); - if (status != 0) - throw cms::Exception("RuntimeError") << "Cannot create predictor!"; + { + std::lock_guard lock(mutex_); + int status = MXPredCreate(model_data->GetBuffer(), + param_data->GetBuffer(), + param_data->GetLength(), + dev_type, + dev_id, + num_input_nodes_, + (const char**) input_keys_.data(), + input_shapes_indices_.data(), + input_shapes_data_.data(), + &pred_hnd_); + if (status != 0){ + throw cms::Exception("RuntimeError") << "Cannot create predictor! " << MXGetLastError(); + } + + } } std::vector MXNetPredictor::predict(const std::vector >& input_data, mx_uint output_index) { @@ -93,13 +99,13 @@ std::vector MXNetPredictor::predict(const std::vector MXNetPredictor::predict(const std::vector MXNetPredictor::predict(const std::vector prediction(size); if (MXPredGetOutput(pred_hnd_, output_index, &(prediction[0]), size) != 0){ - throw cms::Exception("RuntimeError") << "Error getting output values!"; + throw cms::Exception("RuntimeError") << "Error getting output values! " << MXGetLastError(); } return prediction; From a976d4e4e4abbd322287fcedeb1948ede1540e50 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Sun, 22 Jul 2018 21:56:25 +0200 Subject: [PATCH 13/31] Switch to MXNet C++ API. --- .../MXNet/interface/MXNetCppPredictor.h | 80 ++++++++++ PhysicsTools/MXNet/src/MXNetCppPredictor.cc | 147 ++++++++++++++++++ PhysicsTools/MXNet/test/BuildFile.xml | 8 + .../MXNet/test/testMXNetCppPredictor.cc | 52 +++++++ .../plugins/DeepBoostedJetTagsProducer.cc | 46 +++--- 5 files changed, 306 insertions(+), 27 deletions(-) create mode 100644 PhysicsTools/MXNet/interface/MXNetCppPredictor.h create mode 100644 PhysicsTools/MXNet/src/MXNetCppPredictor.cc create mode 100644 PhysicsTools/MXNet/test/testMXNetCppPredictor.cc diff --git a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h new file mode 100644 index 0000000000000..f905eee983eab --- /dev/null +++ b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h @@ -0,0 +1,80 @@ +/* + * MXNetCppPredictor.h + * + * Created on: Jul 19, 2018 + * Author: hqu + */ + +#ifndef PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ +#define PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ + +#include +#include +#include + +#include "mxnet-cpp/MxNetCpp.h" + +namespace mxnet { + +namespace cpp { + +// note: Most of the objects in mxnet::cpp are effective just shared_ptr's + +// Simple class to hold MXNet model (symbol + params) +// designed to be sharable by multiple threads +class Block { +public: + Block(); + Block(const std::string &symbol_file, const std::string ¶m_file); + virtual ~Block(); + + const Symbol& symbol() const { return sym_; } + const std::map& arg_map() const { return arg_map_; } + const std::map& aux_map() const { return aux_map_; } + +private: + void load_parameters(const std::string& param_file); + + // symbol + Symbol sym_; + // argument arrays + std::map arg_map_; + // auxiliary arrays + std::map aux_map_; +}; + +// Simple helper class to run prediction +// this cannot be shared between threads +class MXNetCppPredictor { +public: + MXNetCppPredictor(); + MXNetCppPredictor(const Block &block); + virtual ~MXNetCppPredictor(); + + void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); + void set_output_node_name(const std::string& output_node_name); + const std::vector& predict(const std::vector>& input_data); + +private: + void bind_executor(); + + // context + static const Context context_; + // executor + std::unique_ptr exec_; + // symbol + Symbol sym_; + // argument arrays + std::map arg_map_; + // auxiliary arrays + std::map aux_map_; + // output of the prediction + std::vector pred_; + // names of the input nodes + std::vector input_names_; +}; + +} /* namespace cpp */ +} /* namespace mxnet */ + +#endif /* PHYSICSTOOLS_MXNET_MXNETCPPPREDICTOR_H_ */ diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc new file mode 100644 index 0000000000000..1ff91e90441bf --- /dev/null +++ b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc @@ -0,0 +1,147 @@ +/* + * MXNetCppPredictor.cc + * + * Created on: Jul 19, 2018 + * Author: hqu + */ + +#include +#include "FWCore/Utilities/interface/Exception.h" + +#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" + +namespace mxnet { + +namespace cpp { + +Block::Block() { +} + +Block::Block(const std::string& symbol_file, const std::string& param_file) { + // load the symbol + sym_ = Symbol::Load(symbol_file); + // load the parameters + load_parameters(param_file); +} + +Block::~Block() { +} + +void Block::load_parameters(const std::string& param_file) { + std::map paramters; + NDArray::Load(param_file, nullptr, ¶mters); + for (const auto &k : paramters) { + if (k.first.substr(0, 4) == "aux:") { + auto name = k.first.substr(4, k.first.size() - 4); + aux_map_[name] = k.second; + } + if (k.first.substr(0, 4) == "arg:") { + auto name = k.first.substr(4, k.first.size() - 4); + arg_map_[name] = k.second; + } + } +} + +const Context MXNetCppPredictor::context_ = Context(DeviceType::kCPU, 0); + +MXNetCppPredictor::MXNetCppPredictor() { +} + +MXNetCppPredictor::MXNetCppPredictor(const Block& block) : sym_(block.symbol()), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { +} + +MXNetCppPredictor::~MXNetCppPredictor() { +} + +void MXNetCppPredictor::set_input_shapes(const std::vector& input_names, const std::vector >& input_shapes) { + assert(input_names.size() == input_shapes.size()); + input_names_ = input_names; + // init the input NDArrays and add them to the arg_map + for (unsigned i=0; i& MXNetCppPredictor::predict(const std::vector >& input_data) { + assert(input_names_.size() == input_data.size()); + + try { + // create the executor (if not done yet) + if (!exec_) { bind_executor(); } + assert(exec_); + // set the inputs + for (unsigned i=0; iForward(false); + // copy the output to pred_ + exec_->outputs[0].SyncCopyToCPU(&pred_); + return pred_; + }catch(const dmlc::Error &e){ + throw cms::Exception("RuntimeError") << e.what() << MXGetLastError(); + } +} + +void MXNetCppPredictor::bind_executor() { + + // infer shapes + const auto arg_name_list = sym_.ListArguments(); + std::vector > in_shapes, aux_shapes, out_shapes; + std::map > arg_shapes; + + for (const auto &arg_name : arg_name_list) { + auto iter = arg_map_.find(arg_name); + if (iter != arg_map_.end()) { + arg_shapes[arg_name] = iter->second.GetShape(); + } + } + sym_.InferShape(arg_shapes, &in_shapes, &aux_shapes, &out_shapes); + + // init argument arrays + std::vector arg_arrays; + for (size_t i = 0; i < in_shapes.size(); ++i) { + const auto &shape = in_shapes[i]; + const auto &arg_name = arg_name_list[i]; + auto iter_arg = arg_map_.find(arg_name); + if (iter_arg != arg_map_.end()) { + arg_arrays.push_back(iter_arg->second); + } else { + arg_arrays.push_back(NDArray(shape, context_, false)); + } + } + std::vector grad_arrays(arg_arrays.size()); + std::vector grad_reqs(arg_arrays.size(), kNullOp); + + // init auxiliary array + std::vector aux_arrays; + const auto aux_name_list = sym_.ListAuxiliaryStates(); + for (size_t i = 0; i < aux_shapes.size(); ++i) { + const auto &shape = aux_shapes[i]; + const auto &aux_name = aux_name_list[i]; + auto iter_aux = aux_map_.find(aux_name); + if (iter_aux != aux_map_.end()) { + aux_arrays.push_back(iter_aux->second); + } else { + aux_arrays.push_back(NDArray(shape, context_, false)); + } + } + + // bind executor + exec_.reset(new Executor(sym_, context_, arg_arrays, grad_arrays, grad_reqs, aux_arrays)); + +} + +} + +} /* namespace mxnet */ + diff --git a/PhysicsTools/MXNet/test/BuildFile.xml b/PhysicsTools/MXNet/test/BuildFile.xml index fc4718f406f6f..eb90bcb46ce33 100644 --- a/PhysicsTools/MXNet/test/BuildFile.xml +++ b/PhysicsTools/MXNet/test/BuildFile.xml @@ -6,3 +6,11 @@ + + + + + + + + diff --git a/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc b/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc new file mode 100644 index 0000000000000..8d691f3f9c2b6 --- /dev/null +++ b/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc @@ -0,0 +1,52 @@ +#include + +#include "FWCore/ParameterSet/interface/FileInPath.h" +#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" + +using namespace mxnet::cpp; + +class testMXNetCppPredictor : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(testMXNetCppPredictor); + CPPUNIT_TEST(checkAll); + CPPUNIT_TEST_SUITE_END(); + +public: + void checkAll(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(testMXNetCppPredictor); + +void testMXNetCppPredictor::checkAll() +{ + + std::string model_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-symbol.json").fullPath(); + std::string param_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-0000.params").fullPath(); + + // load model and params + Block *block = nullptr; + CPPUNIT_ASSERT_NO_THROW( block = new Block(model_path, param_path) ); + CPPUNIT_ASSERT(block!=nullptr); + + // create predictor + MXNetCppPredictor predictor(*block); + + // set input shape + std::vector input_names {"data"}; + std::vector> input_shapes {{1, 3}}; + CPPUNIT_ASSERT_NO_THROW( predictor.set_input_shapes(input_names, input_shapes) ); + + // run predictor + std::vector> data {{1, 2, 3,}}; + std::vector outputs; + CPPUNIT_ASSERT_NO_THROW( outputs = predictor.predict(data) ); + + // check outputs + CPPUNIT_ASSERT(outputs.size() == 3); + CPPUNIT_ASSERT(outputs.at(0) == 42); + CPPUNIT_ASSERT(outputs.at(1) == 42); + CPPUNIT_ASSERT(outputs.at(2) == 42); + + delete block; + +} diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc index d4a393cc3e81d..59abafa6c249c 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -13,20 +13,17 @@ #include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" -#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" +#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" #include #include -// Currently hold the raw model/param data in the edm::GlobalCache. -// Can be improved by holding the mxnet Symbol/NDArrays in the cache -// when moving to the MXNet C++ API. -struct MXBufferFileCache { - MXBufferFileCache() : model_data(nullptr), param_data(nullptr) { +// Hold the mxnet model block (symbol + params) in the edm::GlobalCache. +struct MXBlockCache { + MXBlockCache() : block(nullptr) { } - std::atomic model_data; - std::atomic param_data; + std::atomic block; }; // struct to hold preprocessing parameters @@ -52,16 +49,16 @@ struct PreprocessParams { } }; -class DeepBoostedJetTagsProducer : public edm::stream::EDProducer> { +class DeepBoostedJetTagsProducer : public edm::stream::EDProducer> { public: - explicit DeepBoostedJetTagsProducer(const edm::ParameterSet&, const MXBufferFileCache*); + explicit DeepBoostedJetTagsProducer(const edm::ParameterSet&, const MXBlockCache*); ~DeepBoostedJetTagsProducer() override; static void fillDescriptions(edm::ConfigurationDescriptions&); - static std::unique_ptr initializeGlobalCache(const edm::ParameterSet&); - static void globalEndJob(const MXBufferFileCache*); + static std::unique_ptr initializeGlobalCache(const edm::ParameterSet&); + static void globalEndJob(const MXBlockCache*); private: typedef std::vector TagInfoCollection; @@ -84,12 +81,12 @@ class DeepBoostedJetTagsProducer : public edm::stream::EDProducer prep_info_map_; // preprocessing info for each input group std::vector> data_; - std::unique_ptr predictor_; + std::unique_ptr predictor_; bool debug_ = false; }; -DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& iConfig, const MXBufferFileCache* cache) +DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& iConfig, const MXBlockCache* cache) : src_(consumes(iConfig.getParameter("src"))) , debug_(iConfig.getUntrackedParameter("debugMode", false)) { @@ -130,9 +127,8 @@ DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& } // init MXNetPredictor - predictor_ = std::make_unique(); + predictor_.reset(new mxnet::cpp::MXNetCppPredictor(*cache->block)); predictor_->set_input_shapes(input_names_, input_shapes_); - predictor_->load_model(cache->model_data, cache->param_data); // get output names from flav_table const auto & flav_pset = iConfig.getParameter("flav_table"); @@ -190,7 +186,7 @@ void DeepBoostedJetTagsProducer::fillDescriptions(edm::ConfigurationDescriptions descriptions.add("pfDeepBoostedJetTags", desc); } -std::unique_ptr DeepBoostedJetTagsProducer::initializeGlobalCache( +std::unique_ptr DeepBoostedJetTagsProducer::initializeGlobalCache( const edm::ParameterSet& iConfig) { // get the model files @@ -198,19 +194,15 @@ std::unique_ptr DeepBoostedJetTagsProducer::initializeGlobalC std::string param_file = iConfig.getParameter("param_path").fullPath(); // load the model and params and save it in the cache - MXBufferFileCache* cache = new MXBufferFileCache(); - cache->model_data = new mxnet::BufferFile(model_file); - cache->param_data = new mxnet::BufferFile(param_file); - return std::unique_ptr(cache); + MXBlockCache* cache = new MXBlockCache(); + cache->block = new mxnet::cpp::Block(model_file, param_file); + return std::unique_ptr(cache); } -void DeepBoostedJetTagsProducer::globalEndJob(const MXBufferFileCache* cache) +void DeepBoostedJetTagsProducer::globalEndJob(const MXBlockCache* cache) { - if (cache->model_data != nullptr) { - delete cache->model_data; - } - if (cache->param_data != nullptr) { - delete cache->param_data; + if (cache->block != nullptr) { + delete cache->block; } } From 830e1c343e4a08c196ce9e1dcd73d198f91bf04d Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 8 Aug 2018 16:56:23 +0200 Subject: [PATCH 14/31] Protect executor creatation with a mutex in MXNetCppPredictor. --- PhysicsTools/MXNet/interface/MXNetCppPredictor.h | 2 ++ PhysicsTools/MXNet/src/MXNetCppPredictor.cc | 3 +++ 2 files changed, 5 insertions(+) diff --git a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h index f905eee983eab..636b83845cda8 100644 --- a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h +++ b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "mxnet-cpp/MxNetCpp.h" @@ -57,6 +58,7 @@ class MXNetCppPredictor { private: void bind_executor(); + static std::mutex mutex_; // context static const Context context_; diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc index 1ff91e90441bf..9ba77f1ba3543 100644 --- a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc @@ -42,6 +42,7 @@ void Block::load_parameters(const std::string& param_file) { } } +std::mutex MXNetCppPredictor::mutex_; const Context MXNetCppPredictor::context_ = Context(DeviceType::kCPU, 0); MXNetCppPredictor::MXNetCppPredictor() { @@ -93,6 +94,8 @@ const std::vector& MXNetCppPredictor::predict(const std::vector lock(mutex_); // infer shapes const auto arg_name_list = sym_.ListArguments(); From 0966b2486829efa4801f3f13fb18d69e0e1e583e Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Sun, 12 Aug 2018 19:39:56 +0200 Subject: [PATCH 15/31] Refactor MXNetCppPredictor. Re-bind executor every time for thread-safety. --- .../MXNet/interface/MXNetCppPredictor.h | 14 +++++++-- PhysicsTools/MXNet/src/MXNetCppPredictor.cc | 30 +++++++++---------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h index 636b83845cda8..140944f921db8 100644 --- a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h +++ b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h @@ -30,6 +30,7 @@ class Block { virtual ~Block(); const Symbol& symbol() const { return sym_; } + Symbol symbol(const std::string &output_node) const { return sym_.GetInternals()[output_node]; } const std::map& arg_map() const { return arg_map_; } const std::map& aux_map() const { return aux_map_; } @@ -50,16 +51,18 @@ class MXNetCppPredictor { public: MXNetCppPredictor(); MXNetCppPredictor(const Block &block); + MXNetCppPredictor(const Block &block, const std::string &output_node); virtual ~MXNetCppPredictor(); void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); - void set_output_node_name(const std::string& output_node_name); const std::vector& predict(const std::vector>& input_data); private: - void bind_executor(); static std::mutex mutex_; + void infer_shapes(); + void bind_executor(); + // context static const Context context_; // executor @@ -74,6 +77,13 @@ class MXNetCppPredictor { std::vector pred_; // names of the input nodes std::vector input_names_; + + // internal states + std::vector arg_arrays; + std::vector grad_arrays; + std::vector grad_reqs; + std::vector aux_arrays; + }; } /* namespace cpp */ diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc index 9ba77f1ba3543..0e6c57c0dc8c9 100644 --- a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc @@ -51,6 +51,9 @@ MXNetCppPredictor::MXNetCppPredictor() { MXNetCppPredictor::MXNetCppPredictor(const Block& block) : sym_(block.symbol()), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { } +MXNetCppPredictor::MXNetCppPredictor(const Block &block, const std::string &output_node) : sym_(block.symbol(output_node)), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { +} + MXNetCppPredictor::~MXNetCppPredictor() { } @@ -63,21 +66,16 @@ void MXNetCppPredictor::set_input_shapes(const std::vector& input_n NDArray nd(input_shapes.at(i), context_, false); arg_map_[name] = nd; } -} - -void MXNetCppPredictor::set_output_node_name(const std::string& output_node_name) { - if (!output_node_name.empty()){ - sym_ = sym_.GetInternals()[output_node_name]; - } + // infer parameter shapes from input shapes + infer_shapes(); } const std::vector& MXNetCppPredictor::predict(const std::vector >& input_data) { assert(input_names_.size() == input_data.size()); try { - // create the executor (if not done yet) - if (!exec_) { bind_executor(); } - assert(exec_); + // bind executor + bind_executor(); // set the inputs for (unsigned i=0; i& MXNetCppPredictor::predict(const std::vector lock(mutex_); @@ -111,7 +109,7 @@ void MXNetCppPredictor::bind_executor() { sym_.InferShape(arg_shapes, &in_shapes, &aux_shapes, &out_shapes); // init argument arrays - std::vector arg_arrays; + arg_arrays.clear(); for (size_t i = 0; i < in_shapes.size(); ++i) { const auto &shape = in_shapes[i]; const auto &arg_name = arg_name_list[i]; @@ -122,11 +120,11 @@ void MXNetCppPredictor::bind_executor() { arg_arrays.push_back(NDArray(shape, context_, false)); } } - std::vector grad_arrays(arg_arrays.size()); - std::vector grad_reqs(arg_arrays.size(), kNullOp); + grad_arrays = std::vector(arg_arrays.size()); + grad_reqs = std::vector(arg_arrays.size(), kNullOp); // init auxiliary array - std::vector aux_arrays; + aux_arrays.clear(); const auto aux_name_list = sym_.ListAuxiliaryStates(); for (size_t i = 0; i < aux_shapes.size(); ++i) { const auto &shape = aux_shapes[i]; @@ -139,9 +137,11 @@ void MXNetCppPredictor::bind_executor() { } } +} + +void MXNetCppPredictor::bind_executor() { // bind executor exec_.reset(new Executor(sym_, context_, arg_arrays, grad_arrays, grad_reqs, aux_arrays)); - } } From e8632e0acf041cf303a9e4f654edc5c662e1b02b Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 02:14:55 +0200 Subject: [PATCH 16/31] Remove MXNet C API. --- PhysicsTools/MXNet/interface/MXNetPredictor.h | 54 -------- PhysicsTools/MXNet/src/MXNetPredictor.cc | 129 ------------------ PhysicsTools/MXNet/test/BuildFile.xml | 8 -- PhysicsTools/MXNet/test/testMXNetPredictor.cc | 58 -------- 4 files changed, 249 deletions(-) delete mode 100644 PhysicsTools/MXNet/interface/MXNetPredictor.h delete mode 100644 PhysicsTools/MXNet/src/MXNetPredictor.cc delete mode 100644 PhysicsTools/MXNet/test/testMXNetPredictor.cc diff --git a/PhysicsTools/MXNet/interface/MXNetPredictor.h b/PhysicsTools/MXNet/interface/MXNetPredictor.h deleted file mode 100644 index 763d46181c12d..0000000000000 --- a/PhysicsTools/MXNet/interface/MXNetPredictor.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H -#define PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H - -#include -#include -#include -#include -#include "mxnet/c_predict_api.h" - -namespace mxnet{ -// Read file to buffer -class BufferFile { -public: - - explicit BufferFile(std::string file_path); - ~BufferFile() {} - - int GetLength() const { - return buffer_.length(); - } - const char* GetBuffer() const { - return buffer_.c_str(); - } - -private: - std::string file_path_; - std::string buffer_; -}; - -class MXNetPredictor { -public: - MXNetPredictor(); - virtual ~MXNetPredictor(); - - void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); - void load_model(const BufferFile* model_data, const BufferFile* param_data); - std::vector predict(const std::vector>& input_data, mx_uint output_index = 0); - -private: - mx_uint num_input_nodes_ = 0; - std::vector input_names_; - std::vector input_keys_; - - std::vector input_shapes_indices_; - std::vector input_shapes_data_; - - PredictorHandle pred_hnd_ = nullptr; - - static std::mutex mutex_; -}; - -} - -#endif /* PHYSICSTOOLS_MXNET_MXNETPREDICTOR_H */ diff --git a/PhysicsTools/MXNet/src/MXNetPredictor.cc b/PhysicsTools/MXNet/src/MXNetPredictor.cc deleted file mode 100644 index 854a34f54be83..0000000000000 --- a/PhysicsTools/MXNet/src/MXNetPredictor.cc +++ /dev/null @@ -1,129 +0,0 @@ -#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" - -#include -#include -#include -#include -#include -#include "FWCore/MessageLogger/interface/MessageLogger.h" -#include "FWCore/Utilities/interface/Exception.h" - -using namespace mxnet; - -BufferFile::BufferFile(std::string file_path) : file_path_(file_path) { - std::ifstream ifs(file_path.c_str(), std::ios::in | std::ios::binary); - if (!ifs) { - throw cms::Exception("InvalidArgument") << "Can't open the file. Please check " << file_path << ". \n"; - return; - } - - buffer_.assign(std::istreambuf_iterator(ifs), std::istreambuf_iterator()); - edm::LogInfo("NNKit") << file_path.c_str() << " ... "<< buffer_.length() << " bytes"; - ifs.close(); -} - -std::mutex MXNetPredictor::mutex_; - -MXNetPredictor::MXNetPredictor() { -} - -MXNetPredictor::~MXNetPredictor() { - if (pred_hnd_){ - // Release Predictor - MXPredFree(pred_hnd_); - } -} - -void MXNetPredictor::set_input_shapes(const std::vector& input_names, const std::vector >& input_shapes) { - assert(input_names.size() == input_shapes.size()); - - input_names_ = input_names; - num_input_nodes_ = input_names_.size(); - for (const auto &name : input_names_){ - input_keys_.push_back(name.c_str()); - } - - input_shapes_data_.clear(); - input_shapes_indices_ = {0}; - unsigned pos = 0; - for (const auto &shape : input_shapes){ - pos += shape.size(); - input_shapes_indices_.push_back(pos); - input_shapes_data_.insert(input_shapes_data_.end(), shape.begin(), shape.end()); - } -} - -void MXNetPredictor::load_model(const BufferFile* model_data, const BufferFile* param_data) { - if (model_data->GetLength() == 0 || - param_data->GetLength() == 0) { - throw cms::Exception("InvalidArgument") << "Invalid input"; - } - - std::stringstream msg; - msg << "input_shapes_indices_:\n"; - for (const auto &d : input_shapes_indices_) { - msg << d << ","; - } - msg << "\ninput_shapes_data_:\n"; - for (const auto &d : input_shapes_data_) { - msg << d << ","; - } - edm::LogInfo("NNKit") << msg.str(); - - // Create Predictor - int dev_type = 1; // 1: cpu, 2: gpu - int dev_id = 0; // arbitrary. - { - std::lock_guard lock(mutex_); - int status = MXPredCreate(model_data->GetBuffer(), - param_data->GetBuffer(), - param_data->GetLength(), - dev_type, - dev_id, - num_input_nodes_, - (const char**) input_keys_.data(), - input_shapes_indices_.data(), - input_shapes_data_.data(), - &pred_hnd_); - if (status != 0){ - throw cms::Exception("RuntimeError") << "Cannot create predictor! " << MXGetLastError(); - } - - } -} - -std::vector MXNetPredictor::predict(const std::vector >& input_data, mx_uint output_index) { - assert(input_data.size() == input_names_.size()); - assert(pred_hnd_); - - // set input data - for (unsigned i=0; i prediction(size); - - if (MXPredGetOutput(pred_hnd_, output_index, &(prediction[0]), size) != 0){ - throw cms::Exception("RuntimeError") << "Error getting output values! " << MXGetLastError(); - } - - return prediction; -} diff --git a/PhysicsTools/MXNet/test/BuildFile.xml b/PhysicsTools/MXNet/test/BuildFile.xml index eb90bcb46ce33..6be2c0ac674f7 100644 --- a/PhysicsTools/MXNet/test/BuildFile.xml +++ b/PhysicsTools/MXNet/test/BuildFile.xml @@ -1,11 +1,3 @@ - - - - - - - - diff --git a/PhysicsTools/MXNet/test/testMXNetPredictor.cc b/PhysicsTools/MXNet/test/testMXNetPredictor.cc deleted file mode 100644 index 23067fac668d5..0000000000000 --- a/PhysicsTools/MXNet/test/testMXNetPredictor.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include - -#include "FWCore/ParameterSet/interface/FileInPath.h" -#include "PhysicsTools/MXNet/interface/MXNetPredictor.h" - -class testMXNetPredictor : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE(testMXNetPredictor); - CPPUNIT_TEST(checkAll); - CPPUNIT_TEST_SUITE_END(); - -public: - void checkAll(); -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(testMXNetPredictor); - -void testMXNetPredictor::checkAll() -{ - - // load model and params into BufferFile - std::string model_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-symbol.json").fullPath(); - std::string param_path = edm::FileInPath("PhysicsTools/MXNet/test/data/testmxnet-0000.params").fullPath(); - - mxnet::BufferFile *model_file = new mxnet::BufferFile(model_path); - CPPUNIT_ASSERT(model_file != nullptr); - CPPUNIT_ASSERT(model_file->GetLength() > 0); - - mxnet::BufferFile *param_file = new mxnet::BufferFile(param_path); - CPPUNIT_ASSERT(param_file != nullptr); - CPPUNIT_ASSERT(param_file->GetLength() > 0); - - // create predictor - mxnet::MXNetPredictor predictor; - - // set input shape - std::vector input_names {"data"}; - std::vector> input_shapes {{1, 3}}; - CPPUNIT_ASSERT_NO_THROW( predictor.set_input_shapes(input_names, input_shapes) ); - - // load model from BufferFile - CPPUNIT_ASSERT_NO_THROW( predictor.load_model(model_file, param_file) ); - - // run predictor - std::vector> data {{1, 2, 3,}}; - std::vector outputs; - CPPUNIT_ASSERT_NO_THROW( outputs = predictor.predict(data) ); - - // check outputs - CPPUNIT_ASSERT(outputs.size() == 3); - CPPUNIT_ASSERT(outputs.at(0) == 42); - CPPUNIT_ASSERT(outputs.at(1) == 42); - CPPUNIT_ASSERT(outputs.at(2) == 42); - - delete model_file; - delete param_file; - -} From 03fc1967378a88965a17ad97cc4a3d6c4014bde9 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 02:50:51 +0200 Subject: [PATCH 17/31] Move non-TF code in `RecoBTag/TensorFlow` to `RecoBTag/FeatureTools` --- RecoBTag/DeepBoostedJet/plugins/BuildFile.xml | 2 +- .../plugins/DeepBoostedJetTagInfoProducer.cc | 4 +-- RecoBTag/FeatureTools/BuildFile.xml | 6 ++++ .../BoostedDoubleSVTagInfoConverter.h | 12 +++---- .../interface/ChargedCandidateConverter.h | 34 +++++++++--------- .../interface/JetConverter.h | 14 ++++---- .../interface/NeutralCandidateConverter.h | 36 +++++++++---------- .../interface/SecondaryVertexConverter.h | 16 ++++----- .../interface/ShallowTagInfoConverter.h | 12 +++---- .../interface/TrackInfoBuilder.h | 6 ++-- .../interface/deep_helpers.h | 6 ++-- .../interface/sorting_modules.h | 8 ++--- .../src/BoostedDoubleSVTagInfoConverter.cc | 12 +++---- .../src/ChargedCandidateConverter.cc | 2 +- RecoBTag/FeatureTools/src/JetConverter.cc | 4 +++ .../src/NeutralCandidateConverter.cc | 30 ++++++++-------- .../src/SecondaryVertexConverter.cc | 16 ++++----- .../src/ShallowTagInfoConverter.cc | 14 ++++---- .../src/TrackInfoBuilder.cc | 2 +- .../src/deep_helpers.cc | 2 +- .../src/sorting_modules.cc | 2 +- RecoBTag/TensorFlow/BuildFile.xml | 2 -- RecoBTag/TensorFlow/plugins/BuildFile.xml | 1 + .../plugins/DeepDoubleBTagInfoProducer.cc | 14 ++++---- .../plugins/DeepFlavourTagInfoProducer.cc | 24 ++++++------- RecoBTag/TensorFlow/src/JetConverter.cc | 4 --- 26 files changed, 145 insertions(+), 140 deletions(-) create mode 100644 RecoBTag/FeatureTools/BuildFile.xml rename RecoBTag/{TensorFlow => FeatureTools}/interface/BoostedDoubleSVTagInfoConverter.h (59%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/ChargedCandidateConverter.h (87%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/JetConverter.h (74%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/NeutralCandidateConverter.h (83%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/SecondaryVertexConverter.h (66%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/ShallowTagInfoConverter.h (59%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/TrackInfoBuilder.h (92%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/deep_helpers.h (95%) rename RecoBTag/{TensorFlow => FeatureTools}/interface/sorting_modules.h (93%) rename RecoBTag/{TensorFlow => FeatureTools}/src/BoostedDoubleSVTagInfoConverter.cc (95%) rename RecoBTag/{TensorFlow => FeatureTools}/src/ChargedCandidateConverter.cc (96%) create mode 100644 RecoBTag/FeatureTools/src/JetConverter.cc rename RecoBTag/{TensorFlow => FeatureTools}/src/NeutralCandidateConverter.cc (90%) rename RecoBTag/{TensorFlow => FeatureTools}/src/SecondaryVertexConverter.cc (90%) rename RecoBTag/{TensorFlow => FeatureTools}/src/ShallowTagInfoConverter.cc (90%) rename RecoBTag/{TensorFlow => FeatureTools}/src/TrackInfoBuilder.cc (98%) rename RecoBTag/{TensorFlow => FeatureTools}/src/deep_helpers.cc (98%) rename RecoBTag/{TensorFlow => FeatureTools}/src/sorting_modules.cc (87%) delete mode 100644 RecoBTag/TensorFlow/src/JetConverter.cc diff --git a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml index fa7a7cf8bcc61..0b15cd13b46d7 100644 --- a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml +++ b/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml @@ -3,7 +3,7 @@ - + diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index 3188032661ee8..5b1290e084082 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -13,8 +13,8 @@ #include "DataFormats/PatCandidates/interface/PackedCandidate.h" #include "TrackingTools/Records/interface/TransientTrackRecord.h" -#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/TrackInfoBuilder.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" #include "DataFormats/VertexReco/interface/VertexFwd.h" diff --git a/RecoBTag/FeatureTools/BuildFile.xml b/RecoBTag/FeatureTools/BuildFile.xml new file mode 100644 index 0000000000000..af9ecf390f336 --- /dev/null +++ b/RecoBTag/FeatureTools/BuildFile.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/RecoBTag/TensorFlow/interface/BoostedDoubleSVTagInfoConverter.h b/RecoBTag/FeatureTools/interface/BoostedDoubleSVTagInfoConverter.h similarity index 59% rename from RecoBTag/TensorFlow/interface/BoostedDoubleSVTagInfoConverter.h rename to RecoBTag/FeatureTools/interface/BoostedDoubleSVTagInfoConverter.h index 9edec9a8d198d..0781da30a7724 100644 --- a/RecoBTag/TensorFlow/interface/BoostedDoubleSVTagInfoConverter.h +++ b/RecoBTag/FeatureTools/interface/BoostedDoubleSVTagInfoConverter.h @@ -1,19 +1,19 @@ -#ifndef RecoBTag_TensorFlow_BoostedDoubleSVTagInfoConverter_h -#define RecoBTag_TensorFlow_BoostedDoubleSVTagInfoConverter_h +#ifndef RecoBTag_FeatureTools_BoostedDoubleSVTagInfoConverter_h +#define RecoBTag_FeatureTools_BoostedDoubleSVTagInfoConverter_h -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/BoostedDoubleSVTagInfoFeatures.h" #include "DataFormats/BTauReco/interface/BoostedDoubleSVTagInfo.h" #include "DataFormats/BTauReco/interface/TaggingVariable.h" namespace btagbtvdeep { - + void doubleBTagToFeatures(const reco::TaggingVariableList & tag_info_vars, BoostedDoubleSVTagInfoFeatures & tag_info_features) ; - + } -#endif //RecoBTag_TensorFlow_BoostedDoubleSVTagInfoConverter_h +#endif //RecoBTag_FeatureTools_BoostedDoubleSVTagInfoConverter_h diff --git a/RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h b/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h similarity index 87% rename from RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h rename to RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h index c4b24254f5cf0..9c0f3f261e565 100644 --- a/RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h +++ b/RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h @@ -1,8 +1,8 @@ -#ifndef RecoBTag_TensorFlow_ChargedCandidateConverter_h -#define RecoBTag_TensorFlow_ChargedCandidateConverter_h +#ifndef RecoBTag_FeatureTools_ChargedCandidateConverter_h +#define RecoBTag_FeatureTools_ChargedCandidateConverter_h -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" -#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/TrackInfoBuilder.h" #include "DataFormats/BTauReco/interface/ChargedCandidateFeatures.h" #include "DataFormats/PatCandidates/interface/PackedCandidate.h" @@ -10,7 +10,7 @@ #include "DataFormats/PatCandidates/interface/Jet.h" namespace btagbtvdeep { - + template void commonCandidateToFeatures(const CandidateType * c_pf, const reco::Jet & jet, @@ -18,7 +18,7 @@ namespace btagbtvdeep { const float & drminpfcandsv, const float & jetR, ChargedCandidateFeatures & c_pf_features, const bool flip = false) { - + float trackSip2dVal = track_info.getTrackSip2dVal(); float trackSip2dSig = track_info.getTrackSip2dSig(); @@ -33,7 +33,7 @@ namespace btagbtvdeep { c_pf_features.ptrel = catch_infs_and_bound(c_pf->pt()/jet.pt(), 0,-1,0,-1); - + c_pf_features.btagPf_trackEtaRel =catch_infs_and_bound(track_info.getTrackEtaRel(), 0,-5,15); c_pf_features.btagPf_trackPtRel =catch_infs_and_bound(track_info.getTrackPtRel(), 0,-1,4); c_pf_features.btagPf_trackPPar =catch_infs_and_bound(track_info.getTrackPPar(), 0,-1e5,1e5 ); @@ -45,29 +45,29 @@ namespace btagbtvdeep { c_pf_features.btagPf_trackSip2dVal =catch_infs_and_bound(trackSip2dVal, 0, -1,70 ); c_pf_features.btagPf_trackSip2dSig =catch_infs_and_bound(trackSip2dSig, 0, -1,4e4 ); c_pf_features.btagPf_trackJetDistVal =catch_infs_and_bound(track_info.getTrackJetDistVal(),0,-20,1 ); - + c_pf_features.drminsv = catch_infs_and_bound(drminpfcandsv,0,-1.*jetR,0,-1.*jetR); - + } - + void packedCandidateToFeatures(const pat::PackedCandidate * c_pf, const pat::Jet & jet, const TrackInfoBuilder & track_info, - const float drminpfcandsv, const float jetR, + const float drminpfcandsv, const float jetR, ChargedCandidateFeatures & c_pf_features, const bool flip = false) ; - - + + void recoCandidateToFeatures(const reco::PFCandidate * c_pf, const reco::Jet & jet, const TrackInfoBuilder & track_info, const float drminpfcandsv, const float jetR, const float puppiw, const int pv_ass_quality, - const reco::VertexRef & pv, + const reco::VertexRef & pv, ChargedCandidateFeatures & c_pf_features, const bool flip = false) ; - - + + } -#endif //RecoBTag_TensorFlow_ChargedCandidateConverter_h +#endif //RecoBTag_FeatureTools_ChargedCandidateConverter_h diff --git a/RecoBTag/TensorFlow/interface/JetConverter.h b/RecoBTag/FeatureTools/interface/JetConverter.h similarity index 74% rename from RecoBTag/TensorFlow/interface/JetConverter.h rename to RecoBTag/FeatureTools/interface/JetConverter.h index a12c40072296e..faf8f2b087d66 100644 --- a/RecoBTag/TensorFlow/interface/JetConverter.h +++ b/RecoBTag/FeatureTools/interface/JetConverter.h @@ -1,15 +1,15 @@ -#ifndef RecoBTag_TensorFlow_JetConverter_h -#define RecoBTag_TensorFlow_JetConverter_h +#ifndef RecoBTag_FeatureTools_JetConverter_h +#define RecoBTag_FeatureTools_JetConverter_h #include "DataFormats/BTauReco/interface/JetFeatures.h" #include "DataFormats/JetReco/interface/Jet.h" namespace btagbtvdeep { - + class JetConverter { public: - + static void jetToFeatures(const reco::Jet & jet, JetFeatures & jet_features) { jet_features.pt = jet.pt(); // uncorrected @@ -17,9 +17,9 @@ namespace btagbtvdeep { jet_features.mass = jet.mass(); jet_features.energy = jet.energy(); } - }; - + }; + } -#endif //RecoBTag_TensorFlow_JetConverter_h +#endif //RecoBTag_FeatureTools_JetConverter_h diff --git a/RecoBTag/TensorFlow/interface/NeutralCandidateConverter.h b/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h similarity index 83% rename from RecoBTag/TensorFlow/interface/NeutralCandidateConverter.h rename to RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h index 63394184bbed8..99a620e3d5e67 100644 --- a/RecoBTag/TensorFlow/interface/NeutralCandidateConverter.h +++ b/RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h @@ -1,7 +1,7 @@ -#ifndef RecoBTag_TensorFlow_NeutralCandidateConverter_h -#define RecoBTag_TensorFlow_NeutralCandidateConverter_h +#ifndef RecoBTag_FeatureTools_NeutralCandidateConverter_h +#define RecoBTag_FeatureTools_NeutralCandidateConverter_h -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/NeutralCandidateFeatures.h" #include "DataFormats/PatCandidates/interface/PackedCandidate.h" @@ -11,42 +11,42 @@ namespace btagbtvdeep { - - - + + + void packedCandidateToFeatures(const pat::PackedCandidate * n_pf, const pat::Jet & jet, const float drminpfcandsv, const float jetR, NeutralCandidateFeatures & n_pf_features) ; - - + + void recoCandidateToFeatures(const reco::PFCandidate * n_pf, const reco::Jet & jet, const float drminpfcandsv, const float jetR, const float puppiw, NeutralCandidateFeatures & n_pf_features) ; - - + + template static void commonCandidateToFeatures(const CandidateType * n_pf, const reco::Jet & jet, const float & drminpfcandsv, const float & jetR, NeutralCandidateFeatures & n_pf_features) { - + n_pf_features.ptrel = catch_infs_and_bound(n_pf->pt()/jet.pt(), 0,-1,0,-1); n_pf_features.deltaR = catch_infs_and_bound(reco::deltaR(*n_pf,jet), 0,-0.6,0,-0.6); n_pf_features.isGamma = 0; if(std::abs(n_pf->pdgId())==22) n_pf_features.isGamma = 1; - - + + n_pf_features.drminsv = catch_infs_and_bound(drminpfcandsv, 0,-1.*jetR,0,-1.*jetR); - + } - - - + + + } -#endif //RecoBTag_TensorFlow_NeutralCandidateConverter_h +#endif //RecoBTag_FeatureTools_NeutralCandidateConverter_h diff --git a/RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h b/RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h similarity index 66% rename from RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h rename to RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h index cd5eda1fecd12..361a2c8e89e1c 100644 --- a/RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h +++ b/RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h @@ -1,7 +1,7 @@ -#ifndef RecoBTag_TensorFlow_SecondaryVertexConverter_h -#define RecoBTag_TensorFlow_SecondaryVertexConverter_h +#ifndef RecoBTag_FeatureTools_SecondaryVertexConverter_h +#define RecoBTag_FeatureTools_SecondaryVertexConverter_h -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/SecondaryVertexFeatures.h" #include "DataFormats/JetReco/interface/Jet.h" @@ -9,14 +9,14 @@ #include "DataFormats/VertexReco/interface/Vertex.h" namespace btagbtvdeep { - - + + void svToFeatures( const reco::VertexCompositePtrCandidate & sv, const reco::Vertex & pv, const reco::Jet & jet, SecondaryVertexFeatures & sv_features, const bool flip = false) ; - - + + } -#endif //RecoSV_DeepFlavour_SecondaryVertexConverter_h +#endif //RecoBTag_FeatureTools_SecondaryVertexConverter_h diff --git a/RecoBTag/TensorFlow/interface/ShallowTagInfoConverter.h b/RecoBTag/FeatureTools/interface/ShallowTagInfoConverter.h similarity index 59% rename from RecoBTag/TensorFlow/interface/ShallowTagInfoConverter.h rename to RecoBTag/FeatureTools/interface/ShallowTagInfoConverter.h index 70c216b673a5d..5d942f6012069 100644 --- a/RecoBTag/TensorFlow/interface/ShallowTagInfoConverter.h +++ b/RecoBTag/FeatureTools/interface/ShallowTagInfoConverter.h @@ -1,19 +1,19 @@ -#ifndef RecoBTag_TensorFlow_ShallowTagInfoConverter_h -#define RecoBTag_TensorFlow_ShallowTagInfoConverter_h +#ifndef RecoBTag_FeatureTools_ShallowTagInfoConverter_h +#define RecoBTag_FeatureTools_ShallowTagInfoConverter_h -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/ShallowTagInfoFeatures.h" #include "DataFormats/BTauReco/interface/ShallowTagInfo.h" #include "DataFormats/BTauReco/interface/TaggingVariable.h" namespace btagbtvdeep { - + void bTagToFeatures(const reco::TaggingVariableList & tag_info_vars, ShallowTagInfoFeatures & tag_info_features); - + } -#endif //RecoBTag_TensorFlow_ShallowTagInfoConverter_h +#endif //RecoBTag_FeatureTools_ShallowTagInfoConverter_h diff --git a/RecoBTag/TensorFlow/interface/TrackInfoBuilder.h b/RecoBTag/FeatureTools/interface/TrackInfoBuilder.h similarity index 92% rename from RecoBTag/TensorFlow/interface/TrackInfoBuilder.h rename to RecoBTag/FeatureTools/interface/TrackInfoBuilder.h index 68bd9c627063d..033a6db606618 100644 --- a/RecoBTag/TensorFlow/interface/TrackInfoBuilder.h +++ b/RecoBTag/FeatureTools/interface/TrackInfoBuilder.h @@ -1,5 +1,5 @@ -#ifndef RecoBTag_TensorFlow_TrackInfoBuilder_h -#define RecoBTag_TensorFlow_TrackInfoBuilder_h +#ifndef RecoBTag_FeatureTools_TrackInfoBuilder_h +#define RecoBTag_FeatureTools_TrackInfoBuilder_h #include "DataFormats/Candidate/interface/Candidate.h" #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" @@ -53,4 +53,4 @@ class TrackInfoBuilder{ } -#endif //RecoBTag_TensorFlow_TrackInfoBuilder_h +#endif //RecoBTag_FeatureTools_TrackInfoBuilder_h diff --git a/RecoBTag/TensorFlow/interface/deep_helpers.h b/RecoBTag/FeatureTools/interface/deep_helpers.h similarity index 95% rename from RecoBTag/TensorFlow/interface/deep_helpers.h rename to RecoBTag/FeatureTools/interface/deep_helpers.h index e26a5d8c90233..e4a215b38039e 100644 --- a/RecoBTag/TensorFlow/interface/deep_helpers.h +++ b/RecoBTag/FeatureTools/interface/deep_helpers.h @@ -1,5 +1,5 @@ -#ifndef RecoBTag_TensorFlow_deep_helpers_h -#define RecoBTag_TensorFlow_deep_helpers_h +#ifndef RecoBTag_FeatureTools_deep_helpers_h +#define RecoBTag_FeatureTools_deep_helpers_h #include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h" #include "DataFormats/BTauReco/interface/TaggingVariable.h" @@ -76,4 +76,4 @@ namespace btagbtvdeep { float quality_from_pfcand(const reco::PFCandidate &pfcand); float lost_inner_hits_from_pfcand(const reco::PFCandidate &pfcand); } -#endif //RecoBTag_TensorFlow_deep_helpers_h +#endif //RecoBTag_FeatureTools_deep_helpers_h diff --git a/RecoBTag/TensorFlow/interface/sorting_modules.h b/RecoBTag/FeatureTools/interface/sorting_modules.h similarity index 93% rename from RecoBTag/TensorFlow/interface/sorting_modules.h rename to RecoBTag/FeatureTools/interface/sorting_modules.h index 9b8fc37844440..310b93b6bc0e1 100644 --- a/RecoBTag/TensorFlow/interface/sorting_modules.h +++ b/RecoBTag/FeatureTools/interface/sorting_modules.h @@ -1,5 +1,5 @@ -#ifndef RecoBTag_TensorFlow_sorting_modules_h -#define RecoBTag_TensorFlow_sorting_modules_h +#ifndef RecoBTag_FeatureTools_sorting_modules_h +#define RecoBTag_FeatureTools_sorting_modules_h #include #include @@ -25,7 +25,7 @@ class SortingClass{ t_(t), sortValA(sortA), sortValB(sortB), sortValC(sortC) {} const T& get() const {return t_;} - + enum compareResult{cmp_smaller,cmp_greater,cmp_invalid}; static inline compareResult compare(const SortingClass& a, const SortingClass& b,int validx=0){ @@ -81,4 +81,4 @@ class SortingClass{ std::vector invertSortingVector(const std::vector > & in); } -#endif //RecoBTag_TensorFlow_sorting_modules_h +#endif //RecoBTag_FeatureTools_sorting_modules_h diff --git a/RecoBTag/TensorFlow/src/BoostedDoubleSVTagInfoConverter.cc b/RecoBTag/FeatureTools/src/BoostedDoubleSVTagInfoConverter.cc similarity index 95% rename from RecoBTag/TensorFlow/src/BoostedDoubleSVTagInfoConverter.cc rename to RecoBTag/FeatureTools/src/BoostedDoubleSVTagInfoConverter.cc index e2260a8826636..adfc412ddc9fe 100644 --- a/RecoBTag/TensorFlow/src/BoostedDoubleSVTagInfoConverter.cc +++ b/RecoBTag/FeatureTools/src/BoostedDoubleSVTagInfoConverter.cc @@ -1,5 +1,5 @@ -#include "RecoBTag/TensorFlow/interface/BoostedDoubleSVTagInfoConverter.h" -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/BoostedDoubleSVTagInfoConverter.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/BoostedDoubleSVTagInfoFeatures.h" #include "DataFormats/BTauReco/interface/BoostedDoubleSVTagInfo.h" @@ -9,7 +9,7 @@ namespace btagbtvdeep { void doubleBTagToFeatures(const reco::TaggingVariableList & tag_info_vars, BoostedDoubleSVTagInfoFeatures & tag_info_features) { - + tag_info_features.jetNTracks = tag_info_vars.get(reco::btau::jetNTracks, -999); tag_info_features.jetNSecondaryVertices = tag_info_vars.get(reco::btau::jetNSecondaryVertices, -999); tag_info_features.trackSip3dSig_0 = tag_info_vars.get(reco::btau::trackSip3dSig_0, -999); @@ -38,9 +38,9 @@ namespace btagbtvdeep { tag_info_features.tau2_flightDistance2dSig = tag_info_vars.get(reco::btau::tau2_flightDistance2dSig, -999); tag_info_features.tau2_vertexDeltaR = tag_info_vars.get(reco::btau::tau2_vertexDeltaR, -999); // not used tag_info_features.z_ratio = tag_info_vars.get(reco::btau::z_ratio, -999); - - } - + + } + } diff --git a/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc b/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc similarity index 96% rename from RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc rename to RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc index 28660ecd30ee2..fe2723da687dd 100644 --- a/RecoBTag/TensorFlow/src/ChargedCandidateConverter.cc +++ b/RecoBTag/FeatureTools/src/ChargedCandidateConverter.cc @@ -1,4 +1,4 @@ -#include "RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h" +#include "RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h" namespace btagbtvdeep { diff --git a/RecoBTag/FeatureTools/src/JetConverter.cc b/RecoBTag/FeatureTools/src/JetConverter.cc new file mode 100644 index 0000000000000..3fc02477f43cb --- /dev/null +++ b/RecoBTag/FeatureTools/src/JetConverter.cc @@ -0,0 +1,4 @@ +#include "RecoBTag/FeatureTools/interface/JetConverter.h" + + + diff --git a/RecoBTag/TensorFlow/src/NeutralCandidateConverter.cc b/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc similarity index 90% rename from RecoBTag/TensorFlow/src/NeutralCandidateConverter.cc rename to RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc index 716960db602cc..371612f5c948e 100644 --- a/RecoBTag/TensorFlow/src/NeutralCandidateConverter.cc +++ b/RecoBTag/FeatureTools/src/NeutralCandidateConverter.cc @@ -1,29 +1,29 @@ -#include "RecoBTag/TensorFlow/interface/NeutralCandidateConverter.h" +#include "RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h" namespace btagbtvdeep { - - - + + + void packedCandidateToFeatures(const pat::PackedCandidate * n_pf, const pat::Jet & jet, const float drminpfcandsv, const float jetR, NeutralCandidateFeatures & n_pf_features) { - + commonCandidateToFeatures(n_pf, jet, drminpfcandsv, jetR, n_pf_features); - + n_pf_features.hadFrac = n_pf->hcalFraction(); n_pf_features.puppiw = n_pf->puppiWeight(); - - } - + + } + void recoCandidateToFeatures(const reco::PFCandidate * n_pf, const reco::Jet & jet, const float drminpfcandsv, const float jetR, const float puppiw, NeutralCandidateFeatures & n_pf_features) { - + commonCandidateToFeatures(n_pf, jet, drminpfcandsv, jetR, n_pf_features); n_pf_features.puppiw = puppiw; - + // need to get a value map and more stuff to do properly // otherwise will be different than for PackedCandidates // https://github.com/cms-sw/cmssw/blob/master/PhysicsTools/PatAlgos/python/slimming/packedPFCandidates_cfi.py @@ -32,9 +32,9 @@ namespace btagbtvdeep { } else { n_pf_features.hadFrac = 0; } - - } - - + + } + + } diff --git a/RecoBTag/TensorFlow/src/SecondaryVertexConverter.cc b/RecoBTag/FeatureTools/src/SecondaryVertexConverter.cc similarity index 90% rename from RecoBTag/TensorFlow/src/SecondaryVertexConverter.cc rename to RecoBTag/FeatureTools/src/SecondaryVertexConverter.cc index 1770e7c423487..1ab62f0d43580 100644 --- a/RecoBTag/TensorFlow/src/SecondaryVertexConverter.cc +++ b/RecoBTag/FeatureTools/src/SecondaryVertexConverter.cc @@ -1,20 +1,20 @@ -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/SecondaryVertexFeatures.h" #include "DataFormats/JetReco/interface/Jet.h" #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" #include "DataFormats/VertexReco/interface/Vertex.h" -#include "RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h" +#include "RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h" namespace btagbtvdeep { - - + + void svToFeatures( const reco::VertexCompositePtrCandidate & sv, const reco::Vertex & pv, const reco::Jet & jet, SecondaryVertexFeatures & sv_features, const bool flip) { - + math::XYZVector jet_dir = jet.momentum().Unit(); sv_features.pt = sv.pt(); sv_features.deltaR = catch_infs_and_bound(std::fabs(reco::deltaR(sv, jet_dir))-0.5, @@ -34,8 +34,8 @@ namespace btagbtvdeep { 0,-1,800); sv_features.costhetasvpv = (flip ? -1.f : 1.f)* vertexDdotP(sv,pv); sv_features.enratio = sv.energy()/jet.energy(); - - } - + + } + } diff --git a/RecoBTag/TensorFlow/src/ShallowTagInfoConverter.cc b/RecoBTag/FeatureTools/src/ShallowTagInfoConverter.cc similarity index 90% rename from RecoBTag/TensorFlow/src/ShallowTagInfoConverter.cc rename to RecoBTag/FeatureTools/src/ShallowTagInfoConverter.cc index e1c63c7f3c613..e069bc3e2b87c 100644 --- a/RecoBTag/TensorFlow/src/ShallowTagInfoConverter.cc +++ b/RecoBTag/FeatureTools/src/ShallowTagInfoConverter.cc @@ -1,19 +1,19 @@ -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/BTauReco/interface/ShallowTagInfoFeatures.h" #include "DataFormats/BTauReco/interface/ShallowTagInfo.h" #include "DataFormats/BTauReco/interface/TaggingVariable.h" -#include "RecoBTag/TensorFlow/interface/ShallowTagInfoConverter.h" +#include "RecoBTag/FeatureTools/interface/ShallowTagInfoConverter.h" namespace btagbtvdeep { - + static constexpr std::size_t max_jetNSelectedTracks =100; - - + + void bTagToFeatures(const reco::TaggingVariableList & tag_info_vars, ShallowTagInfoFeatures & tag_info_features) { - + tag_info_features.trackSumJetEtRatio = tag_info_vars.get(reco::btau::trackSumJetEtRatio, -999); tag_info_features.trackSumJetDeltaR = tag_info_vars.get(reco::btau::trackSumJetDeltaR, -999); tag_info_features.vertexCategory = tag_info_vars.get(reco::btau::vertexCategory, -999); @@ -24,7 +24,7 @@ namespace btagbtvdeep { tag_info_features.jetNTracksEtaRel = tag_info_vars.get(reco::btau::jetNTracksEtaRel, -1); tag_info_features.jetNSelectedTracks = std::min(tag_info_vars.getList(reco::btau::trackMomentum, false).size(), max_jetNSelectedTracks); - + } } diff --git a/RecoBTag/TensorFlow/src/TrackInfoBuilder.cc b/RecoBTag/FeatureTools/src/TrackInfoBuilder.cc similarity index 98% rename from RecoBTag/TensorFlow/src/TrackInfoBuilder.cc rename to RecoBTag/FeatureTools/src/TrackInfoBuilder.cc index 7c7c939b39ba8..09f8e32e1704c 100644 --- a/RecoBTag/TensorFlow/src/TrackInfoBuilder.cc +++ b/RecoBTag/FeatureTools/src/TrackInfoBuilder.cc @@ -1,4 +1,4 @@ -#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" +#include "RecoBTag/FeatureTools/interface/TrackInfoBuilder.h" #include "DataFormats/PatCandidates/interface/PackedCandidate.h" #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h" #include "DataFormats/BTauReco/interface/JetTagInfo.h" diff --git a/RecoBTag/TensorFlow/src/deep_helpers.cc b/RecoBTag/FeatureTools/src/deep_helpers.cc similarity index 98% rename from RecoBTag/TensorFlow/src/deep_helpers.cc rename to RecoBTag/FeatureTools/src/deep_helpers.cc index 25657ce7611c3..0bdfeae8fd79d 100644 --- a/RecoBTag/TensorFlow/src/deep_helpers.cc +++ b/RecoBTag/FeatureTools/src/deep_helpers.cc @@ -1,4 +1,4 @@ -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "DataFormats/PatCandidates/interface/PackedCandidate.h" namespace btagbtvdeep { diff --git a/RecoBTag/TensorFlow/src/sorting_modules.cc b/RecoBTag/FeatureTools/src/sorting_modules.cc similarity index 87% rename from RecoBTag/TensorFlow/src/sorting_modules.cc rename to RecoBTag/FeatureTools/src/sorting_modules.cc index f58348bc47bf6..6be6db5a7de2d 100644 --- a/RecoBTag/TensorFlow/src/sorting_modules.cc +++ b/RecoBTag/FeatureTools/src/sorting_modules.cc @@ -1,5 +1,5 @@ -#include "RecoBTag/TensorFlow/interface/sorting_modules.h" +#include "RecoBTag/FeatureTools/interface/sorting_modules.h" #include "FWCore/Utilities/interface/Exception.h" #include diff --git a/RecoBTag/TensorFlow/BuildFile.xml b/RecoBTag/TensorFlow/BuildFile.xml index 5d19d8d637294..808744497ceb7 100644 --- a/RecoBTag/TensorFlow/BuildFile.xml +++ b/RecoBTag/TensorFlow/BuildFile.xml @@ -1,7 +1,5 @@ - - diff --git a/RecoBTag/TensorFlow/plugins/BuildFile.xml b/RecoBTag/TensorFlow/plugins/BuildFile.xml index b4787e210de56..b5ad53314db8e 100644 --- a/RecoBTag/TensorFlow/plugins/BuildFile.xml +++ b/RecoBTag/TensorFlow/plugins/BuildFile.xml @@ -4,6 +4,7 @@ + diff --git a/RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc b/RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc index d3cf94f6be533..b69b23a762035 100644 --- a/RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc +++ b/RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc @@ -21,18 +21,18 @@ #include "DataFormats/BTauReco/interface/DeepDoubleBFeatures.h" #include "DataFormats/BTauReco/interface/DeepDoubleBTagInfo.h" -#include "RecoBTag/TensorFlow/interface/BoostedDoubleSVTagInfoConverter.h" -#include "RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h" -#include "RecoBTag/TensorFlow/interface/JetConverter.h" -#include "RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h" +#include "RecoBTag/FeatureTools/interface/BoostedDoubleSVTagInfoConverter.h" +#include "RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h" +#include "RecoBTag/FeatureTools/interface/JetConverter.h" +#include "RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h" -#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" -#include "RecoBTag/TensorFlow/interface/sorting_modules.h" +#include "RecoBTag/FeatureTools/interface/TrackInfoBuilder.h" +#include "RecoBTag/FeatureTools/interface/sorting_modules.h" #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" #include "DataFormats/VertexReco/interface/VertexFwd.h" -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" class DeepDoubleBTagInfoProducer : public edm::stream::EDProducer<> { diff --git a/RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc b/RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc index 20e27736b3ba5..97a2d3e07f30d 100644 --- a/RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc +++ b/RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc @@ -19,19 +19,19 @@ #include "DataFormats/BTauReco/interface/DeepFlavourTagInfo.h" #include "DataFormats/BTauReco/interface/DeepFlavourFeatures.h" -#include "RecoBTag/TensorFlow/interface/JetConverter.h" -#include "RecoBTag/TensorFlow/interface/ShallowTagInfoConverter.h" -#include "RecoBTag/TensorFlow/interface/SecondaryVertexConverter.h" -#include "RecoBTag/TensorFlow/interface/NeutralCandidateConverter.h" -#include "RecoBTag/TensorFlow/interface/ChargedCandidateConverter.h" +#include "RecoBTag/FeatureTools/interface/JetConverter.h" +#include "RecoBTag/FeatureTools/interface/ShallowTagInfoConverter.h" +#include "RecoBTag/FeatureTools/interface/SecondaryVertexConverter.h" +#include "RecoBTag/FeatureTools/interface/NeutralCandidateConverter.h" +#include "RecoBTag/FeatureTools/interface/ChargedCandidateConverter.h" -#include "RecoBTag/TensorFlow/interface/TrackInfoBuilder.h" -#include "RecoBTag/TensorFlow/interface/sorting_modules.h" +#include "RecoBTag/FeatureTools/interface/TrackInfoBuilder.h" +#include "RecoBTag/FeatureTools/interface/sorting_modules.h" #include "DataFormats/VertexReco/interface/VertexFwd.h" #include "DataFormats/Candidate/interface/VertexCompositePtrCandidate.h" -#include "RecoBTag/TensorFlow/interface/deep_helpers.h" +#include "RecoBTag/FeatureTools/interface/deep_helpers.h" #include "FWCore/ParameterSet/interface/Registry.h" #include "FWCore/Common/interface/Provenance.h" @@ -276,8 +276,8 @@ void DeepFlavourTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet const edm::Provenance *prov = shallow_tag_infos.provenance(); const edm::ParameterSet& psetFromProvenance = edm::parameterSet(*prov); - double negative_cut = ( ( psetFromProvenance.getParameter("computer") - ).getParameter("trackSelection") + double negative_cut = ( ( psetFromProvenance.getParameter("computer") + ).getParameter("trackSelection") ).getParameter("sip3dSigMax"); for (unsigned int i = 0; i < jet.numberOfDaughters(); i++){ @@ -320,7 +320,7 @@ void DeepFlavourTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet auto & c_pf_features = features.c_pf_features.at(entry); // fill feature structure if (packed_cand) { - btagbtvdeep::packedCandidateToFeatures(packed_cand, jet, trackinfo, + btagbtvdeep::packedCandidateToFeatures(packed_cand, jet, trackinfo, drminpfcandsv, static_cast (jet_radius_), c_pf_features, flip_); } else if (reco_cand) { // get vertex association quality @@ -346,7 +346,7 @@ void DeepFlavourTagInfoProducer::produce(edm::Event& iEvent, const edm::EventSet const reco::VertexRef & PV_orig = (*pvas)[reco_ptr]; if(PV_orig.isNonnull()) PV = reco::VertexRef(vtxs, PV_orig.key()); } - btagbtvdeep::recoCandidateToFeatures(reco_cand, jet, trackinfo, + btagbtvdeep::recoCandidateToFeatures(reco_cand, jet, trackinfo, drminpfcandsv, static_cast (jet_radius_), puppiw, pv_ass_quality, PV, c_pf_features, flip_); } diff --git a/RecoBTag/TensorFlow/src/JetConverter.cc b/RecoBTag/TensorFlow/src/JetConverter.cc deleted file mode 100644 index 1c798b48c382a..0000000000000 --- a/RecoBTag/TensorFlow/src/JetConverter.cc +++ /dev/null @@ -1,4 +0,0 @@ -#include "RecoBTag/TensorFlow/interface/JetConverter.h" - - - From f13af0cda9dd32cc0c044c2a22d104cee1689147 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 02:52:10 +0200 Subject: [PATCH 18/31] Code style changes. --- .../interface/DeepBoostedJetFeatures.h | 14 ++++--- PhysicsTools/MXNet/src/MXNetCppPredictor.cc | 8 ++-- .../python/recoLayer0/bTagging_cff.py | 40 ++++++++++++++++++- .../plugins/DeepBoostedJetTagInfoProducer.cc | 21 +++++----- .../plugins/DeepBoostedJetTagsProducer.cc | 9 +++-- 5 files changed, 65 insertions(+), 27 deletions(-) diff --git a/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h index e7173b563c297..d22ae84fb7c26 100644 --- a/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h +++ b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h @@ -21,10 +21,11 @@ class DeepBoostedJetFeatures { } void fill(const std::string& name, float value){ - try { - feature_map_.at(name).push_back(value); + auto item = feature_map_.find(name); + if (item != feature_map_.end()){ + item->second.push_back(value); is_empty_ = false; - }catch (const std::out_of_range& e) { + }else{ throw cms::Exception("InvalidArgument") << "[DeepBoostedJetFeatures::fill()] Feature " << name << " has not been registered"; } } @@ -34,9 +35,10 @@ class DeepBoostedJetFeatures { } const std::vector& get(const std::string& name) const { - try { - return feature_map_.at(name); - }catch (const std::out_of_range& e) { + auto item = feature_map_.find(name); + if (item != feature_map_.end()){ + return item->second; + }else{ throw cms::Exception("InvalidArgument") << "[DeepBoostedJetFeatures::get()] Feature " << name << " does not exist!"; } } diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc index 0e6c57c0dc8c9..2511f58f9947c 100644 --- a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc @@ -62,8 +62,8 @@ void MXNetCppPredictor::set_input_shapes(const std::vector& input_n input_names_ = input_names; // init the input NDArrays and add them to the arg_map for (unsigned i=0; i& MXNetCppPredictor::predict(const std::vectorForward(false); diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index b3d42c808091b..cf1d17de7055f 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -231,8 +231,44 @@ #meta-taggers are simple arithmetic on top of other taggers, they are stored here #such that in case you want them re-run also the parent tagger is re-run as well -_pfDeepBoostedJetTagsProbs = ['pfDeepBoostedJetTags:probTbcq', 'pfDeepBoostedJetTags:probTbqq', 'pfDeepBoostedJetTags:probTbc', 'pfDeepBoostedJetTags:probTbq', 'pfDeepBoostedJetTags:probWcq', 'pfDeepBoostedJetTags:probWqq', 'pfDeepBoostedJetTags:probZbb', 'pfDeepBoostedJetTags:probZcc', 'pfDeepBoostedJetTags:probZqq', 'pfDeepBoostedJetTags:probHbb', 'pfDeepBoostedJetTags:probHcc', 'pfDeepBoostedJetTags:probHqqqq', 'pfDeepBoostedJetTags:probQCDbb', 'pfDeepBoostedJetTags:probQCDcc', 'pfDeepBoostedJetTags:probQCDb', 'pfDeepBoostedJetTags:probQCDc', 'pfDeepBoostedJetTags:probQCDothers'] -_pfMassDecorrelatedDeepBoostedJetTagsProbs = ['pfMassDecorrelatedDeepBoostedJetTags:probTbcq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers'] +_pfDeepBoostedJetTagsProbs = [ + 'pfDeepBoostedJetTags:probTbcq', + 'pfDeepBoostedJetTags:probTbqq', + 'pfDeepBoostedJetTags:probTbc', + 'pfDeepBoostedJetTags:probTbq', + 'pfDeepBoostedJetTags:probWcq', + 'pfDeepBoostedJetTags:probWqq', + 'pfDeepBoostedJetTags:probZbb', + 'pfDeepBoostedJetTags:probZcc', + 'pfDeepBoostedJetTags:probZqq', + 'pfDeepBoostedJetTags:probHbb', + 'pfDeepBoostedJetTags:probHcc', + 'pfDeepBoostedJetTags:probHqqqq', + 'pfDeepBoostedJetTags:probQCDbb', + 'pfDeepBoostedJetTags:probQCDcc', + 'pfDeepBoostedJetTags:probQCDb', + 'pfDeepBoostedJetTags:probQCDc', + 'pfDeepBoostedJetTags:probQCDothers', + ] +_pfMassDecorrelatedDeepBoostedJetTagsProbs = [ + 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', + 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', + 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', + 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', + 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers', + ] supportedMetaDiscr = { 'pfDeepCSVDiscriminatorsJetTags:BvsAll' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], 'pfDeepCSVDiscriminatorsJetTags:CvsB' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index 5b1290e084082..56ebe0e92fd26 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -212,7 +212,7 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ - const auto& jet = jets->at(jet_n); + const auto& jet = (*jets)[jet_n]; edm::RefToBase jet_ref(jets, jet_n); // create jet features @@ -239,6 +239,12 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet){ + // require the input to be a pat::Jet + const auto* patJet = dynamic_cast(&jet); + if (!patJet){ + throw edm::Exception(edm::errors::InvalidReference) << "Input is not a pat::Jet."; + } + // do nothing if jet does not have constituents if (jet.numberOfDaughters()==0) return; @@ -360,16 +366,9 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures } fts.fill("pfcand_drminsv", minDR==999 ? -1 : minDR); - pat::JetPtrCollection subjets; - try { - const auto &patJet = dynamic_cast(jet); - subjets = patJet.subjets(); - // sort by pt - std::sort(subjets.begin(), subjets.end(), [](const edm::Ptr& p1, const edm::Ptr& p2){ return p1->pt()>p2->pt(); }); - }catch (const std::bad_cast &e) { - throw edm::Exception(edm::errors::InvalidReference) << "Cannot access subjets because this is not a pat::Jet."; - } - + // subjets + auto subjets = patJet->subjets(); + std::sort(subjets.begin(), subjets.end(), [](const edm::Ptr& p1, const edm::Ptr& p2){ return p1->pt()>p2->pt(); }); // sort by pt fts.fill("pfcand_drsubjet1", !subjets.empty() ? reco::deltaR(*cand, *subjets.at(0)) : -1); fts.fill("pfcand_drsubjet2", subjets.size()>1 ? reco::deltaR(*cand, *subjets.at(1)) : -1); diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc index 59abafa6c249c..c6d37d5adeab6 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -41,9 +41,10 @@ struct PreprocessParams { std::unordered_map var_info_map; VarInfo get_info(const std::string &name) const { - try { - return var_info_map.at(name); - }catch (const std::out_of_range &e) { + auto item = var_info_map.find(name); + if (item != var_info_map.end()){ + return item->second; + } else { throw cms::Exception("InvalidArgument") << "Cannot find variable info for " << name; } } @@ -132,7 +133,7 @@ DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& // get output names from flav_table const auto & flav_pset = iConfig.getParameter("flav_table"); - for (const auto flav_pair : flav_pset.tbl()) { + for (const auto& flav_pair : flav_pset.tbl()) { const auto & flav_name = flav_pair.first; flav_pairs_.emplace_back(flav_name, flav_pset.getParameter>(flav_name)); From 8997d92c4ad60338890b37ce0c9db9b630c74718 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 03:23:19 +0200 Subject: [PATCH 19/31] Fix BuildFile. --- RecoBTag/TensorFlow/BuildFile.xml | 1 + RecoBTag/TensorFlow/plugins/BuildFile.xml | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/RecoBTag/TensorFlow/BuildFile.xml b/RecoBTag/TensorFlow/BuildFile.xml index 808744497ceb7..d8145d03d587e 100644 --- a/RecoBTag/TensorFlow/BuildFile.xml +++ b/RecoBTag/TensorFlow/BuildFile.xml @@ -1,4 +1,5 @@ + diff --git a/RecoBTag/TensorFlow/plugins/BuildFile.xml b/RecoBTag/TensorFlow/plugins/BuildFile.xml index b5ad53314db8e..b4787e210de56 100644 --- a/RecoBTag/TensorFlow/plugins/BuildFile.xml +++ b/RecoBTag/TensorFlow/plugins/BuildFile.xml @@ -4,7 +4,6 @@ - From b12fd79b5dba0c60f2e7e17d4d6f6b3014db6441 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 15:51:05 +0200 Subject: [PATCH 20/31] Modify MXNet source to avoid rebinding. --- .../MXNet/interface/MXNetCppPredictor.h | 10 +++------- PhysicsTools/MXNet/src/MXNetCppPredictor.cc | 20 ++++++++----------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h index 140944f921db8..ed97731ac1574 100644 --- a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h +++ b/PhysicsTools/MXNet/interface/MXNetCppPredictor.h @@ -54,13 +54,15 @@ class MXNetCppPredictor { MXNetCppPredictor(const Block &block, const std::string &output_node); virtual ~MXNetCppPredictor(); + // set input array shapes void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); + + // run prediction const std::vector& predict(const std::vector>& input_data); private: static std::mutex mutex_; - void infer_shapes(); void bind_executor(); // context @@ -78,12 +80,6 @@ class MXNetCppPredictor { // names of the input nodes std::vector input_names_; - // internal states - std::vector arg_arrays; - std::vector grad_arrays; - std::vector grad_reqs; - std::vector aux_arrays; - }; } /* namespace cpp */ diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc index 2511f58f9947c..2ed1bb6b72be6 100644 --- a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/src/MXNetCppPredictor.cc @@ -66,16 +66,15 @@ void MXNetCppPredictor::set_input_shapes(const std::vector& input_n NDArray nd(input_shapes[i], context_, false); arg_map_[name] = nd; } - // infer parameter shapes from input shapes - infer_shapes(); } const std::vector& MXNetCppPredictor::predict(const std::vector >& input_data) { assert(input_names_.size() == input_data.size()); try { - // bind executor - bind_executor(); + // create the executor (if not done yet) + if (!exec_) { bind_executor(); } + assert(exec_); // set the inputs for (unsigned i=0; i& MXNetCppPredictor::predict(const std::vector lock(mutex_); @@ -109,7 +108,7 @@ void MXNetCppPredictor::infer_shapes() { sym_.InferShape(arg_shapes, &in_shapes, &aux_shapes, &out_shapes); // init argument arrays - arg_arrays.clear(); + std::vector arg_arrays; for (size_t i = 0; i < in_shapes.size(); ++i) { const auto &shape = in_shapes[i]; const auto &arg_name = arg_name_list[i]; @@ -120,11 +119,11 @@ void MXNetCppPredictor::infer_shapes() { arg_arrays.push_back(NDArray(shape, context_, false)); } } - grad_arrays = std::vector(arg_arrays.size()); - grad_reqs = std::vector(arg_arrays.size(), kNullOp); + std::vector grad_arrays(arg_arrays.size()); + std::vector grad_reqs(arg_arrays.size(), kNullOp); // init auxiliary array - aux_arrays.clear(); + std::vector aux_arrays; const auto aux_name_list = sym_.ListAuxiliaryStates(); for (size_t i = 0; i < aux_shapes.size(); ++i) { const auto &shape = aux_shapes[i]; @@ -137,9 +136,6 @@ void MXNetCppPredictor::infer_shapes() { } } -} - -void MXNetCppPredictor::bind_executor() { // bind executor exec_.reset(new Executor(sym_, context_, arg_arrays, grad_arrays, grad_reqs, aux_arrays)); } From d1eaf16d88c0de84425ae1d49247e871248f7e31 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 13 Aug 2018 23:11:25 +0200 Subject: [PATCH 21/31] Fix BuildFile. --- RecoBTag/FeatureTools/BuildFile.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/RecoBTag/FeatureTools/BuildFile.xml b/RecoBTag/FeatureTools/BuildFile.xml index af9ecf390f336..cdefe9aeffdd1 100644 --- a/RecoBTag/FeatureTools/BuildFile.xml +++ b/RecoBTag/FeatureTools/BuildFile.xml @@ -1,6 +1,8 @@ + + + - From f4562fa747f527d3f3bff3f3e45492cc6c334efb Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Tue, 14 Aug 2018 00:50:01 +0200 Subject: [PATCH 22/31] Improve the checks when setting up pfDeepBoostedJetTagInfos. --- .../PatAlgos/python/tools/jetTools.py | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index ddb3e48f68bd8..3eb871c5e1a0b 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -631,25 +631,25 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou process, task) if btagInfo == 'pfDeepBoostedJetTagInfos': - jetSrcName = jetSource.value().lower() - if 'slimmed' in jetSrcName or 'updated' in jetSrcName: - # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8 - # daughters are links to original PackedCandidates, so NOT scaled by their puppi weights - has_puppi_weighted_daughters = cms.bool(False) - puppi_value_map = cms.InputTag("") - vertex_associator = cms.InputTag("") - else: - sys.stderr.write("Warning: running pfDeepBoostedJetTagInfos on %s is not supported yet.\n"%jetSource) - has_puppi_weighted_daughters = cms.bool(True) - # daughters are the particles used in jet clustering, so already scaled by their puppi weights - if pfCandidates.value() == 'packedPFCandidates': - # case 2: running on new jet collection whose daughters are PackedCandidates (e.g., recluster jets from MiniAOD files) + if pfCandidates.value() == 'packedPFCandidates': + jetSrcName = jetSource.value().lower() + if 'slimmed' in jetSrcName or 'updated' in jetSrcName or 'packed' in jetSrcName: + # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8, packedPatJetsAK8, etc. + # daughters are links to original PackedCandidates, so NOT scaled by their puppi weights yet + has_puppi_weighted_daughters = cms.bool(False) puppi_value_map = cms.InputTag("") vertex_associator = cms.InputTag("") - elif pfCandidates.value() == 'particleFlow': - # case 3: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD) - puppi_value_map = cms.InputTag("puppi") - vertex_associator = cms.InputTag("primaryVertexAssociation","original") + else: + raise ValueError("Cannot run pfDeepBoostedJetTagInfos on jet collection: %s." % jetSource.value()) + elif pfCandidates.value() == 'particleFlow': + raise ValueError("Running pfDeepBoostedJetTagInfos with reco::PFCandidates is currently not supported.") + # case 2: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD) + # daughters are the particles used in jet clustering, so already scaled by their puppi weights + has_puppi_weighted_daughters = cms.bool(True) + puppi_value_map = cms.InputTag("puppi") + vertex_associator = cms.InputTag("primaryVertexAssociation", "original") + else: + raise ValueError("Invalid pfCandidates collection: %s." % pfCandidates.value()) addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix, btag.pfDeepBoostedJetTagInfos.clone( jets = jetSource, From 3aae32e8a5a6b6310da95fdda68753aa770816e7 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 31 Aug 2018 18:18:40 +0200 Subject: [PATCH 23/31] Update DeepBoostedJet meta-taggers. --- .../python/recoLayer0/bTagging_cff.py | 10 ++-- .../python/slimming/applyDeepBtagging_cff.py | 8 ++- .../pfDeepBoostedDiscriminatorsJetTags_cfi.py | 60 +++++++++++++++++++ ...tedDeepBoostedDiscriminatorsJetTags_cfi.py | 42 +++---------- 4 files changed, 80 insertions(+), 40 deletions(-) diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index cf1d17de7055f..32af1581a7971 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -278,13 +278,15 @@ 'pfDeepCMVADiscriminatorsJetTags:CvsL' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfDeepBoostedJetTagsProbs, 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfDeepBoostedDiscriminatorsJetTags:ZvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfDeepBoostedDiscriminatorsJetTags:ZbbvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfDeepBoostedDiscriminatorsJetTags:HbbvsQCD' : _pfDeepBoostedJetTagsProbs, + 'pfDeepBoostedDiscriminatorsJetTags:H4qvsQCD' : _pfDeepBoostedJetTagsProbs, 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvscc' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsZHcc': _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsLight' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsLight' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, } diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index d9a7b76cf8b71..3c5b32f3adfb5 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -84,6 +84,10 @@ def applyDeepBtagging( process, postfix="" ) : # meta taggers 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD', 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD', + 'pfDeepBoostedDiscriminatorsJetTags:ZvsQCD', + 'pfDeepBoostedDiscriminatorsJetTags:ZbbvsQCD', + 'pfDeepBoostedDiscriminatorsJetTags:HbbvsQCD', + 'pfDeepBoostedDiscriminatorsJetTags:H4qvsQCD', # DeepBoostedJet (mass decorrelated) 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', @@ -106,8 +110,10 @@ def applyDeepBtagging( process, postfix="" ) : # meta taggers 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD', 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD', 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsLight', + 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsLight', ], postfix = 'SlimmedAK8DeepTags'+postfix, diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py index a132d129fd5bf..8bf41417f631d 100644 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py +++ b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py @@ -35,5 +35,65 @@ cms.InputTag('pfDeepBoostedJetTags', 'probQCDothers'), ), ), + cms.PSet( + name = cms.string('ZvsQCD'), + numerator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probZbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probZcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probZqq'), + ), + denominator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probZbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probZcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probZqq'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDothers'), + ), + ), + cms.PSet( + name = cms.string('ZbbvsQCD'), + numerator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probZbb'), + ), + denominator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probZbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDothers'), + ), + ), + cms.PSet( + name = cms.string('HbbvsQCD'), + numerator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probHbb'), + ), + denominator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probHbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDothers'), + ), + ), + cms.PSet( + name = cms.string('H4qvsQCD'), + numerator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probHqqqq'), + ), + denominator = cms.VInputTag( + cms.InputTag('pfDeepBoostedJetTags', 'probHqqqq'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDbb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDcc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDb'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDc'), + cms.InputTag('pfDeepBoostedJetTags', 'probQCDothers'), + ), + ), ) ) diff --git a/RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py b/RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py index 7de7fe0d0c21f..46c4a8e55d735 100644 --- a/RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py +++ b/RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py @@ -37,11 +37,10 @@ ), cms.PSet( - name = cms.string('bbvsQCD'), + name = cms.string('ZHbbvsQCD'), numerator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDbb'), ), denominator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), @@ -54,11 +53,10 @@ ), ), cms.PSet( - name = cms.string('ccvsQCD'), + name = cms.string('ZHccvsQCD'), numerator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHcc'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDcc'), ), denominator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), @@ -70,28 +68,13 @@ cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDothers'), ), ), - cms.PSet( - name = cms.string('bbvscc'), - numerator = cms.VInputTag( - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDbb'), - ), - denominator = cms.VInputTag( - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHcc'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDcc'), - ), - ), cms.PSet( - name = cms.string('ZHbbvsQCD'), + name = cms.string('bbvsLight'), numerator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), + cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDbb'), ), denominator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), @@ -104,10 +87,11 @@ ), ), cms.PSet( - name = cms.string('ZHccvsQCD'), + name = cms.string('ccvsLight'), numerator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHcc'), + cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDcc'), ), denominator = cms.VInputTag( cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), @@ -119,18 +103,6 @@ cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probQCDothers'), ), ), - cms.PSet( - name = cms.string('ZHbbvsZHcc'), - numerator = cms.VInputTag( - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), - ), - denominator = cms.VInputTag( - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHbb'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probZcc'), - cms.InputTag('pfMassDecorrelatedDeepBoostedJetTags', 'probHcc'), - ), - ), + ) ) From 2fcd0fb67cf4e51d86daa9fae6ba54a083812ce3 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 31 Aug 2018 18:21:13 +0200 Subject: [PATCH 24/31] Rename MXNet Predictor. --- .../{MXNetCppPredictor.h => Predictor.h} | 10 +++---- .../{MXNetCppPredictor.cc => Predictor.cc} | 26 ++++++++++--------- ...tMXNetCppPredictor.cc => testPredictor.cc} | 4 +-- .../plugins/DeepBoostedJetTagsProducer.cc | 7 +++-- 4 files changed, 24 insertions(+), 23 deletions(-) rename PhysicsTools/MXNet/interface/{MXNetCppPredictor.h => Predictor.h} (91%) rename PhysicsTools/MXNet/src/{MXNetCppPredictor.cc => Predictor.cc} (78%) rename PhysicsTools/MXNet/test/{testMXNetCppPredictor.cc => testPredictor.cc} (93%) diff --git a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h b/PhysicsTools/MXNet/interface/Predictor.h similarity index 91% rename from PhysicsTools/MXNet/interface/MXNetCppPredictor.h rename to PhysicsTools/MXNet/interface/Predictor.h index ed97731ac1574..693cbf4926d0f 100644 --- a/PhysicsTools/MXNet/interface/MXNetCppPredictor.h +++ b/PhysicsTools/MXNet/interface/Predictor.h @@ -47,12 +47,12 @@ class Block { // Simple helper class to run prediction // this cannot be shared between threads -class MXNetCppPredictor { +class Predictor { public: - MXNetCppPredictor(); - MXNetCppPredictor(const Block &block); - MXNetCppPredictor(const Block &block, const std::string &output_node); - virtual ~MXNetCppPredictor(); + Predictor(); + Predictor(const Block &block); + Predictor(const Block &block, const std::string &output_node); + virtual ~Predictor(); // set input array shapes void set_input_shapes(const std::vector& input_names, const std::vector>& input_shapes); diff --git a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc b/PhysicsTools/MXNet/src/Predictor.cc similarity index 78% rename from PhysicsTools/MXNet/src/MXNetCppPredictor.cc rename to PhysicsTools/MXNet/src/Predictor.cc index 2ed1bb6b72be6..f23e0e630dd7c 100644 --- a/PhysicsTools/MXNet/src/MXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/src/Predictor.cc @@ -5,10 +5,11 @@ * Author: hqu */ +#include "PhysicsTools/MXNet/interface/Predictor.h" + #include #include "FWCore/Utilities/interface/Exception.h" -#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" namespace mxnet { @@ -42,33 +43,34 @@ void Block::load_parameters(const std::string& param_file) { } } -std::mutex MXNetCppPredictor::mutex_; -const Context MXNetCppPredictor::context_ = Context(DeviceType::kCPU, 0); +std::mutex Predictor::mutex_; +const Context Predictor::context_ = Context(DeviceType::kCPU, 0); -MXNetCppPredictor::MXNetCppPredictor() { +Predictor::Predictor() { } -MXNetCppPredictor::MXNetCppPredictor(const Block& block) : sym_(block.symbol()), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { +Predictor::Predictor(const Block& block) +: sym_(block.symbol()), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { } -MXNetCppPredictor::MXNetCppPredictor(const Block &block, const std::string &output_node) : sym_(block.symbol(output_node)), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { +Predictor::Predictor(const Block &block, const std::string &output_node) +: sym_(block.symbol(output_node)), arg_map_(block.arg_map()), aux_map_(block.aux_map()) { } -MXNetCppPredictor::~MXNetCppPredictor() { +Predictor::~Predictor() { } -void MXNetCppPredictor::set_input_shapes(const std::vector& input_names, const std::vector >& input_shapes) { +void Predictor::set_input_shapes(const std::vector& input_names, const std::vector >& input_shapes) { assert(input_names.size() == input_shapes.size()); input_names_ = input_names; // init the input NDArrays and add them to the arg_map for (unsigned i=0; i& MXNetCppPredictor::predict(const std::vector >& input_data) { +const std::vector& Predictor::predict(const std::vector >& input_data) { assert(input_names_.size() == input_data.size()); try { @@ -90,7 +92,7 @@ const std::vector& MXNetCppPredictor::predict(const std::vector lock(mutex_); diff --git a/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc b/PhysicsTools/MXNet/test/testPredictor.cc similarity index 93% rename from PhysicsTools/MXNet/test/testMXNetCppPredictor.cc rename to PhysicsTools/MXNet/test/testPredictor.cc index 8d691f3f9c2b6..e50922c9eb9f2 100644 --- a/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc +++ b/PhysicsTools/MXNet/test/testPredictor.cc @@ -1,7 +1,7 @@ #include +#include "PhysicsTools/MXNet/interface/Predictor.h" #include "FWCore/ParameterSet/interface/FileInPath.h" -#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" using namespace mxnet::cpp; @@ -29,7 +29,7 @@ void testMXNetCppPredictor::checkAll() CPPUNIT_ASSERT(block!=nullptr); // create predictor - MXNetCppPredictor predictor(*block); + Predictor predictor(*block); // set input shape std::vector input_names {"data"}; diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc index c6d37d5adeab6..f7afdaa5dfe6d 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -13,10 +13,9 @@ #include "DataFormats/BTauReco/interface/DeepBoostedJetTagInfo.h" -#include "PhysicsTools/MXNet/interface/MXNetCppPredictor.h" - #include #include +#include "PhysicsTools/MXNet/interface/Predictor.h" // Hold the mxnet model block (symbol + params) in the edm::GlobalCache. struct MXBlockCache { @@ -82,7 +81,7 @@ class DeepBoostedJetTagsProducer : public edm::stream::EDProducer prep_info_map_; // preprocessing info for each input group std::vector> data_; - std::unique_ptr predictor_; + std::unique_ptr predictor_; bool debug_ = false; }; @@ -128,7 +127,7 @@ DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& } // init MXNetPredictor - predictor_.reset(new mxnet::cpp::MXNetCppPredictor(*cache->block)); + predictor_.reset(new mxnet::cpp::Predictor(*cache->block)); predictor_->set_input_shapes(input_names_, input_shapes_); // get output names from flav_table From fad4dc31dbd2c7b922cb0a22999e341c895c96c2 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 3 Sep 2018 17:48:24 +0200 Subject: [PATCH 25/31] Style and performance improvements. --- .../interface/DeepBoostedJetFeatures.h | 15 ++ DataFormats/PatCandidates/interface/Jet.h | 2 +- .../python/recoLayer0/bTagging_cff.py | 108 ++-------- .../python/slimming/applyDeepBtagging_cff.py | 57 +---- .../PatAlgos/python/tools/jetTools.py | 16 +- .../plugins/DeepBoostedJetTagInfoProducer.cc | 196 ++++++++++-------- .../plugins/DeepBoostedJetTagsProducer.cc | 162 +++++++-------- .../pfDeepBoostedJetPreprocessParams_cfi.py | 2 +- .../python/pfDeepBoostedJetTags_cfi.py | 27 --- .../python/pfDeepBoostedJet_cff.py | 32 ++- 10 files changed, 248 insertions(+), 369 deletions(-) delete mode 100644 RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetTags_cfi.py diff --git a/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h index d22ae84fb7c26..508555d3d704e 100644 --- a/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h +++ b/DataFormats/BTauReco/interface/DeepBoostedJetFeatures.h @@ -20,6 +20,10 @@ class DeepBoostedJetFeatures { feature_map_[name]; } + void reserve(const std::string& name, unsigned capacity){ + feature_map_[name].reserve(capacity); + } + void fill(const std::string& name, float value){ auto item = feature_map_.find(name); if (item != feature_map_.end()){ @@ -34,6 +38,17 @@ class DeepBoostedJetFeatures { feature_map_[name] = vec; } + void check_consistency(const std::vector &names) const { + if (names.empty()) return; + const auto ref_len = get(names.front()).size(); + for (unsigned i=1; i& get(const std::string& name) const { auto item = feature_map_.find(name); if (item != feature_map_.end()){ diff --git a/DataFormats/PatCandidates/interface/Jet.h b/DataFormats/PatCandidates/interface/Jet.h index 9b3d2c94b98c4..eeccc7897ce27 100644 --- a/DataFormats/PatCandidates/interface/Jet.h +++ b/DataFormats/PatCandidates/interface/Jet.h @@ -454,7 +454,7 @@ namespace pat { /// clear daughter references void clearDaughters() override { - reco::CompositePtrCandidate::clearDaughters(); + PATObject::clearDaughters(); daughtersTemp_.reset(); // need to reset daughtersTemp_ as well } diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index 32af1581a7971..fd7357fc8f300 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -191,102 +191,30 @@ , 'pfNegativeDeepFlavourJetTags:probg' : [["pfNegativeDeepFlavourTagInfos"], ['pfDeepCSVNegativeTagInfos', "pfImpactParameterTagInfos", 'pfInclusiveSecondaryVertexFinderNegativeTagInfos']] , 'pfDeepDoubleBJetTags:probQ' : [["pfDeepDoubleBTagInfos"], ['pfBoostedDoubleSVAK8TagInfos', "pfImpactParameterAK8TagInfos", 'pfInclusiveSecondaryVertexFinderAK8TagInfos']] , 'pfDeepDoubleBJetTags:probH' : [["pfDeepDoubleBTagInfos"], ['pfBoostedDoubleSVAK8TagInfos', "pfImpactParameterAK8TagInfos", 'pfInclusiveSecondaryVertexFinderAK8TagInfos']] - # DeepBoostedJetTagging - , 'pfDeepBoostedJetTags:probTbcq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probTbqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probTbc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probTbq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probWcq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probWqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probZbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probZcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probZqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probHbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probHcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probHqqqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probQCDbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probQCDcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probQCDb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probQCDc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfDeepBoostedJetTags:probQCDothers' : [["pfDeepBoostedJetTagInfos"]] - # DeepBoostedJetTagging - Mass-decorrelated - , 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probTbc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probTbq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probWcq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probWqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probZbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probZcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probZqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probHbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probHcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc' : [["pfDeepBoostedJetTagInfos"]] - , 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers' : [["pfDeepBoostedJetTagInfos"]] } -#meta-taggers are simple arithmetic on top of other taggers, they are stored here -#such that in case you want them re-run also the parent tagger is re-run as well -_pfDeepBoostedJetTagsProbs = [ - 'pfDeepBoostedJetTags:probTbcq', - 'pfDeepBoostedJetTags:probTbqq', - 'pfDeepBoostedJetTags:probTbc', - 'pfDeepBoostedJetTags:probTbq', - 'pfDeepBoostedJetTags:probWcq', - 'pfDeepBoostedJetTags:probWqq', - 'pfDeepBoostedJetTags:probZbb', - 'pfDeepBoostedJetTags:probZcc', - 'pfDeepBoostedJetTags:probZqq', - 'pfDeepBoostedJetTags:probHbb', - 'pfDeepBoostedJetTags:probHcc', - 'pfDeepBoostedJetTags:probHqqqq', - 'pfDeepBoostedJetTags:probQCDbb', - 'pfDeepBoostedJetTags:probQCDcc', - 'pfDeepBoostedJetTags:probQCDb', - 'pfDeepBoostedJetTags:probQCDc', - 'pfDeepBoostedJetTags:probQCDothers', - ] -_pfMassDecorrelatedDeepBoostedJetTagsProbs = [ - 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers', - ] +# meta-taggers are simple arithmetic on top of other taggers, they are stored here +# such that in case you want them re-run also the parent tagger is re-run as well + supportedMetaDiscr = { 'pfDeepCSVDiscriminatorsJetTags:BvsAll' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], 'pfDeepCSVDiscriminatorsJetTags:CvsB' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], 'pfDeepCSVDiscriminatorsJetTags:CvsL' : ['pfDeepCSVJetTags:probudsg', 'pfDeepCSVJetTags:probb', 'pfDeepCSVJetTags:probc', 'pfDeepCSVJetTags:probbb'], - 'pfDeepCMVADiscriminatorsJetTags:BvsAll' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], - 'pfDeepCMVADiscriminatorsJetTags:CvsB' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], + 'pfDeepCMVADiscriminatorsJetTags:BvsAll' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], + 'pfDeepCMVADiscriminatorsJetTags:CvsB' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], 'pfDeepCMVADiscriminatorsJetTags:CvsL' : ['pfDeepCMVAJetTags:probudsg', 'pfDeepCMVAJetTags:probb', 'pfDeepCMVAJetTags:probc', 'pfDeepCMVAJetTags:probbb'], - 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfDeepBoostedDiscriminatorsJetTags:ZvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfDeepBoostedDiscriminatorsJetTags:ZbbvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfDeepBoostedDiscriminatorsJetTags:HbbvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfDeepBoostedDiscriminatorsJetTags:H4qvsQCD' : _pfDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsLight' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsLight' : _pfMassDecorrelatedDeepBoostedJetTagsProbs, } +# ----------------------------------- +# setup DeepBoostedJet +from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsProbs, _pfDeepBoostedJetTagsMetaDiscrs, \ + _pfMassDecorrelatedDeepBoostedJetTagsProbs, _pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs +# update supportedBtagDiscr +for disc in _pfDeepBoostedJetTagsProbs + _pfMassDecorrelatedDeepBoostedJetTagsProbs: + supportedBtagDiscr[disc] = [["pfDeepBoostedJetTagInfos"]] +# update supportedMetaDiscr +for disc in _pfDeepBoostedJetTagsMetaDiscrs: + supportedMetaDiscr[disc] = _pfDeepBoostedJetTagsProbs +for disc in _pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs: + supportedMetaDiscr[disc] = _pfMassDecorrelatedDeepBoostedJetTagsProbs +# ----------------------------------- diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index 3c5b32f3adfb5..a35b5ef32e9ba 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -42,7 +42,7 @@ def applyDeepBtagging( process, postfix="" ) : # delete module not used anymore (slimmedJets substitutes) delattr(process, 'selectedUpdatedPatJetsSlimmedDeepFlavour'+postfix) - + from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsAll as pfDeepBoostedJetTagsAll # update slimmed jets to include particle-based deep taggers (keep same name) # make clone for DeepTags-less slimmed AK8 jets, so output name is preserved @@ -62,60 +62,7 @@ def applyDeepBtagging( process, postfix="" ) : btagDiscriminators = [ 'pfDeepDoubleBJetTags:probQ', 'pfDeepDoubleBJetTags:probH', - - # DeepBoostedJet (Nominal) - 'pfDeepBoostedJetTags:probTbcq', - 'pfDeepBoostedJetTags:probTbqq', - 'pfDeepBoostedJetTags:probTbc', - 'pfDeepBoostedJetTags:probTbq', - 'pfDeepBoostedJetTags:probWcq', - 'pfDeepBoostedJetTags:probWqq', - 'pfDeepBoostedJetTags:probZbb', - 'pfDeepBoostedJetTags:probZcc', - 'pfDeepBoostedJetTags:probZqq', - 'pfDeepBoostedJetTags:probHbb', - 'pfDeepBoostedJetTags:probHcc', - 'pfDeepBoostedJetTags:probHqqqq', - 'pfDeepBoostedJetTags:probQCDbb', - 'pfDeepBoostedJetTags:probQCDcc', - 'pfDeepBoostedJetTags:probQCDb', - 'pfDeepBoostedJetTags:probQCDc', - 'pfDeepBoostedJetTags:probQCDothers', - # meta taggers - 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:ZvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:ZbbvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:HbbvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:H4qvsQCD', - - # DeepBoostedJet (mass decorrelated) - 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers', - # meta taggers - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsLight', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsLight', - - ], + ] + pfDeepBoostedJetTagsAll, postfix = 'SlimmedAK8DeepTags'+postfix, printWarning = False ) diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index 3eb871c5e1a0b..865dc4865c8f8 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -633,21 +633,21 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou if btagInfo == 'pfDeepBoostedJetTagInfos': if pfCandidates.value() == 'packedPFCandidates': jetSrcName = jetSource.value().lower() - if 'slimmed' in jetSrcName or 'updated' in jetSrcName or 'packed' in jetSrcName: - # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8, packedPatJetsAK8, etc. + if 'slimmed' in jetSrcName or 'updated' in jetSrcName: + # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8, etc. # daughters are links to original PackedCandidates, so NOT scaled by their puppi weights yet - has_puppi_weighted_daughters = cms.bool(False) - puppi_value_map = cms.InputTag("") - vertex_associator = cms.InputTag("") + has_puppi_weighted_daughters = False + puppi_value_map = "" + vertex_associator = "" else: raise ValueError("Cannot run pfDeepBoostedJetTagInfos on jet collection: %s." % jetSource.value()) elif pfCandidates.value() == 'particleFlow': raise ValueError("Running pfDeepBoostedJetTagInfos with reco::PFCandidates is currently not supported.") # case 2: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD) # daughters are the particles used in jet clustering, so already scaled by their puppi weights - has_puppi_weighted_daughters = cms.bool(True) - puppi_value_map = cms.InputTag("puppi") - vertex_associator = cms.InputTag("primaryVertexAssociation", "original") +# has_puppi_weighted_daughters = True +# puppi_value_map = "puppi" +# vertex_associator = "primaryVertexAssociation:original" else: raise ValueError("Invalid pfCandidates collection: %s." % pfCandidates.value()) addToProcessAndTask(btagPrefix+btagInfo+labelName+postfix, diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc index 56ebe0e92fd26..f043896539ea7 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc @@ -60,17 +60,81 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> edm::EDGetTokenT> pvasq_value_map_token_; edm::EDGetTokenT> pvas_token_; - edm::Handle vtxs; - edm::Handle svs; - edm::ESHandle track_builder; - edm::Handle> puppi_value_map; - edm::Handle> pvasq_value_map; - edm::Handle> pvas; - - std::vector feature_names_; + edm::Handle vtxs_; + edm::Handle svs_; + edm::ESHandle track_builder_; + edm::Handle> puppi_value_map_; + edm::Handle> pvasq_value_map_; + edm::Handle> pvas_; + + const static std::vector particle_features_; + const static std::vector sv_features_; const reco::Vertex *pv_ = nullptr; }; +const static std::vector DeepBoostedJetTagInfoProducer::particle_features_ { + "pfcand_puppiw", + "pfcand_hcalFrac", + "pfcand_VTX_ass", + "pfcand_lostInnerHits", + "pfcand_quality", + "pfcand_charge", + "pfcand_isEl", + "pfcand_isMu", + "pfcand_isChargedHad", + "pfcand_isGamma", + "pfcand_isNeutralHad", + "pfcand_phirel", + "pfcand_etarel", + "pfcand_deltaR", + "pfcand_abseta", + "pfcand_ptrel_log", + "pfcand_erel_log", + "pfcand_pt_log", + "pfcand_drminsv", + "pfcand_drsubjet1", + "pfcand_drsubjet2", + "pfcand_normchi2", + "pfcand_dz", + "pfcand_dzsig", + "pfcand_dxy", + "pfcand_dxysig", + "pfcand_dptdpt", + "pfcand_detadeta", + "pfcand_dphidphi", + "pfcand_dxydxy", + "pfcand_dzdz", + "pfcand_dxydz", + "pfcand_dphidxy", + "pfcand_dlambdadz", + "pfcand_btagEtaRel", + "pfcand_btagPtRatio", + "pfcand_btagPParRatio", + "pfcand_btagSip2dVal", + "pfcand_btagSip2dSig", + "pfcand_btagSip3dVal", + "pfcand_btagSip3dSig", + "pfcand_btagJetDistVal", +}; + +const static std::vector DeepBoostedJetTagInfoProducer::sv_features_ { + "sv_phirel", + "sv_etarel", + "sv_deltaR", + "sv_abseta", + "sv_mass", + "sv_ptrel_log", + "sv_erel_log", + "sv_pt_log", + "sv_ntracks", + "sv_normchi2", + "sv_dxy", + "sv_dxysig", + "sv_d3d", + "sv_d3dsig", + "sv_costhetasvpv", +}; + DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::ParameterSet& iConfig) : has_puppi_weighted_daughters_(iConfig.getParameter("has_puppi_weighted_daughters")) , jet_radius_(iConfig.getParameter("jet_radius")) @@ -81,7 +145,6 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete , sv_token_(consumes(iConfig.getParameter("secondary_vertices"))) , use_puppi_value_map_(false) , use_pvasq_value_map_(false) -, feature_names_(iConfig.getParameter>("feature_names")) { const auto & puppi_value_map_tag = iConfig.getParameter("puppi_value_map"); @@ -118,65 +181,6 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti desc.add("jets", edm::InputTag("ak8PFJetsPuppi")); desc.add("puppi_value_map", edm::InputTag("puppi")); desc.add("vertex_associator", edm::InputTag("primaryVertexAssociation","original")); - desc.add>("feature_names", std::vector{ - "pfcand_puppiw", - "pfcand_hcalFrac", - "pfcand_VTX_ass", - "pfcand_lostInnerHits", - "pfcand_quality", - "pfcand_charge", - "pfcand_isEl", - "pfcand_isMu", - "pfcand_isChargedHad", - "pfcand_isGamma", - "pfcand_isNeutralHad", - "pfcand_phirel", - "pfcand_etarel", - "pfcand_deltaR", - "pfcand_abseta", - "pfcand_ptrel_log", - "pfcand_erel_log", - "pfcand_pt_log", - "pfcand_drminsv", - "pfcand_drsubjet1", - "pfcand_drsubjet2", - "pfcand_normchi2", - "pfcand_dz", - "pfcand_dzsig", - "pfcand_dxy", - "pfcand_dxysig", - "pfcand_dptdpt", - "pfcand_detadeta", - "pfcand_dphidphi", - "pfcand_dxydxy", - "pfcand_dzdz", - "pfcand_dxydz", - "pfcand_dphidxy", - "pfcand_dlambdadz", - "pfcand_btagEtaRel", - "pfcand_btagPtRatio", - "pfcand_btagPParRatio", - "pfcand_btagSip2dVal", - "pfcand_btagSip2dSig", - "pfcand_btagSip3dVal", - "pfcand_btagSip3dSig", - "pfcand_btagJetDistVal", - "sv_phirel", - "sv_etarel", - "sv_deltaR", - "sv_abseta", - "sv_mass", - "sv_ptrel_log", - "sv_erel_log", - "sv_pt_log", - "sv_ntracks", - "sv_normchi2", - "sv_dxy", - "sv_dxysig", - "sv_d3d", - "sv_d3dsig", - "sv_costhetasvpv", - }); descriptions.add("pfDeepBoostedJetTagInfos", desc); } @@ -188,26 +192,26 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event edm::Handle> jets; iEvent.getByToken(jet_token_, jets); - iEvent.getByToken(vtx_token_, vtxs); - if (vtxs->empty()){ + iEvent.getByToken(vtx_token_, vtxs_); + if (vtxs_->empty()){ // produce empty TagInfos in case no primary vertex iEvent.put(std::move(output_tag_infos)); return; // exit event } // primary vertex - pv_ = &vtxs->at(0); + pv_ = &vtxs_->at(0); - iEvent.getByToken(sv_token_, svs); + iEvent.getByToken(sv_token_, svs_); - iSetup.get().get("TransientTrackBuilder", track_builder); + iSetup.get().get("TransientTrackBuilder", track_builder_); if (use_puppi_value_map_) { - iEvent.getByToken(puppi_value_map_token_, puppi_value_map); + iEvent.getByToken(puppi_value_map_token_, puppi_value_map_); } if (use_pvasq_value_map_) { - iEvent.getByToken(pvasq_value_map_token_, pvasq_value_map); - iEvent.getByToken(pvas_token_, pvas); + iEvent.getByToken(pvasq_value_map_token_, pvasq_value_map_); + iEvent.getByToken(pvas_token_, pvas_); } for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ @@ -218,7 +222,8 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event // create jet features DeepBoostedJetFeatures features; // declare all the feature variables (init as empty vector) - for (const auto &name : feature_names_) { features.add(name); } + for (const auto &name : particle_features_) { features.add(name); } + for (const auto &name : sv_features_) { features.add(name); } // fill values only if above pt threshold and has daughters, otherwise left empty bool fill_vars = true; @@ -228,6 +233,9 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event if (fill_vars){ fillParticleFeatures(features, jet); fillSVFeatures(features, jet); + + features.check_consistency(particle_features_); + features.check_consistency(sv_features_); } // this should always be done even if features are not filled @@ -253,17 +261,21 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures GlobalVector jet_ref_track_dir(jet.px(), jet.py(), jet.pz()); const float etasign = jet.eta()>0 ? 1 : -1; + std::map puppi_wgt_cache; auto puppiWgt = [&](const reco::CandidatePtr& cand){ const auto* pack_cand = dynamic_cast(&(*cand)); const auto* reco_cand = dynamic_cast(&(*cand)); + float wgt = 1.; if (pack_cand) { - return pack_cand->puppiWeight(); + wgt = pack_cand->puppiWeight(); } else if (reco_cand) { - if (use_puppi_value_map_){ return (*puppi_value_map)[cand]; } + if (use_puppi_value_map_){ wgt = (*puppi_value_map_)[cand]; } else { throw edm::Exception(edm::errors::InvalidReference) << "Puppi value map is missing"; } } else { throw edm::Exception(edm::errors::InvalidReference) << "Cannot convert to either pat::PackedCandidate or reco::PFCandidate"; } + puppi_wgt_cache[cand.key()] = wgt; + return wgt; }; std::vector daughters; @@ -274,11 +286,15 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures } // sort by (Puppi-weighted) pt if (!has_puppi_weighted_daughters_) { - std::sort(daughters.begin(), daughters.end(), [&](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return puppiWgt(a)*a->pt() > puppiWgt(b)*b->pt(); }); + std::sort(daughters.begin(), daughters.end(), [&puppi_wgt_cache](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ + return puppi_wgt_cache.at(a.key())*a->pt() > puppi_wgt_cache.at(b.key())*b->pt(); }); }else{ std::sort(daughters.begin(), daughters.end(), [](const reco::CandidatePtr& a, const reco::CandidatePtr& b){ return a->pt() > b->pt(); }); } + // reserve space + for (const auto &name : particle_features_) { fts.reserve(name, daughters.size()); } + auto useTrackProperties = [&](const reco::PFCandidate* reco_cand) { const auto* trk = reco_cand->bestTrack(); return trk!=nullptr && trk->pt()>min_pt_for_track_properties_; @@ -291,10 +307,9 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures auto puppiP4 = cand->p4(); if (packed_cand){ if (!has_puppi_weighted_daughters_) { - puppiP4 *= packed_cand->puppiWeight(); + puppiP4 *= puppi_wgt_cache.at(cand.key()); } - fts.fill("pfcand_puppiw", packed_cand->puppiWeight()); fts.fill("pfcand_hcalFrac", packed_cand->hcalFraction()); fts.fill("pfcand_VTX_ass", packed_cand->pvAssociationQuality()); fts.fill("pfcand_lostInnerHits", packed_cand->lostInnerHits()); @@ -318,14 +333,13 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures int pv_ass_quality = 0; // fallback value float vtx_ass = 0; if (use_pvasq_value_map_) { - pv_ass_quality = (*pvasq_value_map)[cand]; - const reco::VertexRef & PV_orig = (*pvas)[cand]; + pv_ass_quality = (*pvasq_value_map_)[cand]; + const reco::VertexRef & PV_orig = (*pvas_)[cand]; vtx_ass = vtx_ass_from_pfcand(*reco_cand, pv_ass_quality, PV_orig); } else { throw edm::Exception(edm::errors::InvalidReference) << "Vertex association missing"; } - fts.fill("pfcand_puppiw", puppiWgt(cand)); fts.fill("pfcand_hcalFrac", reco_cand->hcalEnergy()/(reco_cand->ecalEnergy()+reco_cand->hcalEnergy())); fts.fill("pfcand_VTX_ass", vtx_ass); fts.fill("pfcand_lostInnerHits", useTrackProperties(reco_cand) ? lost_inner_hits_from_pfcand(*reco_cand) : 0); @@ -350,6 +364,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures } // basic kinematics + fts.fill("pfcand_puppiw", puppi_wgt_cache.at(cand.key())); fts.fill("pfcand_phirel", reco::deltaPhi(puppiP4, jet)); fts.fill("pfcand_etarel", etasign * (puppiP4.eta() - jet.eta())); fts.fill("pfcand_deltaR", reco::deltaR(puppiP4, jet)); @@ -360,7 +375,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures fts.fill("pfcand_pt_log", catch_infs(std::log(puppiP4.pt()), -99)); double minDR = 999; - for (const auto &sv : *svs){ + for (const auto &sv : *svs_){ double dr = reco::deltaR(*cand, sv); if (dr < minDR) minDR = dr; } @@ -391,7 +406,7 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures fts.fill("pfcand_dphidxy", cov(2,3)); fts.fill("pfcand_dlambdadz", cov(1,4)); - TrackInfoBuilder trkinfo(track_builder); + TrackInfoBuilder trkinfo(track_builder_); trkinfo.buildTrackInfo(&(*cand), jet_dir, jet_ref_track_dir, *pv_); fts.fill("pfcand_btagEtaRel", trkinfo.getTrackEtaRel()); fts.fill("pfcand_btagPtRatio", trkinfo.getTrackPtRatio()); @@ -429,8 +444,8 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts, const reco::Jet &jet){ std::vector jetSVs; - for (const auto &sv : *svs){ - if (reco::deltaR(sv, jet) < jet_radius_) { + for (const auto &sv : *svs_){ + if (reco::deltaR2(sv, jet) < jet_radius_*jet_radius_) { jetSVs.push_back(&sv); } } @@ -439,6 +454,9 @@ void DeepBoostedJetTagInfoProducer::fillSVFeatures(DeepBoostedJetFeatures &fts, return sv_vertex_comparator(*sva, *svb, *pv_); }); + // reserve space + for (const auto &name : sv_features_) { fts.reserve(name, jetSVs.size()); } + const float etasign = jet.eta()>0 ? 1 : -1; for (const auto *sv : jetSVs){ diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc index f7afdaa5dfe6d..f8c8773e37314 100644 --- a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc +++ b/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc @@ -15,6 +15,8 @@ #include #include +#include +#include #include "PhysicsTools/MXNet/interface/Predictor.h" // Hold the mxnet model block (symbol + params) in the edm::GlobalCache. @@ -30,9 +32,9 @@ struct PreprocessParams { struct VarInfo { VarInfo() {} VarInfo(float median, float upper) : - center(median), scale(upper==median ? 1 : upper-median) {} + center(median), norm_factor(upper==median ? 1 : 1./(upper-median)) {} float center = 0; - float scale = 1; + float norm_factor = 1; }; unsigned var_length = 0; @@ -75,7 +77,7 @@ class DeepBoostedJetTagsProducer : public edm::stream::EDProducer src_; - std::vector>> flav_pairs_; + std::vector flav_names_; // names of the output scores std::vector input_names_; // names of each input group - the ordering is important! std::vector> input_shapes_; // shapes of each input group std::unordered_map prep_info_map_; // preprocessing info for each input group @@ -88,12 +90,11 @@ class DeepBoostedJetTagsProducer : public edm::stream::EDProducer(iConfig.getParameter("src"))) +, flav_names_(iConfig.getParameter>("flav_names")) , debug_(iConfig.getUntrackedParameter("debugMode", false)) { // load preprocessing info - input_shapes_.clear(); - prep_info_map_.clear(); const auto &prep_pset = iConfig.getParameterSet("preprocessParams"); input_names_ = prep_pset.getParameter>("input_names"); for (const auto &group_name : input_names_){ @@ -109,20 +110,24 @@ DeepBoostedJetTagsProducer::DeepBoostedJetTagsProducer(const edm::ParameterSet& double upper = var_pset.getParameter("upper"); prep_params.var_info_map[var_name] = PreprocessParams::VarInfo(median, upper); } + + // create data storage with a fixed size vector initilized w/ 0 + unsigned len = prep_params.var_length * prep_params.var_names.size(); + data_.emplace_back(len, 0); } if (debug_) { for (unsigned i=0; iblock)); predictor_->set_input_shapes(input_names_, input_shapes_); - // get output names from flav_table - const auto & flav_pset = iConfig.getParameter("flav_table"); - for (const auto& flav_pair : flav_pset.tbl()) { - const auto & flav_name = flav_pair.first; - flav_pairs_.emplace_back(flav_name, - flav_pset.getParameter>(flav_name)); - } - - for (const auto & flav_pair : flav_pairs_) { - produces(flav_pair.first); + // get output names from flav_names + for (const auto &flav_name : flav_names_) { + produces(flav_name); } } @@ -149,7 +147,6 @@ DeepBoostedJetTagsProducer::~DeepBoostedJetTagsProducer(){ void DeepBoostedJetTagsProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - // pfDeepBoostedJetTags edm::ParameterSetDescription desc; desc.add("src", edm::InputTag("pfDeepBoostedJetTagInfos")); @@ -160,27 +157,25 @@ void DeepBoostedJetTagsProducer::fillDescriptions(edm::ConfigurationDescriptions edm::FileInPath("RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-symbol.json")); desc.add("param_path", edm::FileInPath("RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-0000.params")); - { - edm::ParameterSetDescription psd0; - psd0.add>("probTbcq", {0}); - psd0.add>("probTbqq", {1}); - psd0.add>("probTbc", {2}); - psd0.add>("probTbq", {3}); - psd0.add>("probWcq", {4}); - psd0.add>("probWqq", {5}); - psd0.add>("probZbb", {6}); - psd0.add>("probZcc", {7}); - psd0.add>("probZqq", {8}); - psd0.add>("probHbb", {9}); - psd0.add>("probHcc", {10}); - psd0.add>("probHqqqq", {11}); - psd0.add>("probQCDbb", {12}); - psd0.add>("probQCDcc", {13}); - psd0.add>("probQCDb", {14}); - psd0.add>("probQCDc", {15}); - psd0.add>("probQCDothers", {16}); - desc.add("flav_table", psd0); - } + desc.add>("flav_names", std::vector{ + "probTbcq", + "probTbqq", + "probTbc", + "probTbq", + "probWcq", + "probWqq", + "probZbb", + "probZcc", + "probZqq", + "probHbb", + "probHcc", + "probHqqqq", + "probQCDbb", + "probQCDcc", + "probQCDb", + "probQCDc", + "probQCDothers", + }); desc.addOptionalUntracked("debugMode", false); descriptions.add("pfDeepBoostedJetTags", desc); @@ -214,106 +209,91 @@ void DeepBoostedJetTagsProducer::produce(edm::Event& iEvent, const edm::EventSet // initialize output collection std::vector> output_tags; - for (std::size_t i=0; i < flav_pairs_.size(); i++) { - if (!tag_infos->empty()) { - auto jet_ref = tag_infos->begin()->jet(); - output_tags.emplace_back(std::make_unique( - edm::makeRefToBaseProdFrom(jet_ref, iEvent))); - } else { + if (!tag_infos->empty()) { + auto jet_ref = tag_infos->begin()->jet(); + auto ref2prod = edm::makeRefToBaseProdFrom(jet_ref, iEvent); + for (std::size_t i=0; i < flav_names_.size(); i++) { + output_tags.emplace_back(std::make_unique(ref2prod)); + } + } else { + for (std::size_t i=0; i < flav_names_.size(); i++) { output_tags.emplace_back(std::make_unique()); } } for (unsigned jet_n=0; jet_nsize(); ++jet_n){ - const auto& taginfo = tag_infos->at(jet_n); - std::vector outputs(flav_pairs_.size(), 0); // init as all zeros + const auto& taginfo = (*tag_infos)[jet_n]; + std::vector outputs(flav_names_.size(), 0); // init as all zeros if (!taginfo.features().empty()){ // convert inputs make_inputs(taginfo); // run prediction and get outputs outputs = predictor_->predict(data_); + assert(outputs.size() == flav_names_.size()); } const auto & jet_ref = tag_infos->at(jet_n).jet(); - for (std::size_t flav_n=0; flav_n < flav_pairs_.size(); flav_n++) { - const auto & flav_pair = flav_pairs_.at(flav_n); - float o_sum = 0.; - for (const unsigned int & ind : flav_pair.second) { - o_sum += outputs.at(ind); - } - (*(output_tags.at(flav_n)))[jet_ref] = o_sum; + for (std::size_t flav_n=0; flav_n < flav_names_.size(); flav_n++) { + (*(output_tags[flav_n]))[jet_ref] = outputs[flav_n]; } } if (debug_){ - std::cerr << "=== " << iEvent.id().run() << ":" << iEvent.id().luminosityBlock() << ":" << iEvent.id().event() << " ===" << std::endl; + std::cout << "=== " << iEvent.id().run() << ":" << iEvent.id().luminosityBlock() << ":" << iEvent.id().event() << " ===" << std::endl; for (unsigned jet_n=0; jet_nsize(); ++jet_n){ const auto & jet_ref = tag_infos->at(jet_n).jet(); - std::cerr << " - Jet #" << jet_n << ", pt=" << jet_ref->pt() << ", eta=" << jet_ref->eta() << ", phi=" << jet_ref->phi() << std::endl; - for (std::size_t flav_n=0; flav_n < flav_pairs_.size(); ++flav_n) { - std::cerr << " " << flav_pairs_.at(flav_n).first << " = " << (*(output_tags.at(flav_n)))[jet_ref] << std::endl; + std::cout << " - Jet #" << jet_n << ", pt=" << jet_ref->pt() << ", eta=" << jet_ref->eta() << ", phi=" << jet_ref->phi() << std::endl; + for (std::size_t flav_n=0; flav_n < flav_names_.size(); ++flav_n) { + std::cout << " " << flav_names_.at(flav_n) << " = " << (*(output_tags.at(flav_n)))[jet_ref] << std::endl; } } } // put into the event - for (std::size_t i=0; i < flav_pairs_.size(); i++) { - iEvent.put(std::move(output_tags[i]), flav_pairs_.at(i).first); + for (std::size_t flav_n=0; flav_n < flav_names_.size(); ++flav_n) { + iEvent.put(std::move(output_tags[flav_n]), flav_names_[flav_n]); } } std::vector DeepBoostedJetTagsProducer::center_norm_pad( - const std::vector& input, float center, float scale, + const std::vector& input, float center, float norm_factor, unsigned target_length, float pad_value, float min, float max) { // do variable shifting/scaling/padding/clipping in one go - auto clip = [](float value, float low, float high){ - if (low >= high) throw cms::Exception("InvalidArgument") << "Error in clip: low >= high!"; - if (value < low) return low; - if (value > high) return high; - return value; - }; + assert(min<=pad_value && pad_value<=max); - pad_value = clip(pad_value, min, max); std::vector out(target_length, pad_value); for (unsigned i=0; i(raw_value.size()) != var_ref_len) - throw cms::Exception("InvalidArgument") << "Inconsistent variable length " << raw_value.size() << " for " << varname << ", should be " << var_ref_len; - } const auto &info = prep_params.get_info(varname); - float pad = 0; // pad w/ zero - auto val = center_norm_pad(raw_value, info.center, info.scale, prep_params.var_length, pad, -5, 5); - group_values.insert(group_values.end(), val.begin(), val.end()); + const float pad = 0; // pad w/ zero + auto val = center_norm_pad(raw_value, info.center, info.norm_factor, prep_params.var_length, pad, -5, 5); + std::copy(val.begin(), val.end(), group_values.begin()+curr_pos); + curr_pos += prep_params.var_length; if (debug_){ - std::cerr << " -- var=" << varname << ", center=" << info.center << ", scale=" << info.scale << ", pad=" << pad << std::endl; - std::cerr << "values (first 7 and last 3): " << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << ", " << val.at(3) << ", " << val.at(4) << ", " << val.at(5) << ", " << val.at(6) << " ... " + std::cout << " -- var=" << varname << ", center=" << info.center << ", scale=" << info.norm_factor << ", pad=" << pad << std::endl; + std::cout << "values (first 7 and last 3): " << val.at(0) << ", " << val.at(1) << ", " << val.at(2) << ", " << val.at(3) << ", " << val.at(4) << ", " << val.at(5) << ", " << val.at(6) << " ... " << val.at(prep_params.var_length-3) << ", " << val.at(prep_params.var_length-2) << ", " << val.at(prep_params.var_length-1) << std::endl; } diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py index 988d7df27ef4c..cb709a2794a95 100644 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py +++ b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py @@ -306,4 +306,4 @@ 'sv_costhetasvpv' ) ) -) \ No newline at end of file +) diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetTags_cfi.py b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetTags_cfi.py deleted file mode 100644 index 95122666e95c4..0000000000000 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetTags_cfi.py +++ /dev/null @@ -1,27 +0,0 @@ -import FWCore.ParameterSet.Config as cms - -_pfDeepBoostedJetTags = cms.EDProducer('DeepBoostedJetTagsProducer', - src = cms.InputTag('pfDeepBoostedJetTagInfos'), - preprocessParams = cms.PSet(), - model_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-symbol.json'), - param_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-0000.params'), - flav_table = cms.PSet( - probTbcq = cms.vuint32(0), - probTbqq = cms.vuint32(1), - probTbc = cms.vuint32(2), - probTbq = cms.vuint32(3), - probWcq = cms.vuint32(4), - probWqq = cms.vuint32(5), - probZbb = cms.vuint32(6), - probZcc = cms.vuint32(7), - probZqq = cms.vuint32(8), - probHbb = cms.vuint32(9), - probHcc = cms.vuint32(10), - probHqqqq = cms.vuint32(11), - probQCDbb = cms.vuint32(12), - probQCDcc = cms.vuint32(13), - probQCDb = cms.vuint32(14), - probQCDc = cms.vuint32(15), - probQCDothers = cms.vuint32(16) - ) -) diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py index 02f945be20409..d6931c90e6b1a 100644 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py +++ b/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py @@ -1,7 +1,7 @@ import FWCore.ParameterSet.Config as cms from RecoBTag.DeepBoostedJet.pfDeepBoostedJetTagInfos_cfi import pfDeepBoostedJetTagInfos -from RecoBTag.DeepBoostedJet.pfDeepBoostedJetTags_cfi import _pfDeepBoostedJetTags +from RecoBTag.DeepBoostedJet.pfDeepBoostedJetTags_cfi import pfDeepBoostedJetTags as _pfDeepBoostedJetTags from RecoBTag.DeepBoostedJet.pfDeepBoostedJetPreprocessParams_cfi import pfDeepBoostedJetPreprocessParams from RecoBTag.DeepBoostedJet.pfDeepBoostedDiscriminatorsJetTags_cfi import pfDeepBoostedDiscriminatorsJetTags from RecoBTag.DeepBoostedJet.pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi import pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags @@ -9,17 +9,17 @@ # nominal DeepAK8 pfDeepBoostedJetTags = _pfDeepBoostedJetTags.clone( preprocessParams = pfDeepBoostedJetPreprocessParams, - model_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-symbol.json'), - param_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-0000.params'), - debugMode = cms.untracked.bool(False), # debug + model_path = 'RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-symbol.json', + param_path = 'RecoBTag/Combined/data/DeepBoostedJet/V01/full/resnet-0000.params', + debugMode = False, # debug ) # mass-decorrelated DeepAK8 pfMassDecorrelatedDeepBoostedJetTags = _pfDeepBoostedJetTags.clone( preprocessParams = pfDeepBoostedJetPreprocessParams, - model_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/decorrelated/resnet-symbol.json'), - param_path = cms.FileInPath('RecoBTag/Combined/data/DeepBoostedJet/V01/decorrelated/resnet-0000.params'), - debugMode = cms.untracked.bool(False), # debug + model_path = 'RecoBTag/Combined/data/DeepBoostedJet/V01/decorrelated/resnet-symbol.json', + param_path = 'RecoBTag/Combined/data/DeepBoostedJet/V01/decorrelated/resnet-0000.params', + debugMode = False, # debug ) from CommonTools.PileupAlgos.Puppi_cff import puppi @@ -30,3 +30,21 @@ pfDeepBoostedJetTask = cms.Task(puppi, primaryVertexAssociation, pfDeepBoostedJetTagInfos, pfDeepBoostedJetTags, pfMassDecorrelatedDeepBoostedJetTags, pfDeepBoostedDiscriminatorsJetTags, pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags) + +# declare all the discriminators +# nominal: probs +_pfDeepBoostedJetTagsProbs = ['pfDeepBoostedJetTags:' + flav_name + for flav_name in pfDeepBoostedJetTags.flav_names] +# nominal: meta-taggers +_pfDeepBoostedJetTagsMetaDiscrs = ['pfDeepBoostedDiscriminatorsJetTags:' + disc.name.value() + for disc in pfDeepBoostedDiscriminatorsJetTags.discriminators] + +# mass-decorrelated: probs +_pfMassDecorrelatedDeepBoostedJetTagsProbs = ['pfMassDecorrelatedDeepBoostedJetTags:' + flav_name + for flav_name in pfMassDecorrelatedDeepBoostedJetTags.flav_names] +# mass-decorrelated: meta-taggers +_pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs = ['pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:' + disc.name.value() + for disc in pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags.discriminators] + +_pfDeepBoostedJetTagsAll = _pfDeepBoostedJetTagsProbs + _pfDeepBoostedJetTagsMetaDiscrs + \ + _pfMassDecorrelatedDeepBoostedJetTagsProbs + _pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs From 22d3cb58687f339ea7f3d769445156e735eb2a21 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 3 Sep 2018 18:11:36 +0200 Subject: [PATCH 26/31] Mark DeepBoostedJetTagInfo as transient. --- DataFormats/BTauReco/src/classes_def.xml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/DataFormats/BTauReco/src/classes_def.xml b/DataFormats/BTauReco/src/classes_def.xml index dd4e72bdcade0..38ae4a5b9ad72 100644 --- a/DataFormats/BTauReco/src/classes_def.xml +++ b/DataFormats/BTauReco/src/classes_def.xml @@ -471,16 +471,13 @@ - - - - + - + From 4e5fb07a179c9912cde6e4820ea1a9e1f011de02 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Mon, 3 Sep 2018 18:26:23 +0200 Subject: [PATCH 27/31] Reorganize TagInfo producers and JetTags producers. --- .../PatAlgos/python/recoLayer0/bTagging_cff.py | 2 +- .../PatAlgos/python/slimming/applyDeepBtagging_cff.py | 2 +- RecoBTag/Configuration/python/RecoBTag_cff.py | 2 +- .../plugins/BuildFile.xml | 3 +-- .../plugins/DeepBoostedJetTagInfoProducer.cc | 0 .../plugins/DeepDoubleBTagInfoProducer.cc | 0 .../plugins/DeepFlavourTagInfoProducer.cc | 0 .../python/pfNegativeDeepFlavourTagInfos_cfi.py | 2 +- RecoBTag/MXNet/plugins/BuildFile.xml | 7 +++++++ .../plugins/DeepBoostedJetTagsProducer.cc | 0 .../python/pfDeepBoostedDiscriminatorsJetTags_cfi.py | 0 .../python/pfDeepBoostedJetPreprocessParams_cfi.py | 0 .../python/pfDeepBoostedJet_cff.py | 10 +++++----- ...DecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py | 0 .../test/test_deep_boosted_jet_cfg.py | 0 RecoBTag/TensorFlow/plugins/BuildFile.xml | 7 +------ RecoBTag/TensorFlow/python/pfDeepFlavour_cff.py | 8 +++++--- 17 files changed, 23 insertions(+), 20 deletions(-) rename RecoBTag/{DeepBoostedJet => FeatureTools}/plugins/BuildFile.xml (73%) rename RecoBTag/{DeepBoostedJet => FeatureTools}/plugins/DeepBoostedJetTagInfoProducer.cc (100%) rename RecoBTag/{TensorFlow => FeatureTools}/plugins/DeepDoubleBTagInfoProducer.cc (100%) rename RecoBTag/{TensorFlow => FeatureTools}/plugins/DeepFlavourTagInfoProducer.cc (100%) rename RecoBTag/{TensorFlow => FeatureTools}/python/pfNegativeDeepFlavourTagInfos_cfi.py (75%) create mode 100644 RecoBTag/MXNet/plugins/BuildFile.xml rename RecoBTag/{DeepBoostedJet => MXNet}/plugins/DeepBoostedJetTagsProducer.cc (100%) rename RecoBTag/{DeepBoostedJet => MXNet}/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py (100%) rename RecoBTag/{DeepBoostedJet => MXNet}/python/pfDeepBoostedJetPreprocessParams_cfi.py (100%) rename RecoBTag/{DeepBoostedJet => MXNet}/python/pfDeepBoostedJet_cff.py (81%) rename RecoBTag/{DeepBoostedJet => MXNet}/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py (100%) rename RecoBTag/{DeepBoostedJet => MXNet}/test/test_deep_boosted_jet_cfg.py (100%) diff --git a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py index fd7357fc8f300..d8a42f9dc32b0 100644 --- a/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py +++ b/PhysicsTools/PatAlgos/python/recoLayer0/bTagging_cff.py @@ -207,7 +207,7 @@ # ----------------------------------- # setup DeepBoostedJet -from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsProbs, _pfDeepBoostedJetTagsMetaDiscrs, \ +from RecoBTag.MXNet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsProbs, _pfDeepBoostedJetTagsMetaDiscrs, \ _pfMassDecorrelatedDeepBoostedJetTagsProbs, _pfMassDecorrelatedDeepBoostedJetTagsMetaDiscrs # update supportedBtagDiscr for disc in _pfDeepBoostedJetTagsProbs + _pfMassDecorrelatedDeepBoostedJetTagsProbs: diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index a35b5ef32e9ba..5eb06b01e120b 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -42,7 +42,7 @@ def applyDeepBtagging( process, postfix="" ) : # delete module not used anymore (slimmedJets substitutes) delattr(process, 'selectedUpdatedPatJetsSlimmedDeepFlavour'+postfix) - from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsAll as pfDeepBoostedJetTagsAll + from RecoBTag.MXNet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsAll as pfDeepBoostedJetTagsAll # update slimmed jets to include particle-based deep taggers (keep same name) # make clone for DeepTags-less slimmed AK8 jets, so output name is preserved diff --git a/RecoBTag/Configuration/python/RecoBTag_cff.py b/RecoBTag/Configuration/python/RecoBTag_cff.py index 98ffb4e043ba0..170ceb6abe864 100644 --- a/RecoBTag/Configuration/python/RecoBTag_cff.py +++ b/RecoBTag/Configuration/python/RecoBTag_cff.py @@ -8,7 +8,7 @@ from RecoBTag.CTagging.RecoCTagging_cff import * from RecoBTag.Combined.deepFlavour_cff import * from RecoBTag.TensorFlow.pfDeepFlavour_cff import * -from RecoBTag.DeepBoostedJet.pfDeepBoostedJet_cff import * +from RecoBTag.MXNet.pfDeepBoostedJet_cff import * from RecoVertex.AdaptiveVertexFinder.inclusiveVertexing_cff import * legacyBTaggingTask = cms.Task( diff --git a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml b/RecoBTag/FeatureTools/plugins/BuildFile.xml similarity index 73% rename from RecoBTag/DeepBoostedJet/plugins/BuildFile.xml rename to RecoBTag/FeatureTools/plugins/BuildFile.xml index 0b15cd13b46d7..1fc67a1ed5ace 100644 --- a/RecoBTag/DeepBoostedJet/plugins/BuildFile.xml +++ b/RecoBTag/FeatureTools/plugins/BuildFile.xml @@ -1,10 +1,9 @@ - + - diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc similarity index 100% rename from RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagInfoProducer.cc rename to RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc diff --git a/RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepDoubleBTagInfoProducer.cc similarity index 100% rename from RecoBTag/TensorFlow/plugins/DeepDoubleBTagInfoProducer.cc rename to RecoBTag/FeatureTools/plugins/DeepDoubleBTagInfoProducer.cc diff --git a/RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc similarity index 100% rename from RecoBTag/TensorFlow/plugins/DeepFlavourTagInfoProducer.cc rename to RecoBTag/FeatureTools/plugins/DeepFlavourTagInfoProducer.cc diff --git a/RecoBTag/TensorFlow/python/pfNegativeDeepFlavourTagInfos_cfi.py b/RecoBTag/FeatureTools/python/pfNegativeDeepFlavourTagInfos_cfi.py similarity index 75% rename from RecoBTag/TensorFlow/python/pfNegativeDeepFlavourTagInfos_cfi.py rename to RecoBTag/FeatureTools/python/pfNegativeDeepFlavourTagInfos_cfi.py index cdda7a5720050..50ed1e6b155cf 100644 --- a/RecoBTag/TensorFlow/python/pfNegativeDeepFlavourTagInfos_cfi.py +++ b/RecoBTag/FeatureTools/python/pfNegativeDeepFlavourTagInfos_cfi.py @@ -1,6 +1,6 @@ import FWCore.ParameterSet.Config as cms -from RecoBTag.TensorFlow.pfDeepFlavourTagInfos_cfi import pfDeepFlavourTagInfos +from RecoBTag.FeatureTools.pfDeepFlavourTagInfos_cfi import pfDeepFlavourTagInfos pfNegativeDeepFlavourTagInfos = pfDeepFlavourTagInfos.clone( shallow_tag_infos = 'pfDeepCSVNegativeTagInfos', diff --git a/RecoBTag/MXNet/plugins/BuildFile.xml b/RecoBTag/MXNet/plugins/BuildFile.xml new file mode 100644 index 0000000000000..ff17f046f6995 --- /dev/null +++ b/RecoBTag/MXNet/plugins/BuildFile.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc b/RecoBTag/MXNet/plugins/DeepBoostedJetTagsProducer.cc similarity index 100% rename from RecoBTag/DeepBoostedJet/plugins/DeepBoostedJetTagsProducer.cc rename to RecoBTag/MXNet/plugins/DeepBoostedJetTagsProducer.cc diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py b/RecoBTag/MXNet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py similarity index 100% rename from RecoBTag/DeepBoostedJet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py rename to RecoBTag/MXNet/python/pfDeepBoostedDiscriminatorsJetTags_cfi.py diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py b/RecoBTag/MXNet/python/pfDeepBoostedJetPreprocessParams_cfi.py similarity index 100% rename from RecoBTag/DeepBoostedJet/python/pfDeepBoostedJetPreprocessParams_cfi.py rename to RecoBTag/MXNet/python/pfDeepBoostedJetPreprocessParams_cfi.py diff --git a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py b/RecoBTag/MXNet/python/pfDeepBoostedJet_cff.py similarity index 81% rename from RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py rename to RecoBTag/MXNet/python/pfDeepBoostedJet_cff.py index d6931c90e6b1a..f8915d164f9d3 100644 --- a/RecoBTag/DeepBoostedJet/python/pfDeepBoostedJet_cff.py +++ b/RecoBTag/MXNet/python/pfDeepBoostedJet_cff.py @@ -1,10 +1,10 @@ import FWCore.ParameterSet.Config as cms -from RecoBTag.DeepBoostedJet.pfDeepBoostedJetTagInfos_cfi import pfDeepBoostedJetTagInfos -from RecoBTag.DeepBoostedJet.pfDeepBoostedJetTags_cfi import pfDeepBoostedJetTags as _pfDeepBoostedJetTags -from RecoBTag.DeepBoostedJet.pfDeepBoostedJetPreprocessParams_cfi import pfDeepBoostedJetPreprocessParams -from RecoBTag.DeepBoostedJet.pfDeepBoostedDiscriminatorsJetTags_cfi import pfDeepBoostedDiscriminatorsJetTags -from RecoBTag.DeepBoostedJet.pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi import pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags +from RecoBTag.FeatureTools.pfDeepBoostedJetTagInfos_cfi import pfDeepBoostedJetTagInfos +from RecoBTag.MXNet.pfDeepBoostedJetTags_cfi import pfDeepBoostedJetTags as _pfDeepBoostedJetTags +from RecoBTag.MXNet.pfDeepBoostedJetPreprocessParams_cfi import pfDeepBoostedJetPreprocessParams +from RecoBTag.MXNet.pfDeepBoostedDiscriminatorsJetTags_cfi import pfDeepBoostedDiscriminatorsJetTags +from RecoBTag.MXNet.pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi import pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags # nominal DeepAK8 pfDeepBoostedJetTags = _pfDeepBoostedJetTags.clone( diff --git a/RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py b/RecoBTag/MXNet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py similarity index 100% rename from RecoBTag/DeepBoostedJet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py rename to RecoBTag/MXNet/python/pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags_cfi.py diff --git a/RecoBTag/DeepBoostedJet/test/test_deep_boosted_jet_cfg.py b/RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py similarity index 100% rename from RecoBTag/DeepBoostedJet/test/test_deep_boosted_jet_cfg.py rename to RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py diff --git a/RecoBTag/TensorFlow/plugins/BuildFile.xml b/RecoBTag/TensorFlow/plugins/BuildFile.xml index b4787e210de56..84335c68879f2 100644 --- a/RecoBTag/TensorFlow/plugins/BuildFile.xml +++ b/RecoBTag/TensorFlow/plugins/BuildFile.xml @@ -1,13 +1,8 @@ - + - - - - - diff --git a/RecoBTag/TensorFlow/python/pfDeepFlavour_cff.py b/RecoBTag/TensorFlow/python/pfDeepFlavour_cff.py index adef4a5db356c..9f41d24a4d3cd 100644 --- a/RecoBTag/TensorFlow/python/pfDeepFlavour_cff.py +++ b/RecoBTag/TensorFlow/python/pfDeepFlavour_cff.py @@ -1,11 +1,13 @@ import FWCore.ParameterSet.Config as cms -from RecoBTag.TensorFlow.pfDeepFlavourTagInfos_cfi import pfDeepFlavourTagInfos +from RecoBTag.FeatureTools.pfDeepFlavourTagInfos_cfi import pfDeepFlavourTagInfos +from RecoBTag.FeatureTools.pfNegativeDeepFlavourTagInfos_cfi import pfNegativeDeepFlavourTagInfos +from RecoBTag.FeatureTools.pfDeepDoubleBTagInfos_cfi import pfDeepDoubleBTagInfos + from RecoBTag.TensorFlow.pfDeepFlavourJetTags_cfi import pfDeepFlavourJetTags -from RecoBTag.TensorFlow.pfNegativeDeepFlavourTagInfos_cfi import pfNegativeDeepFlavourTagInfos from RecoBTag.TensorFlow.pfNegativeDeepFlavourJetTags_cfi import pfNegativeDeepFlavourJetTags -from RecoBTag.TensorFlow.pfDeepDoubleBTagInfos_cfi import pfDeepDoubleBTagInfos from RecoBTag.TensorFlow.pfDeepDoubleBJetTags_cfi import pfDeepDoubleBJetTags + from CommonTools.PileupAlgos.Puppi_cff import puppi from PhysicsTools.PatAlgos.slimming.primaryVertexAssociation_cfi import primaryVertexAssociation From 0f3a195990e1d884fef9436d707e377da05e2cb5 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Tue, 4 Sep 2018 01:21:46 +0200 Subject: [PATCH 28/31] A few fixes. --- ...tPredictor.cc => testMXNetCppPredictor.cc} | 0 .../plugins/DeepBoostedJetTagInfoProducer.cc | 4 +- .../MXNet/test/test_deep_boosted_jet_cfg.py | 54 +------------------ 3 files changed, 4 insertions(+), 54 deletions(-) rename PhysicsTools/MXNet/test/{testPredictor.cc => testMXNetCppPredictor.cc} (100%) diff --git a/PhysicsTools/MXNet/test/testPredictor.cc b/PhysicsTools/MXNet/test/testMXNetCppPredictor.cc similarity index 100% rename from PhysicsTools/MXNet/test/testPredictor.cc rename to PhysicsTools/MXNet/test/testMXNetCppPredictor.cc diff --git a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc index f043896539ea7..1a7c4205e4a58 100644 --- a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc @@ -72,7 +72,7 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> const reco::Vertex *pv_ = nullptr; }; -const static std::vector DeepBoostedJetTagInfoProducer::particle_features_ { +const std::vector DeepBoostedJetTagInfoProducer::particle_features_ { "pfcand_puppiw", "pfcand_hcalFrac", "pfcand_VTX_ass", @@ -117,7 +117,7 @@ const static std::vector DeepBoostedJetTagInfoProducer::particle_fe "pfcand_btagJetDistVal", }; -const static std::vector DeepBoostedJetTagInfoProducer::sv_features_ { +const std::vector DeepBoostedJetTagInfoProducer::sv_features_ { "sv_phirel", "sv_etarel", "sv_deltaR", diff --git a/RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py b/RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py index e4bf66bd08fd2..b28dcae0a65eb 100644 --- a/RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py +++ b/RecoBTag/MXNet/test/test_deep_boosted_jet_cfg.py @@ -48,6 +48,7 @@ ## and add them to the event content from PhysicsTools.PatAlgos.tools.jetTools import updateJetCollection +from RecoBTag.MXNet.pfDeepBoostedJet_cff import _pfDeepBoostedJetTagsAll as pfDeepBoostedJetTagsAll updateJetCollection( process, @@ -56,58 +57,7 @@ svSource = cms.InputTag('slimmedSecondaryVertices'), rParam = 0.8, jetCorrections = ('AK8PFPuppi', cms.vstring(['L2Relative', 'L3Absolute']), 'None'), - btagDiscriminators = [ - # DeepBoostedJet (Nominal) - 'pfDeepBoostedJetTags:probTbcq', - 'pfDeepBoostedJetTags:probTbqq', - 'pfDeepBoostedJetTags:probTbc', - 'pfDeepBoostedJetTags:probTbq', - 'pfDeepBoostedJetTags:probWcq', - 'pfDeepBoostedJetTags:probWqq', - 'pfDeepBoostedJetTags:probZbb', - 'pfDeepBoostedJetTags:probZcc', - 'pfDeepBoostedJetTags:probZqq', - 'pfDeepBoostedJetTags:probHbb', - 'pfDeepBoostedJetTags:probHcc', - 'pfDeepBoostedJetTags:probHqqqq', - 'pfDeepBoostedJetTags:probQCDbb', - 'pfDeepBoostedJetTags:probQCDcc', - 'pfDeepBoostedJetTags:probQCDb', - 'pfDeepBoostedJetTags:probQCDc', - 'pfDeepBoostedJetTags:probQCDothers', - # meta taggers - 'pfDeepBoostedDiscriminatorsJetTags:TvsQCD', - 'pfDeepBoostedDiscriminatorsJetTags:WvsQCD', - - # DeepBoostedJet (mass decorrelated) - 'pfMassDecorrelatedDeepBoostedJetTags:probTbcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbc', - 'pfMassDecorrelatedDeepBoostedJetTags:probTbq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWcq', - 'pfMassDecorrelatedDeepBoostedJetTags:probWqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probZbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probZcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probZqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probHbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probHcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probHqqqq', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDbb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDcc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDb', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDc', - 'pfMassDecorrelatedDeepBoostedJetTags:probQCDothers', - # meta taggers - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:TvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:WvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ccvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHccvsQCD', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:bbvscc', - 'pfMassDecorrelatedDeepBoostedDiscriminatorsJetTags:ZHbbvsZHcc', - - ] + btagDiscriminators = pfDeepBoostedJetTagsAll ) from Configuration.EventContent.EventContent_cff import MINIAODSIMEventContent From 1f639cfee246856b54b05f8a3633e788a189b34e Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Fri, 7 Sep 2018 15:48:06 +0200 Subject: [PATCH 29/31] Improve the setup of DeepBoostedJetTagInfos. --- PhysicsTools/PatAlgos/python/tools/jetTools.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index 865dc4865c8f8..bda0998e1a978 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -632,19 +632,27 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou if btagInfo == 'pfDeepBoostedJetTagInfos': if pfCandidates.value() == 'packedPFCandidates': + # case 1: running over jets whose daughters are PackedCandidates (only via updateJetCollection for now) jetSrcName = jetSource.value().lower() - if 'slimmed' in jetSrcName or 'updated' in jetSrcName: - # case 1: update jets whose daughters are PackedCandidates, e.g., slimmedJetsAK8, etc. - # daughters are links to original PackedCandidates, so NOT scaled by their puppi weights yet - has_puppi_weighted_daughters = False + if 'updated' in jetSrcName: puppi_value_map = "" vertex_associator = "" + if 'withpuppidaughter' in jetSrcName: + # special case for Puppi jets reclustered from MiniAOD by analyzers + # need to specify 'WithPuppiDaughters' in the postfix when calling updateJetCollection + # daughters of these jets are already scaled by their puppi weights + has_puppi_weighted_daughters = True + else: + # default case for updating jet collection stored in MiniAOD, e.g., slimmedJetsAK8 + # daughters are links to the original PackedCandidates, so NOT scaled by their puppi weights yet + has_puppi_weighted_daughters = False else: - raise ValueError("Cannot run pfDeepBoostedJetTagInfos on jet collection: %s." % jetSource.value()) + raise ValueError("Invalid jet collection: %s. pfDeepBoostedJetTagInfos only supports running via updateJetCollection." % jetSource.value()) elif pfCandidates.value() == 'particleFlow': raise ValueError("Running pfDeepBoostedJetTagInfos with reco::PFCandidates is currently not supported.") # case 2: running on new jet collection whose daughters are PFCandidates (e.g., cluster jets in RECO/AOD) # daughters are the particles used in jet clustering, so already scaled by their puppi weights + # Uncomment the lines below after running pfDeepBoostedJetTagInfos with reco::PFCandidates becomes supported # has_puppi_weighted_daughters = True # puppi_value_map = "puppi" # vertex_associator = "primaryVertexAssociation:original" From 32b2b2c665cd3887c08c534682518b15b7f0b8a2 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Tue, 11 Sep 2018 16:09:30 +0200 Subject: [PATCH 30/31] Disable DeepBoostedJet in applyDeepBtagging_cff.py. --- PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py index 5eb06b01e120b..b1c54b0c8ac39 100644 --- a/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py +++ b/PhysicsTools/PatAlgos/python/slimming/applyDeepBtagging_cff.py @@ -62,7 +62,8 @@ def applyDeepBtagging( process, postfix="" ) : btagDiscriminators = [ 'pfDeepDoubleBJetTags:probQ', 'pfDeepDoubleBJetTags:probH', - ] + pfDeepBoostedJetTagsAll, + ], +# ] + pfDeepBoostedJetTagsAll, # uncomment it to test DeepBoostedJet postfix = 'SlimmedAK8DeepTags'+postfix, printWarning = False ) From ed815a31904a5fdb5dc9830469466900cba752d0 Mon Sep 17 00:00:00 2001 From: Huilin Qu Date: Wed, 12 Sep 2018 22:11:55 +0200 Subject: [PATCH 31/31] Revert fix on clearDaughters. Use pf2pc instead. --- .../interface/CompositePtrCandidate.h | 2 +- DataFormats/PatCandidates/interface/Jet.h | 10 ++-------- .../PatAlgos/python/tools/jetTools.py | 6 ++++++ .../plugins/DeepBoostedJetTagInfoProducer.cc | 19 ++++++++++++++++++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/DataFormats/Candidate/interface/CompositePtrCandidate.h b/DataFormats/Candidate/interface/CompositePtrCandidate.h index f13b9089c3567..ba84830a61c51 100755 --- a/DataFormats/Candidate/interface/CompositePtrCandidate.h +++ b/DataFormats/Candidate/interface/CompositePtrCandidate.h @@ -48,7 +48,7 @@ namespace reco { /// add a daughter via a reference void addDaughter( const CandidatePtr & ); /// clear daughter references - virtual void clearDaughters() { dau.clear(); } + void clearDaughters() { dau.clear(); } /// reference to daughter at given position virtual CandidatePtr daughterPtr( size_type i ) const { return dau[ i ]; } /// references to daughtes diff --git a/DataFormats/PatCandidates/interface/Jet.h b/DataFormats/PatCandidates/interface/Jet.h index eeccc7897ce27..fdaa76e1465ec 100644 --- a/DataFormats/PatCandidates/interface/Jet.h +++ b/DataFormats/PatCandidates/interface/Jet.h @@ -452,12 +452,6 @@ namespace pat { /// Else return the reco Jet number of constituents size_t numberOfDaughters() const override; - /// clear daughter references - void clearDaughters() override { - PATObject::clearDaughters(); - daughtersTemp_.reset(); // need to reset daughtersTemp_ as well - } - /// accessing Jet ID information reco::JetID const & jetID () const { return jetID_;} @@ -516,10 +510,10 @@ namespace pat { /// Check to see if the subjet collection exists bool hasSubjets( std::string const & label ) const { return find( subjetLabels_.begin(), subjetLabels_.end(), label) != subjetLabels_.end(); } - + /// Number of subjet collections unsigned int nSubjetCollections( ) const { return subjetCollections_.size(); } - + /// Subjet collection names std::vector const & subjetCollectionNames() const { return subjetLabels_; } diff --git a/PhysicsTools/PatAlgos/python/tools/jetTools.py b/PhysicsTools/PatAlgos/python/tools/jetTools.py index bda0998e1a978..eb93baba9c444 100644 --- a/PhysicsTools/PatAlgos/python/tools/jetTools.py +++ b/PhysicsTools/PatAlgos/python/tools/jetTools.py @@ -637,6 +637,7 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou if 'updated' in jetSrcName: puppi_value_map = "" vertex_associator = "" + fix_daughters = False if 'withpuppidaughter' in jetSrcName: # special case for Puppi jets reclustered from MiniAOD by analyzers # need to specify 'WithPuppiDaughters' in the postfix when calling updateJetCollection @@ -646,6 +647,9 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou # default case for updating jet collection stored in MiniAOD, e.g., slimmedJetsAK8 # daughters are links to the original PackedCandidates, so NOT scaled by their puppi weights yet has_puppi_weighted_daughters = False + if 'slimmed' in jetSrcName: + # when adding DeepBoostedJetTag to slimmedJetsAK8 in the MiniAOD step + fix_daughters = True else: raise ValueError("Invalid jet collection: %s. pfDeepBoostedJetTagInfos only supports running via updateJetCollection." % jetSource.value()) elif pfCandidates.value() == 'particleFlow': @@ -654,6 +658,7 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou # daughters are the particles used in jet clustering, so already scaled by their puppi weights # Uncomment the lines below after running pfDeepBoostedJetTagInfos with reco::PFCandidates becomes supported # has_puppi_weighted_daughters = True +# fix_daughters = False # puppi_value_map = "puppi" # vertex_associator = "primaryVertexAssociation:original" else: @@ -664,6 +669,7 @@ def setupBTagging(process, jetSource, pfCandidates, explicitJTA, pvSource, svSou vertices = pvSource, secondary_vertices = svSource, has_puppi_weighted_daughters = has_puppi_weighted_daughters, + fix_daughters = fix_daughters, puppi_value_map = puppi_value_map, vertex_associator = vertex_associator, ), diff --git a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc index 1a7c4205e4a58..e52a14e835d87 100644 --- a/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc +++ b/RecoBTag/FeatureTools/plugins/DeepBoostedJetTagInfoProducer.cc @@ -7,6 +7,7 @@ #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/Utilities/interface/StreamID.h" +#include "DataFormats/Common/interface/RefToPtr.h" #include "DataFormats/Candidate/interface/Candidate.h" #include "DataFormats/PatCandidates/interface/Jet.h" @@ -55,10 +56,12 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> bool use_puppi_value_map_; bool use_pvasq_value_map_; + bool fix_daughters_; edm::EDGetTokenT> puppi_value_map_token_; edm::EDGetTokenT> pvasq_value_map_token_; edm::EDGetTokenT> pvas_token_; + edm::EDGetTokenT> pf2pc_token_; edm::Handle vtxs_; edm::Handle svs_; @@ -66,6 +69,7 @@ class DeepBoostedJetTagInfoProducer : public edm::stream::EDProducer<> edm::Handle> puppi_value_map_; edm::Handle> pvasq_value_map_; edm::Handle> pvas_; + edm::Handle> pf2pc_; const static std::vector particle_features_; const static std::vector sv_features_; @@ -145,6 +149,7 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete , sv_token_(consumes(iConfig.getParameter("secondary_vertices"))) , use_puppi_value_map_(false) , use_pvasq_value_map_(false) +, fix_daughters_(iConfig.getParameter("fix_daughters")) { const auto & puppi_value_map_tag = iConfig.getParameter("puppi_value_map"); @@ -160,6 +165,10 @@ DeepBoostedJetTagInfoProducer::DeepBoostedJetTagInfoProducer(const edm::Paramete use_pvasq_value_map_ = true; } + if (fix_daughters_) { + pf2pc_token_ = consumes >(iConfig.getParameter("packed_candidates")); + } + produces(); } @@ -176,11 +185,13 @@ void DeepBoostedJetTagInfoProducer::fillDescriptions(edm::ConfigurationDescripti desc.add("jet_radius", 0.8); desc.add("min_jet_pt", 150); desc.add("min_pt_for_track_properties", -1); + desc.add("fix_daughters", false); desc.add("vertices", edm::InputTag("offlinePrimaryVertices")); desc.add("secondary_vertices", edm::InputTag("inclusiveCandidateSecondaryVertices")); desc.add("jets", edm::InputTag("ak8PFJetsPuppi")); desc.add("puppi_value_map", edm::InputTag("puppi")); desc.add("vertex_associator", edm::InputTag("primaryVertexAssociation","original")); + desc.add("packed_candidates", edm::InputTag("packedPFCandidates")); descriptions.add("pfDeepBoostedJetTagInfos", desc); } @@ -214,6 +225,11 @@ void DeepBoostedJetTagInfoProducer::produce(edm::Event& iEvent, const edm::Event iEvent.getByToken(pvas_token_, pvas_); } + if (fix_daughters_) { + iEvent.getByToken(pf2pc_token_, pf2pc_); + } + + for (std::size_t jet_n = 0; jet_n < jets->size(); jet_n++){ const auto& jet = (*jets)[jet_n]; @@ -279,7 +295,8 @@ void DeepBoostedJetTagInfoProducer::fillParticleFeatures(DeepBoostedJetFeatures }; std::vector daughters; - for (const auto& cand : jet.daughterPtrVector()){ + for (const auto &dau : jet.daughterPtrVector()){ + auto cand = fix_daughters_ ? edm::refToPtr((*pf2pc_)[dau]) : dau; // remove particles w/ extremely low puppi weights if ((puppiWgt(cand)) < 0.01) continue; daughters.push_back(cand);