Belle II Software  release-08-01-10
flipAndRefit_sample_generation.py
1 #!/usr/bin/env python3
2 
3 
10 
11 from basf2 import process, set_random_seed, create_path, statistics, print_path, B2FATAL
12 
13 from basf2 import conditions as b2c
14 from simulation import add_simulation
15 from tracking import add_prefilter_tracking_reconstruction
16 from tracking.path_utils import add_mc_matcher
17 from tracking.FlippingMVA.savingFlippingVariables import Saving1stMVAData
18 from tracking.FlippingMVA.savingFlippingVariablesFor2ndMVA import Saving2ndMVAData
19 from background import get_background_files
20 import argparse
21 
22 """
23 generating BBbar MC samples for training and testing the track-flipping MVAs
24 """
25 
26 
27 def arg_parser():
28  parser = argparse.ArgumentParser(description=__doc__)
29  parser.add_argument('-n', '--events',
30  default=1000,
31  type=int,
32  help='Number of events to be processed. Default is 1000.',
33  metavar='EVENTS')
34  parser.add_argument('--no_bkg',
35  default=False,
36  action='store_true',
37  help='Flag to include background in the simulation. Default is False')
38  parser.add_argument('--exp',
39  default=0,
40  type=int,
41  help='Experiment number you want to simulate. Default is 0.',
42  metavar='EXP')
43  parser.add_argument('--prepend_gt',
44  default='',
45  type=str,
46  nargs='*',
47  help='Global Tags to be prepended',
48  metavar='GTs')
49  parser.add_argument('--output_file_mva',
50  default='BBbar_mva1.root',
51  type=str,
52  help='Output file name. Default is \'\'.')
53  parser.add_argument('--output_file_mva2',
54  default='BBbar_mva2.root',
55  type=str,
56  help='Output file2 name. Default is \'\'.')
57  parser.add_argument('--num',
58  default=1,
59  type=int,
60  help='The index of track-flipping MVA variables to be saved. (1 or 2). Default is 1.',
61  metavar='NUMS')
62  parser.add_argument('--randseed',
63  default=0,
64  type=int,
65  help='Random seed value. Default is 0.',
66  metavar='NUMS')
67  return parser
68 
69 
70 if __name__ == "__main__":
71 
72 
74  args = arg_parser().parse_args()
75 
76  if args.events < 0:
77  B2FATAL("Number of events to process must be larger than 0. Terminating here.")
78 
79  if args.num not in [1, 2]:
80  B2FATAL("Argument num must be either 1 or 2. Terminating here.")
81  training_mva_number = args.num
82 
83  if args.exp not in [0, 1003]:
84  B2FATAL("Argument exp must be either 0 or 1003. Terminating here.")
85 
86  if args.output_file_mva == '':
87  B2FATAL("Empty output file name. Terminating here.")
88 
89  # Setting the random seed for particle generation
90  set_random_seed(args.randseed)
91 
92  # Prepend and/or append the input GTs
93  if not (args.prepend_gt == ''):
94  for gt in reversed(args.prepend_gt):
95  b2c.prepend_globaltag(gt)
96 
97  # add (or not) the beam background
98  bkgFiles = get_background_files()
99  if args.no_bkg:
100  bkgFiles = None
101 
102 
104  main = create_path()
105  main.add_module('EventInfoSetter', evtNumList=[args.events], runList=[0], expList=[args.exp])
106  main.add_module("EvtGenInput")
107 
108  add_simulation(main, bkgfiles=bkgFiles)
109 
110 
112 
113  add_prefilter_tracking_reconstruction(main)
114 
115  main.add_module('TrackTimeEstimator')
116  add_mc_matcher(main, reco_tracks="RecoTracks")
117 
118  # here we save two samples for MVA1 and MVA2
119  if (training_mva_number):
120  main.add_module("FlipQuality", recoTracksStoreArrayName="RecoTracks",
121  identifier='TRKTrackFlipAndRefit_MVA1_weightfile',
122  indexOfFlippingMVA=1).set_name("FlipQuality_1stMVA")
123 
124  reco_tracks_flipped = "RecoTracks_flipped"
125  main.add_module("RecoTracksReverter", inputStoreArrayName="RecoTracks",
126  outputStoreArrayName=reco_tracks_flipped)
127  main.add_module("DAFRecoFitter", recoTracksStoreArrayName=reco_tracks_flipped).set_name("Combined_DAFRecoFitter_flipped")
128  main.add_module("IPTrackTimeEstimator",
129  recoTracksStoreArrayName=reco_tracks_flipped, useFittedInformation=False)
130  main.add_module("TrackCreator", trackColName="Tracks_flipped",
131  trackFitResultColName="TrackFitResults_flipped",
132  recoTrackColName=reco_tracks_flipped,
133  pdgCodes=[211, 321, 2212]).set_name("TrackCreator_flipped")
134  main.add_module("FlipQuality", recoTracksStoreArrayName="RecoTracks",
135  identifier='TRKTrackFlipAndRefit_MVA2_weightfile',
136  indexOfFlippingMVA=2).set_name("FlipQuality_2ndMVA")
137  if (training_mva_number == 1):
138  saveFirstMVAData = Saving1stMVAData(
139  name="saving1stMVA_BBbar",
140  output_file_name=args.output_file_mva)
141  main.add_module(saveFirstMVAData)
142  else:
143  saveSecondMVAData = Saving2ndMVAData(
144  name="saving2ndMVA_BBbar",
145  output_file_name=args.output_file_mva2)
146  main.add_module(saveSecondMVAData)
147 
148  # Process events
149  print_path(main)
150 
151  main.add_module("Progress")
152  process(main)
153 
154  # Print call statistics
155  print(statistics)