CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/SUSYBSMAnalysis/Skimming/plugins/UpdatedMuonInnerTrackRef.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include "FWCore/Framework/interface/Frameworkfwd.h"
00003 #include "FWCore/Framework/interface/MakerMacros.h"
00004 #include "FWCore/Framework/interface/EDProducer.h"
00005 #include "FWCore/Framework/interface/Event.h"
00006 
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "DataFormats/Common/interface/View.h"
00010 #include "DataFormats/MuonReco/interface/Muon.h"
00011 #include "DataFormats/MuonReco/interface/MuonFwd.h"
00012 #include "DataFormats/TrackReco/interface/Track.h"
00013 
00014 #include "CommonTools/UtilAlgos/interface/DeltaR.h"
00015 
00016 //
00017 // class declaration
00018 //
00019 class UpdatedMuonInnerTrackRef : public edm::EDProducer {
00020    public:
00021       explicit UpdatedMuonInnerTrackRef(const edm::ParameterSet&);
00022       ~UpdatedMuonInnerTrackRef();
00023 
00024    private:
00025       virtual void beginJob() ;
00026       virtual void produce(edm::Event&, const edm::EventSetup&);
00027       virtual void endJob() ;
00028 
00029       reco::TrackRef findNewRef(reco::TrackRef oldTrackRef, edm::Handle<reco::TrackCollection>& newTrackCollection);
00030 
00031       edm::InputTag muonTag_;
00032       edm::InputTag oldTrackTag_;
00033       edm::InputTag newTrackTag_;
00034 
00035       double maxInvPtDiff;
00036       double minDR;
00037 };
00038 
00039 
00041 UpdatedMuonInnerTrackRef::UpdatedMuonInnerTrackRef(const edm::ParameterSet& pset)
00042 {
00043    // What is being produced
00044    produces<std::vector<reco::Muon> >();
00045 
00046     // Input products
00047    muonTag_     = pset.getUntrackedParameter<edm::InputTag> ("MuonTag"    , edm::InputTag("muons"));
00048    oldTrackTag_ = pset.getUntrackedParameter<edm::InputTag> ("OldTrackTag", edm::InputTag("generalTracks"));
00049    newTrackTag_ = pset.getUntrackedParameter<edm::InputTag> ("NewTrackTag", edm::InputTag("generalTracksSkim"));
00050 
00051     // matching criteria products
00052    maxInvPtDiff=pset.getUntrackedParameter<double>("maxInvPtDiff", 0.005);
00053    minDR=pset.getUntrackedParameter<double>("minDR", 0.1);
00054 } 
00055 
00057 UpdatedMuonInnerTrackRef::~UpdatedMuonInnerTrackRef(){
00058 }
00059 
00061 void UpdatedMuonInnerTrackRef::beginJob() {
00062 }
00063 
00065 void UpdatedMuonInnerTrackRef::endJob(){
00066 }
00067 
00069 void UpdatedMuonInnerTrackRef::produce(edm::Event& ev, const edm::EventSetup& iSetup)
00070 {
00071       // Muon collection
00072       edm::Handle<edm::View<reco::Muon> > muonCollectionHandle;
00073       if (!ev.getByLabel(muonTag_, muonCollectionHandle)) {
00074             edm::LogError("") << ">>> Muon collection does not exist !!!";
00075             return;
00076       }
00077 
00078 
00079       edm::Handle<reco::TrackCollection> oldTrackCollection;
00080       if (!ev.getByLabel(oldTrackTag_, oldTrackCollection)) {
00081             edm::LogError("") << ">>> Old Track collection does not exist !!!";
00082             return;
00083       }
00084 
00085       edm::Handle<reco::TrackCollection> newTrackCollection;
00086       if (!ev.getByLabel(newTrackTag_, newTrackCollection)) {
00087             edm::LogError("") << ">>> New Track collection does not exist !!!";
00088             return;
00089       }
00090 
00091       unsigned int muonCollectionSize = muonCollectionHandle->size();
00092       std::auto_ptr<reco::MuonCollection> newmuons (new reco::MuonCollection);
00093 
00094 
00095       for (unsigned int i=0; i<muonCollectionSize; i++) {
00096             edm::RefToBase<reco::Muon> mu = muonCollectionHandle->refAt(i);
00097             reco::Muon* newmu = mu->clone();
00098 
00099             if(mu->innerTrack().isNonnull()){ 
00100                reco::TrackRef newTrackRef = findNewRef(mu->innerTrack(), newTrackCollection);
00101 /*               printf(" %6.2f %+6.2f %+6.2f --> ",mu->innerTrack()->pt (), mu->innerTrack()->eta(), mu->innerTrack()->phi());
00102                if(newTrackRef.isNonnull()){
00103                   printf(" %6.2f %+6.2f %+6.2f\n",newTrackRef->pt (), newTrackRef->eta(), newTrackRef->phi());
00104                }else{
00105                   printf("\n");
00106                }
00107 */
00108                newmu->setInnerTrack(newTrackRef);
00109             }
00110 
00111             newmuons->push_back(*newmu);
00112       }
00113 
00114       ev.put(newmuons);
00115 }
00116 
00117 reco::TrackRef UpdatedMuonInnerTrackRef::findNewRef(reco::TrackRef oldTrackRef, edm::Handle<reco::TrackCollection>& newTrackCollection){
00118    float dRMin=1000; int found = -1;   
00119    for(unsigned int i=0;i<newTrackCollection->size();i++){
00120       reco::TrackRef newTrackRef  = reco::TrackRef( newTrackCollection, i );
00121       if(newTrackRef.isNull())continue;
00122 
00123       if( fabs( (1.0/newTrackRef->pt())-(1.0/oldTrackRef->pt())) > maxInvPtDiff) continue;
00124       float dR = deltaR(newTrackRef->momentum(), oldTrackRef->momentum());
00125       if(dR <= minDR && dR < dRMin){ dRMin=dR; found = i;}
00126    }
00127 
00128    if(found>=0){     
00129       return reco::TrackRef( newTrackCollection, found );
00130    }else{
00131       return reco::TrackRef();
00132    }
00133 }
00134 
00135 DEFINE_FWK_MODULE(UpdatedMuonInnerTrackRef);
00136 
00137 
00138 
00139