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

**URL:** <https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229>\
**Category:** Newbie\
**Created:** [November 5, 2022, 5:02pm UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229 "2022-11-05T17:02:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [November 5, 2022, 5:02pm UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/1 "2022-11-05T17:02:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [November 6, 2022, 5:38am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/2 "2022-11-06T05:38:16Z")

</div>

Check out the methods in TString  
[https://root.cern/doc/master/classTString.html](https://root.cern/doc/master/classTString.html)  
Maybe Atof, Index,…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 6, 2022, 10:34am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/3 "2022-11-06T10:34:05Z")

</div>

You could, e.g., use the [TString::Tokenize](https://root.cern/doc/master/classTString.html#a689ddf39f10539c879dcee0c5f4113ec) (with delim = `"/"`) to extract substrings and then the [TString::Atof](https://root.cern/doc/master/classTString.html#a321710f4eedead99a46c38542d12e3a1) to get their numerical values (i.e., for the first and the last substring, and you can also use the [TString::IsFloat](https://root.cern/doc/master/classTString.html#a366da2c7a0a5dcf1513392a61474950e) to make sure a substring is a number).

```cpp
{
  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";
}

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 9, 2022, 9:08am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/4 "2022-11-09T09:08:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [November 10, 2022, 12:32am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/5 "2022-11-10T00:32:18Z")

</div>

Thanks everyone for your help and advices.

---

<div class="post-metadata">

**Author:** ![alex14](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ab097/32.png) [@alex14](https://root-forum.cern.ch/u/alex14)\
**Post date:** [November 25, 2022, 4:05am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/6 "2022-11-25T04:05:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 7:55am UTC](https://root-forum.cern.ch/t/extract-digits-from-the-histograms-title-and-get-the-mean/52229/7 "2022-11-25T07:55:31Z")

</div>

```cpp
{
  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";
}

```
