]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliLHCDipValT.h
Removing fRandome
[u/mrichter/AliRoot.git] / 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>
8#include "AliDCSArray.h"
9#include "AliLog.h"
9b0229cf 10
11
12/////////////////////////////////////////////////////////////////////////////////
13// //
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 //
17// //
18// Author: ruben.shahoyan@cern.ch //
19// //
20/////////////////////////////////////////////////////////////////////////////////
21
22//_______________________________________________________________________________________
23template<class Element> class AliLHCDipValT : public TObject
24{
25 public:
26 //
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;
35 //
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);}
41 //
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);}
48 //
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;
52 //
53 static Char_t* TimeAsString(double t);
54 //
55 protected:
56 //
57 Double_t fTimeStamp; // timestamp of the entry
58 Int_t fSize;
59 Element* fArray; //[fSize] array of entries
60 //
61 ClassDef(AliLHCDipValT,1)
62};
63
64
65//__________________________________________________________________________
66template<class Element>
67AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t,Int_t nv)
68: fTimeStamp(t),fSize(0),fArray(0)
69{
70 //def. constructor
71 SetNSamplesUsed(nv);
72 SetSize(size);
73}
74
75//__________________________________________________________________________
76template<class Element>
77AliLHCDipValT<Element>::AliLHCDipValT(AliLHCDipValT<Element> &src)
78: TObject(src),fTimeStamp(src.fTimeStamp),fSize(0),fArray(0)
79{
80 //copy constructor
81 SetSize(src.fSize);
82 memcpy(fArray,src.fArray,fSize*sizeof(Element));
83}
84
85//__________________________________________________________________________
86template<class Element>
87AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<Element> &src)
88{
89 //assingment
90 if (this != &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));
95 }
96 return *this;
97}
98
99//__________________________________________________________________________
100template<class Element>
101AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliLHCDipValT<Element> &src)
102{
103 //addition
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
107 }
108 else if (fSize!=src.fSize) {
109 AliError(Form("Sizes of arrays to add must be the same (%d vs %d)",fSize,src.fSize));
110 return *this;
111 }
112 else {
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];
119 }
120 return *this;
121}
122
123//__________________________________________________________________________
124template<class Element>
125AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator+=(const AliDCSArray &src)
126{
127 //addition
128 TString tp = typeid(fArray).name();
129 int isstr = (tp==typeid(Char_t*).name()) + (src.GetType()==AliDCSArray::kString);
130 if ( isstr == 1) {
131 AliError("Only one of the sides is of the string type");
132 return *this;
133 }
134 else if (isstr == 2) { // both are string type
135 if (fSize<1 && src.GetNEntries()>0) {
136 TString str;
137 for (int i=0;i<src.GetNEntries();i++) {
138 str += ((TObjString*)src.GetStringArray(i))->GetName();
139 str += " ";
140 }
141 SetSize(str.Length()+1);
142 memcpy(fArray,str.Data(),fSize*sizeof(char));
143 }
144 } else {
145 if (fSize>src.GetNEntries()) {
146 AliError(Form("Size of RHS (%d) is smaller than size of LHS (%d)",src.GetNEntries(),fSize));
147 return *this;
148 }
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];
157 }
158 }
159 //
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 );
165 //
166 return *this;
167}
168
169//__________________________________________________________________________
170template<class Element>
171Char_t* AliLHCDipValT<Element>::TimeAsString(double t)
172{
173 time_t tt = (time_t) t;
174 char* st = ctime(&tt);
175 *(strchr(st,'\n')) = 0;
176 return st;
177}
178
179//__________________________________________________________________________
180template<class Element>
181void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
182{
183 //assign value
184 if (i>=fSize || i<0) {
185 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
186 return;
187 }
188 fArray[i] = v;
189}
190
191//__________________________________________________________________________
192template<class Element>
193void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
194{
195 //assign value
196 if (n!=fSize) SetSize(n);
197 memcpy(fArray,v,n*sizeof(Element));
198}
199
200//__________________________________________________________________________
201template<class Element>
202Element& AliLHCDipValT<Element>::operator[](Int_t i)
203{
204 //obtain value refeterence
205 if (i>=fSize || i<0) {
206 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
207 return fArray[0];
208 }
209 return fArray[i];
210}
211
212//__________________________________________________________________________
213template<class Element>
214Element AliLHCDipValT<Element>::operator[](Int_t i) const
215{
216 //obtain value
217 if (i>=fSize || i<0) {
218 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
219 return 0;
220 }
221 return fArray[i];
222}
223
224//__________________________________________________________________________
225template<class Element>
226Element AliLHCDipValT<Element>::GetValue(Int_t i) const
227{
228 //obtain value
229 if (i>=fSize || i<0) {
230 AliError(Form("Index %d is out of range 0:%d",i,fSize-1));
231 return 0;
232 }
233 return fArray[i];
234}
235
236//__________________________________________________________________________
237template<class Element>
238void AliLHCDipValT<Element>::SetSize(Int_t sz)
239{
240 //resize
241 Element* arr = 0;
242 if (sz>0) {
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;
248 fArray = arr;
249 fSize = sz;
250 }
251 else {
252 delete[] fArray;
253 fSize = 0;
254 }
255}
256
257//__________________________________________________________________________
258template<class Element>
259void AliLHCDipValT<Element>::Print(const Option_t *opt) const
260{
261 // print time and value
262 TString str = opt;
263 str.ToLower();
264 printf("Timestamp: ");
265 if (str.Contains("raw")) printf("%f",GetTimeStamp());
266 else printf("%s",GetTimeAsString());
267 printf(" | samples %6d | Value: ",GetNSamplesUsed());
268 //
269 TString tp = typeid(fArray).name();
270 if ( tp==typeid(Char_t*).name() ) printf(" => %s\n",(Char_t*)fArray);
271 else {
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]);
276 else printf(" ");
277 if ( (i+1)%5 == 0) printf("\n");
278 }
279 if (fSize%5) printf("\n");
280 }
281 //
282}
283
284//__________________________________________________________________________
285template<class Element>
286void AliLHCDipValT<Element>::Clear(const Option_t *)
287{
288 // reset to 0 everything
289 SetNSamplesUsed(0);
290 SetTimeStamp(0);
291 memset(fArray,0,fSize*sizeof(Element));
292}
293
294//__________________________________________________________________________
295template<class Element>
296void AliLHCDipValT<Element>::Average(const Option_t *)
297{
298 // average of samples
299 int n = GetNSamplesUsed();
300 if (n>0) for (int i=fSize;i--;) fArray[i] /= n;
301 //
302}
303
304
305typedef AliLHCDipValT<Double_t> AliLHCDipValD;
306typedef AliLHCDipValT<Float_t> AliLHCDipValF;
307typedef AliLHCDipValT<Int_t> AliLHCDipValI;
308typedef AliLHCDipValT<Char_t> AliLHCDipValC;
309
310
311
312#endif