Skip to content

Commit

Permalink
Merge pull request #25372 from guitargeek/PhotonIDValueMapProducer_10…
Browse files Browse the repository at this point in the history
…_2_X

[10_2_X] Fall17 V2 Photon ID and PhotonIDValueMapProducer speedup
  • Loading branch information
cmsbuild committed Dec 12, 2018
2 parents 6407722 + 6457cd0 commit ccc1a22
Show file tree
Hide file tree
Showing 31 changed files with 887 additions and 741 deletions.
2 changes: 1 addition & 1 deletion DQMOffline/Trigger/python/HLTEGTnPMonitor_cfi.py
Expand Up @@ -989,7 +989,7 @@
setupVIDSelection(egmGsfElectronIDsForDQM,item)


from RecoEgamma.PhotonIdentification.PhotonIDValueMapProducer_cfi import photonIDValueMapProducer
from RecoEgamma.PhotonIdentification.photonIDValueMapProducer_cff import photonIDValueMapProducer
from RecoEgamma.PhotonIdentification.egmPhotonIDs_cfi import egmPhotonIDs
egmPhotonIDsForDQM = egmPhotonIDs.clone()
egmPhotonIDsForDQM.physicsObjectsIDs = cms.VPSet()
Expand Down
2 changes: 1 addition & 1 deletion PhysicsTools/NanoAOD/python/photons_cff.py
Expand Up @@ -11,7 +11,7 @@

from PhysicsTools.SelectorUtils.tools.vid_id_tools import setupVIDSelection
from RecoEgamma.PhotonIdentification.egmPhotonIDs_cfi import *
from RecoEgamma.PhotonIdentification.PhotonIDValueMapProducer_cfi import *
from RecoEgamma.PhotonIdentification.photonIDValueMapProducer_cff import *
from RecoEgamma.PhotonIdentification.PhotonMVAValueMapProducer_cfi import *
from RecoEgamma.PhotonIdentification.PhotonRegressionValueMapProducer_cfi import *
from RecoEgamma.EgammaIsolationAlgos.egmPhotonIsolationMiniAOD_cff import *
Expand Down
17 changes: 13 additions & 4 deletions PhysicsTools/PatAlgos/python/slimming/miniAOD_tools.py
Expand Up @@ -317,11 +317,20 @@ def miniAOD_customizeCommon(process):

#VID Photon IDs
process.patPhotons.addPhotonID = cms.bool(True)
photon_ids = ['RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V1_TrueVtx_cff',

photon_id_config = cms.PSet(photon_ids = cms.vstring([
'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V1_TrueVtx_cff',
'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V1_cff',
'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V1p1_cff',
'RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Spring16_V2p2_cff',
'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Spring16_nonTrig_V1_cff']
'RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Spring16_nonTrig_V1_cff',
]))

from Configuration.Eras.Modifier_run2_miniAOD_devel_cff import run2_miniAOD_devel
run2_miniAOD_devel.toModify(photon_id_config, photon_ids = photon_id_config.photon_ids + \
["RecoEgamma.PhotonIdentification.Identification.mvaPhotonID_Fall17_94X_V2_cff",
"RecoEgamma.PhotonIdentification.Identification.cutBasedPhotonID_Fall17_94X_V2_cff"])

switchOnVIDPhotonIdProducer(process,DataFormat.AOD, task)
process.egmPhotonIsolation.srcToIsolate = \
cms.InputTag("reducedEgamma","reducedGedPhotons")
Expand All @@ -338,9 +347,9 @@ def miniAOD_customizeCommon(process):
cms.InputTag("reducedEgamma","reducedPhotonPfCandMap")
process.photonMVAValueMapProducer.src = \
cms.InputTag('reducedEgamma','reducedGedPhotons')
for idmod in photon_ids:
for idmod in photon_id_config.photon_ids.value():
setupAllVIDIdsInModule(process,idmod,setupVIDPhotonSelection,None,False,task)

#add the cut base IDs bitmaps of which cuts passed
from RecoEgamma.EgammaTools.egammaObjectModifications_tools import makeVIDBitsModifier
egamma_modifications.append(makeVIDBitsModifier(process,"egmGsfElectronIDs","egmPhotonIDs"))
Expand Down
180 changes: 180 additions & 0 deletions RecoEgamma/EgammaTools/interface/MultiToken.h
@@ -0,0 +1,180 @@
#ifndef RecoEgamma_EgammaTools_MultiToken_H
#define RecoEgamma_EgammaTools_MultiToken_H

#include "DataFormats/Common/interface/Handle.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/InputTag.h"

/*
* This class is a wrapper around a vector of EDM tokens, of which at least one
* is expected to yield a valid handle.
*
* The first time you call getValidHandle() or getHandle(), it will go over all
* tokens and try to get a valid handle. If no token yields a valid handle, it
* will either throw and exception or return the last non-valid handle.
*
* Once it found a valid handle, it will remember which token was used to get
* it and therefore doesn't need to loop over all tokens from there on.
*
* Example use case: auto-detection of AOD vs. MiniAOD.
*
* Created by Jonas Rembser on August 3, 2018.
*/

