Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaVar.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 26/04/2000
2
3
5//
6// THaVar
7//
8// A "global variable". This is essentially a read-only pointer to the
9// actual data, along with support for data types and arrays. It can
10// be used to retrieve data from anywhere in memory, e.g. analysis results
11// of some detector object.
12//
13// THaVar objects are managed by the THaVarList.
14//
15// The standard way to define a variable is via the constructor, e.g.
16//
17// Double_t x = 1.5;
18// THaVar v("x","variable x",x);
19//
20// Constructors and Set() determine the data type automatically.
21// Supported types are
22// Double_t, Float_t, Long64_t, ULong64_t, Int_t, UInt_t,
23// Short_t, UShort_t, Char_t, UChar_t
24//
25// Arrays can be defined as follows:
26//
27// Double_t* a = new Double_t[10];
28// THaVar* va = new THaVar("a[10]","array of doubles",a);
29//
30// One-dimensional variable-size arrays are also supported. The actual
31// size must be contained in a variable of type Int_t.
32// Example:
33//
34// Double_t* a = new Double_t[20];
35// Int_t size;
36// GetValues( a, size ); //Fills a and sets size to number of data.
37// THaVar* va = new THaVar("a","var size array",a,&size);
38//
39// Any array size given in the name string is ignored (e.g. "a[20]"
40// would have the same effect as "a"). The data are always interpreted
41// as a one-dimensional array.
42//
43// Data are retrieved using GetValue(), which always returns Double_t.
44// If access to the raw data is needed, one can use GetValuePointer()
45// (with the appropriate caution).
46//
48
49#include "THaVar.h"
50#include "THaArrayString.h"
51#include "FixedArrayVar.h"
52#include "VariableArrayVar.h"
53#include "VectorVar.h"
55#include "VectorObjMethodVar.h"
56#include "TError.h"
57#include "DataType.h" // for kBig
58
59using namespace std;
60
63
64static const char* const here = "THaVar";
65
66//_____________________________________________________________________________
67template <typename T>
68THaVar::THaVar( const char* name, const char* descript, T& var,
69 const Int_t* count )
70 : TNamed(name,descript), fImpl(nullptr)
71{
72 // Constructor for basic types and fixed and variable-size arrays
73
74 VarType type = Vars::FindType( typeid(T) );
75 if( type >= kIntV && type <= kDoubleV ) {
76 if( count )
77 Warning( here, "Ignoring size counter for std::vector variable %s", name );
78 fImpl = new Podd::VectorVar( this, &var, type );
79 }
80 else if( type != kVarTypeEnd ) {
81 if( count )
82 fImpl = new Podd::VariableArrayVar( this, &var, type, count );
83 else if( fName.Index("[") != kNPOS )
84 fImpl = new Podd::FixedArrayVar( this, &var, type );
85 else
86 fImpl = new Podd::Variable( this, &var, type );
87
88 if( fImpl->IsError() )
89 MakeZombie();
90 }
91 else {
92 Error( here, "Unsupported data type for variable %s", name );
93 MakeZombie();
94 }
95}
96
97//_____________________________________________________________________________
98THaVar::THaVar( const char* name, const char* descript, const void* obj,
99 VarType type, Int_t offset, TMethodCall* method, const Int_t* count )
100 : TNamed(name,descript), fImpl(nullptr)
101{
102 // Generic constructor (used by THaVarList::DefineByType)
103
104 if( type == kObject || type == kObjectP || type == kObject2P ||
105 type == kObjectV || type == kObjectPV ) {
106 Error( here, "Variable %s: Object types not (yet) supported", name );
107 MakeZombie();
108 return;
109 }
110 else if( type >= kIntM && type <= kDoubleM ) {
111 Error( here, "Variable %s: Matrix types not (yet) supported", name );
112 MakeZombie();
113 return;
114 }
115 else if( type >= kIntV && type <= kDoubleV ) {
116 if( count ) {
117 Warning( here, "Ignoring size counter for std::vector variable %s. "
118 "Fix code or call expert", name );
119 }
120 fImpl = new Podd::VectorVar( this, obj, type );
121 }
122 else if( count ) {
123 if( offset >= 0 || method ) {
124 Error( here, "Variable %s: Inconsistent arguments. Cannot specify "
125 "both count and offset/method", name );
126 MakeZombie();
127 return;
128 }
129 fImpl = new Podd::VariableArrayVar( this, obj, type, count );
130 }
131 else if( method || offset >= 0 ) {
132 if( method && offset >= 0 ) {
133 if( offset > 0 ) {
134 Warning( here, "Variable %s: Offset > 0 ignored for method call on "
135 "object in collection. Fix code or call expert", name );
136 }
137 fImpl = new Podd::SeqCollectionMethodVar( this, obj, type, method );
138 } else if( !method )
139 fImpl = new Podd::SeqCollectionVar( this, obj, type, offset );
140 else
141 fImpl = new Podd::MethodVar( this, obj, type, method );
142 }
143 else if( fName.Index("[") != kNPOS )
144 fImpl = new Podd::FixedArrayVar( this, obj, type );
145 else
146 fImpl = new Podd::Variable( this, obj, type );
147
148 if( fImpl->IsError() )
149 MakeZombie();
150}
151
152//_____________________________________________________________________________
153THaVar::THaVar( const char* name, const char* descript, const void* obj,
154 VarType type, Int_t elem_size, Int_t offset, TMethodCall* method )
155 : TNamed(name,descript), fImpl(nullptr)
156{
157 // Generic constructor for std::vector<TObject(*)>
158 // (used by THaVarList::DefineByType)
159
160 if( type > kByte ) {
161 Error(here, "Variable %s: Illegal type %s for data in std::vector "
162 "of objects", name, Vars::GetTypeName(type) );
163 MakeZombie();
164 return;
165 }
166 else if( elem_size < 0 || offset < 0 ) {
167 Error( here, "Variable %s: Illegal parameters elem_size = %d, "
168 "offset = %d. Must be >= 0", name, elem_size, offset );
169 MakeZombie();
170 return;
171 }
172 if( method ) {
173 if( offset > 0 ) {
174 Warning( here, "Variable %s: Offset > 0 ignored for method call on "
175 "object", name );
176 }
177 fImpl = new Podd::VectorObjMethodVar( this, obj, type, elem_size, method );
178 } else {
179 fImpl = new Podd::VectorObjVar( this, obj, type, elem_size, offset );
180 }
181
182 if( fImpl->IsError() )
183 MakeZombie();
184}
185
186//_____________________________________________________________________________
188{
189 delete fImpl;
190}
191
192//_____________________________________________________________________________
193Int_t THaVar::Index( const char* s ) const
194{
195 // Return linear index into this array variable corresponding
196 // to the array element described by the string 's'.
197 // 's' must be either a single integer subscript (for a 1-d array)
198 // or a comma-separated list of subscripts (for multi-dimensional arrays).
199 //
200 // NOTE: This method is less efficient than
201 // THaVar::Index( THaArrayString& ) above because the string has
202 // to be parsed first.
203 //
204 // Return -1 if subscript(s) out of bound(s) or -2 if incompatible arrays.
205
206 if( !s || !*s )
207 return 0;
208
209 TString str = "t[";
210 str.Append(s);
211 str.Append(']');
212
213 return Index( THaArrayString(str.Data()) );
214}
215
216//_____________________________________________________________________________
217// Explicit template instantiations for supported types
218
219template THaVar::THaVar( const char*, const char*, Double_t&, const Int_t* );
220template THaVar::THaVar( const char*, const char*, Float_t&, const Int_t* );
221template THaVar::THaVar( const char*, const char*, Long64_t&, const Int_t* );
222template THaVar::THaVar( const char*, const char*, ULong64_t&, const Int_t* );
223template THaVar::THaVar( const char*, const char*, Int_t&, const Int_t* );
224template THaVar::THaVar( const char*, const char*, UInt_t&, const Int_t* );
225template THaVar::THaVar( const char*, const char*, Short_t&, const Int_t* );
226template THaVar::THaVar( const char*, const char*, UShort_t&, const Int_t* );
227template THaVar::THaVar( const char*, const char*, Char_t&, const Int_t* );
228template THaVar::THaVar( const char*, const char*, UChar_t&, const Int_t* );
229
230template THaVar::THaVar( const char*, const char*, Double_t*&, const Int_t* );
231template THaVar::THaVar( const char*, const char*, Float_t*&, const Int_t* );
232template THaVar::THaVar( const char*, const char*, Long64_t*&, const Int_t* );
233template THaVar::THaVar( const char*, const char*, ULong64_t*&, const Int_t* );
234template THaVar::THaVar( const char*, const char*, Int_t*&, const Int_t* );
235template THaVar::THaVar( const char*, const char*, UInt_t*&, const Int_t* );
236template THaVar::THaVar( const char*, const char*, Short_t*&, const Int_t* );
237template THaVar::THaVar( const char*, const char*, UShort_t*&, const Int_t* );
238template THaVar::THaVar( const char*, const char*, Char_t*&, const Int_t* );
239template THaVar::THaVar( const char*, const char*, UChar_t*&, const Int_t* );
240
241template THaVar::THaVar( const char*, const char*, Double_t**&, const Int_t* );
242template THaVar::THaVar( const char*, const char*, Float_t**&, const Int_t* );
243template THaVar::THaVar( const char*, const char*, Long64_t**&, const Int_t* );
244template THaVar::THaVar( const char*, const char*, ULong64_t**&, const Int_t* );
245template THaVar::THaVar( const char*, const char*, Int_t**&, const Int_t* );
246template THaVar::THaVar( const char*, const char*, UInt_t**&, const Int_t* );
247template THaVar::THaVar( const char*, const char*, Short_t**&, const Int_t* );
248template THaVar::THaVar( const char*, const char*, UShort_t**&, const Int_t* );
249template THaVar::THaVar( const char*, const char*, Char_t**&, const Int_t* );
250template THaVar::THaVar( const char*, const char*, UChar_t**&, const Int_t* );
251
252template THaVar::THaVar( const char*, const char*, vector<int>&, const Int_t* );
253template THaVar::THaVar( const char*, const char*, vector<unsigned int>&, const Int_t* );
254template THaVar::THaVar( const char*, const char*, vector<double>&, const Int_t* );
255template THaVar::THaVar( const char*, const char*, vector<float>&, const Int_t* );
256
257//_____________________________________________________________________________
int Int_t
unsigned int UInt_t
const Data_t kBig
Definition DataType.h:15
unsigned short UShort_t
const Ssiz_t kNPOS
unsigned char UChar_t
char Char_t
float Float_t
short Short_t
double Double_t
const Long64_t kMaxLong64
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 offset
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
virtual Bool_t IsError() const
Definition Variable.cxx:433
THaVar(const char *name, const char *descript, T &var, const Int_t *count=nullptr)
Definition THaVar.cxx:68
Podd::Variable * fImpl
Definition THaVar.h:80
Int_t Index(const char *subscripts) const
Definition THaVar.cxx:193
virtual ~THaVar()
Definition THaVar.cxx:187
static const Double_t kInvalid
Definition THaVar.h:24
static const Long64_t kInvalidInt
Definition THaVar.h:23
TString fName
virtual void Warning(const char *method, const char *msgfmt,...) const
virtual void Error(const char *method, const char *msgfmt,...) const
void MakeZombie()
const char * Data() const
TString & Append(char c, Ssiz_t rep=1)
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
long long Long64_t
unsigned long long ULong64_t
double T(double x)
STL namespace.
ClassImp(TPyArg)