]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliLHCDipValT.h
AddTrial method added, changed trials to unsigned int
[u/mrichter/AliRoot.git] / STEER / AliLHCDipValT.h
index d24f27739861d5287dbe7581f049e92142223356..b2413fc658d3a077a4b1110cd33ed84e6834b38c 100755 (executable)
@@ -5,9 +5,7 @@
 #include <TString.h>
 #include <TObjString.h>
 #include <TObject.h>
-#include "AliDCSArray.h"
 #include "AliLog.h"
-class AliDCSArray;
 
 
 /////////////////////////////////////////////////////////////////////////////////
@@ -25,12 +23,17 @@ template<class Element> class AliLHCDipValT : public TObject
 {
  public:
   //
-  AliLHCDipValT(Int_t size=0, Double_t t=0,Int_t nv=1);
-  AliLHCDipValT(AliLHCDipValT<Element> &src);
+  enum {
+    kLastSpecial=BIT(14),         // last element of array is special (e.g. assigned error etc) 
+    kProcessed1 =BIT(15),         // last element of array is special (e.g. assigned error etc) 
+    kChar=BIT(22),
+    kFloat=BIT(23)
+  };
+  //
+  AliLHCDipValT(Int_t size=0, Double_t t=0);
+  AliLHCDipValT(const AliLHCDipValT<Element> &src);
   virtual ~AliLHCDipValT() {delete[] fArray;}
   AliLHCDipValT& operator=(const AliLHCDipValT<Element> &src);
-  AliLHCDipValT& operator+=(const AliLHCDipValT<Element> &src);
-  AliLHCDipValT& operator+=(const AliDCSArray &src);
   Element&       operator[](Int_t i);
   Element        operator[](Int_t i)                          const;
   //
@@ -38,26 +41,32 @@ template<class Element> class AliLHCDipValT : public TObject
   void                SetValue(Int_t i,Element v);
   void                SetValues(const Element *v, Int_t n);
   void                SetTimeStamp(Double_t v)                      {fTimeStamp = v;}
-  void                SetNSamplesUsed(Int_t n)                      {SetUniqueID((UInt_t)n);}
   // 
-  Int_t               GetSize()                               const {return fSize;}
+  Int_t               GetSizeTotal()                          const {return fSizeTot;}
   Element             GetValue(Int_t i=0)                     const;
   Element*            GetValues()                             const {return (Element*)fArray;}
   Double_t            GetTimeStamp()                          const {return fTimeStamp;}
-  Int_t               GetNSamplesUsed()                       const {return GetUniqueID();}
-  Char_t*             GetTimeAsString()                       const {return TimeAsString(fTimeStamp);}
+  Char_t*             GetTimeAsString(Bool_t utc=kTRUE)       const {return TimeAsString(fTimeStamp,utc);}
+  //
+  void                SetProcessed1(Bool_t v=kTRUE)                 {SetBit(kProcessed1,v);}
+  void                SetLastSpecial(Bool_t v=kTRUE)                {SetBit(kLastSpecial,v);}
+  Bool_t              IsProcessed1()                          const {return TestBit(kProcessed1);}
+  Bool_t              IsLastSpecial()                         const {return TestBit(kLastSpecial);}
+  Int_t               GetSize()                               const {return IsLastSpecial() ? GetSizeTotal()-1:GetSizeTotal();}
+  Bool_t              IsTypeC()                               const {return TestBit(kChar);}
+  Bool_t              IsTypeF()                               const {return TestBit(kFloat);}
+  Bool_t              IsTypeI()                               const {return !TestBit(kFloat|kChar);}
   //
-  virtual void Average(const Option_t *opt="");
   virtual void Clear(const Option_t *opt="");
   virtual void Print(const Option_t *opt="")                  const;
   //
-  static Char_t*      TimeAsString(double t);
+  static Char_t*      TimeAsString(double t,Bool_t utc=kTRUE);
   //
  protected:
   //
   Double_t            fTimeStamp;        // timestamp of the entry
-  Int_t               fSize;
-  Element*            fArray;            //[fSize] array of entries
+  Int_t               fSizeTot;          // vector total size (including special slots, like for errors)
+  Element*            fArray;            //[fSizeTot] array of entries
   //
   ClassDef(AliLHCDipValT,1)
 };
@@ -65,22 +74,25 @@ template<class Element> class AliLHCDipValT : public TObject
 
 //__________________________________________________________________________
 template<class Element>
-AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t,Int_t nv
-: fTimeStamp(t),fSize(0),fArray(0)
+AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t) 
+: fTimeStamp(t),fSizeTot(0),fArray(0)
 {
   //def. constructor
-  SetNSamplesUsed(nv);
   SetSize(size);
+  if      (!strcmp(typeid(fArray).name(),typeid(Char_t*).name())) SetBit(kChar);
+  else if (!strcmp(typeid(fArray).name(),typeid(Double_t*).name()) ||
+          !strcmp(typeid(fArray).name(),typeid(Float_t*).name() )) SetBit(kFloat);
+  //
 }
 
 //__________________________________________________________________________
 template<class Element>
-AliLHCDipValT<Element>::AliLHCDipValT(AliLHCDipValT<Element> &src)
-: TObject(src),fTimeStamp(src.fTimeStamp),fSize(0),fArray(0)
+AliLHCDipValT<Element>::AliLHCDipValT(const AliLHCDipValT<Element> &src)
+: TObject(src),fTimeStamp(src.fTimeStamp),fSizeTot(0),fArray(0)
 {
   //copy constructor
-  SetSize(src.fSize);
-  memcpy(fArray,src.fArray,fSize*sizeof(Element));
+  SetSize(src.GetSizeTotal());
+  memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
 }
 
 //__________________________________________________________________________
@@ -90,91 +102,24 @@ AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<El
   //assingment
   if (this != &src) {
     ((TObject*)this)->operator=(src);
+    if (GetSizeTotal()!=src.GetSizeTotal()) SetSize(src.GetSizeTotal());
     SetTimeStamp(src.GetTimeStamp());
-    if (fSize!=src.fSize) SetSize(src.fSize);
-    memcpy(fArray,src.fArray,fSize*sizeof(Element));    
-  }
-  return *this;
-}
-
-//__________________________________________________________________________
-template<class Element>
-AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliLHCDipValT<Element> &src)
-{
-  //addition
-  TString tp = typeid(fArray).name();
-  if (tp == typeid(Char_t*).name()) {
-    if (fSize<1 && src.fSize>0) *this=src; // for strings reassign only if this is empty
-  }
-  else if (fSize!=src.fSize) {
-    AliError(Form("Sizes of arrays to add must be the same (%d vs %d)",fSize,src.fSize));
-    return *this;    
-  }
-  else {
-    double wtime0 = fTimeStamp*GetNSamplesUsed();
-    double wtime1 = src.fTimeStamp*src.GetNSamplesUsed();
-    int wtot = GetNSamplesUsed() + src.GetNSamplesUsed();
-    SetNSamplesUsed(wtot);
-    if (wtot>0) SetTimeStamp( (wtime0 + wtime1)/wtot );
-    for (int i=fSize;i--;) fArray[i] += src.fArray[i];
+    memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));    
   }
   return *this;
 }
 
 //__________________________________________________________________________
 template<class Element>
-AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliDCSArray &src)
-{
-  //addition
-  TString tp = typeid(fArray).name();
-  int isstr = (tp==typeid(Char_t*).name()) + (src.GetType()==AliDCSArray::kString);
-  if ( isstr == 1) {
-    AliError("Only one of the sides is of the string type");
-    return *this;
-  }
-  else if (isstr == 2) { // both are string type
-    if (fSize<1 && src.GetNEntries()>0) {
-      TString str;
-      for (int i=0;i<src.GetNEntries();i++) {
-       str += ((TObjString*)src.GetStringArray(i))->GetName();
-       str += " ";
-      }
-      SetSize(str.Length()+1);
-      memcpy(fArray,str.Data(),fSize*sizeof(char));
-    }
-  } else {
-    if (fSize>src.GetNEntries()) {
-      AliError(Form("Size of RHS (%d) is smaller than size of LHS (%d)",src.GetNEntries(),fSize));
-      return *this;
-    }
-    for (int i=fSize;i--;) {
-      Element &val = fArray[i];
-      if      (src.GetType() == AliDCSArray::kDouble) val += src.GetDouble()[i];
-      else if (src.GetType() == AliDCSArray::kFloat)  val += src.GetFloat()[i];
-      else if (src.GetType() == AliDCSArray::kInt)    val += src.GetInt()[i];
-      else if (src.GetType() == AliDCSArray::kUInt)   val += src.GetUInt()[i];
-      else if (src.GetType() == AliDCSArray::kChar)   val += src.GetChar()[i];
-      else if (src.GetType() == AliDCSArray::kBool)   val += src.GetBool()[i];
-    }
-  }
-  //
-  double wtime0 = fTimeStamp*GetNSamplesUsed();
-  double wtime1 = src.GetTimeStamp();
-  int wtot = GetNSamplesUsed() + 1;
-  SetNSamplesUsed(wtot);
-  if (wtot>0) SetTimeStamp( (wtime0 + wtime1)/wtot ); 
-  //
-  return *this;
-}
-
-//__________________________________________________________________________
-template<class Element>
-Char_t* AliLHCDipValT<Element>::TimeAsString(double t)
+Char_t* AliLHCDipValT<Element>::TimeAsString(double t, Bool_t utc)
 {
+  // time as string in UTC or local format
+  static char buff[22];
   time_t tt = (time_t) t;
-  char* st = ctime(&tt);
-  *(strchr(st,'\n')) = 0;
-  return st;
+  struct tm *time = utc ? gmtime(&tt) : localtime(&tt);
+  snprintf(buff,21,"%02d:%02d:%02d %02d/%02d/%04d",time->tm_hour,time->tm_min,time->tm_sec,
+         time->tm_mday,time->tm_mon+1,time->tm_year+1900);
+  return (char*)buff;
 }
 
 //__________________________________________________________________________
