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

Last change on this file since 4691 was 4680, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.6 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 readrbk.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 <MSQLServer.h>
66#include <TSQLRow.h>
67#include <TSQLResult.h>
68
69#include <TRegexp.h>
70
71#include <MDirIter.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 {
196 cout << "ERROR: " << query << endl << endl;
197 return 0;
198 }
199
200 delete res;
201 num++;
202
203 entry = "";
204 }
205
206 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
207 cout << (dummy?" DUMMY":"") << endl;
208
209 return 1;
210}
211
212// --------------------------------------------------------------------------
213//
214// loop over all files in this path
215//
216int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
217{
218 MSQLServer serv("mysql://hercules:d99swMT!@localhost");
219 if (!serv.IsConnected())
220 {
221 cout << "ERROR - Connection to database failed." << endl;
222 return 0;
223 }
224
225 cout << endl;
226 cout << "filldotrbk" << endl;
227 cout << "----------" << endl;
228 cout << endl;
229 cout << "Connected to " << serv.GetName() << endl;
230 cout << "Search Path: " << path << endl;
231 cout << endl;
232
233 if (path.EndWith(".rbk"))
234 return insert(serv, dummy, path);
235
236 MDirIter Next(path, "CC_*.rbk", -1);
237 while (1)
238 {
239 TString name = Next();
240 if (name.IsNull())
241 break;
242
243 if (!insert(serv, dummy, name))
244 return 0;
245 }
246
247 return 1;
248}
Note: See TracBrowser for help on using the repository browser.