[Rivet-svn] r2545 - in trunk: bin pyext

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Mon Jun 28 14:01:30 BST 2010


Author: buckley
Date: Mon Jun 28 14:01:40 2010
New Revision: 2545

Log:
Explicitly checking for and requiring Python 2.4 in Rivet scripts: we already needed this due to uses of subprocess, etc., so I thought it better to bring this ticket forward. I've also added a rivet.check_python_version() function, but am not using it yet, until I've made the rivet Python module importable without first having to change the Python module loader's dlopen flags.

Modified:
   trunk/bin/aida2flat
   trunk/bin/aida2root
   trunk/bin/compare-histos
   trunk/bin/flat2aida
   trunk/bin/make-plots
   trunk/bin/rivet
   trunk/bin/rivet-chopbins
   trunk/bin/rivet-mergeruns
   trunk/bin/rivet-mkanalysis
   trunk/bin/rivet-mkhtml
   trunk/bin/rivet-rescale
   trunk/bin/rivet-rmgaps
   trunk/bin/rivetgrid
   trunk/bin/root2flat
   trunk/pyext/rivet.i

Modified: trunk/bin/aida2flat
==============================================================================
--- trunk/bin/aida2flat	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/aida2flat	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,25 +1,17 @@
 #! /usr/bin/env python
 
-import sys, os, logging
+"""\
+%prog aidafile [aidafile2 ...]
+"""
+
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
 
+import os, logging
 import lighthisto
 
-
-## Make "sorted" a builtin function on Python < 2.4
-if 'sorted' not in dir(__builtins__):
-    def sorted(iterable, cmp=None, key=None, reverse=None):
-        rtn = iterable
-        rtn.sort(cmp)
-        return rtn
-
-## Add logging.log if needed
-if 'log' not in dir(logging):
-    def _logit(level, msg):
-        l = logging.getLogger()
-        l.log(level, msg)
-    logging.log = _logit
-
-
 ## Try to load faster but non-standard cElementTree module
 try:
     import xml.etree.cElementTree as ET
@@ -39,13 +31,14 @@
 
 if __name__ == "__main__":
 
+    ## TODO: replace with rivet.getAnalysisRefDirs()
     rivet_data_dir = os.popen('rivet-config --datadir',"r").readline().strip()
     if not rivet_data_dir:
         rivet_data_dir = ""
 
     ## Parse command line options
     from optparse import OptionParser, OptionGroup
-    parser = OptionParser(usage="%prog aidafile [aidafile2 ...]")
+    parser = OptionParser(usage=__doc__)
     parser.add_option("-s", "--split", action="store_true", default=False,
                       help="Write each histo to a separate output file, with names based on the histo path",
                       dest="SPLITOUTPUT")
@@ -69,17 +62,7 @@
 
 
     ## Configure logging
-    try:
-        logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-    except:
-        pass
-    h = logging.StreamHandler()
-    h.setFormatter(logging.Formatter("%(message)s"))
-    logging.getLogger().setLevel(opts.LOGLEVEL)
-    if logging.getLogger().handlers:
-        logging.getLogger().handlers[0] = h
-    else:
-        logging.getLogger().addHandler(h)
+    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
     ## Initialise steering variables which need a bit more care

Modified: trunk/bin/aida2root
==============================================================================
--- trunk/bin/aida2root	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/aida2root	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,18 +1,25 @@
 #! /usr/bin/env python
 
+"""\
+%prog aidafile [aidafile2 ...]"
 
-#Verify in the ROOT user manual what needs to be setup
-# for use of ROOT with python
-#
-#E.g. do the following additional setup steps:
-# setup setenv PYTHONDIR /usr
-# setenv PATH $ROOTSYS/bin:$PYTHONDIR/bin:$PATH
-# setenv LD_LIBRARY_PATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.3:$LD_LIBRARY_PATH
-# setenv PYTHONPATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.3
+Verify in the ROOT user manual what needs to be setup for use of ROOT with python
+
+E.g. do the following additional setup steps:
+ setup setenv PYTHONDIR /usr
+ setenv PATH $ROOTSYS/bin:$PYTHONDIR/bin:$PATH
+ setenv LD_LIBRARY_PATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.3:$LD_LIBRARY_PATH
+ setenv PYTHONPATH $ROOTSYS/lib:$PYTHONDIR/lib/python2.3
+"""
+
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
 
 
 
