First Steps: Histograms


Creating, updating and displaying histograms in real time. ROOT's random number facility.

The Lesson

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 <math.h> // 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


Go Back to the The First Steps Top Page

If you have any comments about this page please send them to Nick West