nd 2 Coding Sprint

1. The ideas behind GammaLib and ctools 2. GammaLib and ctools concepts 3. Coding for GammaLib and ctools 4. Current status 5. Goals of this sprint

Jürgen Knödlseder (IRAP) 1. The ideas behind GammaLib and ctools Commonalies

MeV keV GeV TeV All gamma-ray telescopes measure individual photons as events

Events are characterized by 3 fundamental parameters: • Localisaon • Energy • Time

consideraon 1: In principle it should be possible to handle events from all gamma-ray telescopes using a common soware framework

2nd ctools and gammalib coding sprint 27-31 January 2014 3 (Jürgen Knödlseder) Commonalies

Gamma-ray analysis oen consists of (maximum likelihood) fing of models to the measured event data

consideraon 2: Use maximum likelihood model fing as central element of the analysis framework

Exisng high-energy analysis frameworks share a number of common features: • All data are in FITS format (OGIP standard) • Wide usage of IRAF parameter interface (INTEGRAL, Swi, Chandra, Fermi, ools, …) • Modular soware based on executables doing a simple and well defined job

consideraon 3: Build a toolbox that provides all the bricks that are required to build any kind of analysis executable

… is the client that uses the bricks provided by

CTA specific generic … to build a set of analysis executables for CTA (and alike) (Note: from GammaLib you could also build xtools, ytools, ztools, …) 2nd ctools and gammalib coding sprint 27-31 January 2014 4 (Jürgen Knödlseder) Further consideraons (definitely biased by own experience)

consideraon 4: Avoid dependencies (they make the life very difficult for installaon and maintenance)

consideraon 5: Don’t chose a reference plaorm, try to support as many plaorms as possible, and parcularly those used by the community at large (enables broad usage, reduces maintenance costs)

consideraon 6: Soware should be open and free of charge (prerequisite for broad acceptance, eases re-use, allows for community contribuons)

2nd ctools and gammalib coding sprint 27-31 January 2014 5 (Jürgen Knödlseder) … but it’s a challenge (a Paradigm shi at least for TeV gamma-ray data analysis)

2nd ctools and gammalib coding sprint 27-31 January 2014 6 (Jürgen Knödlseder) 2. GammaLib and ctools concepts GammaLib overview Abstract

Nave

Compliant Extendable

2nd ctools and gammalib coding sprint 27-31 January 2014 8 (Jürgen Knödlseder) It’s all C++ classes

2nd ctools and gammalib coding sprint 27-31 January 2014 9 (Jürgen Knödlseder) Abstract C++ classes for abstract interfaces

2nd ctools and gammalib coding sprint 27-31 January 2014 10 (Jürgen Knödlseder) A ctool is an executable and a class

ctlike is a C++ class …

… or as a C++ class in a C++ program … that can be used as a Python class in a script … (used to build the ctlike executable)

2nd ctools and gammalib coding sprint 27-31 January 2014 11 (Jürgen Knödlseder) Running a ctool executable

CTA event list simulator

2nd ctools and gammalib coding sprint 27-31 January 2014 12 (Jürgen Knödlseder) Wrapping C++ in Python: SWIG hp://www.swig.org/

ctlike.hpp ctlike.i

$ swig -c++ -python -Wall ctlike.i! ctlike.py! ctlike_wrap.cpp! $ gcc ctlike_wrap.cpp! 2nd ctools and gammalib coding sprint 27-31 January 2014 13 (Jürgen Knödlseder) Using GammaLib in Python

… same story for ctools

2nd ctools and gammalib coding sprint 27-31 January 2014 14 (Jürgen Knödlseder) A cscript is a Python script looking like a ctool

2nd ctools and gammalib coding sprint 27-31 January 2014 15 (Jürgen Knödlseder) The overall picture Python C++ cspull! ctobssim! cssens! ctbin! cstsdist! ctselect! ctlike! ctmodel! ctskymap!

_ctools.so! libctools.so! ctools.py! swig

gammalib/! _app.so !app.py! _base.so !base.py! libgamma.so! _com.so !com.py! swig … ! !!…!

2nd ctools and gammalib coding sprint 27-31 January 2014 16 (Jürgen Knödlseder) What should I do if …

… I need a new spectral model? Add a new spectral model class to the GammaLib model module.

… I need a new background model for CTA? Add a new background model class to the GammaLib CTA interface module.

… I want a tool that generates CTA exposure maps? Create a new ctool that uses the CTA response funcons in GammaLib for exposure map computaon.

… I want to implement an analysis workflow or pipeline? Create a Python script that uses the ctools and gammalib Python modules.

