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

Last change on this file since 7498 was 7486, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 6.3 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 0 in case of failure and 1 in case of success.
57//
58/////////////////////////////////////////////////////////////////////////////
59#include <iostream>
60
61#include <TEnv.h>
62#include <TRegexp.h>
63
64#include <TFile.h>
65#include <TSQLResult.h>
66#include <TSQLRow.h>
67
68#include "MSQLServer.h"
69
70#include "MStatusArray.h"
71#include "MHCamera.h"
72
73using namespace std;
74
75Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
76{
77 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
78 TSQLResult *res = serv.Query(query);
79 if (!res)
80 {
81 cout << "ERROR - Query failed: " << query << endl;
82 return kFALSE;
83 }
84
85 Bool_t rc = kFALSE;
86
87 TSQLRow *row=res->Next();
88 if (row && (*row)[0])
89 rc=kTRUE;
90
91 delete res;
92 return rc;
93}
94
95int Process(MSQLServer &serv, TString fname, Int_t runno, Bool_t cal, Bool_t dummy)
96{
97 TEnv env(fname);
98
99 //build query
100 TString query="UPDATE DataCheck SET ";
101
102 //array with part of column names
103 TString values[9] = { "Events" , "HasSignal" , "HasPedestal" , "PositionSignal" , "PositionFWHM" , "PositionAsym" , "HeightSignal" , "HeightFWHM" , "HeightAsym" };
104
105 //get values from the file add them to query
106 TString str;
107 for (Int_t i=0 ; i<9 ; i++)
108 {
109// cout << "value: " << values[i] << endl;
110// cout << "str: " << str << endl;
111 str = env.GetValue(values[i], "");
112 if (str.IsNull())
113 continue;
114
115 if (cal)
116 values[i]+="InterLaced";
117 values[i]+="='";
118 values[i]+=str;
119
120 if (i!=0)
121 query+=", ";
122 query+=" f";
123 query+=values[i];
124 query+="' ";
125
126 cout << "value: " << values[i] << endl;
127
128 }
129
130 query+=Form(" WHERE fRunNumber=%d", runno);
131
132 cout << "Q: " << query << endl;
133 //insert information into db
134 TSQLResult *res = serv.Query(query);
135 if (!res)
136 {
137 cout << "ERROR - Query failed: " << query << endl;
138 return kFALSE;
139 }
140 delete res;
141 return 1;
142}
143
144int fillsinope(Int_t runno, TString datapath, Bool_t dummy=kTRUE)
145{
146 TEnv env("sql.rc");
147
148 MSQLServer serv(env);
149 if (!serv.IsConnected())
150 {
151 cout << "ERROR - Connection to database failed." << endl;
152 return 0;
153 }
154
155 cout << "fillsignal" << endl;
156 cout << "----------" << endl;
157 cout << endl;
158 cout << "Connected to " << serv.GetName() << endl;
159 cout << "Run: " << runno << endl;
160 cout << endl;
161
162 //get date of run from database
163 TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d",
164 runno));
165
166 TSQLResult *res = serv.Query(query);
167 if (!res)
168 {
169 cout << "ERROR - Query failed: " << query << endl;
170 return 0;
171 }
172
173 TSQLRow *row = 0;
174 row = res->Next();
175 TString date=(*row)[0];
176 cout << "date: " << date << endl;
177 delete res;
178
179 //insert entry for the run into the database in the table DataCheck, if it is not yet existing
180 if (!ExistStr(serv, "fRunNumber", "DataCheck", Form("%d", runno)))
181 {
182 query=Form("INSERT DataCheck SET fRunNumber=%d", runno);
183
184 res = serv.Query(query);
185 if (!res)
186 {
187 cout << "ERROR - Query failed: " << query << endl;
188 return 0;
189 }
190 }
191
192 //get filenames of sinope output files
193 TString fname(Form("%s/sinope/%s/sinope-dat%08d.txt",
194 datapath.Data(), date.Data(), runno));
195 cout << "file: " << fname << endl;
196 TString fnamecal(Form("%s/sinope/%s/sinope-cal%08d.txt",
197 datapath.Data(), date.Data(), runno));
198 cout << "file-cal: " << fnamecal << endl;
199
200 Int_t rc=0;
201 //process dat-file
202 rc=Process(serv, fname, runno, kFALSE, dummy);
203 if (rc==0)
204 return rc;
205
206 //process cal-file
207 rc=Process(serv, fnamecal, runno, kTRUE, dummy);
208 return rc;
209}
Note: See TracBrowser for help on using the repository browser.