source: trunk/MagicSoft/Mars/datacenter/macros/getdolist.C@ 9014

Last change on this file since 9014 was 8996, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 7.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): Daniela Dorner, 01/2005 <mailto:dorner@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2006
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// getdolist.C
28// ===========
29//
30// This macro is a key part of the automation concept.
31// It queries from the database, for which dates/runs/sequences/datasets a
32// certain step has to be done and writes the todo-files.
33//
34// Usage:
35// .x getdolist.C+("table", "column", "date", "listpath")
36// The first and second argument give the table and column in the database
37// and specify like this the primary (date/run/sequenc/dataset) and the
38// step.
39// The third argument specifies the date, for which the macro has to be
40// executed. This is currently not used in the automatic analysis. It was
41// forseen to give the possibility to process the data night by night. By
42// giving date='NULL' the macro is executed for all dates.
43// The forth argument gives the path, where teh todo-file is stored.
44//
45// The macro needs apart from sql.rc the resource file steps.rc
46// In this files the interdependencies of the analysis steps are given: To
47// execute a certain step other steps have to be executed first and each
48// step also influences othere steps - this information is stored in steps.rc
49// example:
50// To execute the calibration, all files have to be available on disk (needs).
51// And if the calibration is done, fillcallisto and star can be executed
52// (influences).
53//
54// Make sure, that database and password are corretly set in a resource
55// file called sql.rc and the resource file is found.
56//
57// Returns 0 in case of failure and 1 in case of success.
58//
59/////////////////////////////////////////////////////////////////////////////
60
61#include <iostream>
62#include <iomanip>
63#include <fstream>
64
65#include <TEnv.h>
66#include <TObjString.h>
67#include <TList.h>
68#include <TSystem.h>
69
70#include <TSQLRow.h>
71#include <TSQLResult.h>
72
73#include "MSQLServer.h"
74
75using namespace std;
76
77
78int getdolist(TString table, TString column, TString date, TString listpath)
79{
80 MSQLServer serv("sql.rc");
81 if (!serv.IsConnected())
82 {
83 cout << "ERROR - Connection to database failed." << endl;
84 return 0;
85 }
86 cout << "getstatus" << endl;
87 cout << "---------" << endl;
88 cout << endl;
89 cout << "Connected to " << serv.GetName() << endl;
90 cout << endl;
91
92 TEnv rc("steps.rc");
93
94 //read in needs
95 TString needs = rc.GetValue(table+"."+column+".Needs", "");
96 cout << "Needs: " << needs << endl;
97
98 TList l;
99 while (!needs.IsNull())
100 {
101 needs = needs.Strip(TString::kBoth);
102
103 Int_t idx = needs.First(' ');
104 if (idx<0)
105 idx = needs.Length();
106
107 TString need = needs(0, idx);
108 needs.Remove(0, idx);
109 l.Add(new TObjString(need));
110 }
111
112 //build query
113 // this forms a query like e.g.
114 //SELECT SequenceProcessStatus.fSequenceFirst FROM SequenceProcessStatus
115 // WHERE ISNULL(fCallisto)
116 // AND NOT ISNULL(fAllFilesAvail) AND NOT ISNULL(fSequenceFileWritten)
117 //which would query all sequences for which Callisto has to be done.
118 TString query(Form("SELECT %s.%s FROM %s",
119 table.Data(), rc.GetValue(table+".Primary", ""),
120 table.Data()));
121
122 //get joins
123 //if a date is given, the tables, which contain the information about the
124 // date (given in step.rc as TimerTable), have to be joined
125 if (date!="NULL" && rc.GetValue(table+".TimerTable", "")!="")
126 query+=Form(" left join %s on %s.%s=%s.%s ",
127 rc.GetValue(table+".TimerTable", ""), table.Data(),
128 rc.GetValue(table+".Primary", ""),
129 rc.GetValue(table+".TimerTable", ""),
130 rc.GetValue(table+".Primary", ""));
131 query+=Form(" WHERE ISNULL(%s) AND ISNULL(fReturnCode) AND ISNULL(fFailedCode) AND ISNULL(fFailedCodeAdd) AND ISNULL(fStartTime) AND ISNULL(fFailedTime) ", column.Data());
132
133 TIter Next(&l);
134 TObject *o=0;
135 while ((o=Next()))
136 query+=Form(" AND NOT ISNULL(%s)", o->GetName());
137
138 //if a date is given another condition is added to the query
139 if (date!="NULL")
140 {
141 if (rc.GetValue(table+".TimerTable", "")!="")
142 {
143 TString day=date+" 13:00:00";
144 query+=Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
145 day.Data(), day.Data());
146 }
147 else
148 query+=Form(" AND %s=%s ", rc.GetValue(table+".Primary", ""), date.Data());
149 }
150
151 //to process the newest data first, the results are ordered descending by the primary
152 query+=Form(" ORDER BY %s.%s DESC ", table.Data(), rc.GetValue(table+".Primary", ""));
153
154 //this part of the query is needed, if there are more than 512 results and single
155 //todo files are written, as shell scripts have a limitation for arrays
156 if ((table=="SequenceProcessStatus" && column=="fCallisto") ||
157 (table=="SequenceProcessStatus" && column=="fStar") ||
158 (table=="RunProcessStatus" && column=="fDataCheckDone"))
159 query+="LIMIT 0, 500";
160
161 cout << "query: " << query << endl;
162
163 TSQLResult *res = serv.Query(query);
164 if (!res)
165 {
166 cout << "ERROR - Query failed: " << query << endl;
167 return 0;
168 }
169
170
171 //write todo-file
172 //depending on the kind of step and primary, this looks different
173 //for steps, which do not take much time and can thus be executed
174 // for all data at once (e.g. buildsequenceentries) only one todo
175 // file is written
176 //for steps, which take quite long for one primary (e.g. calibration)
177 // or of which a large number has to be executed (e.g. datacheck)
178 // one todo-file per primary is written
179 TString filename;
180 TSQLRow *row=0;
181
182 if ((table=="SequenceProcessStatus" && column=="fCallisto") ||
183 (table=="SequenceProcessStatus" && column=="fStar") ||
184 (table=="RunProcessStatus" && column=="fDataCheckDone") ||
185 (table=="DataSetProcessStatus" && column=="fGanymed"))
186 {
187 while ((row = res->Next()))
188 {
189 filename=Form("%s/ToDo-%s-%s-%s.txt", listpath.Data(), table.Data(), column.Data(), (*row)[0]);
190 //remove file, if it is already existing
191 // this is needed, that the same step is not executed several times
192 gSystem->Unlink(filename);
193 ofstream fout(filename, ios::app);
194 if (!fout)
195 {
196 cout << "ERROR - Cannot open file " << filename << endl;
197 return 0;
198 }
199 fout << (*row)[0] << endl;
200 }
201 }
202 else
203 {
204 filename=Form("%s/ToDo-%s-%s.txt", listpath.Data(), table.Data(), column.Data());
205 ofstream fout(filename, ios::app);
206 if (!fout)
207 {
208 cout << "ERROR - Cannot open file " << filename << endl;
209 return 0;
210 }
211
212 while ((row = res->Next()))
213 fout << (*row)[0] << endl;
214 }
215
216 delete res;
217
218 return 1;
219}
220
221
Note: See TracBrowser for help on using the repository browser.