ATLAS Offline Software
Classes | Namespaces | Macros | Functions
dictionary.h File Reference
#include <Python.h>
#include <CoolKernel/ChannelSelection.h>
#include <CoolKernel/IObject.h>
#include <CoolKernel/IObjectIterator.h>
#include <CoolKernel/IFolder.h>
#include <CoolKernel/IDatabase.h>
#include <CoolKernel/IDatabaseSvc.h>
#include <CoolKernel/IRecordSelection.h>
#include <CoolKernel/FieldSelection.h>
#include <CoolApplication/DatabaseSvcFactory.h>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  quick_retrieve_dict::dict
 

Namespaces

 quick_retrieve_dict
 

Macros

#define register
 
#define likely(x)   __builtin_expect((x),1)
 
#define unlikely(x)   __builtin_expect((x),0)
 

Functions

const cool::RecordSpecification get_coracool_payload_spec (IDatabasePtr cooldb, const string &folder)
 
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)
 
PyObjectquick_retrieve (const IObjectIteratorPtr &objects, PyObject *object_converter, PyObject *to_fetch=NULL, const long max_records=-1, const bool with_channel=true, const bool loud=false, PyObject *iovkey_wrapper=NULL, PyObject *channel_name_mapping=NULL, const bool with_time=false, const bool as_unicode=false)
 
cool::FieldSelection * make_fieldselection (const std::string &name, const cool::StorageType::TypeId typeId, cool::FieldSelection::Relation relation, PyObject *refValue)
 
vector< const cool::IRecordSelection * > make_selection_vector ()
 

Macro Definition Documentation

◆ likely

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

Definition at line 41 of file dictionary.h.

◆ register

#define register

Definition at line 22 of file dictionary.h.

◆ unlikely

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

Definition at line 42 of file dictionary.h.

Function Documentation

◆ 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 }

◆ 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_fieldselection()

cool::FieldSelection* make_fieldselection ( const std::string &  name,
const cool::StorageType::TypeId  typeId,
cool::FieldSelection::Relation  relation,
PyObject refValue 
)

Definition at line 55 of file quick_retrieve.cxx.

60 {
61  #define MAKE_FS(type, converter) \
62  if (typeId == cool::StorageType::type) \
63  return new cool::FieldSelection(name, typeId, relation, \
64  static_cast<cool::type>(converter(refValue)));
65 
66  MAKE_FS(Bool, PyLong_AsLong)
67  MAKE_FS(Float, PyFloat_AsDouble)
68  MAKE_FS(Double, PyFloat_AsDouble)
69 
70  MAKE_FS(UChar, PyLong_AsUnsignedLong)
71  MAKE_FS(Int16, PyLong_AsLong)
72  MAKE_FS(UInt16, PyLong_AsUnsignedLong)
73  MAKE_FS(Int32, PyLong_AsLong)
74  MAKE_FS(UInt32, PyLong_AsUnsignedLong)
75  MAKE_FS(Int64, PyLong_AsLongLong)
76  MAKE_FS(UInt63, PyLong_AsUnsignedLongLong)
77 
78  MAKE_FS(String255, _PyUnicode_AsString)
79  MAKE_FS(String4k, _PyUnicode_AsString)
80  MAKE_FS(String64k, _PyUnicode_AsString)
81  MAKE_FS(String16M, _PyUnicode_AsString)
82 
83  //MAKE_FS(Blob16M, PyString_AsString)
84  //MAKE_FS(Blob64k, PyString_AsString)
85  throw (std::runtime_error("Unsupported cool type encountered in python conversion"));
86 }

◆ make_selection_vector()

vector<const cool::IRecordSelection*> make_selection_vector ( )

Definition at line 88 of file quick_retrieve.cxx.

89 {
90  return vector<const cool::IRecordSelection*>();
91 
92 }

◆ quick_retrieve()

PyObject* quick_retrieve ( const IObjectIteratorPtr &  objects,
PyObject object_converter,
PyObject to_fetch = NULL,
const long  max_records = -1,
const bool  with_channel = true,
const bool  loud = false,
PyObject iovkey_wrapper = NULL,
PyObject channel_name_mapping = NULL,
const bool  with_time = false,
const bool  as_unicode = false 
)

Definition at line 221 of file quick_retrieve.cxx.

