# TH3::FitSlicesZ behaving differently in macro (.C) as in ROOT console

**URL:** <https://root-forum.cern.ch/t/th3-fitslicesz-behaving-differently-in-macro-c-as-in-root-console/35710>\
**Category:** Newbie\
**Created:** [August 20, 2019, 1:53pm UTC](https://root-forum.cern.ch/t/th3-fitslicesz-behaving-differently-in-macro-c-as-in-root-console/35710 "2019-08-20T13:53:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitlabence](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/958977/32.png) [@mitlabence](https://root-forum.cern.ch/u/mitlabence)\
**Post date:** [August 20, 2019, 1:53pm UTC](https://root-forum.cern.ch/t/th3-fitslicesz-behaving-differently-in-macro-c-as-in-root-console/35710/1 "2019-08-20T13:53:23Z")

</div>

Dear Root users,  
I need to use _[FitSlicesZ](https://root.cern.ch/doc/v608/classTH3.html#a927fb729e31d90ecbcc2b5e4f9e23900)()_, and created an example macro to see if it is working:

```auto
TF3 *gaus3d = new TF3("gaus3d", "[0]*TMath::Gaus(x, [1], [2])*TMath::Gaus(y, [3], [4])*TMath::Gaus(z, [5], [6])", -5, 5, -5, 5, -5, 5);
gaus3d->SetParameters(1, 0, 1, 0, 1, 0, 1);
TH3 *h3 = new TH3D("h3", "title", 50, -5, 5, 50, -5, 5, 50, -5, 5);
h3->FillRandom("gaus3d", 10000);
TFile rootfile("hisTest.root", "RECREATE");
rootfile.cd();
h3->Write();
h3->FitSlicesZ();

```

This runs as expected in the Root console. Creates _hisTest.root_ file, with the _TH2D_ histograms _h3\_0, h3\_1, h3\_2, h3\_chi2,_ and also the original _TH3D h3_ in it.

However, running the exact same code (even after deleting the created root file) as a macro (named _testHist.C_, for example), i.e. inside a void function as follows:

```auto
void testHist(){
//previous code here
}

```

then

```auto
.x testHist.C

```

results in a _.root_ file that has _h3_ in it, nothing else.  
How can I achieve the same result with the macro file (creation of a _.root_ file with the _TH2_ plots also in it)?  
I thought the function’s [source code](https://root.cern.ch/doc/v608/TH3_8cxx_source.html#l00864) might help, but I have the suspicion I won’t find the reason for this behaviour in it.
