Project

General

Profile

Actions

PyROOTPlottingExample

//*This set of commands can be inputted line-by-line in the ipython interactive mode by typing "ipython" into the command line. Alternatively, you may write a file that does all these steps and run it using "ipython yourfilename.py"*//

import ROOT    
import json

//read simulation output file
f = open('simulation.out', 'r')     

//The first several entries in the json file might be empty, so the command "spill" may have to be iterated several times until the desired json entry is found.
spill = f.readline() 

//The following deserializes "spill" to a python object:
spill_dict = json.loads(spill)    

//Now you're ready to plot.
//The python commands to create a ROOT canvas and histogram are as follows:
h1 = ROOT.TH1F("h1","",100, 0, 200)
c1 = ROOT.TCanvas()

//Now you can do something like fill, draw, and print a histogram (note the index 'i' precedes the data with which you are filling your histogram):
for i in range(len(spill_dict['mc'])):
    h1.FIll(spill_dict['mc'][i]['charge'])
h1.Draw()
c1.Print('charge.eps')

Updated by Tunnell, Christopher almost 10 years ago ยท 2 revisions