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

Contents of /chewbacca/YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (show annotations) (download)
Tue Jan 13 13:38:15 2009 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r02, v1r01
Changes since 1.22: +204 -4 lines
Cleaning & co. bugs fixed

1 //
2 #include <iomanip>
3 #include <sstream>
4 //
5 #include <iostream>
6 #include <string>
7 #include <fstream>
8 #include <list>
9 #include <errno.h>
10 //
11 #include <TFile.h>
12 #include <TSystem.h>
13 #include <TSQLResult.h>
14 #include <TSQLRow.h>
15 #include <TTree.h>
16 #include <TGraph.h>
17 #include <TTimeStamp.h>
18 #include <TF1.h>
19 //
20 #include <EventHeader.h>
21 #include <PscuHeader.h>
22 #include <mcmd/McmdEvent.h>
23 #include <mcmd/McmdRecord.h>
24 #include <RunHeaderEvent.h>
25 #include <RunTrailerEvent.h>
26 #include <CalibCalPedEvent.h>
27 #include <CalibCalPulse1Event.h>
28 #include <CalibCalPulse2Event.h>
29 #include <CalibS4Event.h>
30 #include <CalibTrk1Event.h>
31 #include <CalibTrk2Event.h>
32 #include <varDump/VarDumpEvent.h>
33 #include <varDump/VarDumpRecord.h>
34 #include <physics/S4/S4Event.h>
35 //
36 #include <sgp4.h>
37
38 #include <PamelaDBOperations.h>
39 //
40 using namespace std;
41 using namespace pamela;
42
43 // Some function to work with cTle stuff.
44 bool compTLE(cTle* tle1, cTle *tle2);
45 float getTleJulian(cTle *);
46 string getTleDatetime(cTle*);
47
48 /**
49 * Constructor.
50 * @param host hostname for the SQL connection.
51 * @param user username for the SQL connection.
52 * @param password password for the SQL connection.
53 * @param filerawname The path and name to the raw file.
54 * @param filerootname The path and name of the raw file.
55 * @param boot file BOOT number.
56 * @param obt0 file obt0.
57 * @param tsync file timesync.
58 * @param debug debug flag.
59 * @param tlefilename ascii file with TLE 3 line elements.
60 */
61 PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela, Bool_t keepenv){
62 //
63 RELAXED = false;
64 chewbacca = false;
65 chminentry = 0;
66 chID = 0;
67 if ( filerootname.IsDigit() ){
68 if ( debug ) printf(" => using database to process the file \n");
69 chewbacca = true;
70 chID = (UInt_t)filerootname.Atoll();
71 //
72 // are these really necessary?
73 //
74 staticp = false;
75 INSERT_RAW = false;
76 dwinput = 1;
77 //
78 };
79 //
80 KEEPENV = false;
81 if ( keepenv ) KEEPENV=true;
82 STATIC=false;
83 if ( staticp ) STATIC=true;
84 //
85 SetConnection(host,user,password);
86 //
87 SetDebugFlag(debug);
88 //
89 glrun = new GL_RUN();
90 //
91 if ( !chewbacca ){
92 if ( !boot ) SetNOBOOT(false);
93 SetTsync(tsync,gpamela);
94 SetBOOTnumber(boot,gpamela);
95 SetObt0(obt0);
96 };
97 //
98 SetTLEPath(tlefilename);
99 //
100 //
101 if ( !chewbacca ) INSERT_RAW =!filerawname.IsNull();
102 if (INSERT_RAW) SetRawName(filerawname);
103 //
104 INSERT_ROOT = !filerootname.IsNull();
105 if ( INSERT_ROOT ) this->SetRootName(filerootname);
106 this->SetOrbitNo(dwinput);
107 //
108 this->SetID_RAW(0);
109 this->SetID_ROOT(0);
110
111 VALIDATE = false;
112
113 //
114 };
115
116 /**
117 * Destructor
118 */
119 void PamelaDBOperations::Close(){
120 if( conn && conn->IsConnected() ) conn->Close();
121 delete clean_time;
122 delete glrun;
123 delete this;
124 };
125
126 //
127 // SETTERS
128 //
129
130 void PamelaDBOperations::OpenL0File(TString filerootname){
131 if( INSERT_ROOT ){
132 if ( chewbacca ){
133 //
134 // retrieve from the ROOT_TABLE the chewbacca path and filename
135 //
136 stringstream qu;
137 TSQLResult *result = 0;
138 TSQLResult *result2 = 0;
139 TSQLResult *result3 = 0;
140 TSQLRow *row = 0;
141 TSQLRow *row2 = 0;
142 TSQLRow *row3 = 0;
143 TString chpath;
144 TString chfile;
145 UInt_t ridn = 0;
146 qu.str("");
147 qu << "SELECT ROOT_ID_N,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,INSERTED_BY,REAL_TIME_INIT from ROOT_TABLE_MERGING where ID_N=" << chID << ";";
148 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
149 result = conn->Query(qu.str().c_str());
150 if ( result ){
151 row = result->Next();
152 if ( row ){
153 ridn = (UInt_t)atoll(row->GetField(0));
154 chpktmin = (UInt_t)atoll(row->GetField(1));
155 chpktmax = (UInt_t)atoll(row->GetField(2));
156 chobtmin = (UInt_t)atoll(row->GetField(3));
157 chobtmax = (UInt_t)atoll(row->GetField(4));
158 chiby = (TString)(row->GetField(5));
159 chrtinit = (UInt_t)atoll(row->GetField(6));
160 } else {
161 throw -84;
162 };
163 } else {
164 throw -84;
165 };
166 delete result;
167 qu.str("");
168 qu << "SELECT FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,TIME_OFFSET,BOOT_NUMBER,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,REAL_TIME_INIT from ROOT_TABLE where ID_N=" << ridn << ";";
169 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
170 result2 = conn->Query(qu.str().c_str());
171 if ( result2 ){
172 row2 = result2->Next();
173 if ( row2 ){
174 if ( KEEPENV ){
175 chpath = (TString)(row2->GetField(0))+'/';
176 } else {
177 chpath = (TString)gSystem->ExpandPathName(row2->GetField(0))+'/';
178 };
179 chfile = (TString)(row2->GetField(1));
180 chobtts = (UInt_t)atoll(row2->GetField(2));
181 chlastts = (UInt_t)atoll(row2->GetField(3));
182 chresursts = (UInt_t)atoll(row2->GetField(4));
183 chboot = (UInt_t)atoll(row2->GetField(5));
184 //
185 if ( !chboot ){
186 qu.str("");
187 // qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<10000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<7200 group by BOOT_NUMBER order by BOOT_NUMBER asc;";
188 qu << "SELECT BOOT_NUMBER from ROOT_TABLE where BOOT_NUMBER!=0 AND ABS(LAST_TIME_SYNC_INFO-"<< chlastts <<")<40000 AND ABS(REAL_TIME_INIT-"<< row2->GetField(10) <<")<40000 group by BOOT_NUMBER order by BOOT_NUMBER asc;"; // relax conditions... 090112 [8RED: error -29]
189 if ( debug ) printf(" chewbacca: query is %s \n",qu.str().c_str());
190 result3 = conn->Query(qu.str().c_str());
191 if ( result3 ){
192 row3 = result3->Next();
193 if ( row3 && result3->GetRowCount() == 1 ){
194 chboot = (UInt_t)atoll(row3->GetField(0));
195 if ( debug ) printf(" Found boot_number = %u \n",chboot);
196 } else {
197 if ( debug ) printf(" AGH CANNOT DETERMINE THE BOOT NUMBER... \n");
198 throw -29;
199 };
200 };
201 };
202 //
203 chpktinit = (UInt_t)atoll(row2->GetField(6));
204 chpktfinal = (UInt_t)atoll(row2->GetField(7));
205 chobtinit = (UInt_t)atoll(row2->GetField(8));
206 chobtfinal = (UInt_t)atoll(row2->GetField(9));
207 //
208 } else {
209 throw -85;
210 };
211 } else {
212 throw -85;
213 };
214 filerootname = chpath + chfile;// + ".root";
215 };
216 this->SetRootName(filerootname);
217 };
218 if ( debug ) printf(" Filename is %s \n",filerootname.Data());
219 file = TFile::Open(this->GetRootName().Data());
220 // } else {
221 // this->SetRootName("");
222 // this->SetRootName(filerootname);
223 // };
224 }
225
226 //
227 // 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
228 //
229 void PamelaDBOperations::CheckValidate(Long64_t olderthan){
230 clean_time = new TDatime();
231 //
232 if(olderthan >= 0){
233 VALIDATE = true;
234 UInt_t timelim = 0;
235 timelim = (UInt_t)clean_time->Convert(true) - olderthan;
236 clean_time->Set(timelim,false);
237 };
238 };
239
240 /**
241 * Open the DB connection
242 * @param host hostname for the SQL connection.
243 * @param user username for the SQL connection.
244 * @param password password for the SQL connection.
245 */
246 void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
247 if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
248 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
249 };
250
251 /**
252 * Store the ID of the ROOT file.
253 * @param idr ID of the ROOT file
254 */
255 void PamelaDBOperations::SetID_ROOT(UInt_t idr){
256 idroot=idr;
257 };
258
259 /**
260 * Store the ID of the RAW file.
261 * @param idr ID of the RAW file
262 */
263 void PamelaDBOperations::SetID_RAW(UInt_t idr){
264 id=idr;
265 };
266
267 /**
268 * Set the debug flag
269 *
270 */
271 void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
272 debug = dbg;
273 };
274
275 /**
276 * Set the autoboot flag
277 *
278 */
279 void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
280 AUTOBOOT = dbg;
281 };
282
283 /**
284 * Set the pedantic flag
285 *
286 */
287 void PamelaDBOperations::SetPedantic(Bool_t dbg){
288 PEDANTIC = dbg;
289 };
290
291 /**
292 * Set the nofrag flag
293 *
294 */
295 void PamelaDBOperations::SetNoFrag(Bool_t nf){
296 NOFRAG = nf;
297 };
298
299 /**
300 * Store the BOOT number of the RAW file.
301 * @param boot BOOT number of the RAW file
302 */
303 void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
304 this->SetBOOTnumber(boot,false);
305 };
306
307 /**
308 * Store the BOOT number of the RAW file.
309 * @param boot BOOT number of the RAW file
310 */
311 void PamelaDBOperations::SetBOOTnumber(UInt_t boot, Bool_t gpamela){
312 BOOTNO=boot;
313 if ( gpamela ){
314 stringstream oss;
315 TSQLResult *result = 0;
316 TSQLRow *row = 0;
317 if ( !boot ){
318 //
319 BOOTNO = 1;
320 //
321 // look in the DB for the last timesync and the last run
322 //
323 oss.str("");
324 oss << "SELECT BOOT_NUMBER FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
325 result = conn->Query(oss.str().c_str());
326 if ( result ){
327 row = result->Next();
328 if ( row ){
329 BOOTNO = (UInt_t)atoll(row->GetField(0)) + 1;
330 };
331 };
332 };
333 };
334 };
335
336 /**
337 * Store the time sync of the RAW file.
338 * @param boot time sync
339 */
340 void PamelaDBOperations::SetTsync(UInt_t ts){
341 this->SetTsync(ts,false);
342 };
343
344 /**
345 * Store the time sync of the RAW file.
346 * @param boot time sync
347 */
348 void PamelaDBOperations::SetTsync(UInt_t ts, Bool_t gpamela){
349 //
350 // if not gpamela or given tsync file set ts
351 //
352 tsync=ts;
353 if ( gpamela ){
354 stringstream oss;
355 TSQLResult *result = 0;
356 TSQLRow *row = 0;
357 TSQLResult *result2 = 0;
358 TSQLRow *row2 = 0;
359 if ( !ts ){
360 //
361 tsync = 1;
362 //
363 // look in the DB for the last timesync and the last run
364 //
365 oss.str("");
366 oss << "SELECT TIMESYNC FROM GL_TIMESYNC order by TIMESYNC desc limit 1;";
367 result = conn->Query(oss.str().c_str());
368 if ( result ){
369 row = result->Next();
370 if ( row ){
371 tsync = (UInt_t)atoll(row->GetField(0)) + 1;
372 oss.str("");
373 oss << "SELECT (RUNTRAILER_TIME-RUNHEADER_TIME) FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
374 result2 = conn->Query(oss.str().c_str());
375 if ( result2 ){
376 row2 = result2->Next();
377 if ( row2 ){
378 tsync += (UInt_t)atoll(row2->GetField(0));
379 };
380 }
381 };
382 };
383 };
384 };
385 };
386
387 /**
388 * Store the time sync of the RAW file.
389 * @param boot time sync
390 */
391 void PamelaDBOperations::SetObt0(UInt_t ts){
392 obt0=ts;
393 };
394
395 /**
396 * Store the RAW filename.
397 * @param str the RAW filename.
398 */
399 void PamelaDBOperations::SetRawName(TString str){
400 filerawname=str;
401 };
402
403 /**
404 * Store the ROOT filename.
405 * @param str the ROOT filename.
406 */
407 void PamelaDBOperations::SetRootName(TString str){
408 filerootname=str;
409 };
410
411 /**
412 * Store the downlink orbit number from filename.
413 */
414 void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
415 dworbit = 0;
416 //
417 if ( dwinput ){
418 dworbit = dwinput;
419 if ( IsDebug() && !chewbacca ) printf(" Downlink orbit given by hand: %i \n",dworbit);
420 return;
421 };
422 //
423 if ( !chewbacca ){
424 TString name = this->GetRootFile();
425 Int_t nlength = name.Length();
426 if ( nlength < 5 ){
427 if ( IsDebug() ) printf(" Agh problems determining the orbit number! name = %s \n",name.Data());
428 return;
429 };
430 TString dwo = 0;
431 for (Int_t i = 0; i<5; i++){
432 dwo.Append(name[i],1);
433 };
434 if ( dwo.IsDigit() ){
435 dworbit = (UInt_t)dwo.Atoi();
436 } else {
437 dwo="";
438 for (Int_t i = 8; i<13; i++){
439 dwo.Append(name[i],1);
440 };
441 if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
442 };
443 if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
444 };
445 return;
446 };
447
448
449
450 /**
451 * Store the NOBOOT flag.
452 * @param noboot true/false.
453 */
454 void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
455 NOBOOT = noboot;
456 };
457
458 /**
459 * Store path to the TLE file.
460 */
461 void PamelaDBOperations::SetTLEPath(TString str){
462 tlefilename = str;
463 };
464
465 TString PamelaDBOperations::GetRawPath(){
466 if ( STATIC ){
467 return((TString)gSystem->DirName(filerawname.Data())+'/');
468 } else {
469 return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
470 };
471 };
472
473 TString PamelaDBOperations::GetRootPath(){
474 if ( STATIC ){
475 return((TString)gSystem->DirName(this->GetRootName().Data())+'/');
476 } else {
477 if ( KEEPENV ){
478 return((TString)gSystem->ExpandPathName(gSystem->DirName(filerootname.Data()))+'/');
479 } else {
480 return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
481 };
482 };
483 };
484
485 /**
486 * Store the olderthan variable
487 * @param olderthan
488 */
489 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
490 // olderthan = oldthan;
491 // };
492
493 /**
494 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
495 */
496 Bool_t PamelaDBOperations::SetID_RAW(){
497 stringstream oss;
498 TSQLResult *result = 0;
499 TSQLRow *row = 0;
500 oss.str("");
501 if ( STATIC ){
502 oss << "SELECT ID FROM GL_RAW WHERE "
503 << " PATH = '" << this->GetRawPath().Data() << "' AND "
504 << " NAME = '" << this->GetRawFile().Data() << "' ";
505 } else {
506 oss << "SELECT ID FROM GL_RAW WHERE "
507 << " PATH = '$PAM_RAW' AND "
508 << " NAME = '" << this->GetRawFile().Data() << "' ";
509 }
510 result = conn->Query(oss.str().c_str());
511 if ( result == NULL ) throw -4;
512 row = result->Next();
513 if ( !row ) return(false);
514 id = (UInt_t)atoll(row->GetField(0));
515 delete result;
516 return(true);
517 }
518
519 /**
520 *
521 * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
522 *
523 */
524 void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
525 glrun->SetBOOTNUMBER(BOOTNO);
526 glrun->SetRUNHEADER_TIME(absth);
527 glrun->SetRUNTRAILER_TIME(abstt);
528 glrun->SetID_ROOT_L2(0);
529 glrun->SetID_ROOT_L0(idroot);
530 glrun->SetVALIDATION(0);
531 };
532
533 /**
534 *
535 * Set the variables which belogns to physendrun tree
536 *
537 */
538 void PamelaDBOperations::SetPhysEndRunVariables(){
539 //
540 //
541 //
542 TTree *T = 0;
543 T = (TTree*)file->Get("PhysEndRun");
544 if ( !T || T->IsZombie() ) throw -90;
545 //
546 PhysEndRunEvent *pher= 0;
547 EventHeader *eh = 0;
548 T->SetBranchAddress("PhysEndRun", &pher);
549 T->SetBranchAddress("Header", &eh);
550 //
551 UInt_t phobt = 0;
552 UInt_t phpkt = 0;
553 //
554 glrun->SetPHYSENDRUN_MASK_S3S2S12(0);
555 glrun->SetPHYSENDRUN_MASK_S11CRC(0);
556 //
557 for (Int_t p=0; p<T->GetEntries(); p++){
558 //
559 T->GetEntry(p);
560 //
561 phobt = (UInt_t)eh->GetPscuHeader()->GetOrbitalTime();
562 phpkt = (UInt_t)eh->GetPscuHeader()->GetCounter();
563 //
564 if ( this->PKT(phpkt) >= this->PKT(glrun->GetRUNHEADER_PKT()) && this->PKT(phpkt) <= this->PKT(glrun->GetRUNTRAILER_PKT()) && this->OBT(phobt) >= this->OBT(glrun->GetRUNHEADER_OBT()) && this->OBT(phobt) <= this->OBT(glrun->GetRUNTRAILER_OBT()) ){
565 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() || glrun->GetPHYSENDRUN_MASK_S11CRC() ){
566 if ( IsDebug() ) printf(" WARNING while looping in physendrun: found two PhysEndRun packet for the same RUN! \n");
567 if ( IsDebug() ) printf(" Actual values: %X %X New values %X %X \n ",glrun->GetPHYSENDRUN_MASK_S3S2S12(),glrun->GetPHYSENDRUN_MASK_S11CRC(),(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12,(UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
568 if ( PEDANTIC && IsDebug() ) printf(" ERROR while looping in physendrun: found two PhysEndRun packet for the same RUN!\n ");
569 if ( PEDANTIC ) throw -91;
570 } else {
571 glrun->SetPHYSENDRUN_MASK_S3S2S12((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S3S2S12);
572 glrun->SetPHYSENDRUN_MASK_S11CRC((UInt_t)pher->TB_ENDRUN.TB_PMT_MASK_S11CRC);
573 };
574 };
575 };
576 //
577 };
578
579 /**
580 * Patch, look for upper limits to avoid processing retransmitted data
581 */
582 Int_t PamelaDBOperations::SetUpperLimits(){
583 UInt_t nevent = 0;
584 UInt_t pktlast = 0;
585 UInt_t obtlast = 0;
586 Long64_t t_pktlast = 0LL;
587 // UInt_t t_obtlast = 0;
588 Long64_t t_obtlast = 0LL;
589 Long64_t upperpkt2 = 0LL;
590 Long64_t upperobt2 = 0LL;
591 UInt_t zomp = 0;
592 UInt_t jump = 50000; // was 5000
593 EventCounter *code=0;
594 //
595 Long64_t deltapkt = 5000LL;
596 Long64_t deltaobt = 50000LL;
597 //
598 pcksList packetsNames;
599 pcksList::iterator Iter;
600 getPacketsNames(packetsNames);
601 //
602 pktfirst = 0;
603 obtfirst = 0;
604 ppktfirst = 0;
605 pobtfirst = 0;
606 //
607 //
608 //
609 TTree *T = 0;
610 T = (TTree*)file->Get("Physics");
611 if ( !T || T->IsZombie() ) throw -16;
612 EventHeader *eh = 0;
613 PscuHeader *ph = 0;
614 T->SetBranchAddress("Header", &eh);
615 nevent = T->GetEntries();
616 //
617 T->GetEntry(0);
618 ph = eh->GetPscuHeader();
619
620 if ( chewbacca ){
621 if ( IsDebug() ) printf(" FROM CHEWBACCA: upperpkt %u upperobt %u lowerpkt %u lowerobt %u \n",chpktmax,chobtmax,chpktmin,chobtmin);
622 ppktfirst = chpktmin;
623 pobtfirst = chobtts*1000;
624 } else {
625 //
626 pktfirst = ph->GetCounter();
627 obtfirst = ph->GetOrbitalTime();
628 ppktfirst = pktfirst;
629 pobtfirst = obtfirst; // to be changed AFTER time sync determination this is a profiler bug!
630 };
631 //
632 code = eh->GetCounter();
633 UInt_t en = 0;
634 // if ( !chewbacca ){
635 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
636 en = code->Get(GetPacketType(*Iter));
637 if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
638 if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
639 //
640 TTree *TC = 0;
641 TC = (TTree*)file->Get("CalibCalPed");
642 if ( !TC || TC->IsZombie() ) throw -16;
643 EventHeader *ehc = 0;
644 PscuHeader *phc = 0;
645 TC->SetBranchAddress("Header", &ehc);
646 TC->GetEntry(0);
647 phc = ehc->GetPscuHeader();
648 pktfirst = phc->GetCounter();
649 obtfirst = phc->GetOrbitalTime();
650 //
651 };
652 };
653 //
654 T->GetEntry(nevent-1);
655 ph = eh->GetPscuHeader();
656 pktlast = ph->GetCounter();
657 obtlast = ph->GetOrbitalTime();
658 //
659 // paranoid check
660 //
661 if ( chewbacca ) {
662 //
663 // query the ROOT_TABLE to find upper and lower limits of the file and compare them to the given interval
664 //
665 if ( IsDebug() ) printf(" Is good if \n");
666 if ( IsDebug() ) printf("PKT(chpktfinal) >= PKT(chpktmax) && PKT(chpktinit) <= PKT(chpktmin) && OBT(chobtfinal) >= OBT(chobtmax) && OBT(chobtinit) <= OBT(chobtmin) \n");
667 if ( IsDebug() ) printf(" %llu >= %llu && %llu <= %llu && %llu >= %llu && %llu <= %llu \n",PKT(chpktfinal),PKT(chpktmax),PKT(chpktinit),PKT(chpktmin),OBT(chobtfinal),OBT(chobtmax),OBT(chobtinit),OBT(chobtmin));
668 if ( PKT(chpktfinal) >= PKT(chpktmax) && PKT(chpktinit) <= PKT(chpktmin) && OBT(chobtfinal) >= OBT(chobtmax) && OBT(chobtinit) <= OBT(chobtmin) ){
669 if ( IsDebug() ) printf(" OK, the file contains the chewbacca interval\n");
670 } else {
671 if ( IsDebug() ) printf(" Bah, the file seems to be the wrong one, the chewbacca interval is not contained in the file... is the DB correctly filled??\n");
672 if ( PEDANTIC ) throw -89;
673 };
674 };
675 //
676 nrtbef = 0;
677 nrtaf = 0;
678 nrtbef = 0;
679 nrtaf = 0;
680 //
681 if ( chewbacca ){
682 pktfirst = chpktmin;
683 obtfirst = chobtmin;
684 pktlast = chpktmax;
685 obtlast = chobtmax;
686 upperpkt = PKT(chpktmax);
687 upperobt = OBT(chobtmax);
688 pktlast = numeric_limits<UInt_t>::max();
689 Int_t it = 0;
690 UInt_t tjump = 50000;
691 //UInt_t tjump = 100;
692 while ( tjump > 0 ){
693 // pktlast = numeric_limits<UInt_t>::max();
694 pktlast = chpktmax + 1;
695 while ( PKT(pktlast) > PKT(chpktmax) && (Int_t)(nevent-1-it) >= 0 ){
696 if ( (Int_t)(nevent-1-it) >= 0 ){
697 T->GetEntry(nevent-1-it);
698 ph = eh->GetPscuHeader();
699 pktlast = ph->GetCounter();
700 } else {
701 pktlast = chpktmax + 1;
702 };
703 if ( (!(it%1000) || abs((int)(PKT(pktlast) - PKT(chpktmax)))<1000 ) && debug ) printf(" look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
704 it += tjump;
705 };
706 if ( tjump > 1 ) it -= 2*tjump;
707 if ( it < 0 ) it = 0;
708 //
709 if ( debug ) printf(" - look for up %i %i %i nevent %u (nevent-1-it) %i \n",it,pktlast,chpktmax,nevent,(Int_t)(nevent-1-it));
710 if ( debug ) printf(" - up , tjump was %u it was %u \n",tjump,it);
711 if ( tjump == 1 ) tjump = 0;
712 if ( tjump == 10 ) tjump = 1;
713 if ( tjump == 100 ) tjump = 10;
714 if ( tjump == 1000 ) tjump = 100;
715 if ( tjump == 5000 ) tjump = 1000;
716 if ( tjump == 50000 ) tjump = 5000;
717 //
718 };
719 Int_t tupperentry = (Int_t)nevent-1-(Int_t)it+1;//+1+1;
720 if ( tupperentry < 0 ) tupperentry = 0;
721 upperentry = tupperentry;
722 it = 0;
723 pktlast = 0;
724 tjump = 50000;
725 //tjump = 100;
726 while ( tjump > 0 ){
727 // pktlast = 0;
728 pktlast = chpktmin - 1;
729 if ( debug ) printf("LLlook for down %i %i %llu %llu \n",it,pktlast,PKT(pktlast),PKT(chpktmin));
730 while ( PKT(pktlast) < PKT(chpktmin) && it < (Int_t)nevent ){
731 if ( it < (Int_t)nevent ){
732 T->GetEntry(it);
733 ph = eh->GetPscuHeader();
734 pktlast = ph->GetCounter();
735 } else {
736 // pktlast = chpktmax - 1;
737 pktlast = chpktmin - 1;
738 };
739 if ( !(it%1000) && debug ) printf("look for down %i %i %i \n",it,pktlast,chpktmin);
740 it += tjump;
741 };
742 if ( tjump > 1 ) it -= 2*tjump;
743 if ( it < 0 ) it = 0;
744 //
745 if ( debug ) printf(" down , tjump was %u it was %u \n",tjump,it);
746 if ( tjump == 1 ) tjump = 0;
747 if ( tjump == 10 ) tjump = 1;
748 if ( tjump == 100 ) tjump = 10;
749 if ( tjump == 1000 ) tjump = 100;
750 if ( tjump == 5000 ) tjump = 1000;
751 if ( tjump == 50000 ) tjump = 5000;
752 //
753 };
754 Int_t tchminentry = (Int_t)it-1;//-1+1;
755 if ( tchminentry < 0 ) tchminentry = 0;
756 chminentry = tchminentry;
757 //
758 if ( debug ) printf(" Chewbacca: chminentry %i chmaxentry %i delta %i nevent %i \n",chminentry,upperentry,upperentry-chminentry,nevent);
759 //
760 //
761 //
762 TTree *rh=(TTree*)file->Get("RunHeader");
763 if ( !rh || rh->IsZombie() ) throw -17;
764 TTree *rt=(TTree*)file->Get("RunTrailer");
765 if ( !rt || rt->IsZombie() ) throw -18;
766 //
767 rh->SetBranchAddress("RunHeader", &runh);
768 rh->SetBranchAddress("Header", &ehh);
769 //
770 rt->SetBranchAddress("RunTrailer", &runt);
771 rt->SetBranchAddress("Header", &eht);
772 //
773 rhev = rh->GetEntries();
774 rtev = rt->GetEntries();
775 //
776 if ( IsDebug() ){
777 for (Int_t rr=0; rr<rh->GetEntries(); rr++){
778 rh->GetEntry(rr);
779 phh = ehh->GetPscuHeader();
780 printf(" RUNHEADER %i OBT %u PKT %u \n",rr,phh->GetOrbitalTime(),phh->GetCounter());
781 };
782 for (Int_t rr=0; rr<rt->GetEntries(); rr++){
783 rt->GetEntry(rr);
784 pht = eht->GetPscuHeader();
785 printf(" RUNTRAILER %i OBT %u PKT %u \n",rr,pht->GetOrbitalTime(),pht->GetCounter());
786 };
787 };
788 //
789 nrhev = 0;
790 nrhbef = 0;
791 nrhaf = 0;
792 for (Int_t rr=0; rr<rh->GetEntries(); rr++){
793 rh->GetEntry(rr);
794 phh = ehh->GetPscuHeader();
795 if ( debug ) printf(" RRRRRRR %llu %llu %llu %llu %llu %llu \n",PKT(phh->GetCounter()),PKT(pktfirst),upperpkt,OBT(obtfirst),OBT(phh->GetOrbitalTime()),upperobt);
796 if ( PKT(phh->GetCounter()) >= PKT(pktfirst) && PKT(phh->GetCounter()) <= upperpkt && OBT(phh->GetOrbitalTime()) >= OBT(obtfirst) && OBT(phh->GetOrbitalTime()) <= upperobt ){
797 nrhev++;
798 if ( debug ) printf(" ++++ RH %i \n",nrhev);
799 };
800 if ( !nrhev ) nrhbef++;
801 if ( (nrhev && (nrhev+1+nrhbef+1) == rr) || nrhaf ) nrhaf++;
802 };
803 nrtev = 0;
804 nrtbef = 0;
805 nrtaf = 0;
806 for (Int_t rr=0; rr<rt->GetEntries(); rr++){
807 rt->GetEntry(rr);
808 pht = eht->GetPscuHeader();
809 if ( debug ) printf(" TTTTTTT %llu %llu %llu %llu %llu %llu \n",PKT(pht->GetCounter()),PKT(pktfirst),upperpkt,OBT(obtfirst),OBT(pht->GetOrbitalTime()),upperobt);
810 if ( PKT(pht->GetCounter()) >= PKT(pktfirst) && PKT(pht->GetCounter()) <= upperpkt && OBT(pht->GetOrbitalTime()) >= OBT(obtfirst) && OBT(pht->GetOrbitalTime()) <= upperobt ){
811 nrtev++;
812 if ( debug ) printf(" ++++ RT %i \n",nrtev);
813 };
814 if ( !nrtev ) nrtbef++;
815 if ( (nrtev && (nrtev+1+nrtbef+1) == rr) || nrtaf ) nrtaf++;
816 };
817 if ( debug ) printf(" NUMBER OF RH %i RT %i IN THE INTERVAL, NUMBER OF TOTAL RH %i RT %i \n",nrhev,nrtev,rhev,rtev);
818 if ( debug ) printf(" RH before %i RH after %i -- RT before %i RT after %i \n",nrhbef,nrhaf,nrtbef,nrtaf);
819 //
820 T->GetEntry(upperentry);
821 ph = eh->GetPscuHeader();
822 pktlast = ph->GetCounter();
823 nevent = upperentry - chminentry;
824 if ( ((Int_t)upperentry - (Int_t)chminentry) < 0 ) nevent = 0; // nevent is UInt_t! 090112 [8RED: error -88]
825 if ( IsDebug() ) printf(" Setting nevent to %u - upperentry %i chminentry %i \n",nevent,upperentry,chminentry);
826 //
827 } else {
828 upperpkt = PKT(pktlast);
829 upperobt = OBT(obtlast);
830 upperentry = nevent-1;
831 };
832 //
833 if ( chewbacca && nevent < 1 ) {
834 if ( IsDebug() ) printf(" chewbacca and no events in the file \n");
835 pktfirst = chpktmin;
836 upperpkt = PKT(chpktmax);
837 pktlast = chpktmax;
838 obtfirst = chobtmin;
839 obtlast = chobtmax;
840 upperobt = OBT(chobtmax);
841 };
842 //
843 if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u entry %i\n",obtfirst,pktfirst,chminentry);
844 //
845 if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
846 //
847 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ){
848 if ( IsDebug() ) printf(" Inconsistent PKT/OBT sequence: \n (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
849 if ( PEDANTIC ) throw -88;
850 return(32);
851 };
852 //
853 if ( !nevent ) return(64);
854 //
855 if ( nevent < 2 ) return(128);
856 if ( nevent < jump ) jump = 1;
857 // if ( nevent < jump ) jump = int(nevent/10);
858 // if ( !jump ) jump = 1;
859 //
860 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
861 //
862 if ( IsDebug() ) printf(" starting jump %i \n",jump);
863 // if ( IsDebug() ) printf(" (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) %llu < %llu && %llu > %llu \n OR \n (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) %llu > %llu && %llu < %llu \n",PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst),PKT(pktlast),PKT(pktfirst),OBT(obtlast),OBT(obtfirst));
864 if ( IsDebug() ) printf(" labs(PKT(pktlast)-PKT(pktfirst) %lu < deltapkt %lld && labs(OBT(obtlast)-OBT(obtfirst)) %lu <deltaobt %lld && nevent %u > deltapkt %lld \n",(labs(PKT(pktlast)-PKT(pktfirst))),deltapkt, labs(OBT(obtlast)-OBT(obtfirst)),deltaobt, nevent, deltapkt);
865 if ( PEDANTIC ) throw -66;
866 // go back
867 zomp = nevent - 2;
868 //
869 while ( jump > 0 ){
870 //
871 t_pktlast = PKT(pktlast);
872 t_obtlast = OBT(obtlast);
873 //
874 for (UInt_t i = zomp; i>1; i-=jump){
875 //
876 if ( i >= 0 ) T->GetEntry(i);
877 ph = eh->GetPscuHeader();
878 upperpkt = PKT(ph->GetCounter());
879 upperobt = OBT(ph->GetOrbitalTime());
880 upperentry = i;
881 //
882 if ( (i-1) >= 0 ) T->GetEntry(i-1);
883 ph = eh->GetPscuHeader();
884 upperpkt2 = PKT(ph->GetCounter());
885 upperobt2 = OBT(ph->GetOrbitalTime());
886 //
887 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
888 if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
889 if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
890 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
891 throw -13;
892 };
893 //
894 if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
895 zomp = i + jump + 1;
896 if ( zomp > nevent-2 ) zomp = nevent - 2;
897 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
898 break;
899 };
900 //
901 t_pktlast = upperpkt;
902 t_obtlast = upperobt;
903 };
904 //
905 if ( jump == 1 ) jump = 0;
906 if ( jump == 10 ) jump = 1;
907 if ( jump == 100 ) jump = 10;
908 if ( jump == 1000 ) jump = 100;
909 if ( jump == 5000 ) jump = 1000;
910 if ( jump == 50000 ) jump = 5000;
911 //
912 };
913 //
914 };
915 //
916 // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
917 //
918 if ( !chewbacca ){
919 PacketType *pctp=0;
920 TTree *rh=(TTree*)file->Get("RunHeader");
921 if ( !rh || rh->IsZombie() ) throw -17;
922 TTree *rt=(TTree*)file->Get("RunTrailer");
923 if ( !rt || rt->IsZombie() ) throw -18;
924 //
925 rh->SetBranchAddress("RunHeader", &runh);
926 rh->SetBranchAddress("Header", &ehh);
927 //
928 rt->SetBranchAddress("RunTrailer", &runt);
929 rt->SetBranchAddress("Header", &eht);
930 //
931 rhev = rh->GetEntries();
932 rtev = rt->GetEntries();
933 Long64_t sobtt = 0LL;
934 Long64_t sobth = 0LL;
935 Long64_t spktt = 0LL;
936 Long64_t spkth = 0LL;
937 Long64_t pktt = 0LL;
938 Long64_t obtt = 0LL;
939 Long64_t pkth = 0LL;
940 Long64_t obth = 0LL;
941 //
942 if ( rhev || rtev ){
943
944 T->GetEntry(upperentry);
945 code = eh->GetCounter();
946 Int_t lasttrail = code->Get(pctp->RunTrailer);
947 Int_t lasthead = code->Get(pctp->RunHeader);
948 if ( lasttrail < rtev ){
949 rt->GetEntry(lasttrail);
950 pht = eht->GetPscuHeader();
951 pktt = PKT(pht->GetCounter());
952 obtt = OBT(pht->GetOrbitalTime());
953 };
954 //
955 if ( lasthead < rhev ){
956 rh->GetEntry(lasthead);
957 phh = ehh->GetPscuHeader();
958 pkth = PKT(phh->GetCounter());
959 obth = OBT(phh->GetOrbitalTime());
960 };
961 //
962 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
963 if ( pkth > upperpkt && obth > upperobt ){
964 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
965 upperpkt = pkth;
966 upperobt = obth;
967 rhev = lasthead+1;
968 } else {
969 rhev = lasthead;
970 };
971 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
972 //
973 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
974 if ( pktt > upperpkt && obtt > upperobt ){
975 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
976 upperpkt = pktt;
977 upperobt = obtt;
978 rtev = lasttrail+1;
979 } else {
980 rtev = lasttrail;
981 };
982 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
983 // goto kikko;
984 //
985 //
986 // Check if runtrailer/runheader are within lower limits
987 //
988 //
989 pkth = 0LL;
990 obth = 0LL;
991 spkth = 0LL;
992 sobth = 0LL;
993 for (Int_t k=0; k<rhev; k++){
994 if ( k > 0 ){
995 spkth = pkth;
996 sobth = obth;
997 };
998 rh->GetEntry(k);
999 phh = ehh->GetPscuHeader();
1000 pkth = PKT(phh->GetCounter());
1001 obth = OBT(phh->GetOrbitalTime());
1002 //
1003 // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
1004 //
1005 if ( pkth < spkth && obth < sobth ){
1006 if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
1007 if ( PEDANTIC ) throw -66;
1008 //
1009 rhev = k-1;
1010 rh->GetEntry(rhev);
1011 pkth = spkth;
1012 obth = sobth;
1013 //
1014 UInt_t evbefh = 0;
1015 code = ehh->GetCounter();
1016 evbefh = code->Get(pctp->Physics);
1017 if ( evbefh >= 0 ){
1018 T->GetEntry(evbefh);
1019 ph = eh->GetPscuHeader();
1020 t_pktlast = PKT(ph->GetCounter());
1021 t_obtlast = OBT(ph->GetOrbitalTime());
1022 if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
1023 upperpkt = pkth;
1024 upperobt = obth;
1025 upperentry = evbefh-1;
1026 } else {
1027 while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
1028 evbefh++;
1029 T->GetEntry(evbefh);
1030 ph = eh->GetPscuHeader();
1031 t_pktlast = PKT(ph->GetCounter());
1032 t_obtlast = OBT(ph->GetOrbitalTime());
1033 };
1034 T->GetEntry(evbefh-1);
1035 ph = eh->GetPscuHeader();
1036 upperpkt = PKT(ph->GetCounter());
1037 upperobt = OBT(ph->GetOrbitalTime());
1038 upperentry = evbefh-1;
1039 };
1040 };
1041 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1042 goto kikko0;
1043 };
1044 };
1045 kikko0:
1046 //
1047 //
1048 //
1049 pktt = 0LL;
1050 obtt = 0LL;
1051 spktt = 0LL;
1052 sobtt = 0LL;
1053 for (Int_t k=0; k<rtev; k++){
1054 if ( k > 0 ){
1055 spktt = pktt;
1056 sobtt = obtt;
1057 };
1058 rt->GetEntry(k);
1059 pht = eht->GetPscuHeader();
1060 pktt = PKT(pht->GetCounter());
1061 obtt = OBT(pht->GetOrbitalTime());
1062 //
1063 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
1064 //
1065 if ( pktt < spktt && obtt < sobtt ){
1066 if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
1067 if ( PEDANTIC ) throw -66;
1068 //
1069 rtev = k-1;
1070 rt->GetEntry(rtev);
1071 pktt = spktt;
1072 obtt = sobtt;
1073 if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1074 //
1075 UInt_t evbeft = 0;
1076 code = eht->GetCounter();
1077 evbeft = code->Get(pctp->Physics);
1078 if ( evbeft >= 0 ){
1079 T->GetEntry(evbeft);
1080 ph = eh->GetPscuHeader();
1081 t_pktlast = PKT(ph->GetCounter());
1082 t_obtlast = OBT(ph->GetOrbitalTime());
1083 if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
1084 upperpkt = pktt;
1085 upperobt = obtt;
1086 upperentry = evbeft-1;
1087 } else {
1088 while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
1089 evbeft++;
1090 T->GetEntry(evbeft);
1091 ph = eh->GetPscuHeader();
1092 t_pktlast = PKT(ph->GetCounter());
1093 t_obtlast = OBT(ph->GetOrbitalTime());
1094 };
1095 T->GetEntry(evbeft-1);
1096 ph = eh->GetPscuHeader();
1097 upperpkt = PKT(ph->GetCounter());
1098 upperobt = OBT(ph->GetOrbitalTime());
1099 upperentry = evbeft-1;
1100 };
1101 };
1102 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1103 goto kikko;
1104 // break;
1105 //
1106 };
1107 //
1108 };
1109 //
1110 kikko:
1111 //
1112 T->GetEntry(upperentry);
1113 code = eh->GetCounter();
1114 lasttrail = code->Get(pctp->RunTrailer);
1115 lasthead = code->Get(pctp->RunHeader);
1116 if ( lasttrail < rtev ){
1117 rt->GetEntry(lasttrail);
1118 pht = eht->GetPscuHeader();
1119 pktt = PKT(pht->GetCounter());
1120 obtt = OBT(pht->GetOrbitalTime());
1121 };
1122 //
1123 if ( lasthead < rhev ){
1124 rh->GetEntry(lasthead);
1125 phh = ehh->GetPscuHeader();
1126 pkth = PKT(phh->GetCounter());
1127 obth = OBT(phh->GetOrbitalTime());
1128 };
1129 //
1130 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1131 if ( pkth > upperpkt && obth > upperobt ){
1132 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
1133 upperpkt = pkth;
1134 upperobt = obth;
1135 rhev = lasthead+1;
1136 } else {
1137 rhev = lasthead;
1138 };
1139 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
1140 //
1141 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1142 if ( pktt > upperpkt && obtt > upperobt ){
1143 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
1144 upperpkt = pktt;
1145 upperobt = obtt;
1146 rtev = lasttrail+1;
1147 } else {
1148 rtev = lasttrail;
1149 };
1150 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
1151 //
1152 };
1153 };
1154 //
1155 if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
1156 //
1157 return(0);
1158 }
1159
1160 /**
1161 *
1162 * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
1163 * Entries in the _RUNID_GEN table are never deleted.
1164 *
1165 **/
1166 UInt_t PamelaDBOperations::AssignRunID(){
1167 //
1168 TSQLResult *result = 0;
1169 TSQLRow *row = 0;
1170 UInt_t runid = 0;
1171 //
1172 stringstream oss;
1173 //
1174 oss.str("");
1175 if ( chewbacca ){// if chewbacca and tag=none then use chewbacca tag (chiby = chewbacca inserted by), if chewbacca and tag!=none then use tag, if not chewbacca use tag.
1176 if ( !strcmp(tag.Data(),"NONE") ){
1177 oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< chiby.Data() <<"');";
1178 } else {
1179 oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1180 };
1181 } else {
1182 oss << "INSERT INTO _RUNID_GEN VALUES (NULL,'"<< tag.Data() <<"');";
1183 };
1184 result = conn->Query(oss.str().c_str());
1185 if ( !result ) throw -10;
1186 oss.str("");
1187 oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
1188 result = conn->Query(oss.str().c_str());
1189 if ( !result ) throw -10;
1190 //
1191 row = result->Next();
1192 //
1193 if ( !row ) throw -28;
1194 //
1195 runid = (UInt_t)atoll(row->GetField(0));
1196 //
1197 return(runid);
1198 };
1199
1200 //
1201 // GETTERS
1202 //
1203
1204 /**
1205 *
1206 * Returns the DB absolute time needed to associate calibrations to data
1207 *
1208 */
1209 UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
1210 //
1211 return(((UInt_t)(OBT(obt)/1000)+toffset));
1212 //
1213 };
1214
1215 /**
1216 *
1217 * List of packet types (just to make easily the loops)
1218 *
1219 */
1220 const PacketType* PamelaDBOperations::GetPacketType(const char* type){
1221 if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
1222 if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
1223 if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
1224 if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
1225 if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
1226 if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
1227 if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
1228 if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
1229 if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
1230 if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
1231 if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
1232 if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
1233 if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
1234 if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
1235 if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
1236 if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
1237 if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
1238 if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
1239 if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
1240 if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
1241 if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
1242 if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
1243 if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
1244 if ( !strcmp(type,"Log") ) return(PacketType::Log);
1245 if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
1246 if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
1247 if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
1248 if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
1249 if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
1250 if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
1251 if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
1252 if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
1253 if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
1254 if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
1255 if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
1256 if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
1257 if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
1258 if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
1259 if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
1260 if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
1261 if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
1262 if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
1263 if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
1264 if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
1265 if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
1266 if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
1267 if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
1268 return(PacketType::Invalid);
1269 };
1270
1271 //
1272 // PRIVATE FUNCTIONS
1273 //
1274
1275 // /**
1276 // * Open the ROOT filename for reading
1277 // */
1278 // void PamelaDBOperations::OpenFile(){
1279 // file = TFile::Open(this->GetRootName().Data());
1280 // //
1281
1282 void PamelaDBOperations::CheckFile(){
1283 if ( !file ) throw -12;
1284 };
1285
1286
1287 /**
1288 * Check if LEVEL0 file and DB connection have really be opened
1289 */
1290 void PamelaDBOperations::CheckConnection(){
1291 //
1292 // check connection
1293 //
1294 if( !conn ) throw -1;
1295 bool connect = conn->IsConnected();
1296 if( !connect ) throw -1;
1297 //
1298 if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
1299 //
1300 if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
1301 //
1302 // set DB timezone to UTC
1303 //
1304 stringstream oss;
1305 //
1306 oss.str("");
1307 oss << "SET time_zone='+0:00';";
1308 TSQLResult *result = 0;
1309 result = conn->Query(oss.str().c_str());
1310 if ( !result ) throw -10;
1311 oss.str("");
1312 oss << "SET wait_timeout=173000;";
1313 conn->Query(oss.str().c_str());
1314 //
1315 }
1316
1317 /**
1318 * Lock tables
1319 */
1320 void PamelaDBOperations::LockTables(){
1321 //
1322 // check connection
1323 //
1324 if( !conn ) throw -1;
1325 bool connect = conn->IsConnected();
1326 if( !connect ) throw -1;
1327 //
1328 stringstream oss;
1329 //
1330 oss.str("");
1331 oss << "lock table GL_RUN write, GL_ROOT write, GL_RAW write, GL_TIMESYNC write, GL_RESURS_OFFSET write, GL_PARAM write, GL_TLE write, GL_RUN_FRAGMENTS write, GL_RUN_TRASH write, GL_CALO_CALIB write, GL_CALOPULSE_CALIB write, GL_TRK_CALIB write, GL_S4_CALIB write, ROOT_TABLE_MERGING write, ROOT_TABLE_BAD write, ROOT_TABLE write, _RUNID_GEN write;";
1332 TSQLResult *result = 0;
1333 result = conn->Query(oss.str().c_str());
1334 if ( !result ) throw -10;
1335 //
1336 }
1337
1338 /**
1339 * Lock tables
1340 */
1341 void PamelaDBOperations::UnLockTables(){
1342 //
1343 // check connection
1344 //
1345 if( !conn ) throw -1;
1346 bool connect = conn->IsConnected();
1347 if( !connect ) throw -1;
1348 //
1349 stringstream oss;
1350 //
1351 oss.str("");
1352 oss << "unlock tables;";
1353 TSQLResult *result = 0;
1354 result = conn->Query(oss.str().c_str());
1355 if ( !result ) throw -10;
1356 //
1357 }
1358
1359 /**
1360 * Return the correct packet number if we went back to zero
1361 */
1362 Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
1363 //
1364 // if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2));
1365 //
1366 if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){
1367 // if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
1368 return((Long64_t)pkt_num+16777215LL);
1369 };
1370 //
1371 if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
1372 // if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
1373 return((Long64_t)pkt_num-16777215LL);
1374 };
1375 //
1376 // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
1377 return((Long64_t)pkt_num);
1378 //
1379 };
1380
1381 /**
1382 * Return the correct On Board Time if we went back to zero
1383 */
1384 Long64_t PamelaDBOperations::OBT(UInt_t obt){
1385 //
1386 // if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u (numeric_limits<UInt_t>::max()/2) is %u \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
1387 //
1388 if ( obt < ((Long64_t)pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
1389 // if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1390 return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
1391 };
1392 //
1393 if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1394 // if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
1395 // if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1396 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
1397 };
1398 //
1399 // if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
1400 return((Long64_t)obt);
1401 };
1402
1403 /**
1404 *
1405 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
1406 *
1407 */
1408 void PamelaDBOperations::FillClass(){
1409 this->FillClass(false,false,-1,-1);
1410 };
1411
1412 /**
1413 *
1414 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
1415 *
1416 */
1417 void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, Int_t firstev, Int_t lastev){
1418 //
1419 TTree *T = 0;
1420 T = (TTree*)file->Get("Physics");
1421 if ( !T || T->IsZombie() ) throw -16;
1422 //
1423 EventHeader *eh = 0;
1424 PscuHeader *ph = 0;
1425 T->SetBranchAddress("Header", &eh);
1426 PacketType *pctp=0;
1427 EventCounter *codt=0;
1428 EventCounter *codh=0;
1429 UInt_t firstObt = 0;
1430 UInt_t lastObt = 0;
1431 UInt_t firstPkt = 0;
1432 UInt_t lastPkt = 0;
1433 UInt_t rhtime = 0;
1434 UInt_t rttime = 0;
1435 //
1436 if ( IsDebug() ) printf(" A firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1437 //
1438 if ( !mishead ){
1439 codh = ehh->GetCounter();
1440 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1441 if ( IsDebug() ) printf(" B firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1442 firstev = 1;
1443 lastev = 0;
1444 } else {
1445 firstev = codh->Get(pctp->Physics);
1446 };
1447 rhtime = this->GetAbsTime(phh->GetOrbitalTime());
1448 glrun->Set_GL_RUNH(runh,phh);
1449 firstObt = glrun->GetRUNHEADER_OBT();
1450 firstPkt = glrun->GetRUNHEADER_PKT();
1451 };
1452 if ( !mistrail ){
1453 codt = eht->GetCounter();
1454 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1)){
1455 if ( IsDebug() ) printf(" C firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1456 lastev = 0;
1457 firstev = lastev+1;
1458 } else {
1459 lastev = codt->Get(pctp->Physics)-1;
1460 };
1461 rttime = this->GetAbsTime(pht->GetOrbitalTime());
1462 glrun->Set_GL_RUNT(runt,pht);
1463 lastObt = glrun->GetRUNTRAILER_OBT();
1464 lastPkt = glrun->GetRUNTRAILER_PKT();
1465 };
1466 //
1467 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
1468 //
1469 if ( mishead ) {
1470 glrun->Set_GL_RUNH0();
1471 //
1472 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1473 firstObt = lastObt;
1474 firstPkt = lastPkt;
1475 rhtime = rttime;
1476 } else {
1477 T->GetEntry(firstev);
1478 ph = eh->GetPscuHeader();
1479 firstObt = ph->GetOrbitalTime();
1480 rhtime = this->GetAbsTime(firstObt);
1481 firstPkt = ph->GetCounter();
1482 };
1483 //
1484 glrun->SetRUNHEADER_PKT(firstPkt);
1485 glrun->SetRUNHEADER_OBT(firstObt);
1486 //
1487 };
1488 if ( mistrail ){
1489 glrun->Set_GL_RUNT0();
1490 //
1491 if ( (lastev+1 == firstev && lastev != -1) || (lastev == firstev && lastev != -1) ){
1492 lastObt = firstObt;
1493 lastPkt = firstPkt;
1494 rttime = rhtime;
1495 } else {
1496 T->GetEntry(lastev);
1497 ph = eh->GetPscuHeader();
1498 lastObt = ph->GetOrbitalTime();
1499 rttime = this->GetAbsTime(lastObt);
1500 lastPkt = ph->GetCounter();
1501 };
1502 //
1503 glrun->SetRUNTRAILER_OBT(lastObt);
1504 glrun->SetRUNTRAILER_PKT(lastPkt);
1505 //
1506 };
1507 //
1508 // ad hoc solution for INF EV_TO [8RED] 090113
1509 //
1510 if ( !firstev && lastev == -1 ){
1511 if ( IsDebug() ) printf(" EE firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1512 firstev = 1;
1513 lastev = 0;
1514 };
1515 glrun->SetEV_FROM((UInt_t)firstev);
1516 glrun->SetEV_TO((UInt_t)lastev);
1517 glrun->SetNEVENTS((UInt_t)lastev-(UInt_t)firstev+1);
1518 //
1519 if ( IsDebug() ) printf(" firstev %i lastev %i nevents %i \n",firstev,lastev,lastev-firstev+1);
1520 //
1521 this->SetCommonGLRUN(rhtime,rttime);
1522 this->SetPhysEndRunVariables();
1523 //
1524 };
1525
1526 //
1527 // PUBLIC FUNCTIONS
1528 //
1529
1530 /**
1531 * Insert a new row into GL_RAW table.
1532 */
1533 Int_t PamelaDBOperations::insertPamelaRawFile(){
1534
1535 //
1536 Bool_t idr = this->SetID_RAW();
1537 if ( idr ) return(1);
1538
1539 GL_RAW glraw = GL_RAW();
1540
1541 glraw.PATH = GetRawPath();
1542 glraw.NAME = GetRawFile();
1543 // glraw.BOOT_NUMBER = 0;//???
1544 glraw.BOOT_NUMBER = this->GetBOOTnumber();
1545
1546 if( insertPamelaRawFile(&glraw) )return(1);
1547 //
1548 idr = this->SetID_RAW();
1549 if ( !idr ) throw -11;
1550
1551 return(0);
1552 }
1553 /**
1554 * Insert a new row into GL_RAW table.
1555 */
1556 Int_t PamelaDBOperations::insertPamelaRawFile(GL_RAW *glraw){
1557 //
1558 if(!glraw)return(1);//?? ok I think
1559 //
1560 stringstream oss;
1561 //
1562 oss.str("");
1563 if ( STATIC ){
1564 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1565 << glraw->PATH << "', '" << glraw->NAME << "')";
1566 } else {
1567 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << glraw->NAME << "')";
1568 };
1569 if ( debug ) cout <<oss.str().c_str() <<endl;
1570 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1571 //
1572 oss.str("");
1573 oss << "SELECT ID FROM GL_RAW WHERE NAME=\""<<glraw->NAME<<"\";";
1574 if ( debug ) cout << oss.str().c_str()<<endl;
1575 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1576 //
1577 TSQLResult *result = 0;
1578 TSQLRow *row = 0;
1579 result = conn->Query(oss.str().c_str());
1580 if ( result == NULL ) throw -4;
1581 row = result->Next();
1582 if ( !row ) return(1);
1583 glraw->ID = (UInt_t)atoll(row->GetField(0));
1584 if ( debug ) printf(" The ID of the RAW file is %u \n",glraw->ID);
1585 delete result;
1586 delete row;
1587 //
1588 return(0);
1589 }
1590
1591
1592 /**
1593 * Look for one timesync information in the file and
1594 * 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
1595 */
1596 Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1597 //
1598 Int_t signal = 0;
1599 UInt_t idresof = 0;
1600 stringstream oss;
1601 TSQLResult *result = 0;
1602 TSQLRow *row = 0;
1603 UInt_t OBT = 0;
1604 UInt_t TYPE = 0;
1605 UInt_t TSYNC = 0;
1606 UInt_t t0 = 0;
1607 Bool_t existsts = false;
1608 //
1609 if ( chewbacca ){
1610 //
1611 OBT = chobtts * 1000;
1612 TSYNC = chlastts;
1613 t0 = chresursts;
1614 TYPE = 777;
1615 oss.str("");
1616 oss << "select * from GL_RESURS_OFFSET where OFFSET_DATE=FROM_UNIXTIME("<< t0 <<") limit 1;";
1617 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1618 result = conn->Query(oss.str().c_str());
1619 if ( !result ) throw -10;
1620 row = result->Next();
1621 if ( !row ) throw -92;
1622 idresof = (UInt_t)atoll(row->GetField(0));
1623 existsts = true;
1624 goto eout;
1625 //
1626 } else {
1627 //
1628 //signal = this->SetUpperLimits();
1629 //
1630 if ( this->GetID_RAW() == 0 ) throw -11;
1631 //
1632 oss.str("");
1633 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1634 << this->GetRawFile().Data() << "';";
1635 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1636 result = conn->Query(oss.str().c_str());
1637 if ( !result ) throw -10;
1638 row = result->Next();
1639 //
1640 if ( !row ){
1641 oss.str("");
1642 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1643 << dworbit << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
1644 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1645 result = conn->Query(oss.str().c_str());
1646 if ( !result ) throw -10;
1647 row = result->Next();
1648 if ( !row ) throw -10;
1649 };
1650 //
1651 idresof = (UInt_t)atoll(row->GetField(6));
1652 //
1653 TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1654 t0 = (UInt_t)tu.GetSec();
1655 if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5),row->GetField(6));
1656 //
1657 /*
1658 * Verify that the TIMESYNC have been not already processed
1659 */
1660 oss.str("");
1661 oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1662 << " LEFT JOIN GL_RAW "
1663 << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1664 << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1665 << " GROUP BY GL_TIMESYNC.OBT0;";
1666 if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1667 result = conn->Query(oss.str().c_str());
1668 if (result == NULL) throw -10;
1669 row = result->Next();
1670 if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1671 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1672 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1673 //
1674 tsync = (UInt_t)atoll(row->GetField(2));
1675 obt0 = (UInt_t)atoll(row->GetField(1));
1676 //
1677 if ( PEDANTIC ) throw -67;
1678 return(1);
1679 };
1680 //
1681 TTree *T = 0;
1682 //
1683 UInt_t nevent = 0;
1684 UInt_t recEntries = 0;
1685 //
1686 OBT = 0;
1687 TYPE = 0;
1688 TSYNC = 0;
1689 //
1690 Double_t minimum = 0.;
1691 Double_t maximum = 0.;
1692 Double_t minimum2 = 0.;
1693 Double_t maximum2 = 0.;
1694 //
1695 //
1696 pamela::McmdEvent *mc = 0;
1697 pamela::McmdRecord *mcrc = 0;
1698 TArrayC *mcmddata = 0;
1699 //
1700 minimum = numeric_limits<Double_t>::max();
1701 maximum = numeric_limits<Double_t>::min();
1702 minimum2 = numeric_limits<Double_t>::max();
1703 maximum2 = numeric_limits<Double_t>::min();
1704 //
1705 T = (TTree*)file->Get("Mcmd");
1706 if ( !T || T->IsZombie() ) throw -19;
1707 T->SetBranchAddress("Mcmd",&mc);
1708 //
1709 nevent = T->GetEntries();
1710 //
1711 // loop over events
1712 //
1713 existsts = false;
1714 //
1715 for (UInt_t i=0; i<nevent;i++){
1716 //
1717 T->GetEntry(i);
1718 //
1719 recEntries = mc->Records->GetEntries();
1720 //
1721 for (UInt_t j = 0; j < recEntries; j++){
1722 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1723 mcmddata = mcrc->McmdData;
1724 //
1725 if (mcrc->ID1 == 0xE0){ // mcmd timesync
1726 //
1727 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1728 //
1729 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);
1730 //
1731 TYPE = 55;//224;
1732 //
1733 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1734 //
1735 if ( TSYNC && OBT ){
1736 existsts = true;
1737 goto eout;
1738 };
1739 //
1740 };
1741 };
1742 };
1743 if ( !existsts ) { // try with runheader and runtrailer
1744 //
1745 if ( IsDebug() ) printf(" No ts mcmd \n");
1746 signal = 2;
1747 //
1748 TTree *rh=(TTree*)file->Get("RunHeader");
1749 if ( !rh || rh->IsZombie() ) throw -17;
1750 TTree *rt=(TTree*)file->Get("RunTrailer");
1751 if ( !rt || rt->IsZombie() ) throw -18;
1752 //
1753 rh->SetBranchAddress("RunHeader", &runh);
1754 //
1755 rt->SetBranchAddress("RunTrailer", &runt);
1756 //
1757 Int_t nnrhev = rh->GetEntries();
1758 Int_t nnrtev = rt->GetEntries();
1759 if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nnrhev,nnrtev);
1760 //
1761 if ( nnrhev > 0 ){
1762 for (Int_t i=0; i<nnrhev; i++){
1763 //
1764 rh->GetEntry(i);
1765 //
1766 TSYNC = runh->LAST_TIME_SYNC_INFO;
1767 OBT = runh->OBT_TIME_SYNC * 1000;
1768 //
1769 TYPE = 20;
1770 //
1771 if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1772 //
1773 if ( TSYNC && OBT ){
1774 existsts = true;
1775 goto eout;
1776 };
1777 };
1778 //
1779 };
1780 if ( nnrtev > 0 ){
1781 //
1782 if ( IsDebug() ) printf(" No runheader \n");
1783 signal = 6;
1784 //
1785 for (Int_t i=0; i<nnrtev; i++){
1786 //
1787 rt->GetEntry(i);
1788 //
1789 TSYNC = runt->LAST_TYME_SYNC_INFO;
1790 OBT = runt->OBT_TYME_SYNC * 1000;
1791 //
1792 TYPE = 21;
1793 //
1794 if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1795 //
1796 if ( TSYNC && OBT ){
1797 existsts = true;
1798 goto eout;
1799 };
1800 };
1801 //
1802 } else {
1803 if ( IsDebug() ) printf(" No runheader \n");
1804 };
1805 };
1806 //
1807 if ( !existsts ){ // try with inclination mcmd
1808 //
1809 if ( IsDebug() ) printf(" No runtrailer \n");
1810 signal = 14;
1811 //
1812 Double_t timesync = 0.;
1813 for (UInt_t i=0; i<nevent;i++){
1814 //
1815 T->GetEntry(i);
1816 //
1817 recEntries = mc->Records->GetEntries();
1818 // //
1819 for (UInt_t j = 0; j < recEntries; j++){
1820 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1821 mcmddata = mcrc->McmdData;
1822 //
1823 if (mcrc->ID1 == 0xE2){ // mcmd inclination
1824 timesync = 0.;
1825 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);
1826 //
1827 if ( timesync > maximum2){
1828 maximum2 = timesync;
1829 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1830 };
1831 };
1832 //
1833 };
1834 };
1835 if ( maximum2 > numeric_limits<Double_t>::min() ){
1836 TSYNC = (UInt_t)(maximum2 + 0.5);
1837 TYPE = 666;
1838 if ( TSYNC && OBT ){
1839 existsts = true;
1840 goto eout;
1841 };
1842 };
1843 };
1844 //
1845 };
1846 //
1847 if ( !existsts && obt0 ){ // insert timesync by hand
1848 //
1849 if ( PEDANTIC ) throw -68;
1850 if ( IsDebug() ) printf(" No incl mcmd \n");
1851 signal = 30;
1852 //
1853 OBT = obt0;
1854 TSYNC = tsync;
1855 TYPE = 999;
1856 existsts = true;
1857 goto eout;
1858 };
1859 //
1860 eout:
1861 //
1862 if ( !existsts ) throw -3;
1863 //
1864 oss.str("");
1865 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC,ID_RESURS_OFFSET) VALUES ('"
1866 << this->GetID_RAW() << "','"//224'"
1867 << dec << (UInt_t)TYPE << "','"
1868 << dec << (UInt_t)OBT << "','"
1869 << dec << (UInt_t)TSYNC << "','"
1870 << dec << (UInt_t)idresof << "');";
1871 conn->Query(oss.str().c_str());
1872 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1873 if ( conn->GetErrorCode() ){
1874 printf(" OK, you got an error because the database structure you are using is not up to date\n Using backward compability code, hence you can continue safetly \n");
1875 oss.str("");
1876 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1877 << this->GetID_RAW() << "','"//224'"
1878 << dec << (UInt_t)TYPE << "','"
1879 << dec << (UInt_t)OBT << "','"
1880 << dec << (UInt_t)TSYNC << "');";
1881 conn->Query(oss.str().c_str());
1882 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1883 };
1884 //
1885 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1886 //
1887 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1888 //
1889 tsync = TSYNC;
1890 obt0 = OBT;
1891 //
1892 delete result;
1893 return(signal);
1894 }
1895
1896 /**
1897 * Insert all the new rows into GL_ROOT.
1898 * The raw file indicates in the parameters should be already been stored in the database.
1899 */
1900 Int_t PamelaDBOperations::insertPamelaRootFile(){
1901
1902 stringstream oss;
1903 TSQLResult *result = 0;
1904 TSQLRow *row = 0;
1905 //
1906 // ----------------------
1907 // determine the timesync
1908 // ----------------------
1909 UInt_t idtimesync = 0;
1910 //
1911 if ( chewbacca ){
1912 oss.str("");
1913 oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
1914 if ( debug ) printf(" %s \n",oss.str().c_str());
1915 result = conn->Query(oss.str().c_str());
1916 //
1917 if ( !result ) throw -3;
1918 //
1919 row = result->Next();
1920 //
1921 if ( !row ) throw -3;
1922 idtimesync = (UInt_t)atoll(row->GetField(0));
1923 } else {
1924 oss.str("");
1925 if ( STATIC ){
1926 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1927 << " LEFT JOIN GL_ROOT "
1928 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1929 << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1930 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1931 } else {
1932 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1933 << " LEFT JOIN GL_ROOT "
1934 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1935 << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1936 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1937 };
1938 result = conn->Query(oss.str().c_str());
1939 //
1940 if ( !result ) throw -12;
1941 //
1942 row = result->Next();
1943 //
1944 if ( !row ) throw -10;
1945 if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1946 idroot = (UInt_t)atoll(row->GetField(2));
1947 delete row;
1948 delete result;
1949 return(1);
1950 };
1951 //
1952 // determine which timesync has to be used
1953 //
1954 oss.str("");
1955 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;";
1956 result = conn->Query(oss.str().c_str());
1957 //
1958 if ( !result ) throw -3;
1959 //
1960 row = result->Next();
1961 //
1962 if ( !row ) throw -3;
1963 idtimesync = (UInt_t)atoll(row->GetField(0));
1964 };
1965
1966 delete row;
1967 delete result;
1968
1969 // ----------------------
1970 // insert root file
1971 // ----------------------
1972
1973 GL_ROOT glroot = GL_ROOT();
1974
1975 glroot.ID_RAW = GetID_RAW();
1976 glroot.ID_TIMESYNC = idtimesync;
1977 if ( STATIC ){
1978 glroot.PATH = GetRootPath();
1979 } else {
1980 if ( KEEPENV ){
1981 glroot.PATH = gSystem->DirName(filerootname.Data());
1982 } else {
1983 glroot.PATH = "$PAM_L0";
1984 };
1985 };
1986 glroot.NAME = GetRootFile();
1987
1988 if ( insertPamelaRootFile(&glroot) )return 1;
1989
1990 SetID_ROOT(glroot.ID);
1991
1992
1993 return (0);
1994 }
1995 /**
1996 * Insert all the new rows into GL_ROOT.
1997 * The raw file indicates in the parameters should be already been stored in the database.
1998 */
1999 Int_t PamelaDBOperations::insertPamelaRootFile(GL_ROOT *glroot){
2000 stringstream oss;
2001 TSQLResult *result = 0;
2002 TSQLRow *row = 0;
2003 //
2004 //
2005 oss.str("");
2006 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
2007 << glroot->ID_RAW << "', '" << glroot->ID_TIMESYNC << "', '" << glroot->PATH << "', '" << glroot->NAME << "')";
2008 //
2009 if ( debug ) printf("%s \n",oss.str().c_str());
2010 if (conn->Query(oss.str().c_str()) == 0) throw -4;
2011 //
2012 delete result;
2013 //
2014 oss.str("");
2015 // oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
2016 oss << "SELECT ID FROM GL_ROOT WHERE PATH='" << glroot->PATH << "' and NAME='"<< glroot->NAME <<"';";
2017 //
2018 if ( debug ) printf("%s \n",oss.str().c_str());
2019 result = conn->Query(oss.str().c_str());
2020 if ( !result ) throw -12;
2021 row = result->Next();
2022 if ( !row ) throw -3;
2023
2024 glroot->ID = (UInt_t)atoll(row->GetField(0));
2025
2026 if ( debug ) printf(" The ID of the ROOT file is %u \n",glroot->ID);
2027 //
2028 delete result;
2029 //
2030 return(0);
2031 }
2032
2033 /**
2034 * Assign the BOOT_NUMBER to the raw file.
2035 */
2036 Int_t PamelaDBOperations::assignBOOT_NUMBER(){
2037 Bool_t found = false;
2038 UInt_t idRaw = 0;
2039 UInt_t bn = 0;
2040 stringstream oss;
2041 TSQLResult *result = 0;
2042 TSQLRow *row = 0;
2043 if ( chewbacca ){
2044 if ( chboot == 1 ){
2045 // not found!
2046 found = false;
2047 } else {
2048 found = true;
2049 this->SetBOOTnumber(chboot);
2050 };
2051 } else {
2052 oss.str("");
2053 if ( STATIC ){
2054 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
2055 << " PATH = '" << this->GetRawPath().Data() << "' AND "
2056 << " NAME = '" << this->GetRawFile().Data() << "' ";
2057 } else {
2058 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
2059 << " PATH = '$PAM_RAW' AND "
2060 << " NAME = '" << this->GetRawFile().Data() << "' ";
2061 };
2062 result = conn->Query(oss.str().c_str());
2063 //
2064 if ( !result ) throw -4;;
2065 row = result->Next();
2066 if ( !row ) return(16);
2067 if ( row->GetField(1) ){
2068 this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
2069 return(1);
2070 };
2071 if ( !row->GetField(0) ) throw -26;
2072 //
2073 idRaw = (UInt_t)atoll(row->GetField(0));
2074 //
2075 //
2076 //
2077 TTree *trDumpEv = 0;
2078 trDumpEv = (TTree*)file->Get("VarDump");
2079 if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
2080 //
2081 VarDumpEvent *vde = 0;
2082 VarDumpRecord *vdr = 0;
2083 //
2084 trDumpEv->SetBranchAddress("VarDump", &vde);
2085 if ( trDumpEv->GetEntries() > 0 ){
2086 found = false;
2087 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
2088 trDumpEv->GetEntry(i);
2089 // vde->Records->GetEntries();
2090 if ( vde->Records->GetEntries()>5 ){
2091 found = true;
2092 goto fill;
2093 };
2094 };
2095 fill:
2096 if ( found ){
2097 //
2098 vdr = (VarDumpRecord*)vde->Records->At(6);
2099 //
2100 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
2101 //
2102 } else {
2103 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
2104 };
2105 } else {
2106 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
2107 };
2108 //
2109 };
2110 //
2111 Bool_t afound = false;
2112 if ( !found && this->AutoBoot()){
2113 afound = true;
2114 //
2115 // Search for other files with similar timesync
2116 //
2117 if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
2118 UInt_t upperts = tsync-(obt0/1000)+5;
2119 UInt_t lowerts = tsync-(obt0/1000)-5;
2120 if ( chewbacca ){
2121 oss.str("");
2122 oss << "select BOOT_NUMBER from ROOT_TABLE where LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)<"
2123 << upperts
2124 << " AND LAST_TIME_SYNC_INFO-(OBT_TIME_SYNC)>"
2125 << lowerts
2126 << " AND BOOT_NUMBER>1;";
2127 } else {
2128 oss.str("");
2129 oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
2130 << upperts
2131 << " AND TIMESYNC-(OBT0/1000)>"
2132 << lowerts
2133 << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
2134 };
2135 result = conn->Query(oss.str().c_str());
2136 if ( IsDebug() && !chewbacca ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
2137 if ( IsDebug() && chewbacca ) printf(" Query the ROOT_TABLE table to find boot number:\n %s \n",oss.str().c_str());
2138 //
2139 if ( !result ) throw -4;;
2140 found = true;
2141 if ( result->GetRowCount()<3 ){
2142 if ( IsDebug() ) printf(" AGH! no results!\n");
2143 found = false;
2144 } else {
2145 row = result->Next();
2146 bn = (UInt_t)atoll(row->GetField(0));
2147 for ( Int_t r=1; r<result->GetRowCount() ;r++){
2148 if ( !row ) throw -4;
2149 if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
2150 if ( bn != (UInt_t)atoll(row->GetField(0)) ){
2151 if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
2152 found = false;
2153 };
2154 row = result->Next();
2155 };
2156 };
2157 };
2158 //
2159 Int_t sgn = 0;
2160 //
2161 if ( !found && !BOOTNO ){
2162 throw -29;
2163 } else {
2164 if ( afound ){
2165 this->SetBOOTnumber(bn);
2166 sgn = 8;
2167 };
2168 };
2169 //
2170 if ( !chewbacca ){
2171 oss.str("");
2172 oss << " UPDATE GL_RAW "
2173 << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
2174 << " WHERE GL_RAW.ID = '" << idRaw << "'";
2175 conn->Query(oss.str().c_str());
2176 };
2177 //
2178 delete result;
2179 return(sgn);
2180 };
2181
2182 /**
2183 * Scan runtrailer packet, fill the GL_RUN table and
2184 * check for missing and truncated runs
2185 */
2186 Int_t PamelaDBOperations::insertPamelaRUN(){
2187 Int_t signal = 0;
2188 //
2189 stringstream oss;
2190 oss.str("");
2191 //
2192 // signal = this->SetUpperLimits();
2193
2194 //
2195 // loop on runheader and runtrailer events
2196 //
2197 TTree *rh=(TTree*)file->Get("RunHeader");
2198 if ( !rh || rh->IsZombie() ) throw -17;
2199 TTree *rt=(TTree*)file->Get("RunTrailer");
2200 if ( !rt || rt->IsZombie() ) throw -18;
2201 //
2202 PacketType *pctp=0;
2203 EventCounter *cod=0;
2204 //
2205 rh->SetBranchAddress("RunHeader", &runh);
2206 rh->SetBranchAddress("Header", &ehh);
2207 //
2208 rt->SetBranchAddress("RunTrailer", &runt);
2209 rt->SetBranchAddress("Header", &eht);
2210 //
2211 TTree *T = (TTree*)file->Get("Physics");
2212 if ( !T || T->IsZombie() ) throw -16;
2213 EventHeader *eh = 0;
2214 T->SetBranchAddress("Header", &eh);
2215 //
2216 if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
2217 //
2218 UInt_t obtt = 0;
2219 UInt_t obth = 0;
2220 UInt_t pktt = 0;
2221 UInt_t pkth = 0;
2222 Int_t pth = -1;
2223 Int_t ptht = -1;
2224 Int_t evbeft = 0;
2225 Int_t evbefh = 0;
2226 UInt_t tcod;
2227 //
2228 // no runtrailers in the file!
2229 //
2230 if ( !rtev ){
2231 if ( !rhev ){
2232 if ( IsDebug() ) printf(" No runheaders nor runtrailers!! \n");
2233 if ( !(upperentry-chminentry) ){
2234 if ( IsDebug() ) printf(" No physics events nor runs in the file \n"); // di nuovo potrebbe esserci un runtrailer senza eventi (riempimento MM)
2235 // throw -8;
2236 return 0; // one could check if there is any calibration no need to exit with error
2237 } else {
2238 this->HandleRunFragments(true,true,chminentry,upperentry); // no runtrailers ma potrebbe esserci un runheader ora...
2239 };
2240 } else {
2241 //
2242 // we have runheaders but not runtrailers!
2243 //
2244 if ( debug ) printf(" We have runheaders (%i) but not runtrailers (%i) ! \n",rhev,rtev);
2245 for ( pth=0; pth < rhev; pth++ ){
2246 rh->GetEntry(pth);
2247 phh = ehh->GetPscuHeader();
2248 pkth = phh->GetCounter();
2249 obth = phh->GetOrbitalTime();
2250 if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2251 cod = ehh->GetCounter();
2252 tcod = (UInt_t)cod->Get(pctp->Physics);
2253 evbefh = TMath::Max(chminentry,tcod);
2254 // if ( (UInt_t)evbefh == upperentry ) evbefh = upperentry + 1; // this does not work due to the Counter bug in chewbacca
2255 if ( (UInt_t)evbefh == upperentry || !upperentry ) evbefh = upperentry + 1;
2256 //
2257 if ( debug ) printf(" evbefh %i upperentry %u \n",evbefh,upperentry);
2258 //
2259 this->HandleRunFragments(false,true,evbefh,upperentry);
2260 //
2261 };
2262 };
2263 //
2264 };
2265 //
2266 } else {
2267 //
2268 Int_t conptt = -1;
2269 for (Int_t ptt=0; ptt<rtev; ptt++){
2270 //
2271 rt->GetEntry(ptt);
2272 pht = eht->GetPscuHeader();
2273 pktt = pht->GetCounter();
2274 obtt = pht->GetOrbitalTime();
2275 //
2276 if ( PKT(pktt) >= PKT(pktfirst) && PKT(pktt) <= upperpkt ){
2277 //
2278 conptt++;
2279 //
2280 cod = eht->GetCounter();
2281 ptht = cod->Get(pctp->RunHeader) - 1;
2282 // evbeft = cod->Get(pctp->Physics);
2283 tcod = (UInt_t)cod->Get(pctp->Physics);
2284 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2285 evbeft = TMath::Min(upperentry,(tcod-1));
2286 if ( debug ) printf(" Loop in runtrailers, evbeft is %u upperentry %u cod->getetc %u \n",evbeft,upperentry,cod->Get(pctp->Physics));
2287 //
2288 // if ( !conptt && !(ptht+1) ){ // here we assume ptht+1 = 0 at the beginning of the interval that could not be true if the interval start from middle file... it must be equal to the number of RH before the interval that is not counted anywhere
2289 if ( !conptt && (ptht+1) == nrhbef ){
2290 //
2291 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %u \n",ptht,pth,ptt);
2292 //
2293 this->HandleRunFragments(true,false,chminentry,(evbeft));
2294 //
2295 pth = ptht; // ??
2296 //
2297 } else if ( pth == ptht ){
2298 //
2299 if ( IsDebug() ) printf(" Missing header %i %i %u\n",ptht,pth,ptt);
2300 //
2301 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2302 rt->GetEntry(ptt-1);
2303 cod = eht->GetCounter();
2304 tcod = (UInt_t)cod->Get(pctp->Physics);
2305 evbefh = TMath::Max(chminentry,tcod);
2306 //evbefh = cod->Get(pctp->Physics);
2307 rt->GetEntry(ptt);
2308 pht = eht->GetPscuHeader();
2309 //
2310 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %u \n",ptht,pth,ptt);
2311 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2312 //
2313 this->HandleMissingHoT(true,false,evbefh,(evbeft));
2314 //
2315 } else {
2316 //
2317 rh->GetEntry(ptht);
2318 phh = ehh->GetPscuHeader();
2319 pkth = phh->GetCounter();
2320 obth = phh->GetOrbitalTime();
2321 cod = ehh->GetCounter();
2322 tcod = (UInt_t)cod->Get(pctp->Physics);
2323 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2324 evbefh = TMath::Max(chminentry,(tcod-1));
2325 //
2326 if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2327 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %u\n",ptht,pth,ptt);
2328 //
2329 // evbefh = cod->Get(pctp->Physics);
2330 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2331 //
2332 // handle this run
2333 //
2334 this->HandleRun();
2335 //
2336 //
2337 if ( debug ) printf(" Standard case, evbefh is %u chminentry %u cod->getetc %u \n",evbefh,chminentry,cod->Get(pctp->Physics));
2338 //
2339 } else {
2340 // missing header in the considered interval!
2341 if ( debug ) printf(" Missing header in the considered interval \n");
2342 this->HandleRunFragments(true,false,evbefh,evbeft);
2343 };
2344 //
2345 if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && PKT(pkth)<=(upperpkt) && !conptt ){
2346 //
2347 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER evbefh = %u \n",evbefh);
2348 //
2349 if ( evbefh == 0 ) {
2350 //
2351 if ( !chewbacca ){
2352 signal = 8;
2353 if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
2354 } else {
2355 if ( debug ) printf(" The file does not start with a physics packet, no problem continue \n");
2356 };
2357 //
2358 } else {
2359 //
2360 this->HandleRunFragments(true,true,chminentry,(evbefh));
2361 //
2362 // what about pth=ptht-1
2363 };
2364 };
2365 //
2366 //
2367 if ( (ptht - pth) > 1 ){
2368 //
2369 if ( IsDebug() ) printf(" Missing runtrailers! \n");
2370 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %u \n",ptht,pth,ptt);
2371 // is not the consecutive header
2372 while ( pth != ptht ){
2373 //
2374 // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
2375 //
2376 pth++;
2377 //
2378 rh->GetEntry(pth+1);
2379 phh = ehh->GetPscuHeader();
2380 pktt = phh->GetCounter();
2381 obtt = phh->GetOrbitalTime();
2382 cod = ehh->GetCounter();
2383 // evbeft = cod->Get(pctp->Physics);
2384 tcod = (UInt_t)cod->Get(pctp->Physics);
2385 if ( !tcod ) tcod = 1; // IS A BUG SINCE RUNS WITH ZERO EVENTS WILL LOOK LIKE RUNS WITH ONE EVENT
2386 evbeft = TMath::Min(upperentry,(tcod-1));
2387 rh->GetEntry(pth);
2388 phh = ehh->GetPscuHeader();
2389 cod = ehh->GetCounter();
2390 pkth = phh->GetCounter();
2391 obth = phh->GetOrbitalTime();
2392 //evbefh = cod->Get(pctp->Physics);
2393 tcod = (UInt_t)cod->Get(pctp->Physics);
2394 evbefh = TMath::Max(chminentry,tcod);
2395 //
2396 if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt && pth != ptht ){
2397 //
2398 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %u \n",ptht,pth,ptt);
2399 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
2400 //
2401 this->HandleMissingHoT(false,true,evbefh,evbeft);
2402 //
2403 };
2404 };
2405 //
2406 } else if ( !(ptht - pth) ){
2407 //
2408 if ( IsDebug() ) printf(" Missing runheader! \n");
2409 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %u \n",ptht,pth,ptt);
2410 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
2411 if ( PEDANTIC ) throw -87;
2412 //
2413 } else {
2414 //
2415 // go on with next header
2416 //
2417 pth = ptht;
2418 };
2419 //
2420 };
2421 //
2422 // if ( ptt+1 == rtev){
2423 if ( conptt+1 == nrtev ){
2424 // if ( conptt+1 == (nrtev+nrtbef )){
2425 if ( IsDebug() ) printf(" >>>>>>>>>>> %i %u %i %u \n",ptht,rhev,nrtev,conptt);
2426 ptht++;
2427 if ( ptht < rhev ){
2428 rh->GetEntry(ptht);
2429 //pth++;
2430 //if ( pth < rhev ){
2431 //rh->GetEntry(pth);
2432 phh = ehh->GetPscuHeader();
2433 pkth = phh->GetCounter();
2434 obth = phh->GetOrbitalTime();
2435 cod = ehh->GetCounter();
2436 tcod = (UInt_t)cod->Get(pctp->Physics);
2437 evbefh = TMath::Max(chminentry,tcod);
2438 if ( PKT(pkth) >= PKT(pktfirst) && PKT(pkth) <= upperpkt ){
2439 // evbefh = cod->Get(pctp->Physics);
2440 if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
2441 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %u \n",ptht,pth,ptt);
2442 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
2443 if ( IsDebug() ) printf(" evbefh %u upperentry %u \n",(UInt_t)evbefh,upperentry);
2444 //
2445 this->HandleRunFragments(false,true,evbefh,upperentry);
2446 //
2447 };
2448 } else {
2449 //
2450 // check if we have a fragment with no header
2451 //
2452 if ( (UInt_t)evbeft < upperentry-1 && upperentry>0 ){
2453 if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER! evbeft %u upperentry-1 %u \n",(UInt_t)evbeft,upperentry-1);
2454 //
2455 // if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
2456 if ( (rtev-1) < 0 || ptt < 0 ) throw -15; // should never arrive here!
2457 // rt->GetEntry(ptt-1);
2458 rt->GetEntry(rtev-1);
2459 cod = eht->GetCounter();
2460 tcod = (UInt_t)cod->Get(pctp->Physics)+1;
2461 evbefh = TMath::Max(chminentry,tcod);
2462 // evbefh = cod->Get(pctp->Physics);
2463 rt->GetEntry(ptt);
2464 pht = eht->GetPscuHeader();
2465 this->HandleRunFragments(true,true,evbefh,upperentry);
2466 };
2467 };
2468 };
2469 //
2470 };
2471 };
2472 };
2473 //
2474 return(signal);
2475 };
2476
2477 /**
2478 *
2479 * Check if the run has already been inserted
2480 *
2481 */
2482 Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
2483 //
2484 TSQLResult *result = 0;
2485 TSQLRow *row = 0;
2486 //
2487 stringstream oss;
2488 oss.str("");
2489 //
2490 // the where clause is of the type: boot_number = _our_boot && (
2491 // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
2492 // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
2493 // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
2494 // ||
2495 // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time &&
2496 // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
2497 // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
2498 // ||
2499 // ( runhead_time = _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
2500 // )
2501 // ||
2502 // ( runhead_time = _our_runhead_time && runtrail_time > _our_runtrail_time && nevents > 100 )
2503 // )
2504 // ||
2505 // ( runhead_time < _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
2506 // )
2507 //
2508 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
2509 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2510 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2511 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2512 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2513 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2514 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2515 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2516 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2517 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2518 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2519 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2520 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR ";
2521 if ( glrun->GetNEVENTS() < 100 ){
2522 oss<<" RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << "))); ";
2523 } else {
2524 oss << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ")) OR "
2525 << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " // these two lines in a certain way disable the patch below...
2526 << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR" //
2527 << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
2528 << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR" //
2529 << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " //
2530 << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
2531 << " );";
2532 };
2533 //
2534 if ( IsDebug() ) printf(" THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u NEVENTS %u\n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT(),glrun->GetNEVENTS());
2535 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2536 result = conn->Query(oss.str().c_str());
2537 //
2538 if ( !result ) throw -4;
2539 //
2540 row = result->Next();
2541 //
2542 if ( !row ){
2543 if ( IsDebug() ) printf(" The run is new \n");
2544 if ( IsDebug() ) printf(" -> fill the DB \n");
2545 return(false); // the file has not been inserted in the DB, go on.
2546 };
2547 //
2548 Bool_t signal = true;
2549 //
2550 while ( row != NULL ){
2551 if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
2552 //
2553 // the run has already been inserted
2554 //
2555 if ( signal && IsDebug() ) printf(" The run has already been inserted\n");
2556 if ( PEDANTIC ) throw -86;
2557 return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
2558 //
2559 // PATCH!
2560 // 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
2561 // while the old run doesn't have it 3) we have more events than the old run
2562 //
2563 if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
2564 //
2565 if ( IsDebug() ) printf(" The new run has more events than the old one \n");
2566 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2567 // oss.str("");
2568 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2569 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2570 // conn->Query(oss.str().c_str());
2571 if ( signal ) signal = false;
2572 goto gonext;
2573 //
2574 } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
2575 if ( IsDebug() ) printf(" The new run has less events than the old one \n");
2576 if ( IsDebug() ) printf(" The run is already inserted \n");
2577 goto gonext;
2578 };
2579 //
2580 if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
2581 //
2582 if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
2583 //
2584 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2585 // oss.str("");
2586 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2587 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2588 // conn->Query(oss.str().c_str());
2589 //
2590 if ( signal ) signal = false;
2591 goto gonext;
2592 } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
2593 if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
2594 if ( IsDebug() ) printf(" The run is already inserted \n");
2595 goto gonext;
2596 };
2597 //
2598 if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
2599 //
2600 if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
2601 //
2602 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2603 // oss.str("");
2604 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
2605 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
2606 // conn->Query(oss.str().c_str());
2607 if ( signal ) signal = false;
2608 //
2609 };
2610 //
2611 gonext:
2612 // END PATCH!
2613 //
2614 row = result->Next();
2615 //
2616 };
2617 //
2618 delete result;
2619 //
2620 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
2621 if ( !signal && IsDebug() ) printf(" The run existed and has been overridden, fill the DB \n");
2622 if ( PEDANTIC ) throw -86;
2623 return(signal);
2624 };
2625
2626 /**
2627 * Handle runs which seems to be good ones.
2628 **/
2629 void PamelaDBOperations::HandleRun(){
2630 ULong64_t chkpkt = 0;
2631 ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
2632 ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
2633 //
2634 chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
2635 //
2636 if ( labs(chkpkt-pktt)<2 ){
2637 //
2638 if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
2639 //
2640 // it must be a good run, fill the db
2641 //
2642 this->FillClass();
2643 //
2644 if ( !IsRunAlreadyInserted() ){
2645 glrun->SetID(this->AssignRunID());
2646 glrun->SetID_RUN_FRAG(0);
2647 glrun->Fill_GL_RUN(conn);
2648 };
2649 } else {
2650 //
2651 if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
2652 if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
2653 //
2654 this->HandleSuspiciousRun();
2655 //
2656 };
2657 //
2658 //
2659 return;
2660 };
2661
2662
2663 /**
2664 * Handle run fragments at the beginning or at the end of the file
2665 **/
2666 void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2667 //
2668 UInt_t rhfirstev = firstev;
2669 UInt_t rtlastev = lastev;
2670 Bool_t found = false;
2671 Bool_t foundinrun = false;
2672 //
2673 TSQLResult *result = 0;
2674 TSQLRow *row = 0;
2675 //
2676 stringstream oss;
2677 oss.str("");
2678 //
2679 // is the piece of run good (no other packets inside)?
2680 //
2681 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2682 //
2683 // if not, handle other pieces and continue with the first one
2684 //
2685 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2686 //
2687 } else {
2688 //
2689 // we have now the good first piece of a run, fill the glrun object
2690 //
2691 if ( firstev != (lastev+1) ){ // could be a problem sometimes (?)
2692 if ( rhfirstev != firstev && !mishead ) mishead = true;
2693 if ( rtlastev != lastev && !mistrail ) mistrail = true;
2694 };
2695 //
2696 if ( IsDebug() ) printf(" bhere firstev is %i lastev is %i \n",firstev,lastev);
2697 this->FillClass(mishead,mistrail,firstev,lastev);
2698 if ( IsDebug() ) printf(" chere firstev is %i lastev is %i \n",firstev,lastev);
2699 //
2700 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
2701 if ( IsDebug() ) printf(" C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
2702 //
2703 // First of all insert the run in the fragment table...
2704 //
2705 oss.str("");
2706 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
2707 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2708 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2709 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2710 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2711 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2712 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2713 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2714 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2715 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2716 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2717 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2718 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2719 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2720 //
2721 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2722 result = conn->Query(oss.str().c_str());
2723 //
2724 if ( !result ) throw -4;
2725 //
2726 row = result->Next();
2727 //
2728 if ( !row ){
2729 //
2730 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2731 //
2732 if ( IsDebug() ) printf(" The run is new \n");
2733 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2734 //
2735 glrun->SetID(this->AssignRunID());
2736 glrun->SetID_RUN_FRAG(0);
2737 glrun->Fill_GL_RUN_FRAGMENTS(conn);
2738 //
2739 } else {
2740 if ( IsDebug() ) printf(" The run is already present in the fragment table, relaxed %u \n",RELAXED);
2741 if ( !RELAXED ){
2742 if ( PEDANTIC ) throw -69;
2743 return;
2744 } else {
2745 glrun->SetID((UInt_t)atoll(row->GetField(0)));
2746 glrun->SetID_RUN_FRAG(0);
2747 };
2748 };
2749 //
2750 if ( chewbacca && mishead && mistrail ) goto justcheck;
2751 //
2752 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2753 //
2754 if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2755 // missing it no way we can found a piece in the frag table
2756 //
2757 oss.str("");
2758 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2759 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2760 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2761 << " ID != " << glrun->ID
2762 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2763 //
2764 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
2765 result = conn->Query(oss.str().c_str());
2766 //
2767 if ( !result ) throw -4;
2768 //
2769 row = result->Next();
2770 //
2771 if ( !row && NoFrag() ){
2772 //
2773 oss.str("");
2774 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2775 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2776 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2777 << " ID != " << glrun->ID
2778 << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2779 //
2780 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2781 result = conn->Query(oss.str().c_str());
2782 //
2783 if ( !result ) throw -4;
2784 //
2785 foundinrun = true;
2786 //
2787 row = result->Next();
2788 //
2789 };
2790 //
2791 if ( !row ){
2792 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2793 found = false;
2794 } else {
2795 //
2796 found = false; // default value
2797 //
2798 if ( IsDebug() ) printf(" A Found a possible candidate, checking if it is the good one... \n");
2799 //
2800 // if we have both runheader and runtrailer we can check with pkt_counter:
2801 //
2802 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
2803 ULong64_t chkpkt = 0;
2804 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2805 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2806 //
2807 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
2808 //
2809 if ( labs(chkpkt-pktt)<2 ){
2810 //
2811 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2812 //
2813 found = true;
2814 if ( IsDebug() ) printf(" where firstev is %i lastev is %i \n",firstev,lastev);
2815 //
2816 } else {
2817 //
2818 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2819 //
2820 found = false;
2821 //
2822 };
2823 };
2824 if ( !found && chewbacca ) goto justcheck;
2825 if ( !found ){
2826 //
2827 // 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
2828 //
2829 ULong64_t chkpkt1 = 0;
2830 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2831 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2832 chkpkt1 = labs(orunh1-dbrunt1);
2833 //
2834 ULong64_t chkpkt2 = 0;
2835 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2836 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2837 chkpkt2 = labs(orunh2-dbrunt2);
2838 //
2839 ULong64_t chkpkt3 = 0;
2840 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2841 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2842 chkpkt3 = labs(orunh3-dbrunt3);
2843 //
2844 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2845 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2846 //
2847 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2848 //
2849 found = true;
2850 //
2851 } else {
2852 //
2853 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2854 //
2855 found = false;
2856 //
2857 };
2858 };
2859 };
2860 //
2861 if ( found ){
2862 //
2863 // 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
2864 //
2865 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2866 //
2867 if ( foundinrun ){
2868 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2869 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2870 };
2871 //
2872 GL_RUN *glrun1 = new GL_RUN();
2873 //
2874 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2875 //
2876 oss.str("");
2877 oss << " ID="<<row->GetField(0)<<";";
2878 //
2879 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2880 if ( IsDebug() ) printf(" there firstev is %i lastev is %i \n",firstev,lastev);
2881 //
2882 // merge infos
2883 //
2884 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2885 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2886 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2887 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2888 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2889 TTree *T= 0;
2890 T = (TTree*)file->Get("Physics");
2891 if ( !T || T->IsZombie() ) throw -16;
2892 EventHeader *eh = 0;
2893 PscuHeader *ph = 0;
2894 T->SetBranchAddress("Header", &eh);
2895 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2896 T->GetEntry(firstev);
2897 ph = eh->GetPscuHeader();
2898 bpkt = PKT(ph->GetCounter());
2899 bobt = OBT(ph->GetOrbitalTime());
2900 firstev++;
2901 if ( PEDANTIC ) throw -71;
2902 };
2903 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2904 //
2905 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2906 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2907 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2908 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2909 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2910 //
2911 if ( IsDebug() ) printf(" here firstev is %i lastev is %i \n",firstev,lastev);
2912 //
2913 glrun->SetEV_FROM(firstev);
2914 glrun->SetNEVENTS(lastev-firstev+1);
2915 //
2916 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2917 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2918 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2919 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2920 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2921 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2922 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2923 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2924 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2925 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2926 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2927 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2928 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2929 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2930 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2931 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2932 //
2933 if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
2934 if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
2935 //
2936 if ( !IsRunAlreadyInserted() ){
2937 //
2938 // glrun->SetID(this->AssignRunID());
2939 glrun->SetID_RUN_FRAG(glrun1->GetID());
2940 glrun->Fill_GL_RUN(conn);
2941 //
2942 // set id number
2943 //
2944 glrun1->SetID_RUN_FRAG(glrun->GetID());
2945 glrun1->Fill_GL_RUN(conn);
2946 //
2947 };
2948 // delete old entry in fragment table
2949 //
2950 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2951 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2952 //
2953 delete glrun1;
2954 //
2955 //
2956 return;
2957 //
2958 };
2959 //
2960 };
2961 //
2962 if ( mistrail && ( rtlastev == lastev || chewbacca )) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2963 // missing it no way we can found a piece in the frag table
2964 //
2965 oss.str("");
2966 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2967 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2968 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2969 << " ID != " << glrun->ID
2970 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2971 //
2972 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2973 result = conn->Query(oss.str().c_str());
2974 //
2975 if ( !result ) throw -4;
2976 //
2977 row = result->Next();
2978 //
2979 if ( !row && NoFrag() ){
2980 //
2981 oss.str("");
2982 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2983 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2984 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2985 << " ID != " << glrun->ID
2986 << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2987 //
2988 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2989 result = conn->Query(oss.str().c_str());
2990 //
2991 if ( !result ) throw -4;
2992 //
2993 foundinrun = true;
2994 row = result->Next();
2995 //
2996 };
2997 //
2998 if ( !row ){
2999 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3000 found = false;
3001 } else {
3002 //
3003 found = false; // default value
3004 //
3005 if ( IsDebug() ) printf(" B Found a possible candidate, checking if it is the good one... \n");
3006 //
3007 // if we have both runheader and runtrailer we can check with pkt_counter:
3008 //
3009 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
3010 ULong64_t chkpkt = 0;
3011 ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3012 ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3013 //
3014 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
3015 //
3016 if ( labs(chkpkt-pktt)<2 ){
3017 //
3018 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3019 //
3020 found = true;
3021 //
3022 } else {
3023 //
3024 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3025 //
3026 found = false;
3027 //
3028 };
3029 };
3030 if ( !found && chewbacca ) goto justcheck;
3031 if ( !found ){
3032 //
3033 // 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
3034 //
3035 ULong64_t chkpkt1 = 0;
3036 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3037 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3038 chkpkt1 = labs(orunh1-dbrunt1);
3039 //
3040 ULong64_t chkpkt2 = 0;
3041 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
3042 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3043 chkpkt2 = labs(orunh2-dbrunt2);
3044 //
3045 ULong64_t chkpkt3 = 0;
3046 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
3047 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3048 chkpkt3 = labs(orunh3-dbrunt3);
3049 //
3050 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3051 //
3052 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3053 //
3054 found = true;
3055 //
3056 } else {
3057 //
3058 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3059 //
3060 found = false;
3061 //
3062 };
3063 };
3064 };
3065 //
3066 if ( found ){
3067 //
3068 // 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
3069 //
3070 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3071 //
3072 if ( foundinrun ){
3073 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3074 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3075 };
3076 //
3077 GL_RUN *glrun1 = new GL_RUN();
3078 //
3079 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3080 //
3081 oss.str("");
3082 oss << " ID="<<row->GetField(0)<<";";
3083 //
3084 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
3085 //
3086 // merge infos
3087 //
3088 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
3089 ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
3090 UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
3091 ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
3092 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3093 TTree *T= 0;
3094 T = (TTree*)file->Get("Physics");
3095 if ( !T || T->IsZombie() ) throw -16;
3096 EventHeader *eh = 0;
3097 PscuHeader *ph = 0;
3098 T->SetBranchAddress("Header", &eh);
3099 while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
3100 T->GetEntry(lastev);
3101 ph = eh->GetPscuHeader();
3102 apkt = PKT(ph->GetCounter());
3103 aobt = OBT(ph->GetOrbitalTime());
3104 lastev--;
3105 if ( PEDANTIC && !RELAXED) throw -72;
3106 };
3107 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
3108 //
3109 glrun->SetEV_TO(lastev);
3110 glrun->SetNEVENTS(lastev-firstev+1);
3111 glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
3112 glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
3113 glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
3114 glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
3115 glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
3116 //
3117 glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
3118 glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
3119 glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
3120 glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
3121 glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
3122 glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
3123 glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
3124 glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
3125 glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
3126 glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
3127 glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
3128 glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
3129 glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
3130 glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
3131 glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
3132 glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
3133 //
3134 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ) glrun1->SetPHYSENDRUN_MASK_S3S2S12(glrun->GetPHYSENDRUN_MASK_S3S2S12());
3135 if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) glrun1->SetPHYSENDRUN_MASK_S11CRC(glrun->GetPHYSENDRUN_MASK_S11CRC());
3136 //
3137 if ( !IsRunAlreadyInserted() ){
3138 //
3139 // glrun->SetID(this->AssignRunID());
3140 //
3141 glrun->SetID_RUN_FRAG(glrun1->GetID());
3142 glrun->Fill_GL_RUN(conn);
3143 //
3144 // set id number
3145 //
3146 glrun1->SetID_RUN_FRAG(glrun->GetID());
3147 glrun1->Fill_GL_RUN(conn);
3148 //
3149 };
3150 //
3151 // delete old entries in fragment table
3152 //
3153 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3154 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3155 //
3156 delete glrun1;
3157 //
3158 return;
3159 //
3160 };
3161 //
3162 };
3163 //
3164 justcheck:
3165 //
3166 if ( !found ){
3167 //
3168 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3169 //
3170 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3171 //
3172 oss.str("");
3173 oss << " SELECT ID,ID_ROOT_L0 FROM GL_RUN WHERE "
3174 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3175 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3176 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3177 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3178 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3179 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3180 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3181 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3182 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3183 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3184 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3185 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3186 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3187 //
3188 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3189 result = conn->Query(oss.str().c_str());
3190 //
3191 if ( !result ) throw -4;
3192 //
3193 row = result->Next();
3194 //
3195 if ( row ){
3196 //
3197 // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
3198 // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
3199 //
3200 // 090112 [8RED (-70): RUN ALREADY INSERTED]
3201 //
3202 Bool_t OK = false;
3203 UInt_t IDRL2A = 0;
3204 UInt_t IDRL2B = 0;
3205 if ( chewbacca ){
3206 if ( result->GetRowCount() == 2 ){
3207 IDRL2A = (UInt_t)atoll(row->GetField(0));
3208 UInt_t IDRL0A = (UInt_t)atoll(row->GetField(1));
3209 row = result->Next();
3210 IDRL2B = (UInt_t)atoll(row->GetField(0));
3211 UInt_t IDRL0B = (UInt_t)atoll(row->GetField(1));
3212 if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
3213 if ( IDRL0A == IDRL0B ){
3214 TSQLResult *result2 = 0;
3215 TSQLRow *row2 = 0;
3216 oss.str("");
3217 oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
3218 if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3219 result2 = conn->Query(oss.str().c_str());
3220 //
3221 if ( !result2 ) throw -4;
3222 //
3223 row2 = result2->Next();
3224 //
3225 if ( row2 ){
3226 UInt_t PKA = (UInt_t)atoll(row2->GetField(0));
3227 delete result2;
3228 oss.str("");
3229 oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
3230 if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
3231 result2 = conn->Query(oss.str().c_str());
3232 //
3233 if ( !result2 ) throw -4;
3234 //
3235 row2 = result2->Next();
3236 //
3237 if ( row2 ){
3238 UInt_t PKB = (UInt_t)atoll(row2->GetField(0));
3239 //
3240 if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
3241 if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
3242 if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
3243 OK = true;
3244 };
3245 };
3246 };
3247 };
3248 };
3249 };
3250 if ( OK ){
3251 //
3252 // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
3253 //
3254 // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
3255 //
3256 GL_RUN *glA = new GL_RUN();
3257 glA->Query_GL_RUN(IDRL2A,conn);
3258 //
3259 if ( glA->GetACQ_BUILD_INFO() != 0 ){
3260 //
3261 // the first piece contains a good runheader we can update all the other runs with correct infos!
3262 //
3263 oss.str("");
3264 oss << "UPDATE GL_RUN_FRAGMENTS SET "
3265 << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
3266 << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3267 << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
3268 << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
3269 << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
3270 << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
3271 << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
3272 << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
3273 << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
3274 << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
3275 << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "
3276 << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "
3277 << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "
3278 << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "
3279 << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
3280 //
3281 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3282 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
3283 if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3284 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
3285 //
3286 oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
3287 oss << " WHERE ID=" << glrun->GetID() << ";";
3288 if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
3289 conn->Query(oss.str().c_str());
3290 //
3291 } else {
3292 //
3293 // sig no runheader, let set anyway what is possible...
3294 //
3295 oss.str("");
3296 oss << "UPDATE GL_RUN_FRAGMENTS SET "
3297 << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
3298 //
3299 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
3300 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
3301 if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
3302 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
3303 //
3304 oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
3305 << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
3306 oss << " WHERE ID=" << glrun->GetID() << ";";
3307 if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
3308 conn->Query(oss.str().c_str());
3309 };
3310 //
3311 // update runheader ROOT_ID_FRAG
3312 //
3313 oss.str("");
3314 oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
3315 if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
3316 conn->Query(oss.str().c_str());
3317 //
3318 // now let's look for runtrailer if any in the last run
3319 //
3320 glA->Query_GL_RUN(IDRL2B,conn);
3321 //
3322 if ( glA->GetPKT_READY_COUNTER() != 0 ){
3323 //
3324 // the first piece contains a good runtrailer we can update all the other runs with correct infos!
3325 //
3326 oss.str("");
3327 oss << "UPDATE GL_RUN_FRAGMENTS SET "
3328 << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3329 << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
3330 << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
3331 << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
3332 //
3333 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3334 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3335 if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
3336 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3337 //
3338 oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
3339 << " WHERE ID=" << glrun->GetID() << ";";
3340 if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
3341 conn->Query(oss.str().c_str());
3342 //
3343 } else {
3344 //
3345 // sig no runtrailer, let set anyway what is possible...
3346 //
3347 oss.str("");
3348 oss << "UPDATE GL_RUN_FRAGMENTS SET "
3349 << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
3350 << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
3351 //
3352 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
3353 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
3354 if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
3355 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
3356 //
3357 oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
3358 << " WHERE ID=" << glrun->GetID() << ";";
3359 if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
3360 conn->Query(oss.str().c_str());
3361 };
3362 //
3363 UInt_t myi = glrun->GetID();
3364 oss.str("");
3365 oss << " ID= "<< myi;
3366 //
3367 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3368 //
3369 // fill the new run in GL_RUN
3370 //
3371 glrun->SetID_RUN_FRAG(IDRL2B);
3372 glrun->Fill_GL_RUN(conn);
3373 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3374 delete glA;
3375 //
3376 } else {
3377 //
3378 // is just a repetition
3379 //
3380 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3381 if ( PEDANTIC ) throw -70;
3382 };
3383 //
3384 } else {
3385 if ( NoFrag() ){
3386 glrun->SetID_RUN_FRAG(glrun->GetID());
3387 glrun->Fill_GL_RUN(conn);
3388 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3389 };
3390 };
3391 };
3392 }; // EEE
3393 //
3394 return;
3395 };
3396
3397
3398 /**
3399 * Handle run without header or trailer
3400 **/
3401 void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
3402 //
3403 //
3404 // is the piece of run good (no other packets inside)?
3405 //
3406 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
3407 //
3408 // if not, handle other pieces and continue with the first one
3409 //
3410 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
3411 //
3412 } else {
3413 //
3414 this->FillClass(mishead,mistrail,firstev,lastev);
3415 //
3416 if ( !IsRunAlreadyInserted() ){
3417 glrun->SetID(this->AssignRunID());
3418 glrun->SetID_RUN_FRAG(0);
3419 glrun->Fill_GL_RUN(conn); // it'ok we arrive here only inside a file hence in the middle of the runs...
3420 };
3421 //
3422 };
3423 //
3424 return;
3425 };
3426
3427 /**
3428 *
3429 * check if we have non-physics packets inside the run
3430 *
3431 */
3432 Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
3433 //
3434 EventCounter *code=0;
3435 //
3436 UInt_t nevent = 0;
3437 UInt_t checkfirst = 0;
3438 UInt_t checklast = 0;
3439 UInt_t firstentry = 0;
3440 UInt_t lastentry = 0;
3441 UInt_t firstTime = 0;
3442 UInt_t lastTime = 0;
3443 UInt_t firstPkt = 0;
3444 UInt_t lastPkt = 0;
3445 UInt_t firstObt = 0;
3446 UInt_t lastObt = 0;
3447 //
3448 pcksList packetsNames;
3449 pcksList::iterator Iter;
3450 getPacketsNames(packetsNames);
3451 //
3452 TTree *T= 0;
3453 T =(TTree*)file->Get("Physics");
3454 if ( !T || T->IsZombie() ) throw -16;
3455 EventHeader *eh = 0;
3456 PscuHeader *ph = 0;
3457 T->SetBranchAddress("Header", &eh);
3458 nevent = T->GetEntries();
3459 //
3460 //
3461 if ( firstev == lastev+1 || lastev == firstev ) { // no events inside the run!
3462 //if ( firstev <= lastev+1 ) { // no events inside the run!
3463 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
3464 firstev = lastev+1; // this is necessary for files with no Physics entries, should have no influence on other files
3465 // return true is correct
3466 return(true);
3467 //
3468 } else {
3469 //
3470 T->GetEntry(firstev);
3471 code = eh->GetCounter();
3472 checkfirst = 0;
3473 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3474 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
3475 };
3476 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
3477 //
3478 T->GetEntry(lastev);
3479 code = eh->GetCounter();
3480 checklast = 0;
3481 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3482 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
3483 };
3484 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
3485 //
3486 if ( checkfirst == checklast ){
3487 //
3488 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
3489 //
3490 return(true);
3491 //
3492 } else {
3493 //
3494 if ( IsDebug() ) printf(" There are no-physics packets inside the run!\n");
3495 //
3496 // HERE WE MUST HANDLE THAT RUNS AND GO BACK
3497 //
3498 // if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
3499 //
3500 Bool_t emptyruns = false;
3501 UInt_t check = 0;
3502 UInt_t lastevtemp = lastev;
3503 UInt_t firstevno = firstev;
3504 UInt_t rhchko=0;
3505 UInt_t rhchk=0;
3506 //
3507 for (UInt_t i=firstev; i<=lastev; i++){
3508 //
3509 T->GetEntry(i);
3510 code = eh->GetCounter();
3511 //
3512 check = 0;
3513 //
3514
3515 // if we have a runheader set lastev then exit
3516 //
3517 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
3518 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
3519 };
3520 // check here if we have a runheader
3521 rhchko = rhchk;
3522 rhchk = code->Get(GetPacketType("RunHeader"));
3523 //
3524 if ( checkfirst < check || i == lastev ){
3525 //
3526 firstentry = firstevno;
3527 //
3528 if ( checkfirst < check ){
3529 lastentry = i-1;
3530 } else {
3531 lastentry = i;
3532 };
3533 //
3534 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
3535 //
3536 glrun->SetEV_FROM(firstentry);
3537 glrun->SetEV_TO(lastentry);
3538 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
3539 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
3540 lastentry--;
3541 };
3542 glrun->SetNEVENTS(lastentry-firstentry+1);
3543 //
3544 glrun->Set_GL_RUNH0();
3545 glrun->Set_GL_RUNT0();
3546 //
3547 glrun->SetLAST_TIMESYNC(0);
3548 glrun->SetOBT_TIMESYNC(0);
3549 //
3550 T->GetEntry(firstentry);
3551 ph = eh->GetPscuHeader();
3552 firstObt = ph->GetOrbitalTime();
3553 firstTime = this->GetAbsTime(firstObt);
3554 firstPkt = ph->GetCounter();
3555 //
3556 T->GetEntry(lastentry);
3557 ph = eh->GetPscuHeader();
3558 lastObt = ph->GetOrbitalTime();
3559 lastTime = this->GetAbsTime(lastObt);
3560 lastPkt = ph->GetCounter();
3561 //
3562 glrun->SetRUNHEADER_PKT(firstPkt);
3563 glrun->SetRUNTRAILER_PKT(lastPkt);
3564 //
3565 glrun->SetRUNHEADER_OBT(firstObt);
3566 glrun->SetRUNTRAILER_OBT(lastObt);
3567 //
3568 if ( IsDebug() ) printf(" A THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3569 if ( firstev == firstentry && !emptyruns && !mishead ){
3570 glrun->Set_GL_RUNH(runh,phh);
3571 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
3572 if ( IsDebug() ) printf(" We have the runheader \n");
3573 };
3574 if ( lastev == i && !mistrail ){
3575 glrun->Set_GL_RUNT(runt,pht);
3576 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
3577 if ( IsDebug() ) printf(" We have the runtrailer \n");
3578 };
3579 //
3580 if ( IsDebug() ) printf(" B THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3581 if ( lastentry == (firstentry-2) ){ // no events in the run
3582 emptyruns = true;
3583 if ( IsDebug() ) printf(" No events in the run \n");
3584 lastTime = firstTime;
3585 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
3586 lastObt = glrun->RUNHEADER_OBT;
3587 lastPkt = glrun->RUNHEADER_PKT;
3588 } else {
3589 lastObt = firstObt;
3590 lastPkt = firstPkt;
3591 };
3592 glrun->SetRUNTRAILER_PKT(lastPkt);
3593 glrun->SetRUNTRAILER_OBT(lastObt);
3594 lastentry++;
3595 };
3596 //
3597 this->SetCommonGLRUN(firstTime,lastTime);
3598 this->SetPhysEndRunVariables();
3599 //
3600 if ( chminentry == firstentry ){ // EEE
3601 if ( IsDebug() ) printf(" Inside isrunconsistent found a fragment of run at the beginning of the file, put it in the fragment table \n");
3602 //
3603 // this->HandleRunFragments(true,mistrail,firstentry,lastentry); // cannot call it here since it enters a loop which will destroy the already stored variables if we arrive here from HandleRunFragments
3604 //
3605
3606 mishead = true;
3607
3608
3609 UInt_t rhfirstev = firstentry;
3610 // UInt_t rtlastev = lastentry;
3611 Bool_t found = false;
3612 Bool_t foundinrun = false;
3613 //
3614 TSQLResult *result = 0;
3615 TSQLRow *row = 0;
3616 //
3617 stringstream oss;
3618 oss.str("");
3619 //
3620 // we have now the good first piece of a run, fill the glrun object
3621 //
3622 // if ( rhfirstev != firstev && !mishead ) mishead = true;
3623 // if ( rtlastev != lastev && !mistrail ) mistrail = true;
3624 //
3625 // this->FillClass(mishead,mistrail,firstev,lastev);
3626 //
3627 if ( IsDebug() ) printf("zz The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
3628 if ( IsDebug() ) printf("zz C THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
3629 //
3630 // First of all insert the run in the fragment table...
3631 //
3632 oss.str("");
3633 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
3634 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3635 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3636 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3637 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3638 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3639 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3640 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3641 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3642 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3643 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3644 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3645 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3646 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3647 //
3648 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3649 result = conn->Query(oss.str().c_str());
3650 //
3651 if ( !result ) throw -4;
3652 //
3653 row = result->Next();
3654 //
3655 if ( !row ){
3656 //
3657 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
3658 //
3659 if ( IsDebug() ) printf(" The run is new \n");
3660 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
3661 //
3662 glrun->SetID(this->AssignRunID());
3663 glrun->SetID_RUN_FRAG(0);
3664 glrun->Fill_GL_RUN_FRAGMENTS(conn);
3665 //
3666 } else {
3667 if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
3668 if ( PEDANTIC ) throw -69;
3669 // return;
3670 };
3671 //
3672 if ( chewbacca && mishead && mistrail ) goto zjustcheck;
3673 //
3674 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
3675 //
3676 if ( mishead && ( rhfirstev == firstev || chewbacca ) ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
3677 // missing it no way we can found a piece in the frag table
3678 //
3679 oss.str("");
3680 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
3681 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3682 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3683 << " ID != " << glrun->ID
3684 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3685 //
3686 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
3687 result = conn->Query(oss.str().c_str());
3688 //
3689 if ( !result ) throw -4;
3690 //
3691 row = result->Next();
3692 //
3693 if ( !row && NoFrag() ){
3694 //
3695 oss.str("");
3696 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
3697 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
3698 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3699 << " ID != " << glrun->ID
3700 << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
3701 //
3702 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
3703 result = conn->Query(oss.str().c_str());
3704 //
3705 if ( !result ) throw -4;
3706 //
3707 foundinrun = true;
3708 //
3709 row = result->Next();
3710 //
3711 };
3712 //
3713 if ( !row ){
3714 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
3715 found = false;
3716 } else {
3717 //
3718 found = false; // default value
3719 //
3720 if ( IsDebug() ) printf(" C Found a possible candidate, checking if it is the good one... \n");
3721 //
3722 // if we have both runheader and runtrailer we can check with pkt_counter:
3723 //
3724 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
3725 ULong64_t chkpkt = 0;
3726 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
3727 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
3728 //
3729 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
3730 //
3731 if ( labs(chkpkt-pktt)<2 ){
3732 //
3733 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
3734 //
3735 found = true;
3736 //
3737 } else {
3738 //
3739 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
3740 //
3741 found = false;
3742 //
3743 };
3744 };
3745 if ( !found && chewbacca ) goto zjustcheck;
3746 if ( !found ){
3747 //
3748 // 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
3749 //
3750 ULong64_t chkpkt1 = 0;
3751 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
3752 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
3753 chkpkt1 = labs(orunh1-dbrunt1);
3754 //
3755 ULong64_t chkpkt2 = 0;
3756 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
3757 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
3758 chkpkt2 = labs(orunh2-dbrunt2);
3759 //
3760 ULong64_t chkpkt3 = 0;
3761 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
3762 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
3763 chkpkt3 = labs(orunh3-dbrunt3);
3764 //
3765 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
3766 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
3767 //
3768 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
3769 //
3770 found = true;
3771 //
3772 } else {
3773 //
3774 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
3775 //
3776 found = false;
3777 //
3778 };
3779 };
3780 };
3781 //
3782 if ( found ){
3783 //
3784 // 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
3785 //
3786 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
3787 //
3788 if ( foundinrun ){
3789 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3790 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
3791 };
3792 //
3793 GL_RUN *glrun1 = new GL_RUN();
3794 //
3795 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
3796 //
3797 oss.str("");
3798 oss << " ID="<<row->GetField(0)<<";";
3799 //
3800 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
3801 //
3802 // merge infos
3803 //
3804 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
3805 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
3806 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
3807 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
3808 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3809 TTree *T= 0;
3810 T = (TTree*)file->Get("Physics");
3811 if ( !T || T->IsZombie() ) throw -16;
3812 EventHeader *eh = 0;
3813 PscuHeader *ph = 0;
3814 T->SetBranchAddress("Header", &eh);
3815 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
3816 T->GetEntry(firstev);
3817 ph = eh->GetPscuHeader();
3818 bpkt = PKT(ph->GetCounter());
3819 bobt = OBT(ph->GetOrbitalTime());
3820 firstev++;
3821 if ( PEDANTIC ) throw -71;
3822 };
3823 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
3824 //
3825 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
3826 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
3827 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
3828 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
3829 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
3830 //
3831 glrun->SetEV_FROM(firstev);
3832 glrun->SetNEVENTS(lastev-firstev+1);
3833 //
3834 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
3835 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
3836 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
3837 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
3838 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
3839 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
3840 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
3841 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
3842 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
3843 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
3844 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
3845 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
3846 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
3847 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
3848 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
3849 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
3850 //
3851 if ( glrun1->GetPHYSENDRUN_MASK_S3S2S12() ) glrun->SetPHYSENDRUN_MASK_S3S2S12(glrun1->GetPHYSENDRUN_MASK_S3S2S12());
3852 if ( glrun1->GetPHYSENDRUN_MASK_S11CRC() ) glrun->SetPHYSENDRUN_MASK_S11CRC(glrun1->GetPHYSENDRUN_MASK_S11CRC());
3853 //
3854 if ( !IsRunAlreadyInserted() ){
3855 //
3856 // glrun->SetID(this->AssignRunID());
3857 glrun->SetID_RUN_FRAG(glrun1->GetID());
3858 glrun->Fill_GL_RUN(conn);
3859 //
3860 // set id number
3861 //
3862 glrun1->SetID_RUN_FRAG(glrun->GetID());
3863 glrun1->Fill_GL_RUN(conn);
3864 //
3865 };
3866 // delete old entry in fragment table
3867 //
3868 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3869 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3870 //
3871 delete glrun1;
3872 //
3873 //
3874 // return;
3875 //
3876 };
3877 //
3878 };
3879 //
3880 //
3881 zjustcheck:
3882 //
3883 if ( !found ){
3884 //
3885 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
3886 //
3887 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
3888 //
3889 oss.str("");
3890 oss << " SELECT ID FROM GL_RUN WHERE "
3891 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
3892 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3893 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3894 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3895 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3896 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3897 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3898 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3899 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3900 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3901 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3902 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3903 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3904 //
3905 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3906 result = conn->Query(oss.str().c_str());
3907 //
3908 if ( !result ) throw -4;
3909 //
3910 row = result->Next();
3911 //
3912 if ( row ){
3913 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
3914 if ( PEDANTIC ) throw -70;
3915 } else {
3916 if ( NoFrag() ){
3917 glrun->SetID_RUN_FRAG(glrun->GetID());
3918 glrun->Fill_GL_RUN(conn);
3919 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
3920 };
3921 };
3922 }; // EEE
3923
3924
3925 } else {
3926 if ( !IsRunAlreadyInserted() ){
3927 glrun->SetID(this->AssignRunID());
3928 glrun->SetID_RUN_FRAG(0);
3929 glrun->Fill_GL_RUN(conn);
3930 };
3931 }; // EEE
3932 //
3933 firstevno = lastentry + 1;
3934 //
3935 checkfirst = check;
3936 //
3937 };
3938 //
3939 if ( i > firstev ){
3940 if ( rhchko != rhchk ){
3941 if ( IsDebug() ) printf("oh oh... we have a runheader! stop here and handle later the remaining piece\n");
3942 lastev = i;
3943 return(false);
3944 };
3945 };
3946 //
3947 if ( check == checklast && i != lastev ){
3948 lastevtemp = i - 1;
3949 i = lastev - 1;
3950 };
3951 //
3952 };
3953 //
3954 lastev = lastevtemp;
3955 //
3956 return(false);
3957 //
3958 };
3959 };
3960 //
3961 return(false); // should never arrive here
3962 };
3963
3964 /**
3965 *
3966 * 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
3967 * 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
3968 * looking for non-physics packets inside.
3969 *
3970 */
3971 void PamelaDBOperations::HandleSuspiciousRun(){
3972 //
3973 PacketType *pctp=0;
3974 EventCounter *codt=0;
3975 EventCounter *codh=0;
3976 EventCounter *code=0;
3977 UInt_t firstev = 0;
3978 UInt_t lastev = 0;
3979 UInt_t nevent = 0;
3980 UInt_t checkfirst = 0;
3981 UInt_t checklast = 0;
3982 UInt_t firstentry = 0;
3983 UInt_t lastentry = 0;
3984 UInt_t firstTime = 0;
3985 UInt_t lastTime = 0;
3986 UInt_t firstPkt = 0;
3987 UInt_t lastPkt = 0;
3988 UInt_t firstObt = 0;
3989 UInt_t lastObt = 0;
3990 //
3991 pcksList packetsNames;
3992 pcksList::iterator Iter;
3993 getPacketsNames(packetsNames);
3994 //
3995 TTree *rh=0;
3996 rh = (TTree*)file->Get("RunHeader");
3997 if ( !rh || rh->IsZombie() ) throw -17;
3998 TTree *T=0;
3999 T =(TTree*)file->Get("Physics");
4000 if ( !T || T->IsZombie() ) throw -16;
4001 EventHeader *eh = 0;
4002 PscuHeader *ph = 0;
4003 T->SetBranchAddress("Header", &eh);
4004 nevent = T->GetEntries();
4005 //
4006 codt = eht->GetCounter();
4007 codh = ehh->GetCounter();
4008 firstev = codh->Get(pctp->Physics);
4009 lastev = codt->Get(pctp->Physics)-1;
4010 if ( IsDebug() ) printf(" From the current runheader firstev is %u from the runtrailer lastev is %u \n",firstev,lastev);
4011 //
4012 if ( firstev == lastev+1 ) { // no events inside the run!
4013 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
4014 //
4015 this->FillClass();
4016 if ( !IsRunAlreadyInserted() ){
4017 glrun->SetID(this->AssignRunID());
4018 glrun->SetID_RUN_FRAG(0);
4019 glrun->Fill_GL_RUN(conn);
4020 };
4021 //
4022 } else {
4023 //
4024 UInt_t nrunh = 0 + codh->Get(pctp->RunHeader);
4025 UInt_t nrunh1 = 0 + codh->Get(pctp->RunHeader);
4026 T->GetEntry(firstev);
4027 code = eh->GetCounter();
4028 checkfirst = 0;
4029 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4030 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
4031 if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
4032 };
4033 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
4034 //
4035 T->GetEntry(lastev);
4036 code = eh->GetCounter();
4037 checklast = 0;
4038 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4039 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
4040 };
4041 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
4042 //
4043 if ( checkfirst == checklast ){
4044 //
4045 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
4046 //
4047 this->FillClass();
4048 if ( !IsRunAlreadyInserted() ){
4049 glrun->SetID(this->AssignRunID());
4050 glrun->SetID_RUN_FRAG(0);
4051 glrun->Fill_GL_RUN(conn);
4052 };
4053 //
4054 } else {
4055 //
4056 if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
4057 //
4058 Bool_t emptyruns = false;
4059 UInt_t check = 0;
4060 UInt_t firstevno = firstev;
4061 //
4062 for (UInt_t i=firstev; i<=lastev; i++){
4063 //
4064 T->GetEntry(i);
4065 code = eh->GetCounter();
4066 //
4067 check = 0;
4068 //
4069 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
4070 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
4071 if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
4072 };
4073 //
4074 if ( checkfirst < check || i == lastev ){
4075 //
4076 firstentry = firstevno;
4077 //
4078 if ( checkfirst < check ){
4079 lastentry = i-1;
4080 } else {
4081 lastentry = i;
4082 };
4083 //
4084 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
4085 //
4086 glrun->SetEV_FROM(firstentry);
4087 glrun->SetEV_TO(lastentry);
4088 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
4089 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
4090 lastentry--;
4091 };
4092 glrun->SetNEVENTS(lastentry-firstentry+1);
4093 //
4094 glrun->Set_GL_RUNH0();
4095 glrun->Set_GL_RUNT0();
4096 //
4097 glrun->SetLAST_TIMESYNC(0);
4098 glrun->SetOBT_TIMESYNC(0);
4099 //
4100 T->GetEntry(firstentry);
4101 ph = eh->GetPscuHeader();
4102 firstObt = ph->GetOrbitalTime();
4103 firstTime = this->GetAbsTime(firstObt);
4104 firstPkt = ph->GetCounter();
4105 //
4106 T->GetEntry(lastentry);
4107 ph = eh->GetPscuHeader();
4108 lastObt = ph->GetOrbitalTime();
4109 lastTime = this->GetAbsTime(lastObt);
4110 lastPkt = ph->GetCounter();
4111 //
4112 glrun->SetRUNHEADER_PKT(firstPkt);
4113 glrun->SetRUNTRAILER_PKT(lastPkt);
4114 //
4115 glrun->SetRUNHEADER_OBT(firstObt);
4116 glrun->SetRUNTRAILER_OBT(lastObt);
4117 //
4118 if ( IsDebug() ) printf(" AA THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
4119 //
4120 if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
4121 rh->GetEntry(nrunh1-1);
4122 phh = ehh->GetPscuHeader();
4123 nrunh1++;
4124 glrun->Set_GL_RUNH(runh,phh);
4125 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
4126 if ( IsDebug() ) printf(" We have the runheader \n");
4127 };
4128 if ( lastev == i && checkfirst == check ){
4129 glrun->Set_GL_RUNT(runt,pht);
4130 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
4131 if ( IsDebug() ) printf(" We have the runtrailer \n");
4132 };
4133 if ( IsDebug() ) printf(" BB THIS RUN: RUNHEADER_OBT %u RUNTRAILER_OBT %u RUNHEADER_PKT %u RUNTRAILER_PKT %u \n", glrun->GetRUNHEADER_OBT(),glrun->GetRUNTRAILER_OBT(),glrun->GetRUNHEADER_PKT(),glrun->GetRUNTRAILER_PKT());
4134 //
4135 if ( lastentry == (firstentry-2) ){ // no events in the run
4136 emptyruns = true;
4137 if ( IsDebug() ) printf(" No events in the run \n");
4138 lastTime = firstTime;
4139 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
4140 lastObt = glrun->RUNHEADER_OBT;
4141 lastPkt = glrun->RUNHEADER_PKT;
4142 } else {
4143 lastObt = firstObt;
4144 lastPkt = firstPkt;
4145 };
4146 glrun->SetRUNTRAILER_PKT(lastPkt);
4147 glrun->SetRUNTRAILER_OBT(lastObt);
4148 lastentry++;
4149 };
4150 //
4151 this->SetCommonGLRUN(firstTime,lastTime);
4152 this->SetPhysEndRunVariables();
4153 //
4154 if ( !IsRunAlreadyInserted() ){
4155 glrun->SetID(this->AssignRunID());
4156 glrun->SetID_RUN_FRAG(0);
4157 glrun->Fill_GL_RUN(conn);
4158 };
4159 //
4160 if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
4161 //
4162 firstentry = i;
4163 //
4164 lastentry = i;
4165 //
4166 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
4167 //
4168 glrun->SetEV_FROM(firstentry);
4169 glrun->SetEV_TO(lastentry);
4170 glrun->SetNEVENTS(lastentry-firstentry+1);
4171 //
4172 glrun->Set_GL_RUNH0();
4173 //
4174 glrun->SetLAST_TIMESYNC(0);
4175 glrun->SetOBT_TIMESYNC(0);
4176 //
4177 T->GetEntry(firstentry);
4178 ph = eh->GetPscuHeader();
4179 firstObt = ph->GetOrbitalTime();
4180 firstTime = this->GetAbsTime(firstObt);
4181 firstPkt = ph->GetCounter();
4182 //
4183 glrun->SetRUNHEADER_PKT(firstPkt);
4184 //
4185 glrun->SetRUNHEADER_OBT(firstObt);
4186 //
4187 glrun->Set_GL_RUNT(runt,pht);
4188 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
4189 if ( IsDebug() ) printf(" We have the runtrailer \n");
4190 //
4191 this->SetCommonGLRUN(firstTime,lastTime);
4192 this->SetPhysEndRunVariables();
4193 //
4194 if ( !IsRunAlreadyInserted() ){
4195 glrun->SetID(this->AssignRunID());
4196 glrun->SetID_RUN_FRAG(0);
4197 glrun->Fill_GL_RUN(conn);
4198 };
4199 };
4200 //
4201 firstevno = lastentry + 1;
4202 //
4203 checkfirst = check;
4204 //
4205 };
4206 //
4207 if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
4208 //
4209 };
4210 };
4211 };
4212 //
4213 return;
4214 };
4215
4216
4217 /**
4218 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
4219 */
4220 Int_t PamelaDBOperations::insertCALO_CALIB(){
4221 //
4222 TSQLResult *result = 0;
4223 TSQLRow *row = 0;
4224 //
4225 stringstream oss;
4226 oss.str("");
4227 //
4228 CalibCalPedEvent *calibCalPed = 0;
4229 TTree *tr = 0;
4230 EventHeader *eh = 0;
4231 PscuHeader *ph = 0;
4232 //
4233 UInt_t nevents = 0;
4234 UInt_t fromtime = 0;
4235 UInt_t totime = 0;
4236 UInt_t obt = 0;
4237 UInt_t pkt = 0;
4238 //
4239 tr = (TTree*)file->Get("CalibCalPed");
4240 if ( !tr || tr->IsZombie() ) throw -21;
4241 //
4242 tr->SetBranchAddress("CalibCalPed", &calibCalPed);
4243 tr->SetBranchAddress("Header", &eh);
4244 nevents = tr->GetEntries();
4245 //
4246 if ( !nevents ) return(1);
4247 //
4248 for (UInt_t i=0; i < nevents; i++){
4249 tr->GetEntry(i);
4250 for (UInt_t section = 0; section < 4; section++){
4251 //
4252 if ( calibCalPed->cstwerr[section] ){
4253 valid = 1;
4254 if ( calibCalPed->cperror[section] ) valid = 0;
4255 ph = eh->GetPscuHeader();
4256 obt = ph->GetOrbitalTime();
4257 pkt = ph->GetCounter();
4258 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4259 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4260 //
4261 if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4262 //
4263 // check if the calibration has already been inserted
4264 //
4265 oss.str("");
4266 oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
4267 << " SECTION = "<< section << " AND "
4268 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4269 << " OBT = "<< obt << " AND "
4270 << " PKT = "<< pkt << ";";
4271 //
4272 if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4273 result = conn->Query(oss.str().c_str());
4274 //
4275 if ( !result ) throw -4;
4276 //
4277 row = result->Next();
4278 //
4279 if ( row ){
4280 //
4281 if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
4282 if ( PEDANTIC ) throw -73;
4283 //
4284 } else {
4285 //
4286 // we have to insert a new calibration, check where to place it
4287 //
4288 oss.str("");
4289 oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
4290 << " SECTION = "<< section << " AND "
4291 << " FROM_TIME < "<< fromtime << " AND "
4292 << " TO_TIME > "<< fromtime << ";";
4293 //
4294 if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
4295 result = conn->Query(oss.str().c_str());
4296 //
4297 if ( !result ) throw -4;
4298 //
4299 row = result->Next();
4300 //
4301 if ( !row ){
4302 //
4303 // no calibrations in the db contain our calibration
4304 //
4305 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4306 if ( fromtime < 1150871000 ){ //1150866904
4307 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4308 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4309 };
4310 //
4311 oss.str("");
4312 oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
4313 << " SECTION = "<< section << " AND "
4314 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4315 //
4316 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4317 result = conn->Query(oss.str().c_str());
4318 //
4319 if ( !result ) throw -4;
4320 //
4321 row = result->Next();
4322 if ( !row ){
4323 totime = numeric_limits<UInt_t>::max();
4324 } else {
4325 totime = (UInt_t)atoll(row->GetField(0));
4326 };
4327 //
4328 } else {
4329 //
4330 // determine upper and lower limits and make space for the new calibration
4331 //
4332 totime = (UInt_t)atoll(row->GetField(1));
4333 //
4334 oss.str("");
4335 oss << " UPDATE GL_CALO_CALIB SET "
4336 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4337 << " ID = "<< row->GetField(0) << ";";
4338 //
4339 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
4340 result = conn->Query(oss.str().c_str());
4341 //
4342 if ( !result ) throw -4;
4343 //
4344 };
4345 //
4346 oss.str("");
4347 oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4348 << " VALUES (NULL,' "
4349 << idroot << "','"
4350 << i << "','"
4351 << fromtime << "','"
4352 << totime << "','"
4353 << section << "','"
4354 << obt << "','"
4355 << pkt << "','"
4356 << this->GetBOOTnumber() << "','"
4357 << valid << "');";
4358 //
4359 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
4360 //
4361 result = conn->Query(oss.str().c_str());
4362 //
4363 if ( !result ) throw -4;
4364 //
4365 };
4366 //
4367 } else {
4368 //
4369 if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered interval \n",section,fromtime,obt,pkt);
4370 // if ( PEDANTIC ) throw -74;
4371 //
4372 };
4373 //
4374 };
4375 };
4376 };
4377 //
4378 return(0);
4379 };
4380
4381
4382 /**
4383 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
4384 */
4385 Int_t PamelaDBOperations::insertCALOPULSE_CALIB(){
4386 //
4387 TSQLResult *result = 0;
4388 TSQLRow *row = 0;
4389 //
4390 stringstream oss;
4391 oss.str("");
4392 //
4393 oss << " DESCRIBE GL_CALOPULSE_CALIB;";
4394 if ( IsDebug() ) printf(" Check if the GL_CALOPULSE_CALIB table exists: query is \n %s \n",oss.str().c_str());
4395 result = conn->Query(oss.str().c_str());
4396 //
4397 if ( conn->GetErrorCode() ){
4398 if ( IsDebug() ) printf(" The GL_CALOPULSE_CALIB table does not exists! \n");
4399 throw -30;
4400 };
4401 //
4402 // CaloPulse1
4403 //
4404 CalibCalPulse1Event *cp1 = 0;
4405 TTree *tr = 0;
4406 EventHeader *eh = 0;
4407 PscuHeader *ph = 0;
4408 //
4409 UInt_t nevents = 0;
4410 UInt_t fromtime = 0;
4411 UInt_t totime = 0;
4412 UInt_t obt = 0;
4413 UInt_t pkt = 0;
4414 //
4415 tr = (TTree*)file->Get("CalibCalPulse1");
4416 if ( !tr || tr->IsZombie() ) throw -31;
4417 //
4418 tr->SetBranchAddress("CalibCalPulse1", &cp1);
4419 tr->SetBranchAddress("Header", &eh);
4420 nevents = tr->GetEntries();
4421 //
4422 if ( nevents > 0 ){
4423 //
4424 for (UInt_t i=0; i < nevents; i++){
4425 tr->GetEntry(i);
4426 for (UInt_t section = 0; section < 4; section++){
4427 //
4428 if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
4429 valid = 1;
4430 if ( cp1->pperror[section] ) valid = 0;
4431 ph = eh->GetPscuHeader();
4432 obt = ph->GetOrbitalTime();
4433 pkt = ph->GetCounter();
4434 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4435 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4436 // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
4437 //
4438 if ( IsDebug() ) printf(" Calo pulse1 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4439 //
4440 // check if the calibration has already been inserted
4441 //
4442 oss.str("");
4443 oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
4444 << " SECTION = "<< section << " AND "
4445 << " PULSE_AMPLITUDE = 0 AND "
4446 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4447 << " OBT = "<< obt << " AND "
4448 << " PKT = "<< pkt << ";";
4449 //
4450 if ( IsDebug() ) printf(" Check if the calo pulse1 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4451 result = conn->Query(oss.str().c_str());
4452 //
4453 if ( !result ) throw -4;
4454 //
4455 row = result->Next();
4456 //
4457 if ( row ){
4458 //
4459 if ( IsDebug() ) printf(" Calo pulse1 calibration already inserted in the DB\n");
4460 if ( PEDANTIC ) throw -75;
4461 //
4462 } else {
4463 //
4464 // we have to insert a new calibration, check where to place it
4465 //
4466 oss.str("");
4467 oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
4468 << " SECTION = "<< section << " AND "
4469 << " PULSE_AMPLITUDE = 0 AND "
4470 << " SECTION = "<< section << " AND "
4471 << " FROM_TIME < "<< fromtime << " AND "
4472 << " TO_TIME > "<< fromtime << ";";
4473 //
4474 if ( IsDebug() ) printf(" Check where to place the pulse1 calo calibration: query is \n %s \n",oss.str().c_str());
4475 result = conn->Query(oss.str().c_str());
4476 //
4477 if ( !result ) throw -4;
4478 //
4479 row = result->Next();
4480 //
4481 if ( !row ){
4482 //
4483 // no calibrations in the db contain our calibration
4484 //
4485 if ( IsDebug() ) printf(" Pulse1 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4486 if ( fromtime < 1150871000 ){ //1150866904
4487 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4488 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4489 };
4490 //
4491 oss.str("");
4492 oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
4493 << " PULSE_AMPLITUDE = 0 AND "
4494 << " SECTION = "<< section << " AND "
4495 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4496 //
4497 if ( IsDebug() ) printf(" Check the upper limit for pulse1 calibration: query is \n %s \n",oss.str().c_str());
4498 result = conn->Query(oss.str().c_str());
4499 //
4500 if ( !result ) throw -4;
4501 //
4502 row = result->Next();
4503 if ( !row ){
4504 totime = numeric_limits<UInt_t>::max();
4505 } else {
4506 totime = (UInt_t)atoll(row->GetField(0));
4507 };
4508 //
4509 } else {
4510 //
4511 // determine upper and lower limits and make space for the new calibration
4512 //
4513 totime = (UInt_t)atoll(row->GetField(1));
4514 //
4515 oss.str("");
4516 oss << " UPDATE GL_CALOPULSE_CALIB SET "
4517 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4518 << " ID = "<< row->GetField(0) << ";";
4519 //
4520 if ( IsDebug() ) printf(" Make space for the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
4521 result = conn->Query(oss.str().c_str());
4522 //
4523 if ( !result ) throw -4;
4524 //
4525 };
4526 //
4527 oss.str("");
4528 oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4529 << " VALUES (NULL,' "
4530 << idroot << "','"
4531 << i << "','"
4532 << fromtime << "','"
4533 << totime << "','"
4534 << section << "',NULL,'0','"
4535 << obt << "','"
4536 << pkt << "','"
4537 << this->GetBOOTnumber() << "','"
4538 << valid << "');";
4539 //
4540 if ( IsDebug() ) printf(" Insert the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
4541 //
4542 result = conn->Query(oss.str().c_str());
4543 //
4544 if ( !result ) throw -4;
4545 //
4546 };
4547 //
4548 } else {
4549 //
4550 if ( IsDebug() ) printf(" Pulse1 calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered time interval \n",section,fromtime,obt,pkt);
4551 // if ( PEDANTIC ) throw -76;
4552 //
4553 };
4554 //
4555 };
4556 };
4557 };
4558 };
4559 //
4560 // CaloPulse2
4561 //
4562 tr->Reset();
4563 CalibCalPulse2Event *cp2 = 0;
4564 tr = 0;
4565 //
4566 nevents = 0;
4567 fromtime = 0;
4568 totime = 0;
4569 obt = 0;
4570 pkt = 0;
4571 //
4572 tr = (TTree*)file->Get("CalibCalPulse2");
4573 if ( !tr || tr->IsZombie() ) throw -32;
4574 //
4575 tr->SetBranchAddress("CalibCalPulse2", &cp2);
4576 tr->SetBranchAddress("Header", &eh);
4577 nevents = tr->GetEntries();
4578 //
4579 if ( nevents > 0 ){
4580 //
4581 for (UInt_t i=0; i < nevents; i++){
4582 tr->GetEntry(i);
4583 for (UInt_t section = 0; section < 4; section++){
4584 //
4585 if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
4586 valid = 1;
4587 if ( cp2->pperror[section] ) valid = 0;
4588 ph = eh->GetPscuHeader();
4589 obt = ph->GetOrbitalTime();
4590 pkt = ph->GetCounter();
4591 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
4592 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
4593 // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
4594 //
4595 if ( IsDebug() ) printf(" Calo pulse2 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
4596 //
4597 // check if the calibration has already been inserted
4598 //
4599 oss.str("");
4600 oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
4601 << " SECTION = "<< section << " AND "
4602 << " PULSE_AMPLITUDE != 0 AND "
4603 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
4604 << " OBT = "<< obt << " AND "
4605 << " PKT = "<< pkt << ";";
4606 //
4607 if ( IsDebug() ) printf(" Check if the calo pulse2 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4608 result = conn->Query(oss.str().c_str());
4609 //
4610 if ( !result ) throw -4;
4611 //
4612 row = result->Next();
4613 //
4614 if ( row ){
4615 //
4616 if ( IsDebug() ) printf(" Calo pulse2 calibration already inserted in the DB\n");
4617 if ( PEDANTIC ) throw -77;
4618 //
4619 } else {
4620 //
4621 // we have to insert a new calibration
4622 //
4623 //
4624 // Determine the amplitude of the pulse
4625 //
4626 UInt_t pampli = 1;
4627 UInt_t pstrip = 0;
4628 UInt_t se = 0;
4629 if ( section == 1 ) se = 2;
4630 if ( section == 2 ) se = 3;
4631 if ( section == 3 ) se = 1;
4632 for (Int_t ii=0;ii<16;ii++){
4633 if ( cp2->calpuls[se][0][ii] > 10000. ){
4634 pampli = 2;
4635 pstrip = ii;
4636 };
4637 };
4638 if ( pampli == 1 ){
4639 Bool_t found = false;
4640 Float_t delta=0.;
4641 UInt_t cstr = 0;
4642 while ( !found && cstr < 16 ){
4643 for (Int_t ii=0;ii<16;ii++){
4644 delta = cp2->calpuls[se][0][ii] - cp2->calpuls[se][0][cstr];
4645 if ( IsDebug() ) printf(" cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
4646 if ( delta > 500. ){
4647 pampli = 1;
4648 pstrip = ii;
4649 found = true;
4650 if ( IsDebug() ) printf(" FOUND cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
4651 };
4652 };
4653 cstr++;
4654 };
4655 if ( !found ) pstrip = 100;
4656 };
4657 if ( IsDebug() ) printf(" The amplitude of the pulser is %u (where 1 = low pulse, 2 = high pulse), pulsed strip is %u \n",pampli,pstrip);
4658 //
4659 // we have to insert a new calibration, check where to place it
4660 //
4661 oss.str("");
4662 oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
4663 << " SECTION = "<< section << " AND "
4664 << " PULSE_AMPLITUDE = " << pampli << " AND "
4665 << " SECTION = "<< section << " AND "
4666 << " FROM_TIME < "<< fromtime << " AND "
4667 << " TO_TIME > "<< fromtime << ";";
4668 //
4669 if ( IsDebug() ) printf(" Check where to place the pulse2 calo calibration: query is \n %s \n",oss.str().c_str());
4670 result = conn->Query(oss.str().c_str());
4671 //
4672 if ( !result ) throw -4;
4673 //
4674 row = result->Next();
4675 //
4676 if ( !row ){
4677 //
4678 // no calibrations in the db contain our calibration
4679 //
4680 if ( IsDebug() ) printf(" Pulse2 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
4681 if ( fromtime < 1150871000 ){ //1150866904
4682 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
4683 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
4684 };
4685 //
4686 oss.str("");
4687 oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
4688 << " PULSE_AMPLITUDE = " << pampli << " AND "
4689 << " SECTION = "<< section << " AND "
4690 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4691 //
4692 if ( IsDebug() ) printf(" Check the upper limit for pulse2 calibration: query is \n %s \n",oss.str().c_str());
4693 result = conn->Query(oss.str().c_str());
4694 //
4695 if ( !result ) throw -4;
4696 //
4697 row = result->Next();
4698 if ( !row ){
4699 totime = numeric_limits<UInt_t>::max();
4700 } else {
4701 totime = (UInt_t)atoll(row->GetField(0));
4702 };
4703 //
4704 } else {
4705 //
4706 // determine upper and lower limits and make space for the new calibration
4707 //
4708 totime = (UInt_t)atoll(row->GetField(1));
4709 //
4710 oss.str("");
4711 oss << " UPDATE GL_CALOPULSE_CALIB SET "
4712 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4713 << " ID = "<< row->GetField(0) << ";";
4714 //
4715 if ( IsDebug() ) printf(" Make space for the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
4716 result = conn->Query(oss.str().c_str());
4717 //
4718 if ( !result ) throw -4;
4719 //
4720 };
4721 //
4722 // Fill the DB
4723 //
4724 oss.str("");
4725 // oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4726 oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
4727 << " VALUES (NULL,' "
4728 << idroot << "','"
4729 << i << "','"
4730 << fromtime << "','"
4731 << totime << "','"
4732 << section << "','"
4733 << pstrip << "','"
4734 << pampli << "','"
4735 << obt << "','"
4736 << pkt << "','"
4737 << this->GetBOOTnumber() << "','"
4738 << valid << "');";
4739 //
4740 if ( IsDebug() ) printf(" Insert the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
4741 //
4742 result = conn->Query(oss.str().c_str());
4743 //
4744 if ( !result ) throw -4;
4745 //
4746 };
4747 //
4748 } else {
4749 //
4750 if ( IsDebug() ) printf(" Pulse2 calo calibration for section %i at time %u obt %u pkt %u OUTSIDE the considered time interval \n",section,fromtime,obt,pkt);
4751 // if ( PEDANTIC ) throw -78;
4752 //
4753 };
4754 //
4755 };
4756 };
4757 };
4758 };
4759 //
4760 return(0);
4761 };
4762
4763 /**
4764 * Fill the GL_TRK_CALIB table
4765 */
4766 void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
4767
4768 GL_TRK_CALIB *glcal = new GL_TRK_CALIB();
4769 //
4770 glcal->ID = 0;
4771 glcal->ID_ROOT_L0 = GetID_ROOT();
4772 glcal->EV_ROOT_CALIBTRK1 = t1;
4773 glcal->EV_ROOT_CALIBTRK2 = t2;
4774 glcal->FROM_TIME = fromtime;
4775 glcal->TO_TIME = 0;
4776 glcal->OBT1 = obt1;
4777 glcal->OBT2 = obt2;
4778 glcal->PKT1 = pkt1;
4779 glcal->PKT2 = pkt2;
4780 glcal->BOOT_NUMBER = GetBOOTnumber();
4781 glcal->VALIDATION = valid;
4782 //
4783 HandleTRK_CALIB(glcal);
4784 //
4785 delete glcal;
4786 }
4787 /**
4788 * Fill the GL_TRK_CALIB table
4789 */
4790 void PamelaDBOperations::HandleTRK_CALIB(GL_TRK_CALIB *glcal){
4791
4792 Bool_t pk1 = (glcal->OBT1>0&&glcal->PKT1>0);
4793 Bool_t pk2 = (glcal->OBT2>0&&glcal->PKT2>0);
4794 UInt_t boot_number = glcal->BOOT_NUMBER;
4795 UInt_t obt1 = glcal->OBT1;
4796 UInt_t obt2 = glcal->OBT2;
4797 UInt_t pkt1 = glcal->PKT1;
4798 UInt_t pkt2 = glcal->PKT2;
4799 UInt_t fromtime = glcal->FROM_TIME;
4800 UInt_t totime = 0;
4801 UInt_t idroot = glcal->ID_ROOT_L0;
4802 UInt_t t1 = glcal->EV_ROOT_CALIBTRK1;
4803 UInt_t t2 = glcal->EV_ROOT_CALIBTRK2;
4804 UInt_t valid = glcal->VALIDATION;
4805 //
4806 TSQLResult *result = 0;
4807 TSQLRow *row = 0;
4808 //
4809 stringstream oss;
4810 oss.str("");
4811 //
4812 //
4813 if ( !pk1 && !pk2 ){
4814 if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
4815 return;
4816 };
4817 //
4818 // check if the calibration has already been inserted
4819 //
4820 oss.str("");
4821 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
4822 << " BOOT_NUMBER = "<< boot_number; //
4823 oss << " AND FROM_TIME="<<fromtime; /// NEWNEWNEW -- VA BENE ?!?!?!?!
4824 oss << " AND ( ( ";
4825 if ( pk1 ){
4826 oss << " OBT1 = "<< obt1 << " AND "
4827 << " PKT1 = "<< pkt1
4828 << " ) OR ( ";
4829 } else {
4830 oss << " PKT1 = "<< pkt2-1
4831 << " ) OR ( ";
4832 };
4833 if ( pk2 ){
4834 oss << " OBT2 = "<< obt2 << " AND "
4835 << " PKT2 = "<< pkt2;
4836 } else {
4837 oss << " PKT2 = "<< pkt1+1;
4838 };
4839 oss << " ) );";
4840 //
4841 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
4842 result = conn->Query(oss.str().c_str());
4843 //
4844 if ( !result ) throw -4;
4845 //
4846 row = result->Next();
4847 //
4848 if ( row ){
4849 //
4850 if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
4851 if ( PEDANTIC ) throw -80;
4852 //
4853 } else {
4854 //
4855 // we have to insert a new calibration, check where to place it
4856 //
4857 oss.str("");
4858 oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
4859 << " FROM_TIME < "<< fromtime << " AND "
4860 << " TO_TIME > "<< fromtime << ";";
4861 //
4862 if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
4863 result = conn->Query(oss.str().c_str());
4864 //
4865 if ( !result ) throw -4;
4866 //
4867 row = result->Next();
4868 //
4869 if ( !row ){
4870 //
4871 // no calibrations in the db contain our calibration
4872 //
4873 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
4874 if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
4875 //
4876 oss.str("");
4877 oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
4878 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
4879 //
4880 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
4881 result = conn->Query(oss.str().c_str());
4882 //
4883 if ( !result ) throw -4;
4884 //
4885 row = result->Next();
4886 if ( !row ){
4887 totime = numeric_limits<UInt_t>::max();
4888 } else {
4889 totime = (UInt_t)atoll(row->GetField(0));
4890 };
4891 //
4892 } else {
4893 //
4894 // determine upper and lower limits and make space for the new calibration
4895 //
4896 totime = (UInt_t)atoll(row->GetField(1));
4897 //
4898 oss.str("");
4899 oss << " UPDATE GL_TRK_CALIB SET "
4900 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
4901 << " ID = "<< row->GetField(0) << ";";
4902 //
4903 if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
4904 result = conn->Query(oss.str().c_str());
4905 //
4906 if ( !result ) throw -4;
4907 //
4908 };
4909 //
4910 oss.str("");
4911 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) "
4912 << " VALUES (NULL,' "
4913 << idroot << "',";
4914 //
4915 if ( !pk1 ){
4916 oss << "NULL,";
4917 } else {
4918 oss << "'"
4919 << t1 << "',";
4920 };
4921 //
4922 if ( !pk2 ){
4923 oss << "NULL,'";
4924 } else {
4925 oss << "'"
4926 << t2 << "','";
4927 };
4928 //
4929 oss << fromtime << "','"
4930 << totime << "','"
4931 << obt1 << "','"
4932 << pkt1 << "','"
4933 << obt2 << "','"
4934 << pkt2 << "','"
4935 << boot_number << "','"
4936 << valid << "');";
4937 //
4938 if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
4939 //
4940 result = conn->Query(oss.str().c_str());
4941 //
4942 if ( !result ) throw -4;
4943 //
4944 };
4945
4946 oss.str("");
4947 oss << " SELECT ID FROM GL_TRK_CALIB ORDER BY ID DESC LIMIT 1 ;";
4948 if ( IsDebug() ) cout << oss.str().c_str() << endl;
4949 result = conn->Query(oss.str().c_str());
4950 if ( !result ) throw -4;;
4951 row = result->Next();
4952 if(row)glcal->ID = (UInt_t)atoll(row->GetField(0));
4953 //
4954 delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
4955 //
4956 };
4957
4958 /**
4959 * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
4960 */
4961 Int_t PamelaDBOperations::insertTRK_CALIB(){
4962 //
4963 CalibTrk1Event *caltrk1 = 0;
4964 CalibTrk2Event *caltrk2 = 0;
4965 TTree *tr1 = 0;
4966 TTree *tr2 = 0;
4967 EventHeader *eh1 = 0;
4968 PscuHeader *ph1 = 0;
4969 EventHeader *eh2 = 0;
4970 PscuHeader *ph2 = 0;
4971 //
4972 PacketType *pctp=0;
4973 EventCounter *codt2=0;
4974 //
4975 Int_t nevents1 = 0;
4976 Int_t nevents2 = 0;
4977 //
4978 fromtime = 0;
4979 //
4980 obt1 = 0;
4981 pkt1 = 0;
4982 obt2 = 0;
4983 pkt2 = 0;
4984 //
4985 tr1 = (TTree*)file->Get("CalibTrk1");
4986 if ( !tr1 || tr1->IsZombie() ) throw -22;
4987 tr2 = (TTree*)file->Get("CalibTrk2");
4988 if ( !tr2 || tr2->IsZombie() ) throw -23;
4989 //
4990 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
4991 tr1->SetBranchAddress("Header", &eh1);
4992 nevents1 = tr1->GetEntries();
4993 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
4994 tr2->SetBranchAddress("Header", &eh2);
4995 nevents2 = tr2->GetEntries();
4996 //
4997 if ( !nevents1 && !nevents2 ) return(1);
4998 //
4999 t2 = -1;
5000 Int_t pret2 = 0;
5001 Int_t t2t1cal = 0;
5002 //
5003 bool MISSING_pkt1 = true;
5004 bool MISSING_pkt2 = true;
5005 int ncalib = 0;
5006 bool try_to_recover = false;
5007 //
5008 for (t1=0; t1 < nevents1; t1++){//loop over packet1
5009 //
5010 pret2 = t2;
5011 tr1->GetEntry(t1);
5012 //
5013 ph1 = eh1->GetPscuHeader();
5014 obt1 = ph1->GetOrbitalTime();
5015 pkt1 = ph1->GetCounter();
5016 fromtime = GetAbsTime(ph1->GetOrbitalTime());
5017 //
5018 // chek if the packet number and obt are consistent with the other packets ???
5019 //
5020 if ( PKT(pkt1) >= PKT(pktfirst) && PKT(pkt1) <= upperpkt && OBT(obt1) >= OBT(obtfirst) && OBT(obt1) <= upperobt ){
5021 // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
5022 //
5023 if ( IsDebug() ) printf("\n Trk calibration1 %u at time %u obt %u pkt %u \n",t1,fromtime,obt1,pkt1);
5024 //
5025 valid = ValidateTrkCalib( caltrk1, eh1 );
5026 if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
5027 //
5028 // Do we have the second calibration packet?
5029 //
5030 if ( IsDebug() ) cout << " Loop over calibration2 to search associated calibration: "<<endl;
5031 while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
5032 //
5033 t2++;
5034 //
5035 pret2 = t2 - 1; // EMILIANO
5036 //
5037 if ( t2 < nevents2 ){
5038 tr2->GetEntry(t2);
5039 codt2 = eh2->GetCounter();
5040 t2t1cal = codt2->Get(pctp->CalibTrk1);
5041 //
5042 ph2 = eh2->GetPscuHeader();
5043 obt2 = ph2->GetOrbitalTime();
5044 pkt2 = ph2->GetCounter();
5045 //
5046 if ( IsDebug() ) printf(" >> trk calibration2 at obt %u pkt %u t2 is %u , t2t1cal is %u \n",obt2,pkt2,t2,t2t1cal);
5047 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
5048 //
5049 } else {
5050 //
5051 // running out of vector without finding the corresponding calibration, sig
5052 //
5053 if ( IsDebug() ) printf(" t2 >= nevents2 \n");
5054 pret2 = t2;
5055 obt2 = 0;
5056 // pkt2 = pkt1+2;
5057 pkt2 = 0;
5058 t2t1cal = t1+1;
5059 };
5060 // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) && (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5061
5062 // EMILIANO
5063 // if ( (this->PKT(pkt2) < this->PKT(pktfirst) || this->PKT(pkt2) > upperpkt) || (this->OBT(obt2) < this->OBT(obtfirst) || this->OBT(obt2) > upperobt) ){
5064 // // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5065 // if ( IsDebug() ) printf(" running out of vector without finding the corresponding calibration, sig \n");
5066 // //
5067 // // running out of vector without finding the corresponding calibration, sig
5068 // //
5069 // pret2 = t2;
5070 // obt2 = 0;
5071 // // pkt2 = pkt1+2;
5072 // pkt2 = 0;
5073 // t2t1cal = t1+1;
5074 // };
5075
5076
5077 //
5078 };
5079 //
5080 if ( IsDebug() ) printf(" Check if trk calibration2 is the right one \n");
5081 //
5082 // EMILIANO
5083 if ( ( PKT(pkt2) < PKT(pktfirst) || PKT(pkt2) > upperpkt) || (OBT(obt2) < OBT(obtfirst) || OBT(obt2) > upperobt) ){
5084 // if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
5085 if ( IsDebug() ) printf(" *WARNING* The calibration found is outside the interval, sig \n");
5086 //
5087 // running out of vector without finding the corresponding calibration, sig
5088 //
5089 pret2 = t2;
5090 obt2 = 0;
5091 pkt2 = 0;
5092 };
5093 if ( PKT(pkt2) == PKT(pkt1)+1 ){
5094 if ( IsDebug() ) cout << " ...OK"<<endl;
5095 // =======================
5096 // The calibration is good
5097 // =======================
5098 //
5099 // if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
5100 // if ( IsDebug() ) printf(" Trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
5101 // if ( IsDebug() ) printf("\n Trk calibration2 at time %u obt %u pkt %u \n",fromtime,obt2,pkt2);
5102 if ( IsDebug() ) printf(" Trk calibration2 %u at time %u obt %u pkt %u \n",t2,fromtime,obt2,pkt2);
5103 //
5104 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
5105 if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
5106 // valid = valid & valid2;
5107 valid = valid & valid2; //QUESTO VA CAMBIATO
5108 //
5109 // Handle good calib
5110 //
5111 MISSING_pkt1 = false;
5112 MISSING_pkt2 = false;
5113 // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5114 //
5115 // Check for missing calibtrk1
5116 //
5117 if ( t2 != pret2+1 ){
5118 //
5119 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);
5120 //
5121 while ( t2 > pret2+1 ){
5122 //
5123 // handle missing calib1
5124 //
5125 pret2++;
5126 //
5127 obt1 = 0;
5128 pkt1 = 0;
5129 //
5130 tr2->GetEntry(pret2);
5131 ph2 = eh2->GetPscuHeader();
5132 obt2 = ph2->GetOrbitalTime();
5133 pkt2 = ph2->GetCounter();
5134 //
5135 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5136 //
5137 valid = 0;
5138 MISSING_pkt1 = true;
5139 MISSING_pkt2 = false;
5140 // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5141 //
5142 };
5143 //
5144 };
5145 //
5146 } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
5147 //
5148 // Check for missing calibtrk2
5149 //
5150 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5151 t2 = pret2;
5152 //
5153 // handle missing calib2
5154 //
5155 obt2 = 0;
5156 pkt2 = 0;
5157 valid = 0;
5158 MISSING_pkt1 = false;
5159 MISSING_pkt2 = true;
5160 // this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5161 //
5162 };
5163 //
5164
5165 if( !(MISSING_pkt1&MISSING_pkt2) ){
5166 this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5167 ncalib++;
5168 if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5169 }
5170
5171
5172 } else {
5173 //
5174 if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u OUTSIDE the considered time interval \n",fromtime,obt1,pkt1);
5175 // if ( PEDANTIC ) throw -79;
5176 //
5177 };
5178 //
5179
5180 }; //end loop on pkt1
5181
5182
5183
5184 //
5185 // we have one more calib pkt2 !
5186 //
5187 t2++;
5188 while ( t2 < nevents2 ){
5189 //
5190 // handle missing calib1
5191 //
5192 if ( IsDebug() ) printf(" t2 is %u nevents2 is %u \n",t2,nevents2);
5193 obt1 = 0;
5194 pkt1 = 0;
5195 //
5196 tr2->GetEntry(t2);
5197 ph2 = eh2->GetPscuHeader();
5198 obt2 = ph2->GetOrbitalTime();
5199 pkt2 = ph2->GetCounter();
5200 //
5201 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
5202 valid = 0;
5203 // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->PKT(pkt1) <= upperpkt && this->OBT(obt1) >= this->OBT(obtfirst) && this->OBT(obt1) <= upperobt ){
5204 // EMILIANO
5205 if ( this->PKT(pkt2) >= this->PKT(pktfirst) && this->PKT(pkt2 <= upperpkt) && this->OBT(obt2) >= this->OBT(obtfirst) && this->OBT(obt2) <= upperobt ){
5206 // if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
5207 //
5208 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
5209 //
5210 MISSING_pkt1 = true;
5211 MISSING_pkt2 = false;
5212 this->HandleTRK_CALIB(!MISSING_pkt1,!MISSING_pkt2);
5213 ncalib++;
5214 if( MISSING_pkt1||MISSING_pkt2||!valid )try_to_recover=true;
5215 //
5216 };
5217 //
5218 t2++;
5219 //
5220 };
5221
5222 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5223 // -----------------------------------------------------------------
5224 // in case of corruption, check if the calibration can be recovered
5225 // from another chewbacca file
5226 // -----------------------------------------------------------------
5227 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5228
5229 // cout <<" TRY TO RECOVER ?? "<<try_to_recover<<endl;
5230
5231 if(chewbacca&&try_to_recover){
5232
5233
5234 if ( IsDebug() ) cout << endl << ">>>> TRY TO RECOVER TRACKER CALIBRATIONS <<<<"<<endl;
5235
5236 TSQLResult *result = 0;
5237 TSQLRow *row = 0;
5238 //
5239 stringstream oss;
5240 oss.str("");
5241 //
5242
5243 ////////////////////////////////////////////////////////////////////////
5244 // retrieve the name of the current file:
5245 ////////////////////////////////////////////////////////////////////////
5246 oss.str("");
5247 oss << "SELECT NAME FROM GL_ROOT where ID=" << GetID_ROOT() <<";";
5248 if ( IsDebug() ) cout << oss.str().c_str() << endl;
5249
5250 result = conn->Query(oss.str().c_str());
5251 if ( !result ) throw -4;;
5252 row = result->Next();
5253 TString thisfilename = (TString)row->GetField(0);
5254 if ( IsDebug() ) cout << "Current file ==> "<<thisfilename<<endl;
5255
5256 ////////////////////////////////////////////////////////////////////////
5257 // read all the calibrations inserted
5258 ////////////////////////////////////////////////////////////////////////
5259 oss.str("");
5260 oss << " SELECT ";
5261 oss << " ID,FROM_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,TO_TIME";
5262 oss << " FROM GL_TRK_CALIB ";
5263 oss << " ORDER BY ID DESC LIMIT "<<ncalib<<"; ";
5264 if ( IsDebug() ) cout << oss.str().c_str() << endl;
5265
5266 result = conn->Query(oss.str().c_str());
5267 if ( !result ) throw -4;;
5268 if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5269
5270 // -----------------------------------
5271 // loop over calibrations ...
5272 // -----------------------------------
5273 UInt_t nn=0;
5274 do {
5275 row = result->Next();
5276 if(!row)break;
5277
5278 UInt_t id = (UInt_t)atoll(row->GetField(0));
5279 UInt_t fromtime = (UInt_t)atoll(row->GetField(1));
5280 UInt_t obt1 = (UInt_t)atoll(row->GetField(2));
5281 UInt_t pkt1 = (UInt_t)atoll(row->GetField(3));
5282 UInt_t obt2 = (UInt_t)atoll(row->GetField(4));
5283 UInt_t pkt2 = (UInt_t)atoll(row->GetField(5));
5284 UInt_t boot = (UInt_t)atoll(row->GetField(6));
5285 UInt_t valid = (UInt_t)atoll(row->GetField(7));
5286 bool MISSING_pkt1 = (row->GetFieldLength(8)==0);
5287 bool MISSING_pkt2 = (row->GetFieldLength(9)==0);
5288 UInt_t totime = (UInt_t)atoll(row->GetField(10));
5289
5290 // -------------------------------------
5291 // ...check if the entry is corrupted...
5292 // -------------------------------------
5293 cout <<"*** "<< MISSING_pkt1 << MISSING_pkt2 << valid <<endl;
5294 bool CORRUPTED = (MISSING_pkt1||MISSING_pkt2||!valid);
5295
5296 if ( IsDebug() ) cout << "("<<nn<<") ID = "<<id<<" from GL_TRK_CALIB ==> corrupted ? "<<CORRUPTED<<endl;
5297
5298 // if( !CORRUPTED )continue; // nothing to do
5299
5300 /////////////////////////////////////////////////////////
5301 // if it is corrupted, ...look for ather chewbacca files
5302 // containing the same calibrations ...
5303 /////////////////////////////////////////////////////////
5304
5305 bool this_MISSING_pkt1 = false;
5306 bool this_MISSING_pkt2 = false;
5307 int this_t1=0;
5308 int this_t2=0;;
5309 UInt_t this_valid = 0;
5310
5311 TString path = "";
5312 TString name = "";
5313 TString raw = "";
5314 UInt_t obt0 = 0;
5315 UInt_t timesync = 0;
5316 UInt_t boot_number = 0;
5317 bool FOUND = false;
5318
5319 if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5320
5321 // for(int itable=0; itable<2; itable++){
5322 for(int itable=0; itable<1; itable++){
5323
5324 // ------------------------------------------------------
5325 // loop over both ROOT_TABLE and ROOT_TABLE_BAD
5326 // ------------------------------------------------------
5327
5328 TString table = "ROOT_TABLE";
5329 if(itable==1)table = "ROOT_TABLE_BAD";
5330
5331 oss.str("");
5332 oss << " SELECT ";
5333 oss << " FOLDER_NAME,FILE_NAME,OBT_TIME_SYNC,LAST_TIME_SYNC_INFO,BOOT_NUMBER,INPUT_NAME ";
5334 oss << " FROM "<<table;
5335 oss << " WHERE 1 " << endl;
5336 oss << " AND FILE_NAME != \""<< thisfilename<<"\"";
5337 if( !MISSING_pkt1 ){
5338 oss << " AND ";
5339 oss << " PKT_NUMBER_INIT < "<<pkt1;
5340 oss << " AND ";
5341 oss << " PKT_NUMBER_FINAL > "<<pkt1;
5342 oss << " AND ";
5343 oss << " PKT_OBT_INIT < "<<obt1;
5344 oss << " AND ";
5345 oss << " PKT_OBT_FINAL > "<<obt1;
5346 }else{
5347 if(pkt2>1) pkt1 = pkt2-1;//serve dopo
5348 }
5349 if( !MISSING_pkt2 ){
5350 oss << " AND ";
5351 oss << " PKT_NUMBER_INIT < "<<pkt2;
5352 oss << " AND ";
5353 oss << " PKT_NUMBER_FINAL > "<<pkt2;
5354 oss << " AND ";
5355 oss << " PKT_OBT_INIT < "<<obt2;
5356 oss << " AND ";
5357 oss << " PKT_OBT_FINAL > "<<obt2;
5358 }else{
5359 if(pkt1>0) pkt2 = pkt1+1;//serve dopo
5360 }
5361 if( boot> 0 ){
5362 oss << " AND ";
5363 oss << " BOOT_NUMBER = "<<boot;
5364 }else{
5365 }
5366 oss << " ORDER BY BAD_PKT_CALREAD ASC; ";
5367
5368 TSQLResult *result2 = 0;
5369 TSQLRow *row2 = 0;
5370
5371 if ( IsDebug() ) cout << oss.str().c_str() << endl;
5372 result2 = conn->Query(oss.str().c_str());
5373 if ( !result2 ) throw -4;;
5374 if ( IsDebug() ) cout <<"Rows: "<<result2->GetRowCount()<<endl;
5375
5376 // ------------------------------------------------------
5377 // loop over files containing repetition (if any)
5378 // ------------------------------------------------------
5379 do {
5380 row2 = result2->Next();
5381 if(!row2)break;
5382
5383 // ------------------------------------------------------
5384 // ... a repetition is found ...
5385 // ------------------------------------------------------
5386 path = (TString)row2->GetField(0);
5387 name = (TString)row2->GetField(1);
5388 raw = (TString)row2->GetField(5);
5389 obt0 = (UInt_t)atoll(row2->GetField(2));
5390 timesync = (UInt_t)atoll(row2->GetField(3));
5391 boot_number = (UInt_t)atoll(row2->GetField(4));
5392
5393 if ( IsDebug() ) cout << "- - - - - - - - - - -" <<endl;
5394 // cout << path <<endl;
5395 // cout << "File : " <<name <<endl;
5396 // cout << obt0 <<endl;
5397 // cout << timesync <<endl;
5398 // cout << "boot n. : "<<boot_number <<endl;
5399 // cout << raw <<endl;
5400
5401 // ------------------------------------------------------
5402 // ... retrieve the calibration packets.
5403 // ------------------------------------------------------
5404 if ( IsDebug() ) printf(" file is %s/%s \n",((TString)gSystem->ExpandPathName(path.Data())).Data(),name.Data());
5405 TFile *file = new TFile(((TString)gSystem->ExpandPathName(path.Data()))+"/"+name); // EM, path could be symbolic and we must expand it
5406 if(!file)throw -100;
5407 if(file->IsZombie())throw -100;
5408 //
5409 tr1 = (TTree*)file->Get("CalibTrk1");
5410 if ( !tr1 || tr1->IsZombie() ) throw -22;
5411 tr2 = (TTree*)file->Get("CalibTrk2");
5412 if ( !tr2 || tr2->IsZombie() ) throw -23;
5413 //
5414 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
5415 tr1->SetBranchAddress("Header", &eh1);
5416 nevents1 = tr1->GetEntries();
5417 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
5418 tr2->SetBranchAddress("Header", &eh2);
5419 nevents2 = tr2->GetEntries();
5420 for(this_t1=0; this_t1<nevents1; this_t1++){
5421 tr1->GetEntry(this_t1);
5422 if(
5423 (UInt_t)eh1->GetPscuHeader()->GetCounter() == pkt1 &&
5424 true) break;
5425 this_MISSING_pkt1 = true;
5426 }
5427 for(this_t2=0; this_t2<nevents2; this_t2++){
5428 tr2->GetEntry(this_t2);
5429 if(
5430 (UInt_t)eh2->GetPscuHeader()->GetCounter() == pkt2 &&
5431 true) break;
5432 this_MISSING_pkt2 = true;
5433 }
5434 this_valid =
5435 ValidateTrkCalib( caltrk1, eh1, file )
5436 *
5437 ValidateTrkCalib( caltrk2, eh2, file );
5438
5439 // ---------------------------------------------------------------------
5440 // accept the calibration if it is better than the previous:
5441 //
5442 // - if the new calibration is perfect (both valid packets)
5443 // - if the new calibration has both the packets and the previous does not
5444 // ---------------------------------------------------------------------
5445 if(
5446 ( !this_MISSING_pkt1&&!this_MISSING_pkt2&&this_valid )||
5447 ( (MISSING_pkt1||MISSING_pkt2) && (!this_MISSING_pkt1&&!this_MISSING_pkt2) )||
5448 false)FOUND=true;
5449
5450 if(file)file->Close();
5451
5452 if(FOUND)break;
5453
5454 }while(1);//endl loop over root table entries
5455
5456 if(FOUND)break;
5457
5458 }//end loop over tables
5459
5460 if(FOUND){
5461
5462 if ( IsDebug() ) cout << " >>> REPETITION FOUND :-) <<<" <<endl;
5463
5464 ////////////////////////////////////////////
5465 // insert a new entry in GL_TRK_CALIB and
5466 // modify the time-tag of the previous one
5467 ////////////////////////////////////////////
5468
5469 // ---------------------------------------------------------------------
5470 // step 1: insert a new raw file in GL_RAW
5471 // ---------------------------------------------------------------------
5472 //
5473 // check if the raw file already exist
5474 //
5475 UInt_t id_raw = 0; // EM GL_RAW is there only for backward compatibility so we do not need to fill it when in "chewbacca" mode
5476 // oss.str("");
5477 // oss << "SELECT ID FROM GL_RAW where NAME=\"" << gSystem->BaseName(raw.Data()) <<"\";";
5478 // if ( IsDebug() ) cout << oss.str().c_str() << endl;
5479
5480 // result = conn->Query(oss.str().c_str());
5481 // if ( !result ) throw -4;;
5482 // if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5483 // if( result->GetRowCount() == 0){
5484 // if ( IsDebug() ) cout << " << Insert new RAW file >> "<<endl;
5485 // // - - - - - - - - - - -
5486 // // insert new raw file
5487 // // - - - - - - - - - - -
5488 // GL_RAW glraw = GL_RAW();
5489 // glraw.PATH = gSystem->DirName(raw.Data());
5490 // glraw.NAME = gSystem->BaseName(raw.Data());
5491 // glraw.BOOT_NUMBER = boot_number;
5492 // //
5493 // insertPamelaRawFile( &glraw );
5494 // //
5495 // id_raw = glraw.ID;
5496 // }else{
5497 // row = result->Next();
5498 // id_raw = (UInt_t)atoll(row->GetField(0));
5499 // }
5500 // if ( IsDebug() ) cout << "ID_RAW = "<<id_raw<<endl;
5501
5502 // ---------------------------------------------------------------------
5503 // step 1(bis): retrieve the timesync id associated to the file
5504 // (NB, uso lo stesso associato al file iniziale)
5505 // ---------------------------------------------------------------------
5506 UInt_t idtimesync = 0;
5507 oss.str("");
5508 oss << " SELECT ID FROM GL_TIMESYNC where TIMESYNC="<<chlastts<<" AND OBT0="<<chobtts*1000<<" limit 1;";
5509 if ( debug ) printf(" %s \n",oss.str().c_str());
5510 result = conn->Query(oss.str().c_str());
5511 if ( !result ) throw -3;
5512 row = result->Next();
5513 if ( !row ) throw -3;
5514 idtimesync = (UInt_t)atoll(row->GetField(0));
5515 if ( IsDebug() ) cout << "ID_TIMESYNC = "<<idtimesync<<endl;
5516
5517 delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5518 // ---------------------------------------------------------------------
5519 // step 2: insert a new root file in GL_ROOT
5520 // ---------------------------------------------------------------------
5521 //
5522 // check if the root file already exist
5523 //
5524 UInt_t id_root = 0;
5525 oss.str("");
5526 oss << "SELECT ID FROM GL_ROOT where NAME=\"" << gSystem->BaseName(name.Data()) <<"\";";
5527 if ( IsDebug() ) cout << oss.str().c_str() << endl;
5528
5529 result = conn->Query(oss.str().c_str());
5530 if ( !result ) throw -4;;
5531 if ( IsDebug() ) cout <<"Rows: "<<result->GetRowCount()<<endl;
5532 if( result->GetRowCount() == 0){
5533 if ( IsDebug() ) cout << " << Insert new ROOT file >> "<<endl;
5534 // - - - - - - - - - - -
5535 // insert new root file
5536 // - - - - - - - - - - -
5537 GL_ROOT glroot = GL_ROOT();
5538 glroot.ID_RAW = id_raw;
5539 glroot.ID_TIMESYNC = idtimesync;
5540 //
5541 // EM STATIC = the full expanded path must be put in the DB, KEEPENV = the path given as input (or found in ROOT_TABLE) must be used,
5542 // NOT STATIC NOT KEEPENV = $PAM_L0 must be used in the DB
5543 //
5544 if ( STATIC ){
5545 glroot.PATH = (TString)gSystem->ExpandPathName(path);
5546 } else {
5547 if ( KEEPENV ){
5548 glroot.PATH = path;
5549 } else {
5550 glroot.PATH = "$PAM_L0";
5551 };
5552 };
5553 // glroot.PATH = path;
5554 glroot.NAME = name;
5555 //
5556 insertPamelaRootFile( &glroot );
5557 //
5558 id_root = glroot.ID;
5559 }else{
5560 row = result->Next();
5561 if(row)id_root = (UInt_t)atoll(row->GetField(0));
5562 }
5563 if ( IsDebug() ) cout << "ID_ROOT = "<<id_root<<endl;
5564
5565 delete result;// mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5566 // ---------------------------------------------------------------------
5567 // step 3: modify time-tag of corrupted GL_TRK_CALIB entry
5568 // ---------------------------------------------------------------------
5569 if ( IsDebug() ) cout << " << Modify time-tag of calibration ID="<<id<<" >> "<<endl;
5570 oss.str("");
5571 oss << " UPDATE GL_TRK_CALIB SET "
5572 << " TO_TIME=0 , FROM_TIME=0 WHERE "
5573 << " ID = "<< id << ";";
5574 if ( IsDebug() ) cout << oss.str().c_str() << endl;
5575 result = conn->Query(oss.str().c_str());
5576 if ( !result ) throw -4;;
5577
5578 delete result; // mmm... "Error in <TMySQLResult::Next>: result set closed" solved? 090112 [8RED error messages]
5579
5580 // ---------------------------------------------------------------------
5581 // step 4: insert the new calibration:
5582 // ---------------------------------------------------------------------
5583 if ( IsDebug() ) cout << " << Insert new TRK calibration >> "<<endl;
5584 //
5585 GL_TRK_CALIB glcal = GL_TRK_CALIB();
5586 //
5587 glcal.ID_ROOT_L0 = id_root;
5588 glcal.EV_ROOT_CALIBTRK1 = this_t1;
5589 glcal.EV_ROOT_CALIBTRK2 = this_t2;
5590 glcal.FROM_TIME = fromtime;
5591 glcal.TO_TIME = totime;
5592 glcal.OBT1 = obt1;
5593 glcal.OBT2 = obt2;
5594 glcal.PKT1 = pkt1;
5595 glcal.PKT2 = pkt1;
5596 glcal.BOOT_NUMBER = GetBOOTnumber();
5597 glcal.VALIDATION = this_valid;
5598 //
5599 HandleTRK_CALIB(&glcal);
5600 if ( IsDebug() ) cout << "ID = "<<glcal.ID<<endl;
5601 //
5602
5603 }
5604 if ( IsDebug() ) cout << "------------------------------------------------------------" <<endl;
5605
5606 }while(1);//end loop over calibrations
5607
5608
5609 if( result )delete result;
5610 if( row )delete row;
5611
5612
5613
5614
5615
5616
5617 }
5618
5619
5620 // // ------------------------------
5621 // // try to recover the calibration
5622 // // ------------------------------
5623 // cout << "TRY TO RECOVER TRACKER CALIBRATION"<<endl;
5624 // //
5625 // ULong64_t time = 0; //absolute time
5626 // string path[100]; //mettere un limite massimo
5627 // int nrows = 0;
5628 // UInt_t pkt = 0;
5629 // UInt_t obt = 0;
5630 // char *type = "";
5631 // EventHeader *eh = new EventHeader();
5632 // CalibTrk1Event *c = new CalibTrk1Event();
5633
5634 // //
5635 // if(which_is_not_valid==1 || which_is_not_valid==3){
5636 // //
5637 // cout << "PKT1 --> missing or corrupted "<<endl;
5638 // type = "CalibTrk1";
5639 // pkt = pkt1;
5640 // obt = obt1;
5641 // time = this->GetAbsTime(obt1);
5642 // if( pkt1 == 0 ){//missing
5643 // time = this->GetAbsTime(obt2);
5644 // pkt = pkt2-1;
5645 // }
5646 // //
5647 // }else if (which_is_not_valid==2 || which_is_not_valid==3){
5648 // //
5649 // cout << "PKT2--> missing or corrupted "<<endl;
5650 // type = "CalibTrk2 ";
5651 // pkt = pkt2;
5652 // obt = obt2;
5653 // time = this->GetAbsTime(obt2);
5654 // if( pkt2 == 0 ){//missing
5655 // time = this->GetAbsTime(obt1);
5656 // pkt = pkt1+1;
5657 // }
5658 // //
5659 // }else{
5660 // cout << "this should not happen!!! "<<endl;
5661 // trow -666;
5662 // }
5663
5664 // nrows = Query_ROOT_TABLE(time,conn,path);// get the list of file which might contain the packet
5665
5666
5667 // for(int r=0; r<nrows; r++){ //loop over rows
5668 // if(path)cout << r << " >>>> "<<(path+r)->c_str() << endl;
5669 // /// verifica che il file non sia quello gia` aperto
5670 // }
5671
5672 // ////////////////////////////////////////////////////////////////////////
5673
5674 // TSQLResult *result = 0;
5675 // TSQLRow *row = 0;
5676 // //
5677 // stringstream oss;
5678 // oss.str("");
5679 // // ----------------------------------------
5680 // // read the id of last calibration inserted
5681 // // ----------------------------------------
5682 // oss.str("");
5683 // oss << " SELECT ";
5684 // oss << " (ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,PKT1,OBT2,PKT2,BOOT_NUMBER,VALIDATION) ";
5685 // oss << " ORDER BY ID DESC LIMIT 1; ";
5686
5687 // result = conn->Query(oss.str().c_str());
5688 // row = result->Next();
5689 // if( !row )throw -666;
5690
5691 // if( result )delete result;
5692 // if( row )delete row;
5693
5694 // UInt_t id = (UInt_t)atoll(row->GetField(0));
5695
5696 // // -------------------------------------
5697 // // ...and modify it with new parameters
5698 // // -------------------------------------
5699
5700
5701 // }
5702 //
5703 return(0);
5704 };
5705
5706
5707 /**
5708 * Scan S4 calibrations packets, fill the GL_S4_CALIB table
5709 */
5710 Int_t PamelaDBOperations::insertS4_CALIB(){
5711 //
5712 TSQLResult *result = 0;
5713 TSQLRow *row = 0;
5714 //
5715 stringstream oss;
5716 oss.str("");
5717 //
5718 TTree *tr = 0;
5719 EventHeader *eh = 0;
5720 PscuHeader *ph = 0;
5721 //
5722 UInt_t nevents = 0;
5723 UInt_t fromtime = 0;
5724 UInt_t totime = 0;
5725 UInt_t obt = 0;
5726 UInt_t pkt = 0;
5727 //
5728 tr = (TTree*)file->Get("CalibS4");
5729 if ( !tr || tr->IsZombie() ) throw -24;
5730 //
5731 tr->SetBranchAddress("Header", &eh);
5732 //
5733 nevents = tr->GetEntries();
5734 //
5735 if ( !nevents ) return(1);
5736 //
5737 for (UInt_t i = 0; i < nevents; i++){
5738 //
5739 tr->GetEntry(i);
5740 //
5741 ph = eh->GetPscuHeader();
5742 obt = ph->GetOrbitalTime();
5743 pkt = ph->GetCounter();
5744 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
5745 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->PKT(pkt) <= upperpkt && this->OBT(obt) >= this->OBT(obtfirst) && this->OBT(obt) <= upperobt ){
5746 // if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
5747 //
5748 if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
5749 //
5750 // check if the calibration has already been inserted
5751 //
5752 oss.str("");
5753 oss << " SELECT ID FROM GL_S4_CALIB WHERE "
5754 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
5755 << " OBT = "<< obt << " AND "
5756 << " PKT = "<< pkt << ";";
5757 //
5758 if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
5759 result = conn->Query(oss.str().c_str());
5760 //
5761 if ( !result ) throw -4;
5762 //
5763 row = result->Next();
5764 //
5765 if ( row ){
5766 //
5767 if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
5768 if ( PEDANTIC ) throw -81;
5769 //
5770 } else {
5771 //
5772 // we have to insert a new calibration, check where to place it
5773 //
5774 oss.str("");
5775 oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
5776 << " FROM_TIME < "<< fromtime << " AND "
5777 << " TO_TIME > "<< fromtime << ";";
5778 //
5779 if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
5780 result = conn->Query(oss.str().c_str());
5781 //
5782 if ( !result ) throw -4;
5783 //
5784 row = result->Next();
5785 //
5786 if ( !row ){
5787 //
5788 // no calibrations in the db contain our calibration
5789 //
5790 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
5791 if ( fromtime < 1150871000 ){
5792 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
5793 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
5794 };
5795 //
5796 oss.str("");
5797 oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
5798 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
5799 //
5800 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
5801 result = conn->Query(oss.str().c_str());
5802 //
5803 if ( !result ) throw -4;
5804 //
5805 row = result->Next();
5806 if ( !row ){
5807 totime = numeric_limits<UInt_t>::max();
5808 } else {
5809 totime = (UInt_t)atoll(row->GetField(0));
5810 };
5811 //
5812 } else {
5813 //
5814 // determine upper and lower limits and make space for the new calibration
5815 //
5816 totime = (UInt_t)atoll(row->GetField(1));
5817 //
5818 oss.str("");
5819 oss << " UPDATE GL_S4_CALIB SET "
5820 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
5821 << " ID = "<< row->GetField(0) << ";";
5822 //
5823 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
5824 result = conn->Query(oss.str().c_str());
5825 //
5826 if ( !result ) throw -4;
5827 //
5828 };
5829 //
5830 oss.str("");
5831 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
5832 << " VALUES (NULL,' "
5833 << idroot << "','"
5834 << i << "','"
5835 << fromtime << "','"
5836 << totime << "','"
5837 << obt << "','"
5838 << pkt << "','"
5839 << this->GetBOOTnumber() << "');";
5840 //
5841 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
5842 //
5843 result = conn->Query(oss.str().c_str());
5844 //
5845 if ( !result ) throw -4;
5846 //
5847 };
5848 //
5849 } else {
5850 //
5851 if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u OUTSIDE the considered time interval\n",fromtime,obt,pkt);
5852 // if ( PEDANTIC ) throw -82;
5853 //
5854 };
5855 //
5856 };
5857 //
5858 return(0);
5859 };
5860
5861 /**
5862 * Scan the fragment table and move old fragments to the GL_RUN table
5863 */
5864 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
5865 return(this->CleanGL_RUN_FRAGMENTS(""));
5866 };
5867
5868 /**
5869 * Scan the fragment table and move old fragments to the GL_RUN table
5870 */
5871 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(Bool_t runpieces){
5872 return(this->CleanGL_RUN_FRAGMENTS("",runpieces));
5873 };
5874
5875 /**
5876 * Scan the fragment table and move old fragments to the GL_RUN table
5877 */
5878 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
5879 return(this->CleanGL_RUN_FRAGMENTS("",false));
5880 };
5881
5882 /**
5883 * Scan the fragment table and move old fragments to the GL_RUN table
5884 */
5885 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile, Bool_t runpieces){
5886 //
5887 TSQLResult *nresult = 0;
5888 TSQLRow *nrow = 0;
5889 TSQLResult *nresult1 = 0;
5890 TSQLRow *nrow1 = 0;
5891 TSQLResult *result = 0;
5892 TSQLRow *row = 0;
5893 TSQLResult *result2 = 0;
5894 TSQLRow *row2 = 0;
5895 //
5896 UInt_t moved = 0;
5897 //
5898 stringstream oss;
5899 oss.str("");
5900 //
5901 // Before moving blindly the runs from GL_RUN_FRAGMENTS to GL_RUN try to find out if we have runs divided in more than two pieces (chewbacca or explicit flag only)
5902 //
5903 if ( runpieces ){
5904 //
5905 UInt_t nid = 0;
5906 UInt_t myid[500];
5907 memset(myid,0,500*sizeof(UInt_t));
5908 //
5909 oss.str("");
5910 oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE INSERT_TIME <= '" << clean_time->AsSQLString() << "'order BY RUNHEADER_TIME asc;";
5911 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
5912 nresult = conn->Query(oss.str().c_str());
5913 //
5914 if ( nresult ){
5915 //
5916 nrow = nresult->Next();
5917 //
5918 while ( nrow ){
5919 //
5920 UInt_t mbo = (UInt_t)atoll(nrow->GetField(3));
5921 UInt_t mrhp = (UInt_t)atoll(nrow->GetField(2));
5922 UInt_t mrht = (UInt_t)atoll(nrow->GetField(1));
5923 Bool_t anr = true;
5924 Bool_t runisthere = true;
5925 //
5926 for (UInt_t u=0; u<=nid; u++){
5927 if ( (UInt_t)atoll(nrow->GetField(0)) == myid[u] && (UInt_t)atoll(nrow->GetField(0)) != 0 ) runisthere = false;
5928 };
5929 //
5930 // check if the run is still in the fragment table or if we have just move it in the gl_run table!
5931 //
5932 if ( runisthere ){
5933 //
5934 memset(myid,0,500*sizeof(UInt_t));
5935 nid = 0;
5936 myid[nid] = (UInt_t)atoll(nrow->GetField(0));
5937 //
5938 while ( anr ){
5939 //
5940 oss.str("");
5941 oss << "SELECT ID,RUNTRAILER_TIME,RUNTRAILER_PKT,BOOT_NUMBER FROM GL_RUN_FRAGMENTS WHERE BOOT_NUMBER=" << mbo << " AND RUNHEADER_PKT=" << mrhp << "+1 AND ABS(RUNHEADER_TIME-"<< mrht <<")<=1 AND INSERT_TIME <= '" << clean_time->AsSQLString() << "' order BY RUNHEADER_TIME asc;";
5942 if ( IsDebug() ) printf(" In the loop searching for fragmented runs : query is \n %s \n",oss.str().c_str());
5943 //
5944 nresult1 = conn->Query(oss.str().c_str());
5945 //
5946 if ( nresult1 ){
5947 //
5948 if ( nresult1->GetRowCount() == 1 ){
5949 //
5950 // one piece is found
5951 //
5952 nrow1 = nresult1->Next();
5953 //
5954 if ( nrow1 ){
5955 //
5956 nid++;
5957 myid[nid] = (UInt_t)atoll(nrow1->GetField(0));
5958 mbo = (UInt_t)atoll(nrow1->GetField(3));
5959 mrhp = (UInt_t)atoll(nrow1->GetField(2));
5960 mrht = (UInt_t)atoll(nrow1->GetField(1));
5961 if ( debug ) printf(" FOUND A PIECE OF RUN! nid %u myid[nid] %u mbo %u mrhp %u mrht %u \n",nid,myid[nid],mbo,mrhp,mrht);
5962 //
5963 nrow1->Close();
5964 } else {
5965 throw -88;
5966 };
5967 } else {
5968 anr = false;
5969 };
5970 nresult1->Close();
5971 } else {
5972 throw -88;
5973 };
5974 };
5975 //
5976 // handle these runs which are ordered and "good". Does the first contain a valid runheader?
5977 //
5978 oss.str("");
5979 oss << " ID= "<< myid[0];
5980 //
5981 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
5982 //
5983 if ( glrun->GetACQ_BUILD_INFO() != 0 ){
5984 //
5985 // the first piece contains a good runheader we can update all the other runs with correct infos!
5986 //
5987 for (UInt_t u=1; u <= nid ; u++){
5988 oss.str("");
5989 oss << "UPDATE GL_RUN_FRAGMENTS SET "
5990 << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , "
5991 << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
5992 << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT()<< " , "
5993 << " COMPILATIONTIMESTAMP=" << glrun->GetCOMPILATIONTIMESTAMP()<< " , "
5994 << " FAV_WRK_SCHEDULE=" << glrun->GetFAV_WRK_SCHEDULE()<< " , "
5995 << " EFF_WRK_SCHEDULE=" << glrun->GetEFF_WRK_SCHEDULE()<< " , "
5996 << " PRH_VAR_TRG_MODE_A=" << glrun->GetPRH_VAR_TRG_MODE_A()<< " , "
5997 << " PRH_VAR_TRG_MODE_B=" << glrun->GetPRH_VAR_TRG_MODE_B()<< " , "
5998 << " ACQ_BUILD_INFO=" << glrun->GetACQ_BUILD_INFO()<< " , "
5999 << " ACQ_VAR_INFO=" << glrun->GetACQ_VAR_INFO()<< " , "
6000 << " RM_ACQ_AFTER_CALIB=" << glrun->GetRM_ACQ_AFTER_CALIB()<< " , "
6001 << " RM_ACQ_SETTING_MODE=" << glrun->GetRM_ACQ_SETTING_MODE()<< " , "
6002 << " TRK_CALIB_USED=" << glrun->GetTRK_CALIB_USED()<< " , "
6003 << " CAL_DSP_MASK=" << glrun->GetCAL_DSP_MASK()<< " , "
6004 << " LAST_TIMESYNC=" << glrun->GetLAST_TIMESYNC()<< " , ";
6005 //
6006 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6007 oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6008 if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6009 oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6010 //
6011 oss << " OBT_TIMESYNC=" << glrun->GetOBT_TIMESYNC();
6012 oss << " WHERE ID=" << myid[u] << ";";
6013 conn->Query(oss.str().c_str());
6014 };
6015 //
6016 } else {
6017 //
6018 // sig no runheader, let set anyway what is possible...
6019 //
6020 for (UInt_t u=1; u <= nid ; u++){
6021 oss.str("");
6022 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6023 << " RUNHEADER_TIME=" << glrun->GetRUNHEADER_TIME()<< " , ";
6024 //
6025 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() )
6026 oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6027 if ( glrun->GetPHYSENDRUN_MASK_S11CRC() )
6028 oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6029 //
6030 oss << " RUNHEADER_OBT=" << glrun->GetRUNHEADER_OBT()<< " , "
6031 << " RUNHEADER_PKT=" << glrun->GetRUNHEADER_PKT();
6032 oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6033 conn->Query(oss.str().c_str());
6034 };
6035 };
6036 //
6037 // now let's look for runtrailer if any in the last run
6038 //
6039 oss.str("");
6040 oss << " ID= "<< myid[nid];
6041 //
6042 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6043 //
6044 if ( glrun->GetPKT_READY_COUNTER() != 0 ){
6045 //
6046 // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6047 //
6048 for (UInt_t u=0; u < nid ; u++){
6049 oss.str("");
6050 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6051 << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6052 << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , "
6053 << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()<< " , "
6054 << " PKT_COUNTER=" << glrun->GetPKT_COUNTER()<< " , ";
6055 //
6056 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6057 oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6058 if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ) {
6059 oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6060 //
6061 oss << " PKT_READY_COUNTER=" << glrun->GetPKT_READY_COUNTER()
6062 << " WHERE ID=" << myid[u] << ";";
6063 conn->Query(oss.str().c_str());
6064 };
6065 //
6066 } else {
6067 //
6068 // sig no runtrailer, let set anyway what is possible...
6069 //
6070 for (UInt_t u=0; u < nid ; u++){
6071 oss.str("");
6072 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6073 << " RUNTRAILER_TIME=" << glrun->GetRUNTRAILER_TIME()<< " , "
6074 << " RUNTRAILER_OBT=" << glrun->GetRUNTRAILER_OBT()<< " , ";
6075 //
6076 if ( glrun->GetPHYSENDRUN_MASK_S3S2S12() ){
6077 oss << " PHYSENDRUN_MASK_S3S2S12=" << glrun->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6078 if ( glrun->GetPHYSENDRUN_MASK_S11CRC() ){
6079 oss << " PHYSENDRUN_MASK_S11CRC=" << glrun->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6080 //
6081 oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT();
6082 oss << " WHERE ID=" << myid[u] << ";"; // BUG!!! 090112
6083 conn->Query(oss.str().c_str());
6084 };
6085 };
6086 //
6087 // Now we want to cross indexize the runs
6088 //
6089 for (UInt_t u=0; u < nid ; u++){
6090 oss.str("");
6091 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6092 << " ID_RUN_FRAG=" << myid[u+1] << " where ID=" << myid[u] <<";";
6093 conn->Query(oss.str().c_str());
6094 };
6095 oss.str("");
6096 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6097 << " ID_RUN_FRAG=" << myid[0] << " where ID=" << myid[nid] <<";";
6098 conn->Query(oss.str().c_str());
6099 //
6100 // and now we can move the runs in the GL_RUN table
6101 //
6102 for (UInt_t u=0; u <= nid; u++){
6103 oss.str("");
6104 oss << " ID= "<< myid[u];
6105 //
6106 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6107 //
6108 if ( u == 0 ){
6109 oss.str("");
6110 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
6111 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6112 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6113 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
6114 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
6115 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6116 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
6117 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
6118 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
6119 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
6120 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
6121 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6122 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
6123 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
6124 //
6125 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
6126 result2 = conn->Query(oss.str().c_str());
6127 //
6128 if ( !result2 ) throw -4;
6129 //
6130 row2 = result2->Next();
6131 //
6132 if ( row2 ){
6133 if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! \n");
6134 if ( PEDANTIC ) throw -83;
6135 row2->Close();
6136 };
6137 result2->Close();
6138 };
6139 //
6140 if ( IsDebug() ) printf(" The run is new \n");
6141 if ( IsDebug() ) printf(" -> fill the DB \n");
6142 //
6143 glrun->Fill_GL_RUN(conn);
6144 //
6145 if ( IsDebug() ) printf(" Delete run %u from the GL_RUN_FRAGMENTS table \n",myid[u]);
6146 //
6147 glrun->DeleteRun(conn,myid[u],"GL_RUN_FRAGMENTS");
6148 //
6149 moved++;
6150 //
6151 };
6152 //
6153 };
6154 //
6155 nrow = nresult->Next();
6156 };
6157 };
6158
6159
6160
6161 };
6162 //
6163 if ( !strcmp(fcleanfile.Data(),"") ){
6164 //
6165 // check if there are entries older than "olderthan" seconds from now
6166 //
6167 oss.str("");
6168 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
6169 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
6170 //
6171 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
6172 result = conn->Query(oss.str().c_str());
6173 //
6174 } else {
6175 oss.str("");
6176 oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
6177 if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
6178 result = conn->Query(oss.str().c_str());
6179 //
6180 if ( result ){
6181 //
6182 row = result->Next();
6183 //
6184 if ( row ){
6185 oss.str("");
6186 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
6187 << " ID_ROOT_L0=" << row->GetField(0) << ";";
6188 //
6189 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
6190 result = conn->Query(oss.str().c_str());
6191 //
6192 };
6193 } else {
6194 return(2);
6195 };
6196 };
6197 //
6198 if ( result ){
6199 //
6200 row = result->Next();
6201 //
6202 while ( row ){
6203 //
6204 oss.str("");
6205 oss << " ID= "<< row->GetField(0);
6206 //
6207 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6208 //
6209 oss.str("");
6210 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER,ID_ROOT_L0 FROM GL_RUN WHERE "
6211 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
6212 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
6213 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
6214 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
6215 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6216 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
6217 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
6218 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
6219 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
6220 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
6221 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
6222 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
6223 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
6224 //
6225 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
6226 result2 = conn->Query(oss.str().c_str());
6227 //
6228 if ( !result2 ) throw -4;
6229 //
6230 row2 = result2->Next();
6231 //
6232 if ( !row2 ){
6233 //
6234 if ( IsDebug() ) printf(" The run is new \n");
6235 if ( IsDebug() ) printf(" -> fill the DB \n");
6236 //
6237 // glrun->SetID(this->AssignRunID()); we use the old run number!
6238 glrun->SetID_RUN_FRAG(glrun->GetID());
6239 glrun->Fill_GL_RUN(conn);
6240 //
6241 // oss.str("");
6242 // oss << " SELECT ID FROM GL_RUN WHERE "
6243 // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
6244 // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
6245 // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
6246 // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
6247 // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
6248 // //
6249 // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
6250 // result2 = conn->Query(oss.str().c_str());
6251 // //
6252 // if ( !result2 ) throw -4;
6253 // //
6254 // row2 = result2->Next();
6255 // //
6256 // if ( !row2 ) throw -25;
6257 // //
6258 // oss.str("");
6259 // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
6260 // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
6261 // result2 = conn->Query(oss.str().c_str());
6262 // //
6263 // if ( !result2 ) throw -4;
6264 //
6265 moved++;
6266 //
6267 } else {
6268
6269 // questa parte mah mah mah... da controllare
6270
6271 //
6272 // we end up here if chewbacca and we have RH---| small gap |---RT, in this case the two pieces are attached and moved to GL_RUN. We are now processing the
6273 // "small gap" piece... we recognize this since: we have two entries from this query, the pkt number is consistent with our pkt number.
6274 //
6275 // 090112 [8RED (-70): RUN ALREADY INSERTED]
6276 //
6277 Bool_t OK = false;
6278 UInt_t IDRL2A = 0;
6279 UInt_t IDRL2B = 0;
6280 if ( result2->GetRowCount() == 2 ){
6281 IDRL2A = (UInt_t)atoll(row2->GetField(0));
6282 UInt_t IDRL0A = (UInt_t)atoll(row2->GetField(4));
6283 row2 = result2->Next();
6284 IDRL2B = (UInt_t)atoll(row2->GetField(0));
6285 UInt_t IDRL0B = (UInt_t)atoll(row2->GetField(4));
6286 if ( IsDebug() ) printf(" IDRL0A %u B %u IDRL2A %u B %u \n",IDRL0A,IDRL0B,IDRL2A,IDRL2B);
6287 // if ( IDRL0A == IDRL0B ){
6288 // TSQLResult *result2a = 0;
6289 // TSQLRow *row2a = 0;
6290 // oss.str("");
6291 // oss << "select PKT_NUMBER_FINAL from ROOT_TABLE_MERGING where REAL_TIME_INIT<" << chrtinit << " order by REAL_TIME_INIT desc limit 1;";
6292 // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6293 // result2a = conn->Query(oss.str().c_str());
6294 // //
6295 // if ( !result2a ) throw -4;
6296 // //
6297 // row2a = result2a->Next();
6298 // //
6299 // if ( row2a ){
6300 // UInt_t PKA = (UInt_t)atoll(row2a->GetField(0));
6301 // delete result2a;
6302 // oss.str("");
6303 // oss << "select PKT_NUMBER_INIT from ROOT_TABLE_MERGING where REAL_TIME_INIT>" << chrtinit << " order by REAL_TIME_INIT asc limit 1;";
6304 // if ( IsDebug() ) printf(" Check if we are in the case RH---| small gap |---RT: query is \n %s \n",oss.str().c_str());
6305 // result2a = conn->Query(oss.str().c_str());
6306 // //
6307 // if ( !result2a ) throw -4;
6308 // //
6309 // row2a = result2a->Next();
6310 // //
6311 // if ( row2a ){
6312 // UInt_t PKB = (UInt_t)atoll(row2a->GetField(0));
6313 // //
6314 // if ( IsDebug() ) printf(" PKT(PKA) + 1 %llu == runheaderpkt %llu && PKB = runtrailer %llu + 1 %llu \n",PKT(PKA)+1LL, PKT(glrun->GetRUNHEADER_PKT()),PKT(PKB), PKT(glrun->GetRUNTRAILER_PKT())+1LL);
6315 // if ( PKT(PKA)+1LL == PKT(glrun->GetRUNHEADER_PKT()) && PKT(PKB) == PKT(glrun->GetRUNTRAILER_PKT())+1LL ){
6316 // if ( IsDebug() ) printf(" Ok, we are in the case: RH---| small gap |---RT \n");
6317 OK = true;
6318 // };
6319 // };
6320 // };
6321 // };
6322 };
6323 if ( OK ){
6324 //
6325 // this is the case in which we must insert a piece of run between two fragments in the GL_RUN table
6326 //
6327 // we have to update with runheader/trailer infos our run and we have to change the ROOT_ID_FRAG of the header piece ( 1/2 2/1 must become 1/3 3/2 2/1 )
6328 //
6329 GL_RUN *glA = new GL_RUN();
6330 glA->Query_GL_RUN(IDRL2A,conn);
6331 //
6332 if ( glA->GetACQ_BUILD_INFO() != 0 ){
6333 //
6334 // the first piece contains a good runheader we can update all the other runs with correct infos!
6335 //
6336 oss.str("");
6337 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6338 << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , "
6339 << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6340 << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT()<< " , "
6341 << " COMPILATIONTIMESTAMP=" << glA->GetCOMPILATIONTIMESTAMP()<< " , "
6342 << " FAV_WRK_SCHEDULE=" << glA->GetFAV_WRK_SCHEDULE()<< " , "
6343 << " EFF_WRK_SCHEDULE=" << glA->GetEFF_WRK_SCHEDULE()<< " , "
6344 << " PRH_VAR_TRG_MODE_A=" << glA->GetPRH_VAR_TRG_MODE_A()<< " , "
6345 << " PRH_VAR_TRG_MODE_B=" << glA->GetPRH_VAR_TRG_MODE_B()<< " , "
6346 << " ACQ_BUILD_INFO=" << glA->GetACQ_BUILD_INFO()<< " , "
6347 << " ACQ_VAR_INFO=" << glA->GetACQ_VAR_INFO()<< " , "
6348 << " RM_ACQ_AFTER_CALIB=" << glA->GetRM_ACQ_AFTER_CALIB()<< " , "
6349 << " RM_ACQ_SETTING_MODE=" << glA->GetRM_ACQ_SETTING_MODE()<< " , "
6350 << " TRK_CALIB_USED=" << glA->GetTRK_CALIB_USED()<< " , "
6351 << " CAL_DSP_MASK=" << glA->GetCAL_DSP_MASK()<< " , "
6352 << " LAST_TIMESYNC=" << glA->GetLAST_TIMESYNC()<< " , ";
6353 //
6354 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6355 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12() << " , ";
6356 if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6357 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC() << " , ";
6358 //
6359 oss << " OBT_TIMESYNC=" << glA->GetOBT_TIMESYNC();
6360 oss << " WHERE ID=" << glrun->GetID() << ";";
6361 if ( IsDebug() ) printf(" update with correct infos: %s\n",oss.str().c_str());
6362 conn->Query(oss.str().c_str());
6363 //
6364 } else {
6365 //
6366 // sig no runheader, let set anyway what is possible...
6367 //
6368 oss.str("");
6369 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6370 << " RUNHEADER_TIME=" << glA->GetRUNHEADER_TIME()<< " , ";
6371 //
6372 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() )
6373 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , ";
6374 if ( glA->GetPHYSENDRUN_MASK_S11CRC() )
6375 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , ";
6376 //
6377 oss << " RUNHEADER_OBT=" << glA->GetRUNHEADER_OBT()<< " , "
6378 << " RUNHEADER_PKT=" << glA->GetRUNHEADER_PKT();
6379 oss << " WHERE ID=" << glrun->GetID() << ";";
6380 if ( IsDebug() ) printf(" update with correct infos2: %s\n",oss.str().c_str());
6381 conn->Query(oss.str().c_str());
6382 };
6383 //
6384 // update runheader ROOT_ID_FRAG
6385 //
6386 oss.str("");
6387 oss << "UPDATE GL_RUN SET ID_RUN_FRAG = " << glrun->GetID() << " where ID = " << IDRL2A << ";";
6388 if ( IsDebug() ) printf(" update gl_run to have cross indexing: %s\n",oss.str().c_str());
6389 conn->Query(oss.str().c_str());
6390 //
6391 // now let's look for runtrailer if any in the last run
6392 //
6393 glA->Query_GL_RUN(IDRL2B,conn);
6394 //
6395 if ( glA->GetPKT_READY_COUNTER() != 0 ){
6396 //
6397 // the first piece contains a good runtrailer we can update all the other runs with correct infos!
6398 //
6399 oss.str("");
6400 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6401 << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6402 << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , "
6403 << " RUNTRAILER_PKT=" << glA->GetRUNTRAILER_PKT()<< " , "
6404 << " PKT_COUNTER=" << glA->GetPKT_COUNTER()<< " , ";
6405 //
6406 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6407 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6408 if ( glA->GetPHYSENDRUN_MASK_S11CRC() ) {
6409 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6410 //
6411 oss << " PKT_READY_COUNTER=" << glA->GetPKT_READY_COUNTER()
6412 << " WHERE ID=" << glrun->GetID() << ";";
6413 if ( IsDebug() ) printf(" update with correct trailer infos: %s\n",oss.str().c_str());
6414 conn->Query(oss.str().c_str());
6415 //
6416 } else {
6417 //
6418 // sig no runtrailer, let set anyway what is possible...
6419 //
6420 oss.str("");
6421 oss << "UPDATE GL_RUN_FRAGMENTS SET "
6422 << " RUNTRAILER_TIME=" << glA->GetRUNTRAILER_TIME()<< " , "
6423 << " RUNTRAILER_OBT=" << glA->GetRUNTRAILER_OBT()<< " , ";
6424 //
6425 if ( glA->GetPHYSENDRUN_MASK_S3S2S12() ){
6426 oss << " PHYSENDRUN_MASK_S3S2S12=" << glA->GetPHYSENDRUN_MASK_S3S2S12()<< " , "; };
6427 if ( glA->GetPHYSENDRUN_MASK_S11CRC() ){
6428 oss << " PHYSENDRUN_MASK_S11CRC=" << glA->GetPHYSENDRUN_MASK_S11CRC()<< " , "; };
6429 //
6430 oss << " RUNTRAILER_PKT=" << glrun->GetRUNTRAILER_PKT()
6431 << " WHERE ID=" << glrun->GetID() << ";";
6432 if ( IsDebug() ) printf(" update with correct trailer infos2: %s\n",oss.str().c_str());
6433 conn->Query(oss.str().c_str());
6434 };
6435 //
6436 UInt_t myi = glrun->GetID();
6437 oss.str("");
6438 oss << " ID= "<< myi;
6439 //
6440 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
6441 //
6442 // fill the new run in GL_RUN
6443 //
6444 glrun->SetID_RUN_FRAG(IDRL2B);
6445 glrun->Fill_GL_RUN(conn);
6446 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
6447 delete glA;
6448 moved++;
6449 //
6450 } else {
6451 //
6452 // is just a repetition
6453 //
6454 if ( IsDebug() ) printf(" The run %u is already present in the GL_RUN table...\n",glrun->GetID());
6455 // printf(" CCCCCCCCICCCCCCCCCCCCIOOOOOOOOOO si muove Ciccio! %u \n",glrun->GetID());
6456 // if ( PEDANTIC && glrun->GetID() != 61563 ) throw -83;
6457 if ( PEDANTIC ) throw -83;
6458 };
6459 // if ( IsDebug() ) printf(" The run already exists in the GL_RUN table! ...\n");
6460 // if ( PEDANTIC ) throw -83;
6461 };
6462 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
6463 //
6464 //
6465 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
6466 // oss.str("");
6467 // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
6468 // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
6469 // result2 = conn->Query(oss.str().c_str());
6470 // //
6471 // if ( !result2 ) throw -4;
6472 // //
6473 row = result->Next();
6474 };
6475 };
6476 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
6477 return(0);
6478 };
6479
6480 /**
6481 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
6482 */
6483 Int_t PamelaDBOperations::ValidateRuns(){
6484 return(this->ValidateRuns(""));
6485 };
6486
6487 /**
6488 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
6489 */
6490 Int_t PamelaDBOperations::ValidateRuns(TString valfile){
6491 //
6492 TSQLResult *result = 0;
6493 TSQLRow *row = 0;
6494 //
6495 UInt_t calibtime = 50;
6496 //
6497 stringstream oss;
6498 oss.str("");
6499 //
6500 // =======================================================
6501 // validate runs by checking missing calibrations
6502 // =======================================================
6503 UInt_t t_stop = 0;
6504 UInt_t t_start = 0;
6505 if ( !strcmp(valfile.Data(),"") ) {
6506 // --------------------------------------------------------------
6507 // 1) get the OBT of the last run inserted after clean-time limit
6508 // --------------------------------------------------------------
6509 oss.str("");
6510 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
6511 << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6512 if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
6513 result = conn->Query(oss.str().c_str());
6514 if ( !result ) throw -4;
6515 if ( !result->GetRowCount() ) {
6516 printf(" No runs to validate \n");
6517 return(1);
6518 }else{
6519 row = result->Next();
6520 t_start = (UInt_t)atoll(row->GetField(4));
6521 };
6522 // --------------------------------------------------------------
6523 // 2) get the OBT of the last validated run
6524 // --------------------------------------------------------------
6525 oss.str("");
6526 // oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
6527 oss << " SELECT * FROM GL_RUN WHERE VALIDATION>0 AND RUNHEADER_TIME<="<< t_start
6528 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
6529 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
6530 result = conn->Query(oss.str().c_str());
6531 if ( !result ) throw -4;
6532 if ( result->GetRowCount() ){
6533 row = result->Next();
6534 t_stop = (UInt_t)atoll(row->GetField(4));
6535 };
6536 if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
6537 // --------------------------------------------------------------
6538 // now retrieves runs to be validated
6539 // --------------------------------------------------------------
6540 oss.str("");
6541 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
6542 oss << " AND RUNHEADER_TIME >="<< t_stop;
6543 oss << " ORDER BY RUNHEADER_TIME DESC;";
6544 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
6545 result = conn->Query(oss.str().c_str());
6546 } else {
6547 //
6548 stringstream myquery;
6549 UInt_t myid = 0;
6550 myquery.str("");
6551 myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
6552 //
6553 result = conn->Query(myquery.str().c_str());
6554 //
6555 row = result->Next();
6556 if( !row ){
6557 if ( strcmp(valfile.Data(),GetRootName().Data()) ){
6558 if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
6559 return(2);
6560 };
6561 if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
6562 return(0);
6563 };
6564 myid=(UInt_t)atoll(row->GetField(0));
6565 //
6566 myquery.str("");
6567 myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
6568 //
6569 result = conn->Query(myquery.str().c_str());
6570 //
6571 row = result->Next();
6572 if( !row->GetField(0) || !row->GetField(1)){
6573 //
6574 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
6575 //
6576 return(0);
6577 //
6578 } else {
6579 //
6580 UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
6581 UInt_t runttime = (UInt_t)atoll(row->GetField(1));
6582 UInt_t caltime = 0;
6583 //
6584 myquery.str("");
6585 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
6586 myquery << " order by FROM_TIME asc limit 1;";
6587 //
6588 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
6589 //
6590 //
6591 result = conn->Query(myquery.str().c_str());
6592 //
6593 row = result->Next();
6594 if( !row ){
6595 caltime = runhtime;
6596 } else {
6597 caltime = (UInt_t)atoll(row->GetField(0));
6598 };
6599 //
6600 myquery.str("");
6601 myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
6602 myquery << caltime << " order by RUNHEADER_TIME DESC";
6603 //
6604 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
6605 //
6606 result = conn->Query(myquery.str().c_str());
6607 //
6608 };
6609 };
6610 //
6611 if ( !result ) throw -4;
6612 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
6613 //
6614 Int_t nrow = 0;
6615 GL_RUN* this_run = new GL_RUN();
6616 GL_RUN* next_run = new GL_RUN();
6617 Int_t nseq_max = 1000;
6618 // UInt_t* sequence = new UInt_t[100];
6619 vector<UInt_t> sequence(nseq_max);
6620 Int_t nseq = 0;
6621 Bool_t CHECK = false;
6622 Bool_t this_ONLINE = false;
6623 Bool_t next_ONLINE = false;
6624 UInt_t t1=0,t2=0;
6625 // ---------------------------------------------------------------------------------
6626 // - loop over runs, back in time,
6627 // - select sequences of runs close in time (less than calibtime s apart),
6628 // which could be preceeded by a calibration
6629 // - check if there might be a missing calibration
6630 // ---------------------------------------------------------------------------------
6631 while(1){
6632
6633 row = result->Next();
6634 if( row == NULL ) break;
6635
6636 //------------
6637 //get run info
6638 //------------
6639 this_run->Set_GL_RUN(row);
6640
6641 Bool_t this_BAD = false;
6642 if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
6643 else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
6644 else{
6645 // printf("Missing or corrupted header!! \n");
6646 this_ONLINE = false;
6647 this_BAD = true;
6648 };
6649
6650 //-----------------------------------
6651 //compare with previous(next in time)
6652 //-----------------------------------
6653 CHECK = false;
6654 UInt_t interval=0;
6655
6656 if( nrow != 0){
6657
6658
6659 t1 = this_run->GetRUNTRAILER_TIME();
6660 t2 = next_run->GetRUNHEADER_TIME();
6661 interval = (t2-t1);
6662
6663 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
6664
6665 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
6666
6667 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
6668
6669 if( !CHECK && this_run->VALIDATION ){
6670 // for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
6671 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],this_run->VALIDATION);
6672 nseq=0;
6673 }
6674
6675 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
6676
6677 CHECK = true;
6678
6679 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
6680
6681 assignVALIDATION(next_run->ID,true);
6682 nseq=0;
6683 }
6684 }
6685
6686 //----------------------------
6687 //check run sequence for calib
6688 //----------------------------
6689 if( CHECK ){
6690 // check if calibration exists
6691 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
6692 // Bool_t MISSING = MissingTRK_CALIB(t1,t2);
6693 UInt_t MISSING = MissingTRK_CALIB(t1,t2);
6694 UInt_t val = 0;
6695 if ( MISSING == 1 ) val = 0;
6696 if ( MISSING == 0 ) val = 1;
6697 if ( MISSING == 2 ) val = 2;
6698 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],val);
6699 nseq=0;
6700 };
6701 //--------------
6702 //store run info
6703 //--------------
6704 *next_run = *this_run;
6705 next_ONLINE = this_ONLINE;
6706 if( !this_BAD ){
6707 if(nseq < nseq_max){
6708 sequence[nseq] = this_run->ID;
6709 nseq++;
6710 }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
6711 };
6712
6713 if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
6714 nrow++;
6715
6716 };
6717 delete this_run;
6718 delete next_run;
6719 //
6720 return(0);
6721 };
6722 /**
6723 * Check if there might be a missing tracker calibration in a given time interval
6724 * @param t1 From absolute time
6725 * @param t2 To absolute time
6726 * @return true if there might be a missing calibration
6727 */
6728 //Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6729 UInt_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
6730
6731 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
6732
6733 // get the closest calibration before the run start (t2)
6734 // if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
6735 if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(1); //>>> missing
6736
6737 // if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
6738 if ( trkcalib->TO_TIME < t2 ) return(1); //>>> missing
6739
6740 //==============================================================
6741 // Check is done first on the basis of time between calibration,
6742 // which should be equal to the time between ascending-nodes.
6743 //==============================================================
6744 if ( t2 - trkcalib->FROM_TIME > 5700) {
6745 if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
6746 //==============================================================
6747 // there might be a missing calibration, due to:
6748 // - MM full
6749 // - corrupted packets
6750 // - loss of data
6751 // There is an exception in case a download was done during ascending node
6752 //==============================================================
6753 Bool_t DOWNLOAD = false;
6754 // check if the calib was skipped becouse of download .... DA FARE!!
6755 // if(DOWNLOAD)return(false);
6756 if(DOWNLOAD)return(0);
6757
6758 // return(true); //>>> missing
6759 return(1); //>>> missing
6760
6761 };
6762
6763 //==============================================================
6764 // If the last calibration is close to the run less than this time,
6765 // it is enough to say that there are no missing calibrations
6766 //==============================================================
6767 // the long time interval bewteen runs might be due to download
6768 if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
6769 if ( trkcalib->VALIDATION ) return(0);
6770 if ( IsDebug() )printf("Calibration is not validated... :-/ ==> OK but with VALIDATION=2! \n");
6771 return(2);
6772
6773 };
6774 /**
6775 * Assign VALIDATION value to a GL_RUN entry
6776 * @param idrun Run ID
6777 * @param validation true/false
6778 */
6779 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
6780 return(this->assignVALIDATION(idrun,(UInt_t)validation));
6781 }
6782
6783 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, UInt_t validation){
6784 TSQLResult *result = 0;
6785 stringstream oss;
6786 oss.str("");
6787 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
6788 //
6789 // if ( IsDebug() )
6790 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
6791 if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
6792 result = conn->Query(oss.str().c_str());
6793 if ( !result ) throw -4;
6794 return(0);
6795 }
6796
6797
6798
6799 // Insert TLEs from file tlefilename in the table GL_TLE in the db
6800 // opened by conn, sorting them by date from older to newer, if each
6801 // TLE has not been alread inserted.
6802 Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
6803 {
6804 fstream tlefile(tlefilename, ios::in);
6805
6806 if ( !tlefile ) throw -7;
6807
6808 vector<cTle*> ctles;
6809 vector<cTle*>::iterator iter;
6810 int present = 0;
6811
6812 // Get three lines from tlefile, create a cTle object and put it
6813 // into ctles
6814 while(1) {
6815 cTle *tlef;
6816 string str1, str2, str3;
6817
6818 getline(tlefile, str1);
6819 if(tlefile.eof()) break;
6820
6821 getline(tlefile, str2);
6822 if(tlefile.eof()) break;
6823
6824 getline(tlefile, str3);
6825 if(tlefile.eof()) break;
6826
6827 // We now have three good lines for a cTle.
6828 tlef = new cTle(str1, str2, str3);
6829 ctles.push_back(tlef);
6830 }
6831
6832 tlefile.close();
6833
6834 // Sort by date
6835 sort(ctles.begin(), ctles.end(), compTLE);
6836
6837 // Now we insert each TLE into the db
6838 for(iter = ctles.begin(); iter != ctles.end(); iter++) {
6839 cTle *tle = *iter;
6840
6841 // Do nothing if it's already present in the db. Just increase
6842 // the counter present.
6843 if (! isTlePresent(tle))
6844 {
6845 int status = insertTle(tle);
6846
6847 // Insert query failed. Return 1.
6848 if(status == EXIT_FAILURE) {
6849
6850 if( IsDebug() ) {
6851 cerr << "Error: inserting TLE:" << endl
6852 << tle->getName() << endl
6853 << tle->getLine1() << endl
6854 << tle->getLine2() << endl;
6855 }
6856
6857 throw -4;
6858 return 1;
6859 }
6860
6861 }
6862 else
6863 present++;
6864
6865 }
6866
6867 int inserted = ctles.size() - present; // Number of inserted TLE.
6868 if ( IsDebug() )
6869 cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
6870 << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
6871
6872 ctles.clear();
6873
6874
6875 // Return 2 if no new TLE has been inserted. 0 otherwise.
6876 if(! inserted ) return 2;
6877 return 0;
6878 }
6879
6880
6881 // Insert tle in the table GL_TLE using the connection conn.
6882 Int_t PamelaDBOperations::insertTle(cTle *tle)
6883 {
6884 stringstream oss;
6885 TSQLResult *result = 0;
6886
6887 oss.str("");
6888 oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
6889 << " VALUES ( '"
6890 << tle->getName() << "', '"
6891 << tle->getLine1() << "', '"
6892 << tle->getLine2() << "', '"
6893 << getTleDatetime(tle) << "')";
6894
6895 // cout << oss.str().c_str() << endl;
6896 result = conn->Query(oss.str().c_str());
6897 if (result == NULL)
6898 return EXIT_FAILURE;
6899
6900 return EXIT_SUCCESS;
6901 }
6902
6903
6904 // Return whether tle is already in the db connected by conn.
6905 bool PamelaDBOperations::isTlePresent(cTle *tle)
6906 {
6907 stringstream oss;
6908 TSQLResult *result = 0;
6909
6910 oss.str("");
6911 oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
6912 << getTleDatetime(tle) << "'";
6913
6914 result = conn->Query(oss.str().c_str());
6915 if (result == NULL) throw -4;
6916
6917 if (result->GetRowCount())
6918 return true;
6919 else
6920 return false;
6921 }
6922
6923
6924 // Return whether the first TLE is dated early than the second
6925 bool compTLE (cTle *tle1, cTle *tle2)
6926 {
6927 return getTleJulian(tle1) < getTleJulian(tle2);
6928 }
6929
6930
6931 // Return the date of the tle using the format (year-2000)*1e3 +
6932 // julian day. e.g. 6365 is the 31th Dec 2006.
6933 // It does *not* return a cJulian date.
6934 float getTleJulian(cTle *tle) {
6935 return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
6936 }
6937
6938
6939 // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
6940 // format.
6941 string getTleDatetime(cTle *tle)
6942 {
6943 int year, mon, day, hh, mm, ss;
6944 double dom; // day of month (is double!)
6945 stringstream date; // date in datetime format
6946
6947 // create a cJulian from the date in tle
6948 cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
6949
6950 // get year, month, day of month
6951 jdate.getComponent(&year, &mon, &dom);
6952
6953 // build a datetime YYYY-MM-DD hh:mm:ss
6954 date.str("");
6955 day = (int) floor(dom);
6956 hh = (int) floor( (dom - day) * 24);
6957 mm = (int) floor( ((dom - day) * 24 - hh) * 60);
6958 ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
6959 // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
6960
6961 date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
6962
6963 return date.str();
6964 }
6965
6966 /**
6967 * Remove a file from the DB, delete on cascade all entries related to that file
6968 * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
6969 * calibration
6970 **/
6971 Int_t PamelaDBOperations::removeFile(TString remfile){
6972 //
6973 // Determine ID_ROOT_L0 and ID_RAW
6974 //
6975 TSQLResult *pResult;
6976 TSQLRow *Row;
6977 stringstream myquery;
6978 //
6979 myquery.str("");
6980 myquery << " SELECT ID,ID_RAW,ID_TIMESYNC FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
6981 //
6982 pResult = conn->Query(myquery.str().c_str());
6983 //
6984 Row = pResult->Next();
6985 if( !Row ){
6986 if ( strcmp(remfile.Data(),GetRootName().Data()) ){
6987 if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
6988 return(1);
6989 };
6990 if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
6991 return(0);
6992 };
6993 //
6994 this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
6995 this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
6996 UInt_t idtsy=(UInt_t)atoll(Row->GetField(2));
6997 //
6998 this->ValidationOFF();
6999 //
7000 this->RemoveCALIBS();
7001 //
7002 this->RemoveRUNS();
7003 //
7004 this->RemoveFILES(idtsy);
7005 //
7006 this->SetID_ROOT(0);
7007 this->SetID_RAW(0);
7008 //
7009 return(0);
7010 };
7011
7012 /**
7013 *
7014 * Set validation bit to zero for runs following the removing file till
7015 * 1) a run with TRK_CALIB_USED=140
7016 * 2) a run with VALIDATION = 0
7017 * 3) the next calibration
7018 *
7019 **/
7020 void PamelaDBOperations::ValidationOFF(){
7021 TSQLResult *pResult;
7022 TSQLRow *Row;
7023 stringstream myquery;
7024 Int_t unv = 0;
7025 //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
7026 myquery.str("");
7027 myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7028 //
7029 pResult = conn->Query(myquery.str().c_str());
7030 //
7031 Row = pResult->Next();
7032 if( !Row->GetField(0) ){
7033 //
7034 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
7035 //
7036 } else {
7037 //
7038 UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
7039 UInt_t caltime = 0;
7040 //
7041 myquery.str("");
7042 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
7043 myquery << " order by FROM_TIME asc limit 1;";
7044 //
7045 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7046 //
7047 //
7048 delete pResult;
7049 pResult = conn->Query(myquery.str().c_str());
7050 //
7051 Row = pResult->Next();
7052 if( !Row ){
7053 caltime = runhtime;
7054 } else {
7055 caltime = (UInt_t)atoll(Row->GetField(0));
7056 };
7057 //
7058 myquery.str("");
7059 myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
7060 myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
7061 //
7062 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7063 //
7064 pResult = conn->Query(myquery.str().c_str());
7065 //
7066 Row = pResult->Next();
7067 if( !Row ){
7068 //
7069 if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
7070 //
7071 } else {
7072 myquery.str("");
7073 myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
7074 myquery << " RUNHEADER_TIME>=" <<runhtime;
7075 myquery << " order by RUNHEADER_TIME asc;";
7076 //
7077 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7078 //
7079 pResult = conn->Query(myquery.str().c_str());
7080 //
7081 Row = pResult->Next();
7082 while ( Row ){
7083 //
7084 unv++;
7085 this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
7086 Row = pResult->Next();
7087 //
7088 };
7089 };
7090 };
7091 if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
7092 };
7093
7094 /**
7095 *
7096 * Rearrange GL_RUN table and remove runs
7097 *
7098 **/
7099 void PamelaDBOperations::RemoveRUNS(){
7100 TSQLResult *pResult;
7101 TSQLRow *Row;
7102 stringstream myquery;
7103 UInt_t drun = 0;
7104 GL_RUN *delrun = new GL_RUN();
7105 //
7106 myquery.str("");
7107 myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7108 //
7109 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7110 //
7111 pResult = conn->Query(myquery.str().c_str());
7112 //
7113 Row = pResult->Next();
7114 //
7115 //
7116 if ( !Row ){
7117 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
7118 } else {
7119 if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
7120 while ( Row ){
7121 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
7122 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7123 drun++;
7124 Row = pResult->Next();
7125 };
7126 };
7127 //
7128 //
7129 myquery.str("");
7130 myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7131 //
7132 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7133 //
7134 pResult = conn->Query(myquery.str().c_str());
7135 //
7136 Row = pResult->Next();
7137 //
7138 if ( !Row ){
7139 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
7140 } else {
7141 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
7142 while ( Row ){
7143 if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
7144 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
7145 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
7146 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
7147 if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
7148 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7149 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
7150 };
7151 drun++;
7152 Row = pResult->Next();
7153 };
7154 };
7155 //
7156 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
7157 //
7158 //
7159 //
7160 drun = 0;
7161 //
7162 myquery.str("");
7163 myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7164 //
7165 pResult = conn->Query(myquery.str().c_str());
7166 //
7167 Row = pResult->Next();
7168 //
7169 if ( !Row ){
7170 if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
7171 } else {
7172 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
7173 while ( Row ){
7174 if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
7175 myquery.str("");
7176 myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
7177 conn->Query(myquery.str().c_str());
7178 drun++;
7179 Row = pResult->Next();
7180 };
7181 };
7182 //
7183 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);
7184 //
7185 //
7186 //
7187 drun = 0;
7188 //
7189 myquery.str("");
7190 myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
7191 //
7192 pResult = conn->Query(myquery.str().c_str());
7193 //
7194 Row = pResult->Next();
7195 //
7196 if ( !Row ){
7197 if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
7198 } else {
7199 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
7200 while ( Row ){
7201 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
7202 myquery.str("");
7203 myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
7204 conn->Query(myquery.str().c_str());
7205 drun++;
7206 Row = pResult->Next();
7207 };
7208 };
7209 //
7210 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);
7211 //
7212 //
7213 //
7214 delete delrun;
7215 //
7216 };
7217
7218
7219 /**
7220 *
7221 * Rearrange calibration tables
7222 *
7223 **/
7224 void PamelaDBOperations::RemoveFILES(UInt_t idtsy){
7225 stringstream myquery;
7226 //
7227 myquery.str("");
7228 myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
7229 //
7230 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7231 //
7232 conn->Query(myquery.str().c_str());
7233 //
7234 myquery.str("");
7235 myquery << " DELETE FROM GL_ROOT WHERE ID=" <<this->GetID_ROOT() <<";";
7236 //
7237 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7238 //
7239 conn->Query(myquery.str().c_str()); //
7240 //
7241 if ( !chewbacca ){
7242 myquery.str("");
7243 myquery << " DELETE FROM GL_TIMESYNC WHERE ID=" << idtsy <<";";
7244 //
7245 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
7246 //
7247 conn->Query(myquery.str().c_str());
7248 };
7249 //
7250 };
7251
7252 /**
7253 *
7254 * Rearrange calibration tables
7255 *
7256 **/
7257 void PamelaDBOperations::RemoveCALIBS(){
7258 TSQLResult *pResult;
7259 TSQLRow *Row;
7260 stringstream myquery;
7261 //
7262 //
7263 // Calorimeter
7264 //
7265 for (Int_t section = 0; section < 4; section++){
7266 myquery.str("");
7267 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
7268 myquery << " SECTION=" << section << ";";
7269 //
7270 pResult = conn->Query(myquery.str().c_str());
7271 //
7272 Row = pResult->Next();
7273 if( !Row->GetField(0) || !Row->GetField(1) ){
7274 //
7275 if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
7276 //
7277 } else {
7278 //
7279 myquery.str("");
7280 myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
7281 myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
7282 myquery << " SECTION=" << section << ";";
7283 //
7284 pResult = conn->Query(myquery.str().c_str());
7285 //
7286 if( !pResult ){
7287 //
7288 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
7289 //
7290 throw -4;
7291 //
7292 };
7293 //
7294 };
7295 };
7296 Bool_t OLDDB = false;
7297 for (Int_t section = 0; section < 4; section++){
7298 myquery.str("");
7299 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
7300 myquery << " SECTION=" << section << ";";
7301 //
7302 pResult = conn->Query(myquery.str().c_str());
7303 //
7304 if ( conn->GetErrorCode() ){
7305 printf(" Section %i : warning, old databse structure no GL_CALOPULSE_CALIB table!\n",section);
7306 OLDDB=true;
7307 } else {
7308 Row = pResult->Next();
7309 if( !Row->GetField(0) || !Row->GetField(1) ){
7310 //
7311 if ( IsDebug() ) printf(" NO PULSE CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
7312 //
7313 } else {
7314 //
7315 myquery.str("");
7316 myquery << " UPDATE GL_CALOPULSE_CALIB SET TO_TIME=" << Row->GetField(1);
7317 myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
7318 myquery << " SECTION=" << section << ";";
7319 //
7320 pResult = conn->Query(myquery.str().c_str());
7321 //
7322 if( !pResult ){
7323 //
7324 if ( IsDebug() ) printf(" ERROR DELETING CALO PULSE CALIBRATIONS \n");
7325 //
7326 throw -4;
7327 //
7328 };
7329 //
7330 };
7331 };
7332 };
7333 myquery.str("");
7334 myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7335 //
7336 pResult = conn->Query(myquery.str().c_str());
7337 //
7338 if( !pResult ){
7339 //
7340 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
7341 //
7342 throw -4;
7343 //
7344 };
7345 //
7346 myquery.str("");
7347 myquery << " DELETE FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7348 //
7349 pResult = conn->Query(myquery.str().c_str());
7350 if ( IsDebug() ) printf(" Delete from GL_CALOPULSE_CALIB query is %s \n",myquery.str().c_str());
7351 if ( !OLDDB ){
7352 //
7353 if( !pResult ){
7354 //
7355 if ( IsDebug() ) printf(" ERROR DELETING PULSE CALO CALIBRATIONS \n");
7356 //
7357 throw -4;
7358 //
7359 };
7360 };
7361 //
7362 // Tracker
7363 //
7364 myquery.str("");
7365 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7366 //
7367 pResult = conn->Query(myquery.str().c_str());
7368 //
7369 Row = pResult->Next();
7370 if( !Row->GetField(0) || !Row->GetField(1) ){
7371 //
7372 if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
7373 //
7374 } else {
7375 //
7376 myquery.str("");
7377 myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
7378 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
7379 //
7380 pResult = conn->Query(myquery.str().c_str());
7381 //
7382 if( !pResult ){
7383 //
7384 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
7385 //
7386 throw -4;
7387 //
7388 };
7389 //
7390 myquery.str("");
7391 myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7392 //
7393 pResult = conn->Query(myquery.str().c_str());
7394 //
7395 if( !pResult ){
7396 //
7397 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
7398 //
7399 throw -4;
7400 //
7401 };
7402 };
7403 //
7404 //
7405 // S4
7406 //
7407 myquery.str("");
7408 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
7409 //
7410 pResult = conn->Query(myquery.str().c_str());
7411 //
7412 Row = pResult->Next();
7413 if( !Row->GetField(0) || !Row->GetField(1) ){
7414 //
7415 if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
7416 //
7417 } else {
7418 //
7419 myquery.str("");
7420 myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
7421 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
7422 //
7423 pResult = conn->Query(myquery.str().c_str());
7424 //
7425 if( !pResult ){
7426 //
7427 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
7428 //
7429 throw -4;
7430 //
7431 };
7432 //
7433 myquery.str("");
7434 myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
7435 //
7436 pResult = conn->Query(myquery.str().c_str());
7437 //
7438 if( !pResult ){
7439 //
7440 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
7441 //
7442 throw -4;
7443 //
7444 };
7445 //
7446 };
7447 };
7448
7449 /**
7450 *
7451 * Rearrange calibration tables
7452 *
7453 **/
7454 UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh , TFile *file){
7455
7456 if(!caltrk) return 0;
7457
7458 if ( IsDebug() ) cout << "ValidateTrkCalib:"<<endl;
7459
7460 UInt_t validate = 1;
7461 Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
7462 UInt_t timeaftercalib=120000; //2000;
7463 TString classname = caltrk->GetName();
7464
7465 // ----------------------------------
7466 // Check CRCs and failed calibrations
7467 // ----------------------------------
7468 for(Int_t ipkt=0; ipkt<6; ipkt++){
7469 if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7470 if( caltrk->crc_hcal[ipkt] ){
7471 // if(IsDebug())cout<<"(CRC Header)";
7472 validate = 0;
7473 if(IsDebug())cout <<endl<<" *** CRC *** (header DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7474
7475 }
7476 for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] ){
7477 // if(IsDebug())cout<<"(CRC Pkt-"<<ilad<<")";
7478 if(IsDebug())cout <<endl<<" *** CRC *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<" ladder "<<ilad<<")";
7479 validate = 0;
7480 }
7481 if( !(caltrk->ncalib_event[ipkt]==0 && caltrk->cal_flag[ipkt]==0) ){
7482 if(IsDebug())cout <<endl<<" *** FAILURE *** (data DSPn "<<caltrk->DSPnumber[ipkt]<<")";
7483 validate = 0;
7484 }
7485 }else{
7486 // validate=0;
7487 if(IsDebug())cout <<endl<<" *** DSPn *** ("<< caltrk->DSPnumber[ipkt] <<" @pkt "<<ipkt<<")";
7488 }
7489 }
7490
7491 // -----------------------
7492 // Check missing packets:
7493 // -----------------------
7494 // Readout order:
7495 // ------------------
7496 // DSP packet board
7497 // ------------------
7498 // 12 0 1
7499 // 10 1 1
7500 // 8 2 1
7501 // 4 3 1
7502 // 6 4 1
7503 // 2 5 1
7504 // ------------------
7505 // 11 0 2
7506 // 9 1 2
7507 // 7 2 2
7508 // 3 3 2
7509 // 5 4 2
7510 // 1 5 2
7511 // ------------------
7512 // -------------------------------------------------
7513 // Check if it is first or second calibration packet
7514 // -------------------------------------------------
7515 UInt_t build=0;
7516 UInt_t base=0;
7517 UInt_t mask=0;
7518 if(classname.Contains("CalibTrk1Event")){
7519 base=12;
7520 mask=0x03F000;
7521 }
7522 if(classname.Contains("CalibTrk2Event")){
7523 base=18;
7524 mask=0xFC0000;
7525 }
7526 // ----------------------------------------------------
7527 // Count number of valid packets and set build variable
7528 // ----------------------------------------------------
7529 if(IsDebug())cout <<endl<< " DSP: ";
7530 Int_t npkts=0;
7531 for(Int_t ipkt=0; ipkt<6; ipkt++){
7532 if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
7533 if(IsDebug())cout <<" "<<caltrk->DSPnumber[ipkt];
7534 npkts++;
7535 build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
7536 // cout << caltrk->DSPnumber[ipkt]
7537 };
7538 }
7539 if(IsDebug())cout << " ==> "<< hex << build << dec;
7540 // ----------------------------------------------------
7541 // If the number of valid packets is 6, ok exit...
7542 // ----------------------------------------------------
7543 if( npkts==6 ){
7544 return validate; // exit
7545 }
7546 ////////////////////////////////////////////////////////
7547 // ...otherwise there might be some missing packets
7548 //
7549 // In this case check the acq configuration
7550 // (some DSPs might be excluded from acquisition)
7551 ////////////////////////////////////////////////////////
7552
7553 if(!eh || !file || (file&&file->IsZombie()) ){
7554 if ( IsDebug() )cout << " *** MISSING VIEW *** eh="<<eh<<" file="<<file<<" cannot validate"<<endl;
7555 return (0);
7556 }
7557
7558 // -----------------------------------------------
7559 // retrieve the first run header after calib
7560 // -----------------------------------------------
7561
7562 PacketType *pctp;
7563 EventCounter *cod;
7564 cod = eh->GetCounter();
7565 Int_t irun = cod->Get(pctp->RunHeader);
7566 TTree *rh=(TTree*)file->Get("RunHeader");
7567 if ( !rh || rh->IsZombie() ) throw -17;
7568 if( rh->GetEntries() <= irun ){
7569 if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (1) -- cannot validate :-( "<<endl;
7570 return 0; // :-(
7571 }
7572 RunHeaderEvent *run = 0;
7573 EventHeader *hrun = 0;
7574 rh->SetBranchAddress("RunHeader", &run);
7575 rh->SetBranchAddress("Header", &hrun);
7576 rh->GetEntry(irun);
7577 if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
7578 if ( IsDebug() ) cout << " *** MISSING-PKT *** no runs after calib (2) -- cannot validate :-( "<<endl;
7579 return 0; // :-(
7580 }
7581
7582 UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
7583 if( dtime > timeaftercalib ){
7584 if ( IsDebug() ) cout << " *** MISSING-PKT *** run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
7585 return 0; // :-(
7586 }
7587
7588 if ( IsDebug() ) cout <<endl<< " ACQ_BUILD_INFO ==> "<<hex<<(run->ACQ_BUILD_INFO & mask)<<dec;
7589
7590 if( (run->ACQ_BUILD_INFO & mask) != build ){
7591 validate=0; // :-(
7592 cout <<endl<< " *** MISSING-PKT *** packet mismatch: ACQ_BUILD_INFO="<<hex<<(run->ACQ_BUILD_INFO&mask)<<" != "<<build<<dec;
7593 };
7594
7595 return validate;
7596
7597
7598 }
7599
7600 /**
7601 *
7602 * Check the DB (only for overlapping runs at the moment)
7603 *
7604 **/
7605 UInt_t PamelaDBOperations::Check(){
7606 return(this->Check(0,0));
7607 }
7608
7609 UInt_t PamelaDBOperations::Check(UInt_t from, UInt_t to){
7610 //
7611 if ( IsDebug() ) printf(" from %u to %u \n",from,to);
7612 //
7613 UInt_t test = 0;
7614 //
7615 UInt_t thisrht = 0;
7616 UInt_t thisrtt = 0;
7617 UInt_t thisid = 0;
7618 UInt_t prevrht = 0;
7619 UInt_t prevrtt = 0;
7620 UInt_t previd = 0;
7621 //
7622 UInt_t prevl0id = 0;
7623 UInt_t thisl0id = 0;
7624 //
7625 stringstream oss;
7626 TSQLResult *result = 0;
7627 TSQLRow *row = 0;
7628 TSQLResult *result2 = 0;
7629 TSQLRow *row2 = 0;
7630 TSQLResult *result3 = 0;
7631 TSQLRow *row3 = 0;
7632 oss.str("");
7633 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
7634 // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
7635 result = conn->Query(oss.str().c_str());
7636 //
7637 if ( !result ) throw -4;;
7638 //
7639 row = result->Next();
7640 UInt_t nid = 0;
7641 //
7642 while ( row ){
7643 nid++;
7644 if ( !(nid%1000) && nid ) printf(" %iK run scanned \n",nid/1000);
7645 thisid = (UInt_t)atoll(row->GetField(0));
7646 thisl0id = (UInt_t)atoll(row->GetField(1));
7647 thisrht = (UInt_t)atoll(row->GetField(2));
7648 thisrtt = (UInt_t)atoll(row->GetField(3));
7649 //
7650 if ( from > 0 && nid <= from ) goto ss;
7651 if ( to > 0 && nid >= to ) goto ss;
7652 //
7653 if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
7654 //
7655 //
7656 //
7657 oss.str("");
7658 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7659 << thisid << " AND ( RUNHEADER_TIME="
7660 << thisrht << " OR RUNTRAILER_TIME="
7661 << thisrtt << " ) AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7662 result3 = conn->Query(oss.str().c_str());
7663 if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7664 if ( result3 ){
7665 //
7666 oss.str("");
7667 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7668 << thisid << " AND RUNHEADER_TIME="
7669 << thisrht << " AND RUNTRAILER_TIME!="
7670 << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7671 result3 = conn->Query(oss.str().c_str());
7672 if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7673 if ( result3 ){
7674 row3 = result3->Next();
7675 //
7676 while ( row3 ){
7677 //
7678 // 2 runs with same runheader
7679 //
7680 printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7681 row3 = result3->Next();
7682 };
7683 // delete result3;
7684
7685 };
7686 //
7687 oss.str("");
7688 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7689 << thisid << " AND RUNHEADER_TIME!="
7690 << thisrht << " AND RUNTRAILER_TIME="
7691 << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
7692 result3 = conn->Query(oss.str().c_str());
7693 if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
7694 if ( result3 ){
7695 row3 = result3->Next();
7696 //
7697 while ( row3 ){
7698 //
7699 // 2 runs with same runtrailer
7700 //
7701 printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7702 row3 = result3->Next();
7703 };
7704 // delete result3;
7705 };
7706 //
7707 oss.str("");
7708 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7709 << thisid << " AND RUNHEADER_TIME="
7710 << thisrht << " AND RUNTRAILER_TIME="
7711 << thisrtt << " AND ID_RUN_FRAG!="
7712 << thisid << " order by RUNHEADER_TIME asc;";
7713 result3 = conn->Query(oss.str().c_str());
7714 if ( result3 ){
7715 row3 = result3->Next();
7716 //
7717 while ( row3 ){
7718 //
7719 // duplicated run
7720 //
7721 printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7722 row3 = result3->Next();
7723 };
7724 // delete result3;
7725
7726 };
7727 };
7728 //
7729 oss.str("");
7730 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
7731 << thisid << " AND RUNHEADER_TIME>"
7732 << thisrht << " AND RUNTRAILER_TIME<"
7733 << thisrtt << " order by RUNHEADER_TIME asc;";
7734 result3 = conn->Query(oss.str().c_str());
7735 if ( result3 ){
7736 row3 = result3->Next();
7737 //
7738 while ( row3 ){
7739 //
7740 // run contained in the checked one
7741 //
7742 printf(" CHECK n.6 RUN %u CONTAINS RUN %u \n",thisid,(UInt_t)atoll(row3->GetField(0)));
7743 row3 = result3->Next();
7744 };
7745 // delete result3;
7746 };
7747 //
7748 };
7749 //
7750 // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
7751 // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
7752 if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
7753 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
7754 printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
7755 TString prevf = "";
7756 TString thisf = "";
7757 oss.str("");
7758 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
7759 result2 = conn->Query(oss.str().c_str());
7760 if ( !result2 ) throw -4;;
7761 row2 = result2->Next();
7762 prevf = (TString)row2->GetField(0);
7763 oss.str("");
7764 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
7765 result2 = conn->Query(oss.str().c_str());
7766 if ( !result2 ) throw -4;;
7767 row2 = result2->Next();
7768 thisf = (TString)row2->GetField(0);
7769 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7770 test = 1;
7771 // delete result2;
7772 };
7773 //
7774 if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
7775 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
7776 printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
7777 TString prevf = "";
7778 TString thisf = "";
7779 oss.str("");
7780 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
7781 result2 = conn->Query(oss.str().c_str());
7782 if ( !result2 ) throw -4;
7783 row2 = result2->Next();
7784 prevf = (TString)row2->GetField(0);
7785 oss.str("");
7786 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
7787 result2 = conn->Query(oss.str().c_str());
7788 if ( !result2 ) throw -4;;
7789 row2 = result2->Next();
7790 thisf = (TString)row2->GetField(0);
7791 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7792 test = 1;
7793 // delete result2;
7794 };
7795 //
7796 if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
7797 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
7798 printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
7799 TString prevf = "";
7800 TString thisf = "";
7801 oss.str("");
7802 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
7803 result2 = conn->Query(oss.str().c_str());
7804 if ( !result2 ) throw -4;;
7805 row2 = result2->Next();
7806 prevf = (TString)row2->GetField(0);
7807 oss.str("");
7808 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
7809 result2 = conn->Query(oss.str().c_str());
7810 if ( !result2 ) throw -4;;
7811 row2 = result2->Next();
7812 thisf = (TString)row2->GetField(0);
7813 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
7814 test = 1;
7815 // delete result2;
7816 };
7817 ss:
7818 //
7819 prevrht = thisrht;
7820 prevrtt = thisrtt;
7821 previd = thisid;
7822 prevl0id = thisl0id;
7823 row = result->Next();
7824 // if ( result2 ) delete result2;
7825 // if ( result3 ) delete result3;
7826 };
7827 //
7828 return(test);
7829 //
7830 };

  ViewVC Help
Powered by ViewVC 1.1.23