• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCMTFAnalysisFacility.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2012, Daniel Kollar and Kevin Kroeninger.
00003  * All rights reserved.
00004  *
00005  * For the licensing terms see doc/COPYING.
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    // get number of channels
00050    int nchannels = fMTF->GetNChannels();
00051 
00052    // create vector of histograms
00053    std::vector<TH1D> histograms;
00054 
00055    // loop over channels
00056    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00057 
00058       // get channel
00059       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00060 
00061       // create new histogram
00062       TH1D hist( *(channel->GetData()->GetHistogram()) );
00063 
00064       // get number of bins
00065       int nbins = hist.GetNbinsX();
00066 
00067       // loop over all bins
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       // add histogram
00077       histograms.push_back(hist);
00078    }
00079 
00080    // return histograms
00081    return histograms;
00082 }
00083 
00084 // ---------------------------------------------------------
00085 TTree * BCMTFAnalysisFacility::BuildEnsembles(TTree * tree, int nensembles)
00086 {
00087    // get number of channels
00088    int nchannels = fMTF->GetNChannels();
00089 
00090    BCLog::OutDetail(Form("MTF Building %d ensambles for %d channels.",nensembles,nchannels));
00091 
00092    // get number of parameters
00093    int nparameters = fMTF->GetNParameters();
00094 
00095    // create tree variables for input tree
00096    std::vector<double> parameters(nparameters);
00097 
00098    // set branch addresses
00099    for (int i = 0; i < nparameters; ++i) {
00100       tree->SetBranchAddress(Form("Parameter%i", i), &(parameters[i]));
00101    }
00102 
00103    // create tree
00104    TTree * tree_out = new TTree("ensembles", "ensembles");
00105 
00106    // create matrix of number of bins
00107    std::vector< std::vector<double> > nbins_matrix;
00108 
00109    // prepare the tree variables
00110    // loop over channels
00111    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00112       // get channel
00113       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00114 
00115       // get number of bins
00116       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00117 
00118       // create new matrix row
00119       std::vector<double> nbins_column(nbins);
00120 
00121       // add matrix
00122       nbins_matrix.push_back(nbins_column);
00123    }
00124 
00125    std::vector<double> in_parameters(nparameters);
00126 
00127    // create branches
00128    // loop over channels
00129    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00130       // get channel
00131       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00132 
00133       // get number of bins
00134       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00135 
00136       // loop over bins
00137       for (int ibin = 1; ibin <= nbins; ++ibin) {
00138          // create branches
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    // create vector of histograms
00149    std::vector<TH1D> histograms;
00150 
00151    // loop over ensembles
00152    for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00153       // get random event from tree
00154       int index = (int) fRandom->Uniform(tree->GetEntries());
00155       tree->GetEntry(index);
00156 
00157       // create ensembles
00158       histograms = BuildEnsemble(parameters);
00159 
00160       // copy information from histograms into tree variables
00161       // loop over channels
00162       for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00163          // get channel
00164          BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00165 
00166          // get number of bins
00167          int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00168 
00169          // loop over bins
00170          for (int ibin = 1; ibin <= nbins; ++ibin) {
00171             // fill tree variable
00172             (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
00173          }
00174       }
00175 
00176       // copy parameter information
00177       for (int i = 0; i < nparameters; ++i) {
00178          in_parameters[i] = parameters.at(i);
00179       }
00180 
00181       // fill tree
00182       tree_out->Fill();
00183    }
00184 
00185    // return tree
00186    return tree_out;
00187 }
00188 
00189 // ---------------------------------------------------------
00190 TTree * BCMTFAnalysisFacility::BuildEnsembles(const std::vector<double> & parameters, int nensembles)
00191 {
00192    // get number of channels
00193    int nchannels = fMTF->GetNChannels();
00194 
00195    BCLog::OutDetail(Form("MTF Building %d ensambles for %d channels.",nensembles,nchannels));
00196 
00197    // create tree
00198    TTree * tree = new TTree("ensembles", "ensembles");
00199 
00200    // create matrix of number of bins
00201    std::vector< std::vector<double> > nbins_matrix;
00202 
00203    // prepare the tree variables
00204    // loop over channels
00205    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00206       // get channel
00207       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00208 
00209       // get number of bins
00210       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00211 
00212       // create new matrix row
00213       std::vector<double> nbins_column(nbins);
00214 
00215       // add matrix
00216       nbins_matrix.push_back(nbins_column);
00217    }
00218 
00219    // get number of parameters
00220    int nparameters = fMTF->GetNParameters();
00221 
00222    std::vector<double> in_parameters(nparameters);
00223 
00224    // create branches
00225    // loop over channels
00226    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00227       // get channel
00228       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00229 
00230       // get number of bins
00231       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00232 
00233       // loop over bins
00234       for (int ibin = 1; ibin <= nbins; ++ibin) {
00235          // create branches
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    // create vector of histograms
00246    std::vector<TH1D> histograms;
00247 
00248    // loop over ensembles
00249    for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00250       // create ensembles
00251       histograms = BuildEnsemble(parameters);
00252 
00253       // copy information from histograms into tree variables
00254       // loop over channels
00255       for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00256          // get channel
00257          BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00258 
00259          // get number of bins
00260          int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00261 
00262          // loop over bins
00263          for (int ibin = 1; ibin <= nbins; ++ibin) {
00264             // fill tree variable
00265             (nbins_matrix[ichannel])[ibin-1] = histograms.at(ichannel).GetBinContent(ibin);
00266          }
00267       }
00268 
00269       // copy parameter information
00270       for (int i = 0; i < nparameters; ++i) {
00271          in_parameters[i] = parameters.at(i);
00272       }
00273 
00274       // fill tree
00275       tree->Fill();
00276    }
00277 
00278    // return tree
00279    return tree;
00280 }
00281 
00282 // ---------------------------------------------------------
00283 TTree * BCMTFAnalysisFacility::PerformEnsembleTest(const std::vector<double> & parameters, int nensembles)
00284 {
00285    // create new tree
00286    TTree * tree = 0;
00287 
00288    // create ensembles
00289    tree = BuildEnsembles(parameters, nensembles);
00290 
00291    // perform ensemble test
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    // set log level
00304    // It would be better to set the level for the screen and for the file
00305    // separately. Perhaps in the future.
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    // get number of channels
00318    int nchannels = fMTF->GetNChannels();
00319 
00320    // define set of histograms for the original data sets
00321    std::vector<TH1D *> histograms_data(nchannels);
00322 
00323    // create matrix of number of bins
00324    std::vector< std::vector<double> > nbins_matrix;
00325 
00326    // prepare the tree
00327    // loop over channels
00328    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00329       // get channel
00330       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00331 
00332       // get number of bins
00333       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00334 
00335       // create new matrix row
00336       std::vector<double> nbins_column(nbins);
00337 
00338       // add matrix
00339       nbins_matrix.push_back(nbins_column);
00340    }
00341 
00342    // loop over channels
00343    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00344       // get channel
00345       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00346 
00347       // get number of bins
00348       int nbins = channel->GetData()->GetHistogram()->GetNbinsX();
00349 
00350       // loop over bins
00351       for (int ibin = 1; ibin <= nbins; ++ibin) {
00352          // create branches
00353          tree->SetBranchAddress(Form("channel_%i_bin_%i", ichannel, ibin),
00354                                            &(nbins_matrix[ichannel])[ibin-1]);
00355       }
00356    }
00357 
00358    // get number of parameters
00359    int nparameters = fMTF->GetNParameters();
00360 
00361    // define tree variables
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    // copy the original data sets
00369    // loop over channels
00370    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00371       // get channel
00372       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00373 
00374       // set data pointer
00375       histograms_data[ichannel] = channel->GetData()->GetHistogram();
00376    }
00377 
00378    // create output tree
00379    TTree * tree_out = new TTree("ensemble_test", "ensemble test");
00380 
00381    // define tree variables
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    // create branches
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    // loop over ensembles
00437    for (int iensemble = 0; iensemble < nensembles; ++iensemble) {
00438       // print status
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       // get next (commented out: random) event from tree
00447       //      int index = (int) fRandom->Uniform(tree->GetEntries());
00448       int index = iensemble + start;
00449       tree->GetEntry(index);
00450 
00451       // define histograms
00452       std::vector<TH1D> histograms;
00453 
00454       // transform matrix into histograms
00455       histograms = MatrixToHistograms(nbins_matrix);
00456 
00457       // loop over channels
00458       for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00459          // get channel
00460          BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00461 
00462          // overwrite data
00463          if (iensemble == 0)
00464             channel->GetData()->SetHistogram(0);
00465 
00466          // set data histogram
00467          fMTF->SetData(channel->GetName().c_str(), histograms.at(ichannel));
00468       }
00469 
00470       // check if MCMC should be run and perform analysis
00471       if (fFlagMCMC) {
00472          BCLog::SetLogLevel(lls,llf);
00473          BCLog::OutDetail(Form("Running MCMC for ensemble %i",iensemble));
00474          BCLog::SetLogLevel(fLogLevel);
00475 
00476          // work-around: force initialization
00477          fMTF->MCMCResetResults();
00478 
00479          // run mcmc
00480          fMTF->MarginalizeAll();
00481 
00482          // find mode
00483          fMTF->FindMode( fMTF->GetBestFitParameters() );
00484       }
00485       else {
00486          // find mode
00487          fMTF->FindMode();
00488       }
00489 
00490       // fill tree variables
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          // get channel
00517          BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00518 
00519          // get number of events
00520          out_nevents[ichannel] = (int) channel->GetData()->GetHistogram()->Integral();
00521 
00522          // calculate chi2
00523          out_chi2_generated[ichannel] = fMTF->CalculateChi2( ichannel, out_parameters );
00524          out_chi2_mode[ichannel] = fMTF->CalculateChi2( ichannel, fMTF->GetBestFitParameters() );
00525 
00526          // calculate cash statistic
00527          out_cash_generated[ichannel] = fMTF->CalculateCash( ichannel, out_parameters );
00528          out_cash_mode[ichannel] = fMTF->CalculateCash( ichannel, fMTF->GetBestFitParameters() );
00529 
00530          // increase the total number of events
00531          out_nevents_total += out_nevents[ichannel];
00532 
00533          // increase the total chi2
00534          out_chi2_generated_total += out_chi2_generated[ichannel];
00535          out_chi2_mode_total += out_chi2_mode[ichannel];
00536 
00537          // increase the total cash statistic
00538          out_cash_generated_total += out_cash_generated[ichannel];
00539          out_cash_mode_total += out_cash_mode[ichannel];
00540       }
00541 
00542       // fill tree
00543       tree_out->Fill();
00544    }
00545 
00546    // put the original data back in place
00547    // loop over channels
00548    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00549       // get channel
00550       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00551 
00552       // set data pointer
00553       channel->GetData()->SetHistogram(histograms_data.at(ichannel));
00554    }
00555 
00556    // reset log level
00557    BCLog::SetLogLevel(lls,llf);
00558 
00559    // work-around: force initialization
00560    fMTF->MCMCResetResults();
00561 
00562    BCLog::OutSummary("Ensemble test ran successfully.");
00563 
00564    // return output tree
00565    return tree_out;
00566 }
00567 
00568 // ---------------------------------------------------------
00569 std::vector<TH1D> BCMTFAnalysisFacility::MatrixToHistograms(const std::vector< std::vector<double> > & matrix)
00570 {
00571    // create vector of histograms
00572    std::vector<TH1D> histograms;
00573 
00574    // get number of channels
00575    int nchannels = matrix.size();;
00576 
00577    // loop over channels
00578    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00579       // get channel
00580       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00581 
00582       // get column
00583       std::vector<double> nbins_column = matrix[ichannel];
00584 
00585       // create new histogram
00586       TH1D hist( *(channel->GetData()->GetHistogram()) );
00587 
00588       // get number of bins
00589       int nbins = hist.GetNbinsX();
00590 
00591       // fill bin content
00592       for (int ibin = 1; ibin <= nbins; ++ibin) {
00593          hist.SetBinContent(ibin, nbins_column.at(ibin-1));
00594       }
00595 
00596       // add histogram to container
00597       histograms.push_back(hist);
00598    }
00599 
00600    // return histograms
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    // ---- create new directory ---- //
00611 
00612    mkdir(dirname, 0777);
00613    chdir(dirname);
00614 
00615    // ---- check options ---- //
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    // get number of channels
00627    int nchannels = fMTF->GetNChannels();
00628 
00629    // get number of systematics
00630    int nsystematics = fMTF->GetNSystematics();
00631 
00632    // container of flags for channels
00633    std::vector<bool> flag_channel(nchannels);
00634 
00635    // container of flags for systematics
00636    std::vector<bool> flag_systematic(nsystematics);
00637 
00638    // create new container of comparison tools
00639    std::vector<BCMTFComparisonTool *> ctc;
00640    std::vector<BCMTFComparisonTool *> ctc_mcmc;
00641 
00642    // get number of parameters
00643    int nparameters = fMTF->GetNParameters();
00644 
00645    // ---- add one comparison tool for each parameter ---- //
00646 
00647    // loop over all parameters
00648    for (int i = 0; i < nparameters; ++ i) {
00649       // create new comparison tool
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       // add comparison tool to container
00654       ctc.push_back(ct);
00655       ctc_mcmc.push_back(ct_mcmc);
00656    }
00657 
00658    // ---- switch on/off all systematics ---- //
00659 
00660    // loop over all systematics
00661    for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00662       // get systematic
00663       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00664 
00665       // remember old setting
00666       flag_systematic[isystematic] = systematic->GetFlagSystematicActive();
00667 
00668       // switch off systematic
00669       if (flag_systematic[isystematic])
00670          systematic->SetFlagSystematicActive(flag_syst);
00671    }
00672 
00673    // ---- switch on all channels ---- //
00674 
00675    // loop over all channels
00676    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00677       // get channel
00678       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00679 
00680       // remember old setting
00681       flag_channel[ichannel] = channel->GetFlagChannelActive();
00682 
00683       // switch channel on/off
00684       channel->SetFlagChannelActive(true);
00685    }
00686 
00687    // ---- perform analysis for combination ---- //
00688    if (flag_mcmc) {
00689       // work-around: force initialization
00690       fMTF->MCMCResetResults();
00691 
00692       // run mcmc
00693       fMTF->MarginalizeAll();
00694 
00695       // find mode
00696       fMTF->FindMode( fMTF->GetBestFitParameters() );
00697    }
00698    else {
00699       // find mode
00700       fMTF->FindMode();
00701    }
00702 
00703    // print results
00704    if (flag_mcmc)
00705       fMTF->PrintAllMarginalized("marginalized_combined.ps");
00706    fMTF->PrintResults("results_combined.txt");
00707 
00708    // loop over all parameters
00709    for (int i = 0; i < nparameters; ++ i) {
00710       // get comparison tool
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    // ---- switch off all channels ----//
00727 
00728    // loop over all channels
00729    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00730       // get channel
00731       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00732 
00733       // switch off channel
00734       channel->SetFlagChannelActive(false);
00735    }
00736 
00737    // ---- perform analysis on all channels separately ---- //
00738 
00739    // loop over all channels
00740    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00741       // get channel
00742       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00743 
00744       // switch on one channel
00745       channel->SetFlagChannelActive(true);
00746 
00747       // perform analysis
00748 
00749       if (flag_mcmc) {
00750          // work-around: force initialization
00751          fMTF->MCMCResetResults();
00752 
00753          // run mcmc
00754          fMTF->MarginalizeAll();
00755 
00756          // find mode
00757          fMTF->FindMode( fMTF->GetBestFitParameters() );
00758       }
00759       else {
00760          // find mode
00761          fMTF->FindMode();
00762       }
00763 
00764       // print results
00765       if (flag_mcmc)
00766          fMTF->PrintAllMarginalized(Form("marginalized_channel_%i.ps", ichannel));
00767       fMTF->PrintResults(Form("results_channel_%i.txt", ichannel));
00768 
00769       // ---- update comparison tools ---- //
00770 
00771       // loop over all parameters
00772       for (int i = 0; i < nparameters; ++ i) {
00773          // get comparison tool
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          // switch off channel
00789          channel->SetFlagChannelActive(false);
00790       }
00791    }
00792 
00793    // ---- reset all systematics ---- //
00794    // loop over all systematics
00795    for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00796       // get systematic
00797       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00798 
00799       // switch off systematic
00800       if (flag_systematic[isystematic])
00801          systematic->SetFlagSystematicActive(flag_systematic[isystematic]);
00802    }
00803 
00804    // ---- reset all channels ---- //
00805 
00806    // loop over all channels
00807    for (int ichannel = 0; ichannel < nchannels; ++ichannel) {
00808       // get channel
00809       BCMTFChannel * channel = fMTF->GetChannel(ichannel);
00810 
00811       // switch channel on/off
00812       channel->SetFlagChannelActive(flag_channel[ichannel]);
00813    }
00814 
00815    // ---- workaround: reset MCMC ---- //
00816    fMTF->MCMCResetResults();
00817 
00818    // ---- print everything ---- //
00819    TCanvas * c1 = new TCanvas();
00820    c1->cd();
00821 
00822    // draw first one
00823    BCMTFComparisonTool * ct =  ctc.at(0);
00824    ct->DrawOverview();
00825    //   c1->Print((std::string(filename)+std::string("(")).c_str());
00826    c1->Print((std::string("overview.ps")+std::string("(")).c_str());
00827 
00828    // loop over all parameters
00829    for (int i = 1; i < nparameters-1; ++ i) {
00830       // create new comparison tool
00831       BCMTFComparisonTool * ct = ctc.at(i);
00832 
00833       ct->DrawOverview();
00834       c1->Print("overview.ps");
00835    }
00836 
00837    // draw last one
00838    ct =  ctc.at(nparameters-1);
00839    ct->DrawOverview();
00840    c1->Print((std::string("overview.ps")+std::string(")")).c_str());
00841 
00842    // ---- print everything (mcmc) ---- //
00843    if (flag_mcmc) {
00844       TCanvas * c2 = new TCanvas();
00845       c2->cd();
00846 
00847       // draw first one
00848       BCMTFComparisonTool * ct_mcmc =  ctc_mcmc.at(0);
00849       ct_mcmc->DrawOverview();
00850       //   c2->Print((std::string(filename)+std::string("(")).c_str());
00851       c2->Print((std::string("overview_mcmc.ps")+std::string("(")).c_str());
00852 
00853       // loop over all parameters
00854       for (int i = 1; i < nparameters-1; ++ i) {
00855          // create new comparison tool
00856          BCMTFComparisonTool * ct_mcmc = ctc_mcmc.at(i);
00857 
00858          ct_mcmc->DrawOverview();
00859          c2->Print("overview_mcmc.ps");
00860       }
00861 
00862       // draw last one
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    // ---- free memory ---- //
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    // ---- change directory ---- //
00882 
00883    chdir("../");
00884 
00885    BCLog::OutSummary("Single channel analysis ran successfully");
00886 
00887    // no error
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    // ---- create new directory ---- //
00897 
00898    mkdir(dirname, 0777);
00899    chdir(dirname);
00900 
00901    // ---- check options ---- //
00902 
00903    bool flag_mcmc = true;
00904 
00905    if (std::string(options).find("mcmc") < std::string(options).size())
00906       flag_mcmc = true;
00907 
00908    // get number of channels
00909    int nchannels = fMTF->GetNChannels();
00910 
00911    // get number of systematics
00912    int nsystematics = fMTF->GetNSystematics();
00913 
00914    // container of flags for channels
00915    std::vector<bool> flag_channel(nchannels);
00916 
00917    // container of flags for systematics
00918    std::vector<bool> flag_systematic(nsystematics);
00919 
00920    // create new container of comparison tools
00921    std::vector<BCMTFComparisonTool *> ctc;
00922 
00923    // get number of parameters
00924    int nparameters = fMTF->GetNParameters();
00925 
00926    // ---- add one comparison tool for each systematic ---- //
00927 
00928    // loop over all parameters
00929    for (int i = 0; i < nparameters; ++ i) {
00930       // create new comparison tool
00931       BCMTFComparisonTool * ct = new BCMTFComparisonTool(fMTF->GetParameter(i)->GetName().c_str());
00932 
00933       // add comparison tool to container
00934       ctc.push_back(ct);
00935    }
00936 
00937    // ---- switch on all systematics ---- //
00938 
00939    for (int isystematic = 0; isystematic < nsystematics; ++ isystematic) {
00940       // get systematic
00941       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00942 
00943       // remember old setting
00944       flag_systematic[isystematic] = systematic->GetFlagSystematicActive();
00945 
00946       // switch on
00947       systematic->SetFlagSystematicActive(true);
00948    }
00949 
00950    if (flag_mcmc) {
00951       // work-around: force initialization
00952       fMTF->MCMCResetResults();
00953 
00954       // run mcmc
00955       fMTF->MarginalizeAll();
00956 
00957       // find mode
00958       fMTF->FindMode( fMTF->GetBestFitParameters() );
00959    }
00960    else {
00961       // find mode
00962       fMTF->FindMode();
00963    }
00964 
00965    // print results
00966    if (flag_mcmc)
00967       fMTF->PrintAllMarginalized("marginalized_all.ps");
00968    fMTF->PrintResults("results_all.txt");
00969 
00970    // loop over all parameters
00971    for (int i = 0; i < nparameters; ++ i) {
00972       // get comparison tool
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    // ---- switch off all systematics ---- //
00981 
00982    // loop over all systematics
00983    for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00984       // get systematic
00985       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00986 
00987       // switch off
00988       systematic->SetFlagSystematicActive(false);
00989    }
00990 
00991    // ---- perform analysis with all systematics separately ---- //
00992 
00993    // loop over all channels
00994    for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
00995       // get systematic
00996       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
00997 
00998       // switch on systematic
00999       systematic->SetFlagSystematicActive(true);
01000 
01001       // perform analysis
01002       if (flag_mcmc) {
01003          // work-around: force initialization
01004          fMTF->MCMCResetResults();
01005 
01006          // run mcmc
01007          fMTF->MarginalizeAll();
01008 
01009          // find mode
01010          fMTF->FindMode( fMTF->GetBestFitParameters() );
01011       }
01012       else {
01013          // find mode
01014          fMTF->FindMode();
01015       }
01016 
01017       // print results
01018       if (flag_mcmc)
01019          fMTF->PrintAllMarginalized(Form("marginalized_systematic_%i.ps", isystematic));
01020       fMTF->PrintResults(Form("results_systematic_%i.txt", isystematic));
01021 
01022       // ---- update comparison tools ---- //
01023 
01024       // loop over all parameters
01025       for (int i = 0; i < nparameters; ++ i) {
01026          // get comparison tool
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       // switch off systematic
01035       systematic->SetFlagSystematicActive(false);
01036    }
01037 
01038    // ---- analysis without any systematic uncertainty ---- //
01039 
01040    if (flag_mcmc) {
01041       // work-around: force initialization
01042       fMTF->MCMCResetResults();
01043 
01044       // run mcmc
01045       fMTF->MarginalizeAll();
01046 
01047       // find mode
01048       fMTF->FindMode( fMTF->GetBestFitParameters() );
01049    }
01050    else {
01051       // find mode
01052       fMTF->FindMode();
01053    }
01054 
01055    // print results
01056    if (flag_mcmc)
01057       fMTF->PrintAllMarginalized("marginalized_none.ps");
01058    fMTF->PrintResults("results_none.txt");
01059 
01060    // loop over all parameters
01061    for (int i = 0; i < nparameters; ++ i) {
01062       // get comparison tool
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    // ---- reset all systematics ---- //
01073 
01074    // loop over all systematics
01075    for (int isystematic = 0; isystematic < nsystematics; ++isystematic) {
01076       // get systematic
01077       BCMTFSystematic * systematic = fMTF->GetSystematic(isystematic);
01078 
01079       // switch off systematic
01080       if (flag_systematic[isystematic])
01081          systematic->SetFlagSystematicActive(flag_systematic[isystematic]);
01082    }
01083 
01084    // ---- workaround: reset MCMC ---- //
01085    fMTF->MCMCResetResults();
01086 
01087    // ---- print everything ---- //
01088    TCanvas * c1 = new TCanvas();
01089    c1->cd();
01090 
01091    // draw first one
01092    BCMTFComparisonTool * ct =  ctc.at(0);
01093    ct->DrawOverview();
01094    //   c1->Print((std::string(filename)+std::string("(")).c_str());
01095    c1->Print((std::string("overview.ps")+std::string("(")).c_str());
01096 
01097    // loop over all parameters
01098    for (int i = 1; i < nparameters-1; ++i) {
01099       // create new comparison tool
01100       BCMTFComparisonTool * ct = ctc.at(i);
01101 
01102       ct->DrawOverview();
01103       c1->Print("overview.ps");
01104    }
01105 
01106    // draw last one
01107    ct =  ctc.at(nparameters-1);
01108    ct->DrawOverview();
01109    c1->Print((std::string("overview.ps")+std::string(")")).c_str());
01110 
01111    // ---- free memory ---- //
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    // ---- change directory ---- //
01121 
01122    chdir("../");
01123 
01124    BCLog::OutSummary("Single channel analysis ran successfully");
01125 
01126    // no error
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    // ---- create new directory ---- //
01136 
01137    mkdir(dirname, 0777);
01138    chdir(dirname);
01139 
01140    // ---- loop over parameter values and perform analysis  ---- //
01141 
01142    int nvalues = int(parametervalues.size());
01143    for (int ivalue = 0; ivalue < nvalues; ++ivalue) {
01144 
01145       // open file
01146       TFile * file = new TFile(Form("ensemble_%i.root", ivalue), "RECREATE");
01147       file->cd();
01148 
01149       // set parameters
01150       std::vector<double> parameters = default_parameters;
01151       parameters[index] = parametervalues.at(ivalue);
01152 
01153       // create ensemble
01154       TTree * tree = PerformEnsembleTest(parameters, nensembles);
01155 
01156       // write tree
01157       tree->Write();
01158 
01159       // close file
01160       file->Close();
01161 
01162       // free memory
01163       delete file;
01164    }
01165 
01166    // ---- change directory ---- //
01167 
01168    chdir("../");
01169 
01170    BCLog::OutSummary("Calibration analysis ran successfully");
01171 
01172    // no error
01173    return 1;
01174 }
01175 
01176 // ---------------------------------------------------------

Generated by  doxygen 1.7.1