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

Last change on this file since 5138 was 4878, 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 <TEnv.h>
66#include <TRegexp.h>
67
68#include <TSQLRow.h>
69#include <TSQLResult.h>
70
71#include "MDirIter.h"
72#include "MSQLServer.h"
73
74using namespace std;
75
76// --------------------------------------------------------------------------
77//
78// Checks whether an entry for this date is already existing
79//
80Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
81{
82 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
83 TSQLResult *res = serv.Query(query);
84 if (!res)
85 return kFALSE;
86
87 TSQLRow *row;
88
89 Bool_t rc = kFALSE;
90 while ((row=res->Next()))
91 {
92 if ((*row)[0])
93 {
94 rc = kTRUE;
95 break;
96 }
97 }
98
99 delete res;
100
101 return rc;
102}
103
104// --------------------------------------------------------------------------
105//
106// insert the entries from this runbook file into the database
107//
108int insert(MSQLServer &serv, Bool_t dummy, TString fname)
109{
110 //cout << endl;
111 //cout << "FILE: " << fname << endl;
112
113 ifstream fin(fname);
114 if (!fin)
115 {
116 cout << "Could not open file " << fname << endl;
117 return 0;
118 }
119
120 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);
121
122 Int_t num=0;
123
124 Bool_t valid = kFALSE;
125 TString entry;
126 while (1)
127 {
128 TString line;
129 line.ReadLine(fin);
130 if (!fin)
131 break;
132
133 TString l0 = line(regexp);
134
135 if (l0.IsNull())
136 {
137 entry += line;
138 entry += "\n";
139 continue;
140 }
141 if (!valid)
142 {
143 valid = kTRUE;
144 entry = "";
145 //cout << "First entry skipped..." << endl;
146 continue;
147 }
148
149 if (entry.Contains("Operator names: "))
150 {
151 //cout << "OPERATORS: " << entry << flush;
152 entry="";
153 continue;
154 }
155
156 if (entry.Contains("CALIBRATION RUN STATISTICS") ||
157 entry.Contains("DATA RUN STATISTICS") ||
158 entry.Contains("PEDESTAL RUN STATISTICS"))
159 {
160 //cout << "Run entry skipped..." << endl;
161 entry ="";
162 continue;
163 }
164
165 TString date(l0(1, l0.Length()-2));
166
167 if (ExistStr(serv, "fRunBookDate", "MyMagic.RunBook", date))
168 {
169 entry ="";
170 continue;
171 }
172
173 entry.ReplaceAll("'", "\\'");
174 entry.ReplaceAll("\"", "\\\"");
175
176 // This is a sanity check for \0-bytes in .rbk-files
177 for (int i=0; i<entry.Length(); i++)
178 if ((int)entry[i]==0)
179 entry.Remove(i--);
180
181 TString query("INSERT MyMagic.RunBook (fRunBookDate, fRunBookText) VALUES (\"");
182 query += date;
183 query += "\", \"";
184 query += entry;
185 query += "\");";
186
187 if (dummy)
188 {
189 num++;
190 entry = "";
191 continue;
192 }
193
194 TSQLResult *res = serv.Query(query);
195 if (!res)
196 return 0;
197
198 delete res;
199 num++;
200
201 entry = "";
202 }
203
204 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
205 cout << (dummy?" DUMMY":"") << endl;
206
207 return 1;
208}
209
210// --------------------------------------------------------------------------
211//
212// loop over all files in this path
213//
214int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
215{
216 TEnv env("sql.rc");
217
218 MSQLServer serv(env);
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.