[yoda-svn] r543 - in trunk: . bin include/YODA pyext/yoda pyext/yoda/include src

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Fri Nov 16 16:28:51 GMT 2012


Author: hoeth
Date: Fri Nov 16 16:28:51 2012
New Revision: 543

Log:
Adding WriterFLAT and yoda2flat

Added:
   trunk/bin/yoda2flat   (contents, props changed)
   trunk/include/YODA/WriterFLAT.h
   trunk/src/WriterFLAT.cc
Modified:
   trunk/ChangeLog
   trunk/bin/Makefile.am
   trunk/include/YODA/Makefile.am
   trunk/pyext/yoda/declarations.pxd
   trunk/pyext/yoda/include/IO.pyx
   trunk/src/Makefile.am

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/ChangeLog	Fri Nov 16 16:28:51 2012	(r543)
@@ -1,3 +1,7 @@
+2012-11-16  Hendrik Hoeth  <hendrik.hoeth at cern.ch>
+
+	* Adding WriterFLAT and yoda2flat
+
 2012-11-16  Andy Buckley  <andy.buckley at cern.ch>
 
 	* Adding toIntegralHisto(Histo1D&) function.

Modified: trunk/bin/Makefile.am
==============================================================================
--- trunk/bin/Makefile.am	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/bin/Makefile.am	Fri Nov 16 16:28:51 2012	(r543)
@@ -1 +1 @@
-dist_bin_SCRIPTS = yoda2aida yoda-plot
\ No newline at end of file
+dist_bin_SCRIPTS = yoda2aida yoda2flat yoda-plot

Added: trunk/bin/yoda2flat
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/bin/yoda2flat	Fri Nov 16 16:28:51 2012	(r543)
@@ -0,0 +1,29 @@
+#! /usr/bin/env python
+
+"""\
+%prog yodafile [flatfile]
+"""
+
+import yoda, sys, optparse
+
+parser = optparse.OptionParser(usage=__doc__)
+opts, args = parser.parse_args()
+
+INFILE = None
+OUTFILE = None
+
+if len(args) == 1:
+    INFILE = args[0]
+    if INFILE.endswith(".yoda"):
+        OUTFILE = INFILE.replace(".yoda", ".dat")
+    else:
+        OUTFILE = INFILE + ".dat"
+elif len(args) == 2:
+    INFILE = args[0]
+    OUTFILE = args[1]
+else:
+    sys.stderr.write("You must specify the YODA and FLAT file names\n")
+    sys.exit(1)
+
+analysisobjects = yoda.readYODA(INFILE)
+yoda.writeFLAT(analysisobjects, OUTFILE)

Modified: trunk/include/YODA/Makefile.am
==============================================================================
--- trunk/include/YODA/Makefile.am	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/include/YODA/Makefile.am	Fri Nov 16 16:28:51 2012	(r543)
@@ -1,9 +1,9 @@
 pkginclude_HEADERS = \
     Exceptions.h  \
     AnalysisObject.h \
-	Weights.h Bin.h \
-	Dbn1D.h Dbn2D.h Dbn3D.h \
-	Axis1D.h Bin1D.h \
+    Weights.h Bin.h \
+    Dbn1D.h Dbn2D.h Dbn3D.h \
+    Axis1D.h Bin1D.h \
     Axis2D.h Bin2D.h \
     Plot.h \
     Histo1D.h HistoBin1D.h  \
@@ -12,7 +12,7 @@
     Profile2D.h ProfileBin2D.h \
     Scatter2D.h Point2D.h \
     Scatter3D.h Point3D.h \
-    Writer.h WriterAIDA.h WriterYODA.h \
+    Writer.h WriterAIDA.h WriterFLAT.h WriterYODA.h \
     Reader.h ReaderAIDA.h ReaderYODA.h
 
 nobase_pkginclude_HEADERS = \
@@ -21,7 +21,7 @@
 	Utils/Formatting.h \
 	Utils/BinSearcher.h \
 	Utils/sortedvector.h \
-    Utils/cachedvector.h \
+	Utils/cachedvector.h \
 	Utils/nvector.h \
 	Utils/fastlog.h \
 	Config/YodaConfig.h \

