CMS 3D CMS Logo

Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes

FFTJetPFPileupCleaner Class Reference

#include <RecoJets/FFTJetProducers/plugins/FFTJetPFPileupCleaner.cc>

Inheritance diagram for FFTJetPFPileupCleaner:
edm::EDProducer edm::ProducerBase edm::ProductRegistryHelper

List of all members.

Public Member Functions

 FFTJetPFPileupCleaner (const edm::ParameterSet &)
 ~FFTJetPFPileupCleaner ()

Protected Member Functions

void beginJob ()
void endJob ()
void produce (edm::Event &, const edm::EventSetup &)

Private Member Functions

void buildRemovalMask ()
 FFTJetPFPileupCleaner (const FFTJetPFPileupCleaner &)
 FFTJetPFPileupCleaner ()
reco::VertexRef findSomeVertexWFakes (const edm::Handle< reco::VertexCollection > &vertices, const edm::Handle< reco::VertexCollection > &fakeVertices, const reco::PFCandidate &pfcand, bool *fromFakeSet) const
bool isAcceptableVtx (reco::VertexCollection::const_iterator iv) const
bool isRemovable (reco::PFCandidate::ParticleType ptype) const
FFTJetPFPileupCleaneroperator= (const FFTJetPFPileupCleaner &)
void setRemovalBit (reco::PFCandidate::ParticleType ptype, bool onOff)

Private Attributes

bool checkClosestZVertex
double etaMax
double etaMin
edm::InputTag FakePrimaryVertices
bool keepIfPVneighbor
edm::InputTag PFCandidates
unsigned removalMask
bool remove_e
bool remove_egamma_HF
bool remove_gamma
bool remove_h
bool remove_h0
bool remove_h_HF
bool remove_mu
bool remove_X
bool removeMainVertex
bool removeUnassociated
bool reverseRemovalDecision
bool useFakePrimaryVertex
double vertexNdofCut
double vertexZmaxCut
edm::InputTag Vertices
std::vector< std::pair< double,
unsigned > > 
zAssoc

Detailed Description

Description: cleans up a collection of partice flow objects

Implementation: [Notes on implementation]

Definition at line 46 of file FFTJetPFPileupCleaner.cc.


Constructor & Destructor Documentation

FFTJetPFPileupCleaner::FFTJetPFPileupCleaner ( const edm::ParameterSet ps) [explicit]
FFTJetPFPileupCleaner::~FFTJetPFPileupCleaner ( )

Definition at line 163 of file FFTJetPFPileupCleaner.cc.

{
}
FFTJetPFPileupCleaner::FFTJetPFPileupCleaner ( ) [private]
FFTJetPFPileupCleaner::FFTJetPFPileupCleaner ( const FFTJetPFPileupCleaner ) [private]

Member Function Documentation

void FFTJetPFPileupCleaner::beginJob ( void  ) [protected, virtual]

Reimplemented from edm::EDProducer.

Definition at line 501 of file FFTJetPFPileupCleaner.cc.

{
}
void FFTJetPFPileupCleaner::buildRemovalMask ( ) [private]
void FFTJetPFPileupCleaner::endJob ( void  ) [protected, virtual]

Reimplemented from edm::EDProducer.

Definition at line 507 of file FFTJetPFPileupCleaner.cc.

{
}
reco::VertexRef FFTJetPFPileupCleaner::findSomeVertexWFakes ( const edm::Handle< reco::VertexCollection > &  vertices,
const edm::Handle< reco::VertexCollection > &  fakeVertices,
const reco::PFCandidate pfcand,
bool *  fromFakeSet 
) const [private]

Definition at line 282 of file FFTJetPFPileupCleaner.cc.

References abs, checkClosestZVertex, first, getHLTprescales::index, isAcceptableVtx(), keepIfPVneighbor, python::multivaluedict::sort(), reco::PFCandidate::trackRef(), reco::Vertex::tracks_begin(), reco::Vertex::tracks_end(), reco::Vertex::trackWeight(), useFakePrimaryVertex, reco::PFCandidate::vertex(), w(), z, and zAssoc.

