]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/global/AliHLTGlobalHistoComponent.h
converting AliHLTGlobalHistogramVariables to template class in order to be used with...
[u/mrichter/AliRoot.git] / HLT / global / AliHLTGlobalHistoComponent.h
index 52dc4aa51e66cde9c16469180afdc2016de36619..ef5e9ccb946587d2aed52741e8b84a6528c10c8b 100644 (file)
@@ -14,6 +14,8 @@
 ///         on the AliHLTTTreeProcessor
 
 #include "AliHLTTTreeProcessor.h"
+#include "TObjString.h"
+#include "TObjArray.h"
 #include <string>
 #include <map>
 #include <vector>
@@ -72,14 +74,39 @@ class AliHLTGlobalHistoComponent : public AliHLTTTreeProcessor
 
   /// @class AliHLTGlobalHistoVariables
   /// container for the tree branch variables
+  template <typename T>
   class AliHLTGlobalHistoVariables {
   public:
-    AliHLTGlobalHistoVariables();
-    AliHLTGlobalHistoVariables(int capacity, const char* names);
-    ~AliHLTGlobalHistoVariables();
+    AliHLTGlobalHistoVariables()  : fCapacity(0), fArrays(), fCount(), fKeys() {}
+    AliHLTGlobalHistoVariables(const AliHLTGlobalHistoVariables& src)  : fCapacity(0), fArrays(), fCount(), fKeys() {}
+    ~AliHLTGlobalHistoVariables() {Reset();}
 
     /// init the arrays
-    int Init(int capacity, const char* names);
+    int Init(int capacity, const char* names)
+    {
+      /// init the arrays
+      int iResult=0;
+      TString initializer(names);
+      TObjArray* pTokens=initializer.Tokenize(" ");
+      fCapacity=capacity;
+      if (pTokens) {
+       int entries=pTokens->GetEntriesFast();
+       fArrays.resize(entries);
+       fCount.resize(entries);
+       for (int i=0; i<entries; i++) {
+         fKeys[pTokens->At(i)->GetName()]=i;
+         fArrays[i]=new T[fCapacity];
+       }
+       delete pTokens;
+      }
+      if (fArrays.size()!=fCount.size() ||
+         fArrays.size()!=fKeys.size()) {
+       return -EFAULT;
+      }
+
+      ResetCount();
+      return iResult;
+    }
 
     /// capacity for every key
     int Capacity() const {return fCapacity;}
@@ -87,27 +114,79 @@ class AliHLTGlobalHistoComponent : public AliHLTTTreeProcessor
     int Variables() const {return fArrays.size();}
 
     /// fill variable at index
-    int Fill(unsigned index, float value);
+    int Fill(unsigned index, T value)
+    {
+      if (index>=fArrays.size() || index>=fCount.size()) return -ENOENT;
+      if (fCount[index]>=fCapacity) return -ENOSPC;
+
+      (fArrays[index])[fCount[index]++]=value;
+      return fCount[index];
+    }
+
     /// fill variable at key
-    int Fill(const char* key, float value);
+    int Fill(const char* key, T value)
+    {
+      int index=FindKey(key);
+      if (index<0) return -ENOENT;
+      return Fill(index, value);
+    }
+
     /// get array at key
-    float* GetArray(const char* key);
+    T* GetArray(const char* key)
+    {
+      int index=FindKey(key); if (index<0) return NULL;
+      if ((unsigned)index>=fArrays.size()) return NULL;
+      return fArrays[index];
+    }
+
     /// get the key of an array
-    const char* GetKey(int index) const;
+    const char* GetKey(int index) const
+    {
+      for (map<string, int>::const_iterator element=fKeys.begin(); element!=fKeys.end(); element++) {
+       if (element->second==index) return element->first.c_str();
+      }
+      return NULL;
+    }
 
     /// reset and cleanup arrays
-    int Reset();
+    int Reset()
+    {
+      for (unsigned i=0; i<fArrays.size(); i++) {delete fArrays[i];}
+      fArrays.clear(); fCount.clear(); fKeys.clear();
+      return 0;
+    }
 
     /// reset the fill counts
-    int ResetCount();
+    int ResetCount()
+    {
+      for (vector<int>::iterator element=fCount.begin(); element!=fCount.end(); element++) *element=0;
+      return 0;
+    }
+
+    char GetType() const {AliHLTGlobalHistoVariablesType type(T&); return type.GetType();}
 
   private:
-    int FindKey(const char* key) const;
+    int FindKey(const char* key) const
+    {
+      map<string, int>::const_iterator element=fKeys.find(key);
+      if (element==fKeys.end()) return -ENOENT;
+      return element->second;
+    }
+
+    /// internal helper class to get the type of the template
+    class AliHLTGlobalHistoVariablesType {
+    public:
+      AliHLTGlobalHistoVariablesType(float&) : fType('f') {}
+      AliHLTGlobalHistoVariablesType(int&)   : fType('i') {}
+      char GetType() const {return fType;}
+    private:
+      char fType; //!
+    };
 
     /// capacity of all arrays
     int fCapacity; //!
     /// pointers of arrays
-    vector<float*> fArrays; //!
+    vector<T*> fArrays; //!
     /// fill count for arrays
     vector<int> fCount; //!
     /// map of keys
@@ -145,7 +224,8 @@ private:
   float fVertexZ; //!
  
   /// filling arrays for track parameters
-  AliHLTGlobalHistoComponent::AliHLTGlobalHistoVariables fTrackVariables; //!
+  AliHLTGlobalHistoVariables<float> fTrackVariables; //!
+  AliHLTGlobalHistoVariables<int> fTrackVariablesInt; //!
   
   ClassDef(AliHLTGlobalHistoComponent, 0) // HLT Global Histogram component
 };