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 198 of file pythonic_coracool.cxx.

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

◆ get_coracool_payload_spec()

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

Definition at line 184 of file pythonic_coracool.cxx.

185 {
186  return fetch_coracool_folder(cooldb, folder)->payloadSpecification();
187 }

◆ 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 #if PY_VERSION_HEX < 0x03000000
79  MAKE_FS(String255, PyString_AsString)
80  MAKE_FS(String4k, PyString_AsString)
81  MAKE_FS(String64k, PyString_AsString)
82  MAKE_FS(String16M, PyString_AsString)
83 #else
84  MAKE_FS(String255, _PyUnicode_AsString)
85  MAKE_FS(String4k, _PyUnicode_AsString)
86  MAKE_FS(String64k, _PyUnicode_AsString)
87  MAKE_FS(String16M, _PyUnicode_AsString)
88 #endif
89 
90  //MAKE_FS(Blob16M, PyString_AsString)
91  //MAKE_FS(Blob64k, PyString_AsString)
92  throw (std::runtime_error("Unsupported cool type encountered in python conversion"));
93 }

◆ make_selection_vector()

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

Definition at line 95 of file quick_retrieve.cxx.

96 {
97  return vector<const cool::IRecordSelection*>();
98 
99 }

◆ 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 236 of file quick_retrieve.cxx.

246 {
247  IObjectIterator_Guard closeiterator_guard(objects);
248 
249  PyObject* result = PyList_New(0);
250 
251  const long long records = objects->size();
252  const Py_ssize_t count = to_fetch ? PySequence_Size(to_fetch) : 0;
253  const unsigned int fetch_start = 2 + with_channel + with_time;
254  bool first = true;
255  int j = 0;
256  PyObject *py_datetime_class = NULL;
257 
258  vector<payload_fetcher_t> payload_fetchers;
259 
260  while (objects->goToNext())
261  {
262  const IObject& object = objects->currentRef();
263 
264  if (unlikely(first))
265  {
266  // On the first iteration, compute the types in to_fetch, and bind
267  // them into the payload_fetchers.
268  first = false;
269  for (Py_ssize_t i = 0; i < count; i++)
270  {
271  PyObject *py_name = PySequence_GetItem(to_fetch, i);
272 #if PY_VERSION_HEX < 0x03000000
273  const char *name = PyString_AsString(py_name);
274 #else
275  const char *name = _PyUnicode_AsString(py_name);
276 #endif
277  const string type = (object.payload()
278  .specification()[name]
279  .storageType()
280  .name());
281 
283  as_unicode);
285  return NULL;
286  payload_fetchers.push_back(pf);
287  Py_DECREF(py_name);
288  }
289 
290  if (with_time)
291  {
292  PyObject *py_datetime_module = PyImport_ImportModule("datetime");
293  if (!py_datetime_module)
294  throw (std::runtime_error("Could not import python datetime_module"));
295 
296  py_datetime_class = PyObject_GetAttrString(py_datetime_module,
297  "datetime");
298 
299  Py_DECREF(py_datetime_module);
300  }
301  }
302 
303  PyObject *one = PyTuple_New(fetch_start + count);
304 
305  // Fetch (since, until, channel[optional])
306 
307  PyTuple_SET_ITEM(one, 0, make_iov_key(iovkey_wrapper, object.since()));
308  PyTuple_SET_ITEM(one, 1, make_iov_key(iovkey_wrapper, object.until()));
309 
310  if (with_time)
311  {
312  const ITime& t = object.insertionTime();
313  static const char * const argtypes = const_cast<char *>("iiiiiil");
314 
315  PyObject *py_record_time =
316  PyObject_CallFunction(py_datetime_class, argtypes,
317  t.year(), t.month(), t.day(),
318  t.hour(), t.minute(), t.second(), t.nanosecond() / 1000);
319 
320  PyTuple_SetItem(one, 2, py_record_time);
321  }
322 
323  if (with_channel)
324  {
325  if (channel_name_mapping)
326  {
327  // Use a channel name if it is available, otherwise fall back
328  // to standard channelid.
329  PyObject *channelId = PyLong_FromUnsignedLong(object.channelId());
330  PyObject *channelName = PyDict_GetItem(channel_name_mapping,
331  channelId);
332  if (channelName)
333  {
334  Py_INCREF(channelName);
335  Py_DECREF(channelId);
336  PyTuple_SET_ITEM(one, 2+with_time, channelName);
337  } else
338  PyTuple_SET_ITEM(one, 2+with_time, channelId);
339 
340  } else
341  PyTuple_SET_ITEM(one, 2+with_time,
342  PyLong_FromLong(object.channelId()));
343  }
344 
345  // Fetch the objects specified in to_fetch
346  for (Py_ssize_t i = 0; i < count; i++)
347  PyTuple_SET_ITEM(one, fetch_start + i, payload_fetchers[i](object));
348 
349  one = apply_function(object_converter, one);
350  if (!one)
351  {
352  // apply_function returned an error
353  // We won't return the list to python so we need to tell python it
354  // can be deleted
355  Py_DECREF(result);
356  return NULL;
357  }
358 
359  PyList_Append(result, one);
360  Py_DECREF(one);
361 
362  // Print status if loud is active
363  if (unlikely(loud && ((++j) % 1000 == 0)))
364  {
365  cout << "Progress: " << j << " / " << records << "\r";
366  cout.flush();
367  }
368 
369  if (unlikely(max_records > 0 && j >= max_records))
370  break;
371  }
372 
373  if (loud)
374  cout << "Done fetching " << j << " records.\x1B[K" << endl;
375 
376  if (py_datetime_class)
377  {
378  Py_DECREF(py_datetime_class);
379  }
380 
381  return result;
382 }
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:150
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:56
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
MAKE_FS
#define MAKE_FS(type, converter)
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
python.AthDsoLogger.argtypes
argtypes
Definition: AthDsoLogger.py:44
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:92
unlikely
#define unlikely(x)
Definition: pythonic_coracool.cxx:9
apply_function
PyObject * apply_function(PyObject *function, PyObject *object)
Definition: quick_retrieve.cxx:220
apply_function
PyObject * apply_function(PyObject *function, PyObject *object)
Definition: pythonic_coracool.cxx:161
CaloCondBlobAlgs_fillNoiseFromASCII.channelId
channelId
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:122
make_iov_key
PyObject * make_iov_key(PyObject *iovkey_wrapper, unsigned long long value)
Definition: pythonic_coracool.cxx:189
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:172
payload_fetcher_t
boost::function< PyObject *(const IObject &)> payload_fetcher_t
Definition: quick_retrieve.cxx:102
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
no_conversion_available
PyObject * no_conversion_available(const IObject &)
Definition: quick_retrieve.cxx:105
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:211
IObjectIterator_Guard
Definition: quick_retrieve.cxx:203
CoraCoolObjectIterPtr
boost::shared_ptr< CoraCoolObjectIter > CoraCoolObjectIterPtr
Definition: CoraCoolTypes.h:21
python.output.AtlRunQueryRoot.pf
pf
Definition: AtlRunQueryRoot.py:988
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
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:56
pickleTool.object
object
Definition: pickleTool.py:30
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
PyObject
_object PyObject
Definition: IPyComponent.h:26