def setUp(self):
        glb.function_map.update(globals())
        x = Array([1,2,3,4])
        obj = variable('x', x)
        glb.variable_stack.append(['x',obj])

        y = Array([6,7,8,9])
        obj = variable('y', y)
        glb.variable_stack.append(['y',obj])
示例#2
0
    def run(self):
        from visualize.plot import resetFlagInStack
        from utils.recursive import recursive
        from utils.executor import containVariableInGlbStack, getVariableFromGlbStack

        glb.variable_stack.append(self)
        resetFlagInStack()

        #insert iterate variable inside global variable stack
        iterVarObj = variable(self.iter_var, None)
        glb.variable_stack.append([self.iter_var, iterVarObj])
        setPointer = False #whether we should treat iter_var as a pointer

        var_being_iter = getVariableFromGlbStack(self.range_var_name)

        if self.range_var_name and containVariableInGlbStack(self.range_var_name):# for x in range_var_name
            setPointer = True

        #navigates to the next line -- start of the content
        self.line = glb.current_line + 1

        #start the loop
        for index, value in enumerate(self.loop_range):
            #check end_recursive flag
            if self.end_recursive:
                break

            iterVarObj.var_obj = value
            iterVarObj.var_flag = glb.flag_dict['changed']

            if setPointer:
                iterVarObj.pointerIndexList = [index]
                iterVarObj.pointer = getVariableFromGlbStack(self.range_var_name).var_name

            if var_being_iter:
                #update index of this variable
                var_being_iter.indexList = [index]

            # printVar() #do not need to specially call printVar for iter_var
            recursive(self.content, 0, self)

            #clear flags after one loop
            resetFlagInStack()

            if self.continue_flag:
                self.resetEnd()
                self.resetContinue()

        self._end_module()
    def __call__(self, *args, **kwargs):
        from visualize.plot import resetFlagInStack
        from utils.variable import variable
        from utils.executor import isPrimitiveType, getMatchingObject
        from utils.recursive import recursive

        #increase function call depth by one
        glb.funcall_depth += 1

        glb.variable_stack.append(self)
        resetFlagInStack()

        try:
            if len(args) + len(kwargs) != len(self.param_list):
                raise TypeError('{} positional arguments but {} given'.format(
                    len(self.param_list),
                    len(args) + len(kwargs)))
            else:
                from itertools import zip_longest

                #combine args with values of kwargs
                args = list(args) + list(kwargs.values())

                for param, arg in zip_longest(self.param_list, args):
                    #insert parameters into global variable stack and store pointer.
                    #TODO:currently we only deal with single variable pointer. In other words, paramaters like x[1] are not handled.
                    paramVarObj = variable(param, arg)

                    #check whether variables are primitive type
                    if not isPrimitiveType(arg):
                        findObj = getMatchingObject(arg)
                        paramVarObj.pointer = findObj.var_name

                    glb.variable_stack.append([param, paramVarObj])

                #printVar()

                recursive(self.content, 0, self)

                #return function return value
                return self.return_list
        except Exception as e:
            raise Exception(
                "Exception: \"{}\"  occurred during execution of function {}".
                format(e, self.func_name))
        finally:
            self._end_module()
            glb.funcall_depth -= 1
    def __call__(self, *args, **kwargs):
        from visualize.plot import resetFlagInStack
        from utils.variable import variable
        from utils.executor import isPrimitiveType, getMatchingObject
        from utils.recursive import recursive

        #increase function call depth by one
        glb.funcall_depth += 1

        glb.variable_stack.append(self)
        resetFlagInStack()

        try:
            if len(args) + len(kwargs) != len(self.param_list):
                raise TypeError('{} positional arguments but {} given'
                                .format(len(self.param_list), len(args)+len(kwargs)))
            else:
                from itertools import zip_longest

                #combine args with values of kwargs
                args = list(args) + list(kwargs.values())

                for param, arg in zip_longest(self.param_list, args):
                    #insert parameters into global variable stack and store pointer.
                    #TODO:currently we only deal with single variable pointer. In other words, paramaters like x[1] are not handled.
                    paramVarObj = variable(param, arg)

                    #check whether variables are primitive type
                    if not isPrimitiveType(arg):
                        findObj = getMatchingObject(arg)
                        paramVarObj.pointer = findObj.var_name

                    glb.variable_stack.append([param, paramVarObj])

                #printVar()

                recursive(self.content, 0, self)

                #return function return value
                return self.return_list
        except Exception as e:
            raise Exception("Exception: \"{}\"  occurred during execution of function {}".format(e, self.func_name))
        finally:
            self._end_module()
            glb.funcall_depth -= 1
