Exemplo n.º 1
0
    def do(self, args):

        # Skipping empty args
        if len(args) == 0:
            logging.getLogger('MA5').error("wrong syntax")
            self.help()
            return

        foundArguments = False
        foundRegions = False
        foundOptions = False
        foundConditions = False

        # 1. First check : counting number of braces ( ) and [ ]
        # 2. Detecting if there is argument or option
        endArguments = -1
        beginOptions = -1
        beginRegions = -1
        Nbracket1 = 0
        Nbracket2 = 0
        Nbracket3 = 0
        for i in range(0, len(args)):
            if args[i] == '(':
                Nbracket1 += 1
                if i == 0:
                    foundArguments = True
            elif args[i] == ')':
                Nbracket1 -= 1
                endArguments = i
            elif args[i] == '[':
                Nbracket2 += 1
                if Nbracket1 == 0:
                    beginOptions = i
                    foundOptions = True
            elif args[i] == '{':
                Nbracket3 += 1
                beginRegions = i
                foundRegions = True
            elif args[i] == '}':
                Nbracket3 -= 1
            elif args[i] == ']':
                Nbracket2 -= 1

        if Nbracket1 != 0:
            logging.getLogger('MA5').error("number of opening brackets '(' and number of " +\
                          "closing brakets ')' does not match.")
            return
        if Nbracket2 != 0:
            logging.getLogger('MA5').error("number of opening squared-brackets '[' and number of " +\
                          "closing squared-brakets ']' does not match.")
            return

        if Nbracket3 != 0:
            logging.getLogger('MA5').error("number of opening curly-brackets '{' and number of " +\
                          "closing curly-brakets '}' does not match.")
            return

        if not foundOptions:
            beginOptions = len(args)

        # Is there candidate ?
        # Candidate : ( A B C ... ) with A, B, C, ... are not observables
        foundCandidate = False
        endCandidate = -1
        if args[0] == '(':
            Nbracket1 = 1
            nObservables = 0
            for i in range(1, len(args)):
                if args[i] == '(':
                    Nbracket1 += 1
                elif args[i] == ')':
                    Nbracket1 -= 1
                elif args[i] in self.main.observables.full_list:
                    nObservables += 1
                if Nbracket1 == 0:
                    if nObservables == 0:
                        foundCandidate = True
                        endCandidate = i
                    break

        # Extracting the possible candidate
        if foundCandidate:
            parts = self.extract_particle(args[1:endCandidate])
            if parts == None:
                return
            if '[' in parts.GetStringDisplay(
            ) and ']' in parts.GetStringDisplay():
                self.logger.error('Object definitions can only be applied on classes of objects, '+\
                   'and not on a specific object (like the leading muon)')
                return
        else:
            parts = ParticleObject()
        # Extracting the conditions
        if foundRegions:
            endCondition = beginRegions
        else:
            endCondition = beginOptions

        if beginRegions != -1 and beginOptions != -1:
            if beginOptions < beginRegions:
                self.logger.error(
                    'Cut declaration: regions should be declared before the options'
                )
                return

        condition = ConditionSequence(mother=True)
        if foundCandidate:
            argType = ArgumentType.PARTICLE
            for part in parts:
                if len(part) > 1:
                    argType = ArgumentType.COMBINATION


            result = self.extract_sequence(condition,\
                                           args[endCandidate+1:endCondition],\
                                           partType=argType)
        else:
            result = self.extract_sequence(condition,\
                                           args[:endCondition],\
                                           partType=None)
        if result == None:
            return

        # Extracting regions
        if foundRegions:
            CutRegionNames = args[beginRegions + 1:beginOptions - 1]
            # checking all regions exist
            if [
                    reg for reg in CutRegionNames
                    if reg not in self.main.regions.GetNames()
            ] != []:
                self.logger.error(
                    'Cut trying to be attached to a non-existing region')
                return
        else:
            CutRegionNames = self.main.regions.GetNames()

        if CutRegionNames == []:
            CutRegionNames = self.main.regions.GetNames()

        # Creating cut
        if not foundCandidate:
            cut = Cut(parts, condition, self.cut_type, regions=CutRegionNames)
            # Setting options
            if foundOptions:
                if not self.extract_options(
                        cut, args[beginOptions + 1:len(args) - 1]):
                    return
            # Adding the cut to selection
            self.main.selection.Add(cut)
        else:
            setofRegions = self.main.regions.GetClusteredRegions(
                self.main.selection)
            if (setofRegions == [[]] and CutRegionNames == []) or (list(
                    set(CutRegionNames)) in setofRegions):
                cut = Cut(parts,
                          condition,
                          self.cut_type,
                          regions=CutRegionNames)
                # Setting options
                if foundOptions:
                    if not self.extract_options(
                            cut, args[beginOptions + 1:len(args) - 1]):
                        return
                # Adding the cut to selection
                self.main.selection.Add(cut)
            else:
                self.logger.warning(
                    '   Object definition to be attached to distinguishable regions -> multiple declaration:'
                )
                for myset in setofRegions:
                    subCutRegionNames = [
                        x for x in CutRegionNames if x in myset
                    ]
                    if subCutRegionNames == []:
                        continue
                    cut = Cut(parts,
                              condition,
                              self.cut_type,
                              regions=subCutRegionNames)
                    # Getting options
                    if foundOptions:
                        if not self.extract_options(
                                cut, args[beginOptions + 1:len(args) - 1]):
                            return
                    # Adding the cut to selection
                    self.main.selection.Add(cut)
                    title = cut.GetStringDisplay()
                    if ', regions' in title:
                        title = title[:title.find(', regions')]
                    self.logger.warning(title + ' { ' +
                                        ' '.join(subCutRegionNames) + ' }')
