# Extract digits from the histogram's title and get the mean

Hello, I have a question that is related to extracting a title from multiple histograms that I have stored in a root file. Anyway, I have 15 histograms, and their titles are, for example, 2.55/En/3.40, and the numbers from the left and right of the title are different for every histogram. I want to create a TString operation and extract the titles of those histograms, then get the digits from the left and the right, sum them and obtain their mean values. I need those means so that I can plot a graph later on.
I have created a loop, and successfully grabbed histogramsâ€™ titles and printed them, so that works. Now, I assume that I need to add another loop and get those mean values from their titles ( 2.55/En/3.40). Can someone please help me with how to do that? I very much appreciate any help.

Check out the methods in TString
https://root.cern/doc/master/classTString.html
Maybe Atof, Index,â€¦

1 Like

You could, e.g., use the TString::Tokenize (with delim = `"/"`) to extract substrings and then the TString::Atof to get their numerical values (i.e., for the first and the last substring, and you can also use the TString::IsFloat to make sure a substring is a number).

``````{
TString title = "2.55/En/3.40";
Double_t sum = 0.;
TString tok;
Ssiz_t from = 0;
while (title.Tokenize(tok, from, "/")) if (tok.IsFloat()) sum += tok.Atof();
std::cout << "sum of all numbers = " << sum << "\n";
}
``````
2 Likes

TString is certainly useful for this kind of manipulation. In general you can search google/stackoverflow for â€śC++ split stringâ€ť or â€śPython string splitâ€ť for other solutions.

1 Like

Thanks everyone for your help and advices.

I was wondering how we can subtract those two values (3.40 - 2.55) from the title instead of doing their sum. Thanks.

``````{
TString title = "2.55/En/3.40";
Double_t v1 = 0., v2 = 0.;
TString tok;
Ssiz_t from = 0;
title.Tokenize(tok, from, "/"); v1 = tok.Atof();
title.Tokenize(tok, from, "/"); // skip "En"
title.Tokenize(tok, from, "/"); v2 = tok.Atof();
std::cout << "v2 - v1 = " << (v2 - v1) << "\n";
}
``````