Logo ROOT  
Reference Guide
basic.C File Reference

Detailed Description

View in nbviewer Open in SWAN 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.

#include "Riostream.h"
void 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("/./","/");
ifstream in;
in.open(Form("%sbasic.dat",dir.Data()));
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");
while (1) {
in >> x >> y >> z;
if (!in.good()) break;
if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
h1.Fill(x);
ntuple.Fill(x,y,z);
nlines++;
}
printf(" found %d points\n",nlines);
in.close();
f->Write();
}
Author
Rene Brun

Definition in file basic.C.

f
#define f(i)
Definition: RSha256.hxx:122
TNtuple
Definition: TNtuple.h:28
TString::Data
const char * Data() const
Definition: TString.h:369
Form
char * Form(const char *fmt,...)
Float_t
float Float_t
Definition: RtypesCore.h:57
TFile::Open
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:3946
Int_t
int Int_t
Definition: RtypesCore.h:45
x
Double_t x[n]
Definition: legend1.C:17
TString
Definition: TString.h:136
h1
TH1F * h1
Definition: legend1.C:5
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
y
Double_t y[n]
Definition: legend1.C:17
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
Riostream.h
TNtuple::Fill
virtual Int_t Fill()
Fill a Ntuple with current values in fArgs.
Definition: TNtuple.cxx:169
gROOT
#define gROOT
Definition: TROOT.h:406