#! /usr/bin/python

# Usage: addBranch el_std_sieie F el_std_n MAX_ELECTRONS
# Usage: addBranch el_std_n I
# Uasge: addBranch el_std_pfiso_charged std::vector<float> 

import sys

name = sys.argv[1]
type = sys.argv[2]
if (len(sys.argv) > 3):
    index = sys.argv[3]
    limit = sys.argv[4]

file = open("branchdef/branchdef.h", "a")
file.write("TBranch* b_" + name + ";\n")
file.close()

file = open("branchdef/branchdict.h", "a")
file.write("BRANCH_DICT(" + name + ");\n")
file.close()

file = open("branchdef/getbranch.h", "a")
file.write("b_" + name + " = fChain->GetBranch(\"" + name + "\");\n")
file.close()

#######file = open("branchdef/getentry.h", "a")
#######file.write("b_" + name + "->GetEntry(jentry);\n")
#######file.close()

file = open("branchdef/setbranchaddress.h", "a")
file.write("void SetBranchAddress_" + name + "(TTree* tree) { tree->SetBranchAddress(\"" + name + "\", &" + name + ", &b_" + name + "); };\n")
file.close()

file = open("branchdef/treebranch.h", "a")
if ("std::" in type):
    file.write("void Branch_" + name + "(TTree* tree) { tree->Branch(\"" + name + "\", \"" + type + "\", &" + name + "); };\n")
else:
    file.write("void Branch_" + name + "(TTree* tree) { tree->Branch(\"" + name + "\", &" + name + ", \"" + name + "[" + index + "]/" + type[0] + "\"); };\n")
file.close()

file = open("branchdef/treedef.h", "a")
if ("std::" in type):
    file.write(type + "* " + name + ";\n")
elif (type == "F"):
    file.write("Float_t " + name + "[" + limit + "];\n")
elif (type == "I"):
    file.write("Int_t " + name + "[" + limit + "];\n")
elif (type == "O"):
    file.write("Bool_t " + name + "[" + limit + "];\n")
else:
    file.write(type + " " + name + "[" + limit + "];\n")
file.close()

