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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (show annotations) (download)
Wed Feb 14 10:55:00 2007 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.27: +227 -212 lines
Do not throw away calibration if at the beginning of file

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

  ViewVC Help
Powered by ViewVC 1.1.23