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

Last change on this file since 8045 was 8045, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 6.7 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 "MSQLServer.h"
60
61using namespace std;
62
63Bool_t GetRuns(MSQLServer &serv, ofstream &fout, TString query, TString name)
64{
65 TSQLResult *res = serv.Query(query);
66 if (!res)
67 return kFALSE;
68
69 Int_t cnt=0;
70
71 fout << name << ":";
72 TSQLRow *row=0;
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 " << name << " belonging to this sequence found." << endl;
85 return kFALSE;
86 }
87
88 return kTRUE;
89}
90
91TString GetName(MSQLServer &serv, const char *col, const char *n)
92{
93 TString query(Form("SELECT f%sName FROM %s WHERE f%sKEY=%s",
94 col, col, col, n));
95
96 TSQLResult *res = serv.Query(query);
97 if (!res)
98 {
99 cout << "ERROR - Resolving " << col << " failed! " << endl;
100 return "";
101 }
102
103 TSQLRow *row = res->Next();
104 return row ? (*row)[0] : "";
105}
106
107Bool_t GetSequence(MSQLServer &serv, TSQLRow &data, TString sequpath)
108{
109 UShort_t y;
110 Byte_t m, d;
111
112 MTime time;
113 time.SetSqlDateTime(data[8]);
114 time.GetDateOfSunrise(y, m, d);
115
116 TString date = Form("%04d-%02d-%02d", y, (int)m, (int)d);
117
118 Int_t period = MAstro::GetMagicPeriod(time.GetMjd());
119
120 TString str[6];
121 str[0] = GetName(serv, "Project", data[2]);
122 str[1] = GetName(serv, "Source", data[3]);
123 str[2] = GetName(serv, "L1TriggerTable", data[4]);
124 str[3] = GetName(serv, "L2TriggerTable", data[5]);
125 str[4] = GetName(serv, "HvSettings", data[6]);
126 str[5] = GetName(serv, "LightConditions", data[7]);
127
128 if (str[0].IsNull() || str[1].IsNull() || str[2].IsNull() || str[3].IsNull() || str[4].IsNull() || str[5].IsNull())
129 return kFALSE;
130
131 //create sequence file
132 TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(), atoi(data[0])/10000, atoi(data[0])));
133 cout << "Creating " << fname << "..." << flush;
134
135 ofstream fout(fname);
136 if (!fout)
137 {
138 cout << "ERROR - Cannot open file." << endl;
139 return kFALSE;
140 }
141
142 //write information into file
143 fout << "Sequence: " << data[0] << endl;
144 fout << "Period: " << period << endl;
145 fout << "Night: " << date << endl;
146 fout << "LightConditions: " << str[5] << endl;
147 fout << endl;
148 fout << "Start: " << data[8] << endl;
149 fout << "LastRun: " << data[1] << endl;
150 fout << "Project: " << str[0] << endl;
151 fout << "Source: " << str[1] << endl;
152 fout << "ZdMin: " << data[10] << endl;
153 fout << "ZdMax: " << data[11] << endl;
154 fout << "L1TriggerTable: " << str[2] << endl;
155 fout << "L2TriggerTable: " << str[3] << endl;
156 fout << "HvSettings: " << str[4] << endl;
157 fout << "NumEvents: " << data[9] << endl;
158 fout << endl;
159
160 TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop, fNumEvents"
161 " FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
162 " AND fRunTypeKEY %%s ORDER BY fRunNumber",
163 data[0]));
164
165
166 TString queryA(Form(query.Data(), "BETWEEN 2 AND 4"));
167 TString queryC(Form(query.Data(), "BETWEEN 4 AND 4"));
168 TString queryD(Form(query.Data(), "BETWEEN 2 AND 2"));
169 TString queryP(Form(query.Data(), "BETWEEN 3 AND 3"));
170
171 //write runs into sequence file
172 if (!GetRuns(serv, fout, queryA, "Runs"))
173 return kFALSE;
174
175 fout << endl;
176
177 if (!GetRuns(serv, fout, queryC, "CalRuns"))
178 return kFALSE;
179 if (!GetRuns(serv, fout, queryP, "PedRuns"))
180 return kFALSE;
181 if (!GetRuns(serv, fout, queryD, "DatRuns"))
182 return kFALSE;
183
184 fout << endl;
185
186 cout << " done <Nevts=" << data[9] << ">" << endl;
187
188 return kTRUE;
189}
190
191// This tool will work from Period017 (2004_05_17) on...
192int writesequencefile(Int_t sequno, TString sequpath)
193{
194 TEnv env("sql.rc");
195
196 MSQLServer serv(env);
197 if (!serv.IsConnected())
198 {
199 cout << "ERROR - Connection to database failed." << endl;
200 return 0;
201 }
202
203 cout << "writesequencefile" << endl;
204 cout << "-----------------" << endl;
205 cout << endl;
206 cout << "Connected to " << serv.GetName() << endl;
207 cout << endl;
208
209 //get sequence information from database
210 TString query(Form("SELECT fSequenceFirst, fSequenceLast, fProjectKEY, fSourceKEY,"
211 " fL1TriggerTableKEY, fL2TriggerTableKEY, fHvSettingsKEY, "
212 " fLightConditionsKEY, fRunStart, fNumEvents, "
213 " fZenithDistanceMin, fZenithDistanceMax "
214 " FROM Sequences WHERE fSequenceFirst=%d", sequno));
215 TSQLResult *res = serv.Query(query);
216
217 TSQLRow *row = 0;
218 while ((row = res->Next()))
219 if (!GetSequence(serv, *row, sequpath))
220 return 2;
221
222 delete res;
223
224 cout << endl;
225
226 return 1;
227}
Note: See TracBrowser for help on using the repository browser.