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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Thu Nov 9 12:02:55 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
Changes since 1.17: +3 -3 lines
File 00110 bug fixed + GL_TIMESYNC upgrade

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

  ViewVC Help
Powered by ViewVC 1.1.23