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

Last change on this file since 4601 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.3 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//
43// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
44// switched on and nothing will be written into the database. This is usefull
45// for tests.
46//
47// Before an antry is added its existance is checked... if it is added already
48// it is ignored.
49//
50// The macro can also be run without ACLiC but this is a lot slower...
51//
52// Remark: Running it from the commandline looks like this:
53// root -q -l -b filldotrbk.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrbk.log
54//
55// Make sure, that database and password are corretly set in the macro.
56//
57///////////////////////////////////////////////////////////////////////////
58#include <iostream>
59#include <iomanip>
60#include <fstream>
61
62#include <MSQLServer.h>
63#include <TSQLRow.h>
64#include <TSQLResult.h>
65
66#include <TRegexp.h>
67
68#include <MDirIter.h>
69
70using namespace std;
71
72// --------------------------------------------------------------------------
73//
74// Checks whether an entry for this date is already existing
75//
76Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
77{
78 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
79 TSQLResult *res = serv.Query(query);
80 if (!res)
81 return kFALSE;
82
83 TSQLRow *row;
84
85 Bool_t rc = kFALSE;
86 while ((row=res->Next()))
87 {
88 if ((*row)[0])
89 {
90 rc = kTRUE;
91 break;
92 }
93 }
94
95 delete res;
96
97 return rc;
98}
99
100// --------------------------------------------------------------------------
101//
102// insert the entries from this runbook file into the database
103//
104void insert(MSQLServer &serv, Bool_t dummy, TString fname)
105{
106 //cout << endl;
107 //cout << "FILE: " << fname << endl;
108
109 ifstream fin(fname);
110 if (!fin)
111 {
112 cout << "Could not open file " << fname << endl;
113 return;
114 }
115
116 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);
117
118 Int_t num=0;
119
120 Bool_t valid = kFALSE;
121 TString entry;
122 while (1)
123 {
124 TString line;
125 line.ReadLine(fin);
126 if (!fin)
127 break;
128
129 TString l0 = line(regexp);
130
131 if (l0.IsNull())
132 {
133 entry += line;
134 entry += "\n";
135 continue;
136 }
137 if (!valid)
138 {
139 valid = kTRUE;
140 entry = "";
141 //cout << "First entry skipped..." << endl;
142 continue;
143 }
144
145 if (entry.Contains("Operator names: "))
146 {
147 //cout << "OPERATORS: " << entry << flush;
148 entry="";
149 continue;
150 }
151
152 if (entry.Contains("CALIBRATION RUN STATISTICS") ||
153 entry.Contains("DATA RUN STATISTICS") ||
154 entry.Contains("PEDESTAL RUN STATISTICS"))
155 {
156 //cout << "Run entry skipped..." << endl;
157 entry ="";
158 continue;
159 }
160
161 TString date(l0(1, l0.Length()-2));
162
163 if (ExistStr(serv, "fRunBookDate", "MyMagic.RunBook", date))
164 {
165 entry ="";
166 continue;
167 }
168
169 entry.ReplaceAll("'", "\\'");
170 entry.ReplaceAll("\"", "\\\"");
171
172 // This is a sanity check for \0-bytes in .rbk-files
173 for (int i=0; i<entry.Length(); i++)
174 if ((int)entry[i]==0)
175 entry.Remove(i--);
176
177 TString query("INSERT MyMagic.RunBook (fRunBookDate, fRunBookText) VALUES (\"");
178 query += date;
179 query += "\", \"";
180 query += entry;
181 query += "\");";
182
183 if (dummy)
184 {
185 num++;
186 entry = "";
187 continue;
188 }
189
190 TSQLResult *res = serv.Query(query);
191 if (!res)
192 cout << "ERROR: " << query << endl << endl;
193 else
194 {
195 delete res;
196 num++;
197 }
198
199 entry = "";
200 }
201
202 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
203 cout << (dummy?" DUMMY":"") << endl;
204}
205
206// --------------------------------------------------------------------------
207//
208// loop over all files in this path
209//
210void filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
211{
212 MSQLServer serv("mysql://hercules:d99swMT!@localhost");
213 if (!serv.IsConnected())
214 {
215 cout << "ERROR - Connection to database failed." << endl;
216 return;
217 }
218
219 cout << endl;
220 cout << "filldotrbk" << endl;
221 cout << "----------" << endl;
222 cout << endl;
223 cout << "Connected to " << serv.GetName() << endl;
224 cout << "Search Path: " << path << endl;
225 cout << endl;
226
227 MDirIter Next(path, "CC_*.rbk", -1);
228 while (1)
229 {
230 TString name = Next();
231 if (name.IsNull())
232 break;
233
234 insert(serv, dummy, name);
235 }
236}
Note: See TracBrowser for help on using the repository browser.