//#include "Riostream.h" void basic() { // 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 //Author: Rene Brun gStyle->SetOptFit(1); gStyle->SetOptTitle(1); gStyle->SetOptStat(1); TCanvas *c3 = new TCanvas("c3", "c3",0,0,400,400); c3->SetFillColor(10); c3->SetBorderMode(0); c3->Divide(2,2); c3->Draw(); // read file $ROOTSYS/tutorials/tree/basic.dat // this file has 3 columns of float data TString dir = gSystem->UnixPathName(__FILE__); dir.ReplaceAll("basic.C",""); dir.ReplaceAll("/./","/"); ifstream in; in.open(Form("%stracks_nofirstline.txt",dir.Data())); Int_t ch,n; Float_t x,y,z; Int_t nlines = 0; TFile *f = new TFile("basic.root","RECREATE"); TH1F *h1 = new TH1F("h1","x distribution",100,-4,4); TH1F *h2 = new TH1F("h2","pt distribution",100,0,4); TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","n:ch:x:y:z"); while (1) { in >> n >> ch >> x >> y >> z; if (!in.good()) break; if (nlines < 5) printf("n = %2i, ch = %2i, x=%8f, y=%8f, z=%8f\n",n,ch,x,y,z); h1->Fill(x); ntuple->Fill(n,ch,x,y,z); nlines++; h2->Fill(sqrt((x*x) + (y*y))); cout << " px+py =" << x+y << endl; } printf(" found %d points\n",nlines); in.close(); //h1->Draw(); c3->cd(1); h2->SetLineColor(2); h2->Draw(); h2->Fit("gaus"); c3->cd(2); h1->Draw(); f->Write(); }