How to "import" chain->Add() lines from another file

Dear Root experts,

I’m just wondering how to “import” chian->Add() lines from another file, in which we don’t need to define the TChain.

You know, if we define the TChain in that file, say, chainFiles.C

{ TChain *chain = new TChain(...); chain->Add("...") //... chain->Add("...") }

then the following works:

void main(){ gROOT->ProcessLine(".x chainFiles.C"); chain->Draw(...) }

But I want to generate “chainFiles.C” with some job management tool, like Ganga, in a general way, so it is more ideal if I can put only “chain->Add(”…")" lines in “chainFiles.C”, and move the definition of chain in the “main.C” file, you know, it is also possible that there are several TTree’s in the same root file. In this case, we can just change the definition of TChain in “main.C” instead of making difference copy of “chainFiles.C” to process different TTree. What’s the correct way to do this? The following doesn’t work:

void main(){ TChain *chain = new TChain(...); gROOT->ProcessLine(".x chainFiles.C"); chain->Draw(...) }

it works after removing “void main()”, but I also need some user functions here.

Many thanks in advance.

Cheers, Jibo

Create your mymain.C with

void mymain(){ TChain *chain = new TChain(...); gROOT->ProcessLine(".x chainFiles.C(chain)"); chain->Draw(...) }
and chainFiles.C with

void chainFiles(TChain *chain) { chain->Add("...") //... chain->Add("...") }
Rene

Hi Jibo,

I believe you’re almost certainly better of having code like:

TChain *chainPtr = new TChain ("someTree")
loadFilesToChain (chainPtr, "listOfFiles.txt")

where loadFilesToChain looks something like this (you’ll need the right headers, etc).

void loadFilesToChain (TChain *chainPtr, const std::string &filename)
{
   string line;
   ifstream source (listname.c_str(), ios::in);
   while ( getline (source, line) )
   {
      // you may want to clean line here...
     chainPtr->AddFile( line.c_str() );
   }
}

This way, you only need to keep lists of files, not try and write code that loads each set of files.

Cheers,
Charles

Hello, cplager. I am trying to add a list of files (~1000) to a chain and I would like to write the files’ names in a .txt and make the chain read the names of these files. I was trying to implement your code but I have some questions. What do you mean by “source” and “line.c_str() (what kind of file is this)”? Sorry, I am starting to learn about root now…
ps.: file.txt => file with the name of the files.root I want to add.
ch => name of the chain
reMC => name of the tree