#include <memory>

template <typename T>
class MultiTokenT {

using GoodIndexType = std::shared_ptr<std::atomic<int>>;

public:

template <typename ... Tags>
MultiTokenT(edm::ConsumesCollector && cc, Tags ... tags)
: isMaster_(true)
, tokens_({cc.mayConsume<T>(edm::InputTag(tags))...})
, goodIndex_(std::make_shared<std::atomic<int>>(-1))
{}

// Constructor which gets the input tags from a config to create the tokens plus master token
template <typename S, typename ... Tags>
MultiTokenT(const MultiTokenT<S>& master, edm::ConsumesCollector && cc, Tags ... tags)
: isMaster_(false)
, tokens_({cc.mayConsume<T>(edm::InputTag(tags))...})
, goodIndex_(master.getGoodTokenIndexPtr())
{}

// Constructor which gets the input tags from a config to create the tokens
template <typename ... Tags>
MultiTokenT(edm::ConsumesCollector && cc, const edm::ParameterSet& pset, Tags && ... tags)
: isMaster_(true)
, tokens_({cc.mayConsume<T>(pset.getParameter<edm::InputTag>(tags))...})
, goodIndex_(std::make_shared<std::atomic<int>>(-1))
{}

// Constructor which gets the input tags from a config to create the tokens plus master token
template <typename S, typename ... Tags>
MultiTokenT(const MultiTokenT<S>& master, edm::ConsumesCollector && cc, const edm::ParameterSet& pset, Tags && ... tags)
: isMaster_(false)
, tokens_({cc.mayConsume<T>(pset.getParameter<edm::InputTag>(tags))...})
, goodIndex_(master.getGoodTokenIndexPtr())
{}

// Get a handle on the event data, non-valid handle is allowed
edm::Handle<T> getHandle(const edm::Event& iEvent) const
{
edm::Handle<T> handle;

// If we already know which token works, take that one
if (*goodIndex_ >= 0) {
iEvent.getByToken(tokens_[*goodIndex_], handle);
return handle;
}

if (!isMaster_) {
throw cms::Exception("MultiTokenTException") <<
"Trying to get a handle from a depending MultiToken before the master!";
}

// If not, set the good token index parallel to getting the handle
handle = getInitialHandle(iEvent);

if (*goodIndex_ == -1) {
*goodIndex_ = tokens_.size() - 1;
}
return handle;
}

// Get a handle on the event data,
// throw exception if no token yields a valid handle
edm::Handle<T> getValidHandle(const edm::Event& iEvent) const
{
edm::Handle<T> handle;

// If we already know which token works, take that one
if (*goodIndex_ >= 0) {
iEvent.getByToken(tokens_[*goodIndex_], handle);
if (!handle.isValid())
throw cms::Exception("MultiTokenTException") <<
"Token gave valid handle previously but not anymore!";
return handle;
}

if (!isMaster_) {
throw cms::Exception("MultiTokenTException") <<
"Trying to get a handle from a depending MultiToken before the master!";
}

// If not, set the good token index parallel to getting the handle
handle = getInitialHandle(iEvent);

if (*goodIndex_ == -1) {
throw cms::Exception("MultiTokenTException") << "Neither handle is valid!";
}
return handle;
}

// get the good token
edm::EDGetTokenT<T> get(const edm::Event& iEvent) const
{
// If we already know which token works, take that index
if (*goodIndex_ >= 0)
return tokens_[*goodIndex_];

// If this is not a master MultiToken, just return what it got
if (!isMaster_) {
throw cms::Exception("MultiTokenTException") <<
"Trying to get a handle from a depending MultiToken before the master!";
}

// Find which token is the good one by trying to get a handle
edm::Handle<T> handle;
for (auto token:tokens_ ) {
iEvent.getByToken(token, handle);
if (handle.isValid()) {
return token;
}
}

throw cms::Exception("MultiTokenTException") << "Neither token is valid!";
}

int getGoodTokenIndex() const
{
return *goodIndex_;
}

GoodIndexType getGoodTokenIndexPtr() const
{
return goodIndex_;
}

private:

edm::Handle<T> getInitialHandle(const edm::Event& iEvent) const
{
// Try to retrieve the collection from the event. If we fail to
// retrieve the collection with one name, we next look for the one with
// the other name and so on.
edm::Handle<T> handle;
for (size_t i = 0; i < tokens_.size(); ++i) {
iEvent.getByToken(tokens_[i], handle);
if (handle.isValid()) {
*goodIndex_ = i;
return handle;
}
}
return handle;
}

const bool isMaster_;
const std::vector<edm::EDGetTokenT<T>> tokens_;
const GoodIndexType goodIndex_;
};

