Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport FastSim refiner network for NANO (DeepJet for AK4 CHS Jets) #40828

Merged
merged 7 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PhysicsTools/NanoAOD/plugins/BJetEnergyRegressionMVA.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "RecoVertex/VertexPrimitives/interface/ConvertToFromReco.h"
#include "RecoVertex/VertexPrimitives/interface/VertexState.h"

#include "PhysicsTools/PatAlgos/plugins/BaseMVAValueMapProducer.h"
#include "PhysicsTools/PatAlgos/interface/BaseMVAValueMapProducer.h"
#include <vector>

class BJetEnergyRegressionMVA : public BaseMVAValueMapProducer<pat::Jet> {
Expand Down
2 changes: 1 addition & 1 deletion PhysicsTools/NanoAOD/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<use name="PhysicsTools/TensorFlow"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="FWCore/ServiceRegistry"/>
Expand All @@ -14,6 +13,7 @@
<use name="RecoVertex/VertexPrimitives"/>
<use name="DataFormats/L1TGlobal"/>
<use name="IOPool/Provenance"/>
<use name="PhysicsTools/PatAlgos"/>
<use name="DQMServices/Core"/>
<use name="CondFormats/BTauObjects"/>
<use name="CondTools/BTau"/>
Expand Down
60 changes: 60 additions & 0 deletions PhysicsTools/NanoAOD/python/jets_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,66 @@
run2_jme_2017.toModify( cjetNN,outputFormulas = cms.vstring(["at(0)*0.24718524515628815+0.9927206635475159","0.5*(at(2)-at(1))*0.24718524515628815"]))


#
# ML-based FastSim refinement
#
from Configuration.Eras.Modifier_fastSim_cff import fastSim
def nanoAOD_refineFastSim_bTagDeepFlav(process):
rappoccio marked this conversation as resolved.
Show resolved Hide resolved


fastSim.toModify( process.jetTable.variables,
btagDeepFlavBunrefined = process.jetTable.variables.btagDeepFlavB.clone(),
btagDeepFlavCvBunrefined = process.jetTable.variables.btagDeepFlavCvB.clone(),
btagDeepFlavCvLunrefined = process.jetTable.variables.btagDeepFlavCvL.clone(),
btagDeepFlavQGunrefined = process.jetTable.variables.btagDeepFlavQG.clone(),
)

fastSim.toModify( process.jetTable.variables,
btagDeepFlavB = None,
btagDeepFlavCvB = None,
btagDeepFlavCvL = None,
btagDeepFlavQG = None,
)

fastSim.toModify( process.jetTable.externalVariables,
btagDeepFlavB = ExtVar(cms.InputTag("btagDeepFlavRefineNN:btagDeepFlavBrefined"), float, doc="DeepJet b+bb+lepb tag discriminator", precision=10),
btagDeepFlavCvB = ExtVar(cms.InputTag("btagDeepFlavRefineNN:btagDeepFlavCvBrefined"), float, doc="DeepJet c vs b+bb+lepb discriminator", precision=10),
btagDeepFlavCvL = ExtVar(cms.InputTag("btagDeepFlavRefineNN:btagDeepFlavCvLrefined"), float, doc="DeepJet c vs uds+g discriminator", precision=10),
btagDeepFlavQG = ExtVar(cms.InputTag("btagDeepFlavRefineNN:btagDeepFlavQGrefined"), float, doc="DeepJet g vs uds discriminator", precision=10),
)

process.btagDeepFlavRefineNN= cms.EDProducer("JetBaseMVAValueMapProducer",
backend = cms.string("ONNX"),
batch_eval = cms.bool(True),
disableONNXGraphOpt = cms.bool(True),

src = cms.InputTag("linkedObjects","jets"),

weightFile=cms.FileInPath("PhysicsTools/NanoAOD/data/btagDeepFlavRefineNN_CHS.onnx"),#local hack for backporting: cp /cvmfs/cms.cern.ch/slc7_amd64_gcc11/cms/data-PhysicsTools-NanoAOD/V01-03-00/PhysicsTools/NanoAOD/data/btagDeepFlavRefineNN_CHS.onnx PhysicsTools/NanoAOD/data/btagDeepFlavRefineNN_CHS.onnx # cp /nfs/dust/cms/user/wolfmor/Refinement/TrainingOutput/regression_model_20221127_opset11.onnx PhysicsTools/NanoAOD/data/btagDeepFlavRefineNN_CHS.onnx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove that comment?

name = cms.string("btagDeepFlavRefineNN"),

isClassifier = cms.bool(False),
variablesOrder = cms.vstring(["GenJet_pt","GenJet_eta","Jet_hadronFlavour",
"Jet_btagDeepFlavB","Jet_btagDeepFlavCvB","Jet_btagDeepFlavCvL","Jet_btagDeepFlavQG"]),
variables = cms.PSet(
GenJet_pt = cms.string("?genJetFwdRef().backRef().isNonnull()?genJetFwdRef().backRef().pt():pt"),
GenJet_eta = cms.string("?genJetFwdRef().backRef().isNonnull()?genJetFwdRef().backRef().eta():eta"),
Jet_hadronFlavour = cms.string("hadronFlavour()"),
Jet_btagDeepFlavB = cms.string("bDiscriminator('pfDeepFlavourJetTags:probb')+bDiscriminator('pfDeepFlavourJetTags:probbb')+bDiscriminator('pfDeepFlavourJetTags:problepb')"),
Jet_btagDeepFlavCvB = cms.string("?(bDiscriminator('pfDeepFlavourJetTags:probc')+bDiscriminator('pfDeepFlavourJetTags:probb')+bDiscriminator('pfDeepFlavourJetTags:probbb')+bDiscriminator('pfDeepFlavourJetTags:problepb'))>0?bDiscriminator('pfDeepFlavourJetTags:probc')/(bDiscriminator('pfDeepFlavourJetTags:probc')+bDiscriminator('pfDeepFlavourJetTags:probb')+bDiscriminator('pfDeepFlavourJetTags:probbb')+bDiscriminator('pfDeepFlavourJetTags:problepb')):-1"),
Jet_btagDeepFlavCvL = cms.string("?(bDiscriminator('pfDeepFlavourJetTags:probc')+bDiscriminator('pfDeepFlavourJetTags:probuds')+bDiscriminator('pfDeepFlavourJetTags:probg'))>0?bDiscriminator('pfDeepFlavourJetTags:probc')/(bDiscriminator('pfDeepFlavourJetTags:probc')+bDiscriminator('pfDeepFlavourJetTags:probuds')+bDiscriminator('pfDeepFlavourJetTags:probg')):-1"),
Jet_btagDeepFlavQG = cms.string("?(bDiscriminator('pfDeepFlavourJetTags:probg')+bDiscriminator('pfDeepFlavourJetTags:probuds'))>0?bDiscriminator('pfDeepFlavourJetTags:probg')/(bDiscriminator('pfDeepFlavourJetTags:probg')+bDiscriminator('pfDeepFlavourJetTags:probuds')):-1"),
),
inputTensorName = cms.string("input"),
outputTensorName = cms.string("output"),
outputNames = cms.vstring(["btagDeepFlavBrefined","btagDeepFlavCvBrefined","btagDeepFlavCvLrefined","btagDeepFlavQGrefined"]),
outputFormulas = cms.vstring(["at(0)","at(1)","at(2)","at(3)"]),
)

fastSim.toModify(process.jetTables, process.jetTables.insert(0,process.btagDeepFlavRefineNN))

return process


subJetTable = cms.EDProducer("SimpleCandidateFlatTableProducer",
src = cms.InputTag("slimmedJetsAK8PFPuppiSoftDropPacked","SubJets"),
Expand Down
7 changes: 7 additions & 0 deletions PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ namespace cms::Ort {

typedef std::vector<std::vector<float>> FloatArrays;

enum class Backend {
cpu,
cuda,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cuda should be removed to avoid confusion (there is no CUDA support for ONNX in 10_6_X).

Alternatively the whole Backend enum is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed cuda

};

class ONNXRuntime {
public:
ONNXRuntime(const std::string& model_path, const ::Ort::SessionOptions* session_options = nullptr);
ONNXRuntime(const ONNXRuntime&) = delete;
ONNXRuntime& operator=(const ONNXRuntime&) = delete;
~ONNXRuntime();

static ::Ort::SessionOptions defaultSessionOptions(Backend backend = Backend::cpu);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised the tests succeeded. I don't see the implementation of defaultSessionOptions() function anywhere, and thus I don't understand how the call from BaseMVACache constructor passes linker. Maybe because nothing seems to use BaseMVACache constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch... the BaseMVACache doesn't even exist in the original BaseMVAValueMapProducer.h.

I had not realized how much these had diverged between 10_6 and 13_0... @sbein is there any reason we couldn't add the customization to relval workflows, as in #40553, and retrigger the tests here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the Run 2 NANO recipes, including the one for testing the refinement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that, as suggested, the Backend enum and defaultSessionOptions could be omitted in the backport since they're not used anywhere


// Run inference and get outputs
// input_names: list of the names of the input nodes.
// input_values: list of input arrays for each input node. The order of `input_values` must match `input_names`.
Expand Down
3 changes: 3 additions & 0 deletions PhysicsTools/PatAlgos/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<use name="PhysicsTools/PatUtils"/>
<use name="PhysicsTools/Utilities"/>
<use name="PhysicsTools/IsolationAlgos"/>
<use name="PhysicsTools/PatUtils"/>
<use name="PhysicsTools/TensorFlow"/>
<use name="PhysicsTools/ONNXRuntime"/>
<use name="Geometry/CaloTopology"/>
<use name="RecoEgamma/EgammaTools"/>
<use name="RecoTracker/DeDx"/>
Expand Down
Loading