Added: trunk/include/YODA/WriterFLAT.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/include/YODA/WriterFLAT.h	Fri Nov 16 16:28:51 2012	(r543)
@@ -0,0 +1,61 @@
+// -*- C++ -*-
+//
+// This file is part of YODA -- Yet more Objects for Data Analysis
+// Copyright (C) 2008-2012 The YODA collaboration (see AUTHORS for details)
+//
+#ifndef YODA_WRITERFLAT_H
+#define YODA_WRITERFLAT_H
+
+#include "YODA/AnalysisObject.h"
+#include "YODA/Writer.h"
+
+#include <vector>
+#include <string>
+#include <ostream>
+
+
+namespace YODA {
+
+
+  /// @brief Persistency writer for flat text format.
+  class WriterFLAT : public Writer {
+  public:
+
+    /// Singleton creation function
+    static Writer& create() {
+      static WriterFLAT _instance;
+      _instance.setPrecision(6);
+      return _instance;
+    }
+
+
+    // Include definitions of all write methods (all fulfilled by Writer::write(...))
+    #include "YODA/WriterMethods.icc"
+
+
+  protected:
+
+    void writeHeader(std::ostream& stream);
+    void writeFooter(std::ostream& stream);
+
+    void writePlot(std::ostream& stream, const Plot& p);
+    void writeHisto1D(std::ostream& stream, const Histo1D& h);
+    void writeHisto2D(std::ostream& stream, const Histo2D& h);
+    void writeProfile1D(std::ostream& stream, const Profile1D& p);
+    void writeScatter2D(std::ostream& stream, const Scatter2D& s);
+    //void writeScatter3D(std::ostream& stream, const Scatter3D& s);
+
+
+  private:
+
+    void _writeAnnotations(std::ostream& os, const AnalysisObject& ao);
+
+    /// Private since it's a singleton.
+    WriterFLAT() { }
+
+  };
+
+
+}
+
+#endif

Modified: trunk/pyext/yoda/declarations.pxd
==============================================================================
--- trunk/pyext/yoda/declarations.pxd	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/pyext/yoda/declarations.pxd	Fri Nov 16 16:28:51 2012	(r543)
@@ -807,6 +807,9 @@
 cdef extern from "YODA/WriterYODA.h" namespace "YODA":
     Writer& WriterYODA_create "YODA::WriterYODA::create" ()
 
+cdef extern from "YODA/WriterFLAT.h" namespace "YODA":
+    Writer& WriterFLAT_create "YODA::WriterFLAT::create" ()
+
 cdef extern from "YODA/WriterAIDA.h" namespace "YODA":
     Writer& WriterAIDA_create "YODA::WriterAIDA::create" ()
 # Streams }}}

Modified: trunk/pyext/yoda/include/IO.pyx
==============================================================================
--- trunk/pyext/yoda/include/IO.pyx	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/pyext/yoda/include/IO.pyx	Fri Nov 16 16:28:51 2012	(r543)
@@ -83,6 +83,25 @@
             f.write(oss.str().c_str())
 
 
+def writeFLAT(ana_objs, file_or_filename):
+    cdef c.ostringstream oss
+    cdef vector[c.AnalysisObject*] vec
+    cdef AnalysisObject a
+
+    for a in ana_objs:
+        vec.push_back(a._AnalysisObject())
+
+    # Most of the time we just won't care about memory
+    # Perhaps speak with andy re: huge files
+    c.WriterFLAT_create().write(oss, vec)
+
+    if hasattr(file_or_filename, 'write'):
+        file_or_filename.write(oss.str().c_str())
+    else:
+        with open(file_or_filename, 'w') as f:
+            f.write(oss.str().c_str())
+
+
 def writeAIDA(ana_objs, file_or_filename):
     cdef c.ostringstream oss
     cdef vector[c.AnalysisObject*] vec

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	Fri Nov 16 16:21:11 2012	(r542)
+++ trunk/src/Makefile.am	Fri Nov 16 16:28:51 2012	(r543)
@@ -15,6 +15,7 @@
     ReaderYODA.cc \
     Writer.cc \
     WriterAIDA.cc \