… I want to test a new idea (e.g. create a ring background generator)? Create a new cscript that uses the gammalib Python module.

General rule: All generic and reusable code goes in GammaLib, code that is only needed for one specific task goes in ctools. Quick coding is beer done by a cscript.

2nd ctools and gammalib coding sprint 27-31 January 2014 17 (Jürgen Knödlseder) 3. Coding for GammaLib and ctools

- Infrastructure - Communies

• Use official releases • Code and documentaon at Users hp://gammalib..net/ @gammalib hp://cta.irap.omp.eu/ctools/index.html

Developers Managers

• Use git trunk (devel) • Use git • Forge including developer documentaon at • Forge, Jenkins, Sonar hps://cta-.irap.omp.eu/projects/gammalib • Unit tests (make check) hps://cta-redmine.irap.omp.eu/projects/ctools • Documentaon as for developers

2nd ctools and gammalib coding sprint 27-31 January 2014 19 (Jürgen Knödlseder) Our Forge

2nd ctools and gammalib coding sprint 27-31 January 2014 20 (Jürgen Knödlseder) Issue tracking

Please track your work, problems, errors, etc.! • Bug • Acon (something to do) • Feature (a new thing, may be composed of several acons) • Change request 2nd ctools and gammalib coding sprint 27-31 January 2014 21 (Jürgen Knödlseder) Work planning

Collects all features of the sprint

2nd ctools and gammalib coding sprint 27-31 January 2014 22 (Jürgen Knödlseder) Task board

Acons

Graphical representaon of all features and acons, their status and assigned persons Features

2nd ctools and gammalib coding sprint 27-31 January 2014 23 (Jürgen Knödlseder) Repository organizaon The GammaLib and ctools source code are version controlled in two git repositories at IRAP hps://cta-git.irap.omp.eu/gammalib hps://cta-git.irap.omp.eu/ctools

Protected branches: master – last release release – release preparaon devel – developer branch integraon – feature integraon

Other branches: can be setup by any developer as required. Will be regularly cleaned-up aer pulling in changes.

Gammalib and ctools development can also be based on Github: https://github.com/gammalib/gammalib! https://github.com/ctools/ctools! • read-only repositories • synchronized with IRAP repositories

2nd ctools and gammalib coding sprint 27-31 January 2014 24 (Jürgen Knödlseder) A typical git workflow hps://cta-redmine.irap.omp.eu/projects/gammalib//Git_workflow_for_GammaLib

2nd ctools and gammalib coding sprint 27-31 January 2014 25 (Jürgen Knödlseder) Some frequently asked quesons

Should I use IRAP git or github for development? Whatever you prefer. Both repositories should be 100% synchronized at any me.

Which branch should I start from? Always branch from devel. Never branch from master.

How oen should I commit? Whenever you feel necessary. Note that the more oen you commit the beer all changes are tracked and the easier it is to go back to a certain stage of your code development. However, before comming, please check that the code at least compiles (best make also a unit test).

Why can’t I push to master, release, devel or integraon? These branches are protected from pushing. Only the integraon manager is allowed to push to them. See it from the good side: this puts a lower work burden on your side, and prevents you from destroying the repository.

2nd ctools and gammalib coding sprint 27-31 January 2014 26 (Jürgen Knödlseder) Code quality (connuous integraon)

Connuous integraon (build, check, install, analysis)

Mul-plaorm, mul-compiler, 32/64 Bit, Python version, swig version, …

Virtual box with relevant OS

2nd ctools and gammalib coding sprint 27-31 January 2014 27 (Jürgen Knödlseder) Unit tesng

2nd ctools and gammalib coding sprint 27-31 January 2014 28 (Jürgen Knödlseder) Code analysis

2nd ctools and gammalib coding sprint 27-31 January 2014 29 (Jürgen Knödlseder) Hotspots

2nd ctools and gammalib coding sprint 27-31 January 2014 30 (Jürgen Knödlseder) Evoluons

2nd ctools and gammalib coding sprint 27-31 January 2014 31 (Jürgen Knödlseder) Documentaon

Code documentaon User documentaon

Extracts code documentaon directly from Generates documents from reStructuredText source files. Latest version of devel branch files (markup language). Latest version of online at devel branch online at hp://gammalib.sourceforge.net/doxygen/ hp://gammalib.sourceforge.net/ hp://cta.irap.omp.eu/ctools/doxygen/ hp://cta.irap.omp.eu/ctools/

2nd ctools and gammalib coding sprint 27-31 January 2014 32 (Jürgen Knödlseder) Geng help

Python C++

2nd ctools and gammalib coding sprint 27-31 January 2014 33 (Jürgen Knödlseder) 3. Coding for GammaLib and ctools

