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 |
|
---|
62 | #include <TEnv.h>
|
---|
63 | #include <TSystem.h>
|
---|
64 |
|
---|
65 | #include <TSQLRow.h>
|
---|
66 | #include <TSQLResult.h>
|
---|
67 |
|
---|
68 | #include "MSQLMagic.h"
|
---|
69 |
|
---|
70 | using namespace std;
|
---|
71 |
|
---|
72 | //get minimum or maximum runnumber of the runs of a night
|
---|
73 | int GetRunNumber(MSQLServer &serv, Int_t tel, TString date, TString cmd)
|
---|
74 | {
|
---|
75 | TString query;
|
---|
76 | query = Form("SELECT %s(fRunNumber) FROM RunData ", cmd.Data());
|
---|
77 | query += Form("WHERE fTelescopeNumber=%d", tel);
|
---|
78 |
|
---|
79 | if (date!="NULL")
|
---|
80 | {
|
---|
81 | TString day=date+" 13:00:00";
|
---|
82 | query += Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
---|
83 | day.Data(), day.Data());
|
---|
84 | }
|
---|
85 |
|
---|
86 | TSQLResult *res = serv.Query(query);
|
---|
87 | if (!res)
|
---|
88 | {
|
---|
89 | cout << "ERROR - Could not get " << cmd << " fRunNumber." << endl;
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | TSQLRow *row =res->Next();
|
---|
94 | if (TString((*row)[0]).IsNull())
|
---|
95 | {
|
---|
96 | cout << "No run available for " << date << endl;
|
---|
97 | delete res;
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 | delete res;
|
---|
101 |
|
---|
102 | return atoi((*row)[0]);
|
---|
103 | }
|
---|
104 |
|
---|
105 | int doexclusions(Int_t startrun, Int_t stoprun, Int_t tel=1, TString date="NULL", Bool_t dummy=kTRUE)
|
---|
106 | {
|
---|
107 | MSQLMagic serv("sql.rc");
|
---|
108 | if (!serv.IsConnected())
|
---|
109 | {
|
---|
110 | cout << "ERROR - Connection to database failed." << endl;
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // Open rc files with exclusions
|
---|
115 | TEnv rc("resources/exclusions.rc");
|
---|
116 |
|
---|
117 | // Some information for the user
|
---|
118 | cout << "doexclusions" << endl;
|
---|
119 | cout << "------------" << endl;
|
---|
120 | cout << endl;
|
---|
121 | cout << "Connected to " << serv.GetName() << endl;
|
---|
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
|
---|
129 | if (startrun==0 && stoprun==0)
|
---|
130 | {
|
---|
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 | //check format of start- and stoprun
|
---|
139 | if (startrun<0 || stoprun<0)
|
---|
140 | {
|
---|
141 | cout << "ERROR - Startrun<0 or stoprun<0." << endl;
|
---|
142 | return 2;
|
---|
143 | }
|
---|
144 |
|
---|
145 | //if no run for date is available, GetRunNumber() returns 0
|
---|
146 | if (startrun==0 || stoprun==0)
|
---|
147 | return 1;
|
---|
148 |
|
---|
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");
|
---|
153 | if (!res)
|
---|
154 | return 2;
|
---|
155 |
|
---|
156 | //update the exclusion-reasons for all runs
|
---|
157 | TSQLRow *row=0;
|
---|
158 | while ((row = res->Next()))
|
---|
159 | {
|
---|
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);
|
---|
179 | if (!res2)
|
---|
180 | return 2;
|
---|
181 |
|
---|
182 | // Update exlcusion for file
|
---|
183 | TSQLRow *row2=0;
|
---|
184 | while ((row2 = res2->Next()))
|
---|
185 | {
|
---|
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;
|
---|
193 | }
|
---|
194 |
|
---|
195 | delete res2;
|
---|
196 | }
|
---|
197 | delete res;
|
---|
198 |
|
---|
199 | return 1;
|
---|
200 | }
|
---|
201 |
|
---|
202 | //run doexclusions for one night
|
---|
203 | int doexclusions(TString date="NULL", Int_t tel=1, Bool_t dummy=kTRUE)
|
---|
204 | {
|
---|
205 | return doexclusions(0, 0, tel, date, dummy);
|
---|
206 | }
|
---|