CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/PhysicsTools/TagAndProbe/plugins/ProbeTreeProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    ProbeTreeProducer
00004 // Class:      ProbeTreeProducer
00005 // 
00014 #include <memory>
00015 #include <ctype.h>
00016 #include "FWCore/Framework/interface/Frameworkfwd.h"
00017 #include "FWCore/Framework/interface/EDFilter.h"
00018 #include "FWCore/Framework/interface/Event.h"
00019 #include "FWCore/Framework/interface/MakerMacros.h"
00020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00021 #include "FWCore/Utilities/interface/InputTag.h"
00022 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00023 #include "DataFormats/Candidate/interface/Candidate.h"
00024 #include "PhysicsTools/TagAndProbe/interface/BaseTreeFiller.h"
00025 #include <set>
00026 #include "FWCore/ParameterSet/interface/Registry.h"
00027 
00028 class ProbeTreeProducer : public edm::EDFilter {
00029   public:
00030     explicit ProbeTreeProducer(const edm::ParameterSet&);
00031     ~ProbeTreeProducer();
00032 
00033   private:
00034     virtual bool filter(edm::Event&, const edm::EventSetup&);
00035     virtual void endJob();
00036 
00038     edm::InputTag probesTag_;
00039 
00041     StringCutObjectSelector<reco::Candidate, true> cut_;
00042 
00044     bool filter_;
00045 
00047     std::string sortDescendingBy_;
00048 
00050     StringObjectFunction<reco::Candidate, true> sortFunction_;
00051 
00053     int32_t maxProbes_;
00054 
00056     std::auto_ptr<tnp::BaseTreeFiller> probeFiller_;
00057 };
00058 
00059 ProbeTreeProducer::ProbeTreeProducer(const edm::ParameterSet& iConfig) :
00060   probesTag_(iConfig.getParameter<edm::InputTag>("src")),
00061   cut_(iConfig.existsAs<std::string>("cut") ? iConfig.getParameter<std::string>("cut") : ""),
00062   filter_(iConfig.existsAs<bool>("filter") ? iConfig.getParameter<bool>("filter") : false),
00063   sortDescendingBy_(iConfig.existsAs<std::string>("sortDescendingBy") ? iConfig.getParameter<std::string>("sortDescendingBy") : ""),
00064   sortFunction_(sortDescendingBy_.size()>0 ? sortDescendingBy_ : "pt"), //need to pass a valid default
00065   maxProbes_(iConfig.existsAs<int32_t>("maxProbes") ? iConfig.getParameter<int32_t>("maxProbes") : -1),
00066   probeFiller_(new tnp::BaseTreeFiller("probe_tree", iConfig))
00067 {
00068 }
00069 
00070 ProbeTreeProducer::~ProbeTreeProducer(){
00071 }
00072 
00073 bool ProbeTreeProducer::filter(edm::Event& iEvent, const edm::EventSetup& iSetup){
00074   bool result = !filter_;
00075   edm::Handle<reco::CandidateView> probes;
00076   iEvent.getByLabel(probesTag_, probes);
00077   if(!probes.isValid()) return result;
00078   probeFiller_->init(iEvent);
00079   // select probes and calculate the sorting value
00080   typedef std::pair<reco::CandidateBaseRef, double> Pair;
00081   std::vector<Pair> selectedProbes;
00082   for (size_t i = 0; i < probes->size(); ++i){
00083     const reco::CandidateBaseRef &probe = probes->refAt(i);
00084     if(cut_(*probe)){
00085       selectedProbes.push_back(Pair(probe, sortFunction_(*probe)));
00086     }
00087   }
00088   // sort only if a function was provided
00089   if(sortDescendingBy_.size()>0) sort(selectedProbes.begin(), selectedProbes.end(), boost::bind(&Pair::second, _1) > boost::bind(&Pair::second, _2));
00090   // fill the first maxProbes_ into the tree
00091   for (size_t i = 0; i < (maxProbes_<0 ? selectedProbes.size() : std::min((size_t)maxProbes_, selectedProbes.size())); ++i){
00092     probeFiller_->fill(selectedProbes[i].first);
00093     result = true;
00094   }
00095   return result;
00096 }
00097 
00098 void ProbeTreeProducer::endJob(){
00099     // ask to write the current PSet info into the TTree header
00100     probeFiller_->writeProvenance(edm::getProcessParameterSet());
00101 }
00102 
00103 //define this as a plug-in
00104 DEFINE_FWK_MODULE(ProbeTreeProducer);
00105