ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/JetFitAnalyzer/test/JetFinderAnalyzer.cc
Revision: 1.28
Committed: Sat Feb 6 00:01:38 2010 UTC (15 years, 2 months ago) by dnisson
Content type: text/plain
Branch: MAIN
CVS Tags: V02-03-03, V02-03-01, V02-03-00, V02-02-00, V02-01-00
Changes since 1.27: +58 -53 lines
Log Message:
Added intermediate fit functionality

File Contents

# User Rev Content
1 dnisson 1.24 /* A JetFitAnalyzer that makes histograms with smearing */
2 dnisson 1.1
3     #include "UserCode/JetFitAnalyzer/interface/JetFitAnalyzer.h"
4    
5     #include "FWCore/ServiceRegistry/interface/Service.h"
6 dnisson 1.2 #include "FWCore/MessageLogger/interface/MessageLogger.h"
7 dnisson 1.1 #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
8    
9 dnisson 1.18 #include <iostream>
10 dnisson 1.1 #include <sstream>
11    
12     #include "TF2.h"
13 dnisson 1.26 #include "TNtuple.h"
14 dnisson 1.1
15     #define PI 3.141593
16    
17     using namespace std;
18    
19     class JetFinderAnalyzer : public JetFitAnalyzer {
20     public:
21     explicit JetFinderAnalyzer( const edm::ParameterSet&);
22     ~JetFinderAnalyzer() {}
23    
24     private:
25 dnisson 1.15 virtual void beginJob(const edm::EventSetup &es);
26 dnisson 1.26 virtual void analyze_results(HistoFitter::FitResults, std::vector<HistoFitter::Trouble>,
27     TH2 *);
28 dnisson 1.1 };
29    
30     JetFinderAnalyzer::JetFinderAnalyzer(const edm::ParameterSet &pSet)
31     : JetFitAnalyzer(pSet) // this is important!
32     {
33 dnisson 1.26 }
34 dnisson 1.2
35 dnisson 1.26 void JetFinderAnalyzer::beginJob(const edm::EventSetup &es) {
36 dnisson 1.2
37 dnisson 1.1 }
38    
39 dnisson 1.28 double evalFitFunction(HistoFitter::FitResults r, double x, double y,
40     int i) {
41     unsigned nGauss = r.pars[i].size() / 4;
42 dnisson 1.26 double fitVal = 0.0;
43 dnisson 1.28 for (unsigned j = 0; j < nGauss; j++) {
44     double N = r.pval[i][4*j];
45     double mu_x = r.pval[i][4*j + 1];
46     double mu_y = r.pval[i][4*j + 2];
47     double sig = r.pval[i][4*j + 3];
48 dnisson 1.26
49     double rel_x = x - mu_x; double rel_y = y - mu_y;
50     fitVal += (N / 2.0 / M_PI / sig / sig)
51     * exp(-(rel_x * rel_x + rel_y * rel_y)/2.0/sig/sig);
52 dnisson 1.13 }
53 dnisson 1.26 return fitVal;
54     }
55 dnisson 1.13
56 dnisson 1.26 void JetFinderAnalyzer::analyze_results(HistoFitter::FitResults r,
57     std::vector<HistoFitter::Trouble> t,
58     TH2 *hist_orig) {
59     // perform analysis of fit results
60 dnisson 1.15 edm::Service<TFileService> fs;
61 dnisson 1.28 for (unsigned i = 0; i < r.pars.size(); i++) {
62     ostringstream fitHistoOss;
63     fitHistoOss << hist_orig->GetName() << "_fit" << i << flush;
64     TH2D *fitHisto = fs->make<TH2D>(fitHistoOss.str().c_str(),
65     ("Fitted distribution to "
66     +std::string(hist_orig->GetName())).c_str(),
67     hist_orig->GetNbinsX(),
68     hist_orig->GetXaxis()->GetXmin(),
69     hist_orig->GetXaxis()->GetXmax(),
70     hist_orig->GetNbinsY(),
71     hist_orig->GetYaxis()->GetXmin(),
72     hist_orig->GetYaxis()->GetXmax());
73    
74     double Xlo = fitHisto->GetXaxis()->GetXmin();
75     double Xhi = fitHisto->GetXaxis()->GetXmax();
76     double Ylo = fitHisto->GetYaxis()->GetXmin();
77     double Yhi = fitHisto->GetYaxis()->GetXmax();
78     double XbinSize = (Xhi - Xlo) / static_cast<double>(fitHisto->GetNbinsX());
79     double YbinSize = (Yhi - Ylo) / static_cast<double>(fitHisto->GetNbinsY());
80    
81     for (int ib = 1; ib <= fitHisto->GetNbinsX(); ib++) {
82     for (int jb = 1; jb <= fitHisto->GetNbinsY(); jb++) {
83     double x = (static_cast<double>(ib) - 0.5) * XbinSize + Xlo;
84     double y = (static_cast<double>(jb) - 0.5) * YbinSize + Ylo;
85     fitHisto->SetBinContent(ib, jb, evalFitFunction(r, x, y, i) * XbinSize * YbinSize);
86     }
87     }
88    
89     // save fit results to an ntuple
90     ostringstream fitResultsOss;
91     fitResultsOss << hist_orig->GetName() << "_results" << i << flush;
92     TNtuple *rNtuple = fs->make<TNtuple>(fitResultsOss.str().c_str(),
93     ("Fit results for "+std::string(hist_orig->GetName())).c_str(),
94     "N:mu_x:mu_y:sigma");
95     unsigned nGauss = r.pval[i].size() / 4;
96     for (unsigned j = 0; j < nGauss; j++) {
97     rNtuple->Fill(r.pval[i][4*j], r.pval[i][4*j+1], r.pval[i][4*j+2],
98     r.pval[i][4*j+3]);
99     }
100    
101     // save chisquares to ntuple
102     for (unsigned j = 0; j < r.chisquare.size(); j++) {
103     ostringstream csNtupleName, csNtupleTitle;
104     csNtupleName << hist_orig->GetName() << "_chi2_" << j << flush;
105     csNtupleTitle << "Chisquare "<<j<<" for histo "<<hist_orig->GetName()
106     << flush;
107     TNtuple *csNtuple = fs->make<TNtuple>(csNtupleName.str().c_str(),
108     csNtupleTitle.str().c_str(),
109     "chisq");
110     csNtuple->Fill(r.chisquare[j]);
111 dnisson 1.1 }
112     }
113     }
114    
115     DEFINE_FWK_MODULE(JetFinderAnalyzer);