- Under the hood - Code organisaon

hps://cta-git.irap.omp.eu/gammalib hps://cta-git.irap.omp.eu/ctools gammalib/ ctools/ dev Developer material caldb Calibraon data doc Code and user documentaon doc Code and user documentaon examples Example code examples Example code include Core* header files (.hpp) m4 Code configuraon macros inst Instrument modules models Source and background models m4 Code configuraon macros pyext Python extension files (.i) pyext Core* Python extension files (.i) scripts cscripts and Python scripts src Core* source files (.cpp) src ctools test Code for unit tesng test Code for unit tesng *Core means instrument independent code

2nd ctools and gammalib coding sprint 27-31 January 2014 35 (Jürgen Knödlseder) Configuring GammaLib hps://cta-redmine.irap.omp.eu/projects/gammalib/wiki/Contribung_to_GammaLib

generates configure script from configure.ac configures GammaLib for your system

2nd ctools and gammalib coding sprint 27-31 January 2014 36 (Jürgen Knödlseder) Building, checking, installing hps://cta-redmine.irap.omp.eu/projects/gammalib/wiki/Contribung_to_GammaLib compiles code (using 4 cores at maximum) compiles and executes unit test code Note: if you switch branches you may need to issue $ make clean! $ make –j4! for a full recompliaon of the library. Also, if some symbols are missing when doing a unit check, make a full recompilaon (and get some coffee).

installs code (copy of build result)

2nd ctools and gammalib coding sprint 27-31 January 2014 37 (Jürgen Knödlseder) Observaon handling hp://gammalib.sourceforge.net/user_manual/modules/obs.html

2nd ctools and gammalib coding sprint 27-31 January 2014 38 (Jürgen Knödlseder) Model handling hp://gammalib.sourceforge.net/user_manual/modules/model.html

2nd ctools and gammalib coding sprint 27-31 January 2014 39 (Jürgen Knödlseder) Sky maps, coordinates and regions hp://gammalib.sourceforge.net/user_manual/modules/sky.html

2nd ctools and gammalib coding sprint 27-31 January 2014 40 (Jürgen Knödlseder) Creaon of applicaons hp://gammalib.sourceforge.net/user_manual/modules/app.html

2nd ctools and gammalib coding sprint 27-31 January 2014 41 (Jürgen Knödlseder) Linear algebra hp://gammalib.sourceforge.net/user_manual/modules/linalg.html

2nd ctools and gammalib coding sprint 27-31 January 2014 42 (Jürgen Knödlseder) Opmizers hp://gammalib.sourceforge.net/user_manual/modules/opt.html

2nd ctools and gammalib coding sprint 27-31 January 2014 43 (Jürgen Knödlseder) FITS file interface hp://gammalib.sourceforge.net/user_manual/modules/fits.html

2nd ctools and gammalib coding sprint 27-31 January 2014 44 (Jürgen Knödlseder) XML file interface hp://gammalib.sourceforge.net/user_manual/modules/xml.html

2nd ctools and gammalib coding sprint 27-31 January 2014 45 (Jürgen Knödlseder) Xspec and virtual observatory interfaces hp://gammalib.sourceforge.net/user_manual/modules/xspec.html

hp://gammalib.sourceforge.net/user_manual/modules/vo.html

2nd ctools and gammalib coding sprint 27-31 January 2014 46 (Jürgen Knödlseder) Support funcons and classes hp://gammalib.sourceforge.net/user_manual/modules/support.html

2nd ctools and gammalib coding sprint 27-31 January 2014 47 (Jürgen Knödlseder) CTA interface hp://gammalib.sourceforge.net/user_manual/modules/cta.html

2nd ctools and gammalib coding sprint 27-31 January 2014 48 (Jürgen Knödlseder) Fermi/LAT interface hp://gammalib.sourceforge.net/user_manual/modules/lat.html

20 MeV – 300 GeV 2008 -

2nd ctools and gammalib coding sprint 27-31 January 2014 49 (Jürgen Knödlseder) COMPTEL interface hp://gammalib.sourceforge.net/user_manual/modules/comptel.html

0.75 – 30 MeV 1991 - 2000

2nd ctools and gammalib coding sprint 27-31 January 2014 50 (Jürgen Knödlseder) Mul-wavelength interface hp://gammalib.sourceforge.net/user_manual/modules/mwl.html

2nd ctools and gammalib coding sprint 27-31 January 2014 51 (Jürgen Knödlseder) Constants

GMath.hpp

GTools.hpp

2nd ctools and gammalib coding sprint 27-31 January 2014 52 (Jürgen Knödlseder) Funcons GTools.hpp GMath.hpp

