]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TEvtGen/EvtGenBase/EvtValError.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / TEvtGen / EvtGenBase / EvtValError.cxx
CommitLineData
da0e9ce3 1#include "EvtGenBase/EvtPatches.hh"
2/*******************************************************************************
3 * Project: BaBar detector at the SLAC PEP-II B-factory
4 * Package: EvtGenBase
5 * File: $Id: EvtValError.cc,v 1.7 2004/12/21 19:58:50 ryd Exp $
6 * Author: Alexei Dvoretskii, dvoretsk@slac.stanford.edu, 2001-2002
7 *
8 * Copyright (C) 2002 Caltech
9 *******************************************************************************/
10
11#include <assert.h>
12#include <math.h>
13#include <iostream>
14#include "EvtGenBase/EvtValError.hh"
15using std::endl;
16using std::ostream;
17
18EvtValError::EvtValError()
19 : _valKnown(0), _val(0.), _errKnown(0), _err(0.)
20{}
21
22EvtValError::EvtValError(double val)
23 : _valKnown(1), _val(val), _errKnown(0), _err(0.)
24{}
25
26EvtValError::EvtValError(double val, double err)
27 : _valKnown(1), _val(val), _errKnown(1), _err(err)
28{}
29
30EvtValError::EvtValError(const EvtValError& other)
31 : _valKnown(other._valKnown), _val(other._val),
32 _errKnown(other._errKnown), _err(other._err)
33{}
34
35EvtValError::~EvtValError()
36{}
37
38double EvtValError::prec() const
39{
40 assert(_valKnown && _errKnown);
41 return ( _val != 0) ? _err/_val : 0;
42}
43
44void EvtValError::operator=(const EvtValError& other)
45{
46 _valKnown = other._valKnown;
47 _val = other._val;
48 _errKnown = other._errKnown;
49 _err = other._err;
50}
51
52void EvtValError::operator*=(const EvtValError& other)
53{
54 assert(_valKnown && other._valKnown);
55
56 // Relative errors add in quadrature
57 if(_errKnown && other._errKnown)
58 _err = _val * other._val * sqrt(prec()*prec() + other.prec() * other.prec());
59 else _errKnown = 0;
60
61 // Modify the value
62 _val *= other._val;
63}
64
65void EvtValError::operator/=(const EvtValError& other)
66{
67 assert(_valKnown && other._valKnown && other._val != 0.);
68
69 // Relative errors add in quadrature
70 if(_errKnown && other._errKnown)
71 _err = _val/other._val * sqrt(prec()*prec() + other.prec() * other.prec());
72 else _errKnown = 0;
73
74 // Modify the value
75 _val /= other._val;
76}
77
78
79void EvtValError::print(ostream& os) const
80{
81 if(_valKnown) os << _val;
82 else os << "Undef";
83 os << " +/- ";
84 if(_errKnown) os << _err;
85 else os << "Undef";
86 os << endl;
87}
88
89
90void EvtValError::operator+=(const EvtValError& other)
91{
92 assert(_valKnown); assert(other._valKnown);
93 _val += other._val;
94
95 // add errors in quadrature
96
97 if(_errKnown && other._errKnown) {
98
99 _err = sqrt(_err*_err + other._err*other._err);
100 }
101 else {
102
103 _errKnown = 0;
104 }
105}
106
107void EvtValError::operator*=(double c) {
108
109 assert(_valKnown);
110 _val *= c;
111 if(_errKnown) _err*=c;
112}
113
114
115EvtValError operator*(const EvtValError& x1, const EvtValError& x2)
116{
117 EvtValError ret(x1);
118 ret *= x2;
119 return ret;
120}
121
122EvtValError operator/(const EvtValError& x1, const EvtValError& x2)
123{
124 EvtValError ret(x1);
125 ret /= x2;
126 return ret;
127}
128
129
130EvtValError operator+(const EvtValError& x1, const EvtValError& x2)
131{
132 EvtValError ret(x1);
133 ret += x2;
134 return ret;
135}
136
137
138EvtValError operator*(const EvtValError& x,double c)
139{
140 EvtValError ret(x);
141 ret*=c;
142 return ret;
143}
144
145
146EvtValError operator*(double c,const EvtValError& x)
147{
148 EvtValError ret(x);
149 ret*=c;
150 return ret;
151}
152
153
154ostream& operator<<(ostream& os, const EvtValError& other)
155{
156 other.print(os);
157 return os;
158}
159
160