Skip to content

Commit

Permalink
Merge pull request #8249 from jshlee/puppiIntoMiniAOD
Browse files Browse the repository at this point in the history
Puppi into mini aod
  • Loading branch information
cmsbuild committed Mar 15, 2015
2 parents 9975094 + ad33789 commit 592ca91
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 19 deletions.
32 changes: 26 additions & 6 deletions CommonTools/PileupAlgos/plugins/PuppiProducer.cc
Expand Up @@ -12,12 +12,15 @@
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "DataFormats/Common/interface/View.h"
#include "DataFormats/Candidate/interface/Candidate.h"
#include "DataFormats/Candidate/interface/CandidateFwd.h"
#include "DataFormats/PatCandidates/interface/PackedCandidate.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"
#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
#include "DataFormats/GsfTrackReco/interface/GsfTrackFwd.h"
#include "DataFormats/Common/interface/Association.h"
//Main File
#include "fastjet/PseudoJet.hh"
#include "CommonTools/PileupAlgos/plugins/PuppiProducer.h"
Expand All @@ -36,8 +39,10 @@ PuppiProducer::PuppiProducer(const edm::ParameterSet& iConfig) {
= consumes<VertexCollection>(iConfig.getParameter<edm::InputTag>("vertexName"));


produces<edm::ValueMap<float> > ("PuppiWeights");
produces<edm::ValueMap<LorentzVector> > ("PuppiP4s");
produces<edm::ValueMap<float> > ();
produces<edm::ValueMap<LorentzVector> > ();
produces< edm::ValueMap<reco::CandidatePtr> >();

produces<PFOutputCollection>();


Expand Down Expand Up @@ -128,6 +133,7 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
lPupFiller.insert(hPFProduct,lWeights.begin(),lWeights.end());
lPupFiller.fill();


// This is a dummy to access the "translate" method which is a
// non-static member function even though it doesn't need to be.
// Will fix in the future.
Expand All @@ -141,6 +147,9 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
fPuppiCandidates.reset( new PFOutputCollection );
std::auto_ptr<edm::ValueMap<LorentzVector> > p4PupOut(new edm::ValueMap<LorentzVector>());
LorentzVectorCollection puppiP4s;
std::vector<reco::CandidatePtr> values(hPFProduct->size());
//std::vector<int> values(hPFProduct->size());

for ( auto i0 = hPFProduct->begin(),
i0begin = hPFProduct->begin(),
i0end = hPFProduct->end(); i0 != i0end; ++i0 ) {
Expand Down Expand Up @@ -169,10 +178,21 @@ void PuppiProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
edm::ValueMap<LorentzVector>::Filler p4PupFiller(*p4PupOut);
p4PupFiller.insert(hPFProduct,puppiP4s.begin(), puppiP4s.end() );
p4PupFiller.fill();

iEvent.put(lPupOut,"PuppiWeights");
iEvent.put(p4PupOut,"PuppiP4s");
iEvent.put(fPuppiCandidates);

iEvent.put(lPupOut);
iEvent.put(p4PupOut);
edm::OrphanHandle<reco::PFCandidateCollection> oh = iEvent.put( fPuppiCandidates );
for(unsigned int ic=0, nc = oh->size(); ic < nc; ++ic) {
reco::CandidatePtr pkref( oh, ic );
values[ic] = pkref;

}
std::auto_ptr<edm::ValueMap<reco::CandidatePtr> > pfMap_p(new edm::ValueMap<reco::CandidatePtr>());
edm::ValueMap<reco::CandidatePtr>::Filler filler(*pfMap_p);
filler.insert(hPFProduct, values.begin(), values.end());
filler.fill();
iEvent.put(pfMap_p);

}

// ------------------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions DataFormats/PatCandidates/interface/PackedCandidate.h
Expand Up @@ -345,6 +345,10 @@ namespace pat {
virtual bool isConvertedPhoton() const { return false; }
virtual bool isJet() const { return false; }

// puppiweight
void setPuppiWeight(float p);
float puppiWeight() const;

protected:
uint16_t packedPt_, packedEta_, packedPhi_, packedM_;
uint16_t packedDxy_, packedDz_, packedDPhi_;
Expand All @@ -359,6 +363,7 @@ namespace pat {
void packBoth() { pack(false); packVtx(false); unpack(); unpackVtx(); } // do it this way, so that we don't loose precision on the angles before computing dxy,dz
void unpackTrk() const ;

int8_t packedPuppiweight_;
/// the four vector
mutable PolarLorentzVector p4_;
mutable LorentzVector p4c_;
Expand Down
35 changes: 31 additions & 4 deletions DataFormats/PatCandidates/interface/liblogintpack.h
Expand Up @@ -11,8 +11,8 @@ namespace logintpack
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*base;
int8_t r=ceil(centered);
if(centered >= base-1) return r=base-1;
if(centered < 0) return r=0;
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
return r;
}
Expand All @@ -23,12 +23,27 @@ namespace logintpack
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*base;
int8_t r=centered;
if(centered >= base-1) return r=base-1;
if(centered < 0) return r=0;
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
return r;
}

/// pack a value x distributed in [-1,1], with guarantee that -1 and 1 are preserved exactly in packing and unpacking.
/// tries to keep the best precision for x close to the endpoints, sacrifying that in the middle
int8_t pack8logClosed(double x,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*(base-1);
int8_t r=round(centered);
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
return r;
}


double unpack8log(int8_t i,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
Expand All @@ -37,5 +52,17 @@ namespace logintpack
float val=exp(l);
if(i<0) return -val; else return val;
}

/// reverse of pack8logClosed
double unpack8logClosed(int8_t i,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float basef=base-1;
float l=lmin+abs(i)/basef*(lmax-lmin);
if (abs(i) == base-1) l = lmax;
float val=exp(l);
if(i<0) return -val; else return val;
}

}
#endif
4 changes: 3 additions & 1 deletion DataFormats/PatCandidates/src/PackedCandidate.cc
Expand Up @@ -268,6 +268,8 @@ bool pat::PackedCandidate::longLived() const {return false;}