示例#5
0
def execute(statement):
    '''
    :param statement: statement to be executed
    :return the value returned by the statement if any, otherwise return None
    New variables will be added to variable stack.

    Expression type: 1) variable definition 2) if, for, while statement's conditions 3) pure expression

    **********
    For right operand, we could invoke evaluate function directly.
    For left operand, we could invoke evaluate function for the parameters and indices.
    However we can not use evaluate function for the entire left operand because if the returned object of left operand is a string type,
    we can not change the object value by simply assign the right operand value to it.
    Eg. x = 4
    left = evaluate(x)
    right = evaluate(4)
    left = right
    x is not changed!
    '''
    tokenList = lexical_analyze(statement)

    #statement is an assignment. Left operand must be a variable
    #Three types of left operand:
    #1. object -> Need to store new variable to stack
    #2. object.function()
    #3. object[index/key] -> Need to record the index or key

    #if it is an assignment
    if equalToken in tokenList:
        indexOfEqual = tokenList.index(equalToken)
        leftOpTokens = tokenList[:indexOfEqual]
        rightOpTokens = tokenList[indexOfEqual + 1:]

        #get string expression of right operand
        rightOpStr = ''
        for token in rightOpTokens:
            rightOpStr += token[1] + ' '

        #the object of the left operand
        leftObjectName = leftOpTokens[0][1]

        #variable is not defined yet
        if not containVariableInGlbStack(leftObjectName):
            #second or third type of left operand
            if len(leftOpTokens) > 1:
                raise Exception("\"" + leftObjectName +
                                "\" is undefined in statement: " + statement)
            #first type of left operand
            else:
                rightOpValue = evaluate(rightOpStr)
                variableObj = variable(leftObjectName, rightOpValue)
                glb.variable_stack.append([leftObjectName, variableObj])

        #variable is defined previously
        else:
            variableInstance = getVariableFromGlbStack(
                leftObjectName)  #get variable from global stack
            rightOpValue = evaluate(
                rightOpStr)  #evaluate the right operand and get the value
            variableInstance.var_flag = glb.flag_dict['changed']

            #single variable, store in the global stack directly
            if len(leftOpTokens) == 1:
                variableInstance.var_obj = rightOpValue
            else:
                #analyze token before ".", find indices visited. Format: obj[index/key]...[index/key]
                leftOpObject = variableInstance.var_obj
                storeVisitedIndex(leftOpTokens)

                #compile until the last "." or "[".  To split the left operand into 2 parts, the first part is non-primitive and second part is primitive part

                #find the index of last '.' OR '['
                index = 0
                splitIndex = 0
                while index < len(leftOpTokens):
                    if leftOpTokens[index][1] == '.' or leftOpTokens[index][
                            1] == '[':
                        splitIndex = index
                    index += 1

                index = 1  #reset index
                exp = ''
                token = ''  #single token
                currentObject = leftOpObject  #object compiled until now

                while index < len(leftOpTokens):
                    token = leftOpTokens[index][1]
                    #attribute or function call
                    if token == '.':
                        index += 1
                        token = leftOpTokens[index][1]
                        if token not in dir(currentObject):
                            raise Exception("AttributeError: \'" +
                                            leftObjectName +
                                            "\' object has no attribute \'" +
                                            token + "\'")
                        else:
                            if index >= splitIndex:
                                setattr(currentObject, token, rightOpValue)
                            else:
                                currentObject = getattr(currentObject, token)
                    #function call
                    elif token == '(':
                        bracket_stack = []
                        bracket_stack.append(token)
                        isMultipleParam = False

                        while index < len(leftOpTokens) - 1 and bracket_stack:
                            index += 1
                            token = leftOpTokens[index][1]
                            #check if it is multiple parameters
                            if token == ',' and len(bracket_stack) == 1:
                                isMultipleParam = True

                            if token == '(':
                                bracket_stack.append(token)
                            elif token == ')':
                                bracket_stack.pop()
                            if bracket_stack:
                                exp += token
                        paramResult = evaluate(exp)

                        if isMultipleParam:
                            currentObject = currentObject(
                                *paramResult
                            )  #execute the function, paramResult may be a tuple with multiple parameters
                        else:
                            currentObject = currentObject(paramResult)
                    #indices or keys
                    elif token == '[':
                        bracket_stack = []
                        bracket_stack.append(token)

                        while index < len(leftOpTokens) - 1 and bracket_stack:
                            index += 1
                            token = leftOpTokens[index][1]
                            if token == '[':
                                bracket_stack.append(token)
                            elif token == ']':
                                bracket_stack.pop()
                            if bracket_stack:
                                exp += token
                        paramResult = evaluate(exp)
                        if index >= splitIndex:
                            currentObject[paramResult] = rightOpValue
                        else:
                            currentObject = currentObject[
                                paramResult]  #execute the function
                    else:
                        raise Exception("Invalid syntax at " + token +
                                        " in statement " + statement)
                    token = ''
                    exp = ''
                    index += 1

        storePointer(leftOpTokens, rightOpTokens)
    else:
        evaluate(statement)
