-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunbinned_scaling2.cpp
294 lines (253 loc) · 8.96 KB
/
unbinned_scaling2.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
R__LOAD_LIBRARY(libRooFit)
#include <chrono>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <unistd.h> // usleep
#include <sys/types.h> // for kill
#include <signal.h> // for kill
using namespace RooFit;
// call from command line like, for instance:
// root -l 'unbinned_scaling2.cpp()'
// timing_flag is used to activate only selected timing statements [1-7]
void unbinned_scaling2(int num_cpu=1, bool force_num_int=false,
bool time_num_ints=false, int optConst=2,
int N_gaussians=1, int N_observables=1, int N_parameters=2,
int N_events=100000,
int parallel_interleave=0,
int seed=1,
int print_level=0,
int timing_flag=1,
bool cpu_affinity=true,
bool fork_timer = false,
int fork_timer_sleep_us = 100000,
bool debug=false
) {
//gSystem->Exec("top -n1 -b");
// num_cpu: -1 is special option -> overhead communicatie protocol vergelijken (vgl met 1 cpu)
// parallel_interleave: 0 = blokken gelijke grootte, 1 = interleave
//
// binned: generateBinned ipv generate -> als je aantal events ongeveer even groot is als aantal bins krijg je load balancing problemen, dat zou interleave beetje moeten ondervangen
// verwachtingen
// - historisch: tot ~8 cpu gaat het goed
// - weinig data en veel parameters: comm overhead belangrijk (fit snel, veel tijd in oversturen params)
// - weinig parameters, veel data: scalability hoort beter te zijn (minder comm overhead) -> bottom line van wat de software kan
// - observables even op 1 houden, alles optellen klopt conceptueel-statistisch niet
if (debug) {
RooMsgService::instance().addStream(DEBUG);
// extra possible options: Topic(Generation) Topic(RooFit::Eval), ClassName("RooAbsTestStatistic")
}
// int N_parameters(8); // must be even, means and sigmas have diff ranges
if (timing_flag > 0) {
RooJsonListFile outfile;
outfile.open("timing_meta.json");
std::string names[13] = {"N_gaussians", "N_observables", "N_parameters",
"N_events", "num_cpu", "parallel_interleave",
"seed", "pid", "force_num_int", "time_num_ints",
"optConst", "print_level", "timing_flag"};
outfile.set_member_names(names, names + 13);
outfile << N_gaussians << N_observables << N_parameters
<< N_events << num_cpu << parallel_interleave
<< seed << getpid() << force_num_int << time_num_ints
<< optConst << print_level << timing_flag;
}
RooTimer::set_timing_flag(timing_flag);
if (time_num_ints) {
RooTimer::set_time_numInts(kTRUE);
}
// plotting configuration
int obs_plot_x(3);
int obs_plot_y(2);
int obs_plot_x_px(1200);
int obs_plot_y_px(800);
// other stuff
int printlevel(print_level);
int optimizeConst(optConst);
// int N_timing_loops(3); // not used
gRandom->SetSeed(seed);
// some sanity checks
if (obs_plot_x * obs_plot_y < N_observables) {
std::cout << "WARNING: obs_plot_x * obs_plot_y < N_observables,"
" won't be able to plot all observables!"
<< std::endl << std::endl;
}
if (N_parameters % 2 != 0) {
std::cout << "set N_parameters to an even number!" << std::endl;
exit(2);
}
// here we go!
RooWorkspace w("w", 1) ;
RooArgSet obs_set;
// create gaussian parameters
float mean[N_parameters/2], sigma[N_parameters/2];
for (int ix = 0; ix < N_parameters/2; ++ix) {
mean[ix] = gRandom->Gaus(0, 2);
sigma[ix] = 0.1 + abs(gRandom->Gaus(0, 2));
}
// create gaussians and also the observables and parameters they depend on
RooAbsReal* gaussian_ix;
for (int ix = 0; ix < N_gaussians; ++ix) {
std::cout << ix << std::endl;
std::ostringstream os;
// int ix_p = (ix/2) % N_parameters;
int ix_p = ix % (N_parameters / 2);
os << "Gaussian::g" << ix
<< "(x" << ix % N_observables << "[-10,10],"
<< "m" << ix_p << "[" << mean[ix_p] << ",-10,10],"
<< "s" << ix_p << "[" << sigma[ix_p] << ",0.1,10])";
std::string s = os.str();
gaussian_ix = dynamic_cast<RooAbsReal *>(w.factory(s.c_str()));
if (force_num_int) {
// force it
if (gaussian_ix) {
gaussian_ix->forceNumInt(kTRUE);
} else {
std::cout << "GAUSSIAN_IX " << ix << " GOT NULL PTR!!" << std::endl;
exit(1);
}
}
}
// create uniform background signals on each observable
for (int ix = 0; ix < N_observables; ++ix) {
{
std::ostringstream os;
os << "Uniform::u" << ix << "(x" << ix << ")";
std::string s = os.str();
w.factory(s.c_str());
}
// gather the observables in a list for data generation below
{
std::ostringstream os;
os << "x" << ix;
std::string s = os.str();
obs_set.add(*w.arg(s.c_str()));
}
}
RooArgSet pdf_set = w.allPdfs();
// create event counts for all pdfs
RooArgSet count_set;
// ... for the gaussians
for (int ix = 0; ix < N_gaussians; ++ix) {
std::stringstream os, os2;
os << "Nsig" << ix;
std::string s = os.str();
os2 << "#signal events comp " << ix;
std::string s2 = os2.str();
RooRealVar a(s.c_str(), s2.c_str(), 100, 0., 10*N_events);
w.import(a);
}
// gather them in count_set
for (int ix = 0; ix < N_gaussians; ++ix) {
std::stringstream os;
os << "Nsig" << ix;
std::string s = os.str();
count_set.add(*w.arg(s.c_str()));
}
// ... and for the uniform background components
for (int ix = 0; ix < N_observables; ++ix) {
std::stringstream os, os2;
os << "Nbkg" << ix;
std::string s = os.str();
os2 << "#background events comp " << ix;
std::string s2 = os2.str();
RooRealVar a(s.c_str(), s2.c_str(), 100, 0., 10*N_events);
w.import(a);
}
// gather them in count_set
for (int ix = 0; ix < N_observables; ++ix) {
std::stringstream os;
os << "Nbkg" << ix;
std::string s = os.str();
count_set.add(*w.arg(s.c_str()));
}
RooAddPdf sum("sum", "gaussians+uniforms", pdf_set, count_set);
// --- Generate a toyMC sample from composite PDF ---
RooDataSet *data = sum.generate(obs_set, N_events);
/*
// OR reload previously written out sample:
// wegschrijven:
data.write("roofit_demo_random_data_values.dat");
// om het weer in te lezen:
RooDataSet *data = RooDataSet::read("../roofit_demo_random_data_values.dat", RooArgList(mes));
*/
// --- Perform extended ML fit of composite PDF to toy data ---
// sum.fitTo(*data,"Extended") ;
// instead of full fitTo, only do the fit, leave out error matrix, using
// run style of run_higgs.C
RooJsonListFile outfile;
RooWallTimer timer;
if (timing_flag == 1) {
outfile.open("timing_full_minimize.json");
std::string names[2] = {"full_minimize_wall_s", "pid"};
outfile.set_member_names(names, names + 2);
}
Bool_t cpuAffinity;
if (cpu_affinity) {
cpuAffinity = kTRUE;
} else {
cpuAffinity = kFALSE;
}
// for (int it = 0; it < N_timing_loops; ++it)
{
RooAbsReal* nll = sum.createNLL(*data, NumCPU(num_cpu, parallel_interleave),
CPUAffinity(cpuAffinity));//, "Extended");
RooMinimizer m(*nll);
// m.setVerbose(1);
m.setStrategy(0);
m.setProfile(1);
m.setPrintLevel(printlevel);
m.optimizeConst(optimizeConst);
int pid = -1;
if (fork_timer) {
pid = fork();
}
if (pid == 0) {
/* child */
timer.start();
while (true) {
timer.stop();
std::cout << "TIME: " << timer.timing_s() << "s" << std::endl;
usleep(fork_timer_sleep_us);
}
}
else {
/* parent */
if (timing_flag == 1) {
timer.start();
}
// m.hesse();
m.minimize("Minuit2", "migrad");
if (timing_flag == 1) {
timer.stop();
std::cout << timer.timing_s() << "s" << std::endl;
outfile << timer.timing_s() << getpid();
}
if (pid > 0) {
// a child exists
kill(pid, SIGKILL);
}
}
}
// print the "true" values for comparison
std::cout << "--- values of PDF parameters used for data generation:"
<< std::endl;
for (int ix = 0; ix < N_parameters/2; ++ix) {
std::cout << " gauss " << ix << ": m = " << mean[ix] << ", s = "
<< sigma[ix] << std::endl;
}
// --- Plot toy data and composite PDF overlaid ---
TCanvas* c = new TCanvas("unbinned_scaling", "unbinned_scaling",
obs_plot_x_px, obs_plot_y_px);
c->Divide(obs_plot_x, obs_plot_y);
for (int ix = 0; ix < N_observables && ix < obs_plot_x * obs_plot_y; ++ix) {
std::ostringstream os;
os << "x" << ix;
std::string s = os.str();
RooPlot* frame = w.var(s.c_str())->frame();
data->plotOn(frame);
sum.plotOn(frame);
c->cd(ix+1);
frame->Draw();
}
}