bool pat::PackedCandidate::massConstraint() const {return false;}

// puppiweight
void pat::PackedCandidate::setPuppiWeight(float p) { packedPuppiweight_ = pack8logClosed((p-0.5)*2,-2,0,64);}


float pat::PackedCandidate::puppiWeight() const { return unpack8logClosed(packedPuppiweight_,-2,0,64)/2. + 0.5;}

4 changes: 2 additions & 2 deletions DataFormats/PatCandidates/src/classes_def_objects.xml
Expand Up @@ -254,8 +254,8 @@
<version ClassVersion="11" checksum="1239840459"/>
</class>

<class name="pat::PackedCandidate" ClassVersion="16">
<version ClassVersion="17" checksum="1633877368"/>
<class name="pat::PackedCandidate" ClassVersion="17">
<version ClassVersion="17" checksum="1257500115"/>
<version ClassVersion="16" checksum="3261782486"/>
<version ClassVersion="15" checksum="2118306102"/>
<version ClassVersion="14" checksum="1075333340"/>
Expand Down
38 changes: 32 additions & 6 deletions PhysicsTools/PatAlgos/plugins/PATPackedCandidateProducer.cc
Expand Up @@ -69,6 +69,10 @@ namespace pat {
edm::EDGetTokenT<edm::ValueMap<int> > PVAssoQuality_;
edm::EDGetTokenT<reco::VertexCollection> PVOrigs_;
edm::EDGetTokenT<reco::TrackCollection> TKOrigs_;
edm::EDGetTokenT< edm::ValueMap<float> > PuppiWeight_;
edm::EDGetTokenT<edm::ValueMap<reco::CandidatePtr> > PuppiCandsMap_;
edm::EDGetTokenT<std::vector< reco::PFCandidate > > PuppiCands_;

double minPtForTrackProperties_;
// for debugging
float calcDxy(float dx, float dy, float phi) {
Expand All @@ -87,6 +91,9 @@ pat::PATPackedCandidateProducer::PATPackedCandidateProducer(const edm::Parameter
PVAssoQuality_(consumes<edm::ValueMap<int> >(iConfig.getParameter<edm::InputTag>("vertexAssociator"))),
PVOrigs_(consumes<reco::VertexCollection>(iConfig.getParameter<edm::InputTag>("originalVertices"))),
TKOrigs_(consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("originalTracks"))),
PuppiWeight_(consumes<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("PuppiSrc"))),
PuppiCandsMap_(consumes<edm::ValueMap<reco::CandidatePtr> >(iConfig.getParameter<edm::InputTag>("PuppiSrc"))),
PuppiCands_(consumes<std::vector< reco::PFCandidate > >(iConfig.getParameter<edm::InputTag>("PuppiSrc"))),
minPtForTrackProperties_(iConfig.getParameter<double>("minPtForTrackProperties"))
{
produces< std::vector<pat::PackedCandidate> > ();
Expand All @@ -104,6 +111,14 @@ void pat::PATPackedCandidateProducer::produce(edm::Event& iEvent, const edm::Eve
iEvent.getByToken( Cands_, cands );
std::vector<reco::Candidate>::const_iterator cand;

edm::Handle< edm::ValueMap<float> > puppiWeight;
iEvent.getByToken( PuppiWeight_, puppiWeight );
edm::Handle<edm::ValueMap<reco::CandidatePtr> > puppiCandsMap;
iEvent.getByToken( PuppiCandsMap_, puppiCandsMap );
edm::Handle<std::vector< reco::PFCandidate > > puppiCands;
iEvent.getByToken( PuppiCands_, puppiCands );
std::vector<int> mappingPuppi(puppiCands->size());

edm::Handle<reco::VertexCollection> PVOrigs;
iEvent.getByToken( PVOrigs_, PVOrigs );

Expand Down Expand Up @@ -194,28 +209,38 @@ void pat::PATPackedCandidateProducer::produce(edm::Event& iEvent, const edm::Eve
outPtrP->push_back( pat::PackedCandidate(cand.polarP4(), PVpos, cand.phi(), cand.pdgId(), PV));
outPtrP->back().setAssociationQuality(pat::PackedCandidate::PVAssociationQuality(pat::PackedCandidate::UsedInFitTight));
}


mapping[ic] = ic;

if (puppiWeight.isValid()){
reco::PFCandidateRef pkref( cands, ic );
outPtrP->back().setPuppiWeight( (*puppiWeight)[pkref]);
mappingPuppi[((*puppiCandsMap)[pkref]).key()]=ic;
}

mapping[ic] = ic; // trivial at the moment!
if (cand.trackRef().isNonnull() && cand.trackRef().id() == TKOrigs.id()) {
mappingTk[cand.trackRef().key()] = ic;
mappingTk[cand.trackRef().key()] = ic;
}

}

std::auto_ptr< std::vector<pat::PackedCandidate> > outPtrPSorted( new std::vector<pat::PackedCandidate> );
std::vector<size_t> order=sort_indexes(*outPtrP);
std::vector<size_t> reverseOrder(order.size());
for(size_t i=0,nc=cands->size();i<nc;i++) {
outPtrPSorted->push_back((*outPtrP)[order[i]]);
reverseOrder[order[i]] = i;
mappingReverse[order[i]]=i;
}

// Fix track association for sorted candidates
for(size_t i=0,ntk=mappingTk.size();i<ntk;i++){
mappingTk[i]=order[mappingTk[i]];
mappingTk[i]=reverseOrder[mappingTk[i]];
}

for(size_t i=0,ntk=mappingPuppi.size();i<ntk;i++){
mappingPuppi[i]=reverseOrder[mappingPuppi[i]];
}


edm::OrphanHandle<pat::PackedCandidateCollection> oh = iEvent.put( outPtrPSorted );

// now build the two maps
Expand All @@ -227,6 +252,7 @@ void pat::PATPackedCandidateProducer::produce(edm::Event& iEvent, const edm::Eve
pc2pfFiller.insert(oh , order.begin(), order.end());
// include also the mapping track -> packed PFCand
pf2pcFiller.insert(TKOrigs, mappingTk.begin(), mappingTk.end());
pf2pcFiller.insert(puppiCands, mappingPuppi.begin(), mappingPuppi.end());

pf2pcFiller.fill();
pc2pfFiller.fill();
Expand Down
51 changes: 51 additions & 0 deletions PhysicsTools/PatAlgos/python/slimming/miniAOD_tools.py
Expand Up @@ -174,6 +174,57 @@ def miniAOD_customizeCommon(process):
for idmod in electron_ids:
setupAllVIDIdsInModule(process,idmod,setupVIDElectronSelection)

# Adding puppi jets
process.load('CommonTools.PileupAlgos.Puppi_cff')
process.load('RecoJets.JetProducers.ak4PFJetsPuppi_cfi')
#process.puppi.candName = cms.InputTag('packedPFCandidates')
#process.puppi.vertexName = cms.InputTag('offlineSlimmedPrimaryVertices')

from RecoJets.JetAssociationProducers.j2tParametersVX_cfi import j2tParametersVX
process.ak4PFJetsPuppiTracksAssociatorAtVertex = cms.EDProducer("JetTracksAssociatorAtVertex",
j2tParametersVX,
jets = cms.InputTag("ak4PFJetsPuppi")
)
process.patJetPuppiCharge = cms.EDProducer("JetChargeProducer",
src = cms.InputTag("ak4PFJetsPuppiTracksAssociatorAtVertex"),
var = cms.string('Pt'),
exp = cms.double(1.0)
)

addJetCollection(process, postfix = "", labelName = 'Puppi', jetSource = cms.InputTag('ak4PFJetsPuppi'),
jetCorrections = ('AK4PF', ['L1FastJet', 'L2Relative', 'L3Absolute'], ''),
algo= 'AK', rParam = 0.4, btagDiscriminators = map(lambda x: x.value() ,process.patJets.discriminatorSources)
)

process.patJetGenJetMatchPuppi.matched = 'slimmedGenJets'

process.patJetsPuppi.userData.userFloats.src = cms.VInputTag(cms.InputTag(""))
process.patJetsPuppi.jetChargeSource = cms.InputTag("patJetPuppiCharge")
process.patJetsPuppi.tagInfoSources = cms.VInputTag(cms.InputTag("pfSecondaryVertexTagInfosPuppi"))
process.patJetsPuppi.addTagInfos = cms.bool(True)

process.selectedPatJetsPuppi.cut = cms.string("pt > 20")

process.load('PhysicsTools.PatAlgos.slimming.slimmedJets_cfi')
process.slimmedJetsPuppi = process.slimmedJets.clone()
process.slimmedJetsPuppi.src = cms.InputTag("selectedPatJetsPuppi")
process.slimmedJetsPuppi.packedPFCandidates = cms.InputTag("packedPFCandidates")

## puppi met
process.load('RecoMET.METProducers.PFMET_cfi')
process.pfMetPuppi = process.pfMet.clone()
process.pfMetPuppi.src = cms.InputTag("puppi")
process.pfMetPuppi.alias = cms.string('pfMetPuppi')

from PhysicsTools.PatAlgos.tools.metTools import addMETCollection
addMETCollection(process, labelName='patMETPuppi', metSource='pfMetPuppi')

process.load('PhysicsTools.PatAlgos.slimming.slimmedMETs_cfi')
process.slimmedMETsPuppi = process.slimmedMETs.clone()
process.slimmedMETsPuppi.src = cms.InputTag("patMETPuppi")
process.slimmedMETsPuppi.rawUncertainties = cms.InputTag("patPFMet%s")
process.slimmedMETsPuppi.type1Uncertainties = cms.InputTag("patPFMetT1%s")
process.slimmedMETsPuppi.type1p2Uncertainties = cms.InputTag("patPFMetT1T2%s")


def miniAOD_customizeMC(process):
Expand Down
Expand Up @@ -6,5 +6,6 @@
originalVertices = cms.InputTag("offlinePrimaryVertices"),
originalTracks = cms.InputTag("generalTracks"),
vertexAssociator = cms.InputTag("primaryVertexAssociation","original"),
PuppiSrc = cms.InputTag("puppi"),
minPtForTrackProperties = cms.double(0.95)
)

0 comments on commit 592ca91

Please sign in to comment.