00001 #ifndef RecoAlgos_ConstrainedFitCandProducer_h
00002 #define RecoAlgos_ConstrainedFitCandProducer_h
00003
00004
00005
00006
00007
00008 #include "FWCore/Framework/interface/EDProducer.h"
00009 #include "FWCore/ParameterSet/interface/InputTag.h"
00010 #include "PhysicsTools/UtilAlgos/interface/ParameterAdapter.h"
00011 #include "PhysicsTools/UtilAlgos/interface/EventSetupInitTrait.h"
00012 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00013 #include "SimGeneral/HepPDTRecord/interface/PdtEntry.h"
00014 #include <vector>
00015
00016 template<typename Fitter,
00017 typename InputCollection = reco::CandidateCollection,
00018 typename OutputCollection = InputCollection,
00019 typename Init = typename ::reco::modules::EventSetupInit<Fitter>::type>
00020 class ConstrainedFitCandProducer : public edm::EDProducer {
00021 public:
00022 explicit ConstrainedFitCandProducer(const edm::ParameterSet &);
00023
00024 private:
00025 edm::InputTag src_;
00026 bool setLongLived_;
00027 bool setPdgId_;
00028 int pdgId_;
00029 Fitter fitter_;
00030 void produce(edm::Event &, const edm::EventSetup &);
00031 };
00032
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00035 #include "DataFormats/Common/interface/Handle.h"
00036 #include "FWCore/Framework/interface/ESHandle.h"
00037 #include "FWCore/Framework/interface/Event.h"
00038 #include "DataFormats/Candidate/interface/VertexCompositeCandidate.h"
00039 #include "FWCore/Framework/interface/EventSetup.h"
00040 #include <algorithm>
00041
00042 template<typename Fitter, typename InputCollection, typename OutputCollection, typename Init>
00043 ConstrainedFitCandProducer<Fitter, InputCollection, OutputCollection, Init>::ConstrainedFitCandProducer(const edm::ParameterSet & cfg) :
00044 src_(cfg.template getParameter<edm::InputTag>("src")),
00045 setLongLived_(false), setPdgId_(false),
00046 fitter_(reco::modules::make<Fitter>(cfg)) {
00047 using namespace std;
00048 produces<OutputCollection>();
00049 string alias( cfg.getParameter<std::string>("@module_label"));
00050 const string setLongLived("setLongLived");
00051 vector<string> vBoolParams = cfg.template getParameterNamesForType<bool>();
00052 bool found = find(vBoolParams.begin(), vBoolParams.end(), setLongLived) != vBoolParams.end();
00053 if(found) setLongLived_ = cfg.template getParameter<bool>("setLongLived");
00054 const string setPdgId("setPdgId");
00055 vector<string> vIntParams = cfg.getParameterNamesForType<int>();
00056 found = find(vIntParams.begin(), vIntParams.end(), setPdgId) != vIntParams.end();
00057 if(found) { setPdgId_ = true; pdgId_ = cfg.getParameter<int>("setPdgId"); }
00058 }
00059
00060 namespace reco {
00061 namespace fitHelper {
00062 template<typename C>
00063 struct Adder {
00064 static void add(std::auto_ptr<C> & c, std::auto_ptr<reco::VertexCompositeCandidate> t) { c->push_back(*t); }
00065 };
00066
00067 template<typename T>
00068 struct Adder<edm::OwnVector<T> > {
00069 static void add(std::auto_ptr<edm::OwnVector<T> > & c, std::auto_ptr<reco::VertexCompositeCandidate> t) { c->push_back(t); }
00070 };
00071
00072 template<typename C>
00073 inline void add(std::auto_ptr<C> & c, std::auto_ptr<reco::VertexCompositeCandidate> t) {
00074 Adder<C>::add(c, t);
00075 }
00076 }
00077 }
00078
00079 template<typename Fitter, typename InputCollection, typename OutputCollection, typename Init>
00080 void ConstrainedFitCandProducer<Fitter, InputCollection, OutputCollection, Init>::produce(edm::Event & evt, const edm::EventSetup & es) {
00081 using namespace edm;
00082 using namespace reco;
00083 using namespace std;
00084 Init::init(fitter_, evt, es);
00085 Handle<InputCollection> cands;
00086 evt.getByLabel(src_, cands);
00087 auto_ptr<OutputCollection> fitted(new OutputCollection);
00088 fitted->reserve(cands->size());
00089 for(typename InputCollection::const_iterator c = cands->begin(); c != cands->end(); ++ c) {
00090 std::auto_ptr<VertexCompositeCandidate> clone(new VertexCompositeCandidate(*c));
00091 fitter_.set(*clone);
00092 if(setLongLived_) clone->setLongLived();
00093 if(setPdgId_) clone->setPdgId(pdgId_);
00094 fitHelper::add(fitted, clone);
00095 }
00096 evt.put(fitted);
00097 }
00098
00099 #endif