| 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-2004
|
|---|
| 22 | !
|
|---|
| 23 | !
|
|---|
| 24 | \* ======================================================================== */
|
|---|
| 25 |
|
|---|
| 26 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | //
|
|---|
| 28 | // buildsequenceentries.C
|
|---|
| 29 | // ======================
|
|---|
| 30 | //
|
|---|
| 31 | // to group the runs of one night into sequences, this marco:
|
|---|
| 32 | // - reads the runinformation of one night from the database
|
|---|
| 33 | // - group the runs into sets of following runs with the same conditions
|
|---|
| 34 | // - groups the runs in this sets to sequences such that each run belongs
|
|---|
| 35 | // to the nearest (in time) calibration run
|
|---|
| 36 | // - check if the runs with the same runtype have the same calibration script
|
|---|
| 37 | // and the same trigger tables
|
|---|
| 38 | //
|
|---|
| 39 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 40 | #include <iostream>
|
|---|
| 41 | #include <iomanip>
|
|---|
| 42 | #include <fstream>
|
|---|
| 43 |
|
|---|
| 44 | #include <MSQLServer.h>
|
|---|
| 45 | #include <TSQLRow.h>
|
|---|
| 46 | #include <TSQLResult.h>
|
|---|
| 47 |
|
|---|
| 48 | #include <TEnv.h>
|
|---|
| 49 | #include <TMath.h>
|
|---|
| 50 | #include <TExMap.h>
|
|---|
| 51 | #include <TArrayI.h>
|
|---|
| 52 | #include <TRegexp.h>
|
|---|
| 53 | #include <TSystem.h>
|
|---|
| 54 |
|
|---|
| 55 | #include <MTime.h>
|
|---|
| 56 | #include <MDirIter.h>
|
|---|
| 57 |
|
|---|
| 58 | using namespace std;
|
|---|
| 59 |
|
|---|
| 60 | int debug = 0;
|
|---|
| 61 |
|
|---|
| 62 | Bool_t DeleteSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t sequ, Bool_t dummy)
|
|---|
| 63 | {
|
|---|
| 64 | TString query1(Form("DELETE FROM Calibration WHERE fSequenceFirst=%d", sequ));
|
|---|
| 65 | TString query2(Form("DELETE FROM Star WHERE fSequenceFirst=%d", sequ));
|
|---|
| 66 | TString query3(Form("DELETE FROM SequenceProcessStatus WHERE fSequenceFirst=%d", sequ));
|
|---|
| 67 | TString query4(Form("UPDATE RunData SET fSequenceFirst=0 WHERE fSequenceFirst=%d", sequ));
|
|---|
| 68 | TString query5(Form("DELETE FROM Sequences WHERE fSequenceFirst=%d AND fManuallyChangedKEY=1", sequ));
|
|---|
| 69 |
|
|---|
| 70 | TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(),sequ/10000, sequ));
|
|---|
| 71 | TString command(Form("rm -r %s/callisto/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
|---|
| 72 | TString command2(Form("rm -r %s/star/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
|---|
| 73 |
|
|---|
| 74 | if (dummy)
|
|---|
| 75 | {
|
|---|
| 76 | cout << "not using dummy=kTRUE the following commands would be executed: " << endl;
|
|---|
| 77 | cout << "queries: " << endl;
|
|---|
| 78 | cout << query1 << endl;
|
|---|
| 79 | cout << query2 << endl;
|
|---|
| 80 | cout << query3 << endl;
|
|---|
| 81 | cout << query4 << endl;
|
|---|
| 82 | cout << query5 << endl;
|
|---|
| 83 | cout << "removing files:" << endl;
|
|---|
| 84 | cout << "unlink " << fname << endl;
|
|---|
| 85 | cout << command << endl;
|
|---|
| 86 | cout << command2 << endl;
|
|---|
| 87 | return kTRUE;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | TSQLResult *res = serv.Query(query1);
|
|---|
| 91 | if (!res)
|
|---|
| 92 | return kFALSE;
|
|---|
| 93 | delete res;
|
|---|
| 94 |
|
|---|
| 95 | res = serv.Query(query2);
|
|---|
| 96 | if (!res)
|
|---|
| 97 | return kFALSE;
|
|---|
| 98 | delete res;
|
|---|
| 99 |
|
|---|
| 100 | res = serv.Query(query3);
|
|---|
| 101 | if (!res)
|
|---|
| 102 | return kFALSE;
|
|---|
| 103 | delete res;
|
|---|
| 104 |
|
|---|
| 105 | res = serv.Query(query4);
|
|---|
| 106 | if (!res)
|
|---|
| 107 | return kFALSE;
|
|---|
| 108 | delete res;
|
|---|
| 109 |
|
|---|
| 110 | res = serv.Query(query5);
|
|---|
| 111 | if (!res)
|
|---|
| 112 | return kFALSE;
|
|---|
| 113 | delete res;
|
|---|
| 114 |
|
|---|
| 115 | gSystem->Unlink(fname);
|
|---|
| 116 |
|
|---|
| 117 | gSystem->Exec(command);
|
|---|
| 118 | gSystem->Exec(command2);
|
|---|
| 119 |
|
|---|
| 120 | return kTRUE;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | Int_t DoCheck(TSQLResult &res)
|
|---|
| 124 | {
|
|---|
| 125 | TArrayI data(5);
|
|---|
| 126 | Int_t n = 0;
|
|---|
| 127 |
|
|---|
| 128 | TSQLRow *row=0;
|
|---|
| 129 | while ((row=res.Next()))
|
|---|
| 130 | {
|
|---|
| 131 | n++;
|
|---|
| 132 |
|
|---|
| 133 | if (data[0]==0)
|
|---|
| 134 | {
|
|---|
| 135 | for (int i=0; i<data.GetSize(); i++)
|
|---|
| 136 | data[i] = atoi((*row)[i]);
|
|---|
| 137 | continue;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | for (int i=1; i<data.GetSize(); i++)
|
|---|
| 141 | {
|
|---|
| 142 | if (data[i] != atoi((*row)[i]))
|
|---|
| 143 | return i+1;
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | return n==0 ? 0 : -1;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | Bool_t CheckRuns(MSQLServer &serv, Int_t from, Int_t to, Int_t type)
|
|---|
| 150 | {
|
|---|
| 151 | TString query("SELECT fRunNumber, fL1TriggerTableKEY, fL2TriggerTableKEY,"
|
|---|
| 152 | " fCalibrationScriptKEY, fProjectKEY FROM RunData");
|
|---|
| 153 | query += Form(" WHERE fRunTypeKEY=%d AND fExcludedFDAKEY=1 AND "
|
|---|
| 154 | " (fRunNumber BETWEEN %d AND %d)"
|
|---|
| 155 | " ORDER BY fRunNumber", type, from, to);
|
|---|
| 156 |
|
|---|
| 157 | TSQLResult *res = serv.Query(query);
|
|---|
| 158 | if (!res)
|
|---|
| 159 | return kFALSE;
|
|---|
| 160 |
|
|---|
| 161 | Int_t rc = DoCheck(*res);
|
|---|
| 162 | delete res;
|
|---|
| 163 |
|
|---|
| 164 | switch (rc)
|
|---|
| 165 | {
|
|---|
| 166 | case 0: cout << "ERROR - No runs found for check!" << endl; break;
|
|---|
| 167 | case 1: cout << "ERROR - fRunNumber doesn't match!" << endl; break;
|
|---|
| 168 | case 2: cout << "ERROR - fL1TriggerTableKEY doesn't match!" << endl; break;
|
|---|
| 169 | case 3: cout << "ERROR - fL2TriggerTableKEY doesn't match!" << endl; break;
|
|---|
| 170 | case 4: cout << "ERROR - fCalibrationScriptKEY doesn't match!" << endl; break;
|
|---|
| 171 | case 5: cout << "ERROR - fProjectKEY doesn't match!" << endl; break;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | return rc<0;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | Bool_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
|---|
| 178 | {
|
|---|
| 179 | Int_t rc=0; //rc=0 means sequence is still the same -> insert not neccessary
|
|---|
| 180 | //rc=1 means deleting sequence(s) worked -> insert
|
|---|
| 181 | //if deleting sequence doesn't work -> return -1
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 | //getting # of sequence (in sequDB) between from and to
|
|---|
| 185 | TString query(Form("SELECT fSequenceFirst FROM Sequences WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
|---|
| 186 |
|
|---|
| 187 | TSQLResult *res = serv.Query(query);
|
|---|
| 188 | if (!res)
|
|---|
| 189 | return -1;
|
|---|
| 190 |
|
|---|
| 191 | TArrayI sequences;
|
|---|
| 192 | Int_t numsequ=0;
|
|---|
| 193 |
|
|---|
| 194 | TSQLRow *row=0;
|
|---|
| 195 | while ((row=res->Next()))
|
|---|
| 196 | {
|
|---|
| 197 | numsequ++;
|
|---|
| 198 | sequences.Set(numsequ);
|
|---|
| 199 | sequences.AddAt(atoi((*row)[0]), numsequ-1);
|
|---|
| 200 | }
|
|---|
| 201 | delete res;
|
|---|
| 202 |
|
|---|
| 203 | //if there's no sequence in the table Sequences -> check other tables
|
|---|
| 204 | //if there's one sequence -> check if the sequence is identical
|
|---|
| 205 | //if there are more sequences -> delete them
|
|---|
| 206 | switch (numsequ)
|
|---|
| 207 | {
|
|---|
| 208 | case 0:
|
|---|
| 209 | cout << "found no sequence in Sequ-DB -> check other tables" << endl;
|
|---|
| 210 | cout << " deleting every sequence found in Calibration, Star or SequenceProcessStatus between "
|
|---|
| 211 | << from << " and " << to << endl;
|
|---|
| 212 |
|
|---|
| 213 | //calibration table
|
|---|
| 214 | query(Form("SELECT fSequenceFirst FROM Calibration WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
|---|
| 215 | res = serv.Query(query);
|
|---|
| 216 | if (!res)
|
|---|
| 217 | return -1;
|
|---|
| 218 | row=0;
|
|---|
| 219 | while ((row=res->Next()))
|
|---|
| 220 | {
|
|---|
| 221 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
|---|
| 222 | return -1;
|
|---|
| 223 | else
|
|---|
| 224 | rc=1;
|
|---|
| 225 | }
|
|---|
| 226 | delete res;
|
|---|
| 227 |
|
|---|
| 228 | //Star table
|
|---|
| 229 | query(Form("SELECT fSequenceFirst FROM Star WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
|---|
| 230 | res = serv.Query(query);
|
|---|
| 231 | if (!res)
|
|---|
| 232 | return -1;
|
|---|
| 233 | row=0;
|
|---|
| 234 | while ((row=res->Next()))
|
|---|
| 235 | {
|
|---|
| 236 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
|---|
| 237 | return -1;
|
|---|
| 238 | else
|
|---|
| 239 | rc=1;
|
|---|
| 240 | }
|
|---|
| 241 | delete res;
|
|---|
| 242 |
|
|---|
| 243 | //SequenceProcessStatus table
|
|---|
| 244 | query(Form("SELECT fSequenceFirst FROM SequenceProcessStatus WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
|---|
| 245 | res = serv.Query(query);
|
|---|
| 246 | if (!res)
|
|---|
| 247 | return -1;
|
|---|
| 248 | row=0;
|
|---|
| 249 | while ((row=res->Next()))
|
|---|
| 250 | {
|
|---|
| 251 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
|---|
| 252 | return -1;
|
|---|
| 253 | else
|
|---|
| 254 | rc=1;
|
|---|
| 255 | }
|
|---|
| 256 | delete res;
|
|---|
| 257 | break;
|
|---|
| 258 |
|
|---|
| 259 | case 1:
|
|---|
| 260 | cout << "found 1 sequence: " << sequences.At(0) << " -> check sequ# " << endl;
|
|---|
| 261 | if (sequences.At(0)!=from)
|
|---|
| 262 | {
|
|---|
| 263 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
|---|
| 264 | return -1;
|
|---|
| 265 | else
|
|---|
| 266 | rc=1;
|
|---|
| 267 | }
|
|---|
| 268 | else
|
|---|
| 269 | {
|
|---|
| 270 | cout << "sequence# is the same -> checking the runs " << endl;
|
|---|
| 271 |
|
|---|
| 272 | //getting olf runs
|
|---|
| 273 | query(Form("SELECT fRunNumber FROM RunData WHERE fSequenceFirst=%d ", from));
|
|---|
| 274 | res = serv.Query(query);
|
|---|
| 275 | if (!res)
|
|---|
| 276 | return -1;
|
|---|
| 277 |
|
|---|
| 278 | TArrayI oldruns;
|
|---|
| 279 | Int_t count=0;
|
|---|
| 280 | row=0;
|
|---|
| 281 | while ((row=res->Next()))
|
|---|
| 282 | {
|
|---|
| 283 | count++;
|
|---|
| 284 | oldruns.Set(count);
|
|---|
| 285 | oldruns.AddAt(atoi((*row)[0]), count-1);
|
|---|
| 286 | }
|
|---|
| 287 | delete res;
|
|---|
| 288 |
|
|---|
| 289 | //getting new runs
|
|---|
| 290 | query(Form("SELECT fRunNumber FROM RunData WHERE fRunNumber BETWEEN %d and %d AND fExcludedFDAKEY=1", from, to));
|
|---|
| 291 | res = serv.Query(query);
|
|---|
| 292 | if (!res)
|
|---|
| 293 | return -1;
|
|---|
| 294 | TArrayI newruns;
|
|---|
| 295 | count=0;
|
|---|
| 296 | row=0;
|
|---|
| 297 | while ((row=res->Next()))
|
|---|
| 298 | {
|
|---|
| 299 | count++;
|
|---|
| 300 | oldruns.Set(count);
|
|---|
| 301 | oldruns.AddAt(atoi((*row)[0]), count-1);
|
|---|
| 302 | }
|
|---|
| 303 | delete res;
|
|---|
| 304 |
|
|---|
| 305 | //comparing old and new runs (first the # of runs, if it is the same, also the single runnumbers
|
|---|
| 306 | if (oldruns.GetSize()!=newruns.GetSize())
|
|---|
| 307 | {
|
|---|
| 308 | cout << " number of runs is not the same -> deleting sequence " << sequences.At(0) << endl;
|
|---|
| 309 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
|---|
| 310 | return -1;
|
|---|
| 311 | else
|
|---|
| 312 | rc=1;
|
|---|
| 313 | }
|
|---|
| 314 | else
|
|---|
| 315 | {
|
|---|
| 316 | cout << " number of runs is the same -> checking the single runnumbers " << endl;
|
|---|
| 317 |
|
|---|
| 318 | for (Int_t i=0;i<newruns.GetSize();i++)
|
|---|
| 319 | {
|
|---|
| 320 | if (newruns.At(i)==oldruns.At(i))
|
|---|
| 321 | continue;
|
|---|
| 322 |
|
|---|
| 323 | cout << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
|
|---|
| 324 | << ") -> deleting sequence " << sequences.At(0) << endl;
|
|---|
| 325 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
|---|
| 326 | return -1;
|
|---|
| 327 | else
|
|---|
| 328 | rc=1;
|
|---|
| 329 | break;
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 | break;
|
|---|
| 334 |
|
|---|
| 335 | default:
|
|---|
| 336 | cout << "found " << numsequ << " sequences -> deleting them " << endl;
|
|---|
| 337 |
|
|---|
| 338 | for (Int_t i=0;i<sequences.GetSize();i++)
|
|---|
| 339 | {
|
|---|
| 340 | cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
|
|---|
| 341 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
|
|---|
| 342 | return -1;
|
|---|
| 343 | else
|
|---|
| 344 | rc=1;
|
|---|
| 345 | }
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | return rc;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
|
|---|
| 352 | {
|
|---|
| 353 |
|
|---|
| 354 | // ========== Request number of events ==========
|
|---|
| 355 | TString query("SELECT SUM(fNumEvents), "
|
|---|
| 356 | " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
|
|---|
| 357 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
|
|---|
| 358 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
|
|---|
| 359 | query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
|
|---|
| 360 | query += " MIN(fAzimuth), MAX(fAzimuth) ";
|
|---|
| 361 | query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
|
|---|
| 362 | " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
|
|---|
| 363 | from, to);
|
|---|
| 364 |
|
|---|
| 365 | TSQLResult *res = serv.Query(query);
|
|---|
| 366 | if (!res)
|
|---|
| 367 | return kFALSE;
|
|---|
| 368 |
|
|---|
| 369 | TSQLRow *row = res->Next();
|
|---|
| 370 | if (!row || !(*row)[0])
|
|---|
| 371 | {
|
|---|
| 372 | cout << "ERROR - No result from query: " << query << endl;
|
|---|
| 373 | return kFALSE;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | TString nevts = (*row)[0];
|
|---|
| 377 | TString secs = (*row)[1];
|
|---|
| 378 | TString zdmin = (*row)[2];
|
|---|
| 379 | TString zdmax = (*row)[3];
|
|---|
| 380 | TString azmin = (*row)[4];
|
|---|
| 381 | TString azmax = (*row)[5];
|
|---|
| 382 |
|
|---|
| 383 | delete res;
|
|---|
| 384 |
|
|---|
| 385 | // ========== Request start time of sequence ==========
|
|---|
| 386 | query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
|
|---|
| 387 |
|
|---|
| 388 | res = serv.Query(query);
|
|---|
| 389 | if (!res)
|
|---|
| 390 | return kFALSE;
|
|---|
| 391 |
|
|---|
| 392 | row = res->Next();
|
|---|
| 393 | if (!row || !(*row)[0])
|
|---|
| 394 | {
|
|---|
| 395 | cout << "ERROR - No result from query: " << query << endl;
|
|---|
| 396 | return kFALSE;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | TString start((*row)[0]);
|
|---|
| 400 |
|
|---|
| 401 | delete res;
|
|---|
| 402 |
|
|---|
| 403 | // ========== Request data of sequence ==========
|
|---|
| 404 | query = Form("SELECT fSourceKEY, fProjectKEY, "
|
|---|
| 405 | " fL1TriggerTableKEY, fL1TriggerTableKEY,"
|
|---|
| 406 | " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
|
|---|
| 407 | " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
|
|---|
| 408 | " FROM RunData"
|
|---|
| 409 | " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
|---|
| 410 | " LIMIT 1", from, to);
|
|---|
| 411 |
|
|---|
| 412 | res = serv.Query(query);
|
|---|
| 413 | if (!res)
|
|---|
| 414 | return kFALSE;
|
|---|
| 415 |
|
|---|
| 416 | row = res->Next();
|
|---|
| 417 | if (!row)
|
|---|
| 418 | {
|
|---|
| 419 | cout << "ERROR - No result from query: " << query << endl;
|
|---|
| 420 | return kFALSE;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | TString query1("INSERT Sequences SET");
|
|---|
| 424 | query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
|
|---|
| 425 | query1+=Form(" fProjectKEY=%s,", (*row)[0]);
|
|---|
| 426 | query1+=Form(" fSourceKEY=%s,", (*row)[1]);
|
|---|
| 427 | query1+=Form(" fNumEvents=%s,", nevts.Data());
|
|---|
| 428 | query1+=Form(" fRunTime=%s,", secs.Data());
|
|---|
| 429 | query1+=Form(" fRunStart=\"%s\",", start.Data());
|
|---|
| 430 | query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
|
|---|
| 431 | query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
|
|---|
| 432 | query1+=Form(" fAzimuthMin=%s,", azmin.Data());
|
|---|
| 433 | query1+=Form(" fAzimuthMax=%s,", azmax.Data());
|
|---|
| 434 | query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
|
|---|
| 435 | query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
|
|---|
| 436 | query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
|
|---|
| 437 | query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
|
|---|
| 438 | query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
|
|---|
| 439 | query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
|
|---|
| 440 | query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 | TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
|
|---|
| 444 | " (fRunNumber BETWEEN %d AND %d) AND"
|
|---|
| 445 | " (fRunTypeKEY BETWEEN 2 AND 4) AND"
|
|---|
| 446 | " fSourceKEY=%d AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
|
|---|
| 447 | from, from, to, (*row)[1], (*row)[4]);
|
|---|
| 448 |
|
|---|
| 449 | TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
|
|---|
| 450 |
|
|---|
| 451 | delete res;
|
|---|
| 452 |
|
|---|
| 453 | res = serv.Query(query1);
|
|---|
| 454 | if (!res)
|
|---|
| 455 | return kFALSE;
|
|---|
| 456 | delete res;
|
|---|
| 457 |
|
|---|
| 458 | res = serv.Query(query2);
|
|---|
| 459 | if (!res)
|
|---|
| 460 | return kFALSE;
|
|---|
| 461 | delete res;
|
|---|
| 462 |
|
|---|
| 463 | res = serv.Query(query3);
|
|---|
| 464 | if (!res)
|
|---|
| 465 | return kFALSE;
|
|---|
| 466 | delete res;
|
|---|
| 467 |
|
|---|
| 468 | return kTRUE;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
|---|
| 472 | {
|
|---|
| 473 | cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
|
|---|
| 474 |
|
|---|
| 475 | if (!CheckRuns(serv, from, to, 2))
|
|---|
| 476 | {
|
|---|
| 477 | cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
|
|---|
| 478 | //sequence is not built, but kTRUE is returned, to allow
|
|---|
| 479 | //the automatic processing of the other sequences of this day
|
|---|
| 480 | return kTRUE;
|
|---|
| 481 | }
|
|---|
| 482 | if (!CheckRuns(serv, from, to, 3))
|
|---|
| 483 | {
|
|---|
| 484 | cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
|
|---|
| 485 | //sequence is not built, but kTRUE is returned, to allow
|
|---|
| 486 | //the automatic processing of the other sequences of this day
|
|---|
| 487 | return kTRUE;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | cout << "ok." << endl;
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 | cout << "checking Sequence..." << endl;
|
|---|
| 494 |
|
|---|
| 495 | Bool_t rc=kFALSE;
|
|---|
| 496 | switch (CheckSequence(serv, datapath, sequpath, from, to, dummy))
|
|---|
| 497 | {
|
|---|
| 498 | case 0:
|
|---|
| 499 | cout << " inserting sequence not necessary" << endl;
|
|---|
| 500 | return kTRUE;
|
|---|
| 501 |
|
|---|
| 502 | case 1:
|
|---|
| 503 | cout << " deleting successfully finished -> inserting sequence " << from << flush;
|
|---|
| 504 | if (dummy)
|
|---|
| 505 | {
|
|---|
| 506 | cout << " <dummy> " << endl;
|
|---|
| 507 | return kTRUE;
|
|---|
| 508 | }
|
|---|
| 509 | cout << endl;
|
|---|
| 510 | rc = InsertSequence(serv, from, to);
|
|---|
| 511 | if (!rc)
|
|---|
| 512 | cout << "InsertSequence failed!" << endl;
|
|---|
| 513 | break;
|
|---|
| 514 |
|
|---|
| 515 | case -1:
|
|---|
| 516 | cout << " deleting went wrong " << endl;
|
|---|
| 517 | rc=kFALSE;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 | return rc;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
|---|
| 525 | {
|
|---|
| 526 |
|
|---|
| 527 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
|
|---|
| 528 | " FROM RunData"
|
|---|
| 529 | " WHERE fRunNumber BETWEEN %d AND %d AND "
|
|---|
| 530 | " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
|
|---|
| 531 | " ORDER BY fRunNumber", from, to));
|
|---|
| 532 |
|
|---|
| 533 | TSQLResult *res = serv.Query(query);
|
|---|
| 534 | if (!res)
|
|---|
| 535 | return kFALSE;
|
|---|
| 536 |
|
|---|
| 537 | TExMap map;
|
|---|
| 538 |
|
|---|
| 539 | Int_t start=0;
|
|---|
| 540 | Int_t stop=0;
|
|---|
| 541 | Int_t last=0;
|
|---|
| 542 | Int_t first=0;
|
|---|
| 543 |
|
|---|
| 544 | MTime lasttime;
|
|---|
| 545 |
|
|---|
| 546 | TSQLRow *row=0;
|
|---|
| 547 |
|
|---|
| 548 | enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
|
|---|
| 549 | Char_t status = UNKNOWN;
|
|---|
| 550 |
|
|---|
| 551 | Int_t nblocks = 0;
|
|---|
| 552 |
|
|---|
| 553 | while ((row=res->Next()))
|
|---|
| 554 | {
|
|---|
| 555 | if (!(*row)[1])
|
|---|
| 556 | continue;
|
|---|
| 557 |
|
|---|
| 558 | if (start==0)
|
|---|
| 559 | {
|
|---|
| 560 | first = atoi((*row)[0]);
|
|---|
| 561 | if (debug)
|
|---|
| 562 | cout << "First Run: " << first << endl;
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | switch (atoi((*row)[1]))
|
|---|
| 566 | {
|
|---|
| 567 | case CAL: // ---------- CALIBRATION ----------
|
|---|
| 568 | if (status!=CAL)
|
|---|
| 569 | {
|
|---|
| 570 | start = stop = atoi((*row)[0]);
|
|---|
| 571 | if (!(*row)[2])
|
|---|
| 572 | cout << "No time available... skipped." << endl;
|
|---|
| 573 | else
|
|---|
| 574 | {
|
|---|
| 575 | MTime *tm = new MTime;
|
|---|
| 576 | tm->SetSqlDateTime((*row)[2]);
|
|---|
| 577 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
|---|
| 578 | }
|
|---|
| 579 | }
|
|---|
| 580 | status = CAL;
|
|---|
| 581 | break;
|
|---|
| 582 | default:
|
|---|
| 583 | if (status==CAL)
|
|---|
| 584 | {
|
|---|
| 585 | MTime *tm = new MTime(lasttime);
|
|---|
| 586 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
|---|
| 587 |
|
|---|
| 588 | stop = last;
|
|---|
| 589 | nblocks++;
|
|---|
| 590 | if (debug)
|
|---|
| 591 | cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
|
|---|
| 592 | }
|
|---|
| 593 | status = UNKNOWN;
|
|---|
| 594 | break;
|
|---|
| 595 | }
|
|---|
| 596 | last = atoi((*row)[0]);
|
|---|
| 597 | lasttime.SetSqlDateTime((*row)[3]);
|
|---|
| 598 | }
|
|---|
| 599 | if (status==CAL)
|
|---|
| 600 | {
|
|---|
| 601 | stop = last;
|
|---|
| 602 | nblocks++;
|
|---|
| 603 | if (debug)
|
|---|
| 604 | cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | if (debug)
|
|---|
| 608 | cout << "Last Run: " << last << endl;
|
|---|
| 609 | delete res;
|
|---|
| 610 |
|
|---|
| 611 | if (debug)
|
|---|
| 612 | cout << "Found " << nblocks << " calibration blocks" << endl;
|
|---|
| 613 |
|
|---|
| 614 | res = serv.Query(query);
|
|---|
| 615 | if (!res)
|
|---|
| 616 | return kFALSE;
|
|---|
| 617 |
|
|---|
| 618 | Int_t n = -1;
|
|---|
| 619 |
|
|---|
| 620 | Bool_t rc = kTRUE;
|
|---|
| 621 |
|
|---|
| 622 | start = first;
|
|---|
| 623 | while ((row=res->Next()))
|
|---|
| 624 | {
|
|---|
| 625 | if (!(*row)[1])
|
|---|
| 626 | continue;
|
|---|
| 627 |
|
|---|
| 628 | MTime tstart, tstop;
|
|---|
| 629 | tstart.SetSqlDateTime((*row)[2]);
|
|---|
| 630 | tstop.SetSqlDateTime((*row)[3]);
|
|---|
| 631 |
|
|---|
| 632 | MTime min;
|
|---|
| 633 | Int_t nmin = -1;
|
|---|
| 634 | Double_t dmin = 1e35;
|
|---|
| 635 |
|
|---|
| 636 | Long_t key, val;
|
|---|
| 637 | TExMapIter nmap(&map);
|
|---|
| 638 | while (nmap.Next(key, val))
|
|---|
| 639 | {
|
|---|
| 640 | MTime *t = (MTime*)key;
|
|---|
| 641 |
|
|---|
| 642 | if (nmin==-1)
|
|---|
| 643 | {
|
|---|
| 644 | nmin = val;
|
|---|
| 645 | min = *(MTime*)key;
|
|---|
| 646 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
|
|---|
| 650 | {
|
|---|
| 651 | nmin = val;
|
|---|
| 652 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
|---|
| 653 | min = *t;
|
|---|
| 654 | }
|
|---|
| 655 | if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
|
|---|
| 656 | {
|
|---|
| 657 | nmin = val;
|
|---|
| 658 | dmin = fabs((Double_t)*t-(Double_t)tstop);
|
|---|
| 659 | min = *t;
|
|---|
| 660 | }
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | if (n!=nmin)
|
|---|
| 664 | {
|
|---|
| 665 | if (n!=-1)
|
|---|
| 666 | {
|
|---|
| 667 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
|---|
| 668 | {
|
|---|
| 669 | rc = kFALSE;
|
|---|
| 670 | //continue;
|
|---|
| 671 | }
|
|---|
| 672 | }
|
|---|
| 673 | n = nmin;
|
|---|
| 674 | start = atoi((*row)[0]);
|
|---|
| 675 | }
|
|---|
| 676 | last = atoi((*row)[0]);
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | delete res;
|
|---|
| 680 |
|
|---|
| 681 | if (n!=-1 && start!=last)
|
|---|
| 682 | {
|
|---|
| 683 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
|---|
| 684 | rc = kFALSE;
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | if (debug)
|
|---|
| 688 | cout << endl;
|
|---|
| 689 |
|
|---|
| 690 | return rc;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
|
|---|
| 695 | int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
|---|
| 696 | {
|
|---|
| 697 | TEnv env("sql.rc");
|
|---|
| 698 |
|
|---|
| 699 | MSQLServer serv(env);
|
|---|
| 700 | if (!serv.IsConnected())
|
|---|
| 701 | {
|
|---|
| 702 | cout << "ERROR - Connection to database failed." << endl;
|
|---|
| 703 | return 0;
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | cout << "buildsequences" << endl;
|
|---|
| 707 | cout << "--------------" << endl;
|
|---|
| 708 | cout << endl;
|
|---|
| 709 | cout << "Connected to " << serv.GetName() << endl;
|
|---|
| 710 | cout << "Night of sunrise at: " << day << endl;
|
|---|
| 711 | cout << endl;
|
|---|
| 712 |
|
|---|
| 713 | day += " 13:00:00";
|
|---|
| 714 | const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
|---|
| 715 | day.Data(), day.Data()));
|
|---|
| 716 |
|
|---|
| 717 | TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
|
|---|
| 718 |
|
|---|
| 719 | TSQLResult *res = serv.Query(query);
|
|---|
| 720 | if (!res)
|
|---|
| 721 | return 0;
|
|---|
| 722 |
|
|---|
| 723 | TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
|
|---|
| 724 | TString stop = "NULL";
|
|---|
| 725 | TString runstart = "NULL";
|
|---|
| 726 | TString runstop = "NULL";
|
|---|
| 727 | Int_t count = 0;
|
|---|
| 728 | TExMap blocks;
|
|---|
| 729 | Int_t runbegin;
|
|---|
| 730 | Int_t runend;
|
|---|
| 731 |
|
|---|
| 732 | TSQLRow *row=0;
|
|---|
| 733 | while ((row=res->Next()))
|
|---|
| 734 | {
|
|---|
| 735 | if (count==0)
|
|---|
| 736 | {
|
|---|
| 737 | for (Int_t i=1 ; i<7 ; i++)
|
|---|
| 738 | keys[i-1]=(*row)[i];
|
|---|
| 739 | runstart=(*row)[0];
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | for (Int_t i=1 ; i<7 ; i++)
|
|---|
| 743 | {
|
|---|
| 744 | runbegin=atoi(runstart.Data());
|
|---|
| 745 | runend=atoi(runstop.Data());
|
|---|
| 746 | if (i==2 && runbegin>20100 && runend<45100)
|
|---|
| 747 | continue;
|
|---|
| 748 |
|
|---|
| 749 | TString value=(*row)[i];
|
|---|
| 750 | TString key=keys[i-1];
|
|---|
| 751 | if (!value.CompareTo(key))
|
|---|
| 752 | continue;
|
|---|
| 753 |
|
|---|
| 754 | keys[i-1]=value;
|
|---|
| 755 | //hier einfuellen
|
|---|
| 756 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
|---|
| 757 | runstart=(*row)[0];
|
|---|
| 758 | for (Int_t i=1 ; i<7 ; i++)
|
|---|
| 759 | keys[i-1]=(*row)[i];
|
|---|
| 760 | break;
|
|---|
| 761 | }
|
|---|
| 762 | runstop=(*row)[0];
|
|---|
| 763 | count++;
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | //und hier einfuellen (letzter wert)
|
|---|
| 767 | runbegin=atoi(runstart.Data());
|
|---|
| 768 | runend=atoi(runstop.Data());
|
|---|
| 769 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 | Bool_t rc = kTRUE;
|
|---|
| 773 |
|
|---|
| 774 | Long_t key, val;
|
|---|
| 775 | TExMapIter nblocks(&blocks);
|
|---|
| 776 | while (nblocks.Next(key, val))
|
|---|
| 777 | {
|
|---|
| 778 | Int_t runstart2 = (Int_t)key;
|
|---|
| 779 | Int_t runstop2 = (Int_t)val;
|
|---|
| 780 | cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
|
|---|
| 781 |
|
|---|
| 782 | if (!Process(serv, datapath, sequpath, runstart2, runstop2, dummy))
|
|---|
| 783 | rc = kFALSE;
|
|---|
| 784 |
|
|---|
| 785 | }
|
|---|
| 786 | return rc ? 1 : 0;
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 | int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
|---|
| 791 | {
|
|---|
| 792 | TEnv env("sql.rc");
|
|---|
| 793 |
|
|---|
| 794 | MSQLServer serv(env);
|
|---|
| 795 | if (!serv.IsConnected())
|
|---|
| 796 | {
|
|---|
| 797 | cout << "ERROR - Connection to database failed." << endl;
|
|---|
| 798 | return 0;
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | TString query="SELECT fDate FROM SequenceBuildStatus";
|
|---|
| 802 |
|
|---|
| 803 | TSQLResult *res = serv.Query(query);
|
|---|
| 804 | if (!res)
|
|---|
| 805 | return 0;
|
|---|
| 806 |
|
|---|
| 807 | TSQLRow *row=0;
|
|---|
| 808 | while ((row=res->Next()))
|
|---|
| 809 | {
|
|---|
| 810 | cout << "date: " << (*row)[0] << endl;
|
|---|
| 811 | buildsequenceentries((*row)[0], dummy);
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | return 1;
|
|---|
| 815 | }
|
|---|