Skip to content

Commit

Permalink
Make more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
kdlong committed Feb 22, 2021
1 parent e0ce707 commit d5d585c
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 27 deletions.
1 change: 1 addition & 0 deletions CommonTools/RecoAlgos/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<use name="TrackingTools/Records"/>
<use name="CommonTools/Utils"/>
<use name="CommonTools/RecoAlgos"/>
<use name="JetMETCorrections/JetCorrector"/>
</library>
136 changes: 136 additions & 0 deletions CommonTools/RecoAlgos/plugins/RecHitToPFCandAssociationProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// system include files
#include <memory>
#include <string>

// user include files
#include "FWCore/Framework/interface/stream/EDProducer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/Framework/interface/ESHandle.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
#include "SimDataFormats/CaloAnalysis/interface/SimClusterFwd.h"
#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
#include "DataFormats/CaloRecHit/interface/CaloRecHit.h"
#include "SimDataFormats/CaloHit/interface/PCaloHit.h"
#include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"
#include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"

#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
#include "DataFormats/ParticleFlowReco/interface/PFRecHit.h"
#include "DataFormats/ParticleFlowReco/interface/PFRecHitFraction.h"
#include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
#include "DataFormats/ParticleFlowReco/interface/PFBlockFwd.h"
#include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
#include "DataFormats/ParticleFlowReco/interface/PFClusterFwd.h"

#include "DataFormats/Common/interface/Association.h"

#include "FWCore/Utilities/interface/transform.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include <set>

//
// class decleration
//
typedef std::pair<size_t, float> IdxAndFraction;

class RecHitToPFCandAssociationProducer : public edm::stream::EDProducer<> {
public:
explicit RecHitToPFCandAssociationProducer(const edm::ParameterSet &);
~RecHitToPFCandAssociationProducer() override;

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

std::vector<edm::InputTag> caloRechitTags_;
std::vector<edm::EDGetTokenT<edm::PCaloHitContainer>> caloSimhitCollectionTokens_;
//std::vector<edm::EDGetTokenT<std::vector<PSimHit>> trackSimhitCollectionTokens_;
std::vector<edm::EDGetTokenT<edm::View<CaloRecHit>>> caloRechitCollectionTokens_;
edm::EDGetTokenT<reco::PFCandidateCollection> pfCollectionToken_;
};

RecHitToPFCandAssociationProducer::RecHitToPFCandAssociationProducer(const edm::ParameterSet &pset)
: //caloSimhitCollectionTokens_(edm::vector_transform(pset.getParameter<std::vector<edm::InputTag>>("caloSimHits"),
// [this](const edm::InputTag& tag) {return mayConsume<edm::PCaloHitContainer>(tag); })),
//trackSimhitCollectionTokens_(edm::vector_transform(pset.getParameter<edm::InputTag>("trackSimHits"),
// [this](const edm::InputTag& tag) {return mayConsume<std::vector<PSimHit>(tag); }),
caloRechitTags_(pset.getParameter<std::vector<edm::InputTag>>("caloRecHits")),
caloRechitCollectionTokens_(edm::vector_transform(caloRechitTags_,
[this](const edm::InputTag& tag) {return mayConsume<edm::View<CaloRecHit>>(tag); })),
pfCollectionToken_(consumes<reco::PFCandidateCollection>(pset.getParameter<edm::InputTag>("pfCands")))
{
for (auto& tag : caloRechitTags_) {
std::string label = tag.instance();
//TODO: Can this be an edm::View?
produces<edm::Association<reco::PFCandidateCollection>>(label+"ToPFCand");
}
}

RecHitToPFCandAssociationProducer::~RecHitToPFCandAssociationProducer() {}

//
// member functions
//

// ------------ method called to produce the data ------------
void RecHitToPFCandAssociationProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
edm::Handle<reco::PFCandidateCollection> pfCollection;
iEvent.getByToken(pfCollectionToken_, pfCollection);
std::unordered_map<size_t, IdxAndFraction> hitDetIdToIndex;

