Logo ROOT   6.10/09
Reference Guide
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 basic2.C.
6 ///
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 #include "Riostream.h"
12 void basic() {
13 // read file $ROOTSYS/tutorials/tree/basic.dat
14 // this file has 3 columns of float data
15  TString dir = gROOT->GetTutorialDir();
16  dir.Append("/tree/");
17  dir.ReplaceAll("/./","/");
18  ifstream in;
19  in.open(Form("%sbasic.dat",dir.Data()));
20 
21  Float_t x,y,z;
22  Int_t nlines = 0;
23  auto f = TFile::Open("basic.root","RECREATE");
24  TH1F h1("h1","x distribution",100,-4,4);
25  TNtuple ntuple("ntuple","data from ascii file","x:y:z");
26 
27  while (1) {
28  in >> x >> y >> z;
29  if (!in.good()) break;
30  if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
31  h1.Fill(x);
32  ntuple.Fill(x,y,z);
33  nlines++;
34  }
35  printf(" found %d points\n",nlines);
36 
37  in.close();
38 
39  f->Write();
40 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
float Float_t
Definition: RtypesCore.h:53
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
#define gROOT
Definition: TROOT.h:375
Basic string class.
Definition: TString.h:129
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:551
int Int_t
Definition: RtypesCore.h:41
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3909
Double_t x[n]
Definition: legend1.C:17
TString & Append(const char *cs)
Definition: TString.h:497
TH1F * h1
Definition: legend1.C:5
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
char * Form(const char *fmt,...)
double f(double x)
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
const char * Data() const
Definition: TString.h:347