Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hsimple.py File Reference

Detailed Description

View in nbviewer Open in SWAN
This program creates :

  • a one dimensional histogram
  • a two dimensional histogram
  • a profile histogram
  • a memory-resident ntuple

These objects are filled with some random numbers and saved on a file.

import numpy
from ROOT import TH1F, TH2F, TCanvas, TFile, TNtuple, TProfile, gBenchmark, gSystem
# Create a new canvas, and customize it.
c1 = TCanvas("c1", "Dynamic Filling Example", 200, 10, 700, 500)
frame = c1.GetFrame()
# Create some histograms, a profile histogram and an ntuple
hpx = TH1F("hpx", "This is the px distribution", 100, -4, 4)
hpxpy = TH2F("hpxpy", "py vs px", 40, -4, 4, 40, -4, 4)
hprof = TProfile("hprof", "Profile of pz versus px", 100, -4, 4, 0, 20)
ntuple = TNtuple("ntuple", "Demo ntuple", "px:py:pz:random:i")
# Set canvas/frame attributes.
gBenchmark.Start("hsimple")
# Fill histograms randomly.
for i in range(25000):
# Retrieve the generated values
pz = px * px + py * py
random = numpy.random.rand()
# Fill histograms.
hpxpy.Fill(px, py)
hprof.Fill(px, pz)
ntuple.Fill(px, py, pz, random, i)
# Update display every 1000 events.
if i > 0 and i % 1000 == 0:
if gSystem.ProcessEvents(): # allow user interrupt
break
gBenchmark.Show("hsimple")
# Create a new ROOT binary machine independent file.
# Note that this file may contain any kind of ROOT objects, histograms,
# pictures, graphics objects, detector geometries, tracks, events, etc.
with TFile("py-hsimple.root", "RECREATE", "Demo ROOT file with histograms") as hfile:
# Save all created objects in the file
# TNTuple is special because it is a TTree-derived class. To make sure all the
# dataset is properly written to disk, we connect it to the file and then
# we ask the ntuple to write all the information to the file itself.
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:879
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Profile Histogram.
Definition TProfile.h:32
Author
Wim Lavrijsen, Enric Tejedor

Definition in file hsimple.py.