First Steps: Histograms
Creating, updating and displaying histograms in real time.
ROOT's random number facility.
Start by reading about
TH2F
in the ROOT Crib which describes the 2d histogram of floating point numbers.
Now we will put it into practice. You may find it
helpful to open a second browser window looking at the TH2F
documentation as we proceed.
Create the file:-
hist.cxx
containing:-
{
#include
// Reset the stack.
gROOT->Reset();
// Create a TCanvas.
TCanvas* my_canvas = new TCanvas("my_canvas","Histogram Example",200,10,600,400);
// Create a 1D histogram, set its fill colour, and draw it on the current TPad
TH1F *hist = new TH1F("hist","Exponential distribution",100,0.,5.);
hist->SetFillColor(3);
hist->Draw();
// Prepare the random number generator.
gRandom->SetSeed();
// Loop generating data according to an exponetial.
Float_t data;
Int_t dummy;
for ( Int_t i=0; i<10000; i++) {
data = - log( gRandom->Rndm(dummy) );
hist->Fill(data);
if ( i%100 == 0 ) {
my_canvas->Modified();
my_canvas->Update();
}
}
}
Cut and paste if you must! You should be able to understand most of this
macro. gRandom is a
global
pointer to a TRandom object that produces various random numbers. You
can look it up in the ROOT class library:-
TRandom
You can find further information in
How to Use the histogram classes . For example, changing the line:-
hist->Draw();
to:-
hist->Draw("LEGO");
produces a lego plot. Once it has been drawn you can rotate it by
dragging the mouse inside the plot.
For another simple example of histogram use (on which our simple example was based)
see the ROOT tutorial
Filling several histograms and some graphics
options
- In ROOT the histogram concept
encapsulates
both the concept of a histogram as an abstract data representation and its
graphical presentation, something that does not please some purists!
However, for beginners, wanting to collect and display data in histogram
form, its good as all the facilities are collected together in a single
inheritance tree of classes.
- ROOT has a variety of 1D, 2D and 3D histogram classes. Facilities exist to
collect, combine and display them
in a variety of formats For a good overview see
How to Use the histogram classes .
- As with all graphical objects, the
TPad
that displays them does so by holding a link to the real histogram
object. There are two important consequences:-
- Updates to the histogram will be reflected on screen (although you
have to send the TPad its Modified() and Update() messages to do this).
- If you modify the histogram interactively (not covered in these
lessons) then you are really changing the underlying histogram object.
In those cases where you need to break the link between the TPad and
the underlying object it is possible to clone (copy) the object and display
the clone.
Go Back to the
The First Steps Top Page
If you have any comments about this page please send them to
Nick West