CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoTauTag/TauTagTools/interface/CastedRefProducer.h

Go to the documentation of this file.
00001 #ifndef RecoTauTag_TauTagTools_CastedRefProducer_h
00002 #define RecoTauTag_TauTagTools_CastedRefProducer_h
00003 
00004 /*
00005  * Taking a View<BaseType> as input, create a output collection of Refs to the
00006  * original collection with the desired type.
00007  *
00008  * Author: Evan K. Friis (UC Davis)
00009  *
00010  * Based on CommonTools/CandAlgos/interface/ShallowClone.h
00011  *
00012  */
00013 
00014 #include "FWCore/Framework/interface/EDProducer.h"
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/Utilities/interface/InputTag.h"
00018 #include "DataFormats/Common/interface/View.h"
00019 #include "DataFormats/Common/interface/Ref.h"
00020 #include "DataFormats/Common/interface/RefToBaseVector.h"
00021 
00022 namespace reco { namespace tautools {
00023 
00024 template<typename DerivedCollection, typename BaseType>
00025 class CastedRefProducer : public edm::EDProducer {
00026   public:
00027     typedef typename edm::RefToBaseVector<BaseType> base_ref_vector;
00028     typedef typename base_ref_vector::value_type base_ref;
00029     typedef typename edm::Ref<DerivedCollection> derived_ref;
00030     typedef typename edm::RefVector<DerivedCollection> OutputCollection;
00032     explicit CastedRefProducer( const edm::ParameterSet& pset)
00033         :src_(pset.template getParameter<edm::InputTag>("src")) {
00034           produces<OutputCollection>();
00035         }
00037     ~CastedRefProducer(){};
00039     virtual void produce(edm::Event& evt, const edm::EventSetup& es) {
00040       // Output collection
00041       std::auto_ptr<OutputCollection> coll(new OutputCollection());
00042       // Get input
00043       edm::Handle<edm::View<BaseType> > input;
00044       evt.getByLabel(src_, input);
00045       // Get references to the base
00046       const base_ref_vector &baseRefs = input->refVector();
00047       for(size_t i = 0; i < baseRefs.size(); ++i) {
00048         // Cast the base class to the derived class
00049         base_ref base = baseRefs.at(i);
00050         derived_ref derived = base.template castTo<derived_ref>();
00051         coll->push_back(derived);
00052       }
00053       evt.put( coll );
00054     }
00055   private:
00057     edm::InputTag src_;
00058 };
00059 
00060 }}
00061 
00062 #endif