Created
June 13, 2014 00:10
-
-
Save ndawe/d9035a7bc187e5fad4f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rootpy.tree import Tree, TreeModel, FloatCol | |
from rootpy.io import root_open | |
from root_numpy import root2array, array2tree | |
import numpy as np | |
from random import gauss | |
import random | |
random.seed(0) | |
class Sample(TreeModel): | |
a = FloatCol() | |
b = FloatCol() | |
with root_open('sample.root', 'recreate'): | |
# generate toy data in a TTree | |
tree = Tree('sample', model=Sample) | |
for i in xrange(1000): | |
tree.a = gauss(0, 1) | |
tree.b = gauss(1, 1) | |
tree.Fill() | |
tree.write() | |
# read in the TTree as a NumPy array | |
array = root2array('sample.root') | |
with root_open('bootstrap.root', 'recreate') as output: | |
# bootstrap 100 times | |
for bootstrap_idx in xrange(100): | |
print "bootstrap {0} ...".format(bootstrap_idx) | |
# resample with replacement | |
sample_idx = np.random.choice(len(array), size=len(array), | |
replace=True) | |
array_bootstrapped = array[sample_idx] | |
# convert back to a TTree and write it out | |
tree_bootstrapped = array2tree(array_bootstrapped, | |
name='bootstrap_{0}'.format( | |
bootstrap_idx)) | |
tree_bootstrapped.Write() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment