[Rivet-svn] r3081 - trunk/bin

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Thu May 5 18:27:38 BST 2011


Author: hoeth
Date: Thu May  5 18:27:38 2011
New Revision: 3081

Log:
next try to get rid of the duplicates in compare-histos

Modified:
   trunk/bin/compare-histos

Modified: trunk/bin/compare-histos
==============================================================================
--- trunk/bin/compare-histos	Thu May  5 17:38:00 2011	(r3080)
+++ trunk/bin/compare-histos	Thu May  5 18:27:38 2011	(r3081)
@@ -46,38 +46,60 @@
             sys.exit(1)
 
 
+## Function to make output dirs
+def mkoutdir(outdir):
+    if not os.path.exists(outdir):
+        try:
+            os.makedirs(outdir)
+        except:
+            msg = "Can't make output directory '%s'" % outdir
+            logging.error(msg)
+            raise Exception(msg)
+    if not os.access(outdir, os.W_OK):
+        msg = "Can't write to output directory '%s'" % outdir
+        logging.error(msg)
+        raise Exception(msg)
+
+
+def getHistos(aidafile):
+    '''Get a dictionary of histograms indexed by name.'''
+    if not re.match(r'.*\.aida$', aidafile):
+        logging.error("Error: input file '%s' is not an AIDA file" % aidafile)
+        sys.exit(2)
+    aidafilepath = os.path.abspath(aidafile)
+    if not os.access(aidafilepath, os.R_OK):
+        logging.error("Error: cannot read from %s" % aidafile)
+        sys.exit(2)
+
+    histos, titles, xlabels, ylabels = {}, {}, {}, {}
+    tree = ET.parse(aidafilepath)
+    for dps in tree.findall("dataPointSet"):
+        ## Get this histogram's path name
+        dpsname = os.path.join(dps.get("path"), dps.get("name"))
+        h = Histo.fromDPS(dps)
+        ## Is it a data histo?
+        h.isdata = dpsname.upper().startswith("/REF")
+        if h.isdata:
+            dpsname = dpsname.replace("/REF", "")
+            if not titles.has_key(dpsname):
+                titles[dpsname] = h.title
+                xlabels[dpsname] = h.xlabel
+                ylabels[dpsname] = h.ylabel
+        else:
+            if dpsname.count('/') > 2:
+                dpsname = '/' + dpsname.split('/', 2)[-1]
+            titles[dpsname] = h.title
+            xlabels[dpsname] = h.xlabel
+            ylabels[dpsname] = h.ylabel
+        h.expt = dpsname.split("_")[0][1:]
+        ## Hard-coded cosmetic handling for the D0 experiment name!
+        if h.expt == "D0":
+            h.expt = "D\O\ "
+        histos[dpsname] = h
+    return histos, titles, xlabels, ylabels
 
-if __name__ == "__main__":
-    import os, re, logging
-
-    PROGPATH = sys.argv[0]
-    PROGNAME = os.path.basename(PROGPATH)
-
-    ## Try to rename the process on Linux
-    try:
-        import ctypes
-        libc = ctypes.cdll.LoadLibrary('libc.so.6')
-        libc.prctl(15, 'compare-histos', 0, 0, 0)
-    except Exception:
-        pass
-
-    ## Try to use Psyco optimiser
-    try:
-        import psyco
-        psyco.full()
-    except ImportError:
-        pass
-
-    ## Get Rivet data dir
-    rivet_data_dirs = None
-    try:
-        import rivet
-        rivet_data_dirs = rivet.getAnalysisRefPaths()
-    except Exception, e:
-        sys.stderr.write(PROGNAME + " requires the 'rivet' Python module\n")
-        logging.debug(str(e))
-        sys.exit(1)
 
+def getCommandLineOptions():
     ## Parse command line options
     from optparse import OptionParser, OptionGroup
     parser = OptionParser(usage=__doc__)
@@ -138,7 +160,43 @@
     verbgrp.add_option("-v", "--verbose", help="Add extra debug messages", dest="LOGLEVEL",
                        action="store_const", default=logging.INFO, const=logging.DEBUG)
     parser.add_option_group(verbgrp)
+    return parser
+
+
+##################################################################
+
+if __name__ == "__main__":
+    import os, re, logging
+
+    PROGPATH = sys.argv[0]
+    PROGNAME = os.path.basename(PROGPATH)
+
+    ## Try to rename the process on Linux
+    try:
+        import ctypes
+        libc = ctypes.cdll.LoadLibrary('libc.so.6')
+        libc.prctl(15, 'compare-histos', 0, 0, 0)
+    except Exception:
+        pass
 
+    ## Try to use Psyco optimiser
+    try:
+        import psyco
+        psyco.full()
+    except ImportError:
+        pass
+
+    ## Get Rivet data dir
+    rivet_data_dirs = None
+    try:
+        import rivet
+        rivet_data_dirs = rivet.getAnalysisRefPaths()
+    except Exception, e:
+        sys.stderr.write(PROGNAME + " requires the 'rivet' Python module\n")
+        logging.debug(str(e))
+        sys.exit(1)
+
+    parser = getCommandLineOptions()
     opts, args = parser.parse_args()
 
 
