Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:24:35

0001 #ifndef GenericSelectorByValueMap_h
0002 #define GenericSelectorByValueMap_h
0003 
0004 /** \class GenericSelectorByValueMap
0005  *
0006  */
0007 
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/global/EDProducer.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/Utilities/interface/InputTag.h"
0014 #include "DataFormats/Common/interface/ValueMap.h"
0015 #include "DataFormats/Common/interface/View.h"
0016 
0017 namespace edm {
0018 
0019   namespace details {
0020 
0021     // which type should be used in edm::ParameterSet::getParameter<_> to read a parameter compatible with T ?
0022 
0023     // most types can use themselves
0024     template <typename C>
0025     struct CompatibleConfigurationType {
0026       typedef C type;
0027     };
0028 
0029     // "float" is not allowed, as it conflicts with "double"
0030     template <>
0031     struct CompatibleConfigurationType<float> {
0032       typedef double type;
0033     };
0034 
0035   }  // namespace details
0036 
0037   template <typename T, typename C>
0038   class GenericSelectorByValueMap : public edm::global::EDProducer<> {
0039   public:
0040     explicit GenericSelectorByValueMap(edm::ParameterSet const& config);
0041 
0042   private:
0043     typedef T candidate_type;
0044     typedef C selection_type;
0045     typedef typename details::template CompatibleConfigurationType<selection_type>::type cut_type;
0046 
0047     void produce(edm::StreamID, edm::Event& event, edm::EventSetup const& setup) const override;
0048 
0049     edm::EDGetTokenT<edm::View<candidate_type>> token_electrons;
0050     edm::EDGetTokenT<edm::ValueMap<selection_type>> token_selection;
0051 
0052     cut_type m_cut;
0053   };
0054 
0055 }  // namespace edm
0056 
0057 //------------------------------------------------------------------------------
0058 
0059 #include <vector>
0060 #include <memory>
0061 
0062 #include "DataFormats/Common/interface/Handle.h"
0063 #include "DataFormats/Common/interface/Ptr.h"
0064 #include "DataFormats/Common/interface/PtrVector.h"
0065 #include "DataFormats/Common/interface/RefToBaseVector.h"
0066 
0067 //------------------------------------------------------------------------------
0068 
0069 namespace edm {
0070 
0071   template <typename T, typename C>
0072   GenericSelectorByValueMap<T, C>::GenericSelectorByValueMap(edm::ParameterSet const& config)
0073       : token_electrons(consumes<edm::View<candidate_type>>(config.getParameter<edm::InputTag>("input"))),
0074         token_selection(consumes<edm::ValueMap<selection_type>>(config.getParameter<edm::InputTag>("selection"))),
0075         m_cut(config.getParameter<cut_type>("cut")) {
0076     // register the product
0077     produces<edm::RefToBaseVector<candidate_type>>();
0078   }
0079 
0080   //------------------------------------------------------------------------------
0081 
0082   template <typename T, typename C>
0083   void GenericSelectorByValueMap<T, C>::produce(edm::StreamID, edm::Event& event, const edm::EventSetup& setup) const {
0084     auto candidates = std::make_unique<edm::RefToBaseVector<candidate_type>>();
0085 
0086     // read the collection of GsfElectrons from the Event
0087     edm::Handle<edm::View<candidate_type>> h_electrons;
0088     event.getByToken(token_electrons, h_electrons);
0089     edm::View<candidate_type> const& electrons = *h_electrons;
0090 
0091     // read the selection map from the Event
0092     edm::Handle<edm::ValueMap<selection_type>> h_selection;
0093     event.getByToken(token_selection, h_selection);
0094     edm::ValueMap<selection_type> const& selectionMap = *h_selection;
0095 
0096     for (unsigned int i = 0; i < electrons.size(); ++i) {
0097       edm::RefToBase<candidate_type> ptr = electrons.refAt(i);
0098       if (selectionMap[ptr] > m_cut)
0099         candidates->push_back(ptr);
0100     }
0101 
0102     // put the product in the event
0103     event.put(std::move(candidates));
0104   }
0105 
0106 }  // namespace edm
0107 
0108 #endif  // GenericSelectorByValueMap_h