| 1 | #!/usr/bin/env python2
|
|---|
| 2 | # -*- coding: utf-8 -*-
|
|---|
| 3 | """
|
|---|
| 4 | Created on Sun Jan 26 13:35:54 2020
|
|---|
| 5 |
|
|---|
| 6 | @author: jackaraz
|
|---|
| 7 | """
|
|---|
| 8 | import os, json, sys
|
|---|
| 9 |
|
|---|
| 10 | def get_answer(text,answer=[]):
|
|---|
| 11 | question = raw_input(text)
|
|---|
| 12 | if answer == []:
|
|---|
| 13 | return question
|
|---|
| 14 | else:
|
|---|
| 15 | while not question in answer:
|
|---|
| 16 | question = raw_input(text)
|
|---|
| 17 | return question
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | def json_file(file, output, n=0,interactive=False):
|
|---|
| 21 | with open(file,'r') as json_file:
|
|---|
| 22 | input_json = json.load(json_file)
|
|---|
| 23 |
|
|---|
| 24 | output.write('\n <pyhf id="Name_Me_'+str(n)+'">\n')
|
|---|
| 25 | output.write(' <name>'+file+'</name>\n')
|
|---|
| 26 | output.write(' <regions>\n')
|
|---|
| 27 |
|
|---|
| 28 | for channel in input_json['channels']:
|
|---|
| 29 | channel_name = channel['name']
|
|---|
| 30 | output.write(' <channel name="'+channel_name+'">')
|
|---|
| 31 | for x in input_json['observations']:
|
|---|
| 32 | if x['name'] == channel_name:
|
|---|
| 33 | obs = [str(i) for i in x['data']]
|
|---|
| 34 | break
|
|---|
| 35 | channel_data_length = len(obs)
|
|---|
| 36 | txt = '\tPlease write the name of '+str(channel_data_length)+\
|
|---|
| 37 | '\n\tregion from your recast, which corresponds to \n'+\
|
|---|
| 38 | '\tfollowing observed values: '+', '.join(obs)+'\n'
|
|---|
| 39 | if interactive:
|
|---|
| 40 | print('Writing '+channel_name+'...')
|
|---|
| 41 | SR = get_answer(txt)
|
|---|
| 42 | if len(SR.split()) != channel_data_length:
|
|---|
| 43 | print('\x1b[32mPlease note that number of given region does not match with the data...\x1b[0m')
|
|---|
| 44 | if SR == '':
|
|---|
| 45 | output.write(' </channel>\n')
|
|---|
| 46 | else:
|
|---|
| 47 | output.write('\n '+'\n '.join(SR.split()))
|
|---|
| 48 | output.write('\n </channel>\n')
|
|---|
| 49 | else:
|
|---|
| 50 | output.write('\n '+txt)
|
|---|
| 51 | output.write('\n </channel>\n')
|
|---|
| 52 |
|
|---|
| 53 | output.write(' </regions>\n </pyhf>\n')
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | def help():
|
|---|
| 57 | print('Please run the code with json files included:')
|
|---|
| 58 | print('python write_histfactory_info.py [opt] NAME.json NAME1.json NAME2.json')
|
|---|
| 59 | print(' -i interactive option sets the info file with you.')
|
|---|
| 60 | print(' -h prints this amazingly helpful 4 line comment.')
|
|---|
| 61 | print("You're welcome...")
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | if __name__=='__main__':
|
|---|
| 65 | if any([x for x in sys.argv if x in ['-h','-hi','-ih']]):
|
|---|
| 66 | help()
|
|---|
| 67 | sys.exit(0)
|
|---|
| 68 | json_files = [x for x in sys.argv if x.endswith('.json')]
|
|---|
| 69 | if json_files == []:
|
|---|
| 70 | help()
|
|---|
| 71 | sys.exit(0)
|
|---|
| 72 |
|
|---|
| 73 | delete = True
|
|---|
| 74 | if os.path.isfile('MA5_pyhf.info'):
|
|---|
| 75 | delete = get_answer('\x1b[32mWould you like to overwrite MA5_pyhf.info? [Y/N]\x1b[0m \n',
|
|---|
| 76 | answer=['Y','N','y','n']) in ['Y','y']
|
|---|
| 77 | if delete:
|
|---|
| 78 | output = open('MA5_pyhf.info','w')
|
|---|
| 79 | else:
|
|---|
| 80 | output = open('MA5_pyhf.info','a+')
|
|---|
| 81 | if any([x for x in sys.argv if x in ['-i','-hi','-ih']]):
|
|---|
| 82 | print "Please open the relevant table for this particular analysis"
|
|---|
| 83 | print "which you can read the <observed> values. Note that since"
|
|---|
| 84 | print "given json file is not very obvious we will need to match "
|
|---|
| 85 | print "the observed data with SR regions that you named in your analysis."
|
|---|
| 86 | print "Please only use the SR regions that YOU defined, rest is simply empty."
|
|---|
| 87 | for n,(file) in enumerate(json_files):
|
|---|
| 88 | if os.path.isfile(file):
|
|---|
| 89 | json_file(file, output,n=n,
|
|---|
| 90 | interactive=any([x for x in sys.argv if x in ['-i','-hi','-ih']]))
|
|---|
| 91 | else:
|
|---|
| 92 | print('Can not find '+file)
|
|---|
| 93 | output.close()
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|