source: trunk/MagicSoft/Mars/datacenter/macros/fillstar.C@ 7657

Last change on this file since 7657 was 7657, checked in by Daniela Dorner, 18 years ago
*** empty log message ***
File size: 10.6 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, 05/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Daniela Dorner, 05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// fillstar.C
29// ==========
30//
31// This macro is used to read the star-output files.
32// These files are automatically called star00000000.root.
33// From these files the muon parameters (psf, muon number, ratio, muon rate),
34// the rate, the number of islands, the effective ontime, the maximum
35// humidity and a parameter describing the inhomogeneity are extracted from
36// the status display.
37// The sequence number is extracted from the filename.
38//
39// Usage:
40// .x fillstar.C("/magic/data/star/0004/00047421/star00047421.root", kTRUE)
41//
42// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
43// switched on and nothing will be written into the database. This is usefull
44// for tests.
45//
46// The macro can also be run without ACLiC but this is a lot slower...
47//
48// Remark: Running it from the commandline looks like this:
49// root -q -l -b fillstar.C+\(\"filename\"\,kFALSE\) 2>&1 | tee fillstar.log
50//
51// Make sure, that database and password are corretly set in a resource
52// file called sql.rc and the resource file is found.
53//
54// Returns 2 in case of failure, 1 in case of success and 0 if the connection
55// to the database is not working.
56//
57/////////////////////////////////////////////////////////////////////////////
58#include <iostream>
59#include <iomanip>
60
61#include <TEnv.h>
62#include <TRegexp.h>
63
64#include <TH1.h>
65#include <TH2.h>
66#include <TGraph.h>
67#include <TProfile.h>
68#include <TFile.h>
69#include <TSQLResult.h>
70#include <TSQLRow.h>
71
72#include "MSQLServer.h"
73
74#include "MHMuonPar.h"
75#include "MStatusArray.h"
76#include "MGeomCamMagic.h"
77#include "MBadPixelsCam.h"
78
79using namespace std;
80
81// --------------------------------------------------------------------------
82//
83// Checks whether an entry is already existing
84//
85Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
86{
87 TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
88 TSQLResult *res = serv.Query(query);
89 if (!res)
90 return kFALSE;
91
92 TSQLRow *row;
93
94 Bool_t rc = kFALSE;
95 while ((row=res->Next()))
96 {
97 if ((*row)[0])
98 {
99 rc = kTRUE;
100 break;
101 }
102 }
103
104 delete res;
105
106 return rc;
107}
108
109
110int Process(MSQLServer &serv, TString fname, Bool_t dummy)
111{
112 TFile file(fname, "READ");
113 if (!file.IsOpen())
114 {
115 cout << "ERROR - Could not find file " << fname << endl;
116 return 2;
117 }
118
119
120 MStatusArray arr;
121 if (arr.Read()<=0)
122 {
123 cout << "ERROR - Reading of MStatusDisplay failed." << endl;
124 return 2;
125 }
126
127 TH2F *hcog = (TH2F*)arr.FindObjectInCanvas("Center", "TH2F", "MHHillas");
128 if (!hcog)
129 {
130 cout << "WARNING - Reading of MHHillas failed." << endl;
131 return 2;
132 }
133
134 MHMuonPar *hmuon = (MHMuonPar*)arr.FindObjectInCanvas("MHMuonPar", "MHMuonPar", "MHMuonPar");
135 if (!hmuon)
136 {
137 cout << "WARNING - Reading of MHMuon failed." << endl;
138 return 2;
139 }
140
141 Double_t val[6];
142 for (int x=1; x<hcog->GetNbinsX(); x++)
143 for (int y=1; y<hcog->GetNbinsY(); y++)
144 {
145 Stat_t px = hcog->GetXaxis()->GetBinCenter(x);
146 Stat_t py = hcog->GetYaxis()->GetBinCenter(y);
147 Int_t i = (TMath::Nint(3*TMath::ATan2(px,py)/TMath::Pi())+6)%6;
148 val[i] += hcog->GetBinContent(x, y);
149 }
150
151 Double_t inhom = TMath::RMS(6, val)*6/hcog->GetEntries()*100;
152 inhom = TMath::Nint(inhom*10)/10.;
153 TString inhomogen = Form("%5.1f", inhom);
154
155 Float_t mw = hmuon->GetMeanWidth();
156 Float_t psf = 51.505292*mw - 31.147126;
157 psf = TMath::Nint(psf*10)/10.;
158 TString PSF = Form("%5.1f", psf);
159 Int_t num = (int)hmuon->GetEntries();
160
161 Float_t ratiodatamc = (hmuon->GetMeanSize()/9340.)*100;
162 TString ratio = Form("%5.1f", ratiodatamc);
163
164 TH1 *h = (TH1*)arr.FindObjectInCanvas("Islands", "TH1F", "MHImagePar");
165 if (!h)
166 {
167 cout << "WARNING - Reading of Islands failed." << endl;
168 return 2;
169 }
170
171 Float_t quality = h->GetMean();
172 quality = TMath::Nint(quality*100)/100.;
173 TString islands = Form("%6.2f", quality);
174
175 h = (TH1*)arr.FindObjectInCanvas("EffOnTheta", "TH1D", "EffOnTime");
176 if (!h)
177 {
178 cout << "WARNING - Reading of EffOnTheta failed." << endl;
179 return 2;
180 }
181
182 Float_t effon = h->Integral();
183 Float_t mrate = num/effon;
184 mrate = TMath::Nint(mrate*100)/100.;
185 TString muonrate = Form("%6.2f", mrate);
186 Int_t effontime = TMath::Nint(effon);
187
188 h = (TH1*)arr.FindObjectInCanvas("ProjDeltaT", "TH1D", "EffOnTime");
189 if (!h)
190 {
191 cout << "WARNING - Reading of ProjDeltaT failed." << endl;
192 return 2;
193 }
194
195 Int_t numevents = (int)h->GetEntries();
196 Int_t datarate = (int)(numevents/effon);
197
198 TGraph *g = (TGraph*)arr.FindObjectInCanvas("Humidity", "TGraph", "MHWeather");
199 if (!g)
200 {
201 cout << "WARNING - Reading of Humidity failed." << endl;
202 return 2;
203 }
204
205 Double_t max = TMath::MaxElement(g->GetN(), g->GetY());
206 TString maxhum = Form("%6.1f", max);
207
208
209 g = (TGraph*)arr.FindObjectInCanvas("NumStars", "TGraph", "MHPointing");
210 if (!g)
211 {
212 cout << "WARNING - Reading of NumStars failed." << endl;
213 return 2;
214 }
215
216 Double_t numstarmean = g->GetMean(2);
217 TString numstarsmean = Form("%5.1f", numstarmean);
218 Double_t numstarrms = g->GetRMS(2);
219 TString numstarsrms = Form("%5.1f", numstarrms);
220
221 g = (TGraph*)arr.FindObjectInCanvas("Brigtness", "TGraph", "MHPointing");
222 if (!g)
223 {
224 cout << "WARNING - Reading of SkyBrightness failed." << endl;
225 return 2;
226 }
227
228 Double_t brightnessmean = g->GetMean(2);
229 TString skybrightnessmean = Form("%5.1f", brightnessmean);
230 Double_t brightnessrms = g->GetRMS(2);
231 TString skybrightnessrms = Form("%5.1f", brightnessrms);
232
233
234 TString sequence = fname(TRegexp("star[0-9]+[.]root$"));
235 if (sequence.IsNull())
236 {
237 cout << "WARNING - Could get sequence# from filename: " << fname << endl;
238 return 2;
239 }
240
241 Int_t seq = atoi(sequence.Data()+5);
242
243 cout << "Sequence #" << seq << endl;
244 cout << " Inhomogeneity " << inhomogen << endl;
245 cout << " PSF [mm] " << PSF << endl;
246 cout << " Island Quality " << islands << endl;
247 cout << " Ratio [%] " << ratio << endl;
248 cout << " Muon Number " << num << endl;
249 cout << " EffOnTime [s] " << effontime << endl;
250 cout << " Muon Rate [Hz] " << muonrate << endl;
251 cout << " # of Events " << numevents << endl;
252 cout << " Rate after ImgCl [Hz] " << datarate << endl;
253 cout << " Maximum Humidity [%] " << maxhum << endl;
254 cout << " Number of Stars " << numstarsmean << " +/- " << numstarsrms << endl;
255 cout << " Skybrightness " << skybrightnessmean << " +/- " << skybrightnessrms << endl;
256
257 TString query;
258 if (!ExistStr(serv, "fSequenceFirst", "Star", seq))
259 {
260 query = Form("INSERT Star SET"
261 " fSequenceFirst=%d,"
262 " fMeanNumberIslands=%s, "
263 " fRatio=%s, "
264 " fMuonNumber=%d, "
265 " fEffOnTime=%d, "
266 " fMuonRate=%s, "
267 " fPSF=%s, "
268 " fDataRate=%d, "
269 " fMaxHumidity=%s ,"
270 " fNumStarsMean=%s ,"
271 " fNumStarsRMS=%s ,"
272 " fBrightnessMean=%s ,"
273 " fBrightnessRMS=%s ,"
274 " fInhomogeneity=%s ",
275 seq, islands.Data(), ratio.Data(),
276 num, effontime,
277 muonrate.Data(), PSF.Data(),
278 datarate, maxhum.Data(),
279 numstarsmean.Data(), numstarsrms.Data(),
280 skybrightnessmean.Data(), skybrightnessrms.Data(),
281 inhomogen.Data());
282 }
283 else
284 {
285 query = Form("UPDATE Star SET"
286 " fMeanNumberIslands=%s, "
287 " fRatio=%s, "
288 " fMuonNumber=%d, "
289 " fEffOnTime=%d, "
290 " fMuonRate=%s, "
291 " fPSF=%s, "
292 " fDataRate=%d, "
293 " fMaxHumidity=%s, "
294 " fNumStarsMean=%s ,"
295 " fNumStarsRMS=%s ,"
296 " fBrightnessMean=%s ,"
297 " fBrightnessRMS=%s ,"
298 " fInhomogeneity=%s "
299 " WHERE fSequenceFirst=%d ",
300 islands.Data(), ratio.Data(),
301 num, effontime,
302 muonrate.Data(), PSF.Data(),
303 datarate, maxhum.Data(),
304 numstarsmean.Data(), numstarsrms.Data(),
305 skybrightnessmean.Data(), skybrightnessrms.Data(),
306 inhomogen.Data(), seq);
307 }
308
309 cout << "Q: " << query << endl;
310
311 if (dummy)
312 return 1;
313
314 TSQLResult *res = serv.Query(query);
315 if (!res)
316 {
317 cout << "ERROR - Query failed: " << query << endl;
318 return 2;
319 }
320
321 return 1;
322}
323
324int fillstar(TString fname, Bool_t dummy=kTRUE)
325{
326 TEnv env("sql.rc");
327
328 MSQLServer serv(env);
329 if (!serv.IsConnected())
330 {
331 cout << "ERROR - Connection to database failed." << endl;
332 return 0;
333 }
334
335 cout << "fillstar" << endl;
336 cout << "---------" << endl;
337 cout << endl;
338 cout << "Connected to " << serv.GetName() << endl;
339 cout << "File: " << fname << endl;
340 cout << endl;
341
342 return Process(serv, fname, dummy);
343}
Note: See TracBrowser for help on using the repository browser.