Interpreting
ROOT executes your C++ code. There are two ways: loading it into the interpreter or compiling it as a shared library. Either way, it's easiest to provide a function that has the same name as the file. For instance you could have a file printTree.C containing
int printTree(TFile* file, const char* name) {
TTree* tree = 0;
file->GetObject(name, tree);
if (tree) {
tree->Print();
return 0;
} else {
Error("printTree()", "Cannot find tree %s!", name);
return -1;
}
}
You can now run this as
.x printTree.C(file, "MyTree");
This is equivalent to
.L printTree.C
printTree(file, "MyTree");
Compiling
You can also have your code compiled into a shared library, simply by adding a "+" behind the file name:
.x printTree.C+(file, "MyTree");
In the above example this will fail: we need to add the #includes for he compiler to be able to understand the code, while the interpreter has a lot of contextual knowledge and can automatically include headers and load libraries.
Expressions
You saw that with ".x", ROOT prints the result of running the function while when using ".L" and calling th function it does not. The way to trigger the printing of expression results is by omitting the trailing semicolon:
TMath::Gaus(1., 1., 1.)
will print
(Double_t) 1.000000e+00
Help!
For TMath::Gaus() as well as for any other ROOT class or function, please check ROOT's reference guide. Try to find the documentation for TTree::Print()! We have now covered the basics - let's look at the tree's data!