/[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.13 - (show annotations) (download)
Fri Nov 28 14:07:37 2008 UTC (16 years ago) by mocchiut
Branch: MAIN
Changes since 1.12: +27 -19 lines
Orbit bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23