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

Last change on this file since 9474 was 8996, checked in by tbretz, 16 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 <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 delete res;
99 return rc;
100}
101
102int insert(MSQLServer &serv, Bool_t dummy, TString entry, TString date)
103{
104
105 //check if entry is already in database
106 if (ExistStr(serv, "fRunBookDate", "RunBook", date))
107 return 0;
108
109 entry.ReplaceAll("'", "\\'");
110 entry.ReplaceAll("\"", "\\\"");
111
112 // This is a sanity check for \0-bytes in .rbk-files
113 for (int i=0; i<entry.Length(); i++)
114 if ((int)entry[i]==0)
115 entry.Remove(i--);
116
117 //insert entry into the database
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
181 //skip run statistics from old runbooks
182 if (entry.Contains("MAGIC ELECTRONIC RUNBOOK") ||
183 entry.Contains("DATA RUN STATISTICS") ||
184 entry.Contains("CALIBRATION RUN STATISTICS") ||
185 entry.Contains("PEDESTAL RUN STATISTICS"))
186 entry ="";
187
188 if (!entry.IsNull() && !date.IsNull())
189 num += insert(serv, dummy, entry, date);
190
191 date=l0(1, l0.Length()-2);
192 entry="";
193 }
194
195 cout << fname(TRegexp("CC_.*.rbk", kFALSE)) << " <" << num << ">";
196 cout << (dummy?" DUMMY":"") << endl;
197
198 return 1;
199}
200
201// --------------------------------------------------------------------------
202//
203// loop over all files in this path
204//
205int filldotrbk(TString path="/data/MAGIC/Period017/ccdata", Bool_t dummy=kTRUE)
206{
207 MSQLServer serv("sql.rc");
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 //get all runbook files in path
223 if (path.EndsWith(".rbk"))
224 return process(serv, dummy, path);
225
226 //fill entries for each runbook file
227 MDirIter Next(path, "CC_*.rbk", -1);
228 while (1)
229 {
230 TString name = Next();
231 if (name.IsNull())
232 break;
233
234 if (!process(serv, dummy, name))
235 return 0;
236 }
237
238 return 1;
239}
Note: See TracBrowser for help on using the repository browser.