CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/MuonAnalysis/MuonAssociators/plugins/HLTL1MuonMatcher.cc

Go to the documentation of this file.
00001 //
00002 // $Id: HLTL1MuonMatcher.cc,v 1.3 2010/07/12 20:56:11 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 #include "PhysicsTools/PatAlgos/plugins/PATTriggerMatchSelector.h"
00028 
00029 #include "DataFormats/PatCandidates/interface/TriggerObjectStandAlone.h"
00030 
00031 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00032 
00033 namespace pat {
00034 
00035   class HLTL1MuonMatcher : public edm::EDProducer {
00036     public:
00037       explicit HLTL1MuonMatcher(const edm::ParameterSet & iConfig);
00038       virtual ~HLTL1MuonMatcher() { }
00039 
00040       virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00041 
00042       virtual void beginRun(edm::Run & iRun, const edm::EventSetup & iSetup);
00043 
00044 
00046       bool operator()(const pat::TriggerObjectStandAlone &l1) const {
00047           if (resolveAmbiguities_ && (std::find(lockedItems_.begin(), lockedItems_.end(), &l1) != lockedItems_.end())) return false;
00048           return selector_(0,l1);
00049       }
00050     private:
00051       typedef pat::TriggerObjectStandAlone           PATPrimitive;
00052       typedef pat::TriggerObjectStandAloneCollection PATPrimitiveCollection;
00053       typedef pat::TriggerObjectStandAloneMatch      PATTriggerAssociation;
00054 
00055       L1MuonMatcherAlgo matcher_;
00056 
00058       edm::InputTag reco_, l1_;
00059 
00062       pat::PATTriggerMatchSelector<bool,PATPrimitive> selector_;
00063       bool resolveAmbiguities_;
00064 
00066       std::string labelProp_;
00067 
00069       bool writeExtraInfo_;
00070 
00072       template<typename Hand, typename T>
00073       void storeExtraInfo(edm::Event &iEvent, 
00074                      const Hand & handle,
00075                      const std::vector<T> & values,
00076                      const std::string    & label) const ;
00077 
00078       // for ambiguity resolution
00079       std::vector<const pat::TriggerObjectStandAlone *> lockedItems_;
00080   };
00081 
00082 } // namespace
00083 
00084 pat::HLTL1MuonMatcher::HLTL1MuonMatcher(const edm::ParameterSet & iConfig) :
00085     matcher_(iConfig),
00086     reco_(iConfig.getParameter<edm::InputTag>("src")),
00087     l1_(iConfig.getParameter<edm::InputTag>("matched")),
00088     selector_(iConfig),
00089     resolveAmbiguities_(iConfig.getParameter<bool>("resolveAmbiguities")),
00090     labelProp_(iConfig.getParameter<std::string>("setPropLabel")),
00091     writeExtraInfo_(iConfig.existsAs<bool>("writeExtraInfo") ? iConfig.getParameter<bool>("writeExtraInfo") : false)
00092 {
00093     produces<PATPrimitiveCollection>("propagatedReco"); // reco to muon station 2
00094     produces<PATTriggerAssociation>("propagatedReco");  // asso reco to propagated reco
00095     produces<PATTriggerAssociation>();                  // asso reco to l1
00096     if (writeExtraInfo_) {
00097         produces<edm::ValueMap<float> >("deltaR");
00098         produces<edm::ValueMap<float> >("deltaPhi");
00099     }
00100 }
00101 
00102 void 
00103 pat::HLTL1MuonMatcher::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00104     using namespace edm;
00105     using namespace std;
00106 
00107     Handle<View<reco::Candidate> > reco;
00108     Handle<PATPrimitiveCollection> l1s;
00109 
00110     iEvent.getByLabel(reco_, reco);
00111     iEvent.getByLabel(l1_, l1s);
00112 
00113     auto_ptr<PATPrimitiveCollection> propOut(new PATPrimitiveCollection());
00114     vector<int>   propMatches(reco->size(), -1);
00115     vector<int>   fullMatches(reco->size(), -1);
00116     vector<float> deltaRs(reco->size(), 999), deltaPhis(reco->size(), 999);
00117     lockedItems_.clear();
00118     for (int i = 0, n = reco->size(); i < n; ++i) {
00119         TrajectoryStateOnSurface propagated;
00120         const reco::Candidate &mu = (*reco)[i];
00121         int match = matcher_.matchGeneric(mu, *l1s, *this, deltaRs[i], deltaPhis[i], propagated);
00122         if (propagated.isValid()) {
00123             GlobalPoint pos = propagated.globalPosition();
00124             propMatches[i] = propOut->size();
00125             propOut->push_back(PATPrimitive(math::PtEtaPhiMLorentzVector(mu.pt(), pos.eta(), pos.phi(), mu.mass())));
00126             propOut->back().addFilterLabel(labelProp_);
00127             propOut->back().setCharge(mu.charge());
00128         }
00129         fullMatches[i] = match;
00130         if (match != -1) {
00131             lockedItems_.push_back(&(*l1s)[match]);
00132         }
00133     }
00134     lockedItems_.clear();
00135 
00136     OrphanHandle<PATPrimitiveCollection> propDone = iEvent.put(propOut, "propagatedReco");
00137 
00138     auto_ptr<PATTriggerAssociation> propAss(new PATTriggerAssociation(propDone));
00139     PATTriggerAssociation::Filler propFiller(*propAss);
00140     propFiller.insert(reco, propMatches.begin(), propMatches.end());
00141     propFiller.fill();
00142     iEvent.put(propAss, "propagatedReco");
00143 
00144     auto_ptr<PATTriggerAssociation> fullAss(new PATTriggerAssociation(l1s));
00145     PATTriggerAssociation::Filler fullFiller(*fullAss);
00146     fullFiller.insert(reco, fullMatches.begin(), fullMatches.end());
00147     fullFiller.fill();
00148     iEvent.put(fullAss);
00149 
00150     if (writeExtraInfo_) {
00151         storeExtraInfo(iEvent, reco, deltaRs,   "deltaR");
00152         storeExtraInfo(iEvent, reco, deltaPhis, "deltaPhi");
00153     }
00154 }
00155 
00156 template<typename Hand, typename T>
00157 void
00158 pat::HLTL1MuonMatcher::storeExtraInfo(edm::Event &iEvent,
00159                      const Hand & handle,
00160                      const std::vector<T> & values,
00161                      const std::string    & label) const {
00162     using namespace edm; using namespace std;
00163     auto_ptr<ValueMap<T> > valMap(new ValueMap<T>());
00164     typename edm::ValueMap<T>::Filler filler(*valMap);
00165     filler.insert(handle, values.begin(), values.end());
00166     filler.fill();
00167     iEvent.put(valMap, label);
00168 }
00169 
00170 
00171 void 
00172 pat::HLTL1MuonMatcher::beginRun(edm::Run & iRun, const edm::EventSetup & iSetup) {
00173     matcher_.init(iSetup);
00174 }
00175 
00176 
00177 #include "FWCore/Framework/interface/MakerMacros.h"
00178 using namespace pat;
00179 DEFINE_FWK_MODULE(HLTL1MuonMatcher);