74 tag(tag_), tk(tk_), traj(traj_), tass(tass_), tsel(tsel_), tmva(tmva_) {}
78 consumes<std::vector<Trajectory> >(tag), consumes<TrajTrackAssociationCollection >(tag),
83 consumes<std::vector<Trajectory> >(tag), consumes<TrajTrackAssociationCollection >(tag),
140 #include <x86intrin.h> 145 inline volatile unsigned long long rdtsc() {
152 unsigned long long st;
153 long long totBegin=0;
156 unsigned long long timeNo;
157 unsigned long long timeOv;
161 void start() { st=rdtsc(); }
162 void noOverlap() { timeNo += (rdtsc()-st);}
163 void overlap() { timeOv += (rdtsc()-st);}
164 void pre(
int tt) { totPre+=
tt;}
165 void end(
int tt) { totEnd+=
tt;}
166 void de(
float d) {
if (d>maxDE) maxDE=
d;}
167 void dp(
float d) {
if (d>maxDP) maxDP=
d;}
171 std::cout <<
"TrackListMerger stat\nBegin/Pre/End/maxDPhi/maxDEta/Overlap/NoOverlap " 172 << totBegin <<
'/'<< totPre <<
'/'<< totEnd <<
'/'<< maxDP <<
'/'<< maxDE
173 <<
'/'<< timeOv/1000 <<
'/'<< timeNo/1000
177 ~StatCount() {
print();}
194 [[cms::thread_safe]] StatCount statCount;
212 std::vector<edm::InputTag> trackProducerTags(conf.
getParameter<std::vector<edm::InputTag> >(
"TrackProducers"));
214 maxNormalizedChisq_ = conf.
getParameter<
double>(
"MaxNormalizedChisq");
219 allowFirstHitShare_ = conf.
getParameter<
bool>(
"allowFirstHitShare");
220 foundHitBonus_ = conf.
getParameter<
double>(
"FoundHitBonus");
221 lostHitPenalty_ = conf.
getParameter<
double>(
"LostHitPenalty");
222 indivShareFrac_=conf.
getParameter<std::vector<double> >(
"indivShareFrac");
225 if (qualityStr !=
"") {
231 use_sharesInput_ =
true;
232 if ( epsilon_ > 0.0 )use_sharesInput_ =
false;
236 for (
unsigned int i=0;
i<setsToMerge.size();
i++) {
237 listsToMerge_.push_back(setsToMerge[
i].getParameter<std::vector< int> >(
"tLists"));
238 promoteQuality_.push_back(setsToMerge[
i].getParameter<bool>(
"pQual"));
240 hasSelector_ = conf.
getParameter<std::vector<int> > (
"hasSelector");
243 std::vector<edm::InputTag>
selectors(conf.
getParameter<std::vector<edm::InputTag> >(
"selectedTrackQuals"));
244 std::vector<edm::InputTag> mvaStores;
245 if(conf.
exists(
"mvaValueTags")){
246 mvaStores = conf.
getParameter<std::vector<edm::InputTag> >(
"mvaValueTags");
248 for (
int i = 0;
i < (
int)selectors.size();
i++){
250 mvaStores.push_back(ntag);
253 unsigned int numTrkColl = trackProducerTags.size();
254 if (numTrkColl != hasSelector_.size() || numTrkColl != selectors.size()) {
255 throw cms::Exception(
"Inconsistent size") <<
"need same number of track collections and selectors";
257 if (numTrkColl != hasSelector_.size() || numTrkColl != mvaStores.size()) {
258 throw cms::Exception(
"Inconsistent size") <<
"need same number of track collections and MVA stores";
260 for (
unsigned int i = indivShareFrac_.size();
i < numTrkColl;
i++) {
262 indivShareFrac_.push_back(1.0);
265 trkQualMod_=conf.
getParameter<
bool>(
"writeOnlyTrkQuals");
268 for (
unsigned int i=1;
i<numTrkColl;
i++) {
269 if (!(trackProducerTags[
i]==trackProducerTags[0])) ok=
false;
272 throw cms::Exception(
"Bad input") <<
"to use writeOnlyTrkQuals=True all input InputTags must be the same";
274 produces<edm::ValueMap<int> >();
275 produces<QualityMaskCollection>(
"QualityMasks");
278 produces<reco::TrackCollection>();
281 if (makeReKeyedSeeds_){
283 produces<TrajectorySeedCollection>();
287 produces<reco::TrackExtraCollection>();
288 produces<TrackingRecHitCollection>();
290 produces< std::vector<Trajectory> >();
291 produces< TrajTrackAssociationCollection >();
293 produces<edm::ValueMap<float> >(
"MVAVals");
294 produces<MVACollection>(
"MVAValues");
297 trackProducers_.resize(numTrkColl);
298 for (
unsigned int i = 0;
i < numTrkColl; ++
i) {
299 trackProducers_[
i] = hasSelector_[
i]>0 ? edTokens(trackProducerTags[
i], selectors[i], mvaStores[i]) : edTokens(trackProducerTags[i], mvaStores[i]);
330 std::vector<const reco::TrackCollection *> trackColls;
331 std::vector<edm::Handle<reco::TrackCollection> > trackHandles(trackProducers_.size());
332 for (
unsigned int i=0;
i<trackProducers_.size();
i++) {
333 trackColls.push_back(0);
336 if (trackHandles[i].isValid()) {
337 trackColls[
i]= trackHandles[
i].product();
339 edm::LogWarning(
"TrackListMerger") <<
"TrackCollection " << trackProducers_[
i].tag <<
" not found";
340 trackColls[
i]=&s_empty;
344 unsigned int collsSize =trackColls.size();
345 unsigned int rSize=0;
346 unsigned int trackCollSizes[collsSize];
347 unsigned int trackCollFirsts[collsSize];
348 for (
unsigned int i=0;
i!=collsSize;
i++) {
349 trackCollSizes[
i]=trackColls[
i]->size();
350 trackCollFirsts[
i]=rSize;
351 rSize+=trackCollSizes[
i];
354 statCount.begin(rSize);
363 bool trkUpdated[rSize];
364 int trackCollNum[rSize];
365 int trackQuals[rSize];
366 float trackMVAs[rSize];
368 std::vector<reco::TrackBase::AlgoMask> algoMask(rSize);
369 for (
unsigned int j=0; j<rSize;j++) {
374 for (
unsigned int j=0; j!= collsSize; j++) {
380 e.
getByToken(trackProducers_[j].tmva, trackMVAStore);
381 if ( hasSelector_[j]>0 ){
382 e.
getByToken(trackProducers_[j].tsel, trackSelColl);
385 if ( 0<tC1->size() ){
387 for (reco::TrackCollection::const_iterator
track=tC1->begin();
track!=tC1->end();
track++){
390 trackQuals[
i]=
track->qualityMask();
391 oriAlgo[
i]=
track->originalAlgo();
392 algoMask[
i]=
track->algoMask();
396 if( (*trackMVAStore).contains(trkRef.
id()) ) trackMVAs[
i] = (*trackMVAStore)[trkRef];
397 if ( hasSelector_[j]>0 ) {
398 int qual=(*trackSelColl)[trkRef];
408 selected[
i]=trackQuals[
i]+10;
409 if ((
short unsigned)
track->ndof() < 1){
413 if (
track->normalizedChi2() > maxNormalizedChisq_){
417 if (
track->found() < minFound_){
421 if (
track->pt() < minPT_){
433 statCount.pre(ngood);
436 typedef std::pair<unsigned int, const TrackingRecHit*> IHit;
437 std::vector<std::vector<IHit>> rh1(ngood);
443 for (
unsigned int j=0; j<rSize; j++) {
444 if (selected[j]==0)
continue;
447 unsigned int collNum=trackCollNum[j];
448 unsigned int trackNum=j-trackCollFirsts[collNum];
451 algo[
i]=track->
algo();
455 score[
i] = foundHitBonus_*validPixelHits+foundHitBonus_*validHits - lostHitPenalty_*lostHits - track->
chi2();
458 rh1[
i].reserve(validHits) ;
459 auto compById = [](IHit
const & h1, IHit
const & h2) {
return h1.first < h2.first;};
462 unsigned int id = hit->
rawId() ;
464 if likely(hit->
isValid()) { rh1[
i].emplace_back(
id,hit); std::push_heap(rh1[i].
begin(),rh1[i].
end(),compById); }
466 std::sort_heap(rh1[i].
begin(),rh1[i].
end(),compById);
470 if likely(ngood>1 && collsSize>1)
471 for (
unsigned int ltm=0; ltm<listsToMerge_.size(); ltm++) {
472 int saveSelected[rSize];
473 bool notActive[collsSize];
474 for (
unsigned int cn=0;cn!=collsSize;++cn)
475 notActive[cn]=
find(listsToMerge_[ltm].
begin(),listsToMerge_[ltm].end(),cn)==listsToMerge_[ltm].
end();
477 for (
unsigned int i=0; i<rSize; i++) saveSelected[i]=selected[i];
480 for (
unsigned int i=0; i<rSize-1; i++) {
481 if (selected[i]==0)
continue;
482 unsigned int collNum=trackCollNum[
i];
485 if (notActive[collNum])
continue;
488 unsigned int nh1=rh1[k1].size();
489 int qualityMaskT1 = trackQuals[
i];
492 float score1 = score[k1];
495 for (
unsigned int j=i+1; j<rSize; j++) {
496 if (selected[j]==0)
continue;
497 unsigned int collNum2=trackCollNum[j];
498 if ( (collNum == collNum2) && indivShareFrac_[collNum] > 0.99)
continue;
500 if (notActive[collNum2])
continue;
504 int newQualityMask = -9;
505 if (promoteQuality_[ltm]) {
506 int maskT1= saveSelected[
i]>1? saveSelected[
i]-10 : qualityMaskT1;
507 int maskT2= saveSelected[j]>1? saveSelected[j]-10 : trackQuals[j];
508 newQualityMask =(maskT1 | maskT2);
510 unsigned int nh2=rh1[k2].size();
514 auto share = use_sharesInput_ ?
530 if (share(it,jt,epsilon_)) firstoverlap=1;
537 while (ih!=nh1 && jh!=nh2) {
541 auto const id1 = rh1[k1][ih].first;
542 auto const id2 = rh1[k2][jh].first;
544 else if (id2<id1) ++jh;
547 auto li=ih;
while( (++li)!=nh1 && id1 == rh1[k1][li].first);
548 auto lj=jh;
while( (++lj)!=nh2 && id2 == rh1[k2][lj].first);
549 for (
auto ii=ih;
ii!=li; ++
ii)
550 for (
auto jj=jh;
jj!=lj; ++
jj) {
553 if (share(it,jt,epsilon_)) noverlap++;
560 bool dupfound = (collNum != collNum2) ? (noverlap-firstoverlap) > (
std::min(nhit1,nhit2)-firstoverlap)*shareFrac_ :
561 (noverlap-firstoverlap) > (
std::min(nhit1,nhit2)-firstoverlap)*indivShareFrac_[collNum];
563 auto seti = [&](
unsigned int ii,
unsigned int jj) {
565 selected[
ii]=10+newQualityMask;
568 algoMask[
ii] |= algoMask[
jj];
569 algoMask[
jj] = algoMask[
ii];
573 float score2 = score[k2];
575 if ( score1 - score2 > almostSame ) {
577 }
else if ( score2 - score1 > almostSame ) {
605 statCount.noOverlap();
608 if (selected[i]==0)
break;
615 auto vmMVA = std::make_unique<edm::ValueMap<float>>();
622 unsigned int tSize=trackColls[0]->size();
623 auto vm = std::make_unique<edm::ValueMap<int>>();
626 std::vector<int> finalQuals(tSize,-1);
627 for (
unsigned int i=0; i<rSize; i++) {
628 unsigned int tNum=i%tSize;
630 if (selected[i]>1 ) {
631 finalQuals[tNum]=selected[
i]-10;
633 finalQuals[tNum]=(finalQuals[tNum] | (1<<qualityToSet_));
635 if ( selected[i]==1 )
636 finalQuals[tNum]=trackQuals[
i];
639 filler.insert(trackHandles[0], finalQuals.begin(),finalQuals.end());
644 auto quals = std::make_unique<QualityMaskCollection>(finalQuals.begin(),finalQuals.end());
647 std::vector<float> mvaVec(tSize,-99);
649 for(
unsigned int i = 0; i < rSize; i++){
650 unsigned int tNum = i % tSize;
651 mvaVec[tNum] = trackMVAs[tNum];
654 fillerMVA.insert(trackHandles[0],mvaVec.begin(),mvaVec.end());
658 auto mvas = std::make_unique<MVACollection>(mvaVec.begin(),mvaVec.end());
669 std::vector<reco::TrackRef> trackRefs(rSize);
670 std::vector<edm::RefToBase<TrajectorySeed>> seedsRefs(rSize);
672 unsigned int nToWrite=0;
673 for (
unsigned int i=0; i<rSize; i++)
674 if (selected[i]!=0) nToWrite++;
676 std::vector<float> mvaVec;
678 outputTrks = std::make_unique<reco::TrackCollection>();
679 outputTrks->reserve(nToWrite);
683 outputTrkExtras = std::make_unique<reco::TrackExtraCollection>();
684 outputTrkExtras->reserve(nToWrite);
686 outputTrkHits = std::make_unique<TrackingRecHitCollection>();
687 outputTrkHits->reserve(nToWrite*25);
689 if (makeReKeyedSeeds_){
690 outputSeeds = std::make_unique<TrajectorySeedCollection>();
691 outputSeeds->
reserve(nToWrite);
697 outputTrajs = std::make_unique<std::vector<Trajectory>>();
698 outputTrajs->reserve(rSize);
700 for (
unsigned int i=0; i<rSize; i++) {
701 if (selected[i]==0) {
706 unsigned int collNum=trackCollNum[
i];
707 unsigned int trackNum=i-trackCollFirsts[collNum];
710 mvaVec.push_back(trackMVAs[i]);
711 if (selected[i]>1 ) {
712 outputTrks->back().setQualityMask(selected[i]-10);
714 outputTrks->back().setQuality(qualityToSet_);
717 if ( selected[i]==1 )
718 outputTrks->back().setQualityMask(trackQuals[i]);
719 outputTrks->back().setOriginalAlgorithm(oriAlgo[i]);
720 outputTrks->back().setAlgoMask(algoMask[i]);
728 if (makeReKeyedSeeds_){
729 bool doRekeyOnThisSeed=
false;
733 if (origSeedRef->
nHits()!=0){
736 if (firstHit->isValid()){
745 doRekeyOnThisSeed=e.
getByLabel(clusterRemovalInfos,CRIh);
749 if (doRekeyOnThisSeed && !(clusterRemovalInfos==
edm::InputTag(
""))) {
755 for (;iH!=iH_end;++iH){
757 refSetter.
reKey(&newRecHitContainer.
back());
779 seedsRefs[
i]=origSeedRef;
780 outputTrks->back().setExtra(
reco::TrackExtraRef( refTrkExtras, outputTrkExtras->size() - 1) );
786 tx.
setHits(refTrkHits,outputTrkHits->size(),nh1);
790 outputTrkHits->push_back( (*hh)->clone() );
801 outputTTAss = std::make_unique<TrajTrackAssociationCollection>(refTrajs, refTrks);
803 for (
unsigned int ti=0; ti<trackColls.size(); ti++) {
806 e.
getByToken(trackProducers_[ti].traj, hTraj1);
807 e.
getByToken(trackProducers_[ti].tass, hTTAss1);
811 for (
size_t i = 0,
n = hTraj1->size(); i <
n; ++
i) {
814 if (match != hTTAss1->
end()) {
816 uint32_t oldKey = trackCollFirsts[ti]+
static_cast<uint32_t
>(trkRef.
key());
817 if (trackRefs[oldKey].isNonnull()) {
818 outputTrajs->push_back( *trajRef );
820 if (copyExtras_ && makeReKeyedSeeds_)
821 outputTrajs->back().setSeedRef( seedsRefs[oldKey] );
822 outputTTAss->insert (
edm::Ref< std::vector<Trajectory> >(refTrajs, outputTrajs->size() - 1),
829 statCount.end(outputTrks->size());
833 fillerMVA.insert(outHandle,mvaVec.begin(),mvaVec.end());
839 auto mvas = std::make_unique<MVACollection>(mvaVec.begin(),mvaVec.end());
845 if (makeReKeyedSeeds_)
PropagationDirection direction() const
const edm::RefToBase< TrajectorySeed > & seedRef() const
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
TrackListMerger(const edm::ParameterSet &conf)
std::vector< unsigned char > QualityMaskCollection
std::unique_ptr< TrajTrackAssociationCollection > outputTTAss
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
std::vector< bool > promoteQuality_
const TrackExtraRef & extra() const
reference to "extra" object
friend struct const_iterator
const_iterator end() const
last iterator over the map (read only)
reco::TrackRefProd refTrks
bool getByToken(EDGetToken token, Handle< PROD > &result) const
edm::EDGetTokenT< TrajTrackAssociationCollection > tass
edm::RefProd< std::vector< Trajectory > > refTrajs
TkEDGetTokenss edTokens(const edm::InputTag &tag, const edm::InputTag &mvatag)
virtual void produce(Event &, EventSetup const &)=0
TrackQuality
track quality
#define DEFINE_FWK_MODULE(type)
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
std::vector< ParameterSet > VParameterSet
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
void reKey(TrackingRecHit *hit) const
const_iterator find(const key_type &k) const
find element with specified reference key
reco::TrackBase::TrackQuality qualityToSet_
std::vector< Track > TrackCollection
collection of Tracks
bool exists(std::string const ¶meterName) const
checks if a parameter exists
bool innerOk() const
return true if the innermost hit is valid
unsigned short numberOfLostHits() const
number of cases where track crossed a layer without getting a hit.
std::unique_ptr< TrajectorySeedCollection > outputSeeds
key_type key() const
Accessor for product key.
std::string const & processName() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
const math::XYZPoint & outerPosition() const
position of the outermost hit
std::vector< float > MVACollection
edm::RefProd< TrajectorySeedCollection > refTrajSeeds
std::unique_ptr< std::vector< Trajectory > > outputTrajs
ProductID id() const
Accessor for product ID.
TrackAlgorithm
track algorithm
TrackingRecHitRefProd refTrkHits
virtual void produce(edm::Event &e, const edm::EventSetup &c) override
std::vector< double > indivShareFrac_
bool overlap(const reco::Muon &muon1, const reco::Muon &muon2, double pullX=1.0, double pullY=1.0, bool checkAdjacentChambers=false)
const math::XYZPoint & innerPosition() const
position of the innermost hit
TrackAlgorithm algo() const
std::unique_ptr< TrackingRecHitCollection > outputTrkHits
edm::EDGetTokenT< edm::ValueMap< int > > tsel
std::vector< TrajectorySeed > TrajectorySeedCollection
double chi2() const
chi-squared of the fit
recHitContainer::const_iterator const_iterator
CovarianceMatrix outerStateCovariance() const
outermost trajectory state curvilinear errors
edm::EDGetTokenT< reco::TrackCollection > tk
unsigned int outerDetId() const
DetId of the detector on which surface the outermost state is located.
Abs< T >::type abs(const T &t)
TkEDGetTokenss edTokens(const edm::InputTag &tag, const edm::InputTag &seltag, const edm::InputTag &mvatag)
unsigned short numberOfValidHits() const
number of valid hits found
trackingRecHit_iterator recHitsBegin() const
Iterator to first hit on the track.
virtual ~TrackListMerger()
virtual LocalPoint localPosition() const =0
int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum) ...
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
RefProd< PROD > getRefBeforePut()
std::unique_ptr< reco::TrackExtraCollection > outputTrkExtras
std::vector< int > hasSelector_
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
double maxNormalizedChisq_
PTrajectoryStateOnDet const & startingState() const
static TrackQuality qualityByName(const std::string &name)
const math::XYZVector & outerMomentum() const
momentum vector at the outermost hit position
bool outerOk() const
return true if the outermost hit is valid
const PropagationDirection & seedDirection() const
direction of how the hits were sorted in the original seed
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
edm::EDGetTokenT< edm::ValueMap< float > > tmva
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
CovarianceMatrix innerStateCovariance() const
innermost trajectory state curvilinear errors
std::string const & moduleLabel() const
unsigned int nHits() const
std::vector< TkEDGetTokenss > trackProducers_
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
const TrackResiduals & residuals() const
get the residuals
int numberOfValidPixelHits() const
std::unique_ptr< reco::TrackCollection > outputTrks
edm::EDGetTokenT< std::vector< Trajectory > > traj
std::array< unsigned int, reco::TrackBase::algoSize > trackAlgoPriorityOrder
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
TkEDGetTokenss(const edm::InputTag &tag_, edm::EDGetTokenT< reco::TrackCollection > &&tk_, edm::EDGetTokenT< std::vector< Trajectory > > &&traj_, edm::EDGetTokenT< TrajTrackAssociationCollection > &&tass_, edm::EDGetTokenT< edm::ValueMap< int > > &&tsel_, edm::EDGetTokenT< edm::ValueMap< float > > &&tmva_)
DetId geographicalId() const
std::string const & productInstanceName() const
std::string priorityName_
std::vector< std::vector< int > > listsToMerge_
Provenance getProvenance(BranchID const &theID) const
unsigned int innerDetId() const
DetId of the detector on which surface the innermost state is located.
reco::TrackExtraRefProd refTrkExtras
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
trackingRecHit_iterator recHitsEnd() const
Iterator to last hit on the track.