String to const char* for FindObject

Dear root experts,

I am trying to convert a string (or TString) to const char* in order to use it as argument for the FindObject function.

I have tried both c_str() and TString.Data() but in both cases FindObject returns a null pointer (cannot find the relevant histogram)

Any idea about it?

Thank you,
Panagiotis

Try with “FindObjectAny”. If it doesn’t succeed, try: gDirectory->ls();

Hi @Wile_E_Coyote ,

it didnt succeed… I can get the histogram by setting its name directly in FindObject() (or FindObjectAny), but not with the converted string…

Any other idea?

thank you,
Panagiotis

Make sure that your std::string (or TString) contains what you expect.

I guess that by directly you are referring to specifying a string literal as in FindObject("name"). Both, c_str() member function of std::string and TString::Data() should return a C string (NUL terminated), which shouldn’t have any difference w.r.t. a string literal.

Therefore, as @Wile_E_Coyote suggested, make sure that the original std::string or TString contains what you expect.

Cheers,
J.

Hi @Wile_E_Coyote @jalopezg ,

thank you for you replies!

So, the problem may be a bit different. I tried to get the histogram with Get instead of FindObject. It worked for case 1 and 2, but not for case 3 which is the one that i want to use. It looks like coming from the addition of 2 strings. This is the relevant part of the code

ifstream SysTXT("Sys.txt");
string str;

int I=-1;
while (std::getline(SysTXT, str))
{

string s="Systematics/hhttbbVBFl1cvv0p5cv1_SysTAUS_TRUEHADTAU_EFF_RNNID_HIGHPT__1down";
const char * c = s.c_str();

string s1="Systematics/hhttbbVBFl1cvv0p5cv1_";
string s12=s1+str;
const char * c2 = s12.c_str();
cout<<"Sys : "<<c2<<endl;

TH1F* H1    = (TH1F*)file->Get("Systematics/hhttbbVBFl1cvv0p5cv1_SysTAUS_TRUEHADTAU_EFF_RNNID_HIGHPT__1down");
TH1F* H2    = (TH1F*)file->Get(c);
TH1F* H3    = (TH1F*)file->Get(c2);

cout<<H1->GetName()<<endl;
cout<<H2->GetName()<<endl;
cout<<H3->GetName()<<endl;

This is the terminal output :

Sys : Systematics/hhttbbVBFl1cvv0p5cv1_SysTAUS_TRUEHADTAU_EFF_RNNID_HIGHPT__1down
hhttbbVBFl1cvv0p5cv1_SysTAUS_TRUEHADTAU_EFF_RNNID_HIGHPT__1down
hhttbbVBFl1cvv0p5cv1_SysTAUS_TRUEHADTAU_EFF_RNNID_HIGHPT__1down
Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_8:1:
/afs/cern.ch/work/p/pbellos/HH_WS/WSMaker_HH_bbtautau/inputs/Rew.cpp:63:11: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
cout<< H3->GetName()<<endl;

I also checked for aditional space character at the end of the “str” string in the txt file. Any other suggestion?

Thank you,
Panagiotis

It could be that you have some “invisible” character somewhere in your input text file.
Try:

std::cout << strlen(c) << std::endl;
std::cout << strlen(c2) << std::endl;
std::cout << strcmp(c, c2) << std::endl;

Hi @Wile_E_Coyote,

yes, that was the problem. It is running now!

thank you,
Panagiotis

@panagiotis_bellos Also, for future reference (and similar issues), if a string is coming from a text file, you might also need to ensure that the file was not written using a multi-byte encoding.

That might be especially confusing, as your terminal emulator might still render the “expected” character sequence, while the null-terminated C string that you are passing contains extra bytes.

1 Like

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