ATLAS Offline Software
HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
12 #include "TrigPSC/Utils.h"
13 #include "TrigPSC/PscIssues.h"
15 
16 #undef _POSIX_C_SOURCE
17 #include <Python.h>
18 #include <errno.h>
19 
20 //--------------------------------------------------------------------------------
21 // Python helpers
22 //--------------------------------------------------------------------------------
23 
24 bool psc::Utils::execPython (const std::string& pyCmd)
25 {
26  ERS_DEBUG(1, pyCmd );
27  if ( PyRun_SimpleString(const_cast<char*>(pyCmd.c_str())) != 0 ) {
28  ERS_PSC_ERROR("Error executing " << pyCmd);
29  return false;
30  }
31  return true;
32 }
33 
34 
35 bool psc::Utils::execPython (const std::vector<std::string>& pyCmds)
36 {
37  std::vector<std::string>::const_iterator iter;
38  for (iter = pyCmds.begin(); iter != pyCmds.end(); ++iter) {
39  if ( !psc::Utils::execPython(*iter) ) return false;
40  }
41  return true;
42 }
43 
44 
45 bool psc::Utils::pyInclude (const std::string& pyFileName)
46 {
47  std::ostringstream oss;
48  oss << "from AthenaCommon.Include import include" << std::endl;
49  oss << "include(\"" << pyFileName << "\")";
50  return psc::Utils::execPython(oss.str());
51 }
52 
53 
54 bool psc::Utils::execFile (const std::string& pyFileName)
55 {
56  if (pyFileName.find(".py")!=std::string::npos)
57  return pyInclude(pyFileName);
58  else
59  return psc::Utils::execPython("import " + pyFileName);
60 }
61 
62 //--------------------------------------------------------------------------------
63 // ScopeTimer class
64 //--------------------------------------------------------------------------------
65 
67  m_descr(descr),
68  m_t1(std::chrono::system_clock::now()),
69  m_running(true)
70 {
71  std::tm lt;
72  auto t = std::chrono::system_clock::to_time_t(m_t1);
73  localtime_r(&t, &lt);
74  ERS_LOG( m_descr << " started at time: " << std::put_time(&lt, "%Y-%m-%d %H:%M:%S") );
75 }
76 
78 {
80  auto dt_ms = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - m_t1);
81  ERS_LOG( m_descr << " finished. Time used [ms] = " << dt_ms.count() );
82  m_running = false;
83 }
PscIssues.h
ERS Issues for PSC.
psc::Utils::ScopeTimer::ScopeTimer
ScopeTimer(const std::string &descr="Timer")
Create timer and start timing.
Definition: HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx:66
psc::Utils::pyInclude
bool pyInclude(const std::string &pyFileName)
Include a python file via AthenaCommon.Include.
Definition: HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx:45
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
psc::Utils::ScopeTimer::m_descr
std::string m_descr
description of timer
Definition: HLT/Trigger/TrigControl/TrigPSC/TrigPSC/Utils.h:74
ERS_PSC_ERROR
#define ERS_PSC_ERROR(message)
Definition: PscIssues.h:51
python.handimod.now
now
Definition: handimod.py:675
python.TransformConfig.descr
descr
print "%s.properties()" % self.__name__
Definition: TransformConfig.py:360
psc::Utils::execPython
bool execPython(const std::string &pyCmd)
Execute a python command in the python interpreter.
Definition: HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx:24
PathResolver.h
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
Utils.h
Some helpers for the PSC.
psc::Utils::execFile
bool execFile(const std::string &pyFileName)
Execute a python file (via include or import)
Definition: HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx:54
psc::Utils::ScopeTimer::stop
void stop()
Stop timer manually.
Definition: HLT/Trigger/TrigControl/TrigPSC/src/Utils.cxx:77
psc::Utils::ScopeTimer::m_t1
std::chrono::system_clock::time_point m_t1
start time
Definition: HLT/Trigger/TrigControl/TrigPSC/TrigPSC/Utils.h:75