Saving Branches to PDF/PNG (Command Line)

Hey all,
Looking for some help with a root project!
Essentially, I have written a script that saves simulated data about the number of particles detected at certain detection point, modelled in a vector of integers, which is saved to a branch in the output file.

I run the sims on a high throughput compute cluster, and get the vector populated with around 10^9 entries, distributed from 1 to 30. Outputting the vector to a TBranch automatically puts it into a histogram that is acceptable to me. Next, I would like to save this branch (the histogram) as a pdf/png.

Currently, I’m doing this through a TBrowser, navigating to the branch and clicking Save As in the tool bar. However, since the compute cluster does not have graphics, I have to download the file onto my laptop and do this locally. With heavier simulations, this has become a problem as the output file is often more than 2-4GB and saturates the RAM on my laptop.

Is there anyway I can navigate to a vector branch and save the histogram that it automatically plots as a pdf/png through the command line, instead of graphically with TBrowser? This would allow me to just download the histograms as images from the cluster.

For reference, here are the relevant snippets of code:

TTree *tOut = new TTree("OutputTree", "OutputTree"); //output tree
vector<int> *data = 0; // initialises the vector

// some loop that populates the vector with around 10^9 entries

tOut->Branch("data", &data);

ROOT Version: 6.22/08
Platform: macOS
Compiler: Not Provided


There are different ways to do it. One way is to call your macro at the time of running ROOT on the OS command line, running ROOT in batch mode without graphics:

root -l -b -q "mymacro.C"

(root -h will show you the options)
That way you can easily call it from a script, if you need to run it many times, or automatically, for example.
Inside the macro, once you have the tree you can just create a canvas, then draw the branch and save the canvas:

TFile *f = new TFile("myfile.root"); // supposing this macro doesn't already have the tree
TTree *t = (TTree*)f->Get("OutputTree"); //  supposing this macro doesn't already have the tree
TCanvas *c = new TCanvas("c","c",1);
t->Draw("mydata");
c->SaveAs("c.png");  // or pdf

Of course you can make selections, format the histogram, change the output file name using variables, etc. Search the forum for examples or go to the documentation (top of this page) and check out the manual, guides, tutorials…

Works perfectly, thank you very much!