source: tags/Mars-V0.9.4.1/datacenter/macros/writesequencefile.C

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