forked from lpernie/limits_2a4mu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FitLimits.C
192 lines (168 loc) · 6.59 KB
/
FitLimits.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <TH1F.h>
#include <TFile.h>
#include <TMath.h>
#include <TCanvas.h>
#include <TH2F.h>
#include <TH1D.h>
#include <TF1.h>
#include <TGraph.h>
#include <THStack.h>
#include <TRandom3.h>
#include <TFormula.h>
#include <TPad.h>
#include <TLegend.h>
#include <TStyle.h>
#include <TROOT.h>
#include <TMarker.h>
#include <TChain.h>
#include <memory>
#include <string>
#include <map>
#include <vector>
#include "TTree.h"
#include "TLatex.h"
#include "TMath.h"
#include "TBranch.h"
#include "TFile.h"
#include "TStyle.h"
#include "TString.h"
#include "TEventList.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooPolynomial.h"
#include "RooDataHist.h"
#include "RooAbsPdf.h"
#include "RooAddPdf.h"
#include "RooArgSet.h"
#include "RooArgList.h"
#include "RooPlot.h"
#include "RooFitResult.h"
#include "RooNLLVar.h"
#include "RooChi2Var.h"
#include "RooMinuit.h"
#include "RooRealVar.h"
#include "RooGenericPdf.h"
using namespace RooFit;
double getNormalizedValue(double norm, double sigma)
{
// multiply with sqrt(2*pi)*sigma so that the final
// formulation of the PDF only contains terms
// like N*Gauss(...)
return norm; // / sqrt(2*M_PI * sigma * sigma)
}
void FitLimits(){
TCanvas *c1 = new TCanvas("c1","");
gStyle->SetOptStat(111111);
// Get limit Tgraph
TString LimitFile = "plots/C/limit_Events_vs_mGammaD_2016.root";
float NoevIs = 3;//2.8;
//float NoevIs = 2.4; //90% cl
//TString LimitFile = "Real_Limits.root";
TFile *f = new TFile(LimitFile.Data());
TGraph *graph = (TGraph*) f->Get("Graph");
auto nPoints = graph->GetN();
// Convert in Th1F
double xMin, ytmp, bin=100;
graph->GetPoint(0, xMin, ytmp);
double xMax;
graph->GetPoint(nPoints-1, xMax, ytmp);
TH1F *h = new TH1F("h", "", bin, xMin, xMax);
float step = (xMax-xMin)/bin;
for(int i=0; i < nPoints; ++i) {
double x, y;
graph->GetPoint(i, x, y);
int BinToFill = int((x-xMin)/step);
h->SetBinContent(BinToFill+1, x, y);
}
for(int i=0; i < h->GetNbinsX(); ++i) {
if( h->GetBinContent(i+1) < 2. ){
h->SetBinContent(i+1,NoevIs);
}
h->SetBinError(i+1,0.3);
}
h->Draw();
c1->SaveAs("plots/h.pdf");
// Create RooDataHist
RooRealVar x("x", "m_a", 0.2113, 9., "GeV");
RooDataHist dh("dh", "", RooArgList(x), h);
//cout << dh.printMultiline() << endl;
// Parameters
// x here is added to stablize the fit somehow
RooRealVar co("co", "co", NoevIs, NoevIs-0.1, NoevIs+0.1,"");
RooGenericPdf p0("p0","","x*0.00000001 + co",RooArgSet(x,co));
RooRealVar mean1("mean1", "mean", 0.55, 0.5, 0.6,"GeV");
RooRealVar sigma1("sigma1", "sigma", 0.07, 0.04, 0.1,"GeV");
RooGaussian gaus1("gaus1","Gaussian",x, mean1,sigma1);
RooRealVar mean2("mean2", "mean", 0.95, 0.7, 1.,"GeV");
RooRealVar sigma2("sigma2", "sigma", 0.04, 0.035, 0.1,"GeV");
RooGaussian gaus2("gaus2","Gaussian",x, mean2,sigma2);
RooRealVar mean3("mean3", "mean", 1.18, 1.15, 1.2,"GeV");
RooRealVar sigma3("sigma3", "sigma", 0.035, 0.033, 0.1,"GeV");
RooGaussian gaus3("gaus3","Gaussian",x, mean3,sigma3);
RooRealVar mean4("mean4", "mean", 1.5, 1.4, 1.6,"GeV");
RooRealVar sigma4("sigma4", "sigma", 0.04, 0.03, 0.1,"GeV");
RooGaussian gaus4("gaus4","Gaussian",x, mean4,sigma4);
RooRealVar mean5("mean5", "mean", 1.8, 1.75, 2.1,"GeV");
RooRealVar sigma5("sigma5", "sigma", 0.08, 0.05, 0.1,"GeV");
RooGaussian gaus5("gaus5","Gaussian",x, mean5,sigma5);
RooRealVar mean6("mean6", "mean", 2.4, 2.3, 2.5,"GeV");
RooRealVar sigma6("sigma6", "sigma", 0.07, 0.038, 0.1,"GeV");
RooGaussian gaus6("gaus6","Gaussian",x, mean6,sigma6);
RooRealVar mean7("mean7", "mean", 2.9, 2.8, 2.999,"GeV");
RooRealVar sigma7("sigma7", "sigma", 0.05, 0.004, 0.2,"GeV");
RooGaussian gaus7("gaus7","Gaussian",x, mean7,sigma7);
RooRealVar mean8("mean8", "mean", 3.1, 3., 3.2,"GeV");
RooRealVar sigma8("sigma8", "sigma", 0.12, 0.0, 0.2,"GeV");
RooGaussian gaus8("gaus8","Gaussian",x, mean8,sigma8);
RooRealVar N0("N0","norm",291,100.,400.);
RooRealVar N1("N1","norm",0.1,0.01,6.0);
RooRealVar N2("N2","norm",2.0,0.5,6.0);
RooRealVar N3("N3","norm",1.0,0.5,1.5);
RooRealVar N4("N4","norm",1.75,1.5,1.9);
RooRealVar N5("N5","norm",0.5,0.1,1.0);
RooRealVar N6("N6","norm",1.5,1,2);
RooRealVar N7("N7","norm",0.4,0.2,2.0);
RooRealVar N8("N8","norm",7.5,0,20);
// make the combined fit with 5 gaussians
RooAddPdf model("model","model",
RooArgList(p0,/*gaus1,gaus2,*/gaus3,gaus4,gaus5,gaus6,/*gaus7,*/gaus8),
RooArgList(N0,/*N1,N2,*/N3,N4,N5,N6,/*N7,*/N8));
// RooAbsPdf* MyModel = &model;
RooNLLVar nll("nll","log likelihood var",model,dh, RooFit::Extended(true));
RooMinuit m(nll);
m.setVerbose(false);
m.migrad();
//x.setRange("sobRange1",mean1.getVal()-3.*sigma1.getVal(), mean1.getVal()+3.*sigma1.getVal());
//RooAbsReal* integral1 = gaus1.createIntegral(x,NormSet(x),Range("sobRange"));
//float norm1 = integral1->getVal();
//cout<<integral1->getVal()<<" -> "<<N1.getVal()*norm1<<"*exp(-0.5*((m-"<<mean1.getVal()<<")/"<<sigma1.getVal()<<")**2) + "<<endl;
// get the fit results
cout<<"RESULTS:"<<endl;
cout<<co.getVal()<<endl;
cout<<N3.getVal()<<endl;
cout<<N4.getVal()<<endl;
cout<<N5.getVal()<<endl;
cout<<N6.getVal()<<endl;
cout<<N8.getVal()<<endl;
// cout<<N1.getVal()<<"*exp(-0.5*((m-"<<mean1.getVal()<<")/"<<sigma1.getVal()<<")**2) + \\"<<endl;
// cout<<N2.getVal()<<"*exp(-0.5*((m-"<<mean2.getVal()<<")/"<<sigma2.getVal()<<")**2) + \\"<<endl;
// cout<<N7.getVal()<<"*exp(-0.5*((m-"<<mean7.getVal()<<")/"<<sigma7.getVal()<<")**2) + \\"<<endl;
cout<<getNormalizedValue(N3.getVal(), sigma3.getVal())<<"*exp(-0.5*((m-"<<mean3.getVal()<<")/"<<sigma3.getVal()<<")**2) + \\"<<endl;
cout<<getNormalizedValue(N4.getVal(), sigma4.getVal())<<"*exp(-0.5*((m-"<<mean4.getVal()<<")/"<<sigma4.getVal()<<")**2) + \\"<<endl;
cout<<getNormalizedValue(N5.getVal(), sigma5.getVal())<<"*exp(-0.5*((m-"<<mean5.getVal()<<")/"<<sigma5.getVal()<<")**2) + \\"<<endl;
cout<<getNormalizedValue(N6.getVal(), sigma6.getVal())<<"*exp(-0.5*((m-"<<mean6.getVal()<<")/"<<sigma6.getVal()<<")**2) + \\"<<endl;
cout<<getNormalizedValue(N8.getVal(), sigma8.getVal())<<"*exp(-0.5*((m-"<<mean8.getVal()<<")/"<<sigma8.getVal()<<")**2)"<<endl;
RooPlot* xframe = x.frame(h->GetNbinsX());
xframe->SetTitle(h->GetTitle());
dh.plotOn(xframe);
model.plotOn(xframe,Normalization(1.0,RooAbsReal::RelativeExpected)) ;
model.plotOn(xframe,Components(p0),LineStyle(kDashed), LineColor(kRed));
// model.plotOn(xframe);
xframe->Draw();
c1->SaveAs("plots/h_fit.pdf");
}