CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/MuonAnalysis/MuonAssociators/plugins/L1MuonMatcher.cc

Go to the documentation of this file.
00001 //
00002 // $Id: L1MuonMatcher.cc,v 1.4 2011/03/31 09:59:33 gpetrucc Exp $
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/Math/interface/deltaR.h"
00019 #include "DataFormats/Math/interface/deltaPhi.h"
00020 
00021 #include "DataFormats/Common/interface/Association.h"
00022 #include "DataFormats/Common/interface/ValueMap.h"
00023 #include "DataFormats/Common/interface/Ptr.h"
00024 #include "DataFormats/Common/interface/View.h"
00025 
00026 #include "MuonAnalysis/MuonAssociators/interface/L1MuonMatcherAlgo.h"
00027 
00028 #include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
00029 
00030 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00031 
00032 namespace pat {
00033 
00034   class L1MuonMatcher : public edm::EDProducer {
00035     public:
00036       explicit L1MuonMatcher(const edm::ParameterSet & iConfig);
00037       virtual ~L1MuonMatcher() { }
00038 
00039       virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00040 
00041       virtual void beginRun(edm::Run & iRun, const edm::EventSetup & iSetup);
00042     private:
00043       typedef pat::TriggerObjectStandAlone           PATPrimitive;
00044       typedef pat::TriggerObjectStandAloneCollection PATPrimitiveCollection;
00045       typedef pat::TriggerObjectStandAloneMatch      PATTriggerAssociation;
00046 
00047       L1MuonMatcherAlgo matcher_;
00048 
00050       edm::InputTag reco_, l1_;
00051 
00053       std::string labelL1_, labelProp_;
00054 
00056       bool writeExtraInfo_;
00057 
00059       template<typename Hand, typename T>
00060       void storeExtraInfo(edm::Event &iEvent, 
00061                      const Hand & handle,
00062                      const std::vector<T> & values,
00063                      const std::string    & label) const ;
00064   };
00065 
00066 } // namespace
00067 
00068 pat::L1MuonMatcher::L1MuonMatcher(const edm::ParameterSet & iConfig) :
00069     matcher_(iConfig),
00070     reco_(iConfig.getParameter<edm::InputTag>("src")),
00071     l1_(iConfig.getParameter<edm::InputTag>("matched")),
00072     labelL1_(iConfig.getParameter<std::string>(  "setL1Label")),
00073     labelProp_(iConfig.getParameter<std::string>("setPropLabel")),
00074     writeExtraInfo_(iConfig.getParameter<bool>("writeExtraInfo"))
00075 {
00076     produces<PATPrimitiveCollection>("l1muons");        // l1 in PAT format
00077     produces<PATPrimitiveCollection>("propagatedReco"); // reco to muon station 2
00078     produces<PATTriggerAssociation>("propagatedReco");  // asso reco to propagated reco
00079     produces<PATTriggerAssociation>();                  // asso reco to l1
00080     if (writeExtraInfo_) {
00081         produces<edm::ValueMap<float> >("deltaR");
00082         produces<edm::ValueMap<float> >("deltaPhi");
00083         produces<edm::ValueMap<int>   >("quality");
00084         produces<edm::ValueMap<int>   >("bx");
00085         produces<edm::ValueMap<int>   >("isolated");
00086         produces<edm::ValueMap<reco::CandidatePtr> >();
00087         produces<edm::ValueMap<reco::CandidatePtr> >("l1ToReco");
00088     }
00089 }
00090 
00091 void 
00092 pat::L1MuonMatcher::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00093     using namespace edm;
00094     using namespace std;
00095 
00096     Handle<View<reco::Candidate> > reco;
00097     Handle<vector<l1extra::L1MuonParticle> > l1s;
00098 
00099     iEvent.getByLabel(reco_, reco);
00100     iEvent.getByLabel(l1_, l1s);
00101 
00102     auto_ptr<PATPrimitiveCollection> propOut(new PATPrimitiveCollection());
00103     auto_ptr<PATPrimitiveCollection> l1Out(new PATPrimitiveCollection());
00104     std::vector<edm::Ptr<reco::Candidate> > l1rawMatches(reco->size());
00105     vector<int>   isSelected(l1s->size(), -1); 
00106     std::vector<edm::Ptr<reco::Candidate> > whichRecoMatch(l1s->size());
00107     vector<int>   propMatches(reco->size(), -1);
00108     vector<int>   fullMatches(reco->size(), -1);
00109     vector<float> deltaRs(reco->size(), 999), deltaPhis(reco->size(), 999);
00110     vector<int>   quality(reco->size(),   0), bx(reco->size(), -999), isolated(reco->size(), -999);
00111     for (int i = 0, n = reco->size(); i < n; ++i) {
00112         TrajectoryStateOnSurface propagated;
00113         const reco::Candidate &mu = (*reco)[i];
00114         int match = matcher_.match(mu, *l1s, deltaRs[i], deltaPhis[i], propagated);
00115         if (propagated.isValid()) {
00116             GlobalPoint pos = propagated.globalPosition();
00117             propMatches[i] = propOut->size();
00118             propOut->push_back(PATPrimitive(math::PtEtaPhiMLorentzVector(mu.pt(), pos.eta(), pos.phi(), mu.mass())));
00119             propOut->back().addFilterLabel(labelProp_);
00120             propOut->back().setCharge(mu.charge());
00121         }
00122         if (match != -1) {
00123             const l1extra::L1MuonParticle & l1 = (*l1s)[match];
00124             whichRecoMatch[match] = reco->ptrAt(i); 
00125             if (isSelected[match] == -1) { // copy to output if needed
00126                 isSelected[match] = l1Out->size();
00127                 l1Out->push_back(PATPrimitive(l1.polarP4()));
00128                 l1Out->back().addFilterLabel(labelL1_);
00129                 l1Out->back().setCharge(l1.charge());
00130             }
00131             fullMatches[i] = isSelected[match]; // index in the output collection
00132             const L1MuGMTCand & gmt = l1.gmtMuonCand();
00133             quality[i]  = gmt.quality();
00134             bx[i]       = gmt.bx();
00135             isolated[i] = gmt.isol();
00136             l1rawMatches[i] = edm::Ptr<reco::Candidate>(l1s, size_t(match));
00137         }
00138     }
00139 
00140     OrphanHandle<PATPrimitiveCollection> l1Done = iEvent.put(l1Out, "l1muons");
00141     OrphanHandle<PATPrimitiveCollection> propDone = iEvent.put(propOut, "propagatedReco");
00142 
00143     auto_ptr<PATTriggerAssociation> propAss(new PATTriggerAssociation(propDone));
00144     PATTriggerAssociation::Filler propFiller(*propAss);
00145     propFiller.insert(reco, propMatches.begin(), propMatches.end());
00146     propFiller.fill();
00147     iEvent.put(propAss, "propagatedReco");
00148 
00149     auto_ptr<PATTriggerAssociation> fullAss(new PATTriggerAssociation(  l1Done));
00150     PATTriggerAssociation::Filler fullFiller(*fullAss);
00151     fullFiller.insert(reco, fullMatches.begin(), fullMatches.end());
00152     fullFiller.fill();
00153     iEvent.put(fullAss);
00154 
00155     if (writeExtraInfo_) {
00156         storeExtraInfo(iEvent, reco, deltaRs,   "deltaR");
00157         storeExtraInfo(iEvent, reco, deltaPhis, "deltaPhi");
00158         storeExtraInfo(iEvent, reco, bx,        "bx");
00159         storeExtraInfo(iEvent, reco, isolated,  "isolated");
00160         storeExtraInfo(iEvent, reco, quality,   "quality");
00161         storeExtraInfo(iEvent, reco, l1rawMatches,   "");
00162         storeExtraInfo(iEvent, l1s,  whichRecoMatch, "l1ToReco");
00163     }
00164 }
00165 
00166 template<typename Hand, typename T>
00167 void
00168 pat::L1MuonMatcher::storeExtraInfo(edm::Event &iEvent,
00169                      const Hand & 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 void 
00182 pat::L1MuonMatcher::beginRun(edm::Run & iRun, const edm::EventSetup & iSetup) {
00183     matcher_.init(iSetup);
00184 }
00185 
00186 
00187 #include "FWCore/Framework/interface/MakerMacros.h"
00188 using namespace pat;
00189 DEFINE_FWK_MODULE(L1MuonMatcher);