source: tags/Mars-V0.9.4.3/datacenter/macros/doexclusions.C

Last change on this file was 7486, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 8.4 KB
Line 
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/////////////////////////////////////////////////////////////////////////////
56#include <iostream>
57#include <iomanip>
58#include <fstream>
59
60#include <MSQLServer.h>
61#include <TSQLRow.h>
62#include <TSQLResult.h>
63
64#include <TEnv.h>
65#include <TSystem.h>
66
67using namespace std;
68
69//get minimum or maximum runnumber of the runs of a night
70int GetRunNumber(MSQLServer &serv, TString date, TString value)
71{
72 TString query(Form("SELECT %s(fRunNumber) FROM RunData ", value.Data()));
73
74 if (date!="NULL")
75 {
76 TString day=date+" 13:00:00";
77 query+=Form(" WHERE (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
78 day.Data(), day.Data());
79 }
80
81 cout << "query: " << query << endl;
82
83 TSQLResult *res = serv.Query(query);
84 if (!res)
85 {
86 cout << "Error - could not get run#" << endl;
87 return -1;
88 }
89
90 TSQLRow *row =res->Next();
91 cout << (void*)row << endl;
92 if (TString((*row)[0]).IsNull())
93 {
94 cout << "No run available for this date" << endl;
95 delete res;
96 return 0;
97 }
98 delete res;
99 return atoi((*row)[0]);
100}
101
102//get part of a query (left join of tables)
103TString GetJoin(TString table)
104{
105 TString query(Form("left join %s ON RunData.f%sKEY=%s.f%sKEY ",
106 table.Data(), table.Data(), table.Data(), table.Data()));
107 return query;
108}
109
110int doexclusions(Int_t startrun, Int_t stoprun, TString date="NULL")
111{
112 TEnv env("sql.rc");
113 TEnv rc("automatic-exclusions.rc");
114
115 MSQLServer serv(env);
116 if (!serv.IsConnected())
117 {
118 cout << "ERROR - Connection to database failed." << endl;
119 return 0;
120 }
121
122 cout << "doexclusions" << endl;
123 cout << "------------" << endl;
124 cout << endl;
125 cout << "Connected to " << serv.GetName() << endl;
126
127 //if neither start- nor stoprun is given, the minimum and maximum runnumber
128 // is queried from the database to do the exclusions for all runs
129 // if a night is given for all runs of this night
130 if (startrun==0 && stoprun==0)
131 {
132 startrun=GetRunNumber(serv, date, "min");
133 stoprun=GetRunNumber(serv, date, "max");
134 }
135 //check format of start- and stoprun
136 if (startrun<0 || stoprun<0)
137 {
138 cout << "wrong format of date" << endl;
139 return 0;
140 }
141 if (startrun==0 || stoprun==0)
142 return 1;
143
144 //get the condition for the runnumber range
145 TString runcond(Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun));
146
147 //get exclusions-reasons (stored in the table ExcludedFDA) from the database
148 //the exclusions which can be done automatically are marked with the flag fExcludedFDAAutomatic='yes'
149 //and with an importance (one run may be excluded for several reasons,
150 //the reason is chosen according to the importance)
151 TString query="SELECT fExcludedFDAKEY from ExcludedFDA where fExcludedFDAAutomatic='yes'";
152 TSQLResult *res = serv.Query(query);
153 if (!res)
154 {
155 cout << "Error - could not do any automatic excludes" << endl;
156 return 0;
157 }
158
159 //update the exclusion-reasons for all runs
160 TSQLRow *row=0;
161 while ((row = res->Next()))
162 {
163 //read in values from the resource file automatic-exclusions (explanation see above)
164 TString key=(*row)[0];
165 TString column=rc.GetValue("key"+key+".Column", "");
166 TString join1=rc.GetValue("key"+key+".Join1", "");
167 TString join2=rc.GetValue("key"+key+".Join2", "");
168 TString border=rc.GetValue("key"+key+".SpecialRunCond", "");
169
170 //get importance of exclusion-reason from database
171 TString query(Form("SELECT fExcludedFDAImportance from ExcludedFDA where fExcludedFDAKEY=%s ", key.Data()));
172 TSQLResult *res = serv.Query(query);
173 if (!res)
174 {
175 cout << "Error" << endl;
176 return 0;
177 }
178
179 TSQLRow *row2=res->Next();
180 Int_t newimp=atoi((*row2)[0]);
181 delete res;
182
183 //get current importance from database
184 //for the runs which match the exclusion-reason
185 query="SELECT fRunNumber, fExcludedFDAImportance ";
186 if (!column.IsNull())
187 query+=Form(", %s", column.Data());
188 if (!join1.IsNull())
189 query+=Form(", f%sName", join1.Data());
190 if (!join2.IsNull())
191 query+=Form(", f%sName", join2.Data());
192 query +=" FROM RunData ";
193 query +=GetJoin("ExcludedFDA");
194 if (!join1.IsNull())
195 query+=GetJoin(join1.Data());
196 if (!join2.IsNull())
197 query+=GetJoin(join2.Data());
198 query +=Form("WHERE (%s) ", rc.GetValue("key"+key+".Cond", ""));
199 if (!border.IsNull())
200 query+=Form(" AND fRunNumber BETWEEN IF(%s>%d, %d, %s) AND IF (%s<%d, %s, %d) ",
201 border.Data(), startrun, startrun, border.Data(),
202 border.Data(), stoprun, border.Data(), stoprun);
203 else
204 query +=runcond;
205
206 cout << query << endl;
207
208 res = serv.Query(query);
209 if (!res)
210 {
211 cout << "Error - no runs to exclude" << endl;
212 return 0;
213 }
214
215 //compare new and old importance
216 //change or keep the exclusion-reason accordingly
217 while ((row2 = res->Next()))
218 {
219 if (TString((*row2)[1]).IsNull() || atoi((*row2)[1])>newimp)
220 {
221 //change exclusion-reason
222 TString query(Form("UPDATE RunData SET fExcludedFDAKEY=%s WHERE fRunNumber=%s",
223 key.Data(), (*row2)[0]));
224 cout << "QU: " << query << endl;
225 TSQLResult *res = serv.Query(query);
226 if (!res)
227 {
228 cout << "Error - could not insert exclusion" << endl;
229 return 0;
230 }
231 delete res;
232 continue;
233 }
234 //keep exclusion-reason
235 cout << "run#: " << (*row2)[0] << " reason for exclusion is still the same" << endl;
236 }
237 delete res;
238 }
239 delete res;
240 return 1;
241}
242
243//run doexclusions for one night
244int doexclusions(TString date="NULL")
245{
246 return doexclusions(0, 0, date);
247}
Note: See TracBrowser for help on using the repository browser.