source: tags/Mars-V0.9.4.2/datacenter/macros/insertsequence.C

Last change on this file was 7370, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 7.2 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): Daniela Dorner, 01/2005 <mailto:dorner@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// InsertSequence.C
28// ================
29//
30/////////////////////////////////////////////////////////////////////////////
31
32#include <iostream>
33#include <iomanip>
34#include <fstream>
35
36#include <TEnv.h>
37
38#include <MSQLServer.h>
39#include <TSQLRow.h>
40#include <TSQLResult.h>
41
42using namespace std;
43
44Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
45{
46 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
47 TSQLResult *res = serv.Query(query);
48 if (!res)
49 return kFALSE;
50
51 Bool_t rc = kFALSE;
52
53 TSQLRow *row=res->Next();
54 if (row && (*row)[0])
55 rc=kTRUE;
56
57 delete res;
58 return rc;
59}
60
61Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
62{
63 TString query;
64
65 query = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'", col, col, col, name);
66 TSQLResult *res = serv.Query(query);
67 if (!res)
68 return -1;
69
70 TSQLRow *row=res->Next();
71
72 Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1;
73
74 delete res;
75
76 if (rc>=0)
77 return rc;
78
79 if (!insert)
80 return -1;
81
82 query = Form("INSERT %s (f%sName) VALUES (\"%s\");", col, col, name);
83
84 if (dummy)
85 {
86 cout << query << endl;
87 return 0;
88 }
89
90 res=serv.Query(query);
91 if (!res)
92 return -1;
93
94 delete res;
95
96 Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
97 if (key>0)
98 {
99 cout << "New " << col << ": " << name << endl;
100 return key;
101 }
102
103 cout << "ERROR: " << query << endl;
104 return kFALSE;
105}
106
107int insertsequence(TString filename, Bool_t dummy=kTRUE)
108{
109 TEnv env("sql.rc");
110 TEnv sequ(filename);
111
112 MSQLServer serv(env);
113 if (!serv.IsConnected())
114 {
115 cout << "ERROR - Connection to database failed." << endl;
116 return 0;
117 }
118 cout << "insertsequence" << endl;
119 cout << "--------------" << endl;
120 cout << endl;
121 cout << "Connected to " << serv.GetName() << endl;
122 cout << endl;
123
124 TString sequnum=filename;
125 sequnum=sequnum.Remove(0,30);
126 sequnum=sequnum.Remove(8);
127 Int_t seq=atoi(sequnum.Data());
128
129 TString runs;
130 runs = sequ.GetValue("Runs", "");
131 runs.ReplaceAll(" ", ",");
132 if (runs.IsNull())
133 {
134 cout << "ERROR - No runs in file " << filename << " found." << endl;
135 return 0;
136 }
137
138 TString query="SELECT max(fRunNumber), min(fRunStart), ";
139 query +="sum(time_to_sec(fRunStop)-time_to_sec(fRunStart)), ";
140 query +="min(fZenithDistance), max(fZenithDistance), ";
141 query +="min(fAzimuth), max(fAzimuth), sum(fNumEvents) from RunData ";
142 query +=Form("WHERE fRunNumber IN (%s)",runs.Data());
143
144 cout << "runs: " << runs << endl;
145 cout << "q: " << query << endl;
146
147 TSQLResult *res = serv.Query(query);
148 if (!res)
149 return 0;
150
151 TSQLRow *row=res->Next();
152 TString lastrun=(*row)[0];
153 TString starttime=(*row)[1];
154 TString uptime=(*row)[2];
155 TString zdmin=(*row)[3];
156 TString zdmax=(*row)[4];
157 TString azmin=(*row)[5];
158 TString azmax=(*row)[6];
159 TString numevts=(*row)[7];
160
161 delete res;
162
163 query ="SELECT fProjectKEY, fSourceKEY, fHvSettingsKEY, ";
164 query +="fTriggerDelayTableKEY, fDiscriminatorThresholdTableKEY, ";
165 query +="fTestFlagKEY, fLightConditionsKEY, fL1TriggerTableKEY, ";
166 query +=Form("fL2TriggerTableKEY FROM RunData WHERE fRunNumber=%d", seq);
167
168 cout << "q: " << query << endl;
169
170 res = serv.Query(query);
171 if (!res)
172 return 0;
173
174 row=res->Next();
175 TString project=(*row)[0];
176 TString source=(*row)[1];
177 TString hv=(*row)[2];
178 TString delay=(*row)[3];
179 TString dt=(*row)[4];
180 TString testflag=(*row)[5];
181 TString lightcond=(*row)[6];
182 TString l1tt=(*row)[7];
183 TString l2tt=(*row)[8];
184
185 delete res;
186
187 cout << "seq: " << seq << endl;
188 cout << " lastrun " << lastrun << endl;
189 cout << " startime " << starttime << endl;
190 cout << " uptime " << uptime << endl;
191 cout << " zdmin " << zdmin << endl;
192 cout << " zdmax " << zdmax << endl;
193 cout << " azmin " << azmin << endl;
194 cout << " azmax " << azmax << endl;
195 cout << " numevts " << numevts << endl;
196 cout << " keys:" << endl;
197 cout << " project " << project << endl;
198 cout << " source " << source << endl;
199 cout << " hv " << hv << endl;
200 cout << " delay " << delay << endl;
201 cout << " dt " << dt << endl;
202 cout << " testflag " << testflag << endl;
203 cout << " lightcond " << lightcond << endl;
204 cout << " l1tt " << l1tt << endl;
205 cout << " l2tt " << l2tt << endl;
206
207 TString query1="INSERT Sequences SET fManuallyChangedKEY=2, ";
208 query1 +=Form("fSequenceFirst=%d, fSequenceLast=%s, "
209 "fProjectKEY=%s, fSourceKEY=%s, fNumEvents=%s, "
210 "fRunStart='%s', fHvSettingsKEY=%s, fRunTime=%s, "
211 "fTriggerDelayTableKEY=%s, fDiscriminatorThresholdTableKEY=%s, "
212 "fTestFlagKEY=%s, fLightConditionsKEY=%s, fAzimuthMin=%s, "
213 "fAzimuthMax=%s, fZenithDistanceMin=%s, fZenithDistanceMax=%s, "
214 "fL1TriggerTableKEY=%s, fL2TriggerTableKEY=%s ",
215 seq, lastrun.Data(), project.Data(), source.Data(),
216 numevts.Data(), starttime.Data(), hv.Data(), uptime.Data(),
217 delay.Data(), dt.Data(), testflag.Data(), lightcond.Data(),
218 azmin.Data(), azmax.Data(), zdmin.Data(), zdmax.Data(),
219 l1tt.Data(), l2tt.Data());
220
221 TString query2=Form("INSERT SequenceProcessStatus SET "
222 "fSequenceFirst=%d, fSequenceFileWritten='1970-01-01 00:00:00'",
223 seq);
224
225 if (dummy)
226 {
227 cout << "q1: " << query1 << endl;
228 cout << "q2: " << query2 << endl;
229 return 1;
230 }
231
232
233 res = serv.Query(query1);
234 if (!res)
235 {
236 cout << "ERROR: query1 failed: " << query1 << endl;
237 return 0;
238 }
239 delete res;
240
241 res = serv.Query(query2);
242 if (!res)
243 {
244 cout << "ERROR: query2 failed: " << query2 << endl;
245 return 0;
246 }
247 delete res;
248
249 return 1;
250}
251
252
Note: See TracBrowser for help on using the repository browser.