AmpTools
Histogram1D.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/Histogram.h"
42 #include "IUAmpTools/Histogram1D.h"
43 
44 using namespace std;
45 
47 {
48  m_dimensions=1;
49 }
50 
52 Histogram()
53 {
54  m_binContents.resize(hist.nBins);
55  m_binContents.clear();
56  m_entries = 0;
57  m_dimensions=1;
58  m_nBinsX = hist.nBins;
59  m_nBins = hist.nBins;
60  m_xLow = hist.xLow;
61  m_xHigh = hist.xHigh;
62 
63  for( int i = 0; i < m_nBins; ++i ){
64 
65  m_binContents[i] = hist.contents[i];
66  }
67 
68  m_entries = hist.entries;
69 }
70 
71 Histogram1D::Histogram1D( int nBins, double xLow, double xHigh,
72  string name, string title ) :
73 Histogram( name, title )
74 {
75  m_nBinsX = nBins;
76  m_nBins = nBins;
77  m_xLow= xLow;
78  m_xHigh= xHigh;
79  m_entries= 0;
80  m_dimensions= 1;
81  m_binContents.resize(nBins);
82  m_binContents.clear();
83  m_binSizeX = ( xHigh - xLow ) / nBins;
84 }
85 
86 void
87 Histogram1D::fill( vector < double > values, double weight ){
88 
89  assert (values.size()==1); //This is a 1D histogram!
90 
91  double value=values.at(0);
92 
93  if( ( value < m_xHigh ) && ( value >= m_xLow ) ){
94 
95  m_binContents[(int)((value - m_xLow)/m_binSizeX)] += weight;
96  }
97  // count overflows and underflows in the number of entries
98  m_entries += weight;
99 }
100 
101 
102 
103 
104 TH1* Histogram1D::toRoot( void ) const {
105 
106  TH1F *plot= new TH1F( name().c_str(), title().c_str(),
107  m_nBins, m_xLow, m_xHigh );
108 
109  for( unsigned int i = 0; i < m_binContents.size(); ++i ){
110 
111  plot->SetBinContent( i+1, m_binContents[i] );
112  plot->SetBinError( i+1, sqrt( m_binContents[i] ) );
113  }
114 
115  return (TH1*)plot;
116 }
117 
119 Histogram1D::toStruct( void ) const {
120 
121  if( m_nBins > MAXBINS ){
122 
123  cout << "Too many bins in histogram -- increase MAXBINS" << endl;
124  assert( false );
125  }
126 
127  HistStruct hist;
128  hist.xLow = m_xLow;
129  hist.xHigh = m_xHigh;
130  hist.nBins = m_nBins;
131  hist.nBinsX = m_nBins;
132  hist.entries = m_entries;
133 
134  for( int i = 0; i < m_nBins; ++i ){
135 
136  hist.contents[i] = static_cast< float >( m_binContents[i] );
137  }
138 
139  return hist;
140 }
141 
143  Histogram* m_histo;
144  m_histo=(Histogram*)(new Histogram1D(*this));
145  return m_histo;
146 }
int m_nBins
Definition: Histogram.h:84
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
float xLow
Definition: Histogram.h:50
virtual TH1 * toRoot() const
Definition: Histogram1D.cc:104
string name() const
Definition: Histogram.h:78
double m_xHigh
Definition: Histogram.h:86
double sqrt(double)
vector< double > m_binContents
Definition: Histogram.h:89
virtual void fill(vector< double > value, double weight=1.0)
Definition: Histogram1D.cc:87
float contents[MAXBINS]
Definition: Histogram.h:53
virtual HistStruct toStruct() const
Definition: Histogram1D.cc:119
virtual Histogram * Clone() const
Definition: Histogram1D.cc:142
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