Can't Use Histograms Produced in Macro


I know there are similar threads to this but I can’t find any that actually address my problem and so far none of the fixes have worked. I might have missed one that does apply so please feel free to redirect me if that is the case.

I am writing a macro to pull in two files that share the same Tree name and structure but have different data parameters. I want to plot a histogram from each file (EDecayG1) and save the histograms to a separate file (cf.root) to access later. When I execute the macro it works fine and the histogram of sim1240.EDecayG1 appears. However, if I try “simpeak630->Draw()” I get

Error: Symbol simpeak630 is not defined in current scope (tmpfile):1: Error: Failed to evaluate simpeak630->Draw() *** Interpreter error recovered ***

“simpeak1240->Draw()” doesn’t complain until I close the canvas that was automatically produced from executing the macro. Then I get the same error.

This is the code for my macro:


//Open initial simulation file for the first peak
TFile * sim630 = TFile::Open(“26F-1p1n-geant-anal_E630_W50_100k.root”);
//Add the second simulation file to the tree
at->AddFriend(“sim1240 = at”,“26F-1p1n-geant-anal_E1240_W30_100k.root”);
//Draw EDecayG1 histogram for simulations

//Add histograms to the list
TList * list = new TList();

//Change the ownership of the histograms so they aren’t deleted when the file is closed at the end of this macro

//Save the list of histograms to a file
TFile * cf = new TFile(“cf.root”,“recreate”);


Before I simplified down to this I had a section of code at the beginning that pulled in a data file, created a histogram from that file and added it to the list of histograms to be written to cf.root. That section worked perfectly fine and I could reproduce the “datahst” histogram without any problems, but I had the same issues with the simulation files as described above. I took out that portion of the code to try to isolate the problem. If you would like to see the code including the data portion please let me know and I can attach it.


As I understand in your macro “at” is the pointer on the Tree, right? if so, you can try so:

. . . . . at->Draw("EDecayG1>>simpeak630(25,0,5)"); TH1F *simpeak630=(TH1F*)gROOT->FindObject("simpeak630"); . . . . . at->Draw("sim1240.EDecayG1>>simpeak1240(25,0,5)"); TH1F *simpeak1240=(TH1F*)gROOT->FindObject("simpeak1240"); . . . . .


Hi Archil,

Thanks for your suggestion! Unfortunately it didn’t work :frowning: I ended up putting a line in at the end after I close the file to reopen it and it seems to work fine now. I don’t think I’m having any memory leaking issues either. Hopefully it will continue to work and not cause any problems later on [-o<