CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/RecoBTag/SoftLepton/interface/GenericSelectorByValueMap.h

Go to the documentation of this file.
00001 #ifndef GenericSelectorByValueMap_h
00002 #define GenericSelectorByValueMap_h
00003 
00008 #include "FWCore/Framework/interface/Frameworkfwd.h"
00009 #include "FWCore/Framework/interface/EDProducer.h"
00010 #include "FWCore/Framework/interface/Event.h"
00011 #include "FWCore/Framework/interface/EventSetup.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 #include "FWCore/Utilities/interface/InputTag.h"
00014 
00015 namespace edm {
00016 
00017 namespace details {
00018 
00019   // which type should be used in edm::ParameterSet::getParameter<_> to read a parameter compatible with T ?
00020 
00021   // most types can use themselves
00022   template <typename C>
00023   struct CompatibleConfigurationType {
00024     typedef C type;
00025   };
00026 
00027   // "float" is not allowed, as it conflicts with "double"
00028   template <>
00029   struct CompatibleConfigurationType<float> {
00030     typedef double type;
00031   };
00032 
00033 } // namespace details;
00034 
00035 template <typename T, typename C>
00036 class GenericSelectorByValueMap : public edm::EDProducer {
00037 public:
00038   explicit GenericSelectorByValueMap(edm::ParameterSet const & config);
00039 
00040 private:
00041   typedef T candidate_type;
00042   typedef C selection_type;
00043   typedef typename details::template CompatibleConfigurationType<selection_type>::type cut_type;
00044 
00045   void produce(edm::Event & event, edm::EventSetup const & setup);
00046 
00047   edm::InputTag m_electrons;
00048   edm::InputTag m_selection;
00049 
00050   cut_type m_cut;
00051 };
00052 
00053 } // namespace edm;
00054 
00055 //------------------------------------------------------------------------------
00056 
00057 #include <vector>
00058 #include <memory>
00059 
00060 #include "DataFormats/Common/interface/Handle.h"
00061 #include "DataFormats/Common/interface/ValueMap.h"
00062 #include "DataFormats/Common/interface/View.h"
00063 #include "DataFormats/Common/interface/Ptr.h"
00064 #include "DataFormats/Common/interface/PtrVector.h"
00065 #include "DataFormats/Common/interface/RefToBaseVector.h"
00066 
00067 //------------------------------------------------------------------------------
00068 
00069 namespace edm {
00070 
00071 template <typename T, typename C>
00072 GenericSelectorByValueMap<T,C>::GenericSelectorByValueMap(edm::ParameterSet const & config) :
00073   m_electrons(config.getParameter<edm::InputTag>("input")),
00074   m_selection(config.getParameter<edm::InputTag>("selection")),
00075   m_cut(config.getParameter<cut_type>("cut"))
00076 {
00077   // register the product
00078   produces<edm::RefToBaseVector<candidate_type> >();
00079 }
00080 
00081 //------------------------------------------------------------------------------
00082 
00083 template <typename T, typename C>
00084 void GenericSelectorByValueMap<T, C>::produce(edm::Event & event, const edm::EventSetup & setup)
00085 {
00086   std::auto_ptr<edm::RefToBaseVector<candidate_type> > candidates(new edm::RefToBaseVector<candidate_type>());
00087 
00088   // read the collection of GsfElectrons from the Event
00089   edm::Handle<edm::View<candidate_type> > h_electrons;
00090   event.getByLabel(m_electrons, h_electrons);
00091   edm::View<candidate_type> const & electrons = * h_electrons;
00092 
00093   // read the selection map from the Event
00094   edm::Handle<edm::ValueMap<selection_type> > h_selection;
00095   event.getByLabel(m_selection, h_selection);
00096   edm::ValueMap<selection_type> const & selectionMap = * h_selection;
00097  
00098   for (unsigned int i = 0; i < electrons.size(); ++i) {
00099     edm::RefToBase<candidate_type> ptr = electrons.refAt(i);
00100     if (selectionMap[ptr] > m_cut)
00101      candidates->push_back(ptr); 
00102   }
00103 
00104   // put the product in the event
00105   event.put(candidates);
00106 }
00107 
00108 } // namespace edm;
00109 
00110 #endif // GenericSelectorByValueMap_h