# Where to open TFile when using TTree Write?

**URL:** <https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695>\
**Category:** ROOT\
**Created:** [May 2, 2023, 2:02pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695 "2023-05-02T14:02:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![machiel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@machiel](https://root-forum.cern.ch/u/machiel)\
**Post date:** [May 2, 2023, 2:02pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/1 "2023-05-02T14:02:04Z")

</div>

ROOT Version: 6.26/10  
Platform: Ubuntu 18

I have a interactive “\*.C” macro code where I open a ROOT File and read out a TTree from that file. I also create a new TTree and some histograms. I loop over the input TTree and fill the output TTree and the histograms. At the end I want to draw the histogram on screen and save the new TTree to a new TFile. My question is where to put the “TFile aFile(“TEST.root”, “RECREATE”);” line. If I put it at the end of the code, just before “tt\_myTree-\>Write();”, I get an error: “Error in TBranch::WriteBasketImpl: basket’s WriteBuffer failed.”.  
If I put the “File” line at the beginning of the code (just before creating myTree), I cannot draw the histogram (I get an empty screen).

The complete example code is below (my real code is larger, but it comes down to this):

```auto

int Main()
{
    // Input TTree
    cout << "Give root file name" << endl;
    TString fname; cin >> fname;
    TFile* file0 = TFile::Open(fname);
    TTree* tt_PhaseSpace = (TTree*)file0->Get("PhaseSpace");
    int t_EventID;
    tt_PhaseSpace->SetBranchAddress("EventID", &t_EventID);

    int nEntries = tt_PhaseSpace->GetEntries();
    cout << "#Entries: " << nEntries << endl;
    
    // Output TTree
    //
    // TFile aFile("TEST.root", "RECREATE"); // THIS WILL MAKE h1 DISAPPEAR
    TTree* tt_myTree = new TTree("myTree", "TEST");
    int num_entries(0);
    if (tt_myTree)
        tt_myTree->Branch("num", &num_entries);
    
    int currentEventID(-1);
    TH1D* h1 = new TH1D("h1", "testh", 10000, 0., 10000);
    for (int iEntry = 0; iEntry < nEntries; iEntry++)
    {
        tt_PhaseSpace->GetEntry(iEntry);
        if (t_EventID > currentEventID)
        {
            if (currentEventID != -1) 
            {
                h1->Fill(num_entries);
                if (tt_myTree) 
                    tt_myTree->Fill();
            }
            // reset counters
            num_entries = 0;
            currentEventID = t_EventID;
        }
        num_entries++;
    }

    // Finalize
    h1->Fill(num_entries);
    if (tt_myTree)
        tt_myTree->Fill();

    cout << "h1 entries: " << h1->GetEntries() << endl;
    
    // Draw
    TCanvas* m_1 = new TCanvas("m_1", " ", 1200, 600);
    h1->Draw("");
    TCanvas* m_2 = new TCanvas("m_2", " ", 1200, 600);
    tt_myTree->Draw("num");
    
    // Save the TTree
    if (tt_myTree)
    {
        TFile aFile("TEST.root", "RECREATE"); // THIS GIVES AN ERROR 
        tt_myTree->Write();
    }
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 2, 2023, 2:18pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/2 "2023-05-02T14:18:05Z")

</div>

I guess @pcanal can help ytou.

---

<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:** [May 2, 2023, 3:32pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/3 "2023-05-02T15:32:14Z")

</div>

```cpp
    TFile aFile("TEST.root", "RECREATE");
    TTree* tt_myTree = new TTree("myTree", "TEST");
    gROOT->cd(); // newly created histograms should go here

```

---

<div class="post-metadata">

**Author:** ![machiel](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f05b48/32.png) [@machiel](https://root-forum.cern.ch/u/machiel)\
**Post date:** [May 2, 2023, 3:48pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/4 "2023-05-02T15:48:14Z")

</div>

Thank you, that works. At the end of the code before doing the Write, I will have to “cd” back to the “aFile” directory, right? Like this:

```auto
    TFile aFile("TEST.root", "RECREATE");
    TTree* tt_myTree = new TTree("myTree", "TEST");
    gROOT->cd();
    .... // do histogram stuff
    aFile.cd();
    tt_myTree->Write();

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 16, 2023, 3:48pm UTC](https://root-forum.cern.ch/t/where-to-open-tfile-when-using-ttree-write/54695/5 "2023-05-16T15:48:18Z")

</div>

Closed on May 16, 2023, 3:48pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
