-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhdds-mcfast.cpp
1541 lines (1471 loc) · 48.9 KB
/
hdds-mcfast.cpp
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
/*
* hdds-mcfast : an interface utility that reads in a HDDS document
* (Hall D Detector Specification) and writes out a
* geometry description suitable for input to the
* mcfast Monte Carlo detector simulation.
*
* Original version - Richard Jones, May 16 2001.
*
* Notes:
* ------
* 1. The HDDS specification is an xml document, as described by HDDS.dtd.
* 2. Access by hdds-mcfast to the xml source is through the industry-
* standard DOM-1 interface.
* 3. The code has been tested with the xerces-c DOM implementation from
* Apache, and is intended to be used with the xerces-c library.
* 4. Output is sent to standard out through the ordinary c++ i/o library.
* 5. Within the HDDS document are references to mcfast db files that list
* the variables required on each output line. These are looked for
* starting from the current working directory, and are typically given
* as "db/*". Thus hdds-mcfast must generally be invoked after setting
* the current working directory to the one where "db" is located.
* 6. As a by-product of using the DOM parser to access the xml source,
* hdds-mcfast verifies the source against the dtd before translating it.
* Therefore it may also be used as a validator of the xml specification
* (see the -v option).
*/
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/sax/ErrorHandler.hpp>
#include "XString.hpp"
#include "XParsers.hpp"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
XERCES_CPP_NAMESPACE_USE
#define X(XString) XString.unicode_str()
#define S(XString) XString.c_str()
class makeTargetTable
{
public :
int add(DOMElement* const targetEl,
DOMElement* const parentEl = 0);
DOMElement* lookup(const DOMElement* const targetEl,
XString& type, const XString& name);
private :
std::map<const DOMElement*,DOMElement*> fParentElList;
int addElement(DOMElement* const targetEl);
int addComposite(DOMElement* const targetEl);
int addModel(DOMElement* const targetEl);
void set_name(DOMElement* const targetEl);
float set_a(DOMElement* const targetEl);
float set_z(DOMElement* const targetEl);
float set_density(DOMElement* const targetEl);
float set_radlen(DOMElement* const targetEl);
float set_collen(DOMElement* const targetEl);
float set_abslen(DOMElement* const targetEl);
float set_dedx(DOMElement* const targetEl);
void dump(const DOMElement* const targetEl);
void dump(DOMElement* const targetEl);
};
#ifdef BASENAME_IN_LIBGEN
#include <libgen.h>
#elif defined BASENAME_USE_BUILTIN
#undef basename
#define basename _RC_basename
static char* basename(const char *f)
{
const char* base;
for (base = f; *f; f++)
{
if (*f == '/')
base = f+1;
}
return (char *)base;
}
#endif
class modelTableEntry
{
public:
int refcount;
int maxcount;
ostringstream db;
modelTableEntry() : refcount(0), maxcount(0) {};
modelTableEntry(const modelTableEntry& src)
{
refcount = src.refcount;
maxcount = src.maxcount;
db.str(src.db.str());
}
modelTableEntry& operator=(const modelTableEntry& src)
{
refcount = src.refcount;
maxcount = src.maxcount;
db.str(src.db.str());
}
};
std::map<const std::string,modelTableEntry> modelTable;
makeTargetTable targetTable;
void usage()
{
cerr << "\nUsage:\n"
" hdds-mcfast [-v] {HDDS file}\n\n"
"Options:\n"
" -v validate only\n"
<< endl;
}
class DbMaker
{
/* This class contains the utility functions that read the
* MCFast materials and geometry db files and generate the
* output structure based on that information.
*/
public:
static void processTemplateFile(const DOMElement* const targetEl,
const char* const fname);
static void makedb(DOMElement* el);
static void printdb();
};
int main(int argC, char* argV[])
{
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
XString msgS(toCatch.getMessage());
cerr << "hdds-mcfast: Error during initialization! :\n"
<< S(msgS) << endl;
return 1;
}
if (argC < 2)
{
usage();
return 1;
}
else if ((argC == 2) && (strcmp(argV[1], "-?") == 0))
{
usage();
return 2;
}
const char* xmlFile = 0;
bool mcfastOutput = true;
int argInd;
for (argInd = 1; argInd < argC; argInd++)
{
if (argV[argInd][0] != '-')
break;
if (strcmp(argV[argInd],"-v") == 0)
mcfastOutput = false;
else
cerr << "Unknown option '" << argV[argInd]
<< "', ignoring it\n" << endl;
}
if (argInd != argC - 1)
{
usage();
return 1;
}
xmlFile = argV[argInd];
#if defined OLD_STYLE_XERCES_PARSER
DOMDocument* doc = parseInputDocument(xmlFile);
#else
DOMDocument* doc = buildDOMDocument(xmlFile,false);
#endif
if (doc == 0)
{
cerr << "hdds-geant : Error parsing HDDS document, "
<< "cannot continue" << endl;
return 1;
}
if (! mcfastOutput)
{
return 0;
}
modelTableEntry entry;
entry.refcount = 1;
entry.maxcount = 1;
entry.db.str("");
entry.db << "database mcfast 0000" << endl;
modelTable["database"] = entry; // header line must come first
entry.refcount = 0;
entry.maxcount = 0;
entry.db.str("");
modelTable["detector"] = entry; // detector is first declaration
entry.refcount = 0;
entry.maxcount = 0;
entry.db.str("");
modelTable["Material"] = entry; // force materials next
entry.refcount = 0;
entry.maxcount = 0;
entry.db.str("");
modelTable["Mixture"] = entry; // then mixtures
DOMElement* rootEl = doc->getDocumentElement();
DbMaker::makedb(rootEl);
entry.refcount = 1;
entry.maxcount = 1;
entry.db.str("");
entry.db << "include db/hitsontrack.db" << endl
<< "make HitsOnTrack 4 0 0" << endl;
modelTable["hitsontrack"] = entry; // last comes histontrack
DbMaker::printdb();
XMLPlatformUtils::Terminate();
return 0;
}
int makeTargetTable::add(DOMElement* const targetEl,
DOMElement* const parentEl)
{
if (targetEl == 0)
{
cerr << "makeTargetTable::add - called with null pointer" << endl;
return 0;
}
std::map<const DOMElement*,DOMElement*>::iterator iter;
iter = fParentElList.find(targetEl);
if (iter == fParentElList.end())
{
fParentElList[targetEl] = parentEl;
XString tagS = targetEl->getTagName();
if (tagS == "mcfast")
{
addModel(targetEl);
}
else if (tagS == "element")
{
addElement(targetEl);
}
else if (tagS == "composite")
{
addComposite(targetEl);
}
else
{
cerr << "makeTargetTable::add - called with unexpected tag "
<< S(tagS) << endl;
return 0;
}
}
}
int makeTargetTable::addElement(DOMElement* const targetEl)
{
set_name(targetEl);
set_a(targetEl);
set_z(targetEl);
XString modelAttS("model");
XString materialS("Material");
targetEl->setAttribute(X(modelAttS),X(materialS));
XString templAttS("template");
XString dbS("db/materials.db");
targetEl->setAttribute(X(templAttS),X(dbS));
DbMaker::processTemplateFile(targetEl,S(dbS));
return 0;
}
int makeTargetTable::addComposite(DOMElement* const targetEl)
{
XString densityS("density");
XString radlenS("radlen");
XString collenS("collen");
XString abslenS("abslen");
XString dedxS("dedx");
XString realS("real");
if (lookup(targetEl,realS,densityS) ||
lookup(targetEl,realS,radlenS) ||
lookup(targetEl,realS,collenS) ||
lookup(targetEl,realS,abslenS) ||
lookup(targetEl,realS,dedxS))
{
set_density(targetEl);
set_radlen(targetEl);
set_abslen(targetEl);
set_collen(targetEl);
set_dedx(targetEl);
return addElement(targetEl);
}
else
{
set_name(targetEl);
set_a(targetEl);
set_z(targetEl);
set_density(targetEl);
}
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
const int matCount = matList->getLength();
char nmatStr[10];
sprintf(nmatStr,"%d",matCount);
XString nmatS(nmatStr);
XString nmatAttS("nmat");
XString intS("int");
DOMElement* nmatEl = targetEl->getOwnerDocument()->createElement(X(intS));
XString nameAttS("name");
XString valueAttS("value");
nmatEl->setAttribute(X(nameAttS),X(nmatAttS));
nmatEl->setAttribute(X(valueAttS),X(nmatS));
targetEl->appendChild(nmatEl);
std::vector<float> fVol;
std::vector<DOMElement*> dataEl;
XString refvecS("reference_vector");
DOMElement* matVecEl = targetEl->getOwnerDocument()->createElement(X(refvecS));
XString matnameS("matnames");
matVecEl->setAttribute(X(nameAttS),X(matnameS));
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refIdS(matEl->getAttribute(X(materialS)));
XString refdataS("reference_data");
dataEl[m] = targetEl->getOwnerDocument()->createElement(X(refdataS));
XString valueAttS("value");
dataEl[m]->setAttribute(X(valueAttS),X(refIdS));
matVecEl->appendChild(dataEl[m]);
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refIdS));
add(refEl);
XString fractionS("fractionmass");
DOMNodeList* specList = matEl->getElementsByTagName(X(fractionS));
if (specList->getLength() == 0)
{
XString nameAttS("name");
XString nameS = matEl->getAttribute(X(nameAttS));
cerr << "makeTargetTable::addComposite - "
<< "composite " << S(nameS) << " is missing information."
<< endl;
exit(2);
}
DOMElement* specEl = (DOMElement*)specList->item(0);
XString fracAttS("fraction");
XString fS(specEl->getAttribute(X(fracAttS)));
float fMass_m = atof(S(fS));
float density_m = set_density(refEl);
float density = set_density(targetEl);
fVol[m] = fMass_m*(density/density_m);
}
targetEl->appendChild(matVecEl);
XString rarrayS("real_array");
DOMElement* fVolEl = targetEl->getOwnerDocument()->createElement(X(rarrayS));
XString propS("prop");
fVolEl->setAttribute(X(nameAttS),X(propS));
char str[20];
sprintf(str,"%f",fVol[0]);
XString fVolS(str);
for (int m=1; m < matCount; m++)
{
sprintf(str," %f",fVol[m]);
XString xS(str);
fVolS += xS;
}
XString valuesAttS("values");
fVolEl->setAttribute(X(valuesAttS),X(fVolS));
targetEl->appendChild(fVolEl);
XString modelAttS("model");
XString templAttS("template");
XString mixtureS("Mixture");
XString dbS("db/mixtures.db");
targetEl->setAttribute(X(modelAttS),X(mixtureS));
targetEl->setAttribute(X(templAttS),X(dbS));
DbMaker::processTemplateFile(targetEl,S(dbS));
return 0;
}
int makeTargetTable::addModel(DOMElement* const targetEl)
{
XString paramAttS("parameters");
XString parS = targetEl->getAttribute(X(paramAttS));
if (parS.size() != 0)
{
DOMElement* parEl = targetEl->getOwnerDocument()->getElementById(X(parS));
XString refS("reference");
DOMNodeList* matList = parEl->getElementsByTagName(X(refS));
int matCount = matList->getLength();
for (int m=0; m < matCount; m++)
{
DOMElement* ptrEl = (DOMElement*)matList->item(m);
XString valueAttrS("value");
XString valueS = ptrEl->getAttribute(X(valueAttrS));
DOMElement* matEl = ptrEl->getOwnerDocument()->getElementById(X(valueS));
add(matEl);
}
XString refdatS("reference_data");
matList = parEl->getElementsByTagName(X(refdatS));
matCount = matList->getLength();
for (int m=0; m < matCount; m++)
{
DOMElement* ptrEl = (DOMElement*)matList->item(m);
XString valueAttrS("value");
XString valueS = ptrEl->getAttribute(X(valueAttrS));
DOMElement* matEl = ptrEl->getOwnerDocument()->getElementById(X(valueS));
add(matEl);
}
}
XString refS("reference");
DOMNodeList* matList = targetEl->getElementsByTagName(X(refS));
int matCount = matList->getLength();
for (int m=0; m < matCount; m++)
{
DOMElement* ptrEl = (DOMElement*)matList->item(m);
XString valueAttrS("value");
XString valueS = ptrEl->getAttribute(X(valueAttrS));
DOMElement* matEl = ptrEl->getOwnerDocument()->getElementById(X(valueS));
add(matEl);
}
XString refdatS("reference_data");
matList = targetEl->getElementsByTagName(X(refdatS));
matCount = matList->getLength();
for (int m=0; m < matCount; m++)
{
DOMElement* ptrEl = (DOMElement*)matList->item(m);
XString valueAttrS("value");
XString valueS = ptrEl->getAttribute(X(valueAttrS));
DOMElement* matEl = ptrEl->getOwnerDocument()->getElementById(X(valueS));
add(matEl);
}
for (DOMNode* var = targetEl->getFirstChild();
var != 0;
var = var->getNextSibling() )
{
if (var->getNodeType() != DOMNode::ELEMENT_NODE)
continue;
DOMElement* varEl = (DOMElement*)var;
XString varS = varEl->getTagName();
if (varS == "mcfast")
{
add(varEl,targetEl);
}
}
XString templAttS("template");
XString templS = targetEl->getAttribute(X(templAttS));
DbMaker::processTemplateFile(targetEl,S(templS));
return 0;
}
void makeTargetTable::set_name(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString stringS("string");
DOMElement* nameEl = lookup(targetEl,stringS,nameAttS);
if (nameEl == 0)
{
XString nameS = targetEl->getAttribute(X(nameAttS));
nameEl = targetEl->getOwnerDocument()->createElement(X(stringS));
nameEl->setAttribute(X(nameAttS),X(nameAttS));
nameEl->setAttribute(X(valueAttS),X(nameS));
targetEl->appendChild(nameEl);
}
}
float makeTargetTable::set_a(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString aAttS("a");
XString aS = targetEl->getAttribute(X(aAttS));
DOMElement* aEl = lookup(targetEl,realS,aAttS);
if (aEl == 0)
{
if (aS.size() == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float a_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float a_m = set_a(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
wgt_m = n*a_m;
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
a_sum += a_m*wgt_m;
wgt_sum += wgt_m;
}
char aStr[20];
sprintf(aStr,"%g",a_sum/wgt_sum);
XString aStrS(aStr);
aS = aStrS;
}
DOMElement* aEl = targetEl->getOwnerDocument()->createElement(X(realS));
aEl->setAttribute(X(nameAttS),X(aAttS));
aEl->setAttribute(X(valueAttS),X(aS));
targetEl->appendChild(aEl);
}
return atof(S(aS));
}
float makeTargetTable::set_z(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString zAttS("z");
XString zS = targetEl->getAttribute(X(zAttS));
DOMElement* zEl = lookup(targetEl,realS,zAttS);
if (zEl == 0)
{
if (zS.size() == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float z_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float z_m = set_z(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
XString materialS("material");
XString refIdS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refIdS));
wgt_m = n*set_a(refEl);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
z_sum += z_m*wgt_m;
wgt_sum += wgt_m;
}
char zStr[20];
sprintf(zStr,"%g",z_sum/wgt_sum);
XString zStrS(zStr);
zS = zStrS;
}
DOMElement* zEl = targetEl->getOwnerDocument()->createElement(X(realS));
zEl->setAttribute(X(nameAttS),X(zAttS));
zEl->setAttribute(X(valueAttS),X(zS));
targetEl->appendChild(zEl);
}
return atof(S(zS));
}
float makeTargetTable::set_density(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString densityS("density");
XString unitAttS("unit");
DOMElement* densityEl = lookup(targetEl,realS,densityS);
if (densityEl == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float ohr_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float rho_m = set_density(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
XString nameS = matEl->getAttribute(X(nameAttS));
cerr << "makeTargetTable::set_density - "
<< "no automatic density calculation for atomic mixture "
<< S(nameS) << endl;
exit(2);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
ohr_sum += wgt_m/rho_m;
wgt_sum += wgt_m;
}
char rhoStr[20];
sprintf(rhoStr,"%g",wgt_sum/ohr_sum);
XString rhoS(rhoStr);
densityEl = targetEl->getOwnerDocument()->createElement(X(realS));
densityEl->setAttribute(X(nameAttS),X(densityS));
densityEl->setAttribute(X(valueAttS),X(rhoS));
XString unitS("g/cm^3");
densityEl->setAttribute(X(unitAttS),X(unitS));
targetEl->appendChild(densityEl);
}
XString resultS = densityEl->getAttribute(X(valueAttS));
XString unitS = densityEl->getAttribute(X(unitAttS));
if (unitS != "g/cm^3")
{
cerr << "makeTargetTable::set_density - "
<< "unsupported density units " << S(unitS)
<< " found in materials specification" << endl;
exit(2);
}
return atof(S(resultS));
}
float makeTargetTable::set_radlen(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString unitAttS("unit");
XString radlenS("radlen");
DOMElement* radlenEl = lookup(targetEl,realS,radlenS);
if (radlenEl == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float adbmal_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float lambda_m = set_radlen(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
wgt_m = n*set_a(refEl);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
adbmal_sum += wgt_m/lambda_m;
wgt_sum += wgt_m;
}
char lambdaStr[20];
sprintf(lambdaStr,"%g",wgt_sum/adbmal_sum);
XString lambdaS(lambdaStr);
radlenEl = targetEl->getOwnerDocument()->createElement(X(realS));
radlenEl->setAttribute(X(nameAttS),X(radlenS));
radlenEl->setAttribute(X(valueAttS),X(lambdaS));
XString unitS("g/cm^2");
radlenEl->setAttribute(X(unitAttS),X(unitS));
targetEl->appendChild(radlenEl);
}
XString resultS = radlenEl->getAttribute(X(valueAttS));
XString unitS = radlenEl->getAttribute(X(unitAttS));
if (unitS != "g/cm^2")
{
cerr << "makeTargetTable::set_radlen - "
<< "unsupported radlen units " << S(unitS)
<< " found in materials specification" << endl;
exit(2);
}
return atof(S(resultS));
}
float makeTargetTable::set_collen(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString collenS("collen");
XString unitAttS("unit");
DOMElement* collenEl = lookup(targetEl,realS,collenS);
if (collenEl == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float adbmal_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float lambda_m = set_collen(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
wgt_m = n*set_a(refEl);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
adbmal_sum += (wgt_m/lambda_m);
wgt_sum += wgt_m;
}
char lambdaStr[20];
sprintf(lambdaStr,"%g",wgt_sum/adbmal_sum);
XString lambdaS(lambdaStr);
collenEl = targetEl->getOwnerDocument()->createElement(X(realS));
collenEl->setAttribute(X(nameAttS),X(collenS));
collenEl->setAttribute(X(valueAttS),X(lambdaS));
XString unitS("g/cm^2");
collenEl->setAttribute(X(unitAttS),X(unitS));
targetEl->appendChild(collenEl);
}
XString resultS = collenEl->getAttribute(X(valueAttS));
XString unitS = collenEl->getAttribute(X(unitAttS));
if (unitS != "g/cm^2")
{
cerr << "makeTargetTable::set_collen - "
<< "unsupported collen units " << S(unitS)
<< " found in materials specification" << endl;
exit(2);
}
return atof(S(resultS));
}
float makeTargetTable::set_abslen(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString abslenS("abslen");
XString unitAttS("unit");
DOMElement* abslenEl = lookup(targetEl,realS,abslenS);
if (abslenEl == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float adbmal_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float lambda_m = set_abslen(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
wgt_m = n*set_a(refEl);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
adbmal_sum += wgt_m/lambda_m;
wgt_sum += wgt_m;
}
char lambdaStr[20];
sprintf(lambdaStr,"%g",wgt_sum/adbmal_sum);
XString lambdaS(lambdaStr);
abslenEl = targetEl->getOwnerDocument()->createElement(X(realS));
abslenEl->setAttribute(X(nameAttS),X(abslenS));
abslenEl->setAttribute(X(valueAttS),X(lambdaS));
XString unitS("g/cm^2");
abslenEl->setAttribute(X(unitAttS),X(unitS));
targetEl->appendChild(abslenEl);
}
XString resultS = abslenEl->getAttribute(X(valueAttS));
XString unitS = abslenEl->getAttribute(X(unitAttS));
if (unitS != "g/cm^2")
{
cerr << "makeTargetTable::set_abslen - "
<< "unsupported abslen units " << S(unitS)
<< " found in materials specification" << endl;
exit(2);
}
return atof(S(resultS));
}
float makeTargetTable::set_dedx(DOMElement* const targetEl)
{
XString nameAttS("name");
XString valueAttS("value");
XString realS("real");
XString dedxS("dedx");
XString unitAttS("unit");
DOMElement* dedxEl = lookup(targetEl,realS,dedxS);
if (dedxEl == 0)
{
XString addmatS("addmaterial");
DOMNodeList* matList = targetEl->getElementsByTagName(X(addmatS));
int matCount = matList->getLength();
float dedx_sum=0;
float wgt_sum=0;
for (int m=0; m < matCount; m++)
{
DOMElement* matEl = (DOMElement*)matList->item(m);
XString materialS("material");
XString refS = matEl->getAttribute(X(materialS));
DOMElement* refEl = matEl->getOwnerDocument()->getElementById(X(refS));
float dedx_m = set_dedx(refEl);
float wgt_m=0;
XString natomsS("natoms");
DOMNodeList* wgtList = matEl->getElementsByTagName(X(natomsS));
if (wgtList->getLength())
{
DOMElement* natomsEl = (DOMElement*)wgtList->item(0);
XString nAttS("n");
XString nS = natomsEl->getAttribute(X(nAttS));
int n = atoi(S(nS));
wgt_m = n*set_a(refEl);
}
XString fractionS("fractionmass");
wgtList = matEl->getElementsByTagName(X(fractionS));
if (wgtList->getLength())
{
DOMElement* fractionEl = (DOMElement*)wgtList->item(0);
XString fractionAttS("fraction");
XString fracS = fractionEl->getAttribute(X(fractionAttS));
wgt_m = atof(S(fracS));
}
dedx_sum += wgt_m*dedx_m;
wgt_sum += wgt_m;
}
char dedxStr[20];
sprintf(dedxStr,"%g",dedx_sum/wgt_sum);
XString dedxStrS(dedxStr);
dedxEl = targetEl->getOwnerDocument()->createElement(X(realS));
dedxEl->setAttribute(X(nameAttS),X(dedxS));
dedxEl->setAttribute(X(valueAttS),X(dedxStrS));
XString unitS("MeV/g/cm^2");
dedxEl->setAttribute(X(unitAttS),X(unitS));
targetEl->appendChild(dedxEl);
}
XString resultS = dedxEl->getAttribute(X(valueAttS));
XString unitS = dedxEl->getAttribute(X(unitAttS));
if (unitS != "MeV/g/cm^2")
{
cerr << "makeTargetTable::set_dedx - "
<< "unsupported dedx units " << S(unitS)
<< " found in materials specification" << endl;
exit(2);
}
return atof(S(resultS));
}
DOMElement* makeTargetTable::lookup(const DOMElement* const targetEl,
XString& type, const XString& name)
{
if (targetEl == 0)
return 0;
XString nameAttS("name");
DOMNodeList* varList = targetEl->getElementsByTagName(X(type));
int varCount = varList->getLength();
for (int v=0; v < varCount; v++)
{
DOMElement* varEl = (DOMElement*)varList->item(v);
XString nameS = varEl->getAttribute(X(nameAttS));
if (nameS == name)
return varEl;
}
XString paramAttS("parameters");
XString paramS = targetEl->getAttribute(X(paramAttS));
if (paramS.size() != 0)
{
DOMElement* paramEl = targetEl->getOwnerDocument()->getElementById(X(paramS));
varList = paramEl->getElementsByTagName(X(type));
varCount = varList->getLength();
for (int v=0; v < varCount; v++)
{
DOMElement* varEl = (DOMElement*)varList->item(v);
XString nameS = varEl->getAttribute(X(nameAttS));
if (nameS == name)
return varEl;
}
}
std::map<const DOMElement*,DOMElement*>::iterator iter;
iter = fParentElList.find(targetEl);
if (iter != fParentElList.end())