Create tree branch from map content problem

Hello
I’m trying to create a tree with its branch determined by reading input, here is a example if the map contain 3 different module, but in the end, when I look at the out put file, the tree “t1” is empty, does anyone could help? Thanks.

map<string,double> timingPerModule_;
string module;
module=“module1”;
timingPerModule_[module] +=1.1;
timingPerModule_[module] +=2.1;
module=“module2”;
timingPerModule_[module] +=10.1;
module=“module3”;
timingPerModule_[module] +=100.1;

TTree *t1 =new TTree(“t1”,"tree with ");
auto map_it = timingPerModule_.cbegin();
while (map_it != timingPerModule_.cend()){
cout<<“name = “<<map_it->first<<” , value=”<<map_it->second<<endl;

t1->Branch(Form("%s",map_it->first.c_str()),map_it->second);
map_it++;

}
t1->Fill();

Hi,

you did not associate any branch of the tree to the map. You can see how to do this in this tutorial: https://root.cern/doc/master/hvector_8C.html

Cheers,
D

sorry I miss a line when I copy and paste from my code, it is there but not work, the line is :
t1->Branch(Form(“%s”,map_it->first.c_str()),map_it->second);
inside the while loop, so the original code will be this :

TTree *t1 =new TTree(“t1”,"tree with ");
auto map_it = timingPerModule_.cbegin();
while (map_it != timingPerModule_.cend()){
cout<<“name = “<first<first.c_str()),map_it->second);
t1->Branch(Form(”%s”,map_it->first.c_str()),map_it->second);
map_it++;
}
t1->Fill();

I think that what you mean associate a branch to the tree, but it does not work, the tree is empty after fill and file write.

Try with:

t1->Branch(Form("%s",map_it->first.c_str()), &map_it->second);

However it might still not work.
i.e. it is likely that the problem is that the address captured by the branch is that of a temporary …

Cheers,
Philippe.

That’s what I also had tryied , by using &map_it->second, but it will return error before running and stop . error shows :
TTree.h:336:14: error: no matching member function for call to ‘BranchImpRef’

Hi,

this looks like a problem linked to the fact that the addresses are temporary and not a root issue. A way in which you can overcome this hurdle if the keys of your map are always identical is to create for the N keys N fp numbers associated to N branches which you will update for every entry.

Cheers,
D

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