# .root file is empty

**URL:** <https://root-forum.cern.ch/t/root-file-is-empty/50971>\
**Category:** Newbie\
**Created:** [July 26, 2022, 2:46pm UTC](https://root-forum.cern.ch/t/root-file-is-empty/50971 "2022-07-26T14:46:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [July 26, 2022, 2:46pm UTC](https://root-forum.cern.ch/t/root-file-is-empty/50971/1 "2022-07-26T14:46:45Z")

</div>

Hello rooters,  
This maybe a simple question.But I cannot resolve it.  
I tried to read a existing .root file and read a histogram. Okay.It’s working  
Then I wanna create a new .root file which should have a new histogram.But my new .root file is empty.

I tried to shorten my code as much as possible.

```auto
#include <algorithm>
#include <TRandom3.h>
#include <TF2.h>
#include <TH2D.h>
#include <TMath.h>
#include <TFile.h>
#include <TCanvas.h>

void Fitting(){
    
    TFile *AnaFile = new TFile("fit.root", "RECREATE");
    
    TFile *hFile=TFile::Open("xBQ2binning_data.root");
    TH1F *h_pion_invariant_mass_Stat_Syst;
    
    hFile->GetObject("h_pion_invariant_mass_Stat_Syst", h_pion_invariant_mass_Stat_Syst);
    
    
    auto* h_QSq_xBj_log_bin_data=new TH2F("h_QSq_xBj_log_bin_data", "QSq vs xBj log binning cross section", 50,0.0,1.0,100,0.0,10.0);
    
    h_QSq_xBj_log_bin_data->Fill(1,1);
    
    AnaFile->Write();
    AnaFile->Close();
}

```

I expect `h_QSq_xBj_log_bin_data` histogram in the new fit.root file.But fit.root is empty.Not sure why.  
Could you please help me?

Attached the .root file that reading the histogram.  
[xBQ2binning\_data.root](https://root-forum.cern.ch/uploads/short-url/cyUyB3xRgP9ONJxwUs7NM2FEcYN.root) (1.2 MB)

---

<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 26, 2022, 3:11pm UTC](https://root-forum.cern.ch/t/root-file-is-empty/50971/2 "2022-07-26T15:11:45Z")

</div>

Before the line with “`new TH2F`”, add:  
`AnaFile->cd(); // newly created histograms should go here`

BTW. Instead of “`AnaFile->Close();`”, use: `delete AnaFile;`

---

<div class="post-metadata">

**Author:** ![Dil](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dil/32/26887_2.png) [@Dil](https://root-forum.cern.ch/u/Dil)\
**Post date:** [July 26, 2022, 3:24pm UTC](https://root-forum.cern.ch/t/root-file-is-empty/50971/3 "2022-07-26T15:24:09Z")

</div>

Thanks @Wile_E_Coyote .It worked.
