# How to take average of vector values

Hi all,
I need a way to average all the values in a vector, and I am using `std::accumulate` right now with no success…

Here is a snippet of my code, the bold is the use of the vector `v`:

``````std::vector<double> v;
for(int g=0; g<8257; g++){

for (Long64_t j=0; j< 1000; j++) {
if (centry <0 ) break;
cb = chchain->GetEntry(j); cbytes += cb;

if (g==_ch){
v.push_back(_ch_compression);
}

}
double sum = std::accumulate(v.begin(),v.end(),0.0)
double mean = sum/941;
avg_compvch->Fill(_ch,mean);
``````

… but the histogram doesn’t fill… why is this?
Is there something I am doing wrong, or something else I can try?

Thanks!
-lasagna

``````    double sum = std::accumulate(v.begin(), v.end(), 0.0);
v.clear();
double mean = sum / 941.;
avg_compvch->Fill(g, mean);
``````

still empty histogram

See what you get when you try:
`chchain->Draw("_ch_compression:_ch");`

`_ch vs _ch_compression` is actually a different histogram that I also make in my script, and it comes out fine.

Well, you do not show your complete source code so I can only hope that you do “`SetBranchAddress`” for both branches (i.e. “`_ch`” and “`_ch_compression`”).
If yes, I think you could simplify your code:

``````TH1D *avg_compvch = new TH1D("avg_compvch", "avg_compvch", 8257, 0, 8257);
for (Long64_t j = 0; j < 1000; j++) {