CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/MuonAnalysis/MomentumScaleCalibration/plugins/TestResolution.cc

Go to the documentation of this file.
00001 #ifndef TESTRESOLUTION_CC
00002 #define TESTRESOLUTION_CC
00003 
00004 #include "TestResolution.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 TestResolution::TestResolution(const edm::ParameterSet& iConfig) :
00021   theMuonLabel_( iConfig.getParameter<edm::InputTag>( "MuonLabel" ) ),
00022   theMuonType_( iConfig.getParameter<int>( "MuonType" ) ),
00023   theRootFileName_( iConfig.getUntrackedParameter<std::string>("OutputFileName") )
00024 {
00025   //now do what ever initialization is needed
00026   outputFile_ = new TFile(theRootFileName_.c_str(), "RECREATE");
00027   outputFile_->cd();
00028   sigmaPt_ = new TProfile("sigmaPtOverPt", "sigmaPt/Pt vs muon Pt", 1000, 0, 100);
00029   eventCounter_ = 0;
00030   // Create the corrector and set the parameters
00031   resolutionFunction_.reset(new ResolutionFunction( iConfig.getUntrackedParameter<std::string>("ResolutionsIdentifier") ) );
00032   std::cout << "resolutionFunction_ = " << &*resolutionFunction_ << std::endl;
00033 }
00034 
00035 
00036 TestResolution::~TestResolution()
00037 {
00038   outputFile_->cd();
00039   TCanvas canvas("sigmaPtOverPt","sigmaPt/Pt vs muon Pt", 1000, 800);
00040   canvas.cd();
00041   sigmaPt_->GetXaxis()->SetTitle("Pt(GeV)");
00042 //   TLegend * legend = new TLegend(0.7,0.71,0.98,1.);
00043 //   legend->SetTextSize(0.02);
00044 //   legend->SetFillColor(0); // Have a white background
00045 //   legend->AddEntry(uncorrectedPt_, "original pt");
00046 //   legend->AddEntry(correctedPt_, "corrected pt");
00047   sigmaPt_->Draw();
00048 //   legend->Draw("same");
00049 
00050   canvas.Write();
00051   sigmaPt_->Write();
00052   outputFile_->Close();
00053 
00054   std::cout << "Total analyzed events = " << eventCounter_ << std::endl;
00055 }
00056 
00057 
00058 //
00059 // member functions
00060 //
00061 
00062 // ------------ method called to for each event  ------------
00063 void TestResolution::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
00064   using namespace edm;
00065 
00066   ++eventCounter_;
00067   if ( eventCounter_%100 == 0 ) {
00068     std::cout << "Event number " << eventCounter_ << std::endl;
00069   }
00070 
00071   // Take the reco-muons, depending on the type selected in the cfg
00072   // --------------------------------------------------------------
00073 
00074   std::vector<reco::LeafCandidate> muons;
00075 
00076   if (theMuonType_==1) { // GlobalMuons
00077     Handle<reco::MuonCollection> glbMuons;
00078     iEvent.getByLabel (theMuonLabel_, glbMuons);
00079     muons = fillMuonCollection(*glbMuons);
00080   }
00081   else if (theMuonType_==2) { // StandaloneMuons
00082     Handle<reco::TrackCollection> saMuons;
00083     iEvent.getByLabel (theMuonLabel_, saMuons);
00084     muons = fillMuonCollection(*saMuons);
00085   }
00086   else if (theMuonType_==3) { // Tracker tracks
00087     Handle<reco::TrackCollection> tracks;
00088     iEvent.getByLabel (theMuonLabel_, tracks);
00089     muons = fillMuonCollection(*tracks);
00090   }
00091 
00092   // Loop on the recMuons
00093   std::vector<reco::LeafCandidate>::const_iterator recMuon = muons.begin();
00094   for ( ; recMuon!=muons.end(); ++recMuon ) {  
00095 
00096     // Fill the histogram with uncorrected pt values
00097     sigmaPt_->Fill(resolutionFunction_->sigmaPt(*recMuon, 0), recMuon->pt());
00098 
00099   }
00100 }
00101 
00102 //define this as a plug-in
00103 DEFINE_FWK_MODULE(TestResolution);
00104 
00105 #endif // TESTRESOLUTION_CC