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

Last change on this file since 8657 was 8052, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 7.6 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 <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
59#include "MSQLMagic.h"
60
61using namespace std;
62
63Bool_t GetRuns(MSQLServer &serv, ofstream &fout, TString query, TString name, UInt_t nevts=(UInt_t)-1)
64{
65 TSQLResult *res = serv.Query(query);
66 if (!res)
67 return kFALSE;
68
69 UInt_t cnt=0;
70 UInt_t evts=0;
71
72 fout << name << ":";
73 TSQLRow *row=0;
74 while ((row = res->Next()) && evts<nevts)
75 {
76 fout << " " << (*row)[0];
77 evts += atoi((*row)[1]);
78 cnt++;
79 }
80 fout << endl;
81
82 delete res;
83
84 if (cnt==0)
85 {
86 cout << "ERROR - No " << name << " belonging to this sequence found." << endl;
87 return kFALSE;
88 }
89
90 return kTRUE;
91}
92
93TString GetTime(const char *type)
94{
95 return Form("if(TIME_TO_SEC(fRun%s)<12*60*60,"
96 " TIME_TO_SEC(fRun%s)+24*60*60,"
97 " TIME_TO_SEC(fRun%s))", type, type, type);
98}
99
100Int_t GetTimeC(MSQLServer &serv, const TString &str)
101{
102 TSQLResult *res = serv.Query(str);
103 if (!res)
104 return -1;
105
106 TSQLRow *row=res->Next();
107 if (!row)
108 {
109 delete res;
110 return -1;
111 }
112
113 Int_t rc = (*row)[0] ? atoi((*row)[0]) : -1;
114 delete res;
115 return rc;
116}
117
118Bool_t GetSequence(MSQLMagic &serv, TSQLRow &data, TString sequpath)
119{
120 UShort_t y;
121 Byte_t m, d;
122
123 MTime time;
124 time.SetSqlDateTime(data[8]);
125 time.GetDateOfSunrise(y, m, d);
126
127 TString date = Form("%04d-%02d-%02d", y, (int)m, (int)d);
128
129 Int_t period = MAstro::GetMagicPeriod(time.GetMjd());
130
131 TString str[6];
132 str[0] = serv.QueryNameOfKey("Project", data[2]);
133 str[1] = serv.QueryNameOfKey("Source", data[3]);
134 str[2] = serv.QueryNameOfKey("L1TriggerTable", data[4]);
135 str[3] = serv.QueryNameOfKey("L2TriggerTable", data[5]);
136 str[4] = serv.QueryNameOfKey("HvSettings", data[6]);
137 str[5] = serv.QueryNameOfKey("LightConditions", data[7]);
138
139 if (str[0].IsNull() || str[1].IsNull() || str[2].IsNull() || str[3].IsNull() || str[4].IsNull() || str[5].IsNull())
140 return kFALSE;
141
142 //create sequence file
143 TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(), atoi(data[0])/10000, atoi(data[0])));
144 cout << "Creating " << fname << "..." << flush;
145
146 ofstream fout(fname);
147 if (!fout)
148 {
149 cout << "ERROR - Cannot open file." << endl;
150 return kFALSE;
151 }
152
153 //write information into file
154 fout << "Sequence: " << data[0] << endl;
155 fout << "Period: " << period << endl;
156 fout << "Night: " << date << endl;
157 fout << "LightConditions: " << str[5] << endl;
158 fout << endl;
159 fout << "Start: " << data[8] << endl;
160 fout << "LastRun: " << data[1] << endl;
161 fout << "Project: " << str[0] << endl;
162 fout << "Source: " << str[1] << endl;
163 fout << "ZdMin: " << data[10] << endl;
164 fout << "ZdMax: " << data[11] << endl;
165 fout << "L1TriggerTable: " << str[2] << endl;
166 fout << "L2TriggerTable: " << str[3] << endl;
167 fout << "HvSettings: " << str[4] << endl;
168 fout << "NumEvents: " << data[9] << endl;
169 fout << endl;
170
171 TString where(Form(" FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
172 " AND fRunTypeKEY%%s", data[0]));
173
174 TString query1(Form("SELECT fRunNumber, fNumEvents %s", where.Data()));
175 TString query2(Form("SELECT %s %s", GetTime("Start").Data(), where.Data()));
176 TString query3(Form("SELECT %s %s", GetTime("Stop").Data(), where.Data()));
177
178 TString queryA(Form(query1.Data(), " BETWEEN 2 AND 4 ORDER BY fRunNumber"));
179 TString queryC(Form(query1.Data(), "=4 ORDER BY fRunNumber"));
180 TString queryD(Form(query1.Data(), "=2 ORDER BY fRunNumber"));
181 TString queryT(Form(query2.Data(), "=4 ORDER BY fRunNumber LIMIT 1"));
182
183 Int_t timec = GetTimeC(serv, queryT);
184 if (timec<0)
185 {
186 cout << "WARNING - Requesting start time of first calibration run failed." << endl;
187 queryT = Form(query3.Data(), "=4 ORDER BY fRunNumber LIMIT 1");
188 timec = GetTimeC(serv, queryT);
189 if (timec<0)
190 {
191 cout << "ERROR - Neither start nor stop time of first calibration could be requested." << endl;
192 return kFALSE;
193 }
194 }
195
196 TString query4(Form("=3 ORDER BY ABS(%s-%d) ASC", GetTime("Stop").Data(), timec));
197 TString queryP(Form(query1.Data(), query4.Data()));
198
199 //write runs into sequence file
200 if (!GetRuns(serv, fout, queryA, "Runs"))
201 return kFALSE;
202
203 fout << endl;
204
205 if (!GetRuns(serv, fout, queryC, "CalRuns"))
206 return kFALSE;
207 if (!GetRuns(serv, fout, queryP, "PedRuns", 1000))
208 return kFALSE;
209 if (!GetRuns(serv, fout, queryD, "DatRuns"))
210 return kFALSE;
211
212 fout << endl;
213
214 cout << " done <Nevts=" << data[9] << ">" << endl;
215
216 return kTRUE;
217}
218
219// This tool will work from Period017 (2004_05_17) on...
220int writesequencefile(Int_t sequno, TString sequpath)
221{
222 TEnv env("sql.rc");
223
224 MSQLMagic serv(env);
225 if (!serv.IsConnected())
226 {
227 cout << "ERROR - Connection to database failed." << endl;
228 return 0;
229 }
230
231 cout << "writesequencefile" << endl;
232 cout << "-----------------" << endl;
233 cout << endl;
234 cout << "Connected to " << serv.GetName() << endl;
235 cout << endl;
236
237 //get sequence information from database
238 TString query(Form("SELECT fSequenceFirst, fSequenceLast, fProjectKEY, fSourceKEY,"
239 " fL1TriggerTableKEY, fL2TriggerTableKEY, fHvSettingsKEY, "
240 " fLightConditionsKEY, fRunStart, fNumEvents, "
241 " fZenithDistanceMin, fZenithDistanceMax "
242 " FROM Sequences WHERE fSequenceFirst=%d", sequno));
243 TSQLResult *res = serv.Query(query);
244
245 TSQLRow *row = 0;
246 while ((row = res->Next()))
247 if (!GetSequence(serv, *row, sequpath))
248 return 2;
249
250 delete res;
251
252 cout << endl;
253
254 return 1;
255}
Note: See TracBrowser for help on using the repository browser.