ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
basic.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_tree
3 /// Read data from an ascii file and create a root file with an histogram and an ntuple.
4 /// See a variant of this macro in basic2.C.
5 ///
6 /// \macro_code
7 ///
8 /// \author Rene Brun
9 
10 #include "Riostream.h"
11 void basic() {
12 // read file $ROOTSYS/tutorials/tree/basic.dat
13 // this file has 3 columns of float data
14  TString dir = gSystem->UnixPathName(__FILE__);
15  dir.ReplaceAll("basic.C","");
16  dir.ReplaceAll("/./","/");
17  ifstream in;
18  in.open(Form("%sbasic.dat",dir.Data()));
19 
20  Float_t x,y,z;
21  Int_t nlines = 0;
22  auto f = TFile::Open("basic.root","RECREATE");
23  TH1F h1("h1","x distribution",100,-4,4);
24  TNtuple ntuple("ntuple","data from ascii file","x:y:z");
25 
26  while (1) {
27  in >> x >> y >> z;
28  if (!in.good()) break;
29  if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
30  h1.Fill(x);
31  ntuple.Fill(x,y,z);
32  nlines++;
33  }
34  printf(" found %d points\n",nlines);
35 
36  in.close();
37 
38  f->Write();
39 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
tuple ntuple
Definition: hsimple.py:39
float Float_t
Definition: RtypesCore.h:53
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
Basic string class.
Definition: TString.h:137
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
TFile * f
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1020
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:3851
const char * Data() const
Definition: TString.h:349
Double_t x[n]
Definition: legend1.C:17
TH1F * h1
Definition: legend1.C:5
Float_t z[5]
Definition: Ifit.C:16
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:30
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2248
char * Form(const char *fmt,...)
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17