/[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.9 - (show annotations) (download)
Tue Nov 4 17:11:41 2008 UTC (16 years, 1 month ago) by mocchiut
Branch: MAIN
Changes since 1.8: +5 -2 lines
New bugs in setupperlimits fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23