Hello, I need to copy some branches from an input tree and an output tree only for events passing my selection. I don’t think I can use the CloneTree(0) because I need to add the branches to an existing TTree which has its own branches.
I’m doing something like this:
vector<string> branch_names;
branch_names.push_back("Npv");
branch_names.push_back("jet_AntiKt4TopoEMJets_flavor_weight_JetFitterCOMBNN");
for (unsigned int i=0; i!=branch_names.size(); ++i)
{
const char* b = branch_names[i].c_str();
TBranch* branch = fChain->GetBranch(b);
if (string(branch->GetClassName()) == "")
{
output_tree->Branch(b,
fChain->GetBranch(b)->GetAddress());
}
else
{
output_tree->Branch(b,
branch->GetClassName(),
(void*) fChain->GetBranch(b)->GetAddress());
}
}
in particular Npv is a Float_t and jet_AntiKt4TopoEMJets_flavor_weight_JetFitterCOMBNN is a vector. I’m reading the input file using the MakeClass so the variables are:
[quote] It works with a single TTree, will it work with TChain?[/quote]With a TChain the challenge is that the branch ‘address’ can and will change from TTree to TTree unless they have been fixed by a call to TChain::SetBranchAddress.
[quote] It works with a single TTree, will it work with TChain?[/quote]With a TChain the challenge is that the branch ‘address’ can and will change from TTree to TTree unless they have been fixed by a call to TChain::SetBranchAddress.
Philippe.[/quote]
yes, ok, but what about the code. Sometimes it works, sometimes not. The problem is that GetAddress return void* so how to distinguish between the various type? I’ve changed to
for (vector<string>::const_iterator it = m_branchnames_list.begin();
it != m_branchnames_list.end(); ++it)
{
TBranch* branch = input_tree->GetBranch(it->c_str());
assert(branch);
if (string(branch->GetClassName()) == "") {
string id = input_tree->GetLeaf(it->c_str())->GetTypeName();
if (id == "Char_t") id = "B";
else if (id == "UChar_t") id = "b";
else if (id == "Short_t") id = "S";
else if (id == "UShort_t") id = "s";
else if (id == "Int_t") id = "I";
else if (id == "UInt_t") id = "i";
else if (id == "Float_t") id = "F";
else if (id == "Double_t") id = "D";
else if (id == "Long64_t") id = "L";
else if (id == "ULong64_t") id = "l";
else if (id == "Bool_t") id = "O";
else {
cout << "cannot translate " << id << endl;
assert(false);
}
output_tree->Branch(it->c_str(),
branch->GetAddress(),
(branch->GetName() + string("/") + id).c_str());
}
else {
output_tree->Branch(it->c_str(),
branch->GetClassName(),
(void*) branch->GetAddress());
}
}