// Named macro to save filled objects to file // Load in ROOT: ".L make_tree.C" // Execute after loading: "make_tree()" void make_tree(){ //Input file TFile *ifile = new TFile("neppsr_basictutorial2.root"); //Tree in input file TTree *itree = (TTree *)ifile->Get("tree1"); //Create output file. If it already exists, recreate it. TFile *ofile = new TFile("outputfile.root","RECREATE"); // Tell ROOT the addresses of some of the variables that // exist in the input tree so that we may access them event-by-event Int_t event; Float_t ebeam, px,py,pz; itree->SetBranchAddress("ebeam", &ebeam); itree->SetBranchAddress("px", &px); itree->SetBranchAddress("py", &py); itree->SetBranchAddress("pz", &pz); // Define a histogram with 100 bins in the range 100.0 to 150.0 TH1F *hmom = new TH1F("hmom", "Momentum magnitude",100,100.0,150.0); // Create a tree with 2 branches (variables) TTree *newtree = new TTree("newtree","New example tree"); Float_t magmom, sqrt_ebeam; newtree->Branch("magmom",&magmom,"Momentum magnitude/F"); newtree->Branch("sqrt_ebeam", &sqrt_ebeam, "Square root of ebeam/F"); // Find out how many events are in the input tree int nentries = itree->GetEntries(); //Loop over entries in the input tree for (Int_t i = 0; i < nentries; i++) { // Get 1 entry from the tree itree->GetEntry(i); // Perform calculations using the values of the variables in the entry // we got from the tree magmom = sqrt(px*px + py*py + pz*pz); sqrt_ebeam = sqrt(ebeam); // Fill the histogram with one of the results of above calculations hmom->Fill(magmom); // Fill the new tree with results of both calculations newtree->Fill(); } // Make sure ROOT knows we want to write to the output file ofile->cd(); // Write the histogram and tree to the file hmom->Write(); newtree->Write(); // Close the output file ofile->Close(); }