Referenced by produce().

{
    typedef reco::VertexCollection::const_iterator IV;
    typedef reco::Vertex::trackRef_iterator IT;

    *fromFakeSet = false;
    reco::TrackBaseRef trackBaseRef(pfcand.trackRef());

    size_t iVertex = 0;
    unsigned nFoundVertex = 0;
    const IV vertend(vertices->end());

    {
        unsigned index = 0;
        double bestweight = 0.0;
        for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
            if (isAcceptableVtx(iv))
            {
                const reco::Vertex& vtx = *iv;

                // loop on tracks in vertices
                IT trackend(vtx.tracks_end());
                for (IT iTrack=vtx.tracks_begin(); iTrack!=trackend; ++iTrack)
                {
                    const reco::TrackBaseRef& baseRef = *iTrack;

                    // one of the tracks in the vertex is the same as 
                    // the track considered in the function
                    if (baseRef == trackBaseRef)
                    {
                        // select the vertex for which the track has the highest weight
                        const double w = vtx.trackWeight(baseRef);
                        if (w > bestweight)
                        {
                            bestweight=w;
                            iVertex=index;
                            nFoundVertex++;
                        }       
                    }
                }
            }
    }

    if (nFoundVertex > 0)
    {
        if (nFoundVertex != 1)
            edm::LogWarning("TrackOnTwoVertex")
                << "a track is shared by at least two vertices. "
                << "Used to be an assert";

        // Check if we can re-associate this track with one
        // of the fake vertices
        if (useFakePrimaryVertex)
        {
            const double ztrack = pfcand.vertex().z();
            double dzmin = std::abs(ztrack - ((*vertices)[iVertex]).z());

            const IV fakeEnd(fakeVertices->end());
            unsigned index = 0;
            for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
                if (isAcceptableVtx(iv))
                {
                    const double dz = std::abs(ztrack - iv->z());
                    if (dz < dzmin)
                    {
                        dzmin = dz; 
                        iVertex = index;
                        *fromFakeSet = true;
                    }
                }
        }

        return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
    }

    // optional: as a secondary solution, associate the closest vertex in z
    if (checkClosestZVertex) 
    {
        const double ztrack = pfcand.vertex().z();
        bool foundVertex = false;

        if (keepIfPVneighbor)
        {
            // Sort all vertices according to their Z coordinate
            zAssoc.clear();
            unsigned index = 0;
            for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
                if (isAcceptableVtx(iv))
                    zAssoc.push_back(std::pair<double,unsigned>(iv->z(), index));
            const unsigned numRealVertices = index;

            // Mix the fake vertex collection into zAssoc.
            // Note that we do not reset "index" before doing this.
            if (useFakePrimaryVertex)
            {
                const IV fakeEnd(fakeVertices->end());
                for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
                    if (isAcceptableVtx(iv))
                        zAssoc.push_back(std::pair<double,unsigned>(iv->z(), index));
            }

            // Check where the track z position fits into this sequence
            if (!zAssoc.empty())
            {
                std::sort(zAssoc.begin(), zAssoc.end());
                std::pair<double,unsigned> tPair(ztrack, UINT_MAX);
                const unsigned iAbove = std::upper_bound(
                    zAssoc.begin(), zAssoc.end(), tPair) - zAssoc.begin();

                // Check whether one of the vertices with indices
                // iAbove or (iAbove - 1) is a primary vertex.
                // If so, return it. Otherwise return the one
                // with closest distance to the track.
                unsigned ich[2] = {0U, 0U};
                unsigned nch = 1;
                if (iAbove)
                {
                    ich[0] = iAbove - 1U;
                    ich[1] = iAbove;
                    if (iAbove < zAssoc.size())
                        nch = 2;
                }

                double dzmin = 1.0e100;
                unsigned bestVertexNum = UINT_MAX;
                for (unsigned icheck=0; icheck<nch; ++icheck)
                {
                    const unsigned zAssocIndex = ich[icheck];
                    const unsigned vertexNum = zAssoc[zAssocIndex].second;

                    if (vertexNum == numRealVertices || 
                        (!useFakePrimaryVertex && vertexNum == 0U))
                    {
                        bestVertexNum = vertexNum;
                        break;
                    }

                    const double dz = std::abs(ztrack - zAssoc[zAssocIndex].first);
                    if (dz < dzmin)
                    {
                        dzmin = dz; 
                        bestVertexNum = vertexNum;
                    }
                }

                foundVertex = bestVertexNum < UINT_MAX;
                if (foundVertex)
                {
                    iVertex = bestVertexNum;
                    if (iVertex >= numRealVertices)
                    {
                        *fromFakeSet = true;
                        iVertex -= numRealVertices;
                    }
                }
            }
        }
        else
        {
            // This is a simple association algorithm (from PFPileUp)
            // extended to take fake vertices into account
            double dzmin = 1.0e100;
            unsigned index = 0;
            for (IV iv=vertices->begin(); iv!=vertend; ++iv, ++index)
                if (isAcceptableVtx(iv))
                {
                    const double dz = std::abs(ztrack - iv->z());
                    if (dz < dzmin)
                    {
                        dzmin = dz; 
                        iVertex = index;
                        foundVertex = true;
                    }
                }

            if (useFakePrimaryVertex)
            {
                const IV fakeEnd(fakeVertices->end());
                index = 0;
                for (IV iv=fakeVertices->begin(); iv!=fakeEnd; ++iv, ++index)
                    if (isAcceptableVtx(iv))
                    {
                        const double dz = std::abs(ztrack - iv->z());
                        if (dz < dzmin)
                        {
                            dzmin = dz; 
                            iVertex = index;
                            *fromFakeSet = true;
                            foundVertex = true;
                       }
                    }
            }
        }

        if (foundVertex) 
            return reco::VertexRef(*fromFakeSet ? fakeVertices : vertices, iVertex);
    }

    return reco::VertexRef();
}
bool FFTJetPFPileupCleaner::isAcceptableVtx ( reco::VertexCollection::const_iterator  iv) const [private]

