More specifically, it is because those areas have content equal to zero which you are then dividing by nevts
to get nan
. Here is a revised version of the loops:
for (Int_t j = 1; j <= nx; j++) {
x = hxyz->GetXaxis()->GetBinCenter(j);
for (Int_t i = 1; i <= ny; i++) {
y = hxyz->GetYaxis()->GetBinCenter(i);
double nevts = 0;
double avgz = 0.0;
for (Int_t k = 1; k <= nz; k++) {
z = hxyz->GetZaxis()->GetBinCenter(k);
nw = hxyz->GetBinContent(i,j,k);
avgz += (z*nw);
nevts += nw;
}
if (avgz != 0) {
gxyz->SetPoint(nn++,x,y,avgz/nevts);
}
}
}
(Also, your example was missing a semicolon.)