-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrootExample.cpp
More file actions
92 lines (74 loc) · 2.52 KB
/
Copy pathrootExample.cpp
File metadata and controls
92 lines (74 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// A simple C++ program to illustrate the use of some ROOT classes
// in your code
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <TApplication.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TROOT.h>
#include <TRandom2.h>
#include <TH1F.h>
#include <TF1.h>
#include <TString.h>
#include <TAxis.h>
#include <TLine.h>
using namespace std;
void prettyPlot(TCanvas *canvas);
void rootExample();
int main(int argc, char **argv) {
// This allows you to view ROOT-based graphics in your C++ program
// If you don't want view graphics (eg just read/process/write data files),
// this line can be removed
TApplication theApp("App", &argc, argv);
// optional: here all the "root stuff" is put into its own function
// this allows us to either compile the program in C++ as a stand alone
// exe or to use the function in ROOT directly, eg. root -l rootExample.cpp
// If we build a shared library, we can call the function from Python as well
rootExample();
cout << "\nTo exit, quit ROOT from the File menu of the plot (or use control-C)" << endl;
theApp.SetIdleTimer(30,".q"); // set up a failsafe timer to end the program
theApp.Run(true);
return 0;
}
// here we create and display two histograms filled wit hrandom data
void rootExample(){
TCanvas* canvas = new TCanvas();
prettyPlot(canvas);
// Generate some data in a histogram
TRandom2 r(0);
// create a histogram and fill it w/ randomly distributed data
// based on our function
double xmin = 0.0;
double xmax = 5.0;
TH1F *hexp=new TH1F("hexp","exponential distribution;x;events",100,xmin,xmax);
TH1F *hgaus=new TH1F("hgaus","normal distribution;x;events",100,xmin,xmax);
for (int i=0; i<5000; i++){
hexp->Fill(r.Exp(3.14159));
hgaus->Fill(r.Gaus(2.5,0.5));
}
canvas->Divide(2,1);
canvas->cd(1)->SetLogy();
hexp->Draw();
canvas->cd(2);
hgaus->Draw();
}
void prettyPlot(TCanvas *canvas){
// ***************************************
// Set a bunch of parameters to make the plot look nice
canvas->SetFillColor(0);
canvas->UseCurrentStyle();
canvas->SetBorderMode(0);
canvas->SetFrameBorderMode(0);
gROOT->SetStyle("Plain");
canvas->UseCurrentStyle();
gROOT->ForceStyle();
gStyle->SetOptStat(0);
gStyle->SetTitleBorderSize(0);
gStyle->SetTitleSize(0.04);
gStyle->SetTitleFont(42, "hxy"); // for histogram and axis titles
gStyle->SetLabelFont(42, "xyz"); // for axis labels (values)
gROOT->ForceStyle();
// ***************************************
}