Exemplo n.º 2
0
    def do(self, args):

        # Skipping empty args
        if len(args) == 0:
            logging.getLogger('MA5').error("wrong syntax")
            self.help()
            return

        foundArguments = False
        foundRegions = False
        foundOptions = False
        foundConditions = False

        # 1. First check : counting number of braces ( ) and [ ]
        # 2. Detecting if there is argument or option
        endArguments = -1
        beginOptions = -1
        beginRegions = -1
        Nbracket1 = 0
        Nbracket2 = 0
        Nbracket3 = 0
        for i in range(0, len(args)):
            if args[i] == '(':
                Nbracket1 += 1
                if i == 0:
                    foundArguments = True
            elif args[i] == ')':
                Nbracket1 -= 1
                endArguments = i
            elif args[i] == '[':
                Nbracket2 += 1
                beginOptions = i
            elif args[i] == '{':
                Nbracket3 += 1
                beginRegions = i
            elif args[i] == '}':
                Nbracket3 -= 1
                if i == (len(args) - 1) or '[' in args[i + 1:]:
                    foundRegions = True
            elif args[i] == ']':
                Nbracket2 -= 1
                if i == (len(args) - 1) or '{' in args[i + 1:]:
                    foundOptions = True

        if Nbracket1 != 0:
            logging.getLogger('MA5').error("number of opening-bracket '(' and number of " +\
                          "closing-braket ')' does not match.")
            return
        if Nbracket2 != 0:
            logging.getLogger('MA5').error("number of opening-bracket '[' and number of " +\
                          "closing-braket ']' does not match.")
            return

        if Nbracket3 != 0:
            logging.getLogger('MA5').error("number of opening-bracket '{' and number of " +\
                          "closing-braket '}' does not match.")
            return

        if not foundOptions:
            beginOptions = len(args)

        # Is there candidate ?
        # Candidate : ( A B C ... ) with A, B, C, ... are not observables
        foundCandidate = False
        endCandidate = -1
        if args[0] == '(':
            Nbracket1 = 1
            nObservables = 0
            for i in range(1, len(args)):
                if args[i] == '(':
                    Nbracket1 += 1
                elif args[i] == ')':
                    Nbracket1 -= 1
                elif args[i] in self.main.observables.full_list:
                    nObservables += 1
                if Nbracket1 == 0:
                    if nObservables == 0:
                        foundCandidate = True
                        endCandidate = i
                    break

        # Extracting the possible candidate
        if foundCandidate:
            parts = self.extract_particle(args[1:endCandidate])
            if parts == None:
                return
        else:
            parts = ParticleObject()

        # Extracting the conditions
        condition = ConditionSequence(mother=True)
        if foundCandidate:
            argType = ArgumentType.PARTICLE
            for part in parts:
                if len(part) > 1:
                    argType = ArgumentType.COMBINATION

            result = self.extract_sequence(condition,\
                                           args[endCandidate+1:beginOptions],\
                                           partType=argType)
        else:
            result = self.extract_sequence(condition,\
                                           args[:beginOptions],\
                                           partType=None)
        if result == None:
            return

        # Creating cut
        cut = Cut(parts, condition, self.cut_type)

        # Setting options
        if foundOptions:
            if not self.extract_options(cut,
                                        args[beginOptions + 1:len(args) - 1]):
                return

        # Adding the cut to selection
        self.main.selection.Add(cut)
