CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/MuonAnalysis/MomentumScaleCalibration/plugins/TestCorrection.cc

Go to the documentation of this file.
00001 #ifndef TESTCORRECTION_CC
00002 #define TESTCORRECTION_CC
00003 
00004 #include "TestCorrection.h"
00005 
00006 #include "TCanvas.h"
00007 #include "TLegend.h"
00008 
00009 //
00010 // constants, enums and typedefs
00011 //
00012 
00013 //
00014 // static data member definitions
00015 //
00016 
00017 //
00018 // constructors and destructor
00019 //
00020 TestCorrection::TestCorrection(const edm::ParameterSet& iConfig) :
00021   MuScleFitBase( iConfig )
00022 {
00023   //now do what ever initialization is needed
00024   TFile * outputFile = new TFile(theRootFileName_.c_str(), "RECREATE");
00025   theFiles_.push_back(outputFile);
00026   // outputFile_ = new TFile(theRootFileName_.c_str(), "RECREATE");
00027   // outputFile_->cd();
00028   outputFile->cd();
00029   MuScleFitUtils::resfind = iConfig.getParameter<std::vector<int> >("resfind");
00030   fillHistoMap(outputFile, 0);
00031   uncorrectedPt_ = new TH1F("uncorrectedPt", "uncorrected pt", 1000, 0, 100);
00032   uncorrectedPtVsEta_ = new TProfile("uncorrectedPtVsEta", "uncorrected pt vs eta", 1000, 0, 100, -3., 3.);
00033   correctedPt_ = new TH1F("correctedPt", "corrected pt", 1000, 0, 100);
00034   correctedPtVsEta_ = new TProfile("correctedPtVsEta", "corrected pt vs eta", 1000, 0, 100, -3., 3.);
00035   eventCounter_ = 0;
00036   // Create the corrector and set the parameters
00037   corrector_.reset(new MomentumScaleCorrector( iConfig.getUntrackedParameter<std::string>("CorrectionsIdentifier") ) );
00038   std::cout << "corrector_ = " << &*corrector_ << std::endl;
00039   resolution_.reset(new ResolutionFunction(iConfig.getUntrackedParameter<std::string>("ResolutionsIdentifier") ) );
00040   std::cout << "resolution_ = " << &*resolution_ << std::endl;
00041   background_.reset(new BackgroundFunction(iConfig.getUntrackedParameter<std::string>("BackgroundIdentifier") ) );
00042 
00043   // Initialize the parameters of MuScleFitUtils from those saved in the functions.
00044   // MuScleFitUtils::parScale = corrector_.getFunction(0)->parameters();
00045   MuScleFitUtils::resolutionFunction = resolution_->function(0);
00046   MuScleFitUtils::resolutionFunctionForVec = resolutionFunctionVecService( resolution_->identifiers()[0] );
00047 
00048   MuScleFitUtils::parResol = resolution_->parameters();
00049 }
00050 
00051 TestCorrection::~TestCorrection()
00052 {
00053   theFiles_[0]->cd();
00054   TCanvas canvas("ptComparison","pt comparison", 1000, 800);
00055   canvas.cd();
00056   uncorrectedPt_->GetXaxis()->SetTitle("Pt(GeV)");
00057   correctedPt_->SetLineColor(kRed);
00058   TLegend * legend = new TLegend(0.7,0.71,0.98,1.);
00059   legend->SetTextSize(0.02);
00060   legend->SetFillColor(0); // Have a white background
00061   legend->AddEntry(uncorrectedPt_, "original pt");
00062   legend->AddEntry(correctedPt_, "corrected pt");
00063   uncorrectedPt_->Draw();
00064   correctedPt_->Draw("same");
00065   legend->Draw("same");
00066 
00067   canvas.Write();
00068   uncorrectedPt_->Write();
00069   uncorrectedPtVsEta_->Write();
00070   correctedPt_->Write();
00071   correctedPtVsEta_->Write();
00072 
00073   writeHistoMap(0);
00074   theFiles_[0]->Close();
00075 
00076   std::cout << "Total analyzed events = " << eventCounter_ << std::endl;
00077 }
00078 
00079 //
00080 // member functions
00081 //
00082 
00083 // ------------ method called to for each event  ------------
00084 void TestCorrection::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
00085   using namespace edm;
00086 
00087   initialize(iSetup);
00088 
00089   ++eventCounter_;
00090   if ( eventCounter_%100 == 0 ) {
00091     std::cout << "Event number " << eventCounter_ << std::endl;
00092   }
00093 
00094   // Take the reco-muons, depending on the type selected in the cfg
00095   // --------------------------------------------------------------
00096 
00097   std::vector<reco::LeafCandidate> muons;
00098 
00099   if (theMuonType_==1) { // GlobalMuons
00100     Handle<reco::MuonCollection> glbMuons;
00101     iEvent.getByLabel (theMuonLabel_, glbMuons);
00102     muons = fillMuonCollection(*glbMuons);
00103   }
00104   else if (theMuonType_==2) { // StandaloneMuons
00105     Handle<reco::TrackCollection> saMuons;
00106     iEvent.getByLabel (theMuonLabel_, saMuons);
00107     muons = fillMuonCollection(*saMuons);
00108   }
00109   else if (theMuonType_==3) { // Tracker tracks
00110     Handle<reco::TrackCollection> tracks;
00111     iEvent.getByLabel (theMuonLabel_, tracks);
00112     muons = fillMuonCollection(*tracks);
00113   }
00114 
00115   // Find the two muons from the resonance, and set ResFound bool
00116   // ------------------------------------------------------------
00117   std::pair <reco::Particle::LorentzVector, reco::Particle::LorentzVector> recMuFromBestRes = 
00118     MuScleFitUtils::findBestRecoRes (muons);
00119   if (MuScleFitUtils::ResFound) {
00120     MuScleFitUtils::SavedPair.push_back( std::make_pair (recMuFromBestRes.first, recMuFromBestRes.second) );
00121   } else {
00122     MuScleFitUtils::SavedPair.push_back( std::make_pair (lorentzVector(0.,0.,0.,0.), lorentzVector(0.,0.,0.,0.)) );
00123   }
00124 
00125   // If resonance found, do the hard work
00126   // ------------------------------------
00127   if (MuScleFitUtils::ResFound) {
00128 
00129     // Find weight and reference mass for this muon pair
00130     // -------------------------------------------------
00131     // double weight = MuScleFitUtils::computeWeight ((recMu1+recMu2).mass());
00132 
00133     // Use the correction function to correct the pt scale of the muons. Note that this takes into
00134     // account the corrections from all iterations.
00135     lorentzVector recMu1 = correctMuon(recMu1);
00136     lorentzVector recMu2 = correctMuon(recMu2);
00137 
00138     reco::Particle::LorentzVector bestRecRes (recMu1+recMu2);
00139 
00140     //Fill histograms
00141     //------------------
00142     mapHisto_["hRecBestMu"]->Fill(recMu1);
00143     if ((std::abs(recMu1.eta())<2.5) && (recMu1.pt()>2.5)) {
00144       mapHisto_["hRecBestMu_Acc"]->Fill(recMu1);
00145     }
00146     mapHisto_["hRecBestMu"]->Fill(recMu2);
00147     if ((std::abs(recMu2.eta())<2.5) && (recMu2.pt()>2.5)) {
00148       mapHisto_["hRecBestMu_Acc"]->Fill(recMu2);
00149     }
00150     mapHisto_["hDeltaRecBestMu"]->Fill(recMu1, recMu2);
00151     
00152     mapHisto_["hRecBestRes"]->Fill(bestRecRes);
00153     if ((std::abs(recMu1.eta())<2.5) && (recMu1.pt()>2.5) && (std::abs(recMu2.eta())<2.5) &&  (recMu2.pt()>2.5)){
00154       mapHisto_["hRecBestRes_Acc"]->Fill(bestRecRes);
00155       // Fill histogram of Res mass vs muon variable
00156       mapHisto_["hRecBestResVSMu"]->Fill (recMu1, bestRecRes, -1);
00157       mapHisto_["hRecBestResVSMu"]->Fill (recMu2, bestRecRes, +1);
00158     }
00159   }
00160 
00161   // Loop on the recMuons
00162   std::vector<reco::LeafCandidate>::const_iterator recMuon = muons.begin();
00163   int muonCount = 0;
00164   for ( ; recMuon!=muons.end(); ++recMuon, ++muonCount ) {  
00165 
00166     // Fill the histogram with uncorrected pt values
00167     uncorrectedPt_->Fill(recMuon->pt());
00168     uncorrectedPtVsEta_->Fill(recMuon->pt(), recMuon->eta());
00169 
00170     // Fill the histogram with corrected pt values
00171     std::cout << "correcting muon["<<muonCount<<"] with pt = " << recMuon->pt() << std::endl;
00172     double corrPt = (*corrector_)(*recMuon);
00173     std::cout << "to pt = " << corrPt << std::endl;
00174     correctedPt_->Fill(corrPt);
00175     correctedPtVsEta_->Fill(corrPt, recMuon->eta());
00176     // correctedPt_->Fill(recMuon->pt());
00177   }
00178 }
00179 
00180 lorentzVector TestCorrection::correctMuon( const lorentzVector & muon ) {
00181   double corrPt = corrector_->correct(muon);
00182   double ptEtaPhiE[4] = {corrPt, muon.Eta(), muon.Phi(), muon.E()};
00183   return MuScleFitUtils::fromPtEtaPhiToPxPyPz(ptEtaPhiE);
00184 }
00185 
00186 // ------------ method called once each job just before starting event loop  ------------
00187 void TestCorrection::initialize(const edm::EventSetup&)
00188 {
00189   // Read the pdf from root file. They are used by massProb when finding the muon pair, needed
00190   // for the mass histograms.
00191   readProbabilityDistributionsFromFile();
00192 }
00193 
00194 #endif // TESTCORRECTION_CC