source: trunk/Mars/datacenter/macros/doexclusions.C@ 14156

Last change on this file since 14156 was 9196, checked in by snruegam, 16 years ago
*** empty log message ***
File size: 6.8 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", telnum)
39// .x doexclusions.C+(startrun,stoprun, telnum)
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
70using namespace std;
71
72//get minimum or maximum runnumber of the runs of a night
73int 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
105int 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 cout << "Telescope: " << tel << endl;
124
125 serv.SetIsDummy(dummy);
126
127 // if neither start- nor stoprun is given, the minimum and maximum
128 // runnumber is queried from the database to do the exclusions for
129 // all runs if a night is given for all runs of this night
130 if (startrun==0 && stoprun==0)
131 {
132 startrun = GetRunNumber(serv, tel, date, "MIN");
133 stoprun = GetRunNumber(serv, tel, date, "MAX");
134 }
135
136 cout << "Start Run: " << startrun << endl;
137 cout << "Stop Run: " << stoprun << endl;
138
139 //check format of start- and stoprun
140 if (startrun<0 || stoprun<0)
141 {
142 cout << "ERROR - Startrun<0 or stoprun<0." << endl;
143 return 2;
144 }
145
146 //if no run for date is available, GetRunNumber() returns 0
147 if (startrun==0 || stoprun==0)
148 return 1;
149
150 // Get exclusions-reasons (stored in the table ExcludedFDA) from the DB
151 TSQLResult *res = serv.Query("SELECT fExcludedFDAKEY, fExcludedFDAName "
152 "FROM ExcludedFDA "
153 "ORDER BY fExcludedFDAImportance ASC");
154 if (!res)
155 return 2;
156
157 //update the exclusion-reasons for all runs
158 TSQLRow *row=0;
159 while ((row = res->Next()))
160 {
161 // check the key (NULL means "No exclusion")
162 const TString key = (*row)[0];
163 if (key.IsNull())
164 continue;
165
166 // Get the corresponding condition from the file
167 const TString cond = rc.GetValue("key"+key, "");
168 if (cond.IsNull())
169 continue;
170
171 // Get all files to be excluded
172 TString query2 = "SELECT fRunNumber, fFileNumber FROM RunData ";
173 query2 += serv.GetJoins("RunData", query2+cond);
174
175 query2 += Form("WHERE (%s) ", cond.Data());
176 query2 += Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun);
177 query2 += Form("AND fTelescopeNumber=%d", tel);
178
179 TSQLResult *res2 = serv.Query(query2);
180 if (!res2)
181 return 2;
182
183 // Update exlcusion for file
184 TSQLRow *row2=0;
185 while ((row2 = res2->Next()))
186 {
187 TString vars(Form("fExcludedFDAKEY=%s", key.Data()));
188 TString where(Form("fRunNumber=%s AND fFileNumber=%s AND fTelescopeNumber=%d", (*row2)[0], (*row2)[1], tel));
189
190 if (serv.Update("RunData", vars, where)==kFALSE)
191 return 2;
192
193 cout << "File M" << tel << ":" << (*row2)[0] << "/" << setw(3) << setfill('0') << (*row2)[1] << " excluded due to: " << (*row)[1] << "." << endl;
194 }
195
196 delete res2;
197 }
198 delete res;
199
200 return 1;
201}
202
203//run doexclusions for one night
204int doexclusions(TString date="NULL", Int_t tel=1, Bool_t dummy=kTRUE)
205{
206 return doexclusions(0, 0, tel, date, dummy);
207}
Note: See TracBrowser for help on using the repository browser.