# How to use TMath::Mean?

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:

``````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

It’s this constructor:

Double_t TMath::Mean ( Long64_t n,
const T * a,
const Double_t * 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 TMath::Mean ( Iterator first,
Iterator last,
WeightIterator w
)

so you could also use

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

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

Hi, Wile_E_Coyote,

It all makes sense for me now, thank you!