示例#6
0
def execute(statement):
    """
    :param statement: statement to be executed
    :return the value returned by the statement if any, otherwise return None
    New variables will be added to variable stack.

    Expression type: 1) variable definition 2) if, for, while statement's conditions 3) pure expression

    **********
    For right operand, we could invoke evaluate function directly.
    For left operand, we could invoke evaluate function for the parameters and indices.
    However we can not use evaluate function for the entire left operand because if the returned object of left operand is a string type,
    we can not change the object value by simply assign the right operand value to it.
    Eg. x = 4
    left = evaluate(x)
    right = evaluate(4)
    left = right
    x is not changed!
    """
    tokenList = lexical_analyze(statement)

    # statement is an assignment. Left operand must be a variable
    # Three types of left operand:
    # 1. object -> Need to store new variable to stack
    # 2. object.function()
    # 3. object[index/key] -> Need to record the index or key

    # if it is an assignment
    if equalToken in tokenList:
        indexOfEqual = tokenList.index(equalToken)
        leftOpTokens = tokenList[:indexOfEqual]
        rightOpTokens = tokenList[indexOfEqual + 1 :]

        # get string expression of right operand
        rightOpStr = ""
        for token in rightOpTokens:
            rightOpStr += token[1] + " "

        # the object of the left operand
        leftObjectName = leftOpTokens[0][1]

        # variable is not defined yet
        if not containVariableInGlbStack(leftObjectName):
            # second or third type of left operand
            if len(leftOpTokens) > 1:
                raise Exception('"' + leftObjectName + '" is undefined in statement: ' + statement)
            # first type of left operand
            else:
                rightOpValue = evaluate(rightOpStr)
                variableObj = variable(leftObjectName, rightOpValue)
                glb.variable_stack.append([leftObjectName, variableObj])

        # variable is defined previously
        else:
            variableInstance = getVariableFromGlbStack(leftObjectName)  # get variable from global stack
            rightOpValue = evaluate(rightOpStr)  # evaluate the right operand and get the value
            variableInstance.var_flag = glb.flag_dict["changed"]

            # single variable, store in the global stack directly
            if len(leftOpTokens) == 1:
                variableInstance.var_obj = rightOpValue
            else:
                # analyze token before ".", find indices visited. Format: obj[index/key]...[index/key]
                leftOpObject = variableInstance.var_obj
                storeVisitedIndex(leftOpTokens)

                # compile until the last "." or "[".  To split the left operand into 2 parts, the first part is non-primitive and second part is primitive part

                # find the index of last '.' OR '['
                index = 0
                splitIndex = 0
                while index < len(leftOpTokens):
                    if leftOpTokens[index][1] == "." or leftOpTokens[index][1] == "[":
                        splitIndex = index
                    index += 1

                index = 1  # reset index
                exp = ""
                token = ""  # single token
                currentObject = leftOpObject  # object compiled until now

                while index < len(leftOpTokens):
                    token = leftOpTokens[index][1]
                    # attribute or function call
                    if token == ".":
                        index += 1
                        token = leftOpTokens[index][1]
                        if token not in dir(currentObject):
                            raise Exception(
                                "AttributeError: '" + leftObjectName + "' object has no attribute '" + token + "'"
                            )
                        else:
                            if index >= splitIndex:
                                setattr(currentObject, token, rightOpValue)
                            else:
                                currentObject = getattr(currentObject, token)
                    # function call
                    elif token == "(":
                        bracket_stack = []
                        bracket_stack.append(token)
                        isMultipleParam = False

                        while index < len(leftOpTokens) - 1 and bracket_stack:
                            index += 1
                            token = leftOpTokens[index][1]
                            # check if it is multiple parameters
                            if token == "," and len(bracket_stack) == 1:
                                isMultipleParam = True

                            if token == "(":
                                bracket_stack.append(token)
                            elif token == ")":
                                bracket_stack.pop()
                            if bracket_stack:
                                exp += token
                        paramResult = evaluate(exp)

                        if isMultipleParam:
                            currentObject = currentObject(
                                *paramResult
                            )  # execute the function, paramResult may be a tuple with multiple parameters
                        else:
                            currentObject = currentObject(paramResult)
                    # indices or keys
                    elif token == "[":
                        bracket_stack = []
                        bracket_stack.append(token)

                        while index < len(leftOpTokens) - 1 and bracket_stack:
                            index += 1
                            token = leftOpTokens[index][1]
                            if token == "[":
                                bracket_stack.append(token)
                            elif token == "]":
                                bracket_stack.pop()
                            if bracket_stack:
                                exp += token
                        paramResult = evaluate(exp)
                        if index >= splitIndex:
                            currentObject[paramResult] = rightOpValue
                        else:
                            currentObject = currentObject[paramResult]  # execute the function
                    else:
                        raise Exception("Invalid syntax at " + token + " in statement " + statement)
                    token = ""
                    exp = ""
                    index += 1

        storePointer(leftOpTokens, rightOpTokens)
    else:
        evaluate(statement)
 def test_variable(self):
     from utils.variable import variable
     x = variable('a', None)
     attr1 = getattr(x, 'var_name')
     attr2 = getattr(x, 'void')
