[Rivet-svn] r2581 - trunk/bin

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Mon Jul 12 23:24:01 BST 2010


Author: buckley
Date: Mon Jul 12 23:24:15 2010
New Revision: 2581

Log:
Making rivet-rmgaps automatically find Rivet ref data using -R flag, cf. compare-histos etc.

Modified:
   trunk/bin/rivet-rmgaps

Modified: trunk/bin/rivet-rmgaps
==============================================================================
--- trunk/bin/rivet-rmgaps	Mon Jul 12 16:53:03 2010	(r2580)
+++ trunk/bin/rivet-rmgaps	Mon Jul 12 23:24:15 2010	(r2581)
@@ -8,9 +8,6 @@
 leave gaps between bins, hence this clean-up script.
 
 If the output file is not specified, the input MC file will be overwritten.
-
-TODO:
- * Remove need to specify the ref file.
 """
 
 import sys
@@ -22,18 +19,21 @@
 import os, tempfile
 
 class Inputdata:
-    def __init__(self, filename):
+    def __init__(self, filenames):
         self.histos = {}
         self.description = {}
         self.description['DrawOnly'] = []
-        f = open(filename+'.dat', 'r')
-        for line in f:
-            if (line.count('#',0,1)):
-                if (line.count('BEGIN HISTOGRAM')):
-                    title = line.split('BEGIN HISTOGRAM', 1)[1].strip()
-                    self.description['DrawOnly'].append(title)
-                    self.histos[title] = Histogram(f)
-        f.close()
+        if not hasattr(filenames, "__iter__"):
+            filenames = [filenames]
+        for filename in filenames:
+            f = open(filename+'.dat', 'r')
+            for line in f:
+                if (line.count('#',0,1)):
+                    if (line.count('BEGIN HISTOGRAM')):
+                        title = line.split('BEGIN HISTOGRAM', 1)[1].strip()
+                        self.description['DrawOnly'].append(title)
+                        self.histos[title] = Histogram(f)
+            f.close()
 
 
 class Histogram:
@@ -49,17 +49,17 @@
                     break
             else:
                 line = line.rstrip()
-                if (line.count('=')):
+                if line.count('='):
                     linearray = line.split('=', 1)
                     self.description[linearray[0]] = linearray[1]
                 else:
                     linearray = line.split('\t')
-                    if len(linearray)==4:
+                    if len(linearray) == 4:
                         self.data.append({'LowEdge': float(linearray[0]),
                                           'UpEdge':  float(linearray[1]),
                                           'Content': float(linearray[2]),
                                           'Error':   [float(linearray[3]),float(linearray[3])]})
-                    elif len(linearray)==5:
+                    elif len(linearray) == 5:
                         self.data.append({'LowEdge': float(linearray[0]),
                                           'UpEdge':  float(linearray[1]),
                                           'Content': float(linearray[2]),
@@ -111,62 +111,129 @@
         self.write_datapointset_footer(f)
 
     def remove_gaps(self):
-        # only look at histograms which are present in the reference file:
+        ## Only look at histograms which are present in the reference file:
         try:
             refhist = refdata.histos['/REF%s' % self.description['AidaPath']]
         except:
             return
 
-        # check for differences in the binning and remove superfluous MC bins:
+        ## Check for differences in the binning and remove superfluous MC bins:
         if len(refhist.data) != len(self.data):
-            print self.description['AidaPath']
+            logging.info(self.description['AidaPath'])
             newdata = []
             for i in range(len(self.data)):
                 if self.data[i]['LowEdge'] == refhist.data[i]['LowEdge'] and \
                    self.data[i]['UpEdge'] == refhist.data[i]['UpEdge']:
                     newdata.append(self.data[i])
                 else:
-                    print 'Deleted bin %d' %i
+                    logging.info('Deleted bin %d' % i)
                     refhist.data.insert(i, self.data[i])
             self.data = newdata
 
 
+## Command line parsing
+import logging
 from optparse import OptionParser
 parser = OptionParser(usage=__doc__)
+parser.add_option("-R", "--rivet-refs", dest="USE_RIVETREFS", action="store_true", default=False,
+                  help="use the Rivet reference files by default")
+parser.add_option("-v", "--verbose", action="store_const", const=logging.DEBUG, dest="LOGLEVEL",
+                  default=logging.INFO, help="print debug (very verbose) messages")
+parser.add_option("-q", "--quiet", action="store_const", const=logging.WARNING, dest="LOGLEVEL",
+                  default=logging.INFO, help="be very quiet")
 opts, args = parser.parse_args()
 
-if len(args) < 2:
-    sys.stderr.write("Must specify at least the reference and MC input files\n")
-    sys.exit(1)
-OUTFILE = args[1]
-if len(args) == 3:
-    OUTFILE = args[2]
+## Configure logging
+logging.basicConfig(level=opts.LOGLEVEL, format="%(message)s")
+
+
+if opts.USE_RIVETREFS:
+    REFDIRS = []
+    try:
+        import rivet
+        REFDIRS = rivet.getAnalysisRefPaths()
+    except:
+        logging.error("Could not find the Rivet ref paths because the 'rivet' Python module could not be loaded")
+        sys.exit(1)
+
+
+if opts.USE_RIVETREFS:
+    if len(args) < 1:
+        logging.error("Must specify at least the MC input file")
+        sys.exit(1)
+    if len(args) >= 1:
+        REFFILES = []
+        INFILE = args[0]
+        OUTFILE = args[0]
+    if len(args) == 2:
+        OUTFILE = args[1]
+    if len(args) > 2:
+        logging.error("Maximum of two arguments with the -R argument flag!")
+        sys.exit(1)
+else:
+    if len(args) < 2:
+        logging.error("Must specify at least the reference file and the MC input file")
+        sys.exit(1)
+    if len(args) >= 2:
+        REFFILES = [args[0]]
+        INFILE = args[1]
+        OUTFILE = args[1]
+    if len(args) == 3:
+        OUTFILE = args[2]
+    if len(args) > 3:
+        logging.error("Maximum of three arguments with the -R argument flag!")
+        sys.exit(1)
+
 
-# Convert the aida input files to flat files we can parse:
+## Convert the aida input files to flat files we can parse:
 tempdir = tempfile.mkdtemp('.gap_removal')
 
-filename = args[0].replace(".aida", "")
+filename = INFILE.replace(".aida", "")
 os.system("%s/aida2flat %s.aida > %s/%s.dat" % \
               (os.path.dirname(os.path.realpath(sys.argv[0])),
                filename, tempdir, os.path.basename(filename)))
-refdata = Inputdata("%s/%s" % (tempdir, os.path.basename(filename)))
+mcdata = Inputdata(os.path.join(tempdir, os.path.basename(filename)))
 
-filename = args[1].replace(".aida", "")
-os.system("%s/aida2flat %s.aida > %s/%s.dat" % \
-              (os.path.dirname(os.path.realpath(sys.argv[0])),
-               filename, tempdir, os.path.basename(filename)))
-mcdata = Inputdata("%s/%s" % (tempdir, os.path.basename(filename)))
 
-# Cleanup:
+## Build list of Rivet ref files
+if opts.USE_RIVETREFS:
+    anas = set()
+    for hpath in mcdata.description['DrawOnly']:
+        ana = hpath[1:].split("/")[0]
+        anas.add(ana)
+    #print anas
+    for a in anas:
+        for rd in REFDIRS:
+            rp = os.path.join(rd, a+".aida")
+            if os.access(rp, os.R_OK):
+                REFFILES.append(rp)
+                break
+    #print REFFILES
+
+
+## Run over ref files
+refdatfiles = []
+for rf in REFFILES:
+    filename = rf.replace(".aida", "")
+    os.system("%s/aida2flat %s.aida > %s/%s.dat" % \
+                  (os.path.dirname(os.path.realpath(sys.argv[0])),
+                   filename, tempdir, os.path.basename(filename)))
+    refdatfiles.append( os.path.join(tempdir, os.path.basename(filename)) )
+refdata = Inputdata(refdatfiles)
+
+
+## Clean up
 for i in os.listdir(tempdir):
     os.unlink('%s/%s' %(tempdir, i))
 os.rmdir(tempdir)
 
-# Remove gap bins:
+
+## Remove gap bins
 for i in mcdata.description['DrawOnly']:
     mcdata.histos[i].remove_gaps()
 
-# Write the new aida file with removed gap bins:
+
+## Write the new aida file with removed gap bins:
 f = open(OUTFILE, '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')


More information about the Rivet-svn mailing list