ATLAS Offline Software
Macros | Typedefs | Functions | Variables
pythonic_coracool.cxx File Reference
#include <Python.h>
#include <CoolKernel/ChannelSelection.h>
#include <CoolKernel/IFolder.h>
#include <CoolKernel/IDatabase.h>
#include "CoralBase/Attribute.h"
#include "CoralBase/AttributeList.h"
#include "CoralBase/AttributeListSpecification.h"
#include <CoraCool/CoraCoolDatabaseSvcFactory.h>
#include <CoraCool/CoraCoolDatabaseSvc.h>
#include <CoraCool/CoraCoolDatabase.h>
#include <CoraCool/CoraCoolFolder.h>
#include <CoraCool/CoraCoolObjectIter.h>
#include <CoraCool/CoraCoolObject.h>
#include <boost/bind/bind.hpp>
#include <boost/function.hpp>
#include <string>
#include <iostream>
#include "CxxUtils/checker_macros.h"

Go to the source code of this file.

Macros

#define likely(x)   __builtin_expect((x),1)
 
#define unlikely(x)   __builtin_expect((x),0)
 
#define MAKE_FETCHER(type, converter)
 

Typedefs

typedef boost::function< PyObject *(const AttributeList &)> coral_attribute_fetcher_t
 

Functions

PyObjectno_coral_conversion_available (const AttributeList &)
 
PyObjectqr_PyString_FromBlob (const coral::Blob &blob)
 
PyObjectqr_PyString_FromStdString (const string &str)
 
template<typename T >
const T & fetch_attribute_data (const coral::Attribute &A)
 
coral_attribute_fetcher_t create_attribute_fetcher (const char *name, const string &type_name)
 
bool make_fetchers (PyObject *to_fetch, const AttributeList &attribute_list, vector< coral_attribute_fetcher_t > &payload_fetchers)
 
PyObjectapply_function (PyObject *function, PyObject *object)
 
CoraCoolFolderPtr fetch_coracool_folder (IDatabasePtr cooldb, const string &folder)
 
const cool::RecordSpecification get_coracool_payload_spec (IDatabasePtr cooldb, const string &folder)
 
PyObjectmake_iov_key (PyObject *iovkey_wrapper, unsigned long long value)
 
PyObjectbrowse_coracool (IDatabasePtr cooldb, const string &folder, ValidityKey since, ValidityKey until, const ChannelSelection &cs=ChannelSelection::all(), const char *tag="", PyObject *to_fetch=NULL, PyObject *object_converter=NULL, PyObject *inner_object_converter=NULL, PyObject *iovkey_wrapper=NULL)
 

Variables

 ATLAS_NO_CHECK_FILE_THREAD_SAFETY
 

Macro Definition Documentation

◆ likely

#define likely (   x)    __builtin_expect((x),1)

Definition at line 9 of file pythonic_coracool.cxx.

◆ MAKE_FETCHER

