Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-08-09 23:47:42

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