Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

VID fix & core unit tests + Regression ValueMap Producer code cleanup. (76X) #11229

Merged
merged 6 commits into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions RecoEgamma/ElectronIdentification/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
<use name="CondFormats/EgammaObjects"/>
<use name="DataFormats/EgammaCandidates"/>
<use name="DataFormats/EcalRecHit"/>
<use name="CommonTools/UtilAlgos"/>
<use name="root"/>
<use name="rootcore"/>
<use name="RecoEgamma/EgammaTools"/>
<use name="RecoEgamma/ElectronIdentification"/>

<library name="RecoEgammaElectronIdentificationVIDExample" file="VIDUsageExample.cc">
<flags EDM_PLUGIN="1"/>
</library>

<environment>
<bin file="runtestRecoEgammaElectronIdentification.cpp">
<flags TEST_RUNNER_ARGS=" /bin/bash RecoEgamma/ElectronIdentification/test runtests.sh"/>
<use name="FWCore/Utilities"/>
</bin>
</environment>
82 changes: 82 additions & 0 deletions RecoEgamma/ElectronIdentification/test/runElectron_VID.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import FWCore.ParameterSet.Config as cms
import sys

process = cms.Process("TestElectrons")

process.load("FWCore.MessageService.MessageLogger_cfi")
process.MessageLogger.cerr.FwkReport.reportEvery = 100

process.load("Configuration.StandardSequences.GeometryDB_cff")

process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
# NOTE: the pick the right global tag!
# for PHYS14 scenario PU4bx50 : global tag is ???
# for PHYS14 scenario PU20bx25: global tag is PHYS14_25_V1
# as a rule, find the global tag in the DAS under the Configs for given dataset
#process.GlobalTag.globaltag = 'PHYS14_25_V1::All'
from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, 'auto:run2_mc', '')

#
# Define input data to read
#
process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(1000) )

inputFilesAOD = cms.untracked.vstring(
# AOD test files from /store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/3ADB5D32-DD4F-E511-AC01-002618943811.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/54B6CF34-DD4F-E511-9629-002590596490.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/8043D96A-6C4F-E511-81E7-003048FFD736.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/8E554BD2-6D4F-E511-BFD2-0025905A60DE.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/98EB5C3F-6D4F-E511-910B-0025905A6056.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/9C8CF66A-6C4F-E511-BD02-00259059391E.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/D015FB85-6C4F-E511-88FE-002618943902.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/GEN-SIM-RECO/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/D873CC62-6C4F-E511-ABBA-0025905B855E.root'
)

inputFilesMiniAOD = cms.untracked.vstring(
# MiniAOD test files from /store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/MINIAODSIM/PU25ns_76X_mcRun2_asymptotic_v1-v1
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/MINIAODSIM/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/BE21962F-DD4F-E511-B681-002354EF3BDF.root',
'/store/relval/CMSSW_7_6_0_pre4/RelValZEE_13/MINIAODSIM/PU25ns_76X_mcRun2_asymptotic_v1-v1/00000/D2B5E032-DD4F-E511-96A4-0025905A610C.root'
)

# Set up input/output depending on the format
# You can list here either AOD or miniAOD files, but not both types mixed
#

print sys.argv[2]
useAOD = bool(int(sys.argv[2]))

if useAOD == True :
inputFiles = inputFilesAOD
outputFile = "electron_ntuple.root"
print("AOD input files are used")
else :
inputFiles = inputFilesMiniAOD
outputFile = "electron_ntuple_mini.root"
print("MiniAOD input files are used")
process.source = cms.Source ("PoolSource", fileNames = inputFiles )

#
# Set up electron ID (VID framework)
#

from PhysicsTools.SelectorUtils.tools.vid_id_tools import *
# turn on VID producer, indicate data format to be
# DataFormat.AOD or DataFormat.MiniAOD, as appropriate
if useAOD == True :
dataFormat = DataFormat.AOD
else :
dataFormat = DataFormat.MiniAOD

