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

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