Skip to content

Commit

Permalink
Merge pull request #33333 from lecriste/tracksterToSimClAssociator
Browse files Browse the repository at this point in the history
[HGCAL] Tracksters to SimClusters associator
  • Loading branch information
cmsbuild authored Apr 13, 2021
2 parents 645f53d + 087cef2 commit 02d443b
Show file tree
Hide file tree
Showing 13 changed files with 924 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DataFormats/HGCalReco/interface/Trackster.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,7 @@ namespace ticl {
// trackster ID probabilities
std::array<float, 8> id_probabilities_;
};

typedef std::vector<Trackster> TracksterCollection;
} // namespace ticl
#endif

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Original Author: Leonardo Cristella

#include <vector>
#include <map>
#include <unordered_map>
#include <memory> // shared_ptr

#include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
#include "DataFormats/HGCRecHit/interface/HGCRecHit.h"
#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h"
#include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"

namespace edm {
class EDProductGetter;
}

namespace hgcal {
struct detIdInfoInCluster {
bool operator==(const detIdInfoInCluster &o) const { return clusterId == o.clusterId; };
long unsigned int clusterId;
float fraction;
detIdInfoInCluster(long unsigned int cId, float fr) {
clusterId = cId;
fraction = fr;
}
};

struct simClusterOnLayer {
unsigned int simClusterId;
float energy = 0;
std::vector<std::pair<DetId, float>> hits_and_fractions;
std::unordered_map<int, std::pair<float, float>> tracksterIdToEnergyAndScore;
};

typedef std::vector<std::vector<std::pair<unsigned int, float>>> tracksterToSimCluster;
typedef std::vector<hgcal::simClusterOnLayer> simClusterToTrackster;
typedef std::tuple<tracksterToSimCluster, simClusterToTrackster> association;
} // namespace hgcal

