Map or TMap in TTree


I have been able to successfully store a std::map<std::string, int> into a TTree. However, I am only able to access the map w/ a compiled script (aclic) that loops over the individual entries in the tree. My question is: are the standard Draw,Scan,GetEntries, etc. functions supported for such a map? My goal would be to have a map filled per event with string cut-name and int cut decision. I would then like to do this:

root[0] : myTree->Draw(“pt”,“myMap[“IS_SELECTED”]”)

And thus only draw the pt when the flag is true.

I have also tried doing this before loading in the tree:

gROOT->ProcessLine(".L Load.C+")

where the Load.C in the CWD contains these lines:

#ifdef CINT
#pragma link C++ class map<string, int >+;

Also would something like this be possible w/ a TMap? I could use TMap<TObjString, TObject(int)>???

If it turns out that this is not possible on the command line, I will not be heart broken.



why would you use a map for this. Why not define a word in the tree in which you store a bitmask of the cut decisions? You can then do:

root[0] : myTree->Draw(“pt”,“cutmask&IS_SELECTED”)

or if you need more then 64 bits you can use a TBits object instead of a Long64_t.

Note that any solution based on multiple string lookups and comparisons for each event will be very slow.

Cheers, Fons.


Ok I agree that this map is over-kill. We just have two pieces of code one that does our main cut-flow over a large ntuple and another piece that takes information from that code such as cut decisions and simple float variables and dumps into a simple flat ntuple. In the end I decided to just allocate X branches w/ dummy names and then call SetBranchName as soon as one piece of code was able to communicate w/ the other about what the actual cut value names would be. Thus ending up w/ 25 Bools in the tree( or whatever size the map was).

I think I may also look into this TBits class–it seems quite a bit more compact than what I am doing.


Hi Justin,

TTree::Draw does not support calling any function (besides strstr and strcmp) that do not take numerical arguments and thus you would not be able to use a map<string,int> for your purpose within TTree::Draw.
Also the operator[] is special for TTree::Draw and you would not be able to use map’s operator[], which anyway you should not use for this purpose as map’s operator[] is both an insert and a lookup, instead you ought to use map::find.

In addition, map are extremely slow to read in (compared to TBits or even an std::vector).

So for all those reasons, please follow Fons’ advice :slight_smile: