In [ ]:
%load_ext ROOTmagic
T'TH1DTRandomTFilenew) TClass)#includeTObjectROOT Standards differ from the standard C++ library
Functions Start with capital letter, use capitals to separate words
file->FindObject("hist");Good: obj->Write() Bad : obj->write()Good: array->GetSize();Bad : array->Size();Good: hist->GetXaxis() Bad : hist->GetXAxis()
In [11]:
%%ROOT
#include <TFile.h>
#include <TTree.h>
#include <TRandom.h>
#include <TH1D.h>
#include <iostream>
int main()
{
TFile *file = new TFile("rangen.root", "RECREATE");
TRandom *rnd = new TRandom();
TH1D *hist = new TH1D("gaus", "Gaussian Distribution", 1000, -5, 5);
for (int i = 0; i < 70000; i++) {
double value = rnd->Gaus();
hist->Fill(value);
}
file->Write();
std::cout << "Done. Hurray!!";
return 0;
}
ROOT's implementation of string (NOT compatible with std::string!).
TObject. You cannot store in a TObjArray. :-(TObjStringTString str("This is my string. It's just a bunch of chars");
// NO! std::cout << str << '\n'
std:cout << str.Data() << '\n'; // str.Data() returns
// char* to internal
// character array
TObjArray *words = str.Tokenize(" "); // Break string into
// tokens (around spaces)
for (int i = 0; i < words->GetEntries(); i++) {
TString *word = words[i]->At(i);
cout << "Next word is '" << word->Data() << "'\n";
}
In [43]:
%%ROOT
#include <TString.h>
#include <TObjArray.h>
#include <TObjString.h>
#include <iostream>
using namespace std;
int main(){
TString str("This is my string. It's just a bunch of chars");
// NO! std::cout << str << '\n'
std:cout << str.Data() << '\n';
TObjArray *words = str.Tokenize(" "); // Break string into tokens (around spaces)
std::cout << "\nFound : " << words->GetEntries() << " words \n\n";
for (int i = 0; i < words->GetEntries(); i++) {
TString word = ((TObjString*)words->At(i))->GetString();
cout << "Word " << i << " is '" << word.Data() << "'\n";
}
return 0;
}
Idea: Find π using random numbers
RxR square.TRandom3 rng(); // Create Random Number Generator
int inside_circle = 0, total = 100; // total number and
// number inside circle
for (int i = 0; i < total; i++) {
double x = rng.Uniform(-1., 1.), // Get random x,y pair
y = rng.Uniform(-1., 1.); // from uniform
// distribution between
// -1,1
if (x*x + y*y < 1.0) {
inside_circle++; // If x and y are within circle,
// increment 'inside_circle'
}
}
double calc_pi = inside_circle/total * 4; // calculate pi
cout << "Calculated π to be : " << calc_pi << "\n";
In [ ]:
%%ROOT
#include <iostream>
#include <TRandom3.h>
using namespace std;
int main()
{
TRandom3 rng; // Create Random Number Generator
rng.SetSeed(); // Set a random seed
int inside_circle = 0, total = 100; // total number and number inside circle
for (int i = 0; i < total; i++) {
double x = rng.Uniform(-1., 1.), // Get random x,y pair from uniform
y = rng.Uniform(-1., 1); // distribution between -1,1
if (x*x + y*y < 1) {
inside_circle++;
}
}
float calc_pi = 4.0 * inside_circle/total;
w
cout << "Calculated π to be : " << std::fixed << calc_pi << "\n";
cout << "M_PI " << M_PI << "\n";
}
class PiCalculator {
public:
PiCalculator ():_rnd(new TRandom3()) {_rnd->SetSeed();};
PiCalculator (TRandom3* generator):_rnd(generator){};
double GetPi(int repeats) {
int numerator = 0, denominator = repeats;
while (repeats--) {
double x = rng.Uniform(-1., 1.),
y = rng.Uniform(-1., 1);
if (x*x + y*y < 1.0) {
numerator++;
}
}
return 4.0 * numerator / denominator;
}
protected:
TRandom3* _rnd;
};
int main()
{
PiCalculator calc;
std::cout << "pi " << calc.GetPi(100) << "\n";
return 0;
}
In [72]:
%%ROOT
#include <iostream>
#include <TRandom3.h>
class PiCalculator {
public:
PiCalculator ():_rnd(new TRandom3()) {_rnd->SetSeed();};
PiCalculator (TRandom3* generator):_rnd(generator){};
double GetPi(int repeats) {
int numerator = 0, denominator = repeats;
while (repeats--) {
double x = _rnd->Uniform(-1., 1.),
y = _rnd->Uniform(-1., 1);
if (x*x + y*y < 1.0) {
numerator++;
}
}
return 4.0 * numerator / denominator;
}
protected:
TRandom3* _rnd;
};
int main()
{
PiCalculator calc;
std::cout << "pi " << calc.GetPi(1000) << "\n";
return 0;
}
In [ ]:
%%ROOT
#include <iostream>
#include <TRandom3.h>
#include <TFile.h>
#include <TH2D.h>
class PiCalculator {
public:
PiCalculator ():_rnd(new TRandom3()) {_rnd->SetSeed();};
PiCalculator (TRandom3* generator):_rnd(generator){};
double GetPi(int repeats) {
int numerator = 0, denominator = repeats;
while (repeats--) {
double x = _rnd->Uniform(-1., 1.),
y = _rnd->Uniform(-1., 1);
if (x*x + y*y < 1.0) {
numerator++;
}
}
return 4.0 * numerator / denominator;
}
protected:
TRandom3* _rnd;
};
int main()
{
const int REPETITIONS = 500,
MIN = 100,
MAX = 50000;
TFile *file = new TFile("MCPi.root", "RECREATE");
TH2D *pihist = new TH2D("PiHist", "Repeats vs Pi Value",
100, MIN, MAX, // Number of repetitions
100, 3, 3.3); // Pi values
PiCalculator calc;
for (int num = MIN; num < MAX; num += 10000) {
double avg_pi = 0.0;
int rep = REPETITIONS;
while (rep--) {
double pi = calc.GetPi(num);
pihist->Fill(num, pi);
avg_pi += pi;
}
std::cout << num << " : " << avg_pi/REPETITIONS << "\n";
}
file->Write();
std::cout << "Done.\n";
return 0;
}