Exemple #1
0
 def __init__(self, name, xml_filename, **kwargs):
    mva_vars = get_vars(xml_filename)
    variables = [i.name.value() for i in mva_vars]
    self.reader    = ROOT.TMVA.Reader( "!Color:Silent=%s:Verbose=%s" % (kwargs.get('silent','T'), kwargs.get('verbose','F')))
    self.var_map   = {}
    self.name      = name
    self.variables = variables
    self.xml_filename = xml_filename
    for var in variables:
       self.var_map[var] = array.array('f',[0]) 
       self.reader.AddVariable(var, self.var_map[var])
    self.reader.BookMVA(name, xml_filename)
parser.add_argument('CvsBXML', help='XML Weight file of the Charm vs B training')
args = parser.parse_args()

if not os.path.isfile(args.CvsLXML):
   raise ValueError('File %s does not exists!' % args.CvsLXML)
if not os.path.isfile(args.CvsBXML):
   raise ValueError('File %s does not exists!' % args.CvsBXML)

with open(args.CvsLXML) as CvsLXML:
   sha1_CvsLXML = hashlib.sha1(CvsLXML.read()).hexdigest()

with open(args.CvsBXML) as CvsBXML:
   sha1_CvsBXML = hashlib.sha1(CvsBXML.read()).hexdigest()

CvsLXML_vpset = cms.VPSet(
   *get_vars(args.CvsLXML, False) #False: do not mimick FileInPath functionalities and take the bare path as the file path
    )
CvsBXML_vpset = cms.VPSet(
   *get_vars(args.CvsBXML, False)
    )

with open('%s/src/RecoBTag/CTagging/python/training_settings.py' % os.environ['CMSSW_BASE'], 'w') as output:
   output.write('import FWCore.ParameterSet.Config as cms\n')
   output.write('''
## IMPORTANT!
## This file was automatically generated by RecoBTag/CTagging/test/dump_training_vars_cfg.py
## with input xml files:
##    - C vs L: %s sha1 checksum: %s
##    - C vs B: %s sha1 checksum: %s 

''' % (args.CvsLXML, sha1_CvsLXML, args.CvsBXML, sha1_CvsBXML))
mva = {
   'CvsL' : FunctorFromMVA('c_vs_l', '%s/src/RecoBTag/CTagging/data/c_vs_udsg.weight.xml' % os.environ['CMSSW_BASE']),
   'CvsB' : FunctorFromMVA('c_vs_b', '%s/src/RecoBTag/CTagging/data/c_vs_b.weight.xml' % os.environ['CMSSW_BASE'])
}
njets = {}
def get_info(pset):
   ret = {
      'entry_name' : pset.taggingVarName.value(),
      'default' : pset.default.value(),
      }
   if hasattr(pset, 'idx'):
      ret['idx'] = pset.idx.value()
   return ret

mva_vars_info = dict((i.name.value(), get_info(i)) for i in get_vars('%s/src/RecoBTag/CTagging/data/c_vs_udsg.weight.xml' % os.environ['CMSSW_BASE']))

#import jets for flat trees
jet_maps = {}
with open('flat_jet_map') as infile:
   jet_maps = pickle.load(infile)

infile_names = glob('training_trees/CombinedSV*.root')
vtx_categories = {
   'CombinedSVNoVertexNoSoftLepton' : 2,
   'CombinedSVPseudoVertexNoSoftLepton' : 1,
   'CombinedSVRecoVertexNoSoftLepton' : 0,

   'CombinedSVNoVertexSoftElectron' : 8,
   'CombinedSVPseudoVertexSoftElectron' : 7,
   'CombinedSVRecoVertexSoftElectron' : 6,
from RecoBTag.CTagging.helpers import get_vars
import math
import rootpy.io as io
import pickle
import os

mva_vars_cl = set([i.name.value() for i in get_vars('%s/src/RecoBTag/CTagging/data/c_vs_udsg.weight.xml' % os.environ['CMSSW_BASE'])])
mva_vars_cb = set([i.name.value() for i in get_vars('%s/src/RecoBTag/CTagging/data/c_vs_b.weight.xml' % os.environ['CMSSW_BASE'])])
mva_vars_cl.update(mva_vars_cb)
mva_vars_cl.add('vertexCategory')
#automatic set mins and maxes
mms_vars = dict((i, (float('inf'), float('-inf'))) for i in mva_vars_cl)


debug_CvsL_file = io.root_open('trees/ctag_debug_CvsL.root')
debug_CvsL_tree = debug_CvsL_file.tree
for entry in debug_CvsL_tree:
   for name, minmax in mms_vars.iteritems():
      min_val, max_val = minmax
      val = getattr(entry, name)
      min_val = min(min_val, val)
      max_val = max(max_val, val)
      mms_vars[name] = (min_val, max_val)


flat_file = io.root_open('trees/CombinedSV_ALL.root')
flat_tree = flat_file.tree
for entry in debug_CvsL_tree:
   for name, minmax in mms_vars.iteritems():
      min_val, max_val = minmax
      val = getattr(entry, name)
                    help='XML Weight file of the Charm vs B training')
args = parser.parse_args()

if not os.path.isfile(args.CvsLXML):
    raise ValueError('File %s does not exists!' % args.CvsLXML)
if not os.path.isfile(args.CvsBXML):
    raise ValueError('File %s does not exists!' % args.CvsBXML)

with open(args.CvsLXML) as CvsLXML:
    sha1_CvsLXML = hashlib.sha1(CvsLXML.read()).hexdigest()

with open(args.CvsBXML) as CvsBXML:
    sha1_CvsBXML = hashlib.sha1(CvsBXML.read()).hexdigest()

CvsLXML_vpset = cms.VPSet(*get_vars(
    args.CvsLXML, False
)  #False: do not mimick FileInPath functionalities and take the bare path as the file path
                          )
CvsBXML_vpset = cms.VPSet(*get_vars(args.CvsBXML, False))

with open(
        '%s/src/RecoBTag/CTagging/python/training_settings.py' %
        os.environ['CMSSW_BASE'], 'w') as output:
    output.write('import FWCore.ParameterSet.Config as cms\n')
    output.write('''
## IMPORTANT!
## This file was automatically generated by RecoBTag/CTagging/test/dump_training_vars_cfg.py
## with input xml files:
##    - C vs L: %s sha1 checksum: %s
##    - C vs B: %s sha1 checksum: %s