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

Last change on this file since 7460 was 7460, checked in by Daniela Dorner, 19 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): 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 <MSQLServer.h>
71#include <TSQLRow.h>
72#include <TSQLResult.h>
73
74using namespace std;
75
76
77int getdolist(TString table, TString column, TString date, TString listpath)
78{
79 TEnv env("sql.rc");
80
81 MSQLServer serv(env);
82 if (!serv.IsConnected())
83 {
84 cout << "ERROR - Connection to database failed." << endl;
85 return 0;
86 }
87 cout << "getstatus" << endl;
88 cout << "---------" << endl;
89 cout << endl;
90 cout << "Connected to " << serv.GetName() << endl;
91 cout << endl;
92
93 TEnv rc("steps.rc");
94
95 //read in needs
96 TString needs = rc.GetValue(table+"."+column+".Needs", "");
97 cout << "Needs: " << needs << endl;
98
99 TList l;
100 while (!needs.IsNull())
101 {
102 needs = needs.Strip(TString::kBoth);
103
104 Int_t idx = needs.First(' ');
105 if (idx<0)
106 idx = needs.Length();
107
108 TString need = needs(0, idx);
109 needs.Remove(0, idx);
110 l.Add(new TObjString(need));
111 }
112
113 //build query
114 // this forms a query like e.g.
115 //SELECT SequenceProcessStatus.fSequenceFirst FROM SequenceProcessStatus
116 // WHERE ISNULL(fCallisto)
117 // AND NOT ISNULL(fAllFilesAvail) AND NOT ISNULL(fSequenceFileWritten)
118 //which would query all sequences for which Callisto has to be done.
119 TString query(Form("SELECT %s.%s FROM %s",
120 table.Data(), rc.GetValue(table+".Primary", ""),
121 table.Data()));
122
123 //get joins
124 //if a date is given, the tables, which contain the information about the
125 // date (given in step.rc as TimerTable), have to be joined
126 if (date!="NULL" && rc.GetValue(table+".TimerTable", "")!="")
127 query+=Form(" left join %s on %s.%s=%s.%s ",
128 rc.GetValue(table+".TimerTable", ""), table.Data(),
129 rc.GetValue(table+".Primary", ""),
130 rc.GetValue(table+".TimerTable", ""),
131 rc.GetValue(table+".Primary", ""));
132 query+=Form(" WHERE ISNULL(%s)", column.Data());
133
134 TIter Next(&l);
135 TObject *o=0;
136 while ((o=Next()))
137 query+=Form(" AND NOT ISNULL(%s)", o->GetName());
138
139 //if a date is given another condition is added to the query
140 if (date!="NULL")
141 {
142 if (rc.GetValue(table+".TimerTable", "")!="")
143 {
144 TString day=date+" 13:00:00";
145 query+=Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
146 day.Data(), day.Data());
147 }
148 else
149 query+=Form(" AND %s=%s ", rc.GetValue(table+".Primary", ""), date.Data());
150 }
151
152 //to process the newest data first, the results are ordered descending by the primary
153 query+=Form(" ORDER BY %s.%s DESC ", table.Data(), rc.GetValue(table+".Primary", ""));
154
155 //this part of the query is needed, if there are more than 512 results and single
156 //todo files are written, as shell scripts have a limitation for arrays
157 // this is currently only the case, if runs are processed, i.e. for the table RunProcessStatus
158 if (table=="RunProcessStatus")
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.