| 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 | // doexclusions.C
|
|---|
| 29 | // ==============
|
|---|
| 30 | //
|
|---|
| 31 | /////////////////////////////////////////////////////////////////////////////
|
|---|
| 32 | #include <iostream>
|
|---|
| 33 | #include <iomanip>
|
|---|
| 34 | #include <fstream>
|
|---|
| 35 |
|
|---|
| 36 | #include <MSQLServer.h>
|
|---|
| 37 | #include <TSQLRow.h>
|
|---|
| 38 | #include <TSQLResult.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <TEnv.h>
|
|---|
| 41 | #include <TSystem.h>
|
|---|
| 42 |
|
|---|
| 43 | using namespace std;
|
|---|
| 44 |
|
|---|
| 45 | int GetRunNumber(MSQLServer &serv, TString date, TString value)
|
|---|
| 46 | {
|
|---|
| 47 | TString query(Form("SELECT %s(fRunNumber) FROM MyMagic.RunData ", value.Data()));
|
|---|
| 48 |
|
|---|
| 49 | if (date!="NULL")
|
|---|
| 50 | {
|
|---|
| 51 | TString day=date+" 13:00:00";
|
|---|
| 52 | query+=Form(" WHERE (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
|---|
| 53 | day.Data(), day.Data());
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | cout << "query: " << query << endl;
|
|---|
| 57 |
|
|---|
| 58 | TSQLResult *res = serv.Query(query);
|
|---|
| 59 | if (!res)
|
|---|
| 60 | {
|
|---|
| 61 | cout << "Error - could not get run#" << endl;
|
|---|
| 62 | return -1;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | TSQLRow *row =res->Next();
|
|---|
| 66 | cout << (void*)row << endl;
|
|---|
| 67 | if (TString((*row)[0]).IsNull())
|
|---|
| 68 | {
|
|---|
| 69 | cout << "No run available for this date" << endl;
|
|---|
| 70 | delete res;
|
|---|
| 71 | return 0;
|
|---|
| 72 | }
|
|---|
| 73 | delete res;
|
|---|
| 74 | return atoi((*row)[0]);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | TString GetJoin(TString table)
|
|---|
| 78 | {
|
|---|
| 79 | TString query(Form("left join MyMagic.%s ON RunData.f%sKEY=%s.f%sKEY ",
|
|---|
| 80 | table.Data(), table.Data(), table.Data(), table.Data()));
|
|---|
| 81 | return query;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | int doexclusions(Int_t startrun, Int_t stoprun, TString date="NULL")
|
|---|
| 85 | {
|
|---|
| 86 | TEnv env("sql.rc");
|
|---|
| 87 | TEnv rc("automatic-exclusions.rc");
|
|---|
| 88 |
|
|---|
| 89 | MSQLServer serv(env);
|
|---|
| 90 | if (!serv.IsConnected())
|
|---|
| 91 | {
|
|---|
| 92 | cout << "ERROR - Connection to database failed." << endl;
|
|---|
| 93 | return 0;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | cout << "doexclusions" << endl;
|
|---|
| 97 | cout << "------------" << endl;
|
|---|
| 98 | cout << endl;
|
|---|
| 99 | cout << "Connected to " << serv.GetName() << endl;
|
|---|
| 100 |
|
|---|
| 101 | if (startrun==0 && stoprun==0)
|
|---|
| 102 | {
|
|---|
| 103 | startrun=GetRunNumber(serv, date, "min");
|
|---|
| 104 | stoprun=GetRunNumber(serv, date, "max");
|
|---|
| 105 | }
|
|---|
| 106 | if (startrun<0 || stoprun<0)
|
|---|
| 107 | {
|
|---|
| 108 | cout << "wrong format of date" << endl;
|
|---|
| 109 | return 0;
|
|---|
| 110 | }
|
|---|
| 111 | if (startrun==0 || stoprun==0)
|
|---|
| 112 | return 1;
|
|---|
| 113 |
|
|---|
| 114 | TString runcond(Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun));
|
|---|
| 115 |
|
|---|
| 116 | TString query="SELECT fExcludedFDAKEY from MyMagic.ExcludedFDA where fExcludedFDAAutomatic='yes'";
|
|---|
| 117 | TSQLResult *res = serv.Query(query);
|
|---|
| 118 | if (!res)
|
|---|
| 119 | {
|
|---|
| 120 | cout << "Error - could not do any automatic excludes" << endl;
|
|---|
| 121 | return 0;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | TSQLRow *row=0;
|
|---|
| 125 | while ((row = res->Next()))
|
|---|
| 126 | {
|
|---|
| 127 | TString key=(*row)[0];
|
|---|
| 128 | TString column=rc.GetValue("key"+key+".Column", "");
|
|---|
| 129 | TString join1=rc.GetValue("key"+key+".Join1", "");
|
|---|
| 130 | TString join2=rc.GetValue("key"+key+".Join2", "");
|
|---|
| 131 | TString border=rc.GetValue("key"+key+".SpecialRunCond", "");
|
|---|
| 132 |
|
|---|
| 133 | TString query(Form("SELECT fExcludedFDAImportance from MyMagic.ExcludedFDA where fExcludedFDAKEY=%s ", key.Data()));
|
|---|
| 134 | TSQLResult *res = serv.Query(query);
|
|---|
| 135 | if (!res)
|
|---|
| 136 | {
|
|---|
| 137 | cout << "Error" << endl;
|
|---|
| 138 | return 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | TSQLRow *row2=res->Next();
|
|---|
| 142 | Int_t newimp=atoi((*row2)[0]);
|
|---|
| 143 | delete res;
|
|---|
| 144 |
|
|---|
| 145 | query="SELECT fRunNumber, fExcludedFDAImportance ";
|
|---|
| 146 | if (!column.IsNull())
|
|---|
| 147 | query+=Form(", %s", column.Data());
|
|---|
| 148 | if (!join1.IsNull())
|
|---|
| 149 | query+=Form(", f%sName", join1.Data());
|
|---|
| 150 | if (!join2.IsNull())
|
|---|
| 151 | query+=Form(", f%sName", join2.Data());
|
|---|
| 152 | query +=" FROM MyMagic.RunData ";
|
|---|
| 153 | query +=GetJoin("ExcludedFDA");
|
|---|
| 154 | if (!join1.IsNull())
|
|---|
| 155 | query+=GetJoin(join1.Data());
|
|---|
| 156 | if (!join2.IsNull())
|
|---|
| 157 | query+=GetJoin(join2.Data());
|
|---|
| 158 | query +=Form("WHERE %s ", rc.GetValue("key"+key+".Cond", ""));
|
|---|
| 159 | if (!border.IsNull())
|
|---|
| 160 | query+=Form(" AND fRunNumber BETWEEN IF(%s>%d, %d, %s) AND IF (%s<%d, %s, %d) ",
|
|---|
| 161 | border.Data(), startrun, startrun, border.Data(),
|
|---|
| 162 | border.Data(), stoprun, border.Data(), stoprun);
|
|---|
| 163 | else
|
|---|
| 164 | query +=runcond;
|
|---|
| 165 |
|
|---|
| 166 | cout << query << endl;
|
|---|
| 167 |
|
|---|
| 168 | res = serv.Query(query);
|
|---|
| 169 | if (!res)
|
|---|
| 170 | {
|
|---|
| 171 | cout << "Error - no runs to exclude" << endl;
|
|---|
| 172 | return 0;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | while ((row2 = res->Next()))
|
|---|
| 176 | {
|
|---|
| 177 | if (TString((*row2)[1]).IsNull() || atoi((*row2)[1])>newimp)
|
|---|
| 178 | {
|
|---|
| 179 | TString query(Form("UPDATE MyMagic.RunData SET fExcludedFDAKEY=%s WHERE fRunNumber=%s",
|
|---|
| 180 | key.Data(), (*row2)[0]));
|
|---|
| 181 | cout << "QU: " << query << endl;
|
|---|
| 182 | TSQLResult *res = serv.Query(query);
|
|---|
| 183 | if (!res)
|
|---|
| 184 | {
|
|---|
| 185 | cout << "Error - could not insert exclusion" << endl;
|
|---|
| 186 | return 0;
|
|---|
| 187 | }
|
|---|
| 188 | delete res;
|
|---|
| 189 | continue;
|
|---|
| 190 | }
|
|---|
| 191 | cout << "run#: " << (*row2)[0] << " reason for exclusion is still the same" << endl;
|
|---|
| 192 | }
|
|---|
| 193 | delete res;
|
|---|
| 194 | }
|
|---|
| 195 | delete res;
|
|---|
| 196 | return 1;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | int doexclusions(TString date="NULL")
|
|---|
| 200 | {
|
|---|
| 201 | return doexclusions(0, 0, date);
|
|---|
| 202 | }
|
|---|