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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations) (download)
Tue Oct 24 14:24:23 2006 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.14: +39 -33 lines
Bug fixed in GL_TIMESYNC

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

  ViewVC Help
Powered by ViewVC 1.1.23