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

Convert TrackFromPVSelector from legacy producer to global producer #17472

Merged
merged 5 commits into from Feb 14, 2017
Merged
Changes from 4 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
92 changes: 35 additions & 57 deletions Validation/RecoTau/plugins/TrackFromPVSelector.cc
@@ -1,100 +1,78 @@
////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"

#include "CommonTools/Utils/interface/StringCutObjectSelector.h"

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackFwd.h"

#include "DataFormats/VertexReco/interface/Vertex.h"
#include "DataFormats/VertexReco/interface/VertexFwd.h"

#include <algorithm>
#include <memory>
#include <vector>
#include <sstream>


////////////////////////////////////////////////////////////////////////////////
// class definition
////////////////////////////////////////////////////////////////////////////////
class TrackFromPVSelector : public edm::EDProducer
{
class TrackFromPVSelector : public edm::global::EDProducer<> {
public:
// construction/destruction
TrackFromPVSelector(const edm::ParameterSet& iConfig);
virtual ~TrackFromPVSelector();

// member functions
void produce(edm::Event& iEvent,const edm::EventSetup& iSetup) override;

private:
// member data
double max_dxy_ ;
double max_dz_ ;
edm::EDGetTokenT< std::vector<reco::Vertex> > v_recoVertexToken_ ;
edm::EDGetTokenT< std::vector<reco::Track> > v_recoTrackToken_ ;
};

explicit TrackFromPVSelector(edm::ParameterSet const& iConfig);

void produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const& iSetup) const override;

private:

double max_dxy_;
double max_dz_;
edm::EDGetTokenT<std::vector<reco::Vertex>> v_recoVertexToken_;
edm::EDGetTokenT<std::vector<reco::Track>> v_recoTrackToken_;
};


////////////////////////////////////////////////////////////////////////////////
// construction/destruction
////////////////////////////////////////////////////////////////////////////////

//______________________________________________________________________________
TrackFromPVSelector::TrackFromPVSelector(const edm::ParameterSet& iConfig)
: max_dxy_ ( iConfig.getParameter<double>( "max_dxy" ) )
, max_dz_ ( iConfig.getParameter<double>( "max_dz" ) )
, v_recoVertexToken_( consumes< std::vector<reco::Vertex> >( iConfig.getParameter<edm::InputTag>( "srcVertex" ) ) )
, v_recoTrackToken_ ( consumes< std::vector<reco::Track> >( iConfig.getParameter<edm::InputTag>( "srcTrack" ) ) )
TrackFromPVSelector::TrackFromPVSelector(edm::ParameterSet const& iConfig)
: max_dxy_{iConfig.getParameter<double>("max_dxy")}
, max_dz_{iConfig.getParameter<double>("max_dz")}
, v_recoVertexToken_{consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("srcVertex"))}
, v_recoTrackToken_{consumes<std::vector<reco::Track>>(iConfig.getParameter<edm::InputTag>("srcTrack"))}
{
produces<std::vector<reco::Track> >();
produces<std::vector<reco::Track>>();
}


//______________________________________________________________________________
TrackFromPVSelector::~TrackFromPVSelector(){}

////////////////////////////////////////////////////////////////////////////////
// implementation of member functions
////////////////////////////////////////////////////////////////////////////////

//______________________________________________________________________________
void TrackFromPVSelector::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
{
std::unique_ptr<std::vector<reco::Track> > goodTracks(new std::vector<reco::Track >);

edm::Handle< std::vector<reco::Vertex> > VertexHandle;
iEvent.getByToken( v_recoVertexToken_, VertexHandle );

edm::Handle< std::vector<reco::Track> > TrackHandle;
iEvent.getByToken( v_recoTrackToken_, TrackHandle );

if( (VertexHandle->size() == 0) || (TrackHandle->size() == 0) )
{
iEvent.put(std::move(goodTracks));
return ;
}

reco::Vertex PV = VertexHandle->front();
//typename std::vector<reco::Track>::const_iterator TrackIt ;
std::vector<reco::Track>::const_iterator TrackIt ;

for (TrackIt = TrackHandle->begin(); TrackIt != TrackHandle->end(); ++TrackIt) {
if ( fabs(TrackIt->dxy(PV.position())) < max_dxy_ &&
fabs(TrackIt->dz(PV.position())) < max_dz_ ){
goodTracks -> push_back(*TrackIt) ;
void TrackFromPVSelector::produce(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const
{
auto goodTracks = std::make_unique<std::vector<reco::Track>>();

edm::Handle<std::vector<reco::Vertex>> vertices;
edm::Handle<std::vector<reco::Track>> tracks;
if (iEvent.getByToken(v_recoVertexToken_, vertices) && !vertices->empty() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the same logic. Previously if the data product was missing we'd throw an exception at the VertexHandle-> or TrackHandle-> call. Now you will put in an empty container. Please remove the check on return value of getByToken.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack. Good catch. Yes I'll make the change.

iEvent.getByToken(v_recoTrackToken_, tracks) && !tracks->empty())
{
auto const& vtxPos = vertices->front().position();
std::copy_if(std::cbegin(*tracks), std::cend(*tracks), std::back_inserter(*goodTracks),
[this, &vtxPos](auto const& track) {
return fabs(track.dxy(vtxPos)) < max_dxy_ && fabs(track.dz(vtxPos)) < max_dz_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you switch to using std::abs ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep.

});
}
}

iEvent.put(std::move(goodTracks));

}

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(TrackFromPVSelector);