/[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.36 - (show annotations) (download)
Wed Apr 10 11:29:38 2013 UTC (11 years, 8 months ago) by mocchiut
Branch: MAIN
Changes since 1.35: +25 -6 lines
YP MYSQL operator bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23