Data Analysis Using C++

3/10/99


Click here to start


Table of Contents

Data Analysis Using C++

Our Goals

Object Oriented Programming in C++

Supposed benefits of OO

I personally think...

Buzzwords you may have heard

Towards our first C++ example

Some technical stuff so we have something we can use

A word on the header (.h) files

A trivial “cylinder”, non-OO

Now a “cylinder” class

Using our new class

What have we gained?

Example of information hiding

Next Buzzwords: Inheritance and Polymorphism

“Can I use your Phone?”

How many different cars can you drive?

Let’s make a cube

Extracting the commonalties

Commonalties checklist

The “body” class

The C++ code

Ok, ok, but what is it good for?

a “new” object...

Now watch...

“list of Bodies” code example

Real-life example… PDAQ

Pdaq device objects at work...

How does this object stuff work?

So what? you may say… I can do the same thing in C!

Adding a cone in C

This looks easier than it is in practice...

Having to modify debugged code is bad.

And then there is the speed issue

Let’s pause for a moment and review what we’ve got.

But inheritance/polymorphism is only the smaller part...

Bodies with Color

Our Body has a Color

Constructing with Color

Let’s make sure we understand what we did

Using the colorful new bodies

Why is this design flawed?

const - a client-server contract

const can save you a lot of trouble and time

Fixing (some of) Body’s design flaws now...

An ugly (but working) solution...

Overloaded Functions...

...and overloaded operators

Let’s do overloaded functions first...

A fictitious example

Now we better understand this...

A convenient shorthand notation for a default value

Next tool: Object References

Reference example

C++ uses references in many places

Let’s now overload some Operators

Detour 1: Introducing the this pointer

c2=c1: A simple “=“ operation?

Taking care of the Color

“cube c2 = c1” is different from “c2 = c1”!

Why not specifying the copy constructor is usually bad

Now we can finally come back to the colored body...

…and cubes and cylinders

Let’s summarize what we have learned so far

cout << -- an operator not like the others.

What is a friend?

Declaring who our friend is

The ostream<< non-member function

In the end, it’s overloading a function...

and this is how it looks like:

The real final, ultimate body class

Now we see it at work...

Some templates, some STL

Simple templated class (not for actual use)

STL (Standard Template Library)

Just a STL teaser...

Let’s move on to design patterns

What are those “design patterns”?

Some design patterns (mentioned, not explained)

Composite Objects

A Factory

How can we avoid changing a lot of code?

Abstract Histograms

Do Hbook and Root histos have a common parent class?

Visit our Factories

usercode.cc / usercode.o

hbook and root histograms

A note on the abstract histograms

More good stuff: a Singleton

static data members and member functions

A silly example...

Now lets see our RootHistogramFactory Singleton

Let’s review what we did with the designs

On to some analysis examples

Event objects

Getting an Event object

The Coke-Can Iterator in the Physics Department

Trivial PHENIX analysis example

reading from 2 or more files

and now some Packets...

Maybe you didn’t catch it...

Memory leaks, the curse of dynamic memory allocation

A stupid, obvious user memory leak

A class designer’s stupid mistake

A very common mistake

So what’s the problem?

Another common mistake

In which order are constructors and destructors called?

Some more unrelated bits and pieces...

vectors and arrays

more-dimensional arrays

You may have seen the message system...

That’s it!

Author: Martin

Email: purschke@bnl.gov

Home Page: http://ww.phenix.bnl.gov/~purschke