forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cms-sw#24 from jhgoh/edmTuple
Flat edm tuple using LeafCandidate and ValueMap
- Loading branch information
Showing
3 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include "FWCore/Framework/interface/Frameworkfwd.h" | ||
#include "FWCore/Framework/interface/EDProducer.h" | ||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
|
||
#include "FWCore/Framework/interface/Event.h" | ||
#include "FWCore/Framework/interface/EventSetup.h" | ||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSet.h" | ||
#include "FWCore/Utilities/interface/InputTag.h" | ||
|
||
#include "DataFormats/Common/interface/View.h" | ||
#include "DataFormats/Candidate/interface/LeafCandidate.h" | ||
#include "DataFormats/Common/interface/OneToValue.h" | ||
#include "DataFormats/PatCandidates/interface/Muon.h" | ||
#include "DataFormats/PatCandidates/interface/Electron.h" | ||
#include "DataFormats/PatCandidates/interface/Jet.h" | ||
|
||
#include "CommonTools/Utils/interface/StringObjectFunction.h" | ||
|
||
#include <memory> | ||
#include <vector> | ||
#include <string> | ||
#include <boost/assign.hpp> | ||
#include <boost/algorithm/string.hpp> | ||
|
||
using namespace std; | ||
using namespace boost::assign; | ||
|
||
class FlatCandProducer : public edm::EDProducer | ||
{ | ||
public: | ||
FlatCandProducer(const edm::ParameterSet& pset); | ||
void produce(edm::Event& event, const edm::EventSetup& eventSetup); | ||
|
||
private: | ||
typedef std::vector<double> doubles; | ||
typedef std::vector<std::string> strings; | ||
|
||
typedef std::vector<reco::LeafCandidate> Cands; | ||
typedef edm::ValueMap<double> CandValueMap; | ||
typedef edm::RefProd<Cands> CandRefProd; | ||
typedef edm::Ref<Cands> CandRef; | ||
|
||
edm::InputTag srcLabel_; | ||
std::vector<StringObjectFunction<reco::Candidate,true> > exprs_; | ||
std::vector<edm::InputTag> vmapLabels_; | ||
|
||
strings varNames_; | ||
std::vector<doubles> values_; | ||
|
||
}; | ||
|
||
FlatCandProducer::FlatCandProducer(const edm::ParameterSet& pset) | ||
{ | ||
srcLabel_ = pset.getParameter<edm::InputTag>("src"); | ||
edm::ParameterSet vars = pset.getParameter<edm::ParameterSet>("variables"); | ||
|
||
const strings strVars = vars.getParameterNamesForType<std::string>(); | ||
for ( int i=0, n=strVars.size(); i<n; ++i ) | ||
{ | ||
const string& varName = strVars[i]; | ||
const string& varExpr = vars.getParameter<string>(varName); | ||
exprs_.push_back(StringObjectFunction<reco::Candidate,true>(varExpr)); | ||
varNames_.push_back(varName); | ||
} | ||
const strings vmapNames = vars.getParameterNamesForType<edm::InputTag>(); | ||
for ( int i=0, n=vmapNames.size(); i<n; ++i ) | ||
{ | ||
const string& vmapName = vmapNames[i]; | ||
edm::InputTag vmapLabel = vars.getParameter<edm::InputTag>(vmapName); | ||
vmapLabels_.push_back(vmapLabel); | ||
varNames_.push_back(vmapName); | ||
} | ||
|
||
values_.resize(varNames_.size()); | ||
|
||
produces<Cands>(); | ||
for ( int i=0, n=varNames_.size(); i<n; ++i ) | ||
{ | ||
produces<CandValueMap>(varNames_[i]); | ||
} | ||
} | ||
|
||
void FlatCandProducer::produce(edm::Event& event, const edm::EventSetup& eventSetup) | ||
{ | ||
edm::Handle<edm::View<reco::Candidate> > srcHandle; | ||
event.getByLabel(srcLabel_, srcHandle); | ||
|
||
const size_t nExpr = exprs_.size(); | ||
const size_t nVmap = vmapLabels_.size(); | ||
std::vector<edm::Handle<edm::ValueMap<double> > > vmapHandles(nVmap); | ||
for ( size_t i=0; i<nVmap; ++i ) | ||
{ | ||
event.getByLabel(vmapLabels_[i], vmapHandles[i]); | ||
} | ||
|
||
std::auto_ptr<Cands> cands(new Cands); | ||
|
||
// Fill informations | ||
for ( size_t i=0, n=srcHandle->size(); i<n; ++i ) | ||
{ | ||
edm::Ref<edm::View<reco::Candidate> > candRef(srcHandle, i); | ||
reco::LeafCandidate cand(candRef->charge(), candRef->p4()); | ||
cands->push_back(cand); | ||
for ( size_t j=0; j<nExpr; ++j ) | ||
{ | ||
values_[j].push_back(exprs_[j](*candRef)); | ||
} | ||
for ( size_t j=0; j<nVmap; ++j ) | ||
{ | ||
values_[j+nExpr].push_back((*vmapHandles[j])[candRef]); | ||
} | ||
} | ||
|
||
edm::OrphanHandle<Cands> outHandle = event.put(cands); | ||
for ( size_t i=0, n=nExpr+nVmap; i<n; ++i ) | ||
{ | ||
std::auto_ptr<CandValueMap> vmap(new CandValueMap); | ||
CandValueMap::Filler filler(*vmap); | ||
filler.insert(outHandle, values_[i].begin(), values_[i].end()); | ||
filler.fill(); | ||
values_[i].clear(); | ||
|
||
const std::string& varName = varNames_[i]; | ||
event.put(vmap, varName); | ||
} | ||
} | ||
|
||
DEFINE_FWK_MODULE(FlatCandProducer); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include "FWCore/Framework/interface/Frameworkfwd.h" | ||
#include "FWCore/Framework/interface/EDAnalyzer.h" | ||
#include "FWCore/Framework/interface/MakerMacros.h" | ||
|
||
#include "FWCore/Framework/interface/Event.h" | ||
#include "FWCore/Framework/interface/EventSetup.h" | ||
#include "FWCore/Framework/interface/LuminosityBlock.h" | ||
#include "FWCore/MessageLogger/interface/MessageLogger.h" | ||
#include "FWCore/ParameterSet/interface/ParameterSet.h" | ||
#include "FWCore/Utilities/interface/InputTag.h" | ||
#include "DataFormats/Common/interface/MergeableCounter.h" | ||
|
||
#include "FWCore/ServiceRegistry/interface/Service.h" | ||
#include "CommonTools/UtilAlgos/interface/TFileService.h" | ||
|
||
#include "DataFormats/Common/interface/View.h" | ||
#include "DataFormats/Common/interface/ValueMap.h" | ||
#include "DataFormats/Candidate/interface/LeafCandidate.h" | ||
|
||
#include "KrAFT/GenericNtuple/interface/GenericEvent.h" | ||
|
||
#include "TTree.h" | ||
#include "TH1F.h" | ||
|
||
#include <memory> | ||
#include <vector> | ||
#include <string> | ||
|
||
using namespace std; | ||
using namespace edm; | ||
|
||
class FlatCandToNtupleMaker : public edm::EDAnalyzer | ||
{ | ||
public: | ||
FlatCandToNtupleMaker(const edm::ParameterSet& pset); | ||
~FlatCandToNtupleMaker() {}; | ||
|
||
void analyze(const edm::Event& event, const edm::EventSetup& eventSetup); | ||
|
||
private: | ||
typedef std::vector<double> doubles; | ||
typedef std::vector<std::string> strings; | ||
typedef std::vector<edm::InputTag> VInputTag; | ||
|
||
std::vector<edm::InputTag> candLabels_; | ||
std::vector<VInputTag> vmapLabels_; | ||
|
||
TTree* tree_; | ||
std::vector<doubles*> candPt_, candEta_, candPhi_, candM_; | ||
std::vector<std::vector<doubles*> > candVars_; | ||
|
||
}; | ||
|
||
FlatCandToNtupleMaker::FlatCandToNtupleMaker(const edm::ParameterSet& pset) | ||
{ | ||
// Output histograms and tree | ||
edm::Service<TFileService> fs; | ||
tree_ = fs->make<TTree>("event", "event"); | ||
|
||
edm::ParameterSet candPSets = pset.getParameter<edm::ParameterSet>("cands"); | ||
const strings candNames = candPSets.getParameterNamesForType<edm::ParameterSet>(); | ||
for ( size_t i=0, n=candNames.size(); i<n; ++i ) | ||
{ | ||
const string& candName = candNames[i]; | ||
edm::ParameterSet candPSet = candPSets.getParameter<edm::ParameterSet>(candName); | ||
candLabels_.push_back(candPSet.getParameter<edm::InputTag>("src")); | ||
|
||
candPt_ .push_back(new doubles); | ||
candEta_.push_back(new doubles); | ||
candPhi_.push_back(new doubles); | ||
candM_ .push_back(new doubles); | ||
|
||
tree_->Branch((candName+"_pt" ).c_str(), candPt_ .back()); | ||
tree_->Branch((candName+"_eta").c_str(), candEta_.back()); | ||
tree_->Branch((candName+"_phi").c_str(), candPhi_.back()); | ||
tree_->Branch((candName+"_m" ).c_str(), candM_ .back()); | ||
|
||
vmapLabels_.push_back(VInputTag()); | ||
candVars_.push_back(std::vector<doubles*>()); | ||
const string candLabelName = candLabels_.back().label(); | ||
const strings vmapNames = candPSet.getParameter<strings>("vmaps"); | ||
for ( size_t j=0, m=vmapNames.size(); j<m; ++j ) | ||
{ | ||
const string& vmapName = vmapNames[j]; | ||
candVars_.back().push_back(new doubles); | ||
vmapLabels_.back().push_back(edm::InputTag(candLabelName, vmapName)); | ||
|
||
tree_->Branch((candName+"_"+vmapName).c_str(), candVars_.back().back()); | ||
} | ||
} | ||
|
||
} | ||
|
||
void FlatCandToNtupleMaker::analyze(const edm::Event& event, const edm::EventSetup& eventSetup) | ||
{ | ||
typedef edm::View<reco::LeafCandidate> Cands; | ||
|
||
const size_t nCand = candLabels_.size(); | ||
for ( size_t iCand=0; iCand < nCand; ++iCand ) | ||
{ | ||
edm::Handle<Cands> srcHandle; | ||
event.getByLabel(candLabels_[iCand], srcHandle); | ||
|
||
VInputTag vmapLabels = vmapLabels_[iCand]; | ||
const size_t nVar = vmapLabels.size(); | ||
std::vector<edm::Handle<edm::ValueMap<double> > > vmapHandles(nVar); | ||
for ( size_t iVar=0; iVar<nVar; ++iVar ) | ||
{ | ||
event.getByLabel(vmapLabels[iVar], vmapHandles[iVar]); | ||
} | ||
|
||
for ( size_t i=0, n=srcHandle->size(); i<n; ++i ) | ||
{ | ||
edm::Ref<Cands> candRef(srcHandle, i); | ||
candPt_[iCand]->push_back(candRef->pt()); | ||
candEta_[iCand]->push_back(candRef->eta()); | ||
candPhi_[iCand]->push_back(candRef->phi()); | ||
candM_[iCand]->push_back(candRef->mass()); | ||
|
||
for ( size_t iVar=0; iVar<nVar; ++iVar ) | ||
{ | ||
const double var = (*vmapHandles[iVar])[candRef]; | ||
candVars_[iCand][iVar]->push_back(var); | ||
} | ||
} | ||
} | ||
|
||
tree_->Fill(); | ||
for ( size_t iCand=0; iCand<nCand; ++iCand ) | ||
{ | ||
candPt_ [iCand]->clear(); | ||
candEta_[iCand]->clear(); | ||
candPhi_[iCand]->clear(); | ||
candM_ [iCand]->clear(); | ||
const size_t nVar = candVars_[iCand].size(); | ||
for ( size_t iVar=0; iVar<nVar; ++iVar ) | ||
{ | ||
candVars_[iCand][iVar]->clear(); | ||
} | ||
} | ||
} | ||
|
||
DEFINE_FWK_MODULE(FlatCandToNtupleMaker); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import FWCore.ParameterSet.Config as cms | ||
|
||
flatMuons = cms.EDProducer("FlatCandProducer", | ||
src = cms.InputTag("goodMuons"), | ||
variables = cms.PSet( | ||
#isTight = cms.string("userInt('isTight')"), | ||
#isLoose = cms.string("userInt('isLoose')"), | ||
relIso = cms.string("userIso(1)"), | ||
dxy = cms.string("dB"), | ||
dz = cms.string("userFloat('dz')"), | ||
), | ||
) | ||
|
||
flatElectrons = cms.EDProducer("FlatCandProducer", | ||
src = cms.InputTag("goodElectrons"), | ||
variables = cms.PSet( | ||
mva = cms.string("electronID('mvaTrigV0')"), | ||
relIso = cms.string("userIso(2)"), | ||
scEta = cms.string("superCluster.eta"), | ||
dxy = cms.string("dB"), | ||
dz = cms.string("userFloat('dz')"), | ||
#chargeID = cms.string("isGsfCtfScPixChargeConsistent ? 3 : isGsfScPixChargeConsistent ? 2 : isGsfCtfChargeConsistent ? 1 : 0"), | ||
), | ||
) | ||
|
||
flatJets = cms.EDProducer("FlatCandProducer", | ||
src = cms.InputTag("goodJets"), | ||
variables = cms.PSet( | ||
bTagCSV = cms.string("bDiscriminator('combinedSecondaryVertexBJetTags')"), | ||
up = cms.InputTag("goodJets", "up"), | ||
dn = cms.InputTag("goodJets", "dn"), | ||
res = cms.InputTag("goodJets", "res"), | ||
resUp = cms.InputTag("goodJets", "resUp"), | ||
resDn = cms.InputTag("goodJets", "resDn"), | ||
), | ||
) | ||
|
||
fEvent = cms.EDAnalyzer("FlatCandToNtupleMaker", | ||
cands = cms.PSet( | ||
muons = cms.PSet( | ||
src = cms.InputTag("flatMuons"), | ||
vmaps = cms.vstring( | ||
#"isTight", "isLoose", | ||
"relIso", "dxy", "dz", | ||
), | ||
), | ||
electrons = cms.PSet( | ||
src = cms.InputTag("flatElectrons"), | ||
vmaps = cms.vstring( | ||
"mva", "scEta", | ||
"relIso", "dxy", "dz", | ||
#"chargeID", | ||
), | ||
), | ||
jets = cms.PSet( | ||
src = cms.InputTag("flatJets"), | ||
vmaps = cms.vstring( | ||
"bTagCSV", "up", "dn", "res", "resUp", "resDn", | ||
), | ||
), | ||
), | ||
) |