ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/ValueClass.C
Revision: 1.1
Committed: Wed Jun 22 11:07:37 2011 UTC (13 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
Initial commit of Plotting tools

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <TMath.h>
3    
4     using namespace std;
5    
6    
7     /*
8     *
9     * This file contains the class "Unit", the version is 1.0
10     * This class stores the value and error of a given parameter and is able to perform error propagation for addition, subtraction, multiplication, division and even powers. Please add any bugs you find to the change log below.
11     *
12     * V1.0
13     * - First implementation with operators +,-,*,/ and pow; also implemented a custom "<<" operator (i.e. overloaded it)
14     *
15     */
16    
17     class Value
18     {
19     float value;
20     float error;
21    
22     public:
23     Value(float value, float error);
24     float getValue();
25     float getError();
26     Value operator+(const Value& c);
27     Value operator-(const Value& c);
28     Value operator/(const Value& c);
29     Value operator*(const Value& c);
30     Value Pow(Value a, Value b);
31     };
32    
33     Value::Value(float newvalue = 0, float newerror = -999) : value(newvalue) {
34     if(newerror!=-999) {
35     error=newerror;
36     } else {
37     error = TMath::Sqrt(newvalue);
38     }
39     }
40    
41     float Value::getValue()
42     {
43     return value;
44     }
45     float Value::getError()
46     {
47     return error;
48     }
49    
50     Value Value::operator+(const Value& c)
51     {//verified
52     Value result;
53     result.value = (this->value+c.value);
54     result.error = TMath::Sqrt((this->error)*(this->error)+(c.error)*(c.error));
55     return result;
56     }
57    
58     Value Value::operator-(const Value& c)
59     {//verified
60     Value result;
61     result.value = (this->value-c.value);
62     result.error = TMath::Sqrt((this->error)*(this->error)+(c.error)*(c.error));
63     return result;
64     }
65    
66     Value Value::operator*(const Value& c)
67     {//verified
68     Value result;
69     result.value = (this->value*c.value);
70     result.error = TMath::Sqrt(c.value*c.value*(this->error)*(this->error)+(this->value)*(this->value)*(c.error)*(c.error));
71     return result;
72     }
73    
74     Value Value::operator/(const Value& c)
75     {//verified
76     Value result;
77     result.value = (this->value/c.value);
78     result.error = TMath::Sqrt((1/(c.value*c.value))*(this->error)*(this->error) + ((this->value/(c.value*c.value))*(this->value/(c.value*c.value)))*(c.error*c.error));
79     return result;
80     }
81    
82     Value Value::Pow(Value a, Value b)
83     {
84     //this calculates e.g. sqrt(a), where b=(0.5,0)
85     Value result;
86     result.value = TMath::Power(a.value,b.value);
87     result.error=TMath::Power(a.value,b.value)*TMath::Sqrt(TMath::Power(a.value,-2)*(b.value-1)*(b.value-1)*(a.error*a.error)+(TMath::Log(a.value)*TMath::Log(a.value))*(b.error*b.error));
88     return result;
89     }
90    
91     std::ostream &operator<<(std::ostream &ostr, Value v)
92     {//this leads to an output like 2.000 +/- 1.000
93     return ostr << v.getValue() << " +/- " << v.getError();
94     }
95    
96     /*
97    
98     int main()
99     {
100     cout << "This is my value class" << endl;
101     Value a(2,2);
102     Value b(16,7);
103     Value sq(0.5);
104     Value c = a/b;
105     Value d = d.Pow(b,sq);
106     cout << c << endl;
107     cout << "log 11 " << TMath::Log(11) << endl;
108     cout << "Power: " << d << endl;
109     return 0;
110     }
111     */