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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.32 - (show annotations) (download)
Mon Apr 30 08:19:36 2007 UTC (17 years, 10 months ago) by mocchiut
Branch: MAIN
Changes since 1.31: +3 -3 lines
Obt bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23