Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:31:28

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 nullTPs = 0;
0187   unsigned int goodDifferentTPs = 0;
0188   std::vector<const TrackingParticle*> tpAddressVector;
0189 
0190   std::vector<float> tp_mom;
0191 
0192   float tp_tot = 0;
0193 
0194   /// Loop over the TrackingParticles
0195   for (const auto& tp : theseTrackingParticles) {
0196     /// Get the TrackingParticle
0197     const TrackingParticlePtr& curTP = tp;
0198 
0199     /// Count the NULL TrackingParticles
0200     if (curTP.isNull()) {
0201       //      nullTPs++;
0202       tp_mom.push_back(0);
0203     } else {
0204       tp_mom.push_back(curTP.get()->p4().pt());
0205       tp_tot += curTP.get()->p4().pt();
0206     }
0207   }
0208 
0209   if (tp_tot == 0)
0210     return false;
0211 
0212   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0213     /// Get the TrackingParticle
0214     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0215 
0216     /// Count the NULL TrackingParticles
0217     if (tp_mom.at(itp) <= 0.01 * tp_tot) {
0218       nullTPs++;
0219     } else {
0220       /// Store the pointers (addresses) of the TrackingParticle
0221       /// to be able to count how many different there are
0222       tpAddressVector.push_back(curTP.get());
0223     }
0224   }
0225 
0226   /// Count how many different TrackingParticle there are
0227   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0228   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0229   goodDifferentTPs = tpAddressVector.size();
0230 
0231   return (goodDifferentTPs == 1);
0232 }
0233 
0234 template <typename T>
0235 bool TTClusterAssociationMap<T>::isUnknown(TTClusterRefT<T> aCluster) const {
0236   /// Get the TrackingParticles
0237   const std::vector<TrackingParticlePtr>& theseTrackingParticles = this->findTrackingParticlePtrs(aCluster);
0238 
0239   /// If the vector is empty, then the cluster is UNKNOWN
0240   if (theseTrackingParticles.empty())
0241     return true;
0242 
0243   /// If we are here, it means there are some TrackingParticles
0244   unsigned int goodDifferentTPs = 0;
0245   std::vector<const TrackingParticle*> tpAddressVector;
0246 
0247   /// Loop over the TrackingParticles
0248   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0249     /// Get the TrackingParticle
0250     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0251 
0252     /// Count the non-NULL TrackingParticles
0253     if (!curTP.isNull()) {
0254       /// Store the pointers (addresses) of the TrackingParticle
0255       /// to be able to count how many different there are
0256       tpAddressVector.push_back(curTP.get());
0257     }
0258   }
0259 
0260   /// Count how many different TrackingParticle there are
0261   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0262   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0263   goodDifferentTPs = tpAddressVector.size();
0264 
0265   /// UNKNOWN means no good TP is found
0266   return (goodDifferentTPs == 0);
0267 }
0268 
0269 template <typename T>
0270 bool TTClusterAssociationMap<T>::isCombinatoric(TTClusterRefT<T> aCluster) const {
0271   /// Get the TrackingParticles
0272   const std::vector<TrackingParticlePtr>& theseTrackingParticles = this->findTrackingParticlePtrs(aCluster);
0273 
0274   /// If the vector is empty, then the cluster is UNKNOWN
0275   if (theseTrackingParticles.empty())
0276     return false;
0277 
0278   bool genuineClu = this->isGenuine(aCluster);
0279   bool unknownClu = this->isUnknown(aCluster);
0280 
0281   if (genuineClu || unknownClu)
0282     return false;
0283 
0284   return true;
0285 
0286   /// If we are here, it means there are some TrackingParticles
0287   unsigned int nullTPs = 0;
0288   unsigned int goodDifferentTPs = 0;
0289   std::vector<const TrackingParticle*> tpAddressVector;
0290 
0291   /// Loop over the TrackingParticles
0292   for (unsigned int itp = 0; itp < theseTrackingParticles.size(); itp++) {
0293     /// Get the TrackingParticle
0294     TrackingParticlePtr curTP = theseTrackingParticles.at(itp);
0295 
0296     /// Count the NULL TrackingParticles
0297     if (curTP.isNull()) {
0298       nullTPs++;
0299     } else {
0300       /// Store the pointers (addresses) of the TrackingParticle
0301       /// to be able to count how many different there are
0302       tpAddressVector.push_back(curTP.get());
0303     }
0304   }
0305 
0306   /// Count how many different TrackingParticle there are
0307   std::sort(tpAddressVector.begin(), tpAddressVector.end());
0308   tpAddressVector.erase(std::unique(tpAddressVector.begin(), tpAddressVector.end()), tpAddressVector.end());
0309   goodDifferentTPs = tpAddressVector.size();
0310 
0311   /// COMBINATORIC means no NULLs and more than one good TP
0312   /// OR, in alternative, only one good TP but non-zero NULLS
0313   //return ( ( nullTPs == 0 && goodDifferentTPs > 1 ) || ( nullTPs > 0 && goodDifferentTPs > 0 ) );
0314   return (goodDifferentTPs > 1);
0315 }
0316 
0317 #endif