1 |
// |
2 |
// C/C++ headers |
3 |
// |
4 |
#include <fstream> |
5 |
#include <sstream> |
6 |
#include <iostream> |
7 |
// |
8 |
// ROOT headers |
9 |
// |
10 |
#include <TSystem.h> |
11 |
#include <TString.h> |
12 |
#include <TSQLServer.h> |
13 |
// |
14 |
// This package headers |
15 |
// |
16 |
#include <RunInfo.h> |
17 |
#include <RunInfoVerl2.h> |
18 |
// |
19 |
using namespace std; |
20 |
// |
21 |
// Usage subroutine |
22 |
// |
23 |
void usage(){ |
24 |
printf("\nUsage:\n"); |
25 |
printf("\n RunInfo [-v] [-h] [--version] -idRun ID_RUN [-processFile filename] [-processFolder folder]\n"); |
26 |
printf("\n --version print informations about compilation and exit\n"); |
27 |
printf("\n -h | --help print this help and exit \n"); |
28 |
printf("\n -v | --verbose be verbose [default: print nothing on STDOUT]\n"); |
29 |
printf("\n -idRun ID_RUN: ID number of the run to be processed \n"); |
30 |
printf("\n -processFile filename : output filename [default ID_RUN.Level2.root]\n"); |
31 |
printf("\n -processFolder where to store temporary data [default \"runinfoFolder\"]\n"); |
32 |
printf("\n -host name for the host [default = mysql://localhost/pamelaprod]\n"); |
33 |
printf("\n -user username for the DB [default = anonymous] \n"); |
34 |
printf("\n -psw password for the DB [default = \"\"]\n"); |
35 |
printf("\n Notice that parameter order does not matter. \n"); |
36 |
printf("\nExample: \n\nRunInfo -idRun 1085 -processFile nomefile.root\n\n"); |
37 |
}; |
38 |
// |
39 |
Bool_t existfile(TString filename){ |
40 |
ifstream myfile; |
41 |
myfile.open(filename.Data()); |
42 |
if ( !myfile ){ |
43 |
return(false); |
44 |
}; |
45 |
myfile.close(); |
46 |
return(true); |
47 |
} |
48 |
// |
49 |
Bool_t debug = false; |
50 |
// |
51 |
// |
52 |
// Here the main |
53 |
// |
54 |
int main(int numinp, char *inps[]){ |
55 |
// |
56 |
// Variables booking |
57 |
// |
58 |
int nul = 0; |
59 |
Int_t error = 0; |
60 |
Bool_t beverbose = false; |
61 |
UInt_t run = 0; |
62 |
TString filename; |
63 |
TString processFolder = "runinfoFolder"; |
64 |
TSQLServer *dbc = 0; |
65 |
Bool_t givenid = false; |
66 |
TString host = "mysql://localhost/pamelaprod"; |
67 |
TString user = "anonymous"; |
68 |
TString psw = ""; |
69 |
// |
70 |
// Checking input parameters |
71 |
// |
72 |
if ( numinp > 1 ){ |
73 |
for ( int i = 0; i < numinp; i++ ){ |
74 |
if ( !strcmp(inps[i],"--version") ){ |
75 |
RunInfoInfo(true); |
76 |
exit(0); |
77 |
}; |
78 |
if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){ |
79 |
usage(); |
80 |
exit(0); |
81 |
}; |
82 |
if ( !strcmp(inps[i],"-idRun") ) { |
83 |
if ( numinp-1 < i+1 ){ |
84 |
usage(); |
85 |
exit(-3); |
86 |
}; |
87 |
givenid = true; |
88 |
run = (UInt_t)atoll(inps[i+1]); |
89 |
}; |
90 |
if ( !strcmp(inps[i],"-processFile") ) { |
91 |
if ( numinp-1 < i+1 ){ |
92 |
usage(); |
93 |
exit(-3); |
94 |
}; |
95 |
filename = (TString)inps[i+1]; |
96 |
}; |
97 |
if ( !strcmp(inps[i],"-processFolder") ) { |
98 |
if ( numinp-1 < i+1 ){ |
99 |
usage(); |
100 |
exit(-3); |
101 |
}; |
102 |
processFolder = (TString)inps[i+1]; |
103 |
}; |
104 |
if ( !strcmp(inps[i],"-host") ) { |
105 |
if ( numinp-1 < i+1 ){ |
106 |
usage(); |
107 |
exit(-3); |
108 |
}; |
109 |
host = (TString)inps[i+1]; |
110 |
}; |
111 |
if ( !strcmp(inps[i],"-user") ) { |
112 |
if ( numinp-1 < i+1 ){ |
113 |
usage(); |
114 |
exit(-3); |
115 |
}; |
116 |
user = (TString)inps[i+1]; |
117 |
}; |
118 |
if ( !strcmp(inps[i],"-psw") ) { |
119 |
if ( numinp-1 < i+1 ){ |
120 |
usage(); |
121 |
exit(-3); |
122 |
}; |
123 |
psw = (TString)inps[i+1]; |
124 |
}; |
125 |
if ( !strcmp(inps[i],"-v") || !strcmp(inps[i],"--verbose") ) beverbose = true; |
126 |
}; |
127 |
} else { |
128 |
// |
129 |
// no input parameters exit with error, we need at least the run id. |
130 |
// |
131 |
printf(" RunInfo - ERROR: you must provide a run number (at least -1)\n"); |
132 |
exit(-1); |
133 |
}; |
134 |
// |
135 |
// If not in verbose mode redirect to /dev/null the stdout and stderr |
136 |
// |
137 |
if ( !beverbose ){ |
138 |
nul = open("/dev/null", O_CREAT | O_RDWR,S_IREAD | S_IWRITE); |
139 |
dup2(nul,1); |
140 |
dup2(nul,2); |
141 |
}; |
142 |
// |
143 |
// Check that an input run number has been given |
144 |
// |
145 |
if ( !givenid ) { |
146 |
printf(" RunInfo - ERROR: you must provide a run number (at least -1)\n"); |
147 |
exit(-1); |
148 |
}; |
149 |
// |
150 |
char *version = RunInfoInfo(false); |
151 |
// |
152 |
// Start: |
153 |
// |
154 |
printf("\n Welcome to the RunInfo software, version %s \n",version); |
155 |
// |
156 |
// Connect to the DB |
157 |
// |
158 |
printf("\nConnecting to database... \n"); |
159 |
// |
160 |
// |
161 |
dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
162 |
// |
163 |
if( !dbc ) { |
164 |
printf(" RunInfo - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n"); |
165 |
exit(-2); |
166 |
}; |
167 |
bool connect = dbc->IsConnected(); |
168 |
// |
169 |
if( !connect ) { |
170 |
printf(" RunInfo - ERROR: problems connecting to the DB (check username and passwd), exiting...\n\n"); |
171 |
exit(-2); |
172 |
}; |
173 |
printf("...connected! \n\n"); |
174 |
// |
175 |
// |
176 |
// |
177 |
stringstream name; |
178 |
name.str(""); |
179 |
if ( run != 0 ){ |
180 |
// |
181 |
// Run is not 0, we must process only one run . |
182 |
// |
183 |
// |
184 |
// If no filename is given, use the deafult one run_id.Level2.root. |
185 |
// |
186 |
if ( !strcmp(filename.Data(),"") ){ |
187 |
name << run << ".Level2.root"; |
188 |
} else { |
189 |
name << filename.Data(); |
190 |
}; |
191 |
// |
192 |
} else { |
193 |
// |
194 |
// Run is 0, we must process all entries in the given file. |
195 |
// |
196 |
// |
197 |
// OUCH! no filename has been given, run out! |
198 |
// |
199 |
if ( !strcmp(filename.Data(),"") ) { |
200 |
printf(" RunInfo - ERROR: processing all runs but no filename is given\n"); |
201 |
exit(-4); |
202 |
}; |
203 |
name << filename.Data(); |
204 |
// |
205 |
}; |
206 |
TString outputfile; |
207 |
ItoRunInfo *runinfo = 0; |
208 |
TFile *file = 0; |
209 |
// |
210 |
stringstream temprname; |
211 |
const char* routdir = gSystem->WorkingDirectory(); |
212 |
temprname.str(""); |
213 |
temprname << routdir; |
214 |
temprname << "/" << processFolder.Data(); |
215 |
// |
216 |
// Output file is "outputfile" |
217 |
// |
218 |
outputfile = name.str().c_str(); |
219 |
printf("\n Output filename is: \n %s \n\n",outputfile.Data()); |
220 |
// |
221 |
// Check if file exists, if not exit with error (calorimeter needs tracker data). |
222 |
// |
223 |
if ( !existfile(outputfile.Data()) ) { |
224 |
printf(" RunInfo: creating LEVEL2 file \n"); |
225 |
}; |
226 |
// |
227 |
// OK, file exists, open it in "update" mode. |
228 |
// |
229 |
file = new TFile(outputfile.Data(),"UPDATE"); |
230 |
if ( !file->IsOpen() ){ |
231 |
printf(" RunInfo - ERROR: cannot open file for writing\n"); |
232 |
error = -802; |
233 |
goto theend; |
234 |
}; |
235 |
// |
236 |
runinfo = new ItoRunInfo(dbc,file,processFolder); |
237 |
error = runinfo->Update(run,"NONE",""); |
238 |
gSystem->Unlink(temprname.str().c_str()); |
239 |
// if ( error < 0 ) goto theend; |
240 |
runinfo->Close(); |
241 |
if ( file ){ |
242 |
file->Write(); |
243 |
file->Close(); |
244 |
}; |
245 |
// |
246 |
theend: |
247 |
// |
248 |
// Close the DB connection |
249 |
// |
250 |
printf("\nClose the connection to the database... \n"); |
251 |
dbc->Close(); |
252 |
printf("...connection terminated!\n\n"); |
253 |
// |
254 |
// Close redirection if the case. |
255 |
// |
256 |
if ( !beverbose ) close(nul); |
257 |
// |
258 |
// Return "error" |
259 |
// |
260 |
exit(error); |
261 |
} |