示例#8
0
 def test_variable(self):
     from utils.variable import variable
     x = variable('a', None)
     attr1 = getattr(x, 'var_name')
     attr2 = getattr(x, 'void')
def main():
    '''
  all_files_names = ['BcToJPsiMuMu_is_chic0_mu_merged.root',
                   'BcToJPsiMuMu_is_jpsi_tau_merged.root',
                   'BcToJPsiMuMu_is_chic1_mu_merged.root',
                   'BcToJPsiMuMu_is_psi2s_mu_merged.root',
                   'BcToJPsiMuMu_is_chic2_mu_merged.root',
                   #'BcToJPsiMuMu_is_psi2s_tau_merged.root',
                   'BcToJPsiMuMu_is_hc_mu_merged.root',
                   #'BcToJPsiMuMu_is_jpsi_3pi_merged.root',
                   'BcToJPsiMuMu_is_jpsi_hc_merged.root',
                   'HbToJPsiMuMu3MuFilter_ptmax_merged.root',
                   'BcToJPsiMuMu_is_jpsi_mu_merged.root',
                   'HbToJPsiMuMu_ptmax_merged.root',
                   #'BcToJPsiMuMu_is_jpsi_pi_merged.root'
                  ]
  '''
    input_dir = "/gpfs/ddn/srm/cms/store/user/garamire/ntuples/2021Mar23/"
    #inputFile = '/gpfs/ddn/srm/cms/store/user/garamire/ntuples/2021Mar23/data_ptmax_merged.root'
    inputFile = 'data/alldata_selected.root'
    inputFile_mc = 'data/mc_all.root'
    inputTree = 'BTo3Mu'
    common_cuts = " & ".join([preselection, pass_id])
    #data_df = read_root(inputFile, inputTree).query(common_cuts).copy()
    data_df = read_root(inputFile, inputTree)
    mc_df = read_root(inputFile_mc, inputTree)
    #mc_df = pd.concat([read_root(input_dir + sample, inputTree) for sample in all_files_names])
    cat_data = category("data", data_df, "trigger_dimuon0", [2.95, 3.25],
                        "Data", ROOT.kBlack, ROOT.kFullCircle, 1.0)
    cat_mc = category("mc", mc_df, "trigger_dimuon0", [2.95, 3.25], "MC",
                      ROOT.kRed, ROOT.kFullSquare, 1.0)

    var_list = [
        #variable("Q_sq", "Q^{2}", "Q^{2}", "GeV^{2}", 50, 0., 14.),
        #variable("m_miss_sq", "m_{miss}^{2}", "m_{miss}^{2}", "GeV^{2}", 60, 0., 10.),
        #variable("pt_var", "pt_var", "pt_var", "GeV", 50, 0., 50.),
        #variable("E_mu_star", "E^{*}", "E^{*}", "GeV", 60, 0., 3.),
        variable("pv_sv_dist", "Dist(pv,sv)", "Dist(pv,sv)", "cm", 60, 0.,
                 0.02),
        variable("Bpt", "p_{T}(#mu^{+}#mu^{-}#mu)", "p{T}(#mu^{+}#mu^{-}#mu)",
                 "GeV", 50, 0., 80.),
        variable("Bpx", "p_{x}(#mu^{+}#mu^{-}#mu)", "p{x}(#mu^{+}#mu^{-}#mu)",
                 "GeV", 50, 0., 80.),
        variable("Bpy", "p_{y}(#mu^{+}#mu^{-}#mu)", "p{y}(#mu^{+}#mu^{-}#mu)",
                 "GeV", 50, 0., 80.),
        variable("Bpz", "p_{z}(#mu^{+}#mu^{-}#mu)", "p{z}(#mu^{+}#mu^{-}#mu)",
                 "GeV", 50, 0., 80.),
        variable("kpx", "p_{x}(#mu_{3})", "p{x}(#mu_{3})", "GeV", 50, 0., 30.),
        variable("kpy", "p_{y}(#mu_{3})", "p{y}(#mu_{3})", "GeV", 50, 0., 30.),
        variable("kpz", "p_{z}(#mu_{3})", "p{z}(#mu_{3})", "GeV", 50, 0., 30.),
        variable("mu1px", "p_{x}(#mu_{1})", "p{x}(#mu_{1})", "GeV", 50, 0.,
                 30.),
        variable("mu1py", "p_{y}(#mu_{1})", "p{y}(#mu_{1})", "GeV", 50, 0.,
                 30.),
        variable("mu1pz", "p_{z}(#mu_{1})", "p{z}(#mu_{1})", "GeV", 50, 0.,
                 30.),
        variable("mu2px", "p_{x}(#mu_{2})", "p{x}(#mu_{2})", "GeV", 50, 0.,
                 30.),
        variable("mu2py", "p_{y}(#mu_{2})", "p{y}(#mu_{2})", "GeV", 50, 0.,
                 30.),
        variable("mu2pz", "p_{z}(#mu_{2})", "p{z}(#mu_{2})", "GeV", 50, 0.,
                 30.),
        #variable("Bmass", "m(#mu^{+}#mu^{-}#mu)", "m(#mu^{+}#mu^{-}#mu)", "GeV", 50, 3., 10.),
        #variable("Bmass", "m(#mu^{+}#mu^{-}#mu)", "m(#mu^{+}#mu^{-}#mu)", "GeV", 50, 3., 10.),
        variable("Beta", "#eta(#mu^{+}#mu^{-}#mu)", "#eta(#mu^{+}#mu^{-}#mu)",
                 "", 50, 2.5, 2.5),
        variable("Bphi", "#phi(#mu^{+}#mu^{-}#mu)", "#phi(#mu^{+}#mu^{-}#mu)",
                 "", 50, -3.2, 3.2),
        variable("jpsi_mass", "m(#mu^{+}#mu^{-})", "m(#mu^{+}#mu^{-})", "GeV",
                 50, 2.95, 3.25),
        variable("mu1pt", "p_{T}(#mu_{1})", "p_{T}(#mu_{1})", "GeV", 50, 0.,
                 30.),
        variable("mu1eta", "#eta(#mu_{1})", "#eta(#mu_{1})", "GeV", 50, -2.5,
                 2.5),
        variable("mu1phi", "#phi(#mu_{1})", "#phi(#mu_{1})", "GeV", 50, -3.2,
                 3.2),
        variable("mu2pt", "p_{T}(#mu_{2})", "p_{T}(#mu_{2})", "GeV", 50, 0.,
                 30.),
        variable("mu2eta", "#eta(#mu_{2})", "#eta(#mu_{2})", "GeV", 50, -2.5,
                 2.5),
        variable("mu2phi", "#phi(#mu_{2})", "#phi(#mu_{2})", "GeV", 50, -3.2,
                 3.2),
        variable("kpt", "p_{T}(#mu_{3})", "p_{T}(#mu_{3})", "GeV", 50, 0.,
                 30.),
        variable("keta", "#eta(#mu_{3})", "#eta(#mu_{3})", "GeV", 50, -2.5,
                 2.5),
        variable("kphi", "#phi(#mu_{3})", "#phi(#mu_{3})", "GeV", 50, -3.2,
                 3.2),
        #variable("pv_x", "Primary vertex x", "Primary vertex x", "cm", 50, -0.5, 0.5 ),
        #variable("pv_y", "Primary vertex y", "Primary vertex y", "cm", 50, -0.5, 0.5 ),
        variable("pv_z", "Primary vertex z", "Primary vertex z", "cm", 50,
                 -12., 12.),
        #variable("jpsivtx_vtx_x", "Vtx_x(#mu^{+}#mu^{-})", "Vtx_x(#mu^{+}#mu^{-})", "cm", 50, -0.5, 0.5 ),
        #variable("jpsivtx_vtx_y", "Vtx_y(#mu^{+}#mu^{-})", "Vtx_y(#mu^{+}#mu^{-})", "cm", 50, -0.5, 0.5 ),
        #variable("jpsivtx_vtx_z", "Vtx_z(#mu^{+}#mu^{-})", "Vtx_z(#mu^{+}#mu^{-})", "cm", 50, -12., 12. ),
        #variable("jpsivtx_vtx_ex", "Vtx_ex(#mu^{+}#mu^{-})", "Vtx_ex(#mu^{+}#mu^{-})", "cm", 50, 0., 0.03 ),
        #variable("jpsivtx_vtx_ey", "Vtx_ey(#mu^{+}#mu^{-})", "Vtx_ey(#mu^{+}#mu^{-})", "cm", 50, 0., 0.03 ),
        #variable("jpsivtx_vtx_ez", "Vtx_ez(#mu^{+}#mu^{-})", "Vtx_ez(#mu^{+}#mu^{-})", "cm", 50, 0., 0.1 ),
    ]

    plotComparisonByCats([cat_data, cat_mc], var_list, "jpsiregion_dimuon0",
                         True)