-
Notifications
You must be signed in to change notification settings - Fork 4
/
hdds-geant.cpp
1860 lines (1751 loc) · 66.3 KB
/
hdds-geant.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-geant : an interface utility that reads in a HDDS document
* (Hall D Detector Specification) and writes out a
* GEANT-3 geometry description in the form of a
* fortran subroutine.
*
* Revision - Richard Jones, November 25, 2006.
* -added output of optical properties for materials with optical
* properties defined
*
* Revision - Richard Jones, January 25, 2005.
* -added the sphere section as a new supported volume type
*
* Original version - Richard Jones, May 19 2001.
*
* Notes:
* ------
* 1. Output is sent to standard out through the ordinary c++ i/o library.
* 2. As a by-product of using the DOM parser to access the xml source,
* hdds-geant verifies the source against the schema before translating it.
* Therefore it may also be used as a validator of the xml specification
* (see the -v option).
*/
#define APP_NAME "hdds-geant"
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLStringTokenizer.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>
using namespace xercesc;
#include "XString.hpp"
#include "XParsers.hpp"
#include "hddsCommon.hpp"
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <list>
#include <map>
#define X(str) XString(str).unicode_str()
#define S(str) str.c_str()
void usage()
{
std::cerr
<< "Usage: " << APP_NAME << " [-v] {HDDS file}"
<< std::endl << "Options:" << std::endl
<< " -v validate only" << std::endl;
}
class FortranWriter : public CodeWriter
{
public:
void createHeader();
void createTrailer();
int createMaterial(DOMElement* el); // generate code for materials
int createSolid(DOMElement* el,
Refsys& ref); // generate code for solids
int createRotation(Refsys& ref); // generate code for rotations
int createRegion(DOMElement* el,
Refsys& ref); // generate code for regions
int createVolume(DOMElement* el,
Refsys& ref); // generate code for placement
int createDivision(XString& divStr,
Refsys& ref); // generate code for divisions
void createSetFunctions(DOMElement* el,
const XString& ident); // generate code for properties
void createGetFunctions(DOMElement* el,
const XString& ident); // generate code for identifiers
void createMapFunctions(DOMElement* el,
const XString& ident); // generate code for field maps
void createUtilityFunctions(DOMElement* el,
const XString& ident); // generate utility functions
};
int main(int argC, char* argV[])
{
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
XString message(toCatch.getMessage());
std::cerr
<< APP_NAME << " - error during initialization!"
<< std::endl << S(message) << std::endl;
return 1;
}
if (argC < 2)
{
usage();
return 1;
}
else if ((argC == 2) && (strcmp(argV[1], "-?") == 0))
{
usage();
return 2;
}
XString xmlFile;
bool geantOutput = true;
int argInd;
for (argInd = 1; argInd < argC; argInd++)
{
if (argV[argInd][0] != '-')
break;
if (strcmp(argV[argInd], "-v") == 0)
geantOutput = false;
else
std::cerr
<< "Unknown option \'" << argV[argInd]
<< "\', ignoring it\n" << std::endl;
}
if (argInd != argC - 1)
{
usage();
return 1;
}
xmlFile = argV[argInd];
#if defined OLD_STYLE_XERCES_PARSER
DOMDocument* document = parseInputDocument(xmlFile,false);
#else
DOMDocument* document = buildDOMDocument(xmlFile,false);
#endif
if (document == 0)
{
std::cerr
<< APP_NAME << " - error parsing HDDS document, "
<< "cannot continue" << std::endl;
return 1;
}
// DOMNode* docEl; commented out to avoid compiler warnings 4/26/2015 DL
try {
/*docEl =*/ document->getDocumentElement();
}
catch (DOMException& e) {
std::cerr << "Woops " << e.msg << std::endl;
return 1;
}
DOMElement* rootEl = document->getElementById(X("everything"));
if (rootEl == 0)
{
std::cerr
<< APP_NAME << " - error scanning HDDS document, " << std::endl
<< " no element named \"everything\" found" << std::endl;
return 1;
}
if (geantOutput)
{
FortranWriter fout;
fout.translate(rootEl);
}
XMLPlatformUtils::Terminate();
return 0;
}
#ifdef LINUX_CPUTIME_PROFILING
extern CPUtimer timer;
#endif
int FortranWriter::createMaterial(DOMElement* el)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createMaterial: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
int imate = CodeWriter::createMaterial(el);
double a = fSubst.getAtomicWeight();
double z = fSubst.getAtomicNumber();
double dens = fSubst.getDensity();
double radl = fSubst.getRadLength();
double absl = fSubst.getAbsLength();
// double coll = fSubst.getColLength();
// double dedx = fSubst.getMIdEdx();
XString matS = fSubst.getName();
if (fSubst.fBrewList.size() == 0)
{
if ((radl == 0) || (absl == 0))
{
std::cout
<< std::endl
<< " imate = " << imate << std::endl
<< " namate = \'" << S(matS) << "\'" << std::endl
<< " amat(1) = " << a << std::endl
<< " zmat(1) = " << z << std::endl
<< " dens = " << dens << std::endl
<< " nlmat = 1" << std::endl
<< " wmat(1) = 1" << std::endl
<< " call gsmixt(imate,namate,amat,zmat,dens,nlmat,wmat)"
<< std::endl;
}
else
{
std::cout
<< std::endl
<< " imate = " << imate << std::endl
<< " chnama = \'" << S(matS) << "\'" << std::endl
<< " a = " << a << std::endl
<< " z = " << z << std::endl
<< " dens = " << dens << std::endl
<< " radl = " << radl / (dens + 1e-30) << std::endl
<< " absl = " << absl / (dens + 1e-30) << std::endl
<< " nwbuf = 0" << std::endl
<< " call gsmate(imate,chnama,a,z,dens,radl,absl,ubuf,nwbuf)"
<< std::endl;
}
}
else
{
std::cout
<< std::endl
<< " imate = " << imate << std::endl
<< " namate = \'" << S(matS) << "\'" << std::endl;
std::list<Substance::Brew>::iterator iter = fSubst.fBrewList.begin();
for (unsigned int im = 0; im < fSubst.fBrewList.size(); im++, iter++)
{
std::cout
<< " wmat(" << im + 1 << ") = "
<< ((iter->natoms > 0)? (double)iter->natoms : iter->wfact)
<< std::endl
<< " call gfmate(" << iter->sub->fUniqueID << ",chnama,"
<< "amat(" << im + 1 << "),zmat(" << im + 1 << "),"
<< "dens,radl,absl,ubuf,nwbuf)" << std::endl;
}
iter = fSubst.fBrewList.begin();
std::cout
<< " dens = " << dens << std::endl
<< " nlmat = " << ((iter->natoms == 0)? "" : "-")
<< fSubst.fBrewList.size() << std::endl
<< " call gsmixt(imate,namate,amat,zmat,dens,nlmat,wmat)"
<< std::endl;
}
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
return imate;
}
int FortranWriter::createSolid(DOMElement* el, Refsys& ref)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createSolid: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
int ivolu = CodeWriter::createSolid(el,ref);
int imate = fSubst.fUniqueID;
static int itmedCount = 0;
std::map<std::string,double> defaultPar;
defaultPar["ifield"] = 0; // default values for tracking properties
defaultPar["fieldm"] = 0; // are overridden by values in region tag
defaultPar["tmaxfd"] = 0;
defaultPar["stemax"] = 1;
defaultPar["deemax"] = 0;
defaultPar["epsil"] = 1e-3;
defaultPar["stmin"] = 0;
std::map<std::string,double>::iterator iter;
for (iter = defaultPar.begin(); iter != defaultPar.end(); ++iter)
{
if (ref.fPar.find(iter->first) == ref.fPar.end())
{
ref.fPar[iter->first] = defaultPar[iter->first];
}
}
int itmed = ++itmedCount;
XString nameS(el->getAttribute(X("name")));
XString matS(el->getAttribute(X("material")));
XString sensiS(el->getAttribute(X("sensitive")));
std::cout
<< std::endl
<< " itmed = " << itmed << std::endl
<< " natmed = \'" << S(nameS) << " " << S(matS) << "\'"
<< std::endl
<< " nmat = " << imate << std::endl
<< " isvol = " << (sensiS == "true" ? 1 : 0) << std::endl
<< " ifield = " << ref.fPar["ifield"] << std::endl
<< " fieldm = " << ref.fPar["fieldm"] << std::endl
<< " tmaxfd = " << ref.fPar["tmaxfd"] << std::endl
<< " stemax = " << ref.fPar["stemax"] << std::endl
<< " deemax = " << ref.fPar["deemax"] << std::endl
<< " epsil = " << ref.fPar["epsil"] << std::endl
<< " stmin = " << ref.fPar["stmin"] << std::endl
<< " nwbuf = 0" << std::endl
<< " call gstmed(itmed,natmed,nmat,isvol,ifield,fieldm,tmaxfd,"
<< std::endl
<< " + stemax,deemax,epsil,stmin,ubuf,nwbuf)"
<< std::endl;
DOMElement* matEl = el->getOwnerDocument()->getElementById(X(matS));
DOMNodeList* propList = matEl->getElementsByTagName(X("optical_properties"));
if (propList->getLength() > 0)
{
std::cout << " call setoptical" << imate << "(itmed)" << std::endl;
}
Units unit;
unit.getConversions(el);
double par[99];
int npar = 0;
XString shapeS(el->getTagName());
if (shapeS == "box")
{
shapeS = "BOX ";
double xl, yl, zl;
XString xyzS(el->getAttribute(X("X_Y_Z")));
std::stringstream listr(xyzS);
listr >> xl >> yl >> zl;
npar = 3;
par[0] = xl/2 /unit.cm;
par[1] = yl/2 /unit.cm;
par[2] = zl/2 /unit.cm;
}
else if (shapeS == "tubs")
{
shapeS = "TUBS";
double ri, ro, zl, phi0, dphi;
XString riozS(el->getAttribute(X("Rio_Z")));
std::stringstream listr(riozS);
listr >> ri >> ro >> zl;
XString profS(el->getAttribute(X("profile")));
listr.clear(), listr.str(profS);
listr >> phi0 >> dphi;
npar = 5;
par[0] = ri /unit.cm;
par[1] = ro /unit.cm;
par[2] = zl/2 /unit.cm;
par[3] = phi0 /unit.deg;
par[4] = (phi0 + dphi) /unit.deg;
if (dphi == 360*unit.deg)
{
shapeS = "TUBE";
npar = 3;
}
}
else if (shapeS == "eltu")
{
shapeS = "ELTU";
double rx, ry, zl;
XString rxyzS(el->getAttribute(X("Rxy_Z")));
std::stringstream listr(rxyzS);
listr >> rx >> ry >> zl;
npar = 3;
par[0] = rx /unit.cm;
par[1] = ry /unit.cm;
par[2] = zl/2 /unit.cm;
}
else if (shapeS == "trd")
{
shapeS = "TRAP";
double xm, ym, xp, yp, zl;
XString xyzS(el->getAttribute(X("Xmp_Ymp_Z")));
std::stringstream listr(xyzS);
listr >> xm >> xp >> ym >> yp >> zl;
double alph_xz, alph_yz;
XString incS(el->getAttribute(X("inclination")));
listr.clear(), listr.str(incS);
listr >> alph_xz >> alph_yz;
npar = 11;
double x = tan(alph_xz/unit.rad);
double y = tan(alph_yz/unit.rad);
double r = sqrt(x*x + y*y);
par[0] = zl/2 /unit.cm;
par[1] = atan2(r,1)*unit.rad /unit.deg;
par[2] = atan2(y,x)*unit.rad /unit.deg;
par[3] = ym/2 /unit.cm;
par[4] = xm/2 /unit.cm;
par[5] = xm/2 /unit.cm;
par[6] = 0;
par[7] = yp/2 /unit.cm;
par[8] = xp/2 /unit.cm;
par[9] = xp/2 /unit.cm;
par[10] = 0;
}
else if (shapeS == "pcon")
{
shapeS = "PCON";
double phi0, dphi;
XString profS(el->getAttribute(X("profile")));
std::stringstream listr(profS);
listr >> phi0 >> dphi;
DOMNodeList* planeList = el->getElementsByTagName(X("polyplane"));
npar = 3;
par[0] = phi0 /unit.deg;
par[1] = dphi /unit.deg;
par[2] = planeList->getLength();
double zlast = -1e30;
for (unsigned int p = 0; p < planeList->getLength(); p++)
{
double ri, ro, zl;
DOMNode* node = planeList->item(p);
DOMElement* elem = (DOMElement*) node;
XString riozS(elem->getAttribute(X("Rio_Z")));
std::stringstream listr1(riozS);
listr1 >> ri >> ro >> zl;
if (zl < zlast)
{
std::cerr
<< APP_NAME << " error: Please re-order the polyplanes"
<< " of volume " << S(nameS) << " so that the z-values"
<< " are non-decreasing."
<< std::endl;
exit(1);
}
zlast = zl;
par[npar++] = zl /unit.cm;
par[npar++] = ri /unit.cm;
par[npar++] = ro /unit.cm;
}
}
else if (shapeS == "pgon")
{
shapeS = "PGON";
int segments;
XString segS(el->getAttribute(X("segments")));
segments = atoi(S(segS));
double phi0, dphi;
XString profS(el->getAttribute(X("profile")));
std::stringstream listr(profS);
listr >> phi0 >> dphi;
DOMNodeList* planeList = el->getElementsByTagName(X("polyplane"));
npar = 4;
par[0] = phi0 /unit.deg;
par[1] = dphi /unit.deg;
par[2] = segments;
par[3] = planeList->getLength();
double zlast = -1e30;
for (unsigned int p = 0; p < planeList->getLength(); p++)
{
double ri, ro, zl;
DOMNode* node = planeList->item(p);
DOMElement* elem = (DOMElement*) node;
XString riozS(elem->getAttribute(X("Rio_Z")));
std::stringstream listr1(riozS);
listr1 >> ri >> ro >> zl;
if (zl < zlast)
{
std::cerr
<< APP_NAME << " error: Please re-order the polyplanes"
<< " of volume " << S(nameS) << " so that the z-values"
<< " are non-decreasing."
<< std::endl;
exit(1);
}
zlast = zl;
par[npar++] = zl /unit.cm;
par[npar++] = ri /unit.cm;
par[npar++] = ro /unit.cm;
}
}
else if (shapeS == "cons")
{
shapeS = "CONS";
double rim, rip, rom, rop, zl;
XString riozS(el->getAttribute(X("Rio1_Rio2_Z")));
std::stringstream listr(riozS);
listr >> rim >> rom >> rip >> rop >> zl;
double phi0, dphi;
XString profS(el->getAttribute(X("profile")));
listr.clear(), listr.str(profS);
listr >> phi0 >> dphi;
npar = 7;
par[0] = zl/2 /unit.cm;
par[1] = rim /unit.cm;
par[2] = rom /unit.cm;
par[3] = rip /unit.cm;
par[4] = rop /unit.cm;
par[5] = phi0 /unit.deg;
par[6] = (phi0 + dphi) /unit.deg;
if (dphi == 360*unit.deg)
{
shapeS = "CONE";
npar = 5;
}
}
else if (shapeS == "sphere")
{
shapeS = "SPHE";
double ri, ro;
XString rioS(el->getAttribute(X("Rio")));
std::stringstream listr(rioS);
listr >> ri >> ro;
double theta0, theta1;
XString polarS(el->getAttribute(X("polar_bounds")));
listr.clear(), listr.str(polarS);
listr >> theta0 >> theta1;
double phi0, dphi;
XString profS(el->getAttribute(X("profile")));
listr.clear(), listr.str(profS);
listr >> phi0 >> dphi;
npar = 6;
par[0] = ri /unit.cm;
par[1] = ro /unit.cm;
par[2] = theta0 /unit.deg;
par[3] = theta1 /unit.deg;
par[4] = phi0 /unit.deg;
par[5] = (phi0 + dphi) /unit.deg;
}
else
{
std::cerr
<< APP_NAME << " error: volume " << S(nameS)
<< " should be one of the valid shapes, not " << S(shapeS)
<< std::endl;
exit(1);
}
if (nameS.size() > 4)
{
std::cerr
<< APP_NAME << " error: volume name " << S(nameS)
<< " should be no more than 4 characters long." << std::endl;
exit(1);
}
std::cout
<< std::endl
<< " chname = \'" << S(nameS) << "\'" << std::endl
<< " chshap = \'" << S(shapeS) << "\'" << std::endl
<< " nmed = " << itmed << std::endl
<< " npar = " << npar << std::endl;
for (int ipar = 0; ipar < npar; ipar++)
{
std::cout
<< " par(" << ipar + 1 << ") = " << par[ipar] << std::endl;
}
std::cout
<< " call gsvolu(chname,chshap,nmed,par,npar,ivolu)" << std::endl;
/* consistency check #1: require Geant's volume index to match mine
*
* This is required if the getX() lookup functions are going to work.
* I count volumes in the order I define them, starting from 1. If
* Geant does the same thing then this error should never occur.
*/
std::cout
<< " if (ivolu.ne." << ivolu << ")"
<< " stop \'consistency check #1 failed\'" << std::endl;
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
return ivolu;
}
int FortranWriter::createRotation(Refsys& ref)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createRotation: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
int irot = CodeWriter::createRotation(ref);
if (irot > 0)
{
std::cout
<< std::endl
<< " irot = " << irot << std::endl;
for (int i = 0; i < 3; i++)
{
double theta, phi;
double r = sqrt(ref.fRmatrix[0][i] * ref.fRmatrix[0][i]
+ ref.fRmatrix[1][i] * ref.fRmatrix[1][i]);
theta = atan2(r, ref.fRmatrix[2][i]) * 180/M_PI;
phi = atan2(ref.fRmatrix[1][i], ref.fRmatrix[0][i]) * 180/M_PI;
std::cout << std::setprecision(8)
<< " theta" << i + 1 << " = " << theta << std::endl
<< " phi" << i + 1 << " = " << phi << std::endl;
}
std::cout
<< " "
<< "call gsrotm(irot,theta1,phi1,theta2,phi2,theta3,phi3)"
<< std::endl;
}
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
return irot;
}
int FortranWriter::createRegion(DOMElement* el, Refsys& ref)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createRegion: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
int iregion = CodeWriter::createRegion(el,ref);
if (ref.fRegion)
{
DOMNodeList* noBfieldL = ref.fRegion->getElementsByTagName(X("noBfield"));
DOMNodeList* uniBfieldL = ref.fRegion->getElementsByTagName(X("uniformBfield"));
DOMNodeList* compBfieldL = ref.fRegion->getElementsByTagName(X("computedBfield"));
DOMNodeList* mapBfieldL = ref.fRegion->getElementsByTagName(X("mappedBfield"));
DOMNodeList* swimL = ref.fRegion->getElementsByTagName(X("swim"));
if (noBfieldL->getLength() > 0)
{
ref.fPar["ifield"] = 0;
ref.fPar["fieldm"] = 0;
ref.fPar["tmaxfd"] = 0;
}
else if (uniBfieldL->getLength() > 0)
{
Units funit;
DOMElement* uniBfieldEl = (DOMElement*)uniBfieldL->item(0);
funit.getConversions(uniBfieldEl);
XString bvecS(uniBfieldEl->getAttribute(X("Bx_By_Bz")));
std::stringstream str(S(bvecS));
double B[3];
str >> B[0] >> B[1] >> B[2];
ref.fPar["fieldm"] = sqrt(B[0]*B[0] + B[1]*B[1] + B[2]*B[2]);
ref.fPar["fieldm"] /= funit.kG;
ref.fPar["ifield"] = 2;
ref.fPar["tmaxfd"] = 1;
}
else if (compBfieldL->getLength() > 0)
{
Units funit;
DOMElement* compBfieldEl = (DOMElement*)compBfieldL->item(0);
funit.getConversions(compBfieldEl);
XString bmaxS(compBfieldEl->getAttribute(X("maxBfield")));
ref.fPar["fieldm"] = atof(S(bmaxS));
ref.fPar["fieldm"] /= funit.kG;
ref.fPar["ifield"] = 2;
ref.fPar["tmaxfd"] = 1;
}
else if (mapBfieldL->getLength() > 0)
{
Units funit;
DOMElement* mapBfieldEl = (DOMElement*)mapBfieldL->item(0);
funit.getConversions(mapBfieldEl);
XString bmaxS(mapBfieldEl->getAttribute(X("maxBfield")));
ref.fPar["fieldm"] = atof(S(bmaxS));
ref.fPar["fieldm"] /= funit.kG;
ref.fPar["ifield"] = 2;
ref.fPar["tmaxfd"] = 1;
}
if (swimL->getLength() > 0)
{
DOMElement* swimEl = (DOMElement*)swimL->item(0);
XString methodS(swimEl->getAttribute(X("method")));
ref.fPar["ifield"] = (methodS == "RungeKutta")? 1 : 2;
Units unit;
unit.getConversions(swimEl);
XString stepS(swimEl->getAttribute(X("maxArcStep")));
ref.fPar["tmaxfd"] = atof(S(stepS)) /unit.deg;
}
}
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
return iregion;
}
int FortranWriter::createDivision(XString& divStr, Refsys& ref)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createDivision: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
divStr[0] = toupper(divStr[0]);
divStr[1] = toupper(divStr[1]);
divStr[2] = toupper(divStr[2]);
divStr[3] = toupper(divStr[3]);
int ndiv = CodeWriter::createDivision(divStr,ref);
int iaxis;
if (ref.fPartition.axis == "x")
{
iaxis = 1;
}
else if (ref.fPartition.axis == "y")
{
iaxis = 2;
}
else if (ref.fPartition.axis == "z")
{
iaxis = 3;
}
else if (ref.fPartition.axis == "rho")
{
iaxis = 1;
}
else if (ref.fPartition.axis == "phi")
{
iaxis = 2;
}
else
{
XString motherS(ref.fMother->getAttribute(X("name")));
std::cerr
<< APP_NAME << " error: volume " << S(motherS)
<< " is divided along unsupported axis "
<< "\"" << ref.fPartition.axis << "\""
<< std::endl;
exit(1);
}
XString motherS(ref.fMother->getAttribute(X("name")));
std::cout
<< std::endl
<< " chname = \'" << divStr << "\'" << std::endl
<< " chmoth = \'" << S(motherS) << "\'" << std::endl
<< " ndiv = " << ref.fPartition.ncopy << std::endl
<< " iaxis = " << iaxis << std::endl
<< " step = " << ref.fPartition.step << std::endl
<< " c0 = " << ref.fPartition.start << std::endl
<< " numed = 0" << std::endl
<< " ndvmax = 0" << std::endl
<< " call gsdvx(chname,chmoth,ndiv,iaxis,step,c0,numed,ndvmax)"
<< std::endl;
return ndiv;
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
}
int FortranWriter::createVolume(DOMElement* el, Refsys& ref)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createVolume: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
int icopy = CodeWriter::createVolume(el,ref);
if (fPending)
{
XString nameS(el->getAttribute(X("name")));
XString motherS(fRef.fMother->getAttribute(X("name")));
int irot = fRef.fRotation;
std::cout
<< std::endl
<< " chname = \'" << S(nameS) << "\'" << std::endl
<< " nr = " << icopy << std::endl
<< " chmoth = \'" << S(motherS) << "\'" << std::endl
<< " x = " << fRef.fOrigin[0] << std::endl
<< " y = " << fRef.fOrigin[1] << std::endl
<< " z = " << fRef.fOrigin[2] << std::endl
<< " irot = " << irot << std::endl
<< " chonly = \'"
<< ((fRef.fGeometryLayer == 0)? "ONLY" : "MANY")
<< "\'" << std::endl
<< " call gspos(chname,nr,chmoth,x,y,z,irot,chonly)"
<< std::endl;
fPending = false;
}
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
return icopy;
}
void FortranWriter::createHeader()
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createHeader: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
CodeWriter::createHeader();
std::cout
<< "*" << std::endl
<< "* HDDSgeant3 - fortran geometry definition package" << std::endl
<< "* for the Hall D experiment." << std::endl
<< "*" << std::endl
<< "* WARNING: DO NOT EDIT THIS FILE" << std::endl
<< "*" << std::endl
<< "* This file was generated automatically from the" << std::endl
<< "* HDDS xml geometry definition by the hdds-geant" << std::endl
<< "* translator. Any changes made to this file will" << std::endl
<< "* disappear as soon as it is regenerated from the" << std::endl
<< "* xml source. To introduce Geant3 optimizations," << std::endl
<< "* see the subroutine Goptimize() in goptimize.F." << std::endl
<< "*" << std::endl
<< " subroutine HDDSgeant3" << std::endl
<< " implicit none" << std::endl
<< " integer imate" << std::endl
<< " character*20 chnama,namate" << std::endl
<< " real a,z,dens,radl,absl,ubuf(99)" << std::endl
<< " integer nwbuf" << std::endl
<< " real amat(99),zmat(99),wmat(99)" << std::endl
<< " integer nlmat" << std::endl
<< " integer itmed" << std::endl
<< " character*20 natmed" << std::endl
<< " integer nmat,isvol,ifield" << std::endl
<< " real fieldm,tmaxfd,stemax,deemax,epsil,stmin" << std::endl
<< " character*4 chname,chshap,chmoth" << std::endl
<< " integer nmed,npar,ivolu" << std::endl
<< " real par(99)" << std::endl
<< " integer irot" << std::endl
<< " real theta1,phi1,theta2,phi2,theta3,phi3" << std::endl
<< " integer nr,ndiv,iaxis,numed,ndvmax" << std::endl
<< " data nr,ndiv,iaxis,numed,ndvmax/0,0,0,0,0/" << std::endl
<< " real step,c0" << std::endl
<< " data step,c0/0,0/" << std::endl
<< " real x,y" << std::endl
<< " character*4 chonly" << std::endl;
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
}
void FortranWriter::createTrailer()
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createTrailer: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
CodeWriter::createTrailer();
std::cout << " end" << std::endl;
#ifdef LINUX_CPUTIME_PROFILING
timestr << " ( " << timer.getUserDelta() << " ) ";
std::cerr << timestr.str() << std::endl;
#endif
}
void FortranWriter::createSetFunctions(DOMElement* el, const XString& ident)
{
#ifdef LINUX_CPUTIME_PROFILING
std::stringstream timestr;
timestr << "createSetFunctions: " << timer.getUserTime() << ", "
<< timer.getSystemTime() << ", " << timer.getRealTime();
timer.resetClocks();
#endif
CodeWriter::createSetFunctions(el,ident);
DOMNodeList* specL = el->getElementsByTagName(X("specify"));
int len = specL->getLength();
XString subNameStr;
subNameStr = "setoptical" + ident;
std::cout
<< std::endl
<< " subroutine " << subNameStr << "(itmed)" << std::endl
<< " implicit none" << std::endl
<< " integer itmed" << std::endl
<< " integer nbins" << std::endl
<< " real Ephot(" << len << ")" << std::endl
<< " real abslen(" << len << ")" << std::endl
<< " real effic(" << len << ")" << std::endl
<< " real rindex(" << len << ")" << std::endl
<< " real smooth(" << len << ")" << std::endl
<< " real refloss(" << len << ")" << std::endl
<< " common /optical" << ident
<< "/nbins,Ephot,rindex,abslen,refloss,smooth,effic" << std::endl
<< " integer i" << std::endl
<< " data nbins/" << len << "/" << std::endl;
std::vector<double> Ephot;
std::vector<double> abslen;
std::vector<double> effic;
std::vector<double> rindex;
std::vector<double> smooth;
std::vector<double> reflect;
for (int i=0; i < len; ++i)
{
DOMElement* specEl = (DOMElement*)specL->item(i);
XString valS;
valS = specEl->getAttribute(X("E"));
Ephot.push_back(atof(S(valS)));
valS = specEl->getAttribute(X("refindex"));
rindex.push_back(atof(S(valS)));
valS = specEl->getAttribute(X("abslen"));
abslen.push_back(atof(S(valS)));
valS = specEl->getAttribute(X("smooth"));
smooth.push_back(atof(S(valS)));
valS = specEl->getAttribute(X("reflect"));
reflect.push_back(atof(S(valS)));
valS = specEl->getAttribute(X("effic"));
effic.push_back(atof(S(valS)));
}
std::stringstream Ephotstr;
std::stringstream rindexstr;
std::stringstream abslenstr;
std::stringstream smoothstr;
std::stringstream reflosstr;
std::stringstream efficstr;
for (int i=0; i < len; ++i)
{
if (i % 60 == 0)
{
int ilimit = i + 60;
ilimit = (ilimit > len)? len : ilimit;
Ephotstr << " data (Ephot(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
rindexstr << " data (rindex(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
abslenstr << " data (abslen(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
smoothstr << " data (smooth(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
reflosstr << " data (refloss(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
efficstr << " data (effic(i),i=" << i + 1 << "," << ilimit
<< ") /" << std::endl;
}
if (i % 6 == 0)
{
Ephotstr << " + ";
rindexstr << " + ";
abslenstr << " + ";
smoothstr << " + ";
reflosstr << " + ";
efficstr << " + ";
}
Ephotstr << Ephot[i]*1e-9;
rindexstr << rindex[i];
abslenstr << abslen[i];
smoothstr << smooth[i];
reflosstr << 1-reflect[i];
efficstr << effic[i];
if ((i == len-1) || (i % 60 == 59))
{
Ephotstr << "/" << std::endl;
rindexstr << "/" << std::endl;
abslenstr << "/" << std::endl;
smoothstr << "/" << std::endl;
reflosstr << "/" << std::endl;
efficstr << "/" << std::endl;
}
else if (i % 6 == 5)
{
Ephotstr << "," << std::endl;
rindexstr << "," << std::endl;
abslenstr << "," << std::endl;
smoothstr << "," << std::endl;
reflosstr << "," << std::endl;
efficstr << "," << std::endl;
}
else
{