重叠数百个直方图宏问题 [英] Overlap hundreds of histograms macro question

查看:207
本文介绍了重叠数百个直方图宏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录trial,其中包含数百个直方图和一个宏.每个都以hists09876_blinded.roothists12365_blinded.root的方式调用.但是,顺序并非如此.有一些Missig直方图,例如hists10467_blinded.root hists10468_blinded.root hists10470_blinded.root.最终目标是在画布上获得一个直方图,该直方图代表所有组合在一起的直方图.棘手的事情是,每个hists*****_blinded.root中都包含大约15个1D历史数据,我只需要从每个被称为sc*****的图像中拉出一个.

I have a directory trial which contains hundreds of histograms in it and a macro. Each is called in a way hists09876_blinded.root or hists12365_blinded.root. The order, however, is not like that. There are some missig histograms like hists10467_blinded.root hists10468_blinded.root hists10470_blinded.root. The ultimate goal is to get one histogram on a canvas which represents all of those combined together. The tricky thing is that each hists*****_blinded.root has around 15 1D histos in it, I need to pull out just one from each called sc*****.

我有2个想法,但我想我应该将它们结合在一起以获得最终结果.

I have 2 ideas, but I guess I should combine them together to get the final result.

第一个想法是先按histo打开histo,但是由于顺序中遗漏了一些histo,所以效果不佳.

First idea was to open histo by histo, but since there are some missed histos in the order, that does not work well.

void overlap()
{
        TCanvas *time = new TCanvas("c1", "overlap", 0, 0, 800, 600);

        const char* histoname = "sc";

        const int NFiles = 256;
        for (int fileNumber = 09675; fileNumber < NFiles; fileNumber++)
        {
                TFile* myFile = TFile::Open(Form("hists%i_blinded.root", fileNumber));
                if (!myFile)
                {
                        printf("Nope, no such file!\n");
                        return;
                }
                TH1* h1 = (TH1*)myFile->Get(histoname);
                if (!h1)
                {
                        printf("Nope, no such histogram!\n");
                        return;
                }
                h1->SetDirectory(gROOT);
                h1->Draw("same");
                myFile->Close();
        }
}

推荐答案

在阅读了几乎同一问题的多个帖子后(,而这个)我已经弄清楚了

After having read multiple posts on the pretty much the same question (1, 2, and this one) I have figured out what was wrong with my answer here: I did not know the file name may contain a zero if the number in its name is < 10000. Also, I failed to understand that the asterisks in the histogram name, which you refer to as sc*****, actually hide the same number as in the file name! I thought this was something completely different. So in that case I suggest you construct the file name and the histogram name you should be after in the same loop:

void overlap_v2()
{
    TCanvas *time = new TCanvas("c1", "overlap", 0, 0, 800, 600);

    const int firstNumber = 9675;
    const int NFiles = 100000;
    for (int fileNumber = firstNumber; fileNumber < firstNumber+NFiles; fileNumber++)
    {
        const char* filename = Form("trial/hists%05i_blinded.root", fileNumber);

        TFile* myFile = TFile::Open(filename);
        if (!myFile)
        {
            printf("Can not find a file named \"%s\"!\n", filename);
            continue;
        }

        const char* histoname = Form("sc%05i", fileNumber);
        TH1* h1 = (TH1*)myFile->Get(histoname);
        if (!h1)
        {
            printf("Can not find a histogram named \"%s\" in the file named \"%s\"!\n", histoname, filename);
            continue;
        }
        h1->SetDirectory(gROOT);
        h1->Draw("same");
        myFile->Close();
    }
}

这篇关于重叠数百个直方图宏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