Tag Archives: C++

Computer Languages and High Energy Physics

A common question that people who are thinking about going into physics is “What programming language should I learn?”

This question doesn’t necessarily have a good answer. Really, the answer depends not only on what subfield you are interested in but also what experiment you will work on. In the field of experimental high energy physics (i.e. particle physics) there are three languages that are by far the most useful for day-to-day work.

They are: C++, Fortran, and Python.

C++ (or sometimes just C) and Fortran are used because they are fast and already have enormous libraries of optimized code, allowing you to use things like FFTW (Fastest Fourier Transform in the West) and the GSL (GNU Scientific Library). These are older languages and not the easiest to code but allow a lot of flexibility. If you’re doing computationally intensive work, chances are you’re working with one of these languages. In HEP, Fortran is mostly found in older libraries, many of which are being replaced by newer C++ versions. Thus, you get things like¬†PAW, GEANT3, and Garfield – Fortran-based programs found in CERNLIB – being replaced by ROOT, GEANT4 and Garfield++, which are C++ based. Fortran is still used quite extensively, though, and many simulation toolkits continue to be maintained in Fortran. I’m not an expert on what is used in high energy theory, but I believe¬†Fortran is also one of the most common languages there as well (again, due to the huge set of libraries available for scientific computing and due to it remaining one of the fastest languages). C++ has a lot of nice features – it’s always been object-oriented, it includes templates and the STL, etc., and is much newer than Fortran (there aren’t really any obvious vestiges of punch-card programming in C++!).

Python is becoming more and more popular for high level programming. While it’s too slow for many computations, it is a very nice scripting language. You can quickly create programs that will work without needing to deal with Makefiles and compilers. For example, you could create a bunch of C++ programs to process and analyze a dataset using a Python program to control everything. Furthermore, you can even create Python bindings for C++ code (and some other languages too), allowing you to call your C++ functions inside a Python program. This lets you run everything in Python so that the computationally-intensive parts were compiled using C++ code and the rest is just Python.

Another big advantage of these languages is that they are free. Most people in HEP use the GNU compilers gcc, g++, gfortran, etc. to compile the code. These tools and many of the scientific libraries (like the ones I mentioned above) are also free. This is in stark contrast to languages like Matlab and Mathematica, popular in undergraduate coursework and in some other fields but which can also cost hundreds of dollars per user per year. The same is true of IDL, which is popular among astronomers. Python has NumPy and SciPy which mimic much of the functionality of Matlab. Common languages like Java, Perl, and Ruby may be encountered but aren’t used very often by particle physicists. If you’re going to be working with websites or databases, you might also run into SQL, Javascript, PHP, etc, though these are more for “service work” – keeping the experiment running – rather than an actual physics analysis.