CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CommonTools/UtilAlgos/interface/AssociatedVariableCollectionSelector.h

Go to the documentation of this file.
00001 #ifndef CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
00002 #define CommonTools_UtilAlgos_AssociatedVariableCollectionSelector_h
00003 /* \class AssociatedVariableCollectionSelector
00004  *
00005  * \author Luca Lista, INFN
00006  *
00007  * \version $Id: AssociatedVariableCollectionSelector.h,v 1.2 2010/02/20 20:55:13 wmtan Exp $
00008  *
00009  */
00010 #include "FWCore/Utilities/interface/InputTag.h"
00011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "DataFormats/Common/interface/Handle.h"
00014 #include "CommonTools/UtilAlgos/interface/SelectionAdderTrait.h"
00015 #include "CommonTools/UtilAlgos/interface/StoreContainerTrait.h"
00016 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
00017 #include "DataFormats/Common/interface/getRef.h"
00018 
00019 namespace reco {
00020   namespace modules {
00021     template<typename S> struct AssociatedVariableCollectionSelectorEventSetupInit;
00022   }
00023 }
00024 
00025 template<typename InputCollection, typename VarCollection, typename Selector,
00026          typename OutputCollection = typename ::helper::SelectedOutputCollectionTrait<InputCollection>::type, 
00027          typename StoreContainer = typename ::helper::StoreContainerTrait<OutputCollection>::type,
00028          typename RefAdder = typename ::helper::SelectionAdderTrait<InputCollection, StoreContainer>::type>
00029 class AssociatedVariableCollectionSelector {
00030 public:
00031   typedef InputCollection collection;
00032   typedef StoreContainer container;
00033   typedef Selector selector;
00034   typedef typename container::const_iterator const_iterator;
00035   AssociatedVariableCollectionSelector(const edm::ParameterSet & cfg) : 
00036     var_(cfg.template getParameter<edm::InputTag>("var")),
00037     select_(reco::modules::make<Selector>(cfg)) { }
00038   const_iterator begin() const { return selected_.begin(); }
00039   const_iterator end() const { return selected_.end(); }
00040   void select(const edm::Handle<InputCollection>& c, const edm::Event& evt, const edm::EventSetup&) {
00041     selected_.clear();    
00042     edm::Handle<VarCollection> var;
00043     evt.getByLabel(var_, var);
00044     for(size_t idx = 0; idx < c->size(); ++idx) {
00045       if (select_((*c)[idx], (*var)[edm::getRef(c,idx)])) 
00046         addRef_(selected_, c, idx);
00047     }
00048   }
00049 private:
00050   edm::InputTag var_;
00051   container selected_;
00052   selector select_;
00053   RefAdder addRef_;
00054   friend class reco::modules::AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector>;
00055 };
00056 
00057 
00058 #include "CommonTools/UtilAlgos/interface/EventSetupInitTrait.h"
00059 
00060 namespace reco {
00061   namespace modules {
00062     template<typename S>
00063     struct AssociatedVariableCollectionSelectorEventSetupInit {
00064       static void init(S & s, const edm::Event& evt, const edm::EventSetup& es) { 
00065         typedef typename EventSetupInit<typename S::selector>::type ESI;
00066         ESI::init(s.select_, evt, es);
00067       }
00068     };
00069 
00070     template<typename I, typename V, typename S, typename O, typename C, typename R>
00071     struct EventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> > {
00072       typedef AssociatedVariableCollectionSelectorEventSetupInit<AssociatedVariableCollectionSelector<I, V, S, O, C, R> > type;
00073     };
00074   }
00075 }
00076 
00077 #endif