1 #ifndef ALILHCDIPVALT_H
2 #define ALILHCDIPVALT_H
6 #include <TObjString.h>
11 /////////////////////////////////////////////////////////////////////////////////
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 //
17 // Author: ruben.shahoyan@cern.ch //
19 /////////////////////////////////////////////////////////////////////////////////
21 //_______________________________________________________________________________________
22 template<class Element> class AliLHCDipValT : public TObject
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)
33 AliLHCDipValT(Int_t size=0, Double_t t=0);
34 AliLHCDipValT(const AliLHCDipValT<Element> &src);
35 virtual ~AliLHCDipValT() {delete[] fArray;}
36 AliLHCDipValT& operator=(const AliLHCDipValT<Element> &src);
37 Element& operator[](Int_t i);
38 Element operator[](Int_t i) const;
40 void SetSize(Int_t size);
41 void SetValue(Int_t i,Element v);
42 void SetValues(const Element *v, Int_t n);
43 void SetTimeStamp(Double_t v) {fTimeStamp = v;}
45 Int_t GetSizeTotal() const {return fSizeTot;}
46 Element GetValue(Int_t i=0) const;
47 Element* GetValues() const {return (Element*)fArray;}
48 Double_t GetTimeStamp() const {return fTimeStamp;}
49 Char_t* GetTimeAsString(Bool_t utc=kTRUE) const {return TimeAsString(fTimeStamp,utc);}
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);}
60 virtual void Clear(const Option_t *opt="");
61 virtual void Print(const Option_t *opt="") const;
63 static Char_t* TimeAsString(double t,Bool_t utc=kTRUE);
67 Double_t fTimeStamp; // timestamp of the entry
68 Int_t fSizeTot; // vector total size (including special slots, like for errors)
69 Element* fArray; //[fSizeTot] array of entries
71 ClassDef(AliLHCDipValT,1)
75 //__________________________________________________________________________
76 template<class Element>
77 AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t)
78 : fTimeStamp(t),fSizeTot(0),fArray(0)
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);
88 //__________________________________________________________________________
89 template<class Element>
90 AliLHCDipValT<Element>::AliLHCDipValT(const AliLHCDipValT<Element> &src)
91 : TObject(src),fTimeStamp(src.fTimeStamp),fSizeTot(0),fArray(0)
94 SetSize(src.GetSizeTotal());
95 memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
98 //__________________________________________________________________________
99 template<class Element>
100 AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<Element> &src)
104 ((TObject*)this)->operator=(src);
105 if (GetSizeTotal()!=src.GetSizeTotal()) SetSize(src.GetSizeTotal());
106 SetTimeStamp(src.GetTimeStamp());
107 memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
112 //__________________________________________________________________________
113 template<class Element>
114 Char_t* AliLHCDipValT<Element>::TimeAsString(double t, Bool_t utc)
116 // time as string in UTC or local format
117 static char buff[22];
118 time_t tt = (time_t) t;
119 struct tm *time = utc ? gmtime(&tt) : localtime(&tt);
120 snprintf(buff,21,"%02d:%02d:%02d %02d/%02d/%04d",time->tm_hour,time->tm_min,time->tm_sec,
121 time->tm_mday,time->tm_mon+1,time->tm_year+1900);
125 //__________________________________________________________________________
126 template<class Element>
127 void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
130 if (i>=GetSizeTotal() || i<0) {
131 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
137 //__________________________________________________________________________
138 template<class Element>
139 void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
142 if (n!=GetSizeTotal()) SetSize(n);
143 memcpy(fArray,v,n*sizeof(Element));
146 //__________________________________________________________________________
147 template<class Element>
148 Element& AliLHCDipValT<Element>::operator[](Int_t i)
150 //obtain value refeterence
151 if (i>=GetSizeTotal() || i<0) {
152 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
158 //__________________________________________________________________________
159 template<class Element>
160 Element AliLHCDipValT<Element>::operator[](Int_t i) const
163 if (i>=GetSizeTotal() || i<0) {
164 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
170 //__________________________________________________________________________
171 template<class Element>
172 Element AliLHCDipValT<Element>::GetValue(Int_t i) const
175 if (i>=GetSizeTotal() || i<0) {
176 AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
182 //__________________________________________________________________________
183 template<class Element>
184 void AliLHCDipValT<Element>::SetSize(Int_t sz)
189 arr = new Element[sz];
190 int nc = GetSizeTotal() > sz ? sz:GetSizeTotal(); // n elems to copy
191 if (nc && fArray) memcpy(arr, fArray, nc*sizeof(Element));
192 if (nc<sz) memset(arr+nc, 0, (sz-nc)*sizeof(Element));
193 if (fArray) delete[] fArray;
204 //__________________________________________________________________________
205 template<class Element>
206 void AliLHCDipValT<Element>::Print(const Option_t *opt) const
208 // print time and value
211 if (str.Contains("raw")) printf("%.1f ",GetTimeStamp());
212 else printf("[%s] ",GetTimeAsString(!str.Contains("loc")));
214 TString tp = typeid(fArray).name();
215 if ( tp==typeid(Char_t*).name() ) printf(": %s\n",(Char_t*)fArray);
218 if (sz>1) printf("\n");
219 Bool_t eolOK = kFALSE;
220 for (int i=0;i<sz;i++) {
221 if (tp == typeid(Int_t*).name() || tp == typeid(UInt_t*).name() ) {
222 if (!str.Contains("bit")) printf(" %6d |" ,(Int_t)fArray[i]);
225 int val = (int)fArray[i];
226 for (int j=sizeof(int)*8;j--;) printf("%d",(val>>j)&0x1);
231 else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf(" %+.3e |",(Float_t)fArray[i]);
234 if ( (i+1)%5 == 0) {printf("\n"); eolOK = kTRUE;}
236 if (IsLastSpecial()) {
237 if (sz>1 && !eolOK) {printf("\n"); eolOK = kTRUE;}
238 if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) {
239 printf(" Error: %+e\n",(Float_t)fArray[sz]);
243 if (!eolOK) printf("\n");
248 //__________________________________________________________________________
249 template<class Element>
250 void AliLHCDipValT<Element>::Clear(const Option_t *)
252 // reset to 0 everything
254 memset(fArray,0,GetSizeTotal()*sizeof(Element));
258 typedef AliLHCDipValT<Double_t> AliLHCDipValD;
259 typedef AliLHCDipValT<Float_t> AliLHCDipValF;
260 typedef AliLHCDipValT<Int_t> AliLHCDipValI;
261 typedef AliLHCDipValT<Char_t> AliLHCDipValC;