[cernRoot] how to get contours from a TH2D

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kakakaco/article/details/53433675

here, i use a TMultiGraph to save the gotten contours, because the contours may be not closed in a TH2D, with the function TMultiGraph * GetContours(const char * fn, const char * th2dname, int nlevel). And in the Test() function, the TGraph objects of the TMultiGraph are extracted.

void Test()
{
    TMultiGraph * mg = GetContours("res_b64_fit_3id_ddmts_mc.root", "0", 1);
    mg->DrawClone("ACP");
//    TList * list = mg->GetListOfGraphs();
//    TGraph * gr = (TGraph *)list->First();
//    gr->DrawClone("ACP");
//    cout << list->GetSize() << endl;
delete mg;
}
TMultiGraph * GetContours(const char * fn, const char * th2dname, int nlevel)
{
    TGraph ** gr;
    TFile * f = new TFile(fn, "read");
    TH2D * h = (TH2D *) f->Get(th2dname);
    double chisq = h->GetMinimum();
    double contours[2] = {chisq + 2.3, chisq + 6.14};
    h->SetContour(2, contours);
    TCanvas * cc = new TCanvas(th2dname, th2dname);
    cc->Divide(2,2);
    cc->cd(1);
    h->Draw("CONT1");
    cc->cd(2);
    h->Draw("CONT Z LIST");
    cc->Update();/////////////////////////////
    TObjArray * cont = (TObjArray *)(gROOT->GetListOfSpecials()->FindObject("contours"));
    TList * list = (TList *)cont->At(nlevel);
    int ngr = list->GetSize();
    TGraph * grtemp;
    if(ngr > 0)
    {
        gr = new TGraph * [ngr];
        grtemp = (TGraph *)list->First();
        gr[0] = grtemp->Clone();
        for(int igr = 1; igr < ngr; ++ igr)
        {
            grtemp = (TGraph *)list->After(grtemp);
            gr[igr] = grtemp->Clone();
        }
    }
    cc->Close();
    delete cc;
    f->Close();
    delete f;
    delete grtemp;
    TMultiGraph * mg = new TMultiGraph();
    for(int i = 0; i < ngr; ++ i)
        mg->Add(gr[i]);
    delete [] gr;
    return mg;
}
阅读更多
换一批

没有更多推荐了,返回首页