| 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 |  | 
|---|
| 52 | #include <TEnv.h> | 
|---|
| 53 |  | 
|---|
| 54 | #include <MSQLMagic.h> | 
|---|
| 55 | #include <TSQLRow.h> | 
|---|
| 56 | #include <TSQLResult.h> | 
|---|
| 57 |  | 
|---|
| 58 | using namespace std; | 
|---|
| 59 |  | 
|---|
| 60 | int insertsequence(TString filename, Bool_t dummy=kTRUE) | 
|---|
| 61 | { | 
|---|
| 62 | TEnv env("sql.rc"); | 
|---|
| 63 | TEnv sequ(filename); | 
|---|
| 64 |  | 
|---|
| 65 | MSQLMagic serv(env); | 
|---|
| 66 | if (!serv.IsConnected()) | 
|---|
| 67 | { | 
|---|
| 68 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 69 | return 0; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | serv.SetIsDummy(dummy); | 
|---|
| 73 |  | 
|---|
| 74 | cout << "insertsequence" << endl; | 
|---|
| 75 | cout << "--------------" << endl; | 
|---|
| 76 | cout << endl; | 
|---|
| 77 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 78 | cout << endl; | 
|---|
| 79 |  | 
|---|
| 80 | //get sequence number from file | 
|---|
| 81 | TString sequnum; | 
|---|
| 82 | sequnum=sequ.GetValue("Sequence", ""); | 
|---|
| 83 | Int_t seq=atoi(sequnum.Data()); | 
|---|
| 84 |  | 
|---|
| 85 | //get runs from sequence file | 
|---|
| 86 | TString runs; | 
|---|
| 87 | runs = sequ.GetValue("Runs", ""); | 
|---|
| 88 | runs.ReplaceAll(" ", ","); | 
|---|
| 89 | if (runs.IsNull()) | 
|---|
| 90 | { | 
|---|
| 91 | cout << "ERROR - No runs in file " << filename << " found." << endl; | 
|---|
| 92 | return 0; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | //get source key | 
|---|
| 96 | TString sourcename = sequ.GetValue("Source", ""); | 
|---|
| 97 | Int_t sourcekey = serv.QueryKeyOfName("Source", sourcename.Data(), kFALSE); | 
|---|
| 98 | if (sourcekey<0) | 
|---|
| 99 | { | 
|---|
| 100 | cout << "Error - could not get sourcename from DB -> " << flush; | 
|---|
| 101 | cout << "maybe you have the wrong sourcename in your datasetfile" << endl; | 
|---|
| 102 | return 2; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | //get values for the sequence | 
|---|
| 106 | TString query="SELECT max(fRunNumber), min(fRunStart), "; | 
|---|
| 107 | query +="sum(time_to_sec(fRunStop)-time_to_sec(fRunStart)), "; | 
|---|
| 108 | query +="min(fZenithDistance), max(fZenithDistance), "; | 
|---|
| 109 | query +="min(fAzimuth), max(fAzimuth), sum(fNumEvents) from RunData "; | 
|---|
| 110 | query +=Form("WHERE fRunNumber IN (%s)",runs.Data()); | 
|---|
| 111 |  | 
|---|
| 112 | cout << "runs: " << runs << endl; | 
|---|
| 113 | cout << "q1: " << query << endl; | 
|---|
| 114 |  | 
|---|
| 115 | TSQLResult *res = serv.Query(query); | 
|---|
| 116 | if (!res) | 
|---|
| 117 | return 0; | 
|---|
| 118 |  | 
|---|
| 119 | TSQLRow *row=res->Next(); | 
|---|
| 120 | TString lastrun=(*row)[0]; | 
|---|
| 121 | TString starttime=(*row)[1]; | 
|---|
| 122 | TString uptime=(*row)[2]; | 
|---|
| 123 | TString zdmin=(*row)[3]; | 
|---|
| 124 | TString zdmax=(*row)[4]; | 
|---|
| 125 | TString azmin=(*row)[5]; | 
|---|
| 126 | TString azmax=(*row)[6]; | 
|---|
| 127 | TString numevts=(*row)[7]; | 
|---|
| 128 |  | 
|---|
| 129 | delete res; | 
|---|
| 130 |  | 
|---|
| 131 | query  ="SELECT fProjectKEY, fSourceKEY, fHvSettingsKEY, "; | 
|---|
| 132 | query +="fTriggerDelayTableKEY, fDiscriminatorThresholdTableKEY, "; | 
|---|
| 133 | query +="fTestFlagKEY, fLightConditionsKEY, fL1TriggerTableKEY, "; | 
|---|
| 134 | query +=Form("fL2TriggerTableKEY FROM RunData WHERE fRunNumber=%d", seq); | 
|---|
| 135 |  | 
|---|
| 136 | cout << "q2: " << query << endl; | 
|---|
| 137 |  | 
|---|
| 138 | res = serv.Query(query); | 
|---|
| 139 | if (!res) | 
|---|
| 140 | return 0; | 
|---|
| 141 |  | 
|---|
| 142 | row=res->Next(); | 
|---|
| 143 | TString project=(*row)[0]; | 
|---|
| 144 | TString source=(*row)[1]; | 
|---|
| 145 | TString hv=(*row)[2]; | 
|---|
| 146 | TString delay=(*row)[3]; | 
|---|
| 147 | TString dt=(*row)[4]; | 
|---|
| 148 | TString testflag=(*row)[5]; | 
|---|
| 149 | TString lightcond=(*row)[6]; | 
|---|
| 150 | TString l1tt=(*row)[7]; | 
|---|
| 151 | TString l2tt=(*row)[8]; | 
|---|
| 152 |  | 
|---|
| 153 | delete res; | 
|---|
| 154 |  | 
|---|
| 155 | cout << "seq: "       << seq        << endl; | 
|---|
| 156 | cout << "  lastrun   " << lastrun   << endl; | 
|---|
| 157 | cout << "  startime  " << starttime << endl; | 
|---|
| 158 | cout << "  uptime    " << uptime    << endl; | 
|---|
| 159 | cout << "  zdmin     " << zdmin     << endl; | 
|---|
| 160 | cout << "  zdmax     " << zdmax     << endl; | 
|---|
| 161 | cout << "  azmin     " << azmin     << endl; | 
|---|
| 162 | cout << "  azmax     " << azmax     << endl; | 
|---|
| 163 | cout << "  numevts   " << numevts   << endl; | 
|---|
| 164 | cout << " keys:"                    <<  endl; | 
|---|
| 165 | cout << "  project   " << project   << endl; | 
|---|
| 166 | cout << "  source1   " << source    << " (from db -> run "  << seq << ") " << endl; | 
|---|
| 167 | cout << "  source2   " << sourcekey << " (from sequ file) " << endl; | 
|---|
| 168 | if (!(atoi(source.Data())==sourcekey)) | 
|---|
| 169 | { | 
|---|
| 170 | cout << "new source name: " << sourcename << " -> inserting..." << endl; | 
|---|
| 171 | sourcekey = serv.QueryKeyOfName("Source", sourcename.Data(), kFALSE); | 
|---|
| 172 | if (sourcekey<0) | 
|---|
| 173 | { | 
|---|
| 174 | cout << "Error - could not get sourcename from DB -> " << flush; | 
|---|
| 175 | cout << "maybe you have the wrong sourcename in your datasetfile" << endl; | 
|---|
| 176 | return 2; | 
|---|
| 177 | } | 
|---|
| 178 | source=Form("%d",sourcekey); | 
|---|
| 179 | } | 
|---|
| 180 | cout << "  source    " << source    << endl; | 
|---|
| 181 | cout << "  hv        " << hv        << endl; | 
|---|
| 182 | cout << "  delay     " << delay     << endl; | 
|---|
| 183 | cout << "  dt        " << dt        << endl; | 
|---|
| 184 | cout << "  testflag  " << testflag  << endl; | 
|---|
| 185 | cout << "  lightcond " << lightcond << endl; | 
|---|
| 186 | cout << "  l1tt      " << l1tt      << endl; | 
|---|
| 187 | cout << "  l2tt      " << l2tt      << endl; | 
|---|
| 188 |  | 
|---|
| 189 | //build queries | 
|---|
| 190 | TString vals= | 
|---|
| 191 | Form("fManuallyChangedKEY=2, " | 
|---|
| 192 | "fSequenceFirst=%d, fSequenceLast=%s, " | 
|---|
| 193 | "fProjectKEY=%s, fSourceKEY=%s, fNumEvents=%s, " | 
|---|
| 194 | "fRunStart='%s', fHvSettingsKEY=%s, fRunTime=%s, " | 
|---|
| 195 | "fTriggerDelayTableKEY=%s, fDiscriminatorThresholdTableKEY=%s, " | 
|---|
| 196 | "fTestFlagKEY=%s, fLightConditionsKEY=%s, fAzimuthMin=%s, " | 
|---|
| 197 | "fAzimuthMax=%s, fZenithDistanceMin=%s, fZenithDistanceMax=%s, " | 
|---|
| 198 | "fL1TriggerTableKEY=%s, fL2TriggerTableKEY=%s ", | 
|---|
| 199 | seq, lastrun.Data(), project.Data(), source.Data(), | 
|---|
| 200 | numevts.Data(), starttime.Data(), hv.Data(), uptime.Data(), | 
|---|
| 201 | delay.Data(), dt.Data(), testflag.Data(), lightcond.Data(), | 
|---|
| 202 | azmin.Data(), azmax.Data(), zdmin.Data(), zdmax.Data(), | 
|---|
| 203 | l1tt.Data(), l2tt.Data()); | 
|---|
| 204 |  | 
|---|
| 205 | const Int_t rc1 = serv.Insert("Sequences", vals); | 
|---|
| 206 | if (rc1<0)       // dummy | 
|---|
| 207 | return 1; | 
|---|
| 208 | if (rc1==kFALSE) // insert failed | 
|---|
| 209 | return 0; | 
|---|
| 210 |  | 
|---|
| 211 | //the time of the column fSequenceFileWritten is set to 'not to be done' | 
|---|
| 212 | vals=Form("fSequenceFirst=%d, fSequenceFileWritten='1970-01-01 00:00:00'", seq); | 
|---|
| 213 |  | 
|---|
| 214 | const Int_t rc2 = serv.Insert("SequencesProcessStatus", vals); | 
|---|
| 215 |  | 
|---|
| 216 | if (rc2<0)       // dummy | 
|---|
| 217 | return 1; | 
|---|
| 218 | if (rc2==kFALSE) // insert failed | 
|---|
| 219 | return 0; | 
|---|
| 220 |  | 
|---|
| 221 | return 1; | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 |  | 
|---|