@@ -182,8 +127,8 @@ template<class Element>
 void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
 {
   //assign value
-  if (i>=fSize || i<0) {
-    AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
+  if (i>=GetSizeTotal() || i<0) {
+    AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
     return;
   }
   fArray[i] = v;
@@ -194,7 +139,7 @@ template<class Element>
 void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
 {
   //assign value
-  if (n!=fSize) SetSize(n);
+  if (n!=GetSizeTotal()) SetSize(n);
   memcpy(fArray,v,n*sizeof(Element));
 }
 
@@ -203,8 +148,8 @@ template<class Element>
 Element& AliLHCDipValT<Element>::operator[](Int_t i)
 {
   //obtain value refeterence
-  if (i>=fSize || i<0) {
-    AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
+  if (i>=GetSizeTotal() || i<0) {
+    AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
     return fArray[0];
   }
   return fArray[i];
@@ -215,8 +160,8 @@ template<class Element>
 Element AliLHCDipValT<Element>::operator[](Int_t i) const
 {
   //obtain value
-  if (i>=fSize || i<0) {
-    AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
+  if (i>=GetSizeTotal() || i<0) {
+    AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
     return 0;
   }
   return fArray[i];
@@ -227,8 +172,8 @@ template<class Element>
 Element AliLHCDipValT<Element>::GetValue(Int_t i) const
 {
   //obtain value
-  if (i>=fSize || i<0) {
-    AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
+  if (i>=GetSizeTotal() || i<0) {
+    AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
     return 0;
   }
   return fArray[i];
@@ -242,16 +187,17 @@ void AliLHCDipValT<Element>::SetSize(Int_t sz)
   Element* arr = 0;
   if (sz>0) {
     arr = new Element[sz];
-    int nc = fSize > sz ? sz:fSize; // n elems to copy
-    if (nc) memcpy(arr, fArray, nc*sizeof(Element));
+    int nc = GetSizeTotal() > sz ? sz:GetSizeTotal(); // n elems to copy
+    if (nc && fArray) memcpy(arr, fArray, nc*sizeof(Element));
     if (nc<sz) memset(arr+nc, 0, (sz-nc)*sizeof(Element));
-    if (fSize) delete[] fArray;
+    if (fArray) delete[] fArray;
     fArray = arr;
-    fSize = sz;
+    fSizeTot = sz;
   }
   else {
     delete[] fArray;
-    fSize = 0;
+    fArray = 0;
+    fSizeTot = 0;
   }
 }
 
@@ -262,22 +208,39 @@ void AliLHCDipValT<Element>::Print(const Option_t *opt) const
   // print time and value
   TString str = opt; 
   str.ToLower();
-  printf("Timestamp: ");
-  if (str.Contains("raw")) printf("%f",GetTimeStamp());
-  else printf("%s",GetTimeAsString());
-  printf(" | samples %6d | Value: ",GetNSamplesUsed());
+  if (str.Contains("raw")) printf("%.1f ",GetTimeStamp());
+  else printf("[%s] ",GetTimeAsString(!str.Contains("loc")));
   //
   TString tp = typeid(fArray).name();
-  if ( tp==typeid(Char_t*).name() ) printf(" => %s\n",(Char_t*)fArray);
+  if ( tp==typeid(Char_t*).name() ) printf(": %s\n",(Char_t*)fArray);
   else {
-    if (fSize>1) printf("\n");
-    for (int i=0;i<fSize;i++) {
-      if      (tp == typeid(Int_t*).name()    || tp == typeid(UInt_t*).name() ) printf("#%4d %+11d |",i,(Int_t)fArray[i]);
-      else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf("#%4d %+e |",  i,(Double_t)fArray[i]);
+    int sz = GetSize();
+    if (sz>1) printf("\n");
+    Bool_t eolOK = kFALSE;
+    for (int i=0;i<sz;i++) {
+      if      (tp == typeid(Int_t*).name()    || tp == typeid(UInt_t*).name() ) {
+       if (!str.Contains("bit")) printf(" %6d |" ,(Int_t)fArray[i]);
+       else {
+         printf(" ");
+         int val = (int)fArray[i];
+         for (int j=sizeof(int)*8;j--;) printf("%d",(val>>j)&0x1);
+         printf(" |");
+       }
+
+      }
+      else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf(" %+.3e |",(Float_t)fArray[i]);
       else printf(" ");
-      if ( (i+1)%5 == 0) printf("\n");
+      eolOK = kFALSE;
+      if ( (i+1)%5 == 0) {printf("\n"); eolOK = kTRUE;}
+    }
+    if (IsLastSpecial()) {
+      if (sz>1 && !eolOK) {printf("\n"); eolOK = kTRUE;}
+      if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) {
+       printf(" Error: %+e\n",(Float_t)fArray[sz]);
+       eolOK = kTRUE;
+      }
     }
-    if (fSize%5) printf("\n");
+    if (!eolOK) printf("\n");
   }
   //
 }
@@ -287,19 +250,8 @@ template<class Element>
 void AliLHCDipValT<Element>::Clear(const Option_t *)
 {
   // reset to 0 everything
-  SetNSamplesUsed(0);
   SetTimeStamp(0);
-  memset(fArray,0,fSize*sizeof(Element));
-}
-
-//__________________________________________________________________________
-template<class Element>
-void AliLHCDipValT<Element>::Average(const Option_t *)
-{
-  // average of samples
-  int n = GetNSamplesUsed();
-  if (n>0) for (int i=fSize;i--;) fArray[i] /= n;
-  //
+  memset(fArray,0,GetSizeTotal()*sizeof(Element));
 }