Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:25

0001 #ifndef PhysicsTools_UtilAlgos_interface_EDFilterObjectWrapper_h
0002 #define PhysicsTools_UtilAlgos_interface_EDFilterObjectWrapper_h
0003 
0004 /**
0005   \class    EDFilterObjectWrapper EDFilterObjectWrapper.h "PhysicsTools/UtilAlgos/interface/EDFilterObjectWrapper.h"
0006   \brief    Wrapper class for a class of type BasicFilter to "convert" it into a full EDFilter
0007 
0008    This template class is a wrapper round classes of type Selector<T> and similar signature.
0009    It operates on container classes of type C which roughly satisfy std::vector template
0010    parameters.
0011 
0012    From this class the wrapper expects the following member functions:
0013 
0014    + a contructor with a const edm::ParameterSet& as input.
0015    + a filter function that operates on classes of type C::value_type
0016 
0017    the function is called within the wrapper. The wrapper translates the common class into
0018    a basic EDFilter as shown below:
0019 
0020    #include "PhysicsTools/UtilAlgos/interface/EDFilterObjectWrapper.h"
0021    #include "PhysicsTools/SelectorUtils/interface/PFJetIdSelectionFunctor.h"
0022    typedef edm::FilterWrapper<PFJetIdSelectionFunctor> PFJetIdFilter;
0023 
0024    #include "FWCore/Framework/interface/MakerMacros.h"
0025    DEFINE_FWK_MODULE(PFJetIdFilter);
0026 
0027    You can find this example in PhysicsTools/UtilAlgos/plugins/JetIDSelectionFunctorFilter.cc.
0028    In the first place the module will act as an EDProducer: it will put a new collection
0029    containing the selected objects into the event. Depending on the choice of parameter _filter_
0030    it will discard events for which the collection of selected events is empty. It will such also
0031    act as an EDFilter and thus is specified as such. The parameter _filter_ does not need to be
0032    specified.
0033 
0034    NOTE: in the current implementation this wrapper class does not support use of the EventSetup.
0035    If you want to make use of this feature we recommend you to start from an EDProducer from the
0036    very beginning and just to stay within the full framework.
0037 */
0038 
0039 #include "FWCore/Framework/interface/stream/EDFilter.h"
0040 #include "FWCore/Common/interface/EventBase.h"
0041 #include "FWCore/Framework/interface/Event.h"
0042 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0043 
0044 namespace edm {
0045 
0046   template <class T, class C>
0047   class FilterObjectWrapper : public edm::stream::EDFilter<> {
0048   public:
0049     /// some convenient typedefs. Recall that C is a container class.
0050     typename C::iterator iterator;
0051     typename C::const_iterator const_iterator;
0052 
0053     /// default contructor. Declares the output (type "C") and the filter (of type T, operates on C::value_type)
0054     FilterObjectWrapper(const edm::ParameterSet& cfg) : src_(consumes<C>(cfg.getParameter<edm::InputTag>("src"))) {
0055       filter_ = std::shared_ptr<T>(new T(cfg.getParameter<edm::ParameterSet>("filterParams")));
0056       if (cfg.exists("filter")) {
0057         doFilter_ = cfg.getParameter<bool>("filter");
0058       } else {
0059         doFilter_ = false;
0060       }
0061       produces<C>();
0062     }
0063     /// default destructor
0064     ~FilterObjectWrapper() override {}
0065     /// everything which has to be done during the event loop. NOTE: We can't use the eventSetup in FWLite so ignore it
0066     bool filter(edm::Event& event, const edm::EventSetup& eventSetup) override {
0067       // create a collection of the objects to put into the event
0068       auto objsToPut = std::make_unique<C>();
0069       // get the handle to the objects in the event.
0070       edm::Handle<C> h_c;
0071       event.getByToken(src_, h_c);
0072       // loop through and add passing value_types to the output vector
0073       for (typename C::const_iterator ibegin = h_c->begin(), iend = h_c->end(), i = ibegin; i != iend; ++i) {
0074         if ((*filter_)(*i)) {
0075           objsToPut->push_back(*i);
0076         }
0077       }
0078       // put objs in the event
0079       bool pass = !objsToPut->empty();
0080       event.put(std::move(objsToPut));
0081       if (doFilter_)
0082         return pass;
0083       else
0084         return true;
0085     }
0086 
0087   protected:
0088     /// InputTag of the input source
0089     edm::EDGetTokenT<C> src_;
0090     /// shared pointer to analysis class of type BasicAnalyzer
0091     std::shared_ptr<T> filter_;
0092     /// whether or not to filter based on size
0093     bool doFilter_;
0094   };
0095 
0096 }  // namespace edm
0097 
0098 #endif