AmpTools
Histogram2D.cc
Go to the documentation of this file.
1 //******************************************************************************
2 // This file is part of AmpTools, a package for performing Amplitude Analysis
3 //
4 // Copyright Trustees of Indiana University 2010, all rights reserved
5 //
6 // This software written by Matthew Shepherd, Ryan Mitchell, and
7 // Hrayr Matevosyan at Indiana University, Bloomington
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 // 1. Redistributions of source code must retain the above copyright
13 // notice and author attribution, this list of conditions and the
14 // following disclaimer.
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice and author attribution, this list of conditions and the
17 // following disclaimer in the documentation and/or other materials
18 // provided with the distribution.
19 // 3. Neither the name of the University nor the names of its contributors
20 // may be used to endorse or promote products derived from this software
21 // without specific prior written permission.
22 //
23 // Creation of derivative forms of this software for commercial
24 // utilization may be subject to restriction; written permission may be
25 // obtained from the Trustees of Indiana University.
26 //
27 // INDIANA UNIVERSITY AND THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES,
28 // EXPRESS OR IMPLIED. By way of example, but not limitation, INDIANA
29 // UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCANTABILITY OR
30 // FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THIS SOFTWARE OR
31 // DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS,
32 // OR OTHER RIGHTS. Neither Indiana University nor the authors shall be
33 // held liable for any liability with respect to any claim by the user or
34 // any other party arising from use of the program.
35 //******************************************************************************
36 
37 #include <iostream>
38 #include <math.h>
39 #include <assert.h>
40 
41 #include "IUAmpTools/Histogram2D.h"
42 
43 using namespace std;
44 
46 m_nBinsY( 0 ),
47 m_yLow( 0 ),
48 m_yHigh( 0 )
49 {
50  m_dimensions=2;
51 }
52 
54 {
55 
56  m_nBinsX = hist.nBinsX;
57  m_nBinsY = hist.nBinsY;
58  m_xLow = hist.xLow;
59  m_xHigh = hist.xHigh;
60  m_nBinsY = hist.nBinsY;
61  m_yLow = hist.yLow;
62  m_yHigh = hist.yHigh;
63  m_nBins = hist.nBins;
64  m_binContents.resize(m_nBins);
65  m_binContents.clear();
66 
67  for( int i = 0; i < m_nBins; ++i ){
68 
69  m_binContents[i] = hist.contents[i];
70  }
71 
72  m_entries = hist.entries;
73 }
74 
75 Histogram2D::Histogram2D( int nBinsX, double xLow, double xHigh,
76  int nBinsY, double yLow, double yHigh,
77  string name, string title ) :
78 Histogram( name, title )
79  {
80  m_xLow=xLow;
81  m_xHigh=xHigh;
82  m_yLow=yLow;
83  m_yHigh=yHigh;
84  m_nBinsX=nBinsX;
85  m_nBinsY=nBinsY;
86  m_nBins=nBinsX*nBinsY;
87  m_entries=0;
88  m_binContents.resize( nBinsX*nBinsY );
89  m_binContents.clear();
90  m_binSizeX = ( xHigh - xLow ) / nBinsX;
91  m_binSizeY = ( yHigh - yLow ) / nBinsY;
92 }
93 
94 void
95 Histogram2D::fill(vector < double > values, double weight ){
96 
97  assert (values.size()==2);
98  double valueX=values[0];
99  double valueY=values[1];
100  if( ( valueX < m_xHigh ) && ( valueX >= m_xLow ) && ( valueY < m_yHigh ) && ( valueY >= m_yLow ) ){
101  int ibinX=(int)((valueX - m_xLow)/m_binSizeX);
102  int ibinY=(int)((valueY - m_yLow)/m_binSizeY);
103  int ibin=ibinY*m_nBinsX+ibinX; //a-la ROOT
104  m_binContents[ibin] += weight;
105  }
106  // count overflows and underflows in the number of entries
107  m_entries += weight;
108 }
109 
110 
111 
112 
113 TH1* Histogram2D::toRoot( void ) const {
114 
115  TH2F *plot2D=new TH2F( name().c_str(), title().c_str(),
116  m_nBinsX, m_xLow, m_xHigh,m_nBinsY, m_yLow, m_yHigh );
117  int ibinX,ibinY;
118  for( unsigned int i = 0; i < m_binContents.size(); ++i ){
119  // int ibin=ibinY*m_nBinsX+ibinX; I used this. So:
120  ibinX=i % m_nBinsX;
121  ibinY=i / m_nBinsX;
122  plot2D->SetBinContent( ibinX+1,ibinY+1, m_binContents[i] ); //+1 since ROOT considers the bin 0-th as underflow.
123  plot2D->SetBinError( ibinX+1,ibinY+1, sqrt( m_binContents[i] ) );
124  }
125  return plot2D;
126 }
127 
129 Histogram2D::toStruct( void ) const {
130 
131  if( m_nBins > MAXBINS ){
132 
133  cout << "Too many bins in histogram -- increase MAXBINS" << endl;
134  assert( false );
135  }
136 
137  HistStruct hist2D;
138  hist2D.xLow = m_xLow;
139  hist2D.xHigh = m_xHigh;
140  hist2D.nBinsX = m_nBinsX;
141 
142  hist2D.yLow = m_yLow;
143  hist2D.yHigh = m_yHigh;
144  hist2D.nBinsY = m_nBinsY;
145 
146  hist2D.nBins = m_nBins;
147 
148  hist2D.entries = m_entries;
149 
150  for( int i = 0; i < m_nBins; ++i ){
151 
152  hist2D.contents[i] = static_cast< float >( m_binContents[i] );
153  }
154 
155  return hist2D;
156 }
157 
158 
160  return new Histogram2D(*this);
161 }
162 
163 
164 
165 
166 
167 
168 
int m_nBins
Definition: Histogram.h:84
virtual TH1 * toRoot() const
Definition: Histogram2D.cc:113
virtual Histogram * Clone() const
Definition: Histogram2D.cc:159
int m_nBinsX
Definition: Histogram.h:84
int m_dimensions
Definition: Histogram.h:92
#define MAXBINS
Definition: Histogram.h:46
double m_xLow
Definition: Histogram.h:85
float xHigh
Definition: Histogram.h:50
virtual void fill(vector< double > values, double weight=1.0)
Definition: Histogram2D.cc:95
float xLow
Definition: Histogram.h:50
int nBinsY
Definition: Histogram.h:49
virtual HistStruct toStruct() const
Definition: Histogram2D.cc:129
string name() const
Definition: Histogram.h:78
double m_xHigh
Definition: Histogram.h:86
float yLow
Definition: Histogram.h:51
double sqrt(double)
float yHigh
Definition: Histogram.h:51
vector< double > m_binContents
Definition: Histogram.h:89
float contents[MAXBINS]
Definition: Histogram.h:53
double m_binSizeX
Definition: Histogram.h:90
float entries
Definition: Histogram.h:52
int nBinsX
Definition: Histogram.h:49
int nBins
Definition: Histogram.h:49
double m_entries
Definition: Histogram.h:88
string title() const
Definition: Histogram.h:77