TFile* file = new TFile("output.root", "RECREATE"); file->WriteTObject(hist, "hist"); file->Close();
TFile* file = new TFile("output.root", "RECREATE"); TTree* tree = new TTree("tree", "example tree"); double x, y, z; tree->Branch("x", &x, "x/D"); tree->Branch("y", &y, "y/D"); tree->Branch("z", &z, "z/D"); for (int i=0; i<100; i++) { x = i; y = sin(i); z = cos(i); tree->Fill(); } file->WriteTObject(tree, "tree"); file->Close();In this example, we create a new TTree object named "tree" and add three branches with doubles named "x", "y", and "z". We then fill the tree with 100 entries in a for loop. Finally, we call the WriteTObject method on the "tree" object, and give it the name "tree" in the file. We close the file once done. Based on the method name and arguments, TFile WriteTObject is part of the ROOT package library.