# How to use TMath::Mean?

**URL:** <https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824>\
**Category:** Newbie\
**Created:** [July 15, 2022, 9:47pm UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824 "2022-07-15T21:47:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 15, 2022, 9:47pm UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824/1 "2022-07-15T21:47:56Z")

</div>

Hi, ROOT experts,

I want to divide a whole array into parts and find the average value for each part by using TMath::Mean, and then save them as the elements of a vector.

I am referring to this part of the code:

```auto
vector<double> divideArrayIntoParts( const int numPoint, const int nParts, const double *array )
{
  int partLength = numPoint / nParts;
  vector<double> vParameter;
  for (int iPart = 0; iPart < nParts; iPart++)
  {
    int partStart = iPart * partLength;
    vParameter.push_back( TMath::Mean(partLength, array + partStart) );
  }
  return vParameter;
}

```

I don’t understand why the second argument in TMath::Mean looks like this:  
`TMath::Mean( partLength, array + partStart )`  
Why is it `array + partStart`?  
Is this correct or is there a more straightforward way where I can use TMath::Mean for that purpose?

Best Regards,  
Jackhammer

---

<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:** [July 16, 2022, 4:02am UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824/2 "2022-07-16T04:02:38Z")

</div>

[https://root.cern/doc/master/namespaceTMath.html#a938fcf0decadd98478f050668c53192a](https://root.cern/doc/master/namespaceTMath.html#a938fcf0decadd98478f050668c53192a)  
It’s this constructor:

| [Double\_t](https://root.cern/doc/master/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) TMath::Mean | ( | [Long64\_t](https://root.cern/doc/master/RtypesCore_8h.html#adaf6e69e06203ab5ae4994d1e7590647) | _n_, |
| --- | --- | --- | --- |
| | | const T \* | _a_, |
| | | const [Double\_t](https://root.cern/doc/master/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) \* | _w_ = `0` |
| | ) | | |
| “Returns the weighted mean of an array a with length n” | | | |

If you follow the link on “Definition at line…” you’ll see the code, where you’ll see it calls the constructor above it:

| [Double\_t](https://root.cern/doc/master/RtypesCore_8h.html#ab9b5334647b78ec4256db251e3ae1fc6) TMath::Mean | ( | Iterator | _first_, |
| --- | --- | --- | --- |
| | | Iterator | _last_, |
| | | WeightIterator | _w_ |
| | ) | | |

so you could also use

```auto
  TMath::Mean(array+partStart, array+partStart+partLength)

```

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 18, 2022, 1:46am UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824/3 "2022-07-18T01:46:18Z")

</div>

Hi, dastudillo,

Thank you for your reply. Now I know how to put in the arguments for this function, although I still don’t fully understand why it is defined this way and why the “+” sign comes after the array. But at least I know how to use this function now. Thank you.

Best Regards,  
Jackhammer

---

<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:** [July 18, 2022, 6:30am UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824/4 "2022-07-18T06:30:17Z")

</div>

[Uncle Google → C/C++ pointer arithmetic](https://www.google.com/search?q=C%2FC%2B%2B+pointer+arithmetic)

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [July 18, 2022, 7:21am UTC](https://root-forum.cern.ch/t/how-to-use-tmath-mean/50824/5 "2022-07-18T07:21:42Z")

</div>

Hi, Wile\_E\_Coyote,

It all makes sense for me now, thank you!
