source: trunk/MagicSoft/Mars/datacenter/macros/insertsequence.C@ 7400

Last change on this file since 7400 was 7400, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 7.8 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 << "Q:" << query << endl;
87 return 0;
88 }
89
90 res=serv.Query(query);
91 if (!res)
92 {
93 cout << "Error in query " << query << endl;
94 return -1;
95 }
96
97 delete res;
98
99 Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
100 if (key>0)
101 {
102 cout << "New " << col << ": " << name << endl;
103 return key;
104 }
105
106 cout << "ERROR: " << query << endl;
107 return kFALSE;
108}
109
110int insertsequence(TString filename, Bool_t dummy=kTRUE)
111{
112 TEnv env("sql.rc");
113 TEnv sequ(filename);
114
115 MSQLServer serv(env);
116 if (!serv.IsConnected())
117 {
118 cout << "ERROR - Connection to database failed." << endl;
119 return 0;
120 }
121 cout << "insertsequence" << endl;
122 cout << "--------------" << endl;
123 cout << endl;
124 cout << "Connected to " << serv.GetName() << endl;
125 cout << endl;
126
127 TString sequnum;
128 sequnum=sequ.GetValue("Sequence", "");
129 Int_t seq=atoi(sequnum.Data());
130
131 TString runs;
132 runs = sequ.GetValue("Runs", "");
133 runs.ReplaceAll(" ", ",");
134 if (runs.IsNull())
135 {
136 cout << "ERROR - No runs in file " << filename << " found." << endl;
137 return 0;
138 }
139
140 TString sourcename = sequ.GetValue("Source", "");
141 Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", sourcename.Data());
142
143 TString query="SELECT max(fRunNumber), min(fRunStart), ";
144 query +="sum(time_to_sec(fRunStop)-time_to_sec(fRunStart)), ";
145 query +="min(fZenithDistance), max(fZenithDistance), ";
146 query +="min(fAzimuth), max(fAzimuth), sum(fNumEvents) from RunData ";
147 query +=Form("WHERE fRunNumber IN (%s)",runs.Data());
148
149 cout << "runs: " << runs << endl;
150 cout << "q1: " << query << endl;
151
152 TSQLResult *res = serv.Query(query);
153 if (!res)
154 return 0;
155
156 TSQLRow *row=res->Next();
157 TString lastrun=(*row)[0];
158 TString starttime=(*row)[1];
159 TString uptime=(*row)[2];
160 TString zdmin=(*row)[3];
161 TString zdmax=(*row)[4];
162 TString azmin=(*row)[5];
163 TString azmax=(*row)[6];
164 TString numevts=(*row)[7];
165
166 delete res;
167
168 query ="SELECT fProjectKEY, fSourceKEY, fHvSettingsKEY, ";
169 query +="fTriggerDelayTableKEY, fDiscriminatorThresholdTableKEY, ";
170 query +="fTestFlagKEY, fLightConditionsKEY, fL1TriggerTableKEY, ";
171 query +=Form("fL2TriggerTableKEY FROM RunData WHERE fRunNumber=%d", seq);
172
173 cout << "q2: " << query << endl;
174
175 res = serv.Query(query);
176 if (!res)
177 return 0;
178
179 row=res->Next();
180 TString project=(*row)[0];
181 TString source=(*row)[1];
182 TString hv=(*row)[2];
183 TString delay=(*row)[3];
184 TString dt=(*row)[4];
185 TString testflag=(*row)[5];
186 TString lightcond=(*row)[6];
187 TString l1tt=(*row)[7];
188 TString l2tt=(*row)[8];
189
190 delete res;
191
192 cout << "seq: " << seq << endl;
193 cout << " lastrun " << lastrun << endl;
194 cout << " startime " << starttime << endl;
195 cout << " uptime " << uptime << endl;
196 cout << " zdmin " << zdmin << endl;
197 cout << " zdmax " << zdmax << endl;
198 cout << " azmin " << azmin << endl;
199 cout << " azmax " << azmax << endl;
200 cout << " numevts " << numevts << endl;
201 cout << " keys:" << endl;
202 cout << " project " << project << endl;
203 cout << " source1 " << source << " (from db -> run " << seq << ") " << endl;
204 cout << " source2 " << sourcekey << " (from sequ file) " << endl;
205 if (!(atoi(source.Data())==sourcekey))
206 {
207 cout << "new source name: " << sourcename << " -> inserting..." << endl;
208 sourcekey = QueryNameKEY(serv, dummy, "Source", sourcename.Data(), kFALSE);
209 source=Form("%d",sourcekey);
210 }
211 cout << " source " << source << endl;
212 cout << " hv " << hv << endl;
213 cout << " delay " << delay << endl;
214 cout << " dt " << dt << endl;
215 cout << " testflag " << testflag << endl;
216 cout << " lightcond " << lightcond << endl;
217 cout << " l1tt " << l1tt << endl;
218 cout << " l2tt " << l2tt << endl;
219
220 TString query1="INSERT Sequences SET fManuallyChangedKEY=2, ";
221 query1 +=Form("fSequenceFirst=%d, fSequenceLast=%s, "
222 "fProjectKEY=%s, fSourceKEY=%s, fNumEvents=%s, "
223 "fRunStart='%s', fHvSettingsKEY=%s, fRunTime=%s, "
224 "fTriggerDelayTableKEY=%s, fDiscriminatorThresholdTableKEY=%s, "
225 "fTestFlagKEY=%s, fLightConditionsKEY=%s, fAzimuthMin=%s, "
226 "fAzimuthMax=%s, fZenithDistanceMin=%s, fZenithDistanceMax=%s, "
227 "fL1TriggerTableKEY=%s, fL2TriggerTableKEY=%s ",
228 seq, lastrun.Data(), project.Data(), source.Data(),
229 numevts.Data(), starttime.Data(), hv.Data(), uptime.Data(),
230 delay.Data(), dt.Data(), testflag.Data(), lightcond.Data(),
231 azmin.Data(), azmax.Data(), zdmin.Data(), zdmax.Data(),
232 l1tt.Data(), l2tt.Data());
233
234 TString query2=Form("INSERT SequenceProcessStatus SET "
235 "fSequenceFirst=%d, fSequenceFileWritten='1970-01-01 00:00:00'",
236 seq);
237
238 if (dummy)
239 {
240 cout << "q1: " << query1 << endl;
241 cout << "q2: " << query2 << endl;
242 return 1;
243 }
244
245
246 res = serv.Query(query1);
247 if (!res)
248 {
249 cout << "ERROR: query1 failed: " << query1 << endl;
250 return 0;
251 }
252 delete res;
253
254 res = serv.Query(query2);
255 if (!res)
256 {
257 cout << "ERROR: query2 failed: " << query2 << endl;
258 return 0;
259 }
260 delete res;
261
262 return 1;
263}
264
265
Note: See TracBrowser for help on using the repository browser.