-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_my_splines.py
74 lines (62 loc) · 2.82 KB
/
insert_my_splines.py
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
#####################################################################################
# Script name: insert_my_spline.py
# Purpose: Delete values in default spline xml file and insert my splines knots
#
# Important variables:
# fName: Path to original native GENIE splines
# fName2: Path to CSMS spline snippet generated by gen_my_spline9.py
# outName: Path to output GENIE splines with CSMS cross sections
#
# Copied from /Users/peaelle42/Documents/CSMS/codes/play_elementtree/insert_my_splines.py
# Pueh Leng Tan, 22 Aug 2018
#####################################################################################
import xml.etree.cElementTree as ET
import pdb
# Base native GENIE splines
fName = 'native_genie/full_package_10TeV_default_500knots.xml'
# CSMS spline snippet
fName2 = 'full_package_snippet_v9c.xml'
# Output spline
outName = 'full_package_conjoined_v9c.xml'
# native GENIE splines
myTree = ET.parse(fName)
myRoot = myTree.getroot()
# CSMS spline snippets
insertTree = ET.parse(fName2)
insertRoot = insertTree.getroot()
for ins in insertRoot: # for each spline in CSMS snippet
# 'ins' is the layer with <spline ...> ... </spline>
# Get name of the spline
locName = ins.get('name')
# ins[0]/ins[-1] is the first/last knot in the spline
minCut = float(ins[0][0].text) # cause <E> is the first field
maxCut = float(ins[-1][0].text) # cause <E> is the first field
print('Spline %s: minE = %.3f, maxE = %.3f' %(ins, minCut, maxCut))
cnt0 = 0
for spl in myRoot: # for each spline in the native GENIE spline
if locName==spl.get('name'): # Looking for the relevant subchannel spline
rmBag = []
cnt0+=1
print('%i: %s, %s' % (cnt0, spl.tag, spl.attrib))
cnt1 = 0
for knt in spl:
cnt1+=1
print('%i: %s, %s' % (cnt1, knt.tag, knt.attrib))
# knot energy of that particular native GENIE spline
locVal = float(knt[0].text)
# Keeping track of native GENIE spline knots that are within
# range to be cut away and storing the 'knot' element in a bag
if locVal>=minCut and locVal<=maxCut:
print(locVal)
rmBag.append(knt)
print knt
# Remove old E knot points in subchannel splines
for lala in rmBag:
print lala
spl.remove(lala)
# Add CSMS E knot points in subchannel splines
for insKnt in ins:
print('Inserting %s' % insKnt)
spl.append(insKnt)
break # Don't bother checking the rest of the splines
myTree.write(outName)