switchOnVIDElectronIdProducer(process, dataFormat)

# define which IDs we want to produce
my_id_modules = [sys.argv[3]]

#add them to the VID producer
for idmod in my_id_modules:
setupAllVIDIdsInModule(process,idmod,setupVIDElectronSelection)

# Make sure to add the ID sequence upstream from the user analysis module
process.p = cms.Path(process.egmGsfElectronIDSequence)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "FWCore/Utilities/interface/TestHelper.h"

RUNTEST()
15 changes: 15 additions & 0 deletions RecoEgamma/ElectronIdentification/test/runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function die { echo $1: status $2 ; exit $2; }

ids_to_test=(
"RecoEgamma.ElectronIdentification.Identification.cutBasedElectronID_PHYS14_PU20bx25_V2_cff"
"RecoEgamma.ElectronIdentification.Identification.cutBasedElectronID_Spring15_25ns_V1_cff"
"RecoEgamma.ElectronIdentification.Identification.cutBasedElectronID_Spring15_50ns_V1_cff"
"RecoEgamma.ElectronIdentification.Identification.heepElectronID_HEEPV60_cff"
"RecoEgamma.ElectronIdentification.Identification.mvaElectronID_Spring15_25ns_nonTrig_V1_cff"
)

for id_set in "${ids_to_test[@]}"; do
echo Checking: $id_set
cmsRun ${LOCAL_TEST_DIR}/runElectron_VID.py 1 $id_set || die "Failure using runElectron_VID.py on AOD $id_set" $?
cmsRun ${LOCAL_TEST_DIR}/runElectron_VID.py 0 $id_set || die "Failure using runElectron_VID.py on MiniAOD $id_set" $?
done
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,71 @@

#include <memory>
#include <vector>
#include <unordered_map>

namespace {
// Cluster shapes
constexpr char sigmaIPhiIPhi_[] = "sigmaIPhiIPhi";
constexpr char sigmaIEtaIPhi_[] = "sigmaIEtaIPhi";
constexpr char e2x5Max_[] = "e2x5Max";
constexpr char e2x5Left_[] = "e2x5Left";
constexpr char e2x5Right_[] = "e2x5Right";
constexpr char e2x5Top_[] = "e2x5Top";
constexpr char e2x5Bottom_[] = "e2x5Bottom";
enum reg_float_vars { k_sigmaIPhiIPhi = 0,
k_sigmaIEtaIPhi,
k_e2x5Max,
k_e2x5Left,
k_e2x5Right,
k_e2x5Top,
k_e2x5Bottom,
k_NFloatVars };

enum reg_int_vars { k_NIntVars = 0 };

static const std::vector<std::string> float_var_names( { "sigmaIPhiIPhi",
"sigmaIEtaIPhi",
"e2x5Max",
"e2x5Left",
"e2x5Right",
"e2x5Top",
"e2x5Bottom" } );

static const std::vector<std::string> integer_var_names( { } );

inline void set_map_val( const reg_float_vars index, const float value,
std::unordered_map<std::string,float>& map) {
map[float_var_names[index]] = value;
}
inline void set_map_val( const reg_int_vars index, const int value,
std::unordered_map<std::string,int>& map) {
map[integer_var_names[index]] = value;
}

template<typename T>
inline void check_map(const std::unordered_map<std::string,T>& map, unsigned exp_size) {
if( map.size() != exp_size ) {
throw cms::Exception("PhotonRegressionWeirdConfig")
<< "variable map size: " << map.size()
<< " not equal to expected size: " << exp_size << " !"
<< " The regression variable calculation code definitely has a bug, fix it!";
}
}

template<typename LazyTools,typename SeedType>
inline void calculateValues(EcalClusterLazyToolsBase* tools_tocast,
const SeedType& the_seed,
std::unordered_map<std::string,float>& float_vars,
std::unordered_map<std::string,int>& /*int_vars*/ ) {
LazyTools* tools = static_cast<LazyTools*>(tools_tocast);

float spp = -999;
std::vector<float> vCov = tools->localCovariances( the_seed );
spp = (isnan(vCov[2]) ? 0. : sqrt(vCov[2]));
float sep = vCov[1];

set_map_val(k_sigmaIPhiIPhi, spp, float_vars);
set_map_val(k_sigmaIEtaIPhi, sep, float_vars);

set_map_val(k_e2x5Max, tools->e2x5Max(the_seed), float_vars);
set_map_val(k_e2x5Left, tools->e2x5Left(the_seed), float_vars);
set_map_val(k_e2x5Right, tools->e2x5Right(the_seed), float_vars);
set_map_val(k_e2x5Top, tools->e2x5Top(the_seed), float_vars);
set_map_val(k_e2x5Bottom, tools->e2x5Bottom(the_seed), float_vars);
}
}