for (size_t j = 0; j < pfCollection->size(); j++) {
const auto& pfCand = pfCollection->at(j);
const reco::PFCandidate::ElementsInBlocks& elements = pfCand.elementsInBlocks();
for (auto& element : elements) {
const reco::PFBlockRef blockRef = element.first;
for (const auto& block : blockRef->elements()) {
if (block.type() == reco::PFBlockElement::HGCAL) {
const reco::PFClusterRef cluster = block.clusterRef();
const std::vector<reco::PFRecHitFraction>& rhf = cluster->recHitFractions();
for (const auto& hf : rhf) {
auto& hit = hf.recHitRef();
if (!hit)
throw cms::Exception("RecHitToPFCandAssociationProducer") << "Invalid RecHit ref";
size_t detId = hit->detId();
auto entry = hitDetIdToIndex.find(detId);
if (entry == hitDetIdToIndex.end() || entry->second.second < hf.fraction())
hitDetIdToIndex[detId] = {j, hf.fraction()};
}
}
}
}
}

for (size_t i = 0; i < caloRechitCollectionTokens_.size(); i++) {
std::string label = caloRechitTags_.at(i).instance();
std::vector<size_t> rechitIndices;

edm::Handle<edm::View<CaloRecHit>> caloRechitCollection;
iEvent.getByToken(caloRechitCollectionTokens_.at(i), caloRechitCollection);

for (size_t h = 0; h < caloRechitCollection->size(); h++) {
const CaloRecHit& caloRh = caloRechitCollection->at(h);
size_t id = caloRh.detid().rawId();
int match = hitDetIdToIndex.find(id) == hitDetIdToIndex.end() ? -1 : hitDetIdToIndex.at(id).first;
rechitIndices.push_back(match);
}

auto assoc = std::make_unique<edm::Association<reco::PFCandidateCollection>>(pfCollection);
edm::Association<reco::PFCandidateCollection>::Filler filler(*assoc);
filler.insert(caloRechitCollection, rechitIndices.begin(), rechitIndices.end());
filler.fill();
iEvent.put(std::move(assoc), label+"ToPFCand");
}
}

// define this as a plug-in
DEFINE_FWK_MODULE(RecHitToPFCandAssociationProducer);


115 changes: 115 additions & 0 deletions CommonTools/RecoAlgos/plugins/SimHitRecHitAssocitionProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// system include files
#include <memory>
#include <string>

// user include files
#include "FWCore/Framework/interface/stream/EDProducer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/Framework/interface/ESHandle.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
#include "SimDataFormats/CaloAnalysis/interface/SimClusterFwd.h"
#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
#include "DataFormats/CaloRecHit/interface/CaloRecHit.h"
#include "SimDataFormats/CaloHit/interface/PCaloHit.h"
#include "SimDataFormats/CaloHit/interface/PCaloHitContainer.h"


#include "DataFormats/Common/interface/Association.h"

#include "FWCore/Utilities/interface/transform.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include <set>

//
// class decleration
//
typedef std::pair<size_t, float> IdxAndFraction;

class SimHitRecHitAssociationProducer : public edm::stream::EDProducer<> {
public:
explicit SimHitRecHitAssociationProducer(const edm::ParameterSet &);
~SimHitRecHitAssociationProducer() override;

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

std::vector<edm::InputTag> caloRechitTags_;
std::vector<edm::EDGetTokenT<edm::PCaloHitContainer>> caloSimhitCollectionTokens_;
//std::vector<edm::EDGetTokenT<std::vector<PSimHit>> trackSimhitCollectionTokens_;
std::vector<edm::EDGetTokenT<edm::View<CaloRecHit>>> caloRechitCollectionTokens_;
edm::EDGetTokenT<SimClusterCollection> scCollectionToken_;
};

