00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/stat.h>
00011 #include <iostream>
00012
00013 #include <TROOT.h>
00014 #include <TCanvas.h>
00015 #include <TTree.h>
00016 #include <TH1D.h>
00017 #include <TRandom3.h>
00018 #include <TFile.h>
00019
00020 #include "../../BAT/BCLog.h"
00021 #include "../../BAT/BCH1D.h"
00022
00023 #include "BCMTF.h"
00024 #include "BCMTFChannel.h"
00025 #include "BCMTFTemplate.h"
00026 #include "BCMTFSystematic.h"
00027 #include "BCMTFComparisonTool.h"
00028
00029 #include "BCMTFAnalysisFacility.h"
00030
00031
00032 BCMTFAnalysisFacility::BCMTFAnalysisFacility(BCMTF * mtf)
00033 : fRandom(new TRandom3(0))
00034 , fFlagMCMC(false)
00035 , fLogLevel(BCLog::nothing)
00036 {
00037 fMTF = mtf;
00038 BCLog::OutDetail(Form("Prepared Analysis Facility for MTF model \'%s\'",mtf->GetName().c_str()));
00039 }
00040
00041
00042 BCMTFAnalysisFacility::~BCMTFAnalysisFacility()
00043 {
00044 }
00045
00046
00047 std::vector<TH1D> BCMTFAnalysisFacility::BuildEnsemble(const std::vector<double> & parameters)
00048 {
00049
00050 int nchannels = fMTF->GetNChannels();
00051
00052
00053 std::vector<TH1D> histograms;
00054
00055
00056 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00057
00058
00059 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00060
00061
00062 TH1D hist( *(channel->GetData()->GetHistogram()) );
00063
00064
00065 int nbins = hist.GetNbinsX();
00066
00067
00068 for (int ibin = 1; ibin <= nbins; ++ibin) {
00069 double expectation = fMTF->Expectation(ichannel, ibin, parameters);
00070
00071 double observation = gRandom->Poisson(expectation);
00072
00073 hist.SetBinContent(ibin, observation);
00074 }
00075
00076
00077 histograms.push_back(hist);
00078 }
00079
00080
00081 return histograms;
00082 }
00083
00084
00085 TTree * BCMTFAnalysisFacility::BuildEnsembles(TTree * tree, int nensembles)
00086 {
00087
00088 int nchannels = fMTF->GetNChannels();
00089
00090 BCLog::OutDetail(Form("MTF Building %d ensambles for %d channels.",nensembles,nchannels));
00091
00092
00093 int nparameters = fMTF->GetNParameters();
00094
00095
00096 std::vector<double> parameters(nparameters);
00097
00098
00099 for (int i = 0; i < nparameters; ++i) {
00100 tree->SetBranchAddress(Form("Parameter%i", i), &(parameters[i]));
00101 }
00102
00103
00104 TTree * tree_out = new TTree("ensembles", "ensembles");
00105
00106
00107 std::vector< std::vector<double> > nbins_matrix;
00108
00109
00110
00111 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00112
00113 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00114
00115
00116 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00117
00118
00119 std::vector<double> nbins_column(nbins);
00120
00121
00122 nbins_matrix.push_back(nbins_column);
00123 }
00124
00125 std::vector<double> in_parameters(nparameters);
00126
00127
00128
00129 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00130
00131 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00132
00133
00134 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00135
00136
00137 for (int ibin = 1; ibin <= nbins; ++ibin) {
00138
00139 tree_out->Branch(Form("channel_%i_bin_%i", ichannel, ibin),
00140 &(nbins_matrix[ichannel])[ibin-1], "n/D");
00141 }
00142 }
00143
00144 for (int i = 0; i < nparameters; ++i) {
00145 tree_out->Branch(Form("parameter_%i", i), &in_parameters[i], Form("parameter_%i/D", i));
00146 }
00147
00148
00149 std::vector<TH1D> histograms;
00150
00151
00152 for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00153
00154 int index = (int) fRandom->Uniform(tree->GetEntries());
00155 tree->GetEntry(index);
00156
00157
00158 histograms = BuildEnsemble(parameters);
00159
00160
00161
00162 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00163
00164 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00165
00166
00167 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00168
00169
00170 for (int ibin = 1; ibin <= nbins; ++ibin) {
00171
00172 (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
00173 }
00174 }
00175
00176
00177 for (int i = 0; i < nparameters; ++i) {
00178 in_parameters[i] = parameters.at(i);
00179 }
00180
00181
00182 tree_out->Fill();
00183 }
00184
00185
00186 return tree_out;
00187 }
00188
00189
00190 TTree * BCMTFAnalysisFacility::BuildEnsembles(const std::vector<double> & parameters, int nensembles)
00191 {
00192
00193 int nchannels = fMTF->GetNChannels();
00194
00195 BCLog::OutDetail(Form("MTF Building %d ensambles for %d channels.",nensembles,nchannels));
00196
00197
00198 TTree * tree = new TTree("ensembles", "ensembles");
00199
00200
00201 std::vector< std::vector<double> > nbins_matrix;
00202
00203
00204
00205 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00206
00207 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00208
00209
00210 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00211
00212
00213 std::vector<double> nbins_column(nbins);
00214
00215
00216 nbins_matrix.push_back(nbins_column);
00217 }
00218
00219
00220 int nparameters = fMTF->GetNParameters();
00221
00222 std::vector<double> in_parameters(nparameters);
00223
00224
00225
00226 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00227
00228 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00229
00230
00231 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00232
00233
00234 for (int ibin = 1; ibin <= nbins; ++ibin) {
00235
00236 tree->Branch(Form("channel_%i_bin_%i", ichannel, ibin),
00237 &(nbins_matrix[ichannel])[ibin-1], "n/D");
00238 }
00239 }
00240
00241 for (int i = 0; i < nparameters; ++i) {
00242 tree->Branch(Form("parameter_%i", i), &in_parameters[i], Form("parameter_%i/D", i));
00243 }
00244
00245
00246 std::vector<TH1D> histograms;
00247
00248
00249 for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00250
00251 histograms = BuildEnsemble(parameters);
00252
00253
00254
00255 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00256
00257 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00258
00259
00260 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00261
00262
00263 for (int ibin = 1; ibin <= nbins; ++ibin) {
00264
00265 (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
00266 }
00267 }
00268
00269
00270 for (int i = 0; i < nparameters; ++i) {
00271 in_parameters[i] = parameters.at(i);
00272 }
00273
00274
00275 tree->Fill();
00276 }
00277
00278
00279 return tree;
00280 }
00281
00282
00283 TTree * BCMTFAnalysisFacility::PerformEnsembleTest(const std::vector<double> & parameters, int nensembles)
00284 {
00285
00286 TTree * tree = 0;
00287
00288
00289 tree = BuildEnsembles(parameters, nensembles);
00290
00291
00292 return PerformEnsembleTest(tree, nensembles);
00293 }
00294
00295
00296 TTree * BCMTFAnalysisFacility::PerformEnsembleTest(TTree * tree, int nensembles, int start)
00297 {
00298 BCLog::OutSummary("Running ensemble test.");
00299 if (fFlagMCMC) {
00300 BCLog::OutSummary("Fit for each ensemble is going to be run using MCMC. It can take a while.");
00301 }
00302
00303
00304
00305
00306 BCLog::LogLevel lls = BCLog::GetLogLevelScreen();
00307 BCLog::LogLevel llf = BCLog::GetLogLevelFile();
00308 if(fLogLevel==BCLog::nothing) {
00309 BCLog::OutSummary("No log messages for the ensemble fits are going to be printed.");
00310 BCLog::SetLogLevel(fLogLevel);
00311 }
00312 else if(fLogLevel!=lls) {
00313 BCLog::OutSummary(Form("The log level for the ensemble test is set to \'%s\'.",BCLog::ToString(fLogLevel)));
00314 BCLog::SetLogLevel(fLogLevel);
00315 }
00316
00317
00318 int nchannels = fMTF->GetNChannels();
00319
00320
00321 std::vector<TH1D *> histograms_data(nchannels);
00322
00323
00324 std::vector< std::vector<double> > nbins_matrix;
00325
00326
00327
00328 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00329
00330 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00331
00332
00333 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00334
00335
00336 std::vector<double> nbins_column(nbins);
00337
00338
00339 nbins_matrix.push_back(nbins_column);
00340 }
00341
00342
00343 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00344
00345 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00346
00347
00348 int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00349
00350
00351 for (int ibin = 1; ibin <= nbins; ++ibin) {
00352
00353 tree->SetBranchAddress(Form("channel_%i_bin_%i", ichannel, ibin),
00354 &(nbins_matrix[ichannel])[ibin-1]);
00355 }
00356 }
00357
00358
00359 int nparameters = fMTF->GetNParameters();
00360
00361
00362 std::vector<double> out_parameters(nparameters);
00363
00364 for (int i = 0; i < nparameters; ++i) {
00365 tree->SetBranchAddress(Form("parameter_%i", i), &out_parameters[i]);
00366 }
00367
00368
00369
00370 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00371
00372 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00373
00374
00375 histograms_data[ichannel] = channel->GetData()->GetHistogram();
00376 }
00377
00378
00379 TTree * tree_out = new TTree("ensemble_test", "ensemble test");
00380
00381
00382 std::vector<double> out_mode_global(nparameters);
00383 std::vector<double> out_std_global(nparameters);
00384 std::vector<double> out_mode_marginalized(nparameters);
00385 std::vector<double> out_mean_marginalized(nparameters);
00386 std::vector<double> out_median_marginalized(nparameters);
00387 std::vector<double> out_5quantile_marginalized(nparameters);
00388 std::vector<double> out_10quantile_marginalized(nparameters);
00389 std::vector<double> out_16quantile_marginalized(nparameters);
00390 std::vector<double> out_84quantile_marginalized(nparameters);
00391 std::vector<double> out_90quantile_marginalized(nparameters);
00392 std::vector<double> out_95quantile_marginalized(nparameters);
00393 std::vector<double> out_std_marginalized(nparameters);
00394 std::vector<double> out_chi2_generated(nchannels);
00395 std::vector<double> out_chi2_mode(nchannels);
00396 std::vector<double> out_cash_generated(nchannels);
00397 std::vector<double> out_cash_mode(nchannels);
00398 std::vector<int> out_nevents(nchannels);
00399 double out_chi2_generated_total;
00400 double out_chi2_mode_total;
00401 double out_cash_generated_total;
00402 double out_cash_mode_total;
00403 int out_nevents_total;
00404
00405
00406 for (int i = 0; i < nparameters; ++i) {
00407 tree_out->Branch(Form("parameter_%i", i), &out_parameters[i], Form("parameter %i/D", i));
00408 tree_out->Branch(Form("mode_global_%i", i), &out_mode_global[i], Form("global mode of par. %i/D", i));
00409 tree_out->Branch(Form("std_global_%i", i), &out_std_global[i], Form("global std of par. %i/D", i));
00410 if (fFlagMCMC) {
00411 tree_out->Branch(Form("mode_marginalized_%i", i), &out_mode_marginalized[i], Form("marginalized mode of par. %i/D", i));
00412 tree_out->Branch(Form("mean_marginalized_%i", i), &out_mean_marginalized[i], Form("marginalized mean of par. %i/D", i));
00413 tree_out->Branch(Form("median_marginalized_%i", i), &out_median_marginalized[i], Form("median of par. %i/D", i));
00414 tree_out->Branch(Form("5quantile_marginalized_%i", i), &out_5quantile_marginalized[i], Form("marginalized 5 per cent quantile of par. %i/D", i));
00415 tree_out->Branch(Form("10quantile_marginalized_%i", i), &out_10quantile_marginalized[i], Form("marginalized 10 per cent quantile of par. %i/D", i));
00416 tree_out->Branch(Form("16quantile_marginalized_%i", i), &out_16quantile_marginalized[i], Form("marginalized 16 per cent quantile of par. %i/D", i));
00417 tree_out->Branch(Form("84quantile_marginalized_%i", i), &out_84quantile_marginalized[i], Form("marginalized 84 per cent quantile of par. %i/D", i));
00418 tree_out->Branch(Form("90quantile_marginalized_%i", i), &out_90quantile_marginalized[i], Form("marginalized 90 per cent quantile of par. %i/D", i));
00419 tree_out->Branch(Form("95quantile_marginalized_%i", i), &out_95quantile_marginalized[i], Form("marginalized 95 per cent quantile of par. %i/D", i));
00420 tree_out->Branch(Form("std_marginalized_%i", i), &out_std_marginalized[i], Form("marginalized std of par. %i/D", i));
00421 }
00422 }
00423 for (int i = 0; i < nchannels; ++i) {
00424 tree_out->Branch(Form("chi2_generated_%i", i), &out_chi2_generated[i], Form("chi2 (generated par.) in channel %i/D", i));
00425 tree_out->Branch(Form("chi2_mode_%i", i), &out_chi2_mode[i], Form("chi2 (mode of par.) in channel %i/D", i));
00426 tree_out->Branch(Form("cash_generated_%i", i), &out_cash_generated[i], Form("cash statistic (generated par.) in channel %i/D", i));
00427 tree_out->Branch(Form("cash_mode_%i", i), &out_cash_mode[i], Form("cash statistic (mode of par.) in channel %i/D", i));
00428 tree_out->Branch(Form("nevents_%i", i), &out_nevents[i], Form("nevents in channel %i/I", i));
00429 }
00430 tree_out->Branch("chi2_generated_total", &out_chi2_generated_total, "chi2 (generated par.) in all channels/D");
00431 tree_out->Branch("chi2_mode_total", &out_chi2_mode_total, "chi2 (mode of par.) in all channels/D");
00432 tree_out->Branch("cash_generated_total", &out_cash_generated_total, "cash statistic (generated par.) in all channels/D");
00433 tree_out->Branch("cash_mode_total", &out_cash_mode_total, "cash statistic (mode of par.) in all channels/D");
00434 tree_out->Branch("nevents_total", &out_nevents_total, "total number of events/I");
00435
00436
00437 for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00438
00439 if ((iensemble+1)%100 == 0 && iensemble > 0) {
00440 BCLog::SetLogLevel(lls,llf);
00441 int frac = double(iensemble+1) / double(nensembles) * 100.;
00442 BCLog::OutDetail(Form("Fraction of ensembles analyzed: %i%%",frac));
00443 BCLog::SetLogLevel(fLogLevel);
00444 }
00445
00446
00447
00448 int index = iensemble + start;
00449 tree->GetEntry(index);
00450
00451
00452 std::vector<TH1D> histograms;
00453
00454
00455 histograms = MatrixToHistograms(nbins_matrix);
00456
00457
00458 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00459
00460 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00461
00462
00463 if (iensemble == 0)
00464 channel->GetData()->SetHistogram(0);
00465
00466
00467 fMTF->SetData(channel->GetName().c_str(), histograms.at(ichannel));
00468 }
00469
00470
00471 if (fFlagMCMC) {
00472 BCLog::SetLogLevel(lls,llf);
00473 BCLog::OutDetail(Form("Running MCMC for ensemble %i",iensemble));
00474 BCLog::SetLogLevel(fLogLevel);
00475
00476
00477 fMTF->MCMCResetResults();
00478
00479
00480 fMTF->MarginalizeAll();
00481
00482
00483 fMTF->FindMode( fMTF->GetBestFitParameters() );
00484 }
00485 else {
00486
00487 fMTF->FindMode();
00488 }
00489
00490
00491 out_mode_global = fMTF->GetBestFitParameters();
00492 out_std_global = fMTF->GetBestFitParameterErrors();
00493 out_chi2_generated_total = 0;
00494 out_chi2_mode_total = 0;
00495 out_cash_generated_total = 0;
00496 out_cash_mode_total = 0;
00497 out_nevents_total = 0;
00498
00499 for (int i = 0; i < nparameters; ++i) {
00500 if (fFlagMCMC) {
00501 BCH1D * hist = fMTF->GetMarginalized( fMTF->GetParameter(i) );
00502 out_mode_marginalized[i] = hist->GetMode();
00503 out_mean_marginalized[i] = hist->GetMean();
00504 out_median_marginalized[i] = hist->GetMedian();
00505 out_5quantile_marginalized[i] = hist->GetQuantile(0.05);
00506 out_10quantile_marginalized[i] = hist->GetQuantile(0.10);
00507 out_16quantile_marginalized[i] = hist->GetQuantile(0.16);
00508 out_84quantile_marginalized[i] = hist->GetQuantile(0.84);
00509 out_90quantile_marginalized[i] = hist->GetQuantile(0.90);
00510 out_95quantile_marginalized[i] = hist->GetQuantile(0.95);
00511 out_std_marginalized[i]=hist->GetRMS();
00512 }
00513 }
00514
00515 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00516
00517 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00518
00519
00520 out_nevents[ichannel] = (int) channel->GetData()->GetHistogram()->Integral();
00521
00522
00523 out_chi2_generated[ichannel] = fMTF->CalculateChi2( ichannel, out_parameters );
00524 out_chi2_mode[ichannel] = fMTF->CalculateChi2( ichannel, fMTF->GetBestFitParameters() );
00525
00526
00527 out_cash_generated[ichannel] = fMTF->CalculateCash( ichannel, out_parameters );
00528 out_cash_mode[ichannel] = fMTF->CalculateCash( ichannel, fMTF->GetBestFitParameters() );
00529
00530
00531 out_nevents_total += out_nevents[ichannel];
00532
00533
00534 out_chi2_generated_total += out_chi2_generated[ichannel];
00535 out_chi2_mode_total += out_chi2_mode[ichannel];
00536
00537
00538 out_cash_generated_total += out_cash_generated[ichannel];
00539 out_cash_mode_total += out_cash_mode[ichannel];
00540 }
00541
00542
00543 tree_out->Fill();
00544 }
00545
00546
00547
00548 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00549
00550 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00551
00552
00553 channel->GetData()->SetHistogram(histograms_data.at(ichannel));
00554 }
00555
00556
00557 BCLog::SetLogLevel(lls,llf);
00558
00559
00560 fMTF->MCMCResetResults();
00561
00562 BCLog::OutSummary("Ensemble test ran successfully.");
00563
00564
00565 return tree_out;
00566 }
00567
00568
00569 std::vector<TH1D> BCMTFAnalysisFacility::MatrixToHistograms(const std::vector< std::vector<double> > & matrix)
00570 {
00571
00572 std::vector<TH1D> histograms;
00573
00574
00575 int nchannels = matrix.size();;
00576
00577
00578 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00579
00580 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00581
00582
00583 std::vector<double> nbins_column = matrix[ichannel];
00584
00585
00586 TH1D hist( *(channel->GetData()->GetHistogram()) );
00587
00588
00589 int nbins = hist.GetNbinsX();
00590
00591
00592 for (int ibin = 1; ibin <= nbins; ++ibin) {
00593 hist.SetBinContent(ibin, nbins_column.at(ibin-1));
00594 }
00595
00596
00597 histograms.push_back(hist);
00598 }
00599
00600
00601 return histograms;
00602
00603 }
00604
00605
00606 int BCMTFAnalysisFacility::PerformSingleChannelAnalyses(const char * dirname, const char * options)
00607 {
00608 BCLog::OutSummary(Form("Running single channel analysis in directory \'%s\'.",dirname));
00609
00610
00611
00612 mkdir(dirname, 0777);
00613 chdir(dirname);
00614
00615
00616
00617 bool flag_syst = true;
00618 bool flag_mcmc = true;
00619
00620 if (std::string(options).find("nosyst") < std::string(options).size())
00621 flag_syst = false;
00622
00623 if (std::string(options).find("mcmc") < std::string(options).size())
00624 flag_mcmc = true;
00625
00626
00627 int nchannels = fMTF->GetNChannels();
00628
00629
00630 int nsystematics = fMTF->GetNSystematics();
00631
00632
00633 std::vector<bool> flag_channel(nchannels);
00634
00635
00636 std::vector<bool> flag_systematic(nsystematics);
00637
00638
00639 std::vector<BCMTFComparisonTool *> ctc;
00640 std::vector<BCMTFComparisonTool *> ctc_mcmc;
00641
00642
00643 int nparameters = fMTF->GetNParameters();
00644
00645
00646
00647
00648 for (int i = 0; i < nparameters; ++ i) {
00649
00650 BCMTFComparisonTool * ct = new BCMTFComparisonTool(fMTF->GetParameter(i)->GetName().c_str());
00651 BCMTFComparisonTool * ct_mcmc = new BCMTFComparisonTool(fMTF->GetParameter(i)->GetName().c_str());
00652
00653
00654 ctc.push_back(ct);
00655 ctc_mcmc.push_back(ct_mcmc);
00656 }
00657
00658
00659
00660
00661 for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00662
00663 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00664
00665
00666 flag_systematic[isystematic] = systematic->GetFlagSystematicActive();
00667
00668
00669 if (flag_systematic[isystematic])
00670 systematic->SetFlagSystematicActive(flag_syst);
00671 }
00672
00673
00674
00675
00676 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00677
00678 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00679
00680
00681 flag_channel[ichannel] = channel->GetFlagChannelActive();
00682
00683
00684 channel->SetFlagChannelActive(true);
00685 }
00686
00687
00688 if (flag_mcmc) {
00689
00690 fMTF->MCMCResetResults();
00691
00692
00693 fMTF->MarginalizeAll();
00694
00695
00696 fMTF->FindMode( fMTF->GetBestFitParameters() );
00697 }
00698 else {
00699
00700 fMTF->FindMode();
00701 }
00702
00703
00704 if (flag_mcmc)
00705 fMTF->PrintAllMarginalized("marginalized_combined.ps");
00706 fMTF->PrintResults("results_combined.txt");
00707
00708
00709 for (int i = 0; i < nparameters; ++ i) {
00710
00711 BCMTFComparisonTool * ct = ctc.at(i);
00712 BCMTFComparisonTool * ct_mcmc = ctc_mcmc.at(i);
00713
00714 ct->AddContribution("all channels",
00715 fMTF->GetBestFitParameters().at(i),
00716 fMTF->GetBestFitParameterErrors().at(i));
00717 if (flag_mcmc) {
00718 BCH1D * hist = fMTF->GetMarginalized( fMTF->GetParameter(i) );
00719
00720 ct_mcmc->AddContribution("all channels",
00721 hist->GetMean(),
00722 hist->GetRMS());
00723 }
00724 }
00725
00726
00727
00728
00729 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00730
00731 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00732
00733
00734 channel->SetFlagChannelActive(false);
00735 }
00736
00737
00738
00739
00740 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00741
00742 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00743
00744
00745 channel->SetFlagChannelActive(true);
00746
00747
00748
00749 if (flag_mcmc) {
00750
00751 fMTF->MCMCResetResults();
00752
00753
00754 fMTF->MarginalizeAll();
00755
00756
00757 fMTF->FindMode( fMTF->GetBestFitParameters() );
00758 }
00759 else {
00760
00761 fMTF->FindMode();
00762 }
00763
00764
00765 if (flag_mcmc)
00766 fMTF->PrintAllMarginalized(Form("marginalized_channel_%i.ps", ichannel));
00767 fMTF->PrintResults(Form("results_channel_%i.txt", ichannel));
00768
00769
00770
00771
00772 for (int i = 0; i < nparameters; ++ i) {
00773
00774 BCMTFComparisonTool * ct = ctc.at(i);
00775 BCMTFComparisonTool * ct_mcmc = ctc_mcmc.at(i);
00776
00777 ct->AddContribution(channel->GetName().c_str(),
00778 fMTF->GetBestFitParameters().at(i),
00779 fMTF->GetBestFitParameterErrors().at(i));
00780 if (flag_mcmc) {
00781 BCH1D * hist = fMTF->GetMarginalized( fMTF->GetParameter(i) );
00782
00783 ct_mcmc->AddContribution(channel->GetName().c_str(),
00784 hist->GetMean(),
00785 hist->GetRMS());
00786 }
00787
00788
00789 channel->SetFlagChannelActive(false);
00790 }
00791 }
00792
00793
00794
00795 for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00796
00797 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00798
00799
00800 if (flag_systematic[isystematic])
00801 systematic->SetFlagSystematicActive(flag_systematic[isystematic]);
00802 }
00803
00804
00805
00806
00807 for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00808
00809 BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00810
00811
00812 channel->SetFlagChannelActive(flag_channel[ichannel]);
00813 }
00814
00815
00816 fMTF->MCMCResetResults();
00817
00818
00819 TCanvas * c1 = new TCanvas();
00820 c1->cd();
00821
00822
00823 BCMTFComparisonTool * ct = ctc.at(0);
00824 ct->DrawOverview();
00825
00826 c1->Print((std::string("overview.ps")+std::string("(")).c_str());
00827
00828
00829 for (int i = 1; i < nparameters-1; ++ i) {
00830
00831 BCMTFComparisonTool * ct = ctc.at(i);
00832
00833 ct->DrawOverview();
00834 c1->Print("overview.ps");
00835 }
00836
00837
00838 ct = ctc.at(nparameters-1);
00839 ct->DrawOverview();
00840 c1->Print((std::string("overview.ps")+std::string(")")).c_str());
00841
00842
00843 if (flag_mcmc) {
00844 TCanvas * c2 = new TCanvas();
00845 c2->cd();
00846
00847
00848 BCMTFComparisonTool * ct_mcmc = ctc_mcmc.at(0);
00849 ct_mcmc->DrawOverview();
00850
00851 c2->Print((std::string("overview_mcmc.ps")+std::string("(")).c_str());
00852
00853
00854 for (int i = 1; i < nparameters-1; ++ i) {
00855
00856 BCMTFComparisonTool * ct_mcmc = ctc_mcmc.at(i);
00857
00858 ct_mcmc->DrawOverview();
00859 c2->Print("overview_mcmc.ps");
00860 }
00861
00862
00863 ct_mcmc = ctc_mcmc.at(nparameters-1);
00864 ct_mcmc->DrawOverview();
00865 c2->Print((std::string("overview_mcmc.ps")+std::string(")")).c_str());
00866 delete c2;
00867 }
00868
00869
00870 for (int i = 0; i < nparameters; ++i) {
00871 BCMTFComparisonTool * ct = ctc[i];
00872 BCMTFComparisonTool * ct_mcmc = ctc_mcmc[i];
00873 delete ct;
00874 delete ct_mcmc;
00875 }
00876 ctc.clear();
00877 ctc_mcmc.clear();
00878
00879 delete c1;
00880
00881
00882
00883 chdir("../");
00884
00885 BCLog::OutSummary("Single channel analysis ran successfully");
00886
00887
00888 return 1;
00889 }
00890
00891
00892 int BCMTFAnalysisFacility::PerformSingleSystematicAnalyses(const char * dirname, const char * options)
00893 {
00894 BCLog::OutSummary(Form("Running single channel systematic analysis in directory \'%s\'.",dirname));
00895
00896
00897
00898 mkdir(dirname, 0777);
00899 chdir(dirname);
00900
00901
00902
00903 bool flag_mcmc = true;
00904
00905 if (std::string(options).find("mcmc") < std::string(options).size())
00906 flag_mcmc = true;
00907
00908
00909 int nchannels = fMTF->GetNChannels();
00910
00911
00912 int nsystematics = fMTF->GetNSystematics();
00913
00914
00915 std::vector<bool> flag_channel(nchannels);
00916
00917
00918 std::vector<bool> flag_systematic(nsystematics);
00919
00920
00921 std::vector<BCMTFComparisonTool *> ctc;
00922
00923
00924 int nparameters = fMTF->GetNParameters();
00925
00926
00927
00928
00929 for (int i = 0; i < nparameters; ++ i) {
00930
00931 BCMTFComparisonTool * ct = new BCMTFComparisonTool(fMTF->GetParameter(i)->GetName().c_str());
00932
00933
00934 ctc.push_back(ct);
00935 }
00936
00937
00938
00939 for (int isystematic = 0; isystematic < nsystematics; ++ isystematic) {
00940
00941 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00942
00943
00944 flag_systematic[isystematic] = systematic->GetFlagSystematicActive();
00945
00946
00947 systematic->SetFlagSystematicActive(true);
00948 }
00949
00950 if (flag_mcmc) {
00951
00952 fMTF->MCMCResetResults();
00953
00954
00955 fMTF->MarginalizeAll();
00956
00957
00958 fMTF->FindMode( fMTF->GetBestFitParameters() );
00959 }
00960 else {
00961
00962 fMTF->FindMode();
00963 }
00964
00965
00966 if (flag_mcmc)
00967 fMTF->PrintAllMarginalized("marginalized_all.ps");
00968 fMTF->PrintResults("results_all.txt");
00969
00970
00971 for (int i = 0; i < nparameters; ++ i) {
00972
00973 BCMTFComparisonTool * ct = ctc.at(i);
00974
00975 ct->AddContribution("all systematics",
00976 fMTF->GetBestFitParameters().at(i),
00977 fMTF->GetBestFitParameterErrors().at(i));
00978 }
00979
00980
00981
00982
00983 for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00984
00985 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00986
00987
00988 systematic->SetFlagSystematicActive(false);
00989 }
00990
00991
00992
00993
00994 for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00995
00996 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00997
00998
00999 systematic->SetFlagSystematicActive(true);
01000
01001
01002 if (flag_mcmc) {
01003
01004 fMTF->MCMCResetResults();
01005
01006
01007 fMTF->MarginalizeAll();
01008
01009
01010 fMTF->FindMode( fMTF->GetBestFitParameters() );
01011 }
01012 else {
01013
01014 fMTF->FindMode();
01015 }
01016
01017
01018 if (flag_mcmc)
01019 fMTF->PrintAllMarginalized(Form("marginalized_systematic_%i.ps", isystematic));
01020 fMTF->PrintResults(Form("results_systematic_%i.txt", isystematic));
01021
01022
01023
01024
01025 for (int i = 0; i < nparameters; ++ i) {
01026
01027 BCMTFComparisonTool * ct = ctc.at(i);
01028
01029 ct->AddContribution(systematic->GetName().c_str(),
01030 fMTF->GetBestFitParameters().at(i),
01031 fMTF->GetBestFitParameterErrors().at(i));
01032 }
01033
01034
01035 systematic->SetFlagSystematicActive(false);
01036 }
01037
01038
01039
01040 if (flag_mcmc) {
01041
01042 fMTF->MCMCResetResults();
01043
01044
01045 fMTF->MarginalizeAll();
01046
01047
01048 fMTF->FindMode( fMTF->GetBestFitParameters() );
01049 }
01050 else {
01051
01052 fMTF->FindMode();
01053 }
01054
01055
01056 if (flag_mcmc)
01057 fMTF->PrintAllMarginalized("marginalized_none.ps");
01058 fMTF->PrintResults("results_none.txt");
01059
01060
01061 for (int i = 0; i < nparameters; ++ i) {
01062
01063 BCMTFComparisonTool * ct = ctc.at(i);
01064
01065 ct->AddContribution("no systematics",
01066 fMTF->GetBestFitParameters().at(i),
01067 fMTF->GetBestFitParameterErrors().at(i));
01068 }
01069
01070
01071
01072
01073
01074
01075 for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
01076
01077 BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
01078
01079
01080 if (flag_systematic[isystematic])
01081 systematic->SetFlagSystematicActive(flag_systematic[isystematic]);
01082 }
01083
01084
01085 fMTF->MCMCResetResults();
01086
01087
01088 TCanvas * c1 = new TCanvas();
01089 c1->cd();
01090
01091
01092 BCMTFComparisonTool * ct = ctc.at(0);
01093 ct->DrawOverview();
01094
01095 c1->Print((std::string("overview.ps")+std::string("(")).c_str());
01096
01097
01098 for (int i = 1; i < nparameters-1; ++i) {
01099
01100 BCMTFComparisonTool * ct = ctc.at(i);
01101
01102 ct->DrawOverview();
01103 c1->Print("overview.ps");
01104 }
01105
01106
01107 ct = ctc.at(nparameters-1);
01108 ct->DrawOverview();
01109 c1->Print((std::string("overview.ps")+std::string(")")).c_str());
01110
01111
01112 for (int i = 0; i < nparameters; ++i) {
01113 BCMTFComparisonTool * ct = ctc[i];
01114 delete ct;
01115 }
01116 ctc.clear();
01117
01118 delete c1;
01119
01120
01121
01122 chdir("../");
01123
01124 BCLog::OutSummary("Single channel analysis ran successfully");
01125
01126
01127 return 1;
01128 }
01129
01130
01131 int BCMTFAnalysisFacility::PerformCalibrationAnalysis(const char * dirname, const std::vector<double> & default_parameters, int index, const std::vector<double> & parametervalues, int nensembles)
01132 {
01133 BCLog::OutSummary(Form("Running calibration analysis in directory \'%s\'.",dirname));
01134
01135
01136
01137 mkdir(dirname, 0777);
01138 chdir(dirname);
01139
01140
01141
01142 int nvalues = int(parametervalues.size());
01143 for (int ivalue = 0; ivalue < nvalues; ++ivalue) {
01144
01145
01146 TFile * file = new TFile(Form("ensemble_%i.root", ivalue), "RECREATE");
01147 file->cd();
01148
01149
01150 std::vector<double> parameters = default_parameters;
01151 parameters[index] = parametervalues.at(ivalue);
01152
01153
01154 TTree * tree = PerformEnsembleTest(parameters, nensembles);
01155
01156
01157 tree->Write();
01158
01159
01160 file->Close();
01161
01162
01163 delete file;
01164 }
01165
01166
01167
01168 chdir("../");
01169
01170 BCLog::OutSummary("Calibration analysis ran successfully");
01171
01172
01173 return 1;
01174 }
01175
01176