class PhotonRegressionValueMapProducer : public edm::stream::EDProducer<> {
Expand All @@ -43,9 +98,10 @@ class PhotonRegressionValueMapProducer : public edm::stream::EDProducer<> {

virtual void produce(edm::Event&, const edm::EventSetup&) override;

template<typename T>
void writeValueMap(edm::Event &iEvent,
const edm::Handle<edm::View<reco::Photon> > & handle,
const std::vector<float> & values,
const std::vector<T> & values,
const std::string & label) const ;

// The object that will compute 5x5 quantities
Expand Down Expand Up @@ -95,43 +151,18 @@ PhotonRegressionValueMapProducer::PhotonRegressionValueMapProducer(const edm::Pa
// Declare producibles
//
// Cluster shapes
produces<edm::ValueMap<float> >(sigmaIPhiIPhi_);
produces<edm::ValueMap<float> >(sigmaIEtaIPhi_);
produces<edm::ValueMap<float> >(e2x5Max_);
produces<edm::ValueMap<float> >(e2x5Left_);
produces<edm::ValueMap<float> >(e2x5Right_);
produces<edm::ValueMap<float> >(e2x5Top_);
produces<edm::ValueMap<float> >(e2x5Bottom_);
for( const std::string& name : float_var_names ) {
produces<edm::ValueMap<float> >(name);
}

for( const std::string& name : integer_var_names ) {
produces<edm::ValueMap<int> >(name);
}
}

PhotonRegressionValueMapProducer::~PhotonRegressionValueMapProducer()
{}

template<typename LazyTools,typename SeedType>
inline void calculateValues(EcalClusterLazyToolsBase* tools_tocast,
const SeedType& the_seed,
std::vector<float>& sigmaIPhiIPhi,
std::vector<float>& sigmaIEtaIPhi,
std::vector<float>& e2x5Max,
std::vector<float>& e2x5Left,
std::vector<float>& e2x5Right,
std::vector<float>& e2x5Top,
std::vector<float>& e2x5Bottom) {
LazyTools* tools = static_cast<LazyTools*>(tools_tocast);

float spp = -999;
std::vector<float> vCov = tools->localCovariances( the_seed );
spp = (isnan(vCov[2]) ? 0. : sqrt(vCov[2]));
float sep = vCov[1];
sigmaIPhiIPhi.push_back(spp);
sigmaIEtaIPhi.push_back(sep);
e2x5Max .push_back(tools->e2x5Max(the_seed) );
e2x5Left .push_back(tools->e2x5Left(the_seed) );
e2x5Right .push_back(tools->e2x5Right(the_seed) );
e2x5Top .push_back(tools->e2x5Top(the_seed) );
e2x5Bottom.push_back(tools->e2x5Bottom(the_seed) );
}

void PhotonRegressionValueMapProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {

using namespace edm;
Expand Down Expand Up @@ -179,14 +210,11 @@ void PhotonRegressionValueMapProducer::produce(edm::Event& iEvent, const edm::Ev
}
}

// Cluster shapes
std::vector<float> sigmaIPhiIPhi;
std::vector<float> sigmaIEtaIPhi;
std::vector<float> e2x5Max;
std::vector<float> e2x5Left;
std::vector<float> e2x5Right;
std::vector<float> e2x5Top;
std::vector<float> e2x5Bottom;
std::vector<std::vector<float> > float_vars(k_NFloatVars);
std::vector<std::vector<int> > int_vars(k_NIntVars);

std::unordered_map<std::string,float> float_vars_map;
std::unordered_map<std::string,int> int_vars_map;

// reco::Photon::superCluster() is virtual so we can exploit polymorphism
for (unsigned idxpho = 0; idxpho < src->size(); ++idxpho) {
Expand All @@ -200,47 +228,52 @@ void PhotonRegressionValueMapProducer::produce(edm::Event& iEvent, const edm::Ev
if( use_full5x5_ ) {
calculateValues<noZS::EcalClusterLazyTools>(lazyTools.get(),
theseed,
sigmaIPhiIPhi,
sigmaIEtaIPhi,
e2x5Max,
e2x5Left,
e2x5Right,
e2x5Top,
e2x5Bottom);
float_vars_map,
int_vars_map);
} else {
calculateValues<EcalClusterLazyTools>(lazyTools.get(),
theseed,
sigmaIPhiIPhi,
sigmaIEtaIPhi,
e2x5Max,
e2x5Left,
e2x5Right,
e2x5Top,
e2x5Bottom);
}
float_vars_map,
int_vars_map);
}

check_map(float_vars_map, k_NFloatVars);
check_map(int_vars_map, k_NIntVars);

for( unsigned i = 0; i < float_vars.size(); ++i ) {
float_vars[i].emplace_back(float_vars_map.at(float_var_names[i]));
}


for( unsigned i = 0; i < int_vars.size(); ++i ) {
int_vars[i].emplace_back(int_vars_map.at(integer_var_names[i]));
}

}

