]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliLHCDipValT.h
The class for LHC DIP data was completely changed
[u/mrichter/AliRoot.git] / STEER / AliLHCDipValT.h
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 "AliLog.h"
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 //_______________________________________________________________________________________
22 template<class Element> class AliLHCDipValT : public TObject
23 {
24  public:
25   //
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);
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;
39   //
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;}
44   // 
45   Int_t               GetSizeTotal()                          const {return GetUniqueID();}
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()                       const {return TimeAsString(fTimeStamp);}
50   //
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   //
60   virtual void Clear(const Option_t *opt="");
61   virtual void Print(const Option_t *opt="")                  const;
62   //
63   static Char_t*      TimeAsString(double t);
64   //
65  protected:
66   //
67   Double_t            fTimeStamp;        // timestamp of the entry
68   Element*            fArray;            //[fUniqueID] array of entries
69   //
70   ClassDef(AliLHCDipValT,1)
71 };
72
73
74 //__________________________________________________________________________
75 template<class Element>
76 AliLHCDipValT<Element>::AliLHCDipValT(Int_t size,Double_t t) 
77 : fTimeStamp(t),fArray(0)
78 {
79   //def. constructor
80   SetSize(size);
81   if      (!strcmp(typeid(fArray).name(),typeid(Char_t*).name())) SetBit(kChar);
82   else if (!strcmp(typeid(fArray).name(),typeid(Double_t*).name()) ||
83            !strcmp(typeid(fArray).name(),typeid(Float_t*).name() )) SetBit(kFloat);
84   //
85 }
86
87 //__________________________________________________________________________
88 template<class Element>
89 AliLHCDipValT<Element>::AliLHCDipValT(const AliLHCDipValT<Element> &src)
90 : TObject(src),fTimeStamp(src.fTimeStamp),fArray(0)
91 {
92   //copy constructor
93   SetUniqueID(0);
94   SetSize(src.GetSizeTotal());
95   memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));
96 }
97
98 //__________________________________________________________________________
99 template<class Element>
100 AliLHCDipValT<Element>& AliLHCDipValT<Element>::operator=(const AliLHCDipValT<Element> &src)
101 {
102   //assingment
103   if (this != &src) {
104     ((TObject*)this)->operator=(src);
105     SetUniqueID(0);
106     if (GetSizeTotal()!=src.GetSizeTotal()) SetSize(src.GetSizeTotal());
107     SetTimeStamp(src.GetTimeStamp());
108     memcpy(fArray,src.fArray,GetSizeTotal()*sizeof(Element));    
109   }
110   return *this;
111 }
112
113 //__________________________________________________________________________
114 template<class Element>
115 Char_t* AliLHCDipValT<Element>::TimeAsString(double t)
116 {
117   time_t tt = (time_t) t;
118   char* st = ctime(&tt);
119   *(strchr(st,'\n')) = 0;
120   return st;
121 }
122
123 //__________________________________________________________________________
124 template<class Element>
125 void AliLHCDipValT<Element>::SetValue(Int_t i,Element v)
126 {
127   //assign value
128   if (i>=GetSizeTotal() || i<0) {
129     AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
130     return;
131   }
132   fArray[i] = v;
133 }
134
135 //__________________________________________________________________________
136 template<class Element>
137 void AliLHCDipValT<Element>::SetValues(const Element* v, Int_t n)
138 {
139   //assign value
140   if (n!=GetSizeTotal()) SetSize(n);
141   memcpy(fArray,v,n*sizeof(Element));
142 }
143
144 //__________________________________________________________________________
145 template<class Element>
146 Element& AliLHCDipValT<Element>::operator[](Int_t i)
147 {
148   //obtain value refeterence
149   if (i>=GetSizeTotal() || i<0) {
150     AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
151     return fArray[0];
152   }
153   return fArray[i];
154 }
155
156 //__________________________________________________________________________
157 template<class Element>
158 Element AliLHCDipValT<Element>::operator[](Int_t i) const
159 {
160   //obtain value
161   if (i>=GetSizeTotal() || i<0) {
162     AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
163     return 0;
164   }
165   return fArray[i];
166 }
167
168 //__________________________________________________________________________
169 template<class Element>
170 Element AliLHCDipValT<Element>::GetValue(Int_t i) const
171 {
172   //obtain value
173   if (i>=GetSizeTotal() || i<0) {
174     AliError(Form("Index %d is out of range 0:%d",i,GetSizeTotal()-1));
175     return 0;
176   }
177   return fArray[i];
178 }
179
180 //__________________________________________________________________________
181 template<class Element>
182 void AliLHCDipValT<Element>::SetSize(Int_t sz)
183 {
184   //resize
185   Element* arr = 0;
186   if (sz>0) {
187     arr = new Element[sz];
188     int nc = GetSizeTotal() > sz ? sz:GetSizeTotal(); // n elems to copy
189     if (nc) memcpy(arr, fArray, nc*sizeof(Element));
190     if (nc<sz) memset(arr+nc, 0, (sz-nc)*sizeof(Element));
191     if (GetSizeTotal()) delete[] fArray;
192     fArray = arr;
193     SetUniqueID(sz);
194   }
195   else {
196     delete[] fArray;
197     SetUniqueID(0);
198   }
199 }
200
201 //__________________________________________________________________________
202 template<class Element>
203 void AliLHCDipValT<Element>::Print(const Option_t *opt) const
204 {
205   // print time and value
206   TString str = opt; 
207   str.ToLower();
208   if (str.Contains("raw")) printf("%.1f ",GetTimeStamp());
209   else printf("[%s] ",GetTimeAsString());
210   //
211   TString tp = typeid(fArray).name();
212   if ( tp==typeid(Char_t*).name() ) printf(": %s\n",(Char_t*)fArray);
213   else {
214     int sz = GetSize();
215     if (sz>1) printf("\n");
216     Bool_t eolOK = kFALSE;
217     for (int i=0;i<sz;i++) {
218       if      (tp == typeid(Int_t*).name()    || tp == typeid(UInt_t*).name() ) printf(" %+5d |" ,(Int_t)fArray[i]);
219       else if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) printf(" %+.3e |",(Float_t)fArray[i]);
220       else printf(" ");
221       eolOK = kFALSE;
222       if ( (i+1)%5 == 0) {printf("\n"); eolOK = kTRUE;}
223     }
224     if (IsLastSpecial()) {
225       if (sz>1 && !eolOK) {printf("\n"); eolOK = kTRUE;}
226       if (tp == typeid(Double_t*).name() || tp == typeid(Float_t*).name()) {
227         printf(" Error: %+e\n",(Float_t)fArray[sz]);
228         eolOK = kTRUE;
229       }
230     }
231     if (!eolOK) printf("\n");
232   }
233   //
234 }
235
236 //__________________________________________________________________________
237 template<class Element>
238 void AliLHCDipValT<Element>::Clear(const Option_t *)
239 {
240   // reset to 0 everything
241   SetTimeStamp(0);
242   memset(fArray,0,GetSizeTotal()*sizeof(Element));
243 }
244
245
246 typedef AliLHCDipValT<Double_t> AliLHCDipValD;
247 typedef AliLHCDipValT<Float_t>  AliLHCDipValF;
248 typedef AliLHCDipValT<Int_t>    AliLHCDipValI;
249 typedef AliLHCDipValT<Char_t>   AliLHCDipValC;
250
251
252
253 #endif