/[PAMELA software]/yodaUtility/YodaProfiler/PamelaDBOperations.cpp
ViewVC logotype

Contents of /yodaUtility/YodaProfiler/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Sat Jun 10 18:07:03 2006 UTC (18 years, 6 months ago) by kusanagi
Branch: MAIN
CVS Tags: yodaUtility2_0/00, yodaUtility2_1/00
Changes since 1.1: +77 -27 lines
Updating for Orbital and Geomagnetic parameters in Registry class.

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

  ViewVC Help
Powered by ViewVC 1.1.23