// a macro to read in xy data from a file and make a plot // First run root, then // .L draw_XYdata.C // draw() // // At prompt, enter input file name (e.g., XYdata.txt). // Generalized as needed. #include "Riostream.h" void draw() { gROOT->Reset(); // first make a canvas and a 2D histogram for the axes TCanvas* canvas = new TCanvas("canvas", "canvas", 10, 10, 500, 500); canvas->SetFillColor(0); canvas->SetBorderMode(0); canvas->SetFrameBorderMode(0); // need this to turn off red hist frame! gROOT->SetStyle("Plain"); canvas->UseCurrentStyle(); gPad->SetLeftMargin(0.15); gPad->SetRightMargin(0.05); gPad->SetTopMargin(0.07); gPad->SetBottomMargin(0.17); gStyle->SetOptStat(0); gStyle->SetTitleBorderSize(0); gStyle->SetTitleSize(0.04); gStyle->SetTextFont(42); gStyle->SetTextSize(0.04); gStyle->SetTitleFont(42, "hxy"); // for histogram and axis title gStyle->SetLabelFont(42, "xyz"); // for axis labels (values) gStyle->SetTitleOffset(0.8, "h"); // what does this do? gStyle->SetTitleX(0.15); gStyle->SetTitleY(0.99); gROOT->ForceStyle(); // can make histogram or alternatively use the histograms automatically // connected to the TF1 or TGraph objects double xMin = 0.; double xMax = 10.; double yMin = 0.; double yMax = 10.; TH2F* axhist = new TH2F("axhist", "title", 10, xMin, xMax, 10, yMin, yMax); axhist->SetTitle(""); axhist->SetXTitle("x"); axhist->SetYTitle("y"); double u[10]; double x[10][500]; // Read in data from file and insert in TTree TString fileName; cout << "Enter file name: "; cin >> fileName; ifstream inFile; inFile.open(fileName); if (inFile.fail()) { cout << "Couldn't open file!" << endl; exit(1); } bool readLine = true; int lineNum = 0; int ncol; while ( readLine ){ TString line; stringstream ss; line.ReadLine(inFile); readLine = inFile.good(); if ( readLine ) { TString firstChar = line(0,1); bool useLine = firstChar != "#"; if ( useLine ){ int i = 0; stringstream ss; ss << line; // put whole line into ss TString token; bool getToken = true; while ( getToken ) { ss >> token; // extracts one token if ( token.Length() > 0 ) { u[i] = token.Atof(); i++; } else { getToken = false; } } // getToken ncol = i; for (int i=0; iGetXaxis(); TAxis* ya = axhist->GetYaxis(); xa->SetTitleOffset(1.4); // factor multiplies default offset ya->SetTitleOffset(1.4); xa->SetLabelOffset(0.015); ya->SetLabelOffset(0.015); xa->SetTickLength(0.015); // default = 0.03 ya->SetTickLength(0.015); // default = 0.03 xa->SetTitleSize(0.04); ya->SetTitleSize(0.04); // gPad->SetLogx(1); // xa->SetLimits(90., 700.); xa->SetNdivisions(-5); // negative value should force number of divisions? ya->SetNdivisions(-5); xa->SetLabelSize(0.04); ya->SetLabelSize(0.04); // Draw axes and then add stuff // kDot=1, kPlus, kStar, kCircle=4, kMultiply=5, // kFullDotSmall=6, kFullDotMedium=7, kFullDotLarge=8, // kFullCircle=20, kFullSquare=21, kFullTriangleUp=22, // kFullTriangleDown=23, kOpenCircle=24, kOpenSquare=25, // kOpenTriangleUp=26, kOpenDiamond=27, kOpenCross=28, // kFullStar=29, kOpenStar=30 axhist->Draw(); tg1->SetLineColor(1); tg1->SetLineWidth(2); tg1->SetLineStyle(1); tg1->SetMarkerColor(1); tg1->SetMarkerSize(0.8); tg1->SetMarkerStyle(20); tg1->Draw("L"); // or P for points tg2->SetLineColor(2); tg2->SetLineWidth(2); tg2->SetLineStyle(2); tg2->SetMarkerColor(2); tg2->SetMarkerSize(0.8); tg2->SetMarkerStyle(22); tg2->Draw("L,same"); TLegend* leg = new TLegend(0.2, 0.75, 0.5, 0.9); // x1, y1, x2, y2 leg->SetTextSize(0.04); leg->SetBorderSize(0); leg->SetFillColor(0); leg->AddEntry(tg1, "curve 1", "l"); // l for line, f for box, p for point leg->AddEntry(tg2, "curve 2", "l"); leg->Draw(); TLatex* tl = new TLatex(); tl->SetTextAlign(11); tl->SetTextSize(0.04); tl->SetTextFont(42); tl->SetNDC(); tl->DrawLatex(.7, 0.83, "A label"); TPostScript psfile("plot.eps", 113); // 113 makes eps canvas->Draw(); psfile.Close(); // canvas->Print("plot.gif", "gif"); }