00001
00002
00003
00004
00014 #include "FWCore/Framework/interface/EDProducer.h"
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Utilities/interface/InputTag.h"
00017
00018 #include "DataFormats/Common/interface/Association.h"
00019 #include "DataFormats/Common/interface/ValueMap.h"
00020
00021 #include "DataFormats/Candidate/interface/Candidate.h"
00022
00023 #include "DataFormats/PatCandidates/interface/UserData.h"
00024
00025 #include "MuonAnalysis/MuonAssociators/interface/MatcherUsingTracksAlgorithm.h"
00026
00027
00028 namespace edm { using std::advance; }
00029
00030 namespace pat {
00031
00032 class MatcherUsingTracks : public edm::EDProducer {
00033 public:
00034 explicit MatcherUsingTracks(const edm::ParameterSet & iConfig);
00035 virtual ~MatcherUsingTracks() { }
00036
00037 virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00038
00039 private:
00041 edm::InputTag src_, matched_;
00042
00044 MatcherUsingTracksAlgorithm algo_;
00045
00047 bool dontFailOnMissingInput_;
00048 bool writeExtraPATOutput_;
00049
00051 template<typename T>
00052 void storeValueMap(edm::Event &iEvent,
00053 const edm::Handle<edm::View<reco::Candidate> > & handle,
00054 const std::vector<T> & values,
00055 const std::string & label) const ;
00056
00057 };
00058
00059 }
00060
00061 pat::MatcherUsingTracks::MatcherUsingTracks(const edm::ParameterSet & iConfig) :
00062 src_(iConfig.getParameter<edm::InputTag>("src")),
00063 matched_(iConfig.getParameter<edm::InputTag>("matched")),
00064 algo_(iConfig),
00065 dontFailOnMissingInput_(iConfig.existsAs<bool>("dontFailOnMissingInput") ? iConfig.getParameter<bool>("dontFailOnMissingInput") : false),
00066 writeExtraPATOutput_(iConfig.existsAs<bool>("writeExtraPATOutput") ? iConfig.getParameter<bool>("writeExtraPATOutput") : false)
00067 {
00068
00069 produces<edm::ValueMap<reco::CandidatePtr> >();
00070 if (writeExtraPATOutput_) {
00071
00072 produces<edm::OwnVector<pat::UserData> >();
00073 produces<edm::ValueMap<edm::Ptr<pat::UserData> > >();
00074 }
00075
00076 if (algo_.hasMetrics()) {
00077 produces<edm::ValueMap<float> >("deltaR");
00078 produces<edm::ValueMap<float> >("deltaEta");
00079 produces<edm::ValueMap<float> >("deltaPhi");
00080 produces<edm::ValueMap<float> >("deltaLocalPos");
00081 produces<edm::ValueMap<float> >("deltaPtRel");
00082 if (algo_.hasChi2()) {
00083 produces<edm::ValueMap<float> >("chi2");
00084 }
00085 } else {
00086 produces<edm::ValueMap<int> >("matched");
00087 }
00088 }
00089
00090 void
00091 pat::MatcherUsingTracks::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00092 using namespace edm;
00093 using namespace std;
00094
00095 algo_.init(iSetup);
00096
00097 Handle<View<reco::Candidate> > src, matched;
00098
00099 iEvent.getByLabel(src_, src);
00100 iEvent.getByLabel(matched_, matched);
00101
00102
00103 View<reco::Candidate>::const_iterator itsrc, edsrc;
00104 int isrc, nsrc = src->size();
00105
00106
00107 vector<int> match(nsrc, -1);
00108 vector<float> deltaRs(nsrc, 999);
00109 vector<float> deltaEtas(nsrc, 999);
00110 vector<float> deltaPhis(nsrc, 999);
00111 vector<float> deltaPtRel(nsrc, 999);
00112 vector<float> deltaLocalPos(nsrc, 999);
00113 vector<float> chi2(nsrc, 999999);
00114
00115
00116 if (!(matched.failedToGet() && dontFailOnMissingInput_)) {
00117
00118 for (itsrc = src->begin(), edsrc = src->end(), isrc = 0; itsrc != edsrc; ++itsrc, ++isrc) {
00119 match[isrc] = algo_.match(*itsrc, *matched, deltaRs[isrc], deltaEtas[isrc], deltaPhis[isrc], deltaLocalPos[isrc], deltaPtRel[isrc], chi2[isrc]);
00120 }
00121 }
00122
00123 std::vector<reco::CandidatePtr> ptrs(nsrc);
00124 for (isrc = 0; isrc < nsrc; ++isrc) {
00125 if (match[isrc] != -1) {
00126 ptrs[isrc] = matched->ptrAt(match[isrc]);
00127 }
00128 }
00129 storeValueMap<reco::CandidatePtr>(iEvent, src, ptrs, "");
00130
00131 if (writeExtraPATOutput_) {
00132 std::auto_ptr<edm::OwnVector<pat::UserData> > outUDVect(new edm::OwnVector<pat::UserData>());
00133 std::vector<int> idxUD(nsrc, -1);
00134 for (isrc = 0; isrc < nsrc; ++isrc) {
00135 if (match[isrc] != -1) {
00136 outUDVect->push_back(pat::UserData::make(ptrs[isrc]));
00137 idxUD[isrc] = outUDVect->size() - 1;
00138 }
00139 }
00140 edm::OrphanHandle<edm::OwnVector<pat::UserData> > doneUDVect = iEvent.put(outUDVect);
00141 std::vector<edm::Ptr<pat::UserData> > ptrUD(nsrc);
00142 for (isrc = 0; isrc < nsrc; ++isrc) {
00143 if (idxUD[isrc] != -1) ptrUD[isrc] = edm::Ptr<pat::UserData>(doneUDVect, idxUD[isrc]);
00144 }
00145 storeValueMap<edm::Ptr<pat::UserData> >(iEvent, src, ptrUD, "");
00146 }
00147
00148 if (algo_.hasMetrics()) {
00149 storeValueMap<float>(iEvent, src, deltaRs, "deltaR");
00150 storeValueMap<float>(iEvent, src, deltaEtas, "deltaEta");
00151 storeValueMap<float>(iEvent, src, deltaPhis, "deltaPhi");
00152 storeValueMap<float>(iEvent, src, deltaLocalPos, "deltaLocalPos");
00153 storeValueMap<float>(iEvent, src, deltaPtRel, "deltaPtRel");
00154 if (algo_.hasChi2()) {
00155 storeValueMap<float>(iEvent, src, chi2, "chi2");
00156 }
00157 } else {
00158 std::vector<int> ismatched(nsrc, 0);
00159 for (isrc = 0; isrc < nsrc; ++isrc) {
00160 ismatched[isrc] = (match[isrc] != -1);
00161 }
00162 storeValueMap<int>(iEvent, src, ismatched, "matched");
00163 }
00164 }
00165
00166 template<typename T>
00167 void
00168 pat::MatcherUsingTracks::storeValueMap(edm::Event &iEvent,
00169 const edm::Handle<edm::View<reco::Candidate> > & handle,
00170 const std::vector<T> & values,
00171 const std::string & label) const {
00172 using namespace edm; using namespace std;
00173 auto_ptr<ValueMap<T> > valMap(new ValueMap<T>());
00174 typename edm::ValueMap<T>::Filler filler(*valMap);
00175 filler.insert(handle, values.begin(), values.end());
00176 filler.fill();
00177 iEvent.put(valMap, label);
00178 }
00179
00180
00181 #include "FWCore/Framework/interface/MakerMacros.h"
00182 using namespace pat;
00183 DEFINE_FWK_MODULE(MatcherUsingTracks);