Fork me on GitHub

source: svn/trunk/Utilities/HepMC/src/WeightContainer.cc@ 600

Last change on this file since 600 was 571, checked in by cp3-support, 13 years ago

upgrade HepMC to version 2.06.05

File size: 3.8 KB
RevLine 
[571]1//////////////////////////////////////////////////////////////////////////
2// Matt.Dobbs@Cern.CH, November 2000, refer to:
3// M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
4// High Energy Physics", Computer Physics Communications (to be published).
5//
6// Container for the Weights associated with an event or vertex.
7// Basically just an interface to STL vector with extra map-like attributes
8//////////////////////////////////////////////////////////////////////////
9
10#include <iostream>
11#include <iomanip>
12#include <sstream>
13#include <vector>
14#include <string>
15#include <map>
16#include <stdexcept>
17
18#include "WeightContainer.h"
19
20namespace HepMC {
21
22WeightContainer::WeightContainer( size_type n, double value )
23 : m_weights(n,value), m_names()
24{ set_default_names(n); }
25
26WeightContainer::WeightContainer( const std::vector<double>& wgts )
27 : m_weights(wgts), m_names()
28{ set_default_names(size()); }
29
30void WeightContainer::set_default_names( size_type n )
31{
32 // internal program used by the constructors
33 std::ostringstream name;
34 for ( size_type count = 0; count<n; ++count )
35 {
36 name.str(std::string());
37 name << count;
38 m_names[name.str()] = count;
39 }
40}
41
42void WeightContainer::push_back( const double& value)
43{
44 size_type count = m_weights.size();
45 m_weights.push_back(value);
46 std::ostringstream name;
47 name << count;
48 m_names[name.str()] = count;
49}
50
51void WeightContainer::pop_back()
52{
53 // this needs to remove the last entry in the vector
54 // and ALSO the associated map entry
55 size_type vit = size() - 1;
56 for ( map_iterator m = m_names.begin(); m != m_names.end(); ++m )
57 {
58 if( m->second == vit ) {
59 m_names.erase(m->first);
60 continue;
61 }
62 }
63 m_weights.pop_back();
64}
65
66double& WeightContainer::operator[]( const std::string& s )
67{
68 const_map_iterator m = m_names.find(s);
69 if( m != m_names.end() ) {
70 return m_weights[m->second];
71 }
72 // doesn't exist - have to create it
73 size_type count = m_weights.size();
74 m_weights.push_back(0);
75 m_names[s] = count;
76 return m_weights.back();
77}
78
79
80const double& WeightContainer::operator[]( const std::string& s ) const
81{
82 const_map_iterator m = m_names.find(s);
83 if( m != m_names.end() ) {
84 return m_weights[m->second];
85 }
86 // doesn't exist and we cannot create it
87 // note that std::map does not support this (const) operator
88 // throw an appropriate error, we choose the error thrown by std::vector
89 throw std::out_of_range("const WeightContainer::operator[] ERROR: string "+s+" not found in WeightContainer" );
90}
91
92bool WeightContainer::operator==( const WeightContainer & other ) const
93{
94 if( size() != other.size() ) { return false; }
95 if( m_names != other.m_names ) { return false; }
96 if( m_weights != other.m_weights ) { return false; }
97 return true;
98}
99
100bool WeightContainer::operator!=( const WeightContainer & other ) const
101{
102 return !(*this == other );
103}
104
105bool WeightContainer::has_key( const std::string& s ) const
106{
107 // look up the name in the map
108 return m_names.find(s) != m_names.end();
109}
110
111void WeightContainer::print( std::ostream& ostr ) const
112{
113 // print a name, weight pair
114 for ( const_map_iterator m = map_begin(); m != map_end(); ++m )
115 {
116 ostr << "(" << m->first << "," << m_weights[m->second] << ") ";
117 }
118 ostr << std::endl;
119}
120
121void WeightContainer::write( std::ostream& ostr ) const
122{
123 size_type count = 0;
124 for ( const_iterator w = begin(); w != end(); ++w )
125 {
126 std::string name;
127 for ( const_map_iterator m = map_begin(); m != map_end(); ++m )
128 {
129 if( m->second == count ) name = m->first;
130 }
131 ostr << "Weight " << std::setw(4) << count
132 << " with name " << std::setw(10) << name
133 << " is " << *w << std::endl;
134 ++count;
135 }
136}
137
138} // HepMC
139
Note: See TracBrowser for help on using the repository browser.