User Tools

Site Tools


en:prog:asy_root_interface

Accessing ROOT objects from Asymptote

root_interface/basic_manipulations.asy

Source code:

// ROOT objects are represented in Asymptote by one single type rObject
rObject o;
 
// load a ROOT object from a file
string file = "../example.root";
rObject o = rGetObj(file, "graph");
 
// test whether the object is valid
if (!o.valid)
	write("invalid");
 
// print the contents of the object
o.Print();
 
// you can also print (Asymptote) information on the object
write(o);
write("object o: ", o);
 
// you can check the inheritance path of the object
if (o.InheritsFrom("TGraph"))
	write("inherits from TGraph");
 
// ROOT objects can be copied in two ways:
// 1) shallow copy (only pointers are copied, modifications committed to o2 affect o too)
rObject o2 = o;
write("object o2: ", o2);
 
// 2) deep copy (data are copied)
// o3 and o have independent data (the obj pointers are different now)
rObject o3 = o.Copy();	
write("object o3: ", o3);
 
// an example where the deep copy might be useful - the same histogram with two different binnings
rObject h = rGetObj(file, "hist");
rObject h2 = h.Copy();
h.vExec("Rebin", 5);
h2.vExec("Rebin", 2);

Output:

rObject::Print > obj at 0x7f5c701781a0
	class=`TGraphErrors', name=`graph'
TGraphErrors `graph' @ 0x7f5c701781a0
object o: TGraphErrors `graph' @ 0x7f5c701781a0
inherits from TGraph
object o2: TGraphErrors `graph' @ 0x7f5c701781a0
object o3: TGraphErrors `graph' @ 0x7f5c701780b0
root_interface/method_calling.asy

Source code:

rObject o = rGetObj("../example.root", "hist");
 
// dump an object
o.vExec("Dump");
 
// get number of bins
int i = o.iExec("GetNbinsX");
write("bins = ", i);
 
// get the mean value
real m = o.rExec("GetMean");
write("mean = ", m);
 
// get the name of the histogram
write("name = ", o.sExec("GetName"));
 
// get the contents of the first bin
write("contents of the first bin = ", o.rExec("GetBinContent", 1));
 
 
rObject o = rGetObj("../example.root", "graph");
 
// passing parameters by reference
// get the coordinates of the first point of a graph
real[] x = {2.};
real[] y = {2.};
o.vExec("GetPoint", 0, x, y);
write("x = ", x[0]);
write("y = ", y[0]);

Output:

==> Dumping object at: 0x00007fb3c4177e50, name=hist, class=TH1D

