Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 10:05:08

0001 #ifndef SimTracker_TrackerHitAssociation_ClusterTPAssociation_h
0002 #define SimTracker_TrackerHitAssociation_ClusterTPAssociation_h
0003 
0004 #include "DataFormats/Provenance/interface/ProductID.h"
0005 #include "DataFormats/Common/interface/HandleBase.h"
0006 #include "DataFormats/TrackerRecHit2D/interface/OmniClusterRef.h"
0007 #include "FWCore/Utilities/interface/VecArray.h"
0008 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0009 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
0010 
0011 #include <vector>
0012 #include <utility>
0013 #include <algorithm>
0014 
0015 /**
0016  * Maps OmniClusterRefs to TrackingParticleRefs
0017  *
0018  * Assumes that the TrackingParticleRefs point to a single
0019  * TrackingParticle collection.
0020  */
0021 class ClusterTPAssociation {
0022 public:
0023   using key_type = OmniClusterRef;
0024   using mapped_type = TrackingParticleRef;
0025   using value_type = std::pair<key_type, mapped_type>;
0026   using map_type = std::vector<value_type>;
0027   using const_iterator = typename map_type::const_iterator;
0028   using range = std::pair<const_iterator, const_iterator>;
0029 
0030   ClusterTPAssociation() {}
0031   explicit ClusterTPAssociation(const edm::HandleBase& mappedHandle) : ClusterTPAssociation(mappedHandle.id()) {}
0032   explicit ClusterTPAssociation(const edm::ProductID& mappedProductId) : mappedProductId_(mappedProductId) {}
0033 
0034   void addKeyID(edm::ProductID id) {
0035     auto foundKeyID = std::find(std::begin(keyProductIDs_), std::end(keyProductIDs_), id);
0036     if (foundKeyID == std::end(keyProductIDs_)) {
0037       keyProductIDs_.emplace_back(id);
0038     }
0039   }
0040 
0041   void emplace_back(const OmniClusterRef& cluster, const TrackingParticleRef& tp) {
0042     checkMappedProductID(tp);
0043     checkKeyProductID(cluster.id());
0044     map_.emplace_back(cluster, tp);
0045   }
0046   void sortAndUnique() {
0047     std::sort(map_.begin(), map_.end(), compareSort);
0048     auto last = std::unique(map_.begin(), map_.end());
0049     map_.erase(last, map_.end());
0050     map_.shrink_to_fit();
0051   }
0052   void swap(ClusterTPAssociation& other) {
0053     map_.swap(other.map_);
0054     mappedProductId_.swap(other.mappedProductId_);
0055   }
0056 
0057   bool empty() const { return map_.empty(); }
0058   size_t size() const { return map_.size(); }
0059 
0060   const_iterator begin() const { return map_.begin(); }
0061   const_iterator cbegin() const { return map_.cbegin(); }
0062   const_iterator end() const { return map_.end(); }
0063   const_iterator cend() const { return map_.end(); }
0064 
0065   range equal_range(const OmniClusterRef& key) const {
0066     checkKeyProductID(key);
0067     return std::equal_range(map_.begin(), map_.end(), value_type(key, TrackingParticleRef()), compare);
0068   }
0069 
0070   const map_type& map() const { return map_; }
0071 
0072   void checkKeyProductID(const OmniClusterRef& key) const { checkKeyProductID(key.id()); }
0073   void checkKeyProductID(const edm::ProductID& id) const;
0074 
0075   void checkMappedProductID(const edm::HandleBase& mappedHandle) const { checkMappedProductID(mappedHandle.id()); }
0076   void checkMappedProductID(const TrackingParticleRef& tp) const { checkMappedProductID(tp.id()); }
0077   void checkMappedProductID(const edm::ProductID& id) const;
0078 
0079 private:
0080   static bool compare(const value_type& i, const value_type& j) { return i.first.rawIndex() > j.first.rawIndex(); }
0081 
0082   static bool compareSort(const value_type& i, const value_type& j) {
0083     // For sorting compare also TrackingParticle keys in order to
0084     // remove duplicate matches
0085     return compare(i, j) || (i.first.rawIndex() == j.first.rawIndex() && i.second.key() > j.second.key());
0086   }
0087 
0088   map_type map_;
0089   edm::VecArray<edm::ProductID, 2> keyProductIDs_;
0090   edm::ProductID mappedProductId_;
0091 };
0092 
0093 #endif