Download C Outline-Computing and Statistical Data Analysis-Lecture 01 Slides-Physics and more Slides Computational and Statistical Data Analysis in PDF only on Docsity!
Computing and Statistical Data Analysis
(PH4515, UofL PG Lectures)
Glen Cowan
Physics Department
Royal Holloway, University of London
Egham, Surrey TW20 0EX
[email protected] www.pp.rhul.ac.uk/~cowan/stat_course.html
Outline
st
4 weeks will be a crash course in C++
Quick overview of the important stuff
Use UNIX (Linux) environment
Intro to tools like ROOT, gmake, debugger
From week 5, statistical data analysis
Probability, random variables, Monte Carlo methods
Statistical tests
Parameter estimation
Data analysis exercises will use C++ tools
C++ Outline
Approximately by lecture for 1
st
4 weeks:
1 Introduction to C++ and UNIX environment
2 Variables, types, expressions, loops
3 Type casting, functions
4 Files and streams
5 Arrays, strings, pointers
6 Classes, intro to Object Oriented Programming
7 Memory allocation, operator overloading, templates
8 Inheritance, STL, gmake, ddd
Some resources (computing part)
There are many web based resources, e.g.,
www.doc.ic.ac.uk/~wjk/C++Intro (Rob Miller, IC course)
www.cplusplus.com (online reference)
www.icce.rug.nl/documents/cplusplus (F. Brokken)
See links on course site or google for “C++ tutorial”, etc.
There are thousands of books – see e.g.
W. Savitch, Problem Solving with C++, 4
th
edition
(lots of detail – very thick).
B. Stroustrup, The C++ Programming Language
(the classic – even thicker).
Lippman, Lajoie (& Moo), C++ Primer , A-W, 1998.
Basic UNIX
UNIX tasks divide neatly into:
interaction between operating system and computer (the kernel),
interaction between operating system and user (the shell).
Several shells (i.e. command sets) available: sh, csh, tcsh, bash, …
Shell commands typed at a prompt, here [linappserv0]~>
often set to indicate name of computer:
Command pwd to “print working
directory”, i.e., show the directory
(folder) you’re sitting in.
Commands are case sensitive.
PWD will not work.
UNIX file structure
Tree-like structure for files and directories (like folders):
/ ← the ‘root’ directory
usr/ bin/ home/ sys/ tmp/ ... smith/ jones/ jackson/ ... WWW/ code/ thesis/ ...
File/directory names are case sensitive: thesis ≠ Thesis
A few UNIX commands (case sensitive!)
pwd Show present working directory ls List files in present working directory ls -la List files of present working directory with details man ls Show manual page for ls. Works for all commands. man -k keyword Searches man pages for info on “keyword”. cd Change present working directory to home directory. mkdir foo Create subdirectory foo cd foo Change to subdirectory foo (go down in tree) cd .. Go up one directory in tree rmdir foo Remove subdirectory foo (must be empty) emacs foo & Edit file foo with emacs (& to run in background) more foo Display file foo (space for next page) less foo Similar to more foo , but able to back up ( q to quit) rm foo Delete file foo
A few more UNIX commands
cp foo bar Copy file foo to file bar, e.g., cp ~smith/ foo ./ copies Smith’s file foo to my current directory mv foo bar Rename file foo to bar lpr foo Print file foo. Use -P to specify print queue, e.g., lpr -Plj1 foo (site dependent). ps Show existing processes kill 345 Kill process 345 ( kill -9 as last resort) ./ foo Run executable program foo in current directory ctrl -c Terminate currently executing program chmod ug+x foo Change access mode so user and group have privilege to execute foo (Check with ls -la )
Better to read a book or online tutorial and use man pages
Introduction to C++
Language C developed (from B) ~ 1970 at Bell Labs
Used to create parts of UNIX
C++ derived from C in early 1980s by Bjarne Stroustrup
“C with classes”, i.e., user-defined data types that
allow “Object Oriented Programming”.
Java syntax based largely on C++ (head start if you know java)
C++ is case sensitive ( a not same as A ).
Currently most widely used programming language in High
Energy Physics and many other science/engineering fields.
Recent switch after four decades of FORTRAN.
Compiling and running a simple C++ program
// My first C++ program #include using namespace std; int main(){ cout << "Hello World!" << endl; return 0; }
Using,e.g., emacs, create a file HelloWorld.cc containing:
We now need to compile the file (creates machine-readable code):
g++ -o HelloWorld HelloWorld.cc Invokes compiler (gcc) name of output file source code
Run the program: ./HelloWorld ← you type this
Hello World! ← computer shows this
Writing programs in the Real World
Usually create a new directory for each new program.
For trivial programs, type compile commands by hand.
For less trivial but still small projects, create a file (a ‘script’) to
contain the commands needed to build the program:
**#!/bin/sh
File build.sh to build HelloWorld
g++ -o HelloWorld HelloWorld.cc Bonjour.cc GruessGott.cc YoDude.cc**
To use, must first have ‘execute access’ for the file:
chmod ug+x build.sh ← do this only once
./build.sh ← executes the script
A closer look at HelloWorld.cc
// My first C++ program is a comment (preferred style)
The older ‘C style’ comments are also allowed (cannot be nested):
*These lines here are comments / / and so are these /
You should include enough comments in your code to make it
understandable by someone else (or by yourself, later).
Each file should start with comments indicating author’s name,
main purpose of the code, required input, etc.
More HelloWorld.cc
using namespace std; More later. For now, just do it.
A C++ program is made up of functions. Every program contains
exactly one function called main:
int main(){ // body of program goes here return 0; }
Functions “return” a value of a given type; main returns int (integer).
The () are for arguments. Here main takes no arguments.
The body of a function is enclosed in curly braces: { }
return 0; means main returns a value of 0.
Finishing up HelloWorld.cc
The ‘meat’ of HelloWorld is contained in the line
cout << "Hello World!" << endl;
Like all statements, it ends with a semi-colon.
cout is an “output stream object”.
You send strings (sequences of characters) to cout with <<
We will see it also works for numerical quantities (automatic
conversion to strings), e.g., cout << "x = " << x << endl;
Sending endl to cout indicates a new line. (Try omitting this.)
Old style was "Hello World!\n"