Can't get root to plot from multiple Tfiles in single Canvas

If anyone could help me with this that would be great!

Basically I have two root files saved each containing a ttree (BData) of the same variables but run from different generator settings. I want to be able to plot them both on the same Canvas and have written the macro below.

If I just use BData -> Draw(B0e) and never declare or use a TH1F file then it plots them both fine, but I need to create a TH1F to be able to change the axis titles etc. As soon as I change to the second Tfile the first graph disappears. Does anyone have any ideas where ive gone wrong?

Many Thanks

Charlie

[code]#include
#include
#include
#include <stdlib.h>

using namespace std;

void macroenergy()
{

gROOT -> Reset();

TCanvas *multihist = new TCanvas(“multihist”,“Histogram test”);

multihist -> Divide(1,2);
multihist -> SetTitle (“My Histograms”);

multihist -> cd(1);
TFile f1(“firstfile.root”);
TH1F *test1 = new TH1F(“test1”,“Energy with m=10”,100,0,1000);
BData -> Draw(“b0e>>test1”);

//TAxis *graxx = test1->GetXaxis();
// TAxis *graxy = test1->GetYaxis();
//graxx -> SetTitle("My x axis");
//graxy -> SetTitle("My y axis");

multihist -> cd(2);
TFile f2(“secondfile.root”);
TH1F *test2 = new TH1F(“test2”,“Energy with m=5”,100,0,1000);
BData -> Draw(“b0e>>test2”);

//TAxis *graxx = test2->GetXaxis();
// TAxis *graxy = test2->GetYaxis();
//graxx -> SetTitle("My x axis");
//graxy -> SetTitle("My y axis");

multihist -> Print("./rootfiles/multihist.eps"); //save into rootfiles directory

}

[/code]

// ... TFile f1("firstfile.root"); TH1F *test1 = new TH1F("test1","Energy with m=10",100,0,1000); TTree *tree1; f1.GetObject("BData", tree1); if (tree1) tree1->Draw("b0e>>test1"); // test1->SetDirectory(0); // (0) or (gROOT) // ... TFile f2("secondfile.root"); TH1F *test2 = new TH1F("test2","Energy with m=5",100,0,1000); TTree *tree2; f2.GetObject("BData", tree2); if (tree2) tree2->Draw("b0e>>test2"); // test2->SetDirectory(0); // (0) or (gROOT) // ...

Thanks very much for the help! But still having problems:

The graphs appears momentarily on my canvas when I run the macro but then the data on the graph disappears leaving just the outline and the title (the title being b0e{boe>0. && mjets>0} )
My canvas does get stored in my chosen directory with the graphs, but neither graph has any axis labels and the titles are still as above even though I tried two methods to relabel)

Any ideas?

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

void macroetest()
{

  gROOT -> Reset();

  TCanvas *multihist = new TCanvas("multihist","Histogram test");

  multihist -> Divide(1,2);
  multihist -> SetTitle ("My Histograms");

  multihist -> cd(1);
  TFile f1("mz2_R4.root");
  TH1F *test1 = new TH1F("test1","Energy  with  m=10",100,0,8000);
  test1 -> SetDirectory(0);
  TTree *tree1; f1.GetObject("BData", tree1);
  if (tree1) tree1->Draw("b0e>>test1","b0e>0. && mjets>0");

  TAxis *graxx = test1->GetXaxis();
  TAxis *graxy = test1->GetYaxis();
  graxx -> SetTitle("My x axis");
  graxy -> SetTitle("My y axis");
      
  multihist -> cd(2);
  TFile f2("mz5_R4.root");
  TH1F *test2 = new TH1F("test2","Energy with  m=5",100,0,8000);
  test2 -> SetDirectory(0);
  TTree *tree2; f2.GetObject("BData", tree2);
  if(tree2) tree2->Draw("b0e>>test2","b0e>0. && mjets>0");
 
  test2-> SetTitle("My title");
  test2 -> SetXTitle("my x axis");
  test2 -> SetYTitle("my y axis");

   multihist->Update();
   multihist -> Print("./rootfiles/multihist.eps"); //save into rootfiles directory
   multihist -> Draw();

}
 
  

TFile f1("firstfile.root"); TH1F *test1 = new TH1F("test1","Energy with m=10",100,0,1000); TTree *tree1; f1.GetObject("BData", tree1); if (tree1) tree1->Project("test1", "b0e", "b0e>0. && mjets>0"); test1->SetDirectory(0); // (0) or (gROOT) test1->Draw(); // ... TFile f2("secondfile.root"); TH1F *test2 = new TH1F("test2","Energy with m=5",100,0,1000); TTree *tree2; f2.GetObject("BData", tree2); if (tree2) tree2->Project("test2", "b0e", "b0e>0. && mjets>0"); test2->SetDirectory(0); // (0) or (gROOT) test2->Draw();

My code seems to be working now. Thank you very very much!