Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-26 22:29:43

0001 //
0002 // Package:         RecoTracker/FinalTrackSelectors
0003 // Class:           TrackListMerger
0004 //
0005 // Description:     Hit Dumper
0006 //
0007 // Original Author: Steve Wagner, stevew@pizero.colorado.edu
0008 // Created:         Sat Jan 14 22:00:00 UTC 2006
0009 //
0010 //
0011 
0012 #include "DataFormats/Common/interface/Handle.h"
0013 #include "DataFormats/Common/interface/ValueMap.h"
0014 #include "DataFormats/TrackReco/interface/Track.h"
0015 #include "DataFormats/TrackReco/interface/TrackBase.h"
0016 #include "DataFormats/TrackReco/interface/TrackExtra.h"
0017 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0018 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0019 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/Framework/interface/stream/EDProducer.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/Utilities/interface/InputTag.h"
0025 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0026 #include "FWCore/Utilities/interface/ESGetToken.h"
0027 #include "RecoTracker/FinalTrackSelectors/interface/TrackAlgoPriorityOrder.h"
0028 #include "RecoTracker/Record/interface/CkfComponentsRecord.h"
0029 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0030 #include "TrackingTools/PatternTools/interface/Trajectory.h"
0031 
0032 class dso_hidden TrackListMerger : public edm::stream::EDProducer<> {
0033 public:
0034   explicit TrackListMerger(const edm::ParameterSet& conf);
0035 
0036   ~TrackListMerger() override = default;
0037 
0038   void produce(edm::Event& e, const edm::EventSetup& c) override;
0039 
0040 private:
0041   void returnEmptyCollections(edm::Event& e);
0042 
0043   using MVACollection = std::vector<float>;
0044   using QualityMaskCollection = std::vector<unsigned char>;
0045 
0046   std::unique_ptr<reco::TrackCollection> outputTrks;
0047   std::unique_ptr<reco::TrackExtraCollection> outputTrkExtras;
0048   std::unique_ptr<TrackingRecHitCollection> outputTrkHits;
0049   std::unique_ptr<std::vector<Trajectory>> outputTrajs;
0050   std::unique_ptr<TrajTrackAssociationCollection> outputTTAss;
0051   std::unique_ptr<TrajectorySeedCollection> outputSeeds;
0052 
0053   reco::TrackRefProd refTrks;
0054   reco::TrackExtraRefProd refTrkExtras;
0055   TrackingRecHitRefProd refTrkHits;
0056   edm::RefProd<std::vector<Trajectory>> refTrajs;
0057   edm::RefProd<TrajectorySeedCollection> refTrajSeeds;
0058 
0059   bool copyExtras_;
0060   bool makeReKeyedSeeds_;
0061 
0062   edm::ESGetToken<TrackAlgoPriorityOrder, CkfComponentsRecord> priorityToken;
0063 
0064   struct TkEDGetTokenss {
0065     edm::InputTag tag;
0066     edm::EDGetTokenT<reco::TrackCollection> tk;
0067     edm::EDGetTokenT<std::vector<Trajectory>> traj;
0068     edm::EDGetTokenT<TrajTrackAssociationCollection> tass;
0069     edm::EDGetTokenT<edm::ValueMap<int>> tsel;
0070     edm::EDGetTokenT<edm::ValueMap<float>> tmva;
0071     TkEDGetTokenss() {}
0072     TkEDGetTokenss(const edm::InputTag& tag_,
0073                    edm::EDGetTokenT<reco::TrackCollection>&& tk_,
0074                    edm::EDGetTokenT<std::vector<Trajectory>>&& traj_,
0075                    edm::EDGetTokenT<TrajTrackAssociationCollection>&& tass_,
0076                    edm::EDGetTokenT<edm::ValueMap<int>>&& tsel_,
0077                    edm::EDGetTokenT<edm::ValueMap<float>>&& tmva_)
0078         : tag(tag_), tk(tk_), traj(traj_), tass(tass_), tsel(tsel_), tmva(tmva_) {}
0079   };
0080   TkEDGetTokenss edTokens(const edm::InputTag& tag, const edm::InputTag& seltag, const edm::InputTag& mvatag) {
0081     return TkEDGetTokenss(tag,
0082                           consumes<reco::TrackCollection>(tag),
0083                           consumes<std::vector<Trajectory>>(tag),
0084                           consumes<TrajTrackAssociationCollection>(tag),
0085                           consumes<edm::ValueMap<int>>(seltag),
0086                           consumes<edm::ValueMap<float>>(mvatag));
0087   }
0088   TkEDGetTokenss edTokens(const edm::InputTag& tag, const edm::InputTag& mvatag) {
0089     return TkEDGetTokenss(tag,
0090                           consumes<reco::TrackCollection>(tag),
0091                           consumes<std::vector<Trajectory>>(tag),
0092                           consumes<TrajTrackAssociationCollection>(tag),
0093                           edm::EDGetTokenT<edm::ValueMap<int>>(),
0094                           consumes<edm::ValueMap<float>>(mvatag));
0095   }
0096   std::vector<TkEDGetTokenss> trackProducers_;
0097 
0098   std::string priorityName_;
0099 
0100   double maxNormalizedChisq_;
0101   double minPT_;
0102   unsigned int minFound_;
0103   float epsilon_;
0104   float shareFrac_;
0105   float foundHitBonus_;
0106   float lostHitPenalty_;
0107   std::vector<double> indivShareFrac_;
0108 
0109   std::vector<std::vector<int>> listsToMerge_;
0110   std::vector<bool> promoteQuality_;
0111   std::vector<int> hasSelector_;
0112   bool copyMVA_;
0113 
0114   bool allowFirstHitShare_;
0115   reco::TrackBase::TrackQuality qualityToSet_;
0116   bool use_sharesInput_;
0117   bool trkQualMod_;
0118 };
0119 
0120 #include <memory>
0121 #include <string>
0122 #include <iostream>
0123 #include <cmath>
0124 #include <vector>
0125 
0126 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2DCollection.h"
0127 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2DCollection.h"
0128 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1DCollection.h"
0129 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
0130 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
0131 #include "DataFormats/Common/interface/ValueMap.h"
0132 #include "FWCore/Framework/interface/ESHandle.h"
0133 
0134 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0135 
0136 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0137 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0138 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0139 
0140 //#include "DataFormats/TrackReco/src/classes.h"
0141 
0142 #include "TrackingTools/PatternTools/interface/ClusterRemovalRefSetter.h"
0143 
0144 #ifdef STAT_TSB
0145 #include <x86intrin.h>
0146 #endif
0147 
0148 namespace {
0149 #ifdef STAT_TSB
0150   inline volatile unsigned long long rdtsc() { return __rdtsc(); }
0151 
0152   struct StatCount {
0153     float maxDP = 0.;
0154     float maxDE = 0.;
0155     unsigned long long st;
0156     long long totBegin = 0;
0157     long long totPre = 0;
0158     long long totEnd = 0;
0159     unsigned long long timeNo;  // no-overlap
0160     unsigned long long timeOv;  // overlap
0161     void begin(int tt) { totBegin += tt; }
0162     void start() { st = rdtsc(); }
0163     void noOverlap() { timeNo += (rdtsc() - st); }
0164     void overlap() { timeOv += (rdtsc() - st); }
0165     void pre(int tt) { totPre += tt; }
0166     void end(int tt) { totEnd += tt; }
0167     void de(float d) {
0168       if (d > maxDE)
0169         maxDE = d;
0170     }
0171     void dp(float d) {
0172       if (d > maxDP)
0173         maxDP = d;
0174     }
0175 
0176     void print() const {
0177       std::cout << "TrackListMerger stat\nBegin/Pre/End/maxDPhi/maxDEta/Overlap/NoOverlap " << totBegin << '/' << totPre
0178                 << '/' << totEnd << '/' << maxDP << '/' << maxDE << '/' << timeOv / 1000 << '/' << timeNo / 1000
0179                 << std::endl;
0180     }
0181     StatCount() {}
0182     ~StatCount() { print(); }
0183   };
0184   StatCount statCount;
0185 
0186 #else
0187   struct StatCount {
0188     void begin(int) {}
0189     void pre(int) {}
0190     void end(int) {}
0191     void start() {}
0192     void noOverlap() {}
0193     void overlap() {}
0194     void de(float) {}
0195     void dp(float) {}
0196   };
0197   CMS_THREAD_SAFE StatCount statCount;
0198 #endif
0199 
0200 }  // namespace
0201 
0202 namespace {
0203   edm::ProductID clusterProductB(const TrackingRecHit* hit) {
0204     return reinterpret_cast<const BaseTrackerRecHit*>(hit)->firstClusterRef().id();
0205   }
0206 }  // namespace
0207 
0208 TrackListMerger::TrackListMerger(edm::ParameterSet const& conf) {
0209   copyExtras_ = conf.getUntrackedParameter<bool>("copyExtras", true);
0210   priorityName_ = conf.getParameter<std::string>("trackAlgoPriorityOrder");
0211 
0212   std::vector<edm::InputTag> trackProducerTags(conf.getParameter<std::vector<edm::InputTag>>("TrackProducers"));
0213   //which of these do I need to turn into vectors?
0214   maxNormalizedChisq_ = conf.getParameter<double>("MaxNormalizedChisq");
0215   minPT_ = conf.getParameter<double>("MinPT");
0216   minFound_ = (unsigned int)conf.getParameter<int>("MinFound");
0217   epsilon_ = conf.getParameter<double>("Epsilon");
0218   shareFrac_ = conf.getParameter<double>("ShareFrac");
0219   allowFirstHitShare_ = conf.getParameter<bool>("allowFirstHitShare");
0220   foundHitBonus_ = conf.getParameter<double>("FoundHitBonus");
0221   lostHitPenalty_ = conf.getParameter<double>("LostHitPenalty");
0222   indivShareFrac_ = conf.getParameter<std::vector<double>>("indivShareFrac");
0223   std::string qualityStr = conf.getParameter<std::string>("newQuality");
0224   priorityToken = esConsumes<TrackAlgoPriorityOrder, CkfComponentsRecord>(edm::ESInputTag("", priorityName_));
0225 
0226   if (!qualityStr.empty()) {
0227     qualityToSet_ = reco::TrackBase::qualityByName(conf.getParameter<std::string>("newQuality"));
0228   } else
0229     qualityToSet_ = reco::TrackBase::undefQuality;
0230 
0231   use_sharesInput_ = true;
0232   if (epsilon_ > 0.0)
0233     use_sharesInput_ = false;
0234 
0235   edm::VParameterSet setsToMerge = conf.getParameter<edm::VParameterSet>("setsToMerge");
0236 
0237   for (unsigned int i = 0; i < setsToMerge.size(); i++) {
0238     listsToMerge_.push_back(setsToMerge[i].getParameter<std::vector<int>>("tLists"));
0239     promoteQuality_.push_back(setsToMerge[i].getParameter<bool>("pQual"));
0240   }
0241   hasSelector_ = conf.getParameter<std::vector<int>>("hasSelector");
0242   copyMVA_ = conf.getParameter<bool>("copyMVA");
0243 
0244   std::vector<edm::InputTag> selectors(conf.getParameter<std::vector<edm::InputTag>>("selectedTrackQuals"));
0245   std::vector<edm::InputTag> mvaStores;
0246   if (conf.exists("mvaValueTags")) {
0247     mvaStores = conf.getParameter<std::vector<edm::InputTag>>("mvaValueTags");
0248   } else {
0249     for (int i = 0; i < (int)selectors.size(); i++) {
0250       edm::InputTag ntag(selectors[i].label(), "MVAVals");
0251       mvaStores.push_back(ntag);
0252     }
0253   }
0254   unsigned int numTrkColl = trackProducerTags.size();
0255   if (numTrkColl != hasSelector_.size() || numTrkColl != selectors.size()) {
0256     throw cms::Exception("Inconsistent size") << "need same number of track collections and selectors";
0257   }
0258   if (numTrkColl != hasSelector_.size() || numTrkColl != mvaStores.size()) {
0259     throw cms::Exception("Inconsistent size") << "need same number of track collections and MVA stores";
0260   }
0261   for (unsigned int i = indivShareFrac_.size(); i < numTrkColl; i++) {
0262     //      edm::LogWarning("TrackListMerger") << "No indivShareFrac for " << trackProducersTags <<". Using default value of 1";
0263     indivShareFrac_.push_back(1.0);
0264   }
0265 
0266   trkQualMod_ = conf.getParameter<bool>("writeOnlyTrkQuals");
0267   if (trkQualMod_) {
0268     bool ok = true;
0269     for (unsigned int i = 1; i < numTrkColl; i++) {
0270       if (!(trackProducerTags[i] == trackProducerTags[0]))
0271         ok = false;
0272     }
0273     if (!ok) {
0274       throw cms::Exception("Bad input") << "to use writeOnlyTrkQuals=True all input InputTags must be the same";
0275     }
0276     produces<edm::ValueMap<int>>();
0277     produces<QualityMaskCollection>("QualityMasks");
0278   } else {
0279     produces<reco::TrackCollection>();
0280 
0281     makeReKeyedSeeds_ = conf.getUntrackedParameter<bool>("makeReKeyedSeeds", false);
0282     if (makeReKeyedSeeds_) {
0283       copyExtras_ = true;
0284       produces<TrajectorySeedCollection>();
0285     }
0286 
0287     if (copyExtras_) {
0288       produces<reco::TrackExtraCollection>();
0289       produces<TrackingRecHitCollection>();
0290     }
0291     produces<std::vector<Trajectory>>();
0292     produces<TrajTrackAssociationCollection>();
0293   }
0294   produces<edm::ValueMap<float>>("MVAVals");
0295   produces<MVACollection>("MVAValues");
0296 
0297   // Do all the consumes
0298   trackProducers_.resize(numTrkColl);
0299   for (unsigned int i = 0; i < numTrkColl; ++i) {
0300     trackProducers_[i] = hasSelector_[i] > 0 ? edTokens(trackProducerTags[i], selectors[i], mvaStores[i])
0301                                              : edTokens(trackProducerTags[i], mvaStores[i]);
0302   }
0303 }
0304 
0305 // Functions that gets called by framework every event
0306 void TrackListMerger::produce(edm::Event& e, const edm::EventSetup& es) {
0307   // extract tracker geometry
0308   //
0309   //edm::ESHandle<TrackerGeometry> theG;
0310   //es.get<TrackerDigiGeometryRecord>().get(theG);
0311 
0312   //    using namespace reco;
0313 
0314   edm::ESHandle<TrackAlgoPriorityOrder> priorityH = es.getHandle(priorityToken);
0315   auto const& trackAlgoPriorityOrder = *priorityH;
0316 
0317   // get Inputs
0318   // if 1 input list doesn't exist, make an empty list, issue a warning, and continue
0319   // this allows TrackListMerger to be used as a cleaner only if handed just one list
0320   // if both input lists don't exist, will issue 2 warnings and generate an empty output collection
0321   //
0322   static const reco::TrackCollection s_empty;
0323 
0324   std::vector<const reco::TrackCollection*> trackColls;
0325   std::vector<edm::Handle<reco::TrackCollection>> trackHandles(trackProducers_.size());
0326   for (unsigned int i = 0; i < trackProducers_.size(); i++) {
0327     trackColls.push_back(nullptr);
0328     //edm::Handle<reco::TrackCollection> trackColl;
0329     e.getByToken(trackProducers_[i].tk, trackHandles[i]);
0330     if (trackHandles[i].isValid()) {
0331       trackColls[i] = trackHandles[i].product();
0332     } else {
0333       edm::LogWarning("TrackListMerger") << "TrackCollection " << trackProducers_[i].tag << " not found";
0334       trackColls[i] = &s_empty;
0335     }
0336   }
0337 
0338   if (trackColls.empty()) {
0339     // output empty collections and early return
0340     this->returnEmptyCollections(e);
0341     return;
0342   }
0343 
0344   unsigned int collsSize = trackColls.size();
0345   unsigned int rSize = 0;
0346   unsigned int trackCollSizes[collsSize];
0347   unsigned int trackCollFirsts[collsSize];
0348   for (unsigned int i = 0; i != collsSize; i++) {
0349     trackCollSizes[i] = trackColls[i]->size();
0350     trackCollFirsts[i] = rSize;
0351     rSize += trackCollSizes[i];
0352   }
0353 
0354   if (rSize == 0) {
0355     // output empty collections and early return
0356     this->returnEmptyCollections(e);
0357     return;
0358   }
0359 
0360   statCount.begin(rSize);
0361 
0362   //
0363   //  quality cuts first
0364   //
0365   int i = -1;
0366 
0367   int selected[rSize];
0368   int indexG[rSize];
0369   bool trkUpdated[rSize];
0370   int trackCollNum[rSize];
0371   int trackQuals[rSize];
0372   float trackMVAs[rSize];
0373   reco::TrackBase::TrackAlgorithm oriAlgo[rSize];
0374   std::vector<reco::TrackBase::AlgoMask> algoMask(rSize);
0375   for (unsigned int j = 0; j < rSize; j++) {
0376     indexG[j] = -1;
0377     selected[j] = 1;
0378     trkUpdated[j] = false;
0379     trackCollNum[j] = 0;
0380     trackQuals[j] = 0;
0381     trackMVAs[j] = -998.0;
0382     oriAlgo[j] = reco::TrackBase::undefAlgorithm;
0383   }
0384 
0385   int ngood = 0;
0386   for (unsigned int j = 0; j != collsSize; j++) {
0387     const reco::TrackCollection* tC1 = trackColls[j];
0388 
0389     edm::Handle<edm::ValueMap<int>> trackSelColl;
0390     edm::Handle<edm::ValueMap<float>> trackMVAStore;
0391     if (copyMVA_)
0392       e.getByToken(trackProducers_[j].tmva, trackMVAStore);
0393     if (hasSelector_[j] > 0) {
0394       e.getByToken(trackProducers_[j].tsel, trackSelColl);
0395     }
0396 
0397     if (!tC1->empty()) {
0398       unsigned int iC = 0;
0399       for (reco::TrackCollection::const_iterator track = tC1->begin(); track != tC1->end(); track++) {
0400         i++;
0401         trackCollNum[i] = j;
0402         trackQuals[i] = track->qualityMask();
0403         oriAlgo[i] = track->originalAlgo();
0404         algoMask[i] = track->algoMask();
0405 
0406         reco::TrackRef trkRef = reco::TrackRef(trackHandles[j], iC);
0407         if (copyMVA_)
0408           if ((*trackMVAStore).contains(trkRef.id()))
0409             trackMVAs[i] = (*trackMVAStore)[trkRef];
0410         if (hasSelector_[j] > 0) {
0411           int qual = (*trackSelColl)[trkRef];
0412           if (qual < 0) {
0413             selected[i] = 0;
0414             iC++;
0415             continue;
0416           } else {
0417             trackQuals[i] = qual;
0418           }
0419         }
0420         iC++;
0421         selected[i] = trackQuals[i] + 10;  //10 is magic number used throughout...
0422         if ((short unsigned)track->ndof() < 1) {
0423           selected[i] = 0;
0424           continue;
0425         }
0426         if (track->normalizedChi2() > maxNormalizedChisq_) {
0427           selected[i] = 0;
0428           continue;
0429         }
0430         if (track->found() < minFound_) {
0431           selected[i] = 0;
0432           continue;
0433         }
0434         if (track->pt() < minPT_) {
0435           selected[i] = 0;
0436           continue;
0437         }
0438         // good!
0439         indexG[i] = ngood++;
0440         //if ( beVerb) std::cout << "inverb " << track->pt() << " " << selected[i] << std::endl;
0441       }  //end loop over tracks
0442     }    //end more than 0 track
0443   }      // loop over trackcolls
0444 
0445   statCount.pre(ngood);
0446 
0447   //cache the id and rechits of valid hits
0448   typedef std::pair<unsigned int, const TrackingRecHit*> IHit;
0449   std::vector<std::vector<IHit>> rh1(ngood);  // "not an array" of vectors!
0450   //const TrackingRecHit*  fh1[ngood];  // first hit...
0451   reco::TrackBase::TrackAlgorithm algo[ngood];
0452   float score[ngood];
0453 
0454   for (unsigned int j = 0; j < rSize; j++) {
0455     if (selected[j] == 0)
0456       continue;
0457     int i = indexG[j];
0458     assert(i >= 0);
0459     unsigned int collNum = trackCollNum[j];
0460     unsigned int trackNum = j - trackCollFirsts[collNum];
0461     const reco::Track* track = &((trackColls[collNum])->at(trackNum));
0462 
0463     algo[i] = track->algo();
0464     int validHits = track->numberOfValidHits();
0465     int validPixelHits = track->hitPattern().numberOfValidPixelHits();
0466     int lostHits = track->numberOfLostHits();
0467     score[i] =
0468         foundHitBonus_ * validPixelHits + foundHitBonus_ * validHits - lostHitPenalty_ * lostHits - track->chi2();
0469 
0470     rh1[i].reserve(validHits);
0471     auto compById = [](IHit const& h1, IHit const& h2) { return h1.first < h2.first; };
0472     for (trackingRecHit_iterator it = track->recHitsBegin(); it != track->recHitsEnd(); ++it) {
0473       const TrackingRecHit* hit = (*it);
0474       unsigned int id = hit->rawId();
0475       if (hit->geographicalId().subdetId() > 2)
0476         id &= (~3);  // mask mono/stereo in strips...
0477       if LIKELY (hit->isValid()) {
0478         rh1[i].emplace_back(id, hit);
0479         std::push_heap(rh1[i].begin(), rh1[i].end(), compById);
0480       }
0481     }
0482     std::sort_heap(rh1[i].begin(), rh1[i].end(), compById);
0483   }
0484 
0485   //DL here
0486   if LIKELY (ngood > 1 && collsSize > 1)
0487     for (unsigned int ltm = 0; ltm < listsToMerge_.size(); ltm++) {
0488       int saveSelected[rSize];
0489       bool notActive[collsSize];
0490       for (unsigned int cn = 0; cn != collsSize; ++cn)
0491         notActive[cn] = find(listsToMerge_[ltm].begin(), listsToMerge_[ltm].end(), cn) == listsToMerge_[ltm].end();
0492 
0493       for (unsigned int i = 0; i < rSize; i++)
0494         saveSelected[i] = selected[i];
0495 
0496       //DL protect against 0 tracks?
0497       for (unsigned int i = 0; i < rSize - 1; i++) {
0498         if (selected[i] == 0)
0499           continue;
0500         unsigned int collNum = trackCollNum[i];
0501 
0502         //check that this track is in one of the lists for this iteration
0503         if (notActive[collNum])
0504           continue;
0505 
0506         int k1 = indexG[i];
0507         unsigned int nh1 = rh1[k1].size();
0508         int qualityMaskT1 = trackQuals[i];
0509 
0510         int nhit1 = nh1;  // validHits[k1];
0511         float score1 = score[k1];
0512 
0513         // start at next collection
0514         for (unsigned int j = i + 1; j < rSize; j++) {
0515           if (selected[j] == 0)
0516             continue;
0517           unsigned int collNum2 = trackCollNum[j];
0518           if ((collNum == collNum2) && indivShareFrac_[collNum] > 0.99)
0519             continue;
0520           //check that this track is in one of the lists for this iteration
0521           if (notActive[collNum2])
0522             continue;
0523 
0524           int k2 = indexG[j];
0525 
0526           int newQualityMask = -9;  //avoid resetting quality mask if not desired 10+ -9 =1
0527           if (promoteQuality_[ltm]) {
0528             int maskT1 = saveSelected[i] > 1 ? saveSelected[i] - 10 : qualityMaskT1;
0529             int maskT2 = saveSelected[j] > 1 ? saveSelected[j] - 10 : trackQuals[j];
0530             newQualityMask = (maskT1 | maskT2);  // take OR of trackQuality
0531           }
0532           unsigned int nh2 = rh1[k2].size();
0533           int nhit2 = nh2;
0534 
0535           auto share = use_sharesInput_ ? [](const TrackingRecHit* it, const TrackingRecHit* jt, float) -> bool {
0536             return it->sharesInput(jt, TrackingRecHit::some);
0537           }
0538           : [](const TrackingRecHit* it, const TrackingRecHit* jt, float eps) -> bool {
0539               float delta = std::abs(it->localPosition().x() - jt->localPosition().x());
0540               return (it->geographicalId() == jt->geographicalId()) && (delta < eps);
0541             };
0542 
0543           statCount.start();
0544 
0545           //loop over rechits
0546           int noverlap = 0;
0547           int firstoverlap = 0;
0548           // check first hit  (should use REAL first hit?)
0549           if UNLIKELY (allowFirstHitShare_ && rh1[k1][0].first == rh1[k2][0].first) {
0550             const TrackingRecHit* it = rh1[k1][0].second;
0551             const TrackingRecHit* jt = rh1[k2][0].second;
0552             if (share(it, jt, epsilon_))
0553               firstoverlap = 1;
0554           }
0555 
0556           // exploit sorting
0557           unsigned int jh = 0;
0558           unsigned int ih = 0;
0559           while (ih != nh1 && jh != nh2) {
0560             // break if not enough to go...
0561             // if ( nprecut-noverlap+firstoverlap > int(nh1-ih)) break;
0562             // if ( nprecut-noverlap+firstoverlap > int(nh2-jh)) break;
0563             auto const id1 = rh1[k1][ih].first;
0564             auto const id2 = rh1[k2][jh].first;
0565             if (id1 < id2)
0566               ++ih;
0567             else if (id2 < id1)
0568               ++jh;
0569             else {
0570               // in case of split-hit do full conbinatorics
0571               auto li = ih;
0572               while ((++li) != nh1 && id1 == rh1[k1][li].first) {
0573               }
0574               auto lj = jh;
0575               while ((++lj) != nh2 && id2 == rh1[k2][lj].first) {
0576               }
0577               for (auto ii = ih; ii != li; ++ii)
0578                 for (auto jj = jh; jj != lj; ++jj) {
0579                   const TrackingRecHit* it = rh1[k1][ii].second;
0580                   const TrackingRecHit* jt = rh1[k2][jj].second;
0581                   if (share(it, jt, epsilon_))
0582                     noverlap++;
0583                 }
0584               jh = lj;
0585               ih = li;
0586             }  // equal ids
0587 
0588           }  //loop over ih & jh
0589 
0590           bool dupfound =
0591               (collNum != collNum2)
0592                   ? (noverlap - firstoverlap) > (std::min(nhit1, nhit2) - firstoverlap) * shareFrac_
0593                   : (noverlap - firstoverlap) > (std::min(nhit1, nhit2) - firstoverlap) * indivShareFrac_[collNum];
0594 
0595           auto seti = [&](unsigned int ii, unsigned int jj) {
0596             selected[jj] = 0;
0597             selected[ii] = 10 + newQualityMask;  // add 10 to avoid the case where mask = 1
0598             trkUpdated[ii] = true;
0599             if (trackAlgoPriorityOrder.priority(oriAlgo[jj]) < trackAlgoPriorityOrder.priority(oriAlgo[ii]))
0600               oriAlgo[ii] = oriAlgo[jj];
0601             algoMask[ii] |= algoMask[jj];
0602             algoMask[jj] = algoMask[ii];  // in case we keep discarded
0603           };
0604 
0605           if (dupfound) {
0606             float score2 = score[k2];
0607             constexpr float almostSame =
0608                 0.01f;  // difference rather than ratio due to possible negative values for score
0609             if (score1 - score2 > almostSame) {
0610               seti(i, j);
0611             } else if (score2 - score1 > almostSame) {
0612               seti(j, i);
0613             } else {
0614               // If tracks from both iterations are virtually identical, choose the one with the best quality or with lower algo
0615               if ((trackQuals[j] &
0616                    (1 << reco::TrackBase::loose | 1 << reco::TrackBase::tight | 1 << reco::TrackBase::highPurity)) ==
0617                   (trackQuals[i] &
0618                    (1 << reco::TrackBase::loose | 1 << reco::TrackBase::tight | 1 << reco::TrackBase::highPurity))) {
0619                 //same quality, pick earlier algo
0620                 if (trackAlgoPriorityOrder.priority(algo[k1]) <= trackAlgoPriorityOrder.priority(algo[k2])) {
0621                   seti(i, j);
0622                 } else {
0623                   seti(j, i);
0624                 }
0625               } else if ((trackQuals[j] & (1 << reco::TrackBase::loose | 1 << reco::TrackBase::tight |
0626                                            1 << reco::TrackBase::highPurity)) <
0627                          (trackQuals[i] & (1 << reco::TrackBase::loose | 1 << reco::TrackBase::tight |
0628                                            1 << reco::TrackBase::highPurity))) {
0629                 seti(i, j);
0630               } else {
0631                 seti(j, i);
0632               }
0633             }  //end fi < fj
0634             statCount.overlap();
0635             /*
0636         if (at0[k1]&&at0[k2]) {
0637           statCount.dp(dphi);
0638           if (dz<1.f) statCount.de(deta);
0639         }
0640         */
0641           }  //end got a duplicate
0642           else {
0643             statCount.noOverlap();
0644           }
0645           //stop if the ith track is now unselected
0646           if (selected[i] == 0)
0647             break;
0648         }  //end track2 loop
0649       }    //end track loop
0650     }      //end loop over track list sets
0651 
0652   auto vmMVA = std::make_unique<edm::ValueMap<float>>();
0653   edm::ValueMap<float>::Filler fillerMVA(*vmMVA);
0654 
0655   // special case - if just doing the trkquals
0656   if (trkQualMod_) {
0657     unsigned int tSize = trackColls[0]->size();
0658     auto vm = std::make_unique<edm::ValueMap<int>>();
0659     edm::ValueMap<int>::Filler filler(*vm);
0660 
0661     std::vector<int> finalQuals(tSize, -1);  //default is unselected
0662     for (unsigned int i = 0; i < rSize; i++) {
0663       unsigned int tNum = i % tSize;
0664 
0665       if (selected[i] > 1) {
0666         finalQuals[tNum] = selected[i] - 10;
0667         if (trkUpdated[i])
0668           finalQuals[tNum] = (finalQuals[tNum] | (1 << qualityToSet_));
0669       }
0670       if (selected[i] == 1)
0671         finalQuals[tNum] = trackQuals[i];
0672     }
0673 
0674     filler.insert(trackHandles[0], finalQuals.begin(), finalQuals.end());
0675     filler.fill();
0676 
0677     e.put(std::move(vm));
0678     for (auto& q : finalQuals)
0679       q = std::max(q, 0);
0680     auto quals = std::make_unique<QualityMaskCollection>(finalQuals.begin(), finalQuals.end());
0681     e.put(std::move(quals), "QualityMasks");
0682 
0683     std::vector<float> mvaVec(tSize, -99);
0684 
0685     for (unsigned int i = 0; i < rSize; i++) {
0686       unsigned int tNum = i % tSize;
0687       mvaVec[tNum] = trackMVAs[tNum];
0688     }
0689 
0690     fillerMVA.insert(trackHandles[0], mvaVec.begin(), mvaVec.end());
0691     fillerMVA.fill();
0692     if (copyMVA_) {
0693       e.put(std::move(vmMVA), "MVAVals");
0694       auto mvas = std::make_unique<MVACollection>(mvaVec.begin(), mvaVec.end());
0695       e.put(std::move(mvas), "MVAValues");
0696     }
0697     return;
0698   }
0699 
0700   //
0701   //  output selected tracks - if any
0702   //
0703 
0704   std::vector<reco::TrackRef> trackRefs(rSize);
0705   std::vector<edm::RefToBase<TrajectorySeed>> seedsRefs(rSize);
0706 
0707   unsigned int nToWrite = 0;
0708   for (unsigned int i = 0; i < rSize; i++)
0709     if (selected[i] != 0)
0710       nToWrite++;
0711 
0712   std::vector<float> mvaVec;
0713 
0714   outputTrks = std::make_unique<reco::TrackCollection>();
0715   outputTrks->reserve(nToWrite);
0716   refTrks = e.getRefBeforePut<reco::TrackCollection>();
0717 
0718   if (copyExtras_) {
0719     outputTrkExtras = std::make_unique<reco::TrackExtraCollection>();
0720     outputTrkExtras->reserve(nToWrite);
0721     refTrkExtras = e.getRefBeforePut<reco::TrackExtraCollection>();
0722     outputTrkHits = std::make_unique<TrackingRecHitCollection>();
0723     outputTrkHits->reserve(nToWrite * 25);
0724     refTrkHits = e.getRefBeforePut<TrackingRecHitCollection>();
0725     if (makeReKeyedSeeds_) {
0726       outputSeeds = std::make_unique<TrajectorySeedCollection>();
0727       outputSeeds->reserve(nToWrite);
0728       refTrajSeeds = e.getRefBeforePut<TrajectorySeedCollection>();
0729     }
0730   }
0731 
0732   outputTrajs = std::make_unique<std::vector<Trajectory>>();
0733   outputTrajs->reserve(rSize);
0734 
0735   for (unsigned int i = 0; i < rSize; i++) {
0736     if (selected[i] == 0) {
0737       trackRefs[i] = reco::TrackRef();
0738       continue;
0739     }
0740 
0741     unsigned int collNum = trackCollNum[i];
0742     unsigned int trackNum = i - trackCollFirsts[collNum];
0743     const reco::Track* track = &((trackColls[collNum])->at(trackNum));
0744     outputTrks->push_back(reco::Track(*track));
0745     mvaVec.push_back(trackMVAs[i]);
0746     if (selected[i] > 1) {
0747       outputTrks->back().setQualityMask(selected[i] - 10);
0748       if (trkUpdated[i])
0749         outputTrks->back().setQuality(qualityToSet_);
0750     }
0751     //might duplicate things, but doesnt hurt
0752     if (selected[i] == 1)
0753       outputTrks->back().setQualityMask(trackQuals[i]);
0754     outputTrks->back().setOriginalAlgorithm(oriAlgo[i]);
0755     outputTrks->back().setAlgoMask(algoMask[i]);
0756 
0757     // if ( beVerb ) std::cout << "selected " << outputTrks->back().pt() << " " << outputTrks->back().qualityMask() << " " << selected[i] << std::endl;
0758 
0759     //fill the TrackCollection
0760     if (copyExtras_) {
0761       edm::RefToBase<TrajectorySeed> origSeedRef = track->seedRef();
0762       //creating a seed with rekeyed clusters if required
0763       if (makeReKeyedSeeds_) {
0764         bool doRekeyOnThisSeed = false;
0765 
0766         edm::InputTag clusterRemovalInfos("");
0767         //grab on of the hits of the seed
0768         if (origSeedRef->nHits() != 0) {
0769           TrackingRecHit const& hit = *origSeedRef->recHits().begin();
0770           if (hit.isValid()) {
0771             edm::ProductID pID = clusterProductB(&hit);
0772             // the cluster collection either produced a removalInfo or mot
0773             //get the clusterremoval info from the provenance: will rekey if this is found
0774             edm::Handle<reco::ClusterRemovalInfo> CRIh;
0775             edm::StableProvenance const& prov = e.getStableProvenance(pID);
0776             clusterRemovalInfos = edm::InputTag(prov.moduleLabel(), prov.productInstanceName(), prov.processName());
0777             doRekeyOnThisSeed = e.getByLabel(clusterRemovalInfos, CRIh);
0778           }  //valid hit
0779         }    //nhit!=0
0780 
0781         if (doRekeyOnThisSeed && !(clusterRemovalInfos == edm::InputTag(""))) {
0782           ClusterRemovalRefSetter refSetter(e, clusterRemovalInfos);
0783           TrajectorySeed::RecHitContainer newRecHitContainer;
0784           newRecHitContainer.reserve(origSeedRef->nHits());
0785           for (auto const& recHit : origSeedRef->recHits()) {
0786             newRecHitContainer.push_back(recHit);
0787             refSetter.reKey(&newRecHitContainer.back());
0788           }
0789           outputSeeds->push_back(
0790               TrajectorySeed(origSeedRef->startingState(), newRecHitContainer, origSeedRef->direction()));
0791         }
0792         //doRekeyOnThisSeed=true
0793         else {
0794           //just copy the one we had before
0795           outputSeeds->push_back(TrajectorySeed(*origSeedRef));
0796         }
0797         edm::Ref<TrajectorySeedCollection> pureRef(refTrajSeeds, outputSeeds->size() - 1);
0798         origSeedRef = edm::RefToBase<TrajectorySeed>(pureRef);
0799       }  //creating a new seed and rekeying it rechit clusters.
0800 
0801       // Fill TrackExtra collection
0802       outputTrkExtras->push_back(reco::TrackExtra(track->outerPosition(),
0803                                                   track->outerMomentum(),
0804                                                   track->outerOk(),
0805                                                   track->innerPosition(),
0806                                                   track->innerMomentum(),
0807                                                   track->innerOk(),
0808                                                   track->outerStateCovariance(),
0809                                                   track->outerDetId(),
0810                                                   track->innerStateCovariance(),
0811                                                   track->innerDetId(),
0812                                                   track->seedDirection(),
0813                                                   origSeedRef));
0814       seedsRefs[i] = origSeedRef;
0815       outputTrks->back().setExtra(reco::TrackExtraRef(refTrkExtras, outputTrkExtras->size() - 1));
0816       reco::TrackExtra& tx = outputTrkExtras->back();
0817       tx.setResiduals(track->residuals());
0818 
0819       // fill TrackingRecHits
0820       unsigned nh1 = track->recHitsSize();
0821       tx.setHits(refTrkHits, outputTrkHits->size(), nh1);
0822       tx.setTrajParams(track->extra()->trajParams(), track->extra()->chi2sX5());
0823       assert(tx.trajParams().size() == tx.recHitsSize());
0824       for (auto hh = track->recHitsBegin(), eh = track->recHitsEnd(); hh != eh; ++hh) {
0825         outputTrkHits->push_back((*hh)->clone());
0826       }
0827     }
0828     trackRefs[i] = reco::TrackRef(refTrks, outputTrks->size() - 1);
0829 
0830   }  //end faux loop over tracks
0831 
0832   //Fill the trajectories, etc. for 1st collection
0833   refTrajs = e.getRefBeforePut<std::vector<Trajectory>>();
0834 
0835   outputTTAss = std::make_unique<TrajTrackAssociationCollection>(refTrajs, refTrks);
0836 
0837   for (unsigned int ti = 0; ti < trackColls.size(); ti++) {
0838     edm::Handle<std::vector<Trajectory>> hTraj1;
0839     edm::Handle<TrajTrackAssociationCollection> hTTAss1;
0840     e.getByToken(trackProducers_[ti].traj, hTraj1);
0841     e.getByToken(trackProducers_[ti].tass, hTTAss1);
0842 
0843     if (hTraj1.failedToGet() || hTTAss1.failedToGet())
0844       continue;
0845 
0846     for (size_t i = 0, n = hTraj1->size(); i < n; ++i) {
0847       edm::Ref<std::vector<Trajectory>> trajRef(hTraj1, i);
0848       TrajTrackAssociationCollection::const_iterator match = hTTAss1->find(trajRef);
0849       if (match != hTTAss1->end()) {
0850         const edm::Ref<reco::TrackCollection>& trkRef = match->val;
0851         uint32_t oldKey = trackCollFirsts[ti] + static_cast<uint32_t>(trkRef.key());
0852         if (trackRefs[oldKey].isNonnull()) {
0853           outputTrajs->push_back(*trajRef);
0854           //if making extras and the seeds at the same time, change the seed ref on the trajectory
0855           if (copyExtras_ && makeReKeyedSeeds_)
0856             outputTrajs->back().setSeedRef(seedsRefs[oldKey]);
0857           outputTTAss->insert(edm::Ref<std::vector<Trajectory>>(refTrajs, outputTrajs->size() - 1), trackRefs[oldKey]);
0858         }
0859       }
0860     }
0861   }
0862 
0863   statCount.end(outputTrks->size());
0864 
0865   edm::ProductID nPID = refTrks.id();
0866   edm::TestHandle<reco::TrackCollection> outHandle(outputTrks.get(), nPID);
0867   fillerMVA.insert(outHandle, mvaVec.begin(), mvaVec.end());
0868   fillerMVA.fill();
0869 
0870   e.put(std::move(outputTrks));
0871   if (copyMVA_) {
0872     e.put(std::move(vmMVA), "MVAVals");
0873     auto mvas = std::make_unique<MVACollection>(mvaVec.begin(), mvaVec.end());
0874     e.put(std::move(mvas), "MVAValues");
0875   }
0876   if (copyExtras_) {
0877     e.put(std::move(outputTrkExtras));
0878     e.put(std::move(outputTrkHits));
0879     if (makeReKeyedSeeds_)
0880       e.put(std::move(outputSeeds));
0881   }
0882   e.put(std::move(outputTrajs));
0883   e.put(std::move(outputTTAss));
0884   return;
0885 
0886 }  //end produce
0887 
0888 void TrackListMerger::returnEmptyCollections(edm::Event& e) {
0889   if (trkQualMod_) {
0890     auto vm = std::make_unique<edm::ValueMap<int>>();
0891     e.put(std::move(vm));
0892     auto quals = std::make_unique<QualityMaskCollection>();
0893     e.put(std::move(quals), "QualityMasks");
0894   } else {
0895     auto outputTrks = std::make_unique<reco::TrackCollection>();
0896     e.put(std::move(outputTrks));
0897 
0898     if (makeReKeyedSeeds_) {
0899       auto outputSeeds = std::make_unique<TrajectorySeedCollection>();
0900       e.put(std::move(outputSeeds));
0901     }
0902 
0903     if (copyExtras_) {
0904       auto outputTrkExtras = std::make_unique<reco::TrackExtraCollection>();
0905       auto outputTrkHits = std::make_unique<TrackingRecHitCollection>();
0906       e.put(std::move(outputTrkExtras));
0907       e.put(std::move(outputTrkHits));
0908     }
0909 
0910     auto outputTrajs = std::make_unique<std::vector<Trajectory>>();
0911     outputTTAss = std::make_unique<TrajTrackAssociationCollection>();
0912     e.put(std::move(outputTrajs));
0913     e.put(std::move(outputTTAss));
0914   }
0915   auto vmMVA = std::make_unique<edm::ValueMap<float>>();
0916   e.put(std::move(vmMVA), "MVAVals");
0917   auto mvas = std::make_unique<MVACollection>();
0918   e.put(std::move(mvas), "MVAValues");
0919   return;
0920 }
0921 
0922 #include "FWCore/PluginManager/interface/ModuleDef.h"
0923 #include "FWCore/Framework/interface/MakerMacros.h"
0924 
0925 DEFINE_FWK_MODULE(TrackListMerger);