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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (show annotations) (download)
Tue Apr 17 12:07:01 2007 UTC (17 years, 7 months ago) by mocchiut
Branch: MAIN
Changes since 1.28: +63 -17 lines
Symbolic path in DB + default installation of GL_PARAM + moved here install_GL_PARAM

1 //
2 #include <iomanip>
3 #include <sstream>
4 //
5 #include <iostream>
6 #include <string>
7 #include <fstream>
8 #include <list>
9 #include <errno.h>
10 //
11 #include <TFile.h>
12 #include <TSystem.h>
13 #include <TSQLResult.h>
14 #include <TSQLRow.h>
15 #include <TTree.h>
16 #include <TGraph.h>
17 #include <TTimeStamp.h>
18 #include <TF1.h>
19 //
20 #include <EventHeader.h>
21 #include <PscuHeader.h>
22 #include <mcmd/McmdEvent.h>
23 #include <mcmd/McmdRecord.h>
24 #include <RunHeaderEvent.h>
25 #include <RunTrailerEvent.h>
26 #include <CalibCalPedEvent.h>
27 #include <CalibS4Event.h>
28 #include <CalibTrk1Event.h>
29 #include <CalibTrk2Event.h>
30 #include <varDump/VarDumpEvent.h>
31 #include <varDump/VarDumpRecord.h>
32 #include <physics/S4/S4Event.h>
33 //
34 #include <sgp4.h>
35
36 #include <PamelaDBOperations.h>
37 //
38 using namespace std;
39 using namespace pamela;
40
41 // Some function to work with cTle stuff.
42 bool compTLE(cTle* tle1, cTle *tle2);
43 float getTleJulian(cTle *);
44 string getTleDatetime(cTle*);
45
46 /**
47 * Constructor.
48 * @param host hostname for the SQL connection.
49 * @param user username for the SQL connection.
50 * @param password password for the SQL connection.
51 * @param filerawname The path and name to the raw file.
52 * @param filerootname The path and name of the raw file.
53 * @param boot file BOOT number.
54 * @param obt0 file obt0.
55 * @param tsync file timesync.
56 * @param debug debug flag.
57 * @param tlefilename ascii file with TLE 3 line elements.
58 */
59 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, UInt_t dwinput, Bool_t staticp){
60 //
61 STATIC=false;
62 if ( staticp ) STATIC=true;
63 //
64 SetConnection(host,user,password);
65 //
66 SetDebugFlag(debug);
67 //
68 glrun = new GL_RUN();
69 //
70 if ( !boot ) SetNOBOOT(false);
71 SetBOOTnumber(boot);
72 SetTsync(tsync);
73 SetObt0(obt0);
74 //
75 SetTLEPath(tlefilename);
76 //
77 //
78 INSERT_RAW =!filerawname.IsNull();
79 if(INSERT_RAW)SetRawName(filerawname);
80 //
81 INSERT_ROOT = !filerootname.IsNull();
82 if( INSERT_ROOT ){
83 this->SetRootName(filerootname);
84 this->SetOrbitNo(dwinput);
85 file = TFile::Open(this->GetRootName().Data());
86 } else {
87 this->SetRootName("");
88 };
89 //
90 this->SetID_RAW(0);
91 this->SetID_ROOT(0);
92
93 VALIDATE = false;
94
95 //
96 };
97
98 /**
99 * Destructor
100 */
101 void PamelaDBOperations::Close(){
102 if( conn && conn->IsConnected() ) conn->Close();
103 delete clean_time;
104 delete glrun;
105 delete this;
106 };
107
108 //
109 // SETTERS
110 //
111
112 //
113 // 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
114 //
115 void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116 clean_time = new TDatime();
117 //
118 if(olderthan >= 0){
119 VALIDATE = true;
120 UInt_t timelim = 0;
121 timelim = (UInt_t)clean_time->Convert(true) - olderthan;
122 clean_time->Set(timelim,false);
123 };
124 };
125
126 /**
127 * Open the DB connection
128 * @param host hostname for the SQL connection.
129 * @param user username for the SQL connection.
130 * @param password password for the SQL connection.
131 */
132 void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
133 if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
134 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
135 };
136
137 /**
138 * Store the ID of the ROOT file.
139 * @param idr ID of the ROOT file
140 */
141 void PamelaDBOperations::SetID_ROOT(UInt_t idr){
142 idroot=idr;
143 };
144
145 /**
146 * Store the ID of the RAW file.
147 * @param idr ID of the RAW file
148 */
149 void PamelaDBOperations::SetID_RAW(UInt_t idr){
150 id=idr;
151 };
152
153 /**
154 * Set the debug flag
155 *
156 */
157 void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
158 debug = dbg;
159 };
160
161 /**
162 * Set the autoboot flag
163 *
164 */
165 void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166 AUTOBOOT = dbg;
167 };
168
169 /**
170 * Set the nofrag flag
171 *
172 */
173 void PamelaDBOperations::SetNoFrag(Bool_t nf){
174 NOFRAG = nf;
175 };
176
177 /**
178 * Store the BOOT number of the RAW file.
179 * @param boot BOOT number of the RAW file
180 */
181 void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
182 BOOTNO=boot;
183 };
184
185 /**
186 * Store the time sync of the RAW file.
187 * @param boot time sync
188 */
189 void PamelaDBOperations::SetTsync(UInt_t ts){
190 tsync=ts;
191 };
192
193 /**
194 * Store the time sync of the RAW file.
195 * @param boot time sync
196 */
197 void PamelaDBOperations::SetObt0(UInt_t ts){
198 obt0=ts;
199 };
200
201 /**
202 * Store the RAW filename.
203 * @param str the RAW filename.
204 */
205 void PamelaDBOperations::SetRawName(TString str){
206 filerawname=str;
207 };
208
209 /**
210 * Store the ROOT filename.
211 * @param str the ROOT filename.
212 */
213 void PamelaDBOperations::SetRootName(TString str){
214 filerootname=str;
215 };
216
217 /**
218 * Store the downlink orbit number from filename.
219 */
220 void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
221 dworbit = 0;
222 //
223 if ( dwinput ){
224 dworbit = dwinput;
225 if ( IsDebug() ) printf(" Downlink orbit given by hand: %i \n",dworbit);
226 return;
227 };
228 //
229 TString name = this->GetRootFile();
230 Int_t nlength = name.Length();
231 if ( nlength < 5 ) return;
232 TString dwo = 0;
233 for (Int_t i = 0; i<5; i++){
234 dwo.Append(name[i],1);
235 };
236 if ( dwo.IsDigit() ){
237 dworbit = (UInt_t)dwo.Atoi();
238 } else {
239 dwo="";
240 for (Int_t i = 8; i<13; i++){
241 dwo.Append(name[i],1);
242 };
243 if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
244 };
245 if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
246 return;
247 };
248
249
250
251 /**
252 * Store the NOBOOT flag.
253 * @param noboot true/false.
254 */
255 void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
256 NOBOOT = noboot;
257 };
258
259 /**
260 * Store path to the TLE file.
261 */
262 void PamelaDBOperations::SetTLEPath(TString str){
263 tlefilename = str;
264 };
265
266 TString PamelaDBOperations::GetRawPath(){
267 if ( STATIC ){
268 return((TString)gSystem->DirName(filerawname.Data())+'/');
269 } else {
270 return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
271 };
272 };
273
274 TString PamelaDBOperations::GetRootPath(){
275 if ( STATIC ){
276 return((TString)gSystem->DirName(filerootname.Data())+'/');
277 } else {
278 return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
279 };
280 };
281
282 /**
283 * Store the olderthan variable
284 * @param olderthan
285 */
286 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
287 // olderthan = oldthan;
288 // };
289
290 /**
291 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
292 */
293 Bool_t PamelaDBOperations::SetID_RAW(){
294 stringstream oss;
295 TSQLResult *result = 0;
296 TSQLRow *row = 0;
297 oss.str("");
298 if ( STATIC ){
299 oss << "SELECT ID FROM GL_RAW WHERE "
300 << " PATH = '" << this->GetRawPath().Data() << "' AND "
301 << " NAME = '" << this->GetRawFile().Data() << "' ";
302 } else {
303 oss << "SELECT ID FROM GL_RAW WHERE "
304 << " PATH = '$PAM_RAW' AND "
305 << " NAME = '" << this->GetRawFile().Data() << "' ";
306 }
307 result = conn->Query(oss.str().c_str());
308 if ( result == NULL ) throw -4;
309 row = result->Next();
310 if ( !row ) return(false);
311 delete result;
312 id = (UInt_t)atoll(row->GetField(0));
313 return(true);
314 }
315
316 /**
317 *
318 * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
319 *
320 */
321 void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
322 glrun->SetBOOTNUMBER(BOOTNO);
323 glrun->SetRUNHEADER_TIME(absth);
324 glrun->SetRUNTRAILER_TIME(abstt);
325 glrun->SetID_ROOT_L2(0);
326 glrun->SetID_ROOT_L0(idroot);
327 glrun->SetVALIDATION(0);
328 };
329
330 /**
331 * Patch, look for upper limits to avoid processing retransmitted data
332 */
333 Int_t PamelaDBOperations::SetUpperLimits(){
334 UInt_t nevent = 0;
335 UInt_t pktlast = 0;
336 UInt_t obtlast = 0;
337 Long64_t t_pktlast = 0LL;
338 // UInt_t t_obtlast = 0;
339 Long64_t t_obtlast = 0LL;
340 Long64_t upperpkt2 = 0LL;
341 Long64_t upperobt2 = 0LL;
342 UInt_t zomp = 0;
343 UInt_t jump = 50000; // was 5000
344 EventCounter *code=0;
345 //
346 Long64_t deltapkt = 5000LL;
347 Long64_t deltaobt = 50000LL;
348 //
349 pcksList packetsNames;
350 pcksList::iterator Iter;
351 getPacketsNames(packetsNames);
352 //
353 pktfirst = 0;
354 obtfirst = 0;
355 ppktfirst = 0;
356 pobtfirst = 0;
357 //
358 TTree *T = 0;
359 T = (TTree*)file->Get("Physics");
360 if ( !T || T->IsZombie() ) throw -16;
361 EventHeader *eh = 0;
362 PscuHeader *ph = 0;
363 T->SetBranchAddress("Header", &eh);
364 nevent = T->GetEntries();
365 //
366 T->GetEntry(0);
367 ph = eh->GetPscuHeader();
368 pktfirst = ph->GetCounter();
369 obtfirst = ph->GetOrbitalTime();
370 ppktfirst = pktfirst;
371 pobtfirst = obtfirst;
372 //
373 code = eh->GetCounter();
374 UInt_t en = 0;
375 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
376 en = code->Get(GetPacketType(*Iter));
377 if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
378 if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
379 //
380 TTree *TC = 0;
381 TC = (TTree*)file->Get("CalibCalPed");
382 if ( !TC || TC->IsZombie() ) throw -16;
383 EventHeader *ehc = 0;
384 PscuHeader *phc = 0;
385 TC->SetBranchAddress("Header", &ehc);
386 TC->GetEntry(0);
387 phc = ehc->GetPscuHeader();
388 pktfirst = phc->GetCounter();
389 obtfirst = phc->GetOrbitalTime();
390 //
391 };
392 };
393 //
394 T->GetEntry(nevent-1);
395 ph = eh->GetPscuHeader();
396 pktlast = ph->GetCounter();
397 obtlast = ph->GetOrbitalTime();
398 //
399 upperpkt = PKT(pktlast);
400 upperobt = OBT(obtlast);
401 upperentry = nevent-1;
402 //
403 if ( IsDebug() ) printf(" First entries are: OBT %i pkt_num %i \n",obtfirst,pktfirst);
404 //
405 if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
406 //
407 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);
408 //
409 if ( !nevent ) return(2);
410 //
411 if ( nevent < 2 ) return(4);
412 if ( nevent < jump ) jump = 1;
413 // if ( nevent < jump ) jump = int(nevent/10);
414 // if ( !jump ) jump = 1;
415 //
416 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
417 //
418 if ( IsDebug() ) printf(" starting jump %i \n",jump);
419 // go back
420 zomp = nevent - 2;
421 //
422 while ( jump > 0 ){
423 //
424 t_pktlast = PKT(pktlast);
425 t_obtlast = OBT(obtlast);
426 //
427 for (UInt_t i = zomp; i>1; i-=jump){
428 //
429 if ( i >= 0 ) T->GetEntry(i);
430 ph = eh->GetPscuHeader();
431 upperpkt = PKT(ph->GetCounter());
432 upperobt = OBT(ph->GetOrbitalTime());
433 upperentry = i;
434 //
435 if ( (i-1) >= 0 ) T->GetEntry(i-1);
436 ph = eh->GetPscuHeader();
437 upperpkt2 = PKT(ph->GetCounter());
438 upperobt2 = OBT(ph->GetOrbitalTime());
439 //
440 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
441 if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
442 if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
443 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
444 throw -13;
445 };
446 //
447 if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
448 zomp = i + jump + 1;
449 if ( zomp > nevent-2 ) zomp = nevent - 2;
450 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %i upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
451 break;
452 };
453 //
454 t_pktlast = upperpkt;
455 t_obtlast = upperobt;
456 };
457 //
458 if ( jump == 1 ) jump = 0;
459 if ( jump == 10 ) jump = 1;
460 if ( jump == 100 ) jump = 10;
461 if ( jump == 1000 ) jump = 100;
462 if ( jump == 5000 ) jump = 1000;
463 if ( jump == 50000 ) jump = 5000;
464 //
465 };
466 //
467 };
468 //
469 // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
470 //
471 PacketType *pctp=0;
472 TTree *rh=(TTree*)file->Get("RunHeader");
473 if ( !rh || rh->IsZombie() ) throw -17;
474 TTree *rt=(TTree*)file->Get("RunTrailer");
475 if ( !rt || rt->IsZombie() ) throw -18;
476 //
477 rh->SetBranchAddress("RunHeader", &runh);
478 rh->SetBranchAddress("Header", &ehh);
479 //
480 rt->SetBranchAddress("RunTrailer", &runt);
481 rt->SetBranchAddress("Header", &eht);
482 //
483 rhev = rh->GetEntries();
484 rtev = rt->GetEntries();
485 Long64_t sobtt = 0LL;
486 Long64_t sobth = 0LL;
487 Long64_t spktt = 0LL;
488 Long64_t spkth = 0LL;
489 Long64_t pktt = 0LL;
490 Long64_t obtt = 0LL;
491 Long64_t pkth = 0LL;
492 Long64_t obth = 0LL;
493 //
494 if ( rhev || rtev ){
495
496 T->GetEntry(upperentry);
497 code = eh->GetCounter();
498 Int_t lasttrail = code->Get(pctp->RunTrailer);
499 Int_t lasthead = code->Get(pctp->RunHeader);
500 if ( lasttrail < rtev ){
501 rt->GetEntry(lasttrail);
502 pht = eht->GetPscuHeader();
503 pktt = PKT(pht->GetCounter());
504 obtt = OBT(pht->GetOrbitalTime());
505 };
506 //
507 if ( lasthead < rhev ){
508 rh->GetEntry(lasthead);
509 phh = ehh->GetPscuHeader();
510 pkth = PKT(phh->GetCounter());
511 obth = OBT(phh->GetOrbitalTime());
512 };
513 //
514 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515 if ( pkth > upperpkt && obth > upperobt ){
516 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517 upperpkt = pkth;
518 upperobt = obth;
519 rhev = lasthead+1;
520 } else {
521 rhev = lasthead;
522 };
523 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524 //
525 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526 if ( pktt > upperpkt && obtt > upperobt ){
527 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528 upperpkt = pktt;
529 upperobt = obtt;
530 rtev = lasttrail+1;
531 } else {
532 rtev = lasttrail;
533 };
534 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535 // goto kikko;
536 //
537 //
538 // Check if runtrailer/runheader are within lower limits
539 //
540 //
541 pkth = 0LL;
542 obth = 0LL;
543 spkth = 0LL;
544 sobth = 0LL;
545 for (Int_t k=0; k<rhev; k++){
546 if ( k > 0 ){
547 spkth = pkth;
548 sobth = obth;
549 };
550 rh->GetEntry(k);
551 phh = ehh->GetPscuHeader();
552 pkth = PKT(phh->GetCounter());
553 obth = OBT(phh->GetOrbitalTime());
554 //
555 // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556 //
557 if ( pkth < spkth && obth < sobth ){
558 if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
559 //
560 rhev = k-1;
561 rh->GetEntry(rhev);
562 pkth = spkth;
563 obth = sobth;
564 //
565 UInt_t evbefh = 0;
566 code = ehh->GetCounter();
567 evbefh = code->Get(pctp->Physics);
568 if ( evbefh >= 0 ){
569 T->GetEntry(evbefh);
570 ph = eh->GetPscuHeader();
571 t_pktlast = PKT(ph->GetCounter());
572 t_obtlast = OBT(ph->GetOrbitalTime());
573 if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
574 upperpkt = pkth;
575 upperobt = obth;
576 upperentry = evbefh-1;
577 } else {
578 while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
579 evbefh++;
580 T->GetEntry(evbefh);
581 ph = eh->GetPscuHeader();
582 t_pktlast = PKT(ph->GetCounter());
583 t_obtlast = OBT(ph->GetOrbitalTime());
584 };
585 T->GetEntry(evbefh-1);
586 ph = eh->GetPscuHeader();
587 upperpkt = PKT(ph->GetCounter());
588 upperobt = OBT(ph->GetOrbitalTime());
589 upperentry = evbefh-1;
590 };
591 };
592 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
593 goto kikko0;
594 };
595 };
596 kikko0:
597 //
598 //
599 //
600 pktt = 0LL;
601 obtt = 0LL;
602 spktt = 0LL;
603 sobtt = 0LL;
604 for (Int_t k=0; k<rtev; k++){
605 if ( k > 0 ){
606 spktt = pktt;
607 sobtt = obtt;
608 };
609 rt->GetEntry(k);
610 pht = eht->GetPscuHeader();
611 pktt = PKT(pht->GetCounter());
612 obtt = OBT(pht->GetOrbitalTime());
613 //
614 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
615 //
616 if ( pktt < spktt && obtt < sobtt ){
617 if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
618 //
619 rtev = k-1;
620 rt->GetEntry(rtev);
621 pktt = spktt;
622 obtt = sobtt;
623 if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
624 //
625 UInt_t evbeft = 0;
626 code = eht->GetCounter();
627 evbeft = code->Get(pctp->Physics);
628 if ( evbeft >= 0 ){
629 T->GetEntry(evbeft);
630 ph = eh->GetPscuHeader();
631 t_pktlast = PKT(ph->GetCounter());
632 t_obtlast = OBT(ph->GetOrbitalTime());
633 if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
634 upperpkt = pktt;
635 upperobt = obtt;
636 upperentry = evbeft-1;
637 } else {
638 while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
639 evbeft++;
640 T->GetEntry(evbeft);
641 ph = eh->GetPscuHeader();
642 t_pktlast = PKT(ph->GetCounter());
643 t_obtlast = OBT(ph->GetOrbitalTime());
644 };
645 T->GetEntry(evbeft-1);
646 ph = eh->GetPscuHeader();
647 upperpkt = PKT(ph->GetCounter());
648 upperobt = OBT(ph->GetOrbitalTime());
649 upperentry = evbeft-1;
650 };
651 };
652 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
653 goto kikko;
654 // break;
655 //
656 };
657 //
658 };
659 //
660 kikko:
661 //
662 T->GetEntry(upperentry);
663 code = eh->GetCounter();
664 lasttrail = code->Get(pctp->RunTrailer);
665 lasthead = code->Get(pctp->RunHeader);
666 if ( lasttrail < rtev ){
667 rt->GetEntry(lasttrail);
668 pht = eht->GetPscuHeader();
669 pktt = PKT(pht->GetCounter());
670 obtt = OBT(pht->GetOrbitalTime());
671 };
672 //
673 if ( lasthead < rhev ){
674 rh->GetEntry(lasthead);
675 phh = ehh->GetPscuHeader();
676 pkth = PKT(phh->GetCounter());
677 obth = OBT(phh->GetOrbitalTime());
678 };
679 //
680 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
681 if ( pkth > upperpkt && obth > upperobt ){
682 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
683 upperpkt = pkth;
684 upperobt = obth;
685 rhev = lasthead+1;
686 } else {
687 rhev = lasthead;
688 };
689 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
690 //
691 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
692 if ( pktt > upperpkt && obtt > upperobt ){
693 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
694 upperpkt = pktt;
695 upperobt = obtt;
696 rtev = lasttrail+1;
697 } else {
698 rtev = lasttrail;
699 };
700 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
701 //
702 };
703 //
704 if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
705 //
706 return(0);
707 }
708
709 /**
710 *
711 * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
712 * Entries in the _RUNID_GEN table are never deleted.
713 *
714 **/
715 UInt_t PamelaDBOperations::AssignRunID(){
716 //
717 TSQLResult *result = 0;
718 TSQLRow *row = 0;
719 UInt_t runid = 0;
720 //
721 stringstream oss;
722 //
723 oss.str("");
724 oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
725 result = conn->Query(oss.str().c_str());
726 if ( !result ) throw -10;
727 oss.str("");
728 oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
729 result = conn->Query(oss.str().c_str());
730 if ( !result ) throw -10;
731 //
732 row = result->Next();
733 //
734 if ( !row ) throw -28;
735 //
736 runid = (UInt_t)atoll(row->GetField(0));
737 //
738 return(runid);
739 };
740
741 //
742 // GETTERS
743 //
744
745 /**
746 *
747 * Returns the DB absolute time needed to associate calibrations to data
748 *
749 */
750 UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
751 //
752 return(((UInt_t)(OBT(obt)/1000)+toffset));
753 //
754 };
755
756 /**
757 *
758 * List of packet types (just to make easily the loops)
759 *
760 */
761 const PacketType* PamelaDBOperations::GetPacketType(const char* type){
762 if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
763 if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
764 if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
765 if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
766 if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
767 if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
768 if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
769 if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
770 if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
771 if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
772 if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
773 if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
774 if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
775 if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
776 if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
777 if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
778 if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
779 if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
780 if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
781 if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
782 if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
783 if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
784 if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
785 if ( !strcmp(type,"Log") ) return(PacketType::Log);
786 if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
787 if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
788 if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
789 if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
790 if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
791 if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
792 if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
793 if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
794 if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
795 if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
796 if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
797 if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
798 if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
799 if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
800 if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
801 if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
802 if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
803 if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
804 if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
805 if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
806 if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
807 if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
808 if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
809 return(PacketType::Invalid);
810 };
811
812 //
813 // PRIVATE FUNCTIONS
814 //
815
816 // /**
817 // * Open the ROOT filename for reading
818 // */
819 // void PamelaDBOperations::OpenFile(){
820 // file = TFile::Open(this->GetRootName().Data());
821 // //
822
823 void PamelaDBOperations::CheckFile(){
824 if ( !file ) throw -12;
825 };
826
827
828 /**
829 * Check if LEVEL0 file and DB connection have really be opened
830 */
831 void PamelaDBOperations::CheckConnection(){
832 //
833 // check connection
834 //
835 if( !conn ) throw -1;
836 bool connect = conn->IsConnected();
837 if( !connect ) throw -1;
838 //
839 if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
840 //
841 if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
842 //
843 // set DB timezone to UTC
844 //
845 stringstream oss;
846 //
847 oss.str("");
848 oss << "SET time_zone='+0:00';";
849 TSQLResult *result = 0;
850 result = conn->Query(oss.str().c_str());
851 if ( !result ) throw -10;
852 oss.str("");
853 oss << "SET wait_timeout=173000;";
854 conn->Query(oss.str().c_str());
855 //
856 };
857
858 /**
859 * Return the correct packet number if we went back to zero
860 */
861 Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
862 //
863 if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));
864 //
865 if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){
866 if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
867 return((Long64_t)pkt_num+16777215LL);
868 };
869 //
870 if ( pkt_num > ppktfirst*2 && pkt_num > (16777214/2) ){
871 if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
872 return((Long64_t)pkt_num-16777215LL);
873 };
874 //
875 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
876 return((Long64_t)pkt_num);
877 //
878 };
879
880 /**
881 * Return the correct On Board Time if we went back to zero
882 */
883 Long64_t PamelaDBOperations::OBT(UInt_t obt){
884 //
885 if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
886 //
887 if ( obt > (pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
888 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
889 };
890 //
891 return((Long64_t)obt);
892 };
893
894 /**
895 *
896 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
897 *
898 */
899 void PamelaDBOperations::FillClass(){
900 this->FillClass(false,false,0,0);
901 };
902
903 /**
904 *
905 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
906 *
907 */
908 void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
909 //
910 TTree *T = 0;
911 T = (TTree*)file->Get("Physics");
912 if ( !T || T->IsZombie() ) throw -16;
913 //
914 EventHeader *eh = 0;
915 PscuHeader *ph = 0;
916 T->SetBranchAddress("Header", &eh);
917 PacketType *pctp=0;
918 EventCounter *codt=0;
919 EventCounter *codh=0;
920 UInt_t firstObt = 0;
921 UInt_t lastObt = 0;
922 UInt_t firstPkt = 0;
923 UInt_t lastPkt = 0;
924 UInt_t rhtime = 0;
925 UInt_t rttime = 0;
926 if ( !mishead ){
927 codh = ehh->GetCounter();
928 firstev = codh->Get(pctp->Physics);
929 rhtime = this->GetAbsTime(phh->GetOrbitalTime());
930 glrun->Set_GL_RUNH(runh,phh);
931 firstObt = glrun->GetRUNHEADER_OBT();
932 firstPkt = glrun->GetRUNHEADER_PKT();
933 };
934 if ( !mistrail ){
935 codt = eht->GetCounter();
936 lastev = codt->Get(pctp->Physics)-1;
937 rttime = this->GetAbsTime(pht->GetOrbitalTime());
938 glrun->Set_GL_RUNT(runt,pht);
939 lastObt = glrun->GetRUNTRAILER_OBT();
940 lastPkt = glrun->GetRUNTRAILER_PKT();
941 };
942 //
943 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
944 //
945 if ( mishead ) {
946 glrun->Set_GL_RUNH0();
947 //
948 if ( lastev+1 == firstev ){
949 firstObt = lastObt;
950 firstPkt = lastPkt;
951 rhtime = rttime;
952 } else {
953 T->GetEntry(firstev);
954 ph = eh->GetPscuHeader();
955 firstObt = ph->GetOrbitalTime();
956 rhtime = this->GetAbsTime(firstObt);
957 firstPkt = ph->GetCounter();
958 };
959 //
960 glrun->SetRUNHEADER_PKT(firstPkt);
961 glrun->SetRUNHEADER_OBT(firstObt);
962 //
963 };
964 if ( mistrail ){
965 glrun->Set_GL_RUNT0();
966 //
967 if ( lastev+1 == firstev ){
968 lastObt = firstObt;
969 lastPkt = firstPkt;
970 rttime = rhtime;
971 } else {
972 T->GetEntry(lastev);
973 ph = eh->GetPscuHeader();
974 lastObt = ph->GetOrbitalTime();
975 rttime = this->GetAbsTime(lastObt);
976 lastPkt = ph->GetCounter();
977 };
978 //
979 glrun->SetRUNTRAILER_OBT(lastObt);
980 glrun->SetRUNTRAILER_PKT(lastPkt);
981 //
982 };
983 glrun->SetEV_FROM(firstev);
984 glrun->SetEV_TO(lastev);
985 glrun->SetNEVENTS(lastev-firstev+1);
986 //
987 this->SetCommonGLRUN(rhtime,rttime);
988 //
989 };
990
991 //
992 // PUBLIC FUNCTIONS
993 //
994
995 /**
996 * Insert a new row into GL_RAW table.
997 */
998 Int_t PamelaDBOperations::insertPamelaRawFile(){
999 //
1000 stringstream oss;
1001 //
1002 Bool_t idr = this->SetID_RAW();
1003 if ( idr ) return(1);
1004 //
1005 oss.str("");
1006 if ( STATIC ){
1007 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1008 << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1009 } else {
1010 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1011 };
1012 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1013 //
1014 idr = this->SetID_RAW();
1015 if ( !idr ) throw -11;
1016 //
1017 return(0);
1018 }
1019
1020
1021 /**
1022 * Look for one timesync information in the file and
1023 * fill the GL_TIMESYNC table. It will look for: 1) TS-MCMD 2) TS info in the RunHeader 3) TS info in the runtrailer, if none exists exit with error
1024 */
1025 Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1026 //
1027 TSQLResult *result = 0;
1028 TSQLRow *row = 0;
1029 UInt_t t0 = 0;
1030 //
1031 stringstream oss;
1032 //
1033 if ( this->GetID_RAW() == 0 ) throw -11;
1034 //
1035 oss.str("");
1036 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1037 << this->GetRawFile().Data() << "';";
1038 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1039 result = conn->Query(oss.str().c_str());
1040 if ( !result ) throw -10;
1041 row = result->Next();
1042 //
1043 if ( !row ){
1044 oss.str("");
1045 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1046 << dworbit << " order by FROM_ORBIT desc limit 1;";
1047 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1048 result = conn->Query(oss.str().c_str());
1049 if ( !result ) throw -10;
1050 row = result->Next();
1051 if ( !row ) throw -10;
1052 };
1053 //
1054 TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1055 t0 = (UInt_t)tu.GetSec();
1056 if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
1057 //
1058 /*
1059 * Verify that the TIMESYNC have been not already processed
1060 */
1061 oss.str("");
1062 oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1063 << " LEFT JOIN GL_RAW "
1064 << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1065 << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1066 << " GROUP BY GL_TIMESYNC.OBT0;";
1067 if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1068 result = conn->Query(oss.str().c_str());
1069 if (result == NULL) throw -10;
1070 row = result->Next();
1071 if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1072 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1073 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1074 //
1075 tsync = (UInt_t)atoll(row->GetField(2));
1076 obt0 = (UInt_t)atoll(row->GetField(1));
1077 //
1078 return(1);
1079 };
1080 //
1081 TTree *T = 0;
1082 Int_t signal = 0;
1083 //
1084 UInt_t nevent = 0;
1085 UInt_t recEntries = 0;
1086 //
1087 UInt_t OBT = 0;
1088 UInt_t TYPE = 0;
1089 //
1090 Double_t minimum = 0.;
1091 Double_t maximum = 0.;
1092 Double_t minimum2 = 0.;
1093 Double_t maximum2 = 0.;
1094 //
1095 UInt_t TSYNC = 0;
1096 //
1097 pamela::McmdEvent *mc = 0;
1098 pamela::McmdRecord *mcrc = 0;
1099 TArrayC *mcmddata = 0;
1100 //
1101 minimum = numeric_limits<Double_t>::max();
1102 maximum = numeric_limits<Double_t>::min();
1103 minimum2 = numeric_limits<Double_t>::max();
1104 maximum2 = numeric_limits<Double_t>::min();
1105 //
1106 T = (TTree*)file->Get("Mcmd");
1107 if ( !T || T->IsZombie() ) throw -19;
1108 T->SetBranchAddress("Mcmd",&mc);
1109 //
1110 nevent = T->GetEntries();
1111 //
1112 // loop over events
1113 //
1114 Bool_t existsts = false;
1115 //
1116 for (UInt_t i=0; i<nevent;i++){
1117 //
1118 T->GetEntry(i);
1119 //
1120 recEntries = mc->Records->GetEntries();
1121 //
1122 for (UInt_t j = 0; j < recEntries; j++){
1123 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1124 mcmddata = mcrc->McmdData;
1125 //
1126 if (mcrc->ID1 == 0xE0){ // mcmd timesync
1127 //
1128 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1129 //
1130 TSYNC = (((UInt_t)mcmddata->At(0)<<24)&0xFF000000) + (((UInt_t)mcmddata->At(1)<<16)&0x00FF0000) + (((UInt_t)mcmddata->At(2)<<8)&0x0000FF00) + (((UInt_t)mcmddata->At(3))&0x000000FF);
1131 //
1132 TYPE = 55;//224;
1133 //
1134 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1135 //
1136 if ( TSYNC && OBT ){
1137 existsts = true;
1138 goto eout;
1139 };
1140 //
1141 };
1142 };
1143 };
1144 if ( !existsts ) { // try with runheader and runtrailer
1145 //
1146 if ( IsDebug() ) printf(" No ts mcmd \n");
1147 signal = 2;
1148 //
1149 TTree *rh=(TTree*)file->Get("RunHeader");
1150 if ( !rh || rh->IsZombie() ) throw -17;
1151 TTree *rt=(TTree*)file->Get("RunTrailer");
1152 if ( !rt || rt->IsZombie() ) throw -18;
1153 //
1154 rh->SetBranchAddress("RunHeader", &runh);
1155 //
1156 rt->SetBranchAddress("RunTrailer", &runt);
1157 //
1158 Int_t nrhev = rh->GetEntries();
1159 Int_t nrtev = rt->GetEntries();
1160 if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1161 //
1162 if ( nrhev > 0 ){
1163 for (Int_t i=0; i<nrhev; i++){
1164 //
1165 rh->GetEntry(i);
1166 //
1167 TSYNC = runh->LAST_TIME_SYNC_INFO;
1168 OBT = runh->OBT_TIME_SYNC * 1000;
1169 //
1170 TYPE = 20;
1171 //
1172 if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1173 //
1174 if ( TSYNC && OBT ){
1175 existsts = true;
1176 goto eout;
1177 };
1178 };
1179 //
1180 };
1181 if ( nrtev > 0 ){
1182 //
1183 if ( IsDebug() ) printf(" No runheader \n");
1184 signal = 6;
1185 //
1186 for (Int_t i=0; i<nrtev; i++){
1187 //
1188 rt->GetEntry(i);
1189 //
1190 TSYNC = runt->LAST_TYME_SYNC_INFO;
1191 OBT = runt->OBT_TYME_SYNC * 1000;
1192 //
1193 TYPE = 21;
1194 //
1195 if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1196 //
1197 if ( TSYNC && OBT ){
1198 existsts = true;
1199 goto eout;
1200 };
1201 };
1202 //
1203 } else {
1204 if ( IsDebug() ) printf(" No runheader \n");
1205 };
1206 };
1207 //
1208 if ( !existsts ){ // try with inclination mcmd
1209 //
1210 if ( IsDebug() ) printf(" No runtrailer \n");
1211 signal = 14;
1212 //
1213 Double_t timesync = 0.;
1214 for (UInt_t i=0; i<nevent;i++){
1215 //
1216 T->GetEntry(i);
1217 //
1218 recEntries = mc->Records->GetEntries();
1219 // //
1220 for (UInt_t j = 0; j < recEntries; j++){
1221 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1222 mcmddata = mcrc->McmdData;
1223 //
1224 if (mcrc->ID1 == 0xE2){ // mcmd inclination
1225 timesync = 0.;
1226 timesync = (Double_t)(((((UInt_t)mcmddata->At(0) << 24) & 0xFF000000) + (((UInt_t)mcmddata->At(1) << 16) & 0x00FF0000) + (((UInt_t)mcmddata->At(2) << 8) & 0x0000FF00) + ((UInt_t)mcmddata->At(3) & 0x000000FF))/128.0);
1227 //
1228 if ( timesync > maximum2){
1229 maximum2 = timesync;
1230 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1231 };
1232 };
1233 //
1234 };
1235 };
1236 if ( maximum2 > numeric_limits<Double_t>::min() ){
1237 TSYNC = (UInt_t)(maximum2 + 0.5);
1238 TYPE = 666;
1239 if ( TSYNC && OBT ){
1240 existsts = true;
1241 goto eout;
1242 };
1243 };
1244 };
1245 //
1246 if ( !existsts && obt0 ){ // insert timesync by hand
1247 //
1248 if ( IsDebug() ) printf(" No incl mcmd \n");
1249 signal = 30;
1250 //
1251 OBT = obt0;
1252 TSYNC = tsync;
1253 TYPE = 999;
1254 existsts = true;
1255 goto eout;
1256 };
1257 //
1258 eout:
1259 //
1260 if ( !existsts ) throw -3;
1261 //
1262 oss.str("");
1263 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1264 << this->GetID_RAW() << "','"//224'"
1265 << dec << (UInt_t)TYPE << "','"
1266 << dec << (UInt_t)OBT << "','"
1267 << dec << (UInt_t)TSYNC << "');";
1268 conn->Query(oss.str().c_str());
1269 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1270 //
1271 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1272 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1273 //
1274 tsync = TSYNC;
1275 obt0 = OBT;
1276 //
1277 delete result;
1278 return(signal);
1279 }
1280
1281 /**
1282 * Insert all the new rows into GL_ROOT.
1283 * The raw file indicates in the parameters should be already been stored in the database.
1284 */
1285 Int_t PamelaDBOperations::insertPamelaRootFile(){
1286 stringstream oss;
1287 TSQLResult *result = 0;
1288 TSQLRow *row = 0;
1289 UInt_t idtimesync = 0;
1290 //
1291 oss.str("");
1292 if ( STATIC ){
1293 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1294 << " LEFT JOIN GL_ROOT "
1295 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1296 << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1297 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1298 } else {
1299 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1300 << " LEFT JOIN GL_ROOT "
1301 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1302 << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1303 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1304 };
1305 result = conn->Query(oss.str().c_str());
1306 //
1307 if ( !result ) throw -12;
1308 //
1309 row = result->Next();
1310 //
1311 if ( !row ) throw -10;
1312 if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1313 idroot = (UInt_t)atoll(row->GetField(2));
1314 return(1);
1315 };
1316 //
1317 // determine which timesync has to be used
1318 //
1319 oss.str("");
1320 oss << "SELECT GL_TIMESYNC.ID FROM GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW ORDER BY GL_TIMESYNC.ID DESC LIMIT 1;";
1321 result = conn->Query(oss.str().c_str());
1322 //
1323 if ( !result ) throw -3;
1324 //
1325 row = result->Next();
1326 //
1327 if ( !row ) throw -3;
1328 idtimesync = (UInt_t)atoll(row->GetField(0));
1329 //
1330 oss.str("");
1331 if ( STATIC ){
1332 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1333 << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1334 } else {
1335 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1336 << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1337 };
1338 //
1339 if (conn->Query(oss.str().c_str()) == 0) throw -4;
1340 //
1341 delete result;
1342 //
1343 oss.str("");
1344 oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1345 //
1346 result = conn->Query(oss.str().c_str());
1347 if ( !result ) throw -12;
1348 row = result->Next();
1349 this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1350 //
1351 delete result;
1352 //
1353 return(0);
1354 }
1355
1356 /**
1357 * Assign the BOOT_NUMBER to the raw file.
1358 */
1359 Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1360 stringstream oss;
1361 TSQLResult *result = 0;
1362 TSQLRow *row = 0;
1363 oss.str("");
1364 if ( STATIC ){
1365 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1366 << " PATH = '" << this->GetRawPath().Data() << "' AND "
1367 << " NAME = '" << this->GetRawFile().Data() << "' ";
1368 } else {
1369 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1370 << " PATH = '$PAM_RAW' AND "
1371 << " NAME = '" << this->GetRawFile().Data() << "' ";
1372 };
1373 result = conn->Query(oss.str().c_str());
1374 //
1375 if ( !result ) throw -4;;
1376 row = result->Next();
1377 if ( !row ) return(16);
1378 if ( row->GetField(1) ){
1379 this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1380 return(1);
1381 };
1382 if ( !row->GetField(0) ) throw -26;
1383 //
1384 UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1385 //
1386 //
1387 //
1388 TTree *trDumpEv = 0;
1389 trDumpEv = (TTree*)file->Get("VarDump");
1390 if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1391 //
1392 VarDumpEvent *vde = 0;
1393 VarDumpRecord *vdr = 0;
1394 //
1395 Bool_t found = false;
1396 trDumpEv->SetBranchAddress("VarDump", &vde);
1397 if ( trDumpEv->GetEntries() > 0 ){
1398 found = false;
1399 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1400 trDumpEv->GetEntry(i);
1401 // vde->Records->GetEntries();
1402 if ( vde->Records->GetEntries()>5 ){
1403 found = true;
1404 goto fill;
1405 };
1406 };
1407 fill:
1408 if ( found ){
1409 //
1410 vdr = (VarDumpRecord*)vde->Records->At(6);
1411 //
1412 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1413 //
1414 } else {
1415 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1416 };
1417 } else {
1418 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1419 };
1420 //
1421 UInt_t bn = 0;
1422 Bool_t afound = false;
1423 if ( !found && this->AutoBoot()){
1424 afound = true;
1425 //
1426 // Search for other files with similar timesync
1427 //
1428 if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1429 UInt_t upperts = tsync-(obt0/1000)+5;
1430 UInt_t lowerts = tsync-(obt0/1000)-5;
1431 oss.str("");
1432 oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1433 << upperts
1434 << " AND TIMESYNC-(OBT0/1000)>"
1435 << lowerts
1436 << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1437 result = conn->Query(oss.str().c_str());
1438 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1439 //
1440 if ( !result ) throw -4;;
1441 found = true;
1442 if ( result->GetRowCount()<3 ){
1443 if ( IsDebug() ) printf(" AGH! no results!\n");
1444 found = false;
1445 } else {
1446 row = result->Next();
1447 bn = (UInt_t)atoll(row->GetField(0));
1448 for ( Int_t r=1; r<result->GetRowCount() ;r++){
1449 if ( !row ) throw -4;
1450 if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1451 if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1452 if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1453 found = false;
1454 };
1455 row = result->Next();
1456 };
1457 };
1458 };
1459 //
1460 Int_t sgn = 0;
1461 //
1462 if ( !found && !BOOTNO ){
1463 throw -29;
1464 } else {
1465 if ( afound ){
1466 this->SetBOOTnumber(bn);
1467 sgn = 8;
1468 };
1469 };
1470 //
1471 oss.str("");
1472 oss << " UPDATE GL_RAW "
1473 << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1474 << " WHERE GL_RAW.ID = '" << idRaw << "'";
1475 conn->Query(oss.str().c_str());
1476 //
1477 delete result;
1478 return(sgn);
1479 };
1480
1481 /**
1482 * Scan runtrailer packet, fill the GL_RUN table and
1483 * check for missing and truncated runs
1484 */
1485 Int_t PamelaDBOperations::insertPamelaRUN(){
1486 Int_t signal = 0;
1487 //
1488 stringstream oss;
1489 oss.str("");
1490 //
1491 signal = this->SetUpperLimits();
1492 //
1493 // loop on runheader and runtrailer events
1494 //
1495 TTree *rh=(TTree*)file->Get("RunHeader");
1496 if ( !rh || rh->IsZombie() ) throw -17;
1497 TTree *rt=(TTree*)file->Get("RunTrailer");
1498 if ( !rt || rt->IsZombie() ) throw -18;
1499 //
1500 PacketType *pctp=0;
1501 EventCounter *cod=0;
1502 //
1503 rh->SetBranchAddress("RunHeader", &runh);
1504 rh->SetBranchAddress("Header", &ehh);
1505 //
1506 rt->SetBranchAddress("RunTrailer", &runt);
1507 rt->SetBranchAddress("Header", &eht);
1508 //
1509 UInt_t obtt = 0;
1510 UInt_t obth = 0;
1511 UInt_t pktt = 0;
1512 UInt_t pkth = 0;
1513 Int_t pth = -1;
1514 Int_t ptht = -1;
1515 Int_t evbeft = 0;
1516 Int_t evbefh = 0;
1517 //
1518 // no runtrailers in the file!
1519 //
1520 if ( !rtev ){
1521 if ( !upperentry ){
1522 if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1523 throw -8;
1524 } else {
1525 this->HandleRunFragments(true,true,0,upperentry);
1526 };
1527 } else {
1528 //
1529 for (Int_t ptt=0; ptt<rtev; ptt++){
1530 //
1531 rt->GetEntry(ptt);
1532 pht = eht->GetPscuHeader();
1533 pktt = pht->GetCounter();
1534 obtt = pht->GetOrbitalTime();
1535 //
1536 cod = eht->GetCounter();
1537 ptht = cod->Get(pctp->RunHeader) - 1;
1538 evbeft = cod->Get(pctp->Physics);
1539 //
1540 if ( !ptt && !(ptht+1) ){
1541 //
1542 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);
1543 //
1544 this->HandleRunFragments(true,false,0,(evbeft-1));
1545 //
1546 //
1547 } else if ( pth == ptht ){
1548 //
1549 if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1550 //
1551 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1552 rt->GetEntry(ptt-1);
1553 cod = eht->GetCounter();
1554 evbefh = cod->Get(pctp->Physics);
1555 rt->GetEntry(ptt);
1556 pht = eht->GetPscuHeader();
1557 //
1558 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %i \n",ptht,pth,ptt);
1559 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1560 //
1561 this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1562 //
1563 } else {
1564 //
1565 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);
1566 //
1567 rh->GetEntry(ptht);
1568 phh = ehh->GetPscuHeader();
1569 pkth = phh->GetCounter();
1570 obth = phh->GetOrbitalTime();
1571 cod = ehh->GetCounter();
1572 evbefh = cod->Get(pctp->Physics);
1573 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1574 //
1575 // handle this run
1576 //
1577 this->HandleRun();
1578 //
1579 //
1580 //
1581 if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1582 //
1583 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");
1584 //
1585 this->HandleRunFragments(true,true,0,(evbefh-1));
1586 //
1587 };
1588 //
1589 //
1590 if ( (ptht - pth) > 1 ){
1591 //
1592 if ( IsDebug() ) printf(" Missing runtrailers! \n");
1593 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);
1594 // is not the consecutive header
1595 while ( pth != ptht ){
1596 //
1597 // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1598 //
1599 pth++;
1600 //
1601 rh->GetEntry(pth+1);
1602 phh = ehh->GetPscuHeader();
1603 pktt = phh->GetCounter();
1604 obtt = phh->GetOrbitalTime();
1605 cod = ehh->GetCounter();
1606 evbeft = cod->Get(pctp->Physics);
1607 rh->GetEntry(pth);
1608 phh = ehh->GetPscuHeader();
1609 cod = ehh->GetCounter();
1610 pkth = phh->GetCounter();
1611 obth = phh->GetOrbitalTime();
1612 evbefh = cod->Get(pctp->Physics);
1613 //
1614 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %i \n",ptht,pth,ptt);
1615 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1616 //
1617 this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1618 //
1619 };
1620 //
1621 } else if ( !(ptht - pth) ){
1622 //
1623 if ( IsDebug() ) printf(" Missing runheader! \n");
1624 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);
1625 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1626 //
1627 } else {
1628 //
1629 // go on with next header
1630 //
1631 pth = ptht;
1632 };
1633 //
1634 };
1635 //
1636 if ( ptt+1 == rtev){
1637 ptht++;
1638 if ( ptht < rhev ){
1639 rh->GetEntry(ptht);
1640 phh = ehh->GetPscuHeader();
1641 pkth = phh->GetCounter();
1642 obth = phh->GetOrbitalTime();
1643 cod = ehh->GetCounter();
1644 evbefh = cod->Get(pctp->Physics);
1645 if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);
1646 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);
1647 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);
1648 //
1649 this->HandleRunFragments(false,true,evbefh,upperentry);
1650 } else {
1651 //
1652 // check if we have a fragment with no header
1653 //
1654 if ( (UInt_t)evbeft < upperentry-1 ){
1655 if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1656 //
1657 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1658 rt->GetEntry(ptt-1);
1659 cod = eht->GetCounter();
1660 evbefh = cod->Get(pctp->Physics);
1661 rt->GetEntry(ptt);
1662 pht = eht->GetPscuHeader();
1663 this->HandleRunFragments(true,true,evbefh,upperentry);
1664 };
1665 };
1666 };
1667 //
1668 };
1669 };
1670 //
1671 return(signal);
1672 };
1673
1674 /**
1675 *
1676 * Check if the run has already been inserted
1677 *
1678 */
1679 Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1680 //
1681 TSQLResult *result = 0;
1682 TSQLRow *row = 0;
1683 //
1684 stringstream oss;
1685 oss.str("");
1686 //
1687 // the where clause is of the type: boot_number = _our_boot && (
1688 // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1689 // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1690 // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1691 // ||
1692 // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1693 // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1694 // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1695 // )
1696 //
1697 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1698 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1699 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1700 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1701 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1702 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1703 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1704 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1705 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1706 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1707 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1708 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1709 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1710 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1711 //
1712 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1713 result = conn->Query(oss.str().c_str());
1714 //
1715 if ( !result ) throw -4;
1716 //
1717 row = result->Next();
1718 //
1719 if ( !row ){
1720 if ( IsDebug() ) printf(" The run is new \n");
1721 if ( IsDebug() ) printf(" -> fill the DB \n");
1722 return(false); // the file has not been inserted in the DB, go on.
1723 };
1724 //
1725 Bool_t signal = true;
1726 //
1727 while ( row != NULL ){
1728 if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1729 //
1730 // the run has already been inserted
1731 //
1732 // return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1733 //
1734 // PATCH!
1735 // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader
1736 // while the old run doesn't have it 3) we have more events than the old run
1737 //
1738 if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1739 //
1740 if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1741 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1742 // oss.str("");
1743 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1744 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1745 // conn->Query(oss.str().c_str());
1746 if ( signal ) signal = false;
1747 goto gonext;
1748 //
1749 } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1750 if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1751 if ( IsDebug() ) printf(" The run is already inserted \n");
1752 goto gonext;
1753 };
1754 //
1755 if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1756 //
1757 if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1758 //
1759 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1760 // oss.str("");
1761 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1762 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1763 // conn->Query(oss.str().c_str());
1764 //
1765 if ( signal ) signal = false;
1766 goto gonext;
1767 } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1768 if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1769 if ( IsDebug() ) printf(" The run is already inserted \n");
1770 goto gonext;
1771 };
1772 //
1773 if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1774 //
1775 if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1776 //
1777 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1778 // oss.str("");
1779 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1780 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1781 // conn->Query(oss.str().c_str());
1782 if ( signal ) signal = false;
1783 //
1784 };
1785 //
1786 gonext:
1787 // END PATCH!
1788 //
1789 row = result->Next();
1790 //
1791 };
1792 //
1793 delete result;
1794 //
1795 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1796 if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1797 return(signal);
1798 };
1799
1800 /**
1801 * Handle runs which seems to be good ones.
1802 **/
1803 void PamelaDBOperations::HandleRun(){
1804 ULong64_t chkpkt = 0;
1805 ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1806 ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1807 //
1808 chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1809 //
1810 if ( labs(chkpkt-pktt)<2 ){
1811 //
1812 if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1813 //
1814 // it must be a good run, fill the db
1815 //
1816 this->FillClass();
1817 //
1818 if ( !IsRunAlreadyInserted() ){
1819 glrun->SetID(this->AssignRunID());
1820 glrun->SetID_RUN_FRAG(0);
1821 glrun->Fill_GL_RUN(conn);
1822 };
1823 } else {
1824 //
1825 if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1826 if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1827 //
1828 this->HandleSuspiciousRun();
1829 //
1830 };
1831 //
1832 //
1833 return;
1834 };
1835
1836
1837 /**
1838 * Handle run fragments at the beginning or at the end of the file
1839 **/
1840 void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1841 //
1842 UInt_t rhfirstev = firstev;
1843 UInt_t rtlastev = lastev;
1844 Bool_t found = false;
1845 Bool_t foundinrun = false;
1846 //
1847 TSQLResult *result = 0;
1848 TSQLRow *row = 0;
1849 //
1850 stringstream oss;
1851 oss.str("");
1852 //
1853 // is the piece of run good (no other packets inside)?
1854 //
1855 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1856 //
1857 // if not, handle other pieces and continue with the first one
1858 //
1859 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1860 //
1861 };
1862 //
1863 // we have now the good first piece of a run, fill the glrun object
1864 //
1865 if ( rhfirstev != firstev && !mishead ) mishead = true;
1866 if ( rtlastev != lastev && !mistrail ) mistrail = true;
1867 //
1868 this->FillClass(mishead,mistrail,firstev,lastev);
1869 //
1870 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1871 //
1872 // First of all insert the run in the fragment table...
1873 //
1874 oss.str("");
1875 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1876 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1877 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1878 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1879 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1880 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1881 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1882 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1883 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1884 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1885 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1886 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1887 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1888 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1889 //
1890 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1891 result = conn->Query(oss.str().c_str());
1892 //
1893 if ( !result ) throw -4;
1894 //
1895 row = result->Next();
1896 //
1897 if ( !row ){
1898 //
1899 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1900 //
1901 if ( IsDebug() ) printf(" The run is new \n");
1902 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1903 //
1904 glrun->SetID(this->AssignRunID());
1905 glrun->SetID_RUN_FRAG(0);
1906 glrun->Fill_GL_RUN_FRAGMENTS(conn);
1907 //
1908 } else {
1909 if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1910 return;
1911 };
1912 //
1913 //
1914 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1915 //
1916 if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1917 // missing it no way we can found a piece in the frag table
1918 //
1919 oss.str("");
1920 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1921 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1922 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1923 << " ID != " << glrun->ID
1924 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1925 //
1926 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1927 result = conn->Query(oss.str().c_str());
1928 //
1929 if ( !result ) throw -4;
1930 //
1931 row = result->Next();
1932 //
1933 if ( !row && NoFrag() ){
1934 //
1935 oss.str("");
1936 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1937 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1938 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1939 << " ID != " << glrun->ID
1940 << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1941 //
1942 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1943 result = conn->Query(oss.str().c_str());
1944 //
1945 if ( !result ) throw -4;
1946 //
1947 foundinrun = true;
1948 //
1949 row = result->Next();
1950 //
1951 };
1952 //
1953 if ( !row ){
1954 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1955 found = false;
1956 } else {
1957 //
1958 found = false; // default value
1959 //
1960 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1961 //
1962 // if we have both runheader and runtrailer we can check with pkt_counter:
1963 //
1964 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1965 ULong64_t chkpkt = 0;
1966 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1967 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1968 //
1969 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
1970 //
1971 if ( labs(chkpkt-pktt)<2 ){
1972 //
1973 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1974 //
1975 found = true;
1976 //
1977 } else {
1978 //
1979 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1980 //
1981 found = false;
1982 //
1983 };
1984 };
1985 if ( !found ){
1986 //
1987 // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
1988 //
1989 ULong64_t chkpkt1 = 0;
1990 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1991 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1992 chkpkt1 = labs(orunh1-dbrunt1);
1993 //
1994 ULong64_t chkpkt2 = 0;
1995 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
1996 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1997 chkpkt2 = labs(orunh2-dbrunt2);
1998 //
1999 ULong64_t chkpkt3 = 0;
2000 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2001 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2002 chkpkt3 = labs(orunh3-dbrunt3);
2003 //
2004 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2005 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2006 //
2007 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2008 //
2009 found = true;
2010 //
2011 } else {
2012 //
2013 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2014 //
2015 found = false;
2016 //
2017 };
2018 };
2019 };
2020 //
2021 if ( found ){
2022 //
2023 // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
2024 //
2025 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2026 //
2027 if ( foundinrun ){
2028 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2029 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2030 };
2031 //
2032 GL_RUN *glrun1 = new GL_RUN();
2033 //
2034 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2035 //
2036 oss.str("");
2037 oss << " ID="<<row->GetField(0)<<";";
2038 //
2039 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2040 //
2041 // merge infos
2042 //
2043 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2044 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2045 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2046 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2047 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2048 TTree *T= 0;
2049 T = (TTree*)file->Get("Physics");
2050 if ( !T || T->IsZombie() ) throw -16;
2051 EventHeader *eh = 0;
2052 PscuHeader *ph = 0;
2053 T->SetBranchAddress("Header", &eh);
2054 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2055 T->GetEntry(firstev);
2056 ph = eh->GetPscuHeader();
2057 bpkt = PKT(ph->GetCounter());
2058 bobt = OBT(ph->GetOrbitalTime());
2059 firstev++;
2060 };
2061 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2062 //
2063 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2064 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2065 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2066 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2067 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2068 //
2069 glrun->SetEV_FROM(firstev);
2070 glrun->SetNEVENTS(lastev-firstev+1);
2071 //
2072 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2073 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2074 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2075 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2076 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2077 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2078 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2079 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2080 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2081 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2082 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2083 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2084 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2085 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2086 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2087 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2088 //
2089 if ( !IsRunAlreadyInserted() ){
2090 //
2091 // glrun->SetID(this->AssignRunID());
2092 glrun->SetID_RUN_FRAG(glrun1->GetID());
2093 glrun->Fill_GL_RUN(conn);
2094 //
2095 // set id number
2096 //
2097 glrun1->SetID_RUN_FRAG(glrun->GetID());
2098 glrun1->Fill_GL_RUN(conn);
2099 //
2100 };
2101 // delete old entry in fragment table
2102 //
2103 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2104 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2105 //
2106 delete glrun1;
2107 //
2108 //
2109 return;
2110 //
2111 };
2112 //
2113 };
2114 //
2115 if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2116 // missing it no way we can found a piece in the frag table
2117 //
2118 oss.str("");
2119 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2120 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2121 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2122 << " ID != " << glrun->ID
2123 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2124 //
2125 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2126 result = conn->Query(oss.str().c_str());
2127 //
2128 if ( !result ) throw -4;
2129 //
2130 row = result->Next();
2131 //
2132 if ( !row && NoFrag() ){
2133 //
2134 oss.str("");
2135 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2136 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2137 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2138 << " ID != " << glrun->ID
2139 << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2140 //
2141 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2142 result = conn->Query(oss.str().c_str());
2143 //
2144 if ( !result ) throw -4;
2145 //
2146 foundinrun = true;
2147 row = result->Next();
2148 //
2149 };
2150 //
2151 if ( !row ){
2152 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2153 found = false;
2154 } else {
2155 //
2156 found = false; // default value
2157 //
2158 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2159 //
2160 // if we have both runheader and runtrailer we can check with pkt_counter:
2161 //
2162 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2163 ULong64_t chkpkt = 0;
2164 ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2165 ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2166 //
2167 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
2168 //
2169 if ( labs(chkpkt-pktt)<2 ){
2170 //
2171 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2172 //
2173 found = true;
2174 //
2175 } else {
2176 //
2177 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2178 //
2179 found = false;
2180 //
2181 };
2182 };
2183 if ( !found ){
2184 //
2185 // if we arrive here we were not able to decide if the two pieces matches using only the pkt counter information, we must check times and obts
2186 //
2187 ULong64_t chkpkt1 = 0;
2188 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2189 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2190 chkpkt1 = labs(orunh1-dbrunt1);
2191 //
2192 ULong64_t chkpkt2 = 0;
2193 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2194 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2195 chkpkt2 = labs(orunh2-dbrunt2);
2196 //
2197 ULong64_t chkpkt3 = 0;
2198 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2199 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2200 chkpkt3 = labs(orunh3-dbrunt3);
2201 //
2202 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2203 //
2204 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2205 //
2206 found = true;
2207 //
2208 } else {
2209 //
2210 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2211 //
2212 found = false;
2213 //
2214 };
2215 };
2216 };
2217 //
2218 if ( found ){
2219 //
2220 // we have found the missing piece, glue the two together, merge the informations, fill the gl_run table (check first runs do not exists), delete entry in frag table
2221 //
2222 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2223 //
2224 if ( foundinrun ){
2225 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2226 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2227 };
2228 //
2229 GL_RUN *glrun1 = new GL_RUN();
2230 //
2231 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2232 //
2233 oss.str("");
2234 oss << " ID="<<row->GetField(0)<<";";
2235 //
2236 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
2237 //
2238 // merge infos
2239 //
2240 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2241 ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2242 UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2243 ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2244 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2245 TTree *T= 0;
2246 T = (TTree*)file->Get("Physics");
2247 if ( !T || T->IsZombie() ) throw -16;
2248 EventHeader *eh = 0;
2249 PscuHeader *ph = 0;
2250 T->SetBranchAddress("Header", &eh);
2251 while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2252 T->GetEntry(lastev);
2253 ph = eh->GetPscuHeader();
2254 apkt = PKT(ph->GetCounter());
2255 aobt = OBT(ph->GetOrbitalTime());
2256 lastev--;
2257 };
2258 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2259 //
2260 glrun->SetEV_TO(lastev);
2261 glrun->SetNEVENTS(lastev-firstev+1);
2262 glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2263 glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2264 glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2265 glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2266 glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2267 //
2268 glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2269 glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2270 glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2271 glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2272 glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2273 glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2274 glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2275 glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2276 glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2277 glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2278 glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
2279 glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
2280 glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
2281 glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
2282 glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
2283 glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
2284 //
2285 if ( !IsRunAlreadyInserted() ){
2286 //
2287 // glrun->SetID(this->AssignRunID());
2288 //
2289 glrun->SetID_RUN_FRAG(glrun1->GetID());
2290 glrun->Fill_GL_RUN(conn);
2291 //
2292 // set id number
2293 //
2294 glrun1->SetID_RUN_FRAG(glrun->GetID());
2295 glrun1->Fill_GL_RUN(conn);
2296 //
2297 };
2298 //
2299 // delete old entries in fragment table
2300 //
2301 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2302 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2303 //
2304 delete glrun1;
2305 //
2306 return;
2307 //
2308 };
2309 //
2310 };
2311 //
2312 if ( !found ){
2313 //
2314 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2315 //
2316 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2317 //
2318 oss.str("");
2319 oss << " SELECT ID FROM GL_RUN WHERE "
2320 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2321 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2322 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2323 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2324 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2325 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2326 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2327 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2328 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2329 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2330 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2331 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2332 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2333 //
2334 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2335 result = conn->Query(oss.str().c_str());
2336 //
2337 if ( !result ) throw -4;
2338 //
2339 row = result->Next();
2340 //
2341 if ( row ){
2342 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2343 } else {
2344 if ( NoFrag() ){
2345 glrun->SetID_RUN_FRAG(glrun->GetID());
2346 glrun->Fill_GL_RUN(conn);
2347 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2348 };
2349 };
2350 };
2351 //
2352 return;
2353 };
2354
2355
2356 /**
2357 * Handle run without header or trailer
2358 **/
2359 void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2360 //
2361 //
2362 // is the piece of run good (no other packets inside)?
2363 //
2364 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2365 //
2366 // if not, handle other pieces and continue with the first one
2367 //
2368 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2369 //
2370 } else {
2371 //
2372 this->FillClass(mishead,mistrail,firstev,lastev);
2373 //
2374 if ( !IsRunAlreadyInserted() ){
2375 glrun->SetID(this->AssignRunID());
2376 glrun->SetID_RUN_FRAG(0);
2377 glrun->Fill_GL_RUN(conn);
2378 };
2379 //
2380 };
2381 //
2382 return;
2383 };
2384
2385 /**
2386 *
2387 * check if we have non-physics packets inside the run
2388 *
2389 */
2390 Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2391 //
2392 EventCounter *code=0;
2393 //
2394 UInt_t nevent = 0;
2395 UInt_t checkfirst = 0;
2396 UInt_t checklast = 0;
2397 UInt_t firstentry = 0;
2398 UInt_t lastentry = 0;
2399 UInt_t firstTime = 0;
2400 UInt_t lastTime = 0;
2401 UInt_t firstPkt = 0;
2402 UInt_t lastPkt = 0;
2403 UInt_t firstObt = 0;
2404 UInt_t lastObt = 0;
2405 //
2406 pcksList packetsNames;
2407 pcksList::iterator Iter;
2408 getPacketsNames(packetsNames);
2409 //
2410 TTree *T= 0;
2411 T =(TTree*)file->Get("Physics");
2412 if ( !T || T->IsZombie() ) throw -16;
2413 EventHeader *eh = 0;
2414 PscuHeader *ph = 0;
2415 T->SetBranchAddress("Header", &eh);
2416 nevent = T->GetEntries();
2417 //
2418 //
2419 if ( firstev == lastev+1 ) { // no events inside the run!
2420 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2421 // return true is correct
2422 return(true);
2423 //
2424 } else {
2425 //
2426 T->GetEntry(firstev);
2427 code = eh->GetCounter();
2428 checkfirst = 0;
2429 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2430 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2431 };
2432 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2433 //
2434 T->GetEntry(lastev);
2435 code = eh->GetCounter();
2436 checklast = 0;
2437 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2438 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2439 };
2440 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2441 //
2442 if ( checkfirst == checklast ){
2443 //
2444 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2445 //
2446 return(true);
2447 //
2448 } else {
2449 //
2450 if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2451 //
2452 // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2453 //
2454 if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2455 //
2456 Bool_t emptyruns = false;
2457 UInt_t check = 0;
2458 UInt_t lastevtemp = lastev;
2459 UInt_t firstevno = firstev;
2460 //
2461 for (UInt_t i=firstev; i<=lastev; i++){
2462 //
2463 T->GetEntry(i);
2464 code = eh->GetCounter();
2465 //
2466 check = 0;
2467 //
2468 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2469 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2470 };
2471 //
2472 if ( checkfirst < check || i == lastev ){
2473 //
2474 firstentry = firstevno;
2475 //
2476 if ( checkfirst < check ){
2477 lastentry = i-1;
2478 } else {
2479 lastentry = i;
2480 };
2481 //
2482 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2483 //
2484 glrun->SetEV_FROM(firstentry);
2485 glrun->SetEV_TO(lastentry);
2486 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2487 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2488 lastentry--;
2489 };
2490 glrun->SetNEVENTS(lastentry-firstentry+1);
2491 //
2492 glrun->Set_GL_RUNH0();
2493 glrun->Set_GL_RUNT0();
2494 //
2495 glrun->SetLAST_TIMESYNC(0);
2496 glrun->SetOBT_TIMESYNC(0);
2497 //
2498 T->GetEntry(firstentry);
2499 ph = eh->GetPscuHeader();
2500 firstObt = ph->GetOrbitalTime();
2501 firstTime = this->GetAbsTime(firstObt);
2502 firstPkt = ph->GetCounter();
2503 //
2504 T->GetEntry(lastentry);
2505 ph = eh->GetPscuHeader();
2506 lastObt = ph->GetOrbitalTime();
2507 lastTime = this->GetAbsTime(lastObt);
2508 lastPkt = ph->GetCounter();
2509 //
2510 glrun->SetRUNHEADER_PKT(firstPkt);
2511 glrun->SetRUNTRAILER_PKT(lastPkt);
2512 //
2513 glrun->SetRUNHEADER_OBT(firstObt);
2514 glrun->SetRUNTRAILER_OBT(lastObt);
2515 //
2516 if ( firstev == firstentry && !emptyruns && !mishead ){
2517 glrun->Set_GL_RUNH(runh,phh);
2518 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2519 if ( IsDebug() ) printf(" We have the runheader \n");
2520 };
2521 if ( lastev == i && !mistrail ){
2522 glrun->Set_GL_RUNT(runt,pht);
2523 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2524 if ( IsDebug() ) printf(" We have the runtrailer \n");
2525 };
2526 //
2527 if ( lastentry == (firstentry-2) ){ // no events in the run
2528 emptyruns = true;
2529 if ( IsDebug() ) printf(" No events in the run \n");
2530 lastTime = firstTime;
2531 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2532 lastObt = glrun->RUNHEADER_OBT;
2533 lastPkt = glrun->RUNHEADER_PKT;
2534 } else {
2535 lastObt = firstObt;
2536 lastPkt = firstPkt;
2537 };
2538 glrun->SetRUNTRAILER_PKT(lastPkt);
2539 glrun->SetRUNTRAILER_OBT(lastObt);
2540 lastentry++;
2541 };
2542 //
2543 this->SetCommonGLRUN(firstTime,lastTime);
2544 //
2545 if ( !IsRunAlreadyInserted() ){
2546 glrun->SetID(this->AssignRunID());
2547 glrun->SetID_RUN_FRAG(0);
2548 glrun->Fill_GL_RUN(conn);
2549 };
2550 //
2551 firstevno = lastentry + 1;
2552 //
2553 checkfirst = check;
2554 //
2555 };
2556 //
2557 if ( check == checklast && i != lastev ){
2558 lastevtemp = i - 1;
2559 i = lastev - 1;
2560 };
2561 //
2562 };
2563 //
2564 lastev = lastevtemp;
2565 //
2566 return(false);
2567 //
2568 };
2569 };
2570 //
2571 return(false); // should never arrive here
2572 };
2573
2574 /**
2575 *
2576 * we end up here when we have a runheader and a runtrailer but they seems not belonging to the same run since the number of events does not coincide with the
2577 * number of event written in the runtrailer. We try to split into different runs scanning the physics events from the runheader to the runtrailer and
2578 * looking for non-physics packets inside.
2579 *
2580 */
2581 void PamelaDBOperations::HandleSuspiciousRun(){
2582 //
2583 PacketType *pctp=0;
2584 EventCounter *codt=0;
2585 EventCounter *codh=0;
2586 EventCounter *code=0;
2587 UInt_t firstev = 0;
2588 UInt_t lastev = 0;
2589 UInt_t nevent = 0;
2590 UInt_t checkfirst = 0;
2591 UInt_t checklast = 0;
2592 UInt_t firstentry = 0;
2593 UInt_t lastentry = 0;
2594 UInt_t firstTime = 0;
2595 UInt_t lastTime = 0;
2596 UInt_t firstPkt = 0;
2597 UInt_t lastPkt = 0;
2598 UInt_t firstObt = 0;
2599 UInt_t lastObt = 0;
2600 //
2601 pcksList packetsNames;
2602 pcksList::iterator Iter;
2603 getPacketsNames(packetsNames);
2604 //
2605 TTree *rh=0;
2606 rh = (TTree*)file->Get("RunHeader");
2607 if ( !rh || rh->IsZombie() ) throw -17;
2608 TTree *T=0;
2609 T =(TTree*)file->Get("Physics");
2610 if ( !T || T->IsZombie() ) throw -16;
2611 EventHeader *eh = 0;
2612 PscuHeader *ph = 0;
2613 T->SetBranchAddress("Header", &eh);
2614 nevent = T->GetEntries();
2615 //
2616 codt = eht->GetCounter();
2617 codh = ehh->GetCounter();
2618 firstev = codh->Get(pctp->Physics);
2619 lastev = codt->Get(pctp->Physics)-1;
2620 //
2621 if ( firstev == lastev+1 ) { // no events inside the run!
2622 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2623 //
2624 this->FillClass();
2625 if ( !IsRunAlreadyInserted() ){
2626 glrun->SetID(this->AssignRunID());
2627 glrun->SetID_RUN_FRAG(0);
2628 glrun->Fill_GL_RUN(conn);
2629 };
2630 //
2631 } else {
2632 //
2633 UInt_t nrunh = 0;
2634 UInt_t nrunh1 = 0;
2635 T->GetEntry(firstev);
2636 code = eh->GetCounter();
2637 checkfirst = 0;
2638 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2639 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2640 if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2641 };
2642 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2643 //
2644 T->GetEntry(lastev);
2645 code = eh->GetCounter();
2646 checklast = 0;
2647 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2648 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2649 };
2650 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2651 //
2652 if ( checkfirst == checklast ){
2653 //
2654 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2655 //
2656 this->FillClass();
2657 if ( !IsRunAlreadyInserted() ){
2658 glrun->SetID(this->AssignRunID());
2659 glrun->SetID_RUN_FRAG(0);
2660 glrun->Fill_GL_RUN(conn);
2661 };
2662 //
2663 } else {
2664 //
2665 if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2666 //
2667 Bool_t emptyruns = false;
2668 UInt_t check = 0;
2669 UInt_t firstevno = firstev;
2670 //
2671 for (UInt_t i=firstev; i<=lastev; i++){
2672 //
2673 T->GetEntry(i);
2674 code = eh->GetCounter();
2675 //
2676 check = 0;
2677 //
2678 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2679 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2680 if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2681 };
2682 //
2683 if ( checkfirst < check || i == lastev ){
2684 //
2685 firstentry = firstevno;
2686 //
2687 if ( checkfirst < check ){
2688 lastentry = i-1;
2689 } else {
2690 lastentry = i;
2691 };
2692 //
2693 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2694 //
2695 glrun->SetEV_FROM(firstentry);
2696 glrun->SetEV_TO(lastentry);
2697 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2698 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2699 lastentry--;
2700 };
2701 glrun->SetNEVENTS(lastentry-firstentry+1);
2702 //
2703 glrun->Set_GL_RUNH0();
2704 glrun->Set_GL_RUNT0();
2705 //
2706 glrun->SetLAST_TIMESYNC(0);
2707 glrun->SetOBT_TIMESYNC(0);
2708 //
2709 T->GetEntry(firstentry);
2710 ph = eh->GetPscuHeader();
2711 firstObt = ph->GetOrbitalTime();
2712 firstTime = this->GetAbsTime(firstObt);
2713 firstPkt = ph->GetCounter();
2714 //
2715 T->GetEntry(lastentry);
2716 ph = eh->GetPscuHeader();
2717 lastObt = ph->GetOrbitalTime();
2718 lastTime = this->GetAbsTime(lastObt);
2719 lastPkt = ph->GetCounter();
2720 //
2721 glrun->SetRUNHEADER_PKT(firstPkt);
2722 glrun->SetRUNTRAILER_PKT(lastPkt);
2723 //
2724 glrun->SetRUNHEADER_OBT(firstObt);
2725 glrun->SetRUNTRAILER_OBT(lastObt);
2726 //
2727 if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2728 rh->GetEntry(nrunh1-1);
2729 phh = ehh->GetPscuHeader();
2730 nrunh1++;
2731 glrun->Set_GL_RUNH(runh,phh);
2732 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2733 if ( IsDebug() ) printf(" We have the runheader \n");
2734 };
2735 if ( lastev == i && checkfirst == check ){
2736 glrun->Set_GL_RUNT(runt,pht);
2737 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2738 if ( IsDebug() ) printf(" We have the runtrailer \n");
2739 };
2740 //
2741 if ( lastentry == (firstentry-2) ){ // no events in the run
2742 emptyruns = true;
2743 if ( IsDebug() ) printf(" No events in the run \n");
2744 lastTime = firstTime;
2745 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2746 lastObt = glrun->RUNHEADER_OBT;
2747 lastPkt = glrun->RUNHEADER_PKT;
2748 } else {
2749 lastObt = firstObt;
2750 lastPkt = firstPkt;
2751 };
2752 glrun->SetRUNTRAILER_PKT(lastPkt);
2753 glrun->SetRUNTRAILER_OBT(lastObt);
2754 lastentry++;
2755 };
2756 //
2757 this->SetCommonGLRUN(firstTime,lastTime);
2758 //
2759 if ( !IsRunAlreadyInserted() ){
2760 glrun->SetID(this->AssignRunID());
2761 glrun->SetID_RUN_FRAG(0);
2762 glrun->Fill_GL_RUN(conn);
2763 };
2764 //
2765 if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2766 //
2767 firstentry = i;
2768 //
2769 lastentry = i;
2770 //
2771 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2772 //
2773 glrun->SetEV_FROM(firstentry);
2774 glrun->SetEV_TO(lastentry);
2775 glrun->SetNEVENTS(lastentry-firstentry+1);
2776 //
2777 glrun->Set_GL_RUNH0();
2778 //
2779 glrun->SetLAST_TIMESYNC(0);
2780 glrun->SetOBT_TIMESYNC(0);
2781 //
2782 T->GetEntry(firstentry);
2783 ph = eh->GetPscuHeader();
2784 firstObt = ph->GetOrbitalTime();
2785 firstTime = this->GetAbsTime(firstObt);
2786 firstPkt = ph->GetCounter();
2787 //
2788 glrun->SetRUNHEADER_PKT(firstPkt);
2789 //
2790 glrun->SetRUNHEADER_OBT(firstObt);
2791 //
2792 glrun->Set_GL_RUNT(runt,pht);
2793 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2794 if ( IsDebug() ) printf(" We have the runtrailer \n");
2795 //
2796 this->SetCommonGLRUN(firstTime,lastTime);
2797 //
2798 if ( !IsRunAlreadyInserted() ){
2799 glrun->SetID(this->AssignRunID());
2800 glrun->SetID_RUN_FRAG(0);
2801 glrun->Fill_GL_RUN(conn);
2802 };
2803 };
2804 //
2805 firstevno = lastentry + 1;
2806 //
2807 checkfirst = check;
2808 //
2809 };
2810 //
2811 if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2812 //
2813 };
2814 };
2815 };
2816 //
2817 return;
2818 };
2819
2820
2821 /**
2822 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2823 */
2824 Int_t PamelaDBOperations::insertCALO_CALIB(){
2825 //
2826 TSQLResult *result = 0;
2827 TSQLRow *row = 0;
2828 //
2829 stringstream oss;
2830 oss.str("");
2831 //
2832 CalibCalPedEvent *calibCalPed = 0;
2833 TTree *tr = 0;
2834 EventHeader *eh = 0;
2835 PscuHeader *ph = 0;
2836 //
2837 UInt_t nevents = 0;
2838 UInt_t fromtime = 0;
2839 UInt_t totime = 0;
2840 UInt_t obt = 0;
2841 UInt_t pkt = 0;
2842 //
2843 tr = (TTree*)file->Get("CalibCalPed");
2844 if ( !tr || tr->IsZombie() ) throw -21;
2845 //
2846 tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2847 tr->SetBranchAddress("Header", &eh);
2848 nevents = tr->GetEntries();
2849 //
2850 if ( !nevents ) return(1);
2851 //
2852 for (UInt_t i=0; i < nevents; i++){
2853 tr->GetEntry(i);
2854 for (UInt_t section = 0; section < 4; section++){
2855 //
2856 if ( calibCalPed->cstwerr[section] ){
2857 valid = 1;
2858 if ( calibCalPed->cperror[section] ) valid = 0;
2859 ph = eh->GetPscuHeader();
2860 obt = ph->GetOrbitalTime();
2861 pkt = ph->GetCounter();
2862 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2863 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2864 //
2865 if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2866 //
2867 // check if the calibration has already been inserted
2868 //
2869 oss.str("");
2870 oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2871 << " SECTION = "<< section << " AND "
2872 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2873 << " OBT = "<< obt << " AND "
2874 << " PKT = "<< pkt << ";";
2875 //
2876 if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2877 result = conn->Query(oss.str().c_str());
2878 //
2879 if ( !result ) throw -4;
2880 //
2881 row = result->Next();
2882 //
2883 if ( row ){
2884 //
2885 if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2886 //
2887 } else {
2888 //
2889 // we have to insert a new calibration, check where to place it
2890 //
2891 oss.str("");
2892 oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2893 << " SECTION = "<< section << " AND "
2894 << " FROM_TIME < "<< fromtime << " AND "
2895 << " TO_TIME > "<< fromtime << ";";
2896 //
2897 if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2898 result = conn->Query(oss.str().c_str());
2899 //
2900 if ( !result ) throw -4;
2901 //
2902 row = result->Next();
2903 //
2904 if ( !row ){
2905 //
2906 // no calibrations in the db contain our calibration
2907 //
2908 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2909 if ( fromtime < 1150871000 ){ //1150866904
2910 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2911 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2912 };
2913 //
2914 oss.str("");
2915 oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2916 << " SECTION = "<< section << " AND "
2917 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2918 //
2919 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2920 result = conn->Query(oss.str().c_str());
2921 //
2922 if ( !result ) throw -4;
2923 //
2924 row = result->Next();
2925 if ( !row ){
2926 totime = numeric_limits<UInt_t>::max();
2927 } else {
2928 totime = (UInt_t)atoll(row->GetField(0));
2929 };
2930 //
2931 } else {
2932 //
2933 // determine upper and lower limits and make space for the new calibration
2934 //
2935 totime = (UInt_t)atoll(row->GetField(1));
2936 //
2937 oss.str("");
2938 oss << " UPDATE GL_CALO_CALIB SET "
2939 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2940 << " ID = "<< row->GetField(0) << ";";
2941 //
2942 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2943 result = conn->Query(oss.str().c_str());
2944 //
2945 if ( !result ) throw -4;
2946 //
2947 };
2948 //
2949 oss.str("");
2950 oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2951 << " VALUES (NULL,' "
2952 << idroot << "','"
2953 << i << "','"
2954 << fromtime << "','"
2955 << totime << "','"
2956 << section << "','"
2957 << obt << "','"
2958 << pkt << "','"
2959 << this->GetBOOTnumber() << "','"
2960 << valid << "');";
2961 //
2962 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2963 //
2964 result = conn->Query(oss.str().c_str());
2965 //
2966 if ( !result ) throw -4;
2967 //
2968 };
2969 //
2970 } else {
2971 //
2972 if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2973 //
2974 };
2975 //
2976 };
2977 };
2978 };
2979 //
2980 return(0);
2981 };
2982
2983 /**
2984 * Fill the GL_TRK_CALIB table
2985 */
2986 void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
2987 //
2988 TSQLResult *result = 0;
2989 TSQLRow *row = 0;
2990 //
2991 stringstream oss;
2992 oss.str("");
2993 //
2994 UInt_t totime = 0;
2995 //
2996 if ( !pk1 && !pk2 ){
2997 if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
2998 };
2999 //
3000 // check if the calibration has already been inserted
3001 //
3002 oss.str("");
3003 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
3004 << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
3005 oss << " AND ( ( ";
3006 if ( pk1 ){
3007 oss << " OBT1 = "<< obt1 << " AND "
3008 << " PKT1 = "<< pkt1
3009 << " ) OR ( ";
3010 } else {
3011 oss << " PKT1 = "<< pkt2-1
3012 << " ) OR ( ";
3013 };
3014 if ( pk2 ){
3015 oss << " OBT2 = "<< obt2 << " AND "
3016 << " PKT2 = "<< pkt2;
3017 } else {
3018 oss << " PKT2 = "<< pkt1+1;
3019 };
3020 oss << " ) );";
3021 //
3022 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3023 result = conn->Query(oss.str().c_str());
3024 //
3025 if ( !result ) throw -4;
3026 //
3027 row = result->Next();
3028 //
3029 if ( row ){
3030 //
3031 if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
3032 //
3033 } else {
3034 //
3035 // we have to insert a new calibration, check where to place it
3036 //
3037 oss.str("");
3038 oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
3039 << " FROM_TIME < "<< fromtime << " AND "
3040 << " TO_TIME > "<< fromtime << ";";
3041 //
3042 if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
3043 result = conn->Query(oss.str().c_str());
3044 //
3045 if ( !result ) throw -4;
3046 //
3047 row = result->Next();
3048 //
3049 if ( !row ){
3050 //
3051 // no calibrations in the db contain our calibration
3052 //
3053 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
3054 if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
3055 //
3056 oss.str("");
3057 oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
3058 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3059 //
3060 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3061 result = conn->Query(oss.str().c_str());
3062 //
3063 if ( !result ) throw -4;
3064 //
3065 row = result->Next();
3066 if ( !row ){
3067 totime = numeric_limits<UInt_t>::max();
3068 } else {
3069 totime = (UInt_t)atoll(row->GetField(0));
3070 };
3071 //
3072 } else {
3073 //
3074 // determine upper and lower limits and make space for the new calibration
3075 //
3076 totime = (UInt_t)atoll(row->GetField(1));
3077 //
3078 oss.str("");
3079 oss << " UPDATE GL_TRK_CALIB SET "
3080 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3081 << " ID = "<< row->GetField(0) << ";";
3082 //
3083 if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
3084 result = conn->Query(oss.str().c_str());
3085 //
3086 if ( !result ) throw -4;
3087 //
3088 };
3089 //
3090 oss.str("");
3091 oss << " INSERT INTO GL_TRK_CALIB (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) "
3092 << " VALUES (NULL,' "
3093 << idroot << "',";
3094 //
3095 if ( !pk1 ){
3096 oss << "NULL,";
3097 } else {
3098 oss << "'"
3099 << t1 << "',";
3100 };
3101 //
3102 if ( !pk2 ){
3103 oss << "NULL,'";
3104 } else {
3105 oss << "'"
3106 << t2 << "','";
3107 };
3108 //
3109 oss << fromtime << "','"
3110 << totime << "','"
3111 << obt1 << "','"
3112 << pkt1 << "','"
3113 << obt2 << "','"
3114 << pkt2 << "','"
3115 << this->GetBOOTnumber() << "','"
3116 << valid << "');";
3117 //
3118 if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
3119 //
3120 result = conn->Query(oss.str().c_str());
3121 //
3122 if ( !result ) throw -4;
3123 //
3124 };
3125 //
3126 };
3127
3128 /**
3129 * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
3130 */
3131 Int_t PamelaDBOperations::insertTRK_CALIB(){
3132 //
3133 CalibTrk1Event *caltrk1 = 0;
3134 CalibTrk2Event *caltrk2 = 0;
3135 TTree *tr1 = 0;
3136 TTree *tr2 = 0;
3137 EventHeader *eh1 = 0;
3138 PscuHeader *ph1 = 0;
3139 EventHeader *eh2 = 0;
3140 PscuHeader *ph2 = 0;
3141 //
3142 PacketType *pctp=0;
3143 EventCounter *codt2=0;
3144 //
3145 Int_t nevents1 = 0;
3146 Int_t nevents2 = 0;
3147 //
3148 fromtime = 0;
3149 //
3150 obt1 = 0;
3151 pkt1 = 0;
3152 obt2 = 0;
3153 pkt2 = 0;
3154 //
3155 tr1 = (TTree*)file->Get("CalibTrk1");
3156 if ( !tr1 || tr1->IsZombie() ) throw -22;
3157 tr2 = (TTree*)file->Get("CalibTrk2");
3158 if ( !tr2 || tr2->IsZombie() ) throw -23;
3159 //
3160 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
3161 tr1->SetBranchAddress("Header", &eh1);
3162 nevents1 = tr1->GetEntries();
3163 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
3164 tr2->SetBranchAddress("Header", &eh2);
3165 nevents2 = tr2->GetEntries();
3166 //
3167 if ( !nevents1 && !nevents2 ) return(1);
3168 //
3169 t2 = -1;
3170 Int_t pret2 = 0;
3171 Int_t t2t1cal = 0;
3172 //
3173 for (t1=0; t1 < nevents1; t1++){
3174 //
3175 pret2 = t2;
3176 tr1->GetEntry(t1);
3177 //
3178 ph1 = eh1->GetPscuHeader();
3179 obt1 = ph1->GetOrbitalTime();
3180 pkt1 = ph1->GetCounter();
3181 fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
3182 //
3183 // valid = 1;
3184 // //
3185 // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3186 //
3187 //
3188 if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3189 //
3190 if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3191 //
3192 valid = ValidateTrkCalib( caltrk1, eh1 );
3193 if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3194 //
3195 // Do we have the second calibration packet?
3196 //
3197 while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
3198 //
3199 t2++;
3200 //
3201 if ( t2 < nevents2 ){
3202 tr2->GetEntry(t2);
3203 codt2 = eh2->GetCounter();
3204 t2t1cal = codt2->Get(pctp->CalibTrk1);
3205 //
3206 ph2 = eh2->GetPscuHeader();
3207 obt2 = ph2->GetOrbitalTime();
3208 pkt2 = ph2->GetCounter();
3209 //
3210 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3211 //
3212 } else {
3213 //
3214 // running out of vector without finding the corresponding calibration, sig
3215 //
3216 pret2 = t2;
3217 obt2 = 0;
3218 pkt2 = pkt1+2;
3219 t2t1cal = t1+1;
3220 };
3221 if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
3222 //
3223 // running out of vector without finding the corresponding calibration, sig
3224 //
3225 pret2 = t2;
3226 obt2 = 0;
3227 pkt2 = pkt1+2;
3228 t2t1cal = t1+1;
3229 };
3230 //
3231 };
3232 //
3233 if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);
3234 //
3235 // The calibration is good
3236 //
3237 if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3238 //
3239 if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
3240 //
3241 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3242 if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3243 valid = valid & valid2;
3244 //
3245 // Handle good calib
3246 //
3247 this->HandleTRK_CALIB(true,true);
3248 //
3249 // Check for missing calibtrk1
3250 //
3251 if ( t2 != pret2+1 ){
3252 //
3253 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i pret2 is %i \n",obt2,pkt2,t2,pret2);
3254 //
3255 while ( t2 > pret2+1 ){
3256 //
3257 // handle missing calib1
3258 //
3259 pret2++;
3260 //
3261 obt1 = 0;
3262 pkt1 = 0;
3263 //
3264 tr2->GetEntry(pret2);
3265 ph2 = eh2->GetPscuHeader();
3266 obt2 = ph2->GetOrbitalTime();
3267 pkt2 = ph2->GetCounter();
3268 //
3269 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3270 //
3271 valid = 0;
3272 this->HandleTRK_CALIB(false,true);
3273 //
3274 };
3275 //
3276 };
3277 //
3278 } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
3279 //
3280 // Check for missing calibtrk2
3281 //
3282 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3283 t2 = pret2;
3284 //
3285 // handle missing calib2
3286 //
3287 obt2 = 0;
3288 pkt2 = 0;
3289 valid = 0;
3290 this->HandleTRK_CALIB(true,false);
3291 //
3292 };
3293 //
3294 } else {
3295 //
3296 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3297 //
3298 };
3299 //
3300 };
3301 //
3302 // we have one more calib pkt2 !
3303 //
3304 t2++;
3305 while ( t2 < nevents2 ){
3306 //
3307 // handle missing calib1
3308 //
3309 obt1 = 0;
3310 pkt1 = 0;
3311 //
3312 tr2->GetEntry(t2);
3313 ph2 = eh2->GetPscuHeader();
3314 obt2 = ph2->GetOrbitalTime();
3315 pkt2 = ph2->GetCounter();
3316 //
3317 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3318 valid = 0;
3319 if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3320 //
3321 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3322 //
3323 this->HandleTRK_CALIB(false,true);
3324 //
3325 };
3326 //
3327 t2++;
3328 //
3329 };
3330 //
3331 return(0);
3332 };
3333
3334
3335 /**
3336 * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3337 */
3338 Int_t PamelaDBOperations::insertS4_CALIB(){
3339 //
3340 TSQLResult *result = 0;
3341 TSQLRow *row = 0;
3342 //
3343 stringstream oss;
3344 oss.str("");
3345 //
3346 TTree *tr = 0;
3347 EventHeader *eh = 0;
3348 PscuHeader *ph = 0;
3349 //
3350 UInt_t nevents = 0;
3351 UInt_t fromtime = 0;
3352 UInt_t totime = 0;
3353 UInt_t obt = 0;
3354 UInt_t pkt = 0;
3355 //
3356 tr = (TTree*)file->Get("CalibS4");
3357 if ( !tr || tr->IsZombie() ) throw -24;
3358 //
3359 tr->SetBranchAddress("Header", &eh);
3360 //
3361 nevents = tr->GetEntries();
3362 //
3363 if ( !nevents ) return(1);
3364 //
3365 for (UInt_t i = 0; i < nevents; i++){
3366 //
3367 tr->GetEntry(i);
3368 //
3369 ph = eh->GetPscuHeader();
3370 obt = ph->GetOrbitalTime();
3371 pkt = ph->GetCounter();
3372 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3373 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3374 //
3375 if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3376 //
3377 // check if the calibration has already been inserted
3378 //
3379 oss.str("");
3380 oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3381 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3382 << " OBT = "<< obt << " AND "
3383 << " PKT = "<< pkt << ";";
3384 //
3385 if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3386 result = conn->Query(oss.str().c_str());
3387 //
3388 if ( !result ) throw -4;
3389 //
3390 row = result->Next();
3391 //
3392 if ( row ){
3393 //
3394 if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3395 //
3396 } else {
3397 //
3398 // we have to insert a new calibration, check where to place it
3399 //
3400 oss.str("");
3401 oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3402 << " FROM_TIME < "<< fromtime << " AND "
3403 << " TO_TIME > "<< fromtime << ";";
3404 //
3405 if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3406 result = conn->Query(oss.str().c_str());
3407 //
3408 if ( !result ) throw -4;
3409 //
3410 row = result->Next();
3411 //
3412 if ( !row ){
3413 //
3414 // no calibrations in the db contain our calibration
3415 //
3416 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3417 if ( fromtime < 1150871000 ){
3418 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3419 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3420 };
3421 //
3422 oss.str("");
3423 oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3424 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3425 //
3426 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3427 result = conn->Query(oss.str().c_str());
3428 //
3429 if ( !result ) throw -4;
3430 //
3431 row = result->Next();
3432 if ( !row ){
3433 totime = numeric_limits<UInt_t>::max();
3434 } else {
3435 totime = (UInt_t)atoll(row->GetField(0));
3436 };
3437 //
3438 } else {
3439 //
3440 // determine upper and lower limits and make space for the new calibration
3441 //
3442 totime = (UInt_t)atoll(row->GetField(1));
3443 //
3444 oss.str("");
3445 oss << " UPDATE GL_S4_CALIB SET "
3446 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3447 << " ID = "<< row->GetField(0) << ";";
3448 //
3449 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3450 result = conn->Query(oss.str().c_str());
3451 //
3452 if ( !result ) throw -4;
3453 //
3454 };
3455 //
3456 oss.str("");
3457 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3458 << " VALUES (NULL,' "
3459 << idroot << "','"
3460 << i << "','"
3461 << fromtime << "','"
3462 << totime << "','"
3463 << obt << "','"
3464 << pkt << "','"
3465 << this->GetBOOTnumber() << "');";
3466 //
3467 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3468 //
3469 result = conn->Query(oss.str().c_str());
3470 //
3471 if ( !result ) throw -4;
3472 //
3473 };
3474 //
3475 } else {
3476 //
3477 if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3478 //
3479 };
3480 //
3481 };
3482 //
3483 return(0);
3484 };
3485
3486 /**
3487 * Scan the fragment table and move old fragments to the GL_RUN table
3488 */
3489 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3490 return(this->CleanGL_RUN_FRAGMENTS(""));
3491 };
3492
3493 /**
3494 * Scan the fragment table and move old fragments to the GL_RUN table
3495 */
3496 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3497 //
3498 TSQLResult *result = 0;
3499 TSQLRow *row = 0;
3500 TSQLResult *result2 = 0;
3501 TSQLRow *row2 = 0;
3502 //
3503 UInt_t moved = 0;
3504 //
3505 stringstream oss;
3506 oss.str("");
3507 //
3508 if ( !strcmp(fcleanfile.Data(),"") ){
3509 //
3510 // check if there are entries older than "olderthan" seconds from now
3511 //
3512 oss.str("");
3513 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3514 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3515 //
3516 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3517 result = conn->Query(oss.str().c_str());
3518 //
3519 } else {
3520 oss.str("");
3521 oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3522 if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3523 result = conn->Query(oss.str().c_str());
3524 //
3525 if ( result ){
3526 //
3527 row = result->Next();
3528 //
3529 if ( row ){
3530 oss.str("");
3531 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3532 << " ID_ROOT_L0=" << row->GetField(0) << ";";
3533 //
3534 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3535 result = conn->Query(oss.str().c_str());
3536 //
3537 };
3538 } else {
3539 return(2);
3540 };
3541 };
3542 //
3543 if ( result ){
3544 //
3545 row = result->Next();
3546 //
3547 while ( row ){
3548 //
3549 oss.str("");
3550 oss << " ID= "<< row->GetField(0);
3551 //
3552 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3553 //
3554 oss.str("");
3555 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3556 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3557 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3558 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3559 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3560 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3561 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3562 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3563 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3564 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3565 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3566 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3567 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3568 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3569 //
3570 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3571 result2 = conn->Query(oss.str().c_str());
3572 //
3573 if ( !result2 ) throw -4;
3574 //
3575 row2 = result2->Next();
3576 //
3577 if ( !row2 ){
3578 //
3579 if ( IsDebug() ) printf(" The run is new \n");
3580 if ( IsDebug() ) printf(" -> fill the DB \n");
3581 //
3582 // glrun->SetID(this->AssignRunID()); we use the old run number!
3583 glrun->SetID_RUN_FRAG(glrun->GetID());
3584 glrun->Fill_GL_RUN(conn);
3585 //
3586 // oss.str("");
3587 // oss << " SELECT ID FROM GL_RUN WHERE "
3588 // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3589 // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3590 // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3591 // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3592 // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3593 // //
3594 // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3595 // result2 = conn->Query(oss.str().c_str());
3596 // //
3597 // if ( !result2 ) throw -4;
3598 // //
3599 // row2 = result2->Next();
3600 // //
3601 // if ( !row2 ) throw -25;
3602 // //
3603 // oss.str("");
3604 // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3605 // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3606 // result2 = conn->Query(oss.str().c_str());
3607 // //
3608 // if ( !result2 ) throw -4;
3609 //
3610 moved++;
3611 //
3612 } else {
3613 if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3614 };
3615 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3616 //
3617 //
3618 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3619 // oss.str("");
3620 // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3621 // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3622 // result2 = conn->Query(oss.str().c_str());
3623 // //
3624 // if ( !result2 ) throw -4;
3625 // //
3626 row = result->Next();
3627 };
3628 };
3629 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3630 return(0);
3631 };
3632
3633 /**
3634 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3635 */
3636 Int_t PamelaDBOperations::ValidateRuns(){
3637 return(this->ValidateRuns(""));
3638 };
3639
3640 /**
3641 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3642 */
3643 Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3644 //
3645 TSQLResult *result = 0;
3646 TSQLRow *row = 0;
3647 //
3648 UInt_t calibtime = 50;
3649 //
3650 stringstream oss;
3651 oss.str("");
3652 //
3653 // =======================================================
3654 // validate runs by checking missing calibrations
3655 // =======================================================
3656 UInt_t t_stop = 0;
3657 UInt_t t_start = 0;
3658 if ( !strcmp(valfile.Data(),"") ) {
3659 // --------------------------------------------------------------
3660 // 1) get the OBT of the last run inserted after clean-time limit
3661 // --------------------------------------------------------------
3662 oss.str("");
3663 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3664 << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3665 if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3666 result = conn->Query(oss.str().c_str());
3667 if ( !result ) throw -4;
3668 if ( !result->GetRowCount() ) {
3669 printf(" No runs to validate \n");
3670 return(1);
3671 }else{
3672 row = result->Next();
3673 t_start = (UInt_t)atoll(row->GetField(4));
3674 };
3675 // --------------------------------------------------------------
3676 // 2) get the OBT of the last validated run
3677 // --------------------------------------------------------------
3678 oss.str("");
3679 oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3680 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3681 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3682 result = conn->Query(oss.str().c_str());
3683 if ( !result ) throw -4;
3684 if ( result->GetRowCount() ){
3685 row = result->Next();
3686 t_stop = (UInt_t)atoll(row->GetField(4));
3687 };
3688 if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3689 // --------------------------------------------------------------
3690 // now retrieves runs to be validated
3691 // --------------------------------------------------------------
3692 oss.str("");
3693 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3694 oss << " AND RUNHEADER_TIME >="<< t_stop;
3695 oss << " ORDER BY RUNHEADER_TIME DESC;";
3696 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3697 result = conn->Query(oss.str().c_str());
3698 } else {
3699 //
3700 stringstream myquery;
3701 UInt_t myid = 0;
3702 myquery.str("");
3703 myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3704 //
3705 result = conn->Query(myquery.str().c_str());
3706 //
3707 row = result->Next();
3708 if( !row ){
3709 if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3710 if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3711 return(2);
3712 };
3713 if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3714 return(0);
3715 };
3716 myid=(UInt_t)atoll(row->GetField(0));
3717 //
3718 myquery.str("");
3719 myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3720 //
3721 result = conn->Query(myquery.str().c_str());
3722 //
3723 row = result->Next();
3724 if( !row->GetField(0) || !row->GetField(1)){
3725 //
3726 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3727 //
3728 return(0);
3729 //
3730 } else {
3731 //
3732 UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3733 UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3734 UInt_t caltime = 0;
3735 //
3736 myquery.str("");
3737 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3738 myquery << " order by FROM_TIME asc limit 1;";
3739 //
3740 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3741 //
3742 //
3743 result = conn->Query(myquery.str().c_str());
3744 //
3745 row = result->Next();
3746 if( !row ){
3747 caltime = runhtime;
3748 } else {
3749 caltime = (UInt_t)atoll(row->GetField(0));
3750 };
3751 //
3752 myquery.str("");
3753 myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3754 myquery << caltime << " order by RUNHEADER_TIME DESC";
3755 //
3756 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3757 //
3758 result = conn->Query(myquery.str().c_str());
3759 //
3760 };
3761 };
3762 //
3763 if ( !result ) throw -4;
3764 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3765 //
3766 Int_t nrow = 0;
3767 GL_RUN* this_run = new GL_RUN();
3768 GL_RUN* next_run = new GL_RUN();
3769 Int_t nseq_max = 1000;
3770 // UInt_t* sequence = new UInt_t[100];
3771 vector<UInt_t> sequence(nseq_max);
3772 Int_t nseq = 0;
3773 Bool_t CHECK = false;
3774 Bool_t this_ONLINE = false;
3775 Bool_t next_ONLINE = false;
3776 UInt_t t1=0,t2=0;
3777 // ---------------------------------------------------------------------------------
3778 // - loop over runs, back in time,
3779 // - select sequences of runs close in time (less than calibtime s apart),
3780 // which could be preceeded by a calibration
3781 // - check if there might be a missing calibration
3782 // ---------------------------------------------------------------------------------
3783 while(1){
3784
3785 row = result->Next();
3786 if( row == NULL ) break;
3787
3788 //------------
3789 //get run info
3790 //------------
3791 this_run->Set_GL_RUN(row);
3792
3793 Bool_t this_BAD = false;
3794 if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3795 else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3796 else{
3797 // printf("Missing or corrupted header!! \n");
3798 this_ONLINE = false;
3799 this_BAD = true;
3800 };
3801
3802 //-----------------------------------
3803 //compare with previous(next in time)
3804 //-----------------------------------
3805 CHECK = false;
3806 UInt_t interval=0;
3807
3808 if( nrow != 0){
3809
3810
3811 t1 = this_run->GetRUNTRAILER_TIME();
3812 t2 = next_run->GetRUNHEADER_TIME();
3813 interval = (t2-t1);
3814
3815 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3816
3817 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3818
3819 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
3820
3821 if( !CHECK && this_run->VALIDATION ){
3822 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3823 nseq=0;
3824 }
3825
3826 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3827
3828 CHECK = true;
3829
3830 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3831
3832 assignVALIDATION(next_run->ID,true);
3833 nseq=0;
3834 }
3835 }
3836
3837 //----------------------------
3838 //check run sequence for calib
3839 //----------------------------
3840 if( CHECK ){
3841 // check if calibration exists
3842 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3843 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3844 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3845 nseq=0;
3846 };
3847 //--------------
3848 //store run info
3849 //--------------
3850 *next_run = *this_run;
3851 next_ONLINE = this_ONLINE;
3852 if( !this_BAD ){
3853 if(nseq < nseq_max){
3854 sequence[nseq] = this_run->ID;
3855 nseq++;
3856 }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3857 };
3858
3859 if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3860 nrow++;
3861
3862 };
3863 delete this_run;
3864 delete next_run;
3865 //
3866 return(0);
3867 };
3868 /**
3869 * Check if there might be a missing tracker calibration in a given time interval
3870 * @param t1 From absolute time
3871 * @param t2 To absolute time
3872 * @return true if there might be a missing calibration
3873 */
3874 Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3875
3876 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3877
3878 // get the closest VALIDATED calibration before the run start (t2)
3879 if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3880
3881 if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3882
3883 //==============================================================
3884 // Check is done first on the basis of time between calibration,
3885 // which should be equal to the time between ascending-nodes.
3886 //==============================================================
3887 if ( t2 - trkcalib->FROM_TIME > 5700) {
3888 if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3889 //==============================================================
3890 // there might be a missing calibration, due to:
3891 // - MM full
3892 // - corrupted packets
3893 // - loss of data
3894 // There is an exception in case a download was done during ascending node
3895 //==============================================================
3896 Bool_t DOWNLOAD = false;
3897 // check if the calib was skipped becouse of download .... DA FARE!!
3898 if(DOWNLOAD)return(false);
3899
3900 return(true); //>>> missing
3901
3902 };
3903
3904 //==============================================================
3905 // If the last calibration is close to the run less than this time,
3906 // it is enough to say that there are no missing calibrations
3907 //==============================================================
3908 // the long time interval bewteen runs might be due to download
3909 if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3910 return(false);
3911
3912 };
3913 /**
3914 * Assign VALIDATION value to a GL_RUN entry
3915 * @param idrun Run ID
3916 * @param validation true/false
3917 */
3918 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3919 TSQLResult *result = 0;
3920 stringstream oss;
3921 oss.str("");
3922 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3923 //
3924 // if ( IsDebug() )
3925 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3926 if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3927 result = conn->Query(oss.str().c_str());
3928 if ( !result ) throw -4;
3929 return(0);
3930 }
3931
3932
3933
3934 // Insert TLEs from file tlefilename in the table GL_TLE in the db
3935 // opened by conn, sorting them by date from older to newer, if each
3936 // TLE has not been alread inserted.
3937 Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3938 {
3939 fstream tlefile(tlefilename, ios::in);
3940
3941 if ( !tlefile ) throw -7;
3942
3943 vector<cTle*> ctles;
3944 vector<cTle*>::iterator iter;
3945 int present = 0;
3946
3947 // Get three lines from tlefile, create a cTle object and put it
3948 // into ctles
3949 while(1) {
3950 cTle *tlef;
3951 string str1, str2, str3;
3952
3953 getline(tlefile, str1);
3954 if(tlefile.eof()) break;
3955
3956 getline(tlefile, str2);
3957 if(tlefile.eof()) break;
3958
3959 getline(tlefile, str3);
3960 if(tlefile.eof()) break;
3961
3962 // We now have three good lines for a cTle.
3963 tlef = new cTle(str1, str2, str3);
3964 ctles.push_back(tlef);
3965 }
3966
3967 tlefile.close();
3968
3969 // Sort by date
3970 sort(ctles.begin(), ctles.end(), compTLE);
3971
3972 // Now we insert each TLE into the db
3973 for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3974 cTle *tle = *iter;
3975
3976 // Do nothing if it's already present in the db. Just increase
3977 // the counter present.
3978 if (! isTlePresent(tle))
3979 {
3980 int status = insertTle(tle);
3981
3982 // Insert query failed. Return 1.
3983 if(status == EXIT_FAILURE) {
3984
3985 if( IsDebug() ) {
3986 cerr << "Error: inserting TLE:" << endl
3987 << tle->getName() << endl
3988 << tle->getLine1() << endl
3989 << tle->getLine2() << endl;
3990 }
3991
3992 throw -4;
3993 return 1;
3994 }
3995
3996 }
3997 else
3998 present++;
3999
4000 }
4001
4002 int inserted = ctles.size() - present; // Number of inserted TLE.
4003 if ( IsDebug() )
4004 cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4005 << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4006
4007 ctles.clear();
4008
4009
4010 // Return 2 if no new TLE has been inserted. 0 otherwise.
4011 if(! inserted ) return 2;
4012 return 0;
4013 }
4014
4015
4016 // Insert tle in the table GL_TLE using the connection conn.
4017 Int_t PamelaDBOperations::insertTle(cTle *tle)
4018 {
4019 stringstream oss;
4020 TSQLResult *result = 0;
4021
4022 oss.str("");
4023 oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4024 << " VALUES ( '"
4025 << tle->getName() << "', '"
4026 << tle->getLine1() << "', '"
4027 << tle->getLine2() << "', '"
4028 << getTleDatetime(tle) << "')";
4029
4030 // cout << oss.str().c_str() << endl;
4031 result = conn->Query(oss.str().c_str());
4032 if (result == NULL)
4033 return EXIT_FAILURE;
4034
4035 return EXIT_SUCCESS;
4036 }
4037
4038
4039 // Return whether tle is already in the db connected by conn.
4040 bool PamelaDBOperations::isTlePresent(cTle *tle)
4041 {
4042 stringstream oss;
4043 TSQLResult *result = 0;
4044
4045 oss.str("");
4046 oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4047 << getTleDatetime(tle) << "'";
4048
4049 result = conn->Query(oss.str().c_str());
4050 if (result == NULL) throw -4;
4051
4052 if (result->GetRowCount())
4053 return true;
4054 else
4055 return false;
4056 }
4057
4058
4059 // Return whether the first TLE is dated early than the second
4060 bool compTLE (cTle *tle1, cTle *tle2)
4061 {
4062 return getTleJulian(tle1) < getTleJulian(tle2);
4063 }
4064
4065
4066 // Return the date of the tle using the format (year-2000)*1e3 +
4067 // julian day. e.g. 6365 is the 31th Dec 2006.
4068 // It does *not* return a cJulian date.
4069 float getTleJulian(cTle *tle) {
4070 return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4071 }
4072
4073
4074 // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4075 // format.
4076 string getTleDatetime(cTle *tle)
4077 {
4078 int year, mon, day, hh, mm, ss;
4079 double dom; // day of month (is double!)
4080 stringstream date; // date in datetime format
4081
4082 // create a cJulian from the date in tle
4083 cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4084
4085 // get year, month, day of month
4086 jdate.getComponent(&year, &mon, &dom);
4087
4088 // build a datetime YYYY-MM-DD hh:mm:ss
4089 date.str("");
4090 day = (int) floor(dom);
4091 hh = (int) floor( (dom - day) * 24);
4092 mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4093 ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4094 // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4095
4096 date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4097
4098 return date.str();
4099 }
4100
4101 /**
4102 * Remove a file from the DB, delete on cascade all entries related to that file
4103 * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4104 * calibration
4105 **/
4106 Int_t PamelaDBOperations::removeFile(TString remfile){
4107 //
4108 // Determine ID_ROOT_L0 and ID_RAW
4109 //
4110 TSQLResult *pResult;
4111 TSQLRow *Row;
4112 stringstream myquery;
4113 //
4114 myquery.str("");
4115 myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4116 //
4117 pResult = conn->Query(myquery.str().c_str());
4118 //
4119 Row = pResult->Next();
4120 if( !Row ){
4121 if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4122 if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4123 return(1);
4124 };
4125 if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4126 return(0);
4127 };
4128 //
4129 this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4130 this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4131 //
4132 this->ValidationOFF();
4133 //
4134 this->RemoveCALIBS();
4135 //
4136 this->RemoveRUNS();
4137 //
4138 this->RemoveFILES();
4139 //
4140 this->SetID_ROOT(0);
4141 this->SetID_RAW(0);
4142 //
4143 return(0);
4144 };
4145
4146 /**
4147 *
4148 * Set validation bit to zero for runs following the removing file till
4149 * 1) a run with TRK_CALIB_USED=140
4150 * 2) a run with VALIDATION = 0
4151 * 3) the next calibration
4152 *
4153 **/
4154 void PamelaDBOperations::ValidationOFF(){
4155 TSQLResult *pResult;
4156 TSQLRow *Row;
4157 stringstream myquery;
4158 Int_t unv = 0;
4159 //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4160 myquery.str("");
4161 myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4162 //
4163 pResult = conn->Query(myquery.str().c_str());
4164 //
4165 Row = pResult->Next();
4166 if( !Row->GetField(0) ){
4167 //
4168 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4169 //
4170 } else {
4171 //
4172 UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4173 UInt_t caltime = 0;
4174 //
4175 myquery.str("");
4176 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4177 myquery << " order by FROM_TIME asc limit 1;";
4178 //
4179 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4180 //
4181 //
4182 delete pResult;
4183 pResult = conn->Query(myquery.str().c_str());
4184 //
4185 Row = pResult->Next();
4186 if( !Row ){
4187 caltime = runhtime;
4188 } else {
4189 caltime = (UInt_t)atoll(Row->GetField(0));
4190 };
4191 //
4192 myquery.str("");
4193 myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4194 myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4195 //
4196 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4197 //
4198 pResult = conn->Query(myquery.str().c_str());
4199 //
4200 Row = pResult->Next();
4201 if( !Row ){
4202 //
4203 if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4204 //
4205 } else {
4206 myquery.str("");
4207 myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4208 myquery << " RUNHEADER_TIME>=" <<runhtime;
4209 myquery << " order by RUNHEADER_TIME asc;";
4210 //
4211 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4212 //
4213 pResult = conn->Query(myquery.str().c_str());
4214 //
4215 Row = pResult->Next();
4216 while ( Row ){
4217 //
4218 unv++;
4219 this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4220 Row = pResult->Next();
4221 //
4222 };
4223 };
4224 };
4225 if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4226 };
4227
4228 /**
4229 *
4230 * Rearrange GL_RUN table and remove runs
4231 *
4232 **/
4233 void PamelaDBOperations::RemoveRUNS(){
4234 TSQLResult *pResult;
4235 TSQLRow *Row;
4236 stringstream myquery;
4237 UInt_t drun = 0;
4238 GL_RUN *delrun = new GL_RUN();
4239 //
4240 myquery.str("");
4241 myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4242 //
4243 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4244 //
4245 pResult = conn->Query(myquery.str().c_str());
4246 //
4247 Row = pResult->Next();
4248 //
4249 //
4250 if ( !Row ){
4251 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4252 } else {
4253 if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
4254 while ( Row ){
4255 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4256 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4257 drun++;
4258 Row = pResult->Next();
4259 };
4260 };
4261 //
4262 //
4263 myquery.str("");
4264 myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4265 //
4266 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4267 //
4268 pResult = conn->Query(myquery.str().c_str());
4269 //
4270 Row = pResult->Next();
4271 //
4272 if ( !Row ){
4273 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4274 } else {
4275 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
4276 while ( Row ){
4277 if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4278 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4279 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4280 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4281 if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4282 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4283 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4284 };
4285 drun++;
4286 Row = pResult->Next();
4287 };
4288 };
4289 //
4290 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
4291 //
4292 //
4293 //
4294 drun = 0;
4295 //
4296 myquery.str("");
4297 myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4298 //
4299 pResult = conn->Query(myquery.str().c_str());
4300 //
4301 Row = pResult->Next();
4302 //
4303 if ( !Row ){
4304 if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4305 } else {
4306 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
4307 while ( Row ){
4308 if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4309 myquery.str("");
4310 myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
4311 conn->Query(myquery.str().c_str());
4312 drun++;
4313 Row = pResult->Next();
4314 };
4315 };
4316 //
4317 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);
4318 //
4319 //
4320 //
4321 drun = 0;
4322 //
4323 myquery.str("");
4324 myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4325 //
4326 pResult = conn->Query(myquery.str().c_str());
4327 //
4328 Row = pResult->Next();
4329 //
4330 if ( !Row ){
4331 if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4332 } else {
4333 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
4334 while ( Row ){
4335 if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4336 myquery.str("");
4337 myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4338 conn->Query(myquery.str().c_str());
4339 drun++;
4340 Row = pResult->Next();
4341 };
4342 };
4343 //
4344 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);
4345 //
4346 //
4347 //
4348 delete delrun;
4349 //
4350 };
4351
4352
4353 /**
4354 *
4355 * Rearrange calibration tables
4356 *
4357 **/
4358 void PamelaDBOperations::RemoveFILES(){
4359 stringstream myquery;
4360 //
4361 myquery.str("");
4362 myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4363 //
4364 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4365 //
4366 conn->Query(myquery.str().c_str());
4367 //
4368 };
4369
4370 /**
4371 *
4372 * Rearrange calibration tables
4373 *
4374 **/
4375 void PamelaDBOperations::RemoveCALIBS(){
4376 TSQLResult *pResult;
4377 TSQLRow *Row;
4378 stringstream myquery;
4379 //
4380 //
4381 // Calorimeter
4382 //
4383 for (Int_t section = 0; section < 4; section++){
4384 myquery.str("");
4385 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4386 myquery << " SECTION=" << section << ";";
4387 //
4388 pResult = conn->Query(myquery.str().c_str());
4389 //
4390 Row = pResult->Next();
4391 if( !Row->GetField(0) || !Row->GetField(1) ){
4392 //
4393 if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4394 //
4395 } else {
4396 //
4397 myquery.str("");
4398 myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4399 myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4400 myquery << " SECTION=" << section << ";";
4401 //
4402 pResult = conn->Query(myquery.str().c_str());
4403 //
4404 if( !pResult ){
4405 //
4406 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4407 //
4408 throw -4;
4409 //
4410 };
4411 //
4412 };
4413 };
4414 myquery.str("");
4415 myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4416 //
4417 pResult = conn->Query(myquery.str().c_str());
4418 //
4419 if( !pResult ){
4420 //
4421 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4422 //
4423 throw -4;
4424 //
4425 };
4426 //
4427 // Tracker
4428 //
4429 myquery.str("");
4430 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4431 //
4432 pResult = conn->Query(myquery.str().c_str());
4433 //
4434 Row = pResult->Next();
4435 if( !Row->GetField(0) || !Row->GetField(1) ){
4436 //
4437 if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4438 //
4439 } else {
4440 //
4441 myquery.str("");
4442 myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4443 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4444 //
4445 pResult = conn->Query(myquery.str().c_str());
4446 //
4447 if( !pResult ){
4448 //
4449 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4450 //
4451 throw -4;
4452 //
4453 };
4454 //
4455 myquery.str("");
4456 myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4457 //
4458 pResult = conn->Query(myquery.str().c_str());
4459 //
4460 if( !pResult ){
4461 //
4462 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4463 //
4464 throw -4;
4465 //
4466 };
4467 };
4468 //
4469 //
4470 // S4
4471 //
4472 myquery.str("");
4473 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4474 //
4475 pResult = conn->Query(myquery.str().c_str());
4476 //
4477 Row = pResult->Next();
4478 if( !Row->GetField(0) || !Row->GetField(1) ){
4479 //
4480 if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4481 //
4482 } else {
4483 //
4484 myquery.str("");
4485 myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4486 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4487 //
4488 pResult = conn->Query(myquery.str().c_str());
4489 //
4490 if( !pResult ){
4491 //
4492 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4493 //
4494 throw -4;
4495 //
4496 };
4497 //
4498 myquery.str("");
4499 myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4500 //
4501 pResult = conn->Query(myquery.str().c_str());
4502 //
4503 if( !pResult ){
4504 //
4505 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4506 //
4507 throw -4;
4508 //
4509 };
4510 //
4511 };
4512 };
4513
4514 /**
4515 *
4516 * Rearrange calibration tables
4517 *
4518 **/
4519 UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4520
4521 Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4522 UInt_t timeaftercalib=120000; //2000;
4523 // ----------
4524 // Check CRCs
4525 // ----------
4526 for(Int_t ipkt=0; ipkt<6; ipkt++){
4527 if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4528 for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4529 }
4530 // -----------------------
4531 // Check missing packets:
4532 // -----------------------
4533 // Readout order:
4534 // ------------------
4535 // DSP packet board
4536 // ------------------
4537 // 12 0 1
4538 // 10 1 1
4539 // 8 2 1
4540 // 4 3 1
4541 // 6 4 1
4542 // 2 5 1
4543 // ------------------
4544 // 11 0 2
4545 // 9 1 2
4546 // 7 2 2
4547 // 3 3 2
4548 // 5 4 2
4549 // 1 5 2
4550 // ------------------
4551 // -------------------------------------------------
4552 // Check if it is first or second calibration packet
4553 // -------------------------------------------------
4554 UInt_t build=0;
4555 TString classname = caltrk->GetName();
4556 UInt_t base=0;
4557 UInt_t mask=0;
4558 if(classname.Contains("CalibTrk1Event")){
4559 base=12;
4560 mask=0x03F000;
4561 }
4562 if(classname.Contains("CalibTrk2Event")){
4563 base=18;
4564 mask=0xFC0000;
4565 }
4566 // -------------------------------------------------
4567 // Count number of packets and set build variable
4568 // -------------------------------------------------
4569 Int_t npkts=0;
4570 for(Int_t ipkt=0; ipkt<6; ipkt++){
4571 if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4572 npkts++;
4573 build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4574 }
4575 }
4576 // if( npkts==6 )return 1; // :-)
4577
4578 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4579
4580 // -----------------------------------------------
4581 // If missing packets: check the acq configuration
4582 // (some DSPs might be excluded from acquisition)
4583 // -----------------------------------------------
4584
4585 // -----------------------------------------------
4586 // retrieve the first run header after calib
4587 // -----------------------------------------------
4588 PacketType *pctp;
4589 EventCounter *cod;
4590 cod = eh->GetCounter();
4591 Int_t irun = cod->Get(pctp->RunHeader);
4592 TTree *rh=(TTree*)file->Get("RunHeader");
4593 if ( !rh || rh->IsZombie() ) throw -17;
4594 if( rh->GetEntries() == irun ){
4595 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<<endl;
4596 return 0; // :-(
4597 }
4598
4599 RunHeaderEvent *run = 0;
4600 EventHeader *hrun = 0;
4601 rh->SetBranchAddress("RunHeader", &run);
4602 rh->SetBranchAddress("Header", &hrun);
4603 rh->GetEntry(irun);
4604 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4605
4606 if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4607 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4608 return 0; // :-(
4609 }
4610
4611 if( !run->RM_ACQ_AFTER_CALIB ){
4612 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<<endl;
4613 return 0; // :-(
4614 }
4615
4616 UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4617 if( dtime > timeaftercalib ){
4618 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4619 return 0; // :-(
4620 }
4621
4622
4623
4624 if( (run->ACQ_BUILD_INFO & mask) != build ){
4625 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4626 return 0; // :-(
4627 }
4628 return 1; // :-)
4629
4630 }
4631
4632 /**
4633 *
4634 * Check the DB (only for overlapping runs at the moment)
4635 *
4636 **/
4637 UInt_t PamelaDBOperations::Check(){
4638 //
4639 UInt_t test = 0;
4640 //
4641 UInt_t thisrht = 0;
4642 UInt_t thisrtt = 0;
4643 UInt_t thisid = 0;
4644 UInt_t prevrht = 0;
4645 UInt_t prevrtt = 0;
4646 UInt_t previd = 0;
4647 //
4648 UInt_t prevl0id = 0;
4649 UInt_t thisl0id = 0;
4650 //
4651 stringstream oss;
4652 TSQLResult *result = 0;
4653 TSQLRow *row = 0;
4654 TSQLResult *result2 = 0;
4655 TSQLRow *row2 = 0;
4656 oss.str("");
4657 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4658 // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4659 result = conn->Query(oss.str().c_str());
4660 //
4661 if ( !result ) throw -4;;
4662 //
4663 row = result->Next();
4664 //
4665 while ( row ){
4666 thisid = (UInt_t)atoll(row->GetField(0));
4667 thisl0id = (UInt_t)atoll(row->GetField(1));
4668 thisrht = (UInt_t)atoll(row->GetField(2));
4669 thisrtt = (UInt_t)atoll(row->GetField(3));
4670 //
4671 // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4672 // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4673 if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4674 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4675 printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4676 TString prevf = "";
4677 TString thisf = "";
4678 oss.str("");
4679 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4680 result2 = conn->Query(oss.str().c_str());
4681 if ( !result2 ) throw -4;;
4682 row2 = result2->Next();
4683 prevf = (TString)row2->GetField(0);
4684 oss.str("");
4685 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4686 result2 = conn->Query(oss.str().c_str());
4687 if ( !result2 ) throw -4;;
4688 row2 = result2->Next();
4689 thisf = (TString)row2->GetField(0);
4690 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4691 test = 1;
4692 };
4693 //
4694 if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4695 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4696 printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4697 TString prevf = "";
4698 TString thisf = "";
4699 oss.str("");
4700 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4701 result2 = conn->Query(oss.str().c_str());
4702 if ( !result2 ) throw -4;;
4703 row2 = result2->Next();
4704 prevf = (TString)row2->GetField(0);
4705 oss.str("");
4706 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4707 result2 = conn->Query(oss.str().c_str());
4708 if ( !result2 ) throw -4;;
4709 row2 = result2->Next();
4710 thisf = (TString)row2->GetField(0);
4711 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4712 test = 1;
4713 };
4714 //
4715 if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4716 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4717 printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4718 TString prevf = "";
4719 TString thisf = "";
4720 oss.str("");
4721 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4722 result2 = conn->Query(oss.str().c_str());
4723 if ( !result2 ) throw -4;;
4724 row2 = result2->Next();
4725 prevf = (TString)row2->GetField(0);
4726 oss.str("");
4727 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4728 result2 = conn->Query(oss.str().c_str());
4729 if ( !result2 ) throw -4;;
4730 row2 = result2->Next();
4731 thisf = (TString)row2->GetField(0);
4732 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4733 test = 1;
4734 };
4735
4736 //
4737 prevrht = thisrht;
4738 prevrtt = thisrtt;
4739 previd = thisid;
4740 prevl0id = thisl0id;
4741 row = result->Next();
4742 };
4743 //
4744 return(test);
4745 //
4746 };

  ViewVC Help
Powered by ViewVC 1.1.23