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.26 |
double evalFitFunction(HistoFitter::FitResults r, double x, double y) {
|
40 |
|
|
unsigned nFits = r.pars.size();
|
41 |
|
|
unsigned nGauss = r.pars[nFits-1].size() / 4;
|
42 |
|
|
double fitVal = 0.0;
|
43 |
|
|
for (unsigned i = 0; i < nGauss; i++) {
|
44 |
|
|
double N = r.pval[nFits-1][4*i];
|
45 |
|
|
double mu_x = r.pval[nFits-1][4*i + 1];
|
46 |
|
|
double mu_y = r.pval[nFits-1][4*i + 2];
|
47 |
|
|
double sig = r.pval[nFits-1][4*i + 3];
|
48 |
|
|
|
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.26 |
TH2D *fitHisto = fs->make<TH2D>((std::string(hist_orig->GetName())+"_fit").c_str(),
|
62 |
|
|
("Fitted distribution to "
|
63 |
|
|
+std::string(hist_orig->GetName())).c_str(),
|
64 |
|
|
hist_orig->GetNbinsX(),
|
65 |
|
|
hist_orig->GetXaxis()->GetXmin(),
|
66 |
|
|
hist_orig->GetXaxis()->GetXmax(),
|
67 |
|
|
hist_orig->GetNbinsY(),
|
68 |
|
|
hist_orig->GetYaxis()->GetXmin(),
|
69 |
|
|
hist_orig->GetYaxis()->GetXmax());
|
70 |
|
|
|
71 |
|
|
double Xlo = fitHisto->GetXaxis()->GetXmin();
|
72 |
|
|
double Xhi = fitHisto->GetXaxis()->GetXmax();
|
73 |
|
|
double Ylo = fitHisto->GetYaxis()->GetXmin();
|
74 |
|
|
double Yhi = fitHisto->GetYaxis()->GetXmax();
|
75 |
|
|
double XbinSize = (Xhi - Xlo) / static_cast<double>(fitHisto->GetNbinsX());
|
76 |
|
|
double YbinSize = (Yhi - Ylo) / static_cast<double>(fitHisto->GetNbinsY());
|
77 |
|
|
|
78 |
|
|
for (int i = 1; i <= fitHisto->GetNbinsX(); i++) {
|
79 |
|
|
for (int j = 1; j <= fitHisto->GetNbinsY(); j++) {
|
80 |
|
|
double x = (static_cast<double>(i) - 0.5) * XbinSize + Xlo;
|
81 |
|
|
double y = (static_cast<double>(j) - 0.5) * YbinSize + Ylo;
|
82 |
|
|
fitHisto->SetBinContent(i, j, evalFitFunction(r, x, y) * XbinSize * YbinSize);
|
83 |
dnisson |
1.1 |
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
dnisson |
1.26 |
// save fit results to an ntuple
|
87 |
|
|
TNtuple *rNtuple = fs->make<TNtuple>((std::string(hist_orig->GetName())+"_results").c_str(),
|
88 |
|
|
("Fit results for "+std::string(hist_orig->GetName())).c_str(),
|
89 |
|
|
"N:mu_x:mu_y:sigma");
|
90 |
|
|
unsigned nFits = r.pval.size();
|
91 |
|
|
unsigned nGauss = r.pval[nFits-1].size() / 4;
|
92 |
|
|
for (unsigned i = 0; i < nGauss; i++) {
|
93 |
|
|
rNtuple->Fill(r.pval[nFits-1][4*i], r.pval[nFits-1][4*i+1], r.pval[nFits-1][4*i+2],
|
94 |
|
|
r.pval[nFits-1][4*i+3]);
|
95 |
dnisson |
1.12 |
}
|
96 |
dnisson |
1.27 |
|
97 |
|
|
// save chisquares to ntuple
|
98 |
|
|
for (unsigned i = 0; i < r.chisquare.size(); i++) {
|
99 |
|
|
ostringstream csNtupleName, csNtupleTitle;
|
100 |
|
|
csNtupleName << hist_orig->GetName() << "_chi2_" << i << flush;
|
101 |
|
|
csNtupleTitle << "Chisquare "<<i<<" for histo "<<hist_orig->GetName()
|
102 |
|
|
<< flush;
|
103 |
|
|
TNtuple *csNtuple = fs->make<TNtuple>(csNtupleName.str().c_str(),
|
104 |
|
|
csNtupleTitle.str().c_str(),
|
105 |
|
|
"chisq");
|
106 |
|
|
csNtuple->Fill(r.chisquare[i]);
|
107 |
|
|
}
|
108 |
dnisson |
1.1 |
}
|
109 |
|
|
|
110 |
|
|
DEFINE_FWK_MODULE(JetFinderAnalyzer);
|