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

  ViewVC Help
Powered by ViewVC 1.1.23