Home | News | Documentation | Download

Convert TString to string_view

Dear all:

These days, I have some trouble with the string_view which I am not familiar with, but used intensively in RDataFrame.

Do you have some ideas on how to convert TString (which I use a lot and is very convenient) to string_view ?

For example:

TString output_dir = “/home/user/”;
TString ntuples[] = {“a.root”, b.root", “c.root”};
TString variables[] = {“jet_eta”, “jet_pt”, “jet_phi”};

ROOT::RDataFrame df(“tree_incl_all”, home_dir + ntuples[0]);
auto df_enhanced = df.Define(“weight_full”, "weight * weight ");
df_enhanced.Snapshot(“tree_incl_all”, output_dir+ntuples[0], variables);

While in Snapshot, the second parameter (output_root) is not a critical problem, I can change TString to string, but the third parameter: variables is really a issue.

Cheers,
Yingjie


Please read tips for efficient and successful posting and posting code

ROOT Version: root 6.22.06
Platform: centos 7.9
Compiler: gcc 8.3.0


Hi,
the simplest thing is to just use the standard C++ types std::string and std::vector<std::string> instead of TString and TString[]:

void foo(){

    std::string output_dir = "/home/user/";
    std::vector<std::string> ntuples = {"a.root", "b.root", "c.root"};
    std::vector<std::string> variables = {"jet_eta", "jet_pt", "jet_phi"};

    ROOT::RDataFrame df("tree_incl_all", ntuples[0]);
    auto df_enhanced = df.Define("weight_full", "weight * weight ");
    df_enhanced.Snapshot("tree_incl_all", output_dir+ntuples[0], variables);

}

But in any case you can always just convert TStrings to std::string_views with std::string_view(tstring).

Cheers,
Enrico

2 Likes

I really appreciate!

Cheers,
Yingjie

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