Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:07

0001 /*! \class   TTClusterAssociationMap
0002  *  \brief   Stores association of Truth Particles (TP) to L1 Track-Trigger Clusters
0003  *
0004  *  \details Contains two maps. One associates each cluster to a vector
0005  *           of all TPs that made its hits. The other associates each TP 
0006  *           to a vector of all clusters it contributed to.
0007  *
0008  *           (The template structure is used to accomodate types
0009  *           other than PixelDigis, in case they are needed in future).
0010  *
0011  *  \author Nicola Pozzobon
0012  *  \date   2013, Jul 19
0013  *  (tidy up: Ian Tomalin, 2020)
0014  */
0015 
0016 #ifndef L1_TRACK_TRIGGER_CLUSTER_ASSOCIATION_FORMAT_H
0017 #define L1_TRACK_TRIGGER_CLUSTER_ASSOCIATION_FORMAT_H
0018 
0019 #include "DataFormats/Common/interface/Ref.h"
0020 #include "DataFormats/Common/interface/Ptr.h"
0021 #include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
0022 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
0023 #include "DataFormats/Common/interface/DetSet.h"
0024 #include "DataFormats/Common/interface/DetSetVector.h"
0025 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0026 #include "DataFormats/DetId/interface/DetId.h"
0027 #include "DataFormats/Phase2TrackerDigi/interface/Phase2TrackerDigi.h"
0028 #include "DataFormats/GeometryCommonDetAlgo/interface/MeasurementPoint.h"
0029 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"  /// NOTE: this is needed even if it seems not
0030 #include "DataFormats/L1TrackTrigger/interface/TTCluster.h"
0031 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
0032 #include "SimDataFormats/Track/interface/SimTrack.h"
0033 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
0034 #include "SimDataFormats/EncodedEventId/interface/EncodedEventId.h"
0035 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0036 
0037 // Templated aliases
0038 template <typename T>
0039 using MapClusToVecTP = std::map<TTClusterRefT<T>, std::vector<TrackingParticlePtr>>;
0040 template <typename T>
0041 using MapTPToVecClus = std::map<TrackingParticlePtr, std::vector<TTClusterRefT<T>>>;
0042 
0043 template <typename T>
0044 class TTClusterAssociationMap {
0045 public:
0046   /// Constructors
0047   TTClusterAssociationMap();
0048 
0049   /// Destructor
0050   ~TTClusterAssociationMap();
0051 
0052   /// Get/set cluster <-> truth association maps
0053 
0054   const MapClusToVecTP<T>& getTTClusterToTrackingParticlesMap() const { return clusterToTrackingParticleVectorMap_; }
0055   const MapTPToVecClus<T>& getTrackingParticleToTTClustersMap() const { return trackingParticleToClusterVectorMap_; }
0056 
0057   void setTTClusterToTrackingParticlesMap(const MapClusToVecTP<T>& aMap) { clusterToTrackingParticleVectorMap_ = aMap; }
0058   void setTrackingParticleToTTClustersMap(const MapTPToVecClus<T>& aMap) { trackingParticleToClusterVectorMap_ = aMap; }
0059 
0060   /// Get all TPs associated to a cluster
0061   const std::vector<TrackingParticlePtr>& findTrackingParticlePtrs(TTClusterRefT<T> aCluster) const;
0062 
0063   /// Get main TP associated to a cluster. (Non-NULL if isGenuine() below is true).
0064   const TrackingParticlePtr& findTrackingParticlePtr(TTClusterRefT<T> aCluster) const;
0065 
0066   // Get all clusters associated to TP.
0067   const std::vector<TTClusterRefT<T>>& findTTClusterRefs(TrackingParticlePtr aTrackingParticle) const;
0068 
0069   ///--- Get quality of L1 cluster based on truth info.
0070   /// (exactly 1 of following 3 functions is always true)
0071 
0072   /// Cluster "genuine": i.e. cluster associated to exactly 1 TP.
0073   /// (If other TPs are associated, but have in total < 1% of Pt of main TP,
0074   ///  or if they are null, then they are neglected here).
0075   bool isGenuine(TTClusterRefT<T> aCluster) const;
0076   /// Cluster "unknown": i.e. not associated with any TP.
0077   bool isUnknown(TTClusterRefT<T> aCluster) const;
0078   /// Cluster is not "genuine" or "unknown".
0079   bool isCombinatoric(TTClusterRefT<T> aCluster) const;
0080 
0081 private:
0082   /// Data members
0083   MapClusToVecTP<T> clusterToTrackingParticleVectorMap_;
0084   MapTPToVecClus<T> trackingParticleToClusterVectorMap_;
0085 
0086   int nclus;
0087 
0088   // Allow functions to return reference to null.
0089   static const TrackingParticlePtr nullTrackingParticlePtr_;
0090   static const std::vector<TrackingParticlePtr> nullVecTrackingParticlePtr_;
0091   static const std::vector<TTClusterRefT<T>> nullVecClusterRef_;
0092 
0093 };  /// Close class
0094 
0095 /*! \brief   Implementation of methods
0096  *  \details Here, in the header file, the methods which do not depend
0097  *           on the specific type <T> that can fit the template.
0098  *           Other methods, with type-specific features, are implemented
0099  *           in the source file.
0100  */
0101 
0102 // Static constant data members.
0103 template <typename T>
0104 const TrackingParticlePtr TTClusterAssociationMap<T>::nullTrackingParticlePtr_;
0105 template <typename T>
0106 const std::vector<TrackingParticlePtr> TTClusterAssociationMap<T>::nullVecTrackingParticlePtr_;
0107 template <typename T>
0108 const std::vector<TTClusterRefT<T>> TTClusterAssociationMap<T>::nullVecClusterRef_;
0109 
0110 /// Default Constructor
0111 /// NOTE: to be used with setSomething(...) methods
0112 template <typename T>
0113 TTClusterAssociationMap<T>::TTClusterAssociationMap() {
0114   /// Set default data members
0115   nclus = 0;
0116 }
0117 
0118 /// Destructor
0119 template <typename T>
0120 TTClusterAssociationMap<T>::~TTClusterAssociationMap() {}
0121 
0122 /// Operations
0123 template <typename T>
0124 const std::vector<TTClusterRefT<T>>& TTClusterAssociationMap<T>::findTTClusterRefs(
0125     TrackingParticlePtr aTrackingParticle) const {
0126   if (trackingParticleToClusterVectorMap_.find(aTrackingParticle) != trackingParticleToClusterVectorMap_.end()) {
0127     return trackingParticleToClusterVectorMap_.find(aTrackingParticle)->second;
0128   } else {
0129     return nullVecClusterRef_;
0130   }
0131 }
0132 
0133 template <typename T>
0134 const std::vector<TrackingParticlePtr>& TTClusterAssociationMap<T>::findTrackingParticlePtrs(
0135     TTClusterRefT<T> aCluster) const {
0136   if (clusterToTrackingParticleVectorMap_.find(aCluster) != clusterToTrackingParticleVectorMap_.end()) {
0137     return clusterToTrackingParticleVectorMap_.find(aCluster)->second;
0138   } else {
0139     return nullVecTrackingParticlePtr_;
0140   }
0141 }
0142 
0143 template <typename T>
0144 const TrackingParticlePtr& TTClusterAssociationMap<T>::findTrackingParticlePtr(TTClusterRefT<T> aCluster) const {
0145   if (this->isGenuine(aCluster)) {
0146     return this->findTrackingParticlePtrs(aCluster).at(0);
0147   } else {
0148     return nullTrackingParticlePtr_;
0149   }
0150 }
0151 
0152 /// MC truth
0153 /// Table to define Genuine, Combinatoric and Unknown
0154 ///
0155 /// N = number of NULL TP pointers
0156 /// D = number of GOOD TP pointers different from each other
0157 ///
0158 /// OLD DEFINITION
0159 ///
0160 /// N / D--> | 0 | 1 | >1
0161 /// ----------------------
0162 /// 0        | U | G | C
0163 /// ----------------------
0164 /// >0       | U | C | C
0165 ///
0166 
0167 /// NEW DEFINITION SV 060617
0168 ///
0169 /// N / D--> | 0 | 1 | >1 (with 1 TP getting >99% of the total pT) | >1
0170 /// -------------------------------------------------------------------
0171 /// 0        | U | G | G                                           | C
0172 /// -------------------------------------------------------------------
0173 /// >0       | U | G | G                                           | C
0174 ///
0175 
0176 template <typename T>
0177 bool TTClusterAssociationMap<T>::isGenuine(TTClusterRefT<T> aCluster) const {
0178   /// Get the TrackingParticles
0179   const std::vector<TrackingParticlePtr>& theseTrackingParticles = this->findTrackingParticlePtrs(aCluster);
0180 
0181   /// If the vector is empty, then the cluster is UNKNOWN
0182   if (theseTrackingParticles.empty())
0183     return false;
0184 
0185   /// If we are here, it means there are some TrackingParticles
0186   unsigned int goodDifferentTPs = 0;
0187   std::vector<const TrackingParticle*> tpAddressVector;
0188 
0189   std::vector<float> tp_mom;
0190 
0191   float tp_tot = 0;
0192 
0193   /// Loop over the TrackingParticles
0194   for (const auto& tp : theseTrackingParticles) {
0195     /// Get the TrackingParticle
0196     const TrackingParticlePtr& curTP = tp;
0197 
0198     /// Count the NULL TrackingParticles
0199     if (curTP.isNull()) {
0200       tp_mom.push_back(0);
0201     } else {
0202       tp_mom.push_back(curTP.get()->p4().pt());
0203       tp_tot += curTP.get()->p4().pt();
0204     }
0205   }
0206 
0207   if (tp_tot == 0)
0208     return false;
0209 
0210   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0211     /// Get the TrackingParticle
0212     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0213 
0214     /// Count the NULL TrackingParticles
0215     if (tp_mom.at(itp) > 0.01 * tp_tot) {
0216       /// Store the pointers (addresses) of the TrackingParticle
0217       /// to be able to count how many different there are
0218       tpAddressVector.push_back(curTP.get());
0219     }
0220   }
0221 
0222   /// Count how many different TrackingParticle there are
0223   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0224   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0225   goodDifferentTPs = tpAddressVector.size();
0226 
0227   return (goodDifferentTPs == 1);
0228 }
0229 
0230 template <typename T>
0231 bool TTClusterAssociationMap<T>::isUnknown(TTClusterRefT<T> aCluster) const {
0232   /// Get the TrackingParticles
0233   const std::vector<TrackingParticlePtr>& theseTrackingParticles = this->findTrackingParticlePtrs(aCluster);
0234 
0235   /// If the vector is empty, then the cluster is UNKNOWN
0236   if (theseTrackingParticles.empty())
0237     return true;
0238 
0239   /// If we are here, it means there are some TrackingParticles
0240   unsigned int goodDifferentTPs = 0;
0241   std::vector<const TrackingParticle*> tpAddressVector;
0242 
0243   /// Loop over the TrackingParticles
0244   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0245     /// Get the TrackingParticle
0246     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0247 
0248     /// Count the non-NULL TrackingParticles
0249     if (!curTP.isNull()) {
0250       /// Store the pointers (addresses) of the TrackingParticle
0251       /// to be able to count how many different there are
0252       tpAddressVector.push_back(curTP.get());
0253     }
0254   }
0255 
0256   /// Count how many different TrackingParticle there are
0257   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0258   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0259   goodDifferentTPs = tpAddressVector.size();
0260 
0261   /// UNKNOWN means no good TP is found
0262   return (goodDifferentTPs == 0);
0263 }
0264 
0265 template <typename T>
0266 bool TTClusterAssociationMap<T>::isCombinatoric(TTClusterRefT<T> aCluster) const {
0267   /// Get the TrackingParticles
0268   const std::vector<TrackingParticlePtr>& theseTrackingParticles = this->findTrackingParticlePtrs(aCluster);
0269 
0270   /// If the vector is empty, then the cluster is UNKNOWN
0271   if (theseTrackingParticles.empty())
0272     return false;
0273 
0274   bool genuineClu = this->isGenuine(aCluster);
0275   bool unknownClu = this->isUnknown(aCluster);
0276 
0277   if (genuineClu || unknownClu)
0278     return false;
0279 
0280   return true;
0281 
0282   /// If we are here, it means there are some TrackingParticles
0283   unsigned int goodDifferentTPs = 0;
0284   std::vector<const TrackingParticle*> tpAddressVector;
0285 
0286   /// Loop over the TrackingParticles
0287   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0288     /// Get the TrackingParticle
0289     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0290 
0291     /// Count the NULL TrackingParticles
0292     if (!curTP.isNull()) {
0293       /// Store the pointers (addresses) of the TrackingParticle
0294       /// to be able to count how many different there are
0295       tpAddressVector.push_back(curTP.get());
0296     }
0297   }
0298 
0299   /// Count how many different TrackingParticle there are
0300   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0301   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0302   goodDifferentTPs = tpAddressVector.size();
0303 
0304   return (goodDifferentTPs > 1);
0305 }
0306 
0307 #endif