/[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.31 - (show annotations) (download)
Wed Dec 30 10:46:35 2009 UTC (14 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.30: +251 -132 lines
Small bugs fixed (chewbacca and profiler)

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

  ViewVC Help
Powered by ViewVC 1.1.23