source: trunk/MagicSoft/Mars/datacenter/macros/writedatasetfile.C@ 8239

Last change on this file since 8239 was 8179, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 5.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: writedatasetfile.C,v 1.1 2006-10-31 11:38:51 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Thomas Bretz, 11/2006 <mailto:tbretz@astro.uni-wuerzburg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2006
23!
24!
25\* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// writedatasetfile.C
30// ==================
31//
32// reads the dataset information from the database and writes it into a
33// txt file
34//
35// Usage:
36// .x writedatasetfile.C+(dsno, "dspath")
37//
38// Make sure, that database and password are corretly set in a resource
39// file called sql.rc and the resource file is found.
40//
41// Returns 2 in case of failure, 1 in case of success and 0 if the connection
42// to the database is not working.
43//
44/////////////////////////////////////////////////////////////////////////////
45#include <iostream>
46#include <iomanip>
47#include <fstream>
48
49#include <TSQLRow.h>
50#include <TSQLResult.h>
51
52#include <TEnv.h>
53
54#include "MSQLMagic.h"
55
56using namespace std;
57
58Int_t GetDataOnOff(MSQLMagic &serv, Int_t dsno, Int_t type, TString &data)
59{
60 //get sequence information from database
61 TString query1(Form("SELECT fSequenceFirst FROM DataSetSequenceMapping "
62 " WHERE fDataSetNumber=%d AND fOnOff=%d", dsno, type));
63
64 TSQLResult *res = serv.Query(query1);
65 if (!res)
66 {
67 cout << "ERROR - Query failed: " << query1 << endl;
68 return 2;
69 }
70 if (res->GetRowCount()==0 && type==1)
71 {
72 cout << "ERROR - Dataset #" << dsno << " has no on-sequences." << endl;
73 return 2;
74 }
75
76 TSQLRow *row = 0;
77 while ((row=res->Next()))
78 {
79 data += " ";
80 data += (*row)[0];
81 }
82
83 delete res;
84
85 return kTRUE;
86}
87
88Int_t Process(MSQLMagic &serv, Int_t dsno, TString seqpath)
89{
90 //get sequence information from database
91 TString query0(Form("SELECT fSourceName, fComment, fDataSetName, fRunTime, fObservationModeKEY"
92 " FROM DataSets LEFT JOIN Source ON"
93 " Source.fSourceKEY=DataSets.fSourceKEY"
94 " WHERE fDataSetNumber=%d", dsno));
95
96 TSQLResult *res = serv.Query(query0);
97 if (!res)
98 {
99 cout << "ERROR - Query failed: " << query0 << endl;
100 return 2;
101 }
102 if (res->GetRowCount()==0)
103 {
104 cout << "ERROR - No dataset #" << dsno << " found." << endl;
105 return 2;
106 }
107 if (res->GetRowCount()!=1)
108 {
109 cout << "ERROR - Wrong number of datasets #" << dsno << " found." << endl;
110 return 2;
111 }
112
113 TSQLRow *row = res->Next();
114
115 TString source = (*row)[0];
116 TString comment = (*row)[1];
117 TString name = (*row)[2];
118 TString runtime = (*row)[3];
119 TString mode = (*row)[4];
120 TString on = "SequencesOn:";
121 TString off = "SequencesOff:";
122
123 const Bool_t wobble = mode.Atoi()==2;
124
125 delete res;
126
127
128 Int_t rc1 = GetDataOnOff(serv, dsno, 1, on);
129 if (rc1!=kTRUE)
130 return rc1;
131
132 Int_t rc2 = GetDataOnOff(serv, dsno, 2, off);
133 if (rc2!=kTRUE)
134 return rc1;
135
136
137 if (!seqpath.IsNull() && !seqpath.EndsWith("/"))
138 seqpath += "/";
139 seqpath += Form("dataset%08d.txt", dsno);
140
141 ofstream fout(seqpath);
142 if (!fout)
143 {
144 cout << "ERROR - Cannot open file " << seqpath << ": " << strerror(errno) << endl;
145 return 2;
146 }
147
148 fout << "AnalysisNumber: " << dsno << endl << endl;
149 fout << on << endl;
150 if (off.Length()>13)
151 fout << off << endl;
152 fout << endl;
153 fout << "SourceName: " << source << endl;
154 fout << "Catalog: /magic/datacenter/setup/magic_favorites_dc.edb" << endl;
155 if (wobble)
156 fout << "WobbleMode: On" << endl;
157 fout << endl;
158 fout << "RunTime: " << runtime << endl;
159 fout << "Name: " << name << endl;
160 fout << "Comment: " << comment << endl;
161 fout << endl;
162
163 return 1;
164}
165
166int writedatasetfile(Int_t dsno, TString seqpath)
167{
168 TEnv env("sql.rc");
169
170 MSQLMagic serv(env);
171 if (!serv.IsConnected())
172 {
173 cout << "ERROR - Connection to database failed." << endl;
174 return 0;
175 }
176
177 cout << "writedatasetfile" << endl;
178 cout << "----------------" << endl;
179 cout << endl;
180 cout << "Connected to " << serv.GetName() << endl;
181 cout << endl;
182
183 return Process(serv, dsno, seqpath);
184}
Note: See TracBrowser for help on using the repository browser.