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

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Tue Dec 6 17:09:01 GMT 2011


Author: buckley
Date: Tue Dec  6 17:09:01 2011
New Revision: 409

Log:
Adding annotation-fetching methods with a default return value argument to AnalysisObject. Making Histo1D/2D::scaleW() write a ScaledBy annotation.

Modified:
   trunk/ChangeLog
   trunk/include/YODA/AnalysisObject.h
   trunk/include/YODA/Histo1D.h
   trunk/include/YODA/Histo2D.h

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	Tue Dec  6 16:10:44 2011	(r408)
+++ trunk/ChangeLog	Tue Dec  6 17:09:01 2011	(r409)
@@ -1,5 +1,10 @@
 2011-12-06  Andy Buckley  <andy.buckley at cern.ch>
 
+	* Making Histo1D/2D::scaleW() write a ScaledBy annotation.
+
+	* Adding annotation-fetching methods with a default return value
+	argument to AnalysisObject.
+
 	* Adding normalize() methods to Histo1D/2D.
 
 	* Adding weighted RMS calculating methods to Dbn1D, Dbn2D and

Modified: trunk/include/YODA/AnalysisObject.h
==============================================================================
--- trunk/include/YODA/AnalysisObject.h	Tue Dec  6 16:10:44 2011	(r408)
+++ trunk/include/YODA/AnalysisObject.h	Tue Dec  6 17:09:01 2011	(r409)
@@ -70,38 +70,22 @@
     ///@name Annotations
     //@{
 
-    /// @brief Add or set an annotation by name
-    /// Note: Templated on arg type, but stored as a string.
-    template <typename T>
-    void setAnnotation(const std::string& name, const T& value) {
-      _annotations[name] = boost::lexical_cast<std::string>(value);
+    /// Get all the annotations (as const ref)
+    const Annotations& annotations() const {
+      return _annotations;
     }
 
-    /// @brief Add or set an annotation by name
-    /// Note: Templated on arg type, but stored as a string. Synonym for setAnnotation
-    template <typename T>
-    void addAnnotation(const std::string& name, const T& value) {
-      setAnnotation(name, value);
-    }
 
     /// Check if an annotation is defined
     bool hasAnnotation(const std::string& name) const {
       return _annotations.find(name) != _annotations.end();
     }
 
-    /// Get all the annotations (as const ref)
-    const Annotations& annotations() const {
-      return _annotations;
-    }
-
-    /// Set all annotations at once
-    void setAnnotations(const Annotations& anns) {
-      _annotations = anns;
-    }
 
     /// @brief Get an annotation by name (as a string)
     const std::string& annotation(const std::string& name) const {
       Annotations::const_iterator v = _annotations.find(name);
+      // If not found... written this way round on purpose
       if (v == _annotations.end()) {
         std::string missing = "YODA::AnalysisObject: No annotation named " + name;
         throw AnnotationError(missing);
@@ -109,8 +93,18 @@
       return v->second;
     }
 
+
+    /// @brief Get an annotation by name (as a string) with a default in case the annotation is not found
+    const std::string& annotation(const std::string& name, const std::string& defaultreturn) const {
+      Annotations::const_iterator v = _annotations.find(name);
+      if (v != _annotations.end()) return v->second;
+      return defaultreturn;
+    }
+
+
     /// @brief Get an annotation by name (copied to another type)
-    /// Note: templated on return type, with default as string
+    ///
+    /// Note: templated on return type
     template <typename T>
     const T annotation(const std::string& name) const {
       std::string s = annotation(name);
@@ -118,11 +112,50 @@
     }
 
 
+    /// @brief Get an annotation by name (copied to another type) with a default in case the annotation is not found
+    ///
+    /// Note: templated on return type
+    template <typename T>
+    const T annotation(const std::string& name, const T& defaultreturn) const {
+      try {
+        std::string s = annotation(name);
+        return boost::lexical_cast<T>(s);
+      } catch (const AnnotationError& ae) {
+        return defaultreturn;
+      }
+    }
+
+
+    /// @brief Add or set an annotation by name
+    ///
+    /// Note: Templated on arg type, but stored as a string.
+    template <typename T>
+    void setAnnotation(const std::string& name, const T& value) {
+      _annotations[name] = boost::lexical_cast<std::string>(value);
+    }
+
+
+    /// Set all annotations at once
+    void setAnnotations(const Annotations& anns) {
+      _annotations = anns;
+    }
+
+
+    /// @brief Add or set an annotation by name
+    ///
+    /// Note: Templated on arg type, but stored as a string. This is just a synonym for setAnnotation.
+    template <typename T>
+    void addAnnotation(const std::string& name, const T& value) {
+      setAnnotation(name, value);
+    }
+
+
     /// Delete an annotation by name
     void rmAnnotation(const std::string& name) {
       _annotations.erase(name);
     }
 
+
     /// Delete an annotation by name
     void clearAnnotations() {
       _annotations.clear();

Modified: trunk/include/YODA/Histo1D.h
==============================================================================
--- trunk/include/YODA/Histo1D.h	Tue Dec  6 16:10:44 2011	(r408)
+++ trunk/include/YODA/Histo1D.h	Tue Dec  6 17:09:01 2011	(r409)
@@ -109,6 +109,7 @@
 
     /// Rescale as if all fill weights had been different by factor @a scalefactor.
     void scaleW(double scalefactor) {
+      setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
       _axis.scaleW(scalefactor);
     }
 
@@ -119,7 +120,9 @@
     /// the overflow bins included, so that the resulting visible normalisation can
     /// be less than @a normto. This is probably what you want.
     void normalize(double normto=1.0, bool includeoverflows=true) {
-      _axis.scaleW(normto / integral(includeoverflows));
+      const double oldintegral = integral(includeoverflows);
+      if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
+      _axis.scaleW(normto / oldintegral);
     }
 
 

Modified: trunk/include/YODA/Histo2D.h
==============================================================================
--- trunk/include/YODA/Histo2D.h	Tue Dec  6 16:10:44 2011	(r408)
+++ trunk/include/YODA/Histo2D.h	Tue Dec  6 17:09:01 2011	(r409)
@@ -106,6 +106,7 @@
 
     /// Rescale as if all fill weights had been different by factor @a scalefactor.
     void scaleW(double scalefactor) {
+      setAnnotation("ScaledBy", annotation<double>("ScaledBy", 1.0) * scalefactor);
       _axis.scaleW(scalefactor);
     }
 
@@ -116,7 +117,9 @@
     /// the overflow bins included, so that the resulting visible normalisation can
     /// be less than @a normto. This is probably what you want.
     void normalize(double normto=1.0, bool includeoverflows=true) {
-      _axis.scaleW(normto / integral(includeoverflows));
+      const double oldintegral = integral(includeoverflows);
+      if (oldintegral == 0) throw WeightError("Attempted to normalize a histogram with null area");
+      _axis.scaleW(normto / oldintegral);
     }
 
 


More information about the yoda-svn mailing list