Belle II Software development
create_RunInfo.py
1#!/usr/bin/env python3
2
3
10
11"""
12Simple script to import RunInfo from the DAQ database into conditions
13db payloads.
14As input you need a file which contains on each line the following
15fields separated by whitespace::
16
17exp run runtype StartTime StopTime receivedNevent AcceptedNevent sentNevent runLength
18TriggerRate PXDflag SVDflag CDCflag TOPflag ARICHflag ECLflag KLMflag BadRun
19"""
20
21import os
22import argparse
23# make sure ROOT does not steal our command line arguments
24from ROOT import PyConfig
25
26PyConfig.IgnoreCommandLineOptions = True # noqa
27
28PyConfig.StartGuiThread = False # noqa
29# now we can import the Belle2 namespace
30from ROOT import Belle2
31from basf2 import B2FATAL, B2ERROR, B2DEBUG, B2INFO
32
33
34def 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
45if __name__ == "__main__":
46 parser = get_argument_parser()
47 args = parser.parse_args()
48 if not os.path.exists(args.filename):
49 B2FATAL(f"Input filename {args.filename} does not exist")
50
51 # Number of errors
52 errors = 0
53 # Number of successfully imported run infos
54 imported = 0
55 # in reality this should probably not come from a text file but directly
56 # from a postgres query in python and skip the intermediate file.
57 with open(args.filename) as f:
58 for i, line in enumerate(f, 1):
59 # ignore empty lines
60 line = line.strip()
61 if not line:
62 continue
63
64 # exp run runtype StartTime StopTime receivedNevent AcceptedNevent
65 # sentNevent runLength TriggerRate PXDflag SVDflag CDCflag TOPflag
66 # ARICHflag ECLflag KLMflag BadRun
67 # create RunInfo object
68 info = Belle2.RunInfo()
69 try:
70 (
71 exp, run, runtype, starttime, stoptime, receivedNevent, acceptedNevent,
72 sentNevent, runlength, trigger_rate, pxd, svd, cdc, top, arich, ecl,
73 klm, badrun
74 ) = line.split()
75 info.setExp(int(exp))
76 info.setRun(int(run))
77 info.setRunType(int(runtype))
78 info.setStartTime(int(starttime))
79 info.setStopTime(int(stoptime))
80 info.setReceivedNevent(int(receivedNevent))
81 info.setAcceptedNevent(int(acceptedNevent))
82 info.setSentNevent(int(sentNevent))
83 info.setRunLength(int(runlength))
84 info.setTriggerRate(float(trigger_rate))
85 info.setBelle2Detector(*[int(e) for e in (pxd, svd, cdc, top, arich, ecl, klm)])
86 info.setBadRun(int(badrun))
87 # convert all arguments to an int and pass them on
88
89 B2DEBUG(100, f"Exp: {exp}\tRun: {run}\t RunType: {runtype} ...")
90 # seems the line was not formatted correctly so print an error and
91 # don't create a payload
92 except Exception as e:
93 B2ERROR(f"Problem reading line {i}: {e}. Skipping ...")
94 errors += 1
95 continue
96
97 # finally we have a runinfo object, create payload
98 iov = Belle2.IntervalOfValidity(info.getExp(), info.getRun(), info.getExp(),
99 info.getRun())
100 Belle2.Database.Instance().storeData("RunInfo", info, iov)
101 imported += 1
102
103 B2INFO(f"Imported {imported} RunInfo objects")
104 if errors > 0:
105 B2FATAL(f"{errors} errors occurred")
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:41