//#include "Riostream.h" void basic_new3() { // 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(); ///new: define array const Int_t kMaxTrack = 100; Int_t arraysign[kMaxTrack]; Float_t arraypx[kMaxTrack]; Float_t arraypy[kMaxTrack]; Float_t arraypz[kMaxTrack]; // read file $ROOTSYS/tutorials/tree/basic.dat // this file has 3 columns of float data TString dir = gSystem->UnixPathName(__FILE__); dir.ReplaceAll("basic_new3.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_new.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); ///new: fill array arraysign[nlines] = ch; arraypx[nlines] = x; arraypy[nlines] = y; arraypz[nlines] = z; nlines++; h2->Fill(sqrt((x*x) + (y*y))); //cout << " px+py =" << x+y << endl; } printf(" found %d points\n",nlines); ///new: print statement cout<<" new :px at position 3 " <GetV1(1)<Draw(); c3->cd(1); h2->SetLineColor(2); h2->Draw(); //h2->Fit("gaus"); //TF1 *f3 = h2->GetFunction("gaus"); //// new : linear fit TF1 *f2 = new TF1("f2", "[0] + [1]*x", 0, 2); h2->Fit("f2","R"); cout << " fit function values " << f2->GetParameter(0)<< " and " << f2->GetParameter(1)<Eval(1.0)<< endl; cout << " fit integral " << f2->Integral(0,2)<< endl; /// //TF1 *f3 = new TF1("f3", " exp(- x/0.3)"); //f3->Draw(); c3->cd(2); h1->Draw(); f->Write(); }