Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Nans in primary vertex #5490

Merged
merged 7 commits into from Sep 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 40 additions & 50 deletions RecoVertex/AdaptiveVertexFit/src/AdaptiveVertexFitter.cc
Expand Up @@ -18,12 +18,16 @@ using namespace edm;
using namespace std;

namespace {
struct CompareTwoTracks {
int operator() ( const reco::TransientTrack & a, const reco::TransientTrack & b ) {
return ( a.impactPointState().globalMomentum().perp() >
b.impactPointState().globalMomentum().perp() ) ;
};
};
void sortTracksByPt(std::vector<reco::TransientTrack> & cont) {
auto s = cont.size();
float pt2[s]; int ind[s]; int i=0;
for (auto const & tk : cont) { ind[i]=i; pt2[++i] = tk.impactPointState().globalMomentum().perp2();}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should have been i++, or, more straightforward and less error prone, pt2[i] = ...; ++i;
Valgrind reports errors here.
I suspect this is where our vertex/btag discrepancies originate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, is this in 72?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidlt @ktf
Did this get picked up by ASAN (I'm guessing we are running it) or by valgrind (which I guess doesn't run regularly) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is only in 73X

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slava77 currently I don't run ASAN builds. I discussed with @ktf recently of doing so in some future. Yet, I might do it manually in some days if you want.

std::sort(ind,ind+s, [&](int i, int j){return pt2[i]>pt2[j];} );
std::vector<reco::TransientTrack> tmp; tmp.reserve(s);
for (auto i=0U; i<s; ++i) tmp.emplace_back(std::move( cont[ind[i]] ) );
cont.swap(tmp);
}

// typedef ReferenceCountingPointer<VertexTrack<5> > RefCountedVertexTrack;
typedef AdaptiveVertexFitter::RefCountedVertexTrack RefCountedVertexTrack;

Expand All @@ -37,11 +41,8 @@ namespace {
ret(2,2)=initialError;
return ret;
}
GlobalError fitError()
{
static const GlobalError err( initFitError() );
return err;
}

GlobalError const fitError = initFitError();

AlgebraicSymMatrix33 initLinePointError() {
// that's how we model the error of the linearization point.
Expand All @@ -51,28 +52,24 @@ namespace {
// ret(0,0)=1e-7; ret(1,1)=1e-7; ret(2,2)=1e-7;
return ret;
}
GlobalError linPointError()
{
static const GlobalError err( initLinePointError() );
return err;
}

struct DistanceToRefPoint {
DistanceToRefPoint ( const GlobalPoint & ref ) : theRef ( ref ) {}
GlobalError const linPointError = initLinePointError();

bool operator() ( const RefCountedVertexTrack & v1, const RefCountedVertexTrack & v2 )
{
return ( distance ( v1 ) < distance ( v2 ) );
}

float distance ( const RefCountedVertexTrack & v1 )
{
return ( v1->linearizedTrack()->track().initialFreeState().position() - theRef ).mag2();
}

private:
GlobalPoint theRef;
};

void
sortByDistanceToRefPoint (std::vector<RefCountedVertexTrack> & cont, const GlobalPoint ref ) {
auto s = cont.size();
float d2[s]; int ind[s]; int i=0;
for (auto const & tk : cont) { ind[i]=i; d2[++i] = (tk->linearizedTrack()->track().initialFreeState().position() - ref ).mag2();}
std::sort(ind,ind+s, [&](int i, int j){return d2[i]<d2[j];} );
std::vector<RefCountedVertexTrack> tmp; tmp.reserve(s);
for (auto i=0U; i<s; ++i) tmp.emplace_back(std::move( cont[ind[i]] ) );
cont.swap(tmp);
}



#ifdef STORE_WEIGHTS
//NOTE: This is not thread safe
Expand Down Expand Up @@ -163,14 +160,14 @@ AdaptiveVertexFitter::vertex(const vector<reco::TransientTrack> & unstracks) con
return CachingVertex<5>(); // return invalid vertex
};
vector < reco::TransientTrack > tracks = unstracks;
sort ( tracks.begin(), tracks.end(), CompareTwoTracks() );
sortTracksByPt( tracks);
// Linearization Point
GlobalPoint linP = theLinP->getLinearizationPoint(tracks);
// Initial vertex seed, with a very large error matrix
VertexState lseed (linP, linPointError() );
VertexState lseed (linP, linPointError );
vector<RefCountedVertexTrack> vtContainer = linearizeTracks(tracks, lseed);

VertexState seed (linP, fitError() );
VertexState seed (linP, fitError );
return fit(vtContainer, seed, false);
}

Expand All @@ -185,7 +182,7 @@ AdaptiveVertexFitter::vertex(const vector<RefCountedVertexTrack> & tracks) const
};
// Initial vertex seed, with a very small weight matrix
GlobalPoint linP = tracks[0]->linearizedTrack()->linearizationPoint();
VertexState seed (linP, fitError() );
VertexState seed (linP, fitError );
return fit(tracks, seed, false);
}

Expand Down Expand Up @@ -218,9 +215,9 @@ AdaptiveVertexFitter::vertex(const vector<reco::TransientTrack> & tracks,
return CachingVertex<5>(); // return invalid vertex
};
// Initial vertex seed, with a very large error matrix
VertexState seed (linPoint, linPointError() );
VertexState seed (linPoint, linPointError );
vector<RefCountedVertexTrack> vtContainer = linearizeTracks(tracks, seed);
VertexState fitseed (linPoint, fitError() );
VertexState fitseed (linPoint, fitError );
return fit(vtContainer, fitseed, false);
}

