Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree101_basic.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3/// \notebook -nodraw
4/// Read data from an ascii file and create a root file with an histogram and an ntuple.
5/// See a variant of this macro in tree102_basic.C.
6///
7/// \macro_code
8///
9/// \author Rene Brun
10
11#include "Riostream.h"
12
13void tree101_basic()
14{
15 // read file $ROOTSYS/tutorials/io/tree/basic.dat
16 // this file has 3 columns of float data
17 TString dir = gROOT->GetTutorialDir();
18 dir.Append("/io/tree/");
19 dir.ReplaceAll("/./", "/");
20 ifstream in;
21 in.open(TString::Format("%sbasic.dat", dir.Data()));
22
23 Float_t x, y, z;
24 Int_t nlines = 0;
25 auto f = TFile::Open("tree101.root", "RECREATE");
26 TH1F h1("h1", "x distribution", 100, -4, 4);
27 TNtuple ntuple("ntuple","data from ascii file", "x:y:z");
28
29 while (1) {
30 in >> x >> y >> z;
31 if (!in.good())
32 break;
33 if (nlines < 5)
34 printf("x = %+8.6f, y = %+8.6f, z = %+8.6f\n", x, y, z);
35 h1.Fill(x);
36 ntuple.Fill(x, y, z);
37 nlines++;
38 }
39 printf(" found %d points\n", nlines);
40 in.close();
41 f->Write();
42}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:414
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4131
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3316
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TH1F * h1
Definition legend1.C:5