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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.23