~uhh-ssd/+junk/Alibava_Analysis

« back to all changes in this revision

Viewing changes to Helperfunctions.h

  • Committer: jo-erfle
  • Date: 2011-12-31 14:07:56 UTC
  • Revision ID: joachim.erfle@desy.de-20111231140756-u370m5d7jokvld1x
fitting landau-gauss working, messy code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * File:   helperfunctions.h
 
3
 * Author: erfle
 
4
 *
 
5
 * Created on October 14, 2010, 3:17 PM
 
6
 */
 
7
 
 
8
#ifndef HELPERFUNCTIONS_H
 
9
#define HELPERFUNCTIONS_H
 
10
#define CET 1;
 
11
#define CEST 2;
 
12
 
 
13
#include <string>
 
14
#include <sstream>
 
15
#include <fstream>
 
16
#include <iostream>
 
17
#include <iomanip>
 
18
#include <locale>
 
19
#include <exception>
 
20
 
 
21
class Helperfunctions
 
22
{
 
23
    protected:
 
24
            /// returns a string with the content given in x.
 
25
        template<typename T>
 
26
        inline std::string stringify(T const& x)
 
27
        {
 
28
                std::ostringstream o;
 
29
                //o << x;
 
30
                if (!(o << x))
 
31
                std::cout<< "couldn't convert to string!"<<std::endl;
 
32
                        //throw BadConversion(std::string("stringify(")+ typeid(x).name() + ")");
 
33
                return o.str();
 
34
        }
 
35
            /// returns the value of s, with the give type T.
 
36
        template<typename T>
 
37
        inline T convertTo(std::string const& s,bool failIfLeftoverChars = false)
 
38
        {
 
39
                std::istringstream i(s);
 
40
                char c;
 
41
                T x;
 
42
                if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
 
43
                        std::cout<<"couldn't convert from string!"<<std::endl;
 
44
                        //err_msg( "couldn't convert from string!");
 
45
                        //throw BadConversion("couldn't convert from string!");
 
46
                return x;
 
47
        }
 
48
            /// appends a number to a string in the scheme: abc_1.
 
49
        std::string append_num(std::string s,int num)
 
50
        {
 
51
            std::stringstream buffer;
 
52
            buffer<<s<<"_"<<num;
 
53
            return buffer.str();
 
54
    }
 
55
 
 
56
        ///returns a string containing a 3 digit number followed by Hz, KHz, MHz respectively.
 
57
    std::string append_hertz(int freq) {
 
58
        std::string frequs;
 
59
        if (freq >= 1000000) {
 
60
            frequs.append(stringify(freq / 1000000));
 
61
            frequs.append("MHz");
 
62
        } else if (freq >= 1000) {
 
63
            frequs.append(stringify(freq / 1000));
 
64
            frequs.append("KHz");
 
65
        } else {
 
66
            frequs.append(stringify(freq));
 
67
            frequs.append("Hz");
 
68
        }
 
69
        return frequs;
 
70
    }
 
71
 
 
72
    ///returns a timestamp of the format 13102011152700 converted to GMT from CET or CEST respectively. Corrects for summer/wintertime. Uses defined CET and CEST offset value.
 
73
 
 
74
    std::string create_GMT(std::string date) {
 
75
        //create locale and time_conversion object
 
76
        std::locale loc; // "C" locale
 
77
#ifdef linux
 
78
        std::setlocale(LC_ALL, "EN_EN");
 
79
#endif
 
80
#ifndef linux
 
81
        //      setlocale(LC_ALL, "EN_EN");
 
82
#endif
 
83
        const std::time_get<char>& tmget = std::use_facet <std::time_get<char> > (loc);
 
84
        //create manipulator streams and reformat date-part
 
85
        std::ios::iostate state;
 
86
        std::stringstream iss1;
 
87
        iss1 << date.substr(5, 2) << "/" << date.substr(8, 2) << "/" << date.substr(0, 4);
 
88
        std::istringstream iss2(date.substr(11));
 
89
        std::istringstream iss3(date.substr(0, 4));
 
90
 
 
91
 
 
92
        //create iterators, used to read in the corresponding parts of the streams
 
93
        std::istreambuf_iterator<char> itbegin1(iss1); // beginning of iss
 
94
        std::istreambuf_iterator<char> itend1; // end-of-stream
 
95
        std::istreambuf_iterator<char> itbegin2(iss2); // beginning of iss
 
96
        std::istreambuf_iterator<char> itend2; // end-of-stream
 
97
        std::istreambuf_iterator<char> itbegin3(iss3); // beginning of iss
 
98
        std::istreambuf_iterator<char> itend3; // end-of-stream
 
99
 
 
100
        //create time_struct and fill it with the local time and date
 
101
        tm when;
 
102
          tmget.get_date(itbegin1, itend1, iss1, state, &when);
 
103
          tmget.get_year(itbegin3, itend3, iss3, state, &when);
 
104
        tmget.get_time(itbegin2, itend2, iss2, state, &when);
 
105
        
 
106
                int sec = convertTo<int>(date.substr(17, 2));
 
107
                int min = convertTo<int>(date.substr(14, 2));
 
108
        int hour = convertTo<int>(date.substr(11, 2));
 
109
        int mon = convertTo<int>(date.substr(5, 2));
 
110
        int year = convertTo<int>(date.substr(0, 4));
 
111
        int mday = convertTo<int>(date.substr(8, 2));
 
112
        int wday = weekday(mon, mday, when.tm_year);
 
113
                std::cout<<year<<mon<<mday<<hour<<min<<sec<<std::endl;
 
114
        //determine if Summertime
 
115
        if (mon < 3 || mon > 10) {
 
116
            hour -= CET;
 
117
        } else if (mon > 3 && mon < 10) {
 
118
            hour -= CEST;
 
119
        } else if (mon == 3)
 
120
            if ((mday - wday) > 24) {
 
121
                hour -= CEST;
 
122
            } else {
 
123
                hour -= CET;
 
124
            } else if (mon == 10)
 
125
            if ((mday - wday) > 24) {
 
126
                hour -= CET;
 
127
            } else {
 
128
                hour -= CEST;
 
129
            }
 
130
        //correct for false hours/days/month
 
131
        if (hour < 0) {
 
132
            hour += 24;
 
133
            mday - 1;
 
134
        }
 
135
        if (mday < 1) {
 
136
            mon--;
 
137
            if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
 
138
                mday += 30;
 
139
            } else if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10) {
 
140
                mday += 31;
 
141
            } else if (mon == 2) {
 
142
                if (!year % 4 && year % 100) {
 
143
                    mday += 29;
 
144
                } else {
 
145
                    mday += 28;
 
146
                }
 
147
            } else if (mon == 0) {
 
148
                mday += 31;
 
149
                mon = 12;
 
150
            } else
 
151
                std::cout << "This date sucks!" << std::endl;
 
152
        }
 
