basic.C

Go to the documentation of this file.
00001 #include "Riostream.h"
00002 void basic() {
00003 //  Read data from an ascii file and create a root file with an histogram and an ntuple.
00004 //   see a variant of this macro in basic2.C
00005 //Author: Rene Brun
00006       
00007 
00008 // read file $ROOTSYS/tutorials/tree/basic.dat
00009 // this file has 3 columns of float data
00010    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
00011    dir.ReplaceAll("basic.C","");
00012    dir.ReplaceAll("/./","/");
00013    ifstream in;
00014    in.open(Form("%sbasic.dat",dir.Data()));
00015 
00016    Float_t x,y,z;
00017    Int_t nlines = 0;
00018    TFile *f = new TFile("basic.root","RECREATE");
00019    TH1F *h1 = new TH1F("h1","x distribution",100,-4,4);
00020    TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z");
00021 
00022    while (1) {
00023       in >> x >> y >> z;
00024       if (!in.good()) break;
00025       if (nlines < 5) printf("x=%8f, y=%8f, z=%8f\n",x,y,z);
00026       h1->Fill(x);
00027       ntuple->Fill(x,y,z);
00028       nlines++;
00029    }
00030    printf(" found %d points\n",nlines);
00031 
00032    in.close();
00033 
00034    f->Write();
00035 }

Generated on Tue Jul 5 15:45:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1