#include#include // create a TTree and set branch variables void saveData(double x, double y, double z) { TFile * file = new TFile("data.root", "RECREATE"); TTree * tree = new TTree("tree", "tree"); tree->Branch("x", &x); tree->Branch("y", &y); tree->Branch("z", &z); // fill the TTree with some data for (int i = 0; i < 10; i++) { x = i * 1.0; y = i * 2.0; z = i * 3.0; tree->Fill(); } // write the TTree to file tree->Write(); file->Close(); }
#includeIn these examples, we create a TTree with three branch variables (x, y, z) and fill it with data. We then write the TTree to file. In the second example, we read the data from the TTree and print it out. These examples demonstrate the basics of using TTree for storing and analyzing data.#include // read data from a TTree void readData() { TFile * file = new TFile("data.root"); TTree * tree = (TTree*)file->Get("tree"); double x, y, z; tree->SetBranchAddress("x", &x); tree->SetBranchAddress("y", &y); tree->SetBranchAddress("z", &z); for (int i = 0; i < tree->GetEntries(); i++) { tree->GetEntry(i); printf("x = %f, y = %f, z = %f\n", x, y, z); } file->Close(); }