How to read tree branches from several root files

Hello rooters!
I need a help, this is my code. I wanna read branch from all root file the same time.

[code]void practice()
{
gROOT->Reset();
gStyle->SetOptStat(111111);
gStyle->SetOptFit(11111111);
gStyle->SetPalette(1);

TFile *f[4];

*f[0] = (“Beamthrough5runs/DstKKAna_00883.root”);
*f[1] = (“Beamthrough5runs/DstKKAna_00885.root”);
*f[2] = (“Beamthrough5runs/DstKKAna_00887.root”);
*f[3] = (“Beamthrough5runs/DstKKAna_00889.root”);
//gFile->ls();

TTree tree=(TTree)gFile->Get(“kk”);
//tree->Print();

//TTree *tree[];

tree[0] -> *f[0] -> Get(“tree”);
tree[1] -> *f[1] -> Get(“tree”);
tree[2] -> *f[2] -> Get(“tree”);
tree[3] -> *f[3] -> Get(“tree”);[/code]

After compiling I got an error like this. Can anyone to tackle it??

/misc/Dhyp2/iskendir/analyzer/k18analyzer_20160530/./practice.C:51:11: error: base operand of '->' has non-pointer type 'TTree' tree[0] -> *f[0] -> Get("tree"); ^

Hi,

Do you really have this code:

TFile *f[4];
  *f[0] = ("Beamthrough5runs/DstKKAna_00883.root");

or is that an artifact of us changing the forum software? If this is indeed what you wrote then no, that is not C++. And ROOT 6 would have told you that this cannot work.

What you are probably looking for is TChain:

TChain chain("h42"); // name of the tree, in your case "kk"
// Add a few files:
chain.Add("$H1/dstarmb.root");
chain.Add("$H1/dstarp1a.root");
chain.Add("$H1/dstarp1b.root");
chain.Add("$H1/dstarp2.root");

That chain behaves like a TTree spanning multiple TFiles.

Cheers, Axel.

1 Like

Many thanks Dear Axel!!

#include"TFile.h"
#include"TTree.h"
#include"TH1.h"
#include"TH2.h"
#include "TF1.h"
#include"TCanvas.h"
#include "TStyle.h"
#include"TROOT.h"
#include "TPad.h"
#include "TAxis.h"
#include "Rtypes.h"
#include "TAttAxis.h"
#include "TPave.h"
#include "TText.h"
#include "TLine.h"
#include "TLatex.h"
#include "THStack.h"
#include "TMath.h"
#include "TGraph.h"

//C++ language
#include <iostream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

//const int T=50;

void SdcinandoutLocal()
{  
  gROOT->Reset();
  gStyle->SetOptStat(111111);
  gStyle->SetOptFit(11111111);
  gStyle->SetPalette(1);
  
  TFile *f[4];
  
  f[0] = new TFile("KuramaTrack/KuramaTracking_00883.root");
  f[1] = new TFile("KuramaTrack/KuramaTracking_00885.root");
  f[2] = new TFile("KuramaTrack/KuramaTracking_00887.root");
  f[3] = new TFile("KuramaTrack/KuramaTracking_00889.root");
  
  TTree *tree[4];
  tree[0]= (TTree*)f[0]->Get("kurama");
  tree[1]= (TTree*)f[1]->Get("kurama");
  tree[2]= (TTree*)f[2]->Get("kurama");
  tree[3]= (TTree*)f[3]->Get("kurama");

  Int_t nlSdcIn;//SDC1 is from 1 to 6, SSD from 7 to 14, SDC2,3 from 31 to 38
  Int_t nlSdcOut;

  vector<double>  ResL1(20);
  //double ResL1[6];
  vector<double>  ResL2;
  vector<double>  ResL3;
  vector<double>  ResL4;
  vector<double>  ResL5;
  vector<double>  ResL6;
  vector<double>  ResL7;
  vector<double>  ResL8;
  vector<double>  ResL9;
  vector<double>  ResL10;
  vector<double>  ResL11;
  vector<double>  ResL12;
  vector<double>  ResL13;
  vector<double>  ResL14;
  vector<double>  ResL31;
  vector<double>  ResL32;
  vector<double>  ResL33;
  vector<double>  ResL34;
  vector<double>  ResL35;
  vector<double>  ResL36;
  vector<double>  ResL37;
  vector<double>  ResL38;
  Int_t           ntSdcIn;
  //for 883
  tree[0]->SetBranchStatus("*",0);
  tree[0]->SetBranchStatus("ResL1",1);
  tree[0]->SetBranchStatus("ResL2",1);
  tree[0]->SetBranchStatus("ResL3",1);
  tree[0]->SetBranchStatus("ResL4",1);
  tree[0]->SetBranchStatus("ResL5",1);
  tree[0]->SetBranchStatus("ResL6",1);
  tree[0]->SetBranchStatus("ResL7",1);
  tree[0]->SetBranchStatus("ResL8",1);
  tree[0]->SetBranchStatus("ResL9",1);
  tree[0]->SetBranchStatus("ResL10",1);
  tree[0]->SetBranchStatus("ResL11",1);
  tree[0]->SetBranchStatus("ResL12",1);
  tree[0]->SetBranchStatus("ResL13",1);
  tree[0]->SetBranchStatus("ResL14",1);
  tree[0]->SetBranchStatus("ResL31",1);
  tree[0]->SetBranchStatus("ResL32",1);
  tree[0]->SetBranchStatus("ResL33",1);
  tree[0]->SetBranchStatus("ResL34",1);
  tree[0]->SetBranchStatus("ResL35",1);
  tree[0]->SetBranchStatus("ResL36",1);
  tree[0]->SetBranchStatus("ResL37",1);
  tree[0]->SetBranchStatus("ResL38",1);
  tree[0]->SetBranchStatus("ntSdcIn",1);
  
  tree[0]->SetBranchAddress("ResL1",&ResL1);
  tree[0]->SetBranchAddress("ResL2",&ResL2);
  tree[0]->SetBranchAddress("ResL3",&ResL3);
  tree[0]->SetBranchAddress("ResL4",&ResL4);
  tree[0]->SetBranchAddress("ResL5",&ResL5);
  tree[0]->SetBranchAddress("ResL6",&ResL6);
  tree[0]->SetBranchAddress("ResL7",&ResL7);
  tree[0]->SetBranchAddress("ResL8",&ResL8);
  tree[0]->SetBranchAddress("ResL9",&ResL9);
  tree[0]->SetBranchAddress("ResL10",&ResL10);
  tree[0]->SetBranchAddress("ResL11",&ResL11);
  tree[0]->SetBranchAddress("ResL12",&ResL12);
  tree[0]->SetBranchAddress("ResL13",&ResL13);
  tree[0]->SetBranchAddress("ResL14",&ResL14);
  tree[0]->SetBranchAddress("ResL31",&ResL31);
  tree[0]->SetBranchAddress("ResL32",&ResL32);
  tree[0]->SetBranchAddress("ResL33",&ResL33);
  tree[0]->SetBranchAddress("ResL34",&ResL34);
  tree[0]->SetBranchAddress("ResL35",&ResL35);
  tree[0]->SetBranchAddress("ResL36",&ResL36);
  tree[0]->SetBranchAddress("ResL37",&ResL37);
  tree[0]->SetBranchAddress("ResL38",&ResL38);
  tree[0]->SetBranchAddress("ntSdcIn",&ntSdcIn);

  //for 885
  tree[1]->SetBranchStatus("*",0);
  tree[1]->SetBranchStatus("ResL1",1);
  tree[1]->SetBranchStatus("ResL2",1);
  tree[1]->SetBranchStatus("ResL3",1);
  tree[1]->SetBranchStatus("ResL4",1);
  tree[1]->SetBranchStatus("ResL5",1);
  tree[1]->SetBranchStatus("ResL6",1);
  tree[1]->SetBranchStatus("ResL7",1);
  tree[1]->SetBranchStatus("ResL8",1);
  tree[1]->SetBranchStatus("ResL9",1);
  tree[1]->SetBranchStatus("ResL10",1);
  tree[1]->SetBranchStatus("ResL11",1);
  tree[1]->SetBranchStatus("ResL12",1);
  tree[1]->SetBranchStatus("ResL13",1);
  tree[1]->SetBranchStatus("ResL14",1);
  tree[1]->SetBranchStatus("ResL31",1);
  tree[1]->SetBranchStatus("ResL32",1);
  tree[1]->SetBranchStatus("ResL33",1);
  tree[1]->SetBranchStatus("ResL34",1);
  tree[1]->SetBranchStatus("ResL35",1);
  tree[1]->SetBranchStatus("ResL36",1);
  tree[1]->SetBranchStatus("ResL37",1);
  tree[1]->SetBranchStatus("ResL38",1);

  tree[1]->SetBranchAddress("ResL1",&ResL1);
  tree[1]->SetBranchAddress("ResL2",&ResL2);
  tree[1]->SetBranchAddress("ResL3",&ResL3);
  tree[1]->SetBranchAddress("ResL4",&ResL4);
  tree[1]->SetBranchAddress("ResL5",&ResL5);
  tree[1]->SetBranchAddress("ResL6",&ResL6);
  tree[1]->SetBranchAddress("ResL7",&ResL7);
  tree[1]->SetBranchAddress("ResL8",&ResL8);
  tree[1]->SetBranchAddress("ResL9",&ResL9);
  tree[1]->SetBranchAddress("ResL10",&ResL10);
  tree[1]->SetBranchAddress("ResL11",&ResL11);
  tree[1]->SetBranchAddress("ResL12",&ResL12);
  tree[1]->SetBranchAddress("ResL13",&ResL13);
  tree[1]->SetBranchAddress("ResL14",&ResL14);
  tree[1]->SetBranchAddress("ResL31",&ResL31);
  tree[1]->SetBranchAddress("ResL32",&ResL32);
  tree[1]->SetBranchAddress("ResL33",&ResL33);
  tree[1]->SetBranchAddress("ResL34",&ResL34);
  tree[1]->SetBranchAddress("ResL35",&ResL35);
  tree[1]->SetBranchAddress("ResL36",&ResL36);
  tree[1]->SetBranchAddress("ResL37",&ResL37);
  tree[1]->SetBranchAddress("ResL38",&ResL38);

  //for 887
  tree[2]->SetBranchStatus("*",0);
  tree[2]->SetBranchStatus("ResL1",1);
  tree[2]->SetBranchStatus("ResL2",1);
  tree[2]->SetBranchStatus("ResL3",1);
  tree[2]->SetBranchStatus("ResL4",1);
  tree[2]->SetBranchStatus("ResL5",1);
  tree[2]->SetBranchStatus("ResL6",1);
  tree[2]->SetBranchStatus("ResL7",1);
  tree[2]->SetBranchStatus("ResL8",1);
  tree[2]->SetBranchStatus("ResL9",1);
  tree[2]->SetBranchStatus("ResL10",1);
  tree[2]->SetBranchStatus("ResL11",1);
  tree[2]->SetBranchStatus("ResL12",1);
  tree[2]->SetBranchStatus("ResL13",1);
  tree[2]->SetBranchStatus("ResL14",1);
  tree[2]->SetBranchStatus("ResL31",1);
  tree[2]->SetBranchStatus("ResL32",1);
  tree[2]->SetBranchStatus("ResL33",1);
  tree[2]->SetBranchStatus("ResL34",1);
  tree[2]->SetBranchStatus("ResL35",1);
  tree[2]->SetBranchStatus("ResL36",1);
  tree[2]->SetBranchStatus("ResL37",1);
  tree[2]->SetBranchStatus("ResL38",1);
  
  tree[2]->SetBranchAddress("ResL1",&ResL1);
  tree[2]->SetBranchAddress("ResL2",&ResL2);
  tree[2]->SetBranchAddress("ResL3",&ResL3);
  tree[2]->SetBranchAddress("ResL4",&ResL4);
  tree[2]->SetBranchAddress("ResL5",&ResL5);
  tree[2]->SetBranchAddress("ResL6",&ResL6);
  tree[2]->SetBranchAddress("ResL7",&ResL7);
  tree[2]->SetBranchAddress("ResL8",&ResL8);
  tree[2]->SetBranchAddress("ResL9",&ResL9);
  tree[2]->SetBranchAddress("ResL10",&ResL10);
  tree[2]->SetBranchAddress("ResL11",&ResL11);
  tree[2]->SetBranchAddress("ResL12",&ResL12);
  tree[2]->SetBranchAddress("ResL13",&ResL13);
  tree[2]->SetBranchAddress("ResL14",&ResL14);
  tree[2]->SetBranchAddress("ResL31",&ResL31);
  tree[2]->SetBranchAddress("ResL32",&ResL32);
  tree[2]->SetBranchAddress("ResL33",&ResL33);
  tree[2]->SetBranchAddress("ResL34",&ResL34);
  tree[2]->SetBranchAddress("ResL35",&ResL35);
  tree[2]->SetBranchAddress("ResL36",&ResL36);
  tree[2]->SetBranchAddress("ResL37",&ResL37);
  tree[2]->SetBranchAddress("ResL38",&ResL38);

  // for 889
  tree[3]->SetBranchStatus("*",0);
  tree[3]->SetBranchStatus("ResL1",1);
  tree[3]->SetBranchStatus("ResL2",1);
  tree[3]->SetBranchStatus("ResL3",1);
  tree[3]->SetBranchStatus("ResL4",1);
  tree[3]->SetBranchStatus("ResL5",1);
  tree[3]->SetBranchStatus("ResL6",1);
  tree[3]->SetBranchStatus("ResL7",1);
  tree[3]->SetBranchStatus("ResL8",1);
  tree[3]->SetBranchStatus("ResL9",1);
  tree[3]->SetBranchStatus("ResL10",1);
  tree[3]->SetBranchStatus("ResL11",1);
  tree[3]->SetBranchStatus("ResL12",1);
  tree[3]->SetBranchStatus("ResL13",1);
  tree[3]->SetBranchStatus("ResL14",1);
  tree[3]->SetBranchStatus("ResL31",1);
  tree[3]->SetBranchStatus("ResL32",1);
  tree[3]->SetBranchStatus("ResL33",1);
  tree[3]->SetBranchStatus("ResL34",1);
  tree[3]->SetBranchStatus("ResL35",1);
  tree[3]->SetBranchStatus("ResL36",1);
  tree[3]->SetBranchStatus("ResL37",1);
  tree[3]->SetBranchStatus("ResL38",1);

  tree[3]->SetBranchAddress("ResL1",&ResL1);
  tree[3]->SetBranchAddress("ResL2",&ResL2);
  tree[3]->SetBranchAddress("ResL3",&ResL3);
  tree[3]->SetBranchAddress("ResL4",&ResL4);
  tree[3]->SetBranchAddress("ResL5",&ResL5);
  tree[3]->SetBranchAddress("ResL6",&ResL6);
  tree[3]->SetBranchAddress("ResL7",&ResL7);
  tree[3]->SetBranchAddress("ResL8",&ResL8);
  tree[3]->SetBranchAddress("ResL9",&ResL9);
  tree[3]->SetBranchAddress("ResL10",&ResL10);
  tree[3]->SetBranchAddress("ResL11",&ResL11);
  tree[3]->SetBranchAddress("ResL12",&ResL12);
  tree[3]->SetBranchAddress("ResL13",&ResL13);
  tree[3]->SetBranchAddress("ResL14",&ResL14);
  tree[3]->SetBranchAddress("ResL31",&ResL31);
  tree[3]->SetBranchAddress("ResL32",&ResL32);
  tree[3]->SetBranchAddress("ResL33",&ResL33);
  tree[3]->SetBranchAddress("ResL34",&ResL34);
  tree[3]->SetBranchAddress("ResL35",&ResL35);
  tree[3]->SetBranchAddress("ResL36",&ResL36);
  tree[3]->SetBranchAddress("ResL37",&ResL37);
  tree[3]->SetBranchAddress("ResL38",&ResL38);
  
  
  Long64_t nentries = tree[0]->GetEntries();

       
  TCanvas *c1 = new TCanvas("c1","c1",1000,800);
  TCanvas *c2 = new TCanvas("c2","c2",1000,800);
  TCanvas *c3 = new TCanvas("c3","c3",1000,800);
  TCanvas *c4 = new TCanvas("c4","c4",1000,800);
  
  
  c1->Divide(3,3);
  c2->Divide(3,3);
  c3->Divide(3,3);
  c4->Divide(3,3);

  TH1F *residual883_1 = new TH1F("","",100,-5,5);
  TH1F *residual883_2 = new TH1F("","",100,-5,5);
  TH1F *residual883_3 = new TH1F("","",100,-5,5);
  TH1F *residual883_4 = new TH1F("","",100,-5,5);
  TH1F *residual883_5 = new TH1F("","",100,-5,5);
  TH1F *residual883_6 = new TH1F("","",100,-5,5);

  

    
  for(int i=0;i<nentries;i++){ 
    tree[0]->GetEntry(i);
    for(int i=0;i<;i++){
      residual883_1->Fill(ResL1[i]);
    }
  }
  c1->cd(1)->SetGrid();
  residual883_1->Draw();







  
}

Dear Axel,

Hello, please, help me. After running this code I got as below:

Error in <TTree::SetBranchAddress>: The address for "ResL1" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL2" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL3" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL4" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL5" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL6" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL7" should be the address of a pointer!
Error in <TTree::SetBranchAddress>: The address for "ResL8" should be the address of a pointer!
.......

So what should I do?

You will need to do as the error suggests and provide the address of a pointer to a vector:

...
vector<double>  *ResL1 = 0;
...
tree[0]->SetBranchAddress("ResL1",&ResL1);
...
  for(int i=0;i<nentries;i++){ 
    tree[0]->GetEntry(i);
    for(int i=0;i<;i++){
      residual883_1->Fill(ResL1->at(i));
    }
  }
...

Right, I corrected it.

Dear ksmith!

many thanks. However, I got this after running

terminate called after throwing an instance of 'std::out_of_range’
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

How can I fix it?

This is the message (exception) that is thrown when an error occurs in std::vector::at (see the intro and parameters). If you had used the method std::vector::oprator[] a segmentation fault may have occurred instead and most likely would have been harder to track down. Thus the reason I recommended std::vetor::at int he beginning.

As for resolving the issue, I had not looked closely at the details of your for loop previously, and there are a couple tips I can suggest.
You should avoid using the same variable name for your iterators, that causes shadowing and leads to unforeseen issues. It is a good idea to use more descriptive names as well instead of just i, this can become confusing when you have many nested loops. In addition, you should check against the size of the vector to make sure you are not trying to access invalid locations.

for(int entry=0; entry < nentries; entry++){ 
    tree[0]->GetEntry(entry);
    for(int item = 0; item < ResL1->size(); item++){
      residual883_1->Fill(ResL1->at(i));
    }
  }

Or

for(int entry=0; entry < nentries; entry++){ 
    tree[0]->GetEntry(entry);
    for(auto itr = ResL1->begin(); itr != ResL1->end(); ++itr){
      residual883_1->Fill(*itr);
    }
  }

Dear ksmith!!

Many thanks for your useful tips.

Finally, this has been solved.

Best Regards,

Abzal