#endif
25 changes: 25 additions & 0 deletions RecoEgamma/EgammaTools/interface/Utils.h
@@ -0,0 +1,25 @@
#ifndef RecoEgamma_EgammaTools_Utils_H
#define RecoEgamma_EgammaTools_Utils_H

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/ValueMap.h"
#include "FWCore/Framework/interface/Event.h"

#include <memory>
#include <string>
#include <vector>

template<typename ValueType, class HandleType>
void writeValueMap(edm::Event &iEvent,
const edm::Handle<HandleType>& handle,
const std::vector<ValueType>& values,
const std::string& label)
{
auto valMap = std::make_unique<edm::ValueMap<ValueType>>();
typename edm::ValueMap<ValueType>::Filler filler(*valMap);
filler.insert(handle, values.begin(), values.end());
filler.fill();
iEvent.put(std::move(valMap),label);
}

#endif
@@ -0,0 +1,9 @@
# The constants are based on 90% contours of isolation.
# |eta| min |eta| max effective area
0.0000 1.0000 0.0112
1.0000 1.4790 0.0108
1.4790 2.0000 0.0106
2.0000 2.2000 0.01002
2.2000 2.3000 0.0098
2.3000 2.4000 0.0089
2.4000 5.0000 0.0087
@@ -0,0 +1,9 @@
# The constants are based on 90% contours of isolation.
# |eta| min |eta| max effective area
0.0000 1.0000 0.0668
1.0000 1.4790 0.1054
1.4790 2.0000 0.0786
2.0000 2.2000 0.0233
2.2000 2.3000 0.0078
2.3000 2.4000 0.0028
2.4000 5.0000 0.0137
@@ -0,0 +1,9 @@
# The constants are based on 90% contours of isolation.
# |eta| min |eta| max effective area
0.0000 1.0000 0.1113
1.0000 1.4790 0.0953
1.4790 2.0000 0.0619
2.0000 2.2000 0.0837
2.2000 2.3000 0.1070
2.3000 2.4000 0.1212
2.4000 5.0000 0.1466
@@ -1,5 +1,5 @@
#
# File: PhotonMVAEstimatorRun2VariablesFall17V1.txt
# File: PhotonMVAEstimatorRun2VariablesFall17.txt
#
# This file is to be read by the MVAVariableManager class. Lists all the
# variables which appear in the xml files for the MVA based Photon IDs for Run2.
Expand All @@ -22,7 +22,7 @@ s4 full5x5_showerShapeVariables.e2x2/full5x5_showerShapeV
scEta superCluster.eta None None
rho fixedGridRhoAll None None
esEffSigmaRR full5x5_showerShapeVariables.effSigmaRR None None
esEnergy/SCRawE superCluster.preshowerEnergy/superCluster.rawEnergy None None
esEnergyOverRawE superCluster.preshowerEnergy/superCluster.rawEnergy None None
phoIso03 photonIDValueMapProducer:phoPhotonIsolation None None
chgIsoWrtChosenVtx photonIDValueMapProducer:phoChargedIsolation None None
chgIsoWrtWorstVtx photonIDValueMapProducer:phoWorstChargedIsolationWithPVConstraint None None
chgIsoWrtWorstVtx photonIDValueMapProducer:phoWorstChargedIsolationConeVetoPVConstr None None
Expand Up @@ -22,7 +22,7 @@ s4 full5x5_showerShapeVariables.e2x2/full5x5_showerShapeV
scEta superCluster.eta None None
rho fixedGridRhoAll None None
esEffSigmaRR full5x5_showerShapeVariables.effSigmaRR None None
esEnergy/SCRawE superCluster.preshowerEnergy/superCluster.rawEnergy None None
esEnergyOverRawE superCluster.preshowerEnergy/superCluster.rawEnergy None None
phoIso03 photonIDValueMapProducer:phoPhotonIsolation None None
chgIsoWrtChosenVtx photonIDValueMapProducer:phoChargedIsolation None None
chgIsoWrtWorstVtx photonIDValueMapProducer:phoWorstChargedIsolation None None
Expand Up @@ -25,6 +25,6 @@ phiWidth superCluster.phiWidth
CITK_isoPhotons egmPhotonIsolation:gamma-DR030- None None
CITK_isoPhoCorrMax2p5 egmPhotonIsolation:gamma-DR030- None None
CITK_isoChargedHad egmPhotonIsolation:h+-DR030- None None
chgIsoWrtWorstVtx photonIDValueMapProducer:phoWorstChargedIsolationWithConeVeto None None
chgIsoWrtWorstVtx photonIDValueMapProducer:phoWorstChargedIsolationConeVeto None None
esEffSigmaRR full5x5_showerShapeVariables.effSigmaRR None None
esEnergy/SCRawE superCluster.preshowerEnergy/superCluster.rawEnergy None None

0 comments on commit ccc1a22

Please sign in to comment.