2nd ctools and gammalib coding sprint 27-31 January 2014 53 (Jürgen Knödlseder) 3. Coding for GammaLib and ctools

- Coding convenons - Why coding convenons?

From the Java Programming Language, Sun Microsystems: Code convenons are important to programmers for a number of reasons: • 40%-80% of the lifeme cost of a piece of soware goes to maintenance. • Hardly any soware is maintained for its whole life by the original author. • Code convenons improve the readability of the soware, allowing engineers to understand new code more quickly and thoroughly. • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

Is there a unique and best C++ style? Coding style can affect performance and even code correctness, but there are also rules that mainly affect readability (indentaon, placement of brackets, etc.), hence coding style is also a maer of taste (you can certainly argue endless nights about the best coding style).

Take home message: GammaLib and ctools are both developed following coding convenons. Please follow them as good as you can as they may prevent errors, can lead to beer code, and will help newcomers to understand the code base.

2nd ctools and gammalib coding sprint 27-31 January 2014 55 (Jürgen Knödlseder) General coding rules (apply to GammaLib and ctools)

Code format Use C++98 standard • Blocks are indented by 4 characters Do not use C++11 features • No tabs, use spaces • Try to not exceed 80 characters per line • Separate by spaces, e.g. Curly opening bracket at end Funcon format Block format

Curly opening bracket at new line

Always use brackets if a block splits over more than a single line Code alignment

2nd ctools and gammalib coding sprint 27-31 January 2014 56 (Jürgen Knödlseder) C++ classes (.hpp file) - definion File name and short class descripon Dates from creaon to last eding; Person who created the file inially Copyright (GPL 3)

File name, brief descripon, person who created file (Doxygen syntax)

Includes (C, C++ using < >; GammaLib using “ “)

Class descripon (Doxygen syntax)

Protecon Public Constructors Operators Methods

Protected or private Methods Members

2nd ctools and gammalib coding sprint 27-31 January 2014 57 (Jürgen Knödlseder) C++ classes (.cpp file) - implementaon

Header equivalent to .hpp file

Makes compile configuraon available to the source code file.

Method names used in excepons

Compile opons Compile opons for debugging Global constants

2nd ctools and gammalib coding sprint 27-31 January 2014 58 (Jürgen Knödlseder) Python classes (.i file) - extension

Header equivalent to .hpp file

SWIG direcve to include corresponding .hpp file (and whatever else is needed for compilaon)

Basically a copy of the public class definion from the .hpp file without: • operators • print() method • const versions of methods

Extensions to the class only available in Python

2nd ctools and gammalib coding sprint 27-31 January 2014 59 (Jürgen Knödlseder) Add [] operator to Python interface

2nd ctools and gammalib coding sprint 27-31 January 2014 60 (Jürgen Knödlseder) More reading hp://gammalib.sourceforge.net/coding/

2nd ctools and gammalib coding sprint 27-31 January 2014 61 (Jürgen Knödlseder) Benchmarks 2.66 GHz Intel Core i7 Mac OS X 10.6.8, execung 100000000 (one hundred million) mes a given computaon (execuon mes in seconds)

Top mistake!

Was true 20 years ago, sll is true.

2nd ctools and gammalib coding sprint 27-31 January 2014 62 (Jürgen Knödlseder) Benchmarks 2.66 GHz Intel Core i7 Mac OS X 10.6.8, execung 100000000 (one hundred million) mes a given computaon (execuon mes in seconds)

Top mistake!

Was true 20 years ago, sll is true.

2nd ctools and gammalib coding sprint 27-31 January 2014 63 (Jürgen Knödlseder) Benchmarks for various systems double precision computaons

32 Bit 64 Bit 64 Bit 64 Bit 64 Bit 64 Bit Intel AMD AMD intel Intel (VM) Intel (VM)

2nd ctools and gammalib coding sprint 27-31 January 2014 64 (Jürgen Knödlseder) Benchmarks for various systems single precision computaons

32 Bit 64 Bit 64 Bit 64 Bit 64 Bit 64 Bit Intel AMD AMD intel Intel (VM) Intel (VM)

2nd ctools and gammalib coding sprint 27-31 January 2014 65 (Jürgen Knödlseder) Benchmarks lessons hps://cta-redmine.irap.omp.eu/projects/gammalib/wiki/Computaon_Benchmarks

Use double precision Double precision computaons are generally not slower than single precision computaons, but on some systems, single precision are much slower than doubles.

Never use pow(x, 2), always use x*x Don’t: y = pow(x,2) This brings typically a factor of 4 in speed increase. Do: y = x*x

If possible, use mulplicaon instead of division Don’t: y = x/2.0 This brings typically a factor of 2 in speed increase. Do: y = 0.5*x

Use std::sin(), std::cos(), etc. instead of sin(), cos(), etc. Execuon speed of trigonometric funcon depends on how they are called because the compiler generates different codes; the std:: versions have never shown to be slow.

… and of course: never make the same operaon twice! Break down your formula into pieces and store them in variables that can be re-used.

… and: if you want speed, buy a Mac J

2nd ctools and gammalib coding sprint 27-31 January 2014 66 (Jürgen Knödlseder) 3. Coding for GammaLib and ctools

- Tesng - Unit tesng

Code tesng is an integrated feature of gammalib (and ctools), but not all code is yet covered … make check! Each dot is an individual test case: . = okay F = failure (unexpected result) E = error (unexpected behaviour, e.g. seg. fault)

Note: for automake >= 1.13, console dumps end up in test/test_*.log!

2nd ctools and gammalib coding sprint 27-31 January 2014 68 (Jürgen Knödlseder) Unit tesng

Code tesng is an integrated feature of gammalib (and ctools), but not all code is yet covered … make check! Each dot is an individual test case: . = okay F = failure (unexpected result) E = error (unexpected behaviour, e.g. seg. fault)

Note: for automake >= 1.13, console dumps end up in test/test_*.log!

2nd ctools and gammalib coding sprint 27-31 January 2014 69 (Jürgen Knödlseder) Test driven development You may give it a try …

2nd ctools and gammalib coding sprint 27-31 January 2014 70 (Jürgen Knödlseder) How to write a new C++ unit test? As C++ class! (see inst/test/test_CTA.hpp and inst/test/test_CTA.cpp)

Create a class that derived from GTestSuite Append GTestSuite to container, run tests and save results

Implement set method

2nd ctools and gammalib coding sprint 27-31 January 2014 71 (Jürgen Knödlseder) And in Python?

Allocate test class, append to container, Create class derived from GPythonTestSuite run tests and save results

2nd ctools and gammalib coding sprint 27-31 January 2014 72 (Jürgen Knödlseder) Why can’t I see the test results?

Test results are wrien as XML files that can be found in the test/reports directory. XML files are in JUnit format that can be exploited by Jenkins and Sonar.

Disclaimer: the unit test suite has been geared towards code quality assurance (connuous integraon) and not towards the user or the developer. Addional support for developers is probably needed …

2nd ctools and gammalib coding sprint 27-31 January 2014 73 (Jürgen Knödlseder) Test result display with XML editors

2nd ctools and gammalib coding sprint 27-31 January 2014 74 (Jürgen Knödlseder) 4. Current status GammaLib world map 2013 SourceForge downloads

2nd ctools and gammalib coding sprint 27-31 January 2014 76 (Jürgen Knödlseder) GammaLib stascs hps://www.ohloh.net/p/GammaLib

the community is growing …

for your picture!

2nd ctools and gammalib coding sprint 27-31 January 2014 77 (Jürgen Knödlseder) ctools stascs hps://www.ohloh.net/p/ctools_

2nd ctools and gammalib coding sprint 27-31 January 2014 78 (Jürgen Knödlseder) Current status

GammaLib ctools • Release 00-08-00 • Release 00-07-00 • Includes abstract observaon handling, • Provides observaon simulaon, event data modelling, model fing, selecon, binning, model fing, sky applicaon support mapping • Provides FITS and XML interfaces • Supports mul-instrument fing • Homogeneous class interfaces • Support for - CTA (binned and unbinned) - Fermi/LAT (binned) - COMPTEL (binned) - Mul-wavelength

2nd ctools and gammalib coding sprint 27-31 January 2014 79 (Jürgen Knödlseder) Next steps

GammaLib ctools • Freeze interfaces • Extend unit tests • Reduce number of excepon classes • Implement performance tests • Opmize code for binned analysis • Implement regression tests • Extend unit tests to full code coverage • Complete user and developer • Implement performance tests documentaon • Implement regression tests • Provide binary packages • Complete user and developer • … and extend the package documentaon hps://cta-redmine.irap.omp.eu/rb/ • Provide binary packages master_backlog/ctools • … and extend the package hps://cta-redmine.irap.omp.eu/rb/ master_backlog/gammalib

2nd ctools and gammalib coding sprint 27-31 January 2014 80 (Jürgen Knödlseder) 5. Goals of this sprint Goals of this sprint

… and of course: having lots of fun!

2nd ctools and gammalib coding sprint 27-31 January 2014 82 (Jürgen Knödlseder)