CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoTauTag/RecoTau/plugins/RecoTauProducer.cc

Go to the documentation of this file.
00001 /*
00002  * RecoTauProducer
00003  *
00004  * Interface between the various tau algorithms and the edm::Event.  The
00005  * RecoTauProducer takes as data input is a collection (view) of reco::PFJets,
00006  * and Jet-PiZero assoications that give the reco::RecoTauPiZeros for those
00007  * jets.  The actaul building of taus is done by the list of builders - each of
00008  * which constructs a PFTau for each PFJet.  The output collection may have
00009  * multiple taus for each PFJet - these overlaps are to be resolved by the
00010  * RecoTauCleaner module.
00011  *
00012  * Additionally, there are "modifier" plugins, which can do things like add the
00013  * lead track significance, or electron rejection variables.
00014  *
00015  * Author: Evan K. Friis (UC Davis)
00016  *
00017  */
00018 #include <boost/ptr_container/ptr_vector.hpp>
00019 #include <boost/foreach.hpp>
00020 
00021 #include <algorithm>
00022 #include <functional>
00023 
00024 #include "FWCore/Framework/interface/EDProducer.h"
00025 #include "FWCore/Framework/interface/EventSetup.h"
00026 #include "FWCore/Framework/interface/ESHandle.h"
00027 #include "FWCore/Framework/interface/Event.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00029 
00030 #include "RecoTauTag/RecoTau/interface/RecoTauBuilderPlugins.h"
00031 #include "RecoTauTag/RecoTau/interface/RecoTauCommonUtilities.h"
00032 
00033 #include "DataFormats/JetReco/interface/PFJetCollection.h"
00034 #include "DataFormats/TauReco/interface/JetPiZeroAssociation.h"
00035 #include "DataFormats/TauReco/interface/PFTau.h"
00036 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00037 
00038 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00039 
00040 class RecoTauProducer : public edm::EDProducer {
00041   public:
00042     typedef reco::tau::RecoTauBuilderPlugin Builder;
00043     typedef reco::tau::RecoTauModifierPlugin Modifier;
00044     typedef boost::ptr_vector<Builder> BuilderList;
00045     typedef boost::ptr_vector<Modifier> ModifierList;
00046 
00047     explicit RecoTauProducer(const edm::ParameterSet& pset);
00048     ~RecoTauProducer() {}
00049     void produce(edm::Event& evt, const edm::EventSetup& es);
00050 
00051   private:
00052     edm::InputTag jetSrc_;
00053     edm::InputTag piZeroSrc_;
00054     BuilderList builders_;
00055     ModifierList modifiers_;
00056     // Optional selection on the output of the taus
00057     std::auto_ptr<StringCutObjectSelector<reco::PFTau> > outputSelector_;
00058     // Whether or not to add build a tau from a jet for which the builders
00059     // return no taus.  The tau will have no content, only the four vector of
00060     // the orginal jet.
00061     bool buildNullTaus_;
00062 };
00063 
00064 RecoTauProducer::RecoTauProducer(const edm::ParameterSet& pset) {
00065   jetSrc_ = pset.getParameter<edm::InputTag>("jetSrc");
00066   piZeroSrc_ = pset.getParameter<edm::InputTag>("piZeroSrc");
00067 
00068   typedef std::vector<edm::ParameterSet> VPSet;
00069   // Get each of our tau builders
00070   const VPSet& builders = pset.getParameter<VPSet>("builders");
00071   for (VPSet::const_iterator builderPSet = builders.begin();
00072       builderPSet != builders.end(); ++builderPSet) {
00073     // Get plugin name
00074     const std::string& pluginType =
00075         builderPSet->getParameter<std::string>("plugin");
00076     // Build the plugin
00077     builders_.push_back(
00078         RecoTauBuilderPluginFactory::get()->create(pluginType, *builderPSet));
00079   }
00080 
00081   const VPSet& modfiers = pset.getParameter<VPSet>("modifiers");
00082   for (VPSet::const_iterator modfierPSet = modfiers.begin();
00083       modfierPSet != modfiers.end(); ++modfierPSet) {
00084     // Get plugin name
00085     const std::string& pluginType =
00086         modfierPSet->getParameter<std::string>("plugin");
00087     // Build the plugin
00088     modifiers_.push_back(
00089         RecoTauModifierPluginFactory::get()->create(pluginType, *modfierPSet));
00090   }
00091 
00092   // Check if we want to apply a final output selection
00093   if (pset.exists("outputSelection")) {
00094     std::string selection = pset.getParameter<std::string>("outputSelection");
00095     if (selection != "") {
00096       outputSelector_.reset(
00097           new StringCutObjectSelector<reco::PFTau>(selection));
00098     }
00099   }
00100   buildNullTaus_ = pset.getParameter<bool>("buildNullTaus");
00101   produces<reco::PFTauCollection>();
00102 }
00103 
00104 void RecoTauProducer::produce(edm::Event& evt, const edm::EventSetup& es) {
00105   // Get the jet input collection via a view of Candidates
00106   edm::Handle<reco::CandidateView> jetView;
00107   evt.getByLabel(jetSrc_, jetView);
00108 
00109   // Convert to a vector of PFJetRefs
00110   reco::PFJetRefVector jets =
00111       reco::tau::castView<reco::PFJetRefVector>(jetView);
00112 
00113   // Get the pizero input collection
00114   edm::Handle<reco::JetPiZeroAssociation> piZeroAssoc;
00115   evt.getByLabel(piZeroSrc_, piZeroAssoc);
00116 
00117   // Update all our builders and modifiers with the event info
00118   for (BuilderList::iterator builder = builders_.begin();
00119       builder != builders_.end(); ++builder) {
00120     builder->setup(evt, es);
00121   }
00122   for (ModifierList::iterator modifier = modifiers_.begin();
00123       modifier != modifiers_.end(); ++modifier) {
00124     modifier->setup(evt, es);
00125   }
00126 
00127   // Create output collection
00128   std::auto_ptr<reco::PFTauCollection> output(new reco::PFTauCollection());
00129 
00130   // Loop over the jets and build the taus for each jet
00131   BOOST_FOREACH(reco::PFJetRef jetRef, jets) {
00132     // Get the PiZeros associated with this jet
00133     const std::vector<reco::RecoTauPiZero>& piZeros = (*piZeroAssoc)[jetRef];
00134     // Loop over our builders and create the set of taus for this jet
00135     unsigned int nTausBuilt = 0;
00136     for (BuilderList::const_iterator builder = builders_.begin();
00137         builder != builders_.end(); ++builder) {
00138       // Get a ptr_vector of taus from the builder
00139       reco::tau::RecoTauBuilderPlugin::output_type taus(
00140           (*builder)(jetRef, piZeros));
00141       // Check this size of the taus built.
00142       // Grow the vector if necessary
00143       output->reserve(output->size() + taus.size());
00144       // Copy without selection
00145       if (!outputSelector_.get()) {
00146         output->insert(output->end(), taus.begin(), taus.end());
00147         nTausBuilt += taus.size();
00148       } else {
00149         // Copy only those that pass the selection.
00150         BOOST_FOREACH(const reco::PFTau& tau, taus) {
00151           if ((*outputSelector_)(tau)) {
00152             nTausBuilt++;
00153             output->push_back(tau);
00154           }
00155         }
00156       }
00157     }
00158     // If we didn't build *any* taus for this jet, build a null tau if desired.
00159     // The null PFTau has no content, but it's four vector is set to that of the
00160     // jet.
00161     if (!nTausBuilt && buildNullTaus_) {
00162       reco::PFTau nullTau(std::numeric_limits<int>::quiet_NaN(), jetRef->p4());
00163       nullTau.setjetRef(jetRef);
00164       output->push_back(nullTau);
00165     }
00166   }
00167 
00168   // Loop over the taus we have created and apply our modifiers to the taus
00169   for (reco::PFTauCollection::iterator tau = output->begin();
00170       tau != output->end(); ++tau) {
00171     for (ModifierList::const_iterator modifier = modifiers_.begin();
00172         modifier != modifiers_.end(); ++modifier) {
00173       (*modifier)(*tau);
00174     }
00175   }
00176   evt.put(output);
00177 }
00178 
00179 #include "FWCore/Framework/interface/MakerMacros.h"
00180 DEFINE_FWK_MODULE(RecoTauProducer);