CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecoTauProducer.cc
Go to the documentation of this file.
1 /*
2  * RecoTauProducer
3  *
4  * Interface between the various tau algorithms and the edm::Event. The
5  * RecoTauProducer takes as data input is a collection (view) of reco::PFJets,
6  * and Jet-PiZero assoications that give the reco::RecoTauPiZeros for those
7  * jets. The actual building of taus is done by the list of builders - each of
8  * which constructs a PFTau for each PFJet. The output collection may have
9  * multiple taus for each PFJet - these overlaps are to be resolved by the
10  * RecoTauCleaner module.
11  *
12  * Additionally, there are "modifier" plugins, which can do things like add the
13  * lead track significance, or electron rejection variables.
14  *
15  * Authors: Evan K. Friis (UC Davis),
16  * Christian Veelken (LLR)
17  *
18  */
19 #include <boost/ptr_container/ptr_vector.hpp>
20 #include <boost/foreach.hpp>
21 
22 #include <algorithm>
23 #include <functional>
24 
30 
33 
41 
43 
45 {
46  public:
49  typedef boost::ptr_vector<Builder> BuilderList;
50  typedef boost::ptr_vector<Modifier> ModifierList;
51 
52  explicit RecoTauProducer(const edm::ParameterSet& pset);
54  void produce(edm::Event& evt, const edm::EventSetup& es) override;
55 
56  private:
61  //token definition
66 
69  // Optional selection on the output of the taus
70  std::auto_ptr<StringCutObjectSelector<reco::PFTau> > outputSelector_;
71  // Whether or not to add build a tau from a jet for which the builders
72  // return no taus. The tau will have no content, only the four vector of
73  // the orginal jet.
75 };
76 
78 {
79  jetSrc_ = pset.getParameter<edm::InputTag>("jetSrc");
80  jetRegionSrc_ = pset.getParameter<edm::InputTag>("jetRegionSrc");
81  chargedHadronSrc_ = pset.getParameter<edm::InputTag>("chargedHadronSrc");
82  piZeroSrc_ = pset.getParameter<edm::InputTag>("piZeroSrc");
83 
84  //consumes definition
85  jet_token=consumes<reco::CandidateView>(jetSrc_);
86  jetRegion_token = consumes<edm::Association<reco::PFJetCollection> >(jetRegionSrc_);
87  chargedHadron_token = consumes<reco::PFJetChargedHadronAssociation>(chargedHadronSrc_);
88  piZero_token = consumes<reco::JetPiZeroAssociation>(piZeroSrc_);
89 
90  typedef std::vector<edm::ParameterSet> VPSet;
91  // Get each of our tau builders
92  const VPSet& builders = pset.getParameter<VPSet>("builders");
93  for ( VPSet::const_iterator builderPSet = builders.begin();
94  builderPSet != builders.end(); ++builderPSet ) {
95  // Get plugin name
96  const std::string& pluginType = builderPSet->getParameter<std::string>("plugin");
97  // Build the plugin
98  builders_.push_back(RecoTauBuilderPluginFactory::get()->create(pluginType, *builderPSet, consumesCollector()));
99  }
100 
101  const VPSet& modfiers = pset.getParameter<VPSet>("modifiers");
102  for ( VPSet::const_iterator modfierPSet = modfiers.begin();
103  modfierPSet != modfiers.end(); ++modfierPSet) {
104  // Get plugin name
105  const std::string& pluginType = modfierPSet->getParameter<std::string>("plugin");
106  // Build the plugin
108  plugin = RecoTauModifierPluginFactory::get()->create(pluginType, *modfierPSet, consumesCollector());
109  plugin->beginJob(this);
110  modifiers_.push_back(plugin);
111  }
112 
113  // Check if we want to apply a final output selection
114  if ( pset.exists("outputSelection") ) {
115  std::string selection = pset.getParameter<std::string>("outputSelection");
116  if ( selection != "" ) {
118  }
119  }
120  buildNullTaus_ = pset.getParameter<bool>("buildNullTaus");
121 
122  produces<reco::PFTauCollection>();
123 }
124 
126 {
127  // Get the jet input collection via a view of Candidates
129  evt.getByToken(jet_token, jetView);
130 
131  // Convert to a vector of PFJetRefs
132  reco::PFJetRefVector jets = reco::tau::castView<reco::PFJetRefVector>(jetView);
133 
134  // Get the jet region producer
136  evt.getByToken(jetRegion_token, jetRegionHandle);
137 
138  // Get the charged hadron input collection
140  evt.getByToken(chargedHadron_token, chargedHadronAssoc);
141 
142  // Get the pizero input collection
144  evt.getByToken(piZero_token, piZeroAssoc);
145 
146  // Update all our builders and modifiers with the event info
147  for (BuilderList::iterator builder = builders_.begin();
148  builder != builders_.end(); ++builder) {
149  builder->setup(evt, es);
150  }
151  for (ModifierList::iterator modifier = modifiers_.begin();
152  modifier != modifiers_.end(); ++modifier) {
153  modifier->setup(evt, es);
154  }
155 
156  // Create output collection
157  std::auto_ptr<reco::PFTauCollection> output(new reco::PFTauCollection());
158  output->reserve(jets.size());
159 
160  // Loop over the jets and build the taus for each jet
161  BOOST_FOREACH( reco::PFJetRef jetRef, jets ) {
162  // Get the jet with extra constituents from an area around the jet
163  reco::PFJetRef jetRegionRef = (*jetRegionHandle)[jetRef];
164  if ( jetRegionRef.isNull() ) {
165  throw cms::Exception("BadJetRegionRef")
166  << "No jet region can be found for the current jet: " << jetRef.id();
167  }
168  // Remove all the jet constituents from the jet extras
169  std::vector<reco::PFCandidatePtr> jetCands = jetRef->getPFConstituents();
170  std::vector<reco::PFCandidatePtr> allRegionalCands = jetRegionRef->getPFConstituents();
171  // Sort both by ref key
172  std::sort(jetCands.begin(), jetCands.end());
173  std::sort(allRegionalCands.begin(), allRegionalCands.end());
174  // Get the regional junk candidates not in the jet.
175  std::vector<reco::PFCandidatePtr> uniqueRegionalCands;
176 
177  // This can actually be less than zero, if the jet has really crazy soft
178  // stuff really far away from the jet axis.
179  if ( allRegionalCands.size() > jetCands.size() ) {
180  uniqueRegionalCands.reserve(allRegionalCands.size() - jetCands.size());
181  }
182 
183  // Subtract the jet cands from the regional cands
184  std::set_difference(allRegionalCands.begin(), allRegionalCands.end(),
185  jetCands.begin(), jetCands.end(),
186  std::back_inserter(uniqueRegionalCands));
187 
188  // Get the charged hadrons associated with this jet
189  const std::vector<reco::PFRecoTauChargedHadron>& chargedHadrons = (*chargedHadronAssoc)[jetRef];
190 
191  // Get the pizeros associated with this jet
192  const std::vector<reco::RecoTauPiZero>& piZeros = (*piZeroAssoc)[jetRef];
193 
194  // Loop over our builders and create the set of taus for this jet
195  unsigned int nTausBuilt = 0;
196  for ( BuilderList::const_iterator builder = builders_.begin();
197  builder != builders_.end(); ++builder) {
198  // Get a ptr_vector of taus from the builder
199  reco::tau::RecoTauBuilderPlugin::output_type taus((*builder)(jetRef, chargedHadrons, piZeros, uniqueRegionalCands));
200  // Make sure all taus have their jetref set correctly
201  std::for_each(taus.begin(), taus.end(), boost::bind(&reco::PFTau::setjetRef, _1, jetRef));
202  // Copy without selection
203  if ( !outputSelector_.get() ) {
204  output->insert(output->end(), taus.begin(), taus.end());
205  nTausBuilt += taus.size();
206  } else {
207  // Copy only those that pass the selection.
208  BOOST_FOREACH( const reco::PFTau& tau, taus ) {
209  if ( (*outputSelector_)(tau) ) {
210  nTausBuilt++;
211  output->push_back(tau);
212  }
213  }
214  }
215  }
216  // If we didn't build *any* taus for this jet, build a null tau if desired.
217  // The null PFTau has no content, but it's four vector is set to that of the
218  // jet.
219  if ( !nTausBuilt && buildNullTaus_ ) {
220  reco::PFTau nullTau(std::numeric_limits<int>::quiet_NaN(), jetRef->p4());
221  nullTau.setjetRef(jetRef);
222  output->push_back(nullTau);
223  }
224  }
225 
226  // Loop over the taus we have created and apply our modifiers to the taus
227  for ( reco::PFTauCollection::iterator tau = output->begin();
228  tau != output->end(); ++tau ) {
229  for ( ModifierList::const_iterator modifier = modifiers_.begin();
230  modifier != modifiers_.end(); ++modifier ) {
231  (*modifier)(*tau);
232  }
233  }
234 
235  for ( ModifierList::iterator modifier = modifiers_.begin();
236  modifier != modifiers_.end(); ++modifier ) {
237  modifier->endEvent();
238  }
239 
240  evt.put(output);
241 }
242 
T getParameter(std::string const &) const
edm::InputTag piZeroSrc_
boost::ptr_vector< Modifier > ModifierList
edm::EDGetTokenT< reco::CandidateView > jet_token
std::vector< PFTau > PFTauCollection
collection of PFTau objects
Definition: PFTauFwd.h:9
auto_ptr< JetDefinition::Plugin > plugin
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
BuilderList builders_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
selection
main part
Definition: corrVsCorr.py:98
bool exists(std::string const &parameterName) const
checks if a parameter exists
boost::ptr_vector< reco::PFTau > output_type
ModifierList modifiers_
std::auto_ptr< StringCutObjectSelector< reco::PFTau > > outputSelector_
void setjetRef(const PFJetRef &)
Definition: PFTau.cc:53
bool isNull() const
Checks for null.
Definition: Ref.h:247
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.
RecoTauProducer(const edm::ParameterSet &pset)
vector< PseudoJet > jets
edm::InputTag chargedHadronSrc_
edm::InputTag jetSrc_
void produce(edm::Event &evt, const edm::EventSetup &es) override
edm::EDGetTokenT< reco::JetPiZeroAssociation > piZero_token
reco::tau::RecoTauModifierPlugin Modifier
edm::EDGetTokenT< reco::PFJetChargedHadronAssociation > chargedHadron_token
edm::EDGetTokenT< edm::Association< reco::PFJetCollection > > jetRegion_token
size_type size() const
Size of the RefVector.
Definition: RefVector.h:89
reco::tau::RecoTauBuilderPlugin Builder
ProductID id() const
Accessor for product ID.
Definition: Ref.h:256
edm::InputTag jetRegionSrc_
boost::ptr_vector< Builder > BuilderList
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:55