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

Last change on this file since 8986 was 8248, checked in by snruegam, 18 years ago
*** empty log message ***
File size: 5.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: writedatasetfile.C,v 1.2 2007-01-12 17:33:10 snruegam 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#include <errno.h>
54
55#include "MSQLMagic.h"
56
57using namespace std;
58
59Int_t GetDataOnOff(MSQLMagic &serv, Int_t dsno, Int_t type, TString &data)
60{
61 //get sequence information from database
62 TString query1(Form("SELECT fSequenceFirst FROM DataSetSequenceMapping "
63 " WHERE fDataSetNumber=%d AND fOnOff=%d", dsno, type));
64
65 TSQLResult *res = serv.Query(query1);
66 if (!res)
67 {
68 cout << "ERROR - Query failed: " << query1 << endl;
69 return 2;
70 }
71 if (res->GetRowCount()==0 && type==1)
72 {
73 cout << "ERROR - Dataset #" << dsno << " has no on-sequences." << endl;
74 return 2;
75 }
76
77 TSQLRow *row = 0;
78 while ((row=res->Next()))
79 {
80 data += " ";
81 data += (*row)[0];
82 }
83
84 delete res;
85
86 return kTRUE;
87}
88
89Int_t Process(MSQLMagic &serv, Int_t dsno, TString seqpath)
90{
91 //get sequence information from database
92 TString query0(Form("SELECT fSourceName, fComment, fDataSetName, fRunTime, fObservationModeKEY"
93 " FROM DataSets LEFT JOIN Source ON"
94 " Source.fSourceKEY=DataSets.fSourceKEY"
95 " WHERE fDataSetNumber=%d", dsno));
96
97 TSQLResult *res = serv.Query(query0);
98 if (!res)
99 {
100 cout << "ERROR - Query failed: " << query0 << endl;
101 return 2;
102 }
103 if (res->GetRowCount()==0)
104 {
105 cout << "ERROR - No dataset #" << dsno << " found." << endl;
106 return 2;
107 }
108 if (res->GetRowCount()!=1)
109 {
110 cout << "ERROR - Wrong number of datasets #" << dsno << " found." << endl;
111 return 2;
112 }
113
114 TSQLRow *row = res->Next();
115
116 TString source = (*row)[0];
117 TString comment = (*row)[1];
118 TString name = (*row)[2];
119 TString runtime = (*row)[3];
120 TString mode = (*row)[4];
121 TString on = "SequencesOn:";
122 TString off = "SequencesOff:";
123
124 const Bool_t wobble = mode.Atoi()==2;
125
126 delete res;
127
128
129 Int_t rc1 = GetDataOnOff(serv, dsno, 1, on);
130 if (rc1!=kTRUE)
131 return rc1;
132
133 Int_t rc2 = GetDataOnOff(serv, dsno, 2, off);
134 if (rc2!=kTRUE)
135 return rc1;
136
137
138 if (!seqpath.IsNull() && !seqpath.EndsWith("/"))
139 seqpath += "/";
140 seqpath += Form("dataset%08d.txt", dsno);
141
142 ofstream fout(seqpath);
143 if (!fout)
144 {
145 cout << "ERROR - Cannot open file " << seqpath << ": " << strerror(errno) << endl;
146 return 2;
147 }
148
149 fout << "AnalysisNumber: " << dsno << endl << endl;
150 fout << on << endl;
151 if (off.Length()>13)
152 fout << off << endl;
153 fout << endl;
154 fout << "SourceName: " << source << endl;
155 fout << "Catalog: /magic/datacenter/setup/magic_favorites_dc.edb" << endl;
156 if (wobble)
157 fout << "WobbleMode: On" << endl;
158 fout << endl;
159 fout << "RunTime: " << runtime << endl;
160 fout << "Name: " << name << endl;
161 fout << "Comment: " << comment << endl;
162 fout << endl;
163
164 return 1;
165}
166
167int writedatasetfile(Int_t dsno, TString seqpath)
168{
169 TEnv env("sql.rc");
170
171 MSQLMagic serv(env);
172 if (!serv.IsConnected())
173 {
174 cout << "ERROR - Connection to database failed." << endl;
175 return 0;
176 }
177
178 cout << "writedatasetfile" << endl;
179 cout << "----------------" << endl;
180 cout << endl;
181 cout << "Connected to " << serv.GetName() << endl;
182 cout << endl;
183
184 return Process(serv, dsno, seqpath);
185}
Note: See TracBrowser for help on using the repository browser.