-
Notifications
You must be signed in to change notification settings - Fork 12
/
SteerParser.cxx
1083 lines (949 loc) · 35.5 KB
/
SteerParser.cxx
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
*
* @Package: Plotter
*
* Class to parse steering files in C++. Classes have to be enclosed
* in curly brackets and start with <classname>. Example:
* SteerExample(){ .... };
* Steering blocks can also be named, e.g. SteerExample("default") and
* SteerExample("modify").
* The different versions of the class can then be obtained with
* GetSteer(TClass* cl) or GetSteer(TClass* cl, const char* name).
*
*******************************************************************/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <fstream>
#include <TClass.h>
#include <TROOT.h>
#include <RVersion.h>
#include <TObjString.h>
#include <TList.h>
#include <TSystem.h>
#include "SteerParser.h"
#include "BaseSteer.h"
using namespace std;
//__________________________________________________________
SteerParser::SteerParser()
: fFilename((Ssiz_t)1024),fNamespace((Ssiz_t)256),fClassname((Ssiz_t)1024), fObjectname((Ssiz_t)1024), fBlock((Ssiz_t)1024), fLine((Ssiz_t)1024),fInNamespace(kFALSE),fInBlock(kFALSE),fBraces(0),fArrayLength(0),fNameSpaces(0),fCurrentNameIndex(-1)
{
// constructor
}
//__________________________________________________________
SteerParser::~SteerParser()
{
// Deletes the list of steerings
fNameSpaces.Delete();
fSteerings.Delete();
}
//__________________________________________________________
void SteerParser::SetFilename(const char* filename)
{
// Searches for the file 'filename' in $PWD
char *tmpFileName = gSystem->ExpandPathName("$(PWD)/");
fFilename = tmpFileName;
delete tmpFileName;
if(fFilename !="/") {
fFilename.Append(filename);
cout <<"SteerParser: Searching for file "<< fFilename<<endl;
ifstream steer1(fFilename,ios::in);
if(!steer1.fail()) {
cout <<"SteerParser: File "<< fFilename<<" opened for reading"<<endl;
return;
}
}
cout <<"SteerParser: Could not find "<< filename
<<" . "<< endl;
return;
}
//__________________________________________________________
void SteerParser::SetClassname(const char* classname, const char* objectname)
{
//Set class name with optional object name argument
fClassname=classname;
fObjectname=objectname;
if(fObjectname=="")
cout <<"SteerParser: Classname set to "<< fClassname<<endl;
else
cout <<"SteerParser: Classname set to " << fClassname
<< "(\"" << fObjectname <<"\")" << endl;
}
//__________________________________________________________
Bool_t SteerParser::ParseFile(const char* filename)
{
// This method divides the steering file into blocks
// and creates BaseSteer objects for each block found.
// If no prior namespace sets the current namespace to the first found.
SetFilename( filename);
Bool_t status = kTRUE;
cout << "\n======== SteerParser: Reading from file '"<<fFilename<<"' ============="<<endl;
ifstream steer(fFilename,ios::in); // open steering file
if ( steer.is_open() ) {
TString line;
const Int_t numSteerBefore = fSteerings.GetEntriesFast();
while ( ! steer.eof() ) {
line.ReadLine( steer );
if ( (line.Length() == 0) || (line.BeginsWith("//")) ) continue;
RemoveComments(line);
RemoveWhiteSpace(line);
cout << "----- "<< line << endl; // we want to have that in log files
ParseLine(line);
}
if(fInNamespace){
Warning("ParseFile","missing closing brace of namespace %s!", fNamespace.Data());
}
Print(fFilename, numSteerBefore, fSteerings.GetLast()); // print summary at the end
} else { // steering file not open
Warning("ParseFile","Could not open steering file %s!\nWill use defaults!",filename);
status = kFALSE;
}
//cout << "======== SteerParser: Done reading from file '"<<fFilename<<"' ========\n"<<endl;
if(fCurrentNameIndex == -1) fCurrentNameIndex = (fNameSpaces.GetEntriesFast() ? 0 : -1);
return status;
}
//__________________________________________________________
Float_t SteerParser::ReadFloat(const char* keyword)
{
// Get value as a float
Int_t index;
Float_t val=0;
if(FindArrayInFile(keyword)) {
// Statement found
if ( ( index=fLine.Index(",") ) >= 0 ) {
TString value=fLine(0,index);// 0=beginning, index=length (not end!)
//convert value to a float
sscanf(value,"%f",&val);
// cout <<"keyword found " <<keyword<<" = "<<val<<endl;
return val;
}
}
return val;
}
//__________________________________________________________
Int_t SteerParser::ReadInt(const char* keyword)
{
// Get value as an int
Int_t index;
Int_t val=0;
if(FindArrayInFile(keyword)) {
// Statement found
if ( ( index=fLine.Index(",") ) >= 0 ) {
TString value=fLine(0,index);// 0=beginning, index=length (not end!)
//convert value to an int
sscanf(value,"%i",&val);
// cout <<"keyword found " <<keyword<<" = "<<val<<endl;
return val;
}
}
return val;
}
//__________________________________________________________
TString SteerParser::ReadString(const char* keyword)
{
// Get value as a string
Int_t index;
TString value("");
if(FindArrayInFile(keyword)) {
// Statement found
if ( ( index=fLine.Index(",") ) >= 0 ) {
value=fLine(0,index);// 0=beginning, index=length (not end!)
// cout <<"keyword found " <<keyword<<" = "<<value<<endl;
return value;
}
}
return value;
}
//__________________________________________________________
Int_t SteerParser::ReadArray(const char* keywordin,Float_t array[])
{
// Split line up into a list of floats and store results in array
TString value("");
Float_t val;
Int_t indarray=0;
Int_t index=0;
if(FindArrayInFile(keywordin)) {
// Statement found
while( fLine.Length()>0){
if ( ( index=fLine.Index(",") ) >= 0 ) {
value=fLine(0,index);// 0=beginning, index=length (not end!)
fLine = fLine(index+1,fLine.Length()-index-1); //reduce line
//convert value to a float
sscanf(value,"%f",&val);
array[indarray]=val;
if(indarray<fArrayLength)array[indarray]=val;
else if(fArrayLength>0) {
cout<<"SteerParser: Mismatch in declared and actual number of elements and for Array" <<keywordin<< endl;
cout<<"SteerParser: Array index=" <<indarray<<
", Array size=" <<fArrayLength<< endl;
}
// cout <<"found value" <<val<<endl;
indarray++;
}
}
}
if(indarray>0&&indarray<fArrayLength&&fArrayLength>0){
cout<<"SteerParser: Mismatch in declared and actual number of elements and for Array" <<keywordin<< endl;
cout<<"SteerParser: Array index=" <<indarray<<
", Array size" <<fArrayLength<< endl;
}
return fArrayLength;
}
//__________________________________________________________
Int_t SteerParser::ReadArray(const char* keywordin,Int_t array[])
{
// Split line up into a list of ints and store results in array
TString value("");
Int_t val;
Int_t indarray=0;
Int_t index=0;
if(FindArrayInFile(keywordin)) {
// Statement found
while( fLine.Length()>0){
if ( ( index=fLine.Index(",") ) >= 0 ) {
value=fLine(0,index);// 0=beginning, index=length (not end!)
fLine = fLine(index+1,fLine.Length()-index-1); //reduce line
//convert value to a float
sscanf(value,"%i",&val);
array[indarray]=val;
if(indarray<fArrayLength)array[indarray]=val;
else if(fArrayLength>0) {
cout<<"SteerParser: Mismatch in declared and actual number of elements and for Array" <<keywordin<< endl;
cout<<"SteerParser: Array index=" <<indarray<<
", Array size" <<fArrayLength<< endl;
}
// cout <<"found value" <<val<<endl;
indarray++;
}
}
}
if(indarray>0&&indarray<fArrayLength&&fArrayLength>0) {
cout<<"SteerParser: Mismatch in declared and actual number of elements and for Array" <<keywordin<< endl;
cout<<"SteerParser: Array index=" <<indarray<<
", Array size" <<fArrayLength<< endl;
}
return fArrayLength;
}
//__________________________________________________________
Int_t SteerParser::ReadArray(const char* keywordin, TObjArray& array, Bool_t checkNamespace)
{
// Split line up into a list of TObjStrings and store results in array
TString value("");
TObjArray val;
Int_t indarray=0;
Int_t index=0;
if(FindArrayInFile(keywordin, checkNamespace)) {
// Statement found
while( fLine.Length()>0){
if ( ( index=fLine.Index(",") ) >= 0 ) {
value=fLine(0,index);// 0=beginning, index=length (not end!)
fLine = fLine(index+1,fLine.Length()-index-1); //reduce line
value.ReplaceAll("};", "");
value.ReplaceAll("\"", "");
value.ReplaceAll(";", "");
val.AddLast(new TObjString(value));
indarray++;
}
}
}
array.Clear();
array.AddAll(&val);
if(indarray>0&&indarray<fArrayLength&&fArrayLength>0){
cout<<"SteerParser: Mismatch in declared and actual number of elements and for Array " <<keywordin<< endl;
cout<<"SteerParser: Array index=" <<indarray<<
", Array size" <<fArrayLength<< endl;
}
return fArrayLength;
}
//__________________________________________________________
void SteerParser::ReadFastArray(const char* keyword,Float_t array[])
{
// Put the array in the file into the array array.
TString Keyword(keyword);
fBraces=0;
Bool_t classfound=kFALSE;
if(fFilename=="") {
cout << "SteerParser: You must specify a file first with SetFilename()"<<endl;
return;
}
ifstream steer(fFilename,ios::in); // open steering file
if ( steer.is_open() ) {
while ( ! steer.eof() ) {
fLine.ReadLine( steer );
if ( (fLine.Length() == 0) || (fLine.BeginsWith("//")) ) continue;
RemoveComments(fLine);
RemoveWhiteSpace(fLine);
//Look for class heading
if(FindClassnameInLine(fLine)) {
// cout <<"SteerParser: Found class "<< fClassname <<endl;
classfound=kTRUE;
}
if(classfound) {
//Count braces to check we are still in class description
CountBraces(fLine);
if(FindKeywordInLine(fLine,Keyword)) {
// now know the array size, read in that many floats from the file.
Int_t counter = 0;
Float_t tmp = 0;
while(( counter != fArrayLength ) && ! steer.eof()){
steer >> tmp;
array[counter] = tmp;
counter++;
}
// We have read all the information so return
return;
}
// Keep these error messages
if(fBraces==0&&classfound) {
cout<<"SteerParser: Could not find variable "<<
keyword <<" in class "<<fClassname<<endl;
return;
}
}
}
if(!classfound) {
cout<<"SteerParser: Could not find class "<<fClassname
<<" with object "<<fObjectname<<endl;
return;
}
} else { // steering file not open
cout << "SteerParser: Could not open steering file "<<fFilename <<endl;
return;
}
cout <<"SteerParser:Could not find keyword "<<
keyword<< " in file " <<fFilename<<endl;
if(fBraces !=0) cout<<"SteerParser: Braces in file "<<
fFilename <<" don't match "<<fBraces<<endl;
return;
return;
}
//__________________________________________________________
void SteerParser::ReadFastArray(const char* keyword,Int_t array[])
{
TString Keyword(keyword);
fBraces=0;
Bool_t classfound=kFALSE;
if(fFilename=="") {
cout << "SteerParser: You must specify a file first with SetFilename()"<<endl;
return;
}
ifstream steer(fFilename,ios::in); // open steering file
if ( steer.is_open() ) {
while ( ! steer.eof() ) {
fLine.ReadLine( steer );
if ( (fLine.Length() == 0) || (fLine.BeginsWith("//")) ) continue;
RemoveComments(fLine);
RemoveWhiteSpace(fLine);
//Look for class heading
if(FindClassnameInLine(fLine)) {
// cout <<"SteerParser: Found class "<< fClassname <<endl;
classfound=kTRUE;
}
if(classfound) {
//Count braces to check we are still in class description
CountBraces(fLine);
if(FindKeywordInLine(fLine,Keyword)) {
// now know the array size, read in that many ints from the file.
Int_t counter = 0;
Int_t tmp = 0;
while(( counter != fArrayLength ) && ! steer.eof()){
steer >> tmp;
array[counter] = tmp;
counter++;
}
// We have read all the information so return
return;
}
// Keep these error messages
if(fBraces==0&&classfound) {
cout<<"SteerParser: Could not find variable "<<
keyword <<" in class "<<fClassname<<endl;
return;
}
}
}
if(!classfound) {
cout<<"SteerParser: Could not find class "<<fClassname
<<" with object "<<fObjectname<<endl;
return;
}
} else { // steering file not open
cout << "SteerParser: Could not open steering file "<<fFilename <<endl;
return;
}
cout <<"SteerParser:Could not find keyword "<<
keyword<< " in file " <<fFilename<<endl;
if(fBraces !=0) cout<<"SteerParser: Braces in file "<<
fFilename <<" don't match "<<fBraces<<endl;
return;
return;
}
//__________________________________________________________
Bool_t SteerParser::FindArrayInFile(const char* keywordin, Bool_t checkNamespace)
{
//
// Finds an array or single value in the steering file and stores numbers/strings
// separated by commas in fLine
//
// If(checkNamespace) only takes into account lines from the namespace 'fNamespace'
TString keyword(keywordin);
fBraces=0;
if(fFilename=="") {
Warning("FindArrayInFile","You must specify a file first with SetFilename()");
return kFALSE;
}
// cout << "\n======== SteerParser: Reading from file '"<<fFilename<<"' ============="<<endl;
ifstream steer(fFilename,ios::in); // open steering file
if ( steer.is_open() ) {
Bool_t classfound=kFALSE;
TList lines;
while ( ! steer.eof() ) {
TObjString *newLine = new TObjString;
newLine->String().ReadLine( steer );
lines.Add(newLine);
}
if(checkNamespace && !fNamespace.IsNull()) this->SelectNamespace(lines, fNamespace);
TIter nextLine(&lines);
while(TObject* line = nextLine()){
fLine = line->GetName(); // content of string
if ( (fLine.Length() == 0) || (fLine.BeginsWith("//")) ) continue;
RemoveComments(fLine);
RemoveWhiteSpace(fLine);
//Look for class heading
if(FindClassnameInLine(fLine)) {
classfound=kTRUE;
}
if(classfound) { // true for all lines after class has been found
//Count braces to check we are still in class description
CountBraces(fLine);
if(FindKeywordInLine(fLine,keyword)) {
//If line does not end in ';' Keep reading till we find it
while( ! LineEnd(fLine) ){
TObject* newNextLine = nextLine();
if(!newNextLine) break;
TString linetmp = newNextLine->GetName();
if ( (linetmp.Length() == 0)
|| (linetmp.BeginsWith("//")) ) continue;
RemoveComments(linetmp);
RemoveWhiteSpace(linetmp);
CountBraces(fLine);
fLine +=linetmp;
}
//line should now contain a list of strings
fLine +=","; //add comma to end of line to simplify string separations
// We have read all the information so return
return kTRUE;
}
// if block is closed and keyword not found print out an error
// second condition is needed when "SteerClass()" is on a line by itself
// if the steering has no name, line is just ')', else it's empty
if(fBraces==0 &&
! ( fLine.CompareTo(")")==0 || fLine.CompareTo("")==0 ) ) {
Warning("FindArrayInFile","Could not find variable %s in class %s",
keywordin,fClassname.Data());
return kFALSE;
}
}
}
lines.Delete();
if(!classfound) {
Warning("FindArrayInFile","Could not find class %s with object %s",
fClassname.Data(),fObjectname.Data());
return kFALSE;
}
} else { // steering file not open
Warning("FindArrayInFile","Could not open steering file %s",fFilename.Data());
return kFALSE;
}
Warning("FindArrayInFile","Could not find keyword %s in file %s",keywordin,fFilename.Data());
if(fBraces !=0)
Warning("FindArrayInFile","Braces in file %s don't match: %d",fFilename.Data(),fBraces);
return kFALSE;
}
//__________________________________________________________
Bool_t SteerParser::FindKeywordInLine(TString& line, const TString& keyword)
{
//
// Searches for a keyword in the line and returns the remainder of the line
// Returns false if line does not contain keyword
//
Int_t index;
TString keywordtest;
fArrayLength=0;
// if ( ! fInBlock ) { // we're not yet inside a block (enclosed in {})
if ( ( index=line.Index("=") ) >= 0 ) { // found a keyword = statement
// check charecters before "=" sign for compatibility with keyword
keywordtest = line(0,index);
if(keywordtest==keyword) {
line = line(index+1,line.Length()-index-1);
return kTRUE;
}
Int_t index1=line.Index("[");
Int_t index2=line.Index("]");
// perhaps we have an array with declared # elements (eg arr[99]= ...)
if ( index1>= 0 && index2>index1 ) {
// we have found the opening and closing braces
keywordtest = line(0,index1);
if(keywordtest==keyword) {
//Now read the string between the braces and convert into
// an integer which denotes the size of the array
TString arraylength=line(index1+1,index2-index1-1);
line = line(index+1,line.Length()-index-1);
//COnvert string to integer
if(arraylength.Length()>0) sscanf(arraylength,"%i",&fArrayLength);
else fArrayLength=0;
// cout <<"SteerParser: Array "<<keyword<<" found ";
// if(fArrayLength>0) cout << " with length " <<fArrayLength <<endl;
// else cout << " with unspecified length "<<endl;
return kTRUE;
}
}
}
return kFALSE;
}
//__________________________________________________________
Bool_t SteerParser::FindClassnameInLine(TString& line) const
{
//
// Searches line for a classname and object name (if specified) to match
// those given in fClassname and fObjectname
// class and object names are stripped so that line afterwards contains the
// remainder from ')' to the end
// Returns false if line does not contain classname and object name
//
Int_t index;
TString keywordtest;
if ( ( index=line.Index("(") ) >= 0 ) { // found a keyword ( statement
// check characters before "(" sign for compatibility with classname
keywordtest = line(0,index);
if(keywordtest==fClassname) {
line = line(index+1,line.Length()-index-1);
//no Object name specified so block is already found
if(fObjectname=="") return kTRUE;
//Now check Object name
else if ( line.Length()>2&&( index=line.Index(")") ) >= 0 ) {
// found a keyword ) statement
// Do not compare 1st +last elements as these contain " characters
keywordtest = line(1,index-2);
if(keywordtest==fObjectname) {
line = line(index+1,line.Length()-index-1);
return kTRUE;
}
}
}
}
return kFALSE;
}
//__________________________________________________________
Bool_t SteerParser::LineEnd(const TString& line) const
{
// true if line ends in a ';' symbol
return line.Index(";") >= 0 ? kTRUE : kFALSE;
}
//__________________________________________________________
TString SteerParser::ValueOfLine(const TString& line) const
{
//
// Copies the contents of a string until it reaches the ';' character
// This is usually the value of the parameter or for arrays a set
// of values separated by commas
//
Int_t index;
TString value("");
if ( ( index=line.Index(";") ) >= 0 ) { // Look for ';'
value = line(0,index);// 0=beginning, index=length (not end!)
}
else {
cout <<"SteerParser::ValueOfLine: line does not end with ;" << endl;
cout <<" Check Steering file " <<endl;
}
return value;
}
//__________________________________________________________
void SteerParser::ParseLine(TString& line)
{
// Splits lines into components, calls different methods
// according to the internal state
Int_t index;
// First try to find a namespace
if( ! fInNamespace ) {
if(line.BeginsWith("namespace")){
index = line.Index("{");
if(index == kNPOS) index = line.Length(); // '{' in next line, take care below
fNamespace = line(9, index - 9); // 9=beginning, index-9=length
if(fNamespace.Length()) {
if(!fNameSpaces.FindObject(fNamespace))
fNameSpaces.Add(new TObjString(fNamespace));
} else {
Warning("ParseLine", "Expect a name between 'namespace' and '{', use global!");
}
fInNamespace = kTRUE;
line = line(index+1,line.Length()-index-1); // rest after '{'
if ( line.Length() == 0 ) return; // line doesn't contain other parts else go on
}
} else if( ! fInBlock && line[0] == '}'){ // ending namespace
fNamespace = "";
fInNamespace = kFALSE;
if(line.Length() != 1) ParseLine(line.Remove(0, 1)); // parse the rest after '}'
return;
}
TString expression;
// now we can deal with the steerings
if ( ! fInBlock ) { // we're not yet inside a block (enclosed in {})
if(fInNamespace && line[0] == '{') { // assuming starting of namespace
// might be wrong, but than it is a wrong '{' anyway!
if(line.Length() == 1) return; // line doesn't contain other parts
else line.Remove(0, 1); // remove '{' and go on
}
if ( ( index=line.Index("(") ) >= 0 ) { // classname
fClassname = line(0,index); // 0=beginning, index=length (not end!)
line = line(index+1,line.Length()-index-1); // rest after '('
if ( line.Length() > 0 ) ParseLine(line); // line might contain other parts
} else if ( ( index=line.Index(")") ) >= 0 ) { // beginning of block
fInBlock = kTRUE;
if (index == 0){ // unnamed steering
line = line(index+1,line.Length()-index-1); // rest after ')'
if ( line.Length() > 0 ) ParseLine(line); // line might contain other parts
} else {
if (line[0]=='"' && line[index-1]=='"'){ // name in double ticks
fObjectname = line(1,index-2); // extract name
} else {
Warning("ParseLine",
"Expected a name like '%s' in quotation marks! Don't use a name...",
line(1,index-1).Data());
}
line = line(index+1,line.Length()-index-1); // rest after ')'
if ( line.Length() > 0 ) ParseLine(line); // line might contain other parts
}
} else {
Warning("ParseLine","Line '%s' makes no sense here! Skipping...",line.Data());
}
} else { // we're inside a block
Int_t indOpen = line.Index("{");
Int_t indClose = line.Index("}");
Int_t indSemi = line.Index(";");
if ( indOpen >= 0 && (indClose == kNPOS || indOpen < indClose)
&& (indSemi == kNPOS || indOpen < indSemi)) { // starting '{'
index = indOpen;
line = line(index+1,line.Length()-index-1); // rest after '{'
if ( line.Length() > 0 ) ParseLine(line); // line might contain other parts
} else if ( indSemi >= 0 && (indClose == kNPOS || indSemi < indClose)) { // expression
index = indSemi;
expression = line(0,index+1);
fBlock.Append(expression); // add expression to block
line = line(index+1,line.Length()-index-1); // rest after ';'
if ( line.Length() > 0 ) ParseLine(line); // line might contain other parts
} else if ( indClose >= 0 ) { // end of block
index = indClose;
CreateSteering(fClassname,fObjectname,fBlock,fNamespace);
fInBlock = kFALSE;
fBlock="";
fClassname="";
fObjectname="";
if(index < line.Length()-1){
line = line(index+1,line.Length()-index-1); // rest after '}'
ParseLine(line);
}
} else { // assume that expression is continued on next line
fBlock.Append(line); // add entire line to block
}
}
}
//__________________________________________________________
Bool_t SteerParser::CreateSteering(TString& theclassname, TString& objectname, TString& block, const char* nameSpace)
{
// Gets called by ParseLine when the closing brace of a block is found
// or might be directly called by a user
//
// First parameter is the classname of a concrete steering class
// e.g. BaseSteerCreateScatElec
//
// Second parameter is the name of the steering object
//
// Third parameter is a block of expressions to be evaluated by the
// concrete steering class
// e.g. 'value1=0.8; value2=4711; value3=42.0;' and so on...
//
// Optional fourth parameter is a namespace this steering should belong to.
// (Looking for namespace might cause trouble for very long strings IF this method
// was called 'standalone', because in the long string work around in BaseSteer::SetValues
// it is assumed that the correct classname etc. are in the corresponding data members
// of SteerParser.)
TClass *myclass;
RemoveWhiteSpace(theclassname);
RemoveWhiteSpace(objectname);
RemoveWhiteSpace(block);
myclass = gROOT->GetClass(theclassname);
cout<<objectname<<endl;
cout<<nameSpace<<endl;
if ( myclass ) {
BaseSteer *mysteer = static_cast<BaseSteer*>( myclass->New() );
mysteer->SetValues(block);
mysteer->SetName(objectname);
if(nameSpace) mysteer->SetTitle(nameSpace);
fSteerings.AddLast( mysteer );
return kTRUE;
} else {
Warning("CreateSteering","Cannot find steering class '%s'! Skipping...",
theclassname.Data() );
return kFALSE;
}
}
//__________________________________________________________
BaseSteer* SteerParser::GetSteer(TClass* cl)
{
// If a steering corresponding to class 'cl' and without a name
// is in the list of steerings, it is simply returned.
// But if namespaces are defined, a steering from that namespace is preferred
// to a steering without namespace, which is the 'fall back' solution.
//
// If no corresponding steering object exists, a default one is created
// and appended to the list of steerings. Default values are printed to
// screen.
BaseSteer* noNameSpaceSteer = NULL;
TIter next(&fSteerings);
while ( BaseSteer* steer = (BaseSteer*)next() ) {
if ( steer->IsA() == cl && strlen(steer->GetName()) == 0 ){// GetName never is NULL!
if(fCurrentNameIndex == -1){ // no namespace required
return steer;
} else {
const char* nameSpace = steer->GetTitle();
if(nameSpace && !strcmp(nameSpace, fNameSpaces[fCurrentNameIndex]->GetName())){
return steer;
} else if(!nameSpace || !strcmp(nameSpace, "")){// no namescpace of steering
noNameSpaceSteer = steer; //if no matching is found, take this without name
}
}
}
}
if(noNameSpaceSteer) return noNameSpaceSteer;
BaseSteer* thesteer = (BaseSteer*)cl->New();
cout << endl
<< "====== SteerParser: Using default values for class '"
<< cl->GetName()
<<"' ======\n";
thesteer->Print();
cout << "====== SteerParser: End of defaults for class '"
<< cl->GetName()
<<"' ===========\n"
<< endl;
fSteerings.AddLast(thesteer);
return thesteer; // return default object if not explicitly given in file
}
//__________________________________________________________
BaseSteer* SteerParser::GetSteer(TClass* cl, const char* name)
{
// If a steering corresponding to class 'cl' is in the list of steerings
// it is returned if the name matches.
// But if namespaces are defined, a steering from that namespace is preferred
// to a steering without namespace, which is the 'fall back' solution.
//
// If no corresponding steering object exists, NULL is returned.
BaseSteer* noNameSpaceSteer = NULL;
TIter next(&fSteerings);
while ( BaseSteer* steer = (BaseSteer*)next() ) {
if ( steer->IsA() == cl && !strcmp(steer->GetName(), name) ){
if(fCurrentNameIndex == -1){ // no namespace required
return steer;
} else {
const char* nameSpace = steer->GetTitle();
if(nameSpace && !strcmp(nameSpace, fNameSpaces[fCurrentNameIndex]->GetName())){
return steer;
} else if(!nameSpace || !strcmp(nameSpace, "")){// no namescpace of steering
noNameSpaceSteer = steer; //if no matching is found, take this without name
}
}
}
}
if(!noNameSpaceSteer)
Error("GetSteer", "No steering type %s with name %s!", cl->GetName(), name);
return noNameSpaceSteer; // maybe NULL
}
//__________________________________________________________
void SteerParser::RemoveComments(TString& line) const
{
// Removes comments from line (everything after '//')
Int_t index;
if ( ( index=line.Index("//") ) >= 0 ) { // line has comment
line = line(0,index);
}
}
//__________________________________________________________
void SteerParser::RemoveWhiteSpace(TString& line) const
{
// Removes all spaces and tabs from line
Int_t index;
while ( ( index=line.Index(" ") ) >= 0 ) { // space at position index
line = line.Remove(index,1);
}
while ( ( index=line.Index("\t") ) >= 0 ) { // tab at position index
line = line.Remove(index,1);
}
}
//__________________________________________________________
void SteerParser::CountBraces(TString & line)
{
//
// Counts and removes curly braces from line
// fBraces is the running total of open - close braces
// so fBraces=0 is a complete block
Int_t index;
while ( ( index=line.Index("{") ) >= 0 ) { // space at position index
line = line.Remove(index,1);
fBraces++;
}
while ( ( index=line.Index("}") ) >= 0 ) { // space at position index
line = line.Remove(index,1);
fBraces--;
}
}
//__________________________________________________________
Int_t SteerParser::GetNumSteerings(TClass* theclass)
{
// Returns the number of accepted steerings of type theclass
// (the method without argument returns the number of all accepted steerings)
Int_t i=0, num=0, max=fSteerings.GetEntriesFast();
for (i=0; i<max; ++i ){
if (fSteerings.UncheckedAt(i)->IsA() == theclass ) ++num;
}
return num;
}
//__________________________________________________________
void SteerParser::Print(Option_t* option) const
{
// Prints the given steering values to stdout, taking 'option' as filename if given
this->Print(option, 0, fSteerings.GetLast());
}
//__________________________________________________________
void SteerParser::Print(Option_t* option, Int_t first, Int_t last) const
{
// Prints the values of steerings 'first' to 'last' to stdout,
// taking 'option' as filename if given
last = TMath::Min(last, fSteerings.GetLast());
cout << "====== SteerParser: Accepted values";
if(option && strlen(option)) cout << " from file '" << option << "'";
cout << " ======" << endl;
if(fNameSpaces.GetEntriesFast() == 0){
for(Int_t i = first; i <= last; ++i) fSteerings[i]->Print();
} else {
for(Int_t i = 0; i < fNameSpaces.GetEntriesFast(); ++i){
cout << "\n ===== namespace '" << fNameSpaces[i]->GetName() << "':" << endl;
for(Int_t j = first; j <= last; ++j){
if(!strcmp(fSteerings[j]->GetTitle(), fNameSpaces[i]->GetName()))
fSteerings[j]->Print();
}
}
cout << "\n ===== without namespace:"<< endl;
for(Int_t j = first; j <= last; ++j){
if(!strcmp(fSteerings[j]->GetTitle(), "")) fSteerings[j]->Print();
}
}
}
//__________________________________________________________
Int_t SteerParser::NextNameSpace()
{
// switches to next 'namespace' of steerings
// 1 if success, 0 if no further 'namespace' (or no 'namespaces' given at all)
if(fCurrentNameIndex == -1) return 0;
if(++fCurrentNameIndex < fNameSpaces.GetEntriesFast()) return 1; // prefix increment!
fCurrentNameIndex = -1;
return 0;
}
//__________________________________________________________
const char* SteerParser::GetCurrentNameSpace() const
{
// giving name of namespace that steering objects currently need to belong to,
// if non required return NULL
if(fCurrentNameIndex == -1) return NULL;
else return fNameSpaces.At(fCurrentNameIndex)->GetName();
}
//__________________________________________________________
Bool_t SteerParser::SetCurrentNameSpace(const char* name)
{
// makes namespace 'name' the current one, kFALSE if no such namespace found
TObject* newNameSpace = fNameSpaces.FindObject(name); // it's a TObjString
if(newNameSpace){
fCurrentNameIndex = fNameSpaces.IndexOf(newNameSpace);
return kTRUE;
}else{
return kFALSE;
}
}
//__________________________________________________________
void SteerParser::SelectNamespace(TList& steerLines, const char* nameSpace) const
{
// SteerLines is assumed to contain TObjStrings containing lines read in from steerig file,
// removes all lines and part of lines that do not belong to the requested namespace.
Bool_t inNamespace = kFALSE;
Int_t nBraces = 0;