CMS 3D CMS Logo

RecoTauPiZeroProducer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauPiZeroProducer
3  *
4  * Author: Evan K. Friis, UC Davis
5  *
6  * Associates reconstructed PiZeros to PFJets. The PiZeros are built using one
7  * or more RecoTauBuilder plugins. Any overlaps (PiZeros sharing constituents)
8  * are removed, with the best PiZero candidates taken. The 'best' are defined
9  * via the input list of RecoTauPiZeroQualityPlugins, which form a
10  * lexicograpical ranking.
11  *
12  */
13 
14 #include <boost/ptr_container/ptr_vector.hpp>
15 #include <boost/ptr_container/ptr_list.hpp>
16 #include <boost/foreach.hpp>
17 #include <algorithm>
18 #include <functional>
19 
27 
31 
36 
39 
41  public:
44 
46  ~RecoTauPiZeroProducer() override {}
47  void produce(edm::Event& evt, const edm::EventSetup& es) override;
48  void print(const std::vector<reco::RecoTauPiZero>& piZeros,
49  std::ostream& out);
50 
51  private:
52  typedef boost::ptr_vector<Builder> builderList;
53  typedef boost::ptr_vector<Ranker> rankerList;
54  typedef boost::ptr_vector<reco::RecoTauPiZero> PiZeroVector;
55  typedef boost::ptr_list<reco::RecoTauPiZero> PiZeroList;
56 
59 
60  builderList builders_;
61  rankerList rankers_;
62  std::auto_ptr<PiZeroPredicate> predicate_;
63  double piZeroMass_;
64 
65  // Output selector
66  std::auto_ptr<StringCutObjectSelector<reco::RecoTauPiZero> >
68 
69  //consumes interface
71 
72  double minJetPt_;
73  double maxJetAbsEta_;
74 
76 };
77 
79 {
80  cand_token = consumes<reco::CandidateView>( pset.getParameter<edm::InputTag>("jetSrc"));
81  minJetPt_ = ( pset.exists("minJetPt") ) ? pset.getParameter<double>("minJetPt") : -1.0;
82  maxJetAbsEta_ = ( pset.exists("maxJetAbsEta") ) ? pset.getParameter<double>("maxJetAbsEta") : 99.0;
83 
84  typedef std::vector<edm::ParameterSet> VPSet;
85  // Get the mass hypothesis for the pizeros
86  piZeroMass_ = pset.getParameter<double>("massHypothesis");
87 
88  // Get each of our PiZero builders
89  const VPSet& builders = pset.getParameter<VPSet>("builders");
90 
91  for (VPSet::const_iterator builderPSet = builders.begin();
92  builderPSet != builders.end(); ++builderPSet) {
93  // Get plugin name
94  const std::string& pluginType =
95  builderPSet->getParameter<std::string>("plugin");
96  // Build the plugin
98  pluginType, *builderPSet, consumesCollector()));
99  }
100 
101  // Get each of our quality rankers
102  const VPSet& rankers = pset.getParameter<VPSet>("ranking");
103  for (VPSet::const_iterator rankerPSet = rankers.begin();
104  rankerPSet != rankers.end(); ++rankerPSet) {
105  const std::string& pluginType =
106  rankerPSet->getParameter<std::string>("plugin");
108  pluginType, *rankerPSet));
109  }
110 
111  // Build the sorting predicate
112  predicate_ = std::auto_ptr<PiZeroPredicate>(new PiZeroPredicate(rankers_));
113 
114  // Check if we want to apply a final output selection
115  if (pset.exists("outputSelection")) {
116  std::string selection = pset.getParameter<std::string>("outputSelection");
117  if (selection != "") {
118  outputSelector_.reset(
120  }
121  }
122 
123  verbosity_ = ( pset.exists("verbosity") ) ?
124  pset.getParameter<int>("verbosity") : 0;
125 
126  produces<reco::JetPiZeroAssociation>();
127 }
128 
130 {
131  // Get a view of our jets via the base candidates
133  evt.getByToken(cand_token, jetView);
134 
135  // Give each of our plugins a chance at doing something with the edm::Event
136  BOOST_FOREACH(Builder& builder, builders_) {
137  builder.setup(evt, es);
138  }
139 
140  // Convert the view to a RefVector of actual PFJets
141  reco::PFJetRefVector jetRefs =
142  reco::tau::castView<reco::PFJetRefVector>(jetView);
143  // Make our association
144  std::unique_ptr<reco::JetPiZeroAssociation> association;
145 
146  if (!jetRefs.empty()) {
147  edm::Handle<reco::PFJetCollection> pfJetCollectionHandle;
148  evt.get(jetRefs.id(), pfJetCollectionHandle);
149  association = std::make_unique<reco::JetPiZeroAssociation>(reco::PFJetRefProd(pfJetCollectionHandle));
150  } else {
151  association = std::make_unique<reco::JetPiZeroAssociation>();
152  }
153 
154  // Loop over our jets
155  BOOST_FOREACH(const reco::PFJetRef& jet, jetRefs) {
156 
157  if(jet->pt() - minJetPt_ < 1e-5) continue;
158  if(std::abs(jet->eta()) - maxJetAbsEta_ > -1e-5) continue;
159  // Build our global list of RecoTauPiZero
160  PiZeroList dirtyPiZeros;
161 
162  // Compute the pi zeros from this jet for all the desired algorithms
163  BOOST_FOREACH(const Builder& builder, builders_) {
164  try {
165  PiZeroVector result(builder(*jet));
166  dirtyPiZeros.transfer(dirtyPiZeros.end(), result);
167  } catch ( cms::Exception &exception) {
168  edm::LogError("BuilderPluginException")
169  << "Exception caught in builder plugin " << builder.name()
170  << ", rethrowing" << std::endl;
171  throw exception;
172  }
173  }
174  // Rank the candidates according to our quality plugins
175  dirtyPiZeros.sort(*predicate_);
176 
177  // Keep track of the photons in the clean collection
178  std::vector<reco::RecoTauPiZero> cleanPiZeros;
179  std::set<reco::CandidatePtr> photonsInCleanCollection;
180  while (!dirtyPiZeros.empty()) {
181  // Pull our candidate pi zero from the front of the list
182  std::auto_ptr<reco::RecoTauPiZero> toAdd(
183  dirtyPiZeros.pop_front().release());
184  // If this doesn't pass our basic selection, discard it.
185  if (!(*outputSelector_)(*toAdd)) {
186  continue;
187  }
188  // Find the sub-gammas that are not already in the cleaned collection
189  std::vector<reco::CandidatePtr> uniqueGammas;
190  std::set_difference(toAdd->daughterPtrVector().begin(),
191  toAdd->daughterPtrVector().end(),
192  photonsInCleanCollection.begin(),
193  photonsInCleanCollection.end(),
194  std::back_inserter(uniqueGammas));
195  // If the pi zero has no unique gammas, discard it. Note toAdd is deleted
196  // when it goes out of scope.
197  if (uniqueGammas.empty()) {
198  continue;
199  } else if (uniqueGammas.size() == toAdd->daughterPtrVector().size()) {
200  // Check if it is composed entirely of unique gammas. In this case
201  // immediately add it to the clean collection.
202  photonsInCleanCollection.insert(toAdd->daughterPtrVector().begin(),
203  toAdd->daughterPtrVector().end());
204  cleanPiZeros.push_back(*toAdd);
205  } else {
206  // Otherwise update the pizero that contains only the unique gammas and
207  // add it back into the sorted list of dirty PiZeros
208  toAdd->clearDaughters();
209  // Add each of the unique daughters back to the pizero
210  BOOST_FOREACH(const reco::CandidatePtr& gamma, uniqueGammas) {
211  toAdd->addDaughter(gamma);
212  }
213  // Update the four vector
214  AddFourMomenta p4Builder_;
215  p4Builder_.set(*toAdd);
216  // Put this pi zero back into the collection of sorted dirty pizeros
217  PiZeroList::iterator insertionPoint = std::lower_bound(
218  dirtyPiZeros.begin(), dirtyPiZeros.end(), *toAdd, *predicate_);
219  dirtyPiZeros.insert(insertionPoint, toAdd);
220  }
221  }
222  // Apply the mass hypothesis if desired
223  if (piZeroMass_ >= 0) {
224  std::for_each(
225  cleanPiZeros.begin(), cleanPiZeros.end(),
226  std::bind2nd(
227  std::mem_fun_ref(&reco::RecoTauPiZero::setMass), piZeroMass_));
228  }
229  // Add to association
230  if ( verbosity_ >= 2 ) {
231  print(cleanPiZeros, std::cout);
232  }
233  association->setValue(jet.key(), cleanPiZeros);
234  }
235  evt.put(std::move(association));
236 }
237 
238 // Print some helpful information
240  const std::vector<reco::RecoTauPiZero>& piZeros, std::ostream& out) {
241  const unsigned int width = 25;
242  BOOST_FOREACH(const reco::RecoTauPiZero& piZero, piZeros) {
243  out << piZero;
244  out << "* Rankers:" << std::endl;
245  for (rankerList::const_iterator ranker = rankers_.begin();
246  ranker != rankers_.end(); ++ranker) {
247  out << "* " << std::setiosflags(std::ios::left)
248  << std::setw(width) << ranker->name()
249  << " " << std::resetiosflags(std::ios::left)
250  << std::setprecision(3) << (*ranker)(piZero);
251  out << std::endl;
252  }
253  }
254 }
255 
T getParameter(std::string const &) const
void produce(edm::Event &evt, const edm::EventSetup &es) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
void setMass(double m) final
set particle mass
def create(alignables, pedeDump, additionalData, outputFile, config)
edm::EDGetTokenT< reco::CandidateView > cand_token
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
reco::tau::RecoTauPiZeroQualityPlugin Ranker
selection
main part
Definition: corrVsCorr.py:98
bool exists(std::string const &parameterName) const
checks if a parameter exists
RecoTauPiZeroProducer(const edm::ParameterSet &pset)
reco::tau::RecoTauLexicographicalRanking< rankerList, reco::RecoTauPiZero > PiZeroPredicate
key_type key() const
Accessor for product key.
Definition: Ref.h:265
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:104
void print(const std::vector< reco::RecoTauPiZero > &piZeros, std::ostream &out)
void setup(edm::Event &, const edm::EventSetup &)
reco::tau::RecoTauPiZeroBuilderPlugin Builder
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:122
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:341
edm::RefProd< PFJetCollection > PFJetRefProd
boost::ptr_vector< reco::RecoTauPiZero > PiZeroVector
boost::ptr_vector< Builder > builderList
boost::ptr_list< reco::RecoTauPiZero > PiZeroList
boost::ptr_vector< Ranker > rankerList
void set(reco::Candidate &c) const
set up a candidate
std::auto_ptr< StringCutObjectSelector< reco::RecoTauPiZero > > outputSelector_
const std::string & name() const
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55
std::auto_ptr< PiZeroPredicate > predicate_