-import sys, os
+import os
 from array import array
 try:
     from ROOT import TGraphAsymmErrors, TFile
@@ -34,14 +41,7 @@
     sys.exit(1)
 
 
-try:
-    sorted([])
-except:
-    def sorted(coll):
-        coll.sort()
-        return coll
-
-
+## TODO: replace with lighthisto
 class Histo:
     def __init__(self):
         self._bins = []
@@ -124,6 +124,7 @@
         return self.getBin(index)
 
 
+## TODO: replace with lighthisto
 class Bin:
     """A simple container for a binned value with an error."""
     def __init__(self, xval=0, xerrminus=None, xerrplus=None, yval=0, yerrminus=0, yerrplus=0, focus=None):
@@ -237,7 +238,7 @@
 
 
 from optparse import OptionParser
-parser = OptionParser(usage="%prog aidafile [aidafile2 ...]")
+parser = OptionParser(usage=__doc__)
 parser.add_option("-s", "--smart-output", action="store_true", default=True,
                   help="Write to output files with names based on the corresponding input filename",
                   dest="SMARTOUTPUT")

Modified: trunk/bin/compare-histos
==============================================================================
--- trunk/bin/compare-histos	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/compare-histos	Mon Jun 28 14:01:40 2010	(r2545)
@@ -14,17 +14,10 @@
  * ask/force overwrite modes
 """
 
-
-## Make "set" a builtin type on Python < 2.4
-if 'set' not in dir(__builtins__):
-    from sets import Set as set
-
-## Make "sorted" a builtin function on Python < 2.4
-if 'sorted' not in dir(__builtins__):
-    def sorted(iterable, cmp=None, key=None, reverse=None):
-        rtn = iterable
-        rtn.sort(cmp)#, key, reverse)
-        return rtn
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
 
 
 def sanitiseString(s):
@@ -141,17 +134,7 @@
         opts.PLOTINFODIR += getpathvar("RIVET_REF_PATH")
 
     ## Configure logging
-    try:
-        logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-    except:
-        pass
-    h = logging.StreamHandler()
-    h.setFormatter(logging.Formatter("%(message)s"))
-    logging.getLogger().setLevel(opts.LOGLEVEL)
-    if logging.getLogger().handlers:
-        logging.getLogger().handlers[0] = h
-    else:
-        logging.getLogger().addHandler(h)
+    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
     ## Line styles

Modified: trunk/bin/flat2aida
==============================================================================
--- trunk/bin/flat2aida	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/flat2aida	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,7 +1,16 @@
 #! /usr/bin/env python
 
-import os, sys
+"""\
+%prog flatfile [flatfile2 ...]
+"""
+
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
 
+import os
 from htmlentitydefs import codepoint2name
 unichr2entity = dict((unichr(code), u'&%s;' % name) \
                          for code,name in codepoint2name.iteritems() \
@@ -60,30 +69,34 @@
                                           'UpEdge':  float(linearray[1]),
                                           'Content': float(linearray[2]),
                                           'Error':   [float(linearray[3]),float(linearray[4])]})
+
     def write_datapoint(self, f, xval, xerr, yval, yerr):
-            f.write('    <dataPoint>\n')
-            f.write('      <measurement errorPlus="%e" value="%e" errorMinus="%e"/>\n' %(xerr, xval, xerr))
-            f.write('      <measurement errorPlus="%e" value="%e" errorMinus="%e"/>\n' %(yerr[1], yval, yerr[0]))
-            f.write('    </dataPoint>\n')
+        f.write('    <dataPoint>\n')
+        f.write('      <measurement errorPlus="%e" value="%e" errorMinus="%e"/>\n' %(xerr, xval, xerr))
+        f.write('      <measurement errorPlus="%e" value="%e" errorMinus="%e"/>\n' %(yerr[1], yval, yerr[0]))
+        f.write('    </dataPoint>\n')
+
     def write_datapointset_header(self, f, count, bin):
-            path = '/REF/%s/d%02d-x01-y%02d' %(filename.split('/')[-1], count, bin+1)
-            if self.description.has_key("AidaPath"):
-                path = self.description["AidaPath"]
-            f.write('  <dataPointSet name="%s" dimension="2"\n' % (os.path.basename(path)))
-            if not self.description.has_key('Title'):
-                self.description['Title'] = ""
-            f.write('    path="%s" title="%s">\n' % (os.path.dirname(path), htmlescape(self.description['Title'])))
-            if self.description.has_key("XLabel") and self.description["XLabel"] is not None:
-                f.write('    <dimension dim="0" title="%s" />\n' % htmlescape(self.description['XLabel']))
-            if self.description.has_key("YLabel") and self.description["YLabel"] is not None:
-                f.write('    <dimension dim="1" title="%s" />\n' % htmlescape(self.description['YLabel']))
-            f.write('    <annotation>\n')
-            f.write('      <item key="Title" value="%s" sticky="true"/>\n' %(htmlescape(self.description['Title'])))
-            f.write('      <item key="AidaPath" value="%s" sticky="true"/>\n' %(path))
-            f.write('      <item key="FullPath" value="/%s.aida%s" sticky="true"/>\n' %(filename.split('/')[-1], path))
-            f.write('    </annotation>\n')
+        path = '/REF/%s/d%02d-x01-y%02d' %(filename.split('/')[-1], count, bin+1)
+        if self.description.has_key("AidaPath"):
+            path = self.description["AidaPath"]
+        f.write('  <dataPointSet name="%s" dimension="2"\n' % (os.path.basename(path)))
+        if not self.description.has_key('Title'):
+            self.description['Title'] = ""
+        f.write('    path="%s" title="%s">\n' % (os.path.dirname(path), htmlescape(self.description['Title'])))
+        if self.description.has_key("XLabel") and self.description["XLabel"] is not None:
+            f.write('    <dimension dim="0" title="%s" />\n' % htmlescape(self.description['XLabel']))
+        if self.description.has_key("YLabel") and self.description["YLabel"] is not None:
+            f.write('    <dimension dim="1" title="%s" />\n' % htmlescape(self.description['YLabel']))
+        f.write('    <annotation>\n')
+        f.write('      <item key="Title" value="%s" sticky="true"/>\n' %(htmlescape(self.description['Title'])))
+        f.write('      <item key="AidaPath" value="%s" sticky="true"/>\n' %(path))
+        f.write('      <item key="FullPath" value="/%s.aida%s" sticky="true"/>\n' %(filename.split('/')[-1], path))
+        f.write('    </annotation>\n')
+
     def write_datapointset_footer(self, f):
-            f.write('  </dataPointSet>\n')
+        f.write('  </dataPointSet>\n')
+
     def write_datapointset(self, f, count):
         if not opts.SPLITHISTOS:
             self.write_datapointset_header(f, count, 0)
@@ -104,29 +117,30 @@
             self.write_datapointset_footer(f)
 
 
-from optparse import OptionParser
-parser = OptionParser(usage="%prog flatfile [flatfile2 ...]")
-parser.add_option("-s", "--split-histos", action="store_true", default=False,
-                  help="Split histograms into individual bins", dest="SPLITHISTOS")
-opts, args = parser.parse_args()
+if __name__ == "__main__":
+    from optparse import OptionParser
+    parser = OptionParser(usage=__doc__)
+    parser.add_option("-s", "--split-histos", action="store_true", default=False,
+                      help="Split histograms into individual bins", dest="SPLITHISTOS")
+    opts, args = parser.parse_args()
+
+    if len(args) < 1:
+        sys.stderr.write("Must specity at least one histogram file\n")
+        sys.exit(1)
+
+    for flatfile in args:
+        filename = flatfile.replace(".dat", "")
+
+        inputdata = Inputdata(filename)
+
+        f = open(filename+'.aida', 'w')
+        f.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n')
+        f.write('<!DOCTYPE aida SYSTEM "http://aida.freehep.org/schemas/3.3/aida.dtd">\n')
+        f.write('<aida version="3.3">\n')
+        f.write('  <implementation version="1.1" package="FreeHEP"/>\n')
 
-if len(args) < 1:
-    sys.stderr.write("Must specity at least one histogram file\n")
-    sys.exit(1)
+        for i, d in enumerate(inputdata.description['DrawOnly']):
+            inputdata.histos[d].write_datapointset(f, i+1)
 
-for flatfile in args:
-    filename = flatfile.replace(".dat", "")
-    
-    inputdata = Inputdata(filename)
-    
-    f = open(filename+'.aida', 'w')
-    f.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n')
-    f.write('<!DOCTYPE aida SYSTEM "http://aida.freehep.org/schemas/3.3/aida.dtd">\n')
-    f.write('<aida version="3.3">\n')
-    f.write('  <implementation version="1.1" package="FreeHEP"/>\n')
-    
-    for i, d in enumerate(inputdata.description['DrawOnly']):
-        inputdata.histos[d].write_datapointset(f, i+1)
-    
-    f.write('</aida>\n')
-    f.close
+        f.write('</aida>\n')
+        f.close

Modified: trunk/bin/make-plots
==============================================================================
--- trunk/bin/make-plots	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/make-plots	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1720,7 +1720,7 @@
             pngproc.wait()
         else:
             logging.error("Unknown format: %s" % opts.OUTPUT_FORMAT)
-            system.exit(1)
+            sys.exit(1)
         logging.debug(os.listdir(tempdir))
 
     ## Copy results back to main dir
@@ -1848,10 +1848,7 @@
     parser.add_option_group(verbgroup)
 
     opts, args = parser.parse_args()
-    try:
-        logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-    except:
-        logging.getLogger().setLevel(opts.LOGLEVEL)
+    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
     ## Check for no args
@@ -1868,21 +1865,27 @@
 #     if opts.OUTPUT_FORMAT == "EPS":
 #         ps2eps
 
+    import subprocess
+
     ## Check minion font
     if opts.OUTPUT_FONT == "MINION":
-        if os.system('kpsewhich minion.sty > /dev/null') != 0:
+        p = subprocess.Popen(["kpsewhich", "minion.sty"], stdout=subprocess.PIPE)
+        if p.returncode != 0:
             logging.warning('Warning: Using "--minion" requires minion.sty to be installed. Ignoring it.')
             opts.OUTPUT_FONT = "PALATINO"
 
     ## Check for HEP LaTeX packages
     opts.LATEXPKGS = []
     for pkg in ["hepnicenames", "hepunits", "underscore"]:
-        if os.system('kpsewhich %s.sty > /dev/null' % pkg) == 0:
+        p = subprocess.Popen(["kpsewhich", "%s.sty" % pkg], stdout=subprocess.PIPE)
+        if p.returncode == 0:
             opts.LATEXPKGS.append(pkg)
 
     ## Check for Palatino old style figures and small caps
-    if (opts.OUTPUT_FONT == "PALATINO") and not(os.system('kpsewhich ot1pplx.fd > /dev/null')):
-        opts.OUTPUT_FONT = "PALATINO_OSF"
+    if opts.OUTPUT_FONT == "PALATINO":
+        p = subprocess.Popen(["kpsewhich", "ot1pplx.fd"], stdout=subprocess.PIPE)
+        if p.returncode == 0:
+            opts.OUTPUT_FONT = "PALATINO_OSF"
 
     ## Fill queue
     datfiles = Queue.Queue(maxsize=-1)

Modified: trunk/bin/rivet
==============================================================================
--- trunk/bin/rivet	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,26 +1,35 @@
 #! /usr/bin/env python
 
-import sys, os, time
-import logging, signal
-from optparse import OptionParser, OptionGroup
+"""\
+Run Rivet analyses on inputted events from file or Unix pipe
 
