Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-29 23:13:08

0001 // Original Author: Marco Rovere
0002 
0003 #include <vector>
0004 #include <map>
0005 #include <unordered_map>
0006 #include <memory>  // shared_ptr
0007 
0008 #include "DataFormats/ForwardDetId/interface/HGCalDetId.h"
0009 #include "DataFormats/HGCRecHit/interface/HGCRecHit.h"
0010 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0011 #include "DataFormats/ParticleFlowReco/interface/PFRecHit.h"
0012 #include "SimDataFormats/Associations/interface/LayerClusterToCaloParticleAssociator.h"
0013 #include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
0014 
0015 namespace edm {
0016   class EDProductGetter;
0017 }
0018 
0019 namespace ticl {
0020   // This structure is used both for LayerClusters and CaloParticles storing their id and the fraction of a hit
0021   // that belongs to the LayerCluster or CaloParticle. The meaning of the operator is extremely important since
0022   // this struct will be used inside maps and other containers and when searching for one particular occurence
0023   // only the clusterId member will be used in the check, skipping the fraction part.
0024   struct detIdInfoInCluster {
0025     bool operator==(const detIdInfoInCluster &o) const { return clusterId == o.clusterId; };
0026     long unsigned int clusterId;
0027     float fraction;
0028     detIdInfoInCluster(long unsigned int cId, float fr) {
0029       clusterId = cId;
0030       fraction = fr;
0031     }
0032   };
0033 
0034   // This introduces a CaloParticle on layer concept. For a CaloParticle it stores:
0035   // 1. Its id: caloParticleId.
0036   // 2. The energy that the CaloParticle deposited in a specific layer and it was reconstructed.
0037   // 3. The hits_and_fractions that contributed to that deposition. SimHits that aren't reconstructed
0038   //    and doesn't have any matched rechits are disregarded. Keep in mind that since a CaloParticle
0039   //    should most probably have more than one SimCluster, all different contributions from the same CaloParticle
0040   //    to a single hit are merged into a single entry, with the fractions properly summed.
0041   // 4. A map to save the LayerClusters ids (id is the key) that reconstructed at least one SimHit of the CaloParticle under study
0042   //    together with the energy that the LayerCluster reconstructed from the CaloParticle and the score. The energy
0043   //    is not the energy of the LayerCluster, but the energy of the LayerCluster coming from the CaloParticle.
0044   //    So, there will be energy of the LayerCluster that is disregarded here, since there may be LayerCluster's
0045   //    cells that the CaloParticle didn't contribute.
0046   struct caloParticleOnLayer {
0047     unsigned int caloParticleId;
0048     float energy = 0;
0049     std::vector<std::pair<DetId, float>> hits_and_fractions;
0050     std::unordered_map<int, std::pair<float, float>> layerClusterIdToEnergyAndScore;
0051   };
0052 
0053   // This object connects a LayerCluster, identified through its id (lcId), with a vector of pairs containing all the CaloParticles
0054   // (via their ids (cpIds)) that share at least one cell with the LayerCluster. In that pair it
0055   // stores the score (lcId->(cpId,score)). Keep in mind that the association is not unique, since there could be several instances
0056   // of the same CaloParticle from several related SimClusters that each contributed to the same LayerCluster.
0057   typedef std::vector<std::vector<std::pair<unsigned int, float>>> layerClusterToCaloParticle;
0058   // This is used to save the caloParticleOnLayer structure for all CaloParticles in each layer.
0059   // It is not exactly what is returned outside, but out of its entries, the output object is build.
0060   typedef std::vector<std::vector<ticl::caloParticleOnLayer>> caloParticleToLayerCluster;
0061   //This is the output of the makeConnections function that contain all the work with CP2LC and LC2CP
0062   //association. It will be read by the relevant associateSimToReco and associateRecoToSim functions to
0063   //provide the final product.
0064   typedef std::tuple<layerClusterToCaloParticle, caloParticleToLayerCluster> association;
0065 }  // namespace ticl
0066 
0067 template <typename HIT>
0068 class LCToCPAssociatorByEnergyScoreImpl : public ticl::LayerClusterToCaloParticleAssociatorBaseImpl {
0069 public:
0070   explicit LCToCPAssociatorByEnergyScoreImpl(edm::EDProductGetter const &,
0071                                              bool,
0072                                              std::shared_ptr<hgcal::RecHitTools>,
0073                                              const std::unordered_map<DetId, const unsigned int> *,
0074                                              const std::vector<const HIT *> &hits);
0075 
0076   ticl::RecoToSimCollection associateRecoToSim(const edm::Handle<reco::CaloClusterCollection> &cCH,
0077                                                const edm::Handle<CaloParticleCollection> &cPCH) const override;
0078 
0079   ticl::SimToRecoCollection associateSimToReco(const edm::Handle<reco::CaloClusterCollection> &cCH,
0080                                                const edm::Handle<CaloParticleCollection> &cPCH) const override;
0081 
0082 private:
0083   const bool hardScatterOnly_;
0084   std::shared_ptr<hgcal::RecHitTools> recHitTools_;
0085   const std::unordered_map<DetId, const unsigned int> *hitMap_;
0086   unsigned layers_;
0087   edm::EDProductGetter const *productGetter_;
0088   ticl::association makeConnections(const edm::Handle<reco::CaloClusterCollection> &cCH,
0089                                     const edm::Handle<CaloParticleCollection> &cPCH) const;
0090   std::vector<const HIT *> hits_;
0091 };
0092 
0093 extern template class LCToCPAssociatorByEnergyScoreImpl<HGCRecHit>;
0094 extern template class LCToCPAssociatorByEnergyScoreImpl<reco::PFRecHit>;
0095 
0096 using HGCalLCToCPAssociatorByEnergyScoreImpl = LCToCPAssociatorByEnergyScoreImpl<HGCRecHit>;
0097 using BarrelLCToCPAssociatorByEnergyScoreImpl = LCToCPAssociatorByEnergyScoreImpl<reco::PFRecHit>;