Definition at line 168 of file FFTJetPFPileupCleaner.cc.

References abs, vertexNdofCut, and vertexZmaxCut.

Referenced by findSomeVertexWFakes().

{
    return !iv->isFake() &&
            static_cast<double>(iv->ndof()) > vertexNdofCut &&
            std::abs(iv->z()) < vertexZmaxCut;
}
bool FFTJetPFPileupCleaner::isRemovable ( reco::PFCandidate::ParticleType  ptype) const [private]

Definition at line 257 of file FFTJetPFPileupCleaner.cc.

References removalMask, and edm::shift.

Referenced by produce().

{
    const unsigned shift = static_cast<unsigned>(ptype);
    assert(shift < 32U);
    return removalMask & (1U << shift);
}
FFTJetPFPileupCleaner& FFTJetPFPileupCleaner::operator= ( const FFTJetPFPileupCleaner ) [private]
void FFTJetPFPileupCleaner::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
) [protected, virtual]

Implements edm::EDProducer.

Definition at line 178 of file FFTJetPFPileupCleaner.cc.

References eta, etaMax, FakePrimaryVertices, findSomeVertexWFakes(), edm::Event::getByLabel(), i, edm::Ref< C, T, F >::isNull(), isRemovable(), edm::HandleBase::isValid(), edm::Ref< C, T, F >::key(), PFCandidates, reco::tau::pfCandidates(), edm::Event::put(), removeMainVertex, removeUnassociated, reverseRemovalDecision, reco::PFCandidate::setSourceCandidatePtr(), useFakePrimaryVertex, and Vertices.

