Belle II Software  release-06-02-00
DBBunchStructureImporter.py
1 #!/usr/bin/env python
2 
3 
10 
11 # Import bunch structure payload for master GT.
12 # Create payload taking information from the RunDB.
13 # Usage: basf2 DBBunchStructureImporter.py <minexp> <minrun> <maxexp> <maxrun>
14 #
15 # if minexp is 0 the designed filling patter will be created. Meaning 10101010...
16 # if minexp is 1003 the early phase 3 pattern will be created. Take the most used filling pattern.
17 
18 from ROOT import Belle2
19 from rundb import RunDB
20 import sys
21 import basf2
22 
23 
24 def fill(fillPatternHex, firstExp, firstRun, lastExp, lastRun):
25  """Create a payload with the given hexadecmal fill pattern for the given run range"""
26 
27  if fillPatternHex is None:
28  return
29 
30  bunches = Belle2.BunchStructure()
31  fillPatternBin = bin(int(fillPatternHex, 16))
32  fillPatternBin = fillPatternBin.replace('0b', '')
33 
34  for num, bucket in enumerate(fillPatternBin):
35  if(bucket == "1"):
36  bunches.setBucket(num)
37 
38  print("Filling new payload. iov:", firstExp, firstRun, lastExp, lastRun)
39 
40  iov = Belle2.IntervalOfValidity(firstExp, firstRun, lastExp, lastRun)
41 
43  db.storeData("BunchStructure", bunches, iov)
44 
45 
46 # Argument parsing
47 argvs = sys.argv
48 
49 
50 if len(argvs) == 2:
51  minexp = argvs[1]
52 
53 elif len(sys.argv) == 5:
54  minexp = argvs[1]
55  minrun = argvs[2]
56  maxexp = argvs[3]
57  maxrun = argvs[4]
58 else:
59  basf2.B2ERROR("Wrong arguments. Options are:\n",
60  "1 arguments usage: basf2 ", argvs[0], " <0> or <1003>\n"
61  "4 arguments usage: basf2 ", argvs[0], " <minexp> <minrun> <maxexp> <maxrun>")
62  sys.exit()
63 
64 
65 if(minexp == "0"):
66  bunches = Belle2.BunchStructure()
67 
68  extraStep = 0
69  for b in range(0, 5120, 2):
70 
71  # Shift of 1 bunch every 300 bunches to simulate bunch trains
72  if((b % 300 == 0) & (b != 0)):
73  extraStep += 1
74 
75  bunches.setBucket(b + extraStep)
76 
77  iov = Belle2.IntervalOfValidity(0, 0, 0, -1)
78 
80  db.storeData("BunchStructure", bunches, iov)
81 
82 elif(minexp == "1003"):
83 
84  username = input("Enter your username: ")
85  rundb = RunDB(username=username)
86 
87  runInfo = rundb.get_run_info(
88  min_experiment=12,
89  max_experiment=12,
90  min_run=1859,
91  max_run=1859,
92  run_type='physics',
93  expand=True
94  )
95 
96  for it in runInfo:
97  pattern = it['ler']['fill_pattern']
98  fill(pattern, 1003, 0, 1003, -1)
99 
100 
101 else:
102  username = input("Enter your username: ")
103  rundb = RunDB(username=username)
104 
105  runInfo = rundb.get_run_info(
106  min_experiment=minexp,
107  max_experiment=maxexp,
108  min_run=minrun,
109  max_run=maxrun,
110  run_type='physics',
111  expand=True
112  )
113 
114  current_pattern = None
115  current_start = None, None
116  current_end = None, None
117 
118  lumiMax = 0
119  runExpMaxBegin = 0
120  runExpMaxEnd = 0
121 
122  lumi = 0
123  for it in runInfo:
124  exprun = it['experiment'], it['run']
125  pattern = it['ler']['fill_pattern']
126 
127  if(pattern == ""):
128  continue
129 
130  # pattern different to previous one or first run
131  if pattern != current_pattern:
132 
133  if(lumi > lumiMax):
134  runExpMaxBegin = current_start
135  runExpMaxEnd = current_end
136  lumiMax = lumi
137 
138  # close the iov
139  fill(current_pattern, *current_start, *current_end)
140  if current_pattern is not None:
141  print(f"Corresponding to {lumi/1000.:.2f} pb-1\n")
142 
143  # and remember new values
144  current_pattern = pattern
145  current_start = exprun
146  current_end = exprun
147 
148  lumi = it['statistics']['lumi_recorded']
149 
150  else:
151  # pattern unchanged, extend current iov
152  current_end = exprun
153  lumi += it['statistics']['lumi_recorded']
154 
155  # close the last iov if any
156  fill(current_pattern, *current_start, *current_end)
157  print(f"Corresponding to {lumi/1000.:.2f} pb-1\n")
158 
159  print(
160  f"Most used filling pattern:\niov: \
161  {runExpMaxBegin[0]},{runExpMaxBegin[1]},{runExpMaxEnd[0]},{runExpMaxEnd[1]} \
162  \nIntegrated luminosity: {lumiMax/1000:.2f} pb-1")
Class to store the fill pattern of colliding bunches.
A class that describes the interval of experiments/runs for which an object in the database is valid.
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:41