Exemplo n.º 3
0
    def extract_particle(self, words):

        # Checking first and end position
        if words[0] == "and" or words[-1] == "and":
            self.logger.error("the reserved word 'and' is incorrectly used.")
            return
        elif words[0] == "[" or words[-1] == "[":
            self.logger.error("incorrect use of the opening bracket '['.")
            return
        elif words[0] == "]":
            self.logger.error("incorrect use of the closing bracket ']'.")
            return
        elif words[0] == "<" or words[-1] == "<":
            self.logger.error("incorrect use of the '<' character.")
            return

        # Creating ParticleObject
        ALLmode = False
        parts = []
        mothers = []
        object = ParticleObject()
        PTrankMode = 0
        motherMode = 0
        nBrackets = 0
        for item in words:

            # Common part
            if item == "(":
                nBrackets += 1
            elif item == ")":
                nBrackets -= 1
                if nBrackets < 0:
                    self.logger.error("problem with brackets () : too much " +\
                                  "more closing-brackets.")
                    return

            # PT rank part
            elif PTrankMode > 0:
                if PTrankMode == 1:
                    if len(parts) == 0:
                        self.logger.error("PT rank applied to no particle or " +\
                                      "multiparticle.")
                        return
                    if item == "0":
                        self.logger.error("PT rank cannot be equal to 0. " +\
                                      "The first PT rank is 1.")
                        return
                    try:
                        thePTrank = int(item)
                    except:
                        self.logger.error("PT rank '" + item +
                                          "' is not valid")
                        return
                    if len(mothers) == 0:
                        if parts[-1].PTrank != 0:
                            self.logger.error("You cannot specify several PT " +\
                                          "ranks to '"+parts[-1].particle.name+\
                                          "'")
                            return
                        parts[-1].PTrank = thePTrank
                    else:
                        if mothers[-1].PTrank != 0:
                            self.logger.error("You cannot specify several PT " +\
                                          "ranks to '"+mothers[-1].particle.name+\
                                          "'")
                            return
                        mothers[-1].PTrank = thePTrank
                    PTrankMode = 2
                elif PTrankMode == 2:
                    if item != "]":
                        self.logger.error("closing-bracket ']' is expected "+\
                                      "instead of '" + item + "'")
                        return
                    PTrankMode = 0

            # Mother part
            elif motherMode > 0:
                if item == "<":
                    if motherMode != 1:
                        self.logger.error("too much number of '<' character")
                        return
                    motherMode = 2
                elif self.main.multiparticles.Find(item):
                    theMother = ExtraParticle(\
                                    self.main.multiparticles.Get(item))
                    if len(mothers) == 0:
                        parts[-1].mumPart = theMother
                        if motherMode == 1:
                            parts[-1].mumType = "<"
                        else:
                            parts[-1].mumType = "<<"
                    else:
                        mothers[-1].mumPart = theMother
                        if motherMode == 1:
                            mothers[-1].mumType = "<"
                        else:
                            mothers[-1].mumType = "<<"

                    mothers.append(theMother)
                    motherMode = 0
                else:
                    self.logger.error("'"+item+"' is not a defined "+\
                                  "(multi)particle.")
                    return

            # Normal mode
            elif item == "and":
                if ALLmode and len(parts) > 1:
                    self.logger.error(
                        "Reversed word 'all' must be applied in front of only (multi)particle"
                    )
                    return
                object.Add(parts, ALLmode)
                parts = []
                mothers = []
                ALLmode = False
            elif item == "all":
                if len(parts) != 0:
                    self.logger.error(
                        "Reserved word 'all' must be applied in front of a (multi)particle"
                    )
                    return
                ALLmode = True
            elif item == "[":
                if PTrankMode != 0:
                    self.logger.error(
                        "You cannot specify several PT rank '[]'")
                    return
                PTrankMode = 1
            elif item == "<":
                # Should not occur
                if motherMode != 0:
                    self.logger.error("problem with character '<'")
                    return
                motherMode = 1
            elif self.main.multiparticles.Find(item):
                parts.append(ExtraParticle(self.main.multiparticles.Get(item)))
                mothers = []
            else:
                self.logger.error("'" + item + "' is not a defined " +
                                  "(multi)particle.")
                return

        # End
        if nBrackets > 0:
            self.logger.error("problem with brackets () : too much " +\
                                  "more opening-brackets")
            return

        if len(parts) != 0:
            if ALLmode and len(parts) > 1:
                self.logger.error(
                    "Reversed word 'all' must be applied in front of only (multi)particle"
                )
                return
            object.Add(parts, ALLmode)
        return object