SimHitRecHitAssociationProducer::SimHitRecHitAssociationProducer(const edm::ParameterSet &pset)
: //caloSimhitCollectionTokens_(edm::vector_transform(pset.getParameter<std::vector<edm::InputTag>>("caloSimHits"),
// [this](const edm::InputTag& tag) {return mayConsume<edm::PCaloHitContainer>(tag); })),
//trackSimhitCollectionTokens_(edm::vector_transform(pset.getParameter<edm::InputTag>("trackSimHits"),
// [this](const edm::InputTag& tag) {return mayConsume<std::vector<PSimHit>(tag); }),
caloRechitTags_(pset.getParameter<std::vector<edm::InputTag>>("caloRecHits")),
caloRechitCollectionTokens_(edm::vector_transform(caloRechitTags_,
[this](const edm::InputTag& tag) {return mayConsume<edm::View<CaloRecHit>>(tag); })),
scCollectionToken_(consumes<SimClusterCollection>(pset.getParameter<edm::InputTag>("simClusters")))
{
for (auto& tag : caloRechitTags_) {
std::string label = tag.instance();
produces<edm::Association<SimClusterCollection>>(label+"ToSimClus");
}
}

SimHitRecHitAssociationProducer::~SimHitRecHitAssociationProducer() {}

//
// member functions
//

// ------------ method called to produce the data ------------
void SimHitRecHitAssociationProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
edm::Handle<SimClusterCollection> scCollection;
iEvent.getByToken(scCollectionToken_, scCollection);
std::unordered_map<size_t, IdxAndFraction> hitDetIdToIndex;

for (size_t s = 0; s < scCollection->size(); s++) {
const auto& sc = scCollection->at(s);
for (auto& hf : sc.hits_and_fractions()) {
auto entry = hitDetIdToIndex.find(hf.first);
// Update SimCluster assigment if detId has been found in no other SCs or if
// SC has greater fraction of energy in DetId than the SC already found
if (entry == hitDetIdToIndex.end() || entry->second.second < hf.second)
hitDetIdToIndex[hf.first] = {s, hf.second};
}
}

for (size_t i = 0; i < caloRechitCollectionTokens_.size(); i++) {
std::string label = caloRechitTags_.at(i).instance();
std::vector<size_t> rechitIndices;

edm::Handle<edm::View<CaloRecHit>> caloRechitCollection;
iEvent.getByToken(caloRechitCollectionTokens_.at(i), caloRechitCollection);
//edm::Handle<edm::PCaloHitContainer> caloSimhitCollection;
//iEvent.getByToken(caloSimhitCollectionTokens_.at(i), caloSimhitCollection);

for (size_t h = 0; h < caloRechitCollection->size(); h++) {
const CaloRecHit& caloRh = caloRechitCollection->at(h);
size_t id = caloRh.detid().rawId();
int match = hitDetIdToIndex.find(id) == hitDetIdToIndex.end() ? -1 : hitDetIdToIndex.at(id).first;
rechitIndices.push_back(match);
}

auto assoc = std::make_unique<edm::Association<SimClusterCollection>>(scCollection);
edm::Association<SimClusterCollection>::Filler filler(*assoc);
filler.insert(caloRechitCollection, rechitIndices.begin(), rechitIndices.end());
filler.fill();
iEvent.put(std::move(assoc), label+"ToSimClus");
}
}

// define this as a plug-in
DEFINE_FWK_MODULE(SimHitRecHitAssociationProducer);

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ typedef SimpleFlatTableProducer<SimCluster> SimpleSimClusterFlatTableProducer;

#include "SimDataFormats/CaloHit/interface/PCaloHit.h"
typedef SimpleFlatTableProducer<PCaloHit> SimplePCaloHitFlatTableProducer;

#include "SimDataFormats/TrackingHit/interface/PSimHit.h"
typedef SimpleFlatTableProducer<PSimHit> SimplePSimHitFlatTableProducer;
#include "DataFormats/CaloRecHit/interface/CaloRecHit.h"
typedef SimpleFlatTableProducer<CaloRecHit> SimpleCaloRecHitFlatTableProducer;

#include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
typedef SimpleFlatTableProducer<TrackingParticle> SimpleTrackingParticleFlatTableProducer;
Expand All @@ -23,6 +24,8 @@ typedef SimpleFlatTableProducer<reco::PFCandidate> SimplePFCandidateFlatTablePro

