Open file with TChain

Hi,
I have many root files and I want to get data from them. However it didn’t work,
Here is my macro:

#include<iostream>
#include<fstream>
#include"stdio.h"
#include"TFile.h"
#include"TTree.h"
#include"TChain.h"
#include <string>
#include <vector>

using namespace std;

std::vector<std::string> GetFileList(const char* fileName)
{
  std::vector<std::string> fileList;
  char infile[1000];
  sprintf(infile, "%s.txt" , fileName);
  std::string line;
  std::ifstream ifs(infile);
  while(std::getline(ifs, line))
    {
      fileList.push_back(line);
    }
  return fileList;
}

void eldth2(){

  std::vector<std::string> FileList = GetFileList("myListFile");
  TFile* m_f;
  Double_t dth;

  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
 		m_f = TFile::Open(FileList[iFile].c_str());
		TChain *mychain = new TChain("mychain");
		mychain->Add("FileList[iFile].c_str()");
		std::cout << TChain::mychain->GetBranchAddress("dth",&dth) <<std::endl;
		
		}

  m_f->Close();

}

And it turned out:

Error: Can't call TChain::GetBranchAddress("dth",&dth) in current scope eldth2.cpp:37:
Possible candidates are...
(in TChain)
(in TTree)
(class G__CINT_ENDL)27103488
*** Interpreter error recovered ***

Does anyone know how to solve it? Or is there the other way to get data by TChain? I really want to know.
My ROOT version is 5.34/36.

Thanks.

Hi,

the C++ code contains some mistakes: for example, the loop where you build the chain should look like:

...
TChain *mychain = new TChain("mychain");
for (unsigned int iFile=0; iFile<FileList.size(); ++iFile) {
   mychain->Add(FileList[iFile].c_str());		
}
mychain->SetBranchAddress("dth",&dth) ;
...

Cheers,
D

Hi D,
I tried this:

  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile)
    {
 		m_f = TFile::Open(FileList[iFile].c_str());
		TChain *mychain = new TChain("mychain");
		mychain->Add("FileList[iFile].c_str()");
		
		}
  TChain::mychain->GetBranchAddress("dth",&dth);
  std::cout<< *dth << std::endl;

But it didn’t work out…

Error in <TFile::Open>: no url specified
Error: Can't call TChain::GetBranchAddress("dth",&dth) in current scope eldth2.cpp:37:
Possible candidates are...
(in TChain)
(in TTree)
*** Interpreter error recovered ***

Hi,

but this is not exactly what my snippet does :sweat_smile: … For example you again request a file called “FileList[iFile].c_str()” rather than running that code. Perhaps you can use directly my code?

Cheers,
D

Sorry, I forgot to change the code in brackets…
Then…

  TChain *mychain = new TChain("mychain");
  for (unsigned int iFile=0; iFile<FileList.size(); ++iFile){
	mychain->Add("FileList[iFile].c_str()");		
		}
  TChain::mychain->GetBranchAddress("dth",&dth);
  std::cout<< *dth << std::endl;
  m_f->Close();

I got:

Processing eldth2.cpp...
Error: Can't call TChain::GetBranchAddress("dth",&dth) in current scope eldth2.cpp:37:
Possible candidates are...
(in TChain)
(in TTree)
*** Interpreter error recovered ***

Hi,

ok. Again, have a look to the code I posted. You are not properly invoking the GetBranchAddress method.

Cheers,
D

Sorry…but I don’t understand. I think I actually did the same thing as you did?

Hi,
that is not proper c++.
You want to call

mychain->GetBranchAddress(...)

not

TChain::mychain->GetBranchAddress(...)

Where do you see any “GetBranchAddress” method in the TChain class description?

1 Like

Uh-oh, fair enough :sweat_smile: that’s a SetBranchAddress you want to do right?

lol. Yes I think so :slight_smile:

Thanks. I replaced it.
But when it runs std::cout<< dth << std::endl;
I got: 0. (there should have 45 numbers.) I still have something wrong on this…

And I’m confused…I want to get every data in in every entry in every root file (branch’dth’ is a 9X5 array and I have 32 root files). How can I do that without putting mychain->SetBranchAddress("dth",&dth); std::cout<< dth << std::endl; inside the loop?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.