source: branches/Corsika7500Compatibility/datacenter/macros/writedatasetfile.C@ 19940

Last change on this file since 19940 was 8996, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 5.0 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: writedatasetfile.C,v 1.3 2008-07-05 19:01:43 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-2008
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 <errno.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 USING (fSourceKEY)"
93 " WHERE fDataSetNumber=%d", dsno));
94
95 TSQLResult *res = serv.Query(query0);
96 if (!res)
97 {
98 cout << "ERROR - Query failed: " << query0 << endl;
99 return 2;
100 }
101 if (res->GetRowCount()==0)
102 {
103 cout << "ERROR - No dataset #" << dsno << " found." << endl;
104 return 2;
105 }
106 if (res->GetRowCount()!=1)
107 {
108 cout << "ERROR - Wrong number of datasets #" << dsno << " found." << endl;
109 return 2;
110 }
111
112 TSQLRow *row = res->Next();
113
114 TString source = (*row)[0];
115 TString comment = (*row)[1];
116 TString name = (*row)[2];
117 TString runtime = (*row)[3];
118 TString mode = (*row)[4];
119 TString on = "SequencesOn:";
120 TString off = "SequencesOff:";
121
122 const Bool_t wobble = mode.Atoi()==2;
123
124 delete res;
125
126
127 Int_t rc1 = GetDataOnOff(serv, dsno, 1, on);
128 if (rc1!=kTRUE)
129 return rc1;
130
131 Int_t rc2 = GetDataOnOff(serv, dsno, 2, off);
132 if (rc2!=kTRUE)
133 return rc1;
134
135
136 if (!seqpath.IsNull() && !seqpath.EndsWith("/"))
137 seqpath += "/";
138 seqpath += Form("dataset%08d.txt", dsno);
139
140 ofstream fout(seqpath);
141 if (!fout)
142 {
143 cout << "ERROR - Cannot open file " << seqpath << ": " << strerror(errno) << endl;
144 return 2;
145 }
146
147 fout << "AnalysisNumber: " << dsno << endl << endl;
148 fout << on << endl;
149 if (off.Length()>13)
150 fout << off << endl;
151 fout << endl;
152 fout << "SourceName: " << source << endl;
153 fout << "Catalog: /magic/datacenter/setup/magic_favorites_dc.edb" << endl;
154 if (wobble)
155 fout << "WobbleMode: On" << endl;
156 fout << endl;
157 fout << "RunTime: " << runtime << endl;
158 fout << "Name: " << name << endl;
159 fout << "Comment: " << comment << endl;
160 fout << endl;
161
162 return 1;
163}
164
165int writedatasetfile(Int_t dsno, TString seqpath)
166{
167 MSQLMagic serv("sql.rc");
168 if (!serv.IsConnected())
169 {
170 cout << "ERROR - Connection to database failed." << endl;
171 return 0;
172 }
173
174 cout << "writedatasetfile" << endl;
175 cout << "----------------" << endl;
176 cout << endl;
177 cout << "Connected to " << serv.GetName() << endl;
178 cout << endl;
179
180 return Process(serv, dsno, seqpath);
181}
Note: See TracBrowser for help on using the repository browser.