Expand All @@ -244,12 +241,12 @@ AdaptiveVertexFitter::vertex(const vector<reco::TransientTrack> & unstracks,
vector<RefCountedVertexTrack> vtContainer;

vector < reco::TransientTrack > tracks = unstracks;
sort ( tracks.begin(), tracks.end(), CompareTwoTracks() );
sortTracksByPt(tracks);

if (tracks.size() > 1) {
// Linearization Point search if there are more than 1 track
GlobalPoint linP = theLinP->getLinearizationPoint(tracks);
VertexState lpState(linP, linPointError() );
VertexState lpState(linP, linPointError );
vtContainer = linearizeTracks(tracks, lpState);
} else {
// otherwise take the beamspot position.
Expand Down Expand Up @@ -437,8 +434,7 @@ AdaptiveVertexFitter::reWeightTracks(

finalTracks.push_back(vTrData);
}
sort ( finalTracks.begin(), finalTracks.end(),
DistanceToRefPoint ( vertex.position() ) );
sortByDistanceToRefPoint( finalTracks, vertex.position() );
// cout << "[AdaptiveVertexFitter] /now reweight" << endl;
return finalTracks;
}
Expand Down Expand Up @@ -534,12 +530,9 @@ AdaptiveVertexFitter::fit( const vector<RefCountedVertexTrack> & tracks,
priorVertexPosition,priorVertexError,initialTracks,0);
}

// vector<RefCountedVertexTrack> globalVTracks = tracks;
std::vector<RefCountedVertexTrack> globalVTracks = tracks;
// sort the tracks, according to distance to seed!
vector<RefCountedVertexTrack> globalVTracks ( tracks.size() );

partial_sort_copy ( tracks.begin(), tracks.end(),
globalVTracks.begin(), globalVTracks.end(), DistanceToRefPoint ( priorSeed.position() ) );
sortByDistanceToRefPoint (globalVTracks, priorSeed.position() );

// main loop through all the VTracks
int lpStep = 0; int step = 0;
Expand Down Expand Up @@ -586,11 +579,8 @@ AdaptiveVertexFitter::fit( const vector<RefCountedVertexTrack> & tracks,
if ((**i).weight() > 0.) nVertex = theUpdator->add( fVertex, *i );
else nVertex = fVertex;
if (nVertex.isValid()) {
if ( (**i).weight() >= theWeightThreshold )
{
ns_trks++;
};

if ( (**i).weight() >= theWeightThreshold ) ns_trks++;

if ( fabs ( nVertex.position().z() ) > 10000. ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably it's a silly point,
but could we get rid of such hard-coded numbers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody volunteers (the code was there already)

nVertex.position().perp()>120.)
{
Expand All @@ -612,7 +602,7 @@ AdaptiveVertexFitter::fit( const vector<RefCountedVertexTrack> & tracks,
<< "The updator returned an invalid vertex when adding track "
<< i-globalVTracks.begin()
<< ".\n Your vertex might just have lost one good track.";
};
}
}
previousPosition = newPosition;
newPosition = fVertex.position();
Expand Down
Expand Up @@ -155,11 +155,11 @@ PrimaryVertexProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup


// select tracks
std::vector<reco::TransientTrack> seltks = theTrackFilter->select( t_tks );
std::vector<reco::TransientTrack> && seltks = theTrackFilter->select( t_tks );


// clusterize tracks in Z
std::vector< std::vector<reco::TransientTrack> > clusters = theTrackClusterizer->clusterize(seltks);
std::vector< std::vector<reco::TransientTrack> > && clusters = theTrackClusterizer->clusterize(seltks);
if (fVerbose){std::cout << " clustering returned "<< clusters.size() << " clusters from " << seltks.size() << " selected tracks" <<std::endl;}


Expand All @@ -168,7 +168,7 @@ PrimaryVertexProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup


std::auto_ptr<reco::VertexCollection> result(new reco::VertexCollection);
reco::VertexCollection vColl;
reco::VertexCollection & vColl = (*result);


std::vector<TransientVertex> pvs;
Expand Down Expand Up @@ -204,8 +204,11 @@ PrimaryVertexProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup
}


if (clusters.size()>2 && clusters.size() > 2*pvs.size())
edm::LogWarning("PrimaryVertexProducer") << "more than half of candidate vertices lost " << pvs.size() << ' ' << clusters.size();


if (pvs.empty() && seltks.size()>5)
edm::LogWarning("PrimaryVertexProducer") << "no vertex found with " << seltks.size() << " tracks and " << clusters.size() <<" vertex-candidates";

// sort vertices by pt**2 vertex (aka signal vertex tagging)
if(pvs.size()>1){
Expand Down Expand Up @@ -262,8 +265,6 @@ PrimaryVertexProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup
}
}


*result = vColl;
iEvent.put(result, algorithm->label);
}

Expand Down
Expand Up @@ -17,7 +17,7 @@
),

TkClusParameters = cms.PSet(
algorithm = cms.string("DA"),
algorithm = cms.string("DA_vect"),
TkDAClusParameters = cms.PSet(
coolingFactor = cms.double(0.6), # moderate annealing speed
Tmin = cms.double(4.), # end of annealing
Expand Down Expand Up @@ -47,7 +47,3 @@

)

# These lines should be uncommented only for the gcc4X builds, with X>=6
offlinePrimaryVertices.TkClusParameters.algorithm = cms.string("DA_vect" )
offlinePrimaryVertices.TkClusParameters.TkDAClusParameters.use_vdt = cms.untracked.bool( True )