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

Last change on this file since 8015 was 7460, checked in by Daniela Dorner, 19 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-2006
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("/magic/subsystemdata/cc/", kTRUE)
35//
36// The first argument is the directory in which all subdirectories are
37// searched for CC_*.rbk files. All these files are analysed and the runbook
38// entries will be put into the DB, eg:
39// "/magic/subsystemdata/cc" would do it for all data
40// "/magic/subsystemdata/cc/2005" for one year
41// "/magic/subsystemdata/cc/2005/11" for one month
42// "/magic/subsystemdata/cc/2005/11/11" for a single day
43// "/magic/subsystemdata/cc/2005/11/11/file.rbk" for a single file
44//
45// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
46// switched on and nothing will be written into the database. This is usefull
47// for tests.
48//
49// Before an entry is added, its existance is checked... if it is already
50// in the database, it is ignored.
51//
52// The macro can also be run without ACLiC but this is a lot slower...
53//
54// Remark: Running it from the commandline looks like this:
55// root -q -l -b filldotrbk.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrbk.log
56//
57// Make sure, that database and password are corretly set in the macro.
58//
59// Returns 0 in case of failure and 1 in case of success.
60//
61///////////////////////////////////////////////////////////////////////////
62#include <iostream>
63#include <iomanip>
64#include <fstream>
65
66#include <TEnv.h>
67#include <TRegexp.h>
68
69#include <TSQLRow.h>
70#include <TSQLResult.h>
71
72#include "MDirIter.h"
73#include "MSQLServer.h"
74
75using namespace std;
76
77// --------------------------------------------------------------------------
78//
79// Checks whether an entry for this date is already existing
80//
81Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
82{
83 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
84 TSQLResult *res = serv.Query(query);
85 if (!res)
86 return kFALSE;
87
88 TSQLRow *row;
89
90 Bool_t rc = kFALSE;
91 while ((row=res->Next()))
92 {
93 if ((*row)[0])
94 {
95 rc = kTRUE;
96 break;
97 }
98 }
99 delete res;
100 return rc;
101}
102
103int insert(MSQLServer &serv, Bool_t dummy, TString entry, TString date)
104{
105
106 //check if entry is already in database
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 //insert entry into the database
119 TString query("INSERT RunBook (fRunBookDate, fRunBookText) VALUES (\"");
120 query += date;
121 query += "\", \"";
122 query += entry;
123 query += "\");";
124
125 if (dummy)
126 return 1;
127
128 TSQLResult *res = serv.Query(query);
129 if (!res)
130 return 0;
131
132 delete res;
133
134 return 1;
135}
136
137// --------------------------------------------------------------------------
138//
139// insert the entries from this runbook file into the database
140//
141int process(MSQLServer &serv, Bool_t dummy, TString fname)
142{
143 ifstream fin(fname);
144 if (!fin)
145 {
146 cout << "Could not open file " << fname << endl;
147 return 0;
148 }
149
150 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);
151
152 Int_t num=0;
153
154 TString entry="";
155 TString date="";
156 while (1)
157 {
158 TString line;
159 line.ReadLine(fin);
160 if (!fin)
161 {
162 num += insert(serv, dummy, entry, date);
163 break;
164 }
165
166 TString l0 = line(regexp);
167
168 if (l0.IsNull() || entry.IsNull())
169 {
170 entry += line;
171 entry += "\n";
172 continue;
173 }
174/*
175 if (entry.Contains("Operator names: "))
176 {
177 cout << "OPERATORS: " << entry << flush;
178 entry="";
179 }
180*/
181
182 //skip run statistics from old runbooks
183 if (entry.Contains("MAGIC ELECTRONIC RUNBOOK") ||
184 entry.Contains("DATA RUN STATISTICS") ||
185 entry.Contains("CALIBRATION RUN STATISTICS") ||
186 entry.Contains("PEDESTAL RUN STATISTICS"))
187 entry ="";
188
189 if (!entry.IsNull() && !date.IsNull())
190 num += insert(serv, dummy, entry, date);
191
192 date=l0(1, l0.Length()-2);
193 entry="";
194 }
195
196 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
197 cout << (dummy?" DUMMY":"") << endl;
198
199 return 1;
200}
201
202// --------------------------------------------------------------------------
203//
204// loop over all files in this path
205//
206int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
207{
208 TEnv env("sql.rc");
209
210 MSQLServer serv(env);
211 if (!serv.IsConnected())
212 {
213 cout << "ERROR - Connection to database failed." << endl;
214 return 0;
215 }
216
217 cout << endl;
218 cout << "filldotrbk" << endl;
219 cout << "----------" << endl;
220 cout << endl;
221 cout << "Connected to " << serv.GetName() << endl;
222 cout << "Search Path: " << path << endl;
223 cout << endl;
224
225 //get all runbook files in path
226 if (path.EndsWith(".rbk"))
227 return process(serv, dummy, path);
228
229 //fill entries for each runbook file
230 MDirIter Next(path, "CC_*.rbk", -1);
231 while (1)
232 {
233 TString name = Next();
234 if (name.IsNull())
235 break;
236
237 if (!process(serv, dummy, name))
238 return 0;
239 }
240
241 return 1;
242}
Note: See TracBrowser for help on using the repository browser.