+Examples:
+  %prog [options] <hepmcfile>
+  my_generator -o myfifo & \ %prog [options] myfifo
+  agile-runmc <genname> -n 100k | %prog [options]
 
-## Make "sorted" a builtin function on Python < 2.4
-if 'sorted' not in dir(__builtins__):
-    def sorted(iterable, cmp=None, key=None, reverse=None):
-        rtn = iterable
-        rtn.sort(cmp)
-        return rtn
-
-## Add logging.log if needed
-if 'log' not in dir(logging):
-    def _logit(level, msg):
-        l = logging.getLogger()
-        l.log(level, msg)
-    logging.log = _logit
+ENVIRONMENT:
+ * RIVET_ANALYSIS_PATH: list of paths to be searched for plugin
+     analysis libraries at runtime
+ * RIVET_REF_PATH: list of paths to be searched for reference
+     data files
+ * RIVET_INFO_PATH: list of paths to be searched for analysis
+     metadata files
+"""
+
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+
+import os, time
+import logging, signal
+from optparse import OptionParser, OptionGroup
 
 
 ## Change dlopen status to GLOBAL for Rivet lib
+## TODO: Push this into rivet.py
 try:
     import ctypes
     sys.setdlopenflags(sys.getdlopenflags() | ctypes.RTLD_GLOBAL)
@@ -43,25 +52,6 @@
 except ImportError:
     pass
 
-
-## Description / usage message
-usage="""Run Rivet analyses on inputted events from file or Unix pipe
-
-Examples:
-  %prog [options] <hepmcfile>
-  my_generator -o myfifo & \ %prog [options] myfifo
-  agile-runmc <genname> -n 100k | %prog [options]
-
-ENVIRONMENT:
- * RIVET_ANALYSIS_PATH: list of paths to be searched for plugin
-     analysis libraries at runtime
- * RIVET_REF_PATH: list of paths to be searched for reference
-     data files
- * RIVET_INFO_PATH: list of paths to be searched for analysis
-     metadata files
-"""
-
-
 PROGPATH = sys.argv[0]
 PROGNAME = os.path.basename(PROGPATH)
 
@@ -80,6 +70,7 @@
 ## Try importing rivet
 try:
     import rivet
+    rivet.check_python_version()
     #print rivet.getAnalysisLibPaths()
 except Exception, e:
     sys.stderr.write(PROGNAME + " requires the 'rivet' Python module\n");
@@ -88,7 +79,7 @@
 
 
 ## Parse command line options
-parser = OptionParser(usage=usage, version="rivet v%s" % rivet.version())
+parser = OptionParser(usage=__doc__, version="rivet v%s" % rivet.version())
 parser.add_option("-n", "--nevts", dest="MAXEVTNUM", type="int",
                   default=None, metavar="NUM",
                   help="max number of events to read.")
@@ -129,17 +120,7 @@
 
 
 ## Configure logging
-try:
-    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-except:
-    pass
-h = logging.StreamHandler()
-h.setFormatter(logging.Formatter("%(message)s"))
-logging.getLogger().setLevel(opts.LOGLEVEL)
-if logging.getLogger().handlers:
-    logging.getLogger().handlers[0] = h
-else:
-    logging.getLogger().addHandler(h)
+logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
 ## Control native Rivet library logger

Modified: trunk/bin/rivet-chopbins
==============================================================================
--- trunk/bin/rivet-chopbins	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-chopbins	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,4 +1,5 @@
-#!/usr/bin/env python
+#! /usr/bin/env python
+
 """%prog -b <HISTO/PATH:min:max> [ -b ... ] <AIDAFILE> [...]
 
 Strip specified bins from data sets. Histograms not specified will be passed
