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):
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;
}