File indexing completed on 2025-05-29 03:17:57
0001
0002
0003 #include "FWCore/Framework/interface/global/EDProducer.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/Utilities/interface/EDGetToken.h"
0009 #include "DataFormats/HGCalReco/interface/Trackster.h"
0010 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
0011 #include "SimDataFormats/Associations/interface/TICLAssociationMap.h"
0012 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0013 #include "CommonTools/RecoAlgos/interface/MultiVectorManager.h"
0014
0015 class AllHitToTracksterAssociatorsProducer : public edm::global::EDProducer<> {
0016 public:
0017 explicit AllHitToTracksterAssociatorsProducer(const edm::ParameterSet&);
0018 ~AllHitToTracksterAssociatorsProducer() override = default;
0019
0020 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0021
0022 private:
0023 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0024
0025 std::vector<std::pair<std::string, edm::EDGetTokenT<std::vector<ticl::Trackster>>>> tracksterCollectionTokens_;
0026 edm::EDGetTokenT<std::vector<reco::CaloCluster>> layerClustersToken_;
0027 edm::EDGetTokenT<std::unordered_map<DetId, const unsigned int>> hitMapToken_;
0028 std::vector<edm::EDGetTokenT<HGCRecHitCollection>> hitsTokens_;
0029 };
0030
0031 AllHitToTracksterAssociatorsProducer::AllHitToTracksterAssociatorsProducer(const edm::ParameterSet& pset)
0032 : layerClustersToken_(consumes<std::vector<reco::CaloCluster>>(pset.getParameter<edm::InputTag>("layerClusters"))),
0033 hitMapToken_(
0034 consumes<std::unordered_map<DetId, const unsigned int>>(pset.getParameter<edm::InputTag>("hitMapTag"))) {
0035 const auto& tracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("tracksterCollections");
0036 for (const auto& tag : tracksterCollections) {
0037 tracksterCollectionTokens_.emplace_back(tag.label() + tag.instance(), consumes<std::vector<ticl::Trackster>>(tag));
0038 }
0039
0040 for (const auto& tag : pset.getParameter<std::vector<edm::InputTag>>("hits")) {
0041 hitsTokens_.emplace_back(consumes<HGCRecHitCollection>(tag));
0042 }
0043
0044 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0045 produces<ticl::AssociationMap<ticl::mapWithFraction>>("hitTo" + tracksterToken.first);
0046 produces<ticl::AssociationMap<ticl::mapWithFraction>>(tracksterToken.first + "ToHit");
0047 }
0048 }
0049
0050 void AllHitToTracksterAssociatorsProducer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup&) const {
0051 using namespace edm;
0052
0053 Handle<std::vector<reco::CaloCluster>> layer_clusters;
0054 iEvent.getByToken(layerClustersToken_, layer_clusters);
0055
0056 if (!layer_clusters.isValid()) {
0057 edm::LogWarning("AllHitToTracksterAssociatorsProducer") << "Missing LayerCluster collection.";
0058 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0059 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), "hitTo" + tracksterToken.first);
0060 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), tracksterToken.first + "ToHit");
0061 }
0062 return;
0063 }
0064
0065 Handle<std::unordered_map<DetId, const unsigned int>> hitMap;
0066 iEvent.getByToken(hitMapToken_, hitMap);
0067
0068 MultiVectorManager<HGCRecHit> rechitManager;
0069 for (const auto& token : hitsTokens_) {
0070 Handle<HGCRecHitCollection> hitsHandle;
0071 iEvent.getByToken(token, hitsHandle);
0072
0073
0074 if (!hitsHandle.isValid()) {
0075 edm::LogWarning("AllHitToTracksterAssociatorsProducer")
0076 << "Missing HGCRecHitCollection for one of the hitsTokens.";
0077 continue;
0078 }
0079 rechitManager.addVector(*hitsHandle);
0080 }
0081
0082
0083 if (rechitManager.size() == 0) {
0084 edm::LogWarning("HitToSimClusterCaloParticleAssociatorProducer")
0085 << "No valid HGCRecHitCollections found. Association maps will be empty.";
0086 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0087 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), "hitTo" + tracksterToken.first);
0088 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), tracksterToken.first + "ToHit");
0089 }
0090 return;
0091 }
0092
0093 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0094 Handle<std::vector<ticl::Trackster>> tracksters;
0095 iEvent.getByToken(tracksterToken.second, tracksters);
0096
0097 if (!tracksters.isValid()) {
0098 edm::LogWarning("AllHitToTracksterAssociatorsProducer") << "Missing Tracksters for one of the hitsTokens.";
0099 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), "hitTo" + tracksterToken.first);
0100 iEvent.put(std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(), tracksterToken.first + "ToHit");
0101 continue;
0102 }
0103
0104 auto hitToTracksterMap = std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(rechitManager.size());
0105 auto tracksterToHitMap = std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(tracksters->size());
0106
0107 for (unsigned int tracksterId = 0; tracksterId < tracksters->size(); ++tracksterId) {
0108 const auto& trackster = (*tracksters)[tracksterId];
0109 for (unsigned int j = 0; j < trackster.vertices().size(); ++j) {
0110 const auto& lc = (*layer_clusters)[trackster.vertices()[j]];
0111 float invMultiplicity = 1.0f / trackster.vertex_multiplicity()[j];
0112
0113 for (const auto& hitAndFraction : lc.hitsAndFractions()) {
0114 auto hitMapIter = hitMap->find(hitAndFraction.first);
0115 if (hitMapIter != hitMap->end()) {
0116 unsigned int rechitIndex = hitMapIter->second;
0117 float fraction = hitAndFraction.second * invMultiplicity;
0118 hitToTracksterMap->insert(rechitIndex, tracksterId, fraction);
0119 tracksterToHitMap->insert(tracksterId, rechitIndex, fraction);
0120 }
0121 }
0122 }
0123 }
0124
0125 iEvent.put(std::move(hitToTracksterMap), "hitTo" + tracksterToken.first);
0126 iEvent.put(std::move(tracksterToHitMap), tracksterToken.first + "ToHit");
0127 }
0128 }
0129
0130 void AllHitToTracksterAssociatorsProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0131 edm::ParameterSetDescription desc;
0132 desc.add<std::vector<edm::InputTag>>("tracksterCollections",
0133 {edm::InputTag("ticlTrackstersCLUE3DHigh"),
0134 edm::InputTag("ticlTrackstersLinks"),
0135 edm::InputTag("ticlCandidate")});
0136 desc.add<edm::InputTag>("layerClusters", edm::InputTag("hgcalMergeLayerClusters"));
0137 desc.add<edm::InputTag>("hitMapTag", edm::InputTag("recHitMapProducer", "hgcalRecHitMap"));
0138 desc.add<std::vector<edm::InputTag>>("hits",
0139 {edm::InputTag("HGCalRecHit", "HGCEERecHits"),
0140 edm::InputTag("HGCalRecHit", "HGCHEFRecHits"),
0141 edm::InputTag("HGCalRecHit", "HGCHEBRecHits")});
0142 descriptions.add("AllHitToTracksterAssociatorsProducer", desc);
0143 }
0144
0145
0146 DEFINE_FWK_MODULE(AllHitToTracksterAssociatorsProducer);