fNcells                       102                 number of bins(1D), cells (2D) +U/Overflows
fXaxis                        ->7fb3c4177ec8      X axis descriptor
fXaxis.fNbins                 100                 Number of bins
fXaxis.fXmin                  -5                  low edge of first bin
fXaxis.fXmax                  5                   upper edge of last bin
fXaxis.fXbins                 ->7fb3c4177f48      Bin edges array in X
fXaxis.fXbins.*fArray         ->0                 [fN] Array of fN doubles
fXaxis.fXbins.fN              0                   Number of array elements
fXaxis.fFirst                 0                   first bin to display
fXaxis.fLast                  0                   last bin to display
fXaxis.fBits2                 0                   second bit status word
fXaxis.fTimeDisplay           false               on/off displaying time values instead of numerics
fXaxis.fTimeFormat                                Date&time format, ex: 09/12/99 12:34:00
fXaxis.*fParent               ->7fb3c4177e50      !Object owning this axis
fXaxis.*fLabels               ->0                 List of labels
fXaxis.fName                  xaxis               object identifier
fXaxis.fTitle                 x                   object title
fXaxis.fUniqueID              0                   object unique identifier
fXaxis.fBits                  0x03000000          bit field status word
fXaxis.fNdivisions            510                 Number of divisions(10000*n3 + 100*n2 + n1)
fXaxis.fAxisColor             1                   color of the line axis
fXaxis.fLabelColor            1                   color of labels
fXaxis.fLabelFont             42                  font for labels
fXaxis.fLabelOffset           0.005               offset of labels
fXaxis.fLabelSize             0.035               size of labels
fXaxis.fTickLength            0.03                length of tick marks
fXaxis.fTitleOffset           1                   offset of axis title
fXaxis.fTitleSize             0.035               size of axis title
fXaxis.fTitleColor            1                   color of axis title
fXaxis.fTitleFont             42                  font for axis title
fYaxis                        ->7fb3c4177f98      Y axis descriptor
fYaxis.fNbins                 1                   Number of bins
fYaxis.fXmin                  0                   low edge of first bin
fYaxis.fXmax                  1                   upper edge of last bin
fYaxis.fXbins                 ->7fb3c4178018      Bin edges array in X
fYaxis.fXbins.*fArray         ->0                 [fN] Array of fN doubles
fYaxis.fXbins.fN              0                   Number of array elements
fYaxis.fFirst                 0                   first bin to display
fYaxis.fLast                  0                   last bin to display
fYaxis.fBits2                 0                   second bit status word
fYaxis.fTimeDisplay           false               on/off displaying time values instead of numerics
fYaxis.fTimeFormat                                Date&time format, ex: 09/12/99 12:34:00
fYaxis.*fParent               ->7fb3c4177e50      !Object owning this axis
fYaxis.*fLabels               ->0                 List of labels
fYaxis.fName                  yaxis               object identifier
fYaxis.fTitle                                     object title
fYaxis.fUniqueID              0                   object unique identifier
fYaxis.fBits                  0x03000000          bit field status word
fYaxis.fNdivisions            510                 Number of divisions(10000*n3 + 100*n2 + n1)
fYaxis.fAxisColor             1                   color of the line axis
fYaxis.fLabelColor            1                   color of labels
fYaxis.fLabelFont             42                  font for labels
fYaxis.fLabelOffset           0.005               offset of labels
fYaxis.fLabelSize             0.035               size of labels
fYaxis.fTickLength            0.03                length of tick marks
fYaxis.fTitleOffset           1                   offset of axis title
fYaxis.fTitleSize             0.035               size of axis title
fYaxis.fTitleColor            1                   color of axis title
fYaxis.fTitleFont             42                  font for axis title
fZaxis                        ->7fb3c4178068      Z axis descriptor
fZaxis.fNbins                 1                   Number of bins
fZaxis.fXmin                  0                   low edge of first bin
fZaxis.fXmax                  1                   upper edge of last bin
fZaxis.fXbins                 ->7fb3c41780e8      Bin edges array in X
fZaxis.fXbins.*fArray         ->0                 [fN] Array of fN doubles
fZaxis.fXbins.fN              0                   Number of array elements
fZaxis.fFirst                 0                   first bin to display
fZaxis.fLast                  0                   last bin to display
fZaxis.fBits2                 0                   second bit status word
fZaxis.fTimeDisplay           false               on/off displaying time values instead of numerics
fZaxis.fTimeFormat                                Date&time format, ex: 09/12/99 12:34:00
fZaxis.*fParent               ->7fb3c4177e50      !Object owning this axis
fZaxis.*fLabels               ->0                 List of labels
fZaxis.fName                  zaxis               object identifier
fZaxis.fTitle                                     object title
fZaxis.fUniqueID              0                   object unique identifier
fZaxis.fBits                  0x03000000          bit field status word
fZaxis.fNdivisions            510                 Number of divisions(10000*n3 + 100*n2 + n1)
fZaxis.fAxisColor             1                   color of the line axis
fZaxis.fLabelColor            1                   color of labels
fZaxis.fLabelFont             42                  font for labels
fZaxis.fLabelOffset           0.005               offset of labels
fZaxis.fLabelSize             0.035               size of labels
fZaxis.fTickLength            0.03                length of tick marks
fZaxis.fTitleOffset           1                   offset of axis title
fZaxis.fTitleSize             0.035               size of axis title
fZaxis.fTitleColor            1                   color of axis title
fZaxis.fTitleFont             42                  font for axis title
fBarOffset                    0                   (1000*offset) for bar charts or legos
fBarWidth                     1000                (1000*width) for bar charts or legos
fEntries                      1000                Number of entries
fTsumw                        1000                Total Sum of weights
fTsumw2                       1000                Total Sum of squares of weights
fTsumwx                       26.8014             Total Sum of weight*X
fTsumwx2                      1077.86             Total Sum of weight*X*X
fMaximum                      -1111               Maximum value for plotting
fMinimum                      -1111               Minimum value for plotting
fNormFactor                   0                   Normalization factor
fContour                      ->7fb3c4178180      Array to display contour levels
fContour.*fArray              ->0                 [fN] Array of fN doubles
fContour.fN                   0                   Number of array elements
fSumw2                        ->7fb3c4178198      Array of sum of squares of weights
fSumw2.*fArray                ->0                 [fN] Array of fN doubles
fSumw2.fN                     0                   Number of array elements
fOption                                           histogram options
*fFunctions                   ->7fb3c4092d10      ->Pointer to list of functions (fits and user)
fBufferSize                   0                   fBuffer size
*fBuffer                      ->0                 [fBufferSize] entry buffer
*fDirectory                   ->7fb3c4008a90      !Pointer to directory holding this histogram
fDimension                    1                   !Histogram dimension (1, 2 or 3 dim)
*fIntegral                    ->0                 !Integral of bins used by GetRandom
*fPainter                     ->0                 !pointer to histogram painter
fName                         hist                object identifier
fTitle                                            object title
fUniqueID                     0                   object unique identifier
fBits                         0x03000008          bit field status word
fLineColor                    602                 line color
fLineStyle                    1                   line style
fLineWidth                    1                   line width
fFillColor                    0                   fill area color
fFillStyle                    1001                fill area style
fMarkerColor                  1                   Marker color index
fMarkerStyle                  1                   Marker style
fMarkerSize                   1                   Marker size
*fArray                       0                   [fN] Array of fN doubles
fN                            102                 Number of array elements
bins = 100
mean = 0.0268014110408005
name = hist
contents of the first bin = 0
x = -5
y = 25
root_interface/loading_from_file.asy