#include "SimDataFormats/Track/interface/SimTrack.h"
typedef SimpleFlatTableProducer<SimTrack> SimpleSimTrackFlatTableProducer;
#include "DataFormats/TrackReco/interface/Track.h"
typedef SimpleFlatTableProducer<reco::Track> SimpleTrackFlatTableProducer;

#include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
typedef EventSingletonSimpleFlatTableProducer<GenEventInfoProduct> SimpleGenEventFlatTableProducer;
Expand All @@ -41,10 +44,12 @@ typedef EventSingletonSimpleFlatTableProducer<math::XYZPointF> SimpleXYZPointFla

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(SimplePCaloHitFlatTableProducer);
DEFINE_FWK_MODULE(SimpleCaloRecHitFlatTableProducer);
DEFINE_FWK_MODULE(SimplePSimHitFlatTableProducer);
DEFINE_FWK_MODULE(SimpleSimTrackFlatTableProducer);
DEFINE_FWK_MODULE(SimpleSimClusterFlatTableProducer);
DEFINE_FWK_MODULE(SimpleTrackingParticleFlatTableProducer);
DEFINE_FWK_MODULE(SimpleTrackFlatTableProducer);
DEFINE_FWK_MODULE(SimplePFParticleFlatTableProducer);
DEFINE_FWK_MODULE(SimplePFCandidateFlatTableProducer);
DEFINE_FWK_MODULE(SimpleCaloParticleFlatTableProducer);
Expand Down
103 changes: 103 additions & 0 deletions PhysicsTools/NanoAOD/python/hgcRecHits_cff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import FWCore.ParameterSet.Config as cms
from PhysicsTools.NanoAOD.common_cff import Var,P3Vars

hgcEERecHitsTable = cms.EDProducer("SimpleCaloRecHitFlatTableProducer",
src = cms.InputTag("HGCalRecHit:HGCEERecHits"),
cut = cms.string(""),
name = cms.string("RecHitHGCEE"),
doc = cms.string("RecHits in HGCAL Electromagnetic endcap"),
singleton = cms.bool(False), # the number of entries is variable
extension = cms.bool(False), # this is the main table for the muons
variables = cms.PSet(
detId = Var('detid().rawId()', 'int', precision=-1, doc='detId'),
energy = Var('energy', 'float', precision=14, doc='energy'),
time = Var('time', 'float', precision=14, doc='hit time'),
)
)

hgcRecHitsToSimClusters = cms.EDProducer("SimHitRecHitAssociationProducer",
caloRecHits = cms.VInputTag("HGCalRecHit:HGCEERecHits",
"HGCalRecHit:HGCHEFRecHits", "HGCalRecHit:HGCHEBRecHits",
),
simClusters = cms.InputTag("mix:MergedCaloTruth"),
)

hgcEERecHitsToSimClusterTable = cms.EDProducer("CaloRecHitToSimClusterIndexTableProducer",
cut = hgcEERecHitsTable.cut,
src = hgcEERecHitsTable.src,
objName = hgcEERecHitsTable.name,
branchName = cms.string("SimCluster"),
objMap = cms.InputTag("hgcRecHitsToSimClusters:HGCEERecHitsToSimClus"),
docString = cms.string("SimCluster responsible for most sim energy in RecHit DetId")
)

hgcRecHitsToPFCands = cms.EDProducer("RecHitToPFCandAssociationProducer",
caloRecHits = cms.VInputTag("HGCalRecHit:HGCEERecHits",
"HGCalRecHit:HGCHEFRecHits", "HGCalRecHit:HGCHEBRecHits",
),
pfCands = cms.InputTag("particleFlow"),
)

hgcEERecHitsToPFCandTable = cms.EDProducer("CaloRecHitToPFCandIndexTableProducer",
cut = hgcEERecHitsTable.cut,
src = hgcEERecHitsTable.src,
objName = hgcEERecHitsTable.name,
branchName = cms.string("PFCand"),
objMap = cms.InputTag("hgcRecHitsToPFCands:HGCEERecHitsToPFCand"),
docString = cms.string("PFCand with most associated energy in RecHit DetId")
)

