1 #ifndef ALILHCDIPVALT_H
2 #define ALILHCDIPVALT_H
6 #include <TObjString.h>
8 #include "AliDCSArray.h"
12 /////////////////////////////////////////////////////////////////////////////////
14 // AliLHCDipValT: templated class to store the array from processed //
15 // LHC DIP data. Associated with the time stamp either of the value //
16 // acquisition or the last sample timestamp if many samples were processed //
18 // Author: ruben.shahoyan@cern.ch //
20 /////////////////////////////////////////////////////////////////////////////////
22 //_______________________________________________________________________________________
23 template<class Element> class AliLHCDipValT : public TObject
27 AliLHCDipValT(Int_t size=0, Double_t t=0,Int_t nv=1);
28 AliLHCDipValT(AliLHCDipValT<Element> &src);
29 virtual ~AliLHCDipValT() {delete[] fArray;}
30 AliLHCDipValT& operator=(const AliLHCDipValT<Element> &src);
31 AliLHCDipValT& operator+=(const AliLHCDipValT<Element> &src);
32 AliLHCDipValT& operator+=(const AliDCSArray &src);
33 Element& operator[](Int_t i);
34 Element operator[](Int_t i) const;
36 void SetSize(Int_t size);
37 void SetValue(Int_t i,Element v);
38 void SetValues(const Element *v, Int_t n);
39 void SetTimeStamp(Double_t v) {fTimeStamp = v;}
40 void SetNSamplesUsed(Int_t n) {SetUniqueID((UInt_t)n);}
42 Int_t GetSize() const {return fSize;}
43 Element GetValue(Int_t i=0) const;
44 Element* GetValues() const {return (Element*)fArray;}
45 Double_t GetTimeStamp() const {return fTimeStamp;}
46 Int_t GetNSamplesUsed() const {return GetUniqueID();}
47 Char_t* GetTimeAsString() const {return TimeAsString(fTimeStamp);}
49 virtual void Average(const Option_t *opt="");
50 virtual void Clear(const Option_t *opt="");
51 virtual void Print(const Option_t *opt="") const;
53 static Char_t* TimeAsString(double t);
57 Double_t fTimeStamp; // timestamp of the entry
59 Element* fArray; //[fSize] array of entries
61 ClassDef(AliLHCDipValT,1)
65 //__________________________________________________________________________
66 template<class Element>
67 AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t,Int_t nv)
68 : fTimeStamp(t),fSize(0),fArray(0)
75 //__________________________________________________________________________
76 template<class Element>
77 AliLHCDipValT<Element>::AliLHCDipValT(AliLHCDipValT<Element> &src)
78 : TObject(src),fTimeStamp(src.fTimeStamp),fSize(0),fArray(0)
82 memcpy(fArray,src.fArray,fSize*sizeof(Element));
85 //__________________________________________________________________________
86 template<class Element>
87 AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<Element> &src)
91 ((TObject*)this)->operator=(src);
92 SetTimeStamp(src.GetTimeStamp());
93 if (fSize!=src.fSize) SetSize(src.fSize);
94 memcpy(fArray,src.fArray,fSize*sizeof(Element));
99 //__________________________________________________________________________
100 template<class Element>
101 AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliLHCDipValT<Element> &src)
104 TString tp = typeid(fArray).name();
105 if (tp == typeid(Char_t*).name()) {
106 if (fSize<1 && src.fSize>0) *this=src; // for strings reassign only if this is empty
108 else if (fSize!=src.fSize) {
109 AliError(Form("Sizes of arrays to add must be the same (%d vs %d)",fSize,src.fSize));
113 double wtime0 = fTimeStamp*GetNSamplesUsed();
114 double wtime1 = src.fTimeStamp*src.GetNSamplesUsed();
115 int wtot = GetNSamplesUsed() + src.GetNSamplesUsed();
116 SetNSamplesUsed(wtot);
117 if (wtot>0) SetTimeStamp( (wtime0 + wtime1)/wtot );
118 for (int i=fSize;i--;) fArray[i] += src.fArray[i];
123 //__________________________________________________________________________
124 template<class Element>
125 AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliDCSArray &src)
128 TString tp = typeid(fArray).name();
129 int isstr = (tp==typeid(Char_t*).name()) + (src.GetType()==AliDCSArray::kString);
131 AliError("Only one of the sides is of the string type");
134 else if (isstr == 2) { // both are string type
135 if (fSize<1 && src.GetNEntries()>0) {
137 for (int i=0;i<src.GetNEntries();i++) {
138 str += ((TObjString*)src.GetStringArray(i))->GetName();
141 SetSize(str.Length()+1);
142 memcpy(fArray,str.Data(),fSize*sizeof(char));
145 if (fSize>src.GetNEntries()) {
146 AliError(Form("Size of RHS (%d) is smaller than size of LHS (%d)",src.GetNEntries(),fSize));
149 for (int i=fSize;i--;) {
150 Element &val = fArray[i];
151 if (src.GetType() == AliDCSArray::kDouble) val += src.GetDouble()[i];
152 else if (src.GetType() == AliDCSArray::kFloat) val += src.GetFloat()[i];
153 else if (src.GetType() == AliDCSArray::kInt) val += src.GetInt()[i];
154 else if (src.GetType() == AliDCSArray::kUInt) val += src.GetUInt()[i];
155 else if (src.GetType() == AliDCSArray::kChar) val += src.GetChar()[i];
156 else if (src.GetType() == AliDCSArray::kBool) val += src.GetBool()[i];
160 double wtime0 = fTimeStamp*GetNSamplesUsed();
161 double wtime1 = src.GetTimeStamp();
162 int wtot = GetNSamplesUsed() + 1;
163 SetNSamplesUsed(wtot);
164 if (wtot>0) SetTimeStamp( (wtime0 + wtime1)/wtot );
169 //__________________________________________________________________________
170 template<class Element>
171 Char_t* AliLHCDipValT<Element>::TimeAsString(double t)
173 time_t tt = (time_t) t;
174 char* st = ctime(&tt);
175 *(strchr(st,'\n')) = 0;
179 //__________________________________________________________________________
180 template<class Element>
181 void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
184 if (i>=fSize || i<0) {
185 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
191 //__________________________________________________________________________
192 template<class Element>
193 void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
196 if (n!=fSize) SetSize(n);
197 memcpy(fArray,v,n*sizeof(Element));
200 //__________________________________________________________________________
201 template<class Element>
202 Element& AliLHCDipValT<Element>::operator[](Int_t i)
204 //obtain value refeterence
205 if (i>=fSize || i<0) {
206 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
212 //__________________________________________________________________________
213 template<class Element>
214 Element AliLHCDipValT<Element>::operator[](Int_t i) const
217 if (i>=fSize || i<0) {
218 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
224 //__________________________________________________________________________
225 template<class Element>
226 Element AliLHCDipValT<Element>::GetValue(Int_t i) const
229 if (i>=fSize || i<0) {
230 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
236 //__________________________________________________________________________
237 template<class Element>
238 void AliLHCDipValT<Element>::SetSize(Int_t sz)
243 arr = new Element[sz];
244 int nc = fSize > sz ? sz:fSize; // n elems to copy
245 if (nc) memcpy(arr, fArray, nc*sizeof(Element));
246 if (nc<sz) memset(arr+nc, 0, (sz-nc)*sizeof(Element));
247 if (fSize) delete[] fArray;
257 //__________________________________________________________________________
258 template<class Element>
259 void AliLHCDipValT<Element>::Print(const Option_t *opt) const
261 // print time and value
264 printf("Timestamp: ");
265 if (str.Contains("raw")) printf("%f",GetTimeStamp());
266 else printf("%s",GetTimeAsString());
267 printf(" | samples %6d | Value: ",GetNSamplesUsed());
269 TString tp = typeid(fArray).name();
270 if ( tp==typeid(Char_t*).name() ) printf(" => %s\n",(Char_t*)fArray);
272 if (fSize>1) printf("\n");
273 for (int i=0;i<fSize;i++) {
274 if (tp == typeid(Int_t*).name() || tp == typeid(UInt_t*).name() ) printf("#%4d %+11d |",i,(Int_t)fArray[i]);
275 else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf("#%4d %+e |", i,(Double_t)fArray[i]);
277 if ( (i+1)%5 == 0) printf("\n");
279 if (fSize%5) printf("\n");
284 //__________________________________________________________________________
285 template<class Element>
286 void AliLHCDipValT<Element>::Clear(const Option_t *)
288 // reset to 0 everything
291 memset(fArray,0,fSize*sizeof(Element));
294 //__________________________________________________________________________
295 template<class Element>
296 void AliLHCDipValT<Element>::Average(const Option_t *)
298 // average of samples
299 int n = GetNSamplesUsed();
300 if (n>0) for (int i=fSize;i--;) fArray[i] /= n;
305 typedef AliLHCDipValT<Double_t> AliLHCDipValD;
306 typedef AliLHCDipValT<Float_t> AliLHCDipValF;
307 typedef AliLHCDipValT<Int_t> AliLHCDipValI;
308 typedef AliLHCDipValT<Char_t> AliLHCDipValC;