CMS 3D CMS Logo

AssociationVectorSelector.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_UtilAlgos_AssociationVectorSelector_h
00002 #define PhysicsTools_UtilAlgos_AssociationVectorSelector_h
00003 /* \class AssociationVectorSelector
00004  *
00005  * \author Luca Lista, INFN
00006  *
00007  * \version $Id: AssociationVectorSelector.h,v 1.3 2008/01/21 09:22:50 llista Exp $
00008  */
00009 
00010 #include "DataFormats/Common/interface/AssociationVector.h"
00011 #include "FWCore/Framework/interface/EDProducer.h"
00012 #include "FWCore/ParameterSet/interface/InputTag.h"
00013 #include "PhysicsTools/UtilAlgos/interface/AnySelector.h"
00014 
00015 template<typename KeyRefProd, typename CVal, 
00016          typename KeySelector = AnySelector, typename ValSelector = AnySelector>
00017 class AssociationVectorSelector : public edm::EDProducer {
00018  public:
00019   AssociationVectorSelector(const edm::ParameterSet&);
00020  private:
00021   typedef edm::AssociationVector<KeyRefProd, CVal> association_t;
00022   typedef typename association_t::CKey collection_t;
00023   void produce(edm::Event&, const edm::EventSetup&);
00024   edm::InputTag association_;
00025   KeySelector selectKey_;
00026   ValSelector selectVal_;
00027 };
00028 
00029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00030 #include "FWCore/Framework/interface/Event.h"
00031 #include "DataFormats/Common/interface/Handle.h"
00032 #include "PhysicsTools/UtilAlgos/interface/ParameterAdapter.h"
00033 #include "DataFormats/Common/interface/CloneTrait.h"
00034 
00035 template<typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
00036 AssociationVectorSelector<KeyRefProd, CVal, KeySelector, ValSelector>::AssociationVectorSelector(const edm::ParameterSet& cfg) :
00037   association_(cfg.template getParameter<edm::InputTag>("association")),
00038   selectKey_(reco::modules::make<KeySelector>(cfg)),
00039   selectVal_(reco::modules::make<ValSelector>(cfg)) {
00040   std::string alias(cfg.template getParameter<std::string>("@module_label"));
00041   produces<collection_t>().setBranchAlias(alias);
00042   produces<association_t>().setBranchAlias(alias + "Association");
00043 }
00044 
00045 template<typename KeyRefProd, typename CVal, typename KeySelector, typename ValSelector>
00046 void AssociationVectorSelector<KeyRefProd, CVal, KeySelector, ValSelector>::produce(edm::Event& evt, const edm::EventSetup&) {
00047   using namespace edm;
00048   using namespace std;
00049   Handle<association_t> association;
00050   evt.getByLabel(association_, association);
00051   auto_ptr<collection_t> selected(new collection_t);  
00052   vector<typename CVal::value_type> selectedValues;
00053   size_t size = association->size();
00054   selected->reserve(size);
00055   selectedValues.reserve(size);
00056   for(typename association_t::const_iterator i = association->begin();
00057       i != association->end(); ++i) {
00058     const typename association_t::key_type & obj = * i->first;
00059     if(selectKey_(obj)&&selectVal_(i->second)) {
00060       typedef typename edm::clonehelper::CloneTrait<collection_t>::type clone_t;
00061       selected->push_back(clone_t::clone(obj));
00062       selectedValues.push_back(i->second);
00063     }
00064   }
00065   // new association must be created after the
00066   // selected collection is full because it uses the size
00067   KeyRefProd ref = evt.getRefBeforePut<collection_t>();
00068   auto_ptr<association_t> selectedAssociation(new association_t(ref, selected.get()));
00069   size = selected->size();
00070   OrphanHandle<collection_t> oh = evt.put(selected);
00071   for(size_t i = 0; i != size; ++i)
00072     selectedAssociation->setValue(i, selectedValues[i]);
00073   evt.put(selectedAssociation);
00074 }
00075 
00076 #endif

Generated on Tue Jun 9 17:42:06 2009 for CMSSW by  doxygen 1.5.4