ATLAS Offline Software
updateCoolNtuple.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 # !!! UNTESTED !!!
6 
7 
8 from __future__ import print_function
9 
10 
11 """
12 Update beamspot ntuple for cool for runs that are in the calib loop or have exited in the last 24 hours
13 """
14 
15 print ('Updating COOL ntuple ...')
16 
17 import cx_Oracle
18 import time
19 import os
20 import sys
21 
22 from InDetBeamSpotExample import COOLUtils
23 
24 # Connect to ATLAS_COOLPROD DB for NEMO folders
25 try:
26  #oracle = cx_Oracle.connect(dsn="ATLAS_COOLREADER_U",user="ATLAS_COOL_GLOBAL_W")
27  #immediate removal; should use the autorization xml
28  oracle = cx_Oracle.connect(dsn="ATLAS_COOLPROD",user="ATLAS_COOL_READER_U",password="XXXXXXX")
29  cur = oracle.cursor()
30 except Exception as e:
31  print (e)
32  sys.exit('ERROR: Unable to connect to ATLAS_COOLPROD database')
33 
34 
35 # List of all active runs currently in calib loop (STATE=2)
36 try:
37  d = cur.execute("SELECT RUN FROM ATLAS_COOL_GLOBAL.NEMOP_RUN WHERE ACTIVE=1 AND RUNTYPE LIKE 'Physics' AND STATE=2")
38  runsCalib = [r[0] for r in sorted(d)]
39 except Exception as e:
40  print (e)
41  sys.exit('Unable to retrieve runs in calibration loop')
42 
43 # Look back 24 hours (+ half hour safety) from now (last time cron ran).
44 curtime = int(time.time())
45 limit = curtime - 24*3600+30*60
46 
47 # List all active runs exiting calib loop (STATE=4) within this time interval
48 try:
49  cur = oracle.cursor()
50  d = cur.execute("SELECT RUN FROM ATLAS_COOL_GLOBAL.NEMOP_RUN WHERE ACTIVE=1 AND RUNTYPE LIKE 'Physics' AND STATE=4 AND UTIME>%s" % limit)
51  runsExited = [r[0] for r in sorted(d)]
52 except Exception as e:
53  print (e)
54  sys.exit('Unable to retrieve runs exiting calibration loop')
55 
56 # Get lowest run in calib loop or having left since last cron
57 if not bool(runsExited + runsCalib):
58  sys.exit('No runs in calib loop or exited recently so nothing to do')
59 
60 run = min(set(runsExited + runsCalib))
61 runMax = run - 1 # Since ru is inclusive
62 status = "59,83"
63 
64 # Merge previous COOL ntuple into new one excluding runs abve this (which might have been in calib loop last time)
65 name = '/tmp/b7eamspot.' + str(os.getpid()) + '.root'
66 
67 stat = os.system("beamspotnt.py -q -f %s --status '' --ru %s -s BeamSpotNt merge ~atlidbs/nt/cool/beamspotnt-COOL-Current.root" % (name, runMax) )
68 if stat:
69  sys.exit("Unable to copy previous COOL ntuple")
70 
71 # Add in new info from cool tag for runs above this
72 tag = COOLUtils.resolveCurrentBeamSpotFolder()
73 # Fit status included in ntuple
74 
75 stat = os.system("beamspotnt.py -q -f %s --status '%s' --rl %s --fillCOOL -s BeamSpotCOOL merge %s" % (name, status, run, tag) )
76 if stat:
77  sys.exit("Unable to merge new runs into COOL ntuple")
78 
79 # Copy ntuple back (backing up previous version)
80 stat = os.system("mv ~atlidbs/nt/cool/beamspotnt-COOL-Current.root ~atlidbs/nt/cool/beamspotnt-COOL-Current.previous.root")
81 if stat:
82  sys.exit("ERROR: Unable to copy COOL ntuple to final location")
83 
84 stat = os.system("mv %s ~atlidbs/nt/cool/beamspotnt-COOL-Current.root" % name)
85 if stat:
86  sys.exit("ERROR: Unable to copy COOL ntuple to final location")
87 
88 # Bonus: Can we somehow check if resolved tag has changed since last time and if so regenerate from scratch
89 # Can we fill type of run into ntuple (e.g. pp vs Hi) - can get project tag from SOR_Params -> projects
90 
91 # Version for by time
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
str
Definition: BTagTrackIpAccessor.cxx:11
xAOD::bool
setBGCode setTAP setLVL2ErrorBits bool
Definition: TrigDecision_v1.cxx:60