source: trunk/MagicSoft/Mars/datacenter/macros/fillsinope.C@ 7353

Last change on this file since 7353 was 7265, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 5.2 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, 04/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Daniela Dorner, 04/2005 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// fillsinope.C
29// ============
30//
31// This macro is used to read the sinope output files sinope*.txt
32//
33// Usage:
34// .x fillsignal.C(runnumber, kTRUE)
35//
36// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
37// switched on and nothing will be written into the database. This is usefull
38// for tests.
39//
40// Remark: Running it from the commandline looks like this:
41// root -q -l -b fillsinope.C+\(runno\,kFALSE\) 2>&1 | tee fillsinope.log
42//
43// Make sure, that database and password are corretly set in a resource
44// file called sql.rc and the resource file is found.
45//
46// Returns 0 in case of failure and 1 in case of success.
47//
48/////////////////////////////////////////////////////////////////////////////
49#include <iostream>
50
51#include <TEnv.h>
52#include <TRegexp.h>
53
54#include <TFile.h>
55#include <TSQLResult.h>
56#include <TSQLRow.h>
57
58#include "MSQLServer.h"
59
60#include "MStatusArray.h"
61#include "MHCamera.h"
62
63using namespace std;
64
65Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
66{
67 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
68 TSQLResult *res = serv.Query(query);
69 if (!res)
70 {
71 cout << "ERROR - Query failed: " << query << endl;
72 return kFALSE;
73 }
74
75 Bool_t rc = kFALSE;
76
77 TSQLRow *row=res->Next();
78 if (row && (*row)[0])
79 rc=kTRUE;
80
81 delete res;
82 return rc;
83}
84
85int Process(MSQLServer &serv, TString fname, Int_t runno, Bool_t cal, Bool_t dummy)
86{
87 TEnv env(fname);
88
89 TString query="UPDATE DataCheck SET ";
90
91 TString values[9] = { "Events" , "HasSignal" , "HasPedestal" , "PositionSignal" , "PositionFWHM" , "PositionAsym" , "HeightSignal" , "HeightFWHM" , "HeightAsym" };
92
93 TString str;
94 for (Int_t i=0 ; i<9 ; i++)
95 {
96// cout << "value: " << values[i] << endl;
97// cout << "str: " << str << endl;
98 str = env.GetValue(values[i], "");
99 if (str.IsNull())
100 continue;
101
102 if (cal)
103 values[i]+="InterLaced";
104 values[i]+="='";
105 values[i]+=str;
106
107 if (i!=0)
108 query+=", ";
109 query+=" f";
110 query+=values[i];
111 query+="' ";
112
113 cout << "value: " << values[i] << endl;
114
115 }
116
117 query+=Form(" WHERE fRunNumber=%d", runno);
118
119 cout << "Q: " << query << endl;
120 TSQLResult *res = serv.Query(query);
121 if (!res)
122 {
123 cout << "ERROR - Query failed: " << query << endl;
124 return kFALSE;
125 }
126 delete res;
127 return 1;
128}
129
130int fillsinope(Int_t runno, TString datapath, Bool_t dummy=kTRUE)
131{
132 TEnv env("sql.rc");
133
134 MSQLServer serv(env);
135 if (!serv.IsConnected())
136 {
137 cout << "ERROR - Connection to database failed." << endl;
138 return 0;
139 }
140
141 cout << "fillsignal" << endl;
142 cout << "----------" << endl;
143 cout << endl;
144 cout << "Connected to " << serv.GetName() << endl;
145 cout << "Run: " << runno << endl;
146 cout << endl;
147
148 TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d",
149 runno));
150
151 TSQLResult *res = serv.Query(query);
152 if (!res)
153 {
154 cout << "ERROR - Query failed: " << query << endl;
155 return 0;
156 }
157
158 TSQLRow *row = 0;
159 row = res->Next();
160 TString date=(*row)[0];
161 cout << "date: " << date << endl;
162 delete res;
163
164 if (!ExistStr(serv, "fRunNumber", "DataCheck", Form("%d", runno)))
165 {
166 query=Form("INSERT DataCheck SET fRunNumber=%d", runno);
167
168 cout << "U:" << query << endl;
169 res = serv.Query(query);
170 if (!res)
171 {
172 cout << "ERROR - Query failed: " << query << endl;
173 return 0;
174 }
175 }
176
177 TString fname(Form("%s/sinope/%s/sinope-dat%08d.txt",
178 datapath.Data(), date.Data(), runno));
179 cout << "file: " << fname << endl;
180 TString fnamecal(Form("%s/sinope/%s/sinope-cal%08d.txt",
181 datapath.Data(), date.Data(), runno));
182 cout << "file-cal: " << fnamecal << endl;
183
184 Int_t rc=0;
185 rc=Process(serv, fname, runno, kFALSE, dummy);
186 if (rc==0)
187 return rc;
188
189 rc=Process(serv, fnamecal, runno, kTRUE, dummy);
190 return rc;
191}
Note: See TracBrowser for help on using the repository browser.