Source code:

string file = "../example.root";
 
// ROOT objects can be load from ROOT files by calling
// rGetObj(string file, string name, bool error=true, bool search=true)
 
// load a ROOT object from a file
rObject o = rGetObj(file, "graph");
o.Print();
 
// you can load objects from directories
rObject h = rGetObj(file, "dir1/hist");
h.Print();
 
// you can extract ROOT objection even from collection objects (such as TCanvas, ...)
// 1) by index within the collection:
rObject o = rGetObj(file, "canvas#1");
o.Print();
 
// 2) by the name within the collection
rObject o = rGetObj(file, "canvas|hist");
o.Print();
 
// the /, | and # operators can be chained up
o = rGetObj(file, "dir2/canvas|hist#0");
o.Print();
 
// if the | and # operators would clash with object names, their expansion can be inhibited
o = rGetObj(file, "dir3/a#weird|name", search=false);
o.Print();
 
// if the requested object does not exist, Asymptote would issue an error message
// this can be avoided by setting error=false, in this case the returned object is ivalid
o = rGetObj(file, "ThisDoesNotExist", error=false);
o.Print();
 
// for convenience there si `robj' keeping reference to the last loaded object
rGetObj(file, "graph");
robj.Print();
 
// in the following case the loading fails hence robj still referes to the graph from the previous example
rGetObj(file, "ThisDoesNotExist", error=false);
robj.Print();

Output:

rObject::Print > obj at 0x7f3b74178540
	class=`TGraphErrors', name=`graph'
rObject::Print > obj at 0x7f3b742ee770
	class=`TH1D', name=`hist'
rObject::Print > obj at 0x7f3b74403520
	class=`TGraphErrors', name=`graph'
rObject::Print > obj at 0x7f3b743a3ac0
	class=`TH1D', name=`hist'
rObject::Print > obj at 0x7f3b743a1230
	class=`TF1', name=`gaus'
rObject::Print > obj at 0x7f3b743a2b90
	class=`TH1D', name=`a#weird|name'
rObject::Print > obj at (nil)
rObject::Print > obj at 0x7f3b74435750
	class=`TGraphErrors', name=`graph'
rObject::Print > obj at 0x7f3b74435750
	class=`TGraphErrors', name=`graph'
root_interface/quering_file_structure.asy

Source code:

string list[];
 
// this prints the list of sub-directories and objects in the root directory of the file
list = rGetListOfEntries("../example.root", ".");
write("1) ", list);
 
// this prints the list of sub-directories (only!) in the root directory of the file
list = rGetListOfDirectories("../example.root", ".");
write("2) ", list);
 
// this prints the list of objects in the directory called `dir'
list = rGetListOfObjects("../example.root", "dir1");
write("3) ", list);

Output:

1) 
0:	hist
1:	hist2
2:	graph
3:	scatter
4:	graph2
5:	canvas
6:	canvas2
7:	dir1
8:	dir2
9:	dir3
2) 
0:	dir1
1:	dir2
2:	dir3
3) 
0:	hist
en/prog/asy_root_interface.txt · Last modified: 2012/02/10 10:53 (external edit)