# Saving plot as root file

**URL:** <https://root-forum.cern.ch/t/saving-plot-as-root-file/51384>\
**Category:** Newbie\
**Created:** [August 28, 2022, 3:50pm UTC](https://root-forum.cern.ch/t/saving-plot-as-root-file/51384 "2022-08-28T15:50:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 28, 2022, 3:50pm UTC](https://root-forum.cern.ch/t/saving-plot-as-root-file/51384/1 "2022-08-28T15:50:39Z")

</div>

Hi there  
Assume I have a root file that contains Tree-\> Iris and under this tree, I have branch -\>det and then I have many leaves, like TSd1rEnergy, TSd1rTheta, and so on. Now I want to save the plot generated using Iris-\> Draw(“TSd1rEnergy:TSd1Theta”) as a root file. How can I do that?  
Another Question is that if I have another root file having the same tree, branch, and leaves and I want to superimpose Iris-\> Draw(TSd1rEnergy:TSd1Theta) from both root files and save this plot as the root file, how to do that

\_ROOT Version: 6.24/06  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 29, 2022, 12:48am UTC](https://root-forum.cern.ch/t/saving-plot-as-root-file/51384/2 "2022-08-29T00:48:33Z")

</div>

#include \<stdio.h\>

#include

#include

#include “TCanvas.h”

#include “TF1.h”

#include “TFile.h”

#include “TGraphErrors.h”

#include “TH1.h”

#include “TH2.h”

#include “TSpectrum.h”

#include “TChain.h”

using namespace std;

int histocounter=0;

void script1()

{

TFile \*f= new TFile(“tree06855u.root”);

TTree\* Iris = (TTree\*)f-\>Get(“Iris”);

//gROOT-\>cd();

//delete gROOT-\>FindObject(“h”);

//TH2F \*h = new TH2F(“h”,“plot;TSd1rEnergy;TSd1Theta”,180,2000,4000,180,100,1500);

//TH2F \*h = new TH2F(“h”,“plot;TSd1rEnergy;TSd1Theta”,100,2000.,4000.,100,100.,1500.);

Iris-\>Draw(“TSd1rEnergy:TSd1Theta\>\>h”);

//Iris-\>Draw(“det.TSd1rEnergy\>\>h2”);

//Iris-\>Draw(“TSd1rEnergy:TSd1Theta”);

//Iris-\>DrawClone(“col”);

//Iris-\>Draw(“z same”);

TFile f2(“output.root”,“recreate”);

// h-\>Write();

f2.Close();

return 0;

}  
**This is what I am trying to do, please give me suggestions to make some changes inside script for desired results that I want in main forum**

---

<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:** [August 29, 2022, 4:51am UTC](https://root-forum.cern.ch/t/saving-plot-as-root-file/51384/3 "2022-08-29T04:51:52Z")

</div>

Try

```auto
TFile *f= new TFile("tree06855u.root");
TTree* Iris = (TTree*)f->Get("Iris");

TFile f2("output.root","recreate"); // create the output file before creating the histogram/s that will be saved in this file
TH2F *h = new TH2F("h","plot;TSd1rEnergy;TSd1Theta",180,2000,4000,180,100,1500);

Iris->Draw("det.TSd1rEnergy:det.TSd1Theta>>h");

f2.cd(); // a precaution to make sure we write to f2

h->Write("h", TObject::kOverwrite);

f2.Close();

return 0;

```

For 2 input files just open the second file and get the second tree and plot in the same way, using e.g. f2, tree2, h2 (again, create h2 _after_ the output file); then write h1 and h2 to the output file (they will be two separate histograms).  
To plot h1 and h2 together you can do

```auto
tree1->Draw("x:y>>h");
tree2->Draw("x2:y2>>h2","","same");

```

if this doesn’t work, try with “goff” (so it is not drawn) instead of “same” and then draw h2 on top of h1:

```auto
tree1->Draw("x:y>>h");
tree2->Draw("x2:y2>>h2","","goff");
h2->Draw("same");

```

but if the scales are different it will not look good, so you may want to add h1 and h2 to a [THStack](https://root.cern/doc/master/classTHStack.html) instead.
