Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:15

0001 #ifndef CommonTools_UtilAlgos_AssociationMapOneToOne2Association_h
0002 #define CommonTools_UtilAlgos_AssociationMapOneToOne2Association_h
0003 /* \class AssociationMapOneToOne2Association
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  * \version $Id: AssociationMapOneToOne2Association.h,v 1.2 2010/02/20 20:55:14 wmtan Exp $
0008  */
0009 
0010 #include "DataFormats/Common/interface/AssociationMap.h"
0011 #include "DataFormats/Common/interface/OneToOne.h"
0012 #include "DataFormats/Common/interface/Association.h"
0013 #include "FWCore/Framework/interface/global/EDProducer.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 
0016 template <typename CKey, typename CVal>
0017 class AssociationMapOneToOne2Association : public edm::global::EDProducer<> {
0018 public:
0019   AssociationMapOneToOne2Association(const edm::ParameterSet&);
0020 
0021 private:
0022   typedef edm::AssociationMap<edm::OneToOne<CKey, CVal> > am_t;
0023   typedef edm::Association<CVal> as_t;
0024   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0025   edm::EDGetTokenT<am_t> am_;
0026 };
0027 
0028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "DataFormats/Common/interface/Handle.h"
0031 #include "CommonTools/UtilAlgos/interface/ParameterAdapter.h"
0032 #include "DataFormats/Common/interface/CloneTrait.h"
0033 
0034 template <typename CKey, typename CVal>
0035 AssociationMapOneToOne2Association<CKey, CVal>::AssociationMapOneToOne2Association(const edm::ParameterSet& cfg)
0036     : am_(consumes<am_t>(cfg.template getParameter<edm::InputTag>("src"))) {
0037   produces<as_t>();
0038 }
0039 
0040 template <typename CKey, typename CVal>
0041 void AssociationMapOneToOne2Association<CKey, CVal>::produce(edm::StreamID,
0042                                                              edm::Event& evt,
0043                                                              const edm::EventSetup&) const {
0044   using namespace edm;
0045   using namespace std;
0046   Handle<am_t> am;
0047   evt.getByToken(am_, am);
0048 
0049   unique_ptr<as_t> as(new as_t);
0050   typename as_t::Filler filler(*as);
0051   filler.fill();
0052   size_t size = am->size();
0053   vector<int> indices;
0054   indices.reserve(size);
0055   for (typename am_t::const_iterator i = am->begin(); i != am->end(); ++i) {
0056     indices.push_back(i->val.key());
0057   }
0058   filler.insert(am->refProd().key, indices.begin(), indices.end());
0059   evt.put(std::move(as));
0060 }
0061 
0062 #endif