Update master to aliroot
[u/mrichter/AliRoot.git] / STEER / STEER / AliLHCDipValT.h
CommitLineData
9b0229cf 1#ifndef ALILHCDIPVALT_H
2#define ALILHCDIPVALT_H
3
4#include <typeinfo>
5#include <TString.h>
6#include <TObjString.h>
7#include <TObject.h>
9b0229cf 8#include "AliLog.h"
9b0229cf 9
10
11/////////////////////////////////////////////////////////////////////////////////
12// //
13// AliLHCDipValT: templated class to store the array from processed //
14// LHC DIP data. Associated with the time stamp either of the value //
15// acquisition or the last sample timestamp if many samples were processed //
16// //
17// Author: ruben.shahoyan@cern.ch //
18// //
19/////////////////////////////////////////////////////////////////////////////////
20
21//_______________________________________________________________________________________
22template<class Element> class AliLHCDipValT : public TObject
23{
24 public:
25 //
799c6677 26 enum {
27 kLastSpecial=BIT(14), // last element of array is special (e.g. assigned error etc)
28 kProcessed1 =BIT(15), // last element of array is special (e.g. assigned error etc)
29 kChar=BIT(22),
30 kFloat=BIT(23)
31 };
32 //
33 AliLHCDipValT(Int_t size=0, Double_t t=0);
34 AliLHCDipValT(const AliLHCDipValT<Element> &src);
9b0229cf 35 virtual ~AliLHCDipValT() {delete[] fArray;}
36 AliLHCDipValT& operator=(const AliLHCDipValT<Element> &src);
a1063d3a 37 virtual Element& operator[](Int_t i);
38 virtual Element operator[](Int_t i) const;
9b0229cf 39 //
a1063d3a 40 virtual void SetSize(Int_t size);
41 virtual void SetValue(Int_t i,Element v);
42 virtual void SetValues(const Element *v, Int_t n);
9b0229cf 43 void SetTimeStamp(Double_t v) {fTimeStamp = v;}
9b0229cf 44 //
94179526 45 Int_t GetSizeTotal() const {return fSizeTot;}
a1063d3a 46 virtual Element GetValue(Int_t i=0) const;
47 virtual Element* GetValues() const {return (Element*)fArray;}
9b0229cf 48 Double_t GetTimeStamp() const {return fTimeStamp;}
63bd40f4 49 Char_t* GetTimeAsString(Bool_t utc=kTRUE) const {return TimeAsString(fTimeStamp,utc);}
9b0229cf 50 //
799c6677 51 void SetProcessed1(Bool_t v=kTRUE) {SetBit(kProcessed1,v);}
52 void SetLastSpecial(Bool_t v=kTRUE) {SetBit(kLastSpecial,v);}
53 Bool_t IsProcessed1() const {return TestBit(kProcessed1);}
54 Bool_t IsLastSpecial() const {return TestBit(kLastSpecial);}
55 Int_t GetSize() const {return IsLastSpecial() ? GetSizeTotal()-1:GetSizeTotal();}
56 Bool_t IsTypeC() const {return TestBit(kChar);}
57 Bool_t IsTypeF() const {return TestBit(kFloat);}
58 Bool_t IsTypeI() const {return !TestBit(kFloat|kChar);}
59 //
9b0229cf 60 virtual void Clear(const Option_t *opt="");
61 virtual void Print(const Option_t *opt="") const;
62 //
63bd40f4 63 static Char_t* TimeAsString(double t,Bool_t utc=kTRUE);
9b0229cf 64 //
65 protected:
66 //
67 Double_t fTimeStamp; // timestamp of the entry
94179526 68 Int_t fSizeTot; // vector total size (including special slots, like for errors)
69 Element* fArray; //[fSizeTot] array of entries
9b0229cf 70 //
71 ClassDef(AliLHCDipValT,1)
72};
73
74
75//__________________________________________________________________________
76template<class Element>
799c6677 77AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t)
94179526 78: fTimeStamp(t),fSizeTot(0),fArray(0)
9b0229cf 79{
80 //def. constructor
9b0229cf 81 SetSize(size);
799c6677 82 if (!strcmp(typeid(fArray).name(),typeid(Char_t*).name())) SetBit(kChar);
83 else if (!strcmp(typeid(fArray).name(),typeid(Double_t*).name()) ||
84 !strcmp(typeid(fArray).name(),typeid(Float_t*).name() )) SetBit(kFloat);
85 //
9b0229cf 86}
87
88//__________________________________________________________________________
89template<class Element>
799c6677 90AliLHCDipValT<Element>::AliLHCDipValT(const AliLHCDipValT<Element> &src)
94179526 91: TObject(src),fTimeStamp(src.fTimeStamp),fSizeTot(0),fArray(0)
9b0229cf 92{
93 //copy constructor
799c6677 94 SetSize(src.GetSizeTotal());
95 memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
9b0229cf 96}
97
98//__________________________________________________________________________
99template<class Element>
100AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<Element> &src)
101{
102 //assingment
103 if (this != &src) {
104 ((TObject*)this)->operator=(src);
799c6677 105 if (GetSizeTotal()!=src.GetSizeTotal()) SetSize(src.GetSizeTotal());
9b0229cf 106 SetTimeStamp(src.GetTimeStamp());
799c6677 107 memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
9b0229cf 108 }
109 return *this;
110}
111
112//__________________________________________________________________________
113template<class Element>
63bd40f4 114Char_t* AliLHCDipValT<Element>::TimeAsString(double t, Bool_t utc)
9b0229cf 115{
63bd40f4 116 // time as string in UTC or local format
41ff94a3 117 static char buff[22];
9b0229cf 118 time_t tt = (time_t) t;
41ff94a3 119 struct tm *time = utc ? gmtime(&tt) : localtime(&tt);
d97d27ff 120 snprintf(buff,21,"%02d:%02d:%02d %02d/%02d/%04d",time->tm_hour,time->tm_min,time->tm_sec,
41ff94a3 121 time->tm_mday,time->tm_mon+1,time->tm_year+1900);
122 return (char*)buff;
9b0229cf 123}
124
125//__________________________________________________________________________
126template<class Element>
127void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
128{
129 //assign value
799c6677 130 if (i>=GetSizeTotal() || i<0) {
131 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
9b0229cf 132 return;
133 }
134 fArray[i] = v;
135}
136
137//__________________________________________________________________________
138template<class Element>
139void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
140{
141 //assign value
799c6677 142 if (n!=GetSizeTotal()) SetSize(n);
9b0229cf 143 memcpy(fArray,v,n*sizeof(Element));
144}
145
146//__________________________________________________________________________
147template<class Element>
148Element& AliLHCDipValT<Element>::operator[](Int_t i)
149{
150 //obtain value refeterence
799c6677 151 if (i>=GetSizeTotal() || i<0) {
152 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
9b0229cf 153 return fArray[0];
154 }
a1063d3a 155 return (Element&)fArray[i];
9b0229cf 156}
157
158//__________________________________________________________________________
159template<class Element>
160Element AliLHCDipValT<Element>::operator[](Int_t i) const
161{
162 //obtain value
799c6677 163 if (i>=GetSizeTotal() || i<0) {
164 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
9b0229cf 165 return 0;
166 }
a1063d3a 167 return (Element)fArray[i];
9b0229cf 168}
169
170//__________________________________________________________________________
171template<class Element>
172Element AliLHCDipValT<Element>::GetValue(Int_t i) const
173{
174 //obtain value
799c6677 175 if (i>=GetSizeTotal() || i<0) {
176 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
9b0229cf 177 return 0;
178 }
a1063d3a 179 return (Element)fArray[i];
9b0229cf 180}
181
182//__________________________________________________________________________
183template<class Element>
184void AliLHCDipValT<Element>::SetSize(Int_t sz)
185{
186 //resize
187 Element* arr = 0;
188 if (sz>0) {
189 arr = new Element[sz];
799c6677 190 int nc = GetSizeTotal() > sz ? sz:GetSizeTotal(); // n elems to copy
f8b1c575 191 if (nc && fArray) memcpy(arr, fArray, nc*sizeof(Element));
9b0229cf 192 if (nc<sz) memset(arr+nc, 0, (sz-nc)*sizeof(Element));
f8b1c575 193 if (fArray) delete[] fArray;
9b0229cf 194 fArray = arr;
94179526 195 fSizeTot = sz;
9b0229cf 196 }
197 else {
198 delete[] fArray;
94179526 199 fArray = 0;
200 fSizeTot = 0;
9b0229cf 201 }
202}
203
204//__________________________________________________________________________
205template<class Element>
206void AliLHCDipValT<Element>::Print(const Option_t *opt) const
207{
208 // print time and value
209 TString str = opt;
210 str.ToLower();
799c6677 211 if (str.Contains("raw")) printf("%.1f ",GetTimeStamp());
63bd40f4 212 else printf("[%s] ",GetTimeAsString(!str.Contains("loc")));
9b0229cf 213 //
214 TString tp = typeid(fArray).name();
799c6677 215 if ( tp==typeid(Char_t*).name() ) printf(": %s\n",(Char_t*)fArray);
9b0229cf 216 else {
799c6677 217 int sz = GetSize();
218 if (sz>1) printf("\n");
219 Bool_t eolOK = kFALSE;
220 for (int i=0;i<sz;i++) {
63bd40f4 221 if (tp == typeid(Int_t*).name() || tp == typeid(UInt_t*).name() ) {
222 if (!str.Contains("bit")) printf(" %6d |" ,(Int_t)fArray[i]);
223 else {
224 printf(" ");
225 int val = (int)fArray[i];
226 for (int j=sizeof(int)*8;j--;) printf("%d",(val>>j)&0x1);
227 printf(" |");
228 }
229
230 }
a1063d3a 231 else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf(" %+.3e |",(Double_t)fArray[i]);
9b0229cf 232 else printf(" ");
799c6677 233 eolOK = kFALSE;
234 if ( (i+1)%5 == 0) {printf("\n"); eolOK = kTRUE;}
235 }
236 if (IsLastSpecial()) {
237 if (sz>1 && !eolOK) {printf("\n"); eolOK = kTRUE;}
a1063d3a 238 if (tp == typeid(Double_t*).name() || tp == typeid(Double_t*).name()) {
239 printf(" Error: %+e\n",(Double_t)fArray[sz]);
799c6677 240 eolOK = kTRUE;
241 }
9b0229cf 242 }
799c6677 243 if (!eolOK) printf("\n");
9b0229cf 244 }
245 //
246}
247
248//__________________________________________________________________________
249template<class Element>
250void AliLHCDipValT<Element>::Clear(const Option_t *)
251{
252 // reset to 0 everything
9b0229cf 253 SetTimeStamp(0);
799c6677 254 memset(fArray,0,GetSizeTotal()*sizeof(Element));
9b0229cf 255}
256
257
258typedef AliLHCDipValT<Double_t> AliLHCDipValD;
259typedef AliLHCDipValT<Float_t> AliLHCDipValF;
260typedef AliLHCDipValT<Int_t> AliLHCDipValI;
261typedef AliLHCDipValT<Char_t> AliLHCDipValC;
262
263
264
265#endif