diff --git a/Data.cc b/Data.cc new file mode 100644 index 0000000..ddb48af --- /dev/null +++ b/Data.cc @@ -0,0 +1,41 @@ +#include "Data.hh" + +#include +#include +#include + +using namespace std; + +Data::Data(const std::string& filename) { + // read data from file + // return 1 if successful, 0 if otherwise + + // open the file + ifstream file(filename); + + // check if file opened correctly + if (!file.is_open()) { + throw "Error could not open file with name: " + filename; + } + + int size = -1; + file >> size; + + // read in data from file: m_siz+1 bin edges + for (int i = 0; i < size + 1; i++) { + double edge; + file >> edge; + m_bins.push_back(edge); + } + // read in data from file: m_siz bin contents + for (int i = 0; i < size; i++) { + double entries; + file >> entries; + m_data.push_back(entries); + } + + // done! close the file + file.close(); +}; + +void Data::assertSizes() { assert(m_data.size() + 1 == m_bins.size()); } diff --git a/Data.hh b/Data.hh new file mode 100644 index 0000000..e1d0234 --- /dev/null +++ b/Data.hh @@ -0,0 +1,25 @@ +#ifndef DATA_HH +#define DATA_HH + +#include +#include + +class Data { + public: + Data(const std::string& filename); + + unsigned int size() const { return m_data.size(); } + double measurement(int i) const { return m_data[i]; } + double binCenter(int i) const { return 0; } + double binLow(int i) const { return 0; } + double binHigh(int i) const { return 0; } + double error(int i) const { return 0; } + + private: + Data() {} // disallow empty data + void assertSizes(); + std::vector m_data; + std::vector m_bins; +}; + +#endif diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..87ccc2f --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +# Makefile for intro to C++ + +CPP=g++ +LD=g++ + +CFLAGS = -std=c++11 +LFLAGS = + +RCXX=$(CFLAGS) $(ROOTCFLAGS) +RLXX=$(LFLAGS) $(ROOTLIBS) + +SRC1= Data.o + +%.o: %.cc %.hh + $(CPP) $(RCXX) -c $< + +all: run + +run.o: run.cc Data.hh + $(CPP) $(RCXX) -c $< + +run: $(SRC1) run.o + $(LD) $(SRC1) run.o $(RLXX) -o run + @echo '-> run created!' + +clean: + @rm -f *~ + @rm -f *.o + @rm -f *.x diff --git a/exc2/Data.cc b/exc2/Data.cc new file mode 100644 index 0000000..c6daab6 --- /dev/null +++ b/exc2/Data.cc @@ -0,0 +1,80 @@ +#include "Data.hh" + +#include +#include +#include + +using namespace std; + +Data::Data(const std::string& filename) { + // read data from file + // return 1 if successful, 0 if otherwise + + m_name = filename; + + // open the file + ifstream file(filename); + + // check if file opened correctly + if (!file.is_open()) { + throw "Error could not open file with name: " + filename; + } + + int size = -1; + file >> size; + + // read in data from file: m_siz+1 bin edges + for (int i = 0; i < size + 1; i++) { + double edge; + file >> edge; + m_bins.push_back(edge); + } + // read in data from file: m_siz bin contents + for (int i = 0; i < size; i++) { + double entries; + file >> entries; + m_data.push_back(entries); + } + + for (int i = 0; i < size; i++) { + double uncertainty; + file >> uncertainty; + m_uncertainty.push_back(uncertainty); + } + + // done! close the file + file.close(); +}; + +int Data::checkCompatibility(const Data* in, int N) const { + assert(in->size() == size()); + int fails = 0; + + for (int i = 0; i < size(); i++) { + if (abs(in->measurement(i) - measurement(i)) > N * (in->uncertainty(i) + + uncertainty(i))) { + fails++; + } + } + return fails; +} + +Data operator+(const Data& a, const Data& b) { + + assert(a.checkCompatibility(&b, 3) == 0); + vector avg_y; + vector avg_w; + for (unsigned int i = 0; i < a.size(); i++) { + double w1 = 1/(a.uncertainty(i) * a.uncertainty(i)); + double w2 = 1/(b.uncertainty(i) * b.uncertainty(i)); + avg_y.push_back((w1 * a.measurement(i) + w2 * b.measurement(i)) / (w1 + w2)); + avg_w.push_back(sqrt(1/(w1 + w2))); + } + Data d; + d.m_bins = a.m_bins; + d.m_data = avg_y; + d.m_uncertainty = avg_w; + return d; +} + +void Data::assertSizes() { assert(m_data.size() + 1 == m_bins.size()); } diff --git a/exc2/Data.hh b/exc2/Data.hh new file mode 100644 index 0000000..e843a65 --- /dev/null +++ b/exc2/Data.hh @@ -0,0 +1,34 @@ +#ifndef DATA_HH +#define DATA_HH + +#include +#include +#include + +class Data { + public: + Data(const std::string& filename); + + unsigned int size() const { return m_data.size(); } + double measurement(int i) const { return m_data[i]; } + double uncertainty(int i) const { return m_uncertainty[i]; } + double binCenter(int i) const { return 0; } + double binLow(int i) const { return 0; } + double binHigh(int i) const { return 0; } + double error(int i) const { return 0; } + std::string name() const { return m_name; } + + int checkCompatibility(const Data* in, int N) const; + + friend Data operator+(const Data& a, const Data& b); + + private: + Data() {} // disallow empty data + void assertSizes(); + std::string m_name; + std::vector m_data; + std::vector m_uncertainty; + std::vector m_bins; +}; + +#endif diff --git a/exc2/Data.o b/exc2/Data.o new file mode 100644 index 0000000..b41256e Binary files /dev/null and b/exc2/Data.o differ diff --git a/exc2/Makefile b/exc2/Makefile new file mode 100644 index 0000000..87ccc2f --- /dev/null +++ b/exc2/Makefile @@ -0,0 +1,29 @@ +# Makefile for intro to C++ + +CPP=g++ +LD=g++ + +CFLAGS = -std=c++11 +LFLAGS = + +RCXX=$(CFLAGS) $(ROOTCFLAGS) +RLXX=$(LFLAGS) $(ROOTLIBS) + +SRC1= Data.o + +%.o: %.cc %.hh + $(CPP) $(RCXX) -c $< + +all: run + +run.o: run.cc Data.hh + $(CPP) $(RCXX) -c $< + +run: $(SRC1) run.o + $(LD) $(SRC1) run.o $(RLXX) -o run + @echo '-> run created!' + +clean: + @rm -f *~ + @rm -f *.o + @rm -f *.x diff --git a/exc2/exp_A b/exc2/exp_A new file mode 100644 index 0000000..2a907f7 --- /dev/null +++ b/exc2/exp_A @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0654 0.0566 0.0552 0.0524 0.0484 0.0424 0.0404 0.0384 0.0364 0.0316 0.0328 0.0318 0.0286 0.0268 0.0244 0.0218 0.0216 0.0224 0.0166 0.0166 0.0174 0.0154 0.017 0.0126 0.0118 0.0116 0.0114 0.0112 0.0138 0.0126 0.012 0.0118 0.0122 0.01 0.0084 0.0066 0.005 0.0068 0.0066 0.0074 0.007 0.0054 0.0052 0.0044 0.0032 0.0054 0.0042 0.0032 0.0048 0.0046 0.0046 0.0038 0.0036 0.0026 0.003 0.0028 +0.00397829 0.00370097 0.00365491 0.00356101 0.0034224 0.00320325 0.00312679 0.00304841 0.00296796 0.00276536 0.00281737 0.00277409 0.00263082 0.00254668 0.00242998 0.00229687 0.00228631 0.00232826 0.0020043 0.0020043 0.00205202 0.00193049 0.0020283 0.0017462 0.00168985 0.00167547 0.00166096 0.00164633 0.00182746 0.0017462 0.00170411 0.00168985 0.00171825 0.00155563 0.00142576 0.0012638 0.0011 0.00128281 0.0012638 0.00133821 0.00130154 0.00114315 0.00112178 0.00103189 0.00088 0.00114315 0.00100817 0.00088 0.00107778 0.00105508 0.00105508 0.000958958 0.000933381 0.000793221 0.000852056 0.000823165 diff --git a/exc2/exp_B b/exc2/exp_B new file mode 100644 index 0000000..0ad91a6 --- /dev/null +++ b/exc2/exp_B @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0506 0.045 0.0476 0.0434 0.038 0.0392 0.0398 0.0362 0.0344 0.0322 0.0336 0.0266 0.0242 0.0244 0.0254 0.0218 0.026 0.019 0.017 0.0184 0.0178 0.0202 0.0156 0.017 0.0136 0.0164 0.011 0.0134 0.0136 0.0174 0.014 0.012 0.0112 0.0116 0.0108 0.0082 0.0102 0.009 0.0102 0.0078 0.0086 0.0082 0.0074 0.0092 0.0076 0.0064 0.006 0.005 0.0056 0.005 0.0054 0.0044 0.0046 0.004 0.0048 0.004 +0.00572615 0.0054 0.00555381 0.00530313 0.00496226 0.00504 0.00507842 0.0048433 0.00472136 0.00456789 0.00466613 0.00415172 0.00396 0.00397633 0.00405699 0.00375851 0.00410463 0.00350885 0.00331904 0.003453 0.00339623 0.00361796 0.00317943 0.00331904 0.00296864 0.00325994 0.00266983 0.00294673 0.00296864 0.00335786 0.00301198 0.00278855 0.00269399 0.00274168 0.00264545 0.00230512 0.00257091 0.00241495 0.00257091 0.0022482 0.00236068 0.00230512 0.00218979 0.00244164 0.00221919 0.00203647 0.0019718 0.0018 0.00190494 0.0018 0.00187061 0.00168855 0.0017265 0.00160997 0.00176363 0.00160997 diff --git a/exc2/exp_C b/exc2/exp_C new file mode 100644 index 0000000..afe692c --- /dev/null +++ b/exc2/exp_C @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0638 0.0588 0.0536 0.0476 0.0434 0.0498 0.0486 0.0428 0.0376 0.036 0.0304 0.0304 0.0284 0.0256 0.0258 0.0196 0.0204 0.019 0.0206 0.0204 0.0172 0.0158 0.0104 0.0136 0.0148 0.0138 0.014 0.011 0.0108 0.014 0.015 0.0144 0.0088 0.0074 0.009 0.0078 0.0042 0.008 0.0072 0.0052 0.0038 0.0036 0.0052 0.0046 0.0034 0.0044 0.0036 0.004 0.0032 0.0026 0.0046 0.003 0.0038 0.0016 0.0016 0.002 +0.00500096 0.004801 0.0045838 0.00431963 0.00412466 0.00441833 0.00436477 0.00409605 0.00383917 0.00375659 0.00345207 0.00345207 0.00333659 0.00316784 0.00318019 0.00277186 0.00282786 0.0027291 0.00284169 0.00282786 0.00259661 0.00248869 0.00201911 0.00230894 0.00240865 0.00232585 0.00234265 0.00207654 0.00205757 0.00234265 0.00242487 0.00237588 0.00185731 0.00170317 0.0018783 0.0017486 0.00128312 0.00177088 0.00168 0.00142773 0.00122049 0.00118794 0.00142773 0.00134283 0.00115447 0.00131332 0.00118794 0.0012522 0.00112 0.00100955 0.00134283 0.00108444 0.00122049 0.00079196 0.00079196 0.000885438 diff --git a/exc2/exp_D b/exc2/exp_D new file mode 100644 index 0000000..8772125 --- /dev/null +++ b/exc2/exp_D @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0756 0.065 0.063 0.0566 0.0538 0.0508 0.0438 0.0388 0.031 0.0324 0.0302 0.0284 0.0244 0.0242 0.0194 0.0226 0.0168 0.018 0.015 0.0138 0.0144 0.0126 0.0126 0.0098 0.0086 0.0116 0.0098 0.0096 0.0148 0.0136 0.012 0.0136 0.0106 0.009 0.0076 0.0064 0.0064 0.0066 0.0064 0.0044 0.0046 0.005 0.0044 0.005 0.0048 0.0056 0.0034 0.0058 0.0058 0.0054 0.0052 0.004 0.0044 0.003 0.0034 0.0062 +0.00583267 0.00540833 0.00532447 0.00504678 0.00492037 0.00478121 0.00443959 0.00417852 0.00373497 0.00381838 0.00368646 0.00357491 0.00331361 0.0033 0.00295466 0.00318904 0.00274955 0.00284605 0.00259808 0.00249199 0.00254558 0.00238118 0.00238118 0.0021 0.00196723 0.00228473 0.0021 0.00207846 0.0025807 0.00247386 0.00232379 0.00247386 0.00218403 0.00201246 0.00184932 0.00169706 0.00169706 0.00172337 0.00169706 0.00140712 0.00143875 0.0015 0.00140712 0.0015 0.00146969 0.00158745 0.00123693 0.00161555 0.00161555 0.00155885 0.00152971 0.00134164 0.00140712 0.0011619 0.00123693 0.00167033 diff --git a/exc2/run b/exc2/run new file mode 100755 index 0000000..a9909e2 Binary files /dev/null and b/exc2/run differ diff --git a/exc2/run.cc b/exc2/run.cc new file mode 100644 index 0000000..3ce3620 --- /dev/null +++ b/exc2/run.cc @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +#include "Data.hh" + +// generic function comparing two values of some type T +template +bool testEqual(const std::string& name, T expected, T real) { + if (expected != real) { + std::cout << "(" << name << ": <" << expected << ">" + << ", " << real << ") "; + return false; + } + return true; +} + +bool testReadingSize() { + std::cout << "testReadingSize..."; + Data datA("testA"); + return testEqual("size", 1, datA.size()); +} + +bool testReadingMeasurement() { + std::cout << "testReadingMeasurement..."; + Data datA("testA"); + return testEqual("measurement", 10., datA.measurement(0)); +} + +bool testReadingBinEdges() { + std::cout << "testReadingBinEdges..."; + Data datA("testA"); + return testEqual("bin low", 0., datA.binLow(0)) && + testEqual("bin high", 1., datA.binHigh(0)); +} + +bool testReadingErrors() { + std::cout << "testReadingErrors..."; + Data datA("testA"); + return testEqual("error", 2., datA.error(0)); +} + +bool testCopyConstructor() { + std::cout << "testCopyConstructor..."; + Data a("testA"); + Data b(a); + Data c = a; // equivalent to Data c(a) + return testEqual("measurement", 10., b.measurement(0)) && + testEqual("measurement", 10., c.measurement(0)); +} + +void runTests() { + std::cout << "running tests...\n"; + std::vector > tests( + {testReadingSize, testReadingMeasurement, testReadingBinEdges, + testReadingErrors, testCopyConstructor}); + for (auto test : tests) + std::cout << (test() ? " ok" : " FAILED!") << std::endl; +} + +int main() { + using namespace std; + + cout << "******************************************************" << endl; + runTests(); + cout << "******************************************************" << endl; + // create an object which holds data of experiment A + Data datA("exp_A"); + Data datB("exp_B"); + Data datC("exp_C"); + Data datD("exp_D"); + vector datas; + datas.push_back(datA); + datas.push_back(datB); + datas.push_back(datC); + datas.push_back(datD); + + // here is the data from experiment A + cout << "bin 0: from " << datA.binLow(0) << " to " << datA.binHigh(1) << endl; + cout << "measurement of experiment A in bin 0: " << datA.measurement(0) + << endl; + + for(auto data : datas) { + cout << "measurement of " << data.name() << " in bin 27: " + << data.measurement(27) << " +- " << data.uncertainty(27) << endl; + for (auto data2 : datas) { + if (abs(data.measurement(27) - data2.measurement(27)) > + data.uncertainty(27) + data2.uncertainty(27)) { + cout << "Data point doesnt agree" << endl; + } + if (data.name() != data2.name()) { + int fails = data.checkCompatibility(&data2, 3); + cout << "Fails between " << data.name() << " and " << data2.name() << + ": " << fails << endl; + } + } + } + Data dat_avg = datA + datB + datC + datD; + cout << "Combined data: " << dat_avg.measurement(27) << endl + << "Combined uncertainty: " << dat_avg.uncertainty(27) << endl; + + return 0; +} diff --git a/exc2/run.o b/exc2/run.o new file mode 100644 index 0000000..abac25c Binary files /dev/null and b/exc2/run.o differ diff --git a/exc2/testA b/exc2/testA new file mode 100644 index 0000000..b0556a7 --- /dev/null +++ b/exc2/testA @@ -0,0 +1,4 @@ +1 +0 1 +10 +2 diff --git a/exc2/testB b/exc2/testB new file mode 100644 index 0000000..87b5d48 --- /dev/null +++ b/exc2/testB @@ -0,0 +1,5 @@ +1 +0 1 +13 +0 1 + diff --git a/exp_A b/exp_A new file mode 100644 index 0000000..2a907f7 --- /dev/null +++ b/exp_A @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0654 0.0566 0.0552 0.0524 0.0484 0.0424 0.0404 0.0384 0.0364 0.0316 0.0328 0.0318 0.0286 0.0268 0.0244 0.0218 0.0216 0.0224 0.0166 0.0166 0.0174 0.0154 0.017 0.0126 0.0118 0.0116 0.0114 0.0112 0.0138 0.0126 0.012 0.0118 0.0122 0.01 0.0084 0.0066 0.005 0.0068 0.0066 0.0074 0.007 0.0054 0.0052 0.0044 0.0032 0.0054 0.0042 0.0032 0.0048 0.0046 0.0046 0.0038 0.0036 0.0026 0.003 0.0028 +0.00397829 0.00370097 0.00365491 0.00356101 0.0034224 0.00320325 0.00312679 0.00304841 0.00296796 0.00276536 0.00281737 0.00277409 0.00263082 0.00254668 0.00242998 0.00229687 0.00228631 0.00232826 0.0020043 0.0020043 0.00205202 0.00193049 0.0020283 0.0017462 0.00168985 0.00167547 0.00166096 0.00164633 0.00182746 0.0017462 0.00170411 0.00168985 0.00171825 0.00155563 0.00142576 0.0012638 0.0011 0.00128281 0.0012638 0.00133821 0.00130154 0.00114315 0.00112178 0.00103189 0.00088 0.00114315 0.00100817 0.00088 0.00107778 0.00105508 0.00105508 0.000958958 0.000933381 0.000793221 0.000852056 0.000823165 diff --git a/exp_B b/exp_B new file mode 100644 index 0000000..0ad91a6 --- /dev/null +++ b/exp_B @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0506 0.045 0.0476 0.0434 0.038 0.0392 0.0398 0.0362 0.0344 0.0322 0.0336 0.0266 0.0242 0.0244 0.0254 0.0218 0.026 0.019 0.017 0.0184 0.0178 0.0202 0.0156 0.017 0.0136 0.0164 0.011 0.0134 0.0136 0.0174 0.014 0.012 0.0112 0.0116 0.0108 0.0082 0.0102 0.009 0.0102 0.0078 0.0086 0.0082 0.0074 0.0092 0.0076 0.0064 0.006 0.005 0.0056 0.005 0.0054 0.0044 0.0046 0.004 0.0048 0.004 +0.00572615 0.0054 0.00555381 0.00530313 0.00496226 0.00504 0.00507842 0.0048433 0.00472136 0.00456789 0.00466613 0.00415172 0.00396 0.00397633 0.00405699 0.00375851 0.00410463 0.00350885 0.00331904 0.003453 0.00339623 0.00361796 0.00317943 0.00331904 0.00296864 0.00325994 0.00266983 0.00294673 0.00296864 0.00335786 0.00301198 0.00278855 0.00269399 0.00274168 0.00264545 0.00230512 0.00257091 0.00241495 0.00257091 0.0022482 0.00236068 0.00230512 0.00218979 0.00244164 0.00221919 0.00203647 0.0019718 0.0018 0.00190494 0.0018 0.00187061 0.00168855 0.0017265 0.00160997 0.00176363 0.00160997 diff --git a/exp_C b/exp_C new file mode 100644 index 0000000..afe692c --- /dev/null +++ b/exp_C @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0638 0.0588 0.0536 0.0476 0.0434 0.0498 0.0486 0.0428 0.0376 0.036 0.0304 0.0304 0.0284 0.0256 0.0258 0.0196 0.0204 0.019 0.0206 0.0204 0.0172 0.0158 0.0104 0.0136 0.0148 0.0138 0.014 0.011 0.0108 0.014 0.015 0.0144 0.0088 0.0074 0.009 0.0078 0.0042 0.008 0.0072 0.0052 0.0038 0.0036 0.0052 0.0046 0.0034 0.0044 0.0036 0.004 0.0032 0.0026 0.0046 0.003 0.0038 0.0016 0.0016 0.002 +0.00500096 0.004801 0.0045838 0.00431963 0.00412466 0.00441833 0.00436477 0.00409605 0.00383917 0.00375659 0.00345207 0.00345207 0.00333659 0.00316784 0.00318019 0.00277186 0.00282786 0.0027291 0.00284169 0.00282786 0.00259661 0.00248869 0.00201911 0.00230894 0.00240865 0.00232585 0.00234265 0.00207654 0.00205757 0.00234265 0.00242487 0.00237588 0.00185731 0.00170317 0.0018783 0.0017486 0.00128312 0.00177088 0.00168 0.00142773 0.00122049 0.00118794 0.00142773 0.00134283 0.00115447 0.00131332 0.00118794 0.0012522 0.00112 0.00100955 0.00134283 0.00108444 0.00122049 0.00079196 0.00079196 0.000885438 diff --git a/exp_D b/exp_D new file mode 100644 index 0000000..8772125 --- /dev/null +++ b/exp_D @@ -0,0 +1,4 @@ +56 +20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 +0.0756 0.065 0.063 0.0566 0.0538 0.0508 0.0438 0.0388 0.031 0.0324 0.0302 0.0284 0.0244 0.0242 0.0194 0.0226 0.0168 0.018 0.015 0.0138 0.0144 0.0126 0.0126 0.0098 0.0086 0.0116 0.0098 0.0096 0.0148 0.0136 0.012 0.0136 0.0106 0.009 0.0076 0.0064 0.0064 0.0066 0.0064 0.0044 0.0046 0.005 0.0044 0.005 0.0048 0.0056 0.0034 0.0058 0.0058 0.0054 0.0052 0.004 0.0044 0.003 0.0034 0.0062 +0.00583267 0.00540833 0.00532447 0.00504678 0.00492037 0.00478121 0.00443959 0.00417852 0.00373497 0.00381838 0.00368646 0.00357491 0.00331361 0.0033 0.00295466 0.00318904 0.00274955 0.00284605 0.00259808 0.00249199 0.00254558 0.00238118 0.00238118 0.0021 0.00196723 0.00228473 0.0021 0.00207846 0.0025807 0.00247386 0.00232379 0.00247386 0.00218403 0.00201246 0.00184932 0.00169706 0.00169706 0.00172337 0.00169706 0.00140712 0.00143875 0.0015 0.00140712 0.0015 0.00146969 0.00158745 0.00123693 0.00161555 0.00161555 0.00155885 0.00152971 0.00134164 0.00140712 0.0011619 0.00123693 0.00167033 diff --git a/run.cc b/run.cc new file mode 100644 index 0000000..a178113 --- /dev/null +++ b/run.cc @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#include "Data.hh" + +// generic function comparing two values of some type T +template +bool testEqual(const std::string& name, T expected, T real) { + if (expected != real) { + std::cout << "(" << name << ": <" << expected << ">" + << ", " << real << ") "; + return false; + } + return true; +} + +bool testReadingSize() { + std::cout << "testReadingSize..."; + Data datA("testA"); + return testEqual("size", 1, datA.size()); +} + +bool testReadingMeasurement() { + std::cout << "testReadingMeasurement..."; + Data datA("testA"); + return testEqual("measurement", 10., datA.measurement(0)); +} + +bool testReadingBinEdges() { + std::cout << "testReadingBinEdges..."; + Data datA("testA"); + return testEqual("bin low", 0., datA.binLow(0)) && + testEqual("bin high", 1., datA.binHigh(0)); +} + +bool testReadingErrors() { + std::cout << "testReadingErrors..."; + Data datA("testA"); + return testEqual("error", 2., datA.error(0)); +} + +bool testCopyConstructor() { + std::cout << "testCopyConstructor..."; + Data a("testA"); + Data b(a); + Data c = a; // equivalent to Data c(a) + return testEqual("measurement", 10., b.measurement(0)) && + testEqual("measurement", 10., c.measurement(0)); +} + +void runTests() { + std::cout << "running tests...\n"; + std::vector > tests( + {testReadingSize, testReadingMeasurement, testReadingBinEdges, + testReadingErrors, testCopyConstructor}); + for (auto test : tests) + std::cout << (test() ? " ok" : " FAILED!") << std::endl; +} + +int main() { + using namespace std; + + cout << "******************************************************" << endl; + runTests(); + cout << "******************************************************" << endl; + // create an object which holds data of experiment A + Data datA("exp_A"); + + // here is the data from experiment A + cout << "bin 0: from " << datA.binLow(0) << " to " << datA.binHigh(1) << endl; + cout << "measurement of experiment A in bin 0: " << datA.measurement(0) + << endl; + + return 0; +} diff --git a/testA b/testA new file mode 100644 index 0000000..b0556a7 --- /dev/null +++ b/testA @@ -0,0 +1,4 @@ +1 +0 1 +10 +2 diff --git a/testB b/testB new file mode 100644 index 0000000..87b5d48 --- /dev/null +++ b/testB @@ -0,0 +1,5 @@ +1 +0 1 +13 +0 1 +