@@ -187,15 +245,15 @@
             for ls in LINESTYLES:
                 STYLES.append( (c, ls) )
 
+
     ## Get file names and labels
     FILES = []
-    FILEOPTIONS = { }
+    REFFILES = []
+    FILEOPTIONS = {}
     if opts.RIVETREFS and rivet_data_dirs:
-        reffiles = []
         for d in rivet_data_dirs:
             import glob
-            reffiles += glob.glob(os.path.join(d, "*.aida"))
-        args = reffiles + args
+            REFFILES += glob.glob(os.path.join(d, "*.aida"))
     for a in args:
         asplit = a.split(":")
         path = asplit[0]
@@ -207,6 +265,8 @@
                 asplit[i] = "Title=%s" % asplit[i]
             FILEOPTIONS[path].append(asplit[i])
 
+    ## Ignore duplicates
+    REFFILES = list(set(REFFILES))
 
     ## Check that the requested files are sensible
     if (len(FILES) < 1):
@@ -221,44 +281,6 @@
             sys.exit(2)
 
 
-    def getHistos(aidafile):
-        '''Get a dictionary of histograms indexed by name.'''
-        if not re.match(r'.*\.aida$', aidafile):
-            logging.error("Error: input file '%s' is not an AIDA file" % aidafile)
-            sys.exit(2)
-        aidafilepath = os.path.abspath(aidafile)
-        if not os.access(aidafilepath, os.R_OK):
-            logging.error("Error: cannot read from %s" % aidafile)
-            sys.exit(2)
-
-        histos, titles, xlabels, ylabels = {}, {}, {}, {}
-        tree = ET.parse(aidafilepath)
-        for dps in tree.findall("dataPointSet"):
-            ## Get this histogram's path name
-            dpsname = os.path.join(dps.get("path"), dps.get("name"))
-            h = Histo.fromDPS(dps)
-            ## Is it a data histo?
-            h.isdata = dpsname.upper().startswith("/REF")
-            if h.isdata:
-                dpsname = dpsname.replace("/REF", "")
-                if not titles.has_key(dpsname):
-                    titles[dpsname] = h.title
-                    xlabels[dpsname] = h.xlabel
-                    ylabels[dpsname] = h.ylabel
-            else:
-                if dpsname.count('/') > 2:
-                    dpsname = '/' + dpsname.split('/', 2)[-1]
-                titles[dpsname] = h.title
-                xlabels[dpsname] = h.xlabel
-                ylabels[dpsname] = h.ylabel
-            h.expt = dpsname.split("_")[0][1:]
-            ## Hard-coded cosmetic handling for the D0 experiment name!
-            if h.expt == "D0":
-                h.expt = "D\O\ "
-            histos[dpsname] = h
-        return histos, titles, xlabels, ylabels
-
-
     ## Read histo data from files into data structures
     HISTOS = {}
     TITLES = {}
@@ -267,10 +289,10 @@
     LABELS = {}
     NAMES = set()
     MCNAMES = set()
-    for f in FILES:
+    for f in FILES+REFFILES:
         HISTOS[f] = {}
         LABELS[f] = {}
-    for f in FILES:
+    for f in FILES+REFFILES:
         histos, titles, xlabels, ylabels = getHistos(f)
         for n, h in histos.iteritems():
             if h.isdata:
@@ -313,20 +335,6 @@
         MCNAMES = MCNAMES.intersection(hnames)
 
 
-    ## Function to make output dirs
-    def mkoutdir(outdir):
-        if not os.path.exists(outdir):
-            try:
-                os.makedirs(outdir)
-            except:
-                msg = "Can't make output directory '%s'" % outdir
-                logging.error(msg)
-                raise Exception(msg)
-        if not os.access(outdir, os.W_OK):
-            msg = "Can't write to output directory '%s'" % outdir
-            logging.error(msg)
-            raise Exception(msg)
-
     ## Pre-emptively reduce number of files to iterate through
     ## (assuming, reasonably, that there is only one ref file per histo)
     activenames = NAMES
@@ -351,6 +359,12 @@
         ## Identify contributing data files for this histo
         activemcfiles = []
         activereffiles = []
+        for f in REFFILES:
+            if HISTOS.has_key(f):
+                d = HISTOS[f]
+                if d.has_key(name):
+                    if d[name].isdata:
+                        activereffiles.append(f)
         for f in FILES:
             if HISTOS.has_key(f):
                 d = HISTOS[f]
@@ -361,6 +375,7 @@
                         activemcfiles.append(f)
         activefiles = activereffiles + activemcfiles
         #print activereffiles
+        #print activemcfiles
         #print activefiles
         if len(activefiles) == 0:
             logging.warning("Something's wrong... somehow there's no data for histogram '%s'!" % name)


More information about the Rivet-svn mailing list