CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
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 
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 
73 };
74 
76 {
77  cand_token = consumes<reco::CandidateView>( pset.getParameter<edm::InputTag>("jetSrc"));
78 
79  typedef std::vector<edm::ParameterSet> VPSet;
80  // Get the mass hypothesis for the pizeros
81  piZeroMass_ = pset.getParameter<double>("massHypothesis");
82 
83  // Get each of our PiZero builders
84  const VPSet& builders = pset.getParameter<VPSet>("builders");
85 
86  for (VPSet::const_iterator builderPSet = builders.begin();
87  builderPSet != builders.end(); ++builderPSet) {
88  // Get plugin name
89  const std::string& pluginType =
90  builderPSet->getParameter<std::string>("plugin");
91  // Build the plugin
93  pluginType, *builderPSet, consumesCollector()));
94  }
95 
96  // Get each of our quality rankers
97  const VPSet& rankers = pset.getParameter<VPSet>("ranking");
98  for (VPSet::const_iterator rankerPSet = rankers.begin();
99  rankerPSet != rankers.end(); ++rankerPSet) {
100  const std::string& pluginType =
101  rankerPSet->getParameter<std::string>("plugin");
103  pluginType, *rankerPSet));
104  }
105 
106  // Build the sorting predicate
107  predicate_ = std::auto_ptr<PiZeroPredicate>(new PiZeroPredicate(rankers_));
108 
109  // Check if we want to apply a final output selection
110  if (pset.exists("outputSelection")) {
111  std::string selection = pset.getParameter<std::string>("outputSelection");
112  if (selection != "") {
113  outputSelector_.reset(
115  }
116  }
117 
118  verbosity_ = ( pset.exists("verbosity") ) ?
119  pset.getParameter<int>("verbosity") : 0;
120 
121  produces<reco::JetPiZeroAssociation>();
122 }
123 
125 {
126  // Get a view of our jets via the base candidates
128  evt.getByToken(cand_token, jetView);
129 
130  // Give each of our plugins a chance at doing something with the edm::Event
131  BOOST_FOREACH(Builder& builder, builders_) {
132  builder.setup(evt, es);
133  }
134 
135  // Convert the view to a RefVector of actual PFJets
136  reco::PFJetRefVector jetRefs =
137  reco::tau::castView<reco::PFJetRefVector>(jetView);
138  // Make our association
139  std::auto_ptr<reco::JetPiZeroAssociation> association;
140 
141  if (jetRefs.size()) {
142  edm::Handle<reco::PFJetCollection> pfJetCollectionHandle;
143  evt.get(jetRefs.id(), pfJetCollectionHandle);
144  association.reset(
145  new reco::JetPiZeroAssociation(reco::PFJetRefProd(pfJetCollectionHandle)));
146  } else {
147  association.reset(new reco::JetPiZeroAssociation);
148  }
149 
150  // Loop over our jets
151  BOOST_FOREACH(const reco::PFJetRef& jet, jetRefs) {
152  // Build our global list of RecoTauPiZero
153  PiZeroList dirtyPiZeros;
154 
155  // Compute the pi zeros from this jet for all the desired algorithms
156  BOOST_FOREACH(const Builder& builder, builders_) {
157  try {
158  PiZeroVector result(builder(*jet));
159  dirtyPiZeros.transfer(dirtyPiZeros.end(), result);
160  } catch ( cms::Exception &exception) {
161  edm::LogError("BuilderPluginException")
162  << "Exception caught in builder plugin " << builder.name()
163  << ", rethrowing" << std::endl;
164  throw exception;
165  }
166  }
167  // Rank the candidates according to our quality plugins
168  dirtyPiZeros.sort(*predicate_);
169 
170  // Keep track of the photons in the clean collection
171  std::vector<reco::RecoTauPiZero> cleanPiZeros;
172  std::set<reco::CandidatePtr> photonsInCleanCollection;
173  while (dirtyPiZeros.size()) {
174  // Pull our candidate pi zero from the front of the list
175  std::auto_ptr<reco::RecoTauPiZero> toAdd(
176  dirtyPiZeros.pop_front().release());
177  // If this doesn't pass our basic selection, discard it.
178  if (!(*outputSelector_)(*toAdd)) {
179  continue;
180  }
181  // Find the sub-gammas that are not already in the cleaned collection
182  std::vector<reco::CandidatePtr> uniqueGammas;
183  std::set_difference(toAdd->daughterPtrVector().begin(),
184  toAdd->daughterPtrVector().end(),
185  photonsInCleanCollection.begin(),
186  photonsInCleanCollection.end(),
187  std::back_inserter(uniqueGammas));
188  // If the pi zero has no unique gammas, discard it. Note toAdd is deleted
189  // when it goes out of scope.
190  if (!uniqueGammas.size()) {
191  continue;
192  } else if (uniqueGammas.size() == toAdd->daughterPtrVector().size()) {
193  // Check if it is composed entirely of unique gammas. In this case
194  // immediately add it to the clean collection.
195  photonsInCleanCollection.insert(toAdd->daughterPtrVector().begin(),
196  toAdd->daughterPtrVector().end());
197  cleanPiZeros.push_back(*toAdd);
198  } else {
199  // Otherwise update the pizero that contains only the unique gammas and
200  // add it back into the sorted list of dirty PiZeros
201  toAdd->clearDaughters();
202  // Add each of the unique daughters back to the pizero
203  BOOST_FOREACH(const reco::CandidatePtr& gamma, uniqueGammas) {
204  toAdd->addDaughter(gamma);
205  }
206  // Update the four vector
207  AddFourMomenta p4Builder_;
208  p4Builder_.set(*toAdd);
209  // Put this pi zero back into the collection of sorted dirty pizeros
210  PiZeroList::iterator insertionPoint = std::lower_bound(
211  dirtyPiZeros.begin(), dirtyPiZeros.end(), *toAdd, *predicate_);
212  dirtyPiZeros.insert(insertionPoint, toAdd);
213  }
214  }
215  // Apply the mass hypothesis if desired
216  if (piZeroMass_ >= 0) {
217  std::for_each(
218  cleanPiZeros.begin(), cleanPiZeros.end(),
219  std::bind2nd(
220  std::mem_fun_ref(&reco::RecoTauPiZero::setMass), piZeroMass_));
221  }
222  // Add to association
223  if ( verbosity_ >= 2 ) {
224  print(cleanPiZeros, std::cout);
225  }
226  association->setValue(jet.key(), cleanPiZeros);
227  }
228  evt.put(association);
229 }
230 
231 // Print some helpful information
233  const std::vector<reco::RecoTauPiZero>& piZeros, std::ostream& out) {
234  const unsigned int width = 25;
235  BOOST_FOREACH(const reco::RecoTauPiZero& piZero, piZeros) {
236  out << piZero;
237  out << "* Rankers:" << std::endl;
238  for (rankerList::const_iterator ranker = rankers_.begin();
239  ranker != rankers_.end(); ++ranker) {
240  out << "* " << std::setiosflags(std::ios::left)
241  << std::setw(width) << ranker->name()
242  << " " << std::resetiosflags(std::ios::left)
243  << std::setprecision(3) << (*ranker)(piZero);
244  out << std::endl;
245  }
246  }
247 }
248 
T getParameter(std::string const &) const
void produce(edm::Event &evt, const edm::EventSetup &es) override
edm::EDGetTokenT< reco::CandidateView > cand_token
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#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:264
void print(const std::vector< reco::RecoTauPiZero > &piZeros, std::ostream &out)
tuple result
Definition: mps_fire.py:95
void setup(edm::Event &, const edm::EventSetup &)
reco::tau::RecoTauPiZeroBuilderPlugin Builder
ProductID id() const
Accessor for product ID.
Definition: RefVector.h:114
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:331
boost::ptr_vector< reco::RecoTauPiZero > PiZeroVector
boost::ptr_vector< Builder > builderList
boost::ptr_list< reco::RecoTauPiZero > PiZeroList
size_type size() const
Size of the RefVector.
Definition: RefVector.h:99
tuple cout
Definition: gather_cfg.py:145
virtual void setMass(double m) final
set particle mass
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
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:55
std::auto_ptr< PiZeroPredicate > predicate_