Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*! \brief   
0002  *  \details Here, in the source file, the methods which do depend
0003  *           on the specific type <T> that can fit the template.
0004  *
0005  *  \author Nicola Pozzobon
0006  *  \date   2013, Jul 19
0007  *  
0008  */
0009 
0010 #include "SimTracker/TrackTriggerAssociation/plugins/TTTrackAssociator.h"
0011 
0012 /// Implement the producer
0013 template <>
0014 void TTTrackAssociator<Ref_Phase2TrackerDigi_>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0015   /// Exit if real data
0016   if (iEvent.isRealData())
0017     return;
0018 
0019   /// Get the Stub and Cluster MC truth
0020   edm::Handle<TTClusterAssociationMap<Ref_Phase2TrackerDigi_>> ttClusterAssociationMapHandle;
0021   iEvent.getByToken(ttClusterTruthToken_, ttClusterAssociationMapHandle);
0022   edm::Handle<TTStubAssociationMap<Ref_Phase2TrackerDigi_>> ttStubAssociationMapHandle;
0023   iEvent.getByToken(ttStubTruthToken_, ttStubAssociationMapHandle);
0024 
0025   int ncont1 = 0;
0026 
0027   /// Loop over InputTags to handle multiple collections
0028   for (const auto& iTag : ttTracksTokens_) {
0029     /// Prepare output
0030     auto associationMapForOutput = std::make_unique<TTTrackAssociationMap<Ref_Phase2TrackerDigi_>>();
0031 
0032     /// Get the Tracks already stored away
0033     edm::Handle<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>> TTTrackHandle;
0034     iEvent.getByToken(iTag, TTTrackHandle);
0035 
0036     /// Prepare the necessary maps
0037     std::map<TTTrackPtr, TrackingParticlePtr> trackToTrackingParticleMap;
0038     std::map<TrackingParticlePtr, std::vector<TTTrackPtr>> trackingParticleToTrackVectorMap;
0039 
0040     // Start the loop on tracks
0041 
0042     for (unsigned int jTrk = 0; jTrk < TTTrackHandle->size(); jTrk++) {
0043       /// Make the pointer to be put in the map
0044       TTTrackPtr tempTrackPtr(TTTrackHandle, jTrk);
0045 
0046       /// Get all the stubs of the TTTrack (theseStubs)
0047       const std::vector<TTStubRef>& theseStubs = tempTrackPtr->getStubRefs();
0048 
0049       /// Auxiliary map to relate TP addresses and TP edm::Ptr
0050       std::map<const TrackingParticle*, TrackingParticlePtr> auxMap;
0051       int mayCombinUnknown = 0;
0052 
0053       /// Fill the map associating each TP to a vector of L1 tracks.
0054       /// Do this using the association map of the clusters inside each stub,
0055       /// as stub associator misses stub --> all TP map (FIX).
0056       for (const TTStubRef& stub : theseStubs) {
0057         for (unsigned int ic = 0; ic < 2; ic++) {
0058           const std::vector<TrackingParticlePtr>& tempTPs =
0059               ttClusterAssociationMapHandle->findTrackingParticlePtrs(stub->clusterRef(ic));
0060           for (const TrackingParticlePtr& testTP : tempTPs)  // List of TPs linked to stub clusters
0061           {
0062             if (testTP.isNull())  // No TP linked to this cluster
0063               continue;
0064 
0065             /// Prepare the maps wrt TrackingParticle
0066             if (trackingParticleToTrackVectorMap.find(testTP) == trackingParticleToTrackVectorMap.end()) {
0067               std::vector<TTTrackPtr> trackVector;
0068               trackingParticleToTrackVectorMap.emplace(testTP, trackVector);
0069             }
0070             trackingParticleToTrackVectorMap.find(testTP)->second.push_back(tempTrackPtr);  /// Fill the auxiliary map
0071 
0072             /// Fill the other auxiliary map
0073             if (auxMap.find(testTP.get()) == auxMap.end()) {
0074               auxMap.emplace(testTP.get(), testTP);
0075             }
0076           }
0077         }  /// End of loop over the clusters
0078 
0079         /// Check if the stub is unknown
0080         if (ttStubAssociationMapHandle->isUnknown(stub))
0081           ++mayCombinUnknown;
0082 
0083       }  /// End of loop over the stubs
0084 
0085       /// If there are >= 2 unknown stubs, go to the next track
0086       /// as this track may be COMBINATORIC or UNKNOWN
0087       /// (One unknown is allowed, if in 2S module).
0088       if (mayCombinUnknown >= 2)
0089         continue;
0090 
0091       /// If we are here, all the stubs on track are either combinatoric or genuine
0092       /// and there is no more than one fake stub in the track
0093       /// Loop over all the TrackingParticle which have been found in the track
0094       /// (stored in auxMap), to check if any are present in all stubs on Track.
0095 
0096       std::vector<const TrackingParticle*> tpInAllStubs;
0097 
0098       for (const auto& auxPair : auxMap) {
0099         /// Get all associated stubs of this TrackingParticle
0100         const std::vector<TTStubRef>& tempStubs = ttStubAssociationMapHandle->findTTStubRefs(auxPair.second);
0101 
0102         // Count stubs on track that are not related to this TP
0103         int nnotfound = 0;
0104         for (const TTStubRef& stub : theseStubs) {
0105           /// We want that all the stubs of the track are included in the container of
0106           /// all the stubs produced by this particular TrackingParticle which we
0107           /// already know is one of the TrackingParticles that released hits
0108           /// in this track we are evaluating right now
0109           if (std::find(tempStubs.begin(), tempStubs.end(), stub) == tempStubs.end()) {
0110             ++nnotfound;
0111           }
0112         }
0113 
0114         /// If this TP does not appear in all stubs (allowing one wrong stub)
0115         /// then try next TP.
0116         if (nnotfound > 1)
0117           continue;
0118 
0119         /// If we are here, it means that the TrackingParticle
0120         /// generates hits in all stubs (allowing one incorrect one) of the current track
0121         /// so put it into the vector
0122         tpInAllStubs.push_back(auxPair.first);
0123       }
0124 
0125       /// Count how many TrackingParticles were associated to all stubs on this track.
0126       /// FIX: Could avoid this by using std::set for tpInAllStubs?
0127       std::sort(tpInAllStubs.begin(), tpInAllStubs.end());
0128       tpInAllStubs.erase(std::unique(tpInAllStubs.begin(), tpInAllStubs.end()), tpInAllStubs.end());
0129       unsigned int nTPs = tpInAllStubs.size();
0130 
0131       /// If only one TP associated to all stubs (allowing one incorrect) on track: GENUINE or LOOSELY_GENUINE.
0132       /// If 0 or >= 2 TP: COMBINATORIC
0133       /// WARNING: This means if one TP matches all stubs, and another matches all STUBS except
0134       /// one, then the trackToTrackingParticleMap will not be filled.
0135       /// WARNING: This also means that trackToTrackingParticleMap will be filled if
0136       /// one TP matches all stubs, except for an incorrect one in either PS or 2S modules.
0137       if (nTPs != 1)
0138         continue;
0139 
0140       /// Here, the track may only be GENUINE/LOOSELY_GENUINE
0141       /// CHECK: Surely if one incorrect PS stub, it can also be COMBINATORIC?
0142       /// Fill the map associating track to its principle TP.
0143       trackToTrackingParticleMap.emplace(tempTrackPtr, auxMap.find(tpInAllStubs.at(0))->second);
0144 
0145     }  /// End of loop over Tracks
0146 
0147     /// Remove duplicates from the only output map that needs it.
0148     /// (Map gets multiple entries per track if it has several stubs belonging to same TP).
0149     for (auto& p : trackingParticleToTrackVectorMap) {
0150       /// Get the vector of edm::Ptr< TTTrack >
0151       /// (CHECK: Couldn't this be done by reference, to save CPU?)
0152       std::vector<TTTrackPtr>& tempVector = p.second;
0153 
0154       /// Sort and remove duplicates
0155       std::sort(tempVector.begin(), tempVector.end());
0156       tempVector.erase(std::unique(tempVector.begin(), tempVector.end()), tempVector.end());
0157     }
0158 
0159     /// Also, create the pointer to the TTClusterAssociationMap
0160     edm::RefProd<TTStubAssociationMap<Ref_Phase2TrackerDigi_>> theStubAssoMap(ttStubAssociationMapHandle);
0161 
0162     /// Put the maps in the association object
0163     associationMapForOutput->setTTTrackToTrackingParticleMap(trackToTrackingParticleMap);
0164     associationMapForOutput->setTrackingParticleToTTTracksMap(trackingParticleToTrackVectorMap);
0165     associationMapForOutput->setTTStubAssociationMap(theStubAssoMap);
0166     associationMapForOutput->setAllowOneFalse2SStub(TTTrackAllowOneFalse2SStub);
0167 
0168     /// Put output in the event
0169     iEvent.put(std::move(associationMapForOutput), ttTracksInputTags_.at(ncont1).instance());
0170 
0171     ++ncont1;
0172   }  /// End of loop over InputTags
0173 }