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