source: trunk/MagicSoft/Mars/datacenter/macros/fillganymed.C@ 8499

Last change on this file since 8499 was 7777, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 6.9 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/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2006
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// fillganymed.C
28// =============
29//
30// This macro is used to read the ganymed-output files.
31// These files are automatically called ganymed00000.root.
32// From MAlphaFitter and the Status Display the results from ganymed are
33// extracted an inserted into the database, where they are stored in the table
34// Ganymed.
35// The dataset number is extracted from the filename.
36//
37// Usage:
38// .x fillganymed.C("/magic/data/ganymed/0000/0001/ganmymed0001.root", kTRUE)
39//
40// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
41// switched on and nothing will be written into the database. This is usefull
42// for tests.
43//
44// Remark: Running it from the commandline looks like this:
45// root -q -l -b fillganymed.C+\(\"filename\"\,kFALSE\) 2>&1 | tee file.log
46//
47// Make sure, that database and password are corretly set in a resource
48// file called sql.rc and the resource file is found.
49//
50// Returns 2 in case of failure, 1 in case of success and 0 if the connection
51// to the database is not working.
52//
53/////////////////////////////////////////////////////////////////////////////
54#include <iostream>
55#include <iomanip>
56
57#include <TEnv.h>
58#include <TRegexp.h>
59
60#include <TH1.h>
61#include <TGraph.h>
62#include <TProfile.h>
63#include <TFile.h>
64#include <TSQLResult.h>
65#include <TSQLRow.h>
66
67#include "MSQLServer.h"
68
69#include "MStatusArray.h"
70#include "MGeomCamMagic.h"
71#include "MAlphaFitter.h"
72
73using namespace std;
74
75// --------------------------------------------------------------------------
76//
77// Checks whether an entry is already existing
78//
79Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
80{
81 TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
82 TSQLResult *res = serv.Query(query);
83 if (!res)
84 return kFALSE;
85
86 TSQLRow *row;
87
88 Bool_t rc = kFALSE;
89 while ((row=res->Next()))
90 {
91 if ((*row)[0])
92 {
93 rc = kTRUE;
94 break;
95 }
96 }
97
98 delete res;
99
100 return rc;
101}
102
103
104int Process(MSQLServer &serv, TString fname, Bool_t dummy)
105{
106 TFile file(fname, "READ");
107 if (!file.IsOpen())
108 {
109 cout << "ERROR - Could not find file " << fname << endl;
110 return 2;
111 }
112
113 //get excess, signal, background events and the scale factor from MAlphaFitter
114 MAlphaFitter *fit;
115 file.GetObject("MAlphaFitter", fit);
116
117 if (!fit)
118 {
119 cout << "WARNING - Reading of MAlphaFitter failed." << endl;
120 return 2;
121 }
122
123 Int_t exc = (Int_t)fit->GetEventsExcess();
124 Int_t sig = (Int_t)fit->GetEventsSignal();
125 Int_t bgd = (Int_t)fit->GetEventsBackground();
126 Float_t S = fit->GetSignificance();
127 TString signif = Form("%5.1f", S);
128 Float_t f = fit->GetScaleFactor();
129 TString scale = Form("%5.2f", f);
130
131 //get effective ontime from the status display
132 MStatusArray arr;
133 if (arr.Read()<=0)
134 {
135 cout << "ERROR - Reading of MStatusDisplay failed." << endl;
136 return 2;
137 }
138
139 TH1D *vstime = (TH1D*)arr.FindObjectInCanvas("Theta", "TH1D", "OnTime");
140 if (!vstime)
141 {
142 cout << "WARNING - Reading of Theta failed." << endl;
143 return 2;
144 }
145
146
147 Int_t tm = (Int_t)vstime->Integral();
148
149 //get dataset number from filename
150 TString dataset = fname(TRegexp("ganymed[0-9]+[.]root$"));
151 if (dataset.IsNull())
152 {
153 cout << "WARNING - Could get dataset# from filename: " << fname << endl;
154 return 2;
155 }
156
157 Int_t ds = atoi(dataset.Data()+8);
158
159 cout << "Dataset #" << ds << endl;
160 cout << " Excess Events: " << exc << endl;
161 cout << " Background Events: " << bgd << endl;
162 cout << " Signal Events: " << sig << endl;
163 cout << " Significance: " << signif << endl;
164 cout << " Scale Factor: " << scale << endl;
165 cout << " Total eff. on-time: " << tm << "s = " << tm/3600. << "h" << endl;
166
167// cout << " Excess Rate: " << exc*60/tm << " evts/min" << endl;
168// cout << " Background Rate: " << bgd*60/tm << " evts/min" << endl;
169// cout << " Significance Rate: " << S/TMath::Sqrt(tm/3600.) << " sigma/sqrt(h)" << endl;
170
171
172 //build query, insert information into the DB
173 //if entry is already existing: update query
174 //else: insert query
175 TString query;
176 if (!ExistStr(serv, "fDataSetNumber", "Ganymed", ds))
177 {
178 query = Form("INSERT Ganymed SET"
179 " fDataSetNumber=%d,"
180 " fExcessEvents=%d, "
181 " fBackgroundEvents=%d, "
182 " fSignalEvents=%d, "
183 " fSignificance=%s, "
184 " fScaleFactor=%s, "
185 " fEffOnTime=%d ",
186 ds, exc, bgd, sig,
187 signif.Data(),
188 scale.Data(), tm);
189 }
190 else
191 {
192 query = Form("UPDATE Ganymed SET"
193 " fExcessEvents=%d, "
194 " fBackgroundEvents=%d, "
195 " fSignalEvents=%d, "
196 " fSignificance=%s, "
197 " fScaleFactor=%s, "
198 " fEffOnTime=%d "
199 " WHERE fDataSetNumber=%d ",
200 exc, bgd, sig, signif.Data(),
201 scale.Data(), tm, ds);
202 }
203
204 cout << "q: " << query << endl;
205 if (dummy)
206 return 1;
207
208 TSQLResult *res = serv.Query(query);
209 if (!res)
210 {
211 cout << "ERROR - Query failed: " << query << endl;
212 return 2;
213 }
214 delete res;
215 return 1;
216}
217
218int fillganymed(TString fname, Bool_t dummy=kTRUE)
219{
220 TEnv env("sql.rc");
221
222 MSQLServer serv(env);
223 if (!serv.IsConnected())
224 {
225 cout << "ERROR - Connection to database failed." << endl;
226 return 0;
227 }
228
229 cout << "fillganymed" << endl;
230 cout << "-----------" << endl;
231 cout << endl;
232 cout << "Connected to " << serv.GetName() << endl;
233 cout << "File: " << fname << endl;
234 cout << endl;
235
236 //process file
237 return Process(serv, fname, dummy);
238}
Note: See TracBrowser for help on using the repository browser.