[yoda-svn] r553 - in trunk: . include/YODA src

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Sun Dec 30 18:06:01 GMT 2012


Author: buckley
Date: Sun Dec 30 18:06:01 2012
New Revision: 553

Log:
Adding support for Boost.Range arguments and file format autodetection in Writer.

Modified:
   trunk/ChangeLog
   trunk/TODO
   trunk/include/YODA/Histo1D.h
   trunk/include/YODA/Profile1D.h
   trunk/include/YODA/Writer.h
   trunk/include/YODA/WriterAIDA.h
   trunk/src/Writer.cc
   trunk/src/WriterAIDA.cc

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/ChangeLog	Sun Dec 30 18:06:01 2012	(r553)
@@ -1,3 +1,7 @@
+2012-12-30  Andy Buckley  <andy.buckley at cern.ch>
+
+	* Adding support for Boost.Range arguments and file format autodetection in Writer.
+
 2012-11-24  Andy Buckley  <andy.buckley at cern.ch>
 
 	* Bump version to 0.6beta0

Modified: trunk/TODO
==============================================================================
--- trunk/TODO	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/TODO	Sun Dec 30 18:06:01 2012	(r553)
@@ -3,10 +3,7 @@
 
 NOW
 
-
-* Auto-determine input and output formats (= readers/writers) from file extn (AB)
-
-* Make Python interface test scripts (DM,AB)
+* Make Python interface test scripts (DM, AB)
    Test Histo2D and Scatter2D from Python
 
 * Add copy assignment to both C++ and Cython (AB)
@@ -22,37 +19,39 @@
 
 NEXT
 
+* Add a Counter type (mostly a UI/persistency wrapper on Dbn1D) (AB)
+   "Binning"/"axis" option on arbitrary types, particularly strings? ->
+   generalised bar charts. Make counter a pure sumW, sumW2 object and use it in
+   implementation of Dbn1D/2D.
+
+* Generalise/expose Axis*D for binning of general objects, e.g. "binned histos"
+
+* "BinnedValues" type: use Axis behaviour to look up fixed values without Dbn "history".
+
+* Test negative- and mixed-weighted stat calculations, and scaling.
+
+
+AND THEN...
+
 * Remove non-const bin access from Histos and Profiles?
    cf. David D's point re. consistency via email on 7/8/2012
    In practice they can't be removed as they are used internally. Attempting to
    make them protected/private didn't work due to requirements of the Python
-   wrapper code... leaving it be for now.
+   wrapper code... leaving it as-is for now.
 
 * Add Axis2D -> Histo2D/Profile2D bin adding and erasing. (AB)
    Adding 2D operator support and bin-adding/erasing
 
-* Add a Counter type (mostly a UI/persistency wrapper on Dbn1D) (AB)
-   "Binning"/"axis" option on arbitrary types, particularly strings? ->
-   generalised bar charts. Make counter a pure sumW, sumW2 object and use it in
-   implementation of Dbn1D/2D.
-
-* Generalise/expose Axis*D for binning of general objects, e.g. "binned histos"
-
 * 2D bin merging and rebinning (AB)
    Only rebin perfect grids: need to detect them. Global rebinning of Axis2D ->
    Histo2D by generally different integer factors in x and y directions.
 
-* Test negative- and mixed-weighted stat calculations, and scaling.
-
 * Test gap handling on 1D and 2D axes.
 
 * Add Histo2D binning constructors from Scatter3D and Profile2D.
 
 * Add Scatter1D/Point1D (AB)
 
-* "BinnedValues" type: use Axis behaviour to look up fixed values without Dbn "history".
-
-
 
 AND AFTER THAT...
 
@@ -63,8 +62,6 @@
 
 * ReaderFlat -> Scatter2D
 
-* Simple ntupling, done well :-)
-
 * Allow comments on ends of YODA format data lines in ReaderYODA (HH)
    Ignore anything after "#" on data lines -- use this to write convenience
    height & error info for each bin since not obvious from sumWX2 etc.
@@ -81,4 +78,9 @@
 * Template all classes on floating point type (default to double)
 
 * Look into pickling of C++ interfaced classes. WriterS10n? MessagePack? Protobuf?
-  The latter two look awesome...
+
+* Simple ntupling, done well :-)
+   Surprisingly the Protobuf, MsgPack, etc. systems don't seem designed for
+   ntuples... surely something is? Maybe wrap HDF5 or similar? Not to be
+   embarked upon lightly: we'd need to be certain that we could make something
+   really nice.

