Making a histogram from two root files

Howdy all,

I have two ROOT files which I want to use to make histograms. Currently I can do:

TChain chain("treename") chain.Add("file1.root") chain.Add("file2.root") chain.Draw("variablename")

To effectively make histograms of a sum of the two files. What I want, however, is to make plots where all of the data in file1 are one color (let’s say black) while the data in file2 are a different color (red). Unfortunately I’m not sure how to proceed, and any help would be most appreciated.

Thanks!

I don’t think you can do that with a single chain and histogram, but you can use two trees and histograms. You’ll have to make two histograms, fill them, set the colors, and then draw them on the same canvas.

TFile file0("file0.root"); TFile file1("file1.root"); TTree *tree0=(TTree*)file0.Get("treename"); TTree *tree1=(TTree*)file1.Get("treename"); TH1F *hist0=new TH1F("hist0","title",nbins,min,max); TH1F *hist1=new TH1F("hist1","title",nbins,min,max); tree0->Draw("variablename>>hist0"); tree1->Draw("variablename>>hist1"); tree0->SetLineColor(kBlack); tree1->SetLineColor(kRed); tree0->Draw(); tree1->Draw("same");
Using stream extraction operator “>>” in the draw command tells root to output to the named histogram.

Thanks for your response.

Everything seems to work fine until I get to the end:

[code]root [10] tree0->Draw();

*** Break *** segmentation violation
Attaching to program: /proc/10401/exe, process 10401
[Thread debugging using libthread_db enabled]
0x0094a422 in __kernel_vsyscall ()
error detected on stdin
A debugging session is active.

Inferior 1 [process 10401] will be detached.

Quit anyway? (y or n) [answered Y; input not from terminal]
Detaching from program: /proc/10401/exe, process 10401[/code]

After which point I have to close ROOT to return to normal functionality. Any other ideas?

I think that the last four lines of the bwoneill’s example code should be: hist0->SetLineColor(kBlack); hist1->SetLineColor(kRed); hist0->Draw(); hist1->Draw("same"); Also, instead of: tree0->Draw("variablename>>hist0"); tree1->Draw("variablename>>hist1"); you could use: tree0->Project("hist0", "variablename"); tree1->Project("hist1", "variablename");

Thanks, your correction works well.