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

Last change on this file since 9108 was 8996, checked in by tbretz, 16 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 <TRegexp.h>
58
59#include <TH1.h>
60#include <TGraph.h>
61#include <TProfile.h>
62#include <TFile.h>
63#include <TSQLResult.h>
64#include <TSQLRow.h>
65
66#include "MSQLServer.h"
67
68#include "MStatusArray.h"
69#include "MGeomCamMagic.h"
70#include "MAlphaFitter.h"
71
72using namespace std;
73
74// --------------------------------------------------------------------------
75//
76// Checks whether an entry is already existing
77//
78Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
79{
80 TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
81 TSQLResult *res = serv.Query(query);
82 if (!res)
83 return kFALSE;
84
85 TSQLRow *row;
86
87 Bool_t rc = kFALSE;
88 while ((row=res->Next()))
89 {
90 if ((*row)[0])
91 {
92 rc = kTRUE;
93 break;
94 }
95 }
96
97 delete res;
98
99 return rc;
100}
101
102
103int Process(MSQLServer &serv, TString fname, Bool_t dummy)
104{
105 TFile file(fname, "READ");
106 if (!file.IsOpen())
107 {
108 cout << "ERROR - Could not find file " << fname << endl;
109 return 2;
110 }
111
112 //get excess, signal, background events and the scale factor from MAlphaFitter
113 MAlphaFitter *fit;
114 file.GetObject("MAlphaFitter", fit);
115
116 if (!fit)
117 {
118 cout << "WARNING - Reading of MAlphaFitter failed." << endl;
119 return 2;
120 }
121
122 Int_t exc = (Int_t)fit->GetEventsExcess();
123 Int_t sig = (Int_t)fit->GetEventsSignal();
124 Int_t bgd = (Int_t)fit->GetEventsBackground();
125 Float_t S = fit->GetSignificance();
126 TString signif = Form("%5.1f", S);
127 Float_t f = fit->GetScaleFactor();
128 TString scale = Form("%5.2f", f);
129
130 //get effective ontime from the status display
131 MStatusArray arr;
132 if (arr.Read()<=0)
133 {
134 cout << "ERROR - Reading of MStatusDisplay failed." << endl;
135 return 2;
136 }
137
138 TH1D *vstime = (TH1D*)arr.FindObjectInCanvas("Theta", "TH1D", "OnTime");
139 if (!vstime)
140 {
141 cout << "WARNING - Reading of Theta failed." << endl;
142 return 2;
143 }
144
145
146 Int_t tm = (Int_t)vstime->Integral();
147
148 //get dataset number from filename
149 TString dataset = fname(TRegexp("ganymed[0-9]+[.]root$"));
150 if (dataset.IsNull())
151 {
152 cout << "WARNING - Could get dataset# from filename: " << fname << endl;
153 return 2;
154 }
155
156 Int_t ds = atoi(dataset.Data()+8);
157
158 cout << "Dataset #" << ds << endl;
159 cout << " Excess Events: " << exc << endl;
160 cout << " Background Events: " << bgd << endl;
161 cout << " Signal Events: " << sig << endl;
162 cout << " Significance: " << signif << endl;
163 cout << " Scale Factor: " << scale << endl;
164 cout << " Total eff. on-time: " << tm << "s = " << tm/3600. << "h" << endl;
165
166// cout << " Excess Rate: " << exc*60/tm << " evts/min" << endl;
167// cout << " Background Rate: " << bgd*60/tm << " evts/min" << endl;
168// cout << " Significance Rate: " << S/TMath::Sqrt(tm/3600.) << " sigma/sqrt(h)" << endl;
169
170
171 //build query, insert information into the DB
172 //if entry is already existing: update query
173 //else: insert query
174 TString query;
175 if (!ExistStr(serv, "fDataSetNumber", "Ganymed", ds))
176 {
177 query = Form("INSERT Ganymed SET"
178 " fDataSetNumber=%d,"
179 " fExcessEvents=%d, "
180 " fBackgroundEvents=%d, "
181 " fSignalEvents=%d, "
182 " fSignificance=%s, "
183 " fScaleFactor=%s, "
184 " fEffOnTime=%d ",
185 ds, exc, bgd, sig,
186 signif.Data(),
187 scale.Data(), tm);
188 }
189 else
190 {
191 query = Form("UPDATE Ganymed SET"
192 " fExcessEvents=%d, "
193 " fBackgroundEvents=%d, "
194 " fSignalEvents=%d, "
195 " fSignificance=%s, "
196 " fScaleFactor=%s, "
197 " fEffOnTime=%d "
198 " WHERE fDataSetNumber=%d ",
199 exc, bgd, sig, signif.Data(),
200 scale.Data(), tm, ds);
201 }
202
203 cout << "q: " << query << endl;
204 if (dummy)
205 return 1;
206
207 TSQLResult *res = serv.Query(query);
208 if (!res)
209 {
210 cout << "ERROR - Query failed: " << query << endl;
211 return 2;
212 }
213 delete res;
214 return 1;
215}
216
217int fillganymed(TString fname, Bool_t dummy=kTRUE)
218{
219 MSQLServer serv("sql.rc");
220 if (!serv.IsConnected())
221 {
222 cout << "ERROR - Connection to database failed." << endl;
223 return 0;
224 }
225
226 cout << "fillganymed" << endl;
227 cout << "-----------" << endl;
228 cout << endl;
229 cout << "Connected to " << serv.GetName() << endl;
230 cout << "File: " << fname << endl;
231 cout << endl;
232
233 //process file
234 return Process(serv, fname, dummy);
235}
Note: See TracBrowser for help on using the repository browser.