TH1F Example

This example is taken from the SVN repository: ./Examples/TH1F/Test.java.

It creates a ROOT file (FromJava.root), creates a TH1F called "demoA" and fills it with 4096 random points.

A subdirectory called "subdir1" is created, and we move to it and create a second TH1F called "demoB", which is filled with more random data.

import java.util.Random;
import hep.physics.vec.*;
import org.lcsim.thirdparty.javaROOT.*;

public class Test
{
	public static void main(String[] args)
	{
		// Load the library
		try
		{
			System.loadLibrary( "javaROOT" );
		}
		catch( UnsatisfiedLinkError javaROOT_linkError )
		{
			System.err.println( "JavaROOT native library failed to load.\n" + javaROOT_linkError + "\n" );
			System.exit( -1 );
		}

		// Create a RNG to fill our histograms with.
		Random r = new Random( 0 );

		// Create a file
		RootSessionField sess = new RootSessionField( "FromJava.root", "RECREATE", "Test", 1 );

		// Create a TH1F and fill it with some data. (Exactly the same principle for the other histograms.)s
		sess.newTH1F( "demoA", "demoA", 100, 0, 1 );
		for( int i = 0; i < 4096; i++ )
		{
			sess.fillTH1F( "demoA", (float) r.nextGaussian() );
		}

		// Create a subdirectory and switch to it.
		sess.mkdir( "subdir1" );
		sess.cd( "subdir1" );

		// Create a second TH1F (inside the new subdirectory) and fill it.
		sess.newTH1F( "demoB", "demoB", 100, 0, 1 );
		for( int i = 0; i < 4096; i++ )
		{
			sess.fillTH1F( "demoB", (float) 2 * r.nextGaussian() );
		}

		// Essential to ensure all values are properly committed to the file.
		sess.delete();
	}
}

  • No labels