User Tools

Site Tools


en:prog:asy_root_drawing

Plotting ROOT objects

This is a documentation of macros from base/root.asy package.

The first example illustrates the way of plotting ROOT objects in Asymptote.

root_drawing/general.asy

Source code:

import root;
 
size(6cm, 6cm, false);
 
// ROOT objects are drawn by
// void draw(transform tr = identity(), picture pic=currentpicture, rObject obj, string options="", pen pen=currentpen, 
//	marker marker=nomarker, Label legend="")
 
draw(rGetObj("../example.root", "hist"), "", blue);
draw(rGetObj("../example.root", "hist|gaus"), "", red+1pt);
 
// ROOT objects can be transformed while drawn
draw(shift(0, 40)*yscale(-1), robj, heavygreen+dashed);
 
// indeed, combination with other asymptote commands is possible
draw(Label("fit", 0), (3, 30)--(1.5, 15), EndArrow);

Output:

root_drawing_general.pdf

The following examples will show the draw options that are available for each supported ROOT type. For convenience, the pad_layout macro package will be used from now on.

root_drawing/TH1.asy

Source code:

import root;
import pad_layout;
 
// draw the histogram by line
NewPad();
draw(rGetObj("../example.root", "hist"), "", blue);
 
// add error bars
NewPad();
draw(rGetObj("../example.root", "hist"), "eb", blue);
 
// add error bars
NewPad();
draw(rGetObj("../example.root", "hist"), "ec", blue);
 
// set opacity of the error contour
NewPad();
TH1_errorContourOpacity = 0.05;
draw(rGetObj("../example.root", "hist"), "ec", blue);
 
NewRow();
 
// draw the histogram normalized to the integral/sum of 1.
NewPad();
draw(rGetObj("../example.root", "hist"), "n", red);
 
NewRow();
 
// x-range control
NewPad();
TH1_lowLimit = -1;
TH1_highLimit = +1;
draw(rGetObj("../example.root", "hist"), "", magenta);
limits((-5, 0), (5, 60));
 
// force plotting front and back edges (they are drawn from y value of TH1_def_val)
NewPad();
TH1_def_val = 10;
draw(rGetObj("../example.root", "hist"), "e", magenta);
limits((-5, 0), (5, 60));
 
// reset x-range control
TH1_lowLimit = -inf; TH1_highLimit = +inf;
 
// empty/negative bin control in log scale: these bins are assigned value of TH1_def_val (after scaling)
NewPad();
scale(Linear, Log);
TH1_def_val = -5;
draw(rGetObj("../example.root", "hist"), "e", magenta);
 
 
NewRow();
 
// add basic characteristics to the legend
NewPad();
draw(rGetObj("../example.root", "hist"), "lE,lM,lR", heavygreen);
AttachLegend();
 
// or an abbreviation
NewPad();
draw(rGetObj("../example.root", "hist"), "l", heavygreen);
AttachLegend();

Output:

root_drawing_th1.pdf

root_drawing/TH2.asy

Source code:

import root;
import pad_layout;
 
// bin content reflected by marker size
NewPad();
draw(rGetObj("../example.root", "hist2"), "s", blue);
 
// add cell boundaries
NewPad();
TH2_boundaryPen = red;
draw(rGetObj("../example.root", "hist2"), "s,b", blue);
 
NewRow();
 
// bin content reflected by cell color
NewPad(axesAbove=true);
TH2_palette = Gradient(white, blue, heavygreen, red, black);
draw(rGetObj("../example.root", "hist2"), "p");
 
// add palette bar
NewPad(axesAbove=true);
draw(rGetObj("../example.root", "hist2"), "p,bar");
 
// use logarithmic color scale (i.e. scale in z)
NewPad(axesAbove=true);
scale(Linear, Linear, Log);
draw(rGetObj("../example.root", "hist2"), "p,bar");
 
// control palette bar position, size, ticks and lable
NewPad("$x$", "$y$", axesAbove=true);
TH2_paletteBarSpacing = 0.1;
TH2_paletteBarWidth = 0.05;
TH2_zLabel = "$z$";
TH2_paletteTicks = PaletteTicks(Step=20, step=10);
draw(rGetObj("../example.root", "hist2"), "p,bar");
 
NewRow();
 
// z range selection
NewPad(axesAbove=true);
TH2_z_min = 50;
TH2_z_max = 100;
draw(rGetObj("../example.root", "hist2"), "p,bar");
 
// disable z selection
TH2_z_min = 0;
TH2_z_max = -inf;

Output:

root_drawing_th2.pdf

root_drawing/TGraph.asy

Source code:

import root;
import pad_layout;
 
// draw line through points
NewPad();
draw(rGetObj("../example.root", "graph"), "l", red); 
 
// draw line through points and draw error contour (fill the +- 1sigma region around the actual curve)
NewPad();
draw(rGetObj("../example.root", "graph"), "l,ec", heavygreen); 
 
// set the opacity of the error contour
NewPad();
TGraph_errorContourOpacity = 0.05;
draw(rGetObj("../example.root", "graph"), "l,ec", blue); 
 
NewRow();
 
// draw each of the points with a marker
NewPad();
draw(rGetObj("../example.root", "graph"), "p", red, mCi+red+2pt);
 
// error bars with a special color
NewPad();
TGraph_errorBarPen = blue;
draw(rGetObj("../example.root", "graph"), "p,sebc", red, mCi+red+2pt);
 
// ignore error bars
NewPad();
draw(rGetObj("../example.root", "graph"), "p,ieb", mCi+red+2pt);
 
NewRow();
 
// draw only a chosen x region
NewPad();
TGraph_highLimit = 2;
draw(rGetObj("../example.root", "graph"), "p", red, mCi+red+2pt); 
TGraph_lowLimit = 2;
TGraph_highLimit = +inf;
draw(rGetObj("../example.root", "graph"), "l,ec,p", red, mSq+blue+1pt);
TGraph_lowLimit = -inf;
TGraph_highLimit = +inf;
 
// skip certain points (calculation error?) 
NewPad();
TGraph_skipPoints = new int[] {9, 10, 11};
draw(rGetObj("../example.root", "graph"), "p", red, mCi+red+2pt);
 
NewRow();
 
// keep all points
NewPad();
TGraph_reducePoints = 1;
draw(rGetObj("../example.root", "scatter"), "p", mCi+black+1pt);
 
// keep every tenth point only
NewPad();
TGraph_reducePoints = 10;
draw(rGetObj("../example.root", "scatter"), "p", mCi+black+1pt);
 
// limit total number of points (keep first 100 points)
NewPad();
TGraph_N_limit = 100;
draw(rGetObj("../example.root", "scatter"), "p", mCi+black+1pt);

Output:

root_drawing_tgraph.pdf

root_drawing/TF1.asy

Source code:

import root;
import pad_layout;
 
NewPad();
draw(rGetObj("../example.root", "hist|gaus"), red);

Output:

root_drawing_tf1.pdf

en/prog/asy_root_drawing.txt · Last modified: 2012/02/10 10:53 (external edit)