CMS 3D CMS Logo

generateFlavCfromFlavB.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from __future__ import absolute_import
5 import os
6 import sys
7 import itertools
8 from . import dataLoader
9 from . import checkBTagCalibrationConsistency as checker
10 
11 
12 def generate_flav_c(loaded_data):
13  flav_b_data = [e for e in loaded_data.entries if e.params.jetFlavor == 0]
14  flav_b_data = sorted(flav_b_data, key=lambda e: e.params.operatingPoint)
15  flav_b_data = sorted(flav_b_data, key=lambda e: e.params.measurementType)
16  flav_b_data = sorted(flav_b_data, key=lambda e: e.params.etaMin)
17  flav_b_data = sorted(flav_b_data, key=lambda e: e.params.ptMin)
18  flav_b_data = sorted(flav_b_data, key=lambda e: e.params.discrMin)
19  sys_groups = itertools.groupby(
20  flav_b_data,
21  key=lambda e: '%d, %s, %.02f, %.02f, %.02f' % (
22  e.params.operatingPoint,
23  e.params.measurementType,
24  e.params.etaMin,
25  e.params.ptMin,
26  e.params.discrMin
27  )
28  )
29 
30  def gen_entry_dict(groups):
31  for _, grp in groups:
32  grp = list(grp)
33  entries_by_sys = dict((e.params.sysType, e) for e in grp)
34  assert len(grp) == len(entries_by_sys) # every sysType is unique
35  yield entries_by_sys
36  sys_dicts = gen_entry_dict(sys_groups)
37 
38  def gen_flavb_csv_line(dicts):
39  for d in dicts:
40  central = d.pop('central')
41  central.params.jetFlavor = 1
42  yield central.makeCSVLine()
43  for e in d.values():
44  e.params.jetFlavor = 1
45  e.formula = '2*(%s)-(%s)' % (e.formula, central.formula)
46  yield e.makeCSVLine()
47  csv_lines = gen_flavb_csv_line(sys_dicts)
48 
49  return list(l for l in csv_lines)
50 
51 
52 def main():
53  if len(sys.argv) < 3:
54  print('Need input/output filenames as first/second arguments. Exit.')
55  exit(-1)
56  if os.path.exists(sys.argv[2]):
57  print('Output file exists. Exit.')
58  exit(-1)
59 
60  print('\nChecking input file consistency...')
61  loaders = dataLoader.get_data(sys.argv[1])
62  checks = checker.run_check_data(loaders, True, True, False)
63  for data in loaders:
64  typ = data.meas_type
65  if 1 in data.flavs:
66  print('FLAV_C already present in input file for %s. Exit.' % typ)
67  exit(-1)
68  if not any(0 in data.flavs for data in loaders):
69  print('FLAV_B not found in input file. Exit.')
70  exit(-1)
71 
72 
73  print('\nGenerating new csv content...')
74  new_csv_data = list(itertools.chain.from_iterable(
75  l
76  for d in loaders
77  for l in generate_flav_c(d)
78  ))
79 
80  with open(sys.argv[1]) as f:
81  old_csv_data = f.readlines()
82 
83  with open(sys.argv[2], 'w') as f:
84  f.writelines(old_csv_data)
85  f.write('\n')
86  f.writelines(new_csv_data)
87 
88  print('Done.')
89 
90 
91 if __name__ == '__main__':
92  main()
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:36
def generate_flav_c(loaded_data)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Definition: main.py:1
def get_data(filename)
Definition: dataLoader.py:191
def exit(msg="")