-
Notifications
You must be signed in to change notification settings - Fork 52
/
THaVarList.C
680 lines (613 loc) · 20.2 KB
/
THaVarList.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//*-- Author : Ole Hansen 27/04/00
//////////////////////////////////////////////////////////////////////////
//
// THaVarList
//
// A specialized THashList containing global variables of the
// Hall A analyzer.
//
// Variables can be added to the list as follows:
// (assumes THaVarList* gHaVars = new THaVarList;)
//
// Double_t x;
// gHaVars->Define("x","x variable",x);
//
// or for short
// gHaVars->Define("x",x);
//
// Likewise for arrays:
//
// Double_t a[10];
// gHaVars->Define("a[10]","array of doubles",a);
//
// or
// gHaVars->Define("a[10]",a);
//
// (Note: Array definitions register only the name before the bracket
// as the variable name; "a" in the example above. Hence
//
// gHaVars->Define("a",x)
// and
// gHaVars->Define("a[10]",a)
//
// result in a name conflict, i.e. the second definition will fail.
//
// Supported data types are:
//
// Double_t, Float_t, Long_t, ULong_t, Int_t, UInt_t,
// Short_t, UShort_t, Char_t, Byte_t.
//
// The type is determined automatically through function overloading.
// Do not specify the type explicitly using casts. Instead, define variables
// with the type you need.
//
// To get maximum efficiency and accuracy, variables should be Double_t.
// For calculations in THaFormula/THaCut, all data will be promoted to
// double precision anyhow.
//
//////////////////////////////////////////////////////////////////////////
#include "THaVarList.h"
#include "THaVar.h"
#include "THaRTTI.h"
#include "THaArrayString.h"
#include "TRegexp.h"
#include "TString.h"
#include "TClass.h"
#include "TMethodCall.h"
#include "TFunction.h"
#include "TROOT.h"
#include "TMath.h"
#include <cstring>
#include <string> // for TFunction::GetReturnTypeNormalizedName
#include <cassert>
ClassImp(THaVarList)
using namespace std;
static const Int_t kInitVarListCapacity = 100;
static const Int_t kVarListRehashLevel = 3;
//_____________________________________________________________________________
THaVarList::THaVarList() : THashList(kInitVarListCapacity, kVarListRehashLevel)
{
// Default constructor
// THaVarList "owns" the variables put into it, i.e. if the list is deleted
// so are the global variables in it.
SetOwner(kTRUE);
}
//_____________________________________________________________________________
THaVarList::~THaVarList()
{
// Destructor
}
//_____________________________________________________________________________
THaVar* THaVarList::DefineByType( const char* name, const char* descript,
const void* var, VarType type,
const Int_t* count, const char* errloc )
{
// Define a global variable with given type.
// Duplicate names are not allowed; if a name already exists, return 0
THaVar* ptr = Find( name );
if( ptr ) {
Warning( errloc, "Variable %s already exists. Not redefined.",
ptr->GetName() );
return 0;
}
ptr = new THaVar( name, descript, var, type, -1, 0, count );
if( ptr && !ptr->IsZombie() )
AddLast( ptr );
else {
Warning( errloc, "Error creating variable %s", name );
delete ptr; ptr = 0;
}
return ptr;
}
//_____________________________________________________________________________
THaVar* THaVarList::DefineByRTTI( const TString& name, const TString& desc,
const TString& def, const void* const obj,
TClass* const cl, const char* errloc )
{
// Define variable via its ROOT RTTI
typedef vector<TSeqCollection*> VecSC_t;
assert( sizeof(ULong_t) == sizeof(void*) ); // ULong_t must be of pointer size
if( !obj || !cl ) {
Warning( errloc, "Invalid class or object. Variable %s not defined.",
name.Data() );
return 0;
}
THaVar* var = Find( name );
if( var ) {
Warning( errloc, "Variable %s already exists. Not redefined.",
var->GetName() );
return 0;
}
// Find up to two dots in the definition and extract the strings between them
Ssiz_t pos = 0, ppos = 0;
Int_t ndot = 0;
TString s[3];
while( (pos = def.Index( ".", pos )) != kNPOS && ndot<2 ) {
Int_t len = pos - ppos;
if( len >= 0 )
s[ndot] = def(ppos, len);
ppos = ++pos;
++ndot;
}
s[ndot] = def(ppos, def.Length()-ppos );
// More than two dots?
if( pos != kNPOS ) {
Warning( errloc, "Too many dots in definition of variable %s (%s). "
"Variable not defined.", name.Data(), desc.Data() );
return 0;
}
// Any zero-length strings?
Int_t i = 0;
while( i <= ndot )
if( s[i++].Length() == 0 )
return 0;
TClass* theClass = 0;
ULong_t loc = (ULong_t)obj;
THaRTTI objrtti;
Int_t vecidx = -1;
// Parse RTTI info
switch( ndot ) {
case 0:
// Simple member variable or pointer to basic type
theClass = cl;
break;
case 1:
// Member variable contained in a member object, or pointer to it (->scalar);
// or member variable contained in an object inside a vector (->vararray)
// (like ndot==2, except element type is known)
objrtti.Find( cl, s[0], obj );
if( !objrtti.IsValid() ||
!(objrtti.IsObject() || objrtti.IsObjVector()) ) {
Warning( errloc, "Unsupported type of data member %s. "
"Variable %s (%s) not defined.",
s[0].Data(), name.Data(), desc.Data() );
return 0;
}
loc += objrtti.GetOffset();
if( objrtti.IsPointer() ) {
void** ploc = (void**)loc;
loc = (ULong_t)*ploc;
}
theClass = objrtti.GetClass();
assert( theClass ); // else objrtti.Find should not have succeeded
break;
case 2:
// TSeqCollection member object, or pointer to it, or std::vector<TSeqCollection*>
objrtti.Find( cl, s[0], obj );
if( !objrtti.IsValid() || !objrtti.IsObject() ) {
// Check for vector element
bool good = false;
THaArrayString s0(s[0]);
if( s0 && s0.IsArray() && s0.GetNdim() == 1 ) {
objrtti.Find( cl, s0, obj );
if( objrtti.IsValid() && objrtti.IsObjVector() &&
objrtti.GetType() == kObjectPV ) {
vecidx = s0[0];
good = true;
}
}
if( !good ) {
Warning( errloc, "Unsupported type of data member %s. "
"Variable %s (%s) not defined.",
s[0].Data(), name.Data(), desc.Data() );
return 0;
}
}
theClass = objrtti.GetClass();
assert( theClass );
if( !theClass->InheritsFrom( "TSeqCollection" )) {
Warning( errloc, "Data member %s is not a TSeqCollection. "
"Variable %s (%s) not defined.",
s[0].Data(), name.Data(), desc.Data() );
return 0;
}
loc += objrtti.GetOffset();
if( objrtti.IsPointer() ) {
void** ploc = (void**)loc;
loc = (ULong_t)*ploc;
}
// For std::vector<TSeqCollection*>, get the requested vector element
if( objrtti.IsObjVector() ) {
VecSC_t vec = *reinterpret_cast<VecSC_t*>(loc);
if( vecidx < 0 || vecidx >= (Int_t)vec.size() ) {
Warning( errloc, "Illegal index %d into std::vector %s. "
"Current size = %d. Variable %s (%s) not defined.",
vecidx, s[0].Data(), (Int_t)vec.size(), name.Data(),
desc.Data() );
return 0;
}
loc = reinterpret_cast<ULong_t>(vec[vecidx]);
if( !loc ) {
Warning( errloc, "Null pointer in std::vector %s. "
"Variable %s (%s) not defined.",
s[0].Data(), name.Data(), desc.Data() );
return 0;
}
objrtti.Reset(); // clear objrtti.IsObjVector()
}
theClass = gROOT->GetClass( s[1] );
if( !theClass ) {
Warning( errloc, "Cannot determine class of container "
"member object %s. Variable %s (%s) not defined.",
s[1].Data(), name.Data(), desc.Data() );
return 0;
}
break;
default:
assert(false); // not reached
break;
}
// Define the variable based on the parse results
Bool_t function = s[ndot].EndsWith("()");
if( !function ) {
// Member variables
THaRTTI rtti;
rtti.Find( theClass, s[ndot], (ndot==0) ? obj : 0 );
if( !rtti.IsValid() ) {
Warning( errloc, "No RTTI information for variable %s. "
"Not defined.", name.Data() );
return 0;
}
if( rtti.IsObject() ) {
Warning( errloc, "Variable %s is an object. Must be a basic type. "
"Not defined.", name.Data() );
return 0;
}
if( ndot < 2 && !objrtti.IsObjVector() ) {
// Basic data, arrays/vectors of basic data, or basic data in single objects
loc += rtti.GetOffset();
TString theName(name);
Int_t* count_loc = 0;
switch( rtti.GetArrayType() ) {
case THaRTTI::kScalar:
case THaRTTI::kVector:
break;
case THaRTTI::kFixed:
theName.Append( rtti.GetSubscript() );
break;
case THaRTTI::kVariable:
count_loc = (Int_t*)( (ULong_t)obj + rtti.GetCountOffset() );
break;
}
var = DefineByType( theName, desc, (void*)loc, rtti.GetType(),
count_loc, errloc );
} else {
// Collections of objects
if( !objrtti.IsObjVector() )
var = new THaVar( name, desc, (void*)loc, rtti.GetType(),
rtti.GetOffset() );
else {
assert( ndot == 1 );
VarType type = objrtti.GetType();
assert( type == kObjectV || type == kObjectPV );
Int_t sz = (type == kObjectV) ? objrtti.GetClass()->Size() : 0;
var = new THaVar( name, desc, (void*)loc, rtti.GetType(), sz,
rtti.GetOffset() );
}
if( var && !var->IsZombie() )
AddLast( var );
else {
Warning( errloc, "Error creating variable %s", name.Data() );
delete var;
return 0;
}
}
} else { // if( !function )
// Member functions
TString funcName(s[ndot]);
Ssiz_t i = funcName.Index( '(' );
assert( i != kNPOS ); // else EndsWith("()") lied
funcName = funcName(0,i);
TMethodCall* theMethod = new TMethodCall( theClass, funcName, "" );
if( !theMethod->IsValid() ) {
Warning( errloc, "Error getting function information for variable %s. "
"Not defined.", name.Data() );
delete theMethod;
return 0;
}
TFunction* func = theMethod->GetMethod();
if( !func ) {
Warning( errloc, "Function %s does not exist. Variable %s not defined.",
s[ndot].Data(), name.Data() );
delete theMethod;
return 0;
}
TMethodCall::EReturnType rtype = theMethod->ReturnType();
if( rtype != TMethodCall::kLong && rtype != TMethodCall::kDouble ) {
Warning( errloc, "Unsupported return type for function %s. "
"Variable %s not defined.", s[ndot].Data(), name.Data() );
delete theMethod;
return 0;
}
#if ROOT_VERSION_CODE < ROOT_VERSION(5,34,6)
VarType type = ( rtype == TMethodCall::kLong ) ? kLong : kDouble;
#else
// Attempt to get the real function return type
VarType type = kVarTypeEnd;
string ntype = func->GetReturnTypeNormalizedName();
if( ntype == "double" )
type = kDouble;
else if( (sizeof(int) == sizeof(Int_t) && ntype == "int") ||
(sizeof(long) == sizeof(Int_t) && ntype == "long") )
type = kInt;
else if( (sizeof(unsigned int) == sizeof(UInt_t) && ntype == "unsigned int") ||
(sizeof(unsigned long) == sizeof(UInt_t) && ntype == "unsigned long") )
type = kUInt;
else if( ntype == "float" )
type = kFloat;
else if( ntype == "bool" ) {
if( sizeof(bool) == sizeof(Char_t) )
type = kChar;
else if( sizeof(bool) == sizeof(Short_t) )
type = kShort;
else if( sizeof(bool) == sizeof(Int_t) )
type = kInt;
}
else if( (sizeof(long) == sizeof(Long_t) && ntype == "long") ||
(sizeof(long long) == sizeof(Long_t) && ntype == "long long") ||
(sizeof(int) == sizeof(Long_t) && ntype == "int") )
type = kLong;
else if( (sizeof(unsigned long) == sizeof(ULong_t) && ntype == "unsigned long") ||
(sizeof(unsigned long long) == sizeof(ULong_t) && ntype == "unsigned long long") ||
(sizeof(unsigned int) == sizeof(ULong_t) && ntype == "unsigned int") )
type = kULong;
else if( (sizeof(short) == sizeof(Short_t) && ntype == "short") )
type = kShort;
else if( (sizeof(unsigned short) == sizeof(UShort_t) && ntype == "unsigned short") )
type = kUShort;
else if( ntype == "char" )
type = kChar;
else if( ntype == "unsigned char" )
type = kUChar;
if( type == kVarTypeEnd ) {
Warning( errloc, "Unsupported return type \"%s\" for function %s. "
"Variable %s not defined.", ntype.c_str(), s[ndot].Data(), name.Data() );
delete theMethod;
return 0;
}
if( (rtype == TMethodCall::kDouble && type != kDouble && type != kFloat) ||
(rtype == TMethodCall::kLong && (type == kDouble || type == kFloat)) ) {
// Someone lied to us
Warning( errloc, "Ill-formed TMethodCall returned from ROOT for function %s, "
"variable %s. Call expert.", s[ndot].Data(), name.Data() );
delete theMethod;
return 0;
}
#endif
if( !objrtti.IsObjVector() )
var = new THaVar( name, desc, (void*)loc, type, ((ndot==2) ? 0 : -1),
theMethod );
else {
assert( ndot == 1 );
VarType otype = objrtti.GetType();
assert( otype == kObjectV || otype == kObjectPV );
Int_t sz = (otype == kObjectV) ? objrtti.GetClass()->Size() : 0;
var = new THaVar( name, desc, (void*)loc, type, sz, 0, theMethod );
}
if( var && !var->IsZombie() )
AddLast( var );
else {
Warning( errloc, "Error creating variable %s", name.Data() );
delete theMethod;
delete var;
return 0;
}
} // if( !function ) else
return var;
}
//-----------------------------------------------------------------------------
Int_t THaVarList::DefineVariables( const VarDef* list, const char* prefix,
const char* caller )
{
// Add all variables specified in 'list' to the list. 'list' is a C-style
// structure defined in VarDef.h and must be terminated with a NULL name.
//
// Allows definition of:
// scalers
// fixed size arrays
// variable size arrays
// fixed size arrays of pointers
// variable size arrays of pointers
// Data can be anywhere in memory, typically in member variables.
// Data type must be specified explicitly. To use the ROOT RTTI features
// to determine data types (and arrays sizes etc.) automatically, or
// to access data in member ROOT objects, use the second form of this
// method.
//
// The names of all newly created variables will be prefixed with 'prefix',
// if given. Error messages will include 'caller', if given.
// Output a warning if a name already exists. Return values >0 indicate the
// number of variables defined, <0 indicate errors (no variables defined).
TString errloc("DefineVariables [called from ");
if( !caller || !*caller ) {
caller = "(global scope)";
}
errloc.Append(caller);
errloc.Append("]");
if( !list ) {
Error(errloc, "Empty input list. No variables registered.");
return -1;
}
const VarDef* item = list;
Int_t ndef = 0;
size_t nlen = 64;
char* name = new char[nlen];
while( item->name ) {
const char* description = item->desc;
if( !description || !*description )
description = item->name;
size_t len = strlen( item->name ), slen = 0, plen = 0;
// size>1 means 1-d array
if( item->size>1 ) {
slen = static_cast<size_t>( TMath::Log10( item->size )) + 3;
}
if( prefix ) {
plen = strlen( prefix );
}
// Resize name string buffer if necessay
if( plen+len+slen+1 > nlen ) {
delete [] name;
name = new char[plen+len+slen+1];
nlen = plen+len+slen+1;
}
// Assemble the name string
if( plen )
strcpy( name, prefix );
strcpy( name+plen, item->name );
if( slen ) {
sprintf( name+plen+len, "[%d]", item->size );
}
// Define this variable, using the indicated type
THaVar* var = DefineByType( name, description, item->loc,
static_cast<VarType>( item->type ),
item->count, errloc );
if( var )
ndef++;
item++;
}
delete [] name;
return ndef;
}
//-----------------------------------------------------------------------------
Int_t THaVarList::DefineVariables( const RVarDef* list, const TObject* obj,
const char* prefix, const char* caller,
const char* var_prefix )
{
// Add all variables specified in 'list' to the list. 'list' is a C-style
// structure defined in VarDef.h and must be terminated with a NULL name.
//
// Allows definition of:
// scalers
// fixed size arrays
// variable size arrays
//
// Data must be persistent ROOT data members or accessible via
// member functions. They can be member variables, member variables
// of member ROOT objects, or member variables of ROOT objects
// contained in member ROOT containers derived from TSeqCollection.
//
// The names of all newly created variables will be prefixed with 'prefix',
// if given. Error messages will include 'caller', if given.
// Output a warning if a name already exists. Return values >0 indicate the
// number of variables defined, <0 indicate errors (no variables defined).
TString errloc("DefineVariables [called from ");
if( !caller || !*caller) {
caller = "(global scope)";
}
errloc.Append(caller);
errloc.Append("]");
if( !list ) {
Error(errloc, "No input list. No variables registered.");
return -1;
}
if( !obj ) {
Error(errloc, "No base object. No variables registered.");
return -2;
}
// Get the class of the base object
TClass* cl = obj->IsA();
if( !cl ) {
//Oops
Error( errloc, "Base object has no class?!? No variables registered.");
return -3;
}
const RVarDef* item;
Int_t ndef = 0;
while( (item = list++) && item->name ) {
// Assemble the name and description strings
TString name( item->name );
if( name.IsNull() )
continue;
if( prefix )
name.Prepend(prefix);
TString desc( (item->desc && *item->desc ) ? item->desc : name.Data() );
// Process the variable definition
char* c = ::Compress( item->def );
TString def( c );
delete [] c;
if( def.IsNull() ) {
Warning( errloc, "Invalid definition for variable %s (%s). "
"Variable not defined.", name.Data(), desc.Data() );
continue;
}
if( var_prefix && *var_prefix )
def.Prepend( var_prefix );
THaVar* var = DefineByRTTI( name, desc, def, obj, cl, errloc );
if( var )
ndef++;
}
return ndef;
}
//_____________________________________________________________________________
THaVar* THaVarList::Find( const char* name ) const
{
// Find a variable in the list. If 'name' has array syntax ("var[3]"),
// the search is performed for the array basename ("var").
TObject* ptr;
const char* p = strchr( name, '[' );
if( !p )
ptr = FindObject( name );
else {
size_t n = p-name;
char* basename = new char[ n+1 ];
strncpy( basename, name, n );
basename[n] = '\0';
ptr = FindObject( basename );
delete [] basename;
}
return static_cast<THaVar*>(ptr);
}
//_____________________________________________________________________________
void THaVarList::PrintFull( Option_t* option ) const
{
// Print variable names, descriptions, and data.
// Identical to Print() except that variable data is also printed.
// Supports selection of subsets of variables via 'option'.
// E.g.: option="var*" prints only variables whose names start with "var".
if( !option ) option = "";
TRegexp re(option,kTRUE);
TIter next(this);
while( TObject* obj = next() ) {
if( *option ) {
TString s = obj->GetName();
if( s.Index(re) == kNPOS )
continue;
}
obj->Print("FULL");
}
}
//_____________________________________________________________________________
Int_t THaVarList::RemoveName( const char* name )
{
// Delete the variable with the given name from the list.
// Returns 1 if variable was deleted, 0 is it wasn't in the list.
//
// Note: This differs from TList::Remove(), which doesn't delete the
// element itself.
TObject* ptr = Find( name );
if( !ptr )
return 0;
ptr = Remove( ptr );
delete ptr;
return 1;
}
//_____________________________________________________________________________
Int_t THaVarList::RemoveRegexp( const char* expr, Bool_t wildcard )
{
// Delete all variables matching regular expression 'expr'. If 'wildcard'
// is true, the more user-friendly wildcard format is used (see TRegexp).
// Returns number of variables removed, or <0 if error.
TRegexp re( expr, wildcard );
if( re.Status() ) return -1;
Int_t ndel = 0;
TIter next( this );
while( TObject* ptr = next() ) {
TString name = ptr->GetName();
if( name.Index( re ) != kNPOS ) {
ptr = Remove( ptr );
delete ptr;
ndel++;
}
}
return ndel;
}