Opening Two root files simultaneously

Hello All,

I want to plot two histograms (and compare numbers side by side) from data from two root files. How I I do that? At the moment, I am opening a root file, closing it and then opening an other like this:

[code]TFile *rootfile1 = TFile::Open(“X12ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy);


delete rootfile1;

TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy);


delete rootfile2;[/code]

Is there a way to get the corresponding data with both files opened simultaneously? Many thank

Karthik.

simply do:

[code]TFile *rootfile1 = TFile::Open(“X12ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy);

TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain2 = (TTree)gDirectory->Get(“Singles”);
chain2->SetBranchAddress(“energy”,&energy);


[/code]
Rene

Rene,

Thanks, but that I what I already had. Perhaps I did not explain well. So after you open chain1 and chain2, how do you get first energy from chain1 and the first energy from chain2?

Karthik.

[quote=“brun”]simply do:

[code]TFile *rootfile1 = TFile::Open(“X12ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy);

TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain2 = (TTree)gDirectory->Get(“Singles”);
chain2->SetBranchAddress(“energy”,&energy);


[/code]
Rene[/quote]

I am not sure to understand your question.
If you use chain->Draw, you can always direct the output to a histogram like

chain1->Draw("e>>hE1") chain2->Draw("e>>hE2")
Rene

Rene,

Let me see if I can make this clearer. Let’s imaging two root files with similar structure. Let’s also imaging there are 100 values of interest (say energy in a tree). What I do now is open one root file, read the 100 values into some array, close it and then open another and read the 100 values from the other root file. So imagine that all you want to do is print the values side by side like this:

Event No File1 File 2 1 1.2 3.4 2 2.4 0.06 ... ... 100 5 8.2

How do you do that with both files open simultaneously and not using temporary arrays? Thanks.

Karthik.

[quote=“brun”]I am not sure to understand your question.
If you use chain->Draw, you can always direct the output to a histogram like

chain1->Draw("e>>hE1") chain2->Draw("e>>hE2")
Rene[/quote]

OK this is simple. Create 2 variables energy1 and energy2 and do

[code]double energy1, energy2;
TFile *rootfile1 = TFile::Open(“X12ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy1);

TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain2 = (TTree)gDirectory->Get(“Singles”);
chain2->SetBranchAddress(“energy”,&energy2);
[/code]
Rene

Thanks Rene. My question is does chain2 automatically associate with rootfile2 in the following lines:

TFile *rootfile2 = TFile::Open("X20ML.root"); TTree *chain2 = (TTree*)gDirectory->Get("Singles");

[quote=“brun”]OK this is simple. Create 2 variables energy1 and energy2 and do

[code]double energy1, energy2;
TFile *rootfile1 = TFile::Open(“X12ML.root”);
TTree chain1 = (TTree)gDirectory->Get(“Singles”);
chain1->SetBranchAddress(“energy”,&energy1);

TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain2 = (TTree)gDirectory->Get(“Singles”);
chain2->SetBranchAddress(“energy”,&energy2);
[/code]
Rene[/quote]

[quote]My question is does chain2 automatically associate with rootfile2 in the following lines:

Code:
TFile *rootfile2 = TFile::Open(“X20ML.root”);
TTree chain2 = (TTree)gDirectory->Get(“Singles”);
[/quote]

Yes