Opening a histogram from a file

…a cryptically difficult task, apparently.

So I’ve managed to find quite a few threads that more or less cover the issue I’m having… but nothing I have tried seems to be working at all.

The short of it is, I have two files, each with one histogram inside. Not a tree, just a histogram. I’d like to open them one at a time, and make a copy of each of these histograms in a way that I can perform simple functions such as GetXmin() and GetBinContent(bin). Later of course, there are more complicated things to be done with the content, but it’s the first part that doesn’t seem to be working for me.

Let’s start with something a bit more simple, and I’ll work my way towards something I actually want to do, though:

TFile *file1 = new TFile("first_file.root"); // open the file, easy.
c1->Draw(); // the histogram I want is definitely there.
TH1D *hist1 = (TH1D*)file1->Get("c1"); // get the histogram, no complaints here.
hist1->Draw(); // I can draw the new histogram, great! Let's start doing useful things with it.
hist1->GetXaxis()->GetXmax(); // ...Oh. It doesn't work. I get the message: Error: Symbol hist1 is not defined in current scope. Productivity grinds to a screeching halt.
// in fact, at this point I can't even draw the histogram again. I can draw it once, and then it's gone; no longer defined in the current scope.

I’m lost. I can draw it once, but I can’t do anything else with it. Every thread I can find points towards this approach. If I were drawing data from a tree I’d be golden, but the syntax here seems to have taken a leap into the unknown: This is baffling.

Execute the following and post the output here:
TFile *f = TFile::Open(“first_file.root”);

See also, for example:

This seems to work just fine:

root [0] TFile *f = TFile::Open("first_file.root"); root [1] f->ls(); TFile** first_file.root TFile* first_file.root KEY: TCanvas c1;1 c1 root [2] c1->Draw(); root [3] c1->GetListOfPrimitives()->Print(); Collection name='TList', class='TList', size=3 TFrame X1=100.000000 Y1=0.000000 X2=2000.000000 Y2=3102.750000 FillStyle=1001 TH1.Print Name = hMGTDC, Entries= 1089924, Total sum= 1.05843e+06 TPaveText X1=725.962639 Y1=3234.551182 X2=1374.037361 Y2=3471.201610 Collection name='TList', class='TList', size=1 Text X=0.000000 Y=0.000000 Text=MGTDC Energy Font=0 Size=0.000000 Color=0 Align=0

I will take a look at the linked threads for solutions.

after “c1->Draw()” type “c1->cd()” and then “hMGTDC” in the ROOT prompt (it should return something like “(TH[123][DFISC]*)0x12345678”) … one needs to learn what kind of histogram it is.

Quoth the terminal: Error: Symbol hMGTDC is not defined in current scope (tmpfile):1:

Try it with a mouse … after “c1->Draw()” move the mouse cursor to one of the drawn points and click it with the right mouse button -> a new menu should appear and it’s title will tell you what kind of a histogram it is (should be something like “TH[123][DFISC]::hMGTDC”).

It’s TH1I. Ammending previous commands doesn’t seem to change anything.

So now you know everything you need to know about it.
Just follow examples in the links from my first post here.

[quote=“Wile E. Coyote”]So now you know everything you need to know about it.
Just follow examples in the links from my first post here.[/quote]

…this doesn’t help me though. I still cannot create a copy of the histogram in that file, in such a way that allows me to perform tasks with it. As I said before: Ammending previous commands doesn’t seem to change anything.

To be more concise: Knowing that it’s a TH1I still leaves me without the correct syntax to make a copy of some sort, as what I’m doing is clearly wrong.

If I type:

TFile *file1 = new TFile("first_file.root") TH1I *hist1 = (TH1I*)file1->Get("c1")

I have exactly the same issue has I started with. I also cannot use c1 in the way I’d like to be able to use hist1.

Did I miss some explanation here?

TFile *file1 = TFile::Open("first_file.root"); TCanvas *c1; file1->GetObject("c1", c1); if (!c1) {std::cout << "Error: c1 NOT found!" << std::endl;} TH1I *hMGTDC = 0; if (c1) hMGTDC = ((TH1I *)(c1->GetPrimitive("hMGTDC")); if (!hMGTDC) {std::cout << "Error: hMGTDC NOT found!" << std::endl;}
BTW. I’ve got another idea how one can quickly get the kind of the histogram. After “c1->Draw()”, in the ROOT prompt, type one of: