Basic Computing Techniques

Similar documents
Introduction to ROOT and data analysis

Sources: On the Web: Slides will be available on:

Introduction to ROOT

An Incomplete C++ Primer. University of Wyoming MA 5310

Lecture 2 Mathcad Basics

Introduction to Python

Installing C++ compiler for CSc212 Data Structures

Computational Mathematics with Python

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Informatica e Sistemi in Tempo Reale

Computational Mathematics with Python

Java Interview Questions and Answers

Computational Mathematics with Python

Intermediate PowerPoint

CHM 579 Lab 1: Basic Monte Carlo Algorithm

Code::Blocks Student Manual

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

CS 103 Lab Linux and Virtual Machines

13 File Output and Input

CORBA Programming with TAOX11. The C++11 CORBA Implementation

LabVIEW Day 1 Basics. Vern Lindberg. 1 The Look of LabVIEW

Java Crash Course Part I

Drawing a histogram using Excel

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

if and if-else: Part 1

Tutorial on C Language Programming

NLP Programming Tutorial 0 - Programming Basics

Memory Management Simulation Interactive Lab

BNG 202 Biomechanics Lab. Descriptive statistics and probability distributions I

Lesson 8: Simon - Arrays

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

FEEG Applied Programming 5 - Tutorial Session

1001ICT Introduction To Programming Lecture Notes

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

EXERCISE 3: String Variables and ASCII Code

Supplemental Worksheet Problems To Accompany: The Pre-Algebra Tutor: Volume 1 Section 1 Real Numbers

Programming in Access VBA

0 Introduction to Data Analysis Using an Excel Spreadsheet

6.S096 Lecture 1 Introduction to C

Simulation Exercises to Reinforce the Foundations of Statistical Thinking in Online Classes

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

C++ INTERVIEW QUESTIONS

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Object Oriented Software Design

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

The C Programming Language course syllabus associate level

Moving from CS 61A Scheme to CS 61B Java

CPSC 226 Lab Nine Fall 2015

CSC230 Getting Starting in C. Tyler Bletsch

AP Computer Science Java Mr. Clausen Program 9A, 9B

CS 101 Computer Programming and Utilization

Computer Programming I & II*

Chapter One Introduction to Programming

Hypercosm. Studio.

Introduction to Scientific Computing Part II: C and C++ C. David Sherrill School of Chemistry and Biochemistry Georgia Institute of Technology

Lecture 5: Java Fundamentals III

Plots, Curve-Fitting, and Data Modeling in Microsoft Excel

C++ Overloading, Constructors, Assignment operator

Object Oriented Software Design

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Computer Programming C++ Classes and Objects 15 th Lecture

HPTUNERS SCANNER STARTUP GUIDE

Scientific Visualization with wxpython and Matplotlib. Scott Pearse CSCI 5448 Spring 2011

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

C++ Programming Language

Visual Logic Instructions and Assignments

Game Programming with DXFramework

1 One Dimensional Horizontal Motion Position vs. time Velocity vs. time

Computer Science 217

Ansur Test Executive. Users Manual

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

El Dorado Union High School District Educational Services

Web development... the server side (of the force)

Appendix K Introduction to Microsoft Visual C++ 6.0

public static void main(string[] args) { System.out.println("hello, world"); } }

Programmierpraktikum

1 Topic. 2 Scilab. 2.1 What is Scilab?

Athena Knowledge Base

While Loops and Animations

Introduction to Java

C# and Other Languages

RRDtool. Tobi Oetiker Sponsors: AboveNet CAIDA ETH Zurich RRDtool NANOG Nr.

C++ Input/Output: Streams

Lecture 2. Summarizing the Sample

Directions for Frequency Tables, Histograms, and Frequency Bar Charts

The BSN Hardware and Software Platform: Enabling Easy Development of Body Sensor Network Applications

Visual Basic 2010 Essentials

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Object Oriented Software Design II

Introduction to Data Structures

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Q N X S O F T W A R E D E V E L O P M E N T P L A T F O R M v Steps to Developing a QNX Program Quickstart Guide

Visual Basic Programming. An Introduction

INTEL PARALLEL STUDIO EVALUATION GUIDE. Intel Cilk Plus: A Simple Path to Parallelism

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Transcription:

01/25/07 PHY310: Statistical Data Analysis 1 PHY310: Lecture 2 Basic Computing Techniques Road Map Getting started with ROOT C++ Review Getting started with data presentation

01/25/07 PHY310: Statistical Data Analysis 2 Programs for Data Analysis There are several programs available ROOT <-- What we'll use IDL Origin PAW &c All are pretty good, but you need to be careful and understand what each one is doing Remember, you are responsible for your results. If there is a bug in a program and you get the wrong answer, it is still your fault. Most of the semester will be agnostic about which analysis program is used, but for today we'll concentrate on ROOT.

01/25/07 PHY310: Statistical Data Analysis 3 Required Analysis Features Basic Graphics and Plotting Simple 3D Graphics Numeric Function Optimization Interactive Control Language (macros) Interface to some programming language.

01/25/07 PHY310: Statistical Data Analysis 4 ROOT: A Data Analysis Frame Available from <http://root.cern.ch/> Install the latest production version (5.14/00) Linux: There are rpms available for installation Windows: Compiled executables are available, require a C + compiler Works with the few command line version. ROOT will be installed on the Math SINC site linux machines. Site becomes available next week. My basic instructions will be for ROOT on linux Except for command line, works the same on windows.

01/25/07 PHY310: Statistical Data Analysis 5 Starting and Stopping ROOT mcgrew@boxer:~$ root ******************************************* * * * W E L C O M E to R O O T * * * * Version 5.12/00f 23 October 2006 * * * * You are welcome to visit our Web site * * http://root.cern.ch * * * ******************************************* FreeType Engine v2.1.9 used to render TrueType fonts. Compiled on 21 January 2007 for linuxdeb with thread support. CINT/ROOT C/C++ Interpreter version 5.16.13, June 8, 2006 Type? for help. Commands must be C++ statements. Enclose multiple statements between {. C. McGrew's Style Configuration root [0].q mcgrew@boxer:~$ Command to run ROOT The ROOT version The C++ interpreter version.q exits ROOT. In root the command line takes a C++ statement, or a control which starts with.

01/25/07 PHY310: Statistical Data Analysis 6 Entering C++ Commands mcgrew@boxer:~$ root ******************************************* * * * * W E L C O M E to R O O T * * * * Version 5.12/00f 23 October 2006 * * * You are welcome to visit our Web site * * http://root.cern.ch * * * ******************************************* Command to run ROOT FreeType Engine v2.1.9 used to render TrueType fonts. Compiled on 21 January 2007 for linuxdeb with thread support. CINT/ROOT C/C++ Interpreter version 5.16.13, June 8, 2006 Type? for help. Commands must be C++ statements. Enclose multiple statements between {. C. McGrew's Style Configuration root [0] for (int i = 0; i<3; ++i) { end with '', '@':abort > std::cout << "hello world" end with '', '@':abort > << std::endl; end with '', '@':abort > hello world hello world hello world root [1].q mcgrew@boxer:~$ C++ for loop Output from for loop

01/25/07 PHY310: Statistical Data Analysis 7 Simple Macro Files C++ from the command line is a bit troublesome, so most of the time you will use macro files. Create macro files with your favorite editor. End the file name with.c (the capitalization matters). e.g. hello.c { // An example hello world for (int i = 0; i<3; ++i) { std::cout << hello world << std::endl; Run macro in root mcgrew@boxer:macros$ root -n -l root [0].x hello.c hello world hello world hello world root [1].q mcgrew@boxer:macros$ Macro files are just plain C++ code Macros start with a { Macros end with a Options to make it less verbose.x executes the hello.c macro.q exits ROOT.

01/25/07 PHY310: Statistical Data Analysis 8 C++ Review: Output Streams C++ output is done using streams which are just places to put output Output to the screen is done to std::cout std::cout << output This prints output on the screen. Notice the << operator says Put 'output' into 'std::cout' Output to a file is done using a ofstream That's read output file stream Here is the previous hello.c macro modified to write to the file hello.txt { // A simple hello world example. std::ofstream file("hello.txt"); for (int i = 0; i < 3; ++i) { file << "hello world" << std::endl; file.close(); Create a stream attached to hello.txt Most variables can be printed just by putting them into the output stream. { int i = 3; std::cout << The value of i is << i << std::endl; std::endl prints a new line

01/25/07 PHY310: Statistical Data Analysis 9 C++ Review: Declaring Variables C++ Variables must be declared before they are used. Integer are declared with int Floating point numbers are declared with double Strings are declared with std::string { int i = 2; double s = 3.14159; std::string pi = apple ; std::cout << i is << i << std::endl << s is << s << std::endl << pi is << pi << std::endl;

01/25/07 PHY310: Statistical Data Analysis 10 C++ Review: Input Streams C++ input is done using streams which are places to get input from. Input from the keyboard is done with std::cin std::cout >> i; The operator >> puts the input into i Input from a file is done using an std::ifstream This is read input file stream { std::ifstream input( datafile.txt ); int i; for (;;) { input >> i; if (!input.good()) break; std::cout << i; input.close();

01/25/07 PHY310: Statistical Data Analysis 11 C++ Review: Arrays Arrays are declared by adding [] with a count int myarray[3]; // An array of three integers Arrays are indexed from zero The macro prints 1 2 3 { int myarray[3]; myarray[0] = 1; myarray[1] = 2; myarray[2] = 3; for (int i=0; i<3; ++i) { std::cout << << myarray[i]; std::cout << endl;

01/25/07 PHY310: Statistical Data Analysis 12 C++ Review: Pointers Most variables just contain a value An int variable contains an integer value A double variable contains an floating point value But, sometimes (quite often) you want to hold the address of a value This is the definition of a pointer. Pointers are declared using the * int* pointertointeger; // A variable holding the address of an integer The value pointed to by the variable is access using * *pointertointeger = 5; // Put 5 at the address pointed int theinteger = *pointertointeger; Also -> -- more on that later when I talk about classes. To use a pointer, you have to assign it an address int theint = 5; int* pointer = &theint; // pointer holds theint address int* pointer = new int; // Allocate new space for the value. If you allocate space you must delete it with delete pointer;

01/25/07 PHY310: Statistical Data Analysis 13 C++ Review: Conditional Statements Conditional statements are the if-then-else clauses if (condition) { // Code executed if condition is true else { // Code executed if condition is false Logical Conditions if (1<2) std::cout << true ; if (1>2) std::cout << false ; if (1==2) std::cout << false ; if (1<=2) std::cout << true ; if (1>=2) std::cout << false ; if ((1<2) && (1>0)) std::cout << false ; if ((1<2) (1>0)) std::cout << true ;

C++ Review: Loops C++ provides the for loop as an all purpose looping construct for (initial conditions; execute while true; increment) { code for (int count = 0; count < 5; ++count) { std::cout << Count is << count << std::cendl; You can break out of a loop (terminate early) using the break statement for (int count = 0; count < 5; ++count) { Create a new variable count and set to 0 Continue while count is less than five Increment count by one Do something if (count == 3) { break; If count is three, then exit the loop std::cout << Count is << count << std::cendl; 01/25/07 PHY310: Statistical Data Analysis 14

01/25/07 PHY310: Statistical Data Analysis 15 C++ Review: Functions C++ subroutines are called functions May (or may not) return a value void voidfunction(int avalue) { std::cout << value is << avalue; int integerfunction(int avalue) { std::cout << input is << avalue; int myvalue = avalue + 1; std::cout << output is << myvalue; return myvalue;

01/25/07 PHY310: Statistical Data Analysis 16 ROOT Tutorial: Graphs ROOT graphs are ideal for drawing Y vs X graphs (dependent vs. independent variables) By convention, the independent variable goes on the horizontal axis. Data can be automatically read from a file You can control the reading format, but the default works for most files ROOT is heavily object oriented Provides a (huge) library of classes that do (almost) everything needed for an analysis ROOT classes all start with a capital T, so a graph is created using the TGraph object. Declare a pointer to a TGraph Allocate a new TGraph object Data is added to the TGraph during the constructor call (see next slide) Draw the TGraph After drawing, the picture can be saved to a file. Most work is done by writing macro files and executing them inside of ROOT

01/25/07 PHY310: Statistical Data Analysis 17 TGraph Example Plot data from an Ohms law experiment Voltage (V) Current (ma) 1 ± 0.02 0.37 ± 0.02 2 ± 0.04 0.71 ± 0.04 4 ± 0.08 1.4 ± 0.07 5 ± 0.10 1.9 ± 0.10 7 ± 0.14 2.41 ± 0.12 10 ± 0.20 3.39 ± 0.17 ohmslaw.dat Voltage (V), Current (ma), Sigma (V), sigma (ma) 1 0.37 0.02 0.02 2 0.71 0.04 0.04 4 1.40 0.08 0.07 5 1.90 0.10 0.10 7 2.41 0.14 0.12 10 3.39 0.20 0.17 The result will look like this

01/25/07 PHY310: Statistical Data Analysis 18 A ROOT Macro to Make a Graph ohmslaw.c { TGraphErrors* g = new TGraphErrors("ohmsLaw.dat"); g->settitle("current vs Voltage"); g->gethistogram()->setxtitle("voltage (V)"); g->gethistogram()->setytitle("current (ma)"); g->draw("ap"); gpad->print("ohmslaw.png"); Allocate a new graph Set the title Label the X axis Label the Y axis Draw the graph on the canvas Save the picture to a file Most of the work is done by calling the class methods -> is read points to The window where ROOT draws a picture is called a Canvas ROOT creates a canvas when it's needed A pointer to the current canvas is saved in the global variable gpad

01/25/07 PHY310: Statistical Data Analysis 19 C++ Review: Using Classes Classes are the C++ workhorse. We will be using, but probably not writing classes A class creates a new variable type that is used just like a regular type What we need to know A class has internal data that saves the state of an object The data is not shared with other objects of the same class A class has methods which are like functions Methods act on the class data In ROOT, we will usually use pointers to class objects. Example streams are actually class objects { std::ofstream *file = new std::ofstream( myfile.txt ); (*file) << hello worlds << std::endl; file->close(); delete file; Declare a pointer to a file And assign an address of a new file object Reference the object using the * operator Reference the object using the -> operator Delete the object referenced by file

01/25/07 PHY310: Statistical Data Analysis 20 ROOT Tutorial: Creating Histograms ROOT histograms are a type of graph that has been specialized to show the frequency of measurements. By convention, the measurement goes on the X axis and the number of measurements at a particular value goes on the Y axis. Histograms are filled by adding one entry at a time Data is not automatically read in from a file However, with histograms you often do calculations with the data before filling so this isn't really a limitation. ROOT provides several histogram classes We'll mostly use TH1F which is a one dimensional histogram of floating point measurements Later we'll use TH2F for two dimensional histograms.

01/25/07 PHY310: Statistical Data Analysis 21 A ROOT Macro to Make a Histogram Create a histogram from data in a file. { TH1F* h = new TH1F("mudkTime", "Muon Decay Times", 50, 0.0,10000.0); h->setxtitle("decay Times (ns)"); h->setytitle("decays per 200 ns"); std::ifstream input("mudktime.dat"); double time; for (;;) { input >> time; if (!input.good()) break; time *= 1000; h->fill(time); input.close(); h->draw("e"); gpad->print("mudktime.png"); Create a histogram named mudktime With a title Muon Decay Times And 50 bins ranging between 0.0 and 10000.0 Set the X and Y axis labels. Read the data file and Fill the histogram Draw the histogram on the canvas and save the picture to a file.

01/25/07 PHY310: Statistical Data Analysis 22 Random Number Generation We're going to need a lot of data in this class To prove that statistics works We need to know the true distributions We need to repeat the experiments thousands of times The budget for PHY310 lab equipment... minimal (as in non-existent) Solution: A lot of the time, we will use Fake data Usually called Monte Carlo data Monte Carlo data is designed to simulate measured data The best MC data is indistinguishable from measured data. In ROOT we generate random numbers using the grandom object.

01/25/07 PHY310: Statistical Data Analysis 23 Filling a Histogram with a Gaussian { // Make a 1D Gaussian histogram. TH1F* gaus1d = new TH1F("gaus1D", "One Dimensional Gaussian", 60, -3, 3); gaus1d->setxtitle("sigma"); gaus1d->setytitle("counts per 0.1 sigma"); for (int i=0; i<100000; ++i) { double x = grandom->gaus(0,1); gaus1d->fill(x); gaus1d->draw("e"); gpad->print("gaus1d.png"); Create the histogram and label the X and Y axis Fill the histogram with 100,000 random numbers from a Gaussian distribution Draw the histogram and save the picture to a file Other Useful Random Distributions Uniform between [0.0, 1.0) grandom->uniform() Poissonian grandom->pois(3.3) This has a mean of 3.3 Exponential grandom->exp(2.1) This has a mean of 2.1

01/25/07 PHY310: Statistical Data Analysis 24 Finally Today we covered most of the technical computer stuff. Anything else we need will be picked up as we go along C++ is a good tool to have in your box for later You might want pick up a book on C++ The gold standard is Stroustrup's The C++ Programming Language If you have Linux, it comes with an excellent C++ compiler (g++) If you use Windows, there is a free version of Visual C++ I think the current version is something 2005. Starting next week we'll talk about probability, uncertainty, and what you're really trying to say when you report a measurement The End