CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AssociationVectorSelector.h
Go to the documentation of this file.
1 #ifndef CommonTools_UtilAlgos_AssociationVectorSelector_h
2 #define CommonTools_UtilAlgos_AssociationVectorSelector_h
3 /* \class AssociationVectorSelector
4  *
5  * \author Luca Lista, INFN
6  *
7  * \version $Id: AssociationVectorSelector.h,v 1.2 2010/02/20 20:55:16 wmtan Exp $
8  */
9 
14 
15 template<typename KeyRefProd, typename CVal,
16  typename KeySelector = AnySelector, typename ValSelector = AnySelector>
18  public:
20  private:
23  void produce(edm::Event&, const edm::EventSetup&) override;
25  KeySelector selectKey_;
26  ValSelector selectVal_;
27 };
28 
34 
35 template<typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
37  associationToken_(consumes<association_t>(cfg.template getParameter<edm::InputTag>("association"))),
38  selectKey_(reco::modules::make<KeySelector>(cfg, consumesCollector())),
39  selectVal_(reco::modules::make<ValSelector>(cfg, consumesCollector())) {
40  std::string alias(cfg.template getParameter<std::string>("@module_label"));
41  produces<collection_t>().setBranchAlias(alias);
42  produces<association_t>().setBranchAlias(alias + "Association");
43 }
44 
45 template<typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
47  using namespace edm;
48  using namespace std;
49  Handle<association_t> association;
50  evt.getByToken(associationToken_, association);
51  auto_ptr<collection_t> selected(new collection_t);
52  vector<typename CVal::value_type> selectedValues;
53  size_t size = association->size();
54  selected->reserve(size);
55  selectedValues.reserve(size);
56  for(typename association_t::const_iterator i = association->begin();
57  i != association->end(); ++i) {
58  const typename association_t::key_type & obj = * i->first;
59  if(selectKey_(obj)&&selectVal_(i->second)) {
60  typedef typename edm::clonehelper::CloneTrait<collection_t>::type clone_t;
61  selected->push_back(clone_t::clone(obj));
62  selectedValues.push_back(i->second);
63  }
64  }
65  // new association must be created after the
66  // selected collection is full because it uses the size
67  KeyRefProd ref = evt.getRefBeforePut<collection_t>();
68  auto_ptr<association_t> selectedAssociation(new association_t(ref, selected.get()));
69  size = selected->size();
70  OrphanHandle<collection_t> oh = evt.put(selected);
71  for(size_t i = 0; i != size; ++i)
72  selectedAssociation->setValue(i, selectedValues[i]);
73  evt.put(selectedAssociation);
74 }
75 
76 #endif
int i
Definition: DBlmapReader.cc:9
transient_vector_type::const_iterator const_iterator
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
S make(const edm::ParameterSet &cfg)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
edm::EDGetTokenT< association_t > associationToken_
void produce(edm::Event &, const edm::EventSetup &) override
KeyRefProd::product_type CKey
RefProd< PROD > getRefBeforePut()
Definition: Event.h:128
KeyRef::value_type key_type
tuple clone
Definition: statics.py:58
edm::AssociationVector< KeyRefProd, CVal > association_t
tuple size
Write out results.
def template
Definition: svgfig.py:520
AssociationVectorSelector(const edm::ParameterSet &)