1 |
#include <PamelaDBOperations.h> |
2 |
#include <YFile.h> |
3 |
#include <log4cxx/logger.h> |
4 |
#include <iostream> |
5 |
#include <string> |
6 |
#include <fstream> |
7 |
#include <list> |
8 |
#include <errno.h> |
9 |
#include <TSQLResult.h> |
10 |
#include <TRFIOFile.h> |
11 |
#include <TSQLRow.h> |
12 |
#include <TTree.h> |
13 |
#include <TGraph.h> |
14 |
#include <TF1.h> |
15 |
|
16 |
#include <cOrbit.h> |
17 |
#include <cTle.h> |
18 |
#include <cEci.h> |
19 |
|
20 |
#include <EventHeader.h> |
21 |
#include <PscuHeader.h> |
22 |
#include <mcmd/McmdEvent.h> |
23 |
#include <mcmd/McmdRecord.h> |
24 |
#include <RunHeaderEvent.h> |
25 |
#include <RunTrailerEvent.h> |
26 |
#include <CalibCalPedEvent.h> |
27 |
#include <CalibS4Event.h> |
28 |
#include <CalibTrk1Event.h> |
29 |
#include <CalibTrk2Event.h> |
30 |
#include <varDump/VarDumpEvent.h> |
31 |
#include <varDump/VarDumpRecord.h> |
32 |
#include <physics/S4/S4Event.h> |
33 |
|
34 |
extern "C" { |
35 |
#include <dirent.h> |
36 |
} |
37 |
|
38 |
using namespace log4cxx; |
39 |
using namespace std; |
40 |
using namespace pamela; |
41 |
using namespace yngn::YException; |
42 |
using namespace yngn::util; |
43 |
|
44 |
|
45 |
//extern TArrayD S4_paramfit(CalibS4Event *S4CalibEvent); |
46 |
/* |
47 |
* Received from Valeria Malvezzi 06/02/2006 |
48 |
*/ |
49 |
Double_t fitf(Double_t *x, Double_t *par){ |
50 |
|
51 |
Double_t fitval =(par[0]*x[0])+par[1]; |
52 |
return fitval; |
53 |
} |
54 |
|
55 |
/* |
56 |
* Received from Valeria Malvezzi 06/02/2006 |
57 |
*/ |
58 |
TArrayD S4_paramfit(pamela::CalibS4Event *S4CalibEvent){ |
59 |
|
60 |
//----------- variable initialization ------------------------------------------------- |
61 |
|
62 |
Double_t mip[3]={1, 30, 300}; |
63 |
Double_t adc[3] = {0}; |
64 |
TArrayD parametri(2); |
65 |
|
66 |
//------------ Fit calibrations and find parameters to calibrate data ------------------ |
67 |
pamela::S4::S4Event *s4Record; |
68 |
|
69 |
for (Int_t j = 0; j < 4; j++){ |
70 |
for (Int_t i = 0; i < 128; i++){ |
71 |
s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i)); |
72 |
switch (j) { |
73 |
case 0 :{ |
74 |
//calibS4_1->Fill(s4Record->S4_DATA); |
75 |
adc[0]=adc[0]+((s4Record->S4_DATA)-32); |
76 |
break; |
77 |
} |
78 |
case 1 :{ |
79 |
//calibS4_2->Fill(s4Record->S4_DATA); |
80 |
adc[1]=adc[1]+((s4Record->S4_DATA)-32); |
81 |
break; |
82 |
} |
83 |
case 3 :{ |
84 |
//calibS4_4->Fill(s4Record->S4_DATA); |
85 |
adc[2]=adc[2]+((s4Record->S4_DATA)-32); |
86 |
break; |
87 |
} |
88 |
} |
89 |
} |
90 |
} |
91 |
|
92 |
adc[0]=adc[0]/128; |
93 |
adc[1]=adc[1]/128; |
94 |
adc[2]=adc[2]/128; |
95 |
|
96 |
TGraph *fitpar = new TGraph (3, adc, mip); |
97 |
TF1 *func = new TF1("fitf", fitf, -0., 1000., 2); // definizione della funzione, 2 = num. parametri |
98 |
|
99 |
func->SetParameters(1,1); //inizializzazione dei parametri a 1 |
100 |
func->SetParNames("m","q"); //definisce il nome dei parametri |
101 |
fitpar->Fit(func,"r"); //fitta fitpar con la funzione func nel range definito nella funzione |
102 |
|
103 |
parametri[0] = func -> GetParameter(0); |
104 |
parametri[1] = func -> GetParameter(1); |
105 |
|
106 |
return parametri; |
107 |
} |
108 |
|
109 |
|
110 |
#define OK 1 |
111 |
#define NOK 0 |
112 |
|
113 |
static const char* RAW_FILE_NOT_FOUND = "Raw file not found in the database"; |
114 |
|
115 |
const char *PamelaDBOperations::TM_CALO_CALIB = "CREATE TEMPORARY TABLE `TM_CALO_CALIB` ( " |
116 |
" `ID` bigint(64) unsigned NOT NULL auto_increment, " |
117 |
" `ID_REGISTRY` bigint(64) unsigned NOT NULL default '0', " |
118 |
" `REG_EVENT` bigint(64) unsigned NOT NULL default '0', " |
119 |
" `TIME` bigint(64) unsigned NOT NULL default '0', " |
120 |
" `CAL_SECTION` tinyint(3) unsigned NOT NULL default '0', " |
121 |
" `CPERROR` int(32) unsigned NOT NULL default '0', " |
122 |
" `CSTWERR` smallint(8) unsigned NOT NULL default '0', " |
123 |
" PRIMARY KEY (`ID`) " |
124 |
" ) TYPE=MyISAM"; |
125 |
|
126 |
const char *PamelaDBOperations::TM_S4_CALIB = "CREATE TEMPORARY TABLE `TM_S4_CALIB` ( " |
127 |
" `ID` bigint(64) unsigned NOT NULL auto_increment, " |
128 |
" `ID_REGISTRY` bigint(64) unsigned NOT NULL default '0', " |
129 |
" `REG_EVENT` bigint(64) unsigned NOT NULL default '0', " |
130 |
" `TIME` bigint(64) unsigned NOT NULL default '0', " |
131 |
" `PARAM_FIT0` float unsigned NOT NULL default '0', " |
132 |
" `PARAM_FIT1` float unsigned NOT NULL default '0', " |
133 |
" PRIMARY KEY (`ID`) " |
134 |
" ) TYPE=MyISAM"; |
135 |
|
136 |
const char *PamelaDBOperations::TM_TRK_CALIB = "CREATE TEMPORARY TABLE `TM_TRK_CALIB` ( " |
137 |
" `ID` bigint(64) unsigned NOT NULL auto_increment, " |
138 |
" `ID_REGISTRY` bigint(64) unsigned NOT NULL default '0', " |
139 |
" `REG_EVENT` bigint(64) unsigned NOT NULL default '0', " |
140 |
" `TIME` bigint(64) unsigned NOT NULL default '0', " |
141 |
" `TRK_PART` smallint(8) unsigned NOT NULL default '0', " |
142 |
" `PCK_COUNTER` bigint(64) unsigned NOT NULL default '0', " |
143 |
" PRIMARY KEY (`ID`) " |
144 |
" ) TYPE=MyISAM"; |
145 |
|
146 |
const char *PamelaDBOperations::TM_REGISTRY = "CREATE TEMPORARY TABLE `TM_REGISTRY` ( " |
147 |
" `ID` bigint(64) unsigned NOT NULL auto_increment, " |
148 |
" `REG_EVENT` bigint(64) unsigned NOT NULL default '0', " |
149 |
" `TIME` bigint(64) unsigned NOT NULL default '0', " |
150 |
" PRIMARY KEY (`ID`) " |
151 |
" ) TYPE=MyISAM"; |
152 |
|
153 |
const char *PamelaDBOperations::TM_HEADER = "CREATE TEMPORARY TABLE `TM_HEADER` ( " |
154 |
" `ID` bigint(20) NOT NULL auto_increment, " |
155 |
" `ID_ROOT` bigint(20) unsigned NOT NULL default '0', " |
156 |
" `COUNTER` int(10) unsigned NOT NULL default '0', " |
157 |
" `OBT` int(10) unsigned NOT NULL default '0', " |
158 |
" `EVENT` int(10) unsigned NOT NULL default '0', " |
159 |
" `TIME` bigint(20) unsigned NOT NULL default '0', " |
160 |
" `TREE_NAME` varchar(50), " |
161 |
" PRIMARY KEY (`ID`) " |
162 |
" ) TYPE=MyISAM "; |
163 |
|
164 |
const char *PamelaDBOperations::TM_TIMESYNC = "CREATE TEMPORARY TABLE `TM_TIMESYNC` (" |
165 |
" `ID` bigint(20) unsigned NOT NULL auto_increment," |
166 |
" `TS_OBT` bigint(20) unsigned NOT NULL default '0'," |
167 |
" `TS_TIME` bigint(20) unsigned NOT NULL default '0'," |
168 |
" `TYPE` smallint(5) unsigned NOT NULL default '0'," |
169 |
" `ID_HEADER` int(10) unsigned NOT NULL default '0'," |
170 |
" `ID_RAW` int(10) unsigned NOT NULL default '0'," |
171 |
" PRIMARY KEY (`ID`)" |
172 |
" ) TYPE=MyISAM "; |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
static LoggerPtr logger = Logger::getLogger(_T("PamelaDBOperation")); |
179 |
|
180 |
/** |
181 |
* Insert a new row into GL_RAW table. Throw exception if (conn == 0) or the SQL command went wrong. |
182 |
* @param conn An active connection to a suitable (for PAMELA) database. |
183 |
* @param pathName The path to the raw file. |
184 |
* @param fileName The name of the raw file. |
185 |
* @param symbolicName An optional parameter. |
186 |
*/ |
187 |
bool PamelaDBOperations::insertPamelaRawFile(TSQLServer *conn, const char *pathName, const char *fileName) |
188 |
throw (YSQLNotConnectedException, YSQLQueryException, YSQLDuplicateRowException){ |
189 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
190 |
const char *id = PamelaDBOperations::getID_RAW(conn, pathName, fileName); |
191 |
stringstream oss; |
192 |
TSQLResult *result = 0; |
193 |
|
194 |
if (id != NULL) throw YSQLDuplicateRowException("Existing RawFile"); |
195 |
|
196 |
oss.str(""); |
197 |
oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" |
198 |
<< pathName << "', '" << fileName << "')"; |
199 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
200 |
return OK; |
201 |
} |
202 |
|
203 |
|
204 |
/** |
205 |
* Insert a new row into GL_ROOT table. It's private beacuse does not perform cross check on idRaw. |
206 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
207 |
* @param conn An active connection to a suitable (for PAMELA) database. |
208 |
* @param pathName The path to the root file. |
209 |
* @param fileName The name of the root file. |
210 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
211 |
*/ |
212 |
int PamelaDBOperations::insertPamelaRootFiles(TSQLServer *conn, const char *pathName, const char *fileName, const char *idRaw) |
213 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
214 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
215 |
stringstream oss; |
216 |
|
217 |
oss.str(""); |
218 |
oss << "INSERT INTO GL_ROOT (ID_RAW, PATH, NAME) VALUES ('" |
219 |
<< idRaw << "', '" << pathName << "', '" << fileName << "')"; |
220 |
//cout << oss.str().c_str() << "\n" ; |
221 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
222 |
return OK; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Insert all the new rows into GL_ROOT. |
227 |
* The raw file indicates in the parameters should be already been stored in the database. |
228 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
229 |
* @param conn An active connection to a suitable (for PAMELA) database. |
230 |
* @param pathName The path to the root file. |
231 |
* @param fileName The name of the root file. |
232 |
* @param rawPathName The path to the raw file used to generate the root files. |
233 |
* @param rawFileName The name of the raw file used to generate the root files. |
234 |
*/ |
235 |
int PamelaDBOperations::insertPamelaRootFiles(TSQLServer *conn, const char *pathName, const char *fileName, const char *rawPathName, const char *rawFileName) |
236 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
237 |
|
238 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
239 |
stringstream oss; |
240 |
TSQLResult *result = 0; |
241 |
TSQLRow *row = 0; |
242 |
|
243 |
oss.str(""); |
244 |
oss << " SELECT COUNT(GL_ROOT.ID_RAW), GL_RAW.ID FROM GL_RAW " |
245 |
<< " LEFT JOIN GL_ROOT " |
246 |
<< " ON GL_RAW.ID = GL_ROOT.ID_RAW " |
247 |
<< " WHERE GL_RAW.PATH = '" << rawPathName << "' AND " |
248 |
<< " GL_RAW.NAME = '" << rawFileName << "' GROUP BY GL_RAW.ID "; |
249 |
//cout << oss.str().c_str() << "\n"; |
250 |
result = conn->Query(oss.str().c_str()); |
251 |
row = result->Next(); |
252 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("ROOT file already inserted!"); |
253 |
|
254 |
|
255 |
//if ((row != NULL) && (atol(row->GetField(0))) == 0){ |
256 |
//cout << "pathName: " << pathName << " count: " << row->GetField(0) << " ID: " << row->GetField(1) << "\n"; |
257 |
//conn->Query("BEGIN WORK"); |
258 |
if (insertPamelaRootFiles(conn, pathName, fileName, row->GetField(1))) { |
259 |
//conn->Query("COMMIT"); |
260 |
} else { |
261 |
//conn->Query("ROLLBACK"); |
262 |
//} |
263 |
} |
264 |
return OK; |
265 |
} |
266 |
|
267 |
|
268 |
|
269 |
int PamelaDBOperations::insertPamelaTM_HEADER(TSQLServer *conn, const char *idRaw) |
270 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
271 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
272 |
conn->Query(PamelaDBOperations::TM_HEADER); |
273 |
stringstream oss; |
274 |
TSQLResult *result = 0; |
275 |
|
276 |
/*Took all the root file related to a specific raw file*/ |
277 |
oss.str(""); |
278 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
279 |
//cout << oss.str().c_str() << "\n"; |
280 |
result = conn->Query(oss.str().c_str()); |
281 |
if (result == 0) throw YSQLQueryException(oss.str().c_str()); |
282 |
TSQLRow *row = 0; |
283 |
TFile *file = 0; |
284 |
TTree *tr = 0; |
285 |
EventHeader *eh = 0; |
286 |
PscuHeader *ph = 0; |
287 |
ULong64_t nevents = 0; |
288 |
const char *idRoot = 0; |
289 |
row = result->Next(); |
290 |
if (row == 0) return NOK; |
291 |
|
292 |
oss.str(""); |
293 |
oss << row->GetField(1) << "/" << row->GetField(2); |
294 |
idRoot = row->GetField(0); |
295 |
//cout << "Sto per aprire il file: " << oss.str().c_str() << "\n"; |
296 |
file = YFile::Open(oss.str().c_str()); |
297 |
|
298 |
pcksList packetsNames; |
299 |
pcksList::iterator Iter; |
300 |
getPacketsNames(packetsNames); |
301 |
for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ |
302 |
tr = (TTree*)file->Get(*Iter); |
303 |
nevents = tr->GetEntries(); |
304 |
tr->SetBranchAddress("Header", &eh); |
305 |
for (UInt_t i = 0; i < nevents; i++){ |
306 |
tr->GetEntry(i); |
307 |
ph = eh->GetPscuHeader(); |
308 |
oss.str(""); |
309 |
oss << "INSERT INTO TM_HEADER (COUNTER, EVENT, ID_ROOT, OBT, TREE_NAME) VALUES ('" |
310 |
<< ph->Counter << "', '" << i << "', '" << idRoot << "', '" |
311 |
<< ph->OrbitalTime << "', '" << *Iter <<"')"; |
312 |
//cout << oss.str().c_str() << "\n"; |
313 |
if (conn->Query(oss.str().c_str()) == 0) { |
314 |
//delete result; |
315 |
throw YSQLQueryException(oss.str().c_str()); |
316 |
} |
317 |
} |
318 |
} |
319 |
return OK; |
320 |
} |
321 |
|
322 |
|
323 |
|
324 |
/** |
325 |
* Collect all the timesyncs data contained in the specified raw file and |
326 |
* fill the GL_TIMESYNC table. |
327 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
328 |
* @param conn An active connection to a suitable (for PAMELA) database. |
329 |
* @param rawPathName The path to the raw file. |
330 |
* @param rawFileName The name of the raw file. |
331 |
*/ |
332 |
int PamelaDBOperations::insertPamelaGL_TIMESYNC(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
333 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
334 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
335 |
const char *id = PamelaDBOperations::getID_RAW(conn, rawPathName, rawFileName); |
336 |
stringstream oss; |
337 |
|
338 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
339 |
/* |
340 |
* Verify that the TIMESYNC have been not already processed |
341 |
*/ |
342 |
oss.str(""); |
343 |
oss << " SELECT COUNT(GL_TIMESYNC.ID) FROM GL_TIMESYNC " |
344 |
<< " LEFT JOIN GL_RAW " |
345 |
<< " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW " |
346 |
<< " WHERE GL_TIMESYNC.ID_RAW = " << id; |
347 |
TSQLResult *result = conn->Query(oss.str().c_str()); |
348 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
349 |
TSQLRow *row = result->Next(); |
350 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("TIMESYNC already existing!"); |
351 |
|
352 |
try { |
353 |
insertPamelaGL_TIMESYNC(conn, id); |
354 |
} catch (YSQLQueryException exc) { |
355 |
cout << "Eccezione! " << exc.print() << "\n"; |
356 |
return NOK; |
357 |
} catch (YSQLNotConnectedException exc) { |
358 |
cout << "Eccezione! " << exc.print() << "\n"; |
359 |
return NOK; |
360 |
} |
361 |
return OK; |
362 |
} |
363 |
|
364 |
int PamelaDBOperations::insertPamelaGL_TIMESYNC(TSQLServer *conn, const char *idRaw) throw (YSQLNotConnectedException, YSQLQueryException){ |
365 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
366 |
conn->Query(PamelaDBOperations::TM_TIMESYNC); |
367 |
stringstream oss; |
368 |
|
369 |
TSQLResult *result = 0; |
370 |
TSQLResult *secResult = 0; |
371 |
|
372 |
oss.str(""); |
373 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
374 |
result = conn->Query(oss.str().c_str()); |
375 |
//cout << oss.str().c_str() << "\n"; |
376 |
//if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
377 |
if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
378 |
TSQLRow *row = 0; |
379 |
TFile *file = 0; |
380 |
TTree *tr = 0; |
381 |
McmdEvent *mcmdev = 0; |
382 |
McmdRecord *mcmdrc = 0; |
383 |
TArrayC *mcmddata = 0; |
384 |
RunHeaderEvent *runheader = 0; |
385 |
RunTrailerEvent *runtrailer = 0; |
386 |
const char *idRoot = 0; |
387 |
const char *idHeader = 0; |
388 |
ULong64_t nevents = 0; |
389 |
ULong64_t timesync = 0; |
390 |
string name; |
391 |
|
392 |
row = result->Next(); |
393 |
while (!(row == 0)){ |
394 |
oss.str(""); |
395 |
oss << row->GetField(1) << "/" << row->GetField(2); |
396 |
//cout << oss.str().c_str() << "\n"; |
397 |
name = row->GetField(2); |
398 |
idRoot = row->GetField(0); |
399 |
file = YFile::Open(oss.str().c_str()); |
400 |
|
401 |
/* |
402 |
* process RunHeader |
403 |
*/ |
404 |
tr = (TTree*)file->Get("RunHeader"); |
405 |
nevents = tr->GetEntries(); |
406 |
tr->SetBranchAddress("RunHeader", &runheader); |
407 |
for (UInt_t i = 0; i < nevents; i++){ |
408 |
tr->GetEntry(i); |
409 |
idHeader = "000"; |
410 |
oss.str(""); |
411 |
oss << "INSERT INTO TM_TIMESYNC (ID_HEADER, TS_OBT, TS_TIME, TYPE, ID_RAW) VALUES ('" |
412 |
<< idHeader << "', '" << dec << runheader->OBT_TIME_SYNC << "', '" |
413 |
<< dec << runheader->LAST_TIME_SYNC_INFO |
414 |
<< "', 0x20, '" << idRaw << "')"; |
415 |
//cout << oss.str().c_str() << "\n"; |
416 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
417 |
} |
418 |
|
419 |
/* |
420 |
* process RunTrailer |
421 |
*/ |
422 |
tr = (TTree*)file->Get("RunTrailer"); |
423 |
nevents = tr->GetEntries(); |
424 |
tr->SetBranchAddress("RunTrailer", &runtrailer); |
425 |
for (UInt_t i = 0; i < nevents; i++){ |
426 |
tr->GetEntry(i); |
427 |
idHeader = "000"; |
428 |
oss.str(""); |
429 |
oss << "INSERT INTO TM_TIMESYNC (ID_HEADER, TS_OBT, TS_TIME, TYPE, ID_RAW) VALUES ('" |
430 |
<< idHeader << "', '" << dec << runtrailer->OBT_TYME_SYNC << "', '" |
431 |
<< dec << runtrailer->LAST_TYME_SYNC_INFO |
432 |
<< "', 0x21, '" << idRaw << "')"; |
433 |
//cout << oss.str().c_str() << "\n"; |
434 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
435 |
} |
436 |
|
437 |
/* |
438 |
* process Mcmd |
439 |
*/ |
440 |
long int recEntries; |
441 |
tr = (TTree*)file->Get("Mcmd"); |
442 |
tr->SetBranchAddress("Mcmd", &mcmdev); |
443 |
nevents = tr->GetEntries(); |
444 |
for (UInt_t i = 0; i < nevents; i++){ |
445 |
tr->GetEntry(i); |
446 |
recEntries = mcmdev->Records->GetEntries(); |
447 |
for (int j = 0; j < recEntries; j++){ |
448 |
mcmdrc = (McmdRecord*)mcmdev->Records->At(j); |
449 |
if (mcmdrc->ID1 == 0xE0){ |
450 |
mcmddata = mcmdrc->McmdData; |
451 |
timesync = (((ULong64_t)mcmddata->At(0)<<24)&0xFF000000) + |
452 |
(((ULong64_t)mcmddata->At(1)<<16)&0x00FF0000) + |
453 |
(((ULong64_t)mcmddata->At(2)<<8)&0x0000FF00) + |
454 |
(((ULong64_t)mcmddata->At(3))&0x000000FF); |
455 |
idHeader = "000"; |
456 |
oss.str(""); |
457 |
oss << "INSERT INTO TM_TIMESYNC (ID_HEADER, TS_OBT, TS_TIME, TYPE, ID_RAW) VALUES ('" |
458 |
<< idHeader << "', '" << dec << mcmdrc->MCMD_RECORD_OBT << "', '" |
459 |
<< dec << timesync << "', 0x55, '" << idRaw << "')"; |
460 |
//cout << oss.str().c_str() << "\n"; |
461 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
462 |
} |
463 |
} |
464 |
} |
465 |
file->Close(); |
466 |
row = result->Next(); |
467 |
} |
468 |
|
469 |
oss.str(""); |
470 |
oss << "select TS_OBT, TS_TIME " |
471 |
<< " FROM TM_TIMESYNC " |
472 |
<< " WHERE " |
473 |
<< " ID_RAW = " << idRaw << " AND " |
474 |
<< " ((TS_OBT <> 0) OR (TS_TIME <> 0)) AND " |
475 |
<< " TYPE = 0x55 "; |
476 |
//cout << oss.str().c_str() << "\n"; |
477 |
secResult = conn->Query(oss.str().c_str()); |
478 |
Long64_t sizeMcmdTS = secResult->GetRowCount(); |
479 |
double *mcmdTS = new double[sizeMcmdTS]; |
480 |
int index = 0; |
481 |
double tho = 1./1000.; |
482 |
row = secResult->Next(); |
483 |
while (!(row == 0)) { |
484 |
if ((row->GetField(0) != NULL) && (row->GetField(1) != NULL)){ |
485 |
mcmdTS[index++] = atof(row->GetField(1)) - atof(row->GetField(0))*tho; |
486 |
} |
487 |
row = secResult->Next(); |
488 |
} |
489 |
|
490 |
oss.str(""); |
491 |
oss << "select TS_OBT, TS_TIME " |
492 |
<< " FROM TM_TIMESYNC " |
493 |
<< " WHERE " |
494 |
<< " ID_RAW = " << idRaw << " AND " |
495 |
<< " ((TS_OBT <> 0) OR (TS_TIME <> 0)) AND " |
496 |
<< " TYPE = 0x20 "; |
497 |
//cout << oss.str().c_str() << "\n"; |
498 |
secResult = conn->Query(oss.str().c_str()); |
499 |
Long64_t sizeHead = secResult->GetRowCount(); |
500 |
double *runHead = new double[sizeHead]; |
501 |
index = 0; |
502 |
row = secResult->Next(); |
503 |
while (!(row == 0)) { |
504 |
if ((row->GetField(1) != NULL) && (row->GetField(0) != NULL)){ |
505 |
runHead[index++] = atof(row->GetField(1)) - atof(row->GetField(0)); |
506 |
} |
507 |
row = secResult->Next(); |
508 |
} |
509 |
|
510 |
oss.str(""); |
511 |
oss << "select TS_OBT, TS_TIME " |
512 |
<< " FROM TM_TIMESYNC " |
513 |
<< " WHERE " |
514 |
<< " ID_RAW = " << idRaw << " AND " |
515 |
<< " ((TS_OBT <> 0) OR (TS_TIME <> 0)) AND " |
516 |
<< " TYPE = 0x21 "; |
517 |
//cout << oss.str().c_str() << "\n"; |
518 |
secResult = conn->Query(oss.str().c_str()); |
519 |
Long64_t sizeTrail = secResult->GetRowCount(); |
520 |
double *runTrail = new double[sizeTrail]; |
521 |
index = 0; |
522 |
row = secResult->Next(); |
523 |
while (!(row == 0)) { |
524 |
if ((row->GetField(1) != NULL) && (row->GetField(0) != NULL)){ |
525 |
runTrail[index++] = atof(row->GetField(1)) - atof(row->GetField(0)); |
526 |
} |
527 |
row = secResult->Next(); |
528 |
} |
529 |
|
530 |
oss.str(""); |
531 |
oss << "INSERT INTO GL_TIMESYNC (DELTA_OBT_TMSYNC, RMS_DELTA, TYPE, ID_RAW) VALUES ('" |
532 |
<< dec << TMath::Mean(sizeMcmdTS,mcmdTS)*1000. << "', '" << dec << TMath::RMS(sizeMcmdTS, mcmdTS)*1000. << "', " |
533 |
<< " 0x55, '" << idRaw << "')"; |
534 |
//cout << oss.str().c_str() << "\n"; |
535 |
conn->Query(oss.str().c_str()); |
536 |
|
537 |
oss.str(""); |
538 |
oss << "INSERT INTO GL_TIMESYNC (DELTA_OBT_TMSYNC, RMS_DELTA, TYPE, ID_RAW) VALUES ('" |
539 |
<< dec << TMath::Mean(sizeHead,runHead)*1000. << "', '" << dec << TMath::RMS(sizeHead,runHead)*1000. << "', " |
540 |
<< " 0x20, '" << idRaw << "')"; |
541 |
//cout << oss.str().c_str() << "\n"; |
542 |
conn->Query(oss.str().c_str()); |
543 |
|
544 |
oss.str(""); |
545 |
oss << "INSERT INTO GL_TIMESYNC (DELTA_OBT_TMSYNC, RMS_DELTA, TYPE, ID_RAW) VALUES ('" |
546 |
<< dec << TMath::Mean(sizeTrail,runTrail)*1000. << "', '" << dec << TMath::RMS(sizeTrail,runTrail)*1000. << "', " |
547 |
<< " 0x21, '" << idRaw << "')"; |
548 |
//cout << oss.str().c_str() << "\n"; |
549 |
conn->Query(oss.str().c_str()); |
550 |
|
551 |
oss.str(""); |
552 |
oss << " DROP TABLE IF EXISTS TM_TIMESYNC "; |
553 |
// GOOD FOR MySQL ver > 3.2 |
554 |
//oss << " DROP TEMPORARY TABLE IF EXISTS TM_TIMESYNC "; |
555 |
conn->Query(oss.str().c_str()); |
556 |
|
557 |
return OK; |
558 |
} |
559 |
|
560 |
|
561 |
/** |
562 |
* Delete the duplicate row in TM_HEADER and TM_TIMESYNC |
563 |
* generated by the specified raw file and fill the TM_TIMESYNC table. |
564 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
565 |
* @param conn An active connection to a suitable (for PAMELA) database. |
566 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
567 |
*/ |
568 |
int PamelaDBOperations::deletePamelaDuplicate(TSQLServer *conn, const char *idRaw) |
569 |
throw (YSQLNotConnectedException, YSQLQueryException) { |
570 |
|
571 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
572 |
stringstream oss; |
573 |
|
574 |
TSQLResult *result = 0; |
575 |
TSQLRow *row = 0; |
576 |
bool dup = true; |
577 |
//---------------- Clean up the multiple rows in TM_HEADER --------------------------------------- |
578 |
/* |
579 |
* Select the duplicate COUNTER-OBT pairs into a holding table |
580 |
*/ |
581 |
oss.str(""); |
582 |
oss << " CREATE TEMPORARY TABLE holdkey " |
583 |
<< " SELECT ID, OBT, COUNTER, ID_ROOT, count(*) " |
584 |
<< " FROM TM_HEADER " |
585 |
<< " WHERE ID_ROOT = " << idRaw |
586 |
<< " GROUP BY OBT, COUNTER" |
587 |
<< " HAVING count(*) > 1"; |
588 |
//cout << oss.str().c_str() << "\n"; |
589 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
590 |
|
591 |
/* |
592 |
* Select the duplicate rows into a holding table, eliminating duplicates in the process |
593 |
*/ |
594 |
oss.str(""); |
595 |
oss << " CREATE TEMPORARY TABLE holddups " |
596 |
<< " SELECT DISTINCT TM_HEADER.* " |
597 |
<< " FROM TM_HEADER, holdkey " |
598 |
<< " WHERE TM_HEADER.COUNTER = holdkey.COUNTER " |
599 |
<< " AND TM_HEADER.OBT = holdkey.OBT "; |
600 |
//cout << oss.str().c_str() << "\n"; |
601 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
602 |
|
603 |
/* |
604 |
* Verify that holddup contains only one row for each pair |
605 |
*/ |
606 |
oss.str(""); |
607 |
oss << " SELECT ID, COUNTER, ID_ROOT, count(*) " |
608 |
<< " FROM holddups " |
609 |
<< " GROUP BY COUNTER"; |
610 |
//cout << oss.str().c_str() << "\n"; |
611 |
result = conn->Query(oss.str().c_str()); |
612 |
if (result == 0) throw YSQLQueryException(oss.str().c_str()); |
613 |
|
614 |
dup = true; |
615 |
while (true) { |
616 |
row = result->Next(); |
617 |
if (row == 0) break; |
618 |
if ((!atol(row->GetField(3)) == 0)){ |
619 |
dup = false; |
620 |
break; |
621 |
} |
622 |
} |
623 |
|
624 |
if (dup){ |
625 |
/* |
626 |
* Delete the duplicate rows from TM_HEADER |
627 |
*/ |
628 |
oss.str(""); |
629 |
oss << " DELETE TM_HEADER " |
630 |
<< " FROM TM_HEADER, holdkey " |
631 |
<< " WHERE TM_HEADER.COUNTER = holdkey.COUNTER " |
632 |
<< " AND TM_HEADER.ID_ROOT = holdkey.ID_ROOT "; |
633 |
//cout << oss.str().c_str() << "\n"; |
634 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
635 |
conn->Query(oss.str().c_str()); |
636 |
|
637 |
/* |
638 |
* Put the unique rows back in TM_HEADER |
639 |
*/ |
640 |
oss.str(""); |
641 |
oss << " INSERT TM_HEADER SELECT * FROM holddups "; |
642 |
//cout << oss.str().c_str() << "\n"; |
643 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
644 |
conn->Query(oss.str().c_str()); |
645 |
} |
646 |
|
647 |
/* |
648 |
* Delete the temporary Tables |
649 |
*/ |
650 |
oss.str(""); |
651 |
oss << " DROP TABLE IF EXISTS holdkey "; |
652 |
// GOOD FOR MySQL ver > 3.2 |
653 |
//oss << " DROP TEMPORARY TABLE IF EXISTS holdkey "; |
654 |
//cout << oss.str().c_str() << "\n"; |
655 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
656 |
conn->Query(oss.str().c_str()); |
657 |
oss.str(""); |
658 |
oss << " DROP TABLE IF EXISTS holddups "; |
659 |
// GOOD FOR MySQL ver > 3.2 |
660 |
//oss << " DROP TEMPORARY TABLE IF EXISTS holddups "; |
661 |
//cout << oss.str().c_str() << "\n"; |
662 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
663 |
conn->Query(oss.str().c_str()); |
664 |
//---------------- Clean up the multiple rows in TM_HEADER --------------------------------------- |
665 |
return OK; |
666 |
} |
667 |
|
668 |
|
669 |
/** |
670 |
* Assign the collectied TIMESYNCs to the T_HEADERs row relative to the |
671 |
* same ID_ROOT unpacked data. |
672 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
673 |
* @param conn An active connection to a suitable (for PAMELA) database. |
674 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
675 |
*/ |
676 |
int PamelaDBOperations::assignPamelaTIMESYNC(TSQLServer *conn, const char *idRaw) |
677 |
throw (YSQLNotConnectedException, YSQLQueryException) { |
678 |
|
679 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
680 |
stringstream oss; |
681 |
|
682 |
TSQLResult *result = NULL, *secResult = NULL; |
683 |
TSQLRow *row = NULL, *secRow = NULL; |
684 |
ULong64_t syncTIME = 0, absTime = 0; |
685 |
|
686 |
/* In any case this parameter can be changed to set the timesync scale*/ |
687 |
unsigned long id_Raw = (unsigned long)atol(idRaw); |
688 |
bool validTS = false; |
689 |
int types[] = {0x55, 0x20, 0x21}; |
690 |
Long64_t boot_Num = -1; |
691 |
/*--------------------------------------------------*/ |
692 |
|
693 |
oss.str(""); |
694 |
oss << " SELECT COUNT(GL_TIMESYNC.DELTA_OBT_TMSYNC), GL_RAW.BOOT_NUMBER, SUM(GL_TIMESYNC.DELTA_OBT_TMSYNC) from GL_TIMESYNC, GL_RAW " |
695 |
<< " WHERE GL_TIMESYNC.ID_RAW = GL_RAW.ID AND GL_TIMESYNC.ID_RAW = " << dec << id_Raw |
696 |
<< " GROUP BY GL_TIMESYNC.ID_RAW"; |
697 |
// << " AND GL_RAW.BOOT_NUMBER IS NOT NULL GROUP BY GL_TIMESYNC.ID_RAW"; |
698 |
//cout << oss.str().c_str() << "\n"; |
699 |
result = conn->Query(oss.str().c_str()); |
700 |
if (result == 0) throw YSQLQueryException("result: No Timesync!"); |
701 |
row = result->Next(); |
702 |
if (row == 0) throw YSQLQueryException("raw: No Timesync!"); |
703 |
if (row->GetField(1) != 0) boot_Num = atol(row->GetField(1)); |
704 |
//cout << "Questo è il boot number: " << dec << boot_Num; |
705 |
|
706 |
|
707 |
|
708 |
//Get the list of the id_raw belonging to the same BOOT_NUMBER. This have been done to foreseen |
709 |
//situation where a file does not contain a valid timesync, BUT HOPEFULLY A VALID BOOT NUMBER! |
710 |
if ((atol(row->GetField(0)) > 0) && (atol(row->GetField(2)) == 0)){ |
711 |
oss.str(""); |
712 |
oss << " SELECT ID from GL_RAW " |
713 |
<< " WHERE GL_RAW.BOOT_NUMBER = " << dec << boot_Num ; |
714 |
//cout << oss.str().c_str() << "\n"; |
715 |
secResult = conn->Query(oss.str().c_str()); |
716 |
} |
717 |
|
718 |
if (atol(row->GetField(0)) > 0){ |
719 |
//Looks for a valid TIMESYNC (before 0x55, then 0x20 and finally 0x21) |
720 |
while (true){ |
721 |
for(int ty = 0; ty < 3; ty++){ |
722 |
oss.str(""); |
723 |
oss << " SELECT COUNT(GL_TIMESYNC.DELTA_OBT_TMSYNC) from GL_TIMESYNC " |
724 |
<< " WHERE GL_TIMESYNC.ID_RAW = '" << dec << id_Raw << "' AND GL_TIMESYNC.TYPE = " << dec << types[ty] |
725 |
<< " AND GL_TIMESYNC.DELTA_OBT_TMSYNC <> 0"; |
726 |
//cout << oss.str().c_str() << "\n"; |
727 |
result = conn->Query(oss.str().c_str()); |
728 |
row = result->Next(); |
729 |
//Has found a good TIMESYNC? |
730 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)){ |
731 |
oss.str(""); |
732 |
oss << " SELECT GL_TIMESYNC.DELTA_OBT_TMSYNC from GL_TIMESYNC " |
733 |
<< " WHERE " |
734 |
<< " GL_TIMESYNC.ID_RAW = " << dec << id_Raw << " AND " |
735 |
<< " GL_TIMESYNC.TYPE = " << dec << types[ty]; |
736 |
//cout << oss.str().c_str() << "\n"; |
737 |
result = conn->Query(oss.str().c_str()); |
738 |
row = result->Next(); |
739 |
if (row == 0) throw YSQLQueryException("No Timesync!"); |
740 |
syncTIME = (ULong64_t)atof(row->GetField(0)); |
741 |
validTS = true; |
742 |
break; |
743 |
} |
744 |
} |
745 |
|
746 |
//Should exits from the loop? |
747 |
if (validTS) break; |
748 |
|
749 |
//Get another id_raw |
750 |
if (secResult != NULL) { |
751 |
secRow = secResult->Next(); |
752 |
if (secRow == NULL) throw YSQLQueryException("No Timesync!"); |
753 |
id_Raw = (ULong64_t)atof(secRow->GetField(0)); |
754 |
} |
755 |
|
756 |
} |
757 |
|
758 |
} |
759 |
|
760 |
//Collect the TM_HEADER to assign them the correct TIMESYNC |
761 |
oss.str(""); |
762 |
oss << " SELECT TM_HEADER.OBT, TM_HEADER.ID" |
763 |
<< " FROM GL_ROOT, TM_HEADER, GL_RAW " |
764 |
<< " WHERE " |
765 |
<< " GL_ROOT.ID_RAW = " << idRaw << " AND " |
766 |
<< " TM_HEADER.TIME = 0 AND " |
767 |
<< " TM_HEADER.ID_ROOT = GL_ROOT.ID "; |
768 |
//cout << oss.str().c_str() << "\n"; |
769 |
result = conn->Query(oss.str().c_str()); |
770 |
if (result == 0) throw YSQLQueryException(oss.str().c_str()); |
771 |
|
772 |
//Calculate the absolute time for the GL_HEADER item |
773 |
while (true) { |
774 |
row = result->Next(); |
775 |
if (row == 0) break; |
776 |
//diff = 1; |
777 |
//if (syncOBT < (unsigned long)atol(row->GetField(0))) diff = -1; |
778 |
//absTime = (syncTIME + konstant)*1000 + diff*((unsigned long)atol(row->GetField(0)) - syncOBT); |
779 |
absTime = (ULong64_t)atof(row->GetField(0)) + syncTIME; |
780 |
oss.str(""); |
781 |
oss << " UPDATE TM_HEADER " |
782 |
<< " SET TM_HEADER.TIME = '" << absTime << "'" |
783 |
<< " WHERE TM_HEADER.ID = '" << row->GetField(1) << "'"; |
784 |
//cout << oss.str().c_str() << "\n"; |
785 |
if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
786 |
} |
787 |
return OK; |
788 |
} |
789 |
|
790 |
|
791 |
|
792 |
/** |
793 |
* Create the Registry files connected to the processed unpaking. |
794 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
795 |
* @param conn An active connection to a suitable (for PAMELA) database. |
796 |
* @param pathName The path to the root file. |
797 |
* @param fileName The name of the root file. |
798 |
* @param rawPathName The path to the raw file used to generate the root files. |
799 |
* @param rawFileName The name of the raw file used to generate the root files. |
800 |
*/ |
801 |
int PamelaDBOperations::createPamelaRegistry(TSQLServer *conn, const char *rawPathName, const char *rawFileName, bool overwrite = false) |
802 |
throw (YSQLNotConnectedException, YSQLQueryException, YNotExistingFileException, YExistingFileException){ |
803 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
804 |
const char *id = PamelaDBOperations::getID_RAW(conn, rawPathName, rawFileName); |
805 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
806 |
|
807 |
try { |
808 |
createPamelaRegistry(conn, id, overwrite); |
809 |
} catch (YSQLQueryException exc) { |
810 |
cout << "Eccezione! " << exc.print() << "\n"; |
811 |
return NOK; |
812 |
} catch (YSQLNotConnectedException exc) { |
813 |
cout << "Eccezione! " << exc.print() << "\n"; |
814 |
return NOK; |
815 |
} catch (YNotExistingFileException exc) { |
816 |
cout << "Eccezione! " << exc.print() << "\n"; |
817 |
return NOK; |
818 |
} |
819 |
|
820 |
return OK; |
821 |
} |
822 |
|
823 |
/** |
824 |
* Create the Registry files connected to the processed unpaking. |
825 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
826 |
* @param conn An active connection to a suitable (for PAMELA) database. |
827 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
828 |
*/ |
829 |
int PamelaDBOperations::createPamelaRegistry(TSQLServer *conn, const char *idRaw, bool overwrite = false) |
830 |
throw (YSQLNotConnectedException, YSQLQueryException, YNotExistingFileException, YExistingFileException) { |
831 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
832 |
|
833 |
///TBD: Put the check "IF THE REGISTRY ALREDY EXISTS" before this try. |
834 |
try { |
835 |
if (insertPamelaTM_HEADER(conn, idRaw)){ |
836 |
deletePamelaDuplicate(conn, idRaw); |
837 |
assignPamelaTIMESYNC(conn, idRaw); |
838 |
} |
839 |
} catch (YSQLQueryException exc) { |
840 |
cout << "Eccezione! " << exc.print() << "\n"; |
841 |
return NOK; |
842 |
} catch (YSQLNotConnectedException exc) { |
843 |
cout << "Eccezione! " << exc.print() << "\n"; |
844 |
return NOK; |
845 |
} |
846 |
|
847 |
stringstream oss; |
848 |
TSQLResult *result, *secResult; |
849 |
TSQLRow *row, *secRow; |
850 |
|
851 |
const char *idRoot; |
852 |
string registryName; |
853 |
RegistryEvent *registry = new RegistryEvent(); |
854 |
TFile *file = 0; |
855 |
TTree *tr = 0; |
856 |
TBranch *branch = 0; |
857 |
cOrbit *orbit = 0; |
858 |
|
859 |
/*Took all the root file related to a specific raw file*/ |
860 |
oss.str(""); |
861 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
862 |
result = conn->Query(oss.str().c_str()); |
863 |
//cout << oss.str().c_str() << "\n"; |
864 |
//if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
865 |
row = result->Next(); |
866 |
if (row == 0) return NOK; |
867 |
|
868 |
idRoot = row->GetField(0); |
869 |
oss.str(""); |
870 |
oss << row->GetField(1) << "/" << row->GetField(2); |
871 |
file = YFile::Open(oss.str().c_str(), "UPDATE"); |
872 |
pcksList packetsNames; |
873 |
pcksList::iterator Iter; |
874 |
getPacketsNames(packetsNames); |
875 |
|
876 |
for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ |
877 |
//cout << "Tree Name: " << *Iter << "\n"; |
878 |
tr = (TTree*)file->Get(*Iter); |
879 |
|
880 |
//Branch Registry exist? |
881 |
if (!tr->GetBranchStatus("Registry")){ |
882 |
branch = tr->Branch("Registry", "pamela::RegistryEvent", ®istry); |
883 |
} else { |
884 |
if (!overwrite) throw YExistingFileException("The Registry branches already exists: you have to force overwriting!"); |
885 |
tr->SetBranchAddress("Registry", ®istry); |
886 |
branch = tr->GetBranch("Registry"); |
887 |
branch->Reset(); |
888 |
} |
889 |
|
890 |
//This select s temporary. Better selection on time should be done, |
891 |
oss.str(""); |
892 |
oss << "SELECT TLE1, TLE2, TLE3" |
893 |
<< " FROM GL_TLE " |
894 |
<< " WHERE VALID_FROM_TIME = 0 "; |
895 |
//cout << oss.str().c_str() << "\n"; |
896 |
result = conn->Query(oss.str().c_str()); |
897 |
row = result->Next(); |
898 |
if (!(row == 0)){ |
899 |
string tle1 = row->GetField(0); |
900 |
string tle2 = row->GetField(1); |
901 |
string tle3 = row->GetField(2); |
902 |
cTle tle(tle1, tle2, tle3); |
903 |
orbit = new cOrbit(tle); |
904 |
} |
905 |
|
906 |
/*Took all the TM_HEADER rows related to a specific root file and TREE_NAME*/ |
907 |
oss.str(""); |
908 |
oss << "SELECT EVENT, TIME FROM TM_HEADER WHERE ID_ROOT = '" << idRoot << "' " |
909 |
<< " AND TREE_NAME = '" << *Iter << "' ORDER BY EVENT ASC"; |
910 |
secResult = conn->Query(oss.str().c_str()); |
911 |
//cout << oss.str().c_str() << "\n"; |
912 |
|
913 |
//Fill the 'Registry' TBranch |
914 |
secRow = secResult->Next(); |
915 |
while (!(secRow == 0)){ |
916 |
registry->event = (unsigned int)atoi(secRow->GetField(0)); |
917 |
registry->absTime = (ULong64_t)atof(secRow->GetField(1)); |
918 |
|
919 |
branch->Fill(); |
920 |
secRow = secResult->Next(); |
921 |
/* |
922 |
cout << "FileName: " << registryName |
923 |
<< "absTime: " << dec << registry->absTime |
924 |
<< "event: " << dec << registry->event << "\n"; |
925 |
*/ |
926 |
} |
927 |
} |
928 |
file->Write(0,TObject::kOverwrite); |
929 |
file->Close(); |
930 |
} |
931 |
|
932 |
/** |
933 |
* Assign the BOOT_NUMBER to the raw file. |
934 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
935 |
* @param conn An active connection to a suitable (for PAMELA) database. |
936 |
* @param rawPathName The path to the raw file used to generate the root files. |
937 |
* @param rawFileName The name of the raw file used to generate the root files. |
938 |
*/ |
939 |
int PamelaDBOperations::assignBOOT_NUMBER(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
940 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
941 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
942 |
stringstream oss; |
943 |
TSQLResult *result = 0; |
944 |
TSQLRow *row = 0; |
945 |
oss.str(""); |
946 |
oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE " |
947 |
<< " PATH = '" << rawPathName << "' AND " |
948 |
<< " NAME = '" << rawFileName << "' "; |
949 |
result = conn->Query(oss.str().c_str()); |
950 |
//if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
951 |
row = result->Next(); |
952 |
if (row == 0) throw YSQLQueryException("The file is not in the database"); |
953 |
if (row->GetField(1) != 0) throw YSQLQueryException("BOOT number already existing!"); |
954 |
return assignBOOT_NUMBER(conn, row->GetField(0)); |
955 |
|
956 |
} |
957 |
|
958 |
|
959 |
/** |
960 |
* Update GL_RAW.BOOT_NUMBER columns. |
961 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
962 |
* @param conn An active connection to a suitable (for PAMELA) database. |
963 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
964 |
* |
965 |
*/ |
966 |
int PamelaDBOperations::assignBOOT_NUMBER(TSQLServer *conn, const char *idRaw) throw (YSQLNotConnectedException, YSQLQueryException) { |
967 |
|
968 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
969 |
stringstream oss; |
970 |
TSQLResult *result; |
971 |
TSQLRow *row; |
972 |
|
973 |
TFile *file = 0; |
974 |
TTree *tr = 0; |
975 |
|
976 |
oss.str(""); |
977 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
978 |
//cout << oss.str().c_str() << "\n"; |
979 |
result = conn->Query(oss.str().c_str()); |
980 |
if (result == 0) throw YSQLQueryException(oss.str().c_str()); |
981 |
row = result->Next(); |
982 |
oss.str(""); |
983 |
oss << row->GetField(1) << "/" << row->GetField(2); |
984 |
//cout << oss.str().c_str() << "\n"; |
985 |
TFile *varDumpFile = YFile::Open(oss.str().c_str()); |
986 |
|
987 |
TTree *trDumpEv = (TTree*)varDumpFile->Get("VarDump"); |
988 |
if (trDumpEv == NULL) throw YSQLQueryException("Event file is empty"); |
989 |
|
990 |
VarDumpEvent *vde = 0; |
991 |
VarDumpRecord *vdr = 0; |
992 |
|
993 |
trDumpEv->SetBranchAddress("VarDump", &vde); |
994 |
if (trDumpEv->GetEntries() > 0){ |
995 |
trDumpEv->GetEntry(0); |
996 |
vde->Records->GetEntries(); |
997 |
if (vde->Records->GetEntries() > 0){ |
998 |
vdr = (VarDumpRecord*)vde->Records->At(6); |
999 |
oss.str(""); |
1000 |
oss << " UPDATE GL_RAW " |
1001 |
<< " SET GL_RAW.BOOT_NUMBER = '" << dec << vdr->VAR_VALUE << "'" |
1002 |
<< " WHERE GL_RAW.ID = '" << idRaw << "'"; |
1003 |
conn->Query(oss.str().c_str()); |
1004 |
//cout << "Boot Number: " << dec << vdr->VAR_ID << "</VAR_ID> \n"; |
1005 |
//cout << "Boot Number: " << dec << vdr->VAR_VALUE << "</VAR_VALUE> \n"; |
1006 |
} else { |
1007 |
throw YSQLQueryException("No VarDump!"); |
1008 |
} |
1009 |
} |
1010 |
} |
1011 |
|
1012 |
|
1013 |
/** |
1014 |
* Insert RUN items into the GL_RUN table. |
1015 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1016 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1017 |
* @param rawPathName The path to the raw file used to generate the root files. |
1018 |
* @param rawFileName The name of the raw file used to generate the root files. |
1019 |
*/ |
1020 |
int PamelaDBOperations::insertPamelaRUN(TSQLServer *conn, const char *pathName, const char *fileName) |
1021 |
throw (YSQLNotConnectedException, YSQLQueryException, YNotExistingFileException, YEmptyObjectException){ |
1022 |
|
1023 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1024 |
const char *id = PamelaDBOperations::getID_RAW(conn, pathName, fileName); |
1025 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
1026 |
TSQLResult *result = 0; |
1027 |
TSQLRow *row = 0; |
1028 |
/* |
1029 |
* Verify that the RUN have been not already processed |
1030 |
*/ |
1031 |
stringstream oss; |
1032 |
oss.str(""); |
1033 |
oss << " SELECT COUNT(GL_RUN.ID) FROM GL_RUN " |
1034 |
<< " LEFT JOIN GL_ROOT " |
1035 |
<< " ON GL_ROOT.ID = GL_RUN.ID_REG_RUN " |
1036 |
<< " WHERE GL_ROOT.ID_RAW = " << id; |
1037 |
result = conn->Query(oss.str().c_str()); |
1038 |
row = result->Next(); |
1039 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("Runs already inserted in the database"); |
1040 |
/* |
1041 |
* Finally extract the RUNs informations |
1042 |
*/ |
1043 |
try { |
1044 |
insertPamelaRUN(conn, id); |
1045 |
} catch (YSQLQueryException exc) { |
1046 |
cout << "Eccezione! " << exc.print() << "\n"; |
1047 |
return NOK; |
1048 |
} catch (YSQLNotConnectedException exc) { |
1049 |
cout << "Eccezione! " << exc.print() << "\n"; |
1050 |
return NOK; |
1051 |
} catch (YNotExistingFileException exc) { |
1052 |
cout << "Eccezione! " << exc.print() << "\n"; |
1053 |
return NOK; |
1054 |
} catch (YEmptyObjectException exc) { |
1055 |
cout << "Eccezione! " << exc.print() << "\n"; |
1056 |
return NOK; |
1057 |
} |
1058 |
return OK; |
1059 |
} |
1060 |
|
1061 |
/** |
1062 |
* Insert RUN items into the GL_RUN table. |
1063 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1064 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1065 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1066 |
* |
1067 |
* STILL TO BE DONE: When a new RUN is inserted it have to update the GL_CALIB before run |
1068 |
* any insertNEW_CALIBXXX in order to have always the run into one calibration!!! |
1069 |
*/ |
1070 |
int PamelaDBOperations::insertPamelaRUN(TSQLServer *conn, const char *idRaw) |
1071 |
throw (YSQLNotConnectedException, YSQLQueryException, YNotExistingFileException, YEmptyObjectException) { |
1072 |
|
1073 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1074 |
stringstream oss; |
1075 |
TSQLResult *result = 0; |
1076 |
TSQLRow *row = 0; |
1077 |
const char *idRoot = 0; |
1078 |
RegistryEvent *runHeaderRegistry = new RegistryEvent(); |
1079 |
RegistryEvent *runTrailerRegistry = new RegistryEvent(); |
1080 |
RegistryEvent *calPedRegistry = new RegistryEvent(); |
1081 |
RegistryEvent *physRegistry = new RegistryEvent(); |
1082 |
TFile *rootFile = 0; |
1083 |
TTree *calTr, *physTr, *runHeaderEvTr = 0, *runTrailerEvTr = 0; |
1084 |
TBranch *calBr, *physRegBr, *runHeaderRegBr = 0, *runHeaderEvBr = 0, *runTrailerRegBr = 0; |
1085 |
RunHeaderEvent *runheader = 0; |
1086 |
ULong64_t nevents = 0; |
1087 |
|
1088 |
/*----------BEGIN - Create the temporary table to collect the PhysicsRegistry items-----------*/ |
1089 |
conn->Query(PamelaDBOperations::TM_REGISTRY); |
1090 |
|
1091 |
/*Took the root file related to the Physics*/ |
1092 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
1093 |
result = conn->Query(oss.str().c_str()); |
1094 |
//cout << oss.str().c_str() << "\n"; |
1095 |
|
1096 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
1097 |
row = result->Next(); |
1098 |
idRoot = row->GetField(0); |
1099 |
|
1100 |
oss.str(""); |
1101 |
oss << row->GetField(1) << "/" << row->GetField(2); |
1102 |
//cout << oss.str().c_str() << "\n"; |
1103 |
rootFile = YFile::Open(oss.str().c_str()); |
1104 |
if(rootFile->IsZombie()) throw YNotExistingFileException(oss.str().c_str()); |
1105 |
|
1106 |
physTr = (TTree*)rootFile->Get("Physics"); |
1107 |
if (!physTr->GetBranchStatus("Registry")) throw YEmptyObjectException("The 'Registry' TBranch not exist in the 'Physics' TTree"); |
1108 |
physTr->SetBranchAddress("Registry", &physRegistry); |
1109 |
physRegBr = physTr->GetBranch("Registry"); |
1110 |
nevents = physRegBr->GetEntries(); |
1111 |
//cout << "nEvents: " << dec << nevents << "\n"; |
1112 |
|
1113 |
/*INSERT the physics items into TM_REGISTRY*/ |
1114 |
for (UInt_t i = 0; i < nevents; i++){ |
1115 |
physRegBr->GetEntry(i); |
1116 |
oss.str(""); |
1117 |
oss << " INSERT INTO TM_REGISTRY (REG_EVENT, TIME) " |
1118 |
<< " VALUES ( " |
1119 |
<< dec << physRegistry->event << ", " |
1120 |
<< dec << physRegistry->absTime << ")"; |
1121 |
conn->Query(oss.str().c_str()); |
1122 |
} |
1123 |
runHeaderEvTr = (TTree*)rootFile->Get("RunHeader"); |
1124 |
runHeaderEvTr->SetBranchAddress("RunHeader", &runheader); |
1125 |
runHeaderEvTr->SetBranchAddress("Registry", &runHeaderRegistry); |
1126 |
runHeaderRegBr = runHeaderEvTr->GetBranch("Registry"); |
1127 |
|
1128 |
runTrailerEvTr = (TTree*)rootFile->Get("RunTrailer"); |
1129 |
runTrailerEvTr->SetBranchAddress("Registry", &runTrailerRegistry); |
1130 |
runTrailerRegBr = runTrailerEvTr->GetBranch("Registry"); |
1131 |
/*----------END - Create the temporary table to collect the PhysicsRegistry items-----------*/ |
1132 |
|
1133 |
/*----------BEGIN - Fill the GL_RUN table collecting RunHeader and Physics items-----------*/ |
1134 |
|
1135 |
nevents = runHeaderRegBr->GetEntries(); |
1136 |
//cout << "nEvents: " << dec << nevents << "\n"; |
1137 |
/* |
1138 |
* NOTE - Below is aassumed as true that RUNHEADER events are synchronized |
1139 |
* with the RUNTRAILER data. This is true if data will not skip any |
1140 |
* RunHeader or runTrailer. |
1141 |
*/ |
1142 |
for (UInt_t i = 0; i < nevents; i++){ |
1143 |
runTrailerRegBr->GetEntry(i); |
1144 |
|
1145 |
/*----- DO NOT INVERT!!-----*/ |
1146 |
runHeaderRegBr->GetEntry(i); |
1147 |
runHeaderEvTr->GetEntry(runHeaderRegistry->event); |
1148 |
/*----- DO NOT INVERT!!-----*/ |
1149 |
|
1150 |
|
1151 |
/* |
1152 |
* This line is just for test. It should guarantee that the run is consistent. |
1153 |
* The reason for this is that I have verified that on some MCMD TimeSyncTIME |
1154 |
* the value seems stoned BUT the OBT still run. As consequence can happen to |
1155 |
* have a runTrailer "older" then the relative runHeader which is obviously |
1156 |
* an error..... |
1157 |
*/ |
1158 |
if (runHeaderRegistry->absTime > runTrailerRegistry->absTime) continue; |
1159 |
/*---------------------------------------------------------------------------*/ |
1160 |
|
1161 |
/* |
1162 |
* SELECT the interval of events belonging to this RUN |
1163 |
*/ |
1164 |
|
1165 |
oss.str(""); |
1166 |
oss << " SELECT MIN(REG_EVENT), MAX(REG_EVENT) " |
1167 |
<< " FROM TM_REGISTRY " |
1168 |
<< " WHERE TIME BETWEEN " << runHeaderRegistry->absTime |
1169 |
<< " AND " << runTrailerRegistry->absTime ; |
1170 |
//cout << oss.str().c_str() << "\n"; |
1171 |
result = conn->Query(oss.str().c_str()); |
1172 |
if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
1173 |
row = result->Next(); |
1174 |
|
1175 |
if ((row->GetField(0) == NULL) || (row->GetField(1) == NULL)) continue; |
1176 |
/* |
1177 |
* INSERT the GL_RUN item |
1178 |
*/ |
1179 |
|
1180 |
oss.str(""); |
1181 |
oss << " INSERT INTO GL_RUN (ID_REG_RUN, RUNHEADER_TIME, EV_REG_RUNHEADER, " |
1182 |
<< " RUNTRAILER_TIME, EV_REG_RUNTRAILER, TRK_CALIB_USED, " |
1183 |
<< " EV_REG_PHYS_FROM, EV_REG_PHYS_TO, EFF_WRK_SCHEDULE, " |
1184 |
<< " PRH_VAR_TRG_MODE_A, PRH_VAR_TRG_MODE_B, ACQ_BUILD_INFO, ACQ_VAR_INFO) " |
1185 |
<< " VALUES ( " |
1186 |
<< idRoot << ", " |
1187 |
<< dec << runHeaderRegistry->absTime << ", " |
1188 |
<< dec << i << ", " |
1189 |
<< dec << runTrailerRegistry->absTime << ", " |
1190 |
<< dec << i << ", " |
1191 |
<< dec << runheader->TRK_CALIB_USED << ", " |
1192 |
<< row->GetField(0) << ", " |
1193 |
<< row->GetField(1) << ", " |
1194 |
<< dec << runheader->EFFECTIVE_WORKING_SCHEDULE << ", " |
1195 |
<< dec << runheader->PRH_VAR_TRIGGER_MODE_A << ", " |
1196 |
<< dec << runheader->PRH_VAR_TRIGGER_MODE_B << ", " |
1197 |
<< dec << runheader->ACQ_BUILD_INFO << ", " |
1198 |
<< dec << runheader->ACQ_VAR_INFO |
1199 |
<< ")"; |
1200 |
//cout << oss.str().c_str() << "\n"; |
1201 |
conn->Query(oss.str().c_str()); |
1202 |
|
1203 |
/* |
1204 |
* To be consistent the RUN have to be part of one calibration |
1205 |
*/ |
1206 |
for (int sec = 0; sec < 4 ; sec++){ |
1207 |
oss.str(""); |
1208 |
oss << "SELECT ID FROM GL_CALO_CALIB " |
1209 |
<< " WHERE FROM_TIME <= " << dec << runHeaderRegistry->absTime << " AND " |
1210 |
<< " TO_TIME >= " << dec << runTrailerRegistry->absTime << " AND " |
1211 |
<< " SECTION = " << dec << sec; |
1212 |
//cout << oss.str().c_str() << "\n"; |
1213 |
result = conn->Query(oss.str().c_str()); |
1214 |
|
1215 |
//The RUN is included in some calibration? |
1216 |
if (result->GetRowCount() == 0) { |
1217 |
oss.str(""); |
1218 |
oss << "SELECT MAX(TO_TIME) AS MX_TM_CALIB, ID AS ID_CALIB " |
1219 |
<< " FROM GL_CALO_CALIB " |
1220 |
<< " WHERE TO_TIME <= " << dec << runHeaderRegistry->absTime << " AND " |
1221 |
<< " SECTION = " << dec << sec |
1222 |
<< " GROUP BY ID ORDER BY MX_TM_CALIB DESC "; |
1223 |
//cout << oss.str().c_str() << "\n"; |
1224 |
result = conn->Query(oss.str().c_str()); |
1225 |
//Exist a calibration with time less than the RUN? |
1226 |
//if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
1227 |
if (result->GetRowCount() == 0) continue; |
1228 |
row = result->Next(); |
1229 |
oss.str(""); |
1230 |
oss << "UPDATE GL_CALO_CALIB " |
1231 |
<< " SET TO_TIME = " << dec << runTrailerRegistry->absTime |
1232 |
<< " WHERE ID = " << row->GetField(1); |
1233 |
//cout << oss.str().c_str() << "\n"; |
1234 |
result = conn->Query(oss.str().c_str()); |
1235 |
|
1236 |
} |
1237 |
} |
1238 |
|
1239 |
// GL_S4_CALIB |
1240 |
oss.str(""); |
1241 |
oss << "SELECT ID FROM GL_S4_CALIB " |
1242 |
<< " WHERE FROM_TIME <= " << dec << runHeaderRegistry->absTime << " AND " |
1243 |
<< " TO_TIME >= " << dec << runTrailerRegistry->absTime; |
1244 |
//cout << oss.str().c_str() << "\n"; |
1245 |
result = conn->Query(oss.str().c_str()); |
1246 |
row = result->Next(); |
1247 |
//The RUN is included in some calibration? |
1248 |
if (row == NULL) { |
1249 |
oss.str(""); |
1250 |
oss << "SELECT MAX(TO_TIME) AS MX_TM_CALIB, ID AS ID_CALIB " |
1251 |
<< " FROM GL_S4_CALIB " |
1252 |
<< " WHERE TO_TIME <= " << dec << runHeaderRegistry->absTime |
1253 |
<< " GROUP BY ID ORDER BY MX_TM_CALIB DESC "; |
1254 |
//cout << oss.str().c_str() << "\n"; |
1255 |
result = conn->Query(oss.str().c_str()); |
1256 |
//Exist a calibration with time less than the RUN? |
1257 |
//if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
1258 |
row = result->Next(); |
1259 |
if (row == NULL) continue; |
1260 |
oss.str(""); |
1261 |
oss << "UPDATE GL_S4_CALIB " |
1262 |
<< " SET TO_TIME = " << dec << runTrailerRegistry->absTime |
1263 |
<< " WHERE ID = " << row->GetField(1); |
1264 |
//cout << oss.str().c_str() << "\n"; |
1265 |
result = conn->Query(oss.str().c_str()); |
1266 |
} |
1267 |
|
1268 |
|
1269 |
// GL_TRK_CALIB |
1270 |
|
1271 |
oss.str(""); |
1272 |
oss << "SELECT ID FROM GL_TRK_CALIB " |
1273 |
<< " WHERE FROM_TIME <= " << dec << runHeaderRegistry->absTime << " AND " |
1274 |
<< " TO_TIME >= " << dec << runTrailerRegistry->absTime; |
1275 |
//cout << oss.str().c_str() << "\n"; |
1276 |
result = conn->Query(oss.str().c_str()); |
1277 |
|
1278 |
//The RUN is included in some calibration? |
1279 |
if (result->GetRowCount() == 0) { |
1280 |
oss.str(""); |
1281 |
oss << "SELECT MAX(TO_TIME) AS MX_TM_CALIB, ID AS ID_CALIB " |
1282 |
<< " FROM GL_TRK_CALIB " |
1283 |
<< " WHERE TO_TIME <= " << dec << runHeaderRegistry->absTime |
1284 |
<< " GROUP BY ID ORDER BY MX_TM_CALIB DESC "; |
1285 |
//cout << oss.str().c_str() << "\n"; |
1286 |
result = conn->Query(oss.str().c_str()); |
1287 |
//Exist a calibration with time less than the RUN? |
1288 |
//if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
1289 |
if (result->GetRowCount() == 0) continue; |
1290 |
row = result->Next(); |
1291 |
oss.str(""); |
1292 |
oss << "UPDATE GL_TRK_CALIB " |
1293 |
<< " SET TO_TIME = " << dec << runTrailerRegistry->absTime |
1294 |
<< " WHERE ID = " << row->GetField(1); |
1295 |
//cout << oss.str().c_str() << "\n"; |
1296 |
result = conn->Query(oss.str().c_str()); |
1297 |
} |
1298 |
} |
1299 |
|
1300 |
|
1301 |
rootFile->Close(); |
1302 |
//delete result, row; |
1303 |
oss.str(""); |
1304 |
oss << " DROP TABLE IF EXISTS TM_REGISTRY "; |
1305 |
// GOOD FOR MySQL ver > 3.2 |
1306 |
//oss << " DROP TEMPORARY TABLE IF EXISTS TM_REGISTRY "; |
1307 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
1308 |
conn->Query(oss.str().c_str()); |
1309 |
/*----------END - Fill the GL_RUN table collecting RunHeader and Physics items-----------*/ |
1310 |
} |
1311 |
|
1312 |
/** |
1313 |
* Populate the TM_CALIB_CALO_PED. |
1314 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1315 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1316 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1317 |
*/ |
1318 |
int PamelaDBOperations::populateTM_CALO_CALIB(TSQLServer *conn, const char *idRaw) |
1319 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException) { |
1320 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1321 |
stringstream oss; |
1322 |
|
1323 |
const char *idRegistry; |
1324 |
TSQLResult *result; |
1325 |
TSQLRow *row = 0; |
1326 |
TFile *file = 0; |
1327 |
TTree *tr = 0; |
1328 |
TBranch *calibCalPedEvBr = 0, *calibCalPedRegBr = 0; |
1329 |
CalibCalPedEvent *calibCalPed = 0; |
1330 |
RegistryEvent *registry = 0; |
1331 |
Long64_t nevents = 0; |
1332 |
|
1333 |
/*Create the temporary table to collect the Calib_Cal_Ped items*/ |
1334 |
conn->Query(PamelaDBOperations::TM_CALO_CALIB); |
1335 |
|
1336 |
/*Get the Registry file relative to the CalibCalPed*/ |
1337 |
oss.str(""); |
1338 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
1339 |
result = conn->Query(oss.str().c_str()); |
1340 |
//cout << oss.str().c_str() << "\n"; |
1341 |
|
1342 |
if (result->GetRowCount() == 0) throw YSQLQueryException(oss.str().c_str()); |
1343 |
row = result->Next(); |
1344 |
idRegistry = row->GetField(0); |
1345 |
oss.str(""); |
1346 |
oss << row->GetField(1) << "/" << row->GetField(2); |
1347 |
file = YFile::Open(oss.str().c_str()); |
1348 |
if (file->IsZombie()) throw YNotExistingFileException("File not found"); |
1349 |
|
1350 |
tr = (TTree*)file->Get("CalibCalPed"); |
1351 |
if (tr->IsZombie()) throw YEmptyObjectException("CalibCalPed TTree not exist"); |
1352 |
|
1353 |
if (!tr->GetBranchStatus("Registry")) throw YEmptyObjectException("Registry TBranch not existing for CalibCalPed TTree"); |
1354 |
calibCalPedRegBr = tr->GetBranch("Registry"); |
1355 |
tr->SetBranchAddress("Registry", ®istry); |
1356 |
|
1357 |
if (!tr->GetBranchStatus("CalibCalPed")) throw YEmptyObjectException("CalibCalPed TBranch not existing for CalibCalPed TTree"); |
1358 |
calibCalPedEvBr = tr->GetBranch("CalibCalPed"); |
1359 |
tr->SetBranchAddress("CalibCalPed", &calibCalPed); |
1360 |
|
1361 |
nevents = calibCalPedRegBr->GetEntries(); |
1362 |
//cout << "nEvents: " << dec << nevents << "\n"; |
1363 |
for (Int_t i = 0; i < nevents; i++){ |
1364 |
/*----- DO NOT INVERT!!-----*/ |
1365 |
calibCalPedRegBr->GetEntry(i); |
1366 |
calibCalPedEvBr->GetEntry(registry->event); |
1367 |
//cout << "i: " << dec << i << "\n"; |
1368 |
//cout << "registry: " << dec << registry->event << "\n"; |
1369 |
/*----- DO NOT INVERT!!-----*/ |
1370 |
for (int j = 0; j < 4; j++){ |
1371 |
if (calibCalPed->cstwerr[j] == 0) continue; |
1372 |
/* |
1373 |
* INSERT the CalibCalPed section into CL_CALIB_CAL_PED |
1374 |
*/ |
1375 |
oss.str(""); |
1376 |
oss << " INSERT INTO TM_CALO_CALIB (ID_REGISTRY, REG_EVENT, TIME, CAL_SECTION, CPERROR, CSTWERR) " |
1377 |
<< " VALUES ( " << idRegistry << ", " |
1378 |
<< dec << i << ", " |
1379 |
<< dec << registry->absTime << ", " |
1380 |
<< dec << j << ", " |
1381 |
<< dec << calibCalPed->cperror[j] << ", " |
1382 |
<< dec << calibCalPed->cstwerr[j] << ")" ; |
1383 |
//cout << oss.str().c_str() << "\n"; |
1384 |
conn->Query(oss.str().c_str()); |
1385 |
} |
1386 |
} |
1387 |
file->Close(); |
1388 |
return OK; |
1389 |
} |
1390 |
|
1391 |
/** |
1392 |
* Insert a new CALO items into the GL_CALO_XXX table. |
1393 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1394 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1395 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1396 |
*/ |
1397 |
int PamelaDBOperations::insertNEW_CALO_CALIB(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
1398 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
1399 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1400 |
const char *id = PamelaDBOperations::getID_RAW(conn, rawPathName, rawFileName); |
1401 |
stringstream oss; |
1402 |
|
1403 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
1404 |
|
1405 |
/* |
1406 |
* Verify that the CALO_CALIB have been not already processed |
1407 |
*/ |
1408 |
oss.str(""); |
1409 |
oss << " SELECT COUNT(GL_CALO_CALIB.ID) FROM GL_CALO_CALIB " |
1410 |
<< " LEFT JOIN GL_ROOT " |
1411 |
<< " ON GL_ROOT.ID = GL_CALO_CALIB.ID_REG_CALIBCALPED " |
1412 |
<< " WHERE GL_ROOT.ID_RAW = " << id; |
1413 |
TSQLResult *result = conn->Query(oss.str().c_str()); |
1414 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
1415 |
TSQLRow *row = result->Next(); |
1416 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("CaloPed calibrations already inserted in the database"); |
1417 |
|
1418 |
try { |
1419 |
insertNEW_CALO_CALIB(conn, id); |
1420 |
} catch (YSQLQueryException exc) { |
1421 |
cout << "Eccezione! " << exc.print() << "\n"; |
1422 |
return NOK; |
1423 |
} catch (YSQLNotConnectedException exc) { |
1424 |
cout << "Eccezione! " << exc.print() << "\n"; |
1425 |
return NOK; |
1426 |
} |
1427 |
return OK; |
1428 |
} |
1429 |
|
1430 |
/** |
1431 |
* Insert a new CALO items into the GL_CALO_XXX table. |
1432 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1433 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1434 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1435 |
*/ |
1436 |
int PamelaDBOperations::insertNEW_CALO_CALIB(TSQLServer *conn, const char *idRaw) |
1437 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
1438 |
|
1439 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1440 |
stringstream oss; |
1441 |
TSQLResult *result = 0; |
1442 |
TSQLRow *row = 0; |
1443 |
|
1444 |
/*---- Create/Populate the TM_CALOCALIB table ----*/ |
1445 |
try { |
1446 |
populateTM_CALO_CALIB(conn, idRaw); |
1447 |
} catch (YEmptyObjectException exc) { |
1448 |
cout << "Eccezione! " << exc.print() << "\n"; |
1449 |
return NOK; |
1450 |
} |
1451 |
|
1452 |
/*---- BEGIN - Fill/update the GL_CALO_CALIB table looping on GL_RUN matching with TM_CALIB_CAL_PED ----*/ |
1453 |
oss.str(""); |
1454 |
oss << "SELECT TIME as TM_TIME, CAL_SECTION AS SECTION, REG_EVENT AS TM_EV, ID_REGISTRY AS TM_ID_REG, " |
1455 |
<< " ID AS TM_ID " |
1456 |
<< " FROM TM_CALO_CALIB " |
1457 |
<< " WHERE CPERROR = 0 " |
1458 |
<< " ORDER BY TIME "; |
1459 |
|
1460 |
//cout << oss.str().c_str() << "\n"; |
1461 |
result = conn->Query(oss.str().c_str()); |
1462 |
ULong64_t time = 0; |
1463 |
int section = 0; |
1464 |
unsigned long reg_ev = 0; |
1465 |
unsigned long id_reg = 0; |
1466 |
unsigned long tm_id = 0; |
1467 |
while (true){ |
1468 |
row = result->Next(); |
1469 |
if (row == 0) break; |
1470 |
time = (ULong64_t)atof(row->GetField(0)) ; |
1471 |
section = (int)atoi(row->GetField(1)) ; |
1472 |
reg_ev = (unsigned long)atol(row->GetField(2)) ; |
1473 |
id_reg = (unsigned long)atol(row->GetField(3)) ; |
1474 |
tm_id = (unsigned long)atol(row->GetField(4)) ; |
1475 |
try{ |
1476 |
insertNEW_CALO_CALIB(conn, time, section, reg_ev, id_reg, tm_id); |
1477 |
} catch (YEmptyObjectException exc) { |
1478 |
logger->error(exc.print()); |
1479 |
} catch (YProcessErrorException exc) { |
1480 |
logger->error(exc.print()); |
1481 |
} |
1482 |
} |
1483 |
oss.str(""); |
1484 |
oss << " DROP TABLE IF EXISTS TM_CALO_CALIB "; |
1485 |
// GOOD FOR MySQL ver > 3.2 |
1486 |
//oss << " DROP TEMPORARY TABLE IF EXISTS TM_CALO_CALIB "; |
1487 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
1488 |
conn->Query(oss.str().c_str()); |
1489 |
/*---- END - Fill/update the GL_CALO_CALIB table looping on GL_RUN matching with TM_CALO_CALIB ----*/ |
1490 |
} |
1491 |
|
1492 |
/** |
1493 |
* Insert CALOCALIB items into the GL_CALO_CALIB table. |
1494 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1495 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1496 |
* @param time The time of the new calorimeter calibration. |
1497 |
* @param section The section of this calibration. |
1498 |
* @param reg_ev The registry entry number of the calibration. |
1499 |
* @param id_reg The registry id number of the calibration. |
1500 |
* @param tm_id The id of the TM_CALOCALIB. |
1501 |
*/ |
1502 |
int PamelaDBOperations::insertNEW_CALO_CALIB(TSQLServer *conn, ULong64_t time, int section, unsigned long reg_ev, unsigned long id_reg, unsigned long tm_id) |
1503 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException, YProcessErrorException) { |
1504 |
|
1505 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1506 |
if ((id_reg == 0) || (tm_id == 0)) throw YSQLNotConnectedException("Not valid IDs"); |
1507 |
|
1508 |
TSQLResult *result = 0; |
1509 |
TSQLRow *row = 0; |
1510 |
ULong64_t max_time = 0; |
1511 |
unsigned long id_item = 0; |
1512 |
bool processed = false; |
1513 |
stringstream oss; |
1514 |
|
1515 |
|
1516 |
/* Check if any GL_CALO_CALIB interval contain the new calibration for the specific section |
1517 |
It is supposed that NO MORE than one interval could contain the new calibration; if this |
1518 |
happens check the assignement procedure. |
1519 |
*/ |
1520 |
oss.str(""); |
1521 |
oss << " SELECT ID, TO_TIME " |
1522 |
<< " FROM GL_CALO_CALIB " |
1523 |
<< " WHERE ( " << dec << time << " >= FROM_TIME AND " |
1524 |
<< dec << time << " <= TO_TIME ) AND " |
1525 |
<< " SECTION = " << dec << section; |
1526 |
//cout << oss.str().c_str() << "\n"; |
1527 |
result = conn->Query(oss.str().c_str()); |
1528 |
if (result->GetRowCount() != 0) { |
1529 |
/* |
1530 |
The new calibration time is contained in a previous CALO_CALIB item |
1531 |
*/ |
1532 |
row = result->Next(); |
1533 |
|
1534 |
if (time == (ULong64_t)atof(row->GetField(1))) return 0; |
1535 |
/* |
1536 |
Resize the existing CALO_CALIB item |
1537 |
*/ |
1538 |
oss.str(""); |
1539 |
oss << " UPDATE GL_CALO_CALIB " |
1540 |
<< " SET GL_CALO_CALIB.TO_TIME = " << dec << time |
1541 |
<< " WHERE GL_CALO_CALIB.ID = " << row->GetField(0); |
1542 |
//cout << oss.str().c_str() << "\n"; |
1543 |
conn->Query(oss.str().c_str()); |
1544 |
|
1545 |
//insert a new CALO_CALIB item |
1546 |
oss.str(""); |
1547 |
oss << " INSERT INTO GL_CALO_CALIB (EV_REG_CALIBCALPED, ID_REG_CALIBCALPED, " |
1548 |
<< " FROM_TIME, TO_TIME, SECTION) " |
1549 |
<< " VALUES ( " |
1550 |
<< dec << reg_ev << ", " |
1551 |
<< dec << id_reg << ", " |
1552 |
<< dec << time << ", " |
1553 |
<< row->GetField(1) << ", " |
1554 |
<< dec << section << ") "; |
1555 |
//cout << oss.str().c_str() << "\n"; |
1556 |
conn->Query(oss.str().c_str()); |
1557 |
processed = true; |
1558 |
} else { |
1559 |
if (processed) throw YSQLNotConnectedException("TM_CALIB processed twice"); |
1560 |
/* |
1561 |
The calibration is NOT contained in any previous time interval |
1562 |
*/ |
1563 |
oss.str(""); |
1564 |
//THE FROM_TIME > time is not really needed..... |
1565 |
oss << " SELECT ID, MAX(RUNTRAILER_TIME), RUNHEADER_TIME " |
1566 |
<< " FROM GL_RUN " |
1567 |
<< " WHERE (RUNHEADER_TIME > " << dec << time << ") " |
1568 |
<< " GROUP BY ID " |
1569 |
<< " ORDER BY RUNTRAILER_TIME DESC"; |
1570 |
//cout << oss.str().c_str() << "\n"; |
1571 |
result = conn->Query(oss.str().c_str()); |
1572 |
if (result->GetRowCount() != 0) { |
1573 |
row = result->Next(); |
1574 |
/* |
1575 |
The new calibration time is LESS than any new RUNs |
1576 |
(if the RUNs where old they should already belong to some calibration interval). |
1577 |
|
1578 |
Note that this insert will span through all the time interval of the new RUNs, |
1579 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
1580 |
into the calbration previous in time. |
1581 |
*/ |
1582 |
oss.str(""); |
1583 |
oss << " INSERT INTO GL_CALO_CALIB (EV_REG_CALIBCALPED, ID_REG_CALIBCALPED, " |
1584 |
<< " FROM_TIME, TO_TIME, SECTION) " |
1585 |
<< " VALUES ( " |
1586 |
<< dec << reg_ev << ", " |
1587 |
<< dec << id_reg << ", " |
1588 |
<< dec << time << ", " |
1589 |
<< row->GetField(1) << ", " |
1590 |
<< dec << section << ")"; |
1591 |
//cout << oss.str().c_str() << "\n"; |
1592 |
conn->Query(oss.str().c_str()); |
1593 |
|
1594 |
/* |
1595 |
Delete the inserted TM_CALIBCALO |
1596 |
*/ |
1597 |
oss.str(""); |
1598 |
oss << " DELETE FROM TM_CALO_CALIB " |
1599 |
<< " WHERE ID = " << dec << tm_id; |
1600 |
//cout << oss.str().c_str() << "\n"; |
1601 |
conn->Query(oss.str().c_str()); |
1602 |
processed = true; |
1603 |
} else { |
1604 |
/* |
1605 |
The new calibration time is GREATER than the first one of the new RUNs |
1606 |
(if the RUNs where old they should already belong to some calibration interval). |
1607 |
|
1608 |
Note that this insert will span through all the time interval of the new RUNs, |
1609 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
1610 |
into the calbration previous in time. |
1611 |
*/ |
1612 |
|
1613 |
/* |
1614 |
Select the max time included in the calibration the already existing GL_CALO_CALIB |
1615 |
*/ |
1616 |
oss.str(""); |
1617 |
oss << " SELECT ID AS ID_CALIB, MAX(TO_TIME) " |
1618 |
<< " FROM GL_CALO_CALIB " |
1619 |
<< " WHERE SECTION = " << dec << section |
1620 |
<< " GROUP BY ID " |
1621 |
<< "ORDER BY TO_TIME DESC "; |
1622 |
//cout << oss.str().c_str() << "\n"; |
1623 |
result = conn->Query(oss.str().c_str()); |
1624 |
if (result->GetRowCount() != 0) { |
1625 |
row = result->Next(); |
1626 |
id_item = (unsigned long)atol(row->GetField(0)) ; |
1627 |
max_time = (ULong64_t)atof(row->GetField(1)) ; |
1628 |
/* |
1629 |
Resize the existing CALO_CALIB item |
1630 |
*/ |
1631 |
oss.str(""); |
1632 |
oss << " UPDATE GL_CALO_CALIB " |
1633 |
<< " SET GL_CALO_CALIB.TO_TIME = " << dec << max_time |
1634 |
<< " WHERE GL_CALO_CALIB.ID = " << dec << id_item; |
1635 |
//cout << oss.str().c_str() << "\n"; |
1636 |
conn->Query(oss.str().c_str()); |
1637 |
|
1638 |
oss.str(""); |
1639 |
oss << " INSERT INTO GL_CALO_CALIB (EV_REG_CALIBCALPED, ID_REG_CALIBCALPED, " |
1640 |
<< " FROM_TIME, TO_TIME, SECTION) " |
1641 |
<< " VALUES ( " |
1642 |
<< dec << reg_ev << ", " |
1643 |
<< dec << id_reg << ", " |
1644 |
<< dec << max_time << ", " |
1645 |
<< dec << time << ", " |
1646 |
<< dec << section << ") "; |
1647 |
//cout << oss.str().c_str() << "\n"; |
1648 |
conn->Query(oss.str().c_str()); |
1649 |
|
1650 |
/* |
1651 |
Delete the inserted TM_CALIBCALO |
1652 |
*/ |
1653 |
oss.str(""); |
1654 |
oss << " DELETE FROM TM_CALO_CALIB " |
1655 |
<< " WHERE ID = " << dec << tm_id; |
1656 |
//cout << oss.str().c_str() << "\n"; |
1657 |
conn->Query(oss.str().c_str()); |
1658 |
} |
1659 |
processed = true; |
1660 |
} |
1661 |
} |
1662 |
if (!processed) throw YSQLNotConnectedException("TM_CALIB not processed"); |
1663 |
} |
1664 |
|
1665 |
|
1666 |
|
1667 |
/** |
1668 |
* Populate the TM_S4_CALIB. |
1669 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1670 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1671 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1672 |
*/ |
1673 |
|
1674 |
int PamelaDBOperations::populateTM_S4_CALIB(TSQLServer *conn, const char *idRaw) |
1675 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException) { |
1676 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1677 |
stringstream oss; |
1678 |
|
1679 |
const char *idRegistry; |
1680 |
TSQLResult *result; |
1681 |
TSQLRow *row = 0; |
1682 |
TFile *rootFile = 0; |
1683 |
TTree *tr = 0; |
1684 |
TBranch *calibS4RegBr = 0; |
1685 |
TBranch *calibS4EvBr = 0; |
1686 |
CalibS4Event *calibS4 = new CalibS4Event(); |
1687 |
RegistryEvent *registry = 0; |
1688 |
Long64_t nevents = 0; |
1689 |
double param_fit = 0; |
1690 |
|
1691 |
|
1692 |
// Create the temporary table to collect the CalibS4 items |
1693 |
conn->Query(PamelaDBOperations::TM_S4_CALIB); |
1694 |
|
1695 |
//Get the Registry file relative to the CalibS4 |
1696 |
oss.str(""); |
1697 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
1698 |
result = conn->Query(oss.str().c_str()); |
1699 |
//cout << oss.str().c_str() << "\n"; |
1700 |
row = result->Next(); |
1701 |
if (row == 0) throw YSQLQueryException(oss.str().c_str()); |
1702 |
|
1703 |
idRegistry = row->GetField(0); |
1704 |
oss.str(""); |
1705 |
oss << row->GetField(1) << "/" << row->GetField(2); |
1706 |
rootFile = YFile::Open(oss.str().c_str()); |
1707 |
|
1708 |
|
1709 |
if (rootFile->IsZombie()) throw YNotExistingFileException("File not found"); |
1710 |
tr = (TTree*)rootFile->Get("CalibS4"); |
1711 |
if (tr->IsZombie()) throw YEmptyObjectException("Calibs4 TTree not exist"); |
1712 |
|
1713 |
if (!tr->GetBranchStatus("Registry")) throw YEmptyObjectException("Registry TBranch not existing for CalibS4 TTree"); |
1714 |
calibS4RegBr = tr->GetBranch("Registry"); |
1715 |
tr->SetBranchAddress("Registry", ®istry); |
1716 |
|
1717 |
calibS4EvBr = tr->GetBranch("CalibS4"); |
1718 |
tr->SetBranchAddress("CalibS4", &calibS4); |
1719 |
|
1720 |
nevents = calibS4RegBr->GetEntries(); |
1721 |
//cout << "nEvents in CalibS4Registry: " << dec << nevents << "\n"; |
1722 |
for (Int_t i = 0; i < nevents; i++){ |
1723 |
/*----- DO NOT INVERT!!-----*/ |
1724 |
calibS4RegBr->GetEntry(i); |
1725 |
calibS4EvBr->GetEntry(registry->event); |
1726 |
TArrayD params = S4_paramfit(calibS4); |
1727 |
//cout << "Param0:" << dec << params.At(0); |
1728 |
//cout << "Param1:" << dec << params.At(0); |
1729 |
oss.str(""); |
1730 |
oss << " INSERT INTO TM_S4_CALIB (ID_REGISTRY, REG_EVENT, TIME, PARAM_FIT0, PARAM_FIT1) " |
1731 |
<< " VALUES ( " << idRegistry << ", " |
1732 |
<< dec << i << ", " |
1733 |
<< dec << registry->absTime << ", " |
1734 |
<< dec << params.At(0) << ", " |
1735 |
<< dec << params.At(1) << ")" ; |
1736 |
//cout << oss.str().c_str() << "\n"; |
1737 |
conn->Query(oss.str().c_str()); |
1738 |
} |
1739 |
return OK; |
1740 |
} |
1741 |
|
1742 |
/** |
1743 |
* Collect the CALIB_S4 from each section of each RUN. |
1744 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1745 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1746 |
* @param rawPathName The path to the root file. |
1747 |
* @param rawFileName The name of the root file. |
1748 |
*/ |
1749 |
int PamelaDBOperations::insertNEW_S4_CALIB(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
1750 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
1751 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1752 |
const char *id = PamelaDBOperations::getID_RAW(conn, rawPathName, rawFileName); |
1753 |
stringstream oss; |
1754 |
|
1755 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
1756 |
|
1757 |
/* |
1758 |
* Verify that the S4_CALIB have been not already processed |
1759 |
*/ |
1760 |
oss.str(""); |
1761 |
oss << " SELECT COUNT(GL_S4_CALIB.ID) FROM GL_S4_CALIB " |
1762 |
<< " LEFT JOIN GL_ROOT " |
1763 |
<< " ON GL_ROOT.ID = GL_S4_CALIB.ID_REG_CALIBS4 " |
1764 |
<< " WHERE GL_ROOT.ID_RAW = " << id; |
1765 |
TSQLResult *result = conn->Query(oss.str().c_str()); |
1766 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
1767 |
TSQLRow *row = result->Next(); |
1768 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("S4 calibrations already inserted in the database"); |
1769 |
|
1770 |
try { |
1771 |
return insertNEW_S4_CALIB(conn, id); |
1772 |
} catch (YSQLQueryException exc) { |
1773 |
cout << "Eccezione! " << exc.print() << "\n"; |
1774 |
return NOK; |
1775 |
} catch (YSQLNotConnectedException exc) { |
1776 |
cout << "Eccezione! " << exc.print() << "\n"; |
1777 |
return NOK; |
1778 |
} |
1779 |
return OK; |
1780 |
} |
1781 |
|
1782 |
/** |
1783 |
* Insert a new S4 items into the GL_S4_CALIB table. |
1784 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1785 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1786 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
1787 |
*/ |
1788 |
int PamelaDBOperations::insertNEW_S4_CALIB(TSQLServer *conn, const char *idRaw) |
1789 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
1790 |
|
1791 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1792 |
stringstream oss; |
1793 |
TSQLResult *result = 0; |
1794 |
TSQLRow *row = 0; |
1795 |
|
1796 |
/*---- Create/Populate the TM_S4CALIB table ----*/ |
1797 |
try { |
1798 |
populateTM_S4_CALIB(conn, idRaw); |
1799 |
} catch (YEmptyObjectException exc) { |
1800 |
cout << "Eccezione! " << exc.print() << "\n"; |
1801 |
return NOK; |
1802 |
} |
1803 |
|
1804 |
|
1805 |
/*---- BEGIN - Fill/update the GL_S4_CALIB table looping on GL_RUN matching with TM_CALIB_CAL_PED ----*/ |
1806 |
oss.str(""); |
1807 |
oss << "SELECT TIME, PARAM_FIT0, PARAM_FIT1, REG_EVENT, ID_REGISTRY, ID " |
1808 |
<< " FROM TM_S4_CALIB " |
1809 |
<< " ORDER BY TIME "; |
1810 |
|
1811 |
//cout << oss.str().c_str() << "\n"; |
1812 |
result = conn->Query(oss.str().c_str()); |
1813 |
ULong64_t time = 0; |
1814 |
float param_fit0 = 0; |
1815 |
float param_fit1 = 0; |
1816 |
unsigned long reg_ev = 0; |
1817 |
unsigned long id_reg = 0; |
1818 |
unsigned long tm_id = 0; |
1819 |
while (true){ |
1820 |
row = result->Next(); |
1821 |
if (row == 0) break; |
1822 |
time = (ULong64_t)atof(row->GetField(0)) ; |
1823 |
param_fit0 = (float)atof(row->GetField(1)) ; |
1824 |
param_fit1 = (float)atof(row->GetField(2)) ; |
1825 |
reg_ev = (unsigned long)atol(row->GetField(3)) ; |
1826 |
id_reg = (unsigned long)atol(row->GetField(4)) ; |
1827 |
tm_id = (unsigned long)atol(row->GetField(5)) ; |
1828 |
try{ |
1829 |
insertNEW_S4_CALIB(conn, param_fit0, param_fit1, time, reg_ev, id_reg, tm_id); |
1830 |
} catch (YEmptyObjectException exc) { |
1831 |
logger->error(exc.print()); |
1832 |
} catch (YProcessErrorException exc) { |
1833 |
logger->error(exc.print()); |
1834 |
} |
1835 |
} |
1836 |
oss.str(""); |
1837 |
oss << " DROP TABLE IF EXISTS TM_S4_CALIB "; |
1838 |
// GOOD FOR MySQL ver > 3.2 |
1839 |
//oss << " DROP TEMPORARY TABLE IF EXISTS TM_S4_CALIB "; |
1840 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
1841 |
conn->Query(oss.str().c_str()); |
1842 |
/*---- END - Fill/update the GL_CALO_CALIB table looping on GL_RUN matching with TM_CALIB_CAL_PED ----*/ |
1843 |
} |
1844 |
|
1845 |
|
1846 |
/** |
1847 |
* Insert S4CALIB items into the GL_S4_CALIB table. |
1848 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
1849 |
* @param conn An active connection to a suitable (for PAMELA) database. |
1850 |
* @param time The time of the new S4 calibration. |
1851 |
* @param section The section of this calibration. |
1852 |
* @param reg_ev The registry entry number of the calibration. |
1853 |
* @param id_reg The registry id number of the calibration. |
1854 |
* @param tm_id The id of the TM_S4CALIB. |
1855 |
*/ |
1856 |
int PamelaDBOperations::insertNEW_S4_CALIB(TSQLServer *conn, float paramfit0, float paramfit1, ULong64_t time, unsigned long reg_ev, unsigned long id_reg, unsigned long tm_id) |
1857 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException, YProcessErrorException) { |
1858 |
|
1859 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
1860 |
if ((id_reg == 0) || (tm_id == 0)) throw YSQLNotConnectedException("Not valid IDs"); |
1861 |
|
1862 |
TSQLResult *result = 0; |
1863 |
TSQLRow *row = 0; |
1864 |
ULong64_t max_time = 0; |
1865 |
unsigned long id_item = 0; |
1866 |
bool processed = false; |
1867 |
stringstream oss; |
1868 |
|
1869 |
|
1870 |
/* Check if any GL_S4_CALIB interval contain the new calibration for the specific section |
1871 |
It is supposed that NO MORE than one interval could contain the new calibration; if this |
1872 |
happens check the assignement procedure. |
1873 |
*/ |
1874 |
oss.str(""); |
1875 |
oss << " SELECT ID, TO_TIME " |
1876 |
<< " FROM GL_S4_CALIB " |
1877 |
<< " WHERE ( " << dec << time << " >= FROM_TIME AND " |
1878 |
<< dec << time << " <= TO_TIME ) "; |
1879 |
//cout << oss.str().c_str() << "\n"; |
1880 |
result = conn->Query(oss.str().c_str()); |
1881 |
|
1882 |
row = result->Next(); |
1883 |
if (row != 0) { |
1884 |
if (time == (ULong64_t)atof(row->GetField(1))) return 0; |
1885 |
/* |
1886 |
Resize the existing S4_CALIB item |
1887 |
*/ |
1888 |
oss.str(""); |
1889 |
oss << " UPDATE GL_S4_CALIB " |
1890 |
<< " SET GL_S4_CALIB.TO_TIME = " << dec << time |
1891 |
<< " WHERE GL_S4_CALIB.ID = " << row->GetField(0); |
1892 |
//cout << oss.str().c_str() << "\n"; |
1893 |
conn->Query(oss.str().c_str()); |
1894 |
|
1895 |
//insert a new S4_CALIB item |
1896 |
oss.str(""); |
1897 |
oss << " INSERT INTO GL_S4_CALIB (EV_REG_CALIBS4, ID_REG_CALIBS4, " |
1898 |
<< " FROM_TIME, TO_TIME, PARAM_FIT0, PARAM_FIT1) " |
1899 |
<< " VALUES ( " |
1900 |
<< dec << reg_ev << ", " |
1901 |
<< dec << id_reg << ", " |
1902 |
<< dec << time << ", " |
1903 |
<< row->GetField(1) << ", " |
1904 |
<< dec << paramfit0 << ", " |
1905 |
<< dec << paramfit1 << ") "; |
1906 |
//cout << oss.str().c_str() << "\n"; |
1907 |
conn->Query(oss.str().c_str()); |
1908 |
processed = true; |
1909 |
} else { |
1910 |
if (processed) throw YSQLNotConnectedException("TM_CALIB processed twice"); |
1911 |
/* |
1912 |
The calibration is NOT contained in any previous time interval |
1913 |
*/ |
1914 |
oss.str(""); |
1915 |
//THE FROM_TIME > time is not really needed..... |
1916 |
oss << " SELECT ID, MAX(RUNTRAILER_TIME), RUNHEADER_TIME " |
1917 |
<< " FROM GL_RUN " |
1918 |
<< " WHERE (RUNHEADER_TIME > " << dec << time << ") " |
1919 |
<< " GROUP BY ID " |
1920 |
<< " ORDER BY RUNTRAILER_TIME DESC"; |
1921 |
//cout << oss.str().c_str() << "\n"; |
1922 |
result = conn->Query(oss.str().c_str()); |
1923 |
//if (result->GetRowCount() != 0) { |
1924 |
row = result->Next(); |
1925 |
if (row != NULL) { |
1926 |
/* |
1927 |
The new calibration time is LESS than any new RUNs |
1928 |
(if the RUNs where old they should already belong to some calibration interval). |
1929 |
|
1930 |
Note that this insert will span through all the time interval of the new RUNs, |
1931 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
1932 |
into the calbration previous in time. |
1933 |
*/ |
1934 |
oss.str(""); |
1935 |
oss << " INSERT INTO GL_S4_CALIB (EV_REG_CALIBS4, ID_REG_CALIBS4, " |
1936 |
<< " FROM_TIME, TO_TIME, PARAM_FIT0, PARAM_FIT1) " |
1937 |
<< " VALUES ( " |
1938 |
<< dec << reg_ev << ", " |
1939 |
<< dec << id_reg << ", " |
1940 |
<< dec << time << ", " |
1941 |
<< row->GetField(1) << ", " |
1942 |
<< dec << paramfit0 << ", " |
1943 |
<< dec << paramfit1 << ")"; |
1944 |
//cout << oss.str().c_str() << "\n"; |
1945 |
conn->Query(oss.str().c_str()); |
1946 |
|
1947 |
/* |
1948 |
Delete the inserted TM_CALIBCALO |
1949 |
*/ |
1950 |
oss.str(""); |
1951 |
oss << " DELETE FROM TM_S4_CALIB " |
1952 |
<< " WHERE ID = " << dec << tm_id; |
1953 |
//cout << oss.str().c_str() << "\n"; |
1954 |
conn->Query(oss.str().c_str()); |
1955 |
processed = true; |
1956 |
} else { |
1957 |
/* |
1958 |
The new calibration time is GREATER than the first one of the new RUNs |
1959 |
(if the RUNs where old they should already belong to some calibration interval). |
1960 |
|
1961 |
Note that this insert will span through all the time interval of the new RUNs, |
1962 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
1963 |
into the calbration previous in time. |
1964 |
*/ |
1965 |
|
1966 |
/* |
1967 |
Select the max time included in the calibration the already existing GL_S4_CALIB |
1968 |
*/ |
1969 |
oss.str(""); |
1970 |
oss << " SELECT ID AS ID_CALIB, MAX(TO_TIME) " |
1971 |
<< " FROM GL_S4_CALIB " |
1972 |
<< " GROUP BY ID " |
1973 |
<< " ORDER BY TO_TIME DESC"; |
1974 |
//cout << oss.str().c_str() << "\n"; |
1975 |
result = conn->Query(oss.str().c_str()); |
1976 |
//if (result->GetRowCount() != 0) { |
1977 |
if (row != NULL) { |
1978 |
row = result->Next(); |
1979 |
id_item = (unsigned long)atol(row->GetField(0)) ; |
1980 |
max_time = (ULong64_t)atof(row->GetField(1)) ; |
1981 |
/* |
1982 |
Resize the existing CALO_CALIB item |
1983 |
*/ |
1984 |
oss.str(""); |
1985 |
oss << " UPDATE GL_S4_CALIB " |
1986 |
<< " SET GL_S4_CALIB.TO_TIME = " << dec << max_time |
1987 |
<< " WHERE GL_S4_CALIB.ID = " << dec << id_item; |
1988 |
//cout << oss.str().c_str() << "\n"; |
1989 |
conn->Query(oss.str().c_str()); |
1990 |
|
1991 |
oss.str(""); |
1992 |
oss << " INSERT INTO GL_S4_CALIB (EV_REG_CALIBS4, ID_REG_CALIBS4, " |
1993 |
<< " FROM_TIME, TO_TIME, PARAM_FIT0, PARAM_FIT1) " |
1994 |
<< " VALUES ( " |
1995 |
<< dec << reg_ev << ", " |
1996 |
<< dec << id_reg << ", " |
1997 |
<< dec << max_time << ", " |
1998 |
<< dec << time << ", " |
1999 |
<< dec << paramfit0 << ", " |
2000 |
<< dec << paramfit1 << ") "; |
2001 |
//cout << oss.str().c_str() << "\n"; |
2002 |
conn->Query(oss.str().c_str()); |
2003 |
|
2004 |
/* |
2005 |
Delete the inserted TM_CALIBS4 |
2006 |
*/ |
2007 |
oss.str(""); |
2008 |
oss << " DELETE FROM TM_S4_CALIB " |
2009 |
<< " WHERE ID = " << dec << tm_id; |
2010 |
//cout << oss.str().c_str() << "\n"; |
2011 |
conn->Query(oss.str().c_str()); |
2012 |
} |
2013 |
processed = true; |
2014 |
} |
2015 |
} |
2016 |
if (!processed) throw YSQLNotConnectedException("TM_CALIB not processed"); |
2017 |
} |
2018 |
|
2019 |
|
2020 |
//****************************************************************************** |
2021 |
/** |
2022 |
* Collect the CALIB_TRK from each section of each RUN. |
2023 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
2024 |
* @param conn An active connection to a suitable (for PAMELA) database. |
2025 |
* @param rawPathName The path to the root file. |
2026 |
* @param rawFileName The name of the root file. |
2027 |
*/ |
2028 |
int PamelaDBOperations::insertNEW_TRK_CALIB(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
2029 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
2030 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
2031 |
const char *id = PamelaDBOperations::getID_RAW(conn, rawPathName, rawFileName); |
2032 |
stringstream oss; |
2033 |
|
2034 |
if (id == NULL) throw YSQLQueryException(RAW_FILE_NOT_FOUND); |
2035 |
|
2036 |
/* |
2037 |
* Verify that the TRK_CALIB have been not already processed |
2038 |
*/ |
2039 |
oss.str(""); |
2040 |
oss << " SELECT COUNT(GL_TRK_CALIB.ID) FROM GL_TRK_CALIB " |
2041 |
<< " LEFT JOIN GL_ROOT " |
2042 |
<< " ON GL_ROOT.ID = GL_TRK_CALIB.ID_REG_CALIBTRK " |
2043 |
<< " WHERE GL_ROOT.ID_RAW = " << id; |
2044 |
TSQLResult *result = conn->Query(oss.str().c_str()); |
2045 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
2046 |
TSQLRow *row = result->Next(); |
2047 |
if ((row != NULL) && (atol(row->GetField(0)) > 0)) throw YSQLQueryException("TRKx calibrations already inserted in the database"); |
2048 |
|
2049 |
try { |
2050 |
insertNEW_TRK_CALIB(conn, id); |
2051 |
} catch (YSQLQueryException exc) { |
2052 |
cout << "Eccezione! " << exc.print() << "\n"; |
2053 |
return NOK; |
2054 |
} catch (YSQLNotConnectedException exc) { |
2055 |
cout << "Eccezione! " << exc.print() << "\n"; |
2056 |
return NOK; |
2057 |
} |
2058 |
return OK; |
2059 |
} |
2060 |
|
2061 |
|
2062 |
/** |
2063 |
* Insert a new TRK items into the GL_TRK_CALIB table. |
2064 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
2065 |
* @param conn An active connection to a suitable (for PAMELA) database. |
2066 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
2067 |
*/ |
2068 |
int PamelaDBOperations::insertNEW_TRK_CALIB(TSQLServer *conn, const char *idRaw) |
2069 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
2070 |
|
2071 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
2072 |
stringstream oss; |
2073 |
TSQLResult *res = 0; |
2074 |
TSQLResult *res2 = 0; |
2075 |
TSQLRow *row = 0; |
2076 |
TSQLRow *row2 = 0; |
2077 |
|
2078 |
/*---- Create/Populate the TM_TRKCALIB table ----*/ |
2079 |
try { |
2080 |
populateTM_TRK_CALIB(conn, idRaw); |
2081 |
} catch (YEmptyObjectException exc) { |
2082 |
cout << "Eccezione! " << exc.print() << "\n"; |
2083 |
return NOK; |
2084 |
} |
2085 |
|
2086 |
/*---- BEGIN - Fill/update the GL_TRK_CALIB table looping on GL_RUN matching with TM_CALIB_TRK ----*/ |
2087 |
/* According to how the CPU execute the calibration sequence the CalibTrk1 and the CalibTrk2 are one |
2088 |
* after the other. Beacuse of this in the second query we search for the CalibTrk2 which has a |
2089 |
* (packet number + 1) |
2090 |
*/ |
2091 |
ULong64_t time = 0; |
2092 |
unsigned long reg_ev1 = 0; |
2093 |
unsigned long reg_ev2 = 0; |
2094 |
unsigned long id_reg = 0; |
2095 |
const char* tm_id1 = 0; |
2096 |
const char* tm_id2 = 0; |
2097 |
|
2098 |
oss.str(""); |
2099 |
oss << "SELECT TIME, REG_EVENT, ID_REGISTRY, PCK_COUNTER, ID" |
2100 |
<< " FROM TM_TRK_CALIB WHERE TRK_PART = 1" |
2101 |
<< " ORDER BY TIME "; |
2102 |
|
2103 |
//cout << oss.str().c_str() << "\n"; |
2104 |
res = conn->Query(oss.str().c_str()); |
2105 |
row = res->Next(); |
2106 |
while (!(row == 0)){ |
2107 |
id_reg = (unsigned long)atol(row->GetField(2)) ; |
2108 |
reg_ev1 = (unsigned long)atol(row->GetField(1)) ; |
2109 |
time = (ULong64_t)atof(row->GetField(0)) ; |
2110 |
tm_id1 = row->GetField(4); |
2111 |
tm_id2 = row->GetField(1); |
2112 |
oss.str(""); |
2113 |
oss << "SELECT REG_EVENT, ID" |
2114 |
<< " FROM TM_TRK_CALIB WHERE TRK_PART = 2 " |
2115 |
<< " AND ID_REGISTRY = " << row->GetField(2) |
2116 |
<< " AND PCK_COUNTER = " << dec << (atol(row->GetField(3)) + 1) |
2117 |
<< " ORDER BY TIME "; |
2118 |
//cout << oss.str().c_str() << "\n"; |
2119 |
res2 = conn->Query(oss.str().c_str()); |
2120 |
row2 = res2->Next(); |
2121 |
if (!(row2 == 0)){ |
2122 |
reg_ev2 = (unsigned long)atol(row2->GetField(0)) ; |
2123 |
try{ |
2124 |
insertNEW_TRK_CALIB(conn, id_reg, reg_ev1, reg_ev2, time, tm_id1, tm_id2); |
2125 |
} catch (YEmptyObjectException exc) { |
2126 |
logger->error(exc.print()); |
2127 |
} catch (YProcessErrorException exc) { |
2128 |
logger->error(exc.print()); |
2129 |
} |
2130 |
} else { |
2131 |
cout << "The corresponding CalibTrk2 have not been found!!" << "\n"; |
2132 |
} |
2133 |
row = res->Next(); |
2134 |
} |
2135 |
|
2136 |
oss.str(""); |
2137 |
oss << " DROP TABLE IF EXISTS TM_TRK_CALIB "; |
2138 |
// GOOD FOR MySQL ver > 3.2 |
2139 |
//oss << " DROP TEMPORARY TABLE IF EXISTS TM_S4_CALIB "; |
2140 |
//if (conn->Query(oss.str().c_str()) == 0) throw YSQLQueryException(oss.str().c_str()); |
2141 |
conn->Query(oss.str().c_str()); |
2142 |
/*---- END - Fill/update the GL_CALO_CALIB table looping on GL_RUN matching with TM_CALIB_CAL_PED ----*/ |
2143 |
} |
2144 |
|
2145 |
|
2146 |
/** |
2147 |
* Insert TRKCALIB items into the GL_TRK_CALIB table. |
2148 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
2149 |
* @param conn An active connection to a suitable (for PAMELA) database. |
2150 |
* @param id_reg The registry id number of the calibration. |
2151 |
* @param reg_ev1 The registry entry number for the Trk1 calibration. |
2152 |
* @param reg_ev2 The registry entry number for the Trk2 calibration. |
2153 |
* @param time The time of the calibration. |
2154 |
*/ |
2155 |
int PamelaDBOperations::insertNEW_TRK_CALIB(TSQLServer *conn, unsigned long id_reg, unsigned long reg_ev1, unsigned long reg_ev2, ULong64_t time, const char* tm_id1, const char* tm_id2) |
2156 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException, YProcessErrorException) { |
2157 |
|
2158 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
2159 |
//if ((id_reg == 0) || (tm_id == 0)) throw YSQLNotConnectedException("Not valid IDs"); |
2160 |
|
2161 |
TSQLResult *result = 0; |
2162 |
TSQLRow *row = 0; |
2163 |
ULong64_t max_time = 0; |
2164 |
unsigned long id_item = 0; |
2165 |
bool processed = false; |
2166 |
stringstream oss; |
2167 |
|
2168 |
|
2169 |
/* Check if any GL_TRK_CALIB interval contain the new calibration for the specific section |
2170 |
It is supposed that NO MORE than one interval could contain the new calibration; if this |
2171 |
happens check the assignement procedure. |
2172 |
*/ |
2173 |
oss.str(""); |
2174 |
oss << " SELECT ID, TO_TIME " |
2175 |
<< " FROM GL_TRK_CALIB " |
2176 |
<< " WHERE ( " << dec << time << " >= FROM_TIME AND " |
2177 |
<< dec << time << " <= TO_TIME )"; |
2178 |
//cout << oss.str().c_str() << "\n"; |
2179 |
result = conn->Query(oss.str().c_str()); |
2180 |
if (result->GetRowCount() != 0) { |
2181 |
/* |
2182 |
The new calibration time is contained in a previous GL_TRK_CALIB item |
2183 |
*/ |
2184 |
row = result->Next(); |
2185 |
|
2186 |
if (time == (ULong64_t)atof(row->GetField(1))) return 0; |
2187 |
/* |
2188 |
Resize the existing TRK_CALIB item |
2189 |
*/ |
2190 |
oss.str(""); |
2191 |
oss << " UPDATE GL_TRK_CALIB " |
2192 |
<< " SET GL_TRK_CALIB.TO_TIME = " << dec << time |
2193 |
<< " WHERE GL_TRK_CALIB.ID = " << row->GetField(0); |
2194 |
//cout << oss.str().c_str() << "\n"; |
2195 |
conn->Query(oss.str().c_str()); |
2196 |
|
2197 |
//insert a new TRK_CALIB item |
2198 |
oss.str(""); |
2199 |
oss << " INSERT INTO GL_TRK_CALIB (ID_REG_CALIBTRK, EV_REG_CALIBTRK1, EV_REG_CALIBTRK2, " |
2200 |
<< " FROM_TIME, TO_TIME) " |
2201 |
<< " VALUES ( " |
2202 |
<< dec << id_reg << ", " |
2203 |
<< dec << reg_ev1 << ", " |
2204 |
<< dec << reg_ev2 << ", " |
2205 |
<< dec << time << ", " |
2206 |
<< row->GetField(1) << ") "; |
2207 |
//cout << oss.str().c_str() << "\n"; |
2208 |
conn->Query(oss.str().c_str()); |
2209 |
processed = true; |
2210 |
} else { |
2211 |
if (processed) throw YSQLNotConnectedException("TM_CALIB processed twice"); |
2212 |
/* |
2213 |
The calibration is NOT contained in any previous time interval |
2214 |
*/ |
2215 |
oss.str(""); |
2216 |
//THE FROM_TIME > time is not really needed..... |
2217 |
oss << " SELECT ID, MAX(RUNTRAILER_TIME), RUNHEADER_TIME " |
2218 |
<< " FROM GL_RUN " |
2219 |
<< " WHERE (RUNHEADER_TIME > " << dec << time << ") " |
2220 |
<< " GROUP BY ID " |
2221 |
<< " ORDER BY RUNTRAILER_TIME DESC"; |
2222 |
//cout << oss.str().c_str() << "\n"; |
2223 |
result = conn->Query(oss.str().c_str()); |
2224 |
if (result->GetRowCount() != 0) { |
2225 |
row = result->Next(); |
2226 |
/* |
2227 |
The new calibration time is LESS than any new RUNs |
2228 |
(if the RUNs where old they should already belong to some calibration interval). |
2229 |
|
2230 |
Note that this insert will span through all the time interval of the new RUNs, |
2231 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
2232 |
into the calbration previous in time. |
2233 |
*/ |
2234 |
oss.str(""); |
2235 |
oss << " INSERT INTO GL_TRK_CALIB (ID_REG_CALIBTRK, EV_REG_CALIBTRK1, EV_REG_CALIBTRK2, " |
2236 |
<< " FROM_TIME, TO_TIME) " |
2237 |
<< " VALUES ( " |
2238 |
<< dec << id_reg << ", " |
2239 |
<< dec << reg_ev1 << ", " |
2240 |
<< dec << reg_ev2 << ", " |
2241 |
<< dec << time << ", " |
2242 |
<< row->GetField(1) << ")"; |
2243 |
//cout << oss.str().c_str() << "\n"; |
2244 |
conn->Query(oss.str().c_str()); |
2245 |
|
2246 |
/* |
2247 |
Delete the inserted TM_CALIBTRK |
2248 |
*/ |
2249 |
oss.str(""); |
2250 |
oss << " DELETE FROM TM_TRK_CALIB " |
2251 |
<< " WHERE ID = " << tm_id1 |
2252 |
<< " OR ID = " << tm_id2; |
2253 |
//cout << oss.str().c_str() << "\n"; |
2254 |
conn->Query(oss.str().c_str()); |
2255 |
processed = true; |
2256 |
} else { |
2257 |
/* |
2258 |
The new calibration time is GREATER than the first one of the new RUNs |
2259 |
(if the RUNs where old they should already belong to some calibration interval). |
2260 |
|
2261 |
Note that this insert will span through all the time interval of the new RUNs, |
2262 |
so in the next loop new calibrations will not fall into this ELSE but in the previous IF!!!! This splution should guarantee that after this procedure every RUN should fall |
2263 |
into the calbration previous in time. |
2264 |
*/ |
2265 |
|
2266 |
/* |
2267 |
Select the max time included in the calibration the already existing GL_TRK_CALIB |
2268 |
*/ |
2269 |
oss.str(""); |
2270 |
oss << " SELECT ID AS ID_CALIB, MAX(TO_TIME) " |
2271 |
<< " FROM GL_TRK_CALIB " |
2272 |
<< " GROUP BY ID " |
2273 |
<< " ORDER BY TO_TIME DESC"; |
2274 |
//cout << oss.str().c_str() << "\n"; |
2275 |
result = conn->Query(oss.str().c_str()); |
2276 |
if (result->GetRowCount() != 0) { |
2277 |
row = result->Next(); |
2278 |
id_item = (unsigned long)atol(row->GetField(0)) ; |
2279 |
max_time = (ULong64_t)atof(row->GetField(1)) ; |
2280 |
/* |
2281 |
Resize the existing TRK_CALIB item |
2282 |
*/ |
2283 |
oss.str(""); |
2284 |
oss << " UPDATE GL_TRK_CALIB " |
2285 |
<< " SET GL_TRK_CALIB.TO_TIME = " << dec << max_time |
2286 |
<< " WHERE GL_TRK_CALIB.ID = " << dec << id_item; |
2287 |
//cout << oss.str().c_str() << "\n"; |
2288 |
conn->Query(oss.str().c_str()); |
2289 |
|
2290 |
oss.str(""); |
2291 |
oss << " INSERT INTO GL_TRK_CALIB ( ID_REG_CALIBTRK, EV_REG_CALIBTRK1, EV_REG_CALIBTRK2," |
2292 |
<< " FROM_TIME, TO_TIME) " |
2293 |
<< " VALUES ( " |
2294 |
<< dec << id_reg << ", " |
2295 |
<< dec << reg_ev1 << ", " |
2296 |
<< dec << reg_ev2 << ", " |
2297 |
<< dec << max_time << ", " |
2298 |
<< dec << time << ") "; |
2299 |
//cout << oss.str().c_str() << "\n"; |
2300 |
conn->Query(oss.str().c_str()); |
2301 |
|
2302 |
/* |
2303 |
Delete the inserted TM_CALIBTRK |
2304 |
*/ |
2305 |
oss.str(""); |
2306 |
oss << " DELETE FROM TM_TRK_CALIB " |
2307 |
<< " WHERE ID = " << tm_id1 |
2308 |
<< " OR ID = " << tm_id2; |
2309 |
//cout << oss.str().c_str() << "\n"; |
2310 |
conn->Query(oss.str().c_str()); |
2311 |
} |
2312 |
processed = true; |
2313 |
} |
2314 |
} |
2315 |
if (!processed) throw YSQLNotConnectedException("TM_CALIB not processed"); |
2316 |
} |
2317 |
|
2318 |
/** |
2319 |
* Populate the TM_TRK_CALIB. |
2320 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
2321 |
* @param conn An active connection to a suitable (for PAMELA) database. |
2322 |
* @param idRaw The ID code of the raw file according to the GL_RAW table. |
2323 |
*/ |
2324 |
int PamelaDBOperations::populateTM_TRK_CALIB(TSQLServer *conn, const char *idRaw) |
2325 |
throw (YSQLNotConnectedException, YSQLQueryException, YEmptyObjectException) { |
2326 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
2327 |
|
2328 |
//It is "a bit" redundant but for sake of clarity...... |
2329 |
stringstream oss; |
2330 |
const char *idRegistry; |
2331 |
TSQLResult *result; |
2332 |
TSQLRow *row = 0; |
2333 |
TFile *rootFile = 0; |
2334 |
TTree *calibTrk1Tr = 0; |
2335 |
TTree *calibTrk2Tr = 0; |
2336 |
TBranch *calibTrk1EvBr = 0; |
2337 |
TBranch *calibTrk1HdBr = 0; |
2338 |
TBranch *calibTrk1RegBr = 0; |
2339 |
TBranch *calibTrk2EvBr = 0; |
2340 |
TBranch *calibTrk2HdBr = 0; |
2341 |
TBranch *calibTrk2RegBr = 0; |
2342 |
|
2343 |
EventHeader *calibTrk1EH = 0; |
2344 |
PscuHeader *calibTrk1PH = 0; |
2345 |
EventHeader *calibTrk2EH = 0; |
2346 |
PscuHeader *calibTrk2PH = 0; |
2347 |
|
2348 |
CalibTrk1Event *calibTrk1 = new CalibTrk1Event(); |
2349 |
CalibTrk2Event *calibTrk2 = new CalibTrk2Event(); |
2350 |
RegistryEvent *trk1Registry = 0; |
2351 |
RegistryEvent *trk2Registry = 0; |
2352 |
Long64_t nevents = 0; |
2353 |
|
2354 |
/*Create the temporary table to collect the CalibTRK items*/ |
2355 |
conn->Query(PamelaDBOperations::TM_TRK_CALIB); |
2356 |
|
2357 |
/*Get the Registry file relative to the CalibTRK1*/ |
2358 |
oss.str(""); |
2359 |
oss << "SELECT ID, PATH, NAME FROM GL_ROOT WHERE ID_RAW = '" << idRaw << "'"; |
2360 |
result = conn->Query(oss.str().c_str()); |
2361 |
//cout << oss.str().c_str() << "\n"; |
2362 |
row = result->Next(); |
2363 |
if (row == 0) throw YSQLQueryException(oss.str().c_str()); |
2364 |
|
2365 |
idRegistry = row->GetField(0); |
2366 |
oss.str(""); |
2367 |
oss << row->GetField(1) << "/" << row->GetField(2); |
2368 |
rootFile = YFile::Open(oss.str().c_str()); |
2369 |
if (rootFile->IsZombie()) throw YNotExistingFileException("File not found"); |
2370 |
|
2371 |
//Retrieve Registry and Events for CalibTrk1 |
2372 |
calibTrk1Tr = (TTree*)rootFile->Get("CalibTrk1"); |
2373 |
if (calibTrk1Tr->IsZombie()) throw YEmptyObjectException("CalibTrk1 TTree not exist"); |
2374 |
if (!calibTrk1Tr->GetBranchStatus("Registry")) throw YEmptyObjectException("Registry TBranch not existing for CalibTrk1 TTree"); |
2375 |
calibTrk1RegBr = calibTrk1Tr->GetBranch("Registry"); |
2376 |
calibTrk1Tr->SetBranchAddress("Registry", &trk1Registry); |
2377 |
|
2378 |
calibTrk1EvBr = calibTrk1Tr->GetBranch("CalibTrk1"); |
2379 |
calibTrk1Tr->SetBranchAddress("CalibTrk1", &calibTrk1); |
2380 |
|
2381 |
calibTrk1HdBr = calibTrk1Tr->GetBranch("Header"); |
2382 |
calibTrk1Tr->SetBranchAddress("Header", &calibTrk1EH); |
2383 |
|
2384 |
//Populate TM_TRK_CALIB for CalibTrk2 |
2385 |
nevents = calibTrk1RegBr->GetEntries(); |
2386 |
//cout << "nEvents: " << dec << nevents << "\n"; |
2387 |
for (Int_t i = 0; i < nevents; i++){ |
2388 |
/*----- DO NOT INVERT!!-----*/ |
2389 |
calibTrk1RegBr->GetEntry(i); |
2390 |
calibTrk1EvBr->GetEntry(trk1Registry->event); |
2391 |
calibTrk1HdBr->GetEntry(trk1Registry->event); |
2392 |
calibTrk1PH = calibTrk1EH->GetPscuHeader(); |
2393 |
oss.str(""); |
2394 |
oss << " INSERT INTO TM_TRK_CALIB (ID_REGISTRY, REG_EVENT, TIME, TRK_PART, PCK_COUNTER) " |
2395 |
<< " VALUES ( " |
2396 |
<< idRegistry << ", " |
2397 |
<< dec << i << ", " |
2398 |
<< dec << trk1Registry->absTime << ", " |
2399 |
<< dec << 1 << ", " |
2400 |
<< dec << calibTrk1PH->Counter << ")" ; |
2401 |
//cout << oss.str().c_str() << "\n"; |
2402 |
conn->Query(oss.str().c_str()); |
2403 |
} |
2404 |
|
2405 |
|
2406 |
//Retrieve Registry and Events for CalibTrk2 |
2407 |
calibTrk2Tr = (TTree*)rootFile->Get("CalibTrk2"); |
2408 |
if (calibTrk2Tr->IsZombie()) throw YEmptyObjectException("CalibTrk2 TTree not exist"); |
2409 |
if (!calibTrk2Tr->GetBranchStatus("Registry")) throw YEmptyObjectException("Registry TBranch not existing for CalibTrk2 TTree"); |
2410 |
calibTrk2RegBr = calibTrk2Tr->GetBranch("Registry"); |
2411 |
calibTrk2Tr->SetBranchAddress("Registry", &trk2Registry); |
2412 |
|
2413 |
calibTrk2EvBr = calibTrk2Tr->GetBranch("CalibTrk2"); |
2414 |
calibTrk2Tr->SetBranchAddress("CalibTrk2", &calibTrk2); |
2415 |
|
2416 |
calibTrk2HdBr = calibTrk2Tr->GetBranch("Header"); |
2417 |
calibTrk2Tr->SetBranchAddress("Header", &calibTrk2EH); |
2418 |
|
2419 |
//Populate TM_TRK_CALIB for CalibTrk2 |
2420 |
nevents = calibTrk2RegBr->GetEntries(); |
2421 |
//cout << "nEvents: " << dec << nevents << "\n"; |
2422 |
for (Int_t i = 0; i < nevents; i++){ |
2423 |
/*----- DO NOT INVERT!!-----*/ |
2424 |
calibTrk2RegBr->GetEntry(i); |
2425 |
calibTrk2EvBr->GetEntry(trk2Registry->event); |
2426 |
calibTrk2HdBr->GetEntry(trk2Registry->event); |
2427 |
calibTrk2PH = calibTrk2EH->GetPscuHeader(); |
2428 |
oss.str(""); |
2429 |
oss << " INSERT INTO TM_TRK_CALIB (ID_REGISTRY, REG_EVENT, TIME, TRK_PART, PCK_COUNTER) " |
2430 |
<< " VALUES ( " |
2431 |
<< idRegistry << ", " |
2432 |
<< dec << i << ", " |
2433 |
<< dec << trk2Registry->absTime << ", " |
2434 |
<< dec << 2 << ", " |
2435 |
<< dec << calibTrk2PH->Counter << ")" ; |
2436 |
//cout << oss.str().c_str() << "\n"; |
2437 |
conn->Query(oss.str().c_str()); |
2438 |
} |
2439 |
|
2440 |
return OK; |
2441 |
} |
2442 |
|
2443 |
|
2444 |
/** |
2445 |
* Retrieve the ID_RAW, if exists. |
2446 |
* Throw exception if (conn == 0) or the SQL command went wrong. |
2447 |
* @param conn An active connection to a suitable (for PAMELA) database. |
2448 |
* @param rawPathName The path to the root file. |
2449 |
* @param rawFileName The name of the root file. |
2450 |
*/ |
2451 |
const char* PamelaDBOperations::getID_RAW(TSQLServer *conn, const char *rawPathName, const char *rawFileName) |
2452 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
2453 |
|
2454 |
if (conn == 0) throw YSQLNotConnectedException("Connection missing"); |
2455 |
stringstream oss; |
2456 |
TSQLResult *result = 0; |
2457 |
TSQLRow *row = 0; |
2458 |
oss.str(""); |
2459 |
oss << "SELECT ID FROM GL_RAW WHERE " |
2460 |
<< " PATH = '" << rawPathName << "' AND " |
2461 |
<< " NAME = '" << rawFileName << "' "; |
2462 |
result = conn->Query(oss.str().c_str()); |
2463 |
if (result == NULL) throw YSQLQueryException(oss.str().c_str()); |
2464 |
row = result->Next(); |
2465 |
if (row == NULL) return NULL; |
2466 |
return row->GetField(0); |
2467 |
} |
2468 |
|
2469 |
const char* PamelaDBOperations::setGeo(cOrbit *orbit, RegistryEvent ®) |
2470 |
throw (YSQLNotConnectedException, YSQLQueryException){ |
2471 |
cEci eci; |
2472 |
cCoordGeo coo; |
2473 |
orbit->getPosition(reg.absTime, &eci); |
2474 |
coo = eci.toGeo(); |
2475 |
reg.m_Lat = rad2deg(coo.m_Lat); |
2476 |
reg.m_Lon = rad2deg(coo.m_Lon); |
2477 |
reg.m_Alt = coo.m_Alt; |
2478 |
//rate->Fill(rad2deg(coo.m_Lon), rad2deg(coo.m_Lat)); |
2479 |
} |