Modified: trunk/include/YODA/Histo1D.h
==============================================================================
--- trunk/include/YODA/Histo1D.h	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/include/YODA/Histo1D.h	Sun Dec 30 18:06:01 2012	(r553)
@@ -71,15 +71,19 @@
             _axis(bins)
     { }
 
+
     /// Copy constructor with optional new path
+    /// @todo Don't copy the path?
     Histo1D(const Histo1D& h, const std::string& path="");
 
 
     /// Constructor from a Scatter2D's binning, with optional new path
+    /// @todo Don't copy the path?
     Histo1D(const Scatter2D& s, const std::string& path="");
 
 
     /// Constructor from a Profile1D's binning, with optional new path
+    /// @todo Don't copy the path?
     Histo1D(const Profile1D& p, const std::string& path="");
 
 

Modified: trunk/include/YODA/Profile1D.h
==============================================================================
--- trunk/include/YODA/Profile1D.h	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/include/YODA/Profile1D.h	Sun Dec 30 18:06:01 2012	(r553)
@@ -68,14 +68,17 @@
 
 
     /// Copy constructor with optional new path
+    /// @todo Don't copy the path?
     Profile1D(const Profile1D& p, const std::string& path="");
 
 
     /// Constructor from a Scatter2D's binning, with optional new path
+    /// @todo Don't copy the path?
     Profile1D(const Scatter2D& s, const std::string& path="");
 
 
     /// Constructor from a Histo1D's binning, with optional new path
+    /// @todo Don't copy the path?
     Profile1D(const Histo1D& h, const std::string& path="");
 
 

Modified: trunk/include/YODA/Writer.h
==============================================================================
--- trunk/include/YODA/Writer.h	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/include/YODA/Writer.h	Sun Dec 30 18:06:01 2012	(r553)
@@ -13,6 +13,8 @@
 #include "YODA/Profile1D.h"
 #include "YODA/Scatter2D.h"
 
+#include "boost/range.hpp"
+
 #include <string>
 #include <fstream>
 
@@ -43,42 +45,16 @@
     //@{
 
     /// Write out a collection of objects @a objs to output stream @a stream.
