Skip to content

Commit

Permalink
Merge pull request #35708 from guitargeek/ptr_vector_1
Browse files Browse the repository at this point in the history
Replace `boost::ptr_vector` and `boost::ptr_list` with STL collections in RecoTauTag/RecoTau
  • Loading branch information
cmsbuild authored Oct 23, 2021
2 parents 0a16b55 + 184f442 commit ea43e07
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 166 deletions.
11 changes: 4 additions & 7 deletions RecoTauTag/RecoTau/interface/PFRecoTauChargedHadronPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
*
*/

#include <vector>
#include <boost/ptr_container/ptr_vector.hpp>
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "RecoTauTag/RecoTau/interface/RecoTauPluginsCommon.h"

#include "FWCore/Framework/interface/ConsumesCollector.h"
#include <vector>

namespace reco {

Expand All @@ -35,10 +34,8 @@ namespace reco {
class PFRecoTauChargedHadronBuilderPlugin : public RecoTauEventHolderPlugin {
public:
// Return a vector of pointers
typedef boost::ptr_vector<PFRecoTauChargedHadron> ChargedHadronVector;
// Storing the result in an auto ptr on function return allows
// allows us to safely release the ptr_vector in the virtual function
typedef std::unique_ptr<ChargedHadronVector> return_type;
typedef std::vector<std::unique_ptr<PFRecoTauChargedHadron>> ChargedHadronVector;
typedef ChargedHadronVector return_type;
explicit PFRecoTauChargedHadronBuilderPlugin(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
: RecoTauEventHolderPlugin(pset) {}
~PFRecoTauChargedHadronBuilderPlugin() override {}
Expand Down
14 changes: 6 additions & 8 deletions RecoTauTag/RecoTau/interface/RecoTauBuilderPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
*
*/

#include <boost/ptr_container/ptr_vector.hpp>

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/EDProducer.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
Expand All @@ -55,15 +53,15 @@ namespace reco {
/* Class that constructs PFTau(s) from a Jet and its associated PiZeros */
class RecoTauBuilderPlugin : public RecoTauEventHolderPlugin {
public:
typedef boost::ptr_vector<reco::PFTau> output_type;
typedef std::unique_ptr<output_type> return_type;
typedef std::vector<std::unique_ptr<reco::PFTau>> output_type;
typedef output_type return_type;

explicit RecoTauBuilderPlugin(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
: RecoTauEventHolderPlugin(pset),
// The vertex association configuration is specified with the quality cuts.
vertexAssociator_(pset.getParameter<edm::ParameterSet>("qualityCuts"), std::move(iC)) {
pfCandSrc_ = pset.getParameter<edm::InputTag>("pfCandSrc");
pfCand_token = iC.consumes<edm::View<reco::Candidate> >(pfCandSrc_);
pfCand_token = iC.consumes<edm::View<reco::Candidate>>(pfCandSrc_);
};

~RecoTauBuilderPlugin() override {}
Expand All @@ -77,7 +75,7 @@ namespace reco {
const std::vector<CandidatePtr>&) const = 0;

/// Hack to be able to convert Ptrs to Refs
const edm::Handle<edm::View<reco::Candidate> >& getPFCands() const { return pfCands_; };
const edm::Handle<edm::View<reco::Candidate>>& getPFCands() const { return pfCands_; };

/// Get primary vertex associated to this jet
reco::VertexRef primaryVertex(const reco::JetBaseRef& jet) const {
Expand All @@ -95,9 +93,9 @@ namespace reco {
private:
edm::InputTag pfCandSrc_;
// Handle to PFCandidates needed to build Refs
edm::Handle<edm::View<reco::Candidate> > pfCands_;
edm::Handle<edm::View<reco::Candidate>> pfCands_;
reco::tau::RecoTauVertexAssociator vertexAssociator_;
edm::EDGetTokenT<edm::View<reco::Candidate> > pfCand_token;
edm::EDGetTokenT<edm::View<reco::Candidate>> pfCand_token;
};

/* Class that updates a PFTau's members (i.e. electron variables) */
Expand Down
19 changes: 5 additions & 14 deletions RecoTauTag/RecoTau/interface/RecoTauCleaningTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ namespace reco::tau {
explicit RecoTauLexicographicalRanking(const RankingList& rankers) : rankers_(rankers) {}
// Predicate to compare a and b
bool operator()(const Type& a, const Type& b) const {
typename RankingList::const_iterator ranker = rankers_.begin();
while (ranker != rankers_.end()) {
for (auto const& ranker : rankers_) {
double aResult = (*ranker)(a);
double bResult = (*ranker)(b);
if (aResult != bResult)
return (aResult < bResult);
++ranker;
}
// If all aare equal return false
return false;
Expand All @@ -31,29 +29,22 @@ namespace reco::tau {

template <typename Container, class OverlapFunction>
Container cleanOverlaps(const Container& dirty) {
typedef typename Container::const_iterator Iterator;
// Output container of clean objects
Container clean;
OverlapFunction overlapChecker;
for (Iterator candidate = dirty.begin(); candidate != dirty.end(); ++candidate) {
for (auto const& candidate : dirty) {
// Check if this overlaps with a pizero already in the clean list
bool overlaps = false;
for (Iterator cleaned = clean.begin(); cleaned != clean.end() && !overlaps; ++cleaned) {
overlaps = overlapChecker(*candidate, *cleaned);
for (auto cleaned = clean.begin(); cleaned != clean.end() && !overlaps; ++cleaned) {
overlaps = overlapChecker(candidate, *cleaned);
}
// If it didn't overlap with anything clean, add it to the clean list
if (!overlaps)
clean.insert(clean.end(), *candidate);
clean.insert(clean.end(), candidate);
}
return clean;
}

template <typename T>
class SortByDescendingPt {
public:
bool operator()(const T& a, const T& b) const { return a.pt() > b.pt(); }
};

} // namespace reco::tau

#endif
7 changes: 2 additions & 5 deletions RecoTauTag/RecoTau/interface/RecoTauPiZeroPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include <vector>
#include <boost/ptr_container/ptr_vector.hpp>
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
#include "RecoTauTag/RecoTau/interface/RecoTauPluginsCommon.h"

Expand All @@ -33,10 +32,8 @@ namespace reco {
class RecoTauPiZeroBuilderPlugin : public RecoTauEventHolderPlugin {
public:
// Return a vector of pointers
typedef boost::ptr_vector<RecoTauPiZero> PiZeroVector;
// Storing the result in an auto ptr on function return allows
// allows us to safely release the ptr_vector in the virtual function
typedef std::unique_ptr<PiZeroVector> return_type;
typedef std::vector<std::unique_ptr<RecoTauPiZero>> PiZeroVector;
typedef PiZeroVector return_type;
explicit RecoTauPiZeroBuilderPlugin(const edm::ParameterSet& pset, edm::ConsumesCollector&& iC)
: RecoTauEventHolderPlugin(pset) {}
~RecoTauPiZeroBuilderPlugin() override {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ namespace reco {
output.push_back(std::move(chargedHadron));
}

return output.release();
return output;
}

typedef PFRecoTauChargedHadronFromGenericTrackPlugin<reco::Track> PFRecoTauChargedHadronFromTrackPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ namespace reco {
algo = PFRecoTauChargedHadron::kChargedPFCandidate;
else
algo = PFRecoTauChargedHadron::kPFNeutralHadron;
std::unique_ptr<PFRecoTauChargedHadron> chargedHadron(new PFRecoTauChargedHadron(**cand, algo));
auto chargedHadron = std::make_unique<PFRecoTauChargedHadron>(**cand, algo);

const reco::PFCandidate* pfCand = dynamic_cast<const reco::PFCandidate*>(&**cand);
if (pfCand) {
Expand Down Expand Up @@ -281,7 +281,7 @@ namespace reco {
output.push_back(std::move(chargedHadron));
}

return output.release();
return output;
}

} // namespace tau
Expand Down
73 changes: 36 additions & 37 deletions RecoTauTag/RecoTau/plugins/PFRecoTauChargedHadronProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@

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

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/ptr_container/ptr_list.hpp>

#include <memory>

#include <algorithm>
#include <cmath>
#include <functional>
#include <list>
#include <memory>
#include <set>
#include <string>
#include <vector>
Expand All @@ -63,17 +59,16 @@ class PFRecoTauChargedHadronProducer : public edm::stream::EDProducer<> {
~PFRecoTauChargedHadronProducer() override {}
void produce(edm::Event& evt, const edm::EventSetup& es) override;
template <typename T>
void print(const T& chargedHadrons);
void print(const T& chargedHadron);

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
typedef boost::ptr_vector<Builder> builderList;
typedef boost::ptr_vector<Ranker> rankerList;
typedef boost::ptr_vector<reco::PFRecoTauChargedHadron> ChargedHadronVector;
typedef boost::ptr_list<reco::PFRecoTauChargedHadron> ChargedHadronList;
typedef std::vector<std::unique_ptr<Ranker>> RankerList;
typedef Builder::return_type ChargedHadronVector;
typedef std::list<std::unique_ptr<reco::PFRecoTauChargedHadron>> ChargedHadronList;

typedef reco::tau::RecoTauLexicographicalRanking<rankerList, reco::PFRecoTauChargedHadron> ChargedHadronPredicate;
typedef reco::tau::RecoTauLexicographicalRanking<RankerList, reco::PFRecoTauChargedHadron> ChargedHadronPredicate;

std::string moduleLabel_;

Expand All @@ -84,8 +79,8 @@ class PFRecoTauChargedHadronProducer : public edm::stream::EDProducer<> {
double maxJetAbsEta_;

// plugins for building and ranking ChargedHadron candidates
builderList builders_;
rankerList rankers_;
std::vector<std::unique_ptr<Builder>> builders_;
RankerList rankers_;

std::unique_ptr<ChargedHadronPredicate> predicate_;

Expand All @@ -110,7 +105,7 @@ PFRecoTauChargedHadronProducer::PFRecoTauChargedHadronProducer(const edm::Parame
std::string pluginType = pset->getParameter<std::string>("plugin");
edm::ParameterSet pset_modified = (*pset);
pset_modified.addParameter<int>("verbosity", verbosity_);
builders_.push_back(
builders_.emplace_back(
PFRecoTauChargedHadronBuilderPluginFactory::get()->create(pluginType, pset_modified, consumesCollector()));
}

Expand All @@ -120,7 +115,7 @@ PFRecoTauChargedHadronProducer::PFRecoTauChargedHadronProducer(const edm::Parame
std::string pluginType = pset->getParameter<std::string>("plugin");
edm::ParameterSet pset_modified = (*pset);
pset_modified.addParameter<int>("verbosity", verbosity_);
rankers_.push_back(PFRecoTauChargedHadronQualityPluginFactory::get()->create(pluginType, pset_modified));
rankers_.emplace_back(PFRecoTauChargedHadronQualityPluginFactory::get()->create(pluginType, pset_modified));
}

// build the sorting predicate
Expand All @@ -143,7 +138,7 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe

// give each of our plugins a chance at doing something with the edm::Event
for (auto& builder : builders_) {
builder.setup(evt, es);
builder->setup(evt, es);
}

// get a view of our jets via the base candidates
Expand Down Expand Up @@ -179,21 +174,23 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe
// merge candidates reconstructed by all desired algorithm plugins
for (auto const& builder : builders_) {
try {
ChargedHadronVector result(builder(*pfJet));
ChargedHadronVector result((*builder)(*pfJet));
if (verbosity_) {
edm::LogPrint("PFRecoTauChHProducer") << "result of builder = " << builder.name() << ":";
print(result);
edm::LogPrint("PFRecoTauChHProducer") << "result of builder = " << builder->name() << ":";
for (auto const& had : result) {
print(*had);
}
}
uncleanedChargedHadrons.transfer(uncleanedChargedHadrons.end(), result);
std::move(result.begin(), result.end(), std::back_inserter(uncleanedChargedHadrons));
} catch (cms::Exception& exception) {
edm::LogError("BuilderPluginException")
<< "Exception caught in builder plugin " << builder.name() << ", rethrowing" << std::endl;
<< "Exception caught in builder plugin " << builder->name() << ", rethrowing" << std::endl;
throw exception;
}
}

// rank the candidates according to our quality plugins
uncleanedChargedHadrons.sort(*predicate_);
uncleanedChargedHadrons.sort([this](const auto& a, const auto& b) { return (*predicate_)(*a, *b); });

// define collection of cleaned ChargedHadrons;
std::vector<reco::PFRecoTauChargedHadron> cleanedChargedHadrons;
Expand All @@ -205,7 +202,8 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe

while (!uncleanedChargedHadrons.empty()) {
// get next best ChargedHadron candidate
std::unique_ptr<reco::PFRecoTauChargedHadron> nextChargedHadron(uncleanedChargedHadrons.pop_front().release());
std::unique_ptr<reco::PFRecoTauChargedHadron> nextChargedHadron = std::move(uncleanedChargedHadrons.front());
uncleanedChargedHadrons.pop_front();
if (verbosity_) {
edm::LogPrint("PFRecoTauChHProducer") << "processing nextChargedHadron:";
edm::LogPrint("PFRecoTauChHProducer") << (*nextChargedHadron);
Expand Down Expand Up @@ -306,8 +304,10 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe
reco::tau::setChargedHadronP4(*nextChargedHadron);
// reinsert ChargedHadron candidate into list of uncleaned candidates,
// at position according to new rank
ChargedHadronList::iterator insertionPoint = std::lower_bound(
uncleanedChargedHadrons.begin(), uncleanedChargedHadrons.end(), *nextChargedHadron, *predicate_);
auto insertionPoint = std::lower_bound(uncleanedChargedHadrons.begin(),
uncleanedChargedHadrons.end(),
*nextChargedHadron,
[this](const auto& a, const auto& b) { return (*predicate_)(*a, b); });
if (verbosity_) {
edm::LogPrint("PFRecoTauChHProducer") << "--> removing non-unique neutral PFCandidates and reinserting "
"nextChargedHadron in uncleaned collection.";
Expand All @@ -317,7 +317,9 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe
}

if (verbosity_) {
print(cleanedChargedHadrons);
for (auto const& had : cleanedChargedHadrons) {
print(had);
}
}

// add ChargedHadron-to-jet association
Expand All @@ -328,17 +330,14 @@ void PFRecoTauChargedHadronProducer::produce(edm::Event& evt, const edm::EventSe
}

template <typename T>
void PFRecoTauChargedHadronProducer::print(const T& chargedHadrons) {
for (typename T::const_iterator chargedHadron = chargedHadrons.begin(); chargedHadron != chargedHadrons.end();
++chargedHadron) {
edm::LogPrint("PFRecoTauChHProducer") << (*chargedHadron);
edm::LogPrint("PFRecoTauChHProducer") << "Rankers:";
for (rankerList::const_iterator ranker = rankers_.begin(); ranker != rankers_.end(); ++ranker) {
const unsigned width = 25;
edm::LogPrint("PFRecoTauChHProducer")
<< " " << std::setiosflags(std::ios::left) << std::setw(width) << ranker->name() << " "
<< std::resetiosflags(std::ios::left) << std::setprecision(3) << (*ranker)(*chargedHadron) << std::endl;
}
void PFRecoTauChargedHadronProducer::print(const T& chargedHadron) {
edm::LogPrint("PFRecoTauChHProducer") << chargedHadron;
edm::LogPrint("PFRecoTauChHProducer") << "Rankers:";
for (auto const& ranker : rankers_) {
constexpr unsigned width = 25;
edm::LogPrint("PFRecoTauChHProducer")
<< " " << std::setiosflags(std::ios::left) << std::setw(width) << ranker->name() << " "
<< std::resetiosflags(std::ios::left) << std::setprecision(3) << (*ranker)(chargedHadron) << std::endl;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ namespace reco {
}
}

return output.release();
return output;
}

} // namespace tau
Expand Down
4 changes: 2 additions & 2 deletions RecoTauTag/RecoTau/plugins/RecoTauBuilderConePlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ namespace reco {
} else {
// If there is no leading charged candidate at all, return nothing - the
// producer class that owns the plugin will build a null tau if desired.
return output.release();
return output;
}

// Find the leading neutral candidate
Expand Down Expand Up @@ -419,7 +419,7 @@ namespace reco {
setTauQuantities(*tauPtr, minAbsPhotonSumPt_insideSignalCone_, minRelPhotonSumPt_insideSignalCone_);

output.push_back(std::move(tauPtr));
return output.release();
return output;
}
} // namespace tau
} // namespace reco
Expand Down
1 change: 0 additions & 1 deletion RecoTauTag/RecoTau/plugins/RecoTauCleaner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*
*/

#include <boost/ptr_container/ptr_vector.hpp>
#include <algorithm>
#include <memory>

Expand Down
Loading

0 comments on commit ea43e07

Please sign in to comment.