From 29348a20e117740ce345e158d8c27b23c204f4f4 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Thu, 7 Sep 2023 14:22:37 +0200
Subject: [PATCH] Improve template function

Use std:array with template size as input parameter.
Allow template instantiation only for classes derived from THnSparse.
---
 core/base/CbmHistManager.h | 36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)
 mode change 100755 => 100644 core/base/CbmHistManager.h

diff --git a/core/base/CbmHistManager.h b/core/base/CbmHistManager.h
old mode 100755
new mode 100644
index 037b2208c7..d0eaeb3945
--- a/core/base/CbmHistManager.h
+++ b/core/base/CbmHistManager.h
@@ -28,12 +28,14 @@
 #include <TProfile.h>    // for TProfile
 #include <TProfile2D.h>  // for TProfile2D
 
-#include <cassert>  // for assert
-#include <map>      // for map, __map_const_iterator, operator!=
-#include <ostream>  // for string, operator<<, ostream
-#include <string>   // for operator<
-#include <utility>  // for pair, make_pair
-#include <vector>   // for vector
+#include <array>        // for array
+#include <cassert>      // for assert
+#include <map>          // for map, __map_const_iterator, operator!=
+#include <ostream>      // for string, operator<<, ostream
+#include <string>       // for operator<
+#include <type_traits>  // for is_base_of
+#include <utility>      // for pair, make_pair
+#include <vector>       // for vector
 
 class TFile;
 
@@ -169,20 +171,21 @@ public:
 
   /**
     * \brief Helper function for creation of THnSparse objects.
-    * Template argument is a real object type that has to be created, for example,
-    * CreateSparse<THnSparseD>("name", "title", nDim, nBins, minVals, maxVals);
+    * \tparam <T> { real object type that has to be created, for example THnSparseD }
+    * \tparam <nDim> { Array dimensions for nBins, minVals and maxVals }
+    * CreateSparse<THnSparseD, 3>("name", "title", nDim, nBins, minVals, maxVals);
     * \param[in] name Object name.
     * \param[in] title Object title.
-    * \param[in] nDim Number of dimensions.
-    * \param[in] nBins Array with number of bins for each dimension.
-    * \param[in] minVals Array with minimum values for each dimension.
-    * \param[in] maxVals Array with maximum values for each dimension.
+    * \param[in] nBins Array of size nDim with number of bins for each dimension.
+    * \param[in] minVals Array of size nDim with minimum values for each dimension.
+    * \param[in] maxVals Array of size nDim with maximum values for each dimension.
     */
-  template<class T>
-  void CreateSparse(const std::string& name, const std::string& title, Int_t nDim, const Int_t* nBins,
-                     const Double_t* minVals, const Double_t* maxVals)
+  template<class T, int nDim>
+  void CreateSparse(const std::string& name, const std::string& title, const std::array<Int_t, nDim>& nBins,
+                    const std::array<Double_t, nDim>& minVals, const std::array<Double_t, nDim>& maxVals)
   {
-    T* h = new T(name.c_str(), title.c_str(), nDim, nBins, minVals, maxVals);
+    static_assert(std::is_base_of<THnSparse, T>::value, "Class must be derrived from THnSparse");
+    T* h = new T(name.c_str(), title.c_str(), nDim, nBins.data(), minVals.data(), maxVals.data());
     Add(name, h);
   }
 
@@ -195,6 +198,7 @@ public:
     return fMap.find(name)->second;
   }
 
+
   /**
     * \brief Return pointer to TH1 histogram.
     * \param[in] name Name of TH1 histogram.
-- 
GitLab