As of version 2.6 it is possible to access a TTree dynamically. This is very useful for quick examinations of TTree’s:

var f = NTFile.Open(rootFileName, "READ");
var tree = f.Get(treeName) as ROOTNET.NTree;
foreach (dynamic evt in tree)
{
     Console.WriteLine("Run number is {0}", evt.run);
}

Most TTree leaves are supported – the basic types (int, float, double, etc.), vector’s of those, and ROOT.NET objects. Make sure to read the Warnings section below for cases that are not yet supported.

Enumerable Access

Access to the TTree is via IEnumerable. This means that the above foreach construct and LINQ are available:

bool hasRightRun = tree.Cast<dynamic>().All(evt => evt.run == 155073);

Note the cast to dynamic. This is required as I’ve not figured out how to have C++ return an explicitly C# dynamic object.

Performance

As will all things dynamic in ROOT.NET performance is not yet optimal. .NET dynamic allows one to replace dynamic code with compiled code on the fly when the dynamic call site is basically the same. Unfortunately, ROOT.NET does not yet take advantage of these features. So this method is not going to be very fast! In general, you are going to be accessing .NET dynamic and CINT to extract values from the Tree, and then from there processing the values in .NET. If you have a large amount of data to deal with I’d say used C++ or a tool like LINQToROOT.

To improve performance a number of things are done by this package:

  • Leaves are all accessed via SetBranchAddress. This means the quickest access to the ROOT data is done.
  • No branch is read that doesn’t need to be read. Thus if your tree is properly split and you only access one leaf, only the data for that leaf will be loaded.
  • Custom wrappers for vectors of basic types (and ROOT object types) are supported. This also means you can avoid CINT when accessing these. The custom wrappers allow IEnumerable and the-like access to the objects.
  • Strings are custom wrapped in a way that makes it possible to use – and vector<string> as well.

Anything else that isn’t recognized uses the standard ROOT.NET access – a wrapper object if available, or a dynamic object if not.

Warnings

Support for dynamic TTree access is experimental. Expect it to change and (I hope) improve.

There are a number of things that aren’t supported. One pitfall is TChain support. There is none. Worse, the infrastructure will happily let you access files in a TChain and perhaps even the first file will be correct. This will, obviously, be supported in a later release.

C-style arrays are not yet supported.

Last edited Aug 30, 2012 at 1:33 PM by gwatts, version 1

Comments

No comments yet.