0

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();
        }
}
Brick
  • 3,998
  • 8
  • 27
  • 47
sonic
  • 27
  • 4

2 Answers2

0

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++:

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
0

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();
    }
}
Yury
  • 423
  • 2
  • 7
  • 1
    I am trying to figure out now why it doesn't work for hists which exist in the directory. So, for example, the `hists09015_blinded.root` is in the directory, but `hists09016_blinded.root` is not in there. When I do `.x overlap_v2.C`, the terminal counts hist++ and says `...._blinded.root does not exist` for all of them with no exception. I am working with a smaller sample now and I have decreased the `NFiles` number as well. – sonic Jul 09 '19 at 13:46
  • So I basically end up with a blank canvas and hundreds of lines in the terminal saying `_blinded.root does not exist` – sonic Jul 09 '19 at 13:47
  • Ok, so if you have files named `hists09015_blinded.root`, you should change the `firstNumber` to 9015 (or to a smaller number if you have such files, too) – Yury Jul 09 '19 at 13:51
  • I know, let's say now my sample directory contains 10 histograms starting from `hists09013_blinded.root` and ends with `hists09022_blinded.root`. In this case, I have `const int firstNumber = 9013; const int NFiles = 10`. Right? – sonic Jul 09 '19 at 13:55
  • do you have any ideas why I get `Error in : file trial/hists09013_blinded.root does not exist Can not find a file named "trial/hists09013_blinded.root"! ` 10 times for each histogram? I have both `overlap_v2.C` and 10 hists in the same `trial` directory – sonic Jul 09 '19 at 14:10
  • Can you do `tree` in the directory where the `overlap_v2.c` sits and show me the output? – Yury Jul 09 '19 at 14:13
  • The `filename` and `histoname` change automatically in the loop, I don't see why you would need to change them manually. Concerning the `tree`, this is a unix command: just quit `root`, type `tree` in your shell, hit `Enter`, and post here its output. – Yury Jul 09 '19 at 14:32
  • Ah, that explains it. The `overlap_v2()` implies your `*.root` files should be in a directory called `trial`. Instead, you have them just sitting in the same directory with your `overlap_v2.c`. Either move them into `trial` or change the `filename` initialization line to `const char* filename = Form("hists%05i_blinded.root", fileNumber);`. – Yury Jul 09 '19 at 15:04
  • First, you need to do it in a loop over all your `sc*****` histograms. Second, you currently overwrite the content of the `sum` histogram - instead, you should **add** content to it. Something like this: `for (int bin=1; bin <= sc9675->GetNbinsX(); bin++) sum->SetBinContent(bin, sum->GetXaxis()->GetBinContent(bin) + sc9675->GetXaxis()->GetBinContent(bin));` – Yury Jul 11 '19 at 12:22
  • I think there is a mistake with `GetXaxis()` – sonic Jul 12 '19 at 17:09
  • What mistake? Where? – Yury Jul 12 '19 at 17:43