ATLAS Offline Software
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
python.pydraw.Draw_Cmd Class Reference
Inheritance diagram for python.pydraw.Draw_Cmd:
Collaboration diagram for python.pydraw.Draw_Cmd:

Public Member Functions

def __init__ (self, s)
 

Public Attributes

 errstr
 
 excstr
 
 histspec
 
 tuple_o
 
 stmts
 
 sel
 
 exprs
 
 tuple
 
 lo
 
 hi
 

Private Member Functions

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

Private Attributes

 _iddict
 
 _limdict
 
 _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 610 of file pydraw.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 639 of file pydraw.py.

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

Member Function Documentation

◆ _make_func()

def python.pydraw.Draw_Cmd._make_func (   self,
  payload,
  extargs = '' 
)
private
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 933 of file pydraw.py.

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

◆ _mung_expr()

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

Returns the modified string.

Definition at line 924 of file pydraw.py.

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

◆ _mung_expr_dollar()

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

Returns the modified string.

Definition at line 869 of file pydraw.py.

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

◆ _mung_expr_ids()

def python.pydraw.Draw_Cmd._mung_expr_ids (   self,
  s 
)
private
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 901 of file pydraw.py.

901  def _mung_expr_ids (self, s):
902  """Perform id substitution in S.
903 
904  For identifiers in S that are attributes of our tuple,
905  replace them with references to the tuple attribute
906  (using _mung_id).
907 
908  Returns the modified string.
909  """
910  if not s:
911  return s
912 
913  tlist = tokenize.generate_tokens (StringIO(s).readline)
914  out = []
915  for tnum, val, a, b, c in tlist:
916  if tnum == token.NAME:
917  if hasattr (self.tuple_o, val):
918  val = self._mung_id (val)
919  #val = _evtvar + '.' + val
920  out.append ((tnum, val))
921  return _untokenize (out)
922 
923 

◆ _mung_id()

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

Definition at line 771 of file pydraw.py.

771  def _mung_id (self, id):
772  """Given a loop identifier (`foo' in `foo$i'), return the identifier
773  used to reference it in loop functions.
774  """
775  out = self._iddict.get (id)
776  if not out:
777  out = '_e_' + id
778  self._iddict[id] = out
779  return out
780 
781 

◆ _mung_index()

def python.pydraw.Draw_Cmd._mung_index (   self,
  s1,
  s2 
)
private
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 782 of file pydraw.py.

782  def _mung_index (self, s1, s2):
783  """Handle an explicit index reference; i.e., `foo$2'.
784 
785  S1 and S2 are pieces of the string before and after the `$'.
786  Returns the modified string.
787  """
788  pos2 = 0
789  while pos2 < len(s2) and s2[pos2] in string.digits:
790  pos2 += 1
791  if pos2 == 0:
792  self.errstr = "Bad index"
793  return ''
794  i = int (s2[:pos2])
795  if i < 1:
796  self.errstr = "Bad index"
797  return ''
798  s = ("[%d]" % (i-1)) + s2[pos2:]
799  pos2 = len(s1)-1
800  while pos2 >= 0 and s1[pos2] in _idchars:
801  pos2 -= 1
802  pos2 += 1
803  if pos2 == len(s1):
804  self.errstr = "Bad index"
805  return ''
806  id = s1[pos2:]
807  s = s1[:pos2] + self._mung_id (id) + s
808  self._limdict[id] = max (i, self._limdict.get(id, 0))
809  return s
810 
811 

◆ _mung_loop()

def python.pydraw.Draw_Cmd._mung_loop (   self,
  s1,
  s2 
)
private
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 826 of file pydraw.py.

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

◆ _mung_n()

def python.pydraw.Draw_Cmd._mung_n (   self,
  s1,
  s2 
)
private
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 812 of file pydraw.py.

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

◆ _parserange()

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

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

Definition at line 734 of file pydraw.py.

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

◆ _tupleparse()

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

Definition at line 659 of file pydraw.py.

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

Member Data Documentation

◆ _iddict

python.pydraw.Draw_Cmd._iddict
private

Definition at line 645 of file pydraw.py.

◆ _limdict

python.pydraw.Draw_Cmd._limdict
private

Definition at line 646 of file pydraw.py.

◆ _loopdict

python.pydraw.Draw_Cmd._loopdict
private

Definition at line 647 of file pydraw.py.

◆ errstr

python.pydraw.Draw_Cmd.errstr

Definition at line 643 of file pydraw.py.

◆ excstr

python.pydraw.Draw_Cmd.excstr

Definition at line 654 of file pydraw.py.

◆ exprs

python.pydraw.Draw_Cmd.exprs

Definition at line 707 of file pydraw.py.

◆ hi

python.pydraw.Draw_Cmd.hi

Definition at line 767 of file pydraw.py.

◆ histspec

python.pydraw.Draw_Cmd.histspec

Definition at line 669 of file pydraw.py.

◆ lo

python.pydraw.Draw_Cmd.lo

Definition at line 766 of file pydraw.py.

◆ sel

python.pydraw.Draw_Cmd.sel

Definition at line 705 of file pydraw.py.

◆ stmts

python.pydraw.Draw_Cmd.stmts

Definition at line 698 of file pydraw.py.

◆ tuple

python.pydraw.Draw_Cmd.tuple

Definition at line 765 of file pydraw.py.

◆ tuple_o

python.pydraw.Draw_Cmd.tuple_o

Definition at line 690 of file pydraw.py.


The documentation for this class was generated from the following file:
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
str
Definition: BTagTrackIpAccessor.cxx:11