#define MAKE_FETCHER (   type,
  converter 
)
Value:
if (type_name == #type) \
return bind(converter, \
bind(fetch_attribute_data<type>, \
bind(fetch_attribute, _1, name) \
) \
);

◆ unlikely

#define unlikely (   x)    __builtin_expect((x),0)

Definition at line 10 of file pythonic_coracool.cxx.

Typedef Documentation

◆ coral_attribute_fetcher_t

typedef boost::function<PyObject* (const AttributeList&)> coral_attribute_fetcher_t

Definition at line 53 of file pythonic_coracool.cxx.

Function Documentation

◆ apply_function()

PyObject* apply_function ( PyObject function,
PyObject object 
)
inline

Definition at line 157 of file pythonic_coracool.cxx.

158 {
159  // Convert object according to function, taking care of references
160  // If function is null, return unmodified object
161  if (!function || function == Py_None) return object;
162  PyObject *old_object = object;
163  PyObject *new_object = PyObject_CallObject(function, object);
164  Py_DECREF(old_object);
165  return new_object;
166 }

◆ browse_coracool()

PyObject* browse_coracool ( IDatabasePtr  cooldb,
const string &  folder,
ValidityKey  since,
ValidityKey  until,
const ChannelSelection &  cs = ChannelSelection::all(),
const char *  tag = "",
PyObject to_fetch = NULL,
PyObject object_converter = NULL,
PyObject inner_object_converter = NULL,
PyObject iovkey_wrapper = NULL 
)

Definition at line 194 of file pythonic_coracool.cxx.

202 {
203  // Browse CoraCool objects
204  CoraCoolFolderPtr coralFolder = fetch_coracool_folder(cooldb, folder);
205  CoraCoolObjectIterPtr objects = coralFolder->browseObjects(since, until,
206  cs, tag);
207 
208  // Number of attributes to be fetched
209  const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
210 
211  PyObject* result = PyList_New(0); // List which is returned by this function
212  bool first = true; // Is this the first iteration?
213  CoraCoolObject::const_iterator payload; // Current payload
214 
215  // List of functors which convert an Attribute into a python value
216  vector<coral_attribute_fetcher_t> payload_fetchers;
217 
218  // Loop over IoVs
219  while (objects->hasNext())
220  {
221  const CoraCoolObjectPtr& object = objects->next();
222 
223  PyObject *py_payload_tuple = PyTuple_New(object->size());
224  unsigned int payload_index = 0;
225 
226  // Loop over multiple payloads per record
227  for (payload = object->begin();
228  payload != object->end();
229  ++payload, ++payload_index)
230  {
231  if (unlikely(first))
232  {
233  // On the first iteration, figure out the types and create
234  // specialised functions for retrieving them
235  first = false;
236  if (!make_fetchers(to_fetch, *payload, payload_fetchers))
237  return NULL; // Failure, throw python exception from above
238  }
239 
240  // Create a tuple for the payload and fill it from payload fetchers
241  PyObject *py_payload = PyTuple_New(count);
242  for (Py_ssize_t i = 0; i < count; i++)
243  PyTuple_SET_ITEM(py_payload, i, payload_fetchers[i](*payload));
244 
245  py_payload = apply_function(inner_object_converter, py_payload);
246 
247  if (!py_payload)
248  {
249  // apply_function returned an error
250  // We won't return the list to python so we need to tell python it
251  // can be deleted
252  Py_DECREF(py_payload_tuple);
253  Py_DECREF(result);
254  return NULL;
255  }
256 
257  PyTuple_SET_ITEM(py_payload_tuple, payload_index, py_payload);
258  }
259 
260  PyObject *one = PyTuple_New(4);
261 
262  PyTuple_SET_ITEM(one, 0, make_iov_key(iovkey_wrapper, object->since()));
263  PyTuple_SET_ITEM(one, 1, make_iov_key(iovkey_wrapper, object->until()));
264 
265  PyTuple_SET_ITEM(one, 2, PyLong_FromLong(object->channelId()));
266 
267  // SET_ITEM steals reference, no decref needed
268  PyTuple_SET_ITEM(one, 3, py_payload_tuple);
269 
270  one = apply_function(object_converter, one);
271  if (!one)
272  {
273  // apply_function returned an error
274  // We won't return the list to python so we need to tell python it
275  // can be deleted
276  Py_DECREF(result);
277  return NULL;
278  }
279 
280  PyList_Append(result, one);
281  Py_DECREF(one);
282  }
283 
284  return result;
285 }

◆ create_attribute_fetcher()

coral_attribute_fetcher_t create_attribute_fetcher ( const char *  name,
const string &  type_name 
)

Definition at line 71 of file pythonic_coracool.cxx.

73 {
74  // Force the type of fetch_attribute, bind cannot infer it unfortunately.
75  const Attribute& (AttributeList::*fetch_attribute)(const string) const =
76  &AttributeList::operator[];
77 
78  // below, _1 falls through to leave you with a function that "looks like"
79  // [converter(attr[name].data<type>())] (Attribute& attr)
80 
81  // Test type against type_name. If true, return a functor for this type.
82  #define MAKE_FETCHER(type, converter) \
83  if (type_name == #type) \
84  return bind(converter, \
85  bind(fetch_attribute_data<type>, \
86  bind(fetch_attribute, _1, name) \
87  ) \
88  );
89 
90  // See the python c-api reference for python conversion functions
91  // Python/C API Reference Manual >> Concrete Objects Layer
92  // http://docs.python.org/c-api/concrete.html
93 
94  MAKE_FETCHER(short, PyLong_FromLong)
95  MAKE_FETCHER(int, PyLong_FromLong)
96  MAKE_FETCHER(long long, PyLong_FromLongLong)
97 
98  /*
99 
100  Types from COOL:
101  We need to build up a similar list from CORAL, it would be nice to know
102  where they came from.
103 
104  MAKE_FETCHER(Bool, PyBool_FromLong)
105  MAKE_FETCHER(Float, PyFloat_FromDouble)
106  MAKE_FETCHER(Double, PyFloat_FromDouble)
107  MAKE_FETCHER(UChar, PyLong_FromLong)
108  MAKE_FETCHER(UInt16, PyLong_FromUnsignedLong)
109  MAKE_FETCHER(UInt32, PyLong_FromUnsignedLong)
110  MAKE_FETCHER(UInt63, PyLong_FromUnsignedLongLong)
111  MAKE_FETCHER(UInt64, PyLong_FromUnsignedLongLong)
112  MAKE_FETCHER(String255, qr_PyString_FromStdString)
113  MAKE_FETCHER(String4k, qr_PyString_FromStdString)
114  MAKE_FETCHER(String64k, qr_PyString_FromStdString)
115  MAKE_FETCHER(String16M, qr_PyString_FromStdString)
116  MAKE_FETCHER(Blob16M, qr_PyString_FromBlob)
117  MAKE_FETCHER(Blob64k, qr_PyString_FromBlob)
118  */
119 
120  PyErr_Format(PyExc_RuntimeError,
121  "Type '%s' is not in type conversion table. "
122  "Please add it to pythonic_coracool.cxx. "
123  "Can't convert field '%s'.",
124  type_name.c_str(),
125  name);
127 }

◆ fetch_attribute_data()

template<typename T >
const T& fetch_attribute_data ( const coral::Attribute &  A)

Definition at line 63 of file pythonic_coracool.cxx.

64 {
65  return A.data<T>();
66 }

◆ fetch_coracool_folder()

CoraCoolFolderPtr fetch_coracool_folder ( IDatabasePtr  cooldb,
const string &  folder 
)

Definition at line 168 of file pythonic_coracool.cxx.

169 {
170  CoraCoolDatabaseSvc& corasvc = CoraCoolDatabaseSvcFactory::
171  databaseService();
172 
173  CoraCoolDatabasePtr coradb = corasvc.openDatabase(
174  cooldb->databaseId(), cooldb, true);
175 
176  return coradb->getFolder(folder);
177 }

◆ get_coracool_payload_spec()

const cool::RecordSpecification get_coracool_payload_spec ( IDatabasePtr  cooldb,
const string &  folder 
)

Definition at line 180 of file pythonic_coracool.cxx.

181 {
182  return fetch_coracool_folder(cooldb, folder)->payloadSpecification();
183 }

◆ make_fetchers()

bool make_fetchers ( PyObject to_fetch,
const AttributeList &  attribute_list,
vector< coral_attribute_fetcher_t > &  payload_fetchers 
)

Definition at line 132 of file pythonic_coracool.cxx.

136 {
137  const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
138 
139  for (Py_ssize_t i = 0; i < count; i++)
140  {
141  PyObject *py_name = PySequence_GetItem(to_fetch, i);
142  const char *name = _PyUnicode_AsString(py_name);
143  const string type = attribute_list[name].specification().typeName();
144 
146 
148  return false; // Failure: A python exception was thrown above
149 
150  payload_fetchers.push_back(pf);
151  Py_DECREF(py_name);
152  }
153 
154  return true; // Success
155 }

◆ make_iov_key()

PyObject* make_iov_key ( PyObject iovkey_wrapper,
unsigned long long  value 
)
inline

Definition at line 185 of file pythonic_coracool.cxx.

187 {
188  static const char * const argtypes = const_cast<char *>("K");
189  if (iovkey_wrapper && iovkey_wrapper != Py_None)
190  return PyObject_CallFunction(iovkey_wrapper, argtypes, value);
191  return PyLong_FromUnsignedLongLong(value);
192 }

◆ no_coral_conversion_available()

PyObject* no_coral_conversion_available ( const AttributeList &  )

Definition at line 56 of file pythonic_coracool.cxx.

56 {return NULL;}

◆ qr_PyString_FromBlob()

PyObject* qr_PyString_FromBlob ( const coral::Blob &  blob)

Definition at line 101 of file quick_retrieve.cxx.

102 {
103  const char* data = reinterpret_cast<const char*>(blob.startingAddress());
104  return PyBytes_FromStringAndSize(data, blob.size());
105 }

◆ qr_PyString_FromStdString()

PyObject* qr_PyString_FromStdString ( const string &  str)

Definition at line 107 of file quick_retrieve.cxx.

108 {
109  return PyUnicode_FromStringAndSize(str.c_str(), str.size());
110 }

Variable Documentation

◆ ATLAS_NO_CHECK_FILE_THREAD_SAFETY

ATLAS_NO_CHECK_FILE_THREAD_SAFETY

Definition at line 35 of file pythonic_coracool.cxx.

data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
get_generator_info.result
result
Definition: get_generator_info.py:21
make_fetchers
bool make_fetchers(PyObject *to_fetch, const AttributeList &attribute_list, vector< coral_attribute_fetcher_t > &payload_fetchers)
Definition: pythonic_coracool.cxx:132
LArConditions2Ntuple.objects
objects
Definition: LArConditions2Ntuple.py:64
create_attribute_fetcher
coral_attribute_fetcher_t create_attribute_fetcher(const char *name, const string &type_name)
Definition: pythonic_coracool.cxx:71
Trk::one
@ one
Definition: TrkDetDescr/TrkSurfaces/TrkSurfaces/RealQuadraticEquation.h:22
athena.value
value
Definition: athena.py:124
CoraCoolDatabaseSvc
Definition: CoraCoolDatabaseSvc.h:25
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
XMLtoHeader.count
count
Definition: XMLtoHeader.py:84
python.AthDsoLogger.argtypes
argtypes
Definition: AthDsoLogger.py:43
dq_defect_copy_defect_database.since
def since
Definition: dq_defect_copy_defect_database.py:54
dq_defect_copy_defect_database.until
def until
Definition: dq_defect_copy_defect_database.py:55
CoraCoolObject::const_iterator
AttrListVec::const_iterator const_iterator
Definition: CoraCoolObject.h:23
A
lumiFormat.i
int i
Definition: lumiFormat.py:85
unlikely
#define unlikely(x)
Definition: pythonic_coracool.cxx:9
apply_function
PyObject * apply_function(PyObject *function, PyObject *object)
Definition: pythonic_coracool.cxx:157
make_iov_key
PyObject * make_iov_key(PyObject *iovkey_wrapper, unsigned long long value)
Definition: pythonic_coracool.cxx:185
CoraCoolFolderPtr
boost::shared_ptr< CoraCoolFolder > CoraCoolFolderPtr
Definition: CoraCoolTypes.h:15
fetch_coracool_folder
CoraCoolFolderPtr fetch_coracool_folder(IDatabasePtr cooldb, const string &folder)
Definition: pythonic_coracool.cxx:168
CoraCoolDatabasePtr
boost::shared_ptr< CoraCoolDatabase > CoraCoolDatabasePtr
Definition: CoraCoolTypes.h:12
CoraCoolDatabaseSvc::openDatabase
CoraCoolDatabasePtr openDatabase(const std::string &dbconn, cool::IDatabasePtr cooldb, bool readonly=false)
Definition: CoraCoolDatabaseSvc.cxx:23
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
MAKE_FETCHER
#define MAKE_FETCHER(type, converter)
CoraCoolObjectIterPtr
boost::shared_ptr< CoraCoolObjectIter > CoraCoolObjectIterPtr
Definition: CoraCoolTypes.h:21
python.output.AtlRunQueryRoot.pf
pf
Definition: AtlRunQueryRoot.py:988
DeMoScan.first
bool first
Definition: DeMoScan.py:534
CoraCoolObjectPtr
boost::shared_ptr< CoraCoolObject > CoraCoolObjectPtr
Definition: CoraCoolTypes.h:18
CaloCondBlobAlgs_fillNoiseFromASCII.folder
folder
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:55
pickleTool.object
object
Definition: pickleTool.py:29
str
Definition: BTagTrackIpAccessor.cxx:11
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:23
PyObject
_object PyObject
Definition: IPyComponent.h:26
no_coral_conversion_available
PyObject * no_coral_conversion_available(const AttributeList &)
Definition: pythonic_coracool.cxx:56
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
CaloCondBlobAlgs_fillNoiseFromASCII.blob
blob
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:95
coral_attribute_fetcher_t
boost::function< PyObject *(const AttributeList &)> coral_attribute_fetcher_t
Definition: pythonic_coracool.cxx:53