Skip to content

Commit

Permalink
1. add RooDataSet -> TTree transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
VanyaBelyaev committed Oct 29, 2024
1 parent a52df55 commit f13633a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ReleaseNotes/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## New features


1. add `RooDataSet` -> `TTree` transformation

## Backward incompatible

## Bug fixes
Expand Down
58 changes: 57 additions & 1 deletion ostap/fitting/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from ostap.trees.cuts import expression_types, vars_and_cuts, order_warning
from ostap.utils.utils import evt_range, LAST_ENTRY, ALL_ENTRIES
from ostap.stats.statvars import data_decorate, data_range
from ostap.utils.valerrors import VAE
from ostap.utils.valerrors import VAE
import ostap.fitting.roocollections
import ostap.fitting.printable
import ROOT, random, math, sys, ctypes
Expand Down Expand Up @@ -3354,7 +3354,63 @@ def _rad_rows_ ( dataset ,
_new_methods_ += [
ROOT.RooAbsData.rows
]

# ============================================================================
## Convert RooDataSet to TTree
# @code
# dataset = ...
# data = dataset.ds2tree ( name = 'my_tree' , filename= 'aa.root'
# @endcode
# - result of the type <code>ostap.tree.data_utils.Data</code>
# @param name tree name (if not specified dataset name wil lbe used)
# @param filename tile name (if not specified, temporary file will be used)
def _ds_2tree_ ( dataset , name = '' , filename = '' ) :
""" Convert `ROOT.RooDataSet` to `ROOT.TTree`
>>> dataset = ...
>>> data = dataset.ds2tree ( name = 'my_tree' , filename= 'aa.root'
- name : tree name (if not specified dataset name wil lbe used)
- filename : file name (if not specified, temporary file will be used)
- result of the type `ostap.tree.data_utils.Data`
"""
if not filename :
import ostap.utils.cleanup as CU
filename = CU.CleanUp.tempfile ( suffix = '.root' )

if not name : name = dataset.GetName()

import ostap.io.root_file
from ostap.trees.data_utils import Data

store = dataset.store()
dstmp = None
with ROOTCWD() :
with ROOT.TFile ( filename , 'c' ) as rfile :
rfile.cd()
if not isinstance ( store , ROOT.RooTreeDataStore ) :
dstmp = ROOT.RooDataSet ( dataset , dsID() )
dstmp.convertToTreeStore()
store = dstmp.store()
assert isinstance ( store , ROOT.RooTreeDataStore ) , \
'Store is not RooTreeDataStore: %s' % ( type ( store ) .__name__ )
rfile [ name ] = store.tree()

## with ROOT.TFile ( filename , 'r' ) as rfile : rfile.ls()

if dstmp and isinstance ( dstmp , ROOT.RooDataSet ) :
dstmp = Ostap.MoreRooFit.delete_data ( dstmp )
del dstmp

return Data ( chain = name ,
files = [ filename ] ,
description = "TTree from dataset %s/%s " % ( dataset.name , dataset.title ) ,
silent = True )

ROOT.RooDataSet.ds2tree = _ds_2tree_

_new_methods_ += [
ROOT.RooDataSet.ds2tree
]

# ============================================================================

_new_methods_ += list ( data_decorate ( ROOT.RooAbsData ) )
Expand Down

0 comments on commit f13633a

Please sign in to comment.