for( unsigned i = 0; i < float_vars.size(); ++i ) {
writeValueMap(iEvent, src, float_vars[i], float_var_names[i]);
}

for( unsigned i = 0; i < int_vars.size(); ++i ) {
writeValueMap(iEvent, src, int_vars[i], integer_var_names[i]);
}

// Cluster shapes
writeValueMap(iEvent, src, sigmaIPhiIPhi, sigmaIPhiIPhi_);
writeValueMap(iEvent, src, sigmaIEtaIPhi, sigmaIEtaIPhi_);
writeValueMap(iEvent, src, e2x5Max, e2x5Max_);
writeValueMap(iEvent, src, e2x5Left, e2x5Left_);
writeValueMap(iEvent, src, e2x5Right, e2x5Right_);
writeValueMap(iEvent, src, e2x5Top, e2x5Top_);
writeValueMap(iEvent, src, e2x5Bottom, e2x5Bottom_);

lazyTools.reset(nullptr);
}

template<typename T>
void PhotonRegressionValueMapProducer::writeValueMap(edm::Event &iEvent,
const edm::Handle<edm::View<reco::Photon> > & handle,
const std::vector<float> & values,
const std::vector<T> & values,
const std::string & label) const
{
using namespace edm;
using namespace std;
auto_ptr<ValueMap<float> > valMap(new ValueMap<float>());
edm::ValueMap<float>::Filler filler(*valMap);
typedef ValueMap<T> TValueMap;

auto_ptr<TValueMap> valMap(new TValueMap());
typename TValueMap::Filler filler(*valMap);
filler.insert(handle, values.begin(), values.end());
filler.fill();
iEvent.put(valMap, label);
Expand Down
6 changes: 6 additions & 0 deletions RecoEgamma/PhotonIdentification/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<environment>
<bin file="runtestRecoEgammaPhotonIdentification.cpp">
<flags TEST_RUNNER_ARGS=" /bin/bash RecoEgamma/PhotonIdentification/test runtests.sh"/>
<use name="FWCore/Utilities"/>
</bin>
</environment>
Loading