make_template_knob_config.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 # This script outputs a template table of all tunable GENIE knobs pertaining to the tag release indicated by the SAM definition name used for producing the files
4 # One has to setup_nova before using this script since this script utilizes ROOT
5 # Usage:
6 # $ setup_nova
7 # $ python make_template_knob_config.py -d <sam_defname>
8 
9 from ROOT import *
10 import argparse
11 import collections
12 import json
13 import subprocess, os, sys
14 
15 def setknob(knobname, value):
16  if knobname in kconf:
17  kconf[knobname] = value
18 
19 # use production 3 ND MC as the default dataset
20 default_dataset = 'prod_caf_R17-03-01-prod3reco.d_nd_genie_nonswap_fhc_nova_v08_full_v1'
21 
22 # command line parser
23 parser = argparse.ArgumentParser(description='script to make a template configuration file of all tunable GENIE knobs pertaining to the tag release indicated by the SAM definition name used for producing the files')
24 parser.add_argument('-d','--defname',type=str,default=default_dataset)
25 parser.add_argument('--printout', dest='printout', action='store_true')
26 parser.add_argument('--no-printout', dest='printout', action='store_false')
27 parser.set_defaults(printout=True)
28 args = parser.parse_args()
29 
30 # remind of dataset used
31 if default_dataset == args.defname:
32  print '********** NOTE **********'
33 print 'SAM definition being used for generating the config file is'
34 print args.defname
35 if default_dataset == args.defname:
36  print 'It can be changed with -d command line option.'
37 
38 # check if the given SAM definition name contains a valid tag release
39 tag_found = False
40 found_tag = ''
41 (stdout, stderr) = subprocess.Popen('svn list svn+ssh://p-novaart@cdcvs.fnal.gov/cvs/projects/novaart/pkgs.svn/tags', shell=True, stdout=subprocess.PIPE).communicate()
42 for tag in stdout.split('\n'):
43  if tag.rstrip('/') in args.defname:
44  tag_found = True
45  found_tag = tag.rstrip('/')
46  break
47 if not tag_found:
48  print 'The tag version indicated by the input SAM definition name is not found.'
49  sys.exit()
50 
51 
52 # open the source file containing the GENIE knobs and store the knob names in a list
53 (stdout, stderr) = subprocess.Popen('source /grid/fermiapp/nova/novaart/novasvn/setup/setup_nova.sh -r '+found_tag+' &>/dev/null;printenv', shell=True, stdout=subprocess.PIPE).communicate()
54 
55 # fill environment variable dictionary
56 envdict = dict()
57 for line in stdout.split('\n'):
58  if len(line.split('=')) >= 2:
59  envdict[line.split('=')[0]] = line.split('=')[1].rstrip('\n')
60 
61 knoblist = []
62 with open(os.path.join(envdict['NUGEN_DIR'],'source/NuReweight/ReweightLabels.h')) as fsrc:
63  for line in fsrc:
64  if 'genie::rew::' in line:
65  if len(line.split('=')) >= 2:
66  knoblist.append(line.split('=')[0].strip().replace('fReweight','').replace('kReweight',''))
67 
68 if(args.printout):
69  print 'all available knobs:\n', '='*80
70  for knobname in knoblist: sys.stdout.write(knobname+' ')
71 print '\n'+'='*80
72 
73 # It can happen that the number of knobs used in CAF files is different from
74 # that in nugen. What follows is to get the number used in CAF.
75 (stdout, stderr) = subprocess.Popen('source /grid/fermiapp/nova/novaart/novasvn/setup/setup_nova.sh -r '+found_tag+' &>/dev/null; samweb2xrootd `samweb list-files "defname: '+args.defname+' with limit 1"`', shell=True, stdout=subprocess.PIPE).communicate()
76 tfilepn = stdout.rstrip('\n')
77 if args.printout:
78  print 'Opening CAF file:', tfilepn
79 
80 # setup CAF dictionary
81 gSystem.Load("libCintex.so")
82 Cintex.Enable()
83 gSystem.Load(os.path.join(envdict['SRT_PUBLIC_CONTEXT'],'lib/Linux2.6-GCC-debug/libStandardRecord_dict.so'))
84 gInterpreter.AddIncludePath(envdict['SRT_PUBLIC_CONTEXT'])
85 
86 tfile = TFile.Open(tfilepn)
87 recTree = tfile.Get('recTree')
88 nknobs = 0
89 for entry in recTree:
90  if entry.rec.mc.nnu <= 0: continue
91  nknobs = len(entry.rec.mc.nu[0].rwgt.genie)
92  if nknobs: break
93 
94 # knob configuration
95 kconf = collections.OrderedDict()
96 for i in range(0, nknobs):
97  kconf[knoblist[i]] = 1
98 
99 ## knobs that need extra care
100 setknob('Null', 0)
101 setknob('NormCCQEenu', 0)
102 setknob('MaCCQE', 0)
103 # by default, disable z expansion knobs
104 setknob('ZNormCCQE', 0)
105 setknob('ZExpA1CCQE', 0)
106 setknob('ZExpA2CCQE', 0)
107 setknob('ZExpA3CCQE', 0)
108 setknob('ZExpA4CCQE', 0)
109 setknob('AxFFCCQEshape', 0)
110 
111 # output configuration to file
112 #~ with open('knob_config.json', 'w') as outfile:
113  #~ json.dump(kconf, outfile, indent=2)
114 
115 # output configuration to file
116 # Due to a bug in the C++ boost library, I will use plain text format instead.
117 # Boost property tree v1_57_0a when compiled with c++11 results in a
118 # compile time error needing a patch.
119 # See https://svn.boost.org/trac/boost/ticket/6785 for more information.
120 outfilename = 'knob_config.txt'
121 print 'Writing output file to '+outfilename
122 with open(outfilename, 'w') as outfile:
123  for k,v in kconf.items():
124  outfile.write(k+' '+str(v)+'\n')
125 print 'Done'
if(dump)
procfile open("FD_BRL_v0.txt")