# Saving Branches to PDF/PNG (Command Line)

**URL:** <https://root-forum.cern.ch/t/saving-branches-to-pdf-png-command-line/45199>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [June 1, 2021, 4:14pm UTC](https://root-forum.cern.ch/t/saving-branches-to-pdf-png-command-line/45199 "2021-06-01T16:14:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jain-pranay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jain-pranay/32/26344_2.png) [@jain-pranay](https://root-forum.cern.ch/u/jain-pranay)\
**Post date:** [June 1, 2021, 4:14pm UTC](https://root-forum.cern.ch/t/saving-branches-to-pdf-png-command-line/45199/1 "2021-06-01T16:14:27Z")

</div>

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:

```auto
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

* * *

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 1, 2021, 5:39pm UTC](https://root-forum.cern.ch/t/saving-branches-to-pdf-png-command-line/45199/2 "2021-06-01T17:39:38Z")

</div>

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:

```auto
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:

```auto
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…

---

<div class="post-metadata">

**Author:** ![jain-pranay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jain-pranay/32/26344_2.png) [@jain-pranay](https://root-forum.cern.ch/u/jain-pranay)\
**Post date:** [June 2, 2021, 6:52am UTC](https://root-forum.cern.ch/t/saving-branches-to-pdf-png-command-line/45199/3 "2021-06-02T06:52:20Z")

</div>

Works perfectly, thank you very much!