@@ -21,25 +22,14 @@
     * what if the same observable is mentioned multiple times?
 """
 
-import os
 import sys
-import logging
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
 
-import lighthisto
-## Make "sorted" a builtin function on Python < 2.4
-if 'sorted' not in dir(__builtins__):
-    def sorted(iterable, cmp=None, key=None, reverse=None):
-        rtn = iterable
-        rtn.sort(cmp)
-        return rtn
-
-## Add logging.log if needed
-if 'log' not in dir(logging):
-    def _logit(level, msg):
-        l = logging.getLogger()
-        l.log(level, msg)
-    logging.log = _logit
 
+import os, logging
+import lighthisto
 
 ## Try to load faster but non-standard cElementTree module
 try:
@@ -81,17 +71,7 @@
 
 
     ## Configure logging
-    try:
-        logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-    except:
-        pass
-    h = logging.StreamHandler()
-    h.setFormatter(logging.Formatter("%(message)s"))
-    logging.getLogger().setLevel(opts.LOGLEVEL)
-    if logging.getLogger().handlers:
-        logging.getLogger().handlers[0] = h
-    else:
-        logging.getLogger().addHandler(h)
+    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
     if len(args) == 0:

Modified: trunk/bin/rivet-mergeruns
==============================================================================
--- trunk/bin/rivet-mergeruns	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-mergeruns	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,37 +1,35 @@
 #! /usr/bin/env python
 
-## Script for merging parts of multiple histo files made with
-## different run params (kinematic pT cuts and energies) into one
-## histo file for plotting or further analysis.
-##
-## TODO:
-##  * take merge specs from a conf file instead of hard-coding
-##  * generalise to more generic merge ranges (i.e. not just sqrts & pT)
-##  * improve cmd line interface
-##  * rationalise all histogramming formats... remove AIDA!
-##  * use external histo classes (YODA), since I've now lost track
-##    of which changes have had to be made to which copies of Histo, Bin etc.!
-##
-## Usage example:
-##  $ uemerge hwpp/hpp-1800-{030.aida:1800:30,090.aida:1800:90} > hpp-hists.dat
-##  $ flat2aida hpp-hists.dat
-##  $ mkdir plots && cd plots
-##  $ compare_histos.py ../ref04.aida ../hpp-hists.aida
-##  $ make_plot.py --pdf *.dat
+"""%prog
 