class TracksterAssociatorByEnergyScoreImpl : public hgcal::TracksterToSimClusterAssociatorBaseImpl {
public:
explicit TracksterAssociatorByEnergyScoreImpl(edm::EDProductGetter const &,
bool,
std::shared_ptr<hgcal::RecHitTools>,
const std::unordered_map<DetId, const HGCRecHit *> *);

hgcal::RecoToSimCollectionTracksters associateRecoToSim(const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const override;

hgcal::SimToRecoCollectionTracksters associateSimToReco(const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const override;

private:
const bool hardScatterOnly_;
std::shared_ptr<hgcal::RecHitTools> recHitTools_;
const std::unordered_map<DetId, const HGCRecHit *> *hitMap_;
unsigned layers_;
edm::EDProductGetter const *productGetter_;
hgcal::association makeConnections(const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Original author: Leonardo Cristella

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

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

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/ESGetToken.h"

#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h"
#include "TracksterAssociatorByEnergyScoreImpl.h"

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

static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);

private:
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
edm::EDGetTokenT<std::unordered_map<DetId, const HGCRecHit *>> hitMap_;
edm::ESGetToken<CaloGeometry, CaloGeometryRecord> caloGeometry_;
const bool hardScatterOnly_;
std::shared_ptr<hgcal::RecHitTools> rhtools_;
};

TracksterAssociatorByEnergyScoreProducer::TracksterAssociatorByEnergyScoreProducer(const edm::ParameterSet &ps)
: hitMap_(consumes<std::unordered_map<DetId, const HGCRecHit *>>(ps.getParameter<edm::InputTag>("hitMapTag"))),
caloGeometry_(esConsumes<CaloGeometry, CaloGeometryRecord>()),
hardScatterOnly_(ps.getParameter<bool>("hardScatterOnly")) {
rhtools_.reset(new hgcal::RecHitTools());

// Register the product
produces<hgcal::TracksterToSimClusterAssociator>();
}

TracksterAssociatorByEnergyScoreProducer::~TracksterAssociatorByEnergyScoreProducer() {}

void TracksterAssociatorByEnergyScoreProducer::produce(edm::StreamID,
edm::Event &iEvent,
const edm::EventSetup &es) const {
edm::ESHandle<CaloGeometry> geom = es.getHandle(caloGeometry_);
rhtools_->setGeometry(*geom);

const std::unordered_map<DetId, const HGCRecHit *> *hitMap = &iEvent.get(hitMap_);

auto impl = std::make_unique<TracksterAssociatorByEnergyScoreImpl>(
iEvent.productGetter(), hardScatterOnly_, rhtools_, hitMap);
auto toPut = std::make_unique<hgcal::TracksterToSimClusterAssociator>(std::move(impl));
iEvent.put(std::move(toPut));
}

void TracksterAssociatorByEnergyScoreProducer::fillDescriptions(edm::ConfigurationDescriptions &cfg) {
edm::ParameterSetDescription desc;
desc.add<edm::InputTag>("hitMapTag", edm::InputTag("hgcalRecHitMapProducer"));
desc.add<bool>("hardScatterOnly", true);

cfg.add("tracksterAssociatorByEnergyScore", desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(TracksterAssociatorByEnergyScoreProducer);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import FWCore.ParameterSet.Config as cms

tracksterSimClusterAssociation = cms.EDProducer("TSToSCAssociatorEDProducer",
associator = cms.InputTag('tsAssocByEnergyScoreProducer'),
label_scl = cms.InputTag("mix","MergedCaloTruth"),
label_tst = cms.InputTag("ticlTrackstersMerge"),
label_lcl = cms.InputTag("hgcalLayerClusters")
)

from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2
premix_stage2.toModify(tracksterSimClusterAssociation,
label_scl = "mixData:MergedCaloTruth"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef SimDataFormats_Associations_TracksterToSimClusterAssociator_h
#define SimDataFormats_Associations_TracksterToSimClusterAssociator_h
// Original Author: Leonardo Cristella

// system include files
#include <memory>

// user include files

#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h"

// forward declarations

namespace hgcal {

class TracksterToSimClusterAssociator {
public:
TracksterToSimClusterAssociator(std::unique_ptr<hgcal::TracksterToSimClusterAssociatorBaseImpl>);
TracksterToSimClusterAssociator() = default;
TracksterToSimClusterAssociator(TracksterToSimClusterAssociator &&) = default;
TracksterToSimClusterAssociator &operator=(TracksterToSimClusterAssociator &&) = default;
~TracksterToSimClusterAssociator() = default;

// ---------- const member functions ---------------------
/// Associate a Trackster to SimClusters
hgcal::RecoToSimCollectionTracksters associateRecoToSim(const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const {
return m_impl->associateRecoToSim(tCH, lCCH, sCCH);
};

/// Associate a SimCluster to Tracksters
hgcal::SimToRecoCollectionTracksters associateSimToReco(const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const {
return m_impl->associateSimToReco(tCH, lCCH, sCCH);
}

private:
TracksterToSimClusterAssociator(const TracksterToSimClusterAssociator &) = delete; // stop default

const TracksterToSimClusterAssociator &operator=(const TracksterToSimClusterAssociator &) = delete; // stop default

// ---------- member data --------------------------------
std::unique_ptr<TracksterToSimClusterAssociatorBaseImpl> m_impl;
};
} // namespace hgcal

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef SimDataFormats_Associations_TracksterToSimClusterAssociatorBaseImpl_h
#define SimDataFormats_Associations_TracksterToSimClusterAssociatorBaseImpl_h

/** \class TracksterToSimClusterAssociatorBaseImpl
*
* Base class for TracksterToSimClusterAssociator. Methods take as input
* the handles of Tracksters, LayerClusters and SimClusters collections and return an
* AssociationMap (oneToManyWithQuality)
*
* \author Leonardo Cristella
*/

#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/Common/interface/AssociationMap.h"
#include "DataFormats/HGCalReco/interface/Trackster.h"
#include "DataFormats/CaloRecHit/interface/CaloClusterFwd.h"

#include "SimDataFormats/CaloAnalysis/interface/SimClusterFwd.h"

namespace hgcal {

typedef edm::AssociationMap<
edm::OneToManyWithQualityGeneric<SimClusterCollection, ticl::TracksterCollection, std::pair<float, float>>>
SimToRecoCollectionTracksters;
typedef edm::AssociationMap<edm::OneToManyWithQualityGeneric<ticl::TracksterCollection, SimClusterCollection, float>>
RecoToSimCollectionTracksters;

class TracksterToSimClusterAssociatorBaseImpl {
public:
/// Constructor
TracksterToSimClusterAssociatorBaseImpl();
/// Destructor
virtual ~TracksterToSimClusterAssociatorBaseImpl();

/// Associate a Trackster to SimClusters
virtual hgcal::RecoToSimCollectionTracksters associateRecoToSim(
const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const;

/// Associate a SimCluster to Tracksters
virtual hgcal::SimToRecoCollectionTracksters associateSimToReco(
const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const;
};
} // namespace hgcal

#endif
3 changes: 3 additions & 0 deletions SimDataFormats/Associations/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="DataFormats/HGCalReco"/>
<use name="DataFormats/ForwardDetId"/>
<use name="RecoLocalCalo/HGCalRecAlgos"/>
<library name="CPLCAssociation_plugins" file="*.cc">
<flags EDM_PLUGIN="1"/>
</library>
95 changes: 95 additions & 0 deletions SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Original Author: Leonardo Cristella
// Created: Thu Dec 3 10:52:11 CET 2020
//
//

// system include files
#include <memory>
#include <string>

// user include files
#include "FWCore/Framework/interface/global/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 "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h"

#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DataFormats/HGCalReco/interface/Trackster.h"

#include "FWCore/Utilities/interface/EDGetToken.h"

//
// class decleration
//

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

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

edm::EDGetTokenT<SimClusterCollection> SCCollectionToken_;
edm::EDGetTokenT<ticl::TracksterCollection> TSCollectionToken_;
edm::EDGetTokenT<reco::CaloClusterCollection> LCCollectionToken_;
edm::EDGetTokenT<hgcal::TracksterToSimClusterAssociator> associatorToken_;
};

TSToSCAssociatorEDProducer::TSToSCAssociatorEDProducer(const edm::ParameterSet &pset) {
produces<hgcal::SimToRecoCollectionTracksters>();
produces<hgcal::RecoToSimCollectionTracksters>();

SCCollectionToken_ = consumes<SimClusterCollection>(pset.getParameter<edm::InputTag>("label_scl"));
TSCollectionToken_ = consumes<ticl::TracksterCollection>(pset.getParameter<edm::InputTag>("label_tst"));
LCCollectionToken_ = consumes<reco::CaloClusterCollection>(pset.getParameter<edm::InputTag>("label_lcl"));
associatorToken_ = consumes<hgcal::TracksterToSimClusterAssociator>(pset.getParameter<edm::InputTag>("associator"));
}

TSToSCAssociatorEDProducer::~TSToSCAssociatorEDProducer() {}

//
// member functions
//

// ------------ method called to produce the data ------------
void TSToSCAssociatorEDProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const {
using namespace edm;

edm::Handle<hgcal::TracksterToSimClusterAssociator> theAssociator;
iEvent.getByToken(associatorToken_, theAssociator);

Handle<SimClusterCollection> SCCollection;
iEvent.getByToken(SCCollectionToken_, SCCollection);

Handle<ticl::TracksterCollection> TSCollection;
iEvent.getByToken(TSCollectionToken_, TSCollection);

Handle<reco::CaloClusterCollection> LCCollection;
iEvent.getByToken(LCCollectionToken_, LCCollection);

// associate LC and SC
LogTrace("AssociatorValidator") << "Calling associateRecoToSim method\n";
hgcal::RecoToSimCollectionTracksters recSimColl =
theAssociator->associateRecoToSim(TSCollection, LCCollection, SCCollection);

LogTrace("AssociatorValidator") << "Calling associateSimToReco method\n";
hgcal::SimToRecoCollectionTracksters simRecColl =
theAssociator->associateSimToReco(TSCollection, LCCollection, SCCollection);

auto rts = std::make_unique<hgcal::RecoToSimCollectionTracksters>(recSimColl);
auto str = std::make_unique<hgcal::SimToRecoCollectionTracksters>(simRecColl);

iEvent.put(std::move(rts));
iEvent.put(std::move(str));
}

// define this as a plug-in
DEFINE_FWK_MODULE(TSToSCAssociatorEDProducer);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Original Author: Leonardo Cristella

#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h"

hgcal::TracksterToSimClusterAssociator::TracksterToSimClusterAssociator(
std::unique_ptr<hgcal::TracksterToSimClusterAssociatorBaseImpl> ptr)
: m_impl(std::move(ptr)) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Original Author: Leonardo Cristella

#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h"

namespace hgcal {
TracksterToSimClusterAssociatorBaseImpl::TracksterToSimClusterAssociatorBaseImpl(){};
TracksterToSimClusterAssociatorBaseImpl::~TracksterToSimClusterAssociatorBaseImpl(){};

hgcal::RecoToSimCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateRecoToSim(
const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const {
return hgcal::RecoToSimCollectionTracksters();
}

hgcal::SimToRecoCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateSimToReco(
const edm::Handle<ticl::TracksterCollection> &tCH,
const edm::Handle<reco::CaloClusterCollection> &lCCH,
const edm::Handle<SimClusterCollection> &sCCH) const {
return hgcal::SimToRecoCollectionTracksters();
}

} // namespace hgcal
Loading

0 comments on commit 02d443b

Please sign in to comment.