Belle II Software  release-08-01-10
background.py
1 #!/usr/bin/env python3
2 
3 
10 
11 from basf2 import create_path, B2ERROR, B2INFO
12 from basf2.utils import pretty_print_table
13 import os
14 import glob
15 
16 
17 def get_background_files(folder=None, output_file_info=True):
18  """ Loads the location of the background files from the environmant variable
19  BELLE2_BACKGROUND_DIR which is set on the validation server and ensures that background
20  files exist and returns the list of background files which
21  can be directly used with add_simulation() :
22 
23  >>> add_simulation(main, bkgfiles=background.get_background_files())
24 
25  Will fail with an assert if no background folder set or if no background file was
26  found in the set folder.
27 
28  Parameters:
29  folder (str): A specific folder to search for background files can be given as an optional parameter
30  output_file_info (str): If true, a list of the found background files and there size will be printed
31  This is useful to understand later which background campaign has been used
32  to simulate events.
33  """
34 
35  env_name = 'BELLE2_BACKGROUND_DIR'
36  bg = None
37 
38  if folder is None:
39  if env_name not in os.environ:
40  raise RuntimeError("Environment variable {} for backgound files not set. Terminanting this script.".format(env_name))
41  folder = os.environ[env_name]
42 
43  bg = glob.glob(folder + '/*.root')
44 
45  if len(bg) == 0:
46  raise RuntimeError("No background files found in folder {} . Terminating this script.".format(folder))
47 
48  B2INFO("Background files loaded from folder {}".format(folder))
49 
50  # sort for easier comparison
51  bg = sorted(bg)
52 
53  if output_file_info:
54  bg_sizes = [os.path.getsize(f) for f in bg]
55  # reformat to work with pretty_print_table
56  table_rows = [list(entry) for entry in zip(bg, bg_sizes)]
57  table_rows.insert(0, ["- Background file name -", "- file size -"])
58 
59  pretty_print_table(table_rows, [0, 0])
60 
61  return bg
62 
63 
64 def add_output(path, bgType, realTime, sampleType, phase=3, fileName='output.root', excludeBranches=None):
65  '''
66  A function to be used for output of BG simulation.
67  @param path path name
68  @param bgType background type, to get available types: basf2 -m BeamBkgTagSetter
69  @param realTime equivalent time of superKEKB running in [ns]
70  @param sampleType 'study' (for BG studies) or 'usual', 'PXD', 'ECL' (for BG mixer)
71  @param specify the Phase, 1 for Phase 1, 2 for Phase 2, and 3 for Physics Run or Phase 3
72  @param fileName optional file name, can be overridden by basf2 -o
73  '''
74  if excludeBranches is None:
75  excludeBranches = []
76 
77  if sampleType == 'study':
78  madeFor = ''
79  branches = []
80  elif sampleType == 'usual' and phase == 3:
81  madeFor = ''
82  branches = [
83  'PXDSimHits',
84  'SVDSimHits',
85  'CDCSimHits',
86  'TOPSimHits',
87  'ARICHSimHits',
88  'ECLHits',
89  'KLMSimHits',
90  ]
91  elif sampleType == 'usual' and phase == 2:
92  madeFor = ''
93  branches = [
94  'PXDSimHits',
95  'SVDSimHits',
96  'CDCSimHits',
97  'TOPSimHits',
98  'ARICHSimHits',
99  'ECLHits',
100  'KLMSimHits',
101  'CLAWSSimHits',
102  'FANGSSimHits',
103  'PlumeSimHits',
104  'BeamabortSimHits',
105  'PindiodeSimHits',
106  'QcsmonitorSimHits',
107  'He3tubeSimHits',
108  'MicrotpcSimHits',
109  ]
110  elif sampleType == 'usual' and phase == 1:
111  madeFor = ''
112  branches = [
113  'ClawSimHits',
114  'BeamabortSimHits',
115  'PindiodeSimHits',
116  'QcsmonitorSimHits',
117  'He3tubeSimHits',
118  'MicrotpcSimHits',
119  'BgoSimHits',
120  'CsiSimHits',
121  ]
122  elif sampleType == 'ECL':
123  madeFor = 'ECL'
124  branches = ['ECLHits']
125  elif sampleType == 'PXD':
126  madeFor = 'PXD'
127  branches = ['PXDSimHits']
128  else:
129  madeFor = ''
130  branches = []
131  B2ERROR('add_output - invalid value of argument sampleType: %s'
132  % sampleType)
133 
134  # Set background tag in SimHits and add BackgroundMetaData into persistent tree
135  tagSetter = path.add_module('BeamBkgTagSetter', backgroundType=bgType, realTime=realTime,
136  specialFor=madeFor, Phase=phase)
137 
138  # Write out only non-empty events when producing samples for BG mixer
139  if sampleType != 'study':
140  emptyPath = create_path()
141  tagSetter.if_false(emptyPath)
142 
143  # Output to file. We don't need a TTreeIndex for background files and memory
144  # consumption can be improved by setting a lower autoFlushSize so that
145  # fewer and or smaller amounts of data have to be read for each GetEntry()
146  path.add_module('RootOutput', outputFileName=fileName, branchNames=branches, excludeBranchNames=excludeBranches,
147  buildIndex=False, autoFlushSize=-500000)