31 |
#include <varDump/VarDumpRecord.h> |
#include <varDump/VarDumpRecord.h> |
32 |
#include <physics/S4/S4Event.h> |
#include <physics/S4/S4Event.h> |
33 |
// |
// |
34 |
|
#include <cTle.h> |
35 |
|
#include <cEci.h> |
36 |
|
#include <cJulian.h> |
37 |
|
|
38 |
#include <PamelaDBOperations.h> |
#include <PamelaDBOperations.h> |
39 |
// |
// |
40 |
using namespace std; |
using namespace std; |
41 |
using namespace pamela; |
using namespace pamela; |
42 |
|
|
43 |
|
// Some function to work with cTle stuff. |
44 |
|
bool compTLE(cTle* tle1, cTle *tle2); |
45 |
|
float getTleJulian(cTle *); |
46 |
|
string getTleDatetime(cTle*); |
47 |
|
|
48 |
/** |
/** |
49 |
* Constructor. |
* Constructor. |
50 |
* @param host hostname for the SQL connection. |
* @param host hostname for the SQL connection. |
56 |
* @param obt0 file obt0. |
* @param obt0 file obt0. |
57 |
* @param tsync file timesync. |
* @param tsync file timesync. |
58 |
* @param debug debug flag. |
* @param debug debug flag. |
59 |
|
* @param tlefilename ascii file with TLE 3 line elements. |
60 |
*/ |
*/ |
61 |
PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){ |
PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){ |
62 |
// |
// |
63 |
// |
// |
64 |
SetConnection(host,user,password); |
SetConnection(host,user,password); |
72 |
SetTsync(tsync); |
SetTsync(tsync); |
73 |
SetObt0(obt0); |
SetObt0(obt0); |
74 |
// |
// |
75 |
|
SetTLEPath(tlefilename); |
76 |
|
// |
77 |
// |
// |
78 |
SetRootName(filerootname); |
INSERT_RAW =!filerawname.IsNull(); |
79 |
SetRawName(filerawname); |
if(INSERT_RAW)SetRawName(filerawname); |
80 |
// |
// |
81 |
this->OpenFile(); |
INSERT_ROOT = !filerootname.IsNull(); |
82 |
|
if( INSERT_ROOT ){ |
83 |
|
this->SetRootName(filerootname); |
84 |
|
file = TFile::Open(this->GetRootName().Data()); |
85 |
|
}; |
86 |
// |
// |
87 |
this->SetID_RAW(0); |
this->SetID_RAW(0); |
88 |
this->SetID_ROOT(0); |
this->SetID_ROOT(0); |
89 |
|
|
90 |
|
VALIDATE = false; |
91 |
|
|
92 |
// |
// |
93 |
}; |
}; |
94 |
|
|
97 |
*/ |
*/ |
98 |
void PamelaDBOperations::Close(){ |
void PamelaDBOperations::Close(){ |
99 |
if( conn && conn->IsConnected() ) conn->Close(); |
if( conn && conn->IsConnected() ) conn->Close(); |
100 |
|
delete clean_time; |
101 |
delete glrun; |
delete glrun; |
102 |
delete this; |
delete this; |
103 |
}; |
}; |
106 |
// SETTERS |
// SETTERS |
107 |
// |
// |
108 |
|
|
109 |
|
// |
110 |
|
// must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file |
111 |
|
// |
112 |
|
void PamelaDBOperations::CheckValidate(Long64_t olderthan){ |
113 |
|
clean_time = new TDatime(); |
114 |
|
if(olderthan >= 0){ |
115 |
|
VALIDATE = true; |
116 |
|
UInt_t timelim = 0; |
117 |
|
timelim = (UInt_t)clean_time->Convert() - olderthan; |
118 |
|
clean_time->Set(timelim,false); |
119 |
|
}; |
120 |
|
}; |
121 |
|
|
122 |
/** |
/** |
123 |
* Open the DB connection |
* Open the DB connection |
124 |
* @param host hostname for the SQL connection. |
* @param host hostname for the SQL connection. |
202 |
}; |
}; |
203 |
|
|
204 |
/** |
/** |
205 |
|
* Store path to the TLE file. |
206 |
|
*/ |
207 |
|
void PamelaDBOperations::SetTLEPath(TString str){ |
208 |
|
tlefilename = str; |
209 |
|
}; |
210 |
|
|
211 |
|
/** |
212 |
* Store the olderthan variable |
* Store the olderthan variable |
213 |
* @param olderthan |
* @param olderthan |
214 |
*/ |
*/ |
215 |
void PamelaDBOperations::SetOlderThan(Long64_t oldthan){ |
// void PamelaDBOperations::SetOlderThan(Long64_t oldthan){ |
216 |
olderthan = oldthan; |
// olderthan = oldthan; |
217 |
}; |
// }; |
218 |
|
|
219 |
/** |
/** |
220 |
* Retrieve the ID_RAW, if exists, returns NULL if does not exist. |
* Retrieve the ID_RAW, if exists, returns NULL if does not exist. |
360 |
// check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer) |
// check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer) |
361 |
// |
// |
362 |
PacketType *pctp=0; |
PacketType *pctp=0; |
|
T->GetEntry(upperentry); |
|
|
code = eh->GetCounter(); |
|
|
Int_t lasttrail = code->Get(pctp->RunTrailer); |
|
|
Int_t lasthead = code->Get(pctp->RunHeader); |
|
363 |
TTree *rh=(TTree*)file->Get("RunHeader"); |
TTree *rh=(TTree*)file->Get("RunHeader"); |
364 |
if ( !rh || rh->IsZombie() ) throw -17; |
if ( !rh || rh->IsZombie() ) throw -17; |
365 |
TTree *rt=(TTree*)file->Get("RunTrailer"); |
TTree *rt=(TTree*)file->Get("RunTrailer"); |
382 |
UInt_t pkth = 0; |
UInt_t pkth = 0; |
383 |
ULong64_t obth = 0; |
ULong64_t obth = 0; |
384 |
// |
// |
385 |
|
T->GetEntry(upperentry); |
386 |
|
code = eh->GetCounter(); |
387 |
|
Int_t lasttrail = code->Get(pctp->RunTrailer); |
388 |
|
Int_t lasthead = code->Get(pctp->RunHeader); |
389 |
if ( lasttrail < rtev ){ |
if ( lasttrail < rtev ){ |
390 |
rt->GetEntry(lasttrail); |
rt->GetEntry(lasttrail); |
391 |
pht = eht->GetPscuHeader(); |
pht = eht->GetPscuHeader(); |
396 |
if ( lasthead < rhev ){ |
if ( lasthead < rhev ){ |
397 |
rh->GetEntry(lasthead); |
rh->GetEntry(lasthead); |
398 |
phh = ehh->GetPscuHeader(); |
phh = ehh->GetPscuHeader(); |
399 |
pkth = PKT(pht->GetCounter()); |
pkth = PKT(phh->GetCounter()); |
400 |
obth = OBT(pht->GetOrbitalTime()); |
obth = OBT(phh->GetOrbitalTime()); |
401 |
}; |
}; |
402 |
// |
// |
403 |
if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
441 |
pkth = PKT(phh->GetCounter()); |
pkth = PKT(phh->GetCounter()); |
442 |
obth = OBT(phh->GetOrbitalTime()); |
obth = OBT(phh->GetOrbitalTime()); |
443 |
// |
// |
444 |
|
// if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth); |
445 |
|
// |
446 |
if ( pkth < spkth && obth < sobth ){ |
if ( pkth < spkth && obth < sobth ){ |
|
if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
|
447 |
if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev); |
if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev); |
448 |
// |
// |
449 |
rhev--; |
rhev = k-1; |
450 |
rh->GetEntry(rhev); |
rh->GetEntry(rhev); |
451 |
pkth = spkth; |
pkth = spkth; |
452 |
obth = sobth; |
obth = sobth; |
|
if ( IsDebug() ) printf(" lasthead %i pt %i p1 %i ot %u o1 %u \n",rhev,pkth,spkth,obth,sobth); |
|
453 |
// |
// |
454 |
UInt_t evbefh = 0; |
UInt_t evbefh = 0; |
455 |
code = ehh->GetCounter(); |
code = ehh->GetCounter(); |
459 |
ph = eh->GetPscuHeader(); |
ph = eh->GetPscuHeader(); |
460 |
t_pktlast = PKT(ph->GetCounter()); |
t_pktlast = PKT(ph->GetCounter()); |
461 |
t_obtlast = OBT(ph->GetOrbitalTime()); |
t_obtlast = OBT(ph->GetOrbitalTime()); |
462 |
if ( t_pktlast < spkth && t_obtlast < sobth ){ // jump |
if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump |
463 |
upperpkt = pkth; |
upperpkt = pkth; |
464 |
upperobt = obth; |
upperobt = obth; |
465 |
upperentry = evbefh-1; |
upperentry = evbefh-1; |
479 |
}; |
}; |
480 |
}; |
}; |
481 |
if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
482 |
|
goto kikko0; |
483 |
}; |
}; |
484 |
}; |
}; |
485 |
|
kikko0: |
486 |
// |
// |
487 |
// |
// |
488 |
// |
// |
500 |
pktt = PKT(pht->GetCounter()); |
pktt = PKT(pht->GetCounter()); |
501 |
obtt = OBT(pht->GetOrbitalTime()); |
obtt = OBT(pht->GetOrbitalTime()); |
502 |
// |
// |
503 |
|
// if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt); |
504 |
|
// |
505 |
if ( pktt < spktt && obtt < sobtt ){ |
if ( pktt < spktt && obtt < sobtt ){ |
|
if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); |
|
506 |
if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev); |
if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev); |
507 |
// |
// |
508 |
rtev--; |
rtev = k-1; |
509 |
rt->GetEntry(rtev); |
rt->GetEntry(rtev); |
510 |
pktt = spktt; |
pktt = spktt; |
511 |
obtt = sobtt; |
obtt = sobtt; |
519 |
ph = eh->GetPscuHeader(); |
ph = eh->GetPscuHeader(); |
520 |
t_pktlast = PKT(ph->GetCounter()); |
t_pktlast = PKT(ph->GetCounter()); |
521 |
t_obtlast = OBT(ph->GetOrbitalTime()); |
t_obtlast = OBT(ph->GetOrbitalTime()); |
522 |
if ( t_pktlast < spktt && t_obtlast < sobtt ){ // jump |
if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump |
523 |
upperpkt = pktt; |
upperpkt = pktt; |
524 |
upperobt = obtt; |
upperobt = obtt; |
525 |
upperentry = evbeft-1; |
upperentry = evbeft-1; |
539 |
}; |
}; |
540 |
}; |
}; |
541 |
if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); |
if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); |
542 |
break; |
goto kikko; |
543 |
|
// break; |
544 |
// |
// |
545 |
}; |
}; |
546 |
// |
// |
547 |
}; |
}; |
548 |
// |
// |
549 |
// kikko: |
kikko: |
550 |
|
// |
551 |
|
T->GetEntry(upperentry); |
552 |
|
code = eh->GetCounter(); |
553 |
|
lasttrail = code->Get(pctp->RunTrailer); |
554 |
|
lasthead = code->Get(pctp->RunHeader); |
555 |
|
if ( lasttrail < rtev ){ |
556 |
|
rt->GetEntry(lasttrail); |
557 |
|
pht = eht->GetPscuHeader(); |
558 |
|
pktt = PKT(pht->GetCounter()); |
559 |
|
obtt = OBT(pht->GetOrbitalTime()); |
560 |
|
}; |
561 |
|
// |
562 |
|
if ( lasthead < rhev ){ |
563 |
|
rh->GetEntry(lasthead); |
564 |
|
phh = ehh->GetPscuHeader(); |
565 |
|
pkth = PKT(phh->GetCounter()); |
566 |
|
obth = OBT(phh->GetOrbitalTime()); |
567 |
|
}; |
568 |
|
// |
569 |
|
if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
570 |
|
if ( pkth > upperpkt && obth > upperobt ){ |
571 |
|
if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt); |
572 |
|
upperpkt = pkth; |
573 |
|
upperobt = obth; |
574 |
|
rhev = lasthead+1; |
575 |
|
} else { |
576 |
|
rhev = lasthead; |
577 |
|
}; |
578 |
|
if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); |
579 |
|
// |
580 |
|
if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); |
581 |
|
if ( pktt > upperpkt && obtt > upperobt ){ |
582 |
|
if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt); |
583 |
|
upperpkt = pktt; |
584 |
|
upperobt = obtt; |
585 |
|
rtev = lasttrail+1; |
586 |
|
} else { |
587 |
|
rtev = lasttrail; |
588 |
|
}; |
589 |
|
if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); |
590 |
// |
// |
591 |
if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry); |
if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry); |
592 |
// |
// |
668 |
// PRIVATE FUNCTIONS |
// PRIVATE FUNCTIONS |
669 |
// |
// |
670 |
|
|
671 |
/** |
// /** |
672 |
* Open the ROOT filename for reading |
// * Open the ROOT filename for reading |
673 |
*/ |
// */ |
674 |
void PamelaDBOperations::OpenFile(){ |
// void PamelaDBOperations::OpenFile(){ |
675 |
file = TFile::Open(this->GetRootName().Data()); |
// file = TFile::Open(this->GetRootName().Data()); |
676 |
|
// // |
677 |
|
|
678 |
|
void PamelaDBOperations::CheckFile(){ |
679 |
|
if ( !file ) throw -12; |
680 |
}; |
}; |
681 |
|
|
682 |
|
|
683 |
/** |
/** |
684 |
* Check if LEVEL0 file and DB connection have really be opened |
* Check if LEVEL0 file and DB connection have really be opened |
685 |
*/ |
*/ |
686 |
void PamelaDBOperations::CheckFile(){ |
void PamelaDBOperations::CheckConnection(){ |
|
// |
|
|
if ( !file ) throw -12; |
|
687 |
// |
// |
688 |
// check connection |
// check connection |
689 |
// |
// |
690 |
if( !conn ) throw -1; |
if( !conn ) throw -1; |
691 |
bool connect = conn->IsConnected(); |
bool connect = conn->IsConnected(); |
692 |
if( !connect ) throw -1; |
if( !connect ) throw -1; |
693 |
}; |
}; |
694 |
|
|
695 |
/** |
/** |
950 |
// |
// |
951 |
TYPE = 55;//224; |
TYPE = 55;//224; |
952 |
// |
// |
953 |
|
if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT); |
954 |
|
// |
955 |
if ( TSYNC && OBT ){ |
if ( TSYNC && OBT ){ |
956 |
existsts = true; |
existsts = true; |
957 |
goto eout; |
goto eout; |
974 |
// |
// |
975 |
rt->SetBranchAddress("RunTrailer", &runt); |
rt->SetBranchAddress("RunTrailer", &runt); |
976 |
// |
// |
977 |
if ( rhev > 0 ){ |
Int_t nrhev = rh->GetEntries(); |
978 |
rh->GetEntry(0); |
Int_t nrtev = rt->GetEntries(); |
979 |
// |
if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev); |
980 |
TSYNC = runh->LAST_TIME_SYNC_INFO; |
// |
981 |
OBT = runh->OBT_TIME_SYNC * 1000; |
if ( nrhev > 0 ){ |
982 |
// |
for (Int_t i=0; i<nrhev; i++){ |
983 |
TYPE = 20; |
// |
984 |
// |
rh->GetEntry(i); |
985 |
if ( TSYNC && OBT ){ |
// |
986 |
existsts = true; |
TSYNC = runh->LAST_TIME_SYNC_INFO; |
987 |
goto eout; |
OBT = runh->OBT_TIME_SYNC * 1000; |
988 |
|
// |
989 |
|
TYPE = 20; |
990 |
|
// |
991 |
|
if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT); |
992 |
|
// |
993 |
|
if ( TSYNC && OBT ){ |
994 |
|
existsts = true; |
995 |
|
goto eout; |
996 |
|
}; |
997 |
}; |
}; |
998 |
// |
// |
999 |
}; |
}; |
1000 |
if ( rtev > 0 ){ |
if ( nrtev > 0 ){ |
1001 |
// |
// |
1002 |
if ( IsDebug() ) printf(" No runheader \n"); |
if ( IsDebug() ) printf(" No runheader \n"); |
1003 |
signal = 6; |
signal = 6; |
1004 |
// |
// |
1005 |
rt->GetEntry(0); |
for (Int_t i=0; i<nrtev; i++){ |
1006 |
// |
// |
1007 |
TSYNC = runt->LAST_TYME_SYNC_INFO; |
rt->GetEntry(i); |
1008 |
OBT = runt->OBT_TYME_SYNC * 1000; |
// |
1009 |
// |
TSYNC = runt->LAST_TYME_SYNC_INFO; |
1010 |
TYPE = 21; |
OBT = runt->OBT_TYME_SYNC * 1000; |
1011 |
// |
// |
1012 |
if ( TSYNC && OBT ){ |
TYPE = 21; |
1013 |
existsts = true; |
// |
1014 |
goto eout; |
if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT); |
1015 |
|
// |
1016 |
|
if ( TSYNC && OBT ){ |
1017 |
|
existsts = true; |
1018 |
|
goto eout; |
1019 |
|
}; |
1020 |
}; |
}; |
1021 |
// |
// |
1022 |
} else { |
} else { |
3226 |
TSQLRow *row2 = 0; |
TSQLRow *row2 = 0; |
3227 |
// |
// |
3228 |
UInt_t moved = 0; |
UInt_t moved = 0; |
3229 |
UInt_t timelim = 0; |
// UInt_t timelim = 0; |
3230 |
TDatime *time = new TDatime(); |
// TDatime *time = new TDatime(); |
3231 |
// |
// |
3232 |
stringstream oss; |
stringstream oss; |
3233 |
oss.str(""); |
oss.str(""); |
3234 |
// |
// |
3235 |
// |
// |
|
// |
|
|
if ( olderthan < 0 ){ |
|
|
if ( IsDebug() ) printf(" Do not clean GL_RUN_FRAGMENTS table \n"); |
|
|
return(1); |
|
|
}; |
|
|
// |
|
|
// timelim = now - olderthan |
|
|
// |
|
|
time->Set(); |
|
|
timelim = (UInt_t)time->Convert() - olderthan; |
|
|
time->Set(timelim,false); |
|
|
// |
|
3236 |
// check if there are entries older than "olderthan" seconds from now |
// check if there are entries older than "olderthan" seconds from now |
3237 |
// |
// |
3238 |
oss.str(""); |
oss.str(""); |
3239 |
oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" |
oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" |
3240 |
<< " INSERT_TIME <= '" << time->AsSQLString() << "';"; |
<< " INSERT_TIME <= '" << clean_time->AsSQLString() << "';"; |
3241 |
// |
// |
3242 |
if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str()); |
if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str()); |
3243 |
result = conn->Query(oss.str().c_str()); |
result = conn->Query(oss.str().c_str()); |
3244 |
// |
// |
3245 |
if ( result ){ |
if ( result ){ |
3246 |
// |
// |
3247 |
row = result->Next(); |
row = result->Next(); |
3248 |
// |
// |
3249 |
if ( row ){ |
while ( row ){ |
3250 |
// |
// |
3251 |
oss.str(""); |
oss.str(""); |
3252 |
oss << " ID= "<< row->GetField(0); |
oss << " ID= "<< row->GetField(0); |
3323 |
// |
// |
3324 |
if ( !result2 ) throw -4; |
if ( !result2 ) throw -4; |
3325 |
// |
// |
3326 |
|
row = result->Next(); |
3327 |
}; |
}; |
3328 |
}; |
}; |
3329 |
if ( IsDebug() ) printf(" Moved %u runs\n",moved); |
if ( IsDebug() ) printf(" Moved %u runs\n",moved); |
3336 |
Int_t PamelaDBOperations::ValidateRuns(){ |
Int_t PamelaDBOperations::ValidateRuns(){ |
3337 |
// |
// |
3338 |
TSQLResult *result = 0; |
TSQLResult *result = 0; |
3339 |
// TSQLRow *row = 0; |
TSQLRow *row = 0; |
|
// |
|
|
UInt_t timelim = 0; |
|
|
TDatime *time = new TDatime(); |
|
3340 |
// |
// |
3341 |
stringstream oss; |
stringstream oss; |
3342 |
oss.str(""); |
oss.str(""); |
3343 |
// |
// |
3344 |
// |
// ======================================================= |
3345 |
// |
// validate runs by checking missing calibrations |
3346 |
if ( olderthan < 0 ){ |
// ======================================================= |
3347 |
if ( IsDebug() ) printf(" Do not validate runs \n"); |
UInt_t t_stop = 0; |
3348 |
return(1); |
UInt_t t_start = 0; |
3349 |
|
// -------------------------------------------------------------- |
3350 |
|
// 1) get the OBT of the last run inserted after clean-time limit |
3351 |
|
// -------------------------------------------------------------- |
3352 |
|
oss.str(""); |
3353 |
|
oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString() |
3354 |
|
<< "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; |
3355 |
|
if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str()); |
3356 |
|
result = conn->Query(oss.str().c_str()); |
3357 |
|
if ( !result ) throw -4; |
3358 |
|
if ( !result->GetRowCount() ) { |
3359 |
|
printf(" No runs to validate \n"); |
3360 |
|
return(1); |
3361 |
|
}else{ |
3362 |
|
row = result->Next(); |
3363 |
|
t_start = (UInt_t)atoll(row->GetField(4)); |
3364 |
|
}; |
3365 |
|
// -------------------------------------------------------------- |
3366 |
|
// 2) get the OBT of the last validated run |
3367 |
|
// -------------------------------------------------------------- |
3368 |
|
oss.str(""); |
3369 |
|
oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start |
3370 |
|
<<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; |
3371 |
|
if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); |
3372 |
|
result = conn->Query(oss.str().c_str()); |
3373 |
|
if ( !result ) throw -4; |
3374 |
|
if ( result->GetRowCount() ){ |
3375 |
|
row = result->Next(); |
3376 |
|
t_stop = (UInt_t)atoll(row->GetField(4)); |
3377 |
}; |
}; |
3378 |
// |
if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start); |
3379 |
// timelim = now - olderthan |
// -------------------------------------------------------------- |
3380 |
// |
// now retrieves runs to be validated |
3381 |
time->Set(); |
// -------------------------------------------------------------- |
3382 |
timelim = (UInt_t)time->Convert() - olderthan; |
oss.str(""); |
3383 |
time->Set(timelim,false); |
oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start; |
3384 |
// |
oss << " AND RUNHEADER_TIME >="<< t_stop; |
3385 |
// First of all validate runs with default calibration: |
oss << " ORDER BY RUNHEADER_TIME DESC;"; |
3386 |
// |
// if ( IsDebug() ) |
3387 |
oss.str(""); |
if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str()); |
|
oss << " UPDATE GL_RUN SET VALIDATION=1 WHERE" |
|
|
<< " VALIDATION = 0 AND TRK_CALIB_USED=104 AND " |
|
|
<< " INSERT_TIME <= '" << time->AsSQLString() << "';"; |
|
|
// |
|
|
if ( IsDebug() ) printf(" Validate runs with trk default calibration inserted before %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str()); |
|
3388 |
result = conn->Query(oss.str().c_str()); |
result = conn->Query(oss.str().c_str()); |
|
// |
|
3389 |
if ( !result ) throw -4; |
if ( !result ) throw -4; |
3390 |
|
if ( !result->GetRowCount() ) printf(" No runs to validate \n"); |
3391 |
|
// printf("------------------------------------------------------------------------------- \n"); |
3392 |
|
|
3393 |
|
Int_t nrow = 0; |
3394 |
|
GL_RUN* this_run = new GL_RUN(); |
3395 |
|
GL_RUN* next_run = new GL_RUN(); |
3396 |
|
Int_t nseq_max = 1000; |
3397 |
|
// UInt_t* sequence = new UInt_t[100]; |
3398 |
|
vector<UInt_t> sequence(nseq_max); |
3399 |
|
Int_t nseq = 0; |
3400 |
|
Bool_t CHECK = false; |
3401 |
|
Bool_t this_ONLINE = false; |
3402 |
|
Bool_t next_ONLINE = false; |
3403 |
|
UInt_t t1=0,t2=0; |
3404 |
|
// --------------------------------------------------------------------------------- |
3405 |
|
// - loop over runs, back in time, |
3406 |
|
// - select sequences of runs close in time (less than 60 s apart), |
3407 |
|
// which could be preceeded by a calibration |
3408 |
|
// - check if there might be a missing calibration |
3409 |
|
// --------------------------------------------------------------------------------- |
3410 |
|
while(1){ |
3411 |
|
|
3412 |
|
row = result->Next(); |
3413 |
|
if( row == NULL ) break; |
3414 |
|
|
3415 |
|
//------------ |
3416 |
|
//get run info |
3417 |
|
//------------ |
3418 |
|
this_run->Set_GL_RUN(row); |
3419 |
|
|
3420 |
|
Bool_t this_BAD = false; |
3421 |
|
if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true; |
3422 |
|
else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false; |
3423 |
|
else{ |
3424 |
|
// printf("Missing or corrupted header!! \n"); |
3425 |
|
this_ONLINE = false; |
3426 |
|
this_BAD = true; |
3427 |
|
}; |
3428 |
|
|
3429 |
|
//----------------------------------- |
3430 |
|
//compare with previous(next in time) |
3431 |
|
//----------------------------------- |
3432 |
|
CHECK = false; |
3433 |
|
UInt_t interval=0; |
3434 |
|
|
3435 |
|
if( nrow != 0){ |
3436 |
|
|
3437 |
|
|
3438 |
|
t1 = this_run->GetRUNTRAILER_TIME(); |
3439 |
|
t2 = next_run->GetRUNHEADER_TIME(); |
3440 |
|
interval = (t2-t1); |
3441 |
|
|
3442 |
|
if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE |
3443 |
|
|
3444 |
|
if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments |
3445 |
|
|
3446 |
|
if( interval >= 60 )CHECK = true; //more than 60 s => there might be a calibration |
3447 |
|
|
3448 |
|
if( !CHECK && this_run->VALIDATION ){ |
3449 |
|
for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); |
3450 |
|
nseq=0; |
3451 |
|
} |
3452 |
|
|
3453 |
|
}else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE |
3454 |
|
|
3455 |
|
CHECK = true; |
3456 |
|
|
3457 |
|
}else if( !next_ONLINE ){ // this:ANY + next:DEFAULT |
3458 |
|
|
3459 |
|
assignVALIDATION(next_run->ID,true); |
3460 |
|
nseq=0; |
3461 |
|
} |
3462 |
|
} |
3463 |
|
|
3464 |
|
//---------------------------- |
3465 |
|
//check run sequence for calib |
3466 |
|
//---------------------------- |
3467 |
|
if( CHECK ){ |
3468 |
|
// check if calibration exists |
3469 |
|
if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval); |
3470 |
|
Bool_t MISSING = MissingTRK_CALIB(t1,t2); |
3471 |
|
for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING); |
3472 |
|
nseq=0; |
3473 |
|
}; |
3474 |
|
//-------------- |
3475 |
|
//store run info |
3476 |
|
//-------------- |
3477 |
|
*next_run = *this_run; |
3478 |
|
next_ONLINE = this_ONLINE; |
3479 |
|
if( !this_BAD ){ |
3480 |
|
if(nseq < nseq_max){ |
3481 |
|
sequence[nseq] = this_run->ID; |
3482 |
|
nseq++; |
3483 |
|
}else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max); |
3484 |
|
}; |
3485 |
|
|
3486 |
|
if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID); |
3487 |
|
nrow++; |
3488 |
|
|
3489 |
|
}; |
3490 |
|
delete this_run; |
3491 |
|
delete next_run; |
3492 |
// |
// |
3493 |
return(0); |
return(0); |
3494 |
}; |
}; |
3495 |
|
/** |
3496 |
|
* Check if there might be a missing tracker calibration in a given time interval |
3497 |
|
* @param t1 From absolute time |
3498 |
|
* @param t2 To absolute time |
3499 |
|
* @return true if there might be a missing calibration |
3500 |
|
*/ |
3501 |
|
Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){ |
3502 |
|
|
3503 |
|
GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB(); |
3504 |
|
|
3505 |
|
// get the closest VALIDATED calibration before the run start (t2) |
3506 |
|
if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing |
3507 |
|
|
3508 |
|
if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing |
3509 |
|
|
3510 |
|
//============================================================== |
3511 |
|
// Check is done first on the basis of time between calibration, |
3512 |
|
// which should be equal to the time between ascending-nodes. |
3513 |
|
//============================================================== |
3514 |
|
if ( t2 - trkcalib->FROM_TIME > 5700) { |
3515 |
|
if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); |
3516 |
|
//============================================================== |
3517 |
|
// there might be a missing calibration, due to: |
3518 |
|
// - MM full |
3519 |
|
// - corrupted packets |
3520 |
|
// - loss of data |
3521 |
|
// There is an exception in case a download was done during ascending node |
3522 |
|
//============================================================== |
3523 |
|
Bool_t DOWNLOAD = false; |
3524 |
|
// check if the calib was skipped becouse of download .... DA FARE!! |
3525 |
|
if(DOWNLOAD)return(false); |
3526 |
|
|
3527 |
|
return(true); //>>> missing |
3528 |
|
|
3529 |
|
}; |
3530 |
|
|
3531 |
|
//============================================================== |
3532 |
|
// If the last calibration is close to the run less than this time, |
3533 |
|
// it is enough to say that there are no missing calibrations |
3534 |
|
//============================================================== |
3535 |
|
// the long time interval bewteen runs might be due to download |
3536 |
|
if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); |
3537 |
|
return(false); |
3538 |
|
|
3539 |
|
}; |
3540 |
|
/** |
3541 |
|
* Assign VALIDATION value to a GL_RUN entry |
3542 |
|
* @param idrun Run ID |
3543 |
|
* @param validation true/false |
3544 |
|
*/ |
3545 |
|
Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){ |
3546 |
|
TSQLResult *result = 0; |
3547 |
|
stringstream oss; |
3548 |
|
oss.str(""); |
3549 |
|
oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";"; |
3550 |
|
// |
3551 |
|
// if ( IsDebug() ) |
3552 |
|
// printf(" Set VALIDATION = %i for run %i \n",validation,idrun); |
3553 |
|
if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str()); |
3554 |
|
result = conn->Query(oss.str().c_str()); |
3555 |
|
if ( !result ) throw -4; |
3556 |
|
return(0); |
3557 |
|
} |
3558 |
|
|
3559 |
|
|
3560 |
|
|
3561 |
|
// Insert TLEs from file tlefilename in the table GL_TLE in the db |
3562 |
|
// opened by conn, sorting them by date from older to newer, if each |
3563 |
|
// TLE has not been alread inserted. |
3564 |
|
Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile) |
3565 |
|
{ |
3566 |
|
fstream tlefile(tlefilename, ios::in); |
3567 |
|
|
3568 |
|
if ( !tlefile ) throw -7; |
3569 |
|
|
3570 |
|
vector<cTle*> ctles; |
3571 |
|
vector<cTle*>::iterator iter; |
3572 |
|
int present = 0; |
3573 |
|
|
3574 |
|
// Get three lines from tlefile, create a cTle object and put it |
3575 |
|
// into ctles |
3576 |
|
while(1) { |
3577 |
|
cTle *tlef; |
3578 |
|
string str1, str2, str3; |
3579 |
|
|
3580 |
|
getline(tlefile, str1); |
3581 |
|
if(tlefile.eof()) break; |
3582 |
|
|
3583 |
|
getline(tlefile, str2); |
3584 |
|
if(tlefile.eof()) break; |
3585 |
|
|
3586 |
|
getline(tlefile, str3); |
3587 |
|
if(tlefile.eof()) break; |
3588 |
|
|
3589 |
|
// We now have three good lines for a cTle. |
3590 |
|
tlef = new cTle(str1, str2, str3); |
3591 |
|
ctles.push_back(tlef); |
3592 |
|
} |
3593 |
|
|
3594 |
|
tlefile.close(); |
3595 |
|
|
3596 |
|
// Sort by date |
3597 |
|
sort(ctles.begin(), ctles.end(), compTLE); |
3598 |
|
|
3599 |
|
// Now we insert each TLE into the db |
3600 |
|
for(iter = ctles.begin(); iter != ctles.end(); iter++) { |
3601 |
|
cTle *tle = *iter; |
3602 |
|
|
3603 |
|
// Do nothing if it's already present in the db. Just increase |
3604 |
|
// the counter present. |
3605 |
|
if (! isTlePresent(tle)) |
3606 |
|
{ |
3607 |
|
int status = insertTle(tle); |
3608 |
|
|
3609 |
|
// Insert query failed. Return 1. |
3610 |
|
if(status == EXIT_FAILURE) { |
3611 |
|
|
3612 |
|
if( IsDebug() ) { |
3613 |
|
cerr << "Error: inserting TLE:" << endl |
3614 |
|
<< tle->getName() << endl |
3615 |
|
<< tle->getLine1() << endl |
3616 |
|
<< tle->getLine2() << endl; |
3617 |
|
} |
3618 |
|
|
3619 |
|
throw -4; |
3620 |
|
return 1; |
3621 |
|
} |
3622 |
|
|
3623 |
|
} |
3624 |
|
else |
3625 |
|
present++; |
3626 |
|
|
3627 |
|
} |
3628 |
|
|
3629 |
|
int inserted = ctles.size() - present; // Number of inserted TLE. |
3630 |
|
if ( IsDebug() ) |
3631 |
|
cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl |
3632 |
|
<< inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl; |
3633 |
|
|
3634 |
|
ctles.clear(); |
3635 |
|
|
3636 |
|
|
3637 |
|
// Return 2 if no new TLE has been inserted. 0 otherwise. |
3638 |
|
if(! inserted ) return 2; |
3639 |
|
return 0; |
3640 |
|
} |
3641 |
|
|
3642 |
|
|
3643 |
|
// Insert tle in the table GL_TLE using the connection conn. |
3644 |
|
int PamelaDBOperations::insertTle(cTle *tle) |
3645 |
|
{ |
3646 |
|
stringstream oss; |
3647 |
|
TSQLResult *result = 0; |
3648 |
|
|
3649 |
|
oss.str(""); |
3650 |
|
oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)" |
3651 |
|
<< " VALUES ( '" |
3652 |
|
<< tle->getName() << "', '" |
3653 |
|
<< tle->getLine1() << "', '" |
3654 |
|
<< tle->getLine2() << "', '" |
3655 |
|
<< getTleDatetime(tle) << "')"; |
3656 |
|
|
3657 |
|
// cout << oss.str().c_str() << endl; |
3658 |
|
result = conn->Query(oss.str().c_str()); |
3659 |
|
if (result == NULL) |
3660 |
|
return EXIT_FAILURE; |
3661 |
|
|
3662 |
|
return EXIT_SUCCESS; |
3663 |
|
} |
3664 |
|
|
3665 |
|
|
3666 |
|
// Return whether tle is already in the db connected by conn. |
3667 |
|
bool PamelaDBOperations::isTlePresent(cTle *tle) |
3668 |
|
{ |
3669 |
|
stringstream oss; |
3670 |
|
TSQLResult *result = 0; |
3671 |
|
|
3672 |
|
oss.str(""); |
3673 |
|
oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '" |
3674 |
|
<< getTleDatetime(tle) << "'"; |
3675 |
|
|
3676 |
|
result = conn->Query(oss.str().c_str()); |
3677 |
|
if (result == NULL) throw -4; |
3678 |
|
|
3679 |
|
if (result->GetRowCount()) |
3680 |
|
return true; |
3681 |
|
else |
3682 |
|
return false; |
3683 |
|
} |
3684 |
|
|
3685 |
|
|
3686 |
|
// Return whether the first TLE is dated early than the second |
3687 |
|
bool compTLE (cTle *tle1, cTle *tle2) |
3688 |
|
{ |
3689 |
|
return getTleJulian(tle1) < getTleJulian(tle2); |
3690 |
|
} |
3691 |
|
|
3692 |
|
|
3693 |
|
// Return the date of the tle using the format (year-2000)*1e3 + |
3694 |
|
// julian day. e.g. 6365 is the 31th Dec 2006. |
3695 |
|
// It does *not* return a cJulian date. |
3696 |
|
float getTleJulian(cTle *tle) { |
3697 |
|
return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY); |
3698 |
|
} |
3699 |
|
|
3700 |
|
|
3701 |
|
// Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime |
3702 |
|
// format. |
3703 |
|
string getTleDatetime(cTle *tle) |
3704 |
|
{ |
3705 |
|
int year, mon, day, hh, mm, ss; |
3706 |
|
double dom; // day of month (is double!) |
3707 |
|
stringstream date; // date in datetime format |
3708 |
|
|
3709 |
|
// create a cJulian from the date in tle |
3710 |
|
cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY)); |
3711 |
|
|
3712 |
|
// get year, month, day of month |
3713 |
|
jdate.getComponent(&year, &mon, &dom); |
3714 |
|
|
3715 |
|
// build a datetime YYYY-MM-DD hh:mm:ss |
3716 |
|
date.str(""); |
3717 |
|
day = (int) floor(dom); |
3718 |
|
hh = (int) floor( (dom - day) * 24); |
3719 |
|
mm = (int) floor( ((dom - day) * 24 - hh) * 60); |
3720 |
|
ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60)); |
3721 |
|
// ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000); |
3722 |
|
|
3723 |
|
date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss; |
3724 |
|
|
3725 |
|
return date.str(); |
3726 |
|
} |