source: trunk/MagicSoft/Mars/macros/sql/filldotrbk.C@ 4870

Last change on this file since 4870 was 4869, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// filldotrbk.C
28// ============
29//
30// This macro is used to read the central control runbook files from
31// the data center and store their contents in the runbook-database.
32//
33// Usage:
34// .x filldotrbk.C("/data/MAGIC/Period014", kTRUE)
35//
36// While the first argument is the directory in which all subdirectories where
37// searches for CC_*.rbk files. All these files were analysed and the runbook
38// entries will be put into the DB, eg:
39// "/data/MAGIC" would do it for all data
40// "/data/MAGIC/Period019/ccdata" would do it for one Period
41// "/data/MAGIC/Period019/ccdata/2004_05_17" would do it for a single day
42// "/data/MAGIC/Period019/ccdata/file.rbk" would do it for a single file
43//
44// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
45// switched on and nothing will be written into the database. This is usefull
46// for tests.
47//
48// Before an antry is added its existance is checked... if it is added already
49// it is ignored.
50//
51// The macro can also be run without ACLiC but this is a lot slower...
52//
53// Remark: Running it from the commandline looks like this:
54// root -q -l -b filldotrbk.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrbk.log
55//
56// Make sure, that database and password are corretly set in the macro.
57//
58// Returns 0 in case of failure and 1 in case of success.
59//
60///////////////////////////////////////////////////////////////////////////
61#include <iostream>
62#include <iomanip>
63#include <fstream>
64
65#include <TRegexp.h>
66
67#include <TSQLRow.h>
68#include <TSQLResult.h>
69
70#include "MDirIter.h"
71#include "MSQLServer.h"
72
73using namespace std;
74
75// --------------------------------------------------------------------------
76//
77// Checks whether an entry for this date is already existing
78//
79Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
80{
81 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
82 TSQLResult *res = serv.Query(query);
83 if (!res)
84 return kFALSE;
85
86 TSQLRow *row;
87
88 Bool_t rc = kFALSE;
89 while ((row=res->Next()))
90 {
91 if ((*row)[0])
92 {
93 rc = kTRUE;
94 break;
95 }
96 }
97
98 delete res;
99
100 return rc;
101}
102
103// --------------------------------------------------------------------------
104//
105// insert the entries from this runbook file into the database
106//
107int insert(MSQLServer &serv, Bool_t dummy, TString fname)
108{
109 //cout << endl;
110 //cout << "FILE: " << fname << endl;
111
112 ifstream fin(fname);
113 if (!fin)
114 {
115 cout << "Could not open file " << fname << endl;
116 return 0;
117 }
118
119 TRegexp regexp("^.20[0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9].$", kFALSE);
120
121 Int_t num=0;
122
123 Bool_t valid = kFALSE;
124 TString entry;
125 while (1)
126 {
127 TString line;
128 line.ReadLine(fin);
129 if (!fin)
130 break;
131
132 TString l0 = line(regexp);
133
134 if (l0.IsNull())
135 {
136 entry += line;
137 entry += "\n";
138 continue;
139 }
140 if (!valid)
141 {
142 valid = kTRUE;
143 entry = "";
144 //cout << "First entry skipped..." << endl;
145 continue;
146 }
147
148 if (entry.Contains("Operator names: "))
149 {
150 //cout << "OPERATORS: " << entry << flush;
151 entry="";
152 continue;
153 }
154
155 if (entry.Contains("CALIBRATION RUN STATISTICS") ||
156 entry.Contains("DATA RUN STATISTICS") ||
157 entry.Contains("PEDESTAL RUN STATISTICS"))
158 {
159 //cout << "Run entry skipped..." << endl;
160 entry ="";
161 continue;
162 }
163
164 TString date(l0(1, l0.Length()-2));
165
166 if (ExistStr(serv, "fRunBookDate", "MyMagic.RunBook", date))
167 {
168 entry ="";
169 continue;
170 }
171
172 entry.ReplaceAll("'", "\\'");
173 entry.ReplaceAll("\"", "\\\"");
174
175 // This is a sanity check for \0-bytes in .rbk-files
176 for (int i=0; i<entry.Length(); i++)
177 if ((int)entry[i]==0)
178 entry.Remove(i--);
179
180 TString query("INSERT MyMagic.RunBook (fRunBookDate, fRunBookText) VALUES (\"");
181 query += date;
182 query += "\", \"";
183 query += entry;
184 query += "\");";
185
186 if (dummy)
187 {
188 num++;
189 entry = "";
190 continue;
191 }
192
193 TSQLResult *res = serv.Query(query);
194 if (!res)
195 return 0;
196
197 delete res;
198 num++;
199
200 entry = "";
201 }
202
203 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
204 cout << (dummy?" DUMMY":"") << endl;
205
206 return 1;
207}
208
209// --------------------------------------------------------------------------
210//
211// loop over all files in this path
212//
213int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
214{
215 MSQLServer serv("mysql://hercules:d99swMT!@localhost");
216 if (!serv.IsConnected())
217 {
218 cout << "ERROR - Connection to database failed." << endl;
219 return 0;
220 }
221
222 cout << endl;
223 cout << "filldotrbk" << endl;
224 cout << "----------" << endl;
225 cout << endl;
226 cout << "Connected to " << serv.GetName() << endl;
227 cout << "Search Path: " << path << endl;
228 cout << endl;
229
230 if (path.EndWith(".rbk"))
231 return insert(serv, dummy, path);
232
233 MDirIter Next(path, "CC_*.rbk", -1);
234 while (1)
235 {
236 TString name = Next();
237 if (name.IsNull())
238 break;
239
240 if (!insert(serv, dummy, name))
241 return 0;
242 }
243
244 return 1;
245}
Note: See TracBrowser for help on using the repository browser.