source: trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C@ 7669

Last change on this file since 7669 was 7528, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 9.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! Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// writesequencefile.C
29// ===================
30//
31// reads the sequence information from the database and writes it into a
32// txt file
33//
34// Usage:
35// .x writesequencefile.C+(sequno,"sequpath")
36//
37// Make sure, that database and password are corretly set in a resource
38// file called sql.rc and the resource file is found.
39//
40// Returns 2 in case of failure, 1 in case of success and 0 if the connection
41// to the database is not working.
42//
43/////////////////////////////////////////////////////////////////////////////
44#include <iostream>
45#include <iomanip>
46#include <fstream>
47
48#include <MSQLServer.h>
49#include <TSQLRow.h>
50#include <TSQLResult.h>
51
52#include <TEnv.h>
53#include <TMath.h>
54#include <TRegexp.h>
55
56#include <MAstro.h>
57#include <MTime.h>
58#include <MDirIter.h>
59
60using namespace std;
61
62Bool_t GetAllRuns(MSQLServer &serv, ofstream &fout, TString query)
63{
64 TSQLResult *res = serv.Query(query);
65 if (!res)
66 return kFALSE;
67
68 TSQLRow *row=0;
69
70 Int_t cnt = 0;
71
72 fout << "Runs:";
73 while ((row = res->Next()))
74 {
75 fout << " " << (*row)[0];
76 cnt++;
77 }
78 fout << endl;
79
80 delete res;
81
82 if (cnt==0)
83 {
84 cout << "ERROR - No runs belonging to this sequence found." << endl;
85 return kFALSE;
86 }
87
88 return kTRUE;
89}
90
91Bool_t GetCalRuns(MSQLServer &serv, ofstream &fout, TString query, MTime *t)
92{
93 TSQLResult *res = serv.Query(query);
94 if (!res)
95 return kFALSE;
96
97 Int_t first = 0;
98 Int_t cnt = 0;
99
100 fout << "CalRuns:";
101 TSQLRow *row=0;
102 while ((row = res->Next()))
103 if ((*row)[1][0]=='4')
104 {
105 fout << " " << (*row)[0];
106 cnt++;
107
108 if (!first)
109 {
110 t[0].SetSqlDateTime((*row)[2]);
111 first = 1;
112 }
113 t[1].SetSqlDateTime((*row)[3]);
114 }
115 fout << endl;
116
117 delete res;
118
119 if (cnt==0)
120 {
121 cout << "ERROR - No calibration runs belonging to this sequence found." << endl;
122 return kFALSE;
123 }
124
125 return kTRUE;
126}
127
128Bool_t GetPedRuns(MSQLServer &serv, ofstream &fout, TString query, MTime *t)
129{
130 Int_t cnt = 0;
131
132 fout << "PedRuns:";
133
134 Int_t tot = 0;
135
136 while (tot<1000)
137 {
138 TSQLResult *res = serv.Query(query);
139 if (!res)
140 return kFALSE;
141
142 Int_t idx = 0;
143 Int_t n = 0;
144 Double_t diff = 1e35;
145 MTime start, stop;
146
147 TSQLRow *row=0;
148 while ((row=res->Next()))
149 {
150 if ((*row)[1][0]=='3' || (cnt>1 && idx==0 && (*row)[1][0]=='2'))
151 {
152 MTime t0, t1;
153 t0.SetSqlDateTime((*row)[2]);
154 t1.SetSqlDateTime((*row)[3]);
155
156 if ((Double_t)t[0]-(Double_t)t1<diff && t[0]>t1)
157 {
158 diff = (Double_t)t[0]-(Double_t)t1;
159 idx = atoi((*row)[0]);
160 n = atoi((*row)[4]);
161 start = t0;
162 stop = t1;
163 }
164 if ((Double_t)t0-(Double_t)t[1]<diff && t0>t[1])
165 {
166 diff = (Double_t)t0-(Double_t)t[1];
167 idx = atoi((*row)[0]);
168 n = atoi((*row)[4]);
169 start = t0;
170 stop = t1;
171 }
172 }
173 }
174
175 tot += n;
176 if (idx!=0)
177 fout << " " << idx;
178 cnt ++;
179
180 delete res;
181
182 if (start<t[0])
183 t[0] = start;
184 if (stop>t[1])
185 t[1] = stop;
186 }
187
188 fout << endl;
189
190 if (cnt==0)
191 {
192 cout << "ERROR - No pedestal (data) runs belonging to this sequence found." << endl;
193 return kFALSE;
194 }
195
196 return kTRUE;
197}
198
199Bool_t GetDatRuns(MSQLServer &serv, ofstream &fout, TString query)
200{
201 TSQLResult *res = serv.Query(query);
202 if (!res)
203 return kFALSE;
204
205 Int_t cnt=0;
206
207 fout << "DatRuns:";
208 TSQLRow *row=0;
209 while ((row = res->Next()))
210 if ((*row)[1][0]=='2')
211 {
212 fout << " " << (*row)[0];
213 cnt++;
214 }
215 fout << endl;
216
217 delete res;
218
219 if (cnt==0)
220 {
221 cout << "ERROR - No data runs belonging to this sequence found." << endl;
222 return kFALSE;
223 }
224
225 return kTRUE;
226}
227
228TString GetName(MSQLServer &serv, const char *col, const char *n)
229{
230 TString query(Form("SELECT f%sName FROM %s WHERE f%sKEY=%s",
231 col, col, col, n));
232
233 TSQLResult *res = serv.Query(query);
234 if (!res)
235 {
236 cout << "ERROR - Resolving " << col << " failed! " << endl;
237 return "";
238 }
239
240 TSQLRow *row = res->Next();
241 return (*row)[0];
242}
243
244Bool_t GetSequence(MSQLServer &serv, TSQLRow &data, TString sequpath)
245{
246 UShort_t y;
247 Byte_t m, d;
248
249 MTime time;
250 time.SetSqlDateTime(data[8]);
251 time.GetDateOfSunrise(y, m, d);
252
253 TString date = Form("%04d-%02d-%02d", y, (int)m, (int)d);
254
255 Int_t period = MAstro::GetMagicPeriod(time.GetMjd());
256
257 TString str[6];
258 str[0] = GetName(serv, "Project", data[2]);
259 str[1] = GetName(serv, "Source", data[3]);
260 str[2] = GetName(serv, "L1TriggerTable", data[4]);
261 str[3] = GetName(serv, "L2TriggerTable", data[5]);
262 str[4] = GetName(serv, "HvSettings", data[6]);
263 str[5] = GetName(serv, "LightConditions", data[7]);
264
265 if (str[0].IsNull() || str[1].IsNull() || str[2].IsNull() || str[3].IsNull() || str[4].IsNull() || str[5].IsNull())
266 return kFALSE;
267
268 //create sequence file
269 TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(), atoi(data[0])/10000, atoi(data[0])));
270 cout << "Creating " << fname << "..." << flush;
271
272 ofstream fout(fname);
273 if (!fout)
274 {
275 cout << "ERROR - Cannot open file." << endl;
276 return kFALSE;
277 }
278
279 //write information into file
280 fout << "Sequence: " << data[0] << endl;
281 fout << "Period: " << period << endl;
282 fout << "Night: " << date << endl;
283 fout << "LightConditions: " << str[5] << endl;
284 fout << endl;
285 fout << "Start: " << data[8] << endl;
286 fout << "LastRun: " << data[1] << endl;
287 fout << "Project: " << str[0] << endl;
288 fout << "Source: " << str[1] << endl;
289 fout << "ZdMin: " << data[10] << endl;
290 fout << "ZdMax: " << data[11] << endl;
291 fout << "L1TriggerTable: " << str[2] << endl;
292 fout << "L2TriggerTable: " << str[3] << endl;
293 fout << "HvSettings: " << str[4] << endl;
294 fout << "NumEvents: " << data[9] << endl;
295 fout << endl;
296
297 TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop, fNumEvents"
298 " FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
299 " ORDER BY fRunNumber",
300 data[0]));
301
302 //write runs into sequence file
303 if (!GetAllRuns(serv, fout, query))
304 return kFALSE;
305
306 fout << endl;
307
308 MTime t[2];
309 if (!GetCalRuns(serv, fout, query, t))
310 return kFALSE;
311 if (!GetPedRuns(serv, fout, query, t))
312 return kFALSE;
313 if (!GetDatRuns(serv, fout, query))
314 return kFALSE;
315
316 fout << endl;
317
318 cout << " done <Nevts=" << data[9] << ">" << endl;
319
320 return kTRUE;
321}
322
323// This tool will work from Period017 (2004_05_17) on...
324int writesequencefile(Int_t sequno, TString sequpath)
325{
326 TEnv env("sql.rc");
327
328 MSQLServer serv(env);
329 if (!serv.IsConnected())
330 {
331 cout << "ERROR - Connection to database failed." << endl;
332 return 0;
333 }
334
335 cout << "writesequencefile" << endl;
336 cout << "-----------------" << endl;
337 cout << endl;
338 cout << "Connected to " << serv.GetName() << endl;
339 cout << endl;
340
341 //get sequence information from database
342 TString query(Form("SELECT fSequenceFirst, fSequenceLast, fProjectKEY, fSourceKEY,"
343 " fL1TriggerTableKEY, fL2TriggerTableKEY, fHvSettingsKEY, "
344 " fLightConditionsKEY, fRunStart, fNumEvents, "
345 " fZenithDistanceMin, fZenithDistanceMax "
346 " FROM Sequences WHERE fSequenceFirst=%d", sequno));
347 TSQLResult *res = serv.Query(query);
348
349 TSQLRow *row = 0;
350 while ((row = res->Next()))
351 if (!GetSequence(serv, *row, sequpath))
352 return 2;
353
354 delete res;
355
356 cout << endl;
357
358 return 1;
359}
Note: See TracBrowser for help on using the repository browser.