Saving plot as root file

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


#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

Try

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

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:

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 instead.

1 Like