-
Notifications
You must be signed in to change notification settings - Fork 2
/
exercise5.cu
191 lines (156 loc) · 5.14 KB
/
exercise5.cu
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
#include "Variable.hh"
#include "PdfBuilder.hh"
#include "UnbinnedDataSet.hh"
#include "TRandom.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "RooNovosibirsk.h"
#include <sys/time.h>
#include <sys/times.h>
#include <iostream>
using namespace std;
double novosib (double x, double peak, double width, double tail) {
double qa=0,qb=0,qc=0,qx=0,qy=0;
if(fabs(tail) < 1.e-7)
qc = 0.5*pow(((x-peak)/width),2);
else {
qa = tail*sqrt(log(4.));
qb = sinh(qa)/qa;
qx = (x-peak)/width*qb;
qy = 1.+tail*qx;
//---- Cutting curve from right side
if( qy > 1.E-7)
qc = 0.5*(pow((log(qy)/tail),2) + tail*tail);
else
qc = 15.0;
}
//---- Normalize the result
return exp(-qc);
}
int main (int argc, char** argv) {
gStyle->SetCanvasBorderMode(0);
gStyle->SetCanvasColor(10);
gStyle->SetFrameFillColor(10);
gStyle->SetFrameBorderMode(0);
gStyle->SetPadColor(0);
gStyle->SetTitleColor(1);
gStyle->SetStatColor(0);
gStyle->SetFillColor(0);
gStyle->SetFuncWidth(1);
gStyle->SetLineWidth(1);
gStyle->SetLineColor(1);
gStyle->SetPalette(1, 0);
Variable* xvar = new Variable("xvar", -100, 100);
xvar->numbins = 1000; // For such a large range, want more bins for better accuracy in normalisation.
UnbinnedDataSet landdata(xvar);
UnbinnedDataSet bifgdata(xvar);
UnbinnedDataSet novodata(xvar);
TH1F landHist("landHist", "", xvar->numbins, xvar->lowerlimit, xvar->upperlimit);
TH1F bifgHist("bifgHist", "", xvar->numbins, xvar->lowerlimit, xvar->upperlimit);
TH1F novoHist("novoHist", "", xvar->numbins, xvar->lowerlimit, xvar->upperlimit);
landHist.SetStats(false);
bifgHist.SetStats(false);
novoHist.SetStats(false);
TRandom donram(42);
double totalData = 0;
double maxNovo = 0;
for (double x = xvar->lowerlimit; x < xvar->upperlimit; x += 0.01) {
double curr = novosib(x, 0.3, 0.5, 1.0);
if (curr < maxNovo) continue;
maxNovo = curr;
}
double leftSigma = 13;
double rightSigma = 29;
double leftIntegral = 0.5 / (leftSigma * sqrt(2*M_PI));
double rightIntegral = 0.5 / (rightSigma * sqrt(2*M_PI));
double totalIntegral = leftIntegral + rightIntegral;
double bifpoint = -10;
for (int i = 0; i < 100000; ++i) {
xvar->value = xvar->upperlimit + 1;
while (fabs(xvar->value) > xvar->upperlimit) {
xvar->value = donram.Landau(-50, 1);
}
landdata.addEvent();
landHist.Fill(xvar->value);
if (donram.Uniform() < (leftIntegral / totalIntegral)) {
xvar->value = bifpoint - 1;
while ((xvar->value < bifpoint) || (xvar->value > xvar->upperlimit)) xvar->value = donram.Gaus(bifpoint, rightSigma);
}
else {
xvar->value = bifpoint + 1;
while ((xvar->value > bifpoint) || (xvar->value < xvar->lowerlimit)) xvar->value = donram.Gaus(bifpoint, leftSigma);
}
bifgdata.addEvent();
bifgHist.Fill(xvar->value);
while (true) {
xvar->value = donram.Uniform(xvar->lowerlimit, xvar->upperlimit);
double y = donram.Uniform(0, maxNovo);
if (y < novosib(xvar->value, 0.3, 0.5, 1.0)) break;
}
novodata.addEvent();
novoHist.Fill(xvar->value);
totalData++;
}
// EXERCISE: Write one of LandauThrustFunctor, BifurGaussThrustFunctor,
// or NovoSibirskThrustFunctor. Then use your new class to fit one
// of the distributions created above. If you feel ambitious, do two
// or all three.
// There is no solution for this exercise! However, if you get it to work
// well, please give me the code and I will put it in the next release of
// GooFit.
UnbinnedDataSet* data = &landdata;
//data = &bifgdata;
//data = &novodata;
ThrustPdfFunctor* total = 0; // Replace with your PDF constructor.
if (total) {
total->setData(data);
PdfFunctor fitter(total);
fitter.fit();
fitter.getMinuitValues();
}
TH1F pdfHist("pdfHist", "", xvar->numbins, xvar->lowerlimit, xvar->upperlimit);
pdfHist.SetStats(false);
UnbinnedDataSet grid(xvar);
double step = (xvar->upperlimit - xvar->lowerlimit)/xvar->numbins;
for (int i = 0; i < xvar->numbins; ++i) {
xvar->value = xvar->lowerlimit + (i + 0.5) * step;
grid.addEvent();
}
TCanvas foo;
if (total) {
total->setData(&grid);
vector<vector<double> > pdfVals;
total->getCompProbsAtDataPoints(pdfVals);
double totalPdf = 0;
for (int i = 0; i < grid.getNumEvents(); ++i) {
grid.loadEvent(i);
pdfHist.Fill(xvar->value, pdfVals[0][i]);
totalPdf += pdfVals[0][i];
}
for (int i = 0; i < xvar->numbins; ++i) {
double val = pdfHist.GetBinContent(i+1);
val /= totalPdf;
val *= totalData;
pdfHist.SetBinContent(i+1, val);
}
}
foo.SetLogy(true);
landHist.SetMarkerStyle(8);
landHist.SetMarkerSize(0.5);
landHist.Draw("p");
pdfHist.SetLineColor(kBlue);
pdfHist.SetLineWidth(3);
pdfHist.Draw("lsame");
foo.SaveAs("landau.png");
bifgHist.SetMarkerStyle(8);
bifgHist.SetMarkerSize(0.5);
bifgHist.Draw("p");
foo.SaveAs("bifurgauss.png");
novoHist.SetMarkerStyle(8);
novoHist.SetMarkerSize(0.5);
novoHist.Draw("p");
foo.SaveAs("novosibirsk.png");
return 0;
}