Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:45:36

0001 #ifndef CommonTools_UtilAlgos_AssociationVectorSelector_h
0002 #define CommonTools_UtilAlgos_AssociationVectorSelector_h
0003 /* \class AssociationVectorSelector
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  * \version $Id: AssociationVectorSelector.h,v 1.2 2010/02/20 20:55:16 wmtan Exp $
0008  */
0009 
0010 #include "DataFormats/Common/interface/AssociationVector.h"
0011 #include "FWCore/Framework/interface/stream/EDProducer.h"
0012 #include "FWCore/Utilities/interface/InputTag.h"
0013 #include "CommonTools/UtilAlgos/interface/AnySelector.h"
0014 
0015 template <typename KeyRefProd, typename CVal, typename KeySelector = AnySelector, typename ValSelector = AnySelector>
0016 class AssociationVectorSelector : public edm::stream::EDProducer<> {
0017 public:
0018   AssociationVectorSelector(const edm::ParameterSet&);
0019 
0020 private:
0021   typedef edm::AssociationVector<KeyRefProd, CVal> association_t;
0022   typedef typename association_t::CKey collection_t;
0023   void produce(edm::Event&, const edm::EventSetup&) override;
0024   edm::EDGetTokenT<association_t> associationToken_;
0025   KeySelector selectKey_;
0026   ValSelector selectVal_;
0027 };
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Framework/interface/Event.h"
0031 #include "DataFormats/Common/interface/Handle.h"
0032 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0033 #include "DataFormats/Common/interface/CloneTrait.h"
0034 
0035 template <typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
0036 AssociationVectorSelector<KeyRefProd, CVal, KeySelector, ValSelector>::AssociationVectorSelector(
0037     const edm::ParameterSet& cfg)
0038     : associationToken_(consumes<association_t>(cfg.template getParameter<edm::InputTag>("association"))),
0039       selectKey_(reco::modules::make<KeySelector>(cfg, consumesCollector())),
0040       selectVal_(reco::modules::make<ValSelector>(cfg, consumesCollector())) {
0041   std::string alias(cfg.template getParameter<std::string>("@module_label"));
0042   produces<collection_t>().setBranchAlias(alias);
0043   produces<association_t>().setBranchAlias(alias + "Association");
0044 }
0045 
0046 template <typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
0047 void AssociationVectorSelector<KeyRefProd, CVal, KeySelector, ValSelector>::produce(edm::Event& evt,
0048                                                                                     const edm::EventSetup&) {
0049   using namespace edm;
0050   using namespace std;
0051   Handle<association_t> association;
0052   evt.getByToken(associationToken_, association);
0053   unique_ptr<collection_t> selected(new collection_t);
0054   vector<typename CVal::value_type> selectedValues;
0055   size_t size = association->size();
0056   selected->reserve(size);
0057   selectedValues.reserve(size);
0058   for (typename association_t::const_iterator i = association->begin(); i != association->end(); ++i) {
0059     const typename association_t::key_type& obj = *i->first;
0060     if (selectKey_(obj) && selectVal_(i->second)) {
0061       typedef typename edm::clonehelper::CloneTrait<collection_t>::type clone_t;
0062       selected->push_back(clone_t::clone(obj));
0063       selectedValues.push_back(i->second);
0064     }
0065   }
0066   // new association must be created after the
0067   // selected collection is full because it uses the size
0068   KeyRefProd ref = evt.getRefBeforePut<collection_t>();
0069   unique_ptr<association_t> selectedAssociation(new association_t(ref, selected.get()));
0070   size = selected->size();
0071   OrphanHandle<collection_t> oh = evt.put(std::move(selected));
0072   for (size_t i = 0; i != size; ++i)
0073     selectedAssociation->setValue(i, selectedValues[i]);
0074   evt.put(std::move(selectedAssociation));
0075 }
0076 
0077 #endif