// A stand-alone C++ program to illustrate the use of ROOT classes // for booking, filling and storing histograms. The program // displays one of the histograms. All histograms are stored in // a file test.root, which can be read in a further analyzed using // the program root. A minimal interactive root session might be: // > root // root [0] TFile f("test.root"); // read in the file // root [1] f->ls(); // list the histograms // root [2] h1->Draw(); // draw histogram h1 // root [3] h2->Draw(); // draw histogram h2 // For more information see the root home page: root.cern.ch // Glen Cowan // RHUL Physics // 22 November 2004 #include #include #include "TApplication.h" #include "TCanvas.h" #include "TFile.h" #include "TH1F.h" using namespace std; int main(int argc, char **argv) { TApplication* theApp = new TApplication("App", &argc, argv); // Set up output file, book histograms, add to list of histograms. TFile* outfile = new TFile("test.root", "RECREATE"); TList* hList = new TList(); // list of histograms to store TH1F* h1 = new TH1F("h1", "uniform r", 100, 0.0, 1.0); hList->Add(h1); TH1F* h2 = new TH1F("h2", "r1 + r2 - 1", 100, -1.0, 1.0); hList->Add(h2); // Fill with some random numbers. Uses cstdlib's rand(). for (int i = 0; i<10000; ++i){ float r1 = static_cast(rand()) / static_cast(RAND_MAX); float r2 = static_cast(rand()) / static_cast(RAND_MAX); h1->Fill(r1); h2->Fill(r1+r2-1.0); } // Display a histogram. (Alternatively, look at them with the program root.) TCanvas* c = new TCanvas("c", "c", 800, 600); h1->Draw(); // Save all histograms in the list to the output file. hList->Write(); // Run the TApplication (not needed if you only want to store the histograms.) theApp->Run(); // Close up outfile->Close(); return 0; }