| 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 | // insertsequence.C | 
|---|
| 28 | // ================ | 
|---|
| 29 | // | 
|---|
| 30 | // This macro inserts a sequence into the database. It extracts the | 
|---|
| 31 | // information from a sequence file. | 
|---|
| 32 | // This macro is not used by the automatic analysis. It is needed to insert | 
|---|
| 33 | // manually built sequences into the database. | 
|---|
| 34 | // | 
|---|
| 35 | // Usage: | 
|---|
| 36 | //  .x insertsequence.C+("filename", kTRUE) | 
|---|
| 37 | // The first argument is the filename of the manual written sequencefile. | 
|---|
| 38 | // The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is | 
|---|
| 39 | // switched on and nothing will be written into the database. This is usefull | 
|---|
| 40 | // for tests. | 
|---|
| 41 | // | 
|---|
| 42 | // Make sure, that database and password are corretly set in a resource | 
|---|
| 43 | // file called sql.rc and the resource file is found. | 
|---|
| 44 | // | 
|---|
| 45 | // Returns 0 in case of failure and 1 in case of success. | 
|---|
| 46 | // | 
|---|
| 47 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 48 |  | 
|---|
| 49 | #include <iostream> | 
|---|
| 50 | #include <iomanip> | 
|---|
| 51 | #include <fstream> | 
|---|
| 52 |  | 
|---|
| 53 | #include <TEnv.h> | 
|---|
| 54 |  | 
|---|
| 55 | #include <MSQLServer.h> | 
|---|
| 56 | #include <TSQLRow.h> | 
|---|
| 57 | #include <TSQLResult.h> | 
|---|
| 58 |  | 
|---|
| 59 | using namespace std; | 
|---|
| 60 |  | 
|---|
| 61 | Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test) | 
|---|
| 62 | { | 
|---|
| 63 | TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test)); | 
|---|
| 64 | TSQLResult *res = serv.Query(query); | 
|---|
| 65 | if (!res) | 
|---|
| 66 | return kFALSE; | 
|---|
| 67 |  | 
|---|
| 68 | Bool_t rc = kFALSE; | 
|---|
| 69 |  | 
|---|
| 70 | TSQLRow *row=res->Next(); | 
|---|
| 71 | if (row && (*row)[0]) | 
|---|
| 72 | rc=kTRUE; | 
|---|
| 73 |  | 
|---|
| 74 | delete res; | 
|---|
| 75 | return rc; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE) | 
|---|
| 79 | { | 
|---|
| 80 | TString query; | 
|---|
| 81 |  | 
|---|
| 82 | query = Form("SELECT f%sKEY FROM %s WHERE f%sName='%s'", col, col, col, name); | 
|---|
| 83 | TSQLResult *res = serv.Query(query); | 
|---|
| 84 | if (!res) | 
|---|
| 85 | return -1; | 
|---|
| 86 |  | 
|---|
| 87 | TSQLRow *row=res->Next(); | 
|---|
| 88 |  | 
|---|
| 89 | Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1; | 
|---|
| 90 |  | 
|---|
| 91 | delete res; | 
|---|
| 92 |  | 
|---|
| 93 | if (rc>=0) | 
|---|
| 94 | return rc; | 
|---|
| 95 |  | 
|---|
| 96 | if (!insert) | 
|---|
| 97 | return -1; | 
|---|
| 98 |  | 
|---|
| 99 | query = Form("INSERT %s (f%sName) VALUES (\"%s\");", col, col, name); | 
|---|
| 100 |  | 
|---|
| 101 | if (dummy) | 
|---|
| 102 | { | 
|---|
| 103 | cout << "Q:" << query << endl; | 
|---|
| 104 | return 0; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | res=serv.Query(query); | 
|---|
| 108 | if (!res) | 
|---|
| 109 | { | 
|---|
| 110 | cout << "Error in query " << query << endl; | 
|---|
| 111 | return -1; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | delete res; | 
|---|
| 115 |  | 
|---|
| 116 | Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE); | 
|---|
| 117 | if (key>0) | 
|---|
| 118 | { | 
|---|
| 119 | cout << "New " << col << ": " << name << endl; | 
|---|
| 120 | return key; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | cout << "ERROR: " << query << endl; | 
|---|
| 124 | return kFALSE; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | int insertsequence(TString filename, Bool_t dummy=kTRUE) | 
|---|
| 128 | { | 
|---|
| 129 | TEnv env("sql.rc"); | 
|---|
| 130 | TEnv sequ(filename); | 
|---|
| 131 |  | 
|---|
| 132 | MSQLServer serv(env); | 
|---|
| 133 | if (!serv.IsConnected()) | 
|---|
| 134 | { | 
|---|
| 135 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 136 | return 0; | 
|---|
| 137 | } | 
|---|
| 138 | cout << "insertsequence" << endl; | 
|---|
| 139 | cout << "--------------" << endl; | 
|---|
| 140 | cout << endl; | 
|---|
| 141 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 142 | cout << endl; | 
|---|
| 143 |  | 
|---|
| 144 | //get sequence number from file | 
|---|
| 145 | TString sequnum; | 
|---|
| 146 | sequnum=sequ.GetValue("Sequence", ""); | 
|---|
| 147 | Int_t seq=atoi(sequnum.Data()); | 
|---|
| 148 |  | 
|---|
| 149 | //get runs from sequence file | 
|---|
| 150 | TString runs; | 
|---|
| 151 | runs = sequ.GetValue("Runs", ""); | 
|---|
| 152 | runs.ReplaceAll(" ", ","); | 
|---|
| 153 | if (runs.IsNull()) | 
|---|
| 154 | { | 
|---|
| 155 | cout << "ERROR - No runs in file " << filename << " found." << endl; | 
|---|
| 156 | return 0; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | //get source key | 
|---|
| 160 | TString sourcename = sequ.GetValue("Source", ""); | 
|---|
| 161 | Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", sourcename.Data()); | 
|---|
| 162 |  | 
|---|
| 163 | //get values for the sequence | 
|---|
| 164 | TString query="SELECT max(fRunNumber), min(fRunStart), "; | 
|---|
| 165 | query +="sum(time_to_sec(fRunStop)-time_to_sec(fRunStart)), "; | 
|---|
| 166 | query +="min(fZenithDistance), max(fZenithDistance), "; | 
|---|
| 167 | query +="min(fAzimuth), max(fAzimuth), sum(fNumEvents) from RunData "; | 
|---|
| 168 | query +=Form("WHERE fRunNumber IN (%s)",runs.Data()); | 
|---|
| 169 |  | 
|---|
| 170 | cout << "runs: " << runs << endl; | 
|---|
| 171 | cout << "q1: " << query << endl; | 
|---|
| 172 |  | 
|---|
| 173 | TSQLResult *res = serv.Query(query); | 
|---|
| 174 | if (!res) | 
|---|
| 175 | return 0; | 
|---|
| 176 |  | 
|---|
| 177 | TSQLRow *row=res->Next(); | 
|---|
| 178 | TString lastrun=(*row)[0]; | 
|---|
| 179 | TString starttime=(*row)[1]; | 
|---|
| 180 | TString uptime=(*row)[2]; | 
|---|
| 181 | TString zdmin=(*row)[3]; | 
|---|
| 182 | TString zdmax=(*row)[4]; | 
|---|
| 183 | TString azmin=(*row)[5]; | 
|---|
| 184 | TString azmax=(*row)[6]; | 
|---|
| 185 | TString numevts=(*row)[7]; | 
|---|
| 186 |  | 
|---|
| 187 | delete res; | 
|---|
| 188 |  | 
|---|
| 189 | query  ="SELECT fProjectKEY, fSourceKEY, fHvSettingsKEY, "; | 
|---|
| 190 | query +="fTriggerDelayTableKEY, fDiscriminatorThresholdTableKEY, "; | 
|---|
| 191 | query +="fTestFlagKEY, fLightConditionsKEY, fL1TriggerTableKEY, "; | 
|---|
| 192 | query +=Form("fL2TriggerTableKEY FROM RunData WHERE fRunNumber=%d", seq); | 
|---|
| 193 |  | 
|---|
| 194 | cout << "q2: " << query << endl; | 
|---|
| 195 |  | 
|---|
| 196 | res = serv.Query(query); | 
|---|
| 197 | if (!res) | 
|---|
| 198 | return 0; | 
|---|
| 199 |  | 
|---|
| 200 | row=res->Next(); | 
|---|
| 201 | TString project=(*row)[0]; | 
|---|
| 202 | TString source=(*row)[1]; | 
|---|
| 203 | TString hv=(*row)[2]; | 
|---|
| 204 | TString delay=(*row)[3]; | 
|---|
| 205 | TString dt=(*row)[4]; | 
|---|
| 206 | TString testflag=(*row)[5]; | 
|---|
| 207 | TString lightcond=(*row)[6]; | 
|---|
| 208 | TString l1tt=(*row)[7]; | 
|---|
| 209 | TString l2tt=(*row)[8]; | 
|---|
| 210 |  | 
|---|
| 211 | delete res; | 
|---|
| 212 |  | 
|---|
| 213 | cout << "seq: "       << seq        << endl; | 
|---|
| 214 | cout << "  lastrun   " << lastrun   << endl; | 
|---|
| 215 | cout << "  startime  " << starttime << endl; | 
|---|
| 216 | cout << "  uptime    " << uptime    << endl; | 
|---|
| 217 | cout << "  zdmin     " << zdmin     << endl; | 
|---|
| 218 | cout << "  zdmax     " << zdmax     << endl; | 
|---|
| 219 | cout << "  azmin     " << azmin     << endl; | 
|---|
| 220 | cout << "  azmax     " << azmax     << endl; | 
|---|
| 221 | cout << "  numevts   " << numevts   << endl; | 
|---|
| 222 | cout << " keys:"                    <<  endl; | 
|---|
| 223 | cout << "  project   " << project   << endl; | 
|---|
| 224 | cout << "  source1   " << source    << " (from db -> run "  << seq << ") " << endl; | 
|---|
| 225 | cout << "  source2   " << sourcekey << " (from sequ file) " << endl; | 
|---|
| 226 | if (!(atoi(source.Data())==sourcekey)) | 
|---|
| 227 | { | 
|---|
| 228 | cout << "new source name: " << sourcename << " -> inserting..." << endl; | 
|---|
| 229 | sourcekey = QueryNameKEY(serv, dummy, "Source", sourcename.Data(), kFALSE); | 
|---|
| 230 | source=Form("%d",sourcekey); | 
|---|
| 231 | } | 
|---|
| 232 | cout << "  source    " << source    << endl; | 
|---|
| 233 | cout << "  hv        " << hv        << endl; | 
|---|
| 234 | cout << "  delay     " << delay     << endl; | 
|---|
| 235 | cout << "  dt        " << dt        << endl; | 
|---|
| 236 | cout << "  testflag  " << testflag  << endl; | 
|---|
| 237 | cout << "  lightcond " << lightcond << endl; | 
|---|
| 238 | cout << "  l1tt      " << l1tt      << endl; | 
|---|
| 239 | cout << "  l2tt      " << l2tt      << endl; | 
|---|
| 240 |  | 
|---|
| 241 | //build queries | 
|---|
| 242 | TString query1="INSERT Sequences SET fManuallyChangedKEY=2, "; | 
|---|
| 243 | query1 +=Form("fSequenceFirst=%d, fSequenceLast=%s, " | 
|---|
| 244 | "fProjectKEY=%s, fSourceKEY=%s, fNumEvents=%s, " | 
|---|
| 245 | "fRunStart='%s', fHvSettingsKEY=%s, fRunTime=%s, " | 
|---|
| 246 | "fTriggerDelayTableKEY=%s, fDiscriminatorThresholdTableKEY=%s, " | 
|---|
| 247 | "fTestFlagKEY=%s, fLightConditionsKEY=%s, fAzimuthMin=%s, " | 
|---|
| 248 | "fAzimuthMax=%s, fZenithDistanceMin=%s, fZenithDistanceMax=%s, " | 
|---|
| 249 | "fL1TriggerTableKEY=%s, fL2TriggerTableKEY=%s ", | 
|---|
| 250 | seq, lastrun.Data(), project.Data(), source.Data(), | 
|---|
| 251 | numevts.Data(), starttime.Data(), hv.Data(), uptime.Data(), | 
|---|
| 252 | delay.Data(), dt.Data(), testflag.Data(), lightcond.Data(), | 
|---|
| 253 | azmin.Data(), azmax.Data(), zdmin.Data(), zdmax.Data(), | 
|---|
| 254 | l1tt.Data(), l2tt.Data()); | 
|---|
| 255 |  | 
|---|
| 256 | //the time of the column fSequenceFileWritten is set to 'not to be done' | 
|---|
| 257 | TString query2=Form("INSERT SequenceProcessStatus SET " | 
|---|
| 258 | "fSequenceFirst=%d, fSequenceFileWritten='1970-01-01 00:00:00'", | 
|---|
| 259 | seq); | 
|---|
| 260 |  | 
|---|
| 261 | if (dummy) | 
|---|
| 262 | { | 
|---|
| 263 | cout << "q1: " << query1 << endl; | 
|---|
| 264 | cout << "q2: " << query2 << endl; | 
|---|
| 265 | return 1; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 |  | 
|---|
| 269 | res = serv.Query(query1); | 
|---|
| 270 | if (!res) | 
|---|
| 271 | { | 
|---|
| 272 | cout << "ERROR: query1 failed: " << query1 << endl; | 
|---|
| 273 | return 0; | 
|---|
| 274 | } | 
|---|
| 275 | delete res; | 
|---|
| 276 |  | 
|---|
| 277 | res = serv.Query(query2); | 
|---|
| 278 | if (!res) | 
|---|
| 279 | { | 
|---|
| 280 | cout << "ERROR: query2 failed: " << query2 << endl; | 
|---|
| 281 | return 0; | 
|---|
| 282 | } | 
|---|
| 283 | delete res; | 
|---|
| 284 |  | 
|---|
| 285 | return 1; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 |  | 
|---|