Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
Variable.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 16/02/2016
2
3
5//
6// Podd::Variable
7//
9
10#include "THaVar.h"
11#include "THaArrayString.h"
12#include "TError.h"
13#include <cassert>
14#include <iostream>
15#include <cstring>
16
17using namespace std;
18
19#define kInvalid THaVar::kInvalid
20#define kInvalidInt THaVar::kInvalidInt
21
22namespace Podd {
23
24//_____________________________________________________________________________
25Variable::Variable( THaVar* pvar, const void* addr, VarType type )
26 : fSelf(pvar), fValueP(addr), fType(type)
27{
28 // Base class constructor
29
30 assert(fSelf);
31 assert(fValueP);
32}
33
34//_____________________________________________________________________________
35Variable::~Variable() = default;
36
37//_____________________________________________________________________________
38Bool_t Variable::VerifyNonArrayName( const char* name ) const
39{
40 // Return true if the variable string is NOT an array
41
42 const char* const here = "Variable::VerifyNonArrayName";
43
44 THaArrayString parsed_name(name);
45
46 if( parsed_name.IsError() || parsed_name.IsArray() ) {
47 fSelf->Error( here, "Illegal name for variable \"%s\". "
48 "Brackets and commas not allowed.", name );
49 return false;
50 }
51
52 return true;
53}
54
55//_____________________________________________________________________________
56const char* Variable::GetName() const
57{
58 return fSelf->TNamed::GetName();
59}
60
61//_____________________________________________________________________________
63{
64 return Vars::GetTypeSize( fType );
65}
66
67//_____________________________________________________________________________
68const char* Variable::GetTypeName() const
69{
70 return Vars::GetTypeName( fType );
71}
72
73//_____________________________________________________________________________
75{
76 // Get number of elements of the variable
77
78 return 1;
79}
80
81//_____________________________________________________________________________
83{
84 // Return number of dimensions of array. Scalars always have 0 dimensions.
85
86 return 0;
87}
88
89//_____________________________________________________________________________
90const Int_t* Variable::GetDim() const
91{
92 // Return array of dimensions of the array. Scalers return 0
93
94 return nullptr;
95}
96
97//_____________________________________________________________________________
98std::vector<Double_t> Variable::GetValues() const
99{
100 std::vector<Double_t> res;
101 res.reserve(GetLen() );
102 for(int i = 0 ; i < GetLen(); i++) {
103 res.push_back(GetValue(i));
104 }
105 return res;
106}
107
108//_____________________________________________________________________________
110{
111 // Retrieve current value of this global variable as double.
112 // If the variable is an array/vector, return its i-th element.
113
114 const char* const here = "GetValue()";
115
116 if( i<0 || i>=GetLen() ) {
117 fSelf->Error(here, "Index out of range, variable %s, index %d", GetName(), i );
118 return kInvalid;
119 }
120
121 const void* loc = GetDataPointer(i);
122 if( !loc )
123 return kInvalid;
124
125 switch( fType ) {
126 case kDouble:
127 case kDoubleP:
128 case kDouble2P:
129 case kDoubleV:
130 case kDoubleM:
131 return *static_cast<const Double_t*>(loc);
132 case kFloat:
133 case kFloatP:
134 case kFloat2P:
135 case kFloatV:
136 case kFloatM:
137 return *static_cast<const Float_t*>(loc);
138 case kLong:
139 case kLongP:
140 case kLong2P:
141 return *static_cast<const Long64_t*>(loc);
142 case kULong:
143 case kULongP:
144 case kULong2P:
145 return *static_cast<const ULong64_t*>(loc);
146 case kInt:
147 case kIntP:
148 case kInt2P:
149 case kIntV:
150 case kIntM:
151 return *static_cast<const Int_t*>(loc);
152 case kUInt:
153 case kUIntP:
154 case kUInt2P:
155 case kUIntV:
156 return *static_cast<const UInt_t*>(loc);
157 case kShort:
158 case kShortP:
159 case kShort2P:
160 return *static_cast<const Short_t*>(loc);
161 case kUShort:
162 case kUShortP:
163 case kUShort2P:
164 return *static_cast<const UShort_t*>(loc);
165 case kChar:
166 case kCharP:
167 case kChar2P:
168 return *static_cast<const Char_t*>(loc);
169 case kUChar:
170 case kUCharP:
171 case kUChar2P:
172 return *static_cast<const UChar_t*>(loc);
173 case kObject:
174 case kObjectP:
175 case kObject2P:
176 fSelf->Error( here, "Cannot get value from composite object, variable %s",
177 GetName() );
178 break;
179 default:
180 fSelf->Error( here, "Unsupported data type %s, variable %s",
181 Vars::GetEnumName(fType), GetName() );
182 break;
183 }
184 return kInvalid;
185}
186
187//_____________________________________________________________________________
189{
190 // Retrieve current value of this global variable as integer.
191 // If the variable is an array/vector, return its i-th element.
192
193 const char* const here = "GetValueInt()";
194
195 if( i<0 || i>=GetLen() ) {
196 fSelf->Error(here, "Index out of range, variable %s, index %d", GetName(), i );
197 return kInvalidInt;
198 }
199
200 if( IsFloat() ) {
201 fSelf->Error(here, "Variable %s has floating-point type %s, "
202 "call GetValue() instead", GetName(), GetTypeName() );
203 return kInvalidInt;
204 }
205
206 const void* loc = GetDataPointer(i);
207 if( !loc )
208 return kInvalidInt;
209
210 switch( fType ) {
211 case kLong:
212 case kLongP:
213 case kLong2P:
214 return *static_cast<const Long64_t*>(loc);
215 case kULong:
216 case kULongP:
217 case kULong2P:
218 return *static_cast<const ULong64_t*>(loc);
219 case kInt:
220 case kIntP:
221 case kInt2P:
222 case kIntV:
223 case kIntM:
224 return *static_cast<const Int_t*>(loc);
225 case kUInt:
226 case kUIntP:
227 case kUInt2P:
228 case kUIntV:
229 return *static_cast<const UInt_t*>(loc);
230 case kShort:
231 case kShortP:
232 case kShort2P:
233 return *static_cast<const Short_t*>(loc);
234 case kUShort:
235 case kUShortP:
236 case kUShort2P:
237 return *static_cast<const UShort_t*>(loc);
238 case kChar:
239 case kCharP:
240 case kChar2P:
241 return *static_cast<const Char_t*>(loc);
242 case kUChar:
243 case kUCharP:
244 case kUChar2P:
245 return *static_cast<const UChar_t*>(loc);
246 case kObject:
247 case kObjectP:
248 case kObject2P:
249 fSelf->Error( here, "Cannot get value from composite object, variable %s",
250 GetName() );
251 break;
252 default:
253 fSelf->Error( here, "Unsupported data type %s, variable %s",
254 Vars::GetEnumName(fType), GetName() );
255 break;
256 }
257
258 return kInvalidInt;
259}
260
261//_____________________________________________________________________________
262const void* Variable::GetDataPointer( Int_t i ) const
263{
264 // Get pointer to i-th element of the data in memory, including to data in
265 // a non-contiguous pointer array
266
267 const char* const here = "GetDataPointer()";
268
269 assert( fValueP && IsBasic() );
270 static_assert( sizeof(ULong_t) == sizeof(void*), "ULong_t must be of pointer size" );
271
272 if( i<0 || i>=GetLen() ) {
273 fSelf->Error(here, "Index out of range, variable %s, index %d", GetName(), i );
274 return nullptr;
275 }
276
277 if( fType >= kDouble && fType <= kUChar ) {
278 ULong_t loc = reinterpret_cast<ULong_t>(fValueP) + i*GetTypeSize();
279 return reinterpret_cast<const void*>(loc);
280 }
281
282 if( fType >= kDoubleP && fType <= kUCharP ) {
283 const void* ptr = *reinterpret_cast<void* const *>(fValueP);
284 ULong_t loc = reinterpret_cast<ULong_t>(ptr) + i*GetTypeSize();
285 return reinterpret_cast<const void*>(loc);
286 }
287
288 if( fType >= kDouble2P && fType <= kUChar2P ) {
289 const void** const *ptr = reinterpret_cast<const void** const *>(fValueP);
290 if( !*ptr )
291 return nullptr;
292 return (*ptr)[i];
293 }
294
295 return nullptr;
296}
297
298//_____________________________________________________________________________
299size_t Variable::GetData( void* buf ) const
300{
301 // Copy the data into the provided buffer. The buffer must have been allocated
302 // by the caller and must be able to hold at least GetLen()*GetTypeSize() bytes.
303 // The return value is the number of bytes actually copied.
304
305 Int_t nelem = GetLen();
306 size_t type_size = GetTypeSize();
307 size_t nbytes = nelem * type_size;
308 if( nbytes == 0 || !fValueP )
309 return 0;
310
311 if( IsContiguous() ) {
312 // Contiguous data can be copied in one fell swoop
313 const void* src = GetDataPointer();
314 if( !src )
315 return 0;
316 memcpy( buf, src, nbytes );
317 }
318 else {
319 // Non-contiguous data (e.g. pointer array) must be copied element by element
320 static_assert( sizeof(char) == 1, "Need sizeof(char) = 1 byte" );
321 nbytes = 0;
322 for( Int_t i = 0; i<nelem; ++i ) {
323 const void* src = GetDataPointer(i);
324 if( !src )
325 return nbytes;
326 memcpy( static_cast<char*>(buf)+nbytes, src, type_size );
327 nbytes += type_size;
328 }
329 }
330
331 return nbytes;
332}
333
334//_____________________________________________________________________________
335size_t Variable::GetData( void* buf, Int_t i ) const
336{
337 // Copy the data from the i-th element into the provided buffer. The buffer
338 // must have been allocated by the caller and must be able to hold at least
339 // GetTypeSize() bytes.
340 // The return value is the number of bytes actually copied.
341
342 const char* const here = "GetData()";
343
344 size_t nbytes = GetTypeSize();
345 if( nbytes == 0 || !fValueP )
346 return 0;
347
348 if( i<0 || i>=GetLen() ) {
349 fSelf->Error(here, "Index out of range, variable %s, index %d", GetName(), i );
350 return 0;
351 }
352
353 const void* src = GetDataPointer(i);
354 if( !src )
355 return 0;
356
357 memcpy( buf, src, nbytes );
358
359 return nbytes;
360}
361
362//_____________________________________________________________________________
364{
365 // Compare the size (=number of elements) of this variable to that of 'rhs'.
366 // This is the basic version; derived classes will likely override
367
368 Bool_t is_array = IsArray();
369 if( is_array != rhs.IsArray()) // Must be same array/non-array
370 return false;
371 if( !is_array ) // Scalars always agree
372 return true;
373 //FIXME: this isn't correct
374 return (GetLen() == rhs.GetLen()); // Arrays must have same length
375}
376
377//_____________________________________________________________________________
379{
380 return false;
381}
382
383//_____________________________________________________________________________
385{
386 // Return linear index into this array variable corresponding
387 // to the array element described by 'elem'.
388 // Return -1 if subscript(s) out of bound(s) or -2 if incompatible arrays.
389
390 if( elem.IsError() ) return -1;
391 if( !elem.IsArray() ) return 0;
392
393 Int_t ndim = GetNdim();
394 if( ndim != elem.GetNdim() ) return -2;
395
396 const Int_t *adim = GetDim();
397
398 Int_t index = elem[0];
399 for( Int_t i = 0; i<ndim; i++ ) {
400 if( elem[i] >= adim[i] ) return -1;
401 if( i>0 )
402 index = index*adim[i] + elem[i];
403 }
404 if( index >= GetLen() ) return -1;
405 return index;
406}
407
408//_____________________________________________________________________________
410{
411 // Data are an array (GetLen() may be != 1)
412
413 return (GetNdim() > 0);
414}
415
416//_____________________________________________________________________________
418{
419 // Data are basic (POD variable or array)
420
421 return true;
422}
423
424//_____________________________________________________________________________
426{
427 // Data are contiguous in memory
428
429 return true;
430}
431
432//_____________________________________________________________________________
434{
435 // Variable is in error state/invalid (typically because error in constructor)
436
437 return fValueP == nullptr;
438}
439
440//_____________________________________________________________________________
442{
443 // Data are floating-point type
444
445 return ( fType == kDouble || fType == kFloat ||
446 fType == kDoubleP || fType == kFloatP ||
447 fType == kDoubleV || fType == kDoubleM ||
448 fType == kFloatV || fType == kFloatM ||
449 fType == kDouble2P || fType == kFloat2P );
450}
451
452//_____________________________________________________________________________
454{
455 // Data are an array of pointers to data
456
457 return false;
458}
459
460//_____________________________________________________________________________
462{
463 // Variable refers to an object that can be streamed via ROOT I/O
464
465 return IsTObject();
466}
467
468//_____________________________________________________________________________
470{
471 // Variable refers to an object that inherits from TObject
472
473 return false;
474}
475
476//_____________________________________________________________________________
478{
479 // Variable is a variable-sized array
480
481 return false;
482}
483
484//_____________________________________________________________________________
486{
487 // Variable is a std::vector or std::vector< std::vector >
488
489 //TODO: support matrix types
490 // return fType >= kIntV && fType <= kDoubleM;
491 return fType >= kIntV && fType <= kDoubleV;
492}
493
494//_____________________________________________________________________________
495void Variable::Print(Option_t* option) const
496{
497 // Print a description of this variable. If option=="FULL" (default), also
498 // print current data
499
500 fSelf->TNamed::Print(option);
501
502 if( strcmp(option, "FULL") != 0 ) return;
503
504 cout << "(" << fSelf->GetTypeName() << ") ";
505 Int_t len = GetLen();
506 if( IsArray() )
507 cout << "[" << len << "]";
508
509 for( Int_t i=0; i<len; i++ ) {
510 cout << " ";
511 if( IsFloat() )
512 cout << GetValue(i);
513 else
514 cout << GetValueInt(i);
515 }
516 cout << endl;
517
518}
519
520//_____________________________________________________________________________
521void Variable::SetName( const char* name )
522{
523 // Set the name of the variable
524
526 return;
527
528 fSelf->TNamed::SetName( name );
529}
530
531//_____________________________________________________________________________
532void Variable::SetNameTitle( const char* name, const char* descript )
533{
534 // Set name and description of the variable
535
537 return;
538
539 fSelf->TNamed::SetNameTitle( name, descript );
540}
541
542//_____________________________________________________________________________
543
544} // namespace Podd
int Int_t
unsigned int UInt_t
unsigned long ULong_t
bool Bool_t
unsigned short UShort_t
unsigned char UChar_t
char Char_t
float Float_t
short Short_t
double Double_t
const char Option_t
#define kInvalid
#define kInvalidInt
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
static const char *const here
Definition THaVar.cxx:64
size_t GetTypeSize() const
Definition Variable.cxx:62
virtual Bool_t IsFloat() const
Definition Variable.cxx:441
virtual void SetName(const char *name)
Definition Variable.cxx:521
virtual const void * GetDataPointer(Int_t i=0) const
Definition Variable.cxx:262
virtual const Int_t * GetDim() const
Definition Variable.cxx:90
const void * fValueP
Definition Variable.h:60
Bool_t VerifyNonArrayName(const char *name) const
Definition Variable.cxx:38
virtual Double_t GetValue(Int_t i=0) const
Definition Variable.cxx:109
virtual Int_t Index(const THaArrayString &) const
Definition Variable.cxx:384
virtual Int_t GetNdim() const
Definition Variable.cxx:82
virtual Bool_t HasSizeVar() const
Definition Variable.cxx:378
virtual Bool_t IsPointerArray() const
Definition Variable.cxx:453
virtual std::vector< Double_t > GetValues() const
Definition Variable.cxx:98
const char * GetName() const
Definition Variable.cxx:56
virtual Bool_t IsBasic() const
Definition Variable.cxx:417
virtual Bool_t IsArray() const
Definition Variable.cxx:409
Variable(THaVar *pvar, const void *addr, VarType type)
Definition Variable.cxx:25
virtual Bool_t IsTObject() const
Definition Variable.cxx:469
virtual Bool_t HasSameSize(const Variable &rhs) const
Definition Variable.cxx:363
virtual Bool_t IsError() const
Definition Variable.cxx:433
virtual void SetNameTitle(const char *name, const char *descript)
Definition Variable.cxx:532
virtual void Print(Option_t *opt) const
Definition Variable.cxx:495
virtual Bool_t IsVector() const
Definition Variable.cxx:485
THaVar * fSelf
Definition Variable.h:59
VarType fType
Definition Variable.h:61
virtual Long64_t GetValueInt(Int_t i=0) const
Definition Variable.cxx:188
virtual size_t GetData(void *buf) const
Definition Variable.cxx:299
const char * GetTypeName() const
Definition Variable.cxx:68
virtual Int_t GetLen() const
Definition Variable.cxx:74
virtual Bool_t IsContiguous() const
Definition Variable.cxx:425
virtual Bool_t IsStreamable() const
Definition Variable.cxx:461
virtual Bool_t IsVarArray() const
Definition Variable.cxx:477
virtual ~Variable()
Bool_t IsArray() const
Bool_t IsError() const
Int_t GetNdim() const
const char * GetTypeName() const
Definition THaVar.h:45
virtual void Error(const char *method, const char *msgfmt,...) const
long long Long64_t
unsigned long long ULong64_t
STL namespace.