Draw a Histogram from within a root Macro

Okay, so I would like to draw a histogram that I can normally draw by using

After opening the root file that I want to draw.

Now, I would like to draw the same histogram but instead of typing that in on the root command line, I would like to open it with a .C Macro.

I currently have this:

	char newrootpath[];
	*newrootpath = rootpath;

	TFile *f = new TFile(newrootpath);

where rootpath is type string, the basic C standard string.

Everytime I run this root crashes. I do not know why. Please help me by god.

Thank you.


Hi Chris,

you cannot access directly the name of the tree variable without having read it before. Try:


Danilo, you are the kitten’s mittens. Thank you.

I did have to change nullptr to NULL, but that may be because I’m using windows like 1% of the root community.

Thanks Again.


Hi Chris,

the reason for that is that the nullptr literal is available starting from c++11.
This standard is supported only starting from ROOT6: we are working hard to provide this ROOT version natively on windows too.


Additionally, in this case you do not need to initialize the “T” pointer in advance, as the “GetObject” call will always set a new value (regardless whether the “object” was found or not).

Okay so on a similar note, I would like to have my macro display a pedestal histogram that has been created in a root tuple already. I normally do this by just double clicking on the pedestal histogram in the root object browser

Similarly to c1:t1 histogram, I’d like to open this histogram as well.

Here’s my function so far. I’ve verified that up to the cout statement everything works properly.

void MyMainFrame::ViewPedClicked()
	string rootpath = FilePath->GetText();
	rootpath = rootpath.substr(0, rootpath.length() - 4);
	rootpath += "_Analyzed.root";
	cout << rootpath << endl; // For Debugging

	TString newrootpath;
	newrootpath = rootpath;

		TFile f(newrootpath);
		TTree* T = NULL;
		f.GetObject("pedestal", T);


Thanks for the support.


Hi Chris,

you should specify the correct type of object you are reading from the file. In addition, you need to invoke the Draw method of TH1F/D: please refer to root.cern.ch/doc/master/classTH1F.html
Assuming that the histogram is a TH1F (it could be a TH1D, it depends on who wrote it):

      TFile f(newrootpath);
      TH1F* T = NULL;
      f.GetObject("pedestal", T);


Okay after changing T.Draw() to T->Draw(), This code runs, but it just produces a blank canvas, without the histogram drawn on it.





one thing which could be done is to go through the ROOT introductory material we suggest to get started with the Framework: root.cern.ch/getting-started
The histogram goes out of scope and its graphical representation too. Try DrawClone.