ATLAS Offline Software
Loading...
Searching...
No Matches
python.pydraw.Draw_Cmd Class Reference
Inheritance diagram for python.pydraw.Draw_Cmd:
Collaboration diagram for python.pydraw.Draw_Cmd:

Public Member Functions

 __init__ (self, s)

Public Attributes

str errstr = None
 excstr = traceback.format_exc()
list histspec
 tuple_o = eval (self.tuple, _globals)
list stmts = _split_outer (s, '@')
 expr_orig
 sel_orig
 sel = self._mung_expr (self.sel_orig)
list exprs
 tuple = tuple
 lo = lo
 hi = hi

Protected Member Functions

 _tupleparse (self, s)
 _parserange (self, tuple)
 _mung_id (self, id)
 _mung_index (self, s1, s2)
 _mung_n (self, s1, s2)
 _mung_loop (self, s1, s2)
 _mung_expr_dollar (self, s)
 _mung_expr_ids (self, s)
 _mung_expr (self, s)
 _make_func (self, payload, extargs='')

Protected Attributes

dict _iddict = {}
dict _limdict = {}
dict _loopdict = {}

Detailed Description

Holds information used to implement a draw/scan/loop command.

Pass the draw string to the constructor.  See the file-level comments
for details on the syntax of this.  This will define the
following attributes:

  errstr -   If set to a string, there was an error.
             Should be None if everything's ok.
  tuple -    The name of the tuple object.
  tuple_o -  Tuple object.
  lo -       The lower bound for row iteration.
  hi -       The upper bound for row iteration.
  stmts -    List of additional statements.
  exprs -    List of draw expressions.
  sel -      Selection expression or None.
  sel_orig - Untransformed selection expression or None.
  expr_orig- Untransformed plotting expression.
  histspec - The text following `;', split into space-separated words.

Other attributes:
  _iddict -  Map from loop identifiers (`foo' in `foo$i')
             to temp variables used to reference
             them in the loop function.
  _limdict - Map from loop identifiers (`foo' in `foo$2')
             to the largest explicit index seen.
  _loopdict - Map of loop dummy variable names to _Loopvar instances.

Definition at line 604 of file pydraw.py.

Constructor & Destructor Documentation

◆ __init__()

python.pydraw.Draw_Cmd.__init__ ( self,
s )
Initialize from a draw string.  See above for more details.

Definition at line 633 of file pydraw.py.

633 def __init__ (self, s):
634 """Initialize from a draw string. See above for more details."""
635
636 # Assume no error.
637 self.errstr = None
638
639 self._iddict = {}
640 self._limdict = {}
641 self._loopdict = {}
642
643 try:
644 self._tupleparse (s)
645 except Exception as e:
646 import traceback
647 self.errstr = str(e)
648 self.excstr = traceback.format_exc()
649 return
650
651
652

Member Function Documentation

◆ _make_func()

python.pydraw.Draw_Cmd._make_func ( self,
payload,
extargs = '' )
protected
Create the text for the function to process this query.

PAYLOAD is the payload expression to plug in.
EXTARGS is an additional string to add to the end of the
function's argument list (to set default values, for example).
Returns the function definition as a string.

Definition at line 934 of file pydraw.py.

934 def _make_func (self, payload, extargs = ''):
935 """Create the text for the function to process this query.
936
937 PAYLOAD is the payload expression to plug in.
938 EXTARGS is an additional string to add to the end of the
939 function's argument list (to set default values, for example).
940 Returns the function definition as a string.
941 """
942
943 sel = self.sel
944 if self._limdict:
945 limsel = ' and '.join (["len(%s)>=%d" % (self._iddict[p[0]], p[1])
946 for p in self._limdict.items()])
947 if not sel:
948 sel = limsel
949 else:
950 sel = limsel + " and (" + sel + ")"
951
952 ftext = "def _loopfunc(_i, %s%s):\n" % (_evtvar, extargs)
953 for (id1, id2) in sorted(self._iddict.items()):
954 ftext += " %s = %s.%s\n" % (id2, _evtvar, id1)
955 indent = 2
956
957 for (i,l) in sorted(self._loopdict.items()):
958 ids = sorted(l.get_ids())
959 assert (not not ids)
960 if len(ids) == 1:
961 vars = l.itname (ids[0])
962 lists = self._iddict[ids[0]]
963 else:
964 vars = "(" + ','.join([l.itname (id) for id in ids]) + ")"
965 lists = ("zip(" + ','.join([self._iddict[id] for id in ids])
966 + ")")
967 if l.explicit:
968 vars = "(%s,%s)" % (l.dumname(), vars)
969 lists = "enumerate(%s)" % lists
970 ftext += ' '*indent + "for %s in %s:\n" % (vars, lists)
971 indent += 2
972
973 for s in self.stmts:
974 ftext += ' '*indent + s + '\n'
975
976 if sel and sel != '1':
977 ftext += ' '*indent + "if (%s):\n" % sel
978 indent += 2
979
980 ftext += ' '*indent + "%s\n" % payload
981
982 if _debug:
983 print (ftext)
984
985 return ftext
986
987

◆ _mung_expr()

python.pydraw.Draw_Cmd._mung_expr ( self,
s )
protected
Process $ constructions and id substitution in string S.

Returns the modified string.

Definition at line 925 of file pydraw.py.

925 def _mung_expr (self, s):
926 """Process $ constructions and id substitution in string S.
927
928 Returns the modified string.
929 """
930 s = self._mung_expr_dollar (s)
931 return self._mung_expr_ids (s)
932
933

◆ _mung_expr_dollar()

python.pydraw.Draw_Cmd._mung_expr_dollar ( self,
s )
protected
Process $ constructions in string S.

Returns the modified string.

Definition at line 863 of file pydraw.py.

863 def _mung_expr_dollar (self, s):
864 """Process $ constructions in string S.
865
866 Returns the modified string.
867 """
868 if not s:
869 return s
870 pos = 0
871 while 1:
872 (s1, s2) = _find_outer (s[pos:], '$', True)
873 if s2 is None:
874 break
875 snew = None
876 if len(s2) > 0:
877 if s2[0] in string.digits:
878 snew = self._mung_index (s1, s2)
879 elif (s2[0] == 'n' and
880 (not (len(s1) > 0 and s1[-1] in _idchars) or
881 s1.endswith (' and') or
882 s1.endswith (' or') or
883 s1.endswith ('not'))):
884 snew = self._mung_n (s1, s2)
885 elif s2[0] in string.ascii_letters:
886 snew = self._mung_loop (s1, s2)
887 s = s[:pos]
888 if snew is None:
889 snew = s1 + '$' + s2
890 pos = pos + len(s1)+1
891 s = s + snew
892 return s
893
894

◆ _mung_expr_ids()

python.pydraw.Draw_Cmd._mung_expr_ids ( self,
s )
protected
Perform id substitution in S.

For identifiers in S that are attributes of our tuple,
replace them with references to the tuple attribute
(using _mung_id).

Returns the modified string.

Definition at line 895 of file pydraw.py.

895 def _mung_expr_ids (self, s):
896 """Perform id substitution in S.
897
898 For identifiers in S that are attributes of our tuple,
899 replace them with references to the tuple attribute
900 (using _mung_id).
901
902 Returns the modified string.
903 """
904 if not s:
905 return s
906
907 tlist = tokenize.generate_tokens (StringIO(s).readline)
908 out = []
909 afterDot = False
910 for tnum, val, a, b, c in tlist:
911 if tnum == token.NAME and not afterDot:
912 if hasattr (self.tuple_o, val):
913 val = self._mung_id (val)
914 #val = _evtvar + '.' + val
915 out.append ((tnum, val))
916 # Don't mung names after a period. We may have attributes
917 # with the same names as variables in the tuple.
918 if tnum == token.OP and val == '.':
919 afterDot = True
920 else:
921 afterDot = False
922 return _untokenize (out)
923
924

◆ _mung_id()

python.pydraw.Draw_Cmd._mung_id ( self,
id )
protected
Given a loop identifier (`foo' in `foo$i'), return the identifier
used to reference it in loop functions.

Definition at line 765 of file pydraw.py.

765 def _mung_id (self, id):
766 """Given a loop identifier (`foo' in `foo$i'), return the identifier
767 used to reference it in loop functions.
768 """
769 out = self._iddict.get (id)
770 if not out:
771 out = '_e_' + id
772 self._iddict[id] = out
773 return out
774
775

◆ _mung_index()

python.pydraw.Draw_Cmd._mung_index ( self,
s1,
s2 )
protected
Handle an explicit index reference; i.e., `foo$2'.

S1 and S2 are pieces of the string before and after the `$'.
Returns the modified string.

Definition at line 776 of file pydraw.py.

776 def _mung_index (self, s1, s2):
777 """Handle an explicit index reference; i.e., `foo$2'.
778
779 S1 and S2 are pieces of the string before and after the `$'.
780 Returns the modified string.
781 """
782 pos2 = 0
783 while pos2 < len(s2) and s2[pos2] in string.digits:
784 pos2 += 1
785 if pos2 == 0:
786 self.errstr = "Bad index"
787 return ''
788 i = int (s2[:pos2])
789 if i < 1:
790 self.errstr = "Bad index"
791 return ''
792 s = ("[%d]" % (i-1)) + s2[pos2:]
793 pos2 = len(s1)-1
794 while pos2 >= 0 and s1[pos2] in _idchars:
795 pos2 -= 1
796 pos2 += 1
797 if pos2 == len(s1):
798 self.errstr = "Bad index"
799 return ''
800 id = s1[pos2:]
801 s = s1[:pos2] + self._mung_id (id) + s
802 self._limdict[id] = max (i, self._limdict.get(id, 0))
803 return s
804
805
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130

◆ _mung_loop()

python.pydraw.Draw_Cmd._mung_loop ( self,
s1,
s2 )
protected
Handle use of a dummy loop variable, such as foo$i.

S1 and S2 are pieces of the string before and after the `$'.
Returns the modified string.

Definition at line 820 of file pydraw.py.

820 def _mung_loop (self, s1, s2):
821 """Handle use of a dummy loop variable, such as foo$i.
822
823 S1 and S2 are pieces of the string before and after the `$'.
824 Returns the modified string.
825 """
826
827 # Scan after the $ to find the dummy variable.
828 pos2 = 0
829 while pos2 < len(s2) and s2[pos2] in _idchars:
830 pos2 += 1
831 if pos2 == 0:
832 self.errstr = "Bad loop var"
833 return ''
834 loopvar = s2[:pos2]
835
836 # Look it up. Make a new _Loopvar object if it's not in the map.
837 ll = self._loopdict.get (loopvar)
838 if not ll:
839 ll = _Loopvar(loopvar)
840 self._loopdict[loopvar] = ll
841
842 # Is the $ after an identifier?
843 if len(s1) > 0 and s1[-1] in _idchars:
844 # Yes --- find the identifier.
845 pos3 = len(s1)-1
846 while pos3 >= 0 and s1[pos3] in _idchars:
847 pos3 -= 1
848 pos3 += 1
849 assert (len(s1) - pos3 >= 1)
850 id = s1[pos3:]
851
852 # Replace with the iterator.
853 s = s1[:pos3] + ll.add_id(id) + s2[pos2:]
854 self._mung_id (id)
855 else:
856 # Explicit use of the dummy.
857 # Replace with the dummy name and note that it was used explicitly.
858 s = s1 + ("%s" % ll.dumname()) + s2[pos2:]
859 ll.explicit = 1
860 return s
861
862

◆ _mung_n()

python.pydraw.Draw_Cmd._mung_n ( self,
s1,
s2 )
protected
Handle a length reference; i.e., `$nfoo'.

S1 and S2 are pieces of the string before and after the `$'.
Returns the modified string.

Definition at line 806 of file pydraw.py.

806 def _mung_n (self, s1, s2):
807 """Handle a length reference; i.e., `$nfoo'.
808
809 S1 and S2 are pieces of the string before and after the `$'.
810 Returns the modified string.
811 """
812 pos2 = 1
813 while pos2 < len(s2) and s2[pos2] in _idchars:
814 pos2 += 1
815 id = s2[1:pos2]
816 s = s1 + (" len(%s)" % self._mung_id(id)) + s2[pos2:]
817 return s
818
819

◆ _parserange()

python.pydraw.Draw_Cmd._parserange ( self,
tuple )
protected
Parse the range part of a draw string.

See above for more details.
Fills self.tuple, self.lo, self.hi.

Definition at line 728 of file pydraw.py.

728 def _parserange (self, tuple):
729 """Parse the range part of a draw string.
730
731 See above for more details.
732 Fills self.tuple, self.lo, self.hi.
733 """
734 lo = 0
735 hi = sys.maxsize
736 (tuple, tail) = _find_outer (tuple, '[')
737 if tail:
738 g = copy.copy (_globals)
739
740 pos = tail.find (':')
741 pos2 = tail.find (']')
742 if pos2 < 0:
743 pos2 = len (tail) #pragma: NO COVER
744 if pos < 0:
745 slo = tail[:pos2].strip()
746 if len (slo) > 0:
747 lo = int (eval (slo, g))
748 hi = lo + 1
749 else:
750 slo = tail[:pos].strip()
751 if len (slo) > 0:
752 lo = int (eval (slo, g))
753 shi = tail[pos+1:pos2].strip()
754 if len (shi) > 0:
755 hi = int (eval (shi, g))
756
757 if tuple[0] == '(' and tuple[-1] == ')':
758 tuple = tuple[1:-1].strip()
759 self.tuple = tuple
760 self.lo = lo
761 self.hi = hi
762 return
763
764

◆ _tupleparse()

python.pydraw.Draw_Cmd._tupleparse ( self,
s )
protected
Parse a draw string.  See above for more details.

Definition at line 653 of file pydraw.py.

653 def _tupleparse (self, s):
654 """Parse a draw string. See above for more details."""
655
656 # Split off the histspec.
657 (s, self.histspec) = _find_outer (s, ';')
658
659 # ??? Don't split at spaces in delimiters.
660 # _find_outer doesn't really work for this since it operates
661 # on the tokenized string, in which whitespace doesn't appear.
662 if self.histspec is None:
663 self.histspec = []
664 else:
665 self.histspec = self.histspec.split ()
666
667 # Gotta have something.
668 s = s.strip()
669 if not s:
670 self.errstr = "Empty draw string."
671 return
672
673 # Split off the tuple part --- before the first period.
674 (tuple, s) = _find_outer (s, '.')
675 if not s:
676 self.errstr = "Missing period in tuple specification."
677 return
678
679 # Handle a range specification on the sample.
680 self._parserange (tuple)
681
682 # Try to find the sample.
683 try:
684 self.tuple_o = eval (self.tuple, _globals)
685 except NameError:
686 self.tuple_o = None
687 if not self.tuple_o:
688 self.errstr = "Can't find sample " + self.tuple
689 return
690
691 # Look for additional statements.
692 self.stmts = _split_outer (s, '@')
693 s = self.stmts[-1]
694 del self.stmts[-1]
695 self.stmts = [self._mung_expr(x) for x in self.stmts]
696
697 # Split off the selection.
698 (self.expr_orig, self.sel_orig) = _find_outer (s, "if")
699 self.sel = self._mung_expr (self.sel_orig)
700
701 self.exprs = [self._mung_expr(x) for x in
702 _split_outer (self.expr_orig, ':')]
703
704 # Check the interface of the sample. If it doesn't have
705 # the loop interface but has the root tree interface,
706 # use a wrapper.
707 if hasattr (self.tuple_o, 'loop'):
708 # Ok --- has the loop interface.
709 pass
710 elif (hasattr (self.tuple_o, 'GetEntry') and
711 hasattr (self.tuple_o, 'GetEntries')):
712 # Has the TTree interface. Use a wrapper.
713 self.tuple_o = TreeLoopWrapper (self.tuple_o)
714 elif (hasattr (self.tuple_o, 'size') and
715 hasattr (self.tuple_o, 'seekEvent')): #pragma: NO COVER
716 # Has the appmgr interface. Use a wrapper.
717 self.tuple_o = AthenaLoopWrapper (self.tuple_o) #pragma: NO COVER
718 else:
719 # An error --- complain.
720 self.errstr = ("Sample " + self.tuple +
721 " doesn't have a correct interface.")
722 return
723
724 return
725
726
727

Member Data Documentation

◆ _iddict

dict python.pydraw.Draw_Cmd._iddict = {}
protected

Definition at line 639 of file pydraw.py.

◆ _limdict

dict python.pydraw.Draw_Cmd._limdict = {}
protected

Definition at line 640 of file pydraw.py.

◆ _loopdict

dict python.pydraw.Draw_Cmd._loopdict = {}
protected

Definition at line 641 of file pydraw.py.

◆ errstr

str python.pydraw.Draw_Cmd.errstr = None

Definition at line 637 of file pydraw.py.

◆ excstr

python.pydraw.Draw_Cmd.excstr = traceback.format_exc()

Definition at line 648 of file pydraw.py.

◆ expr_orig

python.pydraw.Draw_Cmd.expr_orig

Definition at line 698 of file pydraw.py.

◆ exprs

list python.pydraw.Draw_Cmd.exprs
Initial value:
= [self._mung_expr(x) for x in
_split_outer (self.expr_orig, ':')]

Definition at line 701 of file pydraw.py.

◆ hi

python.pydraw.Draw_Cmd.hi = hi

Definition at line 761 of file pydraw.py.

◆ histspec

list python.pydraw.Draw_Cmd.histspec

Definition at line 657 of file pydraw.py.

◆ lo

python.pydraw.Draw_Cmd.lo = lo

Definition at line 760 of file pydraw.py.

◆ sel

python.pydraw.Draw_Cmd.sel = self._mung_expr (self.sel_orig)

Definition at line 699 of file pydraw.py.

◆ sel_orig

python.pydraw.Draw_Cmd.sel_orig

Definition at line 698 of file pydraw.py.

◆ stmts

list python.pydraw.Draw_Cmd.stmts = _split_outer (s, '@')

Definition at line 692 of file pydraw.py.

◆ tuple

python.pydraw.Draw_Cmd.tuple = tuple

Definition at line 759 of file pydraw.py.

◆ tuple_o

python.pydraw.Draw_Cmd.tuple_o = eval (self.tuple, _globals)

Definition at line 684 of file pydraw.py.


The documentation for this class was generated from the following file: