19 #include "../../BAT/BCLog.h"
20 #include "../../BAT/BCH1D.h"
21 #include "../../BAT/BCParameter.h"
36 : fRandom(new TRandom3(0))
37 , fFlagMarginalize(false)
38 , fLogLevel(
BCLog::nothing)
53 bool flag_data =
false;
56 if (options.find(
"data") < options.size()) {
64 std::vector<TH1D> histograms;
67 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
76 int nbins = hist.GetNbinsX();
79 for (
int ibin = 1; ibin <= nbins; ++ibin) {
82 double observation =
fRandom->Poisson(expectation);
84 hist.SetBinContent(ibin, observation);
89 histograms.push_back(hist);
102 BCLog::OutDetail(Form(
"MTF Building %d ensembles for %d channels.",nensembles,nchannels));
108 std::vector<double> parameters(nparameters);
111 for (
int i = 0; i < nparameters; ++i) {
112 tree->SetBranchAddress(Form(
"Parameter%i", i), &(parameters[i]));
116 TTree * tree_out =
new TTree(
"ensembles",
"ensembles");
119 std::vector< std::vector<double> > nbins_matrix;
123 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
131 std::vector<double> nbins_column(nbins);
134 nbins_matrix.push_back(nbins_column);
137 std::vector<double> in_parameters(nparameters);
141 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
149 for (
int ibin = 1; ibin <= nbins; ++ibin) {
151 tree_out->Branch(Form(
"channel_%i_bin_%i", ichannel, ibin),
152 &(nbins_matrix[ichannel])[ibin-1],
"n/D");
156 for (
int i = 0; i < nparameters; ++i) {
157 tree_out->Branch(Form(
"parameter_%i", i), &in_parameters[i], Form(
"parameter_%i/D", i));
161 std::vector<TH1D> histograms;
164 for (
int iensemble = 0; iensemble < nensembles; ++iensemble) {
166 int index = (int)
fRandom->Uniform(tree->GetEntries());
167 tree->GetEntry(index);
174 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
182 for (
int ibin = 1; ibin <= nbins; ++ibin) {
184 (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
189 for (
int i = 0; i < nparameters; ++i) {
190 in_parameters[i] = parameters.at(i);
207 BCLog::OutDetail(Form(
"MTF Building %d ensambles for %d channels.",nensembles,nchannels));
210 TTree * tree =
new TTree(
"ensembles",
"ensembles");
213 std::vector< std::vector<double> > nbins_matrix;
217 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
225 std::vector<double> nbins_column(nbins);
228 nbins_matrix.push_back(nbins_column);
234 std::vector<double> in_parameters(nparameters);
238 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
246 for (
int ibin = 1; ibin <= nbins; ++ibin) {
248 tree->Branch(Form(
"channel_%i_bin_%i", ichannel, ibin),
249 &(nbins_matrix[ichannel])[ibin-1],
"n/D");
253 for (
int i = 0; i < nparameters; ++i) {
254 tree->Branch(Form(
"parameter_%i", i), &in_parameters[i], Form(
"parameter_%i/D", i));
258 std::vector<TH1D> histograms;
261 for (
int iensemble = 0; iensemble < nensembles; ++iensemble) {
267 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
275 for (
int ibin = 1; ibin <= nbins; ++ibin) {
277 (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
282 for (
int i = 0; i < nparameters; ++i) {
283 if (parameters.size() > 0)
284 in_parameters[i] = parameters.at(i);
286 in_parameters[i] = 0;
315 BCLog::OutSummary(
"Fit for each ensemble is going to be run using MCMC. It can take a while.");
319 bool flag_mc =
false;
322 if (options.find(
"MC") < options.size()) {
332 BCLog::OutSummary(
"No log messages for the ensemble fits are going to be printed.");
344 std::vector<TH1D *> histograms_data(nchannels);
347 std::vector< std::vector<double> > nbins_matrix;
351 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
359 std::vector<double> nbins_column(nbins);
362 nbins_matrix.push_back(nbins_column);
366 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
374 for (
int ibin = 1; ibin <= nbins; ++ibin) {
376 tree->SetBranchAddress(Form(
"channel_%i_bin_%i", ichannel, ibin),
377 &(nbins_matrix[ichannel])[ibin-1]);
385 std::vector<double> out_parameters(nparameters);
387 for (
int i = 0; i < nparameters; ++i) {
388 tree->SetBranchAddress(Form(
"parameter_%i", i), &out_parameters[i]);
393 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
402 TTree * tree_out =
new TTree(
"ensemble_test",
"ensemble test");
405 std::vector<double> out_mode_global(nparameters);
406 std::vector<double> out_std_global(nparameters);
407 std::vector<double> out_mode_marginalized(nparameters);
408 std::vector<double> out_mean_marginalized(nparameters);
409 std::vector<double> out_median_marginalized(nparameters);
410 std::vector<double> out_5quantile_marginalized(nparameters);
411 std::vector<double> out_10quantile_marginalized(nparameters);
412 std::vector<double> out_16quantile_marginalized(nparameters);
413 std::vector<double> out_84quantile_marginalized(nparameters);
414 std::vector<double> out_90quantile_marginalized(nparameters);
415 std::vector<double> out_95quantile_marginalized(nparameters);
416 std::vector<double> out_std_marginalized(nparameters);
417 std::vector<double> out_chi2_generated(nchannels);
418 std::vector<double> out_chi2_mode(nchannels);
419 std::vector<double> out_cash_generated(nchannels);
420 std::vector<double> out_cash_mode(nchannels);
421 std::vector<int> out_nevents(nchannels);
422 double out_chi2_generated_total;
423 double out_chi2_mode_total;
424 double out_cash_generated_total;
425 double out_cash_mode_total;
426 int out_nevents_total;
429 for (
int i = 0; i < nparameters; ++i) {
430 tree_out->Branch(Form(
"parameter_%i", i), &out_parameters[i], Form(
"parameter %i/D", i));
431 tree_out->Branch(Form(
"mode_global_%i", i), &out_mode_global[i], Form(
"global mode of par. %i/D", i));
432 tree_out->Branch(Form(
"std_global_%i", i), &out_std_global[i], Form(
"global std of par. %i/D", i));
434 tree_out->Branch(Form(
"mode_marginalized_%i", i), &out_mode_marginalized[i], Form(
"marginalized mode of par. %i/D", i));
435 tree_out->Branch(Form(
"mean_marginalized_%i", i), &out_mean_marginalized[i], Form(
"marginalized mean of par. %i/D", i));
436 tree_out->Branch(Form(
"median_marginalized_%i", i), &out_median_marginalized[i], Form(
"median of par. %i/D", i));
437 tree_out->Branch(Form(
"5quantile_marginalized_%i", i), &out_5quantile_marginalized[i], Form(
"marginalized 5 per cent quantile of par. %i/D", i));
438 tree_out->Branch(Form(
"10quantile_marginalized_%i", i), &out_10quantile_marginalized[i], Form(
"marginalized 10 per cent quantile of par. %i/D", i));
439 tree_out->Branch(Form(
"16quantile_marginalized_%i", i), &out_16quantile_marginalized[i], Form(
"marginalized 16 per cent quantile of par. %i/D", i));
440 tree_out->Branch(Form(
"84quantile_marginalized_%i", i), &out_84quantile_marginalized[i], Form(
"marginalized 84 per cent quantile of par. %i/D", i));
441 tree_out->Branch(Form(
"90quantile_marginalized_%i", i), &out_90quantile_marginalized[i], Form(
"marginalized 90 per cent quantile of par. %i/D", i));
442 tree_out->Branch(Form(
"95quantile_marginalized_%i", i), &out_95quantile_marginalized[i], Form(
"marginalized 95 per cent quantile of par. %i/D", i));
443 tree_out->Branch(Form(
"std_marginalized_%i", i), &out_std_marginalized[i], Form(
"marginalized std of par. %i/D", i));
446 for (
int i = 0; i < nchannels; ++i) {
447 tree_out->Branch(Form(
"chi2_generated_%i", i), &out_chi2_generated[i], Form(
"chi2 (generated par.) in channel %i/D", i));
448 tree_out->Branch(Form(
"chi2_mode_%i", i), &out_chi2_mode[i], Form(
"chi2 (mode of par.) in channel %i/D", i));
449 tree_out->Branch(Form(
"cash_generated_%i", i), &out_cash_generated[i], Form(
"cash statistic (generated par.) in channel %i/D", i));
450 tree_out->Branch(Form(
"cash_mode_%i", i), &out_cash_mode[i], Form(
"cash statistic (mode of par.) in channel %i/D", i));
451 tree_out->Branch(Form(
"nevents_%i", i), &out_nevents[i], Form(
"nevents in channel %i/I", i));
453 tree_out->Branch(
"chi2_generated_total", &out_chi2_generated_total,
"chi2 (generated par.) in all channels/D");
454 tree_out->Branch(
"chi2_mode_total", &out_chi2_mode_total,
"chi2 (mode of par.) in all channels/D");
455 tree_out->Branch(
"cash_generated_total", &out_cash_generated_total,
"cash statistic (generated par.) in all channels/D");
456 tree_out->Branch(
"cash_mode_total", &out_cash_mode_total,
"cash statistic (mode of par.) in all channels/D");
457 tree_out->Branch(
"nevents_total", &out_nevents_total,
"total number of events/I");
460 std::vector<TH1D*> histlist(0);
463 for (
int iensemble = 0; iensemble < nensembles; ++iensemble) {
465 if ((iensemble+1)%100 == 0 && iensemble > 0) {
467 int frac = int (
double(iensemble+1) /
double(nensembles) * 100.);
474 int index = iensemble + start;
475 tree->GetEntry(index);
478 std::vector<TH1D> histograms;
484 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
502 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
507 for (
unsigned int i = 0; i < ntemplates; ++i) {
511 histlist.push_back(temphist);
545 out_chi2_generated_total = 0;
546 out_chi2_mode_total = 0;
547 out_cash_generated_total = 0;
548 out_cash_mode_total = 0;
549 out_nevents_total = 0;
551 for (
int i = 0; i < nparameters; ++i) {
554 out_mode_marginalized[i] = hist->
GetMode();
555 out_mean_marginalized[i] = hist->
GetMean();
556 out_median_marginalized[i] = hist->
GetMedian();
557 out_5quantile_marginalized[i] = hist->
GetQuantile(0.05);
558 out_10quantile_marginalized[i] = hist->
GetQuantile(0.10);
559 out_16quantile_marginalized[i] = hist->
GetQuantile(0.16);
560 out_84quantile_marginalized[i] = hist->
GetQuantile(0.84);
561 out_90quantile_marginalized[i] = hist->
GetQuantile(0.90);
562 out_95quantile_marginalized[i] = hist->
GetQuantile(0.95);
563 out_std_marginalized[i]=hist->
GetRMS();
567 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
583 out_nevents_total += out_nevents[ichannel];
586 out_chi2_generated_total += out_chi2_generated[ichannel];
587 out_chi2_mode_total += out_chi2_mode[ichannel];
590 out_cash_generated_total += out_cash_generated[ichannel];
591 out_cash_mode_total += out_cash_mode[ichannel];
603 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
608 for (
unsigned int i = 0; i < ntemplates; ++i) {
611 TH1D * temphist = histlist.at(ichannel * ntemplates + i);
626 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
650 std::vector<TH1D> histograms;
653 int nchannels = matrix.size();;
656 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
661 std::vector<double> nbins_column = matrix[ichannel];
667 int nbins = hist.GetNbinsX();
670 for (
int ibin = 1; ibin <= nbins; ++ibin) {
671 hist.SetBinContent(ibin, nbins_column.at(ibin-1));
675 histograms.push_back(hist);
686 BCLog::OutSummary(Form(
"Running single channel analysis in directory \'%s\'.",dirname));
690 mkdir(dirname, 0777);
695 bool flag_syst =
true;
696 bool flag_mcmc =
true;
698 if (std::string(options).find(
"nosyst") < std::string(options).size())
701 if (std::string(options).find(
"mcmc") < std::string(options).size())
711 std::vector<bool> flag_channel(nchannels);
714 std::vector<bool> flag_systematic(nsystematics);
717 std::vector<BCMTFComparisonTool *> ctc;
718 std::vector<BCMTFComparisonTool *> ctc_mcmc;
726 for (
int i = 0; i < nparameters; ++ i) {
733 ctc_mcmc.push_back(ct_mcmc);
739 for (
int isystematic = 0; isystematic < nsystematics; ++isystematic) {
747 if (flag_systematic[isystematic])
754 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
787 for (
int i = 0; i < nparameters; ++ i) {
807 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
818 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
850 for (
int i = 0; i < nparameters; ++ i) {
873 for (
int isystematic = 0; isystematic < nsystematics; ++isystematic) {
878 if (flag_systematic[isystematic])
885 for (
int ichannel = 0; ichannel < nchannels; ++ichannel) {
897 TCanvas * c1 =
new TCanvas();
904 c1->Print((std::string(
"overview.pdf")+std::string(
"(")).c_str());
907 for (
int i = 1; i < nparameters-1; ++ i) {
912 c1->Print(
"overview.pdf");
916 ct = ctc.at(nparameters-1);
918 c1->Print((std::string(
"overview.pdf")+std::string(
")")).c_str());
922 TCanvas * c2 =
new TCanvas();
929 c2->Print((std::string(
"overview_mcmc.pdf")+std::string(
"(")).c_str());
932 for (
int i = 1; i < nparameters-1; ++ i) {
937 c2->Print(
"overview_mcmc.pdf");
941 ct_mcmc = ctc_mcmc.at(nparameters-1);
943 c2->Print((std::string(
"overview_mcmc.pdf")+std::string(
")")).c_str());
948 for (
int i = 0; i < nparameters; ++i) {
972 BCLog::OutSummary(Form(
"Running single channel systematic analysis in directory \'%s\'.",dirname));
976 mkdir(dirname, 0777);
981 bool flag_mcmc =
true;
983 if (std::string(options).find(
"mcmc") < std::string(options).size())
993 std::vector<bool> flag_channel(nchannels);
996 std::vector<bool> flag_systematic(nsystematics);
999 std::vector<BCMTFComparisonTool *> ctc;
1007 for (
int i = 0; i < nparameters; ++ i) {
1017 for (
int isystematic = 0; isystematic < nsystematics; ++ isystematic) {
1049 for (
int i = 0; i < nparameters; ++ i) {
1061 for (
int isystematic = 0; isystematic < nsystematics; ++isystematic) {
1072 for (
int isystematic = 0; isystematic < nsystematics; ++isystematic) {
1103 for (
int i = 0; i < nparameters; ++ i) {
1139 for (
int i = 0; i < nparameters; ++ i) {
1153 for (
int isystematic = 0; isystematic < nsystematics; ++isystematic) {
1158 if (flag_systematic[isystematic])
1166 TCanvas * c1 =
new TCanvas();
1173 c1->Print((std::string(
"overview.pdf")+std::string(
"(")).c_str());
1176 for (
int i = 1; i < nparameters-1; ++i) {
1181 c1->Print(
"overview.pdf");
1185 ct = ctc.at(nparameters-1);
1187 c1->Print((std::string(
"overview.pdf")+std::string(
")")).c_str());
1190 for (
int i = 0; i < nparameters; ++i) {
1211 BCLog::OutSummary(Form(
"Running calibration analysis in directory \'%s\'.",dirname));
1215 mkdir(dirname, 0777);
1220 int nvalues = int(parametervalues.size());
1221 for (
int ivalue = 0; ivalue < nvalues; ++ivalue) {
1224 TFile * file = TFile::Open(Form(
"ensemble_%i.root", ivalue),
"RECREATE");
1228 std::vector<double> parameters = default_parameters;
1229 parameters[index] = parametervalues.at(ivalue);