Exemple #1
0
void ensembleTest()
{
    // open log file
    BCLog::OpenLog("log.txt", BCLog::detail, BCLog::detail);

    // ---- read histograms from a file ---- //

    // open file
    std::string fname = "templates.root";
    TFile* file = TFile::Open(fname.data(), "READ");

    // check if file is open
    if (!file || !file->IsOpen()) {
        BCLog::OutError(Form("Could not open file %s.", fname.c_str()));
        BCLog::OutError("Run macro CreateHistograms.C in Root to create the file.");
        return;
    }

    // read histograms
    TH1D* hist_bkg1  = (TH1D*)file->Get("hist_bkg1");    // background template for channel 1
    TH1D* hist_bkg2  = (TH1D*)file->Get("hist_bkg2");    // background template for channel 2
    TH1D* hist_sgn1  = (TH1D*)file->Get("hist_sgn1");    // signal template for channel 1
    TH1D* hist_sgn2  = (TH1D*)file->Get("hist_sgn2");    // signal template for channel 2
    TH1D* hist_data1 = (TH1D*)file->Get("hist_data1");  // data for channel 1
    TH1D* hist_data2 = (TH1D*)file->Get("hist_data2");  // data for channel 2

    if (!hist_bkg1 || !hist_bkg2 || !hist_sgn1 || !hist_sgn2 || !hist_data1 || !hist_data2) {
        BCLog::OutError("Could not find data histograms");
        return;
    }

    // ---- perform fitting ---- //

    // create new fitter object
    BCMTF* m = new BCMTF("MyModel");

    // set Metropolis as marginalization method
    m->SetMarginalizationMethod(BCIntegrate::kMargMetropolis);

    // set the required precision of the MCMC (kLow, kQuick, kMedium, kHigh)
    // the higher the precision the longer the MCMC run
    m->SetPrecision(BCEngineMCMC::kQuick);

    // add channels
    m->AddChannel("channel1");
    m->AddChannel("channel2");

    // add processes
    m->AddProcess("background_channel1", 700., 900.);
    m->AddProcess("background_channel2", 300., 700.);
    m->AddProcess("signal",                0., 400.);

    // set data
    m->SetData("channel1", *hist_data1);
    m->SetData("channel2", *hist_data2);

    // set template and histograms
    // note: the process "background_channel2" is ignored in channel 1
    m->SetTemplate("channel1", "signal", *hist_sgn1, 0.5);
    m->SetTemplate("channel1", "background_channel1", *hist_bkg1, 1.0);
    // m->SetTemplate("channel1", "background_channel2", *hist_bkg1, 0.0);

    // note: the process "background_channel1" is ignored in channel 2
    m->SetTemplate("channel2", "signal", *hist_sgn2, 1.0);
    m->SetTemplate("channel2", "background_channel2", *hist_bkg2, 1.0);
    // m->SetTemplate("channel2", "background_channel1", *hist_bkg2, 0.0);

    // set priors
    m->GetParameter("background_channel1").SetPrior(new BCGaussianPrior(800, 10));
    m->GetParameter("background_channel2").SetPrior(new BCGaussianPrior(500, 50));
    m->GetParameter("signal").SetPriorConstant();

    // run MCMC
    m->MarginalizeAll();

    // find global mode
    m->FindMode( m->GetBestFitParameters() );

    // print all marginalized distributions
    m->PrintAllMarginalized("marginalized.pdf");

    // print results of the analysis into a text file
    m->PrintSummary();

    // print templates and stacks
    for (int i = 0; i < m->GetNChannels(); ++i) {
        BCMTFChannel* channel = m->GetChannel(i);
        channel->PrintTemplates(channel->GetName() + "_templates.pdf");
        m->PrintStack(i, m->GetBestFitParameters(), channel->GetName() + "_stack.pdf");
    }

    // ---- perform ensemble tests ---- //

    // create new analysis facility
    BCMTFAnalysisFacility* facility = new BCMTFAnalysisFacility(m);

    // settings
    facility->SetFlagMarginalize(false);

    // open new file
    file = TFile::Open("ensembles.root", "RECREATE");
    file->cd();

    // create ensembles
    TTree* tree = facility->BuildEnsembles( m->GetBestFitParameters(), 2000 );

    // run ensemble test
    TTree* tree_out = facility->PerformEnsembleTest(tree, 2000);

    // write trees into file
    tree->Write();
    tree_out->Write();

    // close file
    file->Close();

    // free memory
    delete file;

    // ---- clean up ---- //

    // free memory
    delete m;
}
Exemple #2
0
void twoChannels()
{
    // open log file
    BCLog::OpenLog("log.txt", BCLog::detail, BCLog::detail);

    // ---- read histograms from a file ---- //

    // open file
    std::string fname = "templates.root";
    TFile* file = TFile::Open(fname.c_str(), "READ");

    // check if file is open
    if (!file || !file->IsOpen()) {
        BCLog::OutError(Form("Could not open file %s.", fname.c_str()));
        BCLog::OutError("Run macro CreateHistograms.C in Root to create the file.");
        return;
    }

    // read histograms
    TH1D* hist_bkg1  = (TH1D*)file->Get("hist_bkg1");    // background template for channel 1
    TH1D* hist_bkg2  = (TH1D*)file->Get("hist_bkg2");    // background template for channel 2
    TH1D* hist_sgn1  = (TH1D*)file->Get("hist_sgn1");    // signal template for channel 1
    TH1D* hist_sgn2  = (TH1D*)file->Get("hist_sgn2");    // signal template for channel 2
    TH1D* hist_data1 = (TH1D*)file->Get("hist_data1");  // data for channel 1
    TH1D* hist_data2 = (TH1D*)file->Get("hist_data2");  // data for channel 2

    if (!hist_bkg1 || !hist_bkg2 || !hist_sgn1 || !hist_sgn2 || !hist_data1 || !hist_data2) {
        BCLog::OutError("Could not find data histograms.");
        return;
    }

    // ---- perform fitting ---- //

    // create new fitter object
    BCMTF* m = new BCMTF();

    // set precision
    m->SetPrecision(BCEngineMCMC::kQuick);

    // add channels
    m->AddChannel("channel1");
    m->AddChannel("channel2");

    // add processes
    m->AddProcess("background_channel1", 700., 900.);
    m->AddProcess("background_channel2", 300., 700.);
    m->AddProcess("signal",                0., 400.);

    // set data
    m->SetData("channel1", *hist_data1);
    m->SetData("channel2", *hist_data2);

    // set template and histograms
    // note: the process "background_channel2" is ignored in channel 1
    m->SetTemplate("channel1", "signal", *hist_sgn1, 0.5);
    m->SetTemplate("channel1", "background_channel1", *hist_bkg1, 1.0);

    // note: the process "background_channel1" is ignored in channel 2
    m->SetTemplate("channel2", "signal", *hist_sgn2, 1.0);
    m->SetTemplate("channel2", "background_channel2", *hist_bkg2, 1.0);

    // set priors
    m->GetParameter("background_channel1").SetPrior(new BCGaussianPrior(800., 10.));
    m->GetParameter("background_channel2").SetPrior(new BCGaussianPrior(500., 50.));
    m->GetParameter("signal").SetPriorConstant();

    // marginalize
    m->MarginalizeAll();

    // find global mode
    m->FindMode( m->GetBestFitParameters() );

    // print all marginalized distributions
    m->PrintAllMarginalized("marginalized.pdf");

    // print results of the analysis into a text file
    m->PrintSummary();

    // print templates and stacks
    for (int i = 0; i < m->GetNChannels(); ++i) {
        BCMTFChannel* channel = m->GetChannel(i);
        channel->PrintTemplates(channel->GetSafeName() + "_templates.pdf");
        m->PrintStack(i, m->GetBestFitParameters(), channel->GetSafeName() + "_stack.pdf");
    }

    // ---- clean up ---- //

    // free memory
    delete m;

}
Exemple #3
0
void mcstat()
{
    // ---- set style and open log files ---- //

    // open log file
    BCLog::OpenLog("log.txt");
    BCLog::SetLogLevel(BCLog::detail);

    // set nicer style for drawing than the ROOT default
    BCAux::SetStyle();

    // ---- read histograms from a file ---- //

    // open file
    std::string fname = "templates.root";
    TFile* file = TFile::Open(fname.data(), "READ");

    // check if file is open
    if (!file || !file->IsOpen()) {
        BCLog::OutError(Form("Could not open file %s.", fname.c_str()));
        BCLog::OutError("Run macro CreateHistograms.C in Root to create the file.");
        return;
    }

    // read histograms
    TH1D* hist_signal     = (TH1D*)file->Get("hist_sgn");    // signal template
    TH1D* hist_background = (TH1D*)file->Get("hist_bkg");    // background template
    TH1D* hist_data       = (TH1D*)file->Get("hist_data");   // data

    if (!hist_signal || !hist_background || !hist_data) {
        BCLog::OutError("Could not open data histograms");
        return;
    }

    // ---- perform fitting ---- //

    // create new fitter object
    BCMTF* m = new BCMTF("SingleChannelMTF");

    // add channels
    m->AddChannel("channel1");

    // add processes
    m->AddProcess("background", 200., 400.);
    m->AddProcess("signal",       0., 200.);

    // set data
    m->SetData("channel1", *hist_data);

    // set template and histograms
    m->SetTemplate("channel1", "signal",     *hist_signal,     1.0);
    m->SetTemplate("channel1", "background", *hist_background, 1.0);

    // set priors
    m->GetParameter("background").SetPrior(new BCGaussianPrior(300., 10.));
    m->GetParameter("signal").SetPriorConstant();

    // print templates
    m->GetChannel(0)->PrintTemplates(m->GetChannel(0)->GetSafeName() + "_templates.pdf");

    // ---- perform ensemble tests ---- //

    m->SetPrecision(BCEngineMCMC::kQuick);

    // create new analysis facility
    BCMTFAnalysisFacility* facility = new BCMTFAnalysisFacility(m);

    // settings
    facility->SetFlagMarginalize(true);

    // open new file
    file = TFile::Open("ensembles.root", "RECREATE");
    file->cd();

    // create ensembles; option "data" means that all ensembles equal the data set
    TTree* tree = facility->BuildEnsembles( std::vector<double>(0), 1000, "data");

    // run ensemble test; option "MCP" means that the templates are flucutated via a Poisson model
    TTree* tree_out = facility->PerformEnsembleTest(tree, 1000, 0, "MCP");

    // write trees into file
    tree->Write();
    tree_out->Write();

    // close file
    file->Close();

    // ---- clean up ---- //

    // free memory
    delete file;

    // free memory
    delete m;

}