TTree vector<string> slice problem


ROOT Version: All version
Platform: Mac & Linux
Compiler: Not Provided


Attached is a simple test root file. I post the code which produces it at the end. My problem is that I want to access some entry in the vector<string>, e.g. tree->Draw("br2[3]"); always giving the result tree->Draw("br2[0]"); but I check tree->Scan("br2[3]") is correct. Is this a bug? Or there is any other way to accomplish this?

#include <vector>
using namespace std;
void test(){
    vector<int> br1;
    vector<string> br2;
    vector<string> *br3;
    vector<TString> br4;
    TFile *f = new TFile("testTree.root","RECREATE");
    TTree *tree = new TTree("tree","tree");
    tree->Branch("br1",&br1);
    tree->Branch("br2",&br2);
    tree->Branch("br3",&br3);
    tree->Branch("br4",&br4);
    for(int i=0;i<100;i++){
        br1.clear(); br2.clear();br3->clear();
        br4.clear();
        for(int j=0;j<10;j++){
            br1.push_back(i*10+j);
            br2.push_back(Form("test %d",j));
            br3->push_back(Form("test %d",j));
            br4.push_back(Form("test %d",j));
        }
        tree->Fill();
    }
    f->Write();
    f->Close();
}

testTree.root (9.0 KB)

Hi,

perhaps a way to tackle this is via RDataFrame (to be modeled according to what you want to do with the strings):

ROOT::RDataFrame rdf("tree","testTree.root");
auto rdf2 = rdf.Define("br2_at3","br2[3]");
auto strings = rdf2.Take("br2_at3"); // extract the vector of all strings at position 3 in the vector stored in column br2
rdf2.Foreach([](const std::string & s){ std::cout << s << std::endl;}, {"br2_at3"}); // For each string at position 3 of the vector stored in column br2 execute a lambda that prints the string to string

Cheers,
D

vector<string> *br3 = new vector<string>;

That may be one way to do this, or read the tree branches, but they are much more complicated compared to just tree->Draw() method. I think it’s a bug cause tree->Scan() gives the correct contents while tree->Draw() not.

There is no problem when filling. It’s the problem when drawing. Anyway I tried your suggestion for safe, but the problem continues.

Since all the numerical array or vector slice work correctly, I figure an indirect way to accomplish the requirement, tree->Draw(“br2[3]”,“br1==br1[3]”).

Best.

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