+Script for merging parts of multiple histo files made with different run params
+(kinematic pT cuts and energies) into one histo file for plotting or further
+analysis.
+
+TODO:
+ * take merge specs from a conf file instead of hard-coding
+ * generalise to more generic merge ranges (i.e. not just sqrts & pT)
+ * improve cmd line interface
+ * rationalise all histogramming formats... remove AIDA!
+ * use lighthisto (then YODA)
+
+Usage example:
+ $ uemerge hwpp/hpp-1800-{030.aida:1800:30,090.aida:1800:90} > hpp-hists.dat
+ $ flat2aida hpp-hists.dat
+ $ mkdir plots && cd plots
+ $ compare_histos.py ../ref04.aida ../hpp-hists.aida
+ $ make_plot.py --pdf *.dat
+"""
+
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
 
-import sys, os, copy, re
+import os, copy, re
 from math import sqrt
 
 
-try:
-    sorted([])
-except:
-    def sorted(coll):
-        coll.sort()
-        return coll
-
-
 def mean(*args):
     total, num = 0, 0
     for a in args:
@@ -41,6 +39,7 @@
     return total / float(num)
 
 
+## TODO: replace with lighthisto
 class Histo:
     def __init__(self):
         self.clear()
@@ -133,6 +132,7 @@
         return self.getBin(index)
 
 
+## TODO: replace with lighthisto
 class Bin:
     """A simple container for a binned value with an error."""
     def __init__(self, xlow=None, xhigh=None, yval=0, yerrplus=0, yerrminus=0, focus=None):
@@ -234,6 +234,7 @@
             sys.exit(1)
 
 
+## TODO: replace with lighthisto
 def mkHistoFromDPS(dps):
     """Make a mini histo representation from an AIDA dataPointSet tag."""
     myhist = Histo()

Modified: trunk/bin/rivet-mkanalysis
==============================================================================
--- trunk/bin/rivet-mkanalysis	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-mkanalysis	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,9 +1,5 @@
 #! /usr/bin/env python
 
-from optparse import OptionParser
-import logging
-import os, sys
-
 """\
 %prog: make templates of analysis source files for Rivet"
 