153
        //assemble Timestamp
 
154
                std::cout<<year<<mon<<mday<<hour<<min<<sec<<std::endl;
 
155
        iss1.str("");
 
156
        iss1 << std::setfill('0') << std::setw(4) << year << std::setw(2) << mon << std::setw(2) << mday << std::setw(2) << hour << std::setw(2) << min << std::setw(2) << sec;
 
157
        return iss1.str();
 
158
 
 
159
    }
 
160
        ///determines the weekday from a given date, ranging from 0 to 6, 
 
161
    int weekday(int month, int day, int year) {
 
162
        //determine the weekday. Didn't write it, just found it...
 
163
        int ix = 0, tx = 0, vx = 0;
 
164
 
 
165
        switch (month) {
 
166
            case 2:
 
167
            case 6: vx = 0;
 
168
                break;
 
169
            case 8: vx = 4;
 
170
                break;
 
171
            case 10: vx = 8;
 
172
                break;
 
173
            case 9:
 
174
            case 12: vx = 12;
 
175
                break;
 
176
            case 3:
 
177
            case 11: vx = 16;
 
178
                break;
 
179
            case 1:
 
180
            case 5: vx = 20;
 
181
                break;
 
182
            case 4:
 
183
            case 7: vx = 24;
 
184
                break;
 
185
        }
 
186
 
 
187
        if (year > 1900) // 1900 was not a leap year
 
188
            year -= 1900;
 
189
        ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
 
190
 
 
191
        tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
 
192
 
 
193
        return ((tx + 1) % 7);
 
194
    }
 
195
};
 
196
 
 
197
 
 
198
 
 
199
using namespace std;
 
200
 
 
201
class data_exception: public exception
 
202
{
 
203
  virtual const char* what() const throw()
 
204
  {
 
205
    return "Data exception: Element not found";
 
206
  }
 
207
};
 
208
 
 
209
#endif  /* HELPERFUNCTIONS_H */
 
210