From f0d34bf760a35c90f7b2ac4241f31227b4dc799a Mon Sep 17 00:00:00 2001 From: Heshy Roskes Date: Tue, 16 Oct 2018 15:10:01 +0200 Subject: [PATCH 01/20] fix conddb function in 10_3 --- .../TkAlAllInOneTool/helperFunctions.py | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/helperFunctions.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/helperFunctions.py index 3dde0c5d7ab3f..6f03b645693ae 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/helperFunctions.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/helperFunctions.py @@ -195,6 +195,7 @@ def cppboolstring(string, name): """ return pythonboolstring(string, name).lower() +conddbcode = None def conddb(*args): """ Wrapper for conddb, so that you can run @@ -204,29 +205,36 @@ def conddb(*args): getcommandoutput2(conddb ...) doesn't work, it imports the wrong sqlalchemy in CondCore/Utilities/python/conddblib.py """ - from tempfile import NamedTemporaryFile + global conddbcode + from tempfile import mkdtemp, NamedTemporaryFile - with open(getCommandOutput2("which conddb").strip()) as f: - conddb = f.read() + if conddbcode is None: + conddbfile = getCommandOutput2("which conddb").strip() + tmpdir = mkdtemp() + getCommandOutput2("2to3 -f print -o " + tmpdir + " -n -w " + conddbfile) + + with open(os.path.join(tmpdir, "conddb")) as f: + conddb = f.read() + + conddbcode = conddb.replace("sys.exit", "sysexit") def sysexit(number): if number != 0: raise AllInOneError("conddb exited with status {}".format(number)) namespace = {"sysexit": sysexit, "conddboutput": ""} - conddb = conddb.replace("sys.exit", "sysexit") - bkpargv = sys.argv sys.argv[1:] = args bkpstdout = sys.stdout - with NamedTemporaryFile(bufsize=0) as sys.stdout: - exec(conddb, namespace) - namespace["main"]() - with open(sys.stdout.name) as f: - result = f.read() - - sys.argv[:] = bkpargv - sys.stdout = bkpstdout + try: + with NamedTemporaryFile(bufsize=0) as sys.stdout: + exec(conddbcode, namespace) + namespace["main"]() + with open(sys.stdout.name) as f: + result = f.read() + finally: + sys.argv[:] = bkpargv + sys.stdout = bkpstdout return result From 7e01e404df49d41c287f387e161e20d92ebddf88 Mon Sep 17 00:00:00 2001 From: muhammadansariqbal Date: Fri, 19 Oct 2018 20:07:44 +0200 Subject: [PATCH 02/20] Migration from user based output EOS directories to common output EOS directory for Tracker Alignment Validation. --- .../TkAlAllInOneTool/configTemplates.py | 24 +++++++++---------- .../TkAlAllInOneTool/genericValidation.py | 2 +- .../TkAlAllInOneTool/geometryComparison.py | 2 +- .../offlineValidationTemplates.py | 2 +- .../primaryVertexValidationTemplates.py | 12 +++++----- .../TkAlAllInOneTool/zMuMuValidation.py | 2 +- .../zMuMuValidationTemplates.py | 6 ++--- .../scripts/validateAlignments.py | 4 ++-- 8 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/configTemplates.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/configTemplates.py index c5b3e2e7fba8a..d51d2dcc36cdb 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/configTemplates.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/configTemplates.py @@ -59,10 +59,10 @@ #rfmkdir -p .oO[datadir]Oo. &>! /dev/null #remove possible result file from previous runs -previous_results=$(ls /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo.) +previous_results=$(ls /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo.) for file in ${previous_results} do - if [ ${file} = /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./.oO[outputFile]Oo. ] + if [ ${file} = /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./.oO[outputFile]Oo. ] then xrdcp -f root://eoscms//eos/cms${file} root://eoscms//eos/cms${file}.bak fi @@ -98,7 +98,7 @@ find . -maxdepth 1 -name "LOGFILE*.oO[alignmentName]Oo.*" -print | xargs -I {} bash -c "rfcp {} .oO[logdir]Oo." #copy root files to eos -mkdir -p /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo. +mkdir -p /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo. if [ .oO[parallelJobs]Oo. -eq 1 ] then root_files=$(ls --color=never -d *.oO[alignmentName]Oo.*.root) @@ -109,7 +109,7 @@ for file in ${root_files} do - xrdcp -f ${file} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo. + xrdcp -f ${file} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo. echo ${file} done @@ -237,11 +237,11 @@ ############################################################################### # download root files from eos -root_files=$(ls /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo. \ +root_files=$(ls /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo. \ | grep ".root$" | grep -v "result.root$") #for file in ${root_files} #do -# xrdcp -f root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./${file} . +# xrdcp -f root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./${file} . # echo ${file} #done @@ -284,11 +284,11 @@ ############################################################################### # download root files from eos -root_files=$(ls /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo. \ +root_files=$(ls /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo. \ | grep ".root$" | grep -v "result.root$") #for file in ${root_files} #do -# xrdcp -f root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./${file} . +# xrdcp -f root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./${file} . # echo ${file} #done @@ -326,9 +326,9 @@ compareAlignmentsExecution=""" #merge for .oO[validationId]Oo. if it does not exist or is not up-to-date echo -e "\n\nComparing validations" -mkdir -p /eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./ +mkdir -p /eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./ cp .oO[Alignment/OfflineValidation]Oo./scripts/compareFileAges.C . -root -x -q -b -l "compareFileAges.C(\\\"root://eoscms.cern.ch//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./.oO[validationId]Oo._result.root\\\", \\\".oO[compareStringsPlain]Oo.\\\")" +root -x -q -b -l "compareFileAges.C(\\\"root://eoscms.cern.ch//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./.oO[validationId]Oo._result.root\\\", \\\".oO[compareStringsPlain]Oo.\\\")" comparisonNeeded=${?} if [[ ${comparisonNeeded} -eq 1 ]] @@ -336,10 +336,10 @@ cp .oO[compareAlignmentsPath]Oo. . root -x -q -b -l '.oO[compareAlignmentsName]Oo.++(\".oO[compareStrings]Oo.\", ".oO[legendheader]Oo.", ".oO[customtitle]Oo.", ".oO[customrighttitle]Oo.", .oO[bigtext]Oo.)' mv result.root .oO[validationId]Oo._result.root - xrdcp -f .oO[validationId]Oo._result.root root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo. + xrdcp -f .oO[validationId]Oo._result.root root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo. else echo ".oO[validationId]Oo._result.root is up-to-date, no need to compare again." - xrdcp -f root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./.oO[validationId]Oo._result.root . + xrdcp -f root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./.oO[validationId]Oo._result.root . fi """ diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/genericValidation.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/genericValidation.py index d08c6eafe6843..9bbc1b864d457 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/genericValidation.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/genericValidation.py @@ -398,7 +398,7 @@ def getRepMap(self, alignment = None): outputfile = os.path.expandvars(replaceByMap( "%s_%s_.oO[name]Oo..root" % (self.outputBaseName, self.name) , result)) - resultfile = os.path.expandvars(replaceByMap(("/store/caf/user/$USER/.oO[eosdir]Oo./" + + resultfile = os.path.expandvars(replaceByMap(("/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./" + "%s_%s_.oO[name]Oo..root" % (self.resultBaseName, self.name)) , result)) result.update({ diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/geometryComparison.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/geometryComparison.py index 3c9a991551f44..1edb4660eb2a4 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/geometryComparison.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/geometryComparison.py @@ -257,7 +257,7 @@ def createScript(self, path): "-c \"rfcp {} .oO[datadir]Oo./.oO[name]Oo." ".Comparison_common"+name+"_Images/.oO[common]Oo._.oO[name]Oo..Visualization.gif\"\n") - resultingFile = replaceByMap(("/store/caf/user/$USER/.oO[eosdir]Oo./compared%s_" + resultingFile = replaceByMap(("/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./compared%s_" ".oO[name]Oo..root"%name), repMap) resultingFile = os.path.expandvars( resultingFile ) resultingFile = os.path.abspath( resultingFile ) diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/offlineValidationTemplates.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/offlineValidationTemplates.py index 2b2f4194bdf03..1ecd1d1cdec92 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/offlineValidationTemplates.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/offlineValidationTemplates.py @@ -108,7 +108,7 @@ p.setTreeBaseDir(".oO[OfflineTreeBaseDir]Oo."); p.plotDMR(".oO[DMRMethod]Oo.",.oO[DMRMinimum]Oo.,".oO[DMROptions]Oo."); p.plotSurfaceShapes(".oO[SurfaceShapes]Oo."); - p.plotChi2("root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./.oO[validationId]Oo._result.root"); + p.plotChi2("root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./.oO[validationId]Oo._result.root"); vector moduleids = {.oO[moduleid]Oo.}; for (auto moduleid : moduleids) { p.residual_by_moduleID(moduleid); diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/primaryVertexValidationTemplates.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/primaryVertexValidationTemplates.py index 7f1fc8aa780ae..5a683e28e97e0 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/primaryVertexValidationTemplates.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/primaryVertexValidationTemplates.py @@ -202,10 +202,10 @@ ls -lh . -eos mkdir -p /store/caf/user/$USER/.oO[eosdir]Oo./plots/ +eos mkdir -p /store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/ for RootOutputFile in $(ls *root ) do - xrdcp -f ${RootOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./${RootOutputFile} + xrdcp -f ${RootOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./${RootOutputFile} rfcp ${RootOutputFile} .oO[workingdir]Oo. done @@ -219,12 +219,12 @@ mkdir -p .oO[plotsdir]Oo. for PngOutputFile in $(ls *png ); do - xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./plots/${PngOutputFile} + xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/${PngOutputFile} rfcp ${PngOutputFile} .oO[plotsdir]Oo. done for PdfOutputFile in $(ls *pdf ); do - xrdcp -f ${PdfOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./plots/${PdfOutputFile} + xrdcp -f ${PdfOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/${PdfOutputFile} rfcp ${PdfOutputFile} .oO[plotsdir]Oo. done @@ -292,12 +292,12 @@ root -x -b -q .oO[plottingscriptname]Oo.++ for PdfOutputFile in $(ls *pdf ); do - xrdcp -f ${PdfOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./plots/${PdfOutputFile} + xrdcp -f ${PdfOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/${PdfOutputFile} rfcp ${PdfOutputFile} .oO[datadir]Oo./.oO[PlotsDirName]Oo. done for PngOutputFile in $(ls *png ); do - xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./plots/${PngOutputFile} + xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/${PngOutputFile} rfcp ${PngOutputFile} .oO[datadir]Oo./.oO[PlotsDirName]Oo. done diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidation.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidation.py index 2be50fdbd136c..4549cf898493d 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidation.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidation.py @@ -108,7 +108,7 @@ def appendToPlots(self): returned, else the validation is appended to the list """ repMap = self.getRepMap() - return replaceByMap(' filenames.push_back("root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./BiasCheck.root"); titles.push_back(".oO[title]Oo."); colors.push_back(.oO[color]Oo.); linestyles.push_back(.oO[style]Oo.);\n', repMap) + return replaceByMap(' filenames.push_back("root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./BiasCheck.root"); titles.push_back(".oO[title]Oo."); colors.push_back(.oO[color]Oo.); linestyles.push_back(.oO[style]Oo.);\n', repMap) @classmethod def plottingscriptname(cls): diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidationTemplates.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidationTemplates.py index 28a380a2f87d3..d0920130ead85 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidationTemplates.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/zMuMuValidationTemplates.py @@ -249,16 +249,16 @@ if [[ .oO[zmumureference]Oo. == *store* ]]; then xrdcp -f .oO[zmumureference]Oo. BiasCheck_Reference.root; else ln -fs .oO[zmumureference]Oo. ./BiasCheck_Reference.root; fi root -q -b -l MultiHistoOverlap_.oO[resonance]Oo..C -eos mkdir -p /store/caf/user/$USER/.oO[eosdir]Oo./plots/ +eos mkdir -p /store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/ for RootOutputFile in $(ls *root ) do - xrdcp -f ${RootOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./ + xrdcp -f ${RootOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./ rfcp ${RootOutputFile} .oO[workingdir]Oo. done mkdir -p .oO[plotsdir]Oo. for PngOutputFile in $(ls *png ); do - xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/caf/user/$USER/.oO[eosdir]Oo./plots/ + xrdcp -f ${PngOutputFile} root://eoscms//eos/cms/store/group/alca_trackeralign/AlignmentValidation/.oO[eosdir]Oo./plots/ rfcp ${PngOutputFile} .oO[plotsdir]Oo. done diff --git a/Alignment/OfflineValidation/scripts/validateAlignments.py b/Alignment/OfflineValidation/scripts/validateAlignments.py index 888b3be7937bb..ed4d09d9c30ee 100755 --- a/Alignment/OfflineValidation/scripts/validateAlignments.py +++ b/Alignment/OfflineValidation/scripts/validateAlignments.py @@ -358,7 +358,7 @@ def createMergeScript( path, validations, options ): repMapTemp["doMerge"] += '\n\n\n\necho -e "\n\nMerging results from %s jobs with alignment %s"\n\n' % (validationType.valType,validation.alignmentToValidate.name) repMapTemp["doMerge"] += validation.doMerge() for f in validation.getRepMap()["outputFiles"]: - longName = os.path.join("/eos/cms/store/caf/user/$USER/", + longName = os.path.join("/eos/cms/store/group/alca_trackeralign/AlignmentValidation/", validation.getRepMap()["eosdir"], f) repMapTemp["rmUnmerged"] += " rm "+longName+"\n" @@ -395,7 +395,7 @@ def createMergeScript( path, validations, options ): repMap["beforeMerge"] += validationType.doInitMerge() repMap["doMerge"] += validation.doMerge() for f in validation.getRepMap()["outputFiles"]: - longName = os.path.join("/eos/cms/store/caf/user/$USER/", + longName = os.path.join("/eos/cms/store/group/alca_trackeralign/AlignmentValidation/", validation.getRepMap()["eosdir"], f) repMap["rmUnmerged"] += " rm "+longName+"\n" From 3de7fc0aef0625678780ae850b1b8afbb2bcc5b9 Mon Sep 17 00:00:00 2001 From: muhammadansariqbal Date: Fri, 19 Oct 2018 21:57:41 +0200 Subject: [PATCH 03/20] Tracker alignment validation, made eosdir a mandatory input to avoid outputs being overwritten owing to default names. --- .../python/TkAlAllInOneTool/betterConfigParser.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Alignment/OfflineValidation/python/TkAlAllInOneTool/betterConfigParser.py b/Alignment/OfflineValidation/python/TkAlAllInOneTool/betterConfigParser.py index af5a796d44dfe..037e5b1e2be02 100644 --- a/Alignment/OfflineValidation/python/TkAlAllInOneTool/betterConfigParser.py +++ b/Alignment/OfflineValidation/python/TkAlAllInOneTool/betterConfigParser.py @@ -157,10 +157,12 @@ def getGeneral( self ): "jobmode":"interactive", "datadir":os.getcwd(), "logdir":os.getcwd(), - "eosdir": "", } - self.checkInput("general", knownSimpleOptions = defaults.keys()) - general = self.getResultingSection( "general", defaultDict = defaults ) + mandatories = [ + "eosdir", + ] + self.checkInput("general", knownSimpleOptions = defaults.keys() + mandatories) + general = self.getResultingSection( "general", defaultDict = defaults, demandPars = mandatories ) internal_section = "internals" if not self.has_section(internal_section): self.add_section(internal_section) From 52f592d772684a5bf94fddfa2c9ac68c35cb21e1 Mon Sep 17 00:00:00 2001 From: Konstantin Androsov Date: Fri, 15 Jun 2018 16:33:49 +0200 Subject: [PATCH 04/20] First implementation of deep tau id. --- RecoTauTag/RecoTau/plugins/BuildFile.xml | 1 + RecoTauTag/RecoTau/plugins/DeepTauId.cc | 663 +++++++++++++++++++++ RecoTauTag/RecoTau/python/DeepTauId_cff.py | 11 + RecoTauTag/RecoTau/test/runTauIdMVA.py | 598 +++++++++++++++++++ 4 files changed, 1273 insertions(+) create mode 100644 RecoTauTag/RecoTau/plugins/DeepTauId.cc create mode 100644 RecoTauTag/RecoTau/python/DeepTauId_cff.py create mode 100644 RecoTauTag/RecoTau/test/runTauIdMVA.py diff --git a/RecoTauTag/RecoTau/plugins/BuildFile.xml b/RecoTauTag/RecoTau/plugins/BuildFile.xml index 3bb9162242c43..5362e6be3f4f6 100644 --- a/RecoTauTag/RecoTau/plugins/BuildFile.xml +++ b/RecoTauTag/RecoTau/plugins/BuildFile.xml @@ -36,5 +36,6 @@ + diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc new file mode 100644 index 0000000000000..40782be7cfafd --- /dev/null +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -0,0 +1,663 @@ +/* + * \class DeepTauId + * + * Tau identification using Deep NN + * + * \author Konstantin Androsov, INFN Pisa + */ + +#include +#include "FWCore/Framework/interface/stream/EDProducer.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" +#include "DataFormats/PatCandidates/interface/Electron.h" +#include "DataFormats/PatCandidates/interface/Muon.h" +#include "DataFormats/PatCandidates/interface/Tau.h" +#include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" +#include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" + +namespace { + +struct dnn_inputs_2017v1 { + enum vars { + pt = 0, eta, mass, decayMode, chargedIsoPtSum, neutralIsoPtSum, neutralIsoPtSumWeight, + photonPtSumOutsideSignalCone, puCorrPtSum, + dxy, dxy_sig, dz, ip3d, ip3d_sig, + hasSecondaryVertex, flightLength_r, flightLength_dEta, flightLength_dPhi, + flightLength_sig, leadChargedHadrCand_pt, leadChargedHadrCand_dEta, + leadChargedHadrCand_dPhi, leadChargedHadrCand_mass, pt_weighted_deta_strip, + pt_weighted_dphi_strip, pt_weighted_dr_signal, pt_weighted_dr_iso, + leadingTrackNormChi2, e_ratio, gj_angle_diff, n_photons, emFraction, + has_gsf_track, inside_ecal_crack, + gsf_ele_matched, gsf_ele_pt, gsf_ele_dEta, gsf_ele_dPhi, gsf_ele_mass, gsf_ele_Ee, + gsf_ele_Egamma, gsf_ele_Pin, gsf_ele_Pout, gsf_ele_EtotOverPin, gsf_ele_Eecal, + gsf_ele_dEta_SeedClusterTrackAtCalo, gsf_ele_dPhi_SeedClusterTrackAtCalo, gsf_ele_mvaIn_sigmaEtaEta, + gsf_ele_mvaIn_hadEnergy, + gsf_ele_mvaIn_deltaEta, gsf_ele_Chi2NormGSF, gsf_ele_GSFNumHits, gsf_ele_GSFTrackResol, + gsf_ele_GSFTracklnPt, gsf_ele_Chi2NormKF, gsf_ele_KFNumHits, + leadChargedCand_etaAtEcalEntrance, leadChargedCand_pt, leadChargedHadrCand_HoP, + leadChargedHadrCand_EoP, tau_visMass_innerSigCone, + n_matched_muons, muon_pt, muon_dEta, muon_dPhi, + muon_n_matches_DT_1, muon_n_matches_DT_2, muon_n_matches_DT_3, muon_n_matches_DT_4, + muon_n_matches_CSC_1, muon_n_matches_CSC_2, muon_n_matches_CSC_3, muon_n_matches_CSC_4, + muon_n_hits_DT_2, muon_n_hits_DT_3, muon_n_hits_DT_4, + muon_n_hits_CSC_2, muon_n_hits_CSC_3, muon_n_hits_CSC_4, + muon_n_hits_RPC_2, muon_n_hits_RPC_3, muon_n_hits_RPC_4, + muon_n_stations_with_matches_03, muon_n_stations_with_hits_23, + signalChargedHadrCands_sum_innerSigCone_pt, signalChargedHadrCands_sum_innerSigCone_dEta, + signalChargedHadrCands_sum_innerSigCone_dPhi, signalChargedHadrCands_sum_innerSigCone_mass, + signalChargedHadrCands_sum_outerSigCone_pt, signalChargedHadrCands_sum_outerSigCone_dEta, + signalChargedHadrCands_sum_outerSigCone_dPhi, signalChargedHadrCands_sum_outerSigCone_mass, + signalChargedHadrCands_nTotal_innerSigCone, signalChargedHadrCands_nTotal_outerSigCone, + signalNeutrHadrCands_sum_innerSigCone_pt, signalNeutrHadrCands_sum_innerSigCone_dEta, + signalNeutrHadrCands_sum_innerSigCone_dPhi, signalNeutrHadrCands_sum_innerSigCone_mass, + signalNeutrHadrCands_sum_outerSigCone_pt, signalNeutrHadrCands_sum_outerSigCone_dEta, + signalNeutrHadrCands_sum_outerSigCone_dPhi, signalNeutrHadrCands_sum_outerSigCone_mass, + signalNeutrHadrCands_nTotal_innerSigCone, signalNeutrHadrCands_nTotal_outerSigCone, + signalGammaCands_sum_innerSigCone_pt, signalGammaCands_sum_innerSigCone_dEta, + signalGammaCands_sum_innerSigCone_dPhi, signalGammaCands_sum_innerSigCone_mass, + signalGammaCands_sum_outerSigCone_pt, signalGammaCands_sum_outerSigCone_dEta, + signalGammaCands_sum_outerSigCone_dPhi, signalGammaCands_sum_outerSigCone_mass, + signalGammaCands_nTotal_innerSigCone, signalGammaCands_nTotal_outerSigCone, + isolationChargedHadrCands_sum_pt, isolationChargedHadrCands_sum_dEta, + isolationChargedHadrCands_sum_dPhi, isolationChargedHadrCands_sum_mass, + isolationChargedHadrCands_nTotal, + isolationNeutrHadrCands_sum_pt, isolationNeutrHadrCands_sum_dEta, + isolationNeutrHadrCands_sum_dPhi, isolationNeutrHadrCands_sum_mass, + isolationNeutrHadrCands_nTotal, + isolationGammaCands_sum_pt, isolationGammaCands_sum_dEta, + isolationGammaCands_sum_dPhi, isolationGammaCands_sum_mass, + isolationGammaCands_nTotal, + NumberOfInputs + }; +}; + +template +float dEta(const LVector1& p4, const LVector2& tau_p4) +{ + return static_cast(p4.eta() - tau_p4.eta()); +} + +template +float dPhi(const LVector1& p4, const LVector2& tau_p4) +{ + return static_cast(ROOT::Math::VectorUtil::DeltaPhi(p4, tau_p4)); +} + +namespace MuonSubdetId { +enum { DT = 1, CSC = 2, RPC = 3, GEM = 4, ME0 = 5 }; +} + +struct MuonHitMatch { + static constexpr int n_muon_stations = 4; + + std::map> n_matches, n_hits; + unsigned n_muons{0}; + const pat::Muon* best_matched_muon{nullptr}; + double deltaR2_best_match{-1}; + + MuonHitMatch() + { + n_matches[MuonSubdetId::DT].assign(n_muon_stations, 0); + n_matches[MuonSubdetId::CSC].assign(n_muon_stations, 0); + n_matches[MuonSubdetId::RPC].assign(n_muon_stations, 0); + n_hits[MuonSubdetId::DT].assign(n_muon_stations, 0); + n_hits[MuonSubdetId::CSC].assign(n_muon_stations, 0); + n_hits[MuonSubdetId::RPC].assign(n_muon_stations, 0); + } + + void AddMatchedMuon(const pat::Muon& muon, const pat::Tau& tau) + { + static constexpr int n_stations = 4; + + ++n_muons; + const double dR2 = ROOT::Math::VectorUtil::DeltaR2(tau.p4(), muon.p4()); + if(!best_matched_muon || dR2 < deltaR2_best_match) { + best_matched_muon = &muon; + deltaR2_best_match = dR2; + } + + for(const auto& segment : muon.matches()) { + if(segment.segmentMatches.empty()) continue; + if(n_matches.count(segment.detector())) + ++n_matches.at(segment.detector()).at(segment.station() - 1); + } + + if(muon.outerTrack().isNonnull()) { + const auto& hit_pattern = muon.outerTrack()->hitPattern(); + for(int hit_index = 0; hit_index < hit_pattern.numberOfAllHits(reco::HitPattern::TRACK_HITS); + ++hit_index) { + auto hit_id = hit_pattern.getHitPattern(reco::HitPattern::TRACK_HITS, hit_index); + if(hit_id == 0) break; + if(hit_pattern.muonHitFilter(hit_id) && (hit_pattern.getHitType(hit_id) == TrackingRecHit::valid + || hit_pattern.getHitType(hit_id == TrackingRecHit::bad))) { + const int station = hit_pattern.getMuonStation(hit_id) - 1; + if(station > 0 && station < n_stations) { + std::vector* muon_n_hits = nullptr; + if(hit_pattern.muonDTHitFilter(hit_id)) + muon_n_hits = &n_hits.at(MuonSubdetId::DT); + else if(hit_pattern.muonCSCHitFilter(hit_id)) + muon_n_hits = &n_hits.at(MuonSubdetId::CSC); + else if(hit_pattern.muonRPCHitFilter(hit_id)) + muon_n_hits = &n_hits.at(MuonSubdetId::RPC); + + if(muon_n_hits) + ++muon_n_hits->at(station); + } + } + } + } + } + + static std::vector FindMatchedMuons(const pat::Tau& tau, const pat::MuonCollection& muons, + double deltaR, double minPt) + { + const reco::Muon* hadr_cand_muon = nullptr; + if(tau.leadPFChargedHadrCand().isNonnull() && tau.leadPFChargedHadrCand()->muonRef().isNonnull()) + hadr_cand_muon = tau.leadPFChargedHadrCand()->muonRef().get(); + std::vector matched_muons; + const double deltaR2 = std::pow(deltaR, 2); + for(const pat::Muon& muon : muons) { + const reco::Muon* reco_muon = &muon; + if(muon.pt() <= minPt) continue; + if(reco_muon == hadr_cand_muon) continue; + if(ROOT::Math::VectorUtil::DeltaR2(tau.p4(), muon.p4()) >= deltaR2) continue; + matched_muons.push_back(&muon); + } + return matched_muons; + } + + + template + void FillTensor(const TensorElemGet& get, const pat::Tau& tau, float default_value) const + { + get(dnn::n_matched_muons) = n_muons; + get(dnn::muon_pt) = best_matched_muon != nullptr ? best_matched_muon->p4().pt() : default_value; + get(dnn::muon_dEta) = best_matched_muon != nullptr + ? dEta(best_matched_muon->p4(), tau.p4()) : default_value; + get(dnn::muon_dPhi) = best_matched_muon != nullptr + ? dPhi(best_matched_muon->p4(), tau.p4()) : default_value; + get(dnn::muon_n_matches_DT_1) = n_matches.at(MuonSubdetId::DT).at(0); + get(dnn::muon_n_matches_DT_2) = n_matches.at(MuonSubdetId::DT).at(1); + get(dnn::muon_n_matches_DT_3) = n_matches.at(MuonSubdetId::DT).at(2); + get(dnn::muon_n_matches_DT_4) = n_matches.at(MuonSubdetId::DT).at(3); + get(dnn::muon_n_matches_CSC_1) = n_matches.at(MuonSubdetId::CSC).at(0); + get(dnn::muon_n_matches_CSC_2) = n_matches.at(MuonSubdetId::CSC).at(1); + get(dnn::muon_n_matches_CSC_3) = n_matches.at(MuonSubdetId::CSC).at(2); + get(dnn::muon_n_matches_CSC_4) = n_matches.at(MuonSubdetId::CSC).at(3); + get(dnn::muon_n_hits_DT_2) = n_hits.at(MuonSubdetId::DT).at(1); + get(dnn::muon_n_hits_DT_3) = n_hits.at(MuonSubdetId::DT).at(2); + get(dnn::muon_n_hits_DT_4) = n_hits.at(MuonSubdetId::DT).at(3); + get(dnn::muon_n_hits_CSC_2) = n_hits.at(MuonSubdetId::CSC).at(1); + get(dnn::muon_n_hits_CSC_3) = n_hits.at(MuonSubdetId::CSC).at(2); + get(dnn::muon_n_hits_CSC_4) = n_hits.at(MuonSubdetId::CSC).at(3); + get(dnn::muon_n_hits_RPC_2) = n_hits.at(MuonSubdetId::RPC).at(1); + get(dnn::muon_n_hits_RPC_3) = n_hits.at(MuonSubdetId::RPC).at(2); + get(dnn::muon_n_hits_RPC_4) = n_hits.at(MuonSubdetId::RPC).at(3); + get(dnn::muon_n_stations_with_matches_03) = CountMuonStationsWithMatches(0, 3); + get(dnn::muon_n_stations_with_hits_23) = CountMuonStationsWithHits(2, 3); + } + +private: + unsigned CountMuonStationsWithMatches(size_t first_station, size_t last_station) const + { + static const std::map> masks = { + { MuonSubdetId::DT, { false, false, false, false } }, + { MuonSubdetId::CSC, { true, false, false, false } }, + { MuonSubdetId::RPC, { false, false, false, false } }, + }; + unsigned cnt = 0; + for(unsigned n = first_station; n <= last_station; ++n) { + for(const auto& match : n_matches) { + if(!masks.at(match.first).at(n) && match.second.at(n) > 0) ++cnt; + } + } + return cnt; + } + + unsigned CountMuonStationsWithHits(size_t first_station, size_t last_station) const + { + static const std::map> masks = { + { MuonSubdetId::DT, { false, false, false, false } }, + { MuonSubdetId::CSC, { false, false, false, false } }, + { MuonSubdetId::RPC, { false, false, false, false } }, + }; + + unsigned cnt = 0; + for(unsigned n = first_station; n <= last_station; ++n) { + for(const auto& hit : n_hits) { + if(!masks.at(hit.first).at(n) && hit.second.at(n) > 0) ++cnt; + } + } + return cnt; + } +}; + +} // anonymous namespace + + +class DeepTauId : public edm::stream::EDProducer<> { +public: + using TauType = pat::Tau; + using TauDiscriminator = pat::PATTauDiscriminator; + using TauCollection = std::vector; + using TauRef = edm::Ref; + using TauRefProd = edm::RefProd; + using ElectronCollection = pat::ElectronCollection; + using MuonCollection = pat::MuonCollection; + using LorentzVectorXYZ = ROOT::Math::LorentzVector>; + using GraphPtr = std::shared_ptr; + + static constexpr float default_value = -999.; + + struct Output { + std::vector num, den; + + Output(const std::vector& _num, const std::vector& _den) : num(_num), den(_den) {} + + std::unique_ptr get_value(const edm::Handle& taus, + const tensorflow::Tensor& pred) const + { + auto output = std::make_unique(TauRefProd(taus)); + for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { + float x = 0; + for(size_t num_elem : num) + x += pred.matrix()(tau_index, num_elem); + if(x != 0) { + float den_val = 0; + for(size_t den_elem : den) + den_val += pred.matrix()(tau_index, den_elem); + x = den_val != 0 ? x / den_val : std::numeric_limits::max(); + } + output->setValue(tau_index, x); + } + return output; + } + }; + + using OutputCollection = std::map; + + static const OutputCollection& GetOutputs() + { + static constexpr size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; + static const OutputCollection outputs = { + { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, + { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, + { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, + { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } + }; + return outputs; + } + +public: + explicit DeepTauId(const edm::ParameterSet& cfg) : + electrons_token(consumes(cfg.getParameter("electrons"))), + muons_token(consumes(cfg.getParameter("muons"))), + taus_token(consumes(cfg.getParameter("taus"))), + graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), + session(tensorflow::createSession(graph.get())), + input_layer(graph->node(0).name()), + output_layer(graph->node(graph->node_size() - 1).name()) + { + for(const auto& output_desc : GetOutputs()) + produces(output_desc.first); + } + + virtual ~DeepTauId() override + { + tensorflow::closeSession(session); + } + + virtual void produce(edm::Event& event, const edm::EventSetup& es) override + { + edm::Handle electrons; + event.getByToken(electrons_token, electrons); + + edm::Handle muons; + event.getByToken(muons_token, muons); + + edm::Handle taus; + event.getByToken(taus_token, taus); + + const tensorflow::Tensor& inputs = CreateInputs(*taus, *electrons, *muons); + std::vector pred_vector; + tensorflow::run(session, { { input_layer, inputs } }, { output_layer }, &pred_vector); + const tensorflow::Tensor& pred = pred_vector.at(0); + + for(const auto& output_desc : GetOutputs()) + event.put(output_desc.second.get_value(taus, pred), output_desc.first); + } + +private: + template + tensorflow::Tensor CreateInputs(const TauCollection& taus, const ElectronCollection& electrons, + const MuonCollection& muons) const + { + tensorflow::Tensor inputs(tensorflow::DT_FLOAT, { static_cast(taus.size()), dnn_inputs::NumberOfInputs}); + for(size_t tau_index = 0; tau_index < taus.size(); ++tau_index) + SetInputs(taus, tau_index, inputs, electrons, muons); + return inputs; + } + + template + void SetInputs(const TauCollection& taus, size_t tau_index, tensorflow::Tensor& inputs, + const ElectronCollection& electrons, const MuonCollection& muons) const + { + + static constexpr bool check_all_set = false; + static constexpr float magic_number = -42; + const auto& get = [&](int var_index) -> float& { return inputs.matrix()(tau_index, var_index); }; + const TauType& tau = taus.at(tau_index); + auto leadChargedHadrCand = dynamic_cast(tau.leadChargedHadrCand().get()); + + if(check_all_set) { + for(int var_index = 0; var_index < dnn::NumberOfInputs; ++var_index) { + get(var_index) = magic_number; + } + } + + get(dnn::pt) = tau.p4().pt(); + get(dnn::eta) = tau.p4().eta(); + get(dnn::mass) = tau.p4().mass(); + get(dnn::decayMode) = tau.decayMode(); + get(dnn::chargedIsoPtSum) = tau.tauID("chargedIsoPtSum"); + get(dnn::neutralIsoPtSum) = tau.tauID("neutralIsoPtSum"); + get(dnn::neutralIsoPtSumWeight) = tau.tauID("neutralIsoPtSumWeight"); + get(dnn::photonPtSumOutsideSignalCone) = tau.tauID("photonPtSumOutsideSignalCone"); + get(dnn::puCorrPtSum) = tau.tauID("puCorrPtSum"); + get(dnn::dxy) = tau.dxy(); + get(dnn::dxy_sig) = tau.dxy_Sig(); + get(dnn::dz) = leadChargedHadrCand ? leadChargedHadrCand->dz() : default_value; + get(dnn::ip3d) = tau.ip3d(); + get(dnn::ip3d_sig) = tau.ip3d_Sig(); + get(dnn::hasSecondaryVertex) = tau.hasSecondaryVertex(); + get(dnn::flightLength_r) = tau.flightLength().R(); + get(dnn::flightLength_dEta) = dEta(tau.flightLength(), tau.p4()); + get(dnn::flightLength_dPhi) = dPhi(tau.flightLength(), tau.p4()); + get(dnn::flightLength_sig) = tau.flightLengthSig(); + get(dnn::leadChargedHadrCand_pt) = leadChargedHadrCand ? leadChargedHadrCand->p4().Pt() : default_value; + get(dnn::leadChargedHadrCand_dEta) = leadChargedHadrCand + ? dEta(leadChargedHadrCand->p4(), tau.p4()) : default_value; + get(dnn::leadChargedHadrCand_dPhi) = leadChargedHadrCand + ? dPhi(leadChargedHadrCand->p4(), tau.p4()) : default_value; + get(dnn::leadChargedHadrCand_mass) = leadChargedHadrCand + ? leadChargedHadrCand->p4().mass() : default_value; + get(dnn::pt_weighted_deta_strip) = clusterVariables.tau_pt_weighted_deta_strip(tau, tau.decayMode()); + get(dnn::pt_weighted_dphi_strip) = clusterVariables.tau_pt_weighted_dphi_strip(tau, tau.decayMode()); + get(dnn::pt_weighted_dr_signal) = clusterVariables.tau_pt_weighted_dr_signal(tau, tau.decayMode()); + get(dnn::pt_weighted_dr_iso) = clusterVariables.tau_pt_weighted_dr_iso(tau, tau.decayMode()); + get(dnn::leadingTrackNormChi2) = tau.leadingTrackNormChi2(); + get(dnn::e_ratio) = clusterVariables.tau_Eratio(tau); + get(dnn::gj_angle_diff) = CalculateGottfriedJacksonAngleDifference(tau); + get(dnn::n_photons) = clusterVariables.tau_n_photons_total(tau); + get(dnn::emFraction) = tau.emFraction_MVA(); + get(dnn::has_gsf_track) = leadChargedHadrCand && std::abs(leadChargedHadrCand->pdgId()) == 11; + get(dnn::inside_ecal_crack) = IsInEcalCrack(tau.p4().Eta()); + auto gsf_ele = FindMatchedElectron(tau, electrons, 0.3); + get(dnn::gsf_ele_matched) = gsf_ele != nullptr; + get(dnn::gsf_ele_pt) = gsf_ele != nullptr ? gsf_ele->p4().Pt() : default_value; + get(dnn::gsf_ele_dEta) = gsf_ele != nullptr ? dEta(gsf_ele->p4(), tau.p4()) : default_value; + get(dnn::gsf_ele_dPhi) = gsf_ele != nullptr ? dPhi(gsf_ele->p4(), tau.p4()) : default_value; + get(dnn::gsf_ele_mass) = gsf_ele != nullptr ? gsf_ele->p4().mass() : default_value; + CalculateElectronClusterVars(gsf_ele, get(dnn::gsf_ele_Ee), get(dnn::gsf_ele_Egamma)); + get(dnn::gsf_ele_Pin) = gsf_ele != nullptr ? gsf_ele->trackMomentumAtVtx().R() : default_value; + get(dnn::gsf_ele_Pout) = gsf_ele != nullptr ? gsf_ele->trackMomentumOut().R() : default_value; + get(dnn::gsf_ele_EtotOverPin) = get(dnn::gsf_ele_Pin) > 0 + ? (get(dnn::gsf_ele_Ee) + get(dnn::gsf_ele_Egamma)) / get(dnn::gsf_ele_Pin) + : default_value; + get(dnn::gsf_ele_Eecal) = gsf_ele != nullptr ? gsf_ele->ecalEnergy() : default_value; + get(dnn::gsf_ele_dEta_SeedClusterTrackAtCalo) = gsf_ele != nullptr + ? gsf_ele->deltaEtaSeedClusterTrackAtCalo() : default_value; + get(dnn::gsf_ele_dPhi_SeedClusterTrackAtCalo) = gsf_ele != nullptr + ? gsf_ele->deltaPhiSeedClusterTrackAtCalo() : default_value; + get(dnn::gsf_ele_mvaIn_sigmaEtaEta) = gsf_ele != nullptr + ? gsf_ele->mvaInput().sigmaEtaEta : default_value; + get(dnn::gsf_ele_mvaIn_hadEnergy) = gsf_ele != nullptr ? gsf_ele->mvaInput().hadEnergy : default_value; + get(dnn::gsf_ele_mvaIn_deltaEta) = gsf_ele != nullptr ? gsf_ele->mvaInput().deltaEta : default_value; + + get(dnn::gsf_ele_Chi2NormGSF) = default_value; + get(dnn::gsf_ele_GSFNumHits) = default_value; + get(dnn::gsf_ele_GSFTrackResol) = default_value; + get(dnn::gsf_ele_GSFTracklnPt) = default_value; + if(gsf_ele != nullptr && gsf_ele->gsfTrack().isNonnull()) { + get(dnn::gsf_ele_Chi2NormGSF) = gsf_ele->gsfTrack()->normalizedChi2(); + get(dnn::gsf_ele_GSFNumHits) = gsf_ele->gsfTrack()->numberOfValidHits(); + if(gsf_ele->gsfTrack()->pt() > 0) { + get(dnn::gsf_ele_GSFTrackResol) = gsf_ele->gsfTrack()->ptError() / gsf_ele->gsfTrack()->pt(); + get(dnn::gsf_ele_GSFTracklnPt) = std::log10(gsf_ele->gsfTrack()->pt()); + } + } + + get(dnn::gsf_ele_Chi2NormKF) = default_value; + get(dnn::gsf_ele_KFNumHits) = default_value; + if(gsf_ele != nullptr && gsf_ele->closestCtfTrackRef().isNonnull()) { + get(dnn::gsf_ele_Chi2NormKF) = gsf_ele->closestCtfTrackRef()->normalizedChi2(); + get(dnn::gsf_ele_KFNumHits) = gsf_ele->closestCtfTrackRef()->numberOfValidHits(); + } + get(dnn::leadChargedCand_etaAtEcalEntrance) = tau.etaAtEcalEntranceLeadChargedCand(); + get(dnn::leadChargedCand_pt) = tau.ptLeadChargedCand(); + + get(dnn::leadChargedHadrCand_HoP) = default_value; + get(dnn::leadChargedHadrCand_EoP) = default_value; + if(tau.leadChargedHadrCand()->pt() > 0) { + get(dnn::leadChargedHadrCand_HoP) = tau.hcalEnergyLeadChargedHadrCand() + / tau.leadChargedHadrCand()->pt(); + get(dnn::leadChargedHadrCand_EoP) = tau.ecalEnergyLeadChargedHadrCand() + / tau.leadChargedHadrCand()->pt(); + } + + MuonHitMatch muon_hit_match; + if(tau.leadPFChargedHadrCand().isNonnull() && tau.leadPFChargedHadrCand()->muonRef().isNonnull()) + muon_hit_match.AddMatchedMuon(*tau.leadPFChargedHadrCand()->muonRef(), tau); + + auto matched_muons = muon_hit_match.FindMatchedMuons(tau, muons, 0.3, 5); + for(auto muon : matched_muons) + muon_hit_match.AddMatchedMuon(*muon, tau); + muon_hit_match.FillTensor(get, tau, default_value); + + LorentzVectorXYZ signalChargedHadrCands_sumIn, signalChargedHadrCands_sumOut; + ProcessSignalPFComponents(tau, tau.signalChargedHadrCands(), + signalChargedHadrCands_sumIn, signalChargedHadrCands_sumOut, + get(dnn::signalChargedHadrCands_sum_innerSigCone_pt), + get(dnn::signalChargedHadrCands_sum_innerSigCone_dEta), + get(dnn::signalChargedHadrCands_sum_innerSigCone_dPhi), + get(dnn::signalChargedHadrCands_sum_innerSigCone_mass), + get(dnn::signalChargedHadrCands_sum_outerSigCone_pt), + get(dnn::signalChargedHadrCands_sum_outerSigCone_dEta), + get(dnn::signalChargedHadrCands_sum_outerSigCone_dPhi), + get(dnn::signalChargedHadrCands_sum_outerSigCone_mass), + get(dnn::signalChargedHadrCands_nTotal_innerSigCone), + get(dnn::signalChargedHadrCands_nTotal_outerSigCone)); + + LorentzVectorXYZ signalNeutrHadrCands_sumIn, signalNeutrHadrCands_sumOut; + ProcessSignalPFComponents(tau, tau.signalNeutrHadrCands(), + signalNeutrHadrCands_sumIn, signalNeutrHadrCands_sumOut, + get(dnn::signalNeutrHadrCands_sum_innerSigCone_pt), + get(dnn::signalNeutrHadrCands_sum_innerSigCone_dEta), + get(dnn::signalNeutrHadrCands_sum_innerSigCone_dPhi), + get(dnn::signalNeutrHadrCands_sum_innerSigCone_mass), + get(dnn::signalNeutrHadrCands_sum_outerSigCone_pt), + get(dnn::signalNeutrHadrCands_sum_outerSigCone_dEta), + get(dnn::signalNeutrHadrCands_sum_outerSigCone_dPhi), + get(dnn::signalNeutrHadrCands_sum_outerSigCone_mass), + get(dnn::signalNeutrHadrCands_nTotal_innerSigCone), + get(dnn::signalNeutrHadrCands_nTotal_outerSigCone)); + + + LorentzVectorXYZ signalGammaCands_sumIn, signalGammaCands_sumOut; + ProcessSignalPFComponents(tau, tau.signalGammaCands(), + signalGammaCands_sumIn, signalGammaCands_sumOut, + get(dnn::signalGammaCands_sum_innerSigCone_pt), + get(dnn::signalGammaCands_sum_innerSigCone_dEta), + get(dnn::signalGammaCands_sum_innerSigCone_dPhi), + get(dnn::signalGammaCands_sum_innerSigCone_mass), + get(dnn::signalGammaCands_sum_outerSigCone_pt), + get(dnn::signalGammaCands_sum_outerSigCone_dEta), + get(dnn::signalGammaCands_sum_outerSigCone_dPhi), + get(dnn::signalGammaCands_sum_outerSigCone_mass), + get(dnn::signalGammaCands_nTotal_innerSigCone), + get(dnn::signalGammaCands_nTotal_outerSigCone)); + + LorentzVectorXYZ isolationChargedHadrCands_sum; + ProcessIsolationPFComponents(tau, tau.isolationChargedHadrCands(), isolationChargedHadrCands_sum, + get(dnn::isolationChargedHadrCands_sum_pt), + get(dnn::isolationChargedHadrCands_sum_dEta), + get(dnn::isolationChargedHadrCands_sum_dPhi), + get(dnn::isolationChargedHadrCands_sum_mass), + get(dnn::isolationChargedHadrCands_nTotal)); + + LorentzVectorXYZ isolationNeutrHadrCands_sum; + ProcessIsolationPFComponents(tau, tau.isolationNeutrHadrCands(), isolationNeutrHadrCands_sum, + get(dnn::isolationNeutrHadrCands_sum_pt), + get(dnn::isolationNeutrHadrCands_sum_dEta), + get(dnn::isolationNeutrHadrCands_sum_dPhi), + get(dnn::isolationNeutrHadrCands_sum_mass), + get(dnn::isolationNeutrHadrCands_nTotal)); + + LorentzVectorXYZ isolationGammaCands_sum; + ProcessIsolationPFComponents(tau, tau.isolationGammaCands(), isolationGammaCands_sum, + get(dnn::isolationGammaCands_sum_pt), + get(dnn::isolationGammaCands_sum_dEta), + get(dnn::isolationGammaCands_sum_dPhi), + get(dnn::isolationGammaCands_sum_mass), + get(dnn::isolationGammaCands_nTotal)); + + get(dnn::tau_visMass_innerSigCone) = (signalGammaCands_sumIn + signalChargedHadrCands_sumIn).mass(); + + if(check_all_set) { + for(int var_index = 0; var_index < dnn::NumberOfInputs; ++var_index) { + if(get(var_index) == magic_number) + throw cms::Exception("DeepTauId: variable with index = ") << var_index << " is not set."; + } + } + } + + static void CalculateElectronClusterVars(const pat::Electron* ele, float& elecEe, float& elecEgamma) + { + if(ele) { + elecEe = elecEgamma = 0; + auto superCluster = ele->superCluster(); + if(superCluster.isNonnull() && superCluster.isAvailable() && superCluster->clusters().isNonnull() + && superCluster->clusters().isAvailable()) { + for(auto iter = superCluster->clustersBegin(); iter != superCluster->clustersEnd(); ++iter) { + const double energy = (*iter)->energy(); + if(iter == superCluster->clustersBegin()) elecEe += energy; + else elecEgamma += energy; + } + } + } else { + elecEe = elecEgamma = default_value; + } + } + + template + static void ProcessSignalPFComponents(const pat::Tau& tau, const CandidateCollection& candidates, + LorentzVectorXYZ& p4_inner, LorentzVectorXYZ& p4_outer, + float& pt_inner, float& dEta_inner, float& dPhi_inner, float& m_inner, + float& pt_outer, float& dEta_outer, float& dPhi_outer, float& m_outer, + float& n_inner, float& n_outer) + { + p4_inner = LorentzVectorXYZ(0, 0, 0, 0); + p4_outer = LorentzVectorXYZ(0, 0, 0, 0); + n_inner = 0; + n_outer = 0; + + const double innerSigCone_radius = GetInnerSignalConeRadius(tau.pt()); + for(const auto& cand : candidates) { + const double dR = ROOT::Math::VectorUtil::DeltaR(cand->p4(), tau.leadChargedHadrCand()->p4()); + const bool isInside_innerSigCone = dR < innerSigCone_radius; + if(isInside_innerSigCone) { + p4_inner += cand->p4(); + ++n_inner; + } else { + p4_outer += cand->p4(); + ++n_outer; + } + } + + pt_inner = n_inner != 0 ? p4_inner.Pt() : default_value; + dEta_inner = n_inner != 0 ? dEta(p4_inner, tau.p4()) : default_value; + dPhi_inner = n_inner != 0 ? dPhi(p4_inner, tau.p4()) : default_value; + m_inner = n_inner != 0 ? p4_inner.mass() : default_value; + + pt_outer = n_outer != 0 ? p4_outer.Pt() : default_value; + dEta_outer = n_outer != 0 ? dEta(p4_outer, tau.p4()) : default_value; + dPhi_outer = n_outer != 0 ? dPhi(p4_outer, tau.p4()) : default_value; + m_outer = n_outer != 0 ? p4_outer.mass() : default_value; + } + + template + static void ProcessIsolationPFComponents(const pat::Tau& tau, const CandidateCollection& candidates, + LorentzVectorXYZ& p4, float& pt, float& d_eta, float& d_phi, float& m, + float& n) + { + p4 = LorentzVectorXYZ(0, 0, 0, 0); + n = 0; + + for(const auto& cand : candidates) { + p4 += cand->p4(); + ++n; + } + + pt = n != 0 ? p4.Pt() : default_value; + d_eta = n != 0 ? dEta(p4, tau.p4()) : default_value; + d_phi = n != 0 ? dPhi(p4, tau.p4()) : default_value; + m = n != 0 ? p4.mass() : default_value; + } + + static double GetInnerSignalConeRadius(double pt) + { + return std::max(.05, std::min(.1, 3./std::max(1., pt))); + } + + // Copied from https://github.com/cms-sw/cmssw/blob/CMSSW_9_4_X/RecoTauTag/RecoTau/plugins/PATTauDiscriminationByMVAIsolationRun2.cc#L218 + static float CalculateGottfriedJacksonAngleDifference(const pat::Tau& tau) + { + if(tau.decayMode() == 10) { + static constexpr double mTau = 1.77682; + const double mAOne = tau.p4().M(); + const double pAOneMag = tau.p(); + const double argumentThetaGJmax = (std::pow(mTau,2) - std::pow(mAOne,2) ) / ( 2 * mTau * pAOneMag ); + const double argumentThetaGJmeasured = tau.p4().Vect().Dot(tau.flightLength()) + / ( pAOneMag * tau.flightLength().R() ); + if ( std::abs(argumentThetaGJmax) <= 1. && std::abs(argumentThetaGJmeasured) <= 1. ) { + double thetaGJmax = std::asin( argumentThetaGJmax ); + double thetaGJmeasured = std::acos( argumentThetaGJmeasured ); + return thetaGJmeasured - thetaGJmax; + } + } + return default_value; + } + + static bool IsInEcalCrack(double eta) + { + const double abs_eta = std::abs(eta); + return abs_eta > 1.46 && abs_eta < 1.558; + } + + static const pat::Electron* FindMatchedElectron(const pat::Tau& tau, const pat::ElectronCollection& electrons, + double deltaR) + { + const double deltaR2 = std::pow(deltaR, 2); + const pat::Electron* matched_ele = nullptr; + for(const auto& ele : electrons) { + if(ROOT::Math::VectorUtil::DeltaR2(tau.p4(), ele.p4()) < deltaR2 && + (!matched_ele || matched_ele->pt() < ele.pt())) { + matched_ele = &ele; + } + } + return matched_ele; + } + +private: + edm::EDGetTokenT electrons_token; + edm::EDGetTokenT muons_token; + edm::EDGetTokenT taus_token; + GraphPtr graph; + tensorflow::Session* session; + std::string input_layer, output_layer; + TauIdMVAAuxiliaries clusterVariables; +}; + +#include "FWCore/Framework/interface/MakerMacros.h" +DEFINE_FWK_MODULE(DeepTauId); diff --git a/RecoTauTag/RecoTau/python/DeepTauId_cff.py b/RecoTauTag/RecoTau/python/DeepTauId_cff.py new file mode 100644 index 0000000000000..6d648124ded22 --- /dev/null +++ b/RecoTauTag/RecoTau/python/DeepTauId_cff.py @@ -0,0 +1,11 @@ + +import FWCore.ParameterSet.Config as cms + +deepTauIdraw = cms.EDProducer("DeepTauId", + electrons = cms.InputTag('slimmedElectrons'), + muons = cms.InputTag('slimmedMuons'), + taus = cms.InputTag('slimmedTaus'), + graph_file = cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') +) + +deepTauIdSeq = cms.Sequence(deepTauIdraw) diff --git a/RecoTauTag/RecoTau/test/runTauIdMVA.py b/RecoTauTag/RecoTau/test/runTauIdMVA.py new file mode 100644 index 0000000000000..7e1b98d497427 --- /dev/null +++ b/RecoTauTag/RecoTau/test/runTauIdMVA.py @@ -0,0 +1,598 @@ +from RecoTauTag.RecoTau.TauDiscriminatorTools import noPrediscriminants +from RecoTauTag.RecoTau.PATTauDiscriminationByMVAIsolationRun2_cff import patDiscriminationByIsolationMVArun2v1raw, patDiscriminationByIsolationMVArun2v1VLoose +import os + +class TauIDEmbedder(object): + """class to rerun the tau seq and acces trainings from the database""" + + def __init__(self, process, cms, debug = False, + toKeep = ["2016v1", "newDM2016v1"], + tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + }, + tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + }, + tauIdDiscrMVA_2017_version = "v1", + conditionDB = "" # preparational DB: 'frontier://FrontierPrep/CMS_CONDITIONS' + ): + super(TauIDEmbedder, self).__init__() + self.process = process + self.cms = cms + self.debug = debug + self.process.load('RecoTauTag.Configuration.loadRecoTauTagMVAsFromPrepDB_cfi') + if len(conditionDB) != 0: + self.process.CondDBTauConnection.connect = cms.string(conditionDB) + self.process.loadRecoTauTagMVAsFromPrepDB.connect = cms.string(conditionDB) + # if debug: + # print self.process.CondDBTauConnection.connect + # print dir(self.process.loadRecoTauTagMVAsFromPrepDB) + # print self.process.loadRecoTauTagMVAsFromPrepDB.parameterNames_ + + self.tauIdDiscrMVA_trainings_run2_2017 = tauIdDiscrMVA_trainings_run2_2017 + self.tauIdDiscrMVA_WPs_run2_2017 = tauIdDiscrMVA_WPs_run2_2017 + self.tauIdDiscrMVA_2017_version = tauIdDiscrMVA_2017_version + self.toKeep = toKeep + + + @staticmethod + def get_cmssw_version(debug = False): + """returns 'CMSSW_X_Y_Z'""" + if debug: print "get_cmssw_version:", os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] + return os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] + + @classmethod + def get_cmssw_version_number(klass, debug = False): + """returns 'X_Y_Z' (without 'CMSSW_')""" + if debug: print "get_cmssw_version_number:", map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) + return map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) + + @staticmethod + def versionToInt(release=9, subversion=4, patch=0, debug = False): + if debug: print "versionToInt:", release * 10000 + subversion * 100 + patch + return release * 10000 + subversion * 100 + patch + + @classmethod + def is_above_cmssw_version(klass, release=9, subversion=4, patch=0, debug = False): + split_cmssw_version = klass.get_cmssw_version_number() + if klass.versionToInt(release, subversion, patch) > klass.versionToInt(split_cmssw_version[0], split_cmssw_version[1], split_cmssw_version[2]): + if debug: print "is_above_cmssw_version:", False + return False + else: + if debug: print "is_above_cmssw_version:", True + return True + + def loadMVA_WPs_run2_2017(self): + if self.debug: print "loadMVA_WPs_run2_2017: performed" + global cms + for training, gbrForestName in self.tauIdDiscrMVA_trainings_run2_2017.items(): + + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('GBRWrapperRcd'), + tag = self.cms.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), + label = self.cms.untracked.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) + ) + ) + + for WP in self.tauIdDiscrMVA_WPs_run2_2017[training].keys(): + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('PhysicsTGraphPayloadRcd'), + tag = self.cms.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)), + label = self.cms.untracked.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)) + ) + ) + + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('PhysicsTFormulaPayloadRcd'), + tag = self.cms.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), + label = self.cms.untracked.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) + ) + ) + + def runTauID(self, name='NewTauIDsEmbedded'): + self.process.rerunMvaIsolationSequence = self.cms.Sequence() + tauIDSources = self.cms.PSet() + + # rerun the seq to obtain the 2017 nom training with 0.5 iso cone, old DM, ptph>1, trained on 2017MCv1 + if "2017v1" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v1" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + } + # update the list of available in DB samples + if not self.is_above_cmssw_version(10, 0, 0, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v1" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight + ) + + tauIDSources.byIsolationMVArun2017v1DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw') + tauIDSources.byVVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose') + tauIDSources.byLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Loose') + tauIDSources.byMediumIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Medium') + tauIDSources.byTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Tight') + tauIDSources.byVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VTight') + tauIDSources.byVVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight') + + + if "2017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight') + + if "newDM2017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBnewDMwLT2017' : "tauIdMVAIsoDBnewDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBnewDMwLT2017' : { + 'Eff95' : "DBnewDMwLTEff95", + 'Eff90' : "DBnewDMwLTEff90", + 'Eff80' : "DBnewDMwLTEff80", + 'Eff70' : "DBnewDMwLTEff70", + 'Eff60' : "DBnewDMwLTEff60", + 'Eff50' : "DBnewDMwLTEff50", + 'Eff40' : "DBnewDMwLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access newDM2017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBnewDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBnewDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight') + + if "dR0p32017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : "tauIdMVAIsoDBoldDMdR0p3wLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : { + 'Eff95' : "DBoldDMdR0p3wLTEff95", + 'Eff90' : "DBoldDMdR0p3wLTEff90", + 'Eff80' : "DBoldDMdR0p3wLTEff80", + 'Eff70' : "DBoldDMdR0p3wLTEff70", + 'Eff60' : "DBoldDMdR0p3wLTEff60", + 'Eff50' : "DBoldDMdR0p3wLTEff50", + 'Eff40' : "DBoldDMdR0p3wLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access dR0p32017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2"), + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + srcChargedIsoPtSum = self.cms.string('chargedIsoPtSumdR03'), + srcFootprintCorrection = self.cms.string('footprintCorrectiondR03'), + srcNeutralIsoPtSum = self.cms.string('neutralIsoPtSumdR03'), + srcPhotonPtSumOutsideSignalCone = self.cms.string('photonPtSumOutsideSignalConedR03'), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBoldDMdR0p3wLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight') + + # 2016 training strategy(v2) - essentially the same as 2017 training strategy (v1), trained on 2016MC, old DM - currently not implemented in the tau sequence of any release + # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + # PATTauProducer = self.cms.InputTag('slimmedTaus'), + # Prediscriminants = noPrediscriminants, + # loadMVAfromDB = self.cms.bool(True), + # mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + # mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + # requireDecayMode = self.cms.bool(True), + # verbosity = self.cms.int32(0) + # ) + # # + # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + # PATTauProducer = self.cms.InputTag('slimmedTaus'), + # Prediscriminants = noPrediscriminants, + # toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw'), + # key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw:category'),#? + # loadMVAfromDB = self.cms.bool(True), + # mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + # mapping = self.cms.VPSet( + # self.cms.PSet( + # category = self.cms.uint32(0), + # cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_WPEff90"), #writeTauIdDiscrWPs + # variable = self.cms.string("pt"), + # ) + # ) + # ) + + # 2016 training strategy(v1), trained on 2016MC, old DM + if "2016v1" in self.toKeep: + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1"), + mvaOpt = self.cms.string("DBoldDMwLT"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw:category'), + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_mvaOutput_normalization"), + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff90"), + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight + ) + + tauIDSources.byIsolationMVArun2v1DBoldDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw') + tauIDSources.byVLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VLoose') + tauIDSources.byLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Loose') + tauIDSources.byMediumIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Medium') + tauIDSources.byTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Tight') + tauIDSources.byVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VTight') + tauIDSources.byVVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VVTight') + + # 2016 training strategy(v1), trained on 2016MC, new DM + if "newDM2016v1" in self.toKeep: + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1"), + mvaOpt = self.cms.string("DBnewDMwLT"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw:category'), + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_mvaOutput_normalization"), + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff90"), + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff80") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff70") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff60") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff50") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight + ) + + tauIDSources.byIsolationMVArun2v1DBnewDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw') + tauIDSources.byVLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VLoose') + tauIDSources.byLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Loose') + tauIDSources.byMediumIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Medium') + tauIDSources.byTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Tight') + tauIDSources.byVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VTight') + tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VVTight') + + if "deepTau2017v1" in self.toKeep: + from RecoTauTag.RecoTau.DeepTauId_cff import deepTauIdraw + + self.process.deepTauIdraw = deepTauIdraw.clone( + electrons = self.cms.InputTag('slimmedElectrons'), + muons = self.cms.InputTag('slimmedMuons'), + taus = self.cms.InputTag('slimmedTaus'), + graph_file = self.cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') + ) + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.deepTauIdraw + ) + + tauIDSources.deepTau2017v1tauVSe = self.cms.InputTag('deepTauIdraw', 'tauVSe') + tauIDSources.deepTau2017v1tauVSmu = self.cms.InputTag('deepTauIdraw', 'tauVSmu') + tauIDSources.deepTau2017v1tauVSjet = self.cms.InputTag('deepTauIdraw', 'tauVSjet') + tauIDSources.deepTau2017v1tauVSall = self.cms.InputTag('deepTauIdraw', 'tauVSall') + + embedID = self.cms.EDProducer("PATTauIDEmbedder", + src = self.cms.InputTag('slimmedTaus'), + tauIDSources = tauIDSources + ) + self.process.NewTauIDsEmbedded = embedID + #setattr(process, "NewTauIDsEmbedded", embedID) From 6602b0bafab501eecbbb0cb5c083e7db1cf33a20 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 13 Aug 2018 06:57:17 -0500 Subject: [PATCH 05/20] Building dpf isolation module --- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 348 ++++++++++ RecoTauTag/RecoTau/python/DPFIsolation_cff.py | 10 + RecoTauTag/RecoTau/test/runTauIdMVA.py | 598 ------------------ 3 files changed, 358 insertions(+), 598 deletions(-) create mode 100644 RecoTauTag/RecoTau/plugins/DPFIsolation.cc create mode 100644 RecoTauTag/RecoTau/python/DPFIsolation_cff.py delete mode 100644 RecoTauTag/RecoTau/test/runTauIdMVA.py diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc new file mode 100644 index 0000000000000..b96d8e1c1d33d --- /dev/null +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -0,0 +1,348 @@ +/* + * \class DPFIsolation + * + * Tau identification using Deep NN + * + * \author Konstantin Androsov, INFN Pisa + */ + +#include +#include "FWCore/Framework/interface/stream/EDProducer.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" +#include "DataFormats/PatCandidates/interface/Electron.h" +#include "DataFormats/PatCandidates/interface/Muon.h" +#include "DataFormats/PatCandidates/interface/Tau.h" +#include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" +#include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" +#include "TRandom.h" + +inline int getPFCandidateIndex(edm::Handle pfcands, const reco::CandidatePtr cptr){ + unsigned int pfInd = -1; + for(unsigned int i = 0; i < pfcands->size(); ++i) { +// const pat::PackedCandidate &pf = (*pfcands)[i]; +// if(pf.pt() < candptMin_) continue; + pfInd++; + if(reco::CandidatePtr(pfcands,i) == cptr) { + pfInd = i; + break; + } + } + return pfInd; +} + +//_____________________________________________________________________________ +inline double deltaR2(double eta1, double phi1, double eta2, double phi2) { + double deta = eta1 - eta2; + double dphi = deltaPhi(phi1, phi2); + return deta*deta + dphi*dphi; +} +inline double deltaR(double eta1, double phi1, double eta2, double phi2) { + return std::sqrt(deltaR2 (eta1, phi1, eta2, phi2)); +} + +namespace { + +class DPFIsolation : public edm::stream::EDProducer<> { +public: + using TauType = pat::Tau; + using TauDiscriminator = pat::PATTauDiscriminator; + using TauCollection = std::vector; + using TauRef = edm::Ref; + using TauRefProd = edm::RefProd; + using ElectronCollection = pat::ElectronCollection; + using MuonCollection = pat::MuonCollection; + using LorentzVectorXYZ = ROOT::Math::LorentzVector>; + using GraphPtr = std::shared_ptr; + +std::vector calculate(edm::Handle& taus, edm::Handle pfcands, tensorflow::Tensor tensor, std::vector outputs, tensorflow::Session* session, TRandom * r0) { + + + std::vector predictions; + float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, + pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, + pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, + pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz; + + bool pfCandIsBarrel; + + for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { + pat::Tau tau = taus->at(tau_index); + bool isGoodTau = false; + if(tau.isTauIDAvailable("againstMuonLoose3") + and tau.isTauIDAvailable("againstElectronVLooseMVA6")) { + isGoodTau = (tau.tauID("againstElectronVLooseMVA6") and tau.tauID("againstMuonLoose3") ); + } + + if (isGoodTau == false) predictions.push_back(-1); + if (isGoodTau == false) continue; + + + std::vector signalCandidateInds; + + for(auto c : tau.signalCands()) + signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c)); + + float lepRecoPt = tau.pt(); + float lepRecoPz = TMath::Abs(tau.pz()); + float lepRecoEt = tau.energy(); + float lepRecoEta = tau.eta(); + tensor.flat().setZero(); + + unsigned int iPF = 0; + + std::vector sorted_inds(pfcands->size()); + std::size_t n(0); + std::generate(std::begin(sorted_inds), std::end(sorted_inds), [&]{ return n++; }); + + std::sort( std::begin(sorted_inds), std::end(sorted_inds), + [&](int i1, int i2) { return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } ); + + for(size_t pf_index = 0; pf_index < pfcands->size(); pf_index++) { + pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); + float deltaR_tau_p = deltaR(p.eta(),p.phi(),tau.eta(),tau.phi()); + + if (p.pt() < .5) continue; + if (p.fromPV() < 0) continue; + + if (deltaR_tau_p > .5) continue; + + + if (p.fromPV() < 1 and p.charge() != 0) continue; + pfCandPt = p.pt(); + pfCandPtRel = p.pt()/lepRecoPt; + + pfCandDr = deltaR_tau_p; + pfCandDEta = TMath::Abs(tau.eta() - p.eta()); + pfCandDPhi = TMath::Abs(deltaPhi(tau.phi(), p.phi())); + pfCandEta = p.eta(); + pfCandIsBarrel = (TMath::Abs(p.eta()) < 1.4); + pfCandPz = TMath::Abs(TMath::SinH(pfCandEta)*pfCandPt); + pfCandPzRel = TMath::Abs(TMath::SinH(pfCandEta)*pfCandPt)/lepRecoPz; + pfCandPdgID = TMath::Abs(p.pdgId()); + pfCandCharge = p.charge(); + + if (pfCandCharge != 0 and p.hasTrackDetails()){ + pfCandDz = p.dz(); + pfCandDzErr = p.dzError(); + pfCandDzSig = (TMath::Abs(p.dz()) + .000001)/(p.dzError() + .00001); + pfCandD0 = p.dxy(); + pfCandD0Err = p.dxyError(); + pfCandD0Sig = (TMath::Abs(p.dxy()) + .000001)/ (p.dxyError() + .00001); + pfCandPixHits = p.numberOfPixelHits(); + pfCandHits = p.numberOfHits(); + pfCandLostInnerHits = p.lostInnerHits(); + } else{ + float disp = 1; + if (r0->Rndm(1) > .5){ + disp = -1; + } + pfCandDz = 5*disp; + pfCandDzErr = 0; + pfCandDzSig = 0; + pfCandD0 = 5*disp; + pfCandD0Err = 0; + pfCandD0Sig = 0; + pfCandPixHits = 0; + pfCandHits = 0; + pfCandLostInnerHits = 2.; + } + + pfCandPuppiWeight = p.puppiWeight(); + pfCandFromPV = p.fromPV(); + pfCandVtxQuality = p.pvAssociationQuality();//VtxAssocQual(); + pfCandHighPurityTrk = p.trackHighPurity();//HighPurityTrk(); + float pfCandTauIndMatch_temp = 0; + for (auto i : signalCandidateInds){ + if (i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1; + } + + + pfCandTauIndMatch = pfCandTauIndMatch_temp; + pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel; + if (pfCandPt > 500) pfCandPt = 500.; + pfCandPt = pfCandPt/500.; + + if (pfCandPz > 1000) pfCandPz = 1000.; + pfCandPz = pfCandPz/1000.; + + if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.; + if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.; + pfCandPzRel = pfCandPzRel/100.; + pfCandDr = pfCandDr/.5; + pfCandEta = pfCandEta/2.75; + pfCandDEta = pfCandDEta/.5; + pfCandDPhi = pfCandDPhi/.5; + pfCandPixHits = pfCandPixHits/7.; + pfCandHits = pfCandHits/30.; + + if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1; + pfCandPtRelPtRel = pfCandPtRelPtRel; + + if (pfCandD0 > 5.) pfCandD0 = 5.; + if (pfCandD0 < -5.) pfCandD0 = -5.; + pfCandD0 = pfCandD0/5.; + + if (pfCandDz > 5.) pfCandDz = 5.; + if (pfCandDz < -5.) pfCandDz = -5.; + pfCandDz = pfCandDz/5.; + + if (pfCandD0Err > 1.) pfCandD0Err = 1.; + if (pfCandDzErr > 1.) pfCandDzErr = 1.; + if (pfCandDzSig > 3) pfCandDzSig = 3.; + pfCandDzSig = pfCandDzSig/3.; + + if (pfCandD0Sig > 1) pfCandD0Sig = 1.; + pfCandD0D0 = pfCandD0*pfCandD0; + pfCandDzDz = pfCandDz*pfCandDz; + pfCandD0Dz = pfCandD0*pfCandDz; + pfCandD0Dphi = pfCandD0*pfCandDPhi; + + tensor.tensor()( 0, 60-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; + tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; + tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; + tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; + tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; + tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; + tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; + tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); + tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); + tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); + tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; + tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; + tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; + tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; + tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; + tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; + tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; + tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; + tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; + iPF++; + if (iPF == 60) break; + } + + tensorflow::Status status = session->Run( { {"input_1", tensor}},{"output_node0"}, {}, &outputs); + float output = outputs[0].scalar()(); + /*for (int iPF =0; iPF < 60; iPF++){ + for (int iVar = 0; iVar < 47; iVar++){ + std::cout << tensor.tensor()(0,iPF,iVar) << ", "; + } + std::cout << std::endl; + }*/ + predictions.push_back(output); + } + return predictions; +}; + + struct Output { + std::vector num, den; + Output(std::vector _num, std::vector _den) : num(_num), den(_den) {} + + std::unique_ptr get_value(edm::Handle& taus, std::vector predictions) + { + auto output = std::make_unique(TauRefProd(taus)); + + for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { + output->setValue(tau_index, predictions.at(tau_index) ); + } +// std::cout << " successfully got an output = " << std::endl; + return output; + } + }; + + using OutputCollection = std::map; + + static OutputCollection& GetOutputs() + { + static size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; + static OutputCollection outputs = { + { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, + { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, + { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, + { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } + }; + return outputs; + }; + +public: + explicit DPFIsolation(const edm::ParameterSet& cfg) : + taus_token(consumes(cfg.getParameter("taus"))), + pfcand_token(consumes (cfg.getParameter("pfcands"))), + graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), + graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), + session(tensorflow::createSession(graph.get())) + { + std::cout << " The graph file name = " << graphName << std::endl; + for(auto& output_desc : GetOutputs()) + produces(output_desc.first); +// std::cout << " Running a tau id.. " << std::endl; + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); + }; + + virtual ~DPFIsolation() override + { + tensorflow::closeSession(session); + }; + + virtual void produce(edm::Event& event, const edm::EventSetup& es) override + { + + event.getByToken(taus_token, taus); + event.getByToken(pfcand_token, pfcands); + + r0 = new TRandom(0); + std::vector predictions = DPFIsolation::calculate(taus, pfcands, tensor, outputs, session, r0); + + for(auto& output_desc : GetOutputs()) + event.put(output_desc.second.get_value(taus,predictions), output_desc.first); + }; + +private: + edm::EDGetTokenT taus_token; + edm::EDGetTokenT pfcand_token; + + edm::Handle taus; + edm::Handle pfcands; + + TString graphName; + GraphPtr graph; + tensorflow::Session* session; + TRandom * r0; + static const unsigned int nparticles_v0 = 60; + static const unsigned int nfeatures_v0 = 47; + tensorflow::Tensor tensor; + std::vector outputs; + + +}; +}; +#include "FWCore/Framework/interface/MakerMacros.h" +DEFINE_FWK_MODULE(DPFIsolation); diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py new file mode 100644 index 0000000000000..ba4c1bd606ed2 --- /dev/null +++ b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py @@ -0,0 +1,10 @@ + +import FWCore.ParameterSet.Config as cms + +DPFIsolation = cms.EDProducer("DPFIsolation", + pfcands = cms.InputTag('packedPFCandidates'), + taus = cms.InputTag('slimmedTaus'), + graph_file = cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') +) + +DPFIsolationSeq = cms.Sequence(DPFIsolation) diff --git a/RecoTauTag/RecoTau/test/runTauIdMVA.py b/RecoTauTag/RecoTau/test/runTauIdMVA.py deleted file mode 100644 index 7e1b98d497427..0000000000000 --- a/RecoTauTag/RecoTau/test/runTauIdMVA.py +++ /dev/null @@ -1,598 +0,0 @@ -from RecoTauTag.RecoTau.TauDiscriminatorTools import noPrediscriminants -from RecoTauTag.RecoTau.PATTauDiscriminationByMVAIsolationRun2_cff import patDiscriminationByIsolationMVArun2v1raw, patDiscriminationByIsolationMVArun2v1VLoose -import os - -class TauIDEmbedder(object): - """class to rerun the tau seq and acces trainings from the database""" - - def __init__(self, process, cms, debug = False, - toKeep = ["2016v1", "newDM2016v1"], - tauIdDiscrMVA_trainings_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", - }, - tauIdDiscrMVA_WPs_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : { - 'Eff95' : "DBoldDMwLTEff95", - 'Eff90' : "DBoldDMwLTEff90", - 'Eff80' : "DBoldDMwLTEff80", - 'Eff70' : "DBoldDMwLTEff70", - 'Eff60' : "DBoldDMwLTEff60", - 'Eff50' : "DBoldDMwLTEff50", - 'Eff40' : "DBoldDMwLTEff40" - } - }, - tauIdDiscrMVA_2017_version = "v1", - conditionDB = "" # preparational DB: 'frontier://FrontierPrep/CMS_CONDITIONS' - ): - super(TauIDEmbedder, self).__init__() - self.process = process - self.cms = cms - self.debug = debug - self.process.load('RecoTauTag.Configuration.loadRecoTauTagMVAsFromPrepDB_cfi') - if len(conditionDB) != 0: - self.process.CondDBTauConnection.connect = cms.string(conditionDB) - self.process.loadRecoTauTagMVAsFromPrepDB.connect = cms.string(conditionDB) - # if debug: - # print self.process.CondDBTauConnection.connect - # print dir(self.process.loadRecoTauTagMVAsFromPrepDB) - # print self.process.loadRecoTauTagMVAsFromPrepDB.parameterNames_ - - self.tauIdDiscrMVA_trainings_run2_2017 = tauIdDiscrMVA_trainings_run2_2017 - self.tauIdDiscrMVA_WPs_run2_2017 = tauIdDiscrMVA_WPs_run2_2017 - self.tauIdDiscrMVA_2017_version = tauIdDiscrMVA_2017_version - self.toKeep = toKeep - - - @staticmethod - def get_cmssw_version(debug = False): - """returns 'CMSSW_X_Y_Z'""" - if debug: print "get_cmssw_version:", os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] - return os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] - - @classmethod - def get_cmssw_version_number(klass, debug = False): - """returns 'X_Y_Z' (without 'CMSSW_')""" - if debug: print "get_cmssw_version_number:", map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) - return map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) - - @staticmethod - def versionToInt(release=9, subversion=4, patch=0, debug = False): - if debug: print "versionToInt:", release * 10000 + subversion * 100 + patch - return release * 10000 + subversion * 100 + patch - - @classmethod - def is_above_cmssw_version(klass, release=9, subversion=4, patch=0, debug = False): - split_cmssw_version = klass.get_cmssw_version_number() - if klass.versionToInt(release, subversion, patch) > klass.versionToInt(split_cmssw_version[0], split_cmssw_version[1], split_cmssw_version[2]): - if debug: print "is_above_cmssw_version:", False - return False - else: - if debug: print "is_above_cmssw_version:", True - return True - - def loadMVA_WPs_run2_2017(self): - if self.debug: print "loadMVA_WPs_run2_2017: performed" - global cms - for training, gbrForestName in self.tauIdDiscrMVA_trainings_run2_2017.items(): - - self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( - self.cms.PSet( - record = self.cms.string('GBRWrapperRcd'), - tag = self.cms.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), - label = self.cms.untracked.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) - ) - ) - - for WP in self.tauIdDiscrMVA_WPs_run2_2017[training].keys(): - self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( - self.cms.PSet( - record = self.cms.string('PhysicsTGraphPayloadRcd'), - tag = self.cms.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)), - label = self.cms.untracked.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)) - ) - ) - - self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( - self.cms.PSet( - record = self.cms.string('PhysicsTFormulaPayloadRcd'), - tag = self.cms.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), - label = self.cms.untracked.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) - ) - ) - - def runTauID(self, name='NewTauIDsEmbedded'): - self.process.rerunMvaIsolationSequence = self.cms.Sequence() - tauIDSources = self.cms.PSet() - - # rerun the seq to obtain the 2017 nom training with 0.5 iso cone, old DM, ptph>1, trained on 2017MCv1 - if "2017v1" in self.toKeep: - self.tauIdDiscrMVA_2017_version = "v1" - self.tauIdDiscrMVA_trainings_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", - } - self.tauIdDiscrMVA_WPs_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : { - 'Eff95' : "DBoldDMwLTEff95", - 'Eff90' : "DBoldDMwLTEff90", - 'Eff80' : "DBoldDMwLTEff80", - 'Eff70' : "DBoldDMwLTEff70", - 'Eff60' : "DBoldDMwLTEff60", - 'Eff50' : "DBoldDMwLTEff50", - 'Eff40' : "DBoldDMwLTEff40" - } - } - # update the list of available in DB samples - if not self.is_above_cmssw_version(10, 0, 0, self.debug): - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v1" - self.loadMVA_WPs_run2_2017() - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs - mvaOpt = self.cms.string("DBoldDMwLTwGJ"), - requireDecayMode = self.cms.bool(True), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw:category'),#? - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff90"), #writeTauIdDiscrWPs - variable = self.cms.string("pt"), - ) - ) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff95") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff80") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff70") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff60") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff50") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight - ) - - tauIDSources.byIsolationMVArun2017v1DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw') - tauIDSources.byVVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose') - tauIDSources.byVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose') - tauIDSources.byLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Loose') - tauIDSources.byMediumIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Medium') - tauIDSources.byTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Tight') - tauIDSources.byVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VTight') - tauIDSources.byVVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight') - - - if "2017v2" in self.toKeep: - self.tauIdDiscrMVA_2017_version = "v2" - self.tauIdDiscrMVA_trainings_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", - } - self.tauIdDiscrMVA_WPs_run2_2017 = { - 'tauIdMVAIsoDBoldDMwLT2017' : { - 'Eff95' : "DBoldDMwLTEff95", - 'Eff90' : "DBoldDMwLTEff90", - 'Eff80' : "DBoldDMwLTEff80", - 'Eff70' : "DBoldDMwLTEff70", - 'Eff60' : "DBoldDMwLTEff60", - 'Eff50' : "DBoldDMwLTEff50", - 'Eff40' : "DBoldDMwLTEff40" - } - } - - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v2" - self.loadMVA_WPs_run2_2017() - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs - mvaOpt = self.cms.string("DBoldDMwLTwGJ"), - requireDecayMode = self.cms.bool(True), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw:category'),#? - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs - variable = self.cms.string("pt"), - ) - ), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff95") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff80") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff70") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff60") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff50") - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight - ) - - tauIDSources.byIsolationMVArun2017v2DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw') - tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose') - tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose') - tauIDSources.byLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Loose') - tauIDSources.byMediumIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Medium') - tauIDSources.byTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Tight') - tauIDSources.byVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VTight') - tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight') - - if "newDM2017v2" in self.toKeep: - self.tauIdDiscrMVA_2017_version = "v2" - self.tauIdDiscrMVA_trainings_run2_2017 = { - 'tauIdMVAIsoDBnewDMwLT2017' : "tauIdMVAIsoDBnewDMwLT2017", - } - self.tauIdDiscrMVA_WPs_run2_2017 = { - 'tauIdMVAIsoDBnewDMwLT2017' : { - 'Eff95' : "DBnewDMwLTEff95", - 'Eff90' : "DBnewDMwLTEff90", - 'Eff80' : "DBnewDMwLTEff80", - 'Eff70' : "DBnewDMwLTEff70", - 'Eff60' : "DBnewDMwLTEff60", - 'Eff50' : "DBnewDMwLTEff50", - 'Eff40' : "DBnewDMwLTEff40" - } - } - - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access newDM2017v2" - self.loadMVA_WPs_run2_2017() - - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs - mvaOpt = self.cms.string("DBnewDMwLTwGJ"), - requireDecayMode = self.cms.bool(True), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw:category'),#? - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs - variable = self.cms.string("pt"), - ) - ), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff95") - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff80") - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff70") - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff60") - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff50") - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight - ) - - tauIDSources.byIsolationMVArun2017v2DBnewDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw') - tauIDSources.byVVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose') - tauIDSources.byVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose') - tauIDSources.byLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Loose') - tauIDSources.byMediumIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Medium') - tauIDSources.byTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Tight') - tauIDSources.byVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VTight') - tauIDSources.byVVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight') - - if "dR0p32017v2" in self.toKeep: - self.tauIdDiscrMVA_2017_version = "v2" - self.tauIdDiscrMVA_trainings_run2_2017 = { - 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : "tauIdMVAIsoDBoldDMdR0p3wLT2017", - } - self.tauIdDiscrMVA_WPs_run2_2017 = { - 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : { - 'Eff95' : "DBoldDMdR0p3wLTEff95", - 'Eff90' : "DBoldDMdR0p3wLTEff90", - 'Eff80' : "DBoldDMdR0p3wLTEff80", - 'Eff70' : "DBoldDMdR0p3wLTEff70", - 'Eff60' : "DBoldDMdR0p3wLTEff60", - 'Eff50' : "DBoldDMdR0p3wLTEff50", - 'Eff40' : "DBoldDMdR0p3wLTEff40" - } - } - - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access dR0p32017v2" - self.loadMVA_WPs_run2_2017() - - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2"), - mvaOpt = self.cms.string("DBoldDMwLTwGJ"), - requireDecayMode = self.cms.bool(True), - srcChargedIsoPtSum = self.cms.string('chargedIsoPtSumdR03'), - srcFootprintCorrection = self.cms.string('footprintCorrectiondR03'), - srcNeutralIsoPtSum = self.cms.string('neutralIsoPtSumdR03'), - srcPhotonPtSumOutsideSignalCone = self.cms.string('photonPtSumOutsideSignalConedR03'), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw:category'),#? - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff90"), #writeTauIdDiscrWPs - variable = self.cms.string("pt"), - ) - ), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff95") - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff80") - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff70") - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff60") - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff50") - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight - ) - - tauIDSources.byIsolationMVArun2017v2DBoldDMdR0p3wLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw') - tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose') - tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose') - tauIDSources.byLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose') - tauIDSources.byMediumIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium') - tauIDSources.byTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight') - tauIDSources.byVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight') - tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight') - - # 2016 training strategy(v2) - essentially the same as 2017 training strategy (v1), trained on 2016MC, old DM - currently not implemented in the tau sequence of any release - # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( - # PATTauProducer = self.cms.InputTag('slimmedTaus'), - # Prediscriminants = noPrediscriminants, - # loadMVAfromDB = self.cms.bool(True), - # mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs - # mvaOpt = self.cms.string("DBoldDMwLTwGJ"), - # requireDecayMode = self.cms.bool(True), - # verbosity = self.cms.int32(0) - # ) - # # - # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - # PATTauProducer = self.cms.InputTag('slimmedTaus'), - # Prediscriminants = noPrediscriminants, - # toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw'), - # key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw:category'),#? - # loadMVAfromDB = self.cms.bool(True), - # mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations - # mapping = self.cms.VPSet( - # self.cms.PSet( - # category = self.cms.uint32(0), - # cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_WPEff90"), #writeTauIdDiscrWPs - # variable = self.cms.string("pt"), - # ) - # ) - # ) - - # 2016 training strategy(v1), trained on 2016MC, old DM - if "2016v1" in self.toKeep: - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1"), - mvaOpt = self.cms.string("DBoldDMwLT"), - requireDecayMode = self.cms.bool(True), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw:category'), - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_mvaOutput_normalization"), - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff90"), - variable = self.cms.string("pt"), - ) - ) - ) - - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff80") - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff70") - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff60") - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff50") - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight - ) - - tauIDSources.byIsolationMVArun2v1DBoldDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw') - tauIDSources.byVLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VLoose') - tauIDSources.byLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Loose') - tauIDSources.byMediumIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Medium') - tauIDSources.byTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Tight') - tauIDSources.byVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VTight') - tauIDSources.byVVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VVTight') - - # 2016 training strategy(v1), trained on 2016MC, new DM - if "newDM2016v1" in self.toKeep: - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - loadMVAfromDB = self.cms.bool(True), - mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1"), - mvaOpt = self.cms.string("DBnewDMwLT"), - requireDecayMode = self.cms.bool(True), - verbosity = self.cms.int32(0) - ) - - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( - PATTauProducer = self.cms.InputTag('slimmedTaus'), - Prediscriminants = noPrediscriminants, - toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw'), - key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw:category'), - loadMVAfromDB = self.cms.bool(True), - mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_mvaOutput_normalization"), - mapping = self.cms.VPSet( - self.cms.PSet( - category = self.cms.uint32(0), - cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff90"), - variable = self.cms.string("pt"), - ) - ) - ) - - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff80") - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff70") - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff60") - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff50") - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff40") - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight - ) - - tauIDSources.byIsolationMVArun2v1DBnewDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw') - tauIDSources.byVLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VLoose') - tauIDSources.byLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Loose') - tauIDSources.byMediumIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Medium') - tauIDSources.byTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Tight') - tauIDSources.byVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VTight') - tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VVTight') - - if "deepTau2017v1" in self.toKeep: - from RecoTauTag.RecoTau.DeepTauId_cff import deepTauIdraw - - self.process.deepTauIdraw = deepTauIdraw.clone( - electrons = self.cms.InputTag('slimmedElectrons'), - muons = self.cms.InputTag('slimmedMuons'), - taus = self.cms.InputTag('slimmedTaus'), - graph_file = self.cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') - ) - - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.deepTauIdraw - ) - - tauIDSources.deepTau2017v1tauVSe = self.cms.InputTag('deepTauIdraw', 'tauVSe') - tauIDSources.deepTau2017v1tauVSmu = self.cms.InputTag('deepTauIdraw', 'tauVSmu') - tauIDSources.deepTau2017v1tauVSjet = self.cms.InputTag('deepTauIdraw', 'tauVSjet') - tauIDSources.deepTau2017v1tauVSall = self.cms.InputTag('deepTauIdraw', 'tauVSall') - - embedID = self.cms.EDProducer("PATTauIDEmbedder", - src = self.cms.InputTag('slimmedTaus'), - tauIDSources = tauIDSources - ) - self.process.NewTauIDsEmbedded = embedID - #setattr(process, "NewTauIDsEmbedded", embedID) From bdf80feaa33dbc53bf2d493aa86c81f04277e12d Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 16 Aug 2018 08:20:21 -0500 Subject: [PATCH 06/20] Adding in v1 --- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 255 +++++++++++++----- RecoTauTag/RecoTau/python/DPFIsolation_cff.py | 1 + 2 files changed, 194 insertions(+), 62 deletions(-) diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index b96d8e1c1d33d..1fdb053fb660e 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -55,22 +55,31 @@ class DPFIsolation : public edm::stream::EDProducer<> { using LorentzVectorXYZ = ROOT::Math::LorentzVector>; using GraphPtr = std::shared_ptr; -std::vector calculate(edm::Handle& taus, edm::Handle pfcands, tensorflow::Tensor tensor, std::vector outputs, tensorflow::Session* session, TRandom * r0) { +std::vector calculate(edm::Handle& taus, edm::Handle & pfcands, edm::Handle & vertices, tensorflow::Tensor tensor, std::vector outputs, tensorflow::Session* session, TRandom * r0) { std::vector predictions; float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, - pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz; - + pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz,pfCandDVx_1,pfCandDVy_1,pfCandDVz_1,pfCandD_1; +// std::cout << " Loading the vertices " << std::endl; +// std::cout << " vertices size = " << vertices->size() << std::endl; +// std::cout << " (*vertices)[0] = " << (*vertices)[0] << std::endl; + float pvx = vertices->size() > 0 ? (*vertices)[0].x() : -1; + float pvy = vertices->size() > 0 ? (*vertices)[0].y() : -1; + float pvz = vertices->size() > 0 ? (*vertices)[0].z() : -1; +// std::cout << "vertices loaded " << std::endl; +// std::cout << " taus->size() = " << taus->size() << std::endl; bool pfCandIsBarrel; - + if (taus->size() == 0){ return predictions;} for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { +// std::cout << "booting up a tau" << std::endl; + pat::Tau tau = taus->at(tau_index); bool isGoodTau = false; if(tau.isTauIDAvailable("againstMuonLoose3") - and tau.isTauIDAvailable("againstElectronVLooseMVA6")) { + and tau.isTauIDAvailable("againstElectronVLooseMVA6") and tau.pt() >= 30 and fabs(tau.eta()) < 2.3) { isGoodTau = (tau.tauID("againstElectronVLooseMVA6") and tau.tauID("againstMuonLoose3") ); } @@ -97,10 +106,12 @@ std::vector calculate(edm::Handle& taus, edm::Handleat(i1).pt() > pfcands->at(i2).pt(); } ); +// std::cout << "tau booted" << std::endl; for(size_t pf_index = 0; pf_index < pfcands->size(); pf_index++) { pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); float deltaR_tau_p = deltaR(p.eta(),p.phi(),tau.eta(),tau.phi()); +// std::cout << "booting up a pfcanc" << std::endl; if (p.pt() < .5) continue; if (p.fromPV() < 0) continue; @@ -121,6 +132,16 @@ std::vector calculate(edm::Handle& taus, edm::Handle()( 0, 60-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; + tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; + tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; + tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; + tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; + tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; + tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; + tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); + tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); + tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); + tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; + tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; + tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; + tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; + tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; + tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; + tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; + tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; + tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; + } + + + + if (graphName.EndsWith("v1.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb"){ +// std::cout << " Loading a pfcandidate " << std::endl; + tensor.tensor()( 0, 36-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 36-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 36-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 36-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 36-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 36-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 36-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 36-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 36-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 36-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 36-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 36-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 36-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 36-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 36-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 36-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 36-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 36-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 36-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 36-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 36-1-iPF, 20) = pfCandDVx_1; + tensor.tensor()( 0, 36-1-iPF, 21) = pfCandDVy_1; + tensor.tensor()( 0, 36-1-iPF, 22) = pfCandDVz_1; + tensor.tensor()( 0, 36-1-iPF, 23) = pfCandD_1; + tensor.tensor()( 0, 36-1-iPF, 24) = pfCandDzDz; + tensor.tensor()( 0, 36-1-iPF, 25) = pfCandD0Dz; + tensor.tensor()( 0, 36-1-iPF, 26) = pfCandD0Dphi; + tensor.tensor()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel; + tensor.tensor()( 0, 36-1-iPF, 28) = pfCandPixHits; + tensor.tensor()( 0, 36-1-iPF, 29) = pfCandHits; + tensor.tensor()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 36-1-iPF, 34) = pfCandPuppiWeight; + tensor.tensor()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1); + tensor.tensor()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2); + tensor.tensor()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3); + tensor.tensor()( 0, 36-1-iPF, 42) = pfCandIsBarrel; + tensor.tensor()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk; + tensor.tensor()( 0, 36-1-iPF, 44) = pfCandPdgID==1; + tensor.tensor()( 0, 36-1-iPF, 45) = pfCandPdgID==2; + tensor.tensor()( 0, 36-1-iPF, 46) = pfCandPdgID==11; + tensor.tensor()( 0, 36-1-iPF, 47) = pfCandPdgID==13; + tensor.tensor()( 0, 36-1-iPF, 48) = pfCandPdgID==130; + tensor.tensor()( 0, 36-1-iPF, 49) = pfCandPdgID==211; + tensor.tensor()( 0, 36-1-iPF, 50) = pfCandTauIndMatch; + } + iPF++; - if (iPF == 60) break; + if (graphName.EndsWith("v0.pb") and iPF == 60) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; + if (graphName.EndsWith("v1.pb") and iPF == 36) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; + } +// std::cout << "tensor loaded" << std::endl; +// std::cout << " running predictor " << std::endl; tensorflow::Status status = session->Run( { {"input_1", tensor}},{"output_node0"}, {}, &outputs); - float output = outputs[0].scalar()(); - /*for (int iPF =0; iPF < 60; iPF++){ - for (int iVar = 0; iVar < 47; iVar++){ +// std::cout << " predictor success " << std::endl; + //float output = outputs[0].scalar()(); + tensorflow::TTypes::Flat output = outputs[0].flat(); +// std::cout << "flatting " << std::endl; +// std::cout << " output(0) = " << output(0) << std::endl; +/*if (graphName.EndsWith("v1.pb")){ + for (int iPF =0; iPF < 36; iPF++){ + for (int iVar = 0; iVar < 51; iVar++){ std::cout << tensor.tensor()(0,iPF,iVar) << ", "; } std::cout << std::endl; - }*/ - predictions.push_back(output); + }} + std::cout << " (*vertices)[0].x() = " << (*vertices)[0].x() << " (*vertices)[0].y() = " << (*vertices)[0].y() << " (*vertices)[0].z() = " << (*vertices)[0].z() << std::endl; +*/ + predictions.push_back(output(0)); } return predictions; }; @@ -296,15 +415,20 @@ std::vector calculate(edm::Handle& taus, edm::Handle(cfg.getParameter("taus"))), pfcand_token(consumes (cfg.getParameter("pfcands"))), + vtx_token(consumes (cfg.getParameter("vertices"))), graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), session(tensorflow::createSession(graph.get())) { - std::cout << " The graph file name = " << graphName << std::endl; +// std::cout << " The graph file name = " << graphName << std::endl; for(auto& output_desc : GetOutputs()) produces(output_desc.first); -// std::cout << " Running a tau id.. " << std::endl; - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); +//// std::cout << " Running a tau id.. " << std::endl; + if (graphName.EndsWith("v0.pb")) + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); + if (graphName.EndsWith("v1.pb")) + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v1, nfeatures_v1})); + }; virtual ~DPFIsolation() override @@ -317,9 +441,10 @@ std::vector calculate(edm::Handle& taus, edm::Handle predictions = DPFIsolation::calculate(taus, pfcands, tensor, outputs, session, r0); + std::vector predictions = DPFIsolation::calculate(taus, pfcands, vertices, tensor, outputs, session, r0); for(auto& output_desc : GetOutputs()) event.put(output_desc.second.get_value(taus,predictions), output_desc.first); @@ -328,9 +453,11 @@ std::vector calculate(edm::Handle& taus, edm::Handle taus_token; edm::EDGetTokenT pfcand_token; + edm::EDGetTokenT vtx_token; edm::Handle taus; edm::Handle pfcands; + edm::Handle vertices; TString graphName; GraphPtr graph; @@ -338,6 +465,10 @@ std::vector calculate(edm::Handle& taus, edm::Handle outputs; diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py index ba4c1bd606ed2..a2dd1ea570842 100644 --- a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py +++ b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py @@ -4,6 +4,7 @@ DPFIsolation = cms.EDProducer("DPFIsolation", pfcands = cms.InputTag('packedPFCandidates'), taus = cms.InputTag('slimmedTaus'), + vertices = cms.InputTag('offlineSlimmedPrimaryVertices'), graph_file = cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') ) From ef6a6026ccb833692808aaa7500da58658b4fb5d Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 16 Aug 2018 09:45:59 -0500 Subject: [PATCH 07/20] Adding in runTauIDMVA for other users --- RecoTauTag/RecoTau/python/runTauIdMVA.py | 644 +++++++++++++++++++++++ 1 file changed, 644 insertions(+) create mode 100644 RecoTauTag/RecoTau/python/runTauIdMVA.py diff --git a/RecoTauTag/RecoTau/python/runTauIdMVA.py b/RecoTauTag/RecoTau/python/runTauIdMVA.py new file mode 100644 index 0000000000000..f3bf04a9ea134 --- /dev/null +++ b/RecoTauTag/RecoTau/python/runTauIdMVA.py @@ -0,0 +1,644 @@ +from RecoTauTag.RecoTau.TauDiscriminatorTools import noPrediscriminants +from RecoTauTag.RecoTau.PATTauDiscriminationByMVAIsolationRun2_cff import patDiscriminationByIsolationMVArun2v1raw, patDiscriminationByIsolationMVArun2v1VLoose +import os + +class TauIDEmbedder(object): + """class to rerun the tau seq and acces trainings from the database""" + + def __init__(self, process, cms, debug = False, + toKeep = ["2016v1", "newDM2016v1","deepTau2017v1","DPFTau_2016_v0"], + tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + }, + tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + }, + tauIdDiscrMVA_2017_version = "v1", + conditionDB = "" # preparational DB: 'frontier://FrontierPrep/CMS_CONDITIONS' + ): + super(TauIDEmbedder, self).__init__() + self.process = process + self.cms = cms + self.debug = debug + self.process.load('RecoTauTag.Configuration.loadRecoTauTagMVAsFromPrepDB_cfi') + if len(conditionDB) != 0: + self.process.CondDBTauConnection.connect = cms.string(conditionDB) + self.process.loadRecoTauTagMVAsFromPrepDB.connect = cms.string(conditionDB) + # if debug: + # print self.process.CondDBTauConnection.connect + # print dir(self.process.loadRecoTauTagMVAsFromPrepDB) + # print self.process.loadRecoTauTagMVAsFromPrepDB.parameterNames_ + + self.tauIdDiscrMVA_trainings_run2_2017 = tauIdDiscrMVA_trainings_run2_2017 + self.tauIdDiscrMVA_WPs_run2_2017 = tauIdDiscrMVA_WPs_run2_2017 + self.tauIdDiscrMVA_2017_version = tauIdDiscrMVA_2017_version + self.toKeep = toKeep + + + @staticmethod + def get_cmssw_version(debug = False): + """returns 'CMSSW_X_Y_Z'""" + if debug: print "get_cmssw_version:", os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] + return os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] + + @classmethod + def get_cmssw_version_number(klass, debug = False): + """returns 'X_Y_Z' (without 'CMSSW_')""" + if debug: print "get_cmssw_version_number:", map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) + return map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) + + @staticmethod + def versionToInt(release=9, subversion=4, patch=0, debug = False): + if debug: print "versionToInt:", release * 10000 + subversion * 100 + patch + return release * 10000 + subversion * 100 + patch + + @classmethod + def is_above_cmssw_version(klass, release=9, subversion=4, patch=0, debug = False): + split_cmssw_version = klass.get_cmssw_version_number() + if klass.versionToInt(release, subversion, patch) > klass.versionToInt(split_cmssw_version[0], split_cmssw_version[1], split_cmssw_version[2]): + if debug: print "is_above_cmssw_version:", False + return False + else: + if debug: print "is_above_cmssw_version:", True + return True + + def loadMVA_WPs_run2_2017(self): + if self.debug: print "loadMVA_WPs_run2_2017: performed" + global cms + for training, gbrForestName in self.tauIdDiscrMVA_trainings_run2_2017.items(): + + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('GBRWrapperRcd'), + tag = self.cms.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), + label = self.cms.untracked.string("RecoTauTag_%s%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) + ) + ) + + for WP in self.tauIdDiscrMVA_WPs_run2_2017[training].keys(): + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('PhysicsTGraphPayloadRcd'), + tag = self.cms.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)), + label = self.cms.untracked.string("RecoTauTag_%s%s_WP%s" % (gbrForestName, self.tauIdDiscrMVA_2017_version, WP)) + ) + ) + + self.process.loadRecoTauTagMVAsFromPrepDB.toGet.append( + self.cms.PSet( + record = self.cms.string('PhysicsTFormulaPayloadRcd'), + tag = self.cms.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)), + label = self.cms.untracked.string("RecoTauTag_%s%s_mvaOutput_normalization" % (gbrForestName, self.tauIdDiscrMVA_2017_version)) + ) + ) + + def runTauID(self, name='NewTauIDsEmbedded'): + self.process.rerunMvaIsolationSequence = self.cms.Sequence() + tauIDSources = self.cms.PSet() + + # rerun the seq to obtain the 2017 nom training with 0.5 iso cone, old DM, ptph>1, trained on 2017MCv1 + if "2017v1" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v1" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + } + # update the list of available in DB samples + if not self.is_above_cmssw_version(10, 0, 0, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v1" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight + ) + + tauIDSources.byIsolationMVArun2017v1DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw') + tauIDSources.byVVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose') + tauIDSources.byLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Loose') + tauIDSources.byMediumIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Medium') + tauIDSources.byTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1Tight') + tauIDSources.byVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VTight') + tauIDSources.byVVTightIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight') + + + if "2017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMwLT2017' : { + 'Eff95' : "DBoldDMwLTEff95", + 'Eff90' : "DBoldDMwLTEff90", + 'Eff80' : "DBoldDMwLTEff80", + 'Eff70' : "DBoldDMwLTEff70", + 'Eff60' : "DBoldDMwLTEff60", + 'Eff50' : "DBoldDMwLTEff50", + 'Eff40' : "DBoldDMwLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight') + + if "newDM2017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBnewDMwLT2017' : "tauIdMVAIsoDBnewDMwLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBnewDMwLT2017' : { + 'Eff95' : "DBnewDMwLTEff95", + 'Eff90' : "DBnewDMwLTEff90", + 'Eff80' : "DBnewDMwLTEff80", + 'Eff70' : "DBnewDMwLTEff70", + 'Eff60' : "DBnewDMwLTEff60", + 'Eff50' : "DBnewDMwLTEff50", + 'Eff40' : "DBnewDMwLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access newDM2017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + mvaOpt = self.cms.string("DBnewDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBnewDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight') + + if "dR0p32017v2" in self.toKeep: + self.tauIdDiscrMVA_2017_version = "v2" + self.tauIdDiscrMVA_trainings_run2_2017 = { + 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : "tauIdMVAIsoDBoldDMdR0p3wLT2017", + } + self.tauIdDiscrMVA_WPs_run2_2017 = { + 'tauIdMVAIsoDBoldDMdR0p3wLT2017' : { + 'Eff95' : "DBoldDMdR0p3wLTEff95", + 'Eff90' : "DBoldDMdR0p3wLTEff90", + 'Eff80' : "DBoldDMdR0p3wLTEff80", + 'Eff70' : "DBoldDMdR0p3wLTEff70", + 'Eff60' : "DBoldDMdR0p3wLTEff60", + 'Eff50' : "DBoldDMdR0p3wLTEff50", + 'Eff40' : "DBoldDMdR0p3wLTEff40" + } + } + + if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access dR0p32017v2" + self.loadMVA_WPs_run2_2017() + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2"), + mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + requireDecayMode = self.cms.bool(True), + srcChargedIsoPtSum = self.cms.string('chargedIsoPtSumdR03'), + srcFootprintCorrection = self.cms.string('footprintCorrectiondR03'), + srcNeutralIsoPtSum = self.cms.string('neutralIsoPtSumdR03'), + srcPhotonPtSumOutsideSignalCone = self.cms.string('photonPtSumOutsideSignalConedR03'), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw:category'),#? + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff90"), #writeTauIdDiscrWPs + variable = self.cms.string("pt"), + ) + ), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff95") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight + *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight + ) + + tauIDSources.byIsolationMVArun2017v2DBoldDMdR0p3wLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw') + tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose') + tauIDSources.byVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose') + tauIDSources.byLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose') + tauIDSources.byMediumIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium') + tauIDSources.byTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight') + tauIDSources.byVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight') + tauIDSources.byVVTightIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight') + + # 2016 training strategy(v2) - essentially the same as 2017 training strategy (v1), trained on 2016MC, old DM - currently not implemented in the tau sequence of any release + # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( + # PATTauProducer = self.cms.InputTag('slimmedTaus'), + # Prediscriminants = noPrediscriminants, + # loadMVAfromDB = self.cms.bool(True), + # mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2"),#RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1 writeTauIdDiscrMVAs + # mvaOpt = self.cms.string("DBoldDMwLTwGJ"), + # requireDecayMode = self.cms.bool(True), + # verbosity = self.cms.int32(0) + # ) + # # + # self.process.rerunDiscriminationByIsolationOldDMMVArun2v2VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + # PATTauProducer = self.cms.InputTag('slimmedTaus'), + # Prediscriminants = noPrediscriminants, + # toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw'), + # key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v2raw:category'),#? + # loadMVAfromDB = self.cms.bool(True), + # mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_mvaOutput_normalization"), #writeTauIdDiscrMVAoutputNormalizations + # mapping = self.cms.VPSet( + # self.cms.PSet( + # category = self.cms.uint32(0), + # cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v2_WPEff90"), #writeTauIdDiscrWPs + # variable = self.cms.string("pt"), + # ) + # ) + # ) + + # 2016 training strategy(v1), trained on 2016MC, old DM + if "2016v1" in self.toKeep: + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1"), + mvaOpt = self.cms.string("DBoldDMwLT"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw:category'), + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_mvaOutput_normalization"), + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff90"), + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff80") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff70") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff60") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff50") + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight + *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight + ) + + tauIDSources.byIsolationMVArun2v1DBoldDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw') + tauIDSources.byVLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VLoose') + tauIDSources.byLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Loose') + tauIDSources.byMediumIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Medium') + tauIDSources.byTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1Tight') + tauIDSources.byVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VTight') + tauIDSources.byVVTightIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VVTight') + + # 2016 training strategy(v1), trained on 2016MC, new DM + if "newDM2016v1" in self.toKeep: + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + loadMVAfromDB = self.cms.bool(True), + mvaName = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1"), + mvaOpt = self.cms.string("DBnewDMwLT"), + requireDecayMode = self.cms.bool(True), + verbosity = self.cms.int32(0) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose = patDiscriminationByIsolationMVArun2v1VLoose.clone( + PATTauProducer = self.cms.InputTag('slimmedTaus'), + Prediscriminants = noPrediscriminants, + toMultiplex = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw'), + key = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw:category'), + loadMVAfromDB = self.cms.bool(True), + mvaOutput_normalization = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_mvaOutput_normalization"), + mapping = self.cms.VPSet( + self.cms.PSet( + category = self.cms.uint32(0), + cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff90"), + variable = self.cms.string("pt"), + ) + ) + ) + + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff80") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff70") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff60") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff50") + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff40") + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight + *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight + ) + + tauIDSources.byIsolationMVArun2v1DBnewDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw') + tauIDSources.byVLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VLoose') + tauIDSources.byLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Loose') + tauIDSources.byMediumIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Medium') + tauIDSources.byTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1Tight') + tauIDSources.byVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VTight') + tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VVTight') + + if "deepTau2017v1" in self.toKeep: + print "Adding DeepTau isolation?" + + from RecoTauTag.RecoTau.DeepTauId_cff import deepTauIdraw + + self.process.deepTauIdraw = deepTauIdraw.clone( + electrons = self.cms.InputTag('slimmedElectrons'), + muons = self.cms.InputTag('slimmedMuons'), + taus = self.cms.InputTag('slimmedTaus'), + graph_file = self.cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') + ) + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.deepTauIdraw + ) + + tauIDSources.deepTau2017v1tauVSe = self.cms.InputTag('deepTauIdraw', 'tauVSe') + tauIDSources.deepTau2017v1tauVSmu = self.cms.InputTag('deepTauIdraw', 'tauVSmu') + tauIDSources.deepTau2017v1tauVSjet = self.cms.InputTag('deepTauIdraw', 'tauVSjet')#$= self.cms.InputTag('deepTauIdrawtauVSjet') + tauIDSources.deepTau2017v1tauVSall = self.cms.InputTag('deepTauIdraw', 'tauVSall') + print("Doing an embedding ") + + if "DPFTau_2016_v0" in self.toKeep: + print "Adding DPF isolation?" + + from RecoTauTag.RecoTau.DPFIsolation_cff import DPFIsolation + + self.process.DPFIsolationv0 = DPFIsolation.clone( + electrons = self.cms.InputTag('slimmedElectrons'), + muons = self.cms.InputTag('slimmedMuons'), + taus = self.cms.InputTag('slimmedTaus'), + graph_file = self.cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') + ) + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.DPFIsolationv0 + ) + + tauIDSources.DPFTau_2016_v0tauVSe = self.cms.InputTag('DPFIsolationv0', 'tauVSe') + tauIDSources.DPFTau_2016_v0tauVSmu = self.cms.InputTag('DPFIsolationv0', 'tauVSmu') + tauIDSources.DPFTau_2016_v0tauVSjet = self.cms.InputTag('DPFIsolationv0', 'tauVSjet') + tauIDSources.DPFTau_2016_v0tauVSall = self.cms.InputTag('DPFIsolationv0', 'tauVSall') + + if "DPFTau_2016_v1" in self.toKeep: + print "Adding DPF isolation?" + + from RecoTauTag.RecoTau.DPFIsolation_cff import DPFIsolation + + self.process.DPFIsolationv1 = DPFIsolation.clone( + electrons = self.cms.InputTag('slimmedElectrons'), + muons = self.cms.InputTag('slimmedMuons'), + taus = self.cms.InputTag('slimmedTaus'), + graph_file = self.cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb') + ) + + self.process.rerunMvaIsolationSequence += self.cms.Sequence( + self.process.DPFIsolationv1 + ) + + tauIDSources.DPFTau_2016_v1tauVSe = self.cms.InputTag('DPFIsolationv1', 'tauVSe') + tauIDSources.DPFTau_2016_v1tauVSmu = self.cms.InputTag('DPFIsolationv1', 'tauVSmu') + tauIDSources.DPFTau_2016_v1tauVSjet = self.cms.InputTag('DPFIsolationv1', 'tauVSjet') + tauIDSources.DPFTau_2016_v1tauVSall = self.cms.InputTag('DPFIsolationv1', 'tauVSall') + + + + embedID = self.cms.EDProducer("PATTauIDEmbedder", + src = self.cms.InputTag('slimmedTaus'), + tauIDSources = tauIDSources + ) + self.process.NewTauIDsEmbedded = embedID From 3c2bab01f0ae4786bcadea6200b1156e9279954a Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 17 Aug 2018 03:40:17 -0500 Subject: [PATCH 08/20] making things fully reproducible --- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index 1fdb053fb660e..7879b5bf28b74 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -155,7 +155,8 @@ std::vector calculate(edm::Handle& taus, edm::HandleRndm(1) > .5){ + int psudorand = p.pt()*1000000; + if (psudorand%2 == 0){ disp = -1; } pfCandDz = 5*disp; From 68ad1d4bd9cbdc6c42963718a17baab246053d1c Mon Sep 17 00:00:00 2001 From: Michal Bluj Date: Tue, 25 Sep 2018 17:57:29 +0200 Subject: [PATCH 09/20] Reorganisation of configuration files: cff split to cfi and cff --- RecoTauTag/RecoTau/python/DPFIsolation_cff.py | 10 +++------- RecoTauTag/RecoTau/python/DPFIsolation_cfi.py | 10 ++++++++++ RecoTauTag/RecoTau/python/DeepTauId_cff.py | 10 +++------- RecoTauTag/RecoTau/python/DeepTauId_cfi.py | 10 ++++++++++ 4 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 RecoTauTag/RecoTau/python/DPFIsolation_cfi.py create mode 100644 RecoTauTag/RecoTau/python/DeepTauId_cfi.py diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py index a2dd1ea570842..5807ea27efd2e 100644 --- a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py +++ b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py @@ -1,11 +1,7 @@ import FWCore.ParameterSet.Config as cms -DPFIsolation = cms.EDProducer("DPFIsolation", - pfcands = cms.InputTag('packedPFCandidates'), - taus = cms.InputTag('slimmedTaus'), - vertices = cms.InputTag('offlineSlimmedPrimaryVertices'), - graph_file = cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') -) +from RecoTauTag.RecoTau.DPFIsolation_cff import * -DPFIsolationSeq = cms.Sequence(DPFIsolation) +DPFIsolationTask = cms.Task(DPFIsolation) +DPFIsolationSeq = cms.Sequence(DPFIsolationTask) diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py b/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py new file mode 100644 index 0000000000000..e612aae100e70 --- /dev/null +++ b/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py @@ -0,0 +1,10 @@ + +import FWCore.ParameterSet.Config as cms + +DPFIsolation = cms.EDProducer("DPFIsolation", + pfcands = cms.InputTag('packedPFCandidates'), + taus = cms.InputTag('slimmedTaus'), + vertices = cms.InputTag('offlineSlimmedPrimaryVertices'), + graph_file = cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') +) + diff --git a/RecoTauTag/RecoTau/python/DeepTauId_cff.py b/RecoTauTag/RecoTau/python/DeepTauId_cff.py index 6d648124ded22..940fd19909835 100644 --- a/RecoTauTag/RecoTau/python/DeepTauId_cff.py +++ b/RecoTauTag/RecoTau/python/DeepTauId_cff.py @@ -1,11 +1,7 @@ import FWCore.ParameterSet.Config as cms -deepTauIdraw = cms.EDProducer("DeepTauId", - electrons = cms.InputTag('slimmedElectrons'), - muons = cms.InputTag('slimmedMuons'), - taus = cms.InputTag('slimmedTaus'), - graph_file = cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') -) +from RecoTauTag.RecoTau.DeepTauId_cfi import * -deepTauIdSeq = cms.Sequence(deepTauIdraw) +deepTauIdTask = cms.Task(deepTauIdraw) +deepTauIdSeq = cms.Sequence(deepTauIdTask) diff --git a/RecoTauTag/RecoTau/python/DeepTauId_cfi.py b/RecoTauTag/RecoTau/python/DeepTauId_cfi.py new file mode 100644 index 0000000000000..95a6e7671f8e7 --- /dev/null +++ b/RecoTauTag/RecoTau/python/DeepTauId_cfi.py @@ -0,0 +1,10 @@ + +import FWCore.ParameterSet.Config as cms + +deepTauIdraw = cms.EDProducer("DeepTauId", + electrons = cms.InputTag('slimmedElectrons'), + muons = cms.InputTag('slimmedMuons'), + taus = cms.InputTag('slimmedTaus'), + graph_file = cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') +) + From e7533a5a5063e7f5912a9e217d664c4ae419fa2f Mon Sep 17 00:00:00 2001 From: Michal Bluj Date: Tue, 25 Sep 2018 17:59:01 +0200 Subject: [PATCH 10/20] Some code cleaning --- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 820 ++++++++++----------- RecoTauTag/RecoTau/plugins/DeepTauId.cc | 23 +- 2 files changed, 417 insertions(+), 426 deletions(-) diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index 7879b5bf28b74..86883fb07a842 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -15,7 +15,7 @@ #include "DataFormats/PatCandidates/interface/Tau.h" #include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" #include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" -#include "TRandom.h" +#include "DataFormats/Math/interface/deltaR.h" inline int getPFCandidateIndex(edm::Handle pfcands, const reco::CandidatePtr cptr){ unsigned int pfInd = -1; @@ -32,14 +32,6 @@ inline int getPFCandidateIndex(edm::Handle pfcan } //_____________________________________________________________________________ -inline double deltaR2(double eta1, double phi1, double eta2, double phi2) { - double deta = eta1 - eta2; - double dphi = deltaPhi(phi1, phi2); - return deta*deta + dphi*dphi; -} -inline double deltaR(double eta1, double phi1, double eta2, double phi2) { - return std::sqrt(deltaR2 (eta1, phi1, eta2, phi2)); -} namespace { @@ -55,426 +47,424 @@ class DPFIsolation : public edm::stream::EDProducer<> { using LorentzVectorXYZ = ROOT::Math::LorentzVector>; using GraphPtr = std::shared_ptr; -std::vector calculate(edm::Handle& taus, edm::Handle & pfcands, edm::Handle & vertices, tensorflow::Tensor tensor, std::vector outputs, tensorflow::Session* session, TRandom * r0) { - - - std::vector predictions; - float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, - pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, - pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, - pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz,pfCandDVx_1,pfCandDVy_1,pfCandDVz_1,pfCandD_1; -// std::cout << " Loading the vertices " << std::endl; -// std::cout << " vertices size = " << vertices->size() << std::endl; -// std::cout << " (*vertices)[0] = " << (*vertices)[0] << std::endl; - float pvx = vertices->size() > 0 ? (*vertices)[0].x() : -1; - float pvy = vertices->size() > 0 ? (*vertices)[0].y() : -1; - float pvz = vertices->size() > 0 ? (*vertices)[0].z() : -1; -// std::cout << "vertices loaded " << std::endl; -// std::cout << " taus->size() = " << taus->size() << std::endl; - bool pfCandIsBarrel; - if (taus->size() == 0){ return predictions;} - for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { -// std::cout << "booting up a tau" << std::endl; - - pat::Tau tau = taus->at(tau_index); - bool isGoodTau = false; - if(tau.isTauIDAvailable("againstMuonLoose3") - and tau.isTauIDAvailable("againstElectronVLooseMVA6") and tau.pt() >= 30 and fabs(tau.eta()) < 2.3) { - isGoodTau = (tau.tauID("againstElectronVLooseMVA6") and tau.tauID("againstMuonLoose3") ); - } - - if (isGoodTau == false) predictions.push_back(-1); - if (isGoodTau == false) continue; - - - std::vector signalCandidateInds; - - for(auto c : tau.signalCands()) - signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c)); - - float lepRecoPt = tau.pt(); - float lepRecoPz = TMath::Abs(tau.pz()); - float lepRecoEt = tau.energy(); - float lepRecoEta = tau.eta(); - tensor.flat().setZero(); - - unsigned int iPF = 0; - - std::vector sorted_inds(pfcands->size()); - std::size_t n(0); - std::generate(std::begin(sorted_inds), std::end(sorted_inds), [&]{ return n++; }); - - std::sort( std::begin(sorted_inds), std::end(sorted_inds), - [&](int i1, int i2) { return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } ); -// std::cout << "tau booted" << std::endl; - - for(size_t pf_index = 0; pf_index < pfcands->size(); pf_index++) { - pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); - float deltaR_tau_p = deltaR(p.eta(),p.phi(),tau.eta(),tau.phi()); -// std::cout << "booting up a pfcanc" << std::endl; - - if (p.pt() < .5) continue; - if (p.fromPV() < 0) continue; - - if (deltaR_tau_p > .5) continue; - - - if (p.fromPV() < 1 and p.charge() != 0) continue; - pfCandPt = p.pt(); - pfCandPtRel = p.pt()/lepRecoPt; - - pfCandDr = deltaR_tau_p; - pfCandDEta = TMath::Abs(tau.eta() - p.eta()); - pfCandDPhi = TMath::Abs(deltaPhi(tau.phi(), p.phi())); - pfCandEta = p.eta(); - pfCandIsBarrel = (TMath::Abs(p.eta()) < 1.4); - pfCandPz = TMath::Abs(TMath::SinH(pfCandEta)*pfCandPt); - pfCandPzRel = TMath::Abs(TMath::SinH(pfCandEta)*pfCandPt)/lepRecoPz; - pfCandPdgID = TMath::Abs(p.pdgId()); - pfCandCharge = p.charge(); - pfCandDVx_1 = p.vx() - pvx; - pfCandDVy_1 = p.vy() - pvy; - pfCandDVz_1 = p.vz() - pvz; -// std::cout << " p.pt() = " << p.pt() ; -// std::cout << " p.vx() = " << p.vx() ; -// std::cout << " p.vy() = " << p.vy(); -// std::cout << " p.vz() = " << p.vz() << std::endl; - - pfCandD_1 = TMath::Sqrt(pfCandDVx_1*pfCandDVx_1 + pfCandDVy_1*pfCandDVy_1 + pfCandDVz_1*pfCandDVz_1); - - - if (pfCandCharge != 0 and p.hasTrackDetails()){ - pfCandDz = p.dz(); - pfCandDzErr = p.dzError(); - pfCandDzSig = (TMath::Abs(p.dz()) + .000001)/(p.dzError() + .00001); - pfCandD0 = p.dxy(); - pfCandD0Err = p.dxyError(); - pfCandD0Sig = (TMath::Abs(p.dxy()) + .000001)/ (p.dxyError() + .00001); - pfCandPixHits = p.numberOfPixelHits(); - pfCandHits = p.numberOfHits(); - pfCandLostInnerHits = p.lostInnerHits(); - } else{ - float disp = 1; - int psudorand = p.pt()*1000000; - if (psudorand%2 == 0){ - disp = -1; - } - pfCandDz = 5*disp; - pfCandDzErr = 0; - pfCandDzSig = 0; - pfCandD0 = 5*disp; - pfCandD0Err = 0; - pfCandD0Sig = 0; - pfCandPixHits = 0; - pfCandHits = 0; - pfCandLostInnerHits = 2.; - pfCandDVx_1= 1; - pfCandDVy_1 = 1; - pfCandDVz_1= 1; - pfCandD_1 = 1; - } - - pfCandPuppiWeight = p.puppiWeight(); - pfCandFromPV = p.fromPV(); - pfCandVtxQuality = p.pvAssociationQuality();//VtxAssocQual(); - pfCandHighPurityTrk = p.trackHighPurity();//HighPurityTrk(); - float pfCandTauIndMatch_temp = 0; - for (auto i : signalCandidateInds){ - if (i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1; - } - - - pfCandTauIndMatch = pfCandTauIndMatch_temp; - pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel; - if (pfCandPt > 500) pfCandPt = 500.; - pfCandPt = pfCandPt/500.; - - if (pfCandPz > 1000) pfCandPz = 1000.; - pfCandPz = pfCandPz/1000.; - - if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.; - if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.; - pfCandPzRel = pfCandPzRel/100.; - pfCandDr = pfCandDr/.5; - pfCandEta = pfCandEta/2.75; - pfCandDEta = pfCandDEta/.5; - pfCandDPhi = pfCandDPhi/.5; - pfCandPixHits = pfCandPixHits/7.; - pfCandHits = pfCandHits/30.; - - if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1; - pfCandPtRelPtRel = pfCandPtRelPtRel; - - if (pfCandD0 > 5.) pfCandD0 = 5.; - if (pfCandD0 < -5.) pfCandD0 = -5.; - pfCandD0 = pfCandD0/5.; - - if (pfCandDz > 5.) pfCandDz = 5.; - if (pfCandDz < -5.) pfCandDz = -5.; - pfCandDz = pfCandDz/5.; - - if (pfCandD0Err > 1.) pfCandD0Err = 1.; - if (pfCandDzErr > 1.) pfCandDzErr = 1.; - if (pfCandDzSig > 3) pfCandDzSig = 3.; - pfCandDzSig = pfCandDzSig/3.; - - if (pfCandD0Sig > 1) pfCandD0Sig = 1.; - pfCandD0D0 = pfCandD0*pfCandD0; - pfCandDzDz = pfCandDz*pfCandDz; - pfCandD0Dz = pfCandD0*pfCandDz; - pfCandD0Dphi = pfCandD0*pfCandDPhi; - - - if (pfCandDVx_1 > .05) pfCandDVx_1 = .05; - if (pfCandDVx_1 < -.05) pfCandDVx_1 = -.05; - pfCandDVx_1 = pfCandDVx_1/.05; - - - if (pfCandDVy_1 > .05) pfCandDVy_1 = .05; - if (pfCandDVy_1 < -.05) pfCandDVy_1 = -.05; - pfCandDVy_1 = pfCandDVy_1/.05; - - - if (pfCandDVz_1 > .05) pfCandDVz_1 = .05; - if (pfCandDVz_1 < -.05) pfCandDVz_1= -.05; - pfCandDVz_1 = pfCandDVz_1/.05; - - if (pfCandD_1 > .1) pfCandD_1 = .1; - if (pfCandD_1 < -.1) pfCandD_1 = -.1; - pfCandD_1 = pfCandD_1/.1; - - -// std::cout << "loading tensor " << std::endl; -// std::cout << " graphName = " << graphName << std::endl; - if (graphName.EndsWith("v0.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb"){ -// std::cout << " Loading a pfcandidate " << std::endl; - tensor.tensor()( 0, 60-1-iPF, 0) = pfCandPt; - tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; - tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; - tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; - tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; - tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; - tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; - tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; - tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; - tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; - tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; - tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; - tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; - tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; - tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; - tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; - tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; - tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; - tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; - tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; - tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; - tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; - tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; - tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; - tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; - tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; - tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; - tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; - tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; - tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; - tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; - tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); - tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); - tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); - tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); - tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); - tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); - tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); - tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; - tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; - tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; - tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; - tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; - tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; - tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; - tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; - tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; - } - - - - if (graphName.EndsWith("v1.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb"){ -// std::cout << " Loading a pfcandidate " << std::endl; - tensor.tensor()( 0, 36-1-iPF, 0) = pfCandPt; - tensor.tensor()( 0, 36-1-iPF, 1) = pfCandPz; - tensor.tensor()( 0, 36-1-iPF, 2) = pfCandPtRel; - tensor.tensor()( 0, 36-1-iPF, 3) = pfCandPzRel; - tensor.tensor()( 0, 36-1-iPF, 4) = pfCandDr; - tensor.tensor()( 0, 36-1-iPF, 5) = pfCandDEta; - tensor.tensor()( 0, 36-1-iPF, 6) = pfCandDPhi; - tensor.tensor()( 0, 36-1-iPF, 7) = pfCandEta; - tensor.tensor()( 0, 36-1-iPF, 8) = pfCandDz; - tensor.tensor()( 0, 36-1-iPF, 9) = pfCandDzSig; - tensor.tensor()( 0, 36-1-iPF, 10) = pfCandD0; - tensor.tensor()( 0, 36-1-iPF, 11) = pfCandD0Sig; - tensor.tensor()( 0, 36-1-iPF, 12) = pfCandDzErr; - tensor.tensor()( 0, 36-1-iPF, 13) = pfCandD0Err; - tensor.tensor()( 0, 36-1-iPF, 14) = pfCandD0D0; - tensor.tensor()( 0, 36-1-iPF, 15) = pfCandCharge==0; - tensor.tensor()( 0, 36-1-iPF, 16) = pfCandCharge==1; - tensor.tensor()( 0, 36-1-iPF, 17) = pfCandCharge==-1; - tensor.tensor()( 0, 36-1-iPF, 18) = pfCandPdgID>22; - tensor.tensor()( 0, 36-1-iPF, 19) = pfCandPdgID==22; - tensor.tensor()( 0, 36-1-iPF, 20) = pfCandDVx_1; - tensor.tensor()( 0, 36-1-iPF, 21) = pfCandDVy_1; - tensor.tensor()( 0, 36-1-iPF, 22) = pfCandDVz_1; - tensor.tensor()( 0, 36-1-iPF, 23) = pfCandD_1; - tensor.tensor()( 0, 36-1-iPF, 24) = pfCandDzDz; - tensor.tensor()( 0, 36-1-iPF, 25) = pfCandD0Dz; - tensor.tensor()( 0, 36-1-iPF, 26) = pfCandD0Dphi; - tensor.tensor()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel; - tensor.tensor()( 0, 36-1-iPF, 28) = pfCandPixHits; - tensor.tensor()( 0, 36-1-iPF, 29) = pfCandHits; - tensor.tensor()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1; - tensor.tensor()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0; - tensor.tensor()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1; - tensor.tensor()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2; - tensor.tensor()( 0, 36-1-iPF, 34) = pfCandPuppiWeight; - tensor.tensor()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1); - tensor.tensor()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5); - tensor.tensor()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6); - tensor.tensor()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7); - tensor.tensor()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1); - tensor.tensor()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2); - tensor.tensor()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3); - tensor.tensor()( 0, 36-1-iPF, 42) = pfCandIsBarrel; - tensor.tensor()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk; - tensor.tensor()( 0, 36-1-iPF, 44) = pfCandPdgID==1; - tensor.tensor()( 0, 36-1-iPF, 45) = pfCandPdgID==2; - tensor.tensor()( 0, 36-1-iPF, 46) = pfCandPdgID==11; - tensor.tensor()( 0, 36-1-iPF, 47) = pfCandPdgID==13; - tensor.tensor()( 0, 36-1-iPF, 48) = pfCandPdgID==130; - tensor.tensor()( 0, 36-1-iPF, 49) = pfCandPdgID==211; - tensor.tensor()( 0, 36-1-iPF, 50) = pfCandTauIndMatch; - } - - iPF++; - if (graphName.EndsWith("v0.pb") and iPF == 60) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; - if (graphName.EndsWith("v1.pb") and iPF == 36) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; - - } -// std::cout << "tensor loaded" << std::endl; -// std::cout << " running predictor " << std::endl; - - tensorflow::Status status = session->Run( { {"input_1", tensor}},{"output_node0"}, {}, &outputs); -// std::cout << " predictor success " << std::endl; - //float output = outputs[0].scalar()(); - tensorflow::TTypes::Flat output = outputs[0].flat(); -// std::cout << "flatting " << std::endl; -// std::cout << " output(0) = " << output(0) << std::endl; -/*if (graphName.EndsWith("v1.pb")){ - for (int iPF =0; iPF < 36; iPF++){ - for (int iVar = 0; iVar < 51; iVar++){ - std::cout << tensor.tensor()(0,iPF,iVar) << ", "; - } - std::cout << std::endl; - }} - std::cout << " (*vertices)[0].x() = " << (*vertices)[0].x() << " (*vertices)[0].y() = " << (*vertices)[0].y() << " (*vertices)[0].z() = " << (*vertices)[0].z() << std::endl; -*/ - predictions.push_back(output(0)); + std::vector calculate(edm::Handle& taus, + edm::Handle& pfcands, + edm::Handle& vertices, + tensorflow::Tensor tensor, + std::vector outputs, + tensorflow::Session* session) { + + + std::vector predictions; + float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, + pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, + pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, + pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz,pfCandDVx_1,pfCandDVy_1,pfCandDVz_1,pfCandD_1; + //std::cout << " Loading the vertices " << std::endl; + //std::cout << " vertices size = " << vertices->size() << std::endl; + //std::cout << " (*vertices)[0] = " << (*vertices)[0] << std::endl; + float pvx = !vertices->empty() ? (*vertices)[0].x() : -1; + float pvy = !vertices->empty() ? (*vertices)[0].y() : -1; + float pvz = !vertices->empty() ? (*vertices)[0].z() : -1; + //std::cout << "vertices loaded " << std::endl; + //std::cout << " taus->size() = " << taus->size() << std::endl; + bool pfCandIsBarrel; + if ( taus->empty() ) return predictions; + for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { + //std::cout << "booting up a tau" << std::endl; + + pat::Tau tau = taus->at(tau_index); + bool isGoodTau = false; + if(tau.pt() >= 30 && std::abs(tau.eta()) < 2.3 && + tau.isTauIDAvailable("againstMuonLoose3") && + tau.isTauIDAvailable("againstElectronVLooseMVA6")) { + + isGoodTau = (tau.tauID("againstElectronVLooseMVA6") && tau.tauID("againstMuonLoose3") ); + } + + if ( !isGoodTau) { + predictions.push_back(-1); + continue; + } + + std::vector signalCandidateInds; + + for(auto c : tau.signalCands()) + signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c)); + + float lepRecoPt = tau.pt(); + float lepRecoPz = std::abs(tau.pz()); + + tensor.flat().setZero(); + + unsigned int iPF = 0; + + std::vector sorted_inds(pfcands->size()); + std::size_t n(0); + std::generate(std::begin(sorted_inds), std::end(sorted_inds), [&]{ return n++; }); + + std::sort(std::begin(sorted_inds), std::end(sorted_inds), + [&](int i1, int i2) { return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } ); + //std::cout << "tau booted" << std::endl; + + for(size_t pf_index = 0; pf_index < pfcands->size(); pf_index++) { + pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); + float deltaR_tau_p = deltaR(p.p4(),tau.p4()); + //std::cout << "booting up a pfcanc" << std::endl; + + if (p.pt() < 0.5) continue; + if (p.fromPV() < 0) continue; + + if (deltaR_tau_p > 0.5) continue; + + + if (p.fromPV() < 1 && p.charge() != 0) continue; + pfCandPt = p.pt(); + pfCandPtRel = p.pt()/lepRecoPt; + + pfCandDr = deltaR_tau_p; + pfCandDEta = std::abs(tau.eta() - p.eta()); + pfCandDPhi = std::abs(deltaPhi(tau.phi(), p.phi())); + pfCandEta = p.eta(); + pfCandIsBarrel = (std::abs(pfCandEta) < 1.4); + pfCandPz = std::abs(std::sinh(pfCandEta)*pfCandPt); + pfCandPzRel = std::abs(std::sinh(pfCandEta)*pfCandPt)/lepRecoPz; + pfCandPdgID = std::abs(p.pdgId()); + pfCandCharge = p.charge(); + pfCandDVx_1 = p.vx() - pvx; + pfCandDVy_1 = p.vy() - pvy; + pfCandDVz_1 = p.vz() - pvz; + //std::cout << " p.pt() = " << p.pt(); + //std::cout << " p.vx() = " << p.vx(); + //std::cout << " p.vy() = " << p.vy(); + //std::cout << " p.vz() = " << p.vz() << std::endl; + + pfCandD_1 = std::sqrt(pfCandDVx_1*pfCandDVx_1 + pfCandDVy_1*pfCandDVy_1 + pfCandDVz_1*pfCandDVz_1); + + if (pfCandCharge != 0 and p.hasTrackDetails()){ + pfCandDz = p.dz(); + pfCandDzErr = p.dzError(); + pfCandDzSig = (std::abs(p.dz()) + 0.000001)/(p.dzError() + 0.00001); + pfCandD0 = p.dxy(); + pfCandD0Err = p.dxyError(); + pfCandD0Sig = (std::abs(p.dxy()) + 0.000001)/ (p.dxyError() + 0.00001); + pfCandPixHits = p.numberOfPixelHits(); + pfCandHits = p.numberOfHits(); + pfCandLostInnerHits = p.lostInnerHits(); + } else { + float disp = 1; + int psudorand = p.pt()*1000000; + if (psudorand%2 == 0) disp = -1; + pfCandDz = 5*disp; + pfCandDzErr = 0; + pfCandDzSig = 0; + pfCandD0 = 5*disp; + pfCandD0Err = 0; + pfCandD0Sig = 0; + pfCandPixHits = 0; + pfCandHits = 0; + pfCandLostInnerHits = 2.; + pfCandDVx_1 = 1; + pfCandDVy_1 = 1; + pfCandDVz_1 = 1; + pfCandD_1 = 1; + } + + pfCandPuppiWeight = p.puppiWeight(); + pfCandFromPV = p.fromPV(); + pfCandVtxQuality = p.pvAssociationQuality();//VtxAssocQual(); + pfCandHighPurityTrk = p.trackHighPurity();//HighPurityTrk(); + float pfCandTauIndMatch_temp = 0; + for (auto i : signalCandidateInds){ + if (i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1; } - return predictions; -}; - struct Output { - std::vector num, den; - Output(std::vector _num, std::vector _den) : num(_num), den(_den) {} - - std::unique_ptr get_value(edm::Handle& taus, std::vector predictions) - { - auto output = std::make_unique(TauRefProd(taus)); + pfCandTauIndMatch = pfCandTauIndMatch_temp; + pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel; + if (pfCandPt > 500) pfCandPt = 500.; + pfCandPt = pfCandPt/500.; + + if (pfCandPz > 1000) pfCandPz = 1000.; + pfCandPz = pfCandPz/1000.; + + if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.; + if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.; + pfCandPzRel = pfCandPzRel/100.; + pfCandDr = pfCandDr/.5; + pfCandEta = pfCandEta/2.75; + pfCandDEta = pfCandDEta/.5; + pfCandDPhi = pfCandDPhi/.5; + pfCandPixHits = pfCandPixHits/7.; + pfCandHits = pfCandHits/30.; + + if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1; + pfCandPtRelPtRel = pfCandPtRelPtRel; + + if (pfCandD0 > 5.) pfCandD0 = 5.; + if (pfCandD0 < -5.) pfCandD0 = -5.; + pfCandD0 = pfCandD0/5.; + + if (pfCandDz > 5.) pfCandDz = 5.; + if (pfCandDz < -5.) pfCandDz = -5.; + pfCandDz = pfCandDz/5.; + + if (pfCandD0Err > 1.) pfCandD0Err = 1.; + if (pfCandDzErr > 1.) pfCandDzErr = 1.; + if (pfCandDzSig > 3) pfCandDzSig = 3.; + pfCandDzSig = pfCandDzSig/3.; + + if (pfCandD0Sig > 1) pfCandD0Sig = 1.; + pfCandD0D0 = pfCandD0*pfCandD0; + pfCandDzDz = pfCandDz*pfCandDz; + pfCandD0Dz = pfCandD0*pfCandDz; + pfCandD0Dphi = pfCandD0*pfCandDPhi; + + if (pfCandDVx_1 > .05) pfCandDVx_1 = .05; + if (pfCandDVx_1 < -.05) pfCandDVx_1 = -.05; + pfCandDVx_1 = pfCandDVx_1/.05; + + if (pfCandDVy_1 > 0.05) pfCandDVy_1 = 0.05; + if (pfCandDVy_1 < -0.05) pfCandDVy_1 = -0.05; + pfCandDVy_1 = pfCandDVy_1/0.05; + + if (pfCandDVz_1 > 0.05) pfCandDVz_1 = 0.05; + if (pfCandDVz_1 < -0.05) pfCandDVz_1= -0.05; + pfCandDVz_1 = pfCandDVz_1/0.05; + + if (pfCandD_1 > 0.1) pfCandD_1 = 0.1; + if (pfCandD_1 < -0.1) pfCandD_1 = -0.1; + pfCandD_1 = pfCandD_1/.1; + + + //std::cout << "loading tensor " << std::endl; + //std::cout << " graphName = " << graphName << std::endl; + if (graphName.EndsWith("v0.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb"){ + //std::cout << " Loading a pfcandidate " << std::endl; + tensor.tensor()( 0, 60-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; + tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; + tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; + tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; + tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; + tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; + tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; + tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); + tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); + tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); + tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; + tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; + tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; + tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; + tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; + tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; + tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; + tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; + tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; + } - for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { - output->setValue(tau_index, predictions.at(tau_index) ); - } -// std::cout << " successfully got an output = " << std::endl; - return output; - } - }; - using OutputCollection = std::map; - static OutputCollection& GetOutputs() - { - static size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; - static OutputCollection outputs = { - { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, - { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, - { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, - { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } - }; - return outputs; - }; - -public: - explicit DPFIsolation(const edm::ParameterSet& cfg) : - taus_token(consumes(cfg.getParameter("taus"))), - pfcand_token(consumes (cfg.getParameter("pfcands"))), - vtx_token(consumes (cfg.getParameter("vertices"))), - graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), - graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), - session(tensorflow::createSession(graph.get())) - { -// std::cout << " The graph file name = " << graphName << std::endl; - for(auto& output_desc : GetOutputs()) - produces(output_desc.first); -//// std::cout << " Running a tau id.. " << std::endl; - if (graphName.EndsWith("v0.pb")) - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); - if (graphName.EndsWith("v1.pb")) - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v1, nfeatures_v1})); + if (graphName.EndsWith("v1.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb"){ + //std::cout << " Loading a pfcandidate " << std::endl; + tensor.tensor()( 0, 36-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 36-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 36-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 36-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 36-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 36-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 36-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 36-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 36-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 36-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 36-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 36-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 36-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 36-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 36-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 36-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 36-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 36-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 36-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 36-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 36-1-iPF, 20) = pfCandDVx_1; + tensor.tensor()( 0, 36-1-iPF, 21) = pfCandDVy_1; + tensor.tensor()( 0, 36-1-iPF, 22) = pfCandDVz_1; + tensor.tensor()( 0, 36-1-iPF, 23) = pfCandD_1; + tensor.tensor()( 0, 36-1-iPF, 24) = pfCandDzDz; + tensor.tensor()( 0, 36-1-iPF, 25) = pfCandD0Dz; + tensor.tensor()( 0, 36-1-iPF, 26) = pfCandD0Dphi; + tensor.tensor()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel; + tensor.tensor()( 0, 36-1-iPF, 28) = pfCandPixHits; + tensor.tensor()( 0, 36-1-iPF, 29) = pfCandHits; + tensor.tensor()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 36-1-iPF, 34) = pfCandPuppiWeight; + tensor.tensor()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1); + tensor.tensor()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2); + tensor.tensor()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3); + tensor.tensor()( 0, 36-1-iPF, 42) = pfCandIsBarrel; + tensor.tensor()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk; + tensor.tensor()( 0, 36-1-iPF, 44) = pfCandPdgID==1; + tensor.tensor()( 0, 36-1-iPF, 45) = pfCandPdgID==2; + tensor.tensor()( 0, 36-1-iPF, 46) = pfCandPdgID==11; + tensor.tensor()( 0, 36-1-iPF, 47) = pfCandPdgID==13; + tensor.tensor()( 0, 36-1-iPF, 48) = pfCandPdgID==130; + tensor.tensor()( 0, 36-1-iPF, 49) = pfCandPdgID==211; + tensor.tensor()( 0, 36-1-iPF, 50) = pfCandTauIndMatch; + } - }; + iPF++; + if (graphName.EndsWith("v0.pb") and iPF == 60) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; + if (graphName.EndsWith("v1.pb") and iPF == 36) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; + + } + //std::cout << "tensor loaded" << std::endl; + //std::cout << " running predictor " << std::endl; + + tensorflow::Status status = session->Run( { {"input_1", tensor}},{"output_node0"}, {}, &outputs); + //std::cout << " predictor success " << std::endl; + //float output = outputs[0].scalar()(); + tensorflow::TTypes::Flat output = outputs[0].flat(); + //std::cout << "flatting " << std::endl; + //std::cout << " output(0) = " << output(0) << std::endl; + /*if (graphName.EndsWith("v1.pb")){ + for (int iPF =0; iPF < 36; iPF++){ + for (int iVar = 0; iVar < 51; iVar++){ + std::cout << tensor.tensor()(0,iPF,iVar) << ", "; + } + std::cout << std::endl; + }} + std::cout << " (*vertices)[0].x() = " << (*vertices)[0].x() << " (*vertices)[0].y() = " << (*vertices)[0].y() << " (*vertices)[0].z() = " << (*vertices)[0].z() << std::endl; + */ + predictions.push_back(output(0)); + } + return predictions; + }; - virtual ~DPFIsolation() override - { - tensorflow::closeSession(session); - }; + struct Output { + std::vector num, den; + Output(std::vector _num, std::vector _den) : num(_num), den(_den) {} - virtual void produce(edm::Event& event, const edm::EventSetup& es) override + std::unique_ptr get_value(edm::Handle& taus, std::vector predictions) { + auto output = std::make_unique(TauRefProd(taus)); - event.getByToken(taus_token, taus); - event.getByToken(pfcand_token, pfcands); - event.getByToken(vtx_token, vertices); - - r0 = new TRandom(0); - std::vector predictions = DPFIsolation::calculate(taus, pfcands, vertices, tensor, outputs, session, r0); - - for(auto& output_desc : GetOutputs()) - event.put(output_desc.second.get_value(taus,predictions), output_desc.first); + for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { + output->setValue(tau_index, predictions.at(tau_index) ); + } + //std::cout << " successfully got an output = " << std::endl; + return output; + } + }; + + using OutputCollection = std::map; + + static OutputCollection& GetOutputs() + { + static size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; + static OutputCollection outputs = { + { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, + { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, + { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, + { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } }; + return outputs; + }; -private: - edm::EDGetTokenT taus_token; - edm::EDGetTokenT pfcand_token; - edm::EDGetTokenT vtx_token; - - edm::Handle taus; - edm::Handle pfcands; - edm::Handle vertices; - - TString graphName; - GraphPtr graph; - tensorflow::Session* session; - TRandom * r0; - static const unsigned int nparticles_v0 = 60; - static const unsigned int nfeatures_v0 = 47; - - static const unsigned int nparticles_v1 = 36; - static const unsigned int nfeatures_v1 = 51; - - tensorflow::Tensor tensor; - std::vector outputs; +public: + explicit DPFIsolation(const edm::ParameterSet& cfg) : + taus_token(consumes(cfg.getParameter("taus"))), + pfcand_token(consumes(cfg.getParameter("pfcands"))), + vtx_token(consumes(cfg.getParameter("vertices"))), + graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), + graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), + session(tensorflow::createSession(graph.get())) + { + //std::cout << " The graph file name = " << graphName << std::endl; + for(auto& output_desc : GetOutputs()) + produces(output_desc.first); + //std::cout << " Running a tau id.. " << std::endl; + if (graphName.EndsWith("v0.pb")) + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); + if (graphName.EndsWith("v1.pb")) + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v1, nfeatures_v1})); + + }; + + virtual ~DPFIsolation() override + { + tensorflow::closeSession(session); + }; + + virtual void produce(edm::Event& event, const edm::EventSetup& es) override + { + + event.getByToken(taus_token, taus); + event.getByToken(pfcand_token, pfcands); + event.getByToken(vtx_token, vertices); + + std::vector predictions = DPFIsolation::calculate(taus, pfcands, vertices, tensor, outputs, session); + + for(auto& output_desc : GetOutputs()) + event.put(output_desc.second.get_value(taus,predictions), output_desc.first); + }; +private: + edm::EDGetTokenT taus_token; + edm::EDGetTokenT pfcand_token; + edm::EDGetTokenT vtx_token; + + edm::Handle taus; + edm::Handle pfcands; + edm::Handle vertices; + + TString graphName; + GraphPtr graph; + tensorflow::Session* session; + static const unsigned int nparticles_v0 = 60; + static const unsigned int nfeatures_v0 = 47; + + static const unsigned int nparticles_v1 = 36; + static const unsigned int nfeatures_v1 = 51; + + tensorflow::Tensor tensor; + std::vector outputs; }; -}; +} + #include "FWCore/Framework/interface/MakerMacros.h" DEFINE_FWK_MODULE(DPFIsolation); diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 40782be7cfafd..42878c52628c5 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -15,6 +15,7 @@ #include "DataFormats/PatCandidates/interface/Tau.h" #include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" #include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" +#include "DataFormats/Math/interface/deltaR.h" namespace { @@ -85,7 +86,7 @@ float dPhi(const LVector1& p4, const LVector2& tau_p4) } namespace MuonSubdetId { -enum { DT = 1, CSC = 2, RPC = 3, GEM = 4, ME0 = 5 }; + enum { DT = 1, CSC = 2, RPC = 3, GEM = 4, ME0 = 5 }; } struct MuonHitMatch { @@ -111,10 +112,10 @@ struct MuonHitMatch { static constexpr int n_stations = 4; ++n_muons; - const double dR2 = ROOT::Math::VectorUtil::DeltaR2(tau.p4(), muon.p4()); + const double dR2 = reco::deltaR2(tau.p4(), muon.p4()); if(!best_matched_muon || dR2 < deltaR2_best_match) { - best_matched_muon = &muon; - deltaR2_best_match = dR2; + best_matched_muon = &muon; + deltaR2_best_match = dR2; } for(const auto& segment : muon.matches()) { @@ -156,12 +157,12 @@ struct MuonHitMatch { if(tau.leadPFChargedHadrCand().isNonnull() && tau.leadPFChargedHadrCand()->muonRef().isNonnull()) hadr_cand_muon = tau.leadPFChargedHadrCand()->muonRef().get(); std::vector matched_muons; - const double deltaR2 = std::pow(deltaR, 2); + const double dR2 = deltaR*deltaR; for(const pat::Muon& muon : muons) { const reco::Muon* reco_muon = &muon; if(muon.pt() <= minPt) continue; if(reco_muon == hadr_cand_muon) continue; - if(ROOT::Math::VectorUtil::DeltaR2(tau.p4(), muon.p4()) >= deltaR2) continue; + if(reco::deltaR2(tau.p4(), muon.p4()) >= dR2) continue; matched_muons.push_back(&muon); } return matched_muons; @@ -564,7 +565,7 @@ class DeepTauId : public edm::stream::EDProducer<> { const double innerSigCone_radius = GetInnerSignalConeRadius(tau.pt()); for(const auto& cand : candidates) { - const double dR = ROOT::Math::VectorUtil::DeltaR(cand->p4(), tau.leadChargedHadrCand()->p4()); + const double dR = reco::deltaR(cand->p4(), tau.leadChargedHadrCand()->p4()); const bool isInside_innerSigCone = dR < innerSigCone_radius; if(isInside_innerSigCone) { p4_inner += cand->p4(); @@ -638,12 +639,12 @@ class DeepTauId : public edm::stream::EDProducer<> { static const pat::Electron* FindMatchedElectron(const pat::Tau& tau, const pat::ElectronCollection& electrons, double deltaR) { - const double deltaR2 = std::pow(deltaR, 2); + const double dR2 = deltaR*deltaR; const pat::Electron* matched_ele = nullptr; for(const auto& ele : electrons) { - if(ROOT::Math::VectorUtil::DeltaR2(tau.p4(), ele.p4()) < deltaR2 && - (!matched_ele || matched_ele->pt() < ele.pt())) { - matched_ele = &ele; + if(reco::deltaR2(tau.p4(), ele.p4()) < dR2 && + (!matched_ele || matched_ele->pt() < ele.pt())) { + matched_ele = &ele; } } return matched_ele; From eaa1073dbc9d8097b47f10d0eac6fff6ad35badb Mon Sep 17 00:00:00 2001 From: Michal Bluj Date: Wed, 26 Sep 2018 10:43:53 +0200 Subject: [PATCH 11/20] adapt to cfi/cff reorganization --- RecoTauTag/RecoTau/python/runTauIdMVA.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/RecoTauTag/RecoTau/python/runTauIdMVA.py b/RecoTauTag/RecoTau/python/runTauIdMVA.py index f3bf04a9ea134..edbba9d974f2a 100644 --- a/RecoTauTag/RecoTau/python/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/runTauIdMVA.py @@ -574,7 +574,7 @@ def runTauID(self, name='NewTauIDsEmbedded'): if "deepTau2017v1" in self.toKeep: print "Adding DeepTau isolation?" - from RecoTauTag.RecoTau.DeepTauId_cff import deepTauIdraw + from RecoTauTag.RecoTau.DeepTauId_cfi import deepTauIdraw self.process.deepTauIdraw = deepTauIdraw.clone( electrons = self.cms.InputTag('slimmedElectrons'), @@ -596,7 +596,7 @@ def runTauID(self, name='NewTauIDsEmbedded'): if "DPFTau_2016_v0" in self.toKeep: print "Adding DPF isolation?" - from RecoTauTag.RecoTau.DPFIsolation_cff import DPFIsolation + from RecoTauTag.RecoTau.DPFIsolation_cfi import DPFIsolation self.process.DPFIsolationv0 = DPFIsolation.clone( electrons = self.cms.InputTag('slimmedElectrons'), @@ -617,7 +617,7 @@ def runTauID(self, name='NewTauIDsEmbedded'): if "DPFTau_2016_v1" in self.toKeep: print "Adding DPF isolation?" - from RecoTauTag.RecoTau.DPFIsolation_cff import DPFIsolation + from RecoTauTag.RecoTau.DPFIsolation_cfi import DPFIsolation self.process.DPFIsolationv1 = DPFIsolation.clone( electrons = self.cms.InputTag('slimmedElectrons'), From da38a3a123ddf8306262acdd3639cbb635bf5616 Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Fri, 19 Oct 2018 12:48:00 +0200 Subject: [PATCH 12/20] Review of DPF and DeepTauId code. - Defined base class for deep tau discriminators. - Removed weight files from home cms repository. Now using weights from cms-data. - Defined WP for both discriminators. Now all discriminators return the corresponding WP results. - Removed cfi files. Using fillDescriptions instead. - General code review and cleaning. --- RecoTauTag/RecoTau/BuildFile.xml | 1 + RecoTauTag/RecoTau/interface/DeepTauBase.h | 81 ++ RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 812 ++++++++---------- RecoTauTag/RecoTau/plugins/DeepTauId.cc | 109 +-- RecoTauTag/RecoTau/python/DPFIsolation_cfi.py | 10 - RecoTauTag/RecoTau/python/DeepTauId_cfi.py | 10 - RecoTauTag/RecoTau/python/runTauIdMVA.py | 128 ++- RecoTauTag/RecoTau/src/DeepTauBase.cc | 82 ++ 8 files changed, 652 insertions(+), 581 deletions(-) create mode 100644 RecoTauTag/RecoTau/interface/DeepTauBase.h delete mode 100644 RecoTauTag/RecoTau/python/DPFIsolation_cfi.py delete mode 100644 RecoTauTag/RecoTau/python/DeepTauId_cfi.py create mode 100644 RecoTauTag/RecoTau/src/DeepTauBase.cc diff --git a/RecoTauTag/RecoTau/BuildFile.xml b/RecoTauTag/RecoTau/BuildFile.xml index f58047980c27a..21ff7471a4dcd 100644 --- a/RecoTauTag/RecoTau/BuildFile.xml +++ b/RecoTauTag/RecoTau/BuildFile.xml @@ -27,6 +27,7 @@ + diff --git a/RecoTauTag/RecoTau/interface/DeepTauBase.h b/RecoTauTag/RecoTau/interface/DeepTauBase.h new file mode 100644 index 0000000000000..dde15ef7044ad --- /dev/null +++ b/RecoTauTag/RecoTau/interface/DeepTauBase.h @@ -0,0 +1,81 @@ +#ifndef RecoTauTag_RecoTau_DeepTauBase_h +#define RecoTauTag_RecoTau_DeepTauBase_h + +/* + * \class DeepTauBase + * + * Definition of the base class for tau identification using Deep NN. + * + * \author Konstantin Androsov, INFN Pisa + * \author Maria Rosaria Di Domenico, University of Siena & INFN Pisa + */ + +#include +#include "FWCore/Framework/interface/stream/EDProducer.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" +#include "DataFormats/PatCandidates/interface/Electron.h" +#include "DataFormats/PatCandidates/interface/Muon.h" +#include "DataFormats/PatCandidates/interface/Tau.h" +#include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" +#include "CommonTools/Utils/interface/StringObjectFunction.h" +#include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" +#include "FWCore/ParameterSet/interface/ParameterSetDescription.h" + +namespace deep_tau { + +class DeepTauBase : public edm::stream::EDProducer<> { +public: + using TauType = pat::Tau; + using TauDiscriminator = pat::PATTauDiscriminator; + using TauCollection = std::vector; + using TauRef = edm::Ref; + using TauRefProd = edm::RefProd; + using ElectronCollection = pat::ElectronCollection; + using MuonCollection = pat::MuonCollection; + using LorentzVectorXYZ = ROOT::Math::LorentzVector>; + using GraphPtr = std::shared_ptr; + using Cutter = StringObjectFunction; + using CutterPtr = std::unique_ptr; + using WPMap = std::map; + + + struct Output { + using ResultMap = std::map>; + std::vector num, den; + + Output(const std::vector& _num, const std::vector& _den) : num(_num), den(_den) {} + + ResultMap get_value(const edm::Handle& taus, const tensorflow::Tensor& pred, + const WPMap& working_points) const; + }; + + using OutputCollection = std::map; + + + DeepTauBase(const edm::ParameterSet& cfg, const OutputCollection& outputs); + virtual ~DeepTauBase(); + + virtual void produce(edm::Event& event, const edm::EventSetup& es) override; + +private: + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) = 0; + virtual void CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred); + +protected: + edm::EDGetTokenT taus_token; + edm::Handle taus; + std::string graphName; + GraphPtr graph; + tensorflow::Session* session; + std::map working_points; + OutputCollection outputs; + +}; + +} // namespace deep_tau + + + +#endif diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index 86883fb07a842..5b7cab8aa6692 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -1,470 +1,392 @@ /* * \class DPFIsolation * - * Tau identification using Deep NN + * Deep ParticleFlow tau isolation using Deep NN. * - * \author Konstantin Androsov, INFN Pisa + * \author Owen Colegrove, UCSB */ -#include -#include "FWCore/Framework/interface/stream/EDProducer.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" -#include "DataFormats/PatCandidates/interface/Electron.h" -#include "DataFormats/PatCandidates/interface/Muon.h" -#include "DataFormats/PatCandidates/interface/Tau.h" -#include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" -#include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" -#include "DataFormats/Math/interface/deltaR.h" - -inline int getPFCandidateIndex(edm::Handle pfcands, const reco::CandidatePtr cptr){ - unsigned int pfInd = -1; - for(unsigned int i = 0; i < pfcands->size(); ++i) { -// const pat::PackedCandidate &pf = (*pfcands)[i]; -// if(pf.pt() < candptMin_) continue; - pfInd++; - if(reco::CandidatePtr(pfcands,i) == cptr) { - pfInd = i; - break; +#include "RecoTauTag/RecoTau/interface/DeepTauBase.h" + +namespace { +inline int getPFCandidateIndex(const edm::Handle& pfcands, + const reco::CandidatePtr& cptr) +{ + unsigned int pfInd = -1; + for(unsigned int i = 0; i < pfcands->size(); ++i) { + pfInd++; + if(reco::CandidatePtr(pfcands,i) == cptr) { + pfInd = i; + break; + } } - } - return pfInd; + return pfInd; } +} // anonymous namespace -//_____________________________________________________________________________ -namespace { - -class DPFIsolation : public edm::stream::EDProducer<> { +class DPFIsolation : public deep_tau::DeepTauBase { public: - using TauType = pat::Tau; - using TauDiscriminator = pat::PATTauDiscriminator; - using TauCollection = std::vector; - using TauRef = edm::Ref; - using TauRefProd = edm::RefProd; - using ElectronCollection = pat::ElectronCollection; - using MuonCollection = pat::MuonCollection; - using LorentzVectorXYZ = ROOT::Math::LorentzVector>; - using GraphPtr = std::shared_ptr; - - std::vector calculate(edm::Handle& taus, - edm::Handle& pfcands, - edm::Handle& vertices, - tensorflow::Tensor tensor, - std::vector outputs, - tensorflow::Session* session) { - - - std::vector predictions; - float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, - pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, - pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, - pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig,pfCandD0Err,pfCandPtRelPtRel,pfCandDzDz,pfCandDVx_1,pfCandDVy_1,pfCandDVz_1,pfCandD_1; - //std::cout << " Loading the vertices " << std::endl; - //std::cout << " vertices size = " << vertices->size() << std::endl; - //std::cout << " (*vertices)[0] = " << (*vertices)[0] << std::endl; - float pvx = !vertices->empty() ? (*vertices)[0].x() : -1; - float pvy = !vertices->empty() ? (*vertices)[0].y() : -1; - float pvz = !vertices->empty() ? (*vertices)[0].z() : -1; - //std::cout << "vertices loaded " << std::endl; - //std::cout << " taus->size() = " << taus->size() << std::endl; - bool pfCandIsBarrel; - if ( taus->empty() ) return predictions; - for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { - //std::cout << "booting up a tau" << std::endl; - - pat::Tau tau = taus->at(tau_index); - bool isGoodTau = false; - if(tau.pt() >= 30 && std::abs(tau.eta()) < 2.3 && - tau.isTauIDAvailable("againstMuonLoose3") && - tau.isTauIDAvailable("againstElectronVLooseMVA6")) { - - isGoodTau = (tau.tauID("againstElectronVLooseMVA6") && tau.tauID("againstMuonLoose3") ); - } - - if ( !isGoodTau) { - predictions.push_back(-1); - continue; - } - - std::vector signalCandidateInds; - - for(auto c : tau.signalCands()) - signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c)); - - float lepRecoPt = tau.pt(); - float lepRecoPz = std::abs(tau.pz()); - - tensor.flat().setZero(); - - unsigned int iPF = 0; - - std::vector sorted_inds(pfcands->size()); - std::size_t n(0); - std::generate(std::begin(sorted_inds), std::end(sorted_inds), [&]{ return n++; }); - - std::sort(std::begin(sorted_inds), std::end(sorted_inds), - [&](int i1, int i2) { return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } ); - //std::cout << "tau booted" << std::endl; - - for(size_t pf_index = 0; pf_index < pfcands->size(); pf_index++) { - pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); - float deltaR_tau_p = deltaR(p.p4(),tau.p4()); - //std::cout << "booting up a pfcanc" << std::endl; - - if (p.pt() < 0.5) continue; - if (p.fromPV() < 0) continue; - - if (deltaR_tau_p > 0.5) continue; - - - if (p.fromPV() < 1 && p.charge() != 0) continue; - pfCandPt = p.pt(); - pfCandPtRel = p.pt()/lepRecoPt; - - pfCandDr = deltaR_tau_p; - pfCandDEta = std::abs(tau.eta() - p.eta()); - pfCandDPhi = std::abs(deltaPhi(tau.phi(), p.phi())); - pfCandEta = p.eta(); - pfCandIsBarrel = (std::abs(pfCandEta) < 1.4); - pfCandPz = std::abs(std::sinh(pfCandEta)*pfCandPt); - pfCandPzRel = std::abs(std::sinh(pfCandEta)*pfCandPt)/lepRecoPz; - pfCandPdgID = std::abs(p.pdgId()); - pfCandCharge = p.charge(); - pfCandDVx_1 = p.vx() - pvx; - pfCandDVy_1 = p.vy() - pvy; - pfCandDVz_1 = p.vz() - pvz; - //std::cout << " p.pt() = " << p.pt(); - //std::cout << " p.vx() = " << p.vx(); - //std::cout << " p.vy() = " << p.vy(); - //std::cout << " p.vz() = " << p.vz() << std::endl; - - pfCandD_1 = std::sqrt(pfCandDVx_1*pfCandDVx_1 + pfCandDVy_1*pfCandDVy_1 + pfCandDVz_1*pfCandDVz_1); - - if (pfCandCharge != 0 and p.hasTrackDetails()){ - pfCandDz = p.dz(); - pfCandDzErr = p.dzError(); - pfCandDzSig = (std::abs(p.dz()) + 0.000001)/(p.dzError() + 0.00001); - pfCandD0 = p.dxy(); - pfCandD0Err = p.dxyError(); - pfCandD0Sig = (std::abs(p.dxy()) + 0.000001)/ (p.dxyError() + 0.00001); - pfCandPixHits = p.numberOfPixelHits(); - pfCandHits = p.numberOfHits(); - pfCandLostInnerHits = p.lostInnerHits(); - } else { - float disp = 1; - int psudorand = p.pt()*1000000; - if (psudorand%2 == 0) disp = -1; - pfCandDz = 5*disp; - pfCandDzErr = 0; - pfCandDzSig = 0; - pfCandD0 = 5*disp; - pfCandD0Err = 0; - pfCandD0Sig = 0; - pfCandPixHits = 0; - pfCandHits = 0; - pfCandLostInnerHits = 2.; - pfCandDVx_1 = 1; - pfCandDVy_1 = 1; - pfCandDVz_1 = 1; - pfCandD_1 = 1; - } - - pfCandPuppiWeight = p.puppiWeight(); - pfCandFromPV = p.fromPV(); - pfCandVtxQuality = p.pvAssociationQuality();//VtxAssocQual(); - pfCandHighPurityTrk = p.trackHighPurity();//HighPurityTrk(); - float pfCandTauIndMatch_temp = 0; - for (auto i : signalCandidateInds){ - if (i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1; - } - - pfCandTauIndMatch = pfCandTauIndMatch_temp; - pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel; - if (pfCandPt > 500) pfCandPt = 500.; - pfCandPt = pfCandPt/500.; - - if (pfCandPz > 1000) pfCandPz = 1000.; - pfCandPz = pfCandPz/1000.; - - if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.; - if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.; - pfCandPzRel = pfCandPzRel/100.; - pfCandDr = pfCandDr/.5; - pfCandEta = pfCandEta/2.75; - pfCandDEta = pfCandDEta/.5; - pfCandDPhi = pfCandDPhi/.5; - pfCandPixHits = pfCandPixHits/7.; - pfCandHits = pfCandHits/30.; - - if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1; - pfCandPtRelPtRel = pfCandPtRelPtRel; - - if (pfCandD0 > 5.) pfCandD0 = 5.; - if (pfCandD0 < -5.) pfCandD0 = -5.; - pfCandD0 = pfCandD0/5.; - - if (pfCandDz > 5.) pfCandDz = 5.; - if (pfCandDz < -5.) pfCandDz = -5.; - pfCandDz = pfCandDz/5.; - - if (pfCandD0Err > 1.) pfCandD0Err = 1.; - if (pfCandDzErr > 1.) pfCandDzErr = 1.; - if (pfCandDzSig > 3) pfCandDzSig = 3.; - pfCandDzSig = pfCandDzSig/3.; - - if (pfCandD0Sig > 1) pfCandD0Sig = 1.; - pfCandD0D0 = pfCandD0*pfCandD0; - pfCandDzDz = pfCandDz*pfCandDz; - pfCandD0Dz = pfCandD0*pfCandDz; - pfCandD0Dphi = pfCandD0*pfCandDPhi; - - if (pfCandDVx_1 > .05) pfCandDVx_1 = .05; - if (pfCandDVx_1 < -.05) pfCandDVx_1 = -.05; - pfCandDVx_1 = pfCandDVx_1/.05; - - if (pfCandDVy_1 > 0.05) pfCandDVy_1 = 0.05; - if (pfCandDVy_1 < -0.05) pfCandDVy_1 = -0.05; - pfCandDVy_1 = pfCandDVy_1/0.05; - - if (pfCandDVz_1 > 0.05) pfCandDVz_1 = 0.05; - if (pfCandDVz_1 < -0.05) pfCandDVz_1= -0.05; - pfCandDVz_1 = pfCandDVz_1/0.05; - - if (pfCandD_1 > 0.1) pfCandD_1 = 0.1; - if (pfCandD_1 < -0.1) pfCandD_1 = -0.1; - pfCandD_1 = pfCandD_1/.1; - - - //std::cout << "loading tensor " << std::endl; - //std::cout << " graphName = " << graphName << std::endl; - if (graphName.EndsWith("v0.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb"){ - //std::cout << " Loading a pfcandidate " << std::endl; - tensor.tensor()( 0, 60-1-iPF, 0) = pfCandPt; - tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; - tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; - tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; - tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; - tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; - tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; - tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; - tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; - tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; - tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; - tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; - tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; - tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; - tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; - tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; - tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; - tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; - tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; - tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; - tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; - tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; - tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; - tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; - tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; - tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; - tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; - tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; - tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; - tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; - tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; - tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); - tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); - tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); - tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); - tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); - tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); - tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); - tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; - tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; - tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; - tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; - tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; - tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; - tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; - tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; - tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; - } - - - - if (graphName.EndsWith("v1.pb")){// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb"){ - //std::cout << " Loading a pfcandidate " << std::endl; - tensor.tensor()( 0, 36-1-iPF, 0) = pfCandPt; - tensor.tensor()( 0, 36-1-iPF, 1) = pfCandPz; - tensor.tensor()( 0, 36-1-iPF, 2) = pfCandPtRel; - tensor.tensor()( 0, 36-1-iPF, 3) = pfCandPzRel; - tensor.tensor()( 0, 36-1-iPF, 4) = pfCandDr; - tensor.tensor()( 0, 36-1-iPF, 5) = pfCandDEta; - tensor.tensor()( 0, 36-1-iPF, 6) = pfCandDPhi; - tensor.tensor()( 0, 36-1-iPF, 7) = pfCandEta; - tensor.tensor()( 0, 36-1-iPF, 8) = pfCandDz; - tensor.tensor()( 0, 36-1-iPF, 9) = pfCandDzSig; - tensor.tensor()( 0, 36-1-iPF, 10) = pfCandD0; - tensor.tensor()( 0, 36-1-iPF, 11) = pfCandD0Sig; - tensor.tensor()( 0, 36-1-iPF, 12) = pfCandDzErr; - tensor.tensor()( 0, 36-1-iPF, 13) = pfCandD0Err; - tensor.tensor()( 0, 36-1-iPF, 14) = pfCandD0D0; - tensor.tensor()( 0, 36-1-iPF, 15) = pfCandCharge==0; - tensor.tensor()( 0, 36-1-iPF, 16) = pfCandCharge==1; - tensor.tensor()( 0, 36-1-iPF, 17) = pfCandCharge==-1; - tensor.tensor()( 0, 36-1-iPF, 18) = pfCandPdgID>22; - tensor.tensor()( 0, 36-1-iPF, 19) = pfCandPdgID==22; - tensor.tensor()( 0, 36-1-iPF, 20) = pfCandDVx_1; - tensor.tensor()( 0, 36-1-iPF, 21) = pfCandDVy_1; - tensor.tensor()( 0, 36-1-iPF, 22) = pfCandDVz_1; - tensor.tensor()( 0, 36-1-iPF, 23) = pfCandD_1; - tensor.tensor()( 0, 36-1-iPF, 24) = pfCandDzDz; - tensor.tensor()( 0, 36-1-iPF, 25) = pfCandD0Dz; - tensor.tensor()( 0, 36-1-iPF, 26) = pfCandD0Dphi; - tensor.tensor()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel; - tensor.tensor()( 0, 36-1-iPF, 28) = pfCandPixHits; - tensor.tensor()( 0, 36-1-iPF, 29) = pfCandHits; - tensor.tensor()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1; - tensor.tensor()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0; - tensor.tensor()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1; - tensor.tensor()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2; - tensor.tensor()( 0, 36-1-iPF, 34) = pfCandPuppiWeight; - tensor.tensor()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1); - tensor.tensor()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5); - tensor.tensor()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6); - tensor.tensor()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7); - tensor.tensor()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1); - tensor.tensor()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2); - tensor.tensor()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3); - tensor.tensor()( 0, 36-1-iPF, 42) = pfCandIsBarrel; - tensor.tensor()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk; - tensor.tensor()( 0, 36-1-iPF, 44) = pfCandPdgID==1; - tensor.tensor()( 0, 36-1-iPF, 45) = pfCandPdgID==2; - tensor.tensor()( 0, 36-1-iPF, 46) = pfCandPdgID==11; - tensor.tensor()( 0, 36-1-iPF, 47) = pfCandPdgID==13; - tensor.tensor()( 0, 36-1-iPF, 48) = pfCandPdgID==130; - tensor.tensor()( 0, 36-1-iPF, 49) = pfCandPdgID==211; - tensor.tensor()( 0, 36-1-iPF, 50) = pfCandTauIndMatch; - } - - iPF++; - if (graphName.EndsWith("v0.pb") and iPF == 60) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; - if (graphName.EndsWith("v1.pb") and iPF == 36) break;// == "RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb" and iPF == 60) break; - - } - //std::cout << "tensor loaded" << std::endl; - //std::cout << " running predictor " << std::endl; - - tensorflow::Status status = session->Run( { {"input_1", tensor}},{"output_node0"}, {}, &outputs); - //std::cout << " predictor success " << std::endl; - //float output = outputs[0].scalar()(); - tensorflow::TTypes::Flat output = outputs[0].flat(); - //std::cout << "flatting " << std::endl; - //std::cout << " output(0) = " << output(0) << std::endl; - /*if (graphName.EndsWith("v1.pb")){ - for (int iPF =0; iPF < 36; iPF++){ - for (int iVar = 0; iVar < 51; iVar++){ - std::cout << tensor.tensor()(0,iPF,iVar) << ", "; - } - std::cout << std::endl; - }} - std::cout << " (*vertices)[0].x() = " << (*vertices)[0].x() << " (*vertices)[0].y() = " << (*vertices)[0].y() << " (*vertices)[0].z() = " << (*vertices)[0].z() << std::endl; - */ - predictions.push_back(output(0)); + static OutputCollection& GetOutputs() + { + static size_t tau_index = 0; + static OutputCollection outputs = { { "VSall", Output({tau_index}, {}) } }; + return outputs; + }; + + static unsigned GetNumberOfParticles(unsigned graphVersion) + { + static const std::map nparticles { { 0, 60 }, { 1, 36 } }; + return nparticles.at(graphVersion); } - return predictions; - }; - struct Output { - std::vector num, den; - Output(std::vector _num, std::vector _den) : num(_num), den(_den) {} + static unsigned GetNumberOfFeatures(unsigned graphVersion) + { + static const std::map nfeatures { { 0, 47 }, { 1, 51 } }; + return nfeatures.at(graphVersion); + } - std::unique_ptr get_value(edm::Handle& taus, std::vector predictions) + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) { - auto output = std::make_unique(TauRefProd(taus)); + edm::ParameterSetDescription desc; + desc.add("pfcands", edm::InputTag("packedPFCandidates")); + desc.add("taus", edm::InputTag("slimmedTaus")); + desc.add("vertices", edm::InputTag("offlineSlimmedPrimaryVertices")); + desc.add("graph_file", "RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v0.pb"); + + edm::ParameterSetDescription descWP; + descWP.add("VVVLoose", "0"); + descWP.add("VVLoose", "0"); + descWP.add("VLoose", "0"); + descWP.add("Loose", "0"); + descWP.add("Medium", "0"); + descWP.add("Tight", "0"); + descWP.add("VTight", "0"); + descWP.add("VVTight", "0"); + descWP.add("VVVTight", "0"); + desc.add("VSallWP", descWP); + descriptions.add("DPFTau2016v0", desc); + } - for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { - output->setValue(tau_index, predictions.at(tau_index) ); - } - //std::cout << " successfully got an output = " << std::endl; - return output; + explicit DPFIsolation(const edm::ParameterSet& cfg) : + DeepTauBase(cfg, GetOutputs()), + pfcand_token(consumes(cfg.getParameter("pfcands"))), + vtx_token(consumes(cfg.getParameter("vertices"))) + { + if(graphName.find("v0.pb") != std::string::npos) + graphVersion = 0; + else if(graphName.find("v1.pb") != std::string::npos) + graphVersion = 1; + else + throw cms::Exception("DPFIsolation") << "unknown version of the graph file."; + + tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, {1, + static_cast(GetNumberOfParticles(graphVersion)), static_cast(GetNumberOfFeatures(graphVersion))}); } - }; - - using OutputCollection = std::map; - - static OutputCollection& GetOutputs() - { - static size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; - static OutputCollection outputs = { - { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, - { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, - { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, - { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } - }; - return outputs; - }; -public: - explicit DPFIsolation(const edm::ParameterSet& cfg) : - taus_token(consumes(cfg.getParameter("taus"))), - pfcand_token(consumes(cfg.getParameter("pfcands"))), - vtx_token(consumes(cfg.getParameter("vertices"))), - graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), - graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), - session(tensorflow::createSession(graph.get())) - { - //std::cout << " The graph file name = " << graphName << std::endl; - for(auto& output_desc : GetOutputs()) - produces(output_desc.first); - //std::cout << " Running a tau id.. " << std::endl; - if (graphName.EndsWith("v0.pb")) - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v0, nfeatures_v0})); - if (graphName.EndsWith("v1.pb")) - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape( {1, nparticles_v1, nfeatures_v1})); - - }; - - virtual ~DPFIsolation() override - { - tensorflow::closeSession(session); - }; - - virtual void produce(edm::Event& event, const edm::EventSetup& es) override - { - - event.getByToken(taus_token, taus); - event.getByToken(pfcand_token, pfcands); - event.getByToken(vtx_token, vertices); - - std::vector predictions = DPFIsolation::calculate(taus, pfcands, vertices, tensor, outputs, session); - - for(auto& output_desc : GetOutputs()) - event.put(output_desc.second.get_value(taus,predictions), output_desc.first); - }; +private: + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) override + { + event.getByToken(pfcand_token, pfcands); + event.getByToken(vtx_token, vertices); + + tensorflow::Tensor predictions(tensorflow::DT_FLOAT, { static_cast(taus->size()), 1}); + + float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, + pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, + pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, + pfCandVtxQuality, pfCandHighPurityTrk, pfCandTauIndMatch, pfCandDzSig, pfCandD0Sig, pfCandD0Err, + pfCandPtRelPtRel, pfCandDzDz, pfCandDVx_1, pfCandDVy_1, pfCandDVz_1, pfCandD_1; + float pvx = !vertices->empty() ? (*vertices)[0].x() : -1; + float pvy = !vertices->empty() ? (*vertices)[0].y() : -1; + float pvz = !vertices->empty() ? (*vertices)[0].z() : -1; + + bool pfCandIsBarrel; + + for(size_t tau_index = 0; tau_index < taus->size(); tau_index++) { + pat::Tau tau = taus->at(tau_index); + bool isGoodTau = false; + if(tau.pt() >= 30 && std::abs(tau.eta()) < 2.3 && tau.isTauIDAvailable("againstMuonLoose3") && + tau.isTauIDAvailable("againstElectronVLooseMVA6")) { + isGoodTau = (tau.tauID("againstElectronVLooseMVA6") && tau.tauID("againstMuonLoose3") ); + } + + if (!isGoodTau) { + predictions.matrix()(tau_index, 0) = -1; + continue; + } + + std::vector signalCandidateInds; + + for(auto c : tau.signalCands()) + signalCandidateInds.push_back(getPFCandidateIndex(pfcands,c)); + + float lepRecoPt = tau.pt(); + float lepRecoPz = std::abs(tau.pz()); + + // Use of setZero results in warnings in eigen library during compilation. + //tensor.flat().setZero(); + const unsigned n_inputs = GetNumberOfParticles(graphVersion) * GetNumberOfFeatures(graphVersion); + for(unsigned input_idx = 0; input_idx < n_inputs; ++input_idx) + tensor.flat()(input_idx) = 0; + + unsigned int iPF = 0; + const unsigned max_iPF = GetNumberOfParticles(graphVersion); + + std::vector sorted_inds(pfcands->size()); + std::size_t n = 0; + std::generate(std::begin(sorted_inds), std::end(sorted_inds), [&]{ return n++; }); + + std::sort(std::begin(sorted_inds), std::end(sorted_inds), + [&](int i1, int i2) { return pfcands->at(i1).pt() > pfcands->at(i2).pt(); } ); + + for(size_t pf_index = 0; pf_index < pfcands->size() && iPF < max_iPF; pf_index++) { + pat::PackedCandidate p = pfcands->at(sorted_inds.at(pf_index)); + float deltaR_tau_p = deltaR(p.p4(),tau.p4()); + + if (p.pt() < 0.5) continue; + if (p.fromPV() < 0) continue; + if (deltaR_tau_p > 0.5) continue; + + + if (p.fromPV() < 1 && p.charge() != 0) continue; + pfCandPt = p.pt(); + pfCandPtRel = p.pt()/lepRecoPt; + + pfCandDr = deltaR_tau_p; + pfCandDEta = std::abs(tau.eta() - p.eta()); + pfCandDPhi = std::abs(deltaPhi(tau.phi(), p.phi())); + pfCandEta = p.eta(); + pfCandIsBarrel = (std::abs(pfCandEta) < 1.4); + pfCandPz = std::abs(std::sinh(pfCandEta)*pfCandPt); + pfCandPzRel = std::abs(std::sinh(pfCandEta)*pfCandPt)/lepRecoPz; + pfCandPdgID = std::abs(p.pdgId()); + pfCandCharge = p.charge(); + pfCandDVx_1 = p.vx() - pvx; + pfCandDVy_1 = p.vy() - pvy; + pfCandDVz_1 = p.vz() - pvz; + + pfCandD_1 = std::sqrt(pfCandDVx_1*pfCandDVx_1 + pfCandDVy_1*pfCandDVy_1 + pfCandDVz_1*pfCandDVz_1); + + if (pfCandCharge != 0 and p.hasTrackDetails()){ + pfCandDz = p.dz(); + pfCandDzErr = p.dzError(); + pfCandDzSig = (std::abs(p.dz()) + 0.000001)/(p.dzError() + 0.00001); + pfCandD0 = p.dxy(); + pfCandD0Err = p.dxyError(); + pfCandD0Sig = (std::abs(p.dxy()) + 0.000001)/ (p.dxyError() + 0.00001); + pfCandPixHits = p.numberOfPixelHits(); + pfCandHits = p.numberOfHits(); + pfCandLostInnerHits = p.lostInnerHits(); + } else { + float disp = 1; + int psudorand = p.pt()*1000000; + if (psudorand%2 == 0) disp = -1; + pfCandDz = 5*disp; + pfCandDzErr = 0; + pfCandDzSig = 0; + pfCandD0 = 5*disp; + pfCandD0Err = 0; + pfCandD0Sig = 0; + pfCandPixHits = 0; + pfCandHits = 0; + pfCandLostInnerHits = 2.; + pfCandDVx_1 = 1; + pfCandDVy_1 = 1; + pfCandDVz_1 = 1; + pfCandD_1 = 1; + } + + pfCandPuppiWeight = p.puppiWeight(); + pfCandFromPV = p.fromPV(); + pfCandVtxQuality = p.pvAssociationQuality(); + pfCandHighPurityTrk = p.trackHighPurity(); + float pfCandTauIndMatch_temp = 0; + + for (auto i : signalCandidateInds) { + if (i == sorted_inds.at(pf_index)) pfCandTauIndMatch_temp = 1; + } + + pfCandTauIndMatch = pfCandTauIndMatch_temp; + pfCandPtRelPtRel = pfCandPtRel*pfCandPtRel; + if (pfCandPt > 500) pfCandPt = 500.; + pfCandPt = pfCandPt/500.; + + if (pfCandPz > 1000) pfCandPz = 1000.; + pfCandPz = pfCandPz/1000.; + + if ((pfCandPtRel) > 1 ) pfCandPtRel = 1.; + if ((pfCandPzRel) > 100 ) pfCandPzRel = 100.; + pfCandPzRel = pfCandPzRel/100.; + pfCandDr = pfCandDr/.5; + pfCandEta = pfCandEta/2.75; + pfCandDEta = pfCandDEta/.5; + pfCandDPhi = pfCandDPhi/.5; + pfCandPixHits = pfCandPixHits/7.; + pfCandHits = pfCandHits/30.; + + if (pfCandPtRelPtRel > 1) pfCandPtRelPtRel = 1; + pfCandPtRelPtRel = pfCandPtRelPtRel; + + if (pfCandD0 > 5.) pfCandD0 = 5.; + if (pfCandD0 < -5.) pfCandD0 = -5.; + pfCandD0 = pfCandD0/5.; + + if (pfCandDz > 5.) pfCandDz = 5.; + if (pfCandDz < -5.) pfCandDz = -5.; + pfCandDz = pfCandDz/5.; + + if (pfCandD0Err > 1.) pfCandD0Err = 1.; + if (pfCandDzErr > 1.) pfCandDzErr = 1.; + if (pfCandDzSig > 3) pfCandDzSig = 3.; + pfCandDzSig = pfCandDzSig/3.; + + if (pfCandD0Sig > 1) pfCandD0Sig = 1.; + pfCandD0D0 = pfCandD0*pfCandD0; + pfCandDzDz = pfCandDz*pfCandDz; + pfCandD0Dz = pfCandD0*pfCandDz; + pfCandD0Dphi = pfCandD0*pfCandDPhi; + + if (pfCandDVx_1 > .05) pfCandDVx_1 = .05; + if (pfCandDVx_1 < -.05) pfCandDVx_1 = -.05; + pfCandDVx_1 = pfCandDVx_1/.05; + + if (pfCandDVy_1 > 0.05) pfCandDVy_1 = 0.05; + if (pfCandDVy_1 < -0.05) pfCandDVy_1 = -0.05; + pfCandDVy_1 = pfCandDVy_1/0.05; + + if (pfCandDVz_1 > 0.05) pfCandDVz_1 = 0.05; + if (pfCandDVz_1 < -0.05) pfCandDVz_1= -0.05; + pfCandDVz_1 = pfCandDVz_1/0.05; + + if (pfCandD_1 > 0.1) pfCandD_1 = 0.1; + if (pfCandD_1 < -0.1) pfCandD_1 = -0.1; + pfCandD_1 = pfCandD_1/.1; + + if (graphVersion == 0) { + tensor.tensor()( 0, 60-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 60-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 60-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 60-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 60-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 60-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 60-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 60-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 60-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 60-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 60-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 60-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 60-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 60-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 60-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 60-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 60-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 60-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 60-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 60-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 60-1-iPF, 20) = pfCandDzDz; + tensor.tensor()( 0, 60-1-iPF, 21) = pfCandD0Dz; + tensor.tensor()( 0, 60-1-iPF, 22) = pfCandD0Dphi; + tensor.tensor()( 0, 60-1-iPF, 23) = pfCandPtRelPtRel; + tensor.tensor()( 0, 60-1-iPF, 24) = pfCandPixHits; + tensor.tensor()( 0, 60-1-iPF, 25) = pfCandHits; + tensor.tensor()( 0, 60-1-iPF, 26) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 60-1-iPF, 27) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 60-1-iPF, 28) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 60-1-iPF, 29) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 60-1-iPF, 30) = pfCandPuppiWeight; + tensor.tensor()( 0, 60-1-iPF, 31) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 60-1-iPF, 32) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 60-1-iPF, 33) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 60-1-iPF, 34) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 60-1-iPF, 35) = (pfCandFromPV == 1); + tensor.tensor()( 0, 60-1-iPF, 36) = (pfCandFromPV == 2); + tensor.tensor()( 0, 60-1-iPF, 37) = (pfCandFromPV == 3); + tensor.tensor()( 0, 60-1-iPF, 38) = pfCandIsBarrel; + tensor.tensor()( 0, 60-1-iPF, 39) = pfCandHighPurityTrk; + tensor.tensor()( 0, 60-1-iPF, 40) = pfCandPdgID==1; + tensor.tensor()( 0, 60-1-iPF, 41) = pfCandPdgID==2; + tensor.tensor()( 0, 60-1-iPF, 42) = pfCandPdgID==11; + tensor.tensor()( 0, 60-1-iPF, 43) = pfCandPdgID==13; + tensor.tensor()( 0, 60-1-iPF, 44) = pfCandPdgID==130; + tensor.tensor()( 0, 60-1-iPF, 45) = pfCandPdgID==211; + tensor.tensor()( 0, 60-1-iPF, 46) = pfCandTauIndMatch; + } + + if (graphVersion == 1) { + tensor.tensor()( 0, 36-1-iPF, 0) = pfCandPt; + tensor.tensor()( 0, 36-1-iPF, 1) = pfCandPz; + tensor.tensor()( 0, 36-1-iPF, 2) = pfCandPtRel; + tensor.tensor()( 0, 36-1-iPF, 3) = pfCandPzRel; + tensor.tensor()( 0, 36-1-iPF, 4) = pfCandDr; + tensor.tensor()( 0, 36-1-iPF, 5) = pfCandDEta; + tensor.tensor()( 0, 36-1-iPF, 6) = pfCandDPhi; + tensor.tensor()( 0, 36-1-iPF, 7) = pfCandEta; + tensor.tensor()( 0, 36-1-iPF, 8) = pfCandDz; + tensor.tensor()( 0, 36-1-iPF, 9) = pfCandDzSig; + tensor.tensor()( 0, 36-1-iPF, 10) = pfCandD0; + tensor.tensor()( 0, 36-1-iPF, 11) = pfCandD0Sig; + tensor.tensor()( 0, 36-1-iPF, 12) = pfCandDzErr; + tensor.tensor()( 0, 36-1-iPF, 13) = pfCandD0Err; + tensor.tensor()( 0, 36-1-iPF, 14) = pfCandD0D0; + tensor.tensor()( 0, 36-1-iPF, 15) = pfCandCharge==0; + tensor.tensor()( 0, 36-1-iPF, 16) = pfCandCharge==1; + tensor.tensor()( 0, 36-1-iPF, 17) = pfCandCharge==-1; + tensor.tensor()( 0, 36-1-iPF, 18) = pfCandPdgID>22; + tensor.tensor()( 0, 36-1-iPF, 19) = pfCandPdgID==22; + tensor.tensor()( 0, 36-1-iPF, 20) = pfCandDVx_1; + tensor.tensor()( 0, 36-1-iPF, 21) = pfCandDVy_1; + tensor.tensor()( 0, 36-1-iPF, 22) = pfCandDVz_1; + tensor.tensor()( 0, 36-1-iPF, 23) = pfCandD_1; + tensor.tensor()( 0, 36-1-iPF, 24) = pfCandDzDz; + tensor.tensor()( 0, 36-1-iPF, 25) = pfCandD0Dz; + tensor.tensor()( 0, 36-1-iPF, 26) = pfCandD0Dphi; + tensor.tensor()( 0, 36-1-iPF, 27) = pfCandPtRelPtRel; + tensor.tensor()( 0, 36-1-iPF, 28) = pfCandPixHits; + tensor.tensor()( 0, 36-1-iPF, 29) = pfCandHits; + tensor.tensor()( 0, 36-1-iPF, 30) = pfCandLostInnerHits==-1; + tensor.tensor()( 0, 36-1-iPF, 31) = pfCandLostInnerHits==0; + tensor.tensor()( 0, 36-1-iPF, 32) = pfCandLostInnerHits==1; + tensor.tensor()( 0, 36-1-iPF, 33) = pfCandLostInnerHits==2; + tensor.tensor()( 0, 36-1-iPF, 34) = pfCandPuppiWeight; + tensor.tensor()( 0, 36-1-iPF, 35) = (pfCandVtxQuality == 1); + tensor.tensor()( 0, 36-1-iPF, 36) = (pfCandVtxQuality == 5); + tensor.tensor()( 0, 36-1-iPF, 37) = (pfCandVtxQuality == 6); + tensor.tensor()( 0, 36-1-iPF, 38) = (pfCandVtxQuality == 7); + tensor.tensor()( 0, 36-1-iPF, 39) = (pfCandFromPV == 1); + tensor.tensor()( 0, 36-1-iPF, 40) = (pfCandFromPV == 2); + tensor.tensor()( 0, 36-1-iPF, 41) = (pfCandFromPV == 3); + tensor.tensor()( 0, 36-1-iPF, 42) = pfCandIsBarrel; + tensor.tensor()( 0, 36-1-iPF, 43) = pfCandHighPurityTrk; + tensor.tensor()( 0, 36-1-iPF, 44) = pfCandPdgID==1; + tensor.tensor()( 0, 36-1-iPF, 45) = pfCandPdgID==2; + tensor.tensor()( 0, 36-1-iPF, 46) = pfCandPdgID==11; + tensor.tensor()( 0, 36-1-iPF, 47) = pfCandPdgID==13; + tensor.tensor()( 0, 36-1-iPF, 48) = pfCandPdgID==130; + tensor.tensor()( 0, 36-1-iPF, 49) = pfCandPdgID==211; + tensor.tensor()( 0, 36-1-iPF, 50) = pfCandTauIndMatch; + } + + iPF++; + } + + tensorflow::Status status = session->Run( { {"input_1", tensor} }, {"output_node0"}, {}, &outputs); + predictions.matrix()(tau_index, 0) = outputs[0].flat()(0); + } + return predictions; + } private: - edm::EDGetTokenT taus_token; - edm::EDGetTokenT pfcand_token; - edm::EDGetTokenT vtx_token; - - edm::Handle taus; - edm::Handle pfcands; - edm::Handle vertices; - - TString graphName; - GraphPtr graph; - tensorflow::Session* session; - static const unsigned int nparticles_v0 = 60; - static const unsigned int nfeatures_v0 = 47; - - static const unsigned int nparticles_v1 = 36; - static const unsigned int nfeatures_v1 = 51; - - tensorflow::Tensor tensor; - std::vector outputs; + edm::EDGetTokenT pfcand_token; + edm::EDGetTokenT vtx_token; + edm::Handle pfcands; + edm::Handle vertices; + + unsigned graphVersion; + tensorflow::Tensor tensor; + std::vector outputs; }; -} #include "FWCore/Framework/interface/MakerMacros.h" DEFINE_FWK_MODULE(DPFIsolation); diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 42878c52628c5..c123ecc7e0329 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -1,21 +1,12 @@ /* * \class DeepTauId * - * Tau identification using Deep NN + * Tau identification using Deep NN. * * \author Konstantin Androsov, INFN Pisa */ -#include -#include "FWCore/Framework/interface/stream/EDProducer.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "PhysicsTools/TensorFlow/interface/TensorFlow.h" -#include "DataFormats/PatCandidates/interface/Electron.h" -#include "DataFormats/PatCandidates/interface/Muon.h" -#include "DataFormats/PatCandidates/interface/Tau.h" -#include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" -#include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" -#include "DataFormats/Math/interface/deltaR.h" +#include "RecoTauTag/RecoTau/interface/DeepTauBase.h" namespace { @@ -236,80 +227,58 @@ struct MuonHitMatch { } // anonymous namespace - -class DeepTauId : public edm::stream::EDProducer<> { +class DeepTauId : public deep_tau::DeepTauBase { public: - using TauType = pat::Tau; - using TauDiscriminator = pat::PATTauDiscriminator; - using TauCollection = std::vector; - using TauRef = edm::Ref; - using TauRefProd = edm::RefProd; - using ElectronCollection = pat::ElectronCollection; - using MuonCollection = pat::MuonCollection; - using LorentzVectorXYZ = ROOT::Math::LorentzVector>; - using GraphPtr = std::shared_ptr; static constexpr float default_value = -999.; - struct Output { - std::vector num, den; - - Output(const std::vector& _num, const std::vector& _den) : num(_num), den(_den) {} - - std::unique_ptr get_value(const edm::Handle& taus, - const tensorflow::Tensor& pred) const - { - auto output = std::make_unique(TauRefProd(taus)); - for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { - float x = 0; - for(size_t num_elem : num) - x += pred.matrix()(tau_index, num_elem); - if(x != 0) { - float den_val = 0; - for(size_t den_elem : den) - den_val += pred.matrix()(tau_index, den_elem); - x = den_val != 0 ? x / den_val : std::numeric_limits::max(); - } - output->setValue(tau_index, x); - } - return output; - } - }; - - using OutputCollection = std::map; - static const OutputCollection& GetOutputs() { static constexpr size_t e_index = 0, mu_index = 1, tau_index = 2, jet_index = 3; static const OutputCollection outputs = { - { "tauVSe", Output({tau_index}, {e_index, tau_index}) }, - { "tauVSmu", Output({tau_index}, {mu_index, tau_index}) }, - { "tauVSjet", Output({tau_index}, {jet_index, tau_index}) }, - { "tauVSall", Output({tau_index}, {e_index, mu_index, jet_index, tau_index}) } + { "VSe", Output({tau_index}, {e_index, tau_index}) }, + { "VSmu", Output({tau_index}, {mu_index, tau_index}) }, + { "VSjet", Output({tau_index}, {jet_index, tau_index}) }, }; return outputs; } + static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) + { + edm::ParameterSetDescription desc; + desc.add("electrons", edm::InputTag("slimmedElectrons")); + desc.add("muons", edm::InputTag("slimmedMuons")); + desc.add("taus", edm::InputTag("slimmedTaus")); + desc.add("graph_file", "RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v1_20L1024N.pb"); + + edm::ParameterSetDescription descWP; + descWP.add("VVVLoose", "0"); + descWP.add("VVLoose", "0"); + descWP.add("VLoose", "0"); + descWP.add("Loose", "0"); + descWP.add("Medium", "0"); + descWP.add("Tight", "0"); + descWP.add("VTight", "0"); + descWP.add("VVTight", "0"); + descWP.add("VVVTight", "0"); + desc.add("VSeWP", descWP); + desc.add("VSmuWP", descWP); + desc.add("VSjetWP", descWP); + descriptions.add("DeepTau2017v1", desc); + } + public: explicit DeepTauId(const edm::ParameterSet& cfg) : + DeepTauBase(cfg, GetOutputs()), electrons_token(consumes(cfg.getParameter("electrons"))), muons_token(consumes(cfg.getParameter("muons"))), - taus_token(consumes(cfg.getParameter("taus"))), - graph(tensorflow::loadGraphDef(edm::FileInPath(cfg.getParameter("graph_file")).fullPath())), - session(tensorflow::createSession(graph.get())), input_layer(graph->node(0).name()), output_layer(graph->node(graph->node_size() - 1).name()) { - for(const auto& output_desc : GetOutputs()) - produces(output_desc.first); - } - - virtual ~DeepTauId() override - { - tensorflow::closeSession(session); } - virtual void produce(edm::Event& event, const edm::EventSetup& es) override +private: + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) override { edm::Handle electrons; event.getByToken(electrons_token, electrons); @@ -317,19 +286,12 @@ class DeepTauId : public edm::stream::EDProducer<> { edm::Handle muons; event.getByToken(muons_token, muons); - edm::Handle taus; - event.getByToken(taus_token, taus); - const tensorflow::Tensor& inputs = CreateInputs(*taus, *electrons, *muons); std::vector pred_vector; tensorflow::run(session, { { input_layer, inputs } }, { output_layer }, &pred_vector); - const tensorflow::Tensor& pred = pred_vector.at(0); - - for(const auto& output_desc : GetOutputs()) - event.put(output_desc.second.get_value(taus, pred), output_desc.first); + return pred_vector.at(0); } -private: template tensorflow::Tensor CreateInputs(const TauCollection& taus, const ElectronCollection& electrons, const MuonCollection& muons) const @@ -653,9 +615,6 @@ class DeepTauId : public edm::stream::EDProducer<> { private: edm::EDGetTokenT electrons_token; edm::EDGetTokenT muons_token; - edm::EDGetTokenT taus_token; - GraphPtr graph; - tensorflow::Session* session; std::string input_layer, output_layer; TauIdMVAAuxiliaries clusterVariables; }; diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py b/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py deleted file mode 100644 index e612aae100e70..0000000000000 --- a/RecoTauTag/RecoTau/python/DPFIsolation_cfi.py +++ /dev/null @@ -1,10 +0,0 @@ - -import FWCore.ParameterSet.Config as cms - -DPFIsolation = cms.EDProducer("DPFIsolation", - pfcands = cms.InputTag('packedPFCandidates'), - taus = cms.InputTag('slimmedTaus'), - vertices = cms.InputTag('offlineSlimmedPrimaryVertices'), - graph_file = cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') -) - diff --git a/RecoTauTag/RecoTau/python/DeepTauId_cfi.py b/RecoTauTag/RecoTau/python/DeepTauId_cfi.py deleted file mode 100644 index 95a6e7671f8e7..0000000000000 --- a/RecoTauTag/RecoTau/python/DeepTauId_cfi.py +++ /dev/null @@ -1,10 +0,0 @@ - -import FWCore.ParameterSet.Config as cms - -deepTauIdraw = cms.EDProducer("DeepTauId", - electrons = cms.InputTag('slimmedElectrons'), - muons = cms.InputTag('slimmedMuons'), - taus = cms.InputTag('slimmedTaus'), - graph_file = cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') -) - diff --git a/RecoTauTag/RecoTau/python/runTauIdMVA.py b/RecoTauTag/RecoTau/python/runTauIdMVA.py index edbba9d974f2a..778d9c5fd6084 100644 --- a/RecoTauTag/RecoTau/python/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/runTauIdMVA.py @@ -572,73 +572,119 @@ def runTauID(self, name='NewTauIDsEmbedded'): tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VVTight') if "deepTau2017v1" in self.toKeep: - print "Adding DeepTau isolation?" - - from RecoTauTag.RecoTau.DeepTauId_cfi import deepTauIdraw - - self.process.deepTauIdraw = deepTauIdraw.clone( + print "Adding DeepTau isolation" + + working_points = { + "e": { + "VVVLoose" : "0.96424", + "VVLoose" : "0.98992", + "VLoose" : "0.99574", + "Loose": "0.99831", + "Medium": "0.99868", + "Tight": "0.99898", + "VTight": "0.99911", + "VVTight": "0.99918" + }, + "mu": { + "VVVLoose" : "0.959619", + "VVLoose" : "0.997687", + "VLoose" : "0.999392", + "Loose": "0.999755", + "Medium": "0.999854", + "Tight": "0.999886", + "VTight": "0.99994", + "VVTight": "0.9999971" + }, + + "jet": { + "VVVLoose" : "0.5329", + "VVLoose" : "0.7645", + "VLoose" : "0.8623", + "Loose": "0.9140", + "Medium": "0.9464", + "Tight": "0.9635", + "VTight": "0.9760", + "VVTight": "0.9859" + } + } + self.process.DeepTau2017v1 = self.cms.EDProducer("DeepTauId", electrons = self.cms.InputTag('slimmedElectrons'), muons = self.cms.InputTag('slimmedMuons'), taus = self.cms.InputTag('slimmedTaus'), - graph_file = self.cms.string('RecoTauTag/RecoTau/data/deepTau_2017v1_20L1024N.pb') + graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v1_20L1024N.pb') ) + self.ProcessDeepProducer('DeepTau2017v1', tauIDSources, working_points) + self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.deepTauIdraw + self.process.DeepTau2017v1 ) - tauIDSources.deepTau2017v1tauVSe = self.cms.InputTag('deepTauIdraw', 'tauVSe') - tauIDSources.deepTau2017v1tauVSmu = self.cms.InputTag('deepTauIdraw', 'tauVSmu') - tauIDSources.deepTau2017v1tauVSjet = self.cms.InputTag('deepTauIdraw', 'tauVSjet')#$= self.cms.InputTag('deepTauIdrawtauVSjet') - tauIDSources.deepTau2017v1tauVSall = self.cms.InputTag('deepTauIdraw', 'tauVSall') - print("Doing an embedding ") - if "DPFTau_2016_v0" in self.toKeep: - print "Adding DPF isolation?" + print "Adding DPF isolation" - from RecoTauTag.RecoTau.DPFIsolation_cfi import DPFIsolation + working_points = { + "all": { - self.process.DPFIsolationv0 = DPFIsolation.clone( - electrons = self.cms.InputTag('slimmedElectrons'), - muons = self.cms.InputTag('slimmedMuons'), - taus = self.cms.InputTag('slimmedTaus'), - graph_file = self.cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v0.pb') + "Tight" : "? decayMode == 0 ? (0.898328 - 0.000160992 * pt) : " + + "(? decayMode == 1 ? 0.910138 - 0.000229923 * pt : " + + "(? decayMode == 10 ? (0.873958 - 0.0002328 * pt) : 1))" + # "Tight" : "(decayMode == 0) * (0.898328 - 0.000160992 * pt) + \ + # (decayMode == 1) * (0.910138 - 0.000229923 * pt) + \ + # (decayMode == 10) * (0.873958 - 0.0002328 * pt) " + } + } + + self.process.DPFTau2016v0 = self.cms.EDProducer("DPFIsolation", + pfcands = self.cms.InputTag('packedPFCandidates'), + taus = self.cms.InputTag('slimmedTaus'), + vertices = self.cms.InputTag('offlineSlimmedPrimaryVertices'), + graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v0.pb') ) + self.ProcessDeepProducer('DPFTau2016v0', tauIDSources, working_points) + self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.DPFIsolationv0 + self.process.DPFTau2016v0 ) - tauIDSources.DPFTau_2016_v0tauVSe = self.cms.InputTag('DPFIsolationv0', 'tauVSe') - tauIDSources.DPFTau_2016_v0tauVSmu = self.cms.InputTag('DPFIsolationv0', 'tauVSmu') - tauIDSources.DPFTau_2016_v0tauVSjet = self.cms.InputTag('DPFIsolationv0', 'tauVSjet') - tauIDSources.DPFTau_2016_v0tauVSall = self.cms.InputTag('DPFIsolationv0', 'tauVSall') - if "DPFTau_2016_v1" in self.toKeep: - print "Adding DPF isolation?" + print "Adding DPF isolation" - from RecoTauTag.RecoTau.DPFIsolation_cfi import DPFIsolation + working_points = { + "all": {"Tight" : "0.123"} + } - self.process.DPFIsolationv1 = DPFIsolation.clone( - electrons = self.cms.InputTag('slimmedElectrons'), - muons = self.cms.InputTag('slimmedMuons'), - taus = self.cms.InputTag('slimmedTaus'), - graph_file = self.cms.string('RecoTauTag/RecoTau/data/DPFIsolation_2017v1.pb') + self.process.DPFTau2016v1 = self.cms.EDProducer("DPFIsolation", + pfcands = self.cms.InputTag('packedPFCandidates'), + taus = self.cms.InputTag('slimmedTaus'), + vertices = self.cms.InputTag('offlineSlimmedPrimaryVertices'), + graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v1.pb') ) + self.ProcessDeepProducer('DPFTau2016v1', tauIDSources, working_points) + self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.DPFIsolationv1 + self.process.DPFTau2016v1 ) - tauIDSources.DPFTau_2016_v1tauVSe = self.cms.InputTag('DPFIsolationv1', 'tauVSe') - tauIDSources.DPFTau_2016_v1tauVSmu = self.cms.InputTag('DPFIsolationv1', 'tauVSmu') - tauIDSources.DPFTau_2016_v1tauVSjet = self.cms.InputTag('DPFIsolationv1', 'tauVSjet') - tauIDSources.DPFTau_2016_v1tauVSall = self.cms.InputTag('DPFIsolationv1', 'tauVSall') - - - + print("Doing an embedding ") embedID = self.cms.EDProducer("PATTauIDEmbedder", src = self.cms.InputTag('slimmedTaus'), tauIDSources = tauIDSources ) self.process.NewTauIDsEmbedded = embedID + + + def ProcessDeepProducer(self, producer_name, tauIDSources, working_points): + for target,points in working_points.iteritems(): + cuts = self.cms.PSet() + setattr(tauIDSources, 'by{}VS{}raw'.format(producer_name, target), + self.cms.InputTag(producer_name, 'VS{}'.format(target))) + for point,cut in points.iteritems(): + setattr(cuts, point, self.cms.string(cut)) + + setattr(tauIDSources, 'by{}{}VS{}'.format(point, producer_name, target), + self.cms.InputTag(producer_name, 'VS{}{}'.format(target, point))) + + setattr(getattr(self.process, producer_name), 'VS{}WP'.format(target), cuts) diff --git a/RecoTauTag/RecoTau/src/DeepTauBase.cc b/RecoTauTag/RecoTau/src/DeepTauBase.cc new file mode 100644 index 0000000000000..61ccbcc4693f3 --- /dev/null +++ b/RecoTauTag/RecoTau/src/DeepTauBase.cc @@ -0,0 +1,82 @@ +/* + * \class DeepTauBase + * + * Implementation of the base class for tau identification using Deep NN. + * + * \author Konstantin Androsov, INFN Pisa + * \author Maria Rosaria Di Domenico, University of Siena & INFN Pisa + */ + +#include "RecoTauTag/RecoTau/interface/DeepTauBase.h" + +namespace deep_tau { + +DeepTauBase::Output::ResultMap DeepTauBase::Output::get_value(const edm::Handle& taus, + const tensorflow::Tensor& pred, + const WPMap& working_points) const +{ + ResultMap output; + output[""] = std::make_unique(TauRefProd(taus)); + for(const auto& wp : working_points) + output[wp.first] = std::make_unique(TauRefProd(taus)); + + for(size_t tau_index = 0; tau_index < taus->size(); ++tau_index) { + float x = 0; + for(size_t num_elem : num) + x += pred.matrix()(tau_index, num_elem); + if(x != 0 && den.size() > 0) { + float den_val = 0; + for(size_t den_elem : den) + den_val += pred.matrix()(tau_index, den_elem); + x = den_val != 0 ? x / den_val : std::numeric_limits::max(); + } + output[""]->setValue(tau_index, x); + for(const auto& wp : working_points) { + const auto& tau = taus->at(tau_index); + const bool pass = x > (*wp.second)(tau); + output[wp.first]->setValue(tau_index, pass); + } + } + return output; +} + +DeepTauBase::DeepTauBase(const edm::ParameterSet& cfg, const OutputCollection& outputCollection) : + taus_token(consumes(cfg.getParameter("taus"))), + graphName(edm::FileInPath(cfg.getParameter("graph_file")).fullPath()), + graph(tensorflow::loadGraphDef(graphName)), + session(tensorflow::createSession(graph.get())), + outputs(outputCollection) +{ + for(const auto& output_desc : outputs) { + produces(output_desc.first); + const auto& cut_pset = cfg.getParameter(output_desc.first + "WP"); + for(const std::string& wp_name : cut_pset.getParameterNames()) { + const auto& cut_str = cut_pset.getParameter(wp_name); + working_points[output_desc.first][wp_name] = std::make_unique(cut_str); + produces(output_desc.first + wp_name); + } + } +} + +DeepTauBase::~DeepTauBase() +{ + tensorflow::closeSession(session); +} + +void DeepTauBase::produce(edm::Event& event, const edm::EventSetup& es) +{ + event.getByToken(taus_token, taus); + const tensorflow::Tensor& pred = GetPredictions(event, es); + CreateOutputs(event, pred); +} + +void DeepTauBase::CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred) +{ + for(const auto& output_desc : outputs) { + auto result_map = output_desc.second.get_value(taus, pred, working_points.at(output_desc.first)); + for(auto& result : result_map) + event.put(std::move(result.second), output_desc.first + result.first); + } +} + +} // namespace deep_tau From 31795806f5f233d82d5931dc68abc55325c8f67b Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Fri, 19 Oct 2018 17:22:57 +0200 Subject: [PATCH 13/20] Added example of a python configuration file to produce pat::Tau collection with the new Tau-Ids --- RecoTauTag/RecoTau/python/DPFIsolation_cff.py | 2 +- .../RecoTau/test/runDeepTauIDsOnMiniAOD.py | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py diff --git a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py index 5807ea27efd2e..f3671983fe506 100644 --- a/RecoTauTag/RecoTau/python/DPFIsolation_cff.py +++ b/RecoTauTag/RecoTau/python/DPFIsolation_cff.py @@ -1,7 +1,7 @@ import FWCore.ParameterSet.Config as cms -from RecoTauTag.RecoTau.DPFIsolation_cff import * +from RecoTauTag.RecoTau.DPFIsolation_cfi import * DPFIsolationTask = cms.Task(DPFIsolation) DPFIsolationSeq = cms.Sequence(DPFIsolationTask) diff --git a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py new file mode 100644 index 0000000000000..a59e99ab05fd6 --- /dev/null +++ b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py @@ -0,0 +1,46 @@ +# produce pat::Tau collection with the new DNN Tau-Ids from miniAOD 12Apr2018_94X_mc2017 + +import FWCore.ParameterSet.Config as cms +from FWCore.ParameterSet.VarParsing import VarParsing + +options = VarParsing('analysis') +options.parseArguments() + +process = cms.Process('runDeepTauIDsOnMiniAOD') +process.options = cms.untracked.PSet() +process.options.wantSummary = cms.untracked.bool(False) +process.options.allowUnscheduled = cms.untracked.bool(True) +process.options.numberOfThreads = cms.untracked.uint32(8) +process.options.numberOfStreams = cms.untracked.uint32(0) + +process.load('FWCore.MessageLogger.MessageLogger_cfi') +process.MessageLogger.cerr.FwkReport.reportEvery = 100 + +process.load('Configuration.StandardSequences.MagneticField_cff') +process.load('Configuration.Geometry.GeometryRecoDB_cff') +process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_condDBv2_cff') + +process.GlobalTag.globaltag = '94X_mc2017_realistic_v14' +process.source = cms.Source('PoolSource', fileNames = cms.untracked.vstring( + # File from dataset /GluGluHToTauTau_M125_13TeV_powheg_pythia8/RunIIFall17MiniAODv2-PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/MINIAODSIM + '/store/mc/RunIIFall17MiniAODv2/GluGluHToTauTau_M125_13TeV_powheg_pythia8/MINIAODSIM/PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/90000/0498CD6A-CC42-E811-95D3-008CFA1CB8A8.root' +)) + +process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(100) ) + +import RecoTauTag.RecoTau.runTauIdMVA as tauIdConfig +tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, cms, debug = True, + toKeep = [ "2017v2", "dR0p32017v2", "newDM2017v2", "deepTau2017v1", "DPFTau_2016_v0", + "DPFTau_2016_v1"]) +tauIdEmbedder.runTauID() + +process.out = cms.OutputModule("PoolOutputModule", + fileName = cms.untracked.string('patTuple_newTauIDs.root'), + outputCommands = cms.untracked.vstring('drop *', "keep *_NewTauIDsEmbedded_*_*")) + +process.p = cms.Path( + process.rerunMvaIsolationSequence * + process.NewTauIDsEmbedded +) + +process.outpath = cms.EndPath(process.out) From a73dcf1c23cbc662959d43b6181665b5e3a2aba0 Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Mon, 22 Oct 2018 10:04:59 +0200 Subject: [PATCH 14/20] requested changes on runDeepTauIDsOnMiniAOD.py --- .../RecoTau/test/runDeepTauIDsOnMiniAOD.py | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py index a59e99ab05fd6..1625c05eda513 100644 --- a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py +++ b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py @@ -3,19 +3,9 @@ import FWCore.ParameterSet.Config as cms from FWCore.ParameterSet.VarParsing import VarParsing -options = VarParsing('analysis') -options.parseArguments() - -process = cms.Process('runDeepTauIDsOnMiniAOD') -process.options = cms.untracked.PSet() -process.options.wantSummary = cms.untracked.bool(False) -process.options.allowUnscheduled = cms.untracked.bool(True) -process.options.numberOfThreads = cms.untracked.uint32(8) -process.options.numberOfStreams = cms.untracked.uint32(0) - -process.load('FWCore.MessageLogger.MessageLogger_cfi') -process.MessageLogger.cerr.FwkReport.reportEvery = 100 - +# options = VarParsing('analysis') +# options.parseArguments() +process = cms.Process('TauID') process.load('Configuration.StandardSequences.MagneticField_cff') process.load('Configuration.Geometry.GeometryRecoDB_cff') process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_condDBv2_cff') @@ -42,5 +32,10 @@ process.rerunMvaIsolationSequence * process.NewTauIDsEmbedded ) +process.load('FWCore.MessageLogger.MessageLogger_cfi') +if process.maxEvents.input.value()>10: + process.MessageLogger.cerr.FwkReport.reportEvery = process.maxEvents.input.value()//10 +if process.maxEvents.input.value()>10000 or process.maxEvents.input.value()<0: + process.MessageLogger.cerr.FwkReport.reportEvery = 1000 process.outpath = cms.EndPath(process.out) From 5a5f4b4effe45c17c28502c3b287365a41a5044c Mon Sep 17 00:00:00 2001 From: Michal Bluj Date: Tue, 23 Oct 2018 15:17:58 +0200 Subject: [PATCH 15/20] Clean runTauIdMVA.py tool and test config to run tauIDs --- .../RecoTau/python/{ => tools}/runTauIdMVA.py | 185 ++++++++++-------- .../RecoTau/test/runDeepTauIDsOnMiniAOD.py | 58 ++++-- 2 files changed, 146 insertions(+), 97 deletions(-) rename RecoTauTag/RecoTau/python/{ => tools}/runTauIdMVA.py (85%) diff --git a/RecoTauTag/RecoTau/python/runTauIdMVA.py b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py similarity index 85% rename from RecoTauTag/RecoTau/python/runTauIdMVA.py rename to RecoTauTag/RecoTau/python/tools/runTauIdMVA.py index 778d9c5fd6084..370a961959b7d 100644 --- a/RecoTauTag/RecoTau/python/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py @@ -6,6 +6,7 @@ class TauIDEmbedder(object): """class to rerun the tau seq and acces trainings from the database""" def __init__(self, process, cms, debug = False, + updatedTauName = "slimmedTausNewID", toKeep = ["2016v1", "newDM2016v1","deepTau2017v1","DPFTau_2016_v0"], tauIdDiscrMVA_trainings_run2_2017 = { 'tauIdMVAIsoDBoldDMwLT2017' : "tauIdMVAIsoDBoldDMwLT2017", @@ -28,6 +29,7 @@ def __init__(self, process, cms, debug = False, self.process = process self.cms = cms self.debug = debug + self.updatedTauName = updatedTauName self.process.load('RecoTauTag.Configuration.loadRecoTauTagMVAsFromPrepDB_cfi') if len(conditionDB) != 0: self.process.CondDBTauConnection.connect = cms.string(conditionDB) @@ -100,7 +102,8 @@ def loadMVA_WPs_run2_2017(self): ) ) - def runTauID(self, name='NewTauIDsEmbedded'): + def runTauID(self): + self.process.rerunMvaIsolationTask = self.cms.Task() self.process.rerunMvaIsolationSequence = self.cms.Sequence() tauIDSources = self.cms.PSet() @@ -122,8 +125,8 @@ def runTauID(self, name='NewTauIDsEmbedded'): } } # update the list of available in DB samples - if not self.is_above_cmssw_version(10, 0, 0, self.debug): - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v1" + if not self.is_above_cmssw_version(9, 4, 4, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(9, 4, 4). Will update the list of available in DB samples to access 2017v1" self.loadMVA_WPs_run2_2017() self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw = patDiscriminationByIsolationMVArun2v1raw.clone( @@ -165,16 +168,18 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose.clone() self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v1_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight + self.rerunIsolationOldDMMVArun2017v1Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1raw, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VLoose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Loose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Medium, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1Tight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VTight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v1VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationOldDMMVArun2017v1Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationOldDMMVArun2017v1Task) tauIDSources.byIsolationMVArun2017v1DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1raw') tauIDSources.byVVLooseIsolationMVArun2017v1DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v1VVLoose') @@ -203,8 +208,9 @@ def runTauID(self, name='NewTauIDsEmbedded'): } } - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access 2017v2" - self.loadMVA_WPs_run2_2017() + if not self.is_above_cmssw_version(9, 4, 5, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access 2017v2" + self.loadMVA_WPs_run2_2017() self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( PATTauProducer = self.cms.InputTag('slimmedTaus'), @@ -246,16 +252,18 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose.clone() self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2017v2_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight + self.rerunIsolationOldDMMVArun2017v2Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2raw, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VLoose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Loose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Medium, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2Tight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VTight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2017v2VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationOldDMMVArun2017v2Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationOldDMMVArun2017v2Task) tauIDSources.byIsolationMVArun2017v2DBoldDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2raw') tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2017v2VVLoose') @@ -283,8 +291,9 @@ def runTauID(self, name='NewTauIDsEmbedded'): } } - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access newDM2017v2" - self.loadMVA_WPs_run2_2017() + if not self.is_above_cmssw_version(9, 4, 5, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access newDM2017v2" + self.loadMVA_WPs_run2_2017() self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( PATTauProducer = self.cms.InputTag('slimmedTaus'), @@ -326,16 +335,18 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose.clone() self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2017v2_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight + self.rerunIsolationNewDMMVArun2017v2Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2raw, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VLoose, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Loose, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Medium, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2Tight, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VTight, + self.process.rerunDiscriminationByIsolationNewDMMVArun2017v2VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationNewDMMVArun2017v2Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationNewDMMVArun2017v2Task) tauIDSources.byIsolationMVArun2017v2DBnewDMwLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2raw') tauIDSources.byVVLooseIsolationMVArun2017v2DBnewDMwLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2017v2VVLoose') @@ -363,8 +374,9 @@ def runTauID(self, name='NewTauIDsEmbedded'): } } - if self.debug: print "runTauID: not is_above_cmssw_version(10, 0, 0). Will update the list of available in DB samples to access dR0p32017v2" - self.loadMVA_WPs_run2_2017() + if not self.is_above_cmssw_version(9, 4, 5, self.debug): + if self.debug: print "runTauID: not is_above_cmssw_version(9, 4, 5). Will update the list of available in DB samples to access dR0p32017v2" + self.loadMVA_WPs_run2_2017() self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw = patDiscriminationByIsolationMVArun2v1raw.clone( PATTauProducer = self.cms.InputTag('slimmedTaus'), @@ -410,16 +422,18 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight = self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose.clone() self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMdR0p3wLT2017v2_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight - *self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight + self.rerunIsolationOldDMdR0p3MVArun2017v2Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VLoose, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Loose, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Medium, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2Tight, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VTight, + self.process.rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationOldDMdR0p3MVArun2017v2Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationOldDMdR0p3MVArun2017v2Task) tauIDSources.byIsolationMVArun2017v2DBoldDMdR0p3wLTraw2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2raw') tauIDSources.byVVLooseIsolationMVArun2017v2DBoldDMdR0p3wLT2017 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMdR0p3MVArun2017v2VVLoose') @@ -496,15 +510,17 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose.clone() self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBoldDMwLT2016v1_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight - *self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight + self.rerunIsolationOldDMMVArun2016v1Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1raw, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VLoose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Loose, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Medium, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1Tight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VTight, + self.process.rerunDiscriminationByIsolationOldDMMVArun2v1VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationOldDMMVArun2016v1Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationOldDMMVArun2016v1Task) tauIDSources.byIsolationMVArun2v1DBoldDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1raw') tauIDSources.byVLooseIsolationMVArun2v1DBoldDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationOldDMMVArun2v1VLoose') @@ -553,15 +569,17 @@ def runTauID(self, name='NewTauIDsEmbedded'): self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight = self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose.clone() self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight.mapping[0].cut = self.cms.string("RecoTauTag_tauIdMVAIsoDBnewDMwLT2016v1_WPEff40") - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight - *self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight + self.rerunIsolationNewDMMVArun2016v1Task = self.cms.Task( + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1raw, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VLoose, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Loose, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Medium, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1Tight, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VTight, + self.process.rerunDiscriminationByIsolationNewDMMVArun2v1VVTight ) + self.process.rerunMvaIsolationTask.add(self.rerunIsolationNewDMMVArun2016v1Task) + self.process.rerunMvaIsolationSequence += self.cms.Sequence(self.rerunIsolationNewDMMVArun2016v1Task) tauIDSources.byIsolationMVArun2v1DBnewDMwLTraw2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1raw') tauIDSources.byVLooseIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VLoose') @@ -572,7 +590,7 @@ def runTauID(self, name='NewTauIDsEmbedded'): tauIDSources.byVVTightIsolationMVArun2v1DBnewDMwLT2016 = self.cms.InputTag('rerunDiscriminationByIsolationNewDMMVArun2v1VVTight') if "deepTau2017v1" in self.toKeep: - print "Adding DeepTau isolation" + print "Adding DeepTau IDs" working_points = { "e": { @@ -607,21 +625,20 @@ def runTauID(self, name='NewTauIDsEmbedded'): "VVTight": "0.9859" } } - self.process.DeepTau2017v1 = self.cms.EDProducer("DeepTauId", + self.process.deepTau2017v1 = self.cms.EDProducer("DeepTauId", electrons = self.cms.InputTag('slimmedElectrons'), muons = self.cms.InputTag('slimmedMuons'), taus = self.cms.InputTag('slimmedTaus'), graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v1_20L1024N.pb') ) - self.ProcessDeepProducer('DeepTau2017v1', tauIDSources, working_points) + self.processDeepProducer('deepTau2017v1', tauIDSources, working_points) - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.DeepTau2017v1 - ) + self.process.rerunMvaIsolationTask.add(self.process.deepTau2017v1) + self.process.rerunMvaIsolationSequence += self.process.deepTau2017v1 if "DPFTau_2016_v0" in self.toKeep: - print "Adding DPF isolation" + print "Adding DPFTau isolation (v0)" working_points = { "all": { @@ -635,56 +652,54 @@ def runTauID(self, name='NewTauIDsEmbedded'): } } - self.process.DPFTau2016v0 = self.cms.EDProducer("DPFIsolation", + self.process.dpfTau2016v0 = self.cms.EDProducer("DPFIsolation", pfcands = self.cms.InputTag('packedPFCandidates'), taus = self.cms.InputTag('slimmedTaus'), vertices = self.cms.InputTag('offlineSlimmedPrimaryVertices'), graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v0.pb') ) - self.ProcessDeepProducer('DPFTau2016v0', tauIDSources, working_points) + self.processDeepProducer('dpfTau2016v0', tauIDSources, working_points) - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.DPFTau2016v0 - ) + self.process.rerunMvaIsolationTask.add(self.process.dpfTau2016v0) + self.process.rerunMvaIsolationSequence += self.process.dpfTau2016v0 if "DPFTau_2016_v1" in self.toKeep: - print "Adding DPF isolation" + print "Adding DPFTau isolation (v1)" working_points = { - "all": {"Tight" : "0.123"} + "all": {"Tight" : "0.123"} #FIXME: define WP } - self.process.DPFTau2016v1 = self.cms.EDProducer("DPFIsolation", + self.process.dpfTau2016v1 = self.cms.EDProducer("DPFIsolation", pfcands = self.cms.InputTag('packedPFCandidates'), taus = self.cms.InputTag('slimmedTaus'), vertices = self.cms.InputTag('offlineSlimmedPrimaryVertices'), graph_file = self.cms.string('RecoTauTag/TrainingFiles/data/DPFTauId/DPFIsolation_2017v1.pb') ) - self.ProcessDeepProducer('DPFTau2016v1', tauIDSources, working_points) + self.processDeepProducer('dpfTau2016v1', tauIDSources, working_points) - self.process.rerunMvaIsolationSequence += self.cms.Sequence( - self.process.DPFTau2016v1 - ) + self.process.rerunMvaIsolationTask.add(self.process.dpfTau2016v1) + self.process.rerunMvaIsolationSequence += self.process.dpfTau2016v1 - print("Doing an embedding ") + print('Embedding new TauIDs into \"'+self.updatedTauName+'\"') embedID = self.cms.EDProducer("PATTauIDEmbedder", src = self.cms.InputTag('slimmedTaus'), tauIDSources = tauIDSources ) - self.process.NewTauIDsEmbedded = embedID + setattr(self.process, self.updatedTauName, embedID) - def ProcessDeepProducer(self, producer_name, tauIDSources, working_points): + def processDeepProducer(self, producer_name, tauIDSources, working_points): for target,points in working_points.iteritems(): cuts = self.cms.PSet() - setattr(tauIDSources, 'by{}VS{}raw'.format(producer_name, target), + setattr(tauIDSources, 'by{}VS{}raw'.format(producer_name[0].upper()+producer_name[1:], target), self.cms.InputTag(producer_name, 'VS{}'.format(target))) for point,cut in points.iteritems(): setattr(cuts, point, self.cms.string(cut)) - setattr(tauIDSources, 'by{}{}VS{}'.format(point, producer_name, target), + setattr(tauIDSources, 'by{}{}VS{}'.format(point, producer_name[0].upper()+producer_name[1:], target), self.cms.InputTag(producer_name, 'VS{}{}'.format(target, point))) setattr(getattr(self.process, producer_name), 'VS{}WP'.format(target), cuts) diff --git a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py index 1625c05eda513..057806b3d6809 100644 --- a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py +++ b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py @@ -1,41 +1,75 @@ -# produce pat::Tau collection with the new DNN Tau-Ids from miniAOD 12Apr2018_94X_mc2017 +# Produce pat::Tau collection with the new DNN Tau-Ids from miniAOD 12Apr2018_94X_mc2017 import FWCore.ParameterSet.Config as cms -from FWCore.ParameterSet.VarParsing import VarParsing +# Options +#from FWCore.ParameterSet.VarParsing import VarParsing # options = VarParsing('analysis') # options.parseArguments() +updatedTauName = "slimmedTausNewID" +minimalOutput = True +eventsToProcess = 1000 +nThreads = 2 + process = cms.Process('TauID') process.load('Configuration.StandardSequences.MagneticField_cff') process.load('Configuration.Geometry.GeometryRecoDB_cff') -process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_condDBv2_cff') +process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff') +process.load('Configuration.StandardSequences.EndOfProcess_cff') process.GlobalTag.globaltag = '94X_mc2017_realistic_v14' + +# Input source process.source = cms.Source('PoolSource', fileNames = cms.untracked.vstring( # File from dataset /GluGluHToTauTau_M125_13TeV_powheg_pythia8/RunIIFall17MiniAODv2-PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/MINIAODSIM '/store/mc/RunIIFall17MiniAODv2/GluGluHToTauTau_M125_13TeV_powheg_pythia8/MINIAODSIM/PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/90000/0498CD6A-CC42-E811-95D3-008CFA1CB8A8.root' )) -process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(100) ) +process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(eventsToProcess) ) -import RecoTauTag.RecoTau.runTauIdMVA as tauIdConfig -tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, cms, debug = True, - toKeep = [ "2017v2", "dR0p32017v2", "newDM2017v2", "deepTau2017v1", "DPFTau_2016_v0", - "DPFTau_2016_v1"]) +# Add new TauIDs +import RecoTauTag.RecoTau.tools.runTauIdMVA as tauIdConfig +tauIdEmbedder = tauIdConfig.TauIDEmbedder(process, cms, debug = False, + updatedTauName = updatedTauName, + toKeep = [ "2017v2", "dR0p32017v2", "newDM2017v2", + "deepTau2017v1", + "DPFTau_2016_v0","DPFTau_2016_v1"]) tauIdEmbedder.runTauID() +# Output definition process.out = cms.OutputModule("PoolOutputModule", - fileName = cms.untracked.string('patTuple_newTauIDs.root'), - outputCommands = cms.untracked.vstring('drop *', "keep *_NewTauIDsEmbedded_*_*")) + fileName = cms.untracked.string('patTuple_newTauIDs.root'), + compressionAlgorithm = cms.untracked.string('LZMA'), + compressionLevel = cms.untracked.int32(4), + outputCommands = cms.untracked.vstring('drop *') +) +if not minimalOutput: + print("Store full MiniAOD EventContent") + from Configuration.EventContent.EventContent_cff import MINIAODSIMEventContent + from PhysicsTools.PatAlgos.slimming.MicroEventContent_cff import MiniAODOverrideBranchesSplitLevel + process.out.outputCommands = MINIAODSIMEventContent.outputCommands + process.out.overrideBranchesSplitLevel = MiniAODOverrideBranchesSplitLevel +process.out.outputCommands.append("keep *_"+updatedTauName+"_*_*") +# Path and EndPath definitions process.p = cms.Path( process.rerunMvaIsolationSequence * - process.NewTauIDsEmbedded + getattr(process,updatedTauName) ) +process.endjob = cms.EndPath(process.endOfProcess) +process.outpath = cms.EndPath(process.out) +# Schedule definition +process.schedule = cms.Schedule(process.p,process.endjob,process.outpath) + +## process.load('FWCore.MessageLogger.MessageLogger_cfi') if process.maxEvents.input.value()>10: process.MessageLogger.cerr.FwkReport.reportEvery = process.maxEvents.input.value()//10 if process.maxEvents.input.value()>10000 or process.maxEvents.input.value()<0: process.MessageLogger.cerr.FwkReport.reportEvery = 1000 -process.outpath = cms.EndPath(process.out) +process.options = cms.untracked.PSet( + wantSummary = cms.untracked.bool(False), + numberOfThreads = cms.untracked.uint32(nThreads), + numberOfStreams = cms.untracked.uint32(0) +) From d5e19bc312ae2b255613ba7b61ab892f5693a9eb Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Wed, 24 Oct 2018 17:30:18 +0200 Subject: [PATCH 16/20] Fixed problems to be compatible with branch CMSSW_10_4_X --- RecoTauTag/RecoTau/plugins/DeepTauId.cc | 14 +++++------- .../RecoTau/python/tools/runTauIdMVA.py | 22 +++++++++++++------ .../RecoTau/test/runDeepTauIDsOnMiniAOD.py | 6 ++--- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index c123ecc7e0329..309e00a8048cc 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -250,7 +250,6 @@ class DeepTauId : public deep_tau::DeepTauBase { desc.add("muons", edm::InputTag("slimmedMuons")); desc.add("taus", edm::InputTag("slimmedTaus")); desc.add("graph_file", "RecoTauTag/TrainingFiles/data/DeepTauId/deepTau_2017v1_20L1024N.pb"); - edm::ParameterSetDescription descWP; descWP.add("VVVLoose", "0"); descWP.add("VVLoose", "0"); @@ -345,14 +344,14 @@ class DeepTauId : public deep_tau::DeepTauBase { ? dPhi(leadChargedHadrCand->p4(), tau.p4()) : default_value; get(dnn::leadChargedHadrCand_mass) = leadChargedHadrCand ? leadChargedHadrCand->p4().mass() : default_value; - get(dnn::pt_weighted_deta_strip) = clusterVariables.tau_pt_weighted_deta_strip(tau, tau.decayMode()); - get(dnn::pt_weighted_dphi_strip) = clusterVariables.tau_pt_weighted_dphi_strip(tau, tau.decayMode()); - get(dnn::pt_weighted_dr_signal) = clusterVariables.tau_pt_weighted_dr_signal(tau, tau.decayMode()); - get(dnn::pt_weighted_dr_iso) = clusterVariables.tau_pt_weighted_dr_iso(tau, tau.decayMode()); + get(dnn::pt_weighted_deta_strip) = reco::tau::pt_weighted_deta_strip(tau, tau.decayMode()); + get(dnn::pt_weighted_dphi_strip) = reco::tau::pt_weighted_dphi_strip(tau, tau.decayMode()); + get(dnn::pt_weighted_dr_signal) = reco::tau::pt_weighted_dr_signal(tau, tau.decayMode()); + get(dnn::pt_weighted_dr_iso) = reco::tau::pt_weighted_dr_iso(tau, tau.decayMode()); get(dnn::leadingTrackNormChi2) = tau.leadingTrackNormChi2(); - get(dnn::e_ratio) = clusterVariables.tau_Eratio(tau); + get(dnn::e_ratio) = reco::tau::eratio(tau); get(dnn::gj_angle_diff) = CalculateGottfriedJacksonAngleDifference(tau); - get(dnn::n_photons) = clusterVariables.tau_n_photons_total(tau); + get(dnn::n_photons) = reco::tau::n_photons_total(tau); get(dnn::emFraction) = tau.emFraction_MVA(); get(dnn::has_gsf_track) = leadChargedHadrCand && std::abs(leadChargedHadrCand->pdgId()) == 11; get(dnn::inside_ecal_crack) = IsInEcalCrack(tau.p4().Eta()); @@ -616,7 +615,6 @@ class DeepTauId : public deep_tau::DeepTauBase { edm::EDGetTokenT electrons_token; edm::EDGetTokenT muons_token; std::string input_layer, output_layer; - TauIdMVAAuxiliaries clusterVariables; }; #include "FWCore/Framework/interface/MakerMacros.h" diff --git a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py index 370a961959b7d..6152c79638041 100644 --- a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py @@ -48,19 +48,27 @@ def __init__(self, process, cms, debug = False, @staticmethod def get_cmssw_version(debug = False): """returns 'CMSSW_X_Y_Z'""" - if debug: print "get_cmssw_version:", os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] - return os.environ["CMSSW_RELEASE_BASE"].split('/')[-1] + cmssw_version = os.environ["CMSSW_VERSION"] + if debug: print "get_cmssw_version:", cmssw_version + return cmssw_version @classmethod def get_cmssw_version_number(klass, debug = False): - """returns 'X_Y_Z' (without 'CMSSW_')""" - if debug: print "get_cmssw_version_number:", map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) - return map(int, klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3]) + """returns '(release, subversion, patch)' (without 'CMSSW_')""" + v = klass.get_cmssw_version().split("CMSSW_")[1].split("_")[0:3] + if debug: print "get_cmssw_version_number:", v + if v[2] == "X": + patch = -1 + else: + patch = int(v[2]) + return int(v[0]), int(v[1]), patch @staticmethod def versionToInt(release=9, subversion=4, patch=0, debug = False): - if debug: print "versionToInt:", release * 10000 + subversion * 100 + patch - return release * 10000 + subversion * 100 + patch + version = release * 10000 + subversion * 100 + patch + 1 # shifted by one to account for pre-releases. + if debug: print "versionToInt:", version + return version + @classmethod def is_above_cmssw_version(klass, release=9, subversion=4, patch=0, debug = False): diff --git a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py index 057806b3d6809..7f28843f49768 100644 --- a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py +++ b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py @@ -17,12 +17,12 @@ process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff') process.load('Configuration.StandardSequences.EndOfProcess_cff') -process.GlobalTag.globaltag = '94X_mc2017_realistic_v14' +process.GlobalTag.globaltag = '101X_upgrade2018_realistic_v7' # Input source process.source = cms.Source('PoolSource', fileNames = cms.untracked.vstring( - # File from dataset /GluGluHToTauTau_M125_13TeV_powheg_pythia8/RunIIFall17MiniAODv2-PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/MINIAODSIM - '/store/mc/RunIIFall17MiniAODv2/GluGluHToTauTau_M125_13TeV_powheg_pythia8/MINIAODSIM/PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/90000/0498CD6A-CC42-E811-95D3-008CFA1CB8A8.root' + # File from dataset DY1JetsToLL_M-50_TuneCP5_13TeV-madgraphMLM-pythia8 + '/store/mc/RunIISummer18MiniAOD/DY1JetsToLL_M-50_TuneCP5_13TeV-madgraphMLM-pythia8/MINIAODSIM/101X_upgrade2018_realistic_v7-v1/20000/0617A8FC-1CA0-E811-9992-FA163E4CB6BE.root' )) process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(eventsToProcess) ) From b87e8704517d5e87e708e7ffc0aab4b925d7c202 Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Thu, 25 Oct 2018 15:17:36 +0200 Subject: [PATCH 17/20] Made DeepTauId and DPFIsolation thread-safe --- RecoTauTag/RecoTau/interface/DeepTauBase.h | 7 +++---- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 19 +++++++++---------- RecoTauTag/RecoTau/plugins/DeepTauId.cc | 10 +++++----- .../RecoTau/python/tools/runTauIdMVA.py | 2 +- RecoTauTag/RecoTau/src/DeepTauBase.cc | 8 +++++--- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/RecoTauTag/RecoTau/interface/DeepTauBase.h b/RecoTauTag/RecoTau/interface/DeepTauBase.h index dde15ef7044ad..031ebfd90021a 100644 --- a/RecoTauTag/RecoTau/interface/DeepTauBase.h +++ b/RecoTauTag/RecoTau/interface/DeepTauBase.h @@ -60,18 +60,17 @@ class DeepTauBase : public edm::stream::EDProducer<> { virtual void produce(edm::Event& event, const edm::EventSetup& es) override; private: - virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) = 0; - virtual void CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred); + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es, + edm::Handle taus) = 0; + virtual void CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred, edm::Handle taus); protected: edm::EDGetTokenT taus_token; - edm::Handle taus; std::string graphName; GraphPtr graph; tensorflow::Session* session; std::map working_points; OutputCollection outputs; - }; } // namespace deep_tau diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index 5b7cab8aa6692..a06075db619f2 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -79,17 +79,21 @@ class DPFIsolation : public deep_tau::DeepTauBase { graphVersion = 1; else throw cms::Exception("DPFIsolation") << "unknown version of the graph file."; - - tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, {1, - static_cast(GetNumberOfParticles(graphVersion)), static_cast(GetNumberOfFeatures(graphVersion))}); } private: - virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) override + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es, + edm::Handle taus) override { + edm::Handle pfcands; event.getByToken(pfcand_token, pfcands); + + edm::Handle vertices; event.getByToken(vtx_token, vertices); + tensorflow::Tensor tensor(tensorflow::DT_FLOAT, {1, + static_cast(GetNumberOfParticles(graphVersion)), static_cast(GetNumberOfFeatures(graphVersion))}); + tensorflow::Tensor predictions(tensorflow::DT_FLOAT, { static_cast(taus->size()), 1}); float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, @@ -378,13 +382,8 @@ class DPFIsolation : public deep_tau::DeepTauBase { private: edm::EDGetTokenT pfcand_token; - edm::EDGetTokenT vtx_token; - - edm::Handle pfcands; - edm::Handle vertices; - + edm::EDGetTokenT vtx_token; unsigned graphVersion; - tensorflow::Tensor tensor; std::vector outputs; }; diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 309e00a8048cc..2f2cb9e62bdf6 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -277,7 +277,8 @@ class DeepTauId : public deep_tau::DeepTauBase { } private: - virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es) override + virtual tensorflow::Tensor GetPredictions(edm::Event& event, const edm::EventSetup& es, + edm::Handle taus) override { edm::Handle electrons; event.getByToken(electrons_token, electrons); @@ -305,9 +306,9 @@ class DeepTauId : public deep_tau::DeepTauBase { void SetInputs(const TauCollection& taus, size_t tau_index, tensorflow::Tensor& inputs, const ElectronCollection& electrons, const MuonCollection& muons) const { - static constexpr bool check_all_set = false; static constexpr float magic_number = -42; + static const TauIdMVAAuxiliaries clusterVariables; const auto& get = [&](int var_index) -> float& { return inputs.matrix()(tau_index, var_index); }; const TauType& tau = taus.at(tau_index); auto leadChargedHadrCand = dynamic_cast(tau.leadChargedHadrCand().get()); @@ -603,9 +604,8 @@ class DeepTauId : public deep_tau::DeepTauBase { const double dR2 = deltaR*deltaR; const pat::Electron* matched_ele = nullptr; for(const auto& ele : electrons) { - if(reco::deltaR2(tau.p4(), ele.p4()) < dR2 && - (!matched_ele || matched_ele->pt() < ele.pt())) { - matched_ele = &ele; + if(reco::deltaR2(tau.p4(), ele.p4()) < dR2 && (!matched_ele || matched_ele->pt() < ele.pt())) { + matched_ele = &ele; } } return matched_ele; diff --git a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py index 6152c79638041..9c986d20473af 100644 --- a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py @@ -618,7 +618,7 @@ def runTauID(self): "Loose": "0.999755", "Medium": "0.999854", "Tight": "0.999886", - "VTight": "0.99994", + "VTight": "0.999944", "VVTight": "0.9999971" }, diff --git a/RecoTauTag/RecoTau/src/DeepTauBase.cc b/RecoTauTag/RecoTau/src/DeepTauBase.cc index 61ccbcc4693f3..d94d6d55ebd01 100644 --- a/RecoTauTag/RecoTau/src/DeepTauBase.cc +++ b/RecoTauTag/RecoTau/src/DeepTauBase.cc @@ -65,12 +65,14 @@ DeepTauBase::~DeepTauBase() void DeepTauBase::produce(edm::Event& event, const edm::EventSetup& es) { + edm::Handle taus; event.getByToken(taus_token, taus); - const tensorflow::Tensor& pred = GetPredictions(event, es); - CreateOutputs(event, pred); + + const tensorflow::Tensor& pred = GetPredictions(event, es, taus); + CreateOutputs(event, pred, taus); } -void DeepTauBase::CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred) +void DeepTauBase::CreateOutputs(edm::Event& event, const tensorflow::Tensor& pred, edm::Handle taus) { for(const auto& output_desc : outputs) { auto result_map = output_desc.second.get_value(taus, pred, working_points.at(output_desc.first)); From 15b479e49b0efc09e172ef067e9975b61ad129aa Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Thu, 25 Oct 2018 16:40:18 +0200 Subject: [PATCH 18/20] Finish implement thread-safe requirements on DPFIsolation --- RecoTauTag/RecoTau/plugins/DPFIsolation.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc index a06075db619f2..8676abbf7e5de 100644 --- a/RecoTauTag/RecoTau/plugins/DPFIsolation.cc +++ b/RecoTauTag/RecoTau/plugins/DPFIsolation.cc @@ -96,6 +96,8 @@ class DPFIsolation : public deep_tau::DeepTauBase { tensorflow::Tensor predictions(tensorflow::DT_FLOAT, { static_cast(taus->size()), 1}); + std::vector outputs; + float pfCandPt, pfCandPz, pfCandPtRel, pfCandPzRel, pfCandDr, pfCandDEta, pfCandDPhi, pfCandEta, pfCandDz, pfCandDzErr, pfCandD0, pfCandD0D0, pfCandD0Dz, pfCandD0Dphi, pfCandPuppiWeight, pfCandPixHits, pfCandHits, pfCandLostInnerHits, pfCandPdgID, pfCandCharge, pfCandFromPV, @@ -384,7 +386,6 @@ class DPFIsolation : public deep_tau::DeepTauBase { edm::EDGetTokenT pfcand_token; edm::EDGetTokenT vtx_token; unsigned graphVersion; - std::vector outputs; }; #include "FWCore/Framework/interface/MakerMacros.h" From 9c57e9c95ff55789bd9a57ade7da43c359a0c799 Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Thu, 25 Oct 2018 17:28:56 +0200 Subject: [PATCH 19/20] Remove trace of TauIdMVAAuxiliaries in DeepTauId --- RecoTauTag/RecoTau/plugins/DeepTauId.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 2f2cb9e62bdf6..9ed57c8f94209 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -308,7 +308,6 @@ class DeepTauId : public deep_tau::DeepTauBase { { static constexpr bool check_all_set = false; static constexpr float magic_number = -42; - static const TauIdMVAAuxiliaries clusterVariables; const auto& get = [&](int var_index) -> float& { return inputs.matrix()(tau_index, var_index); }; const TauType& tau = taus.at(tau_index); auto leadChargedHadrCand = dynamic_cast(tau.leadChargedHadrCand().get()); From 08e81298bf36a204e365984191061675d79d3cbc Mon Sep 17 00:00:00 2001 From: Maria Di Domenico Date: Thu, 25 Oct 2018 18:10:13 +0200 Subject: [PATCH 20/20] Disable DPFTau_2016_v1 and issue some warnings --- RecoTauTag/RecoTau/python/tools/runTauIdMVA.py | 2 ++ RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py index 9c986d20473af..f7f0602453592 100644 --- a/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py +++ b/RecoTauTag/RecoTau/python/tools/runTauIdMVA.py @@ -674,6 +674,8 @@ def runTauID(self): if "DPFTau_2016_v1" in self.toKeep: print "Adding DPFTau isolation (v1)" + print "WARNING: WPs are not defined for DPFTau_2016_v1" + print "WARNING: The score of DPFTau_2016_v1 is inverted: i.e. for Sig->0, for Bkg->1 with -1 for undefined input (preselection not passed)." working_points = { "all": {"Tight" : "0.123"} #FIXME: define WP diff --git a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py index 7f28843f49768..01f85a7b39623 100644 --- a/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py +++ b/RecoTauTag/RecoTau/test/runDeepTauIDsOnMiniAOD.py @@ -33,7 +33,9 @@ updatedTauName = updatedTauName, toKeep = [ "2017v2", "dR0p32017v2", "newDM2017v2", "deepTau2017v1", - "DPFTau_2016_v0","DPFTau_2016_v1"]) + "DPFTau_2016_v0", + #"DPFTau_2016_v1" + ]) tauIdEmbedder.runTauID() # Output definition