@@ -13,7 +9,16 @@
 directory.
 """
 
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+import logging, os
+
+
 ## Handle command line
+from optparse import OptionParser
 parser = OptionParser(usage=__doc__)
 parser.add_option("--srcroot", metavar="DIR", dest="SRCROOT", default=None,
                   help="install the templates into the Rivet source tree (rooted " +
@@ -25,10 +30,7 @@
 parser.add_option("-i", "--inline-info", dest="INLINE", action="store_true",
                   default=False, help="Put analysis info into source file instead of separate data file.")
 opts, args = parser.parse_args()
-try:
-    logging.basicConfig(format="%(msg)s", level=opts.LOGLEVEL)
-except:
-    pass
+logging.basicConfig(format="%(msg)s", level=opts.LOGLEVEL)
 ANANAMES = args
 
 ## Work out installation paths

Modified: trunk/bin/rivet-mkhtml
==============================================================================
--- trunk/bin/rivet-mkhtml	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-mkhtml	Mon Jun 28 14:01:40 2010	(r2545)
@@ -9,6 +9,12 @@
 Reference data should be found automatically.
 """
 
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+
 from optparse import OptionParser
 parser = OptionParser(usage=__doc__)
 parser.add_option("-o", "--outputdir", dest="OUTPUTDIR",
@@ -35,6 +41,7 @@
 import sys, os, glob, shutil
 from subprocess import Popen, PIPE
 
+## TODO: replace with rivet.getAnalysisRefPaths()
 if os.environ.has_key('RIVET_REF_PATH'):
     refpaths = os.environ['RIVET_REF_PATH'].split(":")
 else:

Modified: trunk/bin/rivet-rescale
==============================================================================
--- trunk/bin/rivet-rescale	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-rescale	Mon Jun 28 14:01:40 2010	(r2545)
@@ -35,21 +35,13 @@
 
 """
 
-import os, re, sys, logging
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+import os, re, logging
 from lighthisto import Histo
-## Make "sorted" a builtin function on Python < 2.4
-if not 'sorted' in dir(__builtins__):
-    def sorted(iterable, cmp=None, key=None, reverse=None):
-        rtn = iterable
-        rtn.sort(cmp)
-        return rtn
-
-## Add logging.log if needed
-if not 'log' in dir(logging):
-    def _logit(level, msg):
-        l = logging.getLogger()
-        l.log(level, msg)
-    logging.log = _logit
 
 #try:
 #    from IPython.Shell import IPShellEmbed
@@ -71,6 +63,7 @@
             sys.stderr.write("Can't load the ElementTree XML parser: please install it!\n")
             sys.exit(1)
 
+
 def getHistosFromAIDA(aidafile):
     '''Get a dictionary of histograms indexed by name.'''
     if not re.match(r'.*\.aida$', aidafile):
@@ -92,6 +85,7 @@
         histos[dpsname] = h
     return histos
 
+
 def getRefHistos(refpath):
     """ Return dictionary of reference histos {name: histo}.
     Refpath can either be a single file or a directory.
@@ -113,6 +107,7 @@
             logging.info("Read ref histos from folder %s"%refpath)
     return refhistos
 
+
 def readObservableFile(obsfile):
     """ Read observables to normalise from file obsfile.
         Return-values are a list of the histo-names to normalise and a
@@ -144,6 +139,7 @@
         f.close()
     return obslist, obsnorms, bindefs
 
+
 def getBindef(line):
     """ Try to read bin definitions (xlow, xhigh, newarea) from single
         string.
@@ -178,6 +174,8 @@
             pass
     return (path, low, high, area)
 
+
+
 if __name__ == "__main__":
     from optparse import OptionParser, OptionGroup
     parser = OptionParser(usage=__doc__)
@@ -212,17 +210,7 @@
 
 
     # Configure logging
-    try:
-        logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-    except:
-        pass
-    h = logging.StreamHandler()
-    h.setFormatter(logging.Formatter("%(message)s"))
-    logging.getLogger().setLevel(opts.LOGLEVEL)
-    if logging.getLogger().handlers:
-        logging.getLogger().handlers[0] = h
-    else:
-        logging.getLogger().addHandler(h)
+    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
     histos = getHistosFromAIDA(args[0])
 

Modified: trunk/bin/rivet-rmgaps
==============================================================================
--- trunk/bin/rivet-rmgaps	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivet-rmgaps	Mon Jun 28 14:01:40 2010	(r2545)
@@ -13,7 +13,13 @@
  * Remove need to specify the ref file.
 """
 
-import os, sys, tempfile
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+
+import os, tempfile
 
 class Inputdata:
     def __init__(self, filename):

Modified: trunk/bin/rivetgrid
==============================================================================
--- trunk/bin/rivetgrid	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/rivetgrid	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,10 +1,6 @@
 #! /usr/bin/env python
 # -*- python -*-
 
-import os, sys, logging
-import glob, tempfile, commands
-from optparse import OptionParser, OptionGroup
-
 version = "0.2.0"
 usage = """Submit a rivet job to the Grid, using the gLite tools.
 %prog --vo=<VO> [options] <rgargs>
@@ -17,25 +13,34 @@
   * Allow control over the submission system
 """
 
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+import os, logging
+import glob, tempfile, commands
+
 ## Parse command line options
+from optparse import OptionParser, OptionGroup
 parser = OptionParser(usage=usage, version="%prog "+version)
 #parser.add_option("-n", "--number", dest="MAXEVTNUM", type=int,
-#                  default=None, metavar="NUM", 
+#                  default=None, metavar="NUM",
 #                  help="max number of events to read.")
 #parser.add_option("-a", "--analysis", dest="ANALYSES", action="append",
 #                  default=[], metavar="ANA",
 #                  help="add an analysis to the processing list.")
-parser.add_option("--vo", dest="VO", default=None, 
+parser.add_option("--vo", dest="VO", default=None,
                   help="specify the virtual organisation to run under")
-parser.add_option("-m", "--machine", dest="MACHINE", 
-                  choices=["32","64"], default="32", 
+parser.add_option("-m", "--machine", dest="MACHINE",
+                  choices=["32","64"], default="32",
                   help="specify the target machine architecture - either 32 (default) or 64 bit")
-parser.add_option("-u", "--url", dest="ARCHIVE_URL", 
+parser.add_option("-u", "--url", dest="ARCHIVE_URL",
                   default="http://www.hep.ucl.ac.uk/~jmonk/Rivet/",
                   help="specify the base URL from which to fetch the RivetGrid and Genser packages")
-parser.add_option("-j", "--jobname", dest="JOB_NAME", default="Rivet", 
+parser.add_option("-j", "--jobname", dest="JOB_NAME", default="Rivet",
                   help="specify the job name (affects the names of the output log and histo files)")
-parser.add_option("--dry-run", dest="DRY_RUN", action="store_true", default=False, 
+parser.add_option("--dry-run", dest="DRY_RUN", action="store_true", default=False,
                   help="do everything but actually submit the job")
 verbgroup = OptionGroup(parser, "Verbosity control")
 verbgroup.add_option("-v", "--verbose", action="store_const", const=logging.DEBUG, dest="LOGLEVEL",
@@ -43,11 +48,8 @@
 verbgroup.add_option("-q", "--quiet", action="store_const", const=logging.WARNING, dest="LOGLEVEL",
                      default=logging.INFO, help="be very quiet")
 parser.add_option_group(verbgroup)
-(opts, args) = parser.parse_args()
-try:
-    logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
-except:
-    logging.getLogger().setLevel(opts.LOGLEVEL)
+opts, args = parser.parse_args()
+logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
 
 
 ## Check VO specification
@@ -77,7 +79,7 @@
             break
         else:
             logging.critical("Invalid response: '%s'" % cont)
-            
+
 logging.info("Rivet will be run as: 'rivetgun " + RG_ARGS + "'")
 
 
@@ -113,8 +115,8 @@
 mv $WORKDIR/$OUT $SANDBOXDIR/
 mv $WORKDIR/$AIDA $SANDBOXDIR/
 cd $SANDBOXDIR
-rm -r $WORKDIR""" % { 
-    "jname" : opts.JOB_NAME, 
+rm -r $WORKDIR""" % {
+    "jname" : opts.JOB_NAME,
     "rgurl" : RIVETGRID_URL,
     "gensurl" : GENS_URL,
     "rgargs" : RG_ARGS

Modified: trunk/bin/root2flat
==============================================================================
--- trunk/bin/root2flat	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/bin/root2flat	Mon Jun 28 14:01:40 2010	(r2545)
@@ -1,4 +1,5 @@
 #! /usr/bin/env python
+
 """%prog
 
 Read in .root files and write out the histograms in FLAT
@@ -13,6 +14,12 @@
 Use --help to get information for all options.
 """
 
+import sys
+if sys.version_info[:3] < (2,4,0):
+    print "rivet scripts require Python version >= 2.4.0... exiting"
+    sys.exit(1)
+
+
 import os, optparse, logging, ROOT
 # try:
 #     from IPython.Shell import IPShellEmbed
@@ -33,7 +40,7 @@
         for histograms and tries to write the converted histos to files.
     """
     # Open the ROOT file
-    f=ROOT.TFile(rootfile)
+    f = ROOT.TFile(rootfile)
 
     # Initial browse to see the structure
     subdirs, histonames, tgraphnames = browse(f)

Modified: trunk/pyext/rivet.i
==============================================================================
--- trunk/pyext/rivet.i	Mon Jun 28 13:05:48 2010	(r2544)
+++ trunk/pyext/rivet.i	Mon Jun 28 14:01:40 2010	(r2545)
@@ -43,7 +43,6 @@
 // Rivet search paths
 %include "Rivet/Tools/RivetPaths.hh"
 
-
 // Main Rivet class mappings
 namespace Rivet {
 
@@ -144,3 +143,14 @@
 }
 
 %include "Rivet/Run.hh"
+
+
+// Provide an extra Python-only function used to enforce the Rivet scripts' minimal Python version
+%pythoncode %{
+def check_python_version():
+    import sys
+    req_version = (2,4,0)
+    if sys.version_info[:3] < req_version:
+        print "rivet scripts require Python version >= %s... exiting" % ".".join(req_version)
+        sys.exit(1)
+%}


More information about the Rivet-svn mailing list