CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/PhysicsTools/TagAndProbe/plugins/OtherObjectVariableComputer.cc

Go to the documentation of this file.
00001 //
00002 
00011 #include "FWCore/Framework/interface/EDProducer.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/Utilities/interface/InputTag.h"
00015 
00016 #include "DataFormats/Math/interface/deltaR.h"
00017 #include "DataFormats/Common/interface/ValueMap.h"
00018 #include "DataFormats/Common/interface/View.h"
00019 
00020 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00021 #include "DataFormats/Candidate/interface/Candidate.h"
00022 #include "DataFormats/TrackReco/interface/Track.h"
00023 #include "DataFormats/VertexReco/interface/Vertex.h"
00024 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00025 #include "CommonTools/Utils/interface/StringObjectFunction.h"
00026 
00027 template<typename T>
00028 class OtherObjectVariableComputer : public edm::EDProducer {
00029     public:
00030         explicit OtherObjectVariableComputer(const edm::ParameterSet & iConfig);
00031         virtual ~OtherObjectVariableComputer() ;
00032 
00033         virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00034 
00035     private:
00036         edm::InputTag probes_;            
00037         edm::InputTag objects_; 
00038         StringObjectFunction<T,true>    objVar_;
00039         double default_;
00040         StringCutObjectSelector<T,true> objCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00041         bool doSort_;
00042         StringObjectFunction<T,true>    objSort_;
00043 };
00044 
00045 template<typename T>
00046 OtherObjectVariableComputer<T>::OtherObjectVariableComputer(const edm::ParameterSet & iConfig) :
00047     probes_(iConfig.getParameter<edm::InputTag>("probes")),
00048     objects_(iConfig.getParameter<edm::InputTag>("objects")),
00049     objVar_(iConfig.getParameter<std::string>("expression")),
00050     default_(iConfig.getParameter<double>("default")),
00051     objCut_(iConfig.existsAs<std::string>("objectSelection") ? iConfig.getParameter<std::string>("objectSelection") : "", true),
00052     doSort_(iConfig.existsAs<std::string>("objectSortDescendingBy")),
00053     objSort_(doSort_ ? iConfig.getParameter<std::string>("objectSortDescendingBy") : "1", true)
00054 {
00055     produces<edm::ValueMap<float> >();
00056 }
00057 
00058 
00059 template<typename T>
00060 OtherObjectVariableComputer<T>::~OtherObjectVariableComputer()
00061 {
00062 }
00063 
00064 template<typename T>
00065 void 
00066 OtherObjectVariableComputer<T>::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00067     using namespace edm;
00068 
00069     // read input
00070     Handle<View<reco::Candidate> > probes;
00071     Handle<View<T> > objects;
00072     iEvent.getByLabel(probes_,  probes);
00073     iEvent.getByLabel(objects_, objects);
00074     
00075     // fill
00076     std::vector<std::pair<double, double> > selected;
00077     typename View<T>::const_iterator object, endobjects = objects->end();
00078     for (object = objects->begin(); object != endobjects; ++object) {
00079       if (objCut_(*object)) {
00080         selected.push_back(std::pair<double, double>(objSort_(*object), objVar_(*object)));
00081         if (!doSort_) break; // if we take just the first one, there's no need of computing the others
00082       }
00083     }
00084     if (doSort_ && selected.size() > 1) std::sort(selected.begin(), selected.end()); // sorts (ascending)
00085 
00086     // prepare vector for output    
00087     std::vector<float> values(probes->size(), (selected.empty() ? default_ : selected.back().second));
00088 
00089     // convert into ValueMap and store
00090     std::auto_ptr<ValueMap<float> > valMap(new ValueMap<float>());
00091     ValueMap<float>::Filler filler(*valMap);
00092     filler.insert(probes, values.begin(), values.end());
00093     filler.fill();
00094     iEvent.put(valMap);
00095 }
00096 
00097 
00098 typedef OtherObjectVariableComputer<reco::Candidate> OtherCandVariableComputer;
00099 //typedef OtherObjectVariableComputer<reco::Track>     OtherTrackVariableComputer;
00100 //typedef OtherObjectVariableComputer<reco::Vertex>    OtherVertexVariableComputer;
00101 
00102 #include "FWCore/Framework/interface/MakerMacros.h"
00103 DEFINE_FWK_MODULE(OtherCandVariableComputer);
00104 //DEFINE_FWK_MODULE(OtherTrackVariableComputer);
00105 //DEFINE_FWK_MODULE(OtherVertexVariableComputer);