hgcHEfrontRecHitsTable = hgcEERecHitsTable.clone()
hgcHEfrontRecHitsTable.src = "HGCalRecHit:HGCHEFRecHits"
hgcHEfrontRecHitsTable.name = "RecHitHGCHEF"

hgcHEfrontRecHitsToSimClusterTable = hgcEERecHitsToSimClusterTable.clone()
hgcHEfrontRecHitsToSimClusterTable.src = hgcHEfrontRecHitsTable.src
hgcHEfrontRecHitsToSimClusterTable.objName = hgcHEfrontRecHitsTable.name
hgcHEfrontRecHitsToSimClusterTable.objMap = "hgcRecHitsToSimClusters:HGCHEFRecHitsToSimClus"

hgcHEfrontRecHitsToPFCandTable = hgcEERecHitsToPFCandTable.clone()
hgcHEfrontRecHitsToPFCandTable.src = hgcHEfrontRecHitsTable.src
hgcHEfrontRecHitsToPFCandTable.objName = hgcHEfrontRecHitsTable.name
hgcHEfrontRecHitsToPFCandTable.objMap = "hgcRecHitsToPFCands:HGCHEFRecHitsToPFCand"

hgcHEbackRecHitsTable = hgcEERecHitsTable.clone()
hgcHEbackRecHitsTable.src = "HGCalRecHit:HGCHEBRecHits"
hgcHEbackRecHitsTable.name = "RecHitHGCHEB"

hgcHEbackRecHitsToSimClusterTable = hgcEERecHitsToSimClusterTable.clone()
hgcHEbackRecHitsToSimClusterTable.src = hgcHEbackRecHitsTable.src
hgcHEbackRecHitsToSimClusterTable.objName = hgcHEbackRecHitsTable.name
hgcHEbackRecHitsToSimClusterTable.objMap = "hgcRecHitsToSimClusters:HGCHEBRecHitsToSimClus"

hgcHEbackRecHitsToPFCandTable = hgcEERecHitsToPFCandTable.clone()
hgcHEbackRecHitsToPFCandTable.src = hgcHEbackRecHitsTable.src
hgcHEbackRecHitsToPFCandTable.objName = hgcHEbackRecHitsTable.name
hgcHEbackRecHitsToPFCandTable.objMap = "hgcRecHitsToPFCands:HGCHEBRecHitsToPFCand"

hgcEERecHitsPositionTable = cms.EDProducer("HGCRecHitPositionFromDetIDTableProducer",
src = hgcEERecHitsTable.src,
cut = hgcEERecHitsTable.cut,
name = hgcEERecHitsTable.name,
doc = hgcEERecHitsTable.doc,
)
#
hgcHEfrontRecHitsPositionTable = hgcEERecHitsPositionTable.clone()
hgcHEfrontRecHitsPositionTable.name = hgcHEfrontRecHitsTable.name
hgcHEfrontRecHitsPositionTable.src = hgcHEfrontRecHitsTable.src

hgcHEbackRecHitsPositionTable = hgcEERecHitsPositionTable.clone()
hgcHEbackRecHitsPositionTable.name = hgcHEbackRecHitsTable.name
hgcHEbackRecHitsPositionTable.src = hgcHEbackRecHitsTable.src

hgcRecHitsSequence = cms.Sequence(hgcEERecHitsTable+hgcHEbackRecHitsTable+hgcHEfrontRecHitsTable
+hgcRecHitsToSimClusters
+hgcRecHitsToPFCands
+hgcEERecHitsToPFCandTable+hgcHEfrontRecHitsToPFCandTable+hgcHEbackRecHitsToPFCandTable
+hgcEERecHitsPositionTable
+hgcHEfrontRecHitsPositionTable
+hgcHEbackRecHitsPositionTable
+hgcEERecHitsToSimClusterTable
+hgcHEfrontRecHitsToSimClusterTable
+hgcHEbackRecHitsToSimClusterTable
)
Loading

0 comments on commit d5d585c

Please sign in to comment.