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  * $Id $
13  */
14 
15 #include <boost/ptr_container/ptr_vector.hpp>
16 #include <boost/ptr_container/ptr_list.hpp>
17 #include <boost/foreach.hpp>
18 #include <algorithm>
19 #include <functional>
20 
27 
31 
36 
39 
41  public:
44 
45  explicit RecoTauPiZeroProducer(const edm::ParameterSet& pset);
47  void produce(edm::Event& evt, const edm::EventSetup& es);
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 
63  std::auto_ptr<PiZeroPredicate> predicate_;
64  double piZeroMass_;
65 
66  // Output selector
67  std::auto_ptr<StringCutObjectSelector<reco::RecoTauPiZero> >
69 };
70 
72  src_ = pset.getParameter<edm::InputTag>("jetSrc");
73 
74  typedef std::vector<edm::ParameterSet> VPSet;
75  // Get the mass hypothesis for the pizeros
76  piZeroMass_ = pset.getParameter<double>("massHypothesis");
77 
78  // Get each of our PiZero builders
79  const VPSet& builders = pset.getParameter<VPSet>("builders");
80 
81  for (VPSet::const_iterator builderPSet = builders.begin();
82  builderPSet != builders.end(); ++builderPSet) {
83  // Get plugin name
84  const std::string& pluginType =
85  builderPSet->getParameter<std::string>("plugin");
86  // Build the plugin
88  pluginType, *builderPSet));
89  }
90 
91  // Get each of our quality rankers
92  const VPSet& rankers = pset.getParameter<VPSet>("ranking");
93  for (VPSet::const_iterator rankerPSet = rankers.begin();
94  rankerPSet != rankers.end(); ++rankerPSet) {
95  const std::string& pluginType =
96  rankerPSet->getParameter<std::string>("plugin");
98  pluginType, *rankerPSet));
99  }
100 
101  // Build the sorting predicate
102  predicate_ = std::auto_ptr<PiZeroPredicate>(new PiZeroPredicate(rankers_));
103 
104  // Check if we want to apply a final output selection
105  if (pset.exists("outputSelection")) {
106  std::string selection = pset.getParameter<std::string>("outputSelection");
107  if (selection != "") {
108  outputSelector_.reset(
110  }
111  }
112 
113  produces<reco::JetPiZeroAssociation>();
114 }
115 
117  const edm::EventSetup& es) {
118  // Get a view of our jets via the base candidates
120  evt.getByLabel(src_, jetView);
121 
122  // Give each of our plugins a chance at doing something with the edm::Event
123  BOOST_FOREACH(Builder& builder, builders_) {
124  builder.setup(evt, es);
125  }
126 
127  // Convert the view to a RefVector of actual PFJets
128  reco::PFJetRefVector jetRefs =
129  reco::tau::castView<reco::PFJetRefVector>(jetView);
130  // Make our association
131  std::auto_ptr<reco::JetPiZeroAssociation> association;
132 
133  if (jetRefs.size()) {
134  association.reset(
136  } else {
137  association.reset(new reco::JetPiZeroAssociation);
138  }
139 
140  // Loop over our jets
141  BOOST_FOREACH(const reco::PFJetRef& jet, jetRefs) {
142  // Build our global list of RecoTauPiZero
143  PiZeroList dirtyPiZeros;
144 
145  // Compute the pi zeros from this jet for all the desired algorithms
146  BOOST_FOREACH(const Builder& builder, builders_) {
147  try {
148  PiZeroVector result(builder(*jet));
149  dirtyPiZeros.transfer(dirtyPiZeros.end(), result);
150  } catch ( cms::Exception &exception) {
151  edm::LogError("BuilderPluginException")
152  << "Exception caught in builder plugin " << builder.name()
153  << ", rethrowing" << std::endl;
154  throw exception;
155  }
156  }
157  // Rank the candidates according to our quality plugins
158  dirtyPiZeros.sort(*predicate_);
159 
160  // Keep track of the photons in the clean collection
161  std::vector<reco::RecoTauPiZero> cleanPiZeros;
162  std::set<reco::CandidatePtr> photonsInCleanCollection;
163  while (dirtyPiZeros.size()) {
164  // Pull our candidate pi zero from the front of the list
165  std::auto_ptr<reco::RecoTauPiZero> toAdd(
166  dirtyPiZeros.pop_front().release());
167  // If this doesn't pass our basic selection, discard it.
168  if (!(*outputSelector_)(*toAdd)) {
169  continue;
170  }
171  // Find the sub-gammas that are not already in the cleaned collection
172  std::vector<reco::CandidatePtr> uniqueGammas;
173  std::set_difference(toAdd->daughterPtrVector().begin(),
174  toAdd->daughterPtrVector().end(),
175  photonsInCleanCollection.begin(),
176  photonsInCleanCollection.end(),
177  std::back_inserter(uniqueGammas));
178  // If the pi zero has no unique gammas, discard it. Note toAdd is deleted
179  // when it goes out of scope.
180  if (!uniqueGammas.size()) {
181  continue;
182  } else if (uniqueGammas.size() == toAdd->daughterPtrVector().size()) {
183  // Check if it is composed entirely of unique gammas. In this case
184  // immediately add it to the clean collection.
185  photonsInCleanCollection.insert(toAdd->daughterPtrVector().begin(),
186  toAdd->daughterPtrVector().end());
187  cleanPiZeros.push_back(*toAdd);
188  } else {
189  // Otherwise update the pizero that contains only the unique gammas and
190  // add it back into the sorted list of dirty PiZeros
191  toAdd->clearDaughters();
192  // Add each of the unique daughters back to the pizero
193  BOOST_FOREACH(const reco::CandidatePtr& gamma, uniqueGammas) {
194  toAdd->addDaughter(gamma);
195  }
196  // Update the four vector
197  AddFourMomenta p4Builder_;
198  p4Builder_.set(*toAdd);
199  // Put this pi zero back into the collection of sorted dirty pizeros
200  PiZeroList::iterator insertionPoint = std::lower_bound(
201  dirtyPiZeros.begin(), dirtyPiZeros.end(), *toAdd, *predicate_);
202  dirtyPiZeros.insert(insertionPoint, toAdd);
203  }
204  }
205  // Apply the mass hypothesis if desired
206  if (piZeroMass_ >= 0) {
207  std::for_each(
208  cleanPiZeros.begin(), cleanPiZeros.end(),
209  std::bind2nd(
210  std::mem_fun_ref(&reco::RecoTauPiZero::setMass), piZeroMass_));
211  }
212  // Add to association
213  //print(cleanPiZeros, std::cout);
214  association->setValue(jet.key(), cleanPiZeros);
215  }
216  evt.put(association);
217 }
218 
219 // Print some helpful information
221  const std::vector<reco::RecoTauPiZero>& piZeros, std::ostream& out) {
222  const unsigned int width = 25;
223  BOOST_FOREACH(const reco::RecoTauPiZero& piZero, piZeros) {
224  out << piZero;
225  out << "* Rankers:" << std::endl;
226  for (rankerList::const_iterator ranker = rankers_.begin();
227  ranker != rankers_.end(); ++ranker) {
228  out << "* " << std::setiosflags(std::ios::left)
229  << std::setw(width) << ranker->name()
230  << " " << std::resetiosflags(std::ios::left)
231  << std::setprecision(3) << (*ranker)(piZero);
232  out << std::endl;
233  }
234  }
235 }
236 
T getParameter(std::string const &) const
#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
void print(const std::vector< reco::RecoTauPiZero > &piZeros, std::ostream &out)
reco::tau::RecoTauPiZeroBuilderPlugin Builder
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
tuple result
Definition: query.py:137
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
boost::ptr_vector< reco::RecoTauPiZero > PiZeroVector
tuple out
Definition: dbtoconf.py:99
boost::ptr_vector< Builder > builderList
boost::ptr_list< reco::RecoTauPiZero > PiZeroList
key_type key() const
Accessor for product key.
Definition: Ref.h:266
void produce(edm::Event &evt, const edm::EventSetup &es)
size_type size() const
Size of the RefVector.
Definition: RefVector.h:89
boost::ptr_vector< Ranker > rankerList
void set(reco::Candidate &c) const
set up a candidate
std::auto_ptr< StringCutObjectSelector< reco::RecoTauPiZero > > outputSelector_
virtual void setMass(double m)
set particle mass
void setup(const edm::Event &, const edm::EventSetup &)
const std::string & name() const
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:56
std::auto_ptr< PiZeroPredicate > predicate_