Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions utils/generateVariableSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
parser = optparse.OptionParser(usage="%prog [options]")
parser.add_option("-f","--file",dest="file",metavar="FILE",
help="ntuple file to check variables")
parser.add_option("-n",dest="number_of_indices",metavar="NINDICES",default=4,
help="determine how many indices should be considered")
parser.add_option("-n",dest="number_of_indices",metavar="NINDICES",default=0,
help="determine how many indices should be considered,\n\tif <1: jets and tags of the category are used")
parser.add_option("--generateSet",action="store_true",dest="generateSet",default=False,
help="generate printout for generating a variable_set.py file")
parser.add_option("-o",dest="outputfile",
Expand All @@ -20,19 +20,22 @@
parser.error("need to specify file")
opts.number_of_indices = int(opts.number_of_indices)


numbers = []
for i in range(10):
numbers.append(str(i))

def getAllVariables(infile):
f = ROOT.TFile(infile)
t = f.Get("MVATree")
variables = []
vetos = ["Weight", "HTXS", "Gen", "Prescale",
"Electron", "Muon", "AK8",
"Triggered", "DeepFlavour",
"common5_output", "Untagged",
"Electron", "Muon", "AK8", "DeepCSV",
"Triggered", "DeepFlavour", "RecoTTH",
"common5_output", "Untagged", "Flav",
"CSV_DNN", "PileUp", "Evt_ID",
"Evt_Lumi", "Evt_Odd", "Evt_Run",
"PartonFlav", "GoodTags",]
"PartonFlav", "GoodTags", "HEM",
"DeepJet_", "Forward", "Loose",]
for b in list(t.GetListOfBranches()):
ignore = False
for veto in vetos:
Expand All @@ -42,19 +45,33 @@ def getAllVariables(infile):
return variables


def figureOutVectors(variables, infile):
def figureOutVectors(variables, infile, category):
if opts.number_of_indices < 1:
for i,c in enumerate(category):
if c in numbers and category[i+1]=="t":
n_tags = int(c)
print("number of jets:\t{}".format(n_tags))
if c in numbers and category[i+1]=="j":
n_jets = int(c)
print("number of tags:\t{}".format(n_jets))
else:
n_jets = opts.number_of_indices
n_tags = opts.number_of_indices

new_variables = []
with uproot.open(infile) as f:
tree = f["MVATree"]

for v in variables:
print("looking at variable: {}".format(v))
#print("looking at variable: {}".format(v))
df = tree.pandas.df([v])
if "subentry" in df.index.names:
if "LooseLepton" in v:
if "Lepton" in v:
new_variables += [v+"[0]"]
elif "Tagged" in v:
new_variables += [v+"[{}]".format(i) for i in range(n_tags)]
elif "Jet" in v or "CSV" in v:
new_variables += [v+"[{}]".format(i) for i in range(opts.number_of_indices)]
new_variables += [v+"[{}]".format(i) for i in range(n_jets)]
else:
print("vector variable {} did not match name query".format(v))
else:
Expand All @@ -66,8 +83,9 @@ def figureOutVectors(variables, infile):
def generateVariableSet(variables, categories, path):
out = "variables = {}\n"
for cat in categories:
newVars = figureOutVectors(variables, opts.file, cat)
out += "variables[\"{}\"] = [\n".format(cat)
for v in variables:
for v in newVars:
out += " '{}',\n".format(v)
out += " ]\n\n"

Expand All @@ -83,9 +101,9 @@ def generateVariableSet(variables, categories, path):


variables = getAllVariables(opts.file)
new_variables = figureOutVectors(variables, opts.file)
#new_variables = figureOutVectors(variables, opts.file)
if opts.generateSet:
generateVariableSet(new_variables, opts.jtregions.split(","), opts.outputfile)
generateVariableSet(variables, opts.jtregions.split(","), opts.outputfile)
else:
print("variables:")
for v in variables: print(v)
Expand Down
Loading