modifyFHiCL.py
Go to the documentation of this file.
1 #!/bin/env python
2 print "run : --- Find and modify FHiCL"
3 ########################### Imports
4 import os
5 import FhiclConfigurations as fcl_config
6 ########################### Option parser
7 from optparse import OptionParser
8 parser = OptionParser()
9 parser.add_option("-b", "--base_fcl", help="The name of base FHiCL file we will convert", action="store", type=str, dest="base_fcl", default=False)
10 parser.add_option("-a", "--all", help="Apply modifications based on the all key", action="store_true", dest="all", default=False)
11 parser.add_option("-o", "--overlay_file", help="The name of overlay file", action="store", type=str, dest="overlay", default=False)
12 parser.add_option("-g", "--gain_mode", help="The gain mode (nd100, fd100, fd140)", action="store", type=str, dest="gain_mode", default=False)
13 parser.add_option("-f", "--fcl_name", help="The name of outputted FHiCL", action="store", type=str, dest="output", default="test.fcl")
14 parser.add_option("-v", "--verbose", help="turn on verbose mode", action="store_true", dest="verbose", default=False)
15 (options, args) = parser.parse_args()
16 if options.verbose:
17  print "run : --- Options"
18  print "run : base fcl: ",options.base_fcl
19  print "run : all mode: ",options.all
20  print "run : overlay file: ",options.overlay
21  print "run : output fcl name: ",options.output
22  print "run : verbose mode: ",options.verbose
23 # Sanity checks
24 if not options.base_fcl:
25  print "Base FHiCL file must be specified with -b"
26  exit(1)
27 if (not options.all) and (options.base_fcl not in fcl_config.to_replace.keys()):
28  print "Unknown configuration key: %s, the available options are:"%options.base_fcl
29  for k in fcl_config.to_replace.keys(): print k
30  exit(1)
31 if options.overlay and (not (os.path.exists(options.overlay))):
32  print "Warning: overlay file %s doesn't exist, subsequent art jobs will likely fail"%options.overlay
33  # exit(1)
34 
35 if options.gain_mode and (options.gain_mode not in ("nd100","fd100", "fd140")):
36  print "Unknown gain mode %s" % options.gain_mode
37 
38 if options.base_fcl[:2] == "./": options.base_fcl = options.base_fcl[2:]
39 if options.output[:2] == "./": options.output = options.output[2:]
40 ########################### Get the base FHiCL
41 found_fcl = False
42 if os.path.exists(options.base_fcl):
43  if options.verbose: print "run : FHiCL found locally"
44  found_fcl = options.base_fcl
45 else:
46  if "FHICL_FILE_PATH" not in options.base_fcl: options.base_fcl = "FHICL_FILE_PATH/%s"%options.base_fcl
47  if options.verbose: print "run : Searching FHICL_FILE_PATH for base fcl"
48  if "FHICL_FILE_PATH" not in os.environ:
49  print "FHICL_FILE_PATH environment variable not defined, therefore I can't search it."
50  exit(1)
51  paths = os.environ["FHICL_FILE_PATH"].split(":")
52  if options.verbose: print "run : Searching paths: ",paths
53  base_fcl_tokens = options.base_fcl.split("FHICL_FILE_PATH/")
54  if len(base_fcl_tokens) != 2:
55  print "UsageError, if searching FHICL_FILE_PATH usage is 'FHICL_FILE_PATH/FHICL_TO_FIND"
56  exit(1)
57  found = False
58  for path in paths:
59  if path[:-1] != "/": path+= "/"
60  if os.path.exists("%s%s"%(path,base_fcl_tokens[1])):
61  found_fcl = "%s%s"%(path,base_fcl_tokens[1])
62  found = True
63  if options.verbose: print "run : Found FHiCL in %s"%path
64  break
65  if not found:
66  print "Cannot find base FHiCL"
67  exit(1)
68 # else:
69 # found_fcl = options.base_fcl
70 # if (not os.path.exists(found_fcl)):
71 # print "Base FHiCL %s doesn't exist"%options.base_fcl
72 # exit(1)
73 ########################### Setup what to replace
74 """
75 Get a dictionary of strings to replace. The item should be a two element array,
76 the key is the string to find in the base fcl, and the item should be
77 what to replace it with.
78 """
79 if options.all:
80  to_replace = fcl_config.to_replace["all"]
81 else:
82  to_replace = fcl_config.to_replace[options.base_fcl]
83 if options.verbose:
84  for key in to_replace.keys():
85  print "run : Line to replace: %s"%key.strip()
86 ########################### Open the base file
87 if options.verbose: print "run : Reading and altering input file"
88 base_file = open(found_fcl, "r").readlines()
89 to_write = []
90 for line in base_file:
91  # print line.strip("\n")
92  for key in to_replace.keys():
93  if key in line:
94  if options.verbose: print "run : found line to replace: %s"%line.strip()
95  line = line.replace(key, to_replace[key])
96  if "OVERLAY_HOOK" in line and options.overlay:
97  line = line.replace("OVERLAY_HOOK",options.overlay)
98  if options.verbose: print "run : new line: %s"%line
99  to_write.append(line)
100 
101 
102 if options.gain_mode == "fd100":
103  to_write.append("physics.producers.daq.fd.ForceGain: 100\n")
104 elif options.gain_mode == "fd140":
105  to_write.append("physics.producers.daq.fd.ForceGain: 140\n")
106 elif options.gain_mode == "nd140":
107  to_write.append("physics.producers.daq.nd.ForceGain: 100\n")
108 
109 ########################### Append EOF lines
110 for key in to_replace.keys():
111  if key == "EOF": to_write.append(to_replace[key])
112 ########################### Write the output file
113 out_file = open(options.output, "w")
114 for line in to_write: out_file.write(line)
115 out_file.close()
116 ########################### Done
117 if options.verbose: print "run : Done\n"
void split(double tt, double *fr)
procfile open("FD_BRL_v0.txt")
exit(0)