Skip to content

Commit

Permalink
Modernize and adapt the code to the new names
Browse files Browse the repository at this point in the history
  • Loading branch information
bellenot committed Nov 28, 2024
1 parent e461cec commit 61484d9
Show file tree
Hide file tree
Showing 10 changed files with 586 additions and 548 deletions.
34 changes: 18 additions & 16 deletions tutorials/tree/tree101_basic.C
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@
/// \ingroup tutorial_tree
/// \notebook -nodraw
/// Read data from an ascii file and create a root file with an histogram and an ntuple.
/// See a variant of this macro in basic2.C.
/// See a variant of this macro in tree102_basic.C.
///
/// \macro_code
///
/// \author Rene Brun

#include "Riostream.h"
void basic() {
// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data

void tree101_basic()
{
// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gROOT->GetTutorialDir();
dir.Append("/tree/");
dir.ReplaceAll("/./","/");
dir.ReplaceAll("/./", "/");
ifstream in;
in.open(Form("%sbasic.dat",dir.Data()));
in.open(TString::Format("%sbasic.dat", dir.Data()));

Float_t x,y,z;
Float_t x, y, z;
Int_t nlines = 0;
auto f = TFile::Open("basic.root","RECREATE");
TH1F h1("h1","x distribution",100,-4,4);
TNtuple ntuple("ntuple","data from ascii file","x:y:z");
auto f = TFile::Open("tree101.root", "RECREATE");
TH1F h1("h1", "x distribution", 100, -4, 4);
TNtuple ntuple("ntuple","data from ascii file", "x:y:z");

while (1) {
in >> x >> y >> z;
if (!in.good()) break;
if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
if (!in.good())
break;
if (nlines < 5)
printf("x = %+8.6f, y = %+8.6f, z = %+8.6f\n", x, y, z);
h1.Fill(x);
ntuple.Fill(x,y,z);
ntuple.Fill(x, y, z);
nlines++;
}
printf(" found %d points\n",nlines);

printf(" found %d points\n", nlines);
in.close();

f->Write();
}
19 changes: 10 additions & 9 deletions tutorials/tree/tree102_basic.C
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
/// \ingroup tutorial_tree
/// \notebook -js
/// Create can ntuple reading data from an ascii file.
/// This macro is a variant of basic.C
/// This macro is a variant of tree101_basic.C
///
/// \macro_image
/// \macro_code
///
/// \author Rene Brun

void basic2() {
void tree102_basic()
{
TString dir = gROOT->GetTutorialDir();
dir.Append("/tree/");
dir.ReplaceAll("/./","/");
dir.ReplaceAll("/./", "/");

TFile *f = new TFile("basic2.root","RECREATE");
TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
TTree *T = new TTree("ntuple","data from ascii file");
Long64_t nlines = T->ReadFile(Form("%sbasic.dat",dir.Data()),"x:y:z");
printf(" found %lld points\n",nlines);
T->Draw("x","z>2");
auto f = TFile::Open("tree102.root", "RECREATE");
auto h1 = new TH1F("h1", "x distribution", 100, -4, 4);
auto T = new TTree("ntuple", "data from ascii file");
Long64_t nlines = T->ReadFile(TString::Format("%sbasic.dat", dir.Data()), "x:y:z");
printf(" found %lld points\n", nlines);
T->Draw("x", "z>2");
T->Write();
}
113 changes: 57 additions & 56 deletions tutorials/tree/tree103_tree.C
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/// \notebook -nodraw
/// Simple Event class example
///
/// execute as: .x tree0.C++
/// execute as: .x tree103_tree.C++
///
/// You have to copy it first to a directory where you have write access!
/// Note that .x tree0.C cannot work with this example
/// Note that .x tree103_tree.C cannot work with this example
///
/// ### Effect of ClassDef() and ClassImp() macros
///
Expand Down Expand Up @@ -46,74 +46,75 @@

#include <Riostream.h>

//class Det : public TObject {
//class Det : public TObject {
class Det { // each detector gives an energy and time signal
public:
Double_t e; //energy
Double_t t; //time
Double_t e; //energy
Double_t t; //time

// ClassDef(Det,1)
// ClassDef(Det,1)
};

//ClassImp(Det)

//class Event { //TObject is not required by this example
class Event : public TObject {
public:
Det a; // say there are two detectors (a and b) in the experiment
Det b;

Det a; // say there are two detectors (a and b) in the experiment
Det b;
ClassDefOverride(Event,1)
ClassDefOverride(Event,1)
};

ClassImp(Event)

void tree0() {
// create a TTree
TTree *tree = new TTree("tree","treelibrated tree");
Event *e = new Event;

// create a branch with energy
tree->Branch("event",&e);

// fill some events with random numbers
Int_t nevent=10000;
for (Int_t iev=0;iev<nevent;iev++) {
if (iev%1000==0) cout<<"Processing event "<<iev<<"..."<<endl;

Float_t ea,eb;
gRandom->Rannor(ea,eb); // the two energies follow a gaus distribution
e->a.e=ea;
e->b.e=eb;
e->a.t=gRandom->Rndm(); // random
e->b.t=e->a.t + gRandom->Gaus(0.,.1); // identical to a.t but a gaussian
// 'resolution' was added with sigma .1

tree->Fill(); // fill the tree with the current event
}

// start the viewer
// here you can investigate the structure of your Event class
tree->StartViewer();

//gROOT->SetStyle("Plain"); // uncomment to set a different style

// now draw some tree variables
TCanvas *c1 = new TCanvas();
c1->Divide(2,2);
c1->cd(1);
tree->Draw("a.e"); //energy of det a
tree->Draw("a.e","3*(-.2<b.e && b.e<.2)","same"); // same but with condition on energy b; scaled by 3
c1->cd(2);
tree->Draw("b.e:a.e","","colz"); // one energy against the other
c1->cd(3);
tree->Draw("b.t","","e"); // time of b with errorbars
tree->Draw("a.t","","same"); // overlay time of detector a
c1->cd(4);
tree->Draw("b.t:a.t"); // plot time b again time a

cout<<endl;
cout<<"You can now examine the structure of your tree in the TreeViewer"<<endl;
cout<<endl;
void tree103_tree()
{
// create a TTree
auto tree = new TTree("tree", "treelibrated tree");
auto e = new Event;

// create a branch with energy
tree->Branch("event", &e);

// fill some events with random numbers
Int_t nevent = 10000;
for (Int_t iev=0; iev<nevent; iev++) {
if (iev % 1000 == 0)
std::cout << "Processing event " << iev << "..." << std::endl;

Float_t ea, eb;
gRandom->Rannor(ea, eb); // the two energies follow a gaus distribution
e->a.e = ea;
e->b.e = eb;
e->a.t = gRandom->Rndm(); // random
e->b.t = e->a.t + gRandom->Gaus(0., .1); // identical to a.t but a gaussian
// 'resolution' was added with sigma .1
tree->Fill(); // fill the tree with the current event
}

// start the viewer
// here you can investigate the structure of your Event class
tree->StartViewer();

//gROOT->SetStyle("Plain"); // uncomment to set a different style

// now draw some tree variables
auto c1 = new TCanvas();
c1->Divide(2, 2);
c1->cd(1);
tree->Draw("a.e"); //energy of det a
tree->Draw("a.e", "3*(-.2<b.e && b.e<.2)", "same"); // same but with condition on energy b; scaled by 3
c1->cd(2);
tree->Draw("b.e:a.e", "", "colz"); // one energy against the other
c1->cd(3);
tree->Draw("b.t", "", "e"); // time of b with errorbars
tree->Draw("a.t", "", "same"); // overlay time of detector a
c1->cd(4);
tree->Draw("b.t:a.t"); // plot time b again time a

std::cout << std::endl;
std::cout << "You can now examine the structure of your tree in the TreeViewer" << std::endl;
std::cout << std::endl;
}

121 changes: 61 additions & 60 deletions tutorials/tree/tree104_tree.C
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
/// This example can be run in many different ways:
/// 1. Using the Cling interpreter
/// ~~~
/// .x tree1.C
/// .x tree104_tree.C
/// ~~~
/// 2. Using the automatic compiler interface
/// ~~~
/// .x tree1.C++
/// .x tree104_tree.C++
/// ~~~
/// 3.
/// ~~~
/// .L tree1.C or .L tree1.C++
/// .L tree104_tree.C or .L tree104_tree.C++
/// tree1()
/// ~~~
/// One can also run the write and read parts in two separate sessions.
/// For example following one of the sessions above, one can start the session:
/// ~~~
/// .L tree1.C
/// tree1r();
/// .L tree104_tree.C
/// read_tree();
/// ~~~
/// \macro_code
///
Expand All @@ -37,79 +37,80 @@
#include "TH2.h"
#include "TRandom.h"

void tree1w()
void tree104_write()
{
//create a Tree file tree1.root
// create a Tree file tree104.root

//create the file, the Tree and a few branches
TFile f("tree1.root","recreate");
TTree t1("t1","a simple Tree with simple variables");
// create the file, the Tree and a few branches
TFile f("tree104.root", "recreate");
TTree t1("t1", "a simple Tree with simple variables");
Float_t px, py, pz;
Double_t random;
Int_t ev;
t1.Branch("px",&px,"px/F");
t1.Branch("py",&py,"py/F");
t1.Branch("pz",&pz,"pz/F");
t1.Branch("random",&random,"random/D");
t1.Branch("ev",&ev,"ev/I");
t1.Branch("px", &px, "px/F");
t1.Branch("py", &py, "py/F");
t1.Branch("pz", &pz, "pz/F");
t1.Branch("random", &random, "random/D");
t1.Branch("ev", &ev, "ev/I");

//fill the tree
for (Int_t i=0;i<10000;i++) {
gRandom->Rannor(px,py);
pz = px*px + py*py;
random = gRandom->Rndm();
ev = i;
t1.Fill();
}

//save the Tree header. The file will be automatically closed
//when going out of the function scope
t1.Write();
// fill the tree
for (Int_t i=0; i<10000; i++) {
gRandom->Rannor(px, py);
pz = px * px + py * py;
random = gRandom->Rndm();
ev = i;
t1.Fill();
}
// save the Tree header. The file will be automatically closed
// when going out of the function scope
t1.Write();
}

void tree1r()
void tree104_read()
{
//read the Tree generated by tree1w and fill two histograms
// read the Tree generated by tree1w and fill two histograms

//note that we use "new" to create the TFile and TTree objects !
//because we want to keep these objects alive when we leave this function.
TFile *f = new TFile("tree1.root");
TTree *t1 = (TTree*)f->Get("t1");
Float_t px, py, pz;
Double_t random;
Int_t ev;
t1->SetBranchAddress("px",&px);
t1->SetBranchAddress("py",&py);
t1->SetBranchAddress("pz",&pz);
t1->SetBranchAddress("random",&random);
t1->SetBranchAddress("ev",&ev);
// note that we create the TFile and TTree objects on the heap!
// because we want to keep these objects alive when we leave this function.
auto f = TFile::Open("tree104.root");
auto t1 = f->Get<TTree>("t1");
t1->SetBranchAddress("px", &px);
t1->SetBranchAddress("py", &py);
t1->SetBranchAddress("pz", &pz);
t1->SetBranchAddress("random", &random);
t1->SetBranchAddress("ev", &ev);

//create two histograms
TH1F *hpx = new TH1F("hpx","px distribution",100,-3,3);
TH2F *hpxpy = new TH2F("hpxpy","py vs px",30,-3,3,30,-3,3);
// create two histograms
auto hpx = new TH1F("hpx", "px distribution", 100, -3, 3);
auto hpxpy = new TH2F("hpxpy", "py vs px", 30, -3, 3, 30, -3, 3);

//read all entries and fill the histograms
// read all entries and fill the histograms
Long64_t nentries = t1->GetEntries();
for (Long64_t i=0;i<nentries;i++) {
t1->GetEntry(i);
hpx->Fill(px);
hpxpy->Fill(px,py);
}
for (Long64_t i=0; i<nentries; i++) {
t1->GetEntry(i);
hpx->Fill(px);
hpxpy->Fill(px, py);
}

//we do not close the file. We want to keep the generated histograms
//we open a browser and the TreeViewer
if (gROOT->IsBatch()) return;
new TBrowser();
t1->StartViewer();
// in the browser, click on "ROOT Files", then on "tree1.root".
// you can click on the histogram icons in the right panel to draw them.
// in the TreeViewer, follow the instructions in the Help button.
// we do not close the file. We want to keep the generated histograms
// we open a browser and the TreeViewer
if (gROOT->IsBatch())
return;
new TBrowser();
t1->StartViewer();
// in the browser, click on "ROOT Files", then on "tree1.root".
// you can click on the histogram icons in the right panel to draw them.
// in the TreeViewer, follow the instructions in the Help button.

// Allow to use the TTree after the end of the function.
t1->ResetBranchAddresses();
// Allow to use the TTree after the end of the function.
t1->ResetBranchAddresses();
}

void tree1() {
tree1w();
tree1r();
void tree104_tree()
{
tree104_write();
tree104_read();
}
Loading

0 comments on commit 61484d9

Please sign in to comment.