-    void write(std::ostream& stream, const std::vector<AnalysisObject*>& aos) {
-      write(stream, aos.begin(), aos.end());
-    }
-    /// Write out a collection of objects @a objs to file @a filename.
-    void write(const std::string& filename, const std::vector<AnalysisObject*>& aos) {
-      write(filename, aos.begin(), aos.end());
-    }
-
-
-    /// Write out a collection of objects @a objs to output stream @a stream.
-    void write(std::ostream& stream, const std::list<AnalysisObject*>& aos) {
-      write(stream, aos.begin(), aos.end());
-    }
-    /// Write out a collection of objects @a objs to file @a filename.
-    void write(const std::string& filename, const std::list<AnalysisObject*>& aos) {
-      write(filename, aos.begin(), aos.end());
-    }
-
-
-    /// Write out a collection of objects @a objs to output stream @a stream.
-    void write(std::ostream& stream, const std::set<AnalysisObject*>& aos) {
-      write(stream, aos.begin(), aos.end());
+    template <typename RANGE>
+    void write(std::ostream& stream, const RANGE& aos) {
+      typedef typename boost::range_iterator<const RANGE>::type const_iterator;
+      write(stream, boost::begin(aos), boost::end(aos));
     }
     /// Write out a collection of objects @a objs to file @a filename.
-    void write(const std::string& filename, const std::set<AnalysisObject*>& aos) {
-      write(filename, aos.begin(), aos.end());
-    }
-
-
-    /// Write out a collection of objects @a objs to output stream @a stream.
-    void write(std::ostream& stream, const std::deque<AnalysisObject*>& aos) {
-      write(stream, aos.begin(), aos.end());
-    }
-    /// Write out a collection of objects @a objs to file @a filename.
-    void write(const std::string& filename, const std::deque<AnalysisObject*>& aos) {
-      write(filename, aos.begin(), aos.end());
+    template <typename RANGE>
+    void write(const std::string& filename, const RANGE& aos) {
+      typedef typename boost::range_iterator<const RANGE>::type const_iterator;
+      write(filename, boost::begin(aos), boost::end(aos));
     }
 
     //@}
@@ -110,6 +86,37 @@
       outstream.close();
     }
 
+
+    //@}
+
+
+    /// @name Static functions with automatic file extension detection
+    //@{
+
+    /// Factory function to make a writer object by format name or a filename (upper or lower case).
+    static Writer& makeWriter(const std::string& format_name);
+
+    /// Write out object @a ao to file @a filename
+    static void writeTo(const std::string& filename, const AnalysisObject& ao) {
+      Writer& w = makeWriter(filename);
+      w.write(filename, ao);
+    }
+
+    /// Write out a collection of objects @a objs to file @a filename.
+    template <typename RANGE>
+    static void writeTo(const std::string& filename, const RANGE& aos) {
+      Writer& w = makeWriter(filename);
+      w.write(filename, aos);
+    }
+
+    /// @brief Write out the objects specified by start iterator @a begin and end
+    /// iterator @a end to file @a filename.
+    template <typename AOITER>
+    static void writeTo(const std::string& filename, const AOITER& begin, const AOITER& end) {
+      Writer& w = makeWriter(filename);
+      w.write(filename, begin, end);
+    }
+
     //@}
 
 

Modified: trunk/include/YODA/WriterAIDA.h
==============================================================================
--- trunk/include/YODA/WriterAIDA.h	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/include/YODA/WriterAIDA.h	Sun Dec 30 18:06:01 2012	(r553)
@@ -24,8 +24,7 @@
     /// Singleton creation function
     static Writer& create() {
       static WriterAIDA _instance;
-      // Unused anyway; but if used, does not work in yoda2aida
-      // _instance.setPrecision(6);
+      _instance.setPrecision(6);
       return _instance;
     }
 

Modified: trunk/src/Writer.cc
==============================================================================
--- trunk/src/Writer.cc	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/src/Writer.cc	Sun Dec 30 18:06:01 2012	(r553)
@@ -5,6 +5,10 @@
 //
 
 #include "YODA/Writer.h"
+#include "YODA/WriterYODA.h"
+#include "YODA/WriterAIDA.h"
+#include "YODA/WriterFLAT.h"
+#include "boost/algorithm/string.hpp"
 #include <iostream>
 #include <typeinfo>
 #include <sstream>
@@ -14,6 +18,16 @@
 namespace YODA {
 
 
+  Writer& Writer::makeWriter(const std::string& name) {
+    const size_t lastdot = name.find_last_of(".");
+    const string fmt = boost::to_lower_copy((lastdot == std::string::npos) ? name : name.substr(lastdot+1));
+    if (fmt == "yoda") return WriterYODA::create();
+    if (fmt == "aida") return WriterAIDA::create();
+    if (fmt == "flat") return WriterFLAT::create();
+    throw UserError("Format cannot be identified from string '" + name + "'");
+  }
+
+
   void Writer::write(std::ostream& stream, const AnalysisObject& ao) {
     writeHeader(stream);
     writeBody(stream, ao);

Modified: trunk/src/WriterAIDA.cc
==============================================================================
--- trunk/src/WriterAIDA.cc	Wed Nov 28 11:45:07 2012	(r552)
+++ trunk/src/WriterAIDA.cc	Sun Dec 30 18:06:01 2012	(r553)
@@ -62,8 +62,8 @@
 
   void WriterAIDA::writeScatter2D(std::ostream& os, const Scatter2D& s) {
     ios_base::fmtflags oldflags = os.flags();
-    const int precision = 8;
-    os << scientific << showpoint << setprecision(precision);
+    // const int precision = 8;
+    os << scientific << showpoint << setprecision(_precision);
 
     string name = "";
     string path = "/";
@@ -90,12 +90,12 @@
     foreach (Point2D pt, s.points()) {
       os << "    <dataPoint>\n";
       os << "      <measurement value=\"" << pt.x()
-	 << "\" errorPlus=\"" << pt.xErrPlus() 
-         << "\" errorMinus=\"" << pt.xErrMinus() 
+	 << "\" errorPlus=\"" << pt.xErrPlus()
+         << "\" errorMinus=\"" << pt.xErrMinus()
 	 << "\"/>\n";
       os << "      <measurement value=\"" << pt.y()
-	 << "\" errorPlus=\"" << pt.yErrPlus() 
-         << "\" errorMinus=\"" << pt.yErrMinus() 
+	 << "\" errorPlus=\"" << pt.yErrPlus()
+         << "\" errorMinus=\"" << pt.yErrMinus()
 	 << "\"/>\n";
       os << "    </dataPoint>\n";
     }


More information about the yoda-svn mailing list