Overlap hundreds of histograms macro question

社会主义新天地 提交于 2019-12-11 16:38:27

问题


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*****.

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

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();
        }
}

回答1:


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();
    }
}



回答2:


Since it is expected that some files are "missing", I suggest not to try to guess the names of the files that actually exist. Instead, use a function that lists all files in a given directory and from that list filter out those files that match the pattern of files you want to read. See for example these links for how to read the content of a directory in C++:

  • How can I get the list of files in a directory using C or C++?
  • http://www.martinbroadhurst.com/list-the-files-in-a-directory-in-c.html


来源:https://stackoverflow.com/questions/56946349/overlap-hundreds-of-histograms-macro-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!