00001 #include "RunTimesFactory.h"
00002
00005 class RunTimesAscii : public RunTimes
00006 {
00007 protected:
00008 virtual bool Init();
00009 };
00010
00011 namespace
00012 {
00013 RunTimes* creator()
00014 {
00015 return new RunTimesAscii;
00016 }
00017
00018 const std::string name = "Ascii";
00019 const bool registered =
00020 RunTimesFactory::instance().registerCreator(name,
00021 creator,
00022 "RunTimes");
00023
00024
00025 PHTimeStamp
00026 BuildTimeStamp(const std::string& thedate)
00027 {
00028 static std::map<std::string, int> months;
00029
00030 if (months.empty())
00031 {
00032 months["Jan"] = 1;
00033 months["Feb"] = 2;
00034 months["Mar"] = 3;
00035 months["Apr"] = 4;
00036 months["May"] = 5;
00037 months["Jun"] = 6;
00038 months["Jul"] = 7;
00039 months["Aug"] = 8;
00040 months["Sep"] = 9;
00041 months["Oct"] = 10;
00042 months["Nov"] = 11;
00043 months["Dec"] = 12;
00044 }
00045
00046 std::string smonth = thedate.substr(4, 3);
00047 std::string sday = thedate.substr(8, 2);
00048 std::string shour = thedate.substr(11, 2);
00049 std::string smin = thedate.substr(14, 2);
00050 std::string ssec = thedate.substr(17, 2);
00051 std::string syear = thedate.substr(20, 4);
00052
00053 int year = atoi(syear.c_str());
00054 int month = months[smonth];
00055 int day = atoi(sday.c_str());
00056 int hour = atoi(shour.c_str());
00057 int min = atoi(smin.c_str());
00058 int sec = atoi(ssec.c_str());
00059
00060 return PHTimeStamp(year, month, day, hour, min, sec, 0);
00061 }
00062
00063 }
00064
00065 #include "emcDefines.h"
00066 #include <fstream>
00067
00068 using namespace std;
00069
00070
00071 bool
00072 RunTimesAscii::Init()
00073 {
00074 ifstream in(fFilename.c_str());
00075 if (!in)
00076 {
00077 cerr << EMC_ERROR_MSG << "RunTimes::InitFromFile : cannot open "
00078 << fFilename << endl;
00079 return false;
00080 }
00081
00082 int n = 0;
00083
00084 PHTimeStamp future(2010, 1, 1, 0, 0, 0, 0);
00085
00086 char s[80];
00087 string str;
00088
00089 while ( in.getline(s, 80, '\n') )
00090 {
00091
00092 str = s;
00093
00094 if (str[0] == '#')
00095 continue;
00096
00097 size_t p1 = str.find(',');
00098 size_t p2 = str.find_last_of(',');
00099
00100 string srun = str.substr(0, p1);
00101 string st1 = str.substr(p1 + 1, p2 - p1 - 1);
00102 string st2 = str.substr(p2 + 1);
00103
00104 PHTimeStamp t1, t2;
00105 t1 = BuildTimeStamp(st1);
00106 t2 = BuildTimeStamp(st2);
00107 int run = atoi(srun.c_str());
00108
00109 fTimes[run] = new RunLite(run, t1, t2);
00110
00111 n++;
00112 }
00113
00114 in.close();
00115
00116 cout << EMC_INFO_MSG << "RunTimes::InitFromFile : Successfully read "
00117 << n << " runs from file " << fFilename << endl;
00118
00119 fInit = true;
00120
00121 return true;
00122 }