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

Last change on this file since 7744 was 7528, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 6.4 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-2006
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// For each run sinope is run twice: once for the data events and once for the
33// calibration events. The pulse position and height is checked. The output is
34// stored in a sinope*.root and a sinope*.txt file. The files for data events
35// are marked with -dat and the ones for calibration events with -cal.
36// From the txt files the result is extracted and inserted into the database
37// in the table DataCheck, which stores the datacheck results.
38// As the column names in the database differ only by the addition 'Interlaced'
39// which is given to columns corresponding to the calibration events, the
40// files can be processed with the same function.
41//
42// Usage:
43// .x fillsinope.C(runnumber, "datapath", kTRUE)
44//
45// The first argument is the runnumber (given as int), the second argument is
46// the datapath, where the rawfiles can be found. The last argument is the
47// 'dummy-mode'. If it is kTRUE dummy-mode is switched on and nothing will be
48// written into the database. This is usefull for tests.
49//
50// Remark: Running it from the commandline looks like this:
51// root -q -l -b fillsinope.C+\(runno\,kFALSE\) 2>&1 | tee fillsinope.log
52//
53// Make sure, that database and password are corretly set in a resource
54// file called sql.rc and the resource file is found.
55//
56// Returns 2 in case of failure, 1 in case of success and 0 if the connection
57// to the database is not working.
58//
59/////////////////////////////////////////////////////////////////////////////
60#include <iostream>
61
62#include <TEnv.h>
63#include <TRegexp.h>
64
65#include <TFile.h>
66#include <TSQLResult.h>
67#include <TSQLRow.h>
68
69#include "MSQLServer.h"
70
71#include "MStatusArray.h"
72#include "MHCamera.h"
73
74using namespace std;
75
76Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
77{
78 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
79 TSQLResult *res = serv.Query(query);
80 if (!res)
81 {
82 cout << "ERROR - Query failed: " << query << endl;
83 return kFALSE;
84 }
85
86 Bool_t rc = kFALSE;
87
88 TSQLRow *row=res->Next();
89 if (row && (*row)[0])
90 rc=kTRUE;
91
92 delete res;
93 return rc;
94}
95
96int Process(MSQLServer &serv, TString fname, Int_t runno, Bool_t cal, Bool_t dummy)
97{
98 TEnv env(fname);
99
100 //build query
101 TString query="UPDATE DataCheck SET ";
102
103 //array with part of column names
104 TString values[9] = { "Events" , "HasSignal" , "HasPedestal" , "PositionSignal" , "PositionFWHM" , "PositionAsym" , "HeightSignal" , "HeightFWHM" , "HeightAsym" };
105
106 //get values from the file add them to query
107 TString str;
108 for (Int_t i=0 ; i<9 ; i++)
109 {
110// cout << "value: " << values[i] << endl;
111// cout << "str: " << str << endl;
112 str = env.GetValue(values[i], "");
113 if (str.IsNull())
114 continue;
115
116 if (cal)
117 values[i]+="InterLaced";
118 values[i]+="='";
119 values[i]+=str;
120
121 if (i!=0)
122 query+=", ";
123 query+=" f";
124 query+=values[i];
125 query+="' ";
126
127 cout << "value: " << values[i] << endl;
128
129 }
130
131 query+=Form(" WHERE fRunNumber=%d", runno);
132
133 cout << "Q: " << query << endl;
134 //insert information into db
135 TSQLResult *res = serv.Query(query);
136 if (!res)
137 {
138 cout << "ERROR - Query failed: " << query << endl;
139 return 2;
140 }
141 delete res;
142 return 1;
143}
144
145int fillsinope(Int_t runno, TString datapath, Bool_t dummy=kTRUE)
146{
147 TEnv env("sql.rc");
148
149 MSQLServer serv(env);
150 if (!serv.IsConnected())
151 {
152 cout << "ERROR - Connection to database failed." << endl;
153 return 0;
154 }
155
156 cout << "fillsignal" << endl;
157 cout << "----------" << endl;
158 cout << endl;
159 cout << "Connected to " << serv.GetName() << endl;
160 cout << "Run: " << runno << endl;
161 cout << endl;
162
163 //get date of run from database
164 TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d",
165 runno));
166
167 TSQLResult *res = serv.Query(query);
168 if (!res)
169 {
170 cout << "ERROR - Query failed: " << query << endl;
171 return 2;
172 }
173
174 TSQLRow *row = 0;
175 row = res->Next();
176 TString date=(*row)[0];
177 cout << "date: " << date << endl;
178 delete res;
179
180 //insert entry for the run into the database in the table DataCheck, if it is not yet existing
181 if (!ExistStr(serv, "fRunNumber", "DataCheck", Form("%d", runno)))
182 {
183 query=Form("INSERT DataCheck SET fRunNumber=%d", runno);
184
185 res = serv.Query(query);
186 if (!res)
187 {
188 cout << "ERROR - Query failed: " << query << endl;
189 return 2;
190 }
191 }
192
193 //get filenames of sinope output files
194 TString fname(Form("%s/sinope/%s/sinope-dat%08d.txt",
195 datapath.Data(), date.Data(), runno));
196 cout << "file: " << fname << endl;
197 TString fnamecal(Form("%s/sinope/%s/sinope-cal%08d.txt",
198 datapath.Data(), date.Data(), runno));
199 cout << "file-cal: " << fnamecal << endl;
200
201 Int_t rc=0;
202 //process dat-file
203 rc=Process(serv, fname, runno, kFALSE, dummy);
204 if (rc==2)
205 return rc;
206
207 //process cal-file
208 rc=Process(serv, fnamecal, runno, kTRUE, dummy);
209 return rc;
210}
Note: See TracBrowser for help on using the repository browser.