Changeset 9006 for trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C
- Timestamp:
- 07/17/08 16:08:04 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C
r7777 r9006 59 59 #include <iostream> 60 60 #include <iomanip> 61 #include <fstream> 62 63 #include <MSQLServer.h> 61 62 #include <TEnv.h> 63 #include <TSystem.h> 64 64 65 #include <TSQLRow.h> 65 66 #include <TSQLResult.h> 66 67 67 #include <TEnv.h> 68 #include <TSystem.h> 68 #include "MSQLMagic.h" 69 69 70 70 using namespace std; 71 71 72 72 //get minimum or maximum runnumber of the runs of a night 73 int GetRunNumber(MSQLServer &serv, TString date, TString value)73 int GetRunNumber(MSQLServer &serv, Int_t tel, TString date, TString cmd) 74 74 { 75 TString query(Form("SELECT %s(fRunNumber) FROM RunData ", value.Data())); 75 TString query; 76 query = Form("SELECT %s(fRunNumber) FROM RunData ", cmd.Data()); 77 query += Form("WHERE fTelescopeNumber=%d", tel); 76 78 77 79 if (date!="NULL") 78 80 { 79 81 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; 82 query += Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")", 83 day.Data(), day.Data()); 84 } 85 85 86 86 TSQLResult *res = serv.Query(query); 87 87 if (!res) 88 88 { 89 cout << "E rror - could not get run#" << endl;89 cout << "ERROR - Could not get " << cmd << " fRunNumber." << endl; 90 90 return -1; 91 91 } 92 92 93 93 TSQLRow *row =res->Next(); 94 cout << (void*)row << endl;95 94 if (TString((*row)[0]).IsNull()) 96 95 { 97 cout << "No run available for this date"<< endl;96 cout << "No run available for " << date << endl; 98 97 delete res; 99 98 return 0; 100 99 } 101 100 delete res; 101 102 102 return atoi((*row)[0]); 103 103 } 104 104 105 //get part of a query (left join of tables) 106 TString GetJoin(TString table) 105 int doexclusions(Int_t startrun, Int_t stoprun, Int_t tel=1, TString date="NULL", Bool_t dummy=kTRUE) 107 106 { 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); 107 MSQLMagic serv("sql.rc"); 119 108 if (!serv.IsConnected()) 120 109 { … … 123 112 } 124 113 114 // Open rc files with exclusions 115 TEnv rc("resources/exclusions.rc"); 116 117 // Some information for the user 125 118 cout << "doexclusions" << endl; 126 119 cout << "------------" << endl; 127 120 cout << endl; 128 121 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 122 cout << "Date: " << date << endl; 123 124 serv.SetIsDummy(dummy); 125 126 // if neither start- nor stoprun is given, the minimum and maximum 127 // runnumber is queried from the database to do the exclusions for 128 // all runs if a night is given for all runs of this night 133 129 if (startrun==0 && stoprun==0) 134 130 { 135 startrun=GetRunNumber(serv, date, "min"); 136 stoprun=GetRunNumber(serv, date, "max"); 137 } 131 startrun = GetRunNumber(serv, tel, date, "MIN"); 132 stoprun = GetRunNumber(serv, tel, date, "MAX"); 133 } 134 135 cout << "Start Run: " << startrun << endl; 136 cout << "Stop Run: " << stoprun << endl; 137 138 138 //check format of start- and stoprun 139 139 if (startrun<0 || stoprun<0) 140 140 { 141 cout << " wrong format of runno" << endl;141 cout << "ERROR - Startrun<0 or stoprun<0." << endl; 142 142 return 2; 143 143 } 144 144 145 //if no run for date is available, GetRunNumber() returns 0 145 146 if (startrun==0 || stoprun==0) 146 147 return 1; 147 148 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); 149 // Get exclusions-reasons (stored in the table ExcludedFDA) from the DB 150 TSQLResult *res = serv.Query("SELECT fExcludedFDAKEY, fExcludedFDAName " 151 "FROM ExcludedFDA " 152 "ORDER BY fExcludedFDAImportance ASC"); 157 153 if (!res) 158 {159 cout << "Error - could not do any automatic excludes." << endl;160 154 return 2; 161 }162 155 163 156 //update the exclusion-reasons for all runs … … 165 158 while ((row = res->Next())) 166 159 { 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); 160 // check the key (NULL means "No exclusion") 161 const TString key = (*row)[0]; 162 if (key.IsNull()) 163 continue; 164 165 // Get the corresponding condition from the file 166 const TString cond = rc.GetValue("key"+key, ""); 167 if (cond.IsNull()) 168 continue; 169 170 // Get all files to be excluded 171 TString query2 = "SELECT fRunNumber, fFileNumber FROM RunData "; 172 query2 += serv.GetJoins("RunData", query2+cond); 173 174 query2 += Form("WHERE (%s) ", cond.Data()); 175 query2 += Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun); 176 query2 += Form("AND fTelescopeNumber=%d", tel); 177 178 TSQLResult *res2 = serv.Query(query2); 177 179 if (!res2) 178 {179 cout << "Error - could not get importance." << endl;180 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 181 182 // Update exlcusion for file 183 TSQLRow *row2=0; 221 184 while ((row2 = res2->Next())) 222 185 { 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; 186 TString vars(Form("fExcludedFDAKEY=%s", key.Data())); 187 TString where(Form("fRunNumber=%s AND fFileNumber=%s", (*row2)[0], (*row2)[1])); 188 189 if (serv.Update("RunData", vars, where)==kFALSE) 190 return 2; 191 192 cout << "File M" << tel << ":" << (*row2)[0] << "/" << setw(3) << setfill('0') << (*row2)[1] << " excluded due to: " << (*row)[1] << "." << endl; 240 193 } 194 241 195 delete res2; 242 196 } 243 197 delete res; 198 244 199 return 1; 245 200 } 246 201 247 202 //run doexclusions for one night 248 int doexclusions(TString date="NULL" )203 int doexclusions(TString date="NULL", Int_t tel=1, Bool_t dummy=kTRUE) 249 204 { 250 return doexclusions(0, 0, date);205 return doexclusions(0, 0, tel, date, dummy); 251 206 }
Note:
See TracChangeset
for help on using the changeset viewer.