source: trunk/MagicSoft/Mars/datacenter/macros/filldotrbk.C@ 7428

Last change on this file since 7428 was 7112, checked in by Daniela Dorner, 19 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 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
104int insert(MSQLServer &serv, Bool_t dummy, TString entry, TString date)
105{
106
107 if (ExistStr(serv, "fRunBookDate", "RunBook", date))
108 return 0;
109
110 entry.ReplaceAll("'", "\\'");
111 entry.ReplaceAll("\"", "\\\"");
112
113 // This is a sanity check for \0-bytes in .rbk-files
114 for (int i=0; i<entry.Length(); i++)
115 if ((int)entry[i]==0)
116 entry.Remove(i--);
117
118 TString query("INSERT RunBook (fRunBookDate, fRunBookText) VALUES (\"");
119 query += date;
120 query += "\", \"";
121 query += entry;
122 query += "\");";
123
124 if (dummy)
125 return 1;
126
127 TSQLResult *res = serv.Query(query);
128 if (!res)
129 return 0;
130
131 delete res;
132
133 return 1;
134}
135
136// --------------------------------------------------------------------------
137//
138// insert the entries from this runbook file into the database
139//
140int process(MSQLServer &serv, Bool_t dummy, TString fname)
141{
142 ifstream fin(fname);
143 if (!fin)
144 {
145 cout << "Could not open file " << fname << endl;
146 return 0;
147 }
148
149 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);
150
151 Int_t num=0;
152
153 TString entry="";
154 TString date="";
155 while (1)
156 {
157 TString line;
158 line.ReadLine(fin);
159 if (!fin)
160 {
161 num += insert(serv, dummy, entry, date);
162 break;
163 }
164
165 TString l0 = line(regexp);
166
167 if (l0.IsNull() || entry.IsNull())
168 {
169 entry += line;
170 entry += "\n";
171 continue;
172 }
173/*
174 if (entry.Contains("Operator names: "))
175 {
176 cout << "OPERATORS: " << entry << flush;
177 entry="";
178 }
179*/
180 if (entry.Contains("MAGIC ELECTRONIC RUNBOOK") ||
181 entry.Contains("DATA RUN STATISTICS") ||
182 entry.Contains("CALIBRATION RUN STATISTICS") ||
183 entry.Contains("PEDESTAL RUN STATISTICS"))
184 entry ="";
185
186 if (!entry.IsNull() && !date.IsNull())
187 num += insert(serv, dummy, entry, date);
188
189 date=l0(1, l0.Length()-2);
190 entry="";
191 }
192
193 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
194 cout << (dummy?" DUMMY":"") << endl;
195
196 return 1;
197}
198
199// --------------------------------------------------------------------------
200//
201// loop over all files in this path
202//
203int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
204{
205 TEnv env("sql.rc");
206
207 MSQLServer serv(env);
208 if (!serv.IsConnected())
209 {
210 cout << "ERROR - Connection to database failed." << endl;
211 return 0;
212 }
213
214 cout << endl;
215 cout << "filldotrbk" << endl;
216 cout << "----------" << endl;
217 cout << endl;
218 cout << "Connected to " << serv.GetName() << endl;
219 cout << "Search Path: " << path << endl;
220 cout << endl;
221
222 if (path.EndsWith(".rbk"))
223 return process(serv, dummy, path);
224
225 MDirIter Next(path, "CC_*.rbk", -1);
226 while (1)
227 {
228 TString name = Next();
229 if (name.IsNull())
230 break;
231
232 if (!process(serv, dummy, name))
233 return 0;
234 }
235
236 return 1;
237}
Note: See TracBrowser for help on using the repository browser.