Operations on histograms

Below is a list of methods implementing operations on histograms.

Operations with scalars

void add( double x )

Add "x" to current histo.

void sub( double x )

Substract "x" from current histo.

void mul( double x )

Multiply "x" with current histo.

void div( double x )

Divide current histo by "x".

Operations with another histogram

Ref_Like_Current add( const Ref_Like_Current &other )

Add "other" histo to current histo.

void add( const I_Histo &other ) , Ref_Like_Current sub( const Ref_Like_Current &other )

Subtract "other" histo from current histo.

void sub( const I_Histo &other ) , Ref_Like_Current mul( const Ref_Like_Current &other )

Multiply "other" histo with current histo.

void mul( const I_Histo &other ) , Ref_Like_Current div( const Ref_Like_Current &other )

Divide current histogram by "other" histo.

void div( const I_Histo &other ) , Ref_Like_Current binomial_div( const Ref_Like_Current &other )

Divide current histo by "other" histo using "binomial error"

void binomial_div( const I_Histo &other )

Notice that operations with another histogram exists with two signatures, the first one allowing to directly assign the result of an operation.

This is an example of code to add two histograms:

HepRef(PHisto1D) histo1 = 
  new(db_)  PHisto1D( "Histo1: parabolic function", 20,0.,20.); 
  ... 
  for (int i = 0; i < 50000; i++) 
    histo1->fill(x,w); 

  // Now create a clone of histo1 
  HepRef(Histo1D) histo2 = histo1->clone(); 
  // Add histo1 to histo2 and change histogram name 
  histo2->add( *histo1 ); 
  histo2->set_name( "Histo2 =  Histo2+Histo1 = 2*Histo1" ); 

  //
  // It is also possible to clone and perform an operation on single
  // statement:
  // 	histo2 = histo1->clone()->add( histo1 );
  //

As discussed in the first paragraph, it's important to first make a clone of the original histogram. Another way to clone a histogram is using a copy constructor, as described in Chapter Chapter 8., Constructors.