"smart" histogram plotting program

I currently have a root program (it runs as a c++ program, not through ROOT) that generates a .root file with a number of histograms. I use a separate root macro (run in root, the file is attached) that reads in the .root output file and saves each histogram as a .png file. Essentially what it does is (for each plot):

TFile *file = new TFile(DataFileName+".root"); //load the input file
TCanvas *c = new TCanvas; // make a canvas

//get the histograms, plot to canvas and save to file
TH1F *h = (TH1F*)file->Get("dRjj");  //read in the specific histogram
h->Draw();  //draw the histogram to canvas
c->Print("dRjj.png"); // save the histogram to file
c->Clear();  //clear the canvas for a new histogram

What I’d like to be able to do is run this outside of root (as a c++ program). I have tried this but it fails when it tries to make the canvas and save the plots. If there is a way to do this without making the canvas or just a way that it will work as a C++ program I’d be happy.

Also, it would be great if I could make a “smart” histogram plotter that would read in all of the histograms without having to know their names in advance (I currently have to make a separate piece of code to plot each histogram name that I know is in the .root file).

Thanks for your help!
plotter.c (2.85 KB)

In your standalone program you should create a TApplication object.
and you may have to explictly call c->Update(); after drawing.

I currently have a root program (it runs as a c++ program, not through ROOT) that generates a .root file with a number of histograms. I use a separate root macro (run in root, the file is attached) that reads in the .root output file and saves each histogram as a .png file. Essentially what it does is (for each plot):

Use your TDirectory (or TFile) object list of TKey (GetListOfKeys) to get the list of available object in the file.

Cheers,
Philippe