<commit_before>#!/usr/bin/env python

# rfwsutils - utilities for manipulating RooFit workspaces from the command line
#
# Copyright 2013 University of California, San Diego
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import sys, os

#----------------------------------------------------------------------

def findWorkspace(rootFile):
    """ searches for a toplevel RooWorkspace instance in the given TFile """

    import ROOT

    retval = None

    for key in rootFile.GetListOfKeys():
        name = key.GetName()

        obj = rootFile.Get(name)

        if isinstance(obj,ROOT.RooWorkspace):
            if retval == None:
                retval = obj
            else:
                print >> sys.stderr,"more than one instance of RooWorkspace found in file",rootFile
                sys.exit(1)

    if retval == None:
        print >> sys.stderr,"no RooWorkspace found in file",rootFile
        sys.exit(1)

    return retval    


#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------

from optparse import OptionParser

parser = OptionParser(   """
   usage: %prog [options] input_workspace.root output_workspace.root old_pattern new_pattern [ old_pattern2 new_pattern2 ...]

   Tool for mass renaming objects in a RooWorkspace.

   WARNING: the program will overwrite the output workspace file without asking for confirmation.

   example:

   %prog --lib $CMSSW_BASE/lib/$SCRAM_ARCH/libHiggsAnalysisCombinedLimit.so input.root output.root \\
      '(.*)_v2$' '\\1'

   will rename e.g. XYZ_v2 to XYZ      

""")


parser.add_option("--lib",
                  dest="libs",
                  default = [],
                  type="str",
                  action="append", # append to list
                  help="library to load before opening the files. Can be specified multiple times.",
                  metavar="LIB")

(options, ARGV) = parser.parse_args()


if len(ARGV) < 4:
    print >> sys.stderr,"must specify at least four positional arguments. Run with -h to get more information"
    sys.exit(1)

inputFname = ARGV.pop(0)
outputFname = ARGV.pop(0)

if len(ARGV) % 2 != 0:
    print >> sys.stderr,"must specify an even number of arguments after the input and output files"
    sys.exit(1)

renameArgs = zip(ARGV[::2],ARGV[1::2])

for lib in options.libs:
    if not os.path.exists(lib):
        print >> sys.stderr,"library " + lib + " does not exist"
        sys.exit(1)


#----------------------------------------

import ROOT; gcs = []
for lib in options.libs:
    ROOT.gSystem.Load(lib)

fin = ROOT.TFile.Open(inputFname)

if fin == None or not fin.IsOpen():
    print >> sys.stderr,"error opening input file " + inputFname
    sys.exit(1)


ws = findWorkspace(fin)
<commit_msg>now using wsutils.loadLibraries(..) to load the libraries instead of doing it by hand (also using the default options provided by wsutils in general)<commit_after>#!/usr/bin/env python

# rfwsutils - utilities for manipulating RooFit workspaces from the command line
#
# Copyright 2013 University of California, San Diego
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import sys, os, wsutils

#----------------------------------------------------------------------

def findWorkspace(rootFile):
    """ searches for a toplevel RooWorkspace instance in the given TFile """

    import ROOT

    retval = None

    for key in rootFile.GetListOfKeys():
        name = key.GetName()

        obj = rootFile.Get(name)

        if isinstance(obj,ROOT.RooWorkspace):
            if retval == None:
                retval = obj
            else:
                print >> sys.stderr,"more than one instance of RooWorkspace found in file",rootFile
                sys.exit(1)

    if retval == None:
        print >> sys.stderr,"no RooWorkspace found in file",rootFile
        sys.exit(1)

    return retval    


#----------------------------------------------------------------------
# main
#----------------------------------------------------------------------

from optparse import OptionParser

parser = OptionParser(   """
   usage: %prog [options] input_workspace.root output_workspace.root old_pattern new_pattern [ old_pattern2 new_pattern2 ...]

   Tool for mass renaming objects in a RooWorkspace.

   WARNING: the program will overwrite the output workspace file without asking for confirmation.

   example:

   %prog --lib $CMSSW_BASE/lib/$SCRAM_ARCH/libHiggsAnalysisCombinedLimit.so input.root output.root \\
      '(.*)_v2$' '\\1'

   will rename e.g. XYZ_v2 to XYZ      

""")


# parser.add_option("--lib",
#                   dest="libs",
#                   default = [],
#                   type="str",
#                   action="append", # append to list
#                   help="library to load before opening the files. Can be specified multiple times.",
#                   metavar="LIB")

wsutils.addCommonOptions(parser)
(options, ARGV) = parser.parse_args()

#----------------------------------------
wsutils.checkCommonOptions(options)


if len(ARGV) < 4:
    print >> sys.stderr,"must specify at least four positional arguments. Run with -h to get more information"
    sys.exit(1)

inputFname = ARGV.pop(0)
outputFname = ARGV.pop(0)

if len(ARGV) % 2 != 0:
    print >> sys.stderr,"must specify an even number of arguments after the input and output files"
    sys.exit(1)

renameArgs = zip(ARGV[::2],ARGV[1::2])


#----------------------------------------

import ROOT; gcs = []

wsutils.loadLibraries(options)

fin = ROOT.TFile.Open(inputFname)

if fin == None or not fin.IsOpen():
    print >> sys.stderr,"error opening input file " + inputFname
    sys.exit(1)


ws = findWorkspace(fin)

{'fraction_non_alphanumeric': 0.17093127045522166, 'fraction_numerical': 0.00788455816721214, 'mean_word_length': 2.1153846153846154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2582063', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1799, 'n_words': 633}