Belle II Software  release-08-01-10
create_RunInfo.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """
12 Simple script to import RunInfo from the DAQ database into conditions
13 db payloads.
14 As input you need a file which contains on each line the following
15 fields separated by whitespace::
16 
17 exp run runtype StartTime StopTime receivedNevent AcceptedNevent sentNevent runLength
18 TriggerRate PXDflag SVDflag CDCflag TOPflag ARICHflag ECLflag KLMflag BadRun
19 """
20 
21 import os
22 import argparse
23 # make sure ROOT does not steal our command line arguments
24 from ROOT import PyConfig
25 
26 PyConfig.IgnoreCommandLineOptions = True # noqa
27 
28 PyConfig.StartGuiThread = False # noqa
29 # now we can import the Belle2 namespace
30 from ROOT import Belle2
31 from basf2 import B2FATAL, B2ERROR, B2DEBUG, B2INFO
32 
33 
34 def get_argument_parser():
35  """Function returning the argument parser. Done this way for the automatic
36  documentation in sphinx"""
37  # we already formatted the docstring in the beginning of the file to look the way we want it: don't
38  # reformat it but use it as description
39  parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
40  parser.add_argument("filename", help="Filename containing the run information, "
41  "one run per line with the values separated by whitespace")
42  return parser
43 
44 
45 # Doxygen complains about undocumented variables below which are not exported
46 # ... so hide them on purpose
47 # @cond this_is_a_main_block_and_not_exported
48 
49 if __name__ == "__main__":
50  parser = get_argument_parser()
51  args = parser.parse_args()
52  if not os.path.exists(args.filename):
53  B2FATAL(f"Input filename {args.filename} does not exist")
54 
55  # Number of errors
56  errors = 0
57  # Number of successfully imported run infos
58  imported = 0
59  # in reality this should probably not come from a text file but directly
60  # from a postgres query in python and skip the intermediate file.
61  with open(args.filename) as f:
62  for i, line in enumerate(f, 1):
63  # ignore empty lines
64  line = line.strip()
65  if not line:
66  continue
67 
68  # exp run runtype StartTime StopTime receivedNevent AcceptedNevent
69  # sentNevent runLength TriggerRate PXDflag SVDflag CDCflag TOPflag
70  # ARICHflag ECLflag KLMflag BadRun
71  # create RunInfo object
72  info = Belle2.RunInfo()
73  try:
74  (
75  exp, run, runtype, starttime, stoptime, receivedNevent, acceptedNevent,
76  sentNevent, runlength, trigger_rate, pxd, svd, cdc, top, arich, ecl,
77  klm, badrun
78  ) = line.split()
79  info.setExp(int(exp))
80  info.setRun(int(run))
81  info.setRunType(int(runtype))
82  info.setStartTime(int(starttime))
83  info.setStopTime(int(stoptime))
84  info.setReceivedNevent(int(receivedNevent))
85  info.setAcceptedNevent(int(acceptedNevent))
86  info.setSentNevent(int(sentNevent))
87  info.setRunLength(int(runlength))
88  info.setTriggerRate(float(trigger_rate))
89  info.setBelle2Detector(*[int(e) for e in (pxd, svd, cdc, top, arich, ecl, klm)])
90  info.setBadRun(int(badrun))
91  # convert all arguments to an int and pass them on
92 
93  B2DEBUG(100, f"Exp: {exp}\tRun: {run}\t RunType: {runtype} ...")
94  # seems the line was not formatted correctly so print an error and
95  # don't create a payload
96  except Exception as e:
97  B2ERROR(f"Problem reading line {i}: {e}. Skipping ...")
98  errors += 1
99  continue
100 
101  # finally we have a runinfo object, create payload
102  iov = Belle2.IntervalOfValidity(info.getExp(), info.getRun(), info.getExp(),
103  info.getRun())
104  Belle2.Database.Instance().storeData("RunInfo", info, iov)
105  imported += 1
106 
107  B2INFO(f"Imported {imported} RunInfo objects")
108  if errors > 0:
109  B2FATAL(f"{errors} errors occured")
110 
111 # @endcond
A class that describes the interval of experiments/runs for which an object in the database is valid.
Database object for Run Information.
Definition: RunInfo.h:24
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42