Trees, Chain and Friends, how to use correctly

Hallo,

so now a real problem:
I have a list of files that have a number of trees and I want to join them all to one tree (via friends), that I can access in a standalone programm. Here is what I do:


TTree *fChain;
vector <TChain> chains;
TChain	*mainchain;

Int_t aan_EV110005::init(Int_t iflags, vector <string> filetreenames, char* oname)
 {
 aan_analysis::init( iflags, filetreenames, oname);
 treenames.clear();
 treenames.push_back("EV0");
 treenames.push_back("Truth0");
 treenames.push_back("CollectionTree");

 chains.push_back(new TChain(treenames[0].c_str()));
 for (int i=0; i <filetreenames>Add(inputfilenames[i].c_str());
  }

 mainchain = chains[0];

 for (int i=1 ; i < treenames.size(); i++) {

  chains.push_back( new TChain(treenames[i].c_str()));
  for (int j=0; j <inputfilenames>Add(inputfilenames[j].c_str());
   }

  mainchain->AddFriend((TTree*)chains[i],treenames[i].c_str());
  }
  
  fChain=(TTree*)mainchain;
  
//  fChain->Scan("EV0.HT");
//  fChain->Scan("Truth0.Truth_N");
//  fChain->Scan("CollectionTree.EventNumber");

 Init();
}

I have to create a TChain* (chains[0]) with the first tree (EV0), and add the list of files. This point I assign to mainchain. Then for the next trees, I do the same steps, but after each Add I add them as Friends to mainchain.
In the end I need a TTree* so I cast mainchain to a TTree* and assign it to fChain.
Sounds complicated, but this works.

I started ealier with an empty Tree* fChain and created all chains and added them as friends to fChain, but this didn’t work, since no tree variables (actually of the friends) were retrieved.

If I start this now and come to point where I do fChain->SetBranchAddress(“EV0.HT”,&HT) (i want to make sure to get the right variables in case of double naming of variables in the different trees), I get the warning:

but whenever I do fChain->GetEntry() the variables are retrieved correctly and the values are ok. So this is not necessary the problem.

I can get rid of this message if I uncomment the lines in the code which call ->Scan(“…”), but this is only a poor work around.

Has anyone a better idea? Or can I live with the error message?

Thank you
Duc

[quote]Sounds complicated, but this works. [/quote]And is the right way :slight_smile:

[quote]In the end I need a TTree* so I cast mainchain to a TTree* and assign it to fChain.[/quote]The cast should not be necessary since TChain inherits from TTree.

[quote]If I start this now and come to point where I do fChain->SetBranchAddress(“EV0.HT”,&HT) (i want to make sure to get the right variables in case of double naming of variables in the different trees), I get the warning:…[/quote]This seems like it might a bug :slight_smile:. Could you send me a complete example to reproduce the problem?

Cheers,
Philippe.

Actually the code is embedded in a large piece of code, you should be able to reproduce the bug with the code above.

Leave out the line with
[ul]

  • aan_analysis::init( iflags, filetreenames, oname); which only copies the parameters from the function to some internal variables of the class
    *Init()
    *and change

Int_t aan_EV110005::init(Int_t iflags, vector <string> filetreenames, char* oname) to something like

*and the was a typo in the variable definiton:

vector <TChain> chains; should be of course

vector <TChain> chains; (the * after TChain cannot be displayed !)
[/ul]

I can send you of course the full code (which spans over a couple of files) or prepare some short example.

Cheers Duc

[quote]I can send you of course the full code (which spans over a couple of files) or prepare some short example. [/quote]Could you please send me a short running example?

Thanks,
Philippe

Hallo,

sorry, I couldn’t answer earlier and I saw that the code in my first post doesn’t run without some major modification.

I have attached a .C file which compiles with ROOT and that demonstrates the behavior. I also included the .root files that is loaded in the script. You can change that if you like. The lines where to change it are marked.

Cheers
Duc
TopViewAANtuple.root (75.6 KB)
short_ex.C (1.43 KB)

HUmm … I must be missing something essential and can not reproduce your problem.

Your comment:[quote]// uncomment these blocks to get rid of error[/quote]
seems to indicates that I should get the error message without changing the code. However I get:

> root.exe -b -l short_ex.C+ root [0] Processing short_ex.C+... Info in <TUnixSystem::ACLiC>: creating shared library /var/tmp/./short_ex_C.so In file included from /var/tmp/./file6ofZjH.h:32, from /var/tmp/./file6ofZjH.cxx:16: /var/tmp/./short_ex.C: In function `void short_ex()': /var/tmp/./short_ex.C:29: warning: comparison between signed and unsigned integer expressions /var/tmp/./short_ex.C:35: warning: comparison between signed and unsigned integer expressions /var/tmp/./short_ex.C:38: warning: comparison between signed and unsigned integer expressions new chains[1] = Truth0 add filename[0]=TopViewAANtuple.root Add Friend new chains[2] = CollectionTree add filename[0]=TopViewAANtuple.root Add Friend Warning in <TClass::TClass>: no dictionary for class AttributeListLayout is available Warning in <TClass::TClass>: no dictionary for class pair<string,string> is available 324524 root [1] .q 3.550u 0.470s 2:02.88 3.2% 0+0k 0+0io 9802pf+0w

At this point my best guess is that you are using an older version of ROOT and that the problem has been fixed since.

Cheers,
Philippe

Hi,

so the version I use is 5.10.00 and in 5.13.04 it is gone.

Thanks
Duc