231 {
232  IObjectIterator_Guard closeiterator_guard(objects);
233 
234  PyObject* result = PyList_New(0);
235 
236  const long long records = objects->size();
237  const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
238  const unsigned int fetch_start = 2 + with_channel + with_time;
239  bool first = true;
240  int j = 0;
241  PyObject *py_datetime_class = NULL;
242 
243  vector<payload_fetcher_t> payload_fetchers;
244 
245  while (objects->goToNext())
246  {
247  const IObject& object = objects->currentRef();
248 
249  if (unlikely(first))
250  {
251  // On the first iteration, compute the types in to_fetch, and bind
252  // them into the payload_fetchers.
253  first = false;
254  for (Py_ssize_t i = 0; i < count; i++)
255  {
256  PyObject *py_name = PySequence_GetItem(to_fetch, i);
257  const char *name = _PyUnicode_AsString(py_name);
258  const string type = (object.payload()
259  .specification()[name]
260  .storageType()
261  .name());
262 
264  as_unicode);
266  return NULL;
267  payload_fetchers.push_back(pf);
268  Py_DECREF(py_name);
269  }
270 
271  if (with_time)
272  {
273  PyObject *py_datetime_module = PyImport_ImportModule("datetime");
274  if (!py_datetime_module)
275  throw (std::runtime_error("Could not import python datetime_module"));
276 
277  py_datetime_class = PyObject_GetAttrString(py_datetime_module,
278  "datetime");
279 
280  Py_DECREF(py_datetime_module);
281  }
282  }
283 
284  PyObject *one = PyTuple_New(fetch_start + count);
285 
286  // Fetch (since, until, channel[optional])
287 
288  PyTuple_SET_ITEM(one, 0, make_iov_key(iovkey_wrapper, object.since()));
289  PyTuple_SET_ITEM(one, 1, make_iov_key(iovkey_wrapper, object.until()));
290 
291  if (with_time)
292  {
293  const ITime& t = object.insertionTime();
294  static const char * const argtypes = const_cast<char *>("iiiiiil");
295 
296  PyObject *py_record_time =
297  PyObject_CallFunction(py_datetime_class, argtypes,
298  t.year(), t.month(), t.day(),
299  t.hour(), t.minute(), t.second(), t.nanosecond() / 1000);
300 
301  PyTuple_SetItem(one, 2, py_record_time);
302  }
303 
304  if (with_channel)
305  {
306  if (channel_name_mapping)
307  {
308  // Use a channel name if it is available, otherwise fall back
309  // to standard channelid.
310  PyObject *channelId = PyLong_FromUnsignedLong(object.channelId());
311  PyObject *channelName = PyDict_GetItem(channel_name_mapping,
312  channelId);
313  if (channelName)
314  {
315  Py_INCREF(channelName);
316  Py_DECREF(channelId);
317  PyTuple_SET_ITEM(one, 2+with_time, channelName);
318  } else
319  PyTuple_SET_ITEM(one, 2+with_time, channelId);
320 
321  } else
322  PyTuple_SET_ITEM(one, 2+with_time,
323  PyLong_FromLong(object.channelId()));
324  }
325 
326  // Fetch the objects specified in to_fetch
327  for (Py_ssize_t i = 0; i < count; i++)
328  PyTuple_SET_ITEM(one, fetch_start + i, payload_fetchers[i](object));
329 
330  one = apply_function(object_converter, one);
331  if (!one)
332  {
333  // apply_function returned an error
334  // We won't return the list to python so we need to tell python it
335  // can be deleted
336  Py_DECREF(result);
337  return NULL;
338  }
339 
340  PyList_Append(result, one);
341  Py_DECREF(one);
342 
343  // Print status if loud is active
344  if (unlikely(loud && ((++j) % 1000 == 0)))
345  {
346  cout << "Progress: " << j << " / " << records << "\r";
347  cout.flush();
348  }
349 
350  if (unlikely(max_records > 0 && j >= max_records))
351  break;
352  }
353 
354  if (loud)
355  cout << "Done fetching " << j << " records.\x1B[K" << endl;
356 
357  if (py_datetime_class)
358  {
359  Py_DECREF(py_datetime_class);
360  }
361 
362  return result;
363 }
create_payload_fetcher
payload_fetcher_t create_payload_fetcher(const char *name, const string &type_name, bool string_to_unicode=false)
Definition: quick_retrieve.cxx:135
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
unlikely
#define unlikely(x)
Definition: quick_retrieve.cxx:6
Trk::one
@ one
Definition: TrkDetDescr/TrkSurfaces/TrkSurfaces/RealQuadraticEquation.h:22
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
MAKE_FS
#define MAKE_FS(type, converter)
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
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: quick_retrieve.cxx:205
apply_function
PyObject * apply_function(PyObject *function, PyObject *object)
Definition: pythonic_coracool.cxx:157
CaloCondBlobAlgs_fillNoiseFromASCII.channelId
channelId
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:121
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
payload_fetcher_t
boost::function< PyObject *(const IObject &)> payload_fetcher_t
Definition: quick_retrieve.cxx:95
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
no_conversion_available
PyObject * no_conversion_available(const IObject &)
Definition: quick_retrieve.cxx:98
PixelModuleFeMask_create_db.payload
string payload
Definition: PixelModuleFeMask_create_db.py:69
make_iov_key
PyObject * make_iov_key(PyObject *iovkey_wrapper, unsigned long long value)
Definition: quick_retrieve.cxx:196
IObjectIterator_Guard
Definition: quick_retrieve.cxx:188
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
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:23
PyObject
_object PyObject
Definition: IPyComponent.h:26