Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-16 05:06:39

0001 // Author: Felice Pantaleo, felice.pantaleo@cern.ch 06/2024
0002 #include "HitToSimClusterCaloParticleAssociatorProducer.h"
0003 #include "FWCore/Framework/interface/Event.h"
0004 #include "FWCore/Framework/interface/MakerMacros.h"
0005 #include "FWCore/Framework/interface/ESHandle.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/Provenance/interface/ProductID.h"
0013 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0014 #include "CommonTools/RecoAlgos/interface/MultiVectorManager.h"
0015 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0016 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0017 
0018 HitToSimClusterCaloParticleAssociatorProducer::HitToSimClusterCaloParticleAssociatorProducer(
0019     const edm::ParameterSet &pset)
0020     : simClusterToken_(consumes<std::vector<SimCluster>>(pset.getParameter<edm::InputTag>("simClusters"))),
0021       caloParticleToken_(consumes<std::vector<CaloParticle>>(pset.getParameter<edm::InputTag>("caloParticles"))),
0022       hitMapToken_(
0023           consumes<std::unordered_map<DetId, const unsigned int>>(pset.getParameter<edm::InputTag>("hitMap"))) {
0024   auto hitsTags = pset.getParameter<std::vector<edm::InputTag>>("hits");
0025   for (const auto &tag : hitsTags) {
0026     hitsTokens_.push_back(consumes<HGCRecHitCollection>(tag));
0027   }
0028   produces<ticl::AssociationMap<ticl::mapWithFraction>>("hitToSimClusterMap");
0029   produces<ticl::AssociationMap<ticl::mapWithFraction>>("hitToCaloParticleMap");
0030 }
0031 
0032 HitToSimClusterCaloParticleAssociatorProducer::~HitToSimClusterCaloParticleAssociatorProducer() {}
0033 
0034 void HitToSimClusterCaloParticleAssociatorProducer::produce(edm::StreamID,
0035                                                             edm::Event &iEvent,
0036                                                             const edm::EventSetup &iSetup) const {
0037   using namespace edm;
0038 
0039   Handle<std::vector<CaloParticle>> caloParticlesHandle;
0040   iEvent.getByToken(caloParticleToken_, caloParticlesHandle);
0041   const auto &caloParticles = *caloParticlesHandle;
0042 
0043   Handle<std::vector<SimCluster>> simClustersHandle;
0044   iEvent.getByToken(simClusterToken_, simClustersHandle);
0045   Handle<std::unordered_map<DetId, const unsigned int>> hitMap;
0046   iEvent.getByToken(hitMapToken_, hitMap);
0047 
0048   MultiVectorManager<HGCRecHit> rechitManager;
0049   for (const auto &token : hitsTokens_) {
0050     Handle<HGCRecHitCollection> hitsHandle;
0051     iEvent.getByToken(token, hitsHandle);
0052     rechitManager.addVector(*hitsHandle);
0053   }
0054 
0055   // Create association maps
0056   auto hitToSimClusterMap = std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(rechitManager.size());
0057   auto hitToCaloParticleMap = std::make_unique<ticl::AssociationMap<ticl::mapWithFraction>>(rechitManager.size());
0058 
0059   // Loop over caloParticles
0060   for (unsigned int cpId = 0; cpId < caloParticles.size(); ++cpId) {
0061     const auto &caloParticle = caloParticles[cpId];
0062     // Loop over simClusters in caloParticle
0063     for (const auto &simCluster : caloParticle.simClusters()) {
0064       // Loop over hits in simCluster
0065       for (const auto &hitAndFraction : simCluster->hits_and_fractions()) {
0066         auto hitMapIter = hitMap->find(hitAndFraction.first);
0067         if (hitMapIter != hitMap->end()) {
0068           unsigned int rechitIndex = hitMapIter->second;
0069           float fraction = hitAndFraction.second;
0070           hitToSimClusterMap->insert(rechitIndex, simCluster.key(), fraction);
0071           hitToCaloParticleMap->insert(rechitIndex, cpId, fraction);
0072         }
0073       }
0074     }
0075   }
0076   iEvent.put(std::move(hitToSimClusterMap), "hitToSimClusterMap");
0077   iEvent.put(std::move(hitToCaloParticleMap), "hitToCaloParticleMap");
0078 }
0079 
0080 void HitToSimClusterCaloParticleAssociatorProducer::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0081   edm::ParameterSetDescription desc;
0082   desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0083   desc.add<edm::InputTag>("simClusters", edm::InputTag("mix", "MergedCaloTruth"));
0084 
0085   desc.add<edm::InputTag>("hitMap", edm::InputTag("recHitMapProducer", "hgcalRecHitMap"));
0086   desc.add<std::vector<edm::InputTag>>("hits",
0087                                        {edm::InputTag("HGCalRecHit", "HGCEERecHits"),
0088                                         edm::InputTag("HGCalRecHit", "HGCHEFRecHits"),
0089                                         edm::InputTag("HGCalRecHit", "HGCHEBRecHits")});
0090   descriptions.add("hitToSimClusterCaloParticleAssociator", desc);
0091 }
0092 
0093 // Define this as a plug-in
0094 DEFINE_FWK_MODULE(HitToSimClusterCaloParticleAssociatorProducer);