Filling TCanvas by arrays and graphing functions

Hello,

I’m working on a program in C++ that should graph and save px, py, pz from a tree of generated events. I want to fill TCanvas using arrays and then graph. I’m not sure exactly how to do this, can anybody offer some advice? I’ve copied the code I have so far below. (Please forgive any incorrect terminology or errors- I am a very new beginner!)

#include “TChain.h”
#include “TCanvas.h”

void TChainPractice(){
//creates a chain with tree "T"
TChain chain(“h16”);
//Adds all the files needed
chain.Add(“kstar*.root”);

 //Now to get the events:                                                   
 chain.SetBranchAddress("idhep==211"); //to recognize the mass of pi        
 chain.SetBranchAddress("px");
 chain.SetBranchAddress("py");
 chain.SetBranchAddress("pz");

 Int_t nevent = chain.GetEntries();
 for(Int_t i =0;i<nevent;i++){
   if(idhep==211){
   chain.GetEvent(i);
   abs(idhep)==211
     }
   //Still need to fill TCanvas...                                          
   // ...                                                                   

   h16->Draw("px", "abs(idhep)==211");
   c1->SaveAs("px.png");

   h16->Draw("py," "abs(idehep)==211");
   c1->SaveAs("py.png");

   h16->Draw("pz", "abso(idhep)==211");
   c1->SaveAs(("pz.png");

You macro draws the 3 variables px py and pz and you generates the corresponding png image for each of them.
I looks fine.

you said

I am not sure to understand what you mean by “filling a canvas”… you can Draw on a canvas and that’s what you are doing … But what does mean “filling” it … ?

Try: { TCanvas *c = new TCanvas("c", "c"); TChain ch("h16"); ch.Add("kstar*.root"); ch.Draw("px", "abs(idhep)==211"); c->SaveAs("px.png"); ch.Draw("py", "abs(idhep)==211"); c->SaveAs("py.png"); ch.Draw("pz", "abs(idhep)==211"); c->SaveAs("pz.png"); delete c; }

I believe I meant drawing as opposed to “filling” the canvas. I’m in the very beginning process of learning ROOT and am still getting used to terminology. Thank you for your response!