CMS 3D CMS Logo

mps_parse_pedechi2hist.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Original author: Joerg Behr
4 # Translation from Perl to Python: Gregor Mittag
5 #
6 # This script reads the histogram file produced by Pede and it extracts the plot
7 # showing the average chi2/ndf per Mille binary number. After reading the MPS
8 # database, for which the file name has to be provided, an output file called
9 # chi2pedehis.txt is produced where the first column corresponds to the
10 # associated name, the second column corresponds to the Mille binary number, and
11 # the last column is equal to <chi2/ndf>. As further argument this scripts
12 # expects the file name of the Pede histogram file -- usually millepede.his. The
13 # last required argument represents the location of the Python config which was
14 # used by CMSSW.
15 #
16 # Use createChi2ndfplot.C to plot the output of this script.
17 
18 import os
19 import sys
20 import re
21 import argparse
22 
23 import Alignment.MillePedeAlignmentAlgorithm.mpslib.tools as mps_tools
24 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
25 
26 
27 ################################################################################
28 def main(argv = None):
29  """Main routine of the script.
30 
31  Arguments:
32  - `argv`: arguments passed to the main routine
33  """
34 
35  if argv == None:
36  argv = sys.argv[1:]
37 
38  parser = argparse.ArgumentParser(description="Analysis pede histogram file")
39  parser.add_argument("-d", "--mps-db", dest="mps_db", required=True,
40  metavar="PATH", help="MPS database file ('mps.db')")
41  parser.add_argument("--his", dest="his_file", required=True,
42  metavar="PATH", help="pede histogram file")
43  parser.add_argument("-c", "--cfg", dest="cfg", metavar="PATH", required=True,
44  help="python configuration file of pede job")
45  parser.add_argument("-b", "--no-binary-check", dest="no_binary_check",
46  default=False, action="store_true",
47  help=("skip check for existing binaries "
48  "(possibly needed if used interactively)"))
49  args = parser.parse_args(argv)
50 
51 
52  for input_file in (args.mps_db, args.his_file, args.cfg):
53  if not os.path.exists(input_file):
54  print "Could not find input file:", input_file
55  sys.exit(1)
56 
57  ids, names = get_all_ids_names(args.mps_db)
58  used_binaries = get_used_binaries(args.cfg, args.no_binary_check)
59  his_data = get_his_data(args.his_file)
60 
61  if len(his_data) != len(used_binaries):
62  print "The number of used binaries is", len(used_binaries),
63  print "whereas in contrast, however, the <chi2/ndf> histogram in Pede has",
64  print len(his_data), "bins (Pede version >= rev92 might help if #bins < #binaries).",
65  print "Exiting."
66  sys.exit(1)
67 
68  with open("chi2pedehis.txt", "w") as f:
69  for i, b in enumerate(used_binaries):
70  index = ids.index(b)
71  name = names[index]
72  f.write(" ".join([name, "{:03d}".format(b), his_data[i]])+"\n")
73 
74 
75 ################################################################################
76 def get_all_ids_names(mps_db):
77  """Returns two lists containing the mille job IDs and the associated names.
78 
79  Arguments:
80  - `mps_db`: path to the MPS database file
81  """
82 
83  lib = mpslib.jobdatabase()
84  lib.read_db(mps_db)
85 
86  ids = lib.JOBNUMBER[:lib.nJobs]
87  names = lib.JOBSP3[:lib.nJobs]
88 
89  return ids, names
90 
91 
92 def get_used_binaries(cfg, no_binary_check):
93  """Returns list of used binary IDs.
94 
95  Arguments:
96  - `cfg`: python config used to run the pede job
97  - `no_binary_check`: if 'True' a check for file existence is skipped
98  """
99 
100  cms_process = mps_tools.get_process_object(cfg)
101 
102  binaries = cms_process.AlignmentProducer.algoConfig.mergeBinaryFiles
103  if no_binary_check:
104  used_binaries = binaries
105  else:
106  # following check works only if 'args.cfg' was run from the same directory:
107  used_binaries = [b for b in binaries
108  if os.path.exists(os.path.join(os.path.dirname(cfg), b))]
109 
110  used_binaries = [int(re.sub(r"milleBinary(\d+)\.dat", r"\1", b))
111  for b in used_binaries]
112 
113  return used_binaries
114 
115 
116 def get_his_data(his_file):
117  """Parse the pede histogram file.
118 
119  Arguments:
120  - `his_file`: pede histogram file
121  """
122 
123  his_data = []
124  with open(his_file, "r") as his:
125  found_chi2_start = False;
126 
127  for line in his:
128  if r"final <Chi^2/Ndf> from accepted local fits vs file number" in line:
129  found_chi2_start = True
130  if not found_chi2_start:
131  continue
132  else:
133  if r"end of xy-data" in line: break
134  if not re.search("\d", line): continue
135  if re.search(r"[a-z]", line): continue
136  splitted = line.split()
137  his_data.append(splitted[-1])
138 
139  return his_data
140 
141 
142 ################################################################################
143 if __name__ == "__main__":
144  main()
def get_used_binaries(cfg, no_binary_check)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Definition: main.py:1