+    WriterFLAT.cc \
     WriterYODA.cc
 
 libYODA_la_LIBADD = tinyxml/libtinyxml.la

Added: trunk/src/WriterFLAT.cc
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/src/WriterFLAT.cc	Fri Nov 16 16:28:51 2012	(r543)
@@ -0,0 +1,108 @@
+// -*- C++ -*-
+//
+// This file is part of YODA -- Yet more Objects for Data Analysis
+// Copyright (C) 2008-2012 The YODA collaboration (see AUTHORS for details)
+//
+#include "YODA/WriterFLAT.h"
+
+#include "YODA/Plot.h"
+#include "YODA/Histo1D.h"
+#include "YODA/Histo2D.h"
+#include "YODA/Profile1D.h"
+#include "YODA/Scatter2D.h"
+
+#include <iostream>
+#include <iomanip>
+
+using namespace std;
+
+namespace YODA {
+
+
+  void WriterFLAT::writeHeader(std::ostream& os) {
+    os << flush;
+  }
+
+
+  void WriterFLAT::writeFooter(std::ostream& os) {
+    os << flush;
+  }
+
+
+  void WriterFLAT::_writeAnnotations(std::ostream& os, const AnalysisObject& ao) {
+    os << scientific << showpoint << setprecision(_precision);
+    typedef pair<string,string> sspair;
+    foreach (const sspair& kv, ao.annotations()) {
+      if (kv.first.empty() || kv.second.empty()) continue; // <- good idea?
+      os << kv.first << "=" << kv.second << "\n";
+    }
+  }
+
+
+  void WriterFLAT::writePlot(std::ostream& os, const Plot& p) {
+    _writeAnnotations(os, p);
+  }
+
+
+  void WriterFLAT::writeHisto1D(std::ostream& os, const Histo1D& h) {
+    Scatter2D tmp = mkScatter(h);
+    tmp.setAnnotation("Type", "Histo1D");
+    writeScatter2D(os, tmp);
+  }
+
+
+  void WriterFLAT::writeHisto2D(std::ostream& os, const Histo2D& h) {
+    // FIXME: Currently not supported
+    os << flush;
+
+    //Scatter3D tmp = mkScatter(h);
+    //tmp.setAnnotation("Type", "Histo2D");
+    //writeScatter3D(os, tmp);
+  }
+
+
+  void WriterFLAT::writeProfile1D(std::ostream& os, const Profile1D& p) {
+    Scatter2D tmp = mkScatter(p);
+    tmp.setAnnotation("Type", "Profile1D");
+    writeScatter2D(os, tmp);
+  }
+
+
+  void WriterFLAT::writeScatter2D(std::ostream& os, const Scatter2D& s) {
+    ios_base::fmtflags oldflags = os.flags();
+    os << scientific << showpoint << setprecision(_precision);
+
+    os << "# BEGIN HISTOGRAM " << s.path() << "\n";
+    _writeAnnotations(os, s);
+    os << "# xlow\t xhigh\t val\t errminus\t errplus\n";
+    foreach (Point2D pt, s.points()) {
+      os << pt.x()-pt.xErrMinus() << "\t" << pt.x()+pt.xErrPlus() << "\t";
+      os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrPlus() << "\n";
+    }
+    os << "# END HISTOGRAM\n\n";
+
+    os << flush;
+    os.flags(oldflags);
+  }
+
+
+  /*void WriterFLAT::writeScatter3D(std::ostream& os, const Scatter3D& s) {
+    ios_base::fmtflags oldflags = os.flags();
+    os << scientific << showpoint << setprecision(_precision);
+
+    os << "# BEGIN YODA_SCATTER3D " << s.path() << "\n";
+    _writeAnnotations(os, s);
+    os << "# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t zval\t zerr-\t zerr+\n";
+    foreach (Point3D pt, s.points()) {
+      os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrMinus() << "\t";
+      os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrMinus() << "\t";
+      os << pt.z() << "\t" << pt.zErrMinus() << "\t" << pt.zErrMinus() << "\n";
+    }
+    os << "# END YODA_SCATTER2D\n";
+
+    os << flush;
+    os.flags(oldflags);
+  }*/
+
+
+}


More information about the yoda-svn mailing list