{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "### This page shows aanet's print functions and json output ###\n", "\n", "Each aanet class has a `__str__()` function that is called by python's print statement. This provides a one-line description of the object, often not complete, but enough to get some idea of the contents of the object." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "id\n", "pos\n", "dir\n", "t\n", "E\n", "len\n", "lik\n", "type\n", "rec_type\n", "rec_stages\n", "status\n", "mother_id\n", "fitinf\n", "hit_ids\n", "error_matrix\n", "comment\n", "fgIsA\n" ] } ], "source": [ "c = ROOT.TClass.GetClass('Trk')\n", "for x in list(c.GetListOfDataMembers()):\n", " print x.GetName()" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading root.... /home/aart/work/root\n", "Welcome to JupyROOT 6.16/00\n", "aha : you are in ipyhon\n", "{'_dh': [u'/home/aart/work/aanet/examples/notebook'], '_sh': , '_i1': u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()', '_ii': u'', 'quit': , '__builtins__': {'bytearray': , 'IndexError': , 'all': , 'help': Type help() for interactive help, or help(object) for help about object., 'vars': , 'SyntaxError': , 'unicode': , 'UnicodeDecodeError': , 'memoryview': , 'isinstance': , 'copyright': Copyright (c) 2001-2018 Python Software Foundation.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 2000 BeOpen.com.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n", "All Rights Reserved., 'NameError': , 'BytesWarning': , 'dict': , 'input': at 0x7f04dc766320>, 'oct': , 'bin': , 'SystemExit': , 'StandardError': , 'format': , 'repr': , 'sorted': , 'False': False, 'RuntimeWarning': , 'list': , 'iter': , 'reload': , 'Warning': , '__package__': None, 'round': , 'dir': , 'cmp': , 'set': , 'bytes': , 'reduce': , 'intern': , 'issubclass': , 'Ellipsis': Ellipsis, 'EOFError': , 'locals': , 'BufferError': , 'slice': , 'FloatingPointError': , 'sum': , 'getattr': , 'abs': , 'print': , 'True': True, 'FutureWarning': , 'ImportWarning': , 'None': None, 'hash': , 'ReferenceError': , 'len': , 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n", " for supporting Python development. See www.python.org for more information., 'frozenset': , '__name__': '__builtin__', 'ord': , 'super': , 'TypeError': , 'license': Type license() to see the full license text, 'KeyboardInterrupt': , 'UserWarning': , 'filter': , 'range': , 'staticmethod': , 'SystemError': , 'BaseException': , 'pow': , 'RuntimeError': , 'float': , 'MemoryError': , 'StopIteration': , 'globals': , 'divmod': , 'enumerate': , 'apply': , 'LookupError': , 'open': , 'basestring': , 'UnicodeError': , 'zip': , 'hex': , 'long': , 'next': , 'ImportError': , 'chr': , 'xrange': , 'type': , '__doc__': \"Built-in functions, exceptions, and other objects.\\n\\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.\", 'Exception': , '__IPYTHON__': True, 'tuple': , 'UnicodeTranslateError': , 'reversed': , 'UnicodeEncodeError': , 'IOError': , 'hasattr': , 'delattr': , 'setattr': , 'raw_input': >, 'SyntaxWarning': , 'compile': , 'ArithmeticError': , 'str': , 'property': , 'dreload': , 'display': , 'GeneratorExit': , 'int': , '__import__': , 'KeyError': , 'coerce': , 'PendingDeprecationWarning': , 'file': , 'EnvironmentError': , 'unichr': , 'id': , 'OSError': , 'DeprecationWarning': , 'min': , 'UnicodeWarning': , 'execfile': , 'any': , 'complex': , 'bool': , 'get_ipython': >, 'ValueError': , 'NotImplemented': NotImplemented, 'map': , 'buffer': , 'max': , 'object': , 'TabError': , 'callable': , 'ZeroDivisionError': , 'eval': , '__debug__': True, 'IndentationError': , 'AssertionError': , 'classmethod': , 'UnboundLocalError': , 'NotImplementedError': , 'AttributeError': , 'OverflowError': }, 'In': ['', u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()'], '__builtin__': , '_oh': {}, '_iii': u'', 'exit': , 'get_ipython': >, '__': '', '_ih': ['', u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()'], '_i': u'', '__name__': '__main__', '___': '', '__package__': None, '__doc__': 'Automatically created module for IPython interactive environment', '_': '', 'Out': {}} {'_dh': [u'/home/aart/work/aanet/examples/notebook'], '_sh': , '_i1': u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()', '_ii': u'', 'quit': , '__builtins__': {'bytearray': , 'IndexError': , 'all': , 'help': Type help() for interactive help, or help(object) for help about object., 'vars': , 'SyntaxError': , 'unicode': , 'UnicodeDecodeError': , 'memoryview': , 'isinstance': , 'copyright': Copyright (c) 2001-2018 Python Software Foundation.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 2000 BeOpen.com.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n", "All Rights Reserved.\n", "\n", "Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n", "All Rights Reserved., 'NameError': , 'BytesWarning': , 'dict': , 'input': at 0x7f04dc766320>, 'oct': , 'bin': , 'SystemExit': , 'StandardError': , 'format': , 'repr': , 'sorted': , 'False': False, 'RuntimeWarning': , 'list': , 'iter': , 'reload': , 'Warning': , '__package__': None, 'round': , 'dir': , 'cmp': , 'set': , 'bytes': , 'reduce': , 'intern': , 'issubclass': , 'Ellipsis': Ellipsis, 'EOFError': , 'locals': , 'BufferError': , 'slice': , 'FloatingPointError': , 'sum': , 'getattr': , 'abs': , 'print': , 'True': True, 'FutureWarning': , 'ImportWarning': , 'None': None, 'hash': , 'ReferenceError': , 'len': , 'credits': Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands\n", " for supporting Python development. See www.python.org for more information., 'frozenset': , '__name__': '__builtin__', 'ord': , 'super': , 'TypeError': , 'license': Type license() to see the full license text, 'KeyboardInterrupt': , 'UserWarning': , 'filter': , 'range': , 'staticmethod': , 'SystemError': , 'BaseException': , 'pow': , 'RuntimeError': , 'float': , 'MemoryError': , 'StopIteration': , 'globals': , 'divmod': , 'enumerate': , 'apply': , 'LookupError': , 'open': , 'basestring': , 'UnicodeError': , 'zip': , 'hex': , 'long': , 'next': , 'ImportError': , 'chr': , 'xrange': , 'type': , '__doc__': \"Built-in functions, exceptions, and other objects.\\n\\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.\", 'Exception': , '__IPYTHON__': True, 'tuple': , 'UnicodeTranslateError': , 'reversed': , 'UnicodeEncodeError': , 'IOError': , 'hasattr': , 'delattr': , 'setattr': , 'raw_input': >, 'SyntaxWarning': , 'compile': , 'ArithmeticError': , 'str': , 'property': , 'dreload': , 'display': , 'GeneratorExit': , 'int': , '__import__': , 'KeyError': , 'coerce': , 'PendingDeprecationWarning': , 'file': , 'EnvironmentError': , 'unichr': , 'id': , 'OSError': , 'DeprecationWarning': , 'min': , 'UnicodeWarning': , 'execfile': , 'any': , 'complex': , 'bool': , 'get_ipython': >, 'ValueError': , 'NotImplemented': NotImplemented, 'map': , 'buffer': , 'max': , 'object': , 'TabError': , 'callable': , 'ZeroDivisionError': , 'eval': , '__debug__': True, 'IndentationError': , 'AssertionError': , 'classmethod': , 'UnboundLocalError': , 'NotImplementedError': , 'AttributeError': , 'OverflowError': }, 'In': ['', u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()'], '__builtin__': , '_oh': {}, '_iii': u'', 'exit': , 'get_ipython': >, '__': '', '_ih': ['', u'import aa, ROOT\\nfrom ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\\nfrom ROOT import dump\\n\\nprint Vec()\\nprint Hit()\\nprint Trk()\\nprint Evt()\\nprint Det()\\nprint Dom()\\nprint Pmt()'], '_i': u'', '__name__': '__main__', '___': '', '__package__': None, '__doc__': 'Automatically created module for IPython interactive environment', '_': '', 'Out': {}}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "loading /home/aart/work/aanet/lib/libaa.so ... ok (0.61 s.)\n", "Vec(0.000000, 0.000000, 0.000000)\n", "Hit: id=0 dom/channel/pmt=0/00 t/tot=0.000000/0, a=0.00 pos=Vec: 0.000000 0.000000 0.000000 dir=Vec: 0.000000 0.000000 0.000000\n", "a track\n", "Evt: id=0 run_id=0 #hits=0 #mc_hits=0 #trks=0 #mc_trks=0\n", "Det: id=0, 0 DOMs, -0.000 deg lon, -0.000 deg lat\n", "Dom: 0, line=0, floor=0 pos=Vec: 0.000000 0.000000 0.000000\n", "Pmt: id=0, dom_id=0, channel_id=0, pos=Vec: 0.000000 0.000000 0.000000\n" ] } ], "source": [ "import aa, ROOT\n", "from ROOT import Vec, Hit, Trk, Evt, Det, Dom, Pmt, EventFile\n", "from ROOT import dump\n", "\n", "print Vec()\n", "print Hit()\n", "print Trk()\n", "print Evt()\n", "print Det()\n", "print Dom()\n", "print Pmt()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can, objects are zero-initialized in aanet. \n", "\n", "In c++, the << operator has the same functionality, our you can use aanet's print function. Here we use ROOT's %%cpp cell magic to jit-compile some c++ code. (more on mixing c++ and python in a later notebook)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Trk: id=0, pos=0.000000,0.000000,0.000000, dir=0.000000,0.000000,0.000000, t=0.000000, E=0.000000 type=0\n", "Trk: id=0, pos=0.000000,0.000000,0.000000, dir=0.000000,0.000000,0.000000, t=0.000000, E=0.000000 type=0\n" ] } ], "source": [ "%%cpp\n", "\n", "cout << Trk() << endl;\n", "print (Trk()); // does the same thing" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--------------------------------------------------------------------------------------------------------------------------------------\n", " type name value comment \n", "--------------------------------------------------------------------------------------------------------------------------------------\n", " int id 0 track id \n", " Vec pos 0, 0, 0 postion of the track at time t \n", " Vec dir 0, 0, 0 track direction \n", " double t 0 track time (when the particle is at pos ) \n", " double E 0 Energy (either mc truth or reconstructed) \n", " double len 0 length, if applicable \n", " double lik 0 likelihood or lambda value (for aafit, lambda) \n", " int type 0 MC: particle type in PDG encoding. \n", " int rec_type 0 identifyer for the overall fitting algorithm/chain/strategy \n", " vector rec_stages 0 entries list of identifyers of succesfull fitting stages resulting in this track \n", " int status 0 MC status code \n", " int mother_id 0 MC id of the parent particle \n", " vector fitinf 0 entries place to store additional fit info \n", " vector hit_ids 0 entries list of associated hit-ids (corresponds to Hit::id). \n", " vector error_matrix 0 entries (5x5) error covariance matrix (stored as linear vector) \n", " string comment \"\" use as you like \n", " vector usr_data \">\" \n", " vector usr_names 0 entries \n", "--------------------------------------------------------------------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "dump( Trk() )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A full printout of all members of an object can be obtained via the dump() function. \n", "dump() is a slightly modified verion of ROOT's TObject::Dump(). It uses ROOT's introspection\n", "mechanism to print all members, their type and even the documentataion." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "void StdoutDumper::operator()(const TObject& obj) =>\n could not convert argument 1", "output_type": "error", "traceback": [ "\u001b[0;31m\u001b[0m", "\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdump\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mHit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: void StdoutDumper::operator()(const TObject& obj) =>\n could not convert argument 1" ] } ], "source": [ "dump( Hit() ) # hit is not anymore a tobject" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-----------------------------------------------------------------------------------------------------\n", " type name value comment \n", "-----------------------------------------------------------------------------------------------------\n", " int id 0 \n", " int version -1717986919 \n", " TTimeStamp validity_range[2] \"2019-03-01 12:27:25\" \n", " string utm_reference_elipsoid \"\" \n", " int utm_zone -1717986919 \n", " double utm_ref_easting -2.35344e-185 \n", " double utm_ref_northing -2.35344e-185 \n", " double utm_ref_z -2.35344e-185 \n", " double longitude -2.35344e-185 \n", " double latitude -2.35344e-185 \n", " double meridian_convergence_angle -2.35344e-185 \n", " map doms 0 entries DOMs by dom-id. \n", " map pmts \">\" PMT pointers by global id \n", "-----------------------------------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "dump( ROOT.Det() )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "json is a very popular format for representing and transmitting structured data. \n", "It is, for example, used for the aanet's event display, aa3d" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"id\": 0, \n", " \"det_id\": 0, \n", " \"mc_id\": 0, \n", " \"run_id\": 0, \n", " \"frame_index\": 0, \n", " \"trigger_mask\": 0, \n", " \"trigger_counter\": 0, \n", " \"overlays\": 0, \n", " \"t\": \"Tue, 06 Dec 2016 21:19:55 +0000 (GMT) +499960000 nsec\", \n", " \"hits\": [{\n", " \"id\": 0, \n", " \"dom_id\": 0, \n", " \"channel_id\": 0, \n", " \"tdc\": 0, \n", " \"tot\": 0, \n", " \"trig\": 0, \n", " \"pmt_id\": 0, \n", " \"t\": 0, \n", " \"a\": 0, \n", " \"pos\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"dir\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"pure_t\": 0, \n", " \"pure_a\": 0, \n", " \"type\": 0, \n", " \"origin\": 0, \n", " \"pattern_flags\": 0, \n", " \"usr\": [], \n", " \"usr_names\": []}, \n", " {\n", " \"id\": 0, \n", " \"dom_id\": 0, \n", " \"channel_id\": 0, \n", " \"tdc\": 0, \n", " \"tot\": 0, \n", " \"trig\": 0, \n", " \"pmt_id\": 0, \n", " \"t\": 0, \n", " \"a\": 0, \n", " \"pos\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"dir\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"pure_t\": 0, \n", " \"pure_a\": 0, \n", " \"type\": 0, \n", " \"origin\": 0, \n", " \"pattern_flags\": 0, \n", " \"usr\": [], \n", " \"usr_names\": []}, \n", " {\n", " \"id\": 0, \n", " \"dom_id\": 0, \n", " \"channel_id\": 0, \n", " \"tdc\": 0, \n", " \"tot\": 0, \n", " \"trig\": 0, \n", " \"pmt_id\": 0, \n", " \"t\": 0, \n", " \"a\": 0, \n", " \"pos\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"dir\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"pure_t\": 0, \n", " \"pure_a\": 0, \n", " \"type\": 0, \n", " \"origin\": 0, \n", " \"pattern_flags\": 0, \n", " \"usr\": [], \n", " \"usr_names\": []}], \n", " \"trks\": [{\n", " \"id\": 0, \n", " \"pos\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"dir\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"t\": 0, \n", " \"E\": 0, \n", " \"len\": 0, \n", " \"lik\": 0, \n", " \"type\": 0, \n", " \"rec_type\": 0, \n", " \"rec_stage\": 0, \n", " \"fitinf\": [], \n", " \"hit_ids\": [], \n", " \"error_matrix\": [], \n", " \"comment\": \"\", \n", " \"usr\": [], \n", " \"usr_names\": []}, \n", " {\n", " \"id\": 0, \n", " \"pos\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"dir\": { \"x\": 0, \"y\": 0, \"z\": 0}, \n", " \"t\": 0, \n", " \"E\": 0, \n", " \"len\": 0, \n", " \"lik\": 0, \n", " \"type\": 0, \n", " \"rec_type\": 0, \n", " \"rec_stage\": 0, \n", " \"fitinf\": [], \n", " \"hit_ids\": [], \n", " \"error_matrix\": [], \n", " \"comment\": \"\", \n", " \"usr\": [], \n", " \"usr_names\": []}], \n", " \"w\": [], \n", " \"w2list\": [], \n", " \"w3list\": [], \n", " \"mc_t\": -2.35344e-185, \n", " \"mc_hits\": [], \n", " \"mc_trks\": [], \n", " \"comment\": \"\", \n", " \"index\": 0, \n", " \"flags\": 0, \n", " \"usr\": [], \n", " \"usr_names\": []}\n" ] } ], "source": [ "#lets give an Evt some Hit's to make it interesting\n", "e = Evt()\n", "e.hits.resize(3)\n", "e.trks.resize(2)\n", "\n", "print ROOT.JsonDumper()(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As part of its I/O capabilities, aanet provides a function to write events in the \n", "ascii .evt format:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'e' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m\u001b[0m", "\u001b[0;31mNameError\u001b[0mTraceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mok\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mROOT\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mROOT\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcout\u001b[0m \u001b[0;34m)\u001b[0m \u001b[0;31m# assigning to ok to prevent echoing \"True\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'e' is not defined" ] } ], "source": [ "ok = ROOT.write( e, ROOT.cout ) # assigning to ok to prevent echoing \"True\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15rc1" }, "widgets": { "state": {}, "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 1 }