{
    // get PFCandidates
    std::auto_ptr<reco::PFCandidateCollection> 
        pOutput(new reco::PFCandidateCollection);

    edm::Handle<reco::PFCandidateCollection> pfCandidates;
    iEvent.getByLabel(PFCandidates, pfCandidates);

    // get vertices
    edm::Handle<reco::VertexCollection> vertices;
    iEvent.getByLabel(Vertices, vertices);

    edm::Handle<reco::VertexCollection> fakeVertices;
    if (useFakePrimaryVertex)
    {
        iEvent.getByLabel(FakePrimaryVertices, fakeVertices);
        if (!fakeVertices.isValid())
            throw cms::Exception("FFTJetBadConfig")
                << "ERROR in FFTJetPFPileupCleaner:"
                " could not find fake vertices"
                << std::endl;
    }

    const unsigned ncand = pfCandidates->size();
    for (unsigned i=0; i<ncand; ++i)
    {
        reco::PFCandidatePtr candptr(pfCandidates, i);
        bool remove = false;

        if (isRemovable(candptr->particleId()))
        {
            bool fromFakeSet = false;
            reco::VertexRef vertexref(findSomeVertexWFakes(vertices, fakeVertices,
                                                           *candptr, &fromFakeSet));
            if (vertexref.isNull())
            {
                // Track is not associated with any vertex 
                // in any of the vertex sets
                remove = removeUnassociated;
            }
            else if (vertexref.key() == 0 && 
                     (!useFakePrimaryVertex || fromFakeSet))
            {
                // Track is associated with the main primary vertex
                // However, if we are using fake vertices, this only
                // matters if the vertex comes from the fake set. If
                // it comes from the real set, remove the track anyway
                // because in the combined set the associated vertex
                // would not be the main primary vertex.
                remove = removeMainVertex;
            }
            else
                remove = true;
        }

        // Check the eta range
        if (!remove)
        {
            const double eta = candptr->p4().Eta();
            remove = eta < etaMin || eta > etaMax;
        }

        // Should we remember this candidate?
        if (reverseRemovalDecision)
            remove = !remove;
        if (!remove)
        {
            const reco::PFCandidate& cand = (*pfCandidates)[i];
            pOutput->push_back(cand);
            pOutput->back().setSourceCandidatePtr(candptr);
        }
    }

    iEvent.put(pOutput);
}
void FFTJetPFPileupCleaner::setRemovalBit ( reco::PFCandidate::ParticleType  ptype,
bool  onOff 
) [private]

Definition at line 266 of file FFTJetPFPileupCleaner.cc.

References removalMask, and edm::shift.

Referenced by buildRemovalMask().

{
    const unsigned shift = static_cast<unsigned>(ptype);
    assert(shift < 32U);
    const unsigned mask = (1U << shift);
    if (value)
        removalMask |= mask;
    else
        removalMask &= ~mask;
}

Member Data Documentation

Definition at line 83 of file FFTJetPFPileupCleaner.cc.

Referenced by findSomeVertexWFakes().

Definition at line 119 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 118 of file FFTJetPFPileupCleaner.cc.

Definition at line 75 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 90 of file FFTJetPFPileupCleaner.cc.

Referenced by findSomeVertexWFakes().

Definition at line 73 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 115 of file FFTJetPFPileupCleaner.cc.

Referenced by isRemovable(), and setRemovalBit().

Definition at line 107 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 112 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 109 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 106 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 110 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 111 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 108 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 105 of file FFTJetPFPileupCleaner.cc.

Referenced by buildRemovalMask().

Definition at line 94 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 98 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 101 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

Definition at line 79 of file FFTJetPFPileupCleaner.cc.

Referenced by findSomeVertexWFakes(), and produce().

Definition at line 122 of file FFTJetPFPileupCleaner.cc.

Referenced by isAcceptableVtx().

Definition at line 125 of file FFTJetPFPileupCleaner.cc.

Referenced by isAcceptableVtx().

Definition at line 74 of file FFTJetPFPileupCleaner.cc.

Referenced by produce().

std::vector<std::pair<double, unsigned> > FFTJetPFPileupCleaner::zAssoc [mutable, private]

Definition at line 128 of file FFTJetPFPileupCleaner.cc.

Referenced by findSomeVertexWFakes().