| 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-2006 | 
|---|
| 22 | ! | 
|---|
| 23 | ! | 
|---|
| 24 | \* ======================================================================== */ | 
|---|
| 25 |  | 
|---|
| 26 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 27 | // | 
|---|
| 28 | // doexclusions.C | 
|---|
| 29 | // ============== | 
|---|
| 30 | // | 
|---|
| 31 | // this macro sets the ExcludedFDA flag for runs, that can be excluded | 
|---|
| 32 | // automatically | 
|---|
| 33 | // the information which runs have to be excluded is retrieved from the | 
|---|
| 34 | // resource file automatic-exclusions.rc | 
|---|
| 35 | // | 
|---|
| 36 | // the macro can be executed either for a night or for a range of runnumbers | 
|---|
| 37 | // or for all nights | 
|---|
| 38 | // .x doexclusions.C+("night") | 
|---|
| 39 | // .x doexclusions.C+(startrun,stoprun) | 
|---|
| 40 | // .x doexclusions.C+ | 
|---|
| 41 | // | 
|---|
| 42 | // resource file: automatic-exclustions.rc | 
|---|
| 43 | // for each exclusion-reason there is one block of 6 lines in this file: | 
|---|
| 44 | // example for one exclusion-reason: | 
|---|
| 45 | //  #NumEventsSmaller10:                   (name of exclusion) | 
|---|
| 46 | //  key15.Column: fNumEvents               (name of the affected column) | 
|---|
| 47 | //  #key15.Join1:                          (name of column that has to | 
|---|
| 48 | //  #key15.Join2:                                             be joined) | 
|---|
| 49 | //  key15.Cond: fNumEvents between 2 and 9 (condition that fulfils exclusion) | 
|---|
| 50 | //  #key15.SpecialRunCond:                 (special condition, if exclusion | 
|---|
| 51 | //                                          is needed only for certain runs) | 
|---|
| 52 | // if a value is not needed for an exclusion (like the joins and the special | 
|---|
| 53 | // condition in this example), the line is commented out | 
|---|
| 54 | // | 
|---|
| 55 | // Returns 2 in case of failure, 1 in case of success and 0 if the connection | 
|---|
| 56 | // to the database is not working. | 
|---|
| 57 | // | 
|---|
| 58 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 59 | #include <iostream> | 
|---|
| 60 | #include <iomanip> | 
|---|
| 61 | #include <fstream> | 
|---|
| 62 |  | 
|---|
| 63 | #include <MSQLServer.h> | 
|---|
| 64 | #include <TSQLRow.h> | 
|---|
| 65 | #include <TSQLResult.h> | 
|---|
| 66 |  | 
|---|
| 67 | #include <TEnv.h> | 
|---|
| 68 | #include <TSystem.h> | 
|---|
| 69 |  | 
|---|
| 70 | using namespace std; | 
|---|
| 71 |  | 
|---|
| 72 | //get minimum or maximum runnumber of the runs of a night | 
|---|
| 73 | int GetRunNumber(MSQLServer &serv, TString date, TString value) | 
|---|
| 74 | { | 
|---|
| 75 | TString query(Form("SELECT %s(fRunNumber) FROM RunData ", value.Data())); | 
|---|
| 76 |  | 
|---|
| 77 | if (date!="NULL") | 
|---|
| 78 | { | 
|---|
| 79 | TString day=date+" 13:00:00"; | 
|---|
| 80 | query+=Form(" WHERE (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")", | 
|---|
| 81 | day.Data(), day.Data()); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | cout << "query: " << query << endl; | 
|---|
| 85 |  | 
|---|
| 86 | TSQLResult *res = serv.Query(query); | 
|---|
| 87 | if (!res) | 
|---|
| 88 | { | 
|---|
| 89 | cout << "Error - could not get run#" << endl; | 
|---|
| 90 | return -1; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | TSQLRow *row =res->Next(); | 
|---|
| 94 | cout << (void*)row << endl; | 
|---|
| 95 | if (TString((*row)[0]).IsNull()) | 
|---|
| 96 | { | 
|---|
| 97 | cout << "No run available for this date" << endl; | 
|---|
| 98 | delete res; | 
|---|
| 99 | return 0; | 
|---|
| 100 | } | 
|---|
| 101 | delete res; | 
|---|
| 102 | return atoi((*row)[0]); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | //get part of a query (left join of tables) | 
|---|
| 106 | TString GetJoin(TString table) | 
|---|
| 107 | { | 
|---|
| 108 | TString query(Form("left join %s ON RunData.f%sKEY=%s.f%sKEY ", | 
|---|
| 109 | table.Data(), table.Data(), table.Data(), table.Data())); | 
|---|
| 110 | return query; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | int doexclusions(Int_t startrun, Int_t stoprun, TString date="NULL") | 
|---|
| 114 | { | 
|---|
| 115 | TEnv env("sql.rc"); | 
|---|
| 116 | TEnv rc("automatic-exclusions.rc"); | 
|---|
| 117 |  | 
|---|
| 118 | MSQLServer serv(env); | 
|---|
| 119 | if (!serv.IsConnected()) | 
|---|
| 120 | { | 
|---|
| 121 | cout << "ERROR - Connection to database failed." << endl; | 
|---|
| 122 | return 0; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | cout << "doexclusions" << endl; | 
|---|
| 126 | cout << "------------" << endl; | 
|---|
| 127 | cout << endl; | 
|---|
| 128 | cout << "Connected to " << serv.GetName() << endl; | 
|---|
| 129 |  | 
|---|
| 130 | //if neither start- nor stoprun is given, the minimum and maximum runnumber | 
|---|
| 131 | // is queried from the database to do the exclusions for all runs | 
|---|
| 132 | // if a night is given for all runs of this night | 
|---|
| 133 | if (startrun==0 && stoprun==0) | 
|---|
| 134 | { | 
|---|
| 135 | startrun=GetRunNumber(serv, date, "min"); | 
|---|
| 136 | stoprun=GetRunNumber(serv, date, "max"); | 
|---|
| 137 | } | 
|---|
| 138 | //check format of start- and stoprun | 
|---|
| 139 | if (startrun<0 || stoprun<0) | 
|---|
| 140 | { | 
|---|
| 141 | cout << "wrong format of runno" << endl; | 
|---|
| 142 | return 2; | 
|---|
| 143 | } | 
|---|
| 144 | //if no run for date is available, GetRunNumber() returns 0 | 
|---|
| 145 | if (startrun==0 || stoprun==0) | 
|---|
| 146 | return 1; | 
|---|
| 147 |  | 
|---|
| 148 | //get the condition for the runnumber range | 
|---|
| 149 | TString runcond(Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun)); | 
|---|
| 150 |  | 
|---|
| 151 | //get exclusions-reasons (stored in the table ExcludedFDA) from the database | 
|---|
| 152 | //the exclusions which can be done automatically are marked with the flag fExcludedFDAAutomatic='yes' | 
|---|
| 153 | //and with an importance (one run may be excluded for several reasons, | 
|---|
| 154 | //the reason is chosen according to the importance) | 
|---|
| 155 | TString query="SELECT fExcludedFDAKEY from ExcludedFDA where fExcludedFDAAutomatic='yes'"; | 
|---|
| 156 | TSQLResult *res = serv.Query(query); | 
|---|
| 157 | if (!res) | 
|---|
| 158 | { | 
|---|
| 159 | cout << "Error - could not do any automatic excludes." << endl; | 
|---|
| 160 | return 2; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | //update the exclusion-reasons for all runs | 
|---|
| 164 | TSQLRow *row=0; | 
|---|
| 165 | while ((row = res->Next())) | 
|---|
| 166 | { | 
|---|
| 167 | //read in values from the resource file automatic-exclusions (explanation see above) | 
|---|
| 168 | TString key=(*row)[0]; | 
|---|
| 169 | TString column=rc.GetValue("key"+key+".Column", ""); | 
|---|
| 170 | TString join1=rc.GetValue("key"+key+".Join1", ""); | 
|---|
| 171 | TString join2=rc.GetValue("key"+key+".Join2", ""); | 
|---|
| 172 | TString border=rc.GetValue("key"+key+".SpecialRunCond", ""); | 
|---|
| 173 |  | 
|---|
| 174 | //get importance of exclusion-reason from database | 
|---|
| 175 | TString query(Form("SELECT fExcludedFDAImportance from ExcludedFDA where fExcludedFDAKEY=%s ", key.Data())); | 
|---|
| 176 | TSQLResult *res2 = serv.Query(query); | 
|---|
| 177 | if (!res2) | 
|---|
| 178 | { | 
|---|
| 179 | cout << "Error - could not get importance." << endl; | 
|---|
| 180 | return 2; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | TSQLRow *row2=res2->Next(); | 
|---|
| 184 | Int_t newimp=atoi((*row2)[0]); | 
|---|
| 185 | delete res2; | 
|---|
| 186 |  | 
|---|
| 187 | //get current importance from database | 
|---|
| 188 | //for the runs which match the exclusion-reason | 
|---|
| 189 | query="SELECT fRunNumber, fExcludedFDAImportance "; | 
|---|
| 190 | if (!column.IsNull()) | 
|---|
| 191 | query+=Form(", %s", column.Data()); | 
|---|
| 192 | if (!join1.IsNull()) | 
|---|
| 193 | query+=Form(", f%sName", join1.Data()); | 
|---|
| 194 | if (!join2.IsNull()) | 
|---|
| 195 | query+=Form(", f%sName", join2.Data()); | 
|---|
| 196 | query +=" FROM RunData "; | 
|---|
| 197 | query +=GetJoin("ExcludedFDA"); | 
|---|
| 198 | if (!join1.IsNull()) | 
|---|
| 199 | query+=GetJoin(join1.Data()); | 
|---|
| 200 | if (!join2.IsNull()) | 
|---|
| 201 | query+=GetJoin(join2.Data()); | 
|---|
| 202 | query +=Form("WHERE (%s) ", rc.GetValue("key"+key+".Cond", "")); | 
|---|
| 203 | if (!border.IsNull()) | 
|---|
| 204 | query+=Form(" AND fRunNumber BETWEEN IF(%s>%d, %d, %s) AND IF (%s<%d, %s, %d) ", | 
|---|
| 205 | border.Data(), startrun, startrun, border.Data(), | 
|---|
| 206 | border.Data(), stoprun, border.Data(), stoprun); | 
|---|
| 207 | else | 
|---|
| 208 | query +=runcond; | 
|---|
| 209 |  | 
|---|
| 210 | cout << query << endl; | 
|---|
| 211 |  | 
|---|
| 212 | res2 = serv.Query(query); | 
|---|
| 213 | if (!res2) | 
|---|
| 214 | { | 
|---|
| 215 | cout << "Error - no runs to exclude" << endl; | 
|---|
| 216 | return 2; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | //compare new and old importance | 
|---|
| 220 | //change or keep the exclusion-reason accordingly | 
|---|
| 221 | while ((row2 = res2->Next())) | 
|---|
| 222 | { | 
|---|
| 223 | if (TString((*row2)[1]).IsNull() || atoi((*row2)[1])>newimp) | 
|---|
| 224 | { | 
|---|
| 225 | //change exclusion-reason | 
|---|
| 226 | TString query(Form("UPDATE RunData SET fExcludedFDAKEY=%s WHERE fRunNumber=%s", | 
|---|
| 227 | key.Data(), (*row2)[0])); | 
|---|
| 228 | cout << "QU: " << query << endl; | 
|---|
| 229 | TSQLResult *res3 = serv.Query(query); | 
|---|
| 230 | if (!res3) | 
|---|
| 231 | { | 
|---|
| 232 | cout << "Error - could not insert exclusion" << endl; | 
|---|
| 233 | return 2; | 
|---|
| 234 | } | 
|---|
| 235 | delete res3; | 
|---|
| 236 | continue; | 
|---|
| 237 | } | 
|---|
| 238 | //keep exclusion-reason | 
|---|
| 239 | cout << "run#: " << (*row2)[0] << " reason for exclusion is still the same" << endl; | 
|---|
| 240 | } | 
|---|
| 241 | delete res2; | 
|---|
| 242 | } | 
|---|
| 243 | delete res; | 
|---|
| 244 | return 1; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | //run doexclusions for one night | 
|---|
| 248 | int doexclusions(TString date="NULL") | 
|---|
| 249 | { | 
|---|
| 250 | return doexclusions(0, 0, date); | 
|---|
| 251 | } | 
|---|