CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/PhysicsTools/TagAndProbe/plugins/NearbyCandCountComputer.cc

Go to the documentation of this file.
00001 //
00002 // $Id: NearbyCandCountComputer.cc,v 1.3 2010/10/05 15:05:10 gpetrucc Exp $
00003 //
00004 
00016 #include "FWCore/Framework/interface/EDProducer.h"
00017 #include "FWCore/Framework/interface/Event.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 #include "FWCore/Utilities/interface/InputTag.h"
00020 
00021 #include "DataFormats/Math/interface/deltaR.h"
00022 #include "DataFormats/Common/interface/ValueMap.h"
00023 #include "DataFormats/Common/interface/View.h"
00024 
00025 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00026 #include "DataFormats/Candidate/interface/Candidate.h"
00027 #include "PhysicsTools/PatUtils/interface/PATDiObjectProxy.h"
00028 
00029 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00030 
00031 class NearbyCandCountComputer : public edm::EDProducer {
00032     public:
00033         explicit NearbyCandCountComputer(const edm::ParameterSet & iConfig);
00034         virtual ~NearbyCandCountComputer() ;
00035 
00036         virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00037 
00038     private:
00039         edm::InputTag probes_;            
00040         edm::InputTag objects_; 
00041         double deltaR2_;
00042         StringCutObjectSelector<reco::Candidate,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00043         StringCutObjectSelector<pat::DiObjectProxy,true> pairCut_;
00044 };
00045 
00046 NearbyCandCountComputer::NearbyCandCountComputer(const edm::ParameterSet & iConfig) :
00047     probes_(iConfig.getParameter<edm::InputTag>("probes")),
00048     objects_(iConfig.getParameter<edm::InputTag>("objects")),
00049     deltaR2_(std::pow(iConfig.getParameter<double>("deltaR"), 2)),
00050     objCut_(iConfig.existsAs<std::string>("objectSelection") ? iConfig.getParameter<std::string>("objectSelection") : "", true),
00051     pairCut_(iConfig.existsAs<std::string>("pairSelection") ? iConfig.getParameter<std::string>("pairSelection") : "", true)
00052 {
00053     produces<edm::ValueMap<float> >();
00054 }
00055 
00056 
00057 NearbyCandCountComputer::~NearbyCandCountComputer()
00058 {
00059 }
00060 
00061 void 
00062 NearbyCandCountComputer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00063     using namespace edm;
00064 
00065     // read input
00066     Handle<View<reco::Candidate> > probes, objects;
00067     iEvent.getByLabel(probes_,  probes);
00068     iEvent.getByLabel(objects_, objects);
00069 
00070     // prepare vector for output    
00071     std::vector<float> values;
00072     
00073     // fill
00074     View<reco::Candidate>::const_iterator probe, endprobes = probes->end();
00075     View<reco::Candidate>::const_iterator object, beginobjects = objects->begin(), endobjects = objects->end();
00076     for (probe = probes->begin(); probe != endprobes; ++probe) {
00077         float count = 0;
00078         for (object = beginobjects; object != endobjects; ++object) {
00079             if ((deltaR2(*probe, *object) >= deltaR2_) &&
00080                 objCut_(*object) && 
00081                 pairCut_(pat::DiObjectProxy(*probe, *object))) {
00082                     count++;
00083             }             
00084         }
00085         values.push_back(count);
00086     }
00087 
00088     // convert into ValueMap and store
00089     std::auto_ptr<ValueMap<float> > valMap(new ValueMap<float>());
00090     ValueMap<float>::Filler filler(*valMap);
00091     filler.insert(probes, values.begin(), values.end());
00092     filler.fill();
00093     iEvent.put(valMap);
00094 }
00095 
00096 
00097 #include "FWCore/Framework/interface/MakerMacros.h"
00098 DEFINE_FWK_MODULE(NearbyCandCountComputer);