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