| 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 |  | 
|---|
| 74 | using namespace std; | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | int 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) AND ISNULL(fReturnCode) AND ISNULL(fFailedCode) AND ISNULL(fFailedCodeAdd) AND ISNULL(fStartTime) AND ISNULL(fFailedTime) ", 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 | if ((table=="SequenceProcessStatus" && column=="fCallisto")      || | 
|---|
| 158 | (table=="SequenceProcessStatus" && column=="fStar")          || | 
|---|
| 159 | (table=="RunProcessStatus"      && column=="fDataCheckDone")) | 
|---|
| 160 | query+="LIMIT 0, 500"; | 
|---|
| 161 |  | 
|---|
| 162 | cout << "query: " << query << endl; | 
|---|
| 163 |  | 
|---|
| 164 | TSQLResult *res = serv.Query(query); | 
|---|
| 165 | if (!res) | 
|---|
| 166 | { | 
|---|
| 167 | cout << "ERROR - Query failed: " << query << endl; | 
|---|
| 168 | return 0; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | //write todo-file | 
|---|
| 173 | //depending on the kind of step and primary, this looks different | 
|---|
| 174 | //for steps, which do not take much time and can thus be executed | 
|---|
| 175 | // for all data at once (e.g. buildsequenceentries) only one todo | 
|---|
| 176 | // file is written | 
|---|
| 177 | //for steps, which take quite long for one primary (e.g. calibration) | 
|---|
| 178 | // or of which a large number has to be executed (e.g. datacheck) | 
|---|
| 179 | // one todo-file per primary is written | 
|---|
| 180 | TString filename; | 
|---|
| 181 | TSQLRow *row=0; | 
|---|
| 182 |  | 
|---|
| 183 | if ((table=="SequenceProcessStatus" && column=="fCallisto")      || | 
|---|
| 184 | (table=="SequenceProcessStatus" && column=="fStar")          || | 
|---|
| 185 | (table=="RunProcessStatus"      && column=="fDataCheckDone") || | 
|---|
| 186 | (table=="DataSetProcessStatus"  && column=="fGanymed")) | 
|---|
| 187 | { | 
|---|
| 188 | while ((row = res->Next())) | 
|---|
| 189 | { | 
|---|
| 190 | filename=Form("%s/ToDo-%s-%s-%s.txt", listpath.Data(), table.Data(), column.Data(), (*row)[0]); | 
|---|
| 191 | //remove file, if it is already existing | 
|---|
| 192 | // this is needed, that the same step is not executed several times | 
|---|
| 193 | gSystem->Unlink(filename); | 
|---|
| 194 | ofstream fout(filename, ios::app); | 
|---|
| 195 | if (!fout) | 
|---|
| 196 | { | 
|---|
| 197 | cout << "ERROR - Cannot open file " << filename << endl; | 
|---|
| 198 | return 0; | 
|---|
| 199 | } | 
|---|
| 200 | fout << (*row)[0] << endl; | 
|---|
| 201 | } | 
|---|
| 202 | } | 
|---|
| 203 | else | 
|---|
| 204 | { | 
|---|
| 205 | filename=Form("%s/ToDo-%s-%s.txt", listpath.Data(), table.Data(), column.Data()); | 
|---|
| 206 | ofstream fout(filename, ios::app); | 
|---|
| 207 | if (!fout) | 
|---|
| 208 | { | 
|---|
| 209 | cout << "ERROR - Cannot open file " << filename << endl; | 
|---|
| 210 | return 0; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | while ((row = res->Next())) | 
|---|
| 214 | fout << (*row)[0] << endl; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | delete res; | 
|---|
| 218 |  | 
|---|
| 219 | return 1; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 |  | 
|---|