/[PAMELA software]/YodaProfiler/src/PamelaDBOperations.cpp
ViewVC logotype

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.46 - (show annotations) (download)
Thu Aug 30 15:04:44 2007 UTC (17 years, 10 months ago) by mocchiut
Branch: MAIN
Changes since 1.45: +92 -4 lines
Removed patch which created problems and overlapping runs

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 <CalibS4Event.h>
28 #include <CalibTrk1Event.h>
29 #include <CalibTrk2Event.h>
30 #include <varDump/VarDumpEvent.h>
31 #include <varDump/VarDumpRecord.h>
32 #include <physics/S4/S4Event.h>
33 //
34 #include <sgp4.h>
35
36 #include <PamelaDBOperations.h>
37 //
38 using namespace std;
39 using namespace pamela;
40
41 // Some function to work with cTle stuff.
42 bool compTLE(cTle* tle1, cTle *tle2);
43 float getTleJulian(cTle *);
44 string getTleDatetime(cTle*);
45
46 /**
47 * Constructor.
48 * @param host hostname for the SQL connection.
49 * @param user username for the SQL connection.
50 * @param password password for the SQL connection.
51 * @param filerawname The path and name to the raw file.
52 * @param filerootname The path and name of the raw file.
53 * @param boot file BOOT number.
54 * @param obt0 file obt0.
55 * @param tsync file timesync.
56 * @param debug debug flag.
57 * @param tlefilename ascii file with TLE 3 line elements.
58 */
59 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){
60 //
61 STATIC=false;
62 if ( staticp ) STATIC=true;
63 //
64 SetConnection(host,user,password);
65 //
66 SetDebugFlag(debug);
67 //
68 glrun = new GL_RUN();
69 //
70 if ( !boot ) SetNOBOOT(false);
71 SetBOOTnumber(boot);
72 SetTsync(tsync);
73 SetObt0(obt0);
74 //
75 SetTLEPath(tlefilename);
76 //
77 //
78 INSERT_RAW =!filerawname.IsNull();
79 if(INSERT_RAW)SetRawName(filerawname);
80 //
81 INSERT_ROOT = !filerootname.IsNull();
82 if( INSERT_ROOT ){
83 this->SetRootName(filerootname);
84 this->SetOrbitNo(dwinput);
85 file = TFile::Open(this->GetRootName().Data());
86 } else {
87 this->SetRootName("");
88 };
89 //
90 this->SetID_RAW(0);
91 this->SetID_ROOT(0);
92
93 VALIDATE = false;
94
95 //
96 };
97
98 /**
99 * Destructor
100 */
101 void PamelaDBOperations::Close(){
102 if( conn && conn->IsConnected() ) conn->Close();
103 delete clean_time;
104 delete glrun;
105 delete this;
106 };
107
108 //
109 // SETTERS
110 //
111
112 //
113 // 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
114 //
115 void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116 clean_time = new TDatime();
117 //
118 if(olderthan >= 0){
119 VALIDATE = true;
120 UInt_t timelim = 0;
121 timelim = (UInt_t)clean_time->Convert(true) - olderthan;
122 clean_time->Set(timelim,false);
123 };
124 };
125
126 /**
127 * Open the DB connection
128 * @param host hostname for the SQL connection.
129 * @param user username for the SQL connection.
130 * @param password password for the SQL connection.
131 */
132 void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
133 if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
134 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
135 };
136
137 /**
138 * Store the ID of the ROOT file.
139 * @param idr ID of the ROOT file
140 */
141 void PamelaDBOperations::SetID_ROOT(UInt_t idr){
142 idroot=idr;
143 };
144
145 /**
146 * Store the ID of the RAW file.
147 * @param idr ID of the RAW file
148 */
149 void PamelaDBOperations::SetID_RAW(UInt_t idr){
150 id=idr;
151 };
152
153 /**
154 * Set the debug flag
155 *
156 */
157 void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
158 debug = dbg;
159 };
160
161 /**
162 * Set the autoboot flag
163 *
164 */
165 void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166 AUTOBOOT = dbg;
167 };
168
169 /**
170 * Set the nofrag flag
171 *
172 */
173 void PamelaDBOperations::SetNoFrag(Bool_t nf){
174 NOFRAG = nf;
175 };
176
177 /**
178 * Store the BOOT number of the RAW file.
179 * @param boot BOOT number of the RAW file
180 */
181 void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
182 BOOTNO=boot;
183 };
184
185 /**
186 * Store the time sync of the RAW file.
187 * @param boot time sync
188 */
189 void PamelaDBOperations::SetTsync(UInt_t ts){
190 tsync=ts;
191 };
192
193 /**
194 * Store the time sync of the RAW file.
195 * @param boot time sync
196 */
197 void PamelaDBOperations::SetObt0(UInt_t ts){
198 obt0=ts;
199 };
200
201 /**
202 * Store the RAW filename.
203 * @param str the RAW filename.
204 */
205 void PamelaDBOperations::SetRawName(TString str){
206 filerawname=str;
207 };
208
209 /**
210 * Store the ROOT filename.
211 * @param str the ROOT filename.
212 */
213 void PamelaDBOperations::SetRootName(TString str){
214 filerootname=str;
215 };
216
217 /**
218 * Store the downlink orbit number from filename.
219 */
220 void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
221 dworbit = 0;
222 //
223 if ( dwinput ){
224 dworbit = dwinput;
225 if ( IsDebug() ) printf(" Downlink orbit given by hand: %i \n",dworbit);
226 return;
227 };
228 //
229 TString name = this->GetRootFile();
230 Int_t nlength = name.Length();
231 if ( nlength < 5 ) return;
232 TString dwo = 0;
233 for (Int_t i = 0; i<5; i++){
234 dwo.Append(name[i],1);
235 };
236 if ( dwo.IsDigit() ){
237 dworbit = (UInt_t)dwo.Atoi();
238 } else {
239 dwo="";
240 for (Int_t i = 8; i<13; i++){
241 dwo.Append(name[i],1);
242 };
243 if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
244 };
245 if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
246 return;
247 };
248
249
250
251 /**
252 * Store the NOBOOT flag.
253 * @param noboot true/false.
254 */
255 void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
256 NOBOOT = noboot;
257 };
258
259 /**
260 * Store path to the TLE file.
261 */
262 void PamelaDBOperations::SetTLEPath(TString str){
263 tlefilename = str;
264 };
265
266 TString PamelaDBOperations::GetRawPath(){
267 if ( STATIC ){
268 return((TString)gSystem->DirName(filerawname.Data())+'/');
269 } else {
270 return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
271 };
272 };
273
274 TString PamelaDBOperations::GetRootPath(){
275 if ( STATIC ){
276 return((TString)gSystem->DirName(filerootname.Data())+'/');
277 } else {
278 return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
279 };
280 };
281
282 /**
283 * Store the olderthan variable
284 * @param olderthan
285 */
286 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
287 // olderthan = oldthan;
288 // };
289
290 /**
291 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
292 */
293 Bool_t PamelaDBOperations::SetID_RAW(){
294 stringstream oss;
295 TSQLResult *result = 0;
296 TSQLRow *row = 0;
297 oss.str("");
298 if ( STATIC ){
299 oss << "SELECT ID FROM GL_RAW WHERE "
300 << " PATH = '" << this->GetRawPath().Data() << "' AND "
301 << " NAME = '" << this->GetRawFile().Data() << "' ";
302 } else {
303 oss << "SELECT ID FROM GL_RAW WHERE "
304 << " PATH = '$PAM_RAW' AND "
305 << " NAME = '" << this->GetRawFile().Data() << "' ";
306 }
307 result = conn->Query(oss.str().c_str());
308 if ( result == NULL ) throw -4;
309 row = result->Next();
310 if ( !row ) return(false);
311 delete result;
312 id = (UInt_t)atoll(row->GetField(0));
313 return(true);
314 }
315
316 /**
317 *
318 * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
319 *
320 */
321 void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
322 glrun->SetBOOTNUMBER(BOOTNO);
323 glrun->SetRUNHEADER_TIME(absth);
324 glrun->SetRUNTRAILER_TIME(abstt);
325 glrun->SetID_ROOT_L2(0);
326 glrun->SetID_ROOT_L0(idroot);
327 glrun->SetVALIDATION(0);
328 };
329
330 /**
331 * Patch, look for upper limits to avoid processing retransmitted data
332 */
333 Int_t PamelaDBOperations::SetUpperLimits(){
334 UInt_t nevent = 0;
335 UInt_t pktlast = 0;
336 UInt_t obtlast = 0;
337 Long64_t t_pktlast = 0LL;
338 // UInt_t t_obtlast = 0;
339 Long64_t t_obtlast = 0LL;
340 Long64_t upperpkt2 = 0LL;
341 Long64_t upperobt2 = 0LL;
342 UInt_t zomp = 0;
343 UInt_t jump = 50000; // was 5000
344 EventCounter *code=0;
345 //
346 Long64_t deltapkt = 5000LL;
347 Long64_t deltaobt = 50000LL;
348 //
349 pcksList packetsNames;
350 pcksList::iterator Iter;
351 getPacketsNames(packetsNames);
352 //
353 pktfirst = 0;
354 obtfirst = 0;
355 ppktfirst = 0;
356 pobtfirst = 0;
357 //
358 TTree *T = 0;
359 T = (TTree*)file->Get("Physics");
360 if ( !T || T->IsZombie() ) throw -16;
361 EventHeader *eh = 0;
362 PscuHeader *ph = 0;
363 T->SetBranchAddress("Header", &eh);
364 nevent = T->GetEntries();
365 //
366 T->GetEntry(0);
367 ph = eh->GetPscuHeader();
368 pktfirst = ph->GetCounter();
369 obtfirst = ph->GetOrbitalTime();
370 ppktfirst = pktfirst;
371 pobtfirst = obtfirst;
372 //
373 code = eh->GetCounter();
374 UInt_t en = 0;
375 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
376 en = code->Get(GetPacketType(*Iter));
377 if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
378 if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
379 //
380 TTree *TC = 0;
381 TC = (TTree*)file->Get("CalibCalPed");
382 if ( !TC || TC->IsZombie() ) throw -16;
383 EventHeader *ehc = 0;
384 PscuHeader *phc = 0;
385 TC->SetBranchAddress("Header", &ehc);
386 TC->GetEntry(0);
387 phc = ehc->GetPscuHeader();
388 pktfirst = phc->GetCounter();
389 obtfirst = phc->GetOrbitalTime();
390 //
391 };
392 };
393 //
394 T->GetEntry(nevent-1);
395 ph = eh->GetPscuHeader();
396 pktlast = ph->GetCounter();
397 obtlast = ph->GetOrbitalTime();
398 //
399 upperpkt = PKT(pktlast);
400 upperobt = OBT(obtlast);
401 upperentry = nevent-1;
402 //
403 if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u \n",obtfirst,pktfirst);
404 //
405 if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
406 //
407 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(32);
408 //
409 if ( !nevent ) return(64);
410 //
411 if ( nevent < 2 ) return(128);
412 if ( nevent < jump ) jump = 1;
413 // if ( nevent < jump ) jump = int(nevent/10);
414 // if ( !jump ) jump = 1;
415 //
416 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
417 //
418 if ( IsDebug() ) printf(" starting jump %i \n",jump);
419 // go back
420 zomp = nevent - 2;
421 //
422 while ( jump > 0 ){
423 //
424 t_pktlast = PKT(pktlast);
425 t_obtlast = OBT(obtlast);
426 //
427 for (UInt_t i = zomp; i>1; i-=jump){
428 //
429 if ( i >= 0 ) T->GetEntry(i);
430 ph = eh->GetPscuHeader();
431 upperpkt = PKT(ph->GetCounter());
432 upperobt = OBT(ph->GetOrbitalTime());
433 upperentry = i;
434 //
435 if ( (i-1) >= 0 ) T->GetEntry(i-1);
436 ph = eh->GetPscuHeader();
437 upperpkt2 = PKT(ph->GetCounter());
438 upperobt2 = OBT(ph->GetOrbitalTime());
439 //
440 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
441 if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);
442 if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);
443 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);
444 throw -13;
445 };
446 //
447 if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
448 zomp = i + jump + 1;
449 if ( zomp > nevent-2 ) zomp = nevent - 2;
450 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);
451 break;
452 };
453 //
454 t_pktlast = upperpkt;
455 t_obtlast = upperobt;
456 };
457 //
458 if ( jump == 1 ) jump = 0;
459 if ( jump == 10 ) jump = 1;
460 if ( jump == 100 ) jump = 10;
461 if ( jump == 1000 ) jump = 100;
462 if ( jump == 5000 ) jump = 1000;
463 if ( jump == 50000 ) jump = 5000;
464 //
465 };
466 //
467 };
468 //
469 // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
470 //
471 PacketType *pctp=0;
472 TTree *rh=(TTree*)file->Get("RunHeader");
473 if ( !rh || rh->IsZombie() ) throw -17;
474 TTree *rt=(TTree*)file->Get("RunTrailer");
475 if ( !rt || rt->IsZombie() ) throw -18;
476 //
477 rh->SetBranchAddress("RunHeader", &runh);
478 rh->SetBranchAddress("Header", &ehh);
479 //
480 rt->SetBranchAddress("RunTrailer", &runt);
481 rt->SetBranchAddress("Header", &eht);
482 //
483 rhev = rh->GetEntries();
484 rtev = rt->GetEntries();
485 Long64_t sobtt = 0LL;
486 Long64_t sobth = 0LL;
487 Long64_t spktt = 0LL;
488 Long64_t spkth = 0LL;
489 Long64_t pktt = 0LL;
490 Long64_t obtt = 0LL;
491 Long64_t pkth = 0LL;
492 Long64_t obth = 0LL;
493 //
494 if ( rhev || rtev ){
495
496 T->GetEntry(upperentry);
497 code = eh->GetCounter();
498 Int_t lasttrail = code->Get(pctp->RunTrailer);
499 Int_t lasthead = code->Get(pctp->RunHeader);
500 if ( lasttrail < rtev ){
501 rt->GetEntry(lasttrail);
502 pht = eht->GetPscuHeader();
503 pktt = PKT(pht->GetCounter());
504 obtt = OBT(pht->GetOrbitalTime());
505 };
506 //
507 if ( lasthead < rhev ){
508 rh->GetEntry(lasthead);
509 phh = ehh->GetPscuHeader();
510 pkth = PKT(phh->GetCounter());
511 obth = OBT(phh->GetOrbitalTime());
512 };
513 //
514 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515 if ( pkth > upperpkt && obth > upperobt ){
516 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517 upperpkt = pkth;
518 upperobt = obth;
519 rhev = lasthead+1;
520 } else {
521 rhev = lasthead;
522 };
523 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524 //
525 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526 if ( pktt > upperpkt && obtt > upperobt ){
527 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528 upperpkt = pktt;
529 upperobt = obtt;
530 rtev = lasttrail+1;
531 } else {
532 rtev = lasttrail;
533 };
534 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535 // goto kikko;
536 //
537 //
538 // Check if runtrailer/runheader are within lower limits
539 //
540 //
541 pkth = 0LL;
542 obth = 0LL;
543 spkth = 0LL;
544 sobth = 0LL;
545 for (Int_t k=0; k<rhev; k++){
546 if ( k > 0 ){
547 spkth = pkth;
548 sobth = obth;
549 };
550 rh->GetEntry(k);
551 phh = ehh->GetPscuHeader();
552 pkth = PKT(phh->GetCounter());
553 obth = OBT(phh->GetOrbitalTime());
554 //
555 // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556 //
557 if ( pkth < spkth && obth < sobth ){
558 if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
559 //
560 rhev = k-1;
561 rh->GetEntry(rhev);
562 pkth = spkth;
563 obth = sobth;
564 //
565 UInt_t evbefh = 0;
566 code = ehh->GetCounter();
567 evbefh = code->Get(pctp->Physics);
568 if ( evbefh >= 0 ){
569 T->GetEntry(evbefh);
570 ph = eh->GetPscuHeader();
571 t_pktlast = PKT(ph->GetCounter());
572 t_obtlast = OBT(ph->GetOrbitalTime());
573 if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
574 upperpkt = pkth;
575 upperobt = obth;
576 upperentry = evbefh-1;
577 } else {
578 while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
579 evbefh++;
580 T->GetEntry(evbefh);
581 ph = eh->GetPscuHeader();
582 t_pktlast = PKT(ph->GetCounter());
583 t_obtlast = OBT(ph->GetOrbitalTime());
584 };
585 T->GetEntry(evbefh-1);
586 ph = eh->GetPscuHeader();
587 upperpkt = PKT(ph->GetCounter());
588 upperobt = OBT(ph->GetOrbitalTime());
589 upperentry = evbefh-1;
590 };
591 };
592 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
593 goto kikko0;
594 };
595 };
596 kikko0:
597 //
598 //
599 //
600 pktt = 0LL;
601 obtt = 0LL;
602 spktt = 0LL;
603 sobtt = 0LL;
604 for (Int_t k=0; k<rtev; k++){
605 if ( k > 0 ){
606 spktt = pktt;
607 sobtt = obtt;
608 };
609 rt->GetEntry(k);
610 pht = eht->GetPscuHeader();
611 pktt = PKT(pht->GetCounter());
612 obtt = OBT(pht->GetOrbitalTime());
613 //
614 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
615 //
616 if ( pktt < spktt && obtt < sobtt ){
617 if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
618 //
619 rtev = k-1;
620 rt->GetEntry(rtev);
621 pktt = spktt;
622 obtt = sobtt;
623 if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
624 //
625 UInt_t evbeft = 0;
626 code = eht->GetCounter();
627 evbeft = code->Get(pctp->Physics);
628 if ( evbeft >= 0 ){
629 T->GetEntry(evbeft);
630 ph = eh->GetPscuHeader();
631 t_pktlast = PKT(ph->GetCounter());
632 t_obtlast = OBT(ph->GetOrbitalTime());
633 if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
634 upperpkt = pktt;
635 upperobt = obtt;
636 upperentry = evbeft-1;
637 } else {
638 while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
639 evbeft++;
640 T->GetEntry(evbeft);
641 ph = eh->GetPscuHeader();
642 t_pktlast = PKT(ph->GetCounter());
643 t_obtlast = OBT(ph->GetOrbitalTime());
644 };
645 T->GetEntry(evbeft-1);
646 ph = eh->GetPscuHeader();
647 upperpkt = PKT(ph->GetCounter());
648 upperobt = OBT(ph->GetOrbitalTime());
649 upperentry = evbeft-1;
650 };
651 };
652 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
653 goto kikko;
654 // break;
655 //
656 };
657 //
658 };
659 //
660 kikko:
661 //
662 T->GetEntry(upperentry);
663 code = eh->GetCounter();
664 lasttrail = code->Get(pctp->RunTrailer);
665 lasthead = code->Get(pctp->RunHeader);
666 if ( lasttrail < rtev ){
667 rt->GetEntry(lasttrail);
668 pht = eht->GetPscuHeader();
669 pktt = PKT(pht->GetCounter());
670 obtt = OBT(pht->GetOrbitalTime());
671 };
672 //
673 if ( lasthead < rhev ){
674 rh->GetEntry(lasthead);
675 phh = ehh->GetPscuHeader();
676 pkth = PKT(phh->GetCounter());
677 obth = OBT(phh->GetOrbitalTime());
678 };
679 //
680 if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
681 if ( pkth > upperpkt && obth > upperobt ){
682 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
683 upperpkt = pkth;
684 upperobt = obth;
685 rhev = lasthead+1;
686 } else {
687 rhev = lasthead;
688 };
689 if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
690 //
691 if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
692 if ( pktt > upperpkt && obtt > upperobt ){
693 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
694 upperpkt = pktt;
695 upperobt = obtt;
696 rtev = lasttrail+1;
697 } else {
698 rtev = lasttrail;
699 };
700 if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
701 //
702 };
703 //
704 if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
705 //
706 return(0);
707 }
708
709 /**
710 *
711 * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
712 * Entries in the _RUNID_GEN table are never deleted.
713 *
714 **/
715 UInt_t PamelaDBOperations::AssignRunID(){
716 //
717 TSQLResult *result = 0;
718 TSQLRow *row = 0;
719 UInt_t runid = 0;
720 //
721 stringstream oss;
722 //
723 oss.str("");
724 oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
725 result = conn->Query(oss.str().c_str());
726 if ( !result ) throw -10;
727 oss.str("");
728 oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
729 result = conn->Query(oss.str().c_str());
730 if ( !result ) throw -10;
731 //
732 row = result->Next();
733 //
734 if ( !row ) throw -28;
735 //
736 runid = (UInt_t)atoll(row->GetField(0));
737 //
738 return(runid);
739 };
740
741 //
742 // GETTERS
743 //
744
745 /**
746 *
747 * Returns the DB absolute time needed to associate calibrations to data
748 *
749 */
750 UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
751 //
752 return(((UInt_t)(OBT(obt)/1000)+toffset));
753 //
754 };
755
756 /**
757 *
758 * List of packet types (just to make easily the loops)
759 *
760 */
761 const PacketType* PamelaDBOperations::GetPacketType(const char* type){
762 if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
763 if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
764 if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
765 if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
766 if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
767 if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
768 if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
769 if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
770 if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
771 if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
772 if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
773 if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
774 if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
775 if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
776 if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
777 if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
778 if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
779 if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
780 if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
781 if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
782 if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
783 if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
784 if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
785 if ( !strcmp(type,"Log") ) return(PacketType::Log);
786 if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
787 if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
788 if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
789 if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
790 if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
791 if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
792 if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
793 if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
794 if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
795 if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
796 if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
797 if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
798 if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
799 if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
800 if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
801 if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
802 if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
803 if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
804 if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
805 if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
806 if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
807 if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
808 if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
809 return(PacketType::Invalid);
810 };
811
812 //
813 // PRIVATE FUNCTIONS
814 //
815
816 // /**
817 // * Open the ROOT filename for reading
818 // */
819 // void PamelaDBOperations::OpenFile(){
820 // file = TFile::Open(this->GetRootName().Data());
821 // //
822
823 void PamelaDBOperations::CheckFile(){
824 if ( !file ) throw -12;
825 };
826
827
828 /**
829 * Check if LEVEL0 file and DB connection have really be opened
830 */
831 void PamelaDBOperations::CheckConnection(){
832 //
833 // check connection
834 //
835 if( !conn ) throw -1;
836 bool connect = conn->IsConnected();
837 if( !connect ) throw -1;
838 //
839 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());
840 //
841 if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
842 //
843 // set DB timezone to UTC
844 //
845 stringstream oss;
846 //
847 oss.str("");
848 oss << "SET time_zone='+0:00';";
849 TSQLResult *result = 0;
850 result = conn->Query(oss.str().c_str());
851 if ( !result ) throw -10;
852 oss.str("");
853 oss << "SET wait_timeout=173000;";
854 conn->Query(oss.str().c_str());
855 //
856 };
857
858 /**
859 * Return the correct packet number if we went back to zero
860 */
861 Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){
862 //
863 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));
864 //
865 if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){
866 if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
867 return((Long64_t)pkt_num+16777215LL);
868 };
869 //
870 if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
871 if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
872 return((Long64_t)pkt_num-16777215LL);
873 };
874 //
875 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
876 return((Long64_t)pkt_num);
877 //
878 };
879
880 /**
881 * Return the correct On Board Time if we went back to zero
882 */
883 Long64_t PamelaDBOperations::OBT(UInt_t obt){
884 //
885 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));
886 //
887 if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
888 if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
889 return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
890 };
891 //
892 if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
893 if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
894 if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
895 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
896 };
897 //
898 if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
899 return((Long64_t)obt);
900 };
901
902 /**
903 *
904 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
905 *
906 */
907 void PamelaDBOperations::FillClass(){
908 this->FillClass(false,false,0,0);
909 };
910
911 /**
912 *
913 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
914 *
915 */
916 void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
917 //
918 TTree *T = 0;
919 T = (TTree*)file->Get("Physics");
920 if ( !T || T->IsZombie() ) throw -16;
921 //
922 EventHeader *eh = 0;
923 PscuHeader *ph = 0;
924 T->SetBranchAddress("Header", &eh);
925 PacketType *pctp=0;
926 EventCounter *codt=0;
927 EventCounter *codh=0;
928 UInt_t firstObt = 0;
929 UInt_t lastObt = 0;
930 UInt_t firstPkt = 0;
931 UInt_t lastPkt = 0;
932 UInt_t rhtime = 0;
933 UInt_t rttime = 0;
934 if ( !mishead ){
935 codh = ehh->GetCounter();
936 firstev = codh->Get(pctp->Physics);
937 rhtime = this->GetAbsTime(phh->GetOrbitalTime());
938 glrun->Set_GL_RUNH(runh,phh);
939 firstObt = glrun->GetRUNHEADER_OBT();
940 firstPkt = glrun->GetRUNHEADER_PKT();
941 };
942 if ( !mistrail ){
943 codt = eht->GetCounter();
944 lastev = codt->Get(pctp->Physics)-1;
945 rttime = this->GetAbsTime(pht->GetOrbitalTime());
946 glrun->Set_GL_RUNT(runt,pht);
947 lastObt = glrun->GetRUNTRAILER_OBT();
948 lastPkt = glrun->GetRUNTRAILER_PKT();
949 };
950 //
951 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
952 //
953 if ( mishead ) {
954 glrun->Set_GL_RUNH0();
955 //
956 if ( lastev+1 == firstev ){
957 firstObt = lastObt;
958 firstPkt = lastPkt;
959 rhtime = rttime;
960 } else {
961 T->GetEntry(firstev);
962 ph = eh->GetPscuHeader();
963 firstObt = ph->GetOrbitalTime();
964 rhtime = this->GetAbsTime(firstObt);
965 firstPkt = ph->GetCounter();
966 };
967 //
968 glrun->SetRUNHEADER_PKT(firstPkt);
969 glrun->SetRUNHEADER_OBT(firstObt);
970 //
971 };
972 if ( mistrail ){
973 glrun->Set_GL_RUNT0();
974 //
975 if ( lastev+1 == firstev ){
976 lastObt = firstObt;
977 lastPkt = firstPkt;
978 rttime = rhtime;
979 } else {
980 T->GetEntry(lastev);
981 ph = eh->GetPscuHeader();
982 lastObt = ph->GetOrbitalTime();
983 rttime = this->GetAbsTime(lastObt);
984 lastPkt = ph->GetCounter();
985 };
986 //
987 glrun->SetRUNTRAILER_OBT(lastObt);
988 glrun->SetRUNTRAILER_PKT(lastPkt);
989 //
990 };
991 glrun->SetEV_FROM(firstev);
992 glrun->SetEV_TO(lastev);
993 glrun->SetNEVENTS(lastev-firstev+1);
994 //
995 this->SetCommonGLRUN(rhtime,rttime);
996 //
997 };
998
999 //
1000 // PUBLIC FUNCTIONS
1001 //
1002
1003 /**
1004 * Insert a new row into GL_RAW table.
1005 */
1006 Int_t PamelaDBOperations::insertPamelaRawFile(){
1007 //
1008 stringstream oss;
1009 //
1010 Bool_t idr = this->SetID_RAW();
1011 if ( idr ) return(1);
1012 //
1013 oss.str("");
1014 if ( STATIC ){
1015 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1016 << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1017 } else {
1018 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1019 };
1020 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1021 //
1022 idr = this->SetID_RAW();
1023 if ( !idr ) throw -11;
1024 //
1025 return(0);
1026 }
1027
1028
1029 /**
1030 * Look for one timesync information in the file and
1031 * 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
1032 */
1033 Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
1034 //
1035 TSQLResult *result = 0;
1036 TSQLRow *row = 0;
1037 UInt_t t0 = 0;
1038 Int_t signal = 0;
1039 //
1040 signal = this->SetUpperLimits();
1041 //
1042 stringstream oss;
1043 //
1044 if ( this->GetID_RAW() == 0 ) throw -11;
1045 //
1046 oss.str("");
1047 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1048 << this->GetRawFile().Data() << "';";
1049 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1050 result = conn->Query(oss.str().c_str());
1051 if ( !result ) throw -10;
1052 row = result->Next();
1053 //
1054 if ( !row ){
1055 oss.str("");
1056 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1057 << dworbit << " order by FROM_ORBIT desc limit 1;";
1058 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1059 result = conn->Query(oss.str().c_str());
1060 if ( !result ) throw -10;
1061 row = result->Next();
1062 if ( !row ) throw -10;
1063 };
1064 //
1065 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);
1066 t0 = (UInt_t)tu.GetSec();
1067 if ( IsDebug() ) printf(" t0 is %u ti is %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));
1068 //
1069 /*
1070 * Verify that the TIMESYNC have been not already processed
1071 */
1072 oss.str("");
1073 oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
1074 << " LEFT JOIN GL_RAW "
1075 << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
1076 << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
1077 << " GROUP BY GL_TIMESYNC.OBT0;";
1078 if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
1079 result = conn->Query(oss.str().c_str());
1080 if (result == NULL) throw -10;
1081 row = result->Next();
1082 if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1083 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1084 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1085 //
1086 tsync = (UInt_t)atoll(row->GetField(2));
1087 obt0 = (UInt_t)atoll(row->GetField(1));
1088 //
1089 return(1);
1090 };
1091 //
1092 TTree *T = 0;
1093 //
1094 UInt_t nevent = 0;
1095 UInt_t recEntries = 0;
1096 //
1097 UInt_t OBT = 0;
1098 UInt_t TYPE = 0;
1099 //
1100 Double_t minimum = 0.;
1101 Double_t maximum = 0.;
1102 Double_t minimum2 = 0.;
1103 Double_t maximum2 = 0.;
1104 //
1105 UInt_t TSYNC = 0;
1106 //
1107 pamela::McmdEvent *mc = 0;
1108 pamela::McmdRecord *mcrc = 0;
1109 TArrayC *mcmddata = 0;
1110 //
1111 minimum = numeric_limits<Double_t>::max();
1112 maximum = numeric_limits<Double_t>::min();
1113 minimum2 = numeric_limits<Double_t>::max();
1114 maximum2 = numeric_limits<Double_t>::min();
1115 //
1116 T = (TTree*)file->Get("Mcmd");
1117 if ( !T || T->IsZombie() ) throw -19;
1118 T->SetBranchAddress("Mcmd",&mc);
1119 //
1120 nevent = T->GetEntries();
1121 //
1122 // loop over events
1123 //
1124 Bool_t existsts = false;
1125 //
1126 for (UInt_t i=0; i<nevent;i++){
1127 //
1128 T->GetEntry(i);
1129 //
1130 recEntries = mc->Records->GetEntries();
1131 //
1132 for (UInt_t j = 0; j < recEntries; j++){
1133 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1134 mcmddata = mcrc->McmdData;
1135 //
1136 if (mcrc->ID1 == 0xE0){ // mcmd timesync
1137 //
1138 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1139 //
1140 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);
1141 //
1142 TYPE = 55;//224;
1143 //
1144 if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1145 //
1146 if ( TSYNC && OBT ){
1147 existsts = true;
1148 goto eout;
1149 };
1150 //
1151 };
1152 };
1153 };
1154 if ( !existsts ) { // try with runheader and runtrailer
1155 //
1156 if ( IsDebug() ) printf(" No ts mcmd \n");
1157 signal = 2;
1158 //
1159 TTree *rh=(TTree*)file->Get("RunHeader");
1160 if ( !rh || rh->IsZombie() ) throw -17;
1161 TTree *rt=(TTree*)file->Get("RunTrailer");
1162 if ( !rt || rt->IsZombie() ) throw -18;
1163 //
1164 rh->SetBranchAddress("RunHeader", &runh);
1165 //
1166 rt->SetBranchAddress("RunTrailer", &runt);
1167 //
1168 Int_t nrhev = rh->GetEntries();
1169 Int_t nrtev = rt->GetEntries();
1170 if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1171 //
1172 if ( nrhev > 0 ){
1173 for (Int_t i=0; i<nrhev; i++){
1174 //
1175 rh->GetEntry(i);
1176 //
1177 TSYNC = runh->LAST_TIME_SYNC_INFO;
1178 OBT = runh->OBT_TIME_SYNC * 1000;
1179 //
1180 TYPE = 20;
1181 //
1182 if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1183 //
1184 if ( TSYNC && OBT ){
1185 existsts = true;
1186 goto eout;
1187 };
1188 };
1189 //
1190 };
1191 if ( nrtev > 0 ){
1192 //
1193 if ( IsDebug() ) printf(" No runheader \n");
1194 signal = 6;
1195 //
1196 for (Int_t i=0; i<nrtev; i++){
1197 //
1198 rt->GetEntry(i);
1199 //
1200 TSYNC = runt->LAST_TYME_SYNC_INFO;
1201 OBT = runt->OBT_TYME_SYNC * 1000;
1202 //
1203 TYPE = 21;
1204 //
1205 if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1206 //
1207 if ( TSYNC && OBT ){
1208 existsts = true;
1209 goto eout;
1210 };
1211 };
1212 //
1213 } else {
1214 if ( IsDebug() ) printf(" No runheader \n");
1215 };
1216 };
1217 //
1218 if ( !existsts ){ // try with inclination mcmd
1219 //
1220 if ( IsDebug() ) printf(" No runtrailer \n");
1221 signal = 14;
1222 //
1223 Double_t timesync = 0.;
1224 for (UInt_t i=0; i<nevent;i++){
1225 //
1226 T->GetEntry(i);
1227 //
1228 recEntries = mc->Records->GetEntries();
1229 // //
1230 for (UInt_t j = 0; j < recEntries; j++){
1231 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1232 mcmddata = mcrc->McmdData;
1233 //
1234 if (mcrc->ID1 == 0xE2){ // mcmd inclination
1235 timesync = 0.;
1236 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);
1237 //
1238 if ( timesync > maximum2){
1239 maximum2 = timesync;
1240 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1241 };
1242 };
1243 //
1244 };
1245 };
1246 if ( maximum2 > numeric_limits<Double_t>::min() ){
1247 TSYNC = (UInt_t)(maximum2 + 0.5);
1248 TYPE = 666;
1249 if ( TSYNC && OBT ){
1250 existsts = true;
1251 goto eout;
1252 };
1253 };
1254 };
1255 //
1256 if ( !existsts && obt0 ){ // insert timesync by hand
1257 //
1258 if ( IsDebug() ) printf(" No incl mcmd \n");
1259 signal = 30;
1260 //
1261 OBT = obt0;
1262 TSYNC = tsync;
1263 TYPE = 999;
1264 existsts = true;
1265 goto eout;
1266 };
1267 //
1268 eout:
1269 //
1270 if ( !existsts ) throw -3;
1271 //
1272 oss.str("");
1273 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1274 << this->GetID_RAW() << "','"//224'"
1275 << dec << (UInt_t)TYPE << "','"
1276 << dec << (UInt_t)OBT << "','"
1277 << dec << (UInt_t)TSYNC << "');";
1278 conn->Query(oss.str().c_str());
1279 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1280 //
1281 if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1282 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1283 //
1284 tsync = TSYNC;
1285 obt0 = OBT;
1286 //
1287 delete result;
1288 return(signal);
1289 }
1290
1291 /**
1292 * Insert all the new rows into GL_ROOT.
1293 * The raw file indicates in the parameters should be already been stored in the database.
1294 */
1295 Int_t PamelaDBOperations::insertPamelaRootFile(){
1296 stringstream oss;
1297 TSQLResult *result = 0;
1298 TSQLRow *row = 0;
1299 UInt_t idtimesync = 0;
1300 //
1301 oss.str("");
1302 if ( STATIC ){
1303 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1304 << " LEFT JOIN GL_ROOT "
1305 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1306 << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1307 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1308 } else {
1309 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1310 << " LEFT JOIN GL_ROOT "
1311 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1312 << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1313 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1314 };
1315 result = conn->Query(oss.str().c_str());
1316 //
1317 if ( !result ) throw -12;
1318 //
1319 row = result->Next();
1320 //
1321 if ( !row ) throw -10;
1322 if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1323 idroot = (UInt_t)atoll(row->GetField(2));
1324 return(1);
1325 };
1326 //
1327 // determine which timesync has to be used
1328 //
1329 oss.str("");
1330 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;";
1331 result = conn->Query(oss.str().c_str());
1332 //
1333 if ( !result ) throw -3;
1334 //
1335 row = result->Next();
1336 //
1337 if ( !row ) throw -3;
1338 idtimesync = (UInt_t)atoll(row->GetField(0));
1339 //
1340 oss.str("");
1341 if ( STATIC ){
1342 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1343 << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1344 } else {
1345 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1346 << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1347 };
1348 //
1349 if (conn->Query(oss.str().c_str()) == 0) throw -4;
1350 //
1351 delete result;
1352 //
1353 oss.str("");
1354 oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1355 //
1356 result = conn->Query(oss.str().c_str());
1357 if ( !result ) throw -12;
1358 row = result->Next();
1359 this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1360 //
1361 delete result;
1362 //
1363 return(0);
1364 }
1365
1366 /**
1367 * Assign the BOOT_NUMBER to the raw file.
1368 */
1369 Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1370 stringstream oss;
1371 TSQLResult *result = 0;
1372 TSQLRow *row = 0;
1373 oss.str("");
1374 if ( STATIC ){
1375 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1376 << " PATH = '" << this->GetRawPath().Data() << "' AND "
1377 << " NAME = '" << this->GetRawFile().Data() << "' ";
1378 } else {
1379 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1380 << " PATH = '$PAM_RAW' AND "
1381 << " NAME = '" << this->GetRawFile().Data() << "' ";
1382 };
1383 result = conn->Query(oss.str().c_str());
1384 //
1385 if ( !result ) throw -4;;
1386 row = result->Next();
1387 if ( !row ) return(16);
1388 if ( row->GetField(1) ){
1389 this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1390 return(1);
1391 };
1392 if ( !row->GetField(0) ) throw -26;
1393 //
1394 UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1395 //
1396 //
1397 //
1398 TTree *trDumpEv = 0;
1399 trDumpEv = (TTree*)file->Get("VarDump");
1400 if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1401 //
1402 VarDumpEvent *vde = 0;
1403 VarDumpRecord *vdr = 0;
1404 //
1405 Bool_t found = false;
1406 trDumpEv->SetBranchAddress("VarDump", &vde);
1407 if ( trDumpEv->GetEntries() > 0 ){
1408 found = false;
1409 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1410 trDumpEv->GetEntry(i);
1411 // vde->Records->GetEntries();
1412 if ( vde->Records->GetEntries()>5 ){
1413 found = true;
1414 goto fill;
1415 };
1416 };
1417 fill:
1418 if ( found ){
1419 //
1420 vdr = (VarDumpRecord*)vde->Records->At(6);
1421 //
1422 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1423 //
1424 } else {
1425 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1426 };
1427 } else {
1428 if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1429 };
1430 //
1431 UInt_t bn = 0;
1432 Bool_t afound = false;
1433 if ( !found && this->AutoBoot()){
1434 afound = true;
1435 //
1436 // Search for other files with similar timesync
1437 //
1438 if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1439 UInt_t upperts = tsync-(obt0/1000)+5;
1440 UInt_t lowerts = tsync-(obt0/1000)-5;
1441 oss.str("");
1442 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)<"
1443 << upperts
1444 << " AND TIMESYNC-(OBT0/1000)>"
1445 << lowerts
1446 << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1447 result = conn->Query(oss.str().c_str());
1448 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1449 //
1450 if ( !result ) throw -4;;
1451 found = true;
1452 if ( result->GetRowCount()<3 ){
1453 if ( IsDebug() ) printf(" AGH! no results!\n");
1454 found = false;
1455 } else {
1456 row = result->Next();
1457 bn = (UInt_t)atoll(row->GetField(0));
1458 for ( Int_t r=1; r<result->GetRowCount() ;r++){
1459 if ( !row ) throw -4;
1460 if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1461 if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1462 if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1463 found = false;
1464 };
1465 row = result->Next();
1466 };
1467 };
1468 };
1469 //
1470 Int_t sgn = 0;
1471 //
1472 if ( !found && !BOOTNO ){
1473 throw -29;
1474 } else {
1475 if ( afound ){
1476 this->SetBOOTnumber(bn);
1477 sgn = 8;
1478 };
1479 };
1480 //
1481 oss.str("");
1482 oss << " UPDATE GL_RAW "
1483 << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1484 << " WHERE GL_RAW.ID = '" << idRaw << "'";
1485 conn->Query(oss.str().c_str());
1486 //
1487 delete result;
1488 return(sgn);
1489 };
1490
1491 /**
1492 * Scan runtrailer packet, fill the GL_RUN table and
1493 * check for missing and truncated runs
1494 */
1495 Int_t PamelaDBOperations::insertPamelaRUN(){
1496 Int_t signal = 0;
1497 //
1498 stringstream oss;
1499 oss.str("");
1500 //
1501 // signal = this->SetUpperLimits();
1502 //
1503 // loop on runheader and runtrailer events
1504 //
1505 TTree *rh=(TTree*)file->Get("RunHeader");
1506 if ( !rh || rh->IsZombie() ) throw -17;
1507 TTree *rt=(TTree*)file->Get("RunTrailer");
1508 if ( !rt || rt->IsZombie() ) throw -18;
1509 //
1510 PacketType *pctp=0;
1511 EventCounter *cod=0;
1512 //
1513 rh->SetBranchAddress("RunHeader", &runh);
1514 rh->SetBranchAddress("Header", &ehh);
1515 //
1516 rt->SetBranchAddress("RunTrailer", &runt);
1517 rt->SetBranchAddress("Header", &eht);
1518 //
1519 TTree *T = (TTree*)file->Get("Physics");
1520 if ( !T || T->IsZombie() ) throw -16;
1521 EventHeader *eh = 0;
1522 T->SetBranchAddress("Header", &eh);
1523 //
1524 if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
1525 //
1526 UInt_t obtt = 0;
1527 UInt_t obth = 0;
1528 UInt_t pktt = 0;
1529 UInt_t pkth = 0;
1530 Int_t pth = -1;
1531 Int_t ptht = -1;
1532 Int_t evbeft = 0;
1533 Int_t evbefh = 0;
1534 //
1535 // no runtrailers in the file!
1536 //
1537 if ( !rtev ){
1538 if ( !upperentry ){
1539 if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1540 throw -8;
1541 } else {
1542 this->HandleRunFragments(true,true,0,upperentry);
1543 };
1544 } else {
1545 //
1546 for (Int_t ptt=0; ptt<rtev; ptt++){
1547 //
1548 rt->GetEntry(ptt);
1549 pht = eht->GetPscuHeader();
1550 pktt = pht->GetCounter();
1551 obtt = pht->GetOrbitalTime();
1552 //
1553 cod = eht->GetCounter();
1554 ptht = cod->Get(pctp->RunHeader) - 1;
1555 evbeft = cod->Get(pctp->Physics);
1556 //
1557 if ( !ptt && !(ptht+1) ){
1558 //
1559 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %u %u %u \n",ptht,pth,ptt);
1560 //
1561 this->HandleRunFragments(true,false,0,(evbeft-1));
1562 //
1563 //
1564 } else if ( pth == ptht ){
1565 //
1566 if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1567 //
1568 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1569 rt->GetEntry(ptt-1);
1570 cod = eht->GetCounter();
1571 evbefh = cod->Get(pctp->Physics);
1572 rt->GetEntry(ptt);
1573 pht = eht->GetPscuHeader();
1574 //
1575 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %u %u %u \n",ptht,pth,ptt);
1576 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1577 //
1578 this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1579 //
1580 } else {
1581 //
1582 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %u %u %u\n",ptht,pth,ptt);
1583 //
1584 rh->GetEntry(ptht);
1585 phh = ehh->GetPscuHeader();
1586 pkth = phh->GetCounter();
1587 obth = phh->GetOrbitalTime();
1588 cod = ehh->GetCounter();
1589 evbefh = cod->Get(pctp->Physics);
1590 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1591 //
1592 // handle this run
1593 //
1594 this->HandleRun();
1595 //
1596 //
1597 //
1598 if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1599 //
1600 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER evbefh = %u \n",evbefh);
1601 //
1602 if ( evbefh == 0 ) {
1603 //
1604 signal = 8;
1605 if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1606 //
1607 } else {
1608 //
1609 this->HandleRunFragments(true,true,0,(evbefh-1));
1610 //
1611 };
1612 };
1613 //
1614 //
1615 if ( (ptht - pth) > 1 ){
1616 //
1617 if ( IsDebug() ) printf(" Missing runtrailers! \n");
1618 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt);
1619 // is not the consecutive header
1620 while ( pth != ptht ){
1621 //
1622 // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1623 //
1624 pth++;
1625 //
1626 rh->GetEntry(pth+1);
1627 phh = ehh->GetPscuHeader();
1628 pktt = phh->GetCounter();
1629 obtt = phh->GetOrbitalTime();
1630 cod = ehh->GetCounter();
1631 evbeft = cod->Get(pctp->Physics);
1632 rh->GetEntry(pth);
1633 phh = ehh->GetPscuHeader();
1634 cod = ehh->GetCounter();
1635 pkth = phh->GetCounter();
1636 obth = phh->GetOrbitalTime();
1637 evbefh = cod->Get(pctp->Physics);
1638 //
1639 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %u %u %u \n",ptht,pth,ptt);
1640 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1641 //
1642 this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1643 //
1644 };
1645 //
1646 } else if ( !(ptht - pth) ){
1647 //
1648 if ( IsDebug() ) printf(" Missing runheader! \n");
1649 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt);
1650 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1651 //
1652 } else {
1653 //
1654 // go on with next header
1655 //
1656 pth = ptht;
1657 };
1658 //
1659 };
1660 //
1661 if ( ptt+1 == rtev){
1662 ptht++;
1663 if ( ptht < rhev ){
1664 rh->GetEntry(ptht);
1665 phh = ehh->GetPscuHeader();
1666 pkth = phh->GetCounter();
1667 obth = phh->GetOrbitalTime();
1668 cod = ehh->GetCounter();
1669 evbefh = cod->Get(pctp->Physics);
1670 if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
1671 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1672 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1673 //
1674 this->HandleRunFragments(false,true,evbefh,upperentry);
1675 } else {
1676 //
1677 // check if we have a fragment with no header
1678 //
1679 if ( (UInt_t)evbeft < upperentry-1 ){
1680 if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1681 //
1682 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1683 rt->GetEntry(ptt-1);
1684 cod = eht->GetCounter();
1685 evbefh = cod->Get(pctp->Physics);
1686 rt->GetEntry(ptt);
1687 pht = eht->GetPscuHeader();
1688 this->HandleRunFragments(true,true,evbefh,upperentry);
1689 };
1690 };
1691 };
1692 //
1693 };
1694 };
1695 //
1696 return(signal);
1697 };
1698
1699 /**
1700 *
1701 * Check if the run has already been inserted
1702 *
1703 */
1704 Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1705 //
1706 TSQLResult *result = 0;
1707 TSQLRow *row = 0;
1708 //
1709 stringstream oss;
1710 oss.str("");
1711 //
1712 // the where clause is of the type: boot_number = _our_boot && (
1713 // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1714 // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1715 // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1716 // ||
1717 // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1718 // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1719 // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1720 // ||
1721 // ( runhead_time = _our_runhead_time && runtrail_time = _our_runtrail_time && nevents > 100 )
1722 // )
1723 //
1724 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1725 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1726 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1727 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1728 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1729 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1730 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1731 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1732 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1733 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1734 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1735 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1736 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1737 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") OR "
1738 << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " // these two lines in a certain way disable the patch below...
1739 << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)" //
1740 << " ));";
1741 //
1742 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1743 result = conn->Query(oss.str().c_str());
1744 //
1745 if ( !result ) throw -4;
1746 //
1747 row = result->Next();
1748 //
1749 if ( !row ){
1750 if ( IsDebug() ) printf(" The run is new \n");
1751 if ( IsDebug() ) printf(" -> fill the DB \n");
1752 return(false); // the file has not been inserted in the DB, go on.
1753 };
1754 //
1755 Bool_t signal = true;
1756 //
1757 while ( row != NULL ){
1758 if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1759 //
1760 // the run has already been inserted
1761 //
1762 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1763 return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1764 //
1765 // PATCH!
1766 // 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
1767 // while the old run doesn't have it 3) we have more events than the old run
1768 //
1769 if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1770 //
1771 if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1772 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1773 // oss.str("");
1774 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1775 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1776 // conn->Query(oss.str().c_str());
1777 if ( signal ) signal = false;
1778 goto gonext;
1779 //
1780 } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1781 if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1782 if ( IsDebug() ) printf(" The run is already inserted \n");
1783 goto gonext;
1784 };
1785 //
1786 if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1787 //
1788 if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1789 //
1790 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1791 // oss.str("");
1792 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1793 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1794 // conn->Query(oss.str().c_str());
1795 //
1796 if ( signal ) signal = false;
1797 goto gonext;
1798 } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1799 if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1800 if ( IsDebug() ) printf(" The run is already inserted \n");
1801 goto gonext;
1802 };
1803 //
1804 if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1805 //
1806 if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1807 //
1808 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1809 // oss.str("");
1810 // oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1811 // if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1812 // conn->Query(oss.str().c_str());
1813 if ( signal ) signal = false;
1814 //
1815 };
1816 //
1817 gonext:
1818 // END PATCH!
1819 //
1820 row = result->Next();
1821 //
1822 };
1823 //
1824 delete result;
1825 //
1826 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1827 if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1828 return(signal);
1829 };
1830
1831 /**
1832 * Handle runs which seems to be good ones.
1833 **/
1834 void PamelaDBOperations::HandleRun(){
1835 ULong64_t chkpkt = 0;
1836 ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1837 ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1838 //
1839 chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1840 //
1841 if ( labs(chkpkt-pktt)<2 ){
1842 //
1843 if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1844 //
1845 // it must be a good run, fill the db
1846 //
1847 this->FillClass();
1848 //
1849 if ( !IsRunAlreadyInserted() ){
1850 glrun->SetID(this->AssignRunID());
1851 glrun->SetID_RUN_FRAG(0);
1852 glrun->Fill_GL_RUN(conn);
1853 };
1854 } else {
1855 //
1856 if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1857 if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1858 //
1859 this->HandleSuspiciousRun();
1860 //
1861 };
1862 //
1863 //
1864 return;
1865 };
1866
1867
1868 /**
1869 * Handle run fragments at the beginning or at the end of the file
1870 **/
1871 void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1872 //
1873 UInt_t rhfirstev = firstev;
1874 UInt_t rtlastev = lastev;
1875 Bool_t found = false;
1876 Bool_t foundinrun = false;
1877 //
1878 TSQLResult *result = 0;
1879 TSQLRow *row = 0;
1880 //
1881 stringstream oss;
1882 oss.str("");
1883 //
1884 // is the piece of run good (no other packets inside)?
1885 //
1886 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1887 //
1888 // if not, handle other pieces and continue with the first one
1889 //
1890 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1891 //
1892 };
1893 //
1894 // we have now the good first piece of a run, fill the glrun object
1895 //
1896 if ( rhfirstev != firstev && !mishead ) mishead = true;
1897 if ( rtlastev != lastev && !mistrail ) mistrail = true;
1898 //
1899 this->FillClass(mishead,mistrail,firstev,lastev);
1900 //
1901 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1902 //
1903 // First of all insert the run in the fragment table...
1904 //
1905 oss.str("");
1906 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1907 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1908 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1909 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1910 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1911 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1912 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1913 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1914 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1915 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1916 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1917 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1918 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1919 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1920 //
1921 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1922 result = conn->Query(oss.str().c_str());
1923 //
1924 if ( !result ) throw -4;
1925 //
1926 row = result->Next();
1927 //
1928 if ( !row ){
1929 //
1930 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1931 //
1932 if ( IsDebug() ) printf(" The run is new \n");
1933 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1934 //
1935 glrun->SetID(this->AssignRunID());
1936 glrun->SetID_RUN_FRAG(0);
1937 glrun->Fill_GL_RUN_FRAGMENTS(conn);
1938 //
1939 } else {
1940 if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1941 return;
1942 };
1943 //
1944 //
1945 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1946 //
1947 if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1948 // missing it no way we can found a piece in the frag table
1949 //
1950 oss.str("");
1951 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1952 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1953 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1954 << " ID != " << glrun->ID
1955 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1956 //
1957 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1958 result = conn->Query(oss.str().c_str());
1959 //
1960 if ( !result ) throw -4;
1961 //
1962 row = result->Next();
1963 //
1964 if ( !row && NoFrag() ){
1965 //
1966 oss.str("");
1967 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1968 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1969 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1970 << " ID != " << glrun->ID
1971 << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1972 //
1973 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1974 result = conn->Query(oss.str().c_str());
1975 //
1976 if ( !result ) throw -4;
1977 //
1978 foundinrun = true;
1979 //
1980 row = result->Next();
1981 //
1982 };
1983 //
1984 if ( !row ){
1985 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1986 found = false;
1987 } else {
1988 //
1989 found = false; // default value
1990 //
1991 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1992 //
1993 // if we have both runheader and runtrailer we can check with pkt_counter:
1994 //
1995 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1996 ULong64_t chkpkt = 0;
1997 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1998 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1999 //
2000 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
2001 //
2002 if ( labs(chkpkt-pktt)<2 ){
2003 //
2004 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2005 //
2006 found = true;
2007 //
2008 } else {
2009 //
2010 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2011 //
2012 found = false;
2013 //
2014 };
2015 };
2016 if ( !found ){
2017 //
2018 // 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
2019 //
2020 ULong64_t chkpkt1 = 0;
2021 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2022 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2023 chkpkt1 = labs(orunh1-dbrunt1);
2024 //
2025 ULong64_t chkpkt2 = 0;
2026 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
2027 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2028 chkpkt2 = labs(orunh2-dbrunt2);
2029 //
2030 ULong64_t chkpkt3 = 0;
2031 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
2032 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2033 chkpkt3 = labs(orunh3-dbrunt3);
2034 //
2035 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2036 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
2037 //
2038 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2039 //
2040 found = true;
2041 //
2042 } else {
2043 //
2044 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2045 //
2046 found = false;
2047 //
2048 };
2049 };
2050 };
2051 //
2052 if ( found ){
2053 //
2054 // 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
2055 //
2056 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2057 //
2058 if ( foundinrun ){
2059 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2060 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2061 };
2062 //
2063 GL_RUN *glrun1 = new GL_RUN();
2064 //
2065 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2066 //
2067 oss.str("");
2068 oss << " ID="<<row->GetField(0)<<";";
2069 //
2070 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
2071 //
2072 // merge infos
2073 //
2074 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
2075 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2076 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2077 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2078 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2079 TTree *T= 0;
2080 T = (TTree*)file->Get("Physics");
2081 if ( !T || T->IsZombie() ) throw -16;
2082 EventHeader *eh = 0;
2083 PscuHeader *ph = 0;
2084 T->SetBranchAddress("Header", &eh);
2085 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
2086 T->GetEntry(firstev);
2087 ph = eh->GetPscuHeader();
2088 bpkt = PKT(ph->GetCounter());
2089 bobt = OBT(ph->GetOrbitalTime());
2090 firstev++;
2091 };
2092 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2093 //
2094 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2095 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
2096 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
2097 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
2098 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
2099 //
2100 glrun->SetEV_FROM(firstev);
2101 glrun->SetNEVENTS(lastev-firstev+1);
2102 //
2103 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2104 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2105 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
2106 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
2107 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
2108 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
2109 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
2110 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
2111 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
2112 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
2113 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
2114 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
2115 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
2116 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
2117 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
2118 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
2119 //
2120 if ( !IsRunAlreadyInserted() ){
2121 //
2122 // glrun->SetID(this->AssignRunID());
2123 glrun->SetID_RUN_FRAG(glrun1->GetID());
2124 glrun->Fill_GL_RUN(conn);
2125 //
2126 // set id number
2127 //
2128 glrun1->SetID_RUN_FRAG(glrun->GetID());
2129 glrun1->Fill_GL_RUN(conn);
2130 //
2131 };
2132 // delete old entry in fragment table
2133 //
2134 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2135 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2136 //
2137 delete glrun1;
2138 //
2139 //
2140 return;
2141 //
2142 };
2143 //
2144 };
2145 //
2146 if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2147 // missing it no way we can found a piece in the frag table
2148 //
2149 oss.str("");
2150 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2151 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2152 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2153 << " ID != " << glrun->ID
2154 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2155 //
2156 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
2157 result = conn->Query(oss.str().c_str());
2158 //
2159 if ( !result ) throw -4;
2160 //
2161 row = result->Next();
2162 //
2163 if ( !row && NoFrag() ){
2164 //
2165 oss.str("");
2166 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2167 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2168 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2169 << " ID != " << glrun->ID
2170 << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2171 //
2172 if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2173 result = conn->Query(oss.str().c_str());
2174 //
2175 if ( !result ) throw -4;
2176 //
2177 foundinrun = true;
2178 row = result->Next();
2179 //
2180 };
2181 //
2182 if ( !row ){
2183 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2184 found = false;
2185 } else {
2186 //
2187 found = false; // default value
2188 //
2189 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
2190 //
2191 // if we have both runheader and runtrailer we can check with pkt_counter:
2192 //
2193 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
2194 ULong64_t chkpkt = 0;
2195 ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
2196 ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
2197 //
2198 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
2199 //
2200 if ( labs(chkpkt-pktt)<2 ){
2201 //
2202 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
2203 //
2204 found = true;
2205 //
2206 } else {
2207 //
2208 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
2209 //
2210 found = false;
2211 //
2212 };
2213 };
2214 if ( !found ){
2215 //
2216 // 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
2217 //
2218 ULong64_t chkpkt1 = 0;
2219 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
2220 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
2221 chkpkt1 = labs(orunh1-dbrunt1);
2222 //
2223 ULong64_t chkpkt2 = 0;
2224 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
2225 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
2226 chkpkt2 = labs(orunh2-dbrunt2);
2227 //
2228 ULong64_t chkpkt3 = 0;
2229 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
2230 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
2231 chkpkt3 = labs(orunh3-dbrunt3);
2232 //
2233 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
2234 //
2235 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
2236 //
2237 found = true;
2238 //
2239 } else {
2240 //
2241 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
2242 //
2243 found = false;
2244 //
2245 };
2246 };
2247 };
2248 //
2249 if ( found ){
2250 //
2251 // 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
2252 //
2253 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2254 //
2255 if ( foundinrun ){
2256 glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2257 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2258 };
2259 //
2260 GL_RUN *glrun1 = new GL_RUN();
2261 //
2262 // UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2263 //
2264 oss.str("");
2265 oss << " ID="<<row->GetField(0)<<";";
2266 //
2267 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
2268 //
2269 // merge infos
2270 //
2271 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
2272 ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2273 UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2274 ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2275 if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2276 TTree *T= 0;
2277 T = (TTree*)file->Get("Physics");
2278 if ( !T || T->IsZombie() ) throw -16;
2279 EventHeader *eh = 0;
2280 PscuHeader *ph = 0;
2281 T->SetBranchAddress("Header", &eh);
2282 while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
2283 T->GetEntry(lastev);
2284 ph = eh->GetPscuHeader();
2285 apkt = PKT(ph->GetCounter());
2286 aobt = OBT(ph->GetOrbitalTime());
2287 lastev--;
2288 };
2289 if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2290 //
2291 glrun->SetEV_TO(lastev);
2292 glrun->SetNEVENTS(lastev-firstev+1);
2293 glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
2294 glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
2295 glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
2296 glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2297 glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2298 //
2299 glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2300 glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2301 glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
2302 glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
2303 glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
2304 glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
2305 glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
2306 glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
2307 glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
2308 glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
2309 glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
2310 glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
2311 glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
2312 glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
2313 glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
2314 glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
2315 //
2316 if ( !IsRunAlreadyInserted() ){
2317 //
2318 // glrun->SetID(this->AssignRunID());
2319 //
2320 glrun->SetID_RUN_FRAG(glrun1->GetID());
2321 glrun->Fill_GL_RUN(conn);
2322 //
2323 // set id number
2324 //
2325 glrun1->SetID_RUN_FRAG(glrun->GetID());
2326 glrun1->Fill_GL_RUN(conn);
2327 //
2328 };
2329 //
2330 // delete old entries in fragment table
2331 //
2332 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2333 glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2334 //
2335 delete glrun1;
2336 //
2337 return;
2338 //
2339 };
2340 //
2341 };
2342 //
2343 if ( !found ){
2344 //
2345 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2346 //
2347 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2348 //
2349 oss.str("");
2350 oss << " SELECT ID FROM GL_RUN WHERE "
2351 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2352 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2353 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2354 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2355 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2356 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2357 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2358 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2359 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2360 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2361 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2362 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2363 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2364 //
2365 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2366 result = conn->Query(oss.str().c_str());
2367 //
2368 if ( !result ) throw -4;
2369 //
2370 row = result->Next();
2371 //
2372 if ( row ){
2373 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2374 } else {
2375 if ( NoFrag() ){
2376 glrun->SetID_RUN_FRAG(glrun->GetID());
2377 glrun->Fill_GL_RUN(conn);
2378 glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2379 };
2380 };
2381 };
2382 //
2383 return;
2384 };
2385
2386
2387 /**
2388 * Handle run without header or trailer
2389 **/
2390 void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2391 //
2392 //
2393 // is the piece of run good (no other packets inside)?
2394 //
2395 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2396 //
2397 // if not, handle other pieces and continue with the first one
2398 //
2399 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2400 //
2401 } else {
2402 //
2403 this->FillClass(mishead,mistrail,firstev,lastev);
2404 //
2405 if ( !IsRunAlreadyInserted() ){
2406 glrun->SetID(this->AssignRunID());
2407 glrun->SetID_RUN_FRAG(0);
2408 glrun->Fill_GL_RUN(conn);
2409 };
2410 //
2411 };
2412 //
2413 return;
2414 };
2415
2416 /**
2417 *
2418 * check if we have non-physics packets inside the run
2419 *
2420 */
2421 Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2422 //
2423 EventCounter *code=0;
2424 //
2425 UInt_t nevent = 0;
2426 UInt_t checkfirst = 0;
2427 UInt_t checklast = 0;
2428 UInt_t firstentry = 0;
2429 UInt_t lastentry = 0;
2430 UInt_t firstTime = 0;
2431 UInt_t lastTime = 0;
2432 UInt_t firstPkt = 0;
2433 UInt_t lastPkt = 0;
2434 UInt_t firstObt = 0;
2435 UInt_t lastObt = 0;
2436 //
2437 pcksList packetsNames;
2438 pcksList::iterator Iter;
2439 getPacketsNames(packetsNames);
2440 //
2441 TTree *T= 0;
2442 T =(TTree*)file->Get("Physics");
2443 if ( !T || T->IsZombie() ) throw -16;
2444 EventHeader *eh = 0;
2445 PscuHeader *ph = 0;
2446 T->SetBranchAddress("Header", &eh);
2447 nevent = T->GetEntries();
2448 //
2449 //
2450 if ( firstev == lastev+1 ) { // no events inside the run!
2451 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2452 // return true is correct
2453 return(true);
2454 //
2455 } else {
2456 //
2457 T->GetEntry(firstev);
2458 code = eh->GetCounter();
2459 checkfirst = 0;
2460 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2461 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2462 };
2463 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2464 //
2465 T->GetEntry(lastev);
2466 code = eh->GetCounter();
2467 checklast = 0;
2468 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2469 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2470 };
2471 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2472 //
2473 if ( checkfirst == checklast ){
2474 //
2475 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2476 //
2477 return(true);
2478 //
2479 } else {
2480 //
2481 if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2482 //
2483 // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2484 //
2485 if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2486 //
2487 Bool_t emptyruns = false;
2488 UInt_t check = 0;
2489 UInt_t lastevtemp = lastev;
2490 UInt_t firstevno = firstev;
2491 //
2492 for (UInt_t i=firstev; i<=lastev; i++){
2493 //
2494 T->GetEntry(i);
2495 code = eh->GetCounter();
2496 //
2497 check = 0;
2498 //
2499 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2500 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2501 };
2502 //
2503 if ( checkfirst < check || i == lastev ){
2504 //
2505 firstentry = firstevno;
2506 //
2507 if ( checkfirst < check ){
2508 lastentry = i-1;
2509 } else {
2510 lastentry = i;
2511 };
2512 //
2513 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2514 //
2515 glrun->SetEV_FROM(firstentry);
2516 glrun->SetEV_TO(lastentry);
2517 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2518 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2519 lastentry--;
2520 };
2521 glrun->SetNEVENTS(lastentry-firstentry+1);
2522 //
2523 glrun->Set_GL_RUNH0();
2524 glrun->Set_GL_RUNT0();
2525 //
2526 glrun->SetLAST_TIMESYNC(0);
2527 glrun->SetOBT_TIMESYNC(0);
2528 //
2529 T->GetEntry(firstentry);
2530 ph = eh->GetPscuHeader();
2531 firstObt = ph->GetOrbitalTime();
2532 firstTime = this->GetAbsTime(firstObt);
2533 firstPkt = ph->GetCounter();
2534 //
2535 T->GetEntry(lastentry);
2536 ph = eh->GetPscuHeader();
2537 lastObt = ph->GetOrbitalTime();
2538 lastTime = this->GetAbsTime(lastObt);
2539 lastPkt = ph->GetCounter();
2540 //
2541 glrun->SetRUNHEADER_PKT(firstPkt);
2542 glrun->SetRUNTRAILER_PKT(lastPkt);
2543 //
2544 glrun->SetRUNHEADER_OBT(firstObt);
2545 glrun->SetRUNTRAILER_OBT(lastObt);
2546 //
2547 if ( firstev == firstentry && !emptyruns && !mishead ){
2548 glrun->Set_GL_RUNH(runh,phh);
2549 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2550 if ( IsDebug() ) printf(" We have the runheader \n");
2551 };
2552 if ( lastev == i && !mistrail ){
2553 glrun->Set_GL_RUNT(runt,pht);
2554 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2555 if ( IsDebug() ) printf(" We have the runtrailer \n");
2556 };
2557 //
2558 if ( lastentry == (firstentry-2) ){ // no events in the run
2559 emptyruns = true;
2560 if ( IsDebug() ) printf(" No events in the run \n");
2561 lastTime = firstTime;
2562 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2563 lastObt = glrun->RUNHEADER_OBT;
2564 lastPkt = glrun->RUNHEADER_PKT;
2565 } else {
2566 lastObt = firstObt;
2567 lastPkt = firstPkt;
2568 };
2569 glrun->SetRUNTRAILER_PKT(lastPkt);
2570 glrun->SetRUNTRAILER_OBT(lastObt);
2571 lastentry++;
2572 };
2573 //
2574 this->SetCommonGLRUN(firstTime,lastTime);
2575 //
2576 if ( !IsRunAlreadyInserted() ){
2577 glrun->SetID(this->AssignRunID());
2578 glrun->SetID_RUN_FRAG(0);
2579 glrun->Fill_GL_RUN(conn);
2580 };
2581 //
2582 firstevno = lastentry + 1;
2583 //
2584 checkfirst = check;
2585 //
2586 };
2587 //
2588 if ( check == checklast && i != lastev ){
2589 lastevtemp = i - 1;
2590 i = lastev - 1;
2591 };
2592 //
2593 };
2594 //
2595 lastev = lastevtemp;
2596 //
2597 return(false);
2598 //
2599 };
2600 };
2601 //
2602 return(false); // should never arrive here
2603 };
2604
2605 /**
2606 *
2607 * 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
2608 * 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
2609 * looking for non-physics packets inside.
2610 *
2611 */
2612 void PamelaDBOperations::HandleSuspiciousRun(){
2613 //
2614 PacketType *pctp=0;
2615 EventCounter *codt=0;
2616 EventCounter *codh=0;
2617 EventCounter *code=0;
2618 UInt_t firstev = 0;
2619 UInt_t lastev = 0;
2620 UInt_t nevent = 0;
2621 UInt_t checkfirst = 0;
2622 UInt_t checklast = 0;
2623 UInt_t firstentry = 0;
2624 UInt_t lastentry = 0;
2625 UInt_t firstTime = 0;
2626 UInt_t lastTime = 0;
2627 UInt_t firstPkt = 0;
2628 UInt_t lastPkt = 0;
2629 UInt_t firstObt = 0;
2630 UInt_t lastObt = 0;
2631 //
2632 pcksList packetsNames;
2633 pcksList::iterator Iter;
2634 getPacketsNames(packetsNames);
2635 //
2636 TTree *rh=0;
2637 rh = (TTree*)file->Get("RunHeader");
2638 if ( !rh || rh->IsZombie() ) throw -17;
2639 TTree *T=0;
2640 T =(TTree*)file->Get("Physics");
2641 if ( !T || T->IsZombie() ) throw -16;
2642 EventHeader *eh = 0;
2643 PscuHeader *ph = 0;
2644 T->SetBranchAddress("Header", &eh);
2645 nevent = T->GetEntries();
2646 //
2647 codt = eht->GetCounter();
2648 codh = ehh->GetCounter();
2649 firstev = codh->Get(pctp->Physics);
2650 lastev = codt->Get(pctp->Physics)-1;
2651 //
2652 if ( firstev == lastev+1 ) { // no events inside the run!
2653 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2654 //
2655 this->FillClass();
2656 if ( !IsRunAlreadyInserted() ){
2657 glrun->SetID(this->AssignRunID());
2658 glrun->SetID_RUN_FRAG(0);
2659 glrun->Fill_GL_RUN(conn);
2660 };
2661 //
2662 } else {
2663 //
2664 UInt_t nrunh = 0;
2665 UInt_t nrunh1 = 0;
2666 T->GetEntry(firstev);
2667 code = eh->GetCounter();
2668 checkfirst = 0;
2669 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2670 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2671 if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2672 };
2673 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2674 //
2675 T->GetEntry(lastev);
2676 code = eh->GetCounter();
2677 checklast = 0;
2678 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2679 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2680 };
2681 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2682 //
2683 if ( checkfirst == checklast ){
2684 //
2685 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2686 //
2687 this->FillClass();
2688 if ( !IsRunAlreadyInserted() ){
2689 glrun->SetID(this->AssignRunID());
2690 glrun->SetID_RUN_FRAG(0);
2691 glrun->Fill_GL_RUN(conn);
2692 };
2693 //
2694 } else {
2695 //
2696 if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2697 //
2698 Bool_t emptyruns = false;
2699 UInt_t check = 0;
2700 UInt_t firstevno = firstev;
2701 //
2702 for (UInt_t i=firstev; i<=lastev; i++){
2703 //
2704 T->GetEntry(i);
2705 code = eh->GetCounter();
2706 //
2707 check = 0;
2708 //
2709 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2710 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2711 if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2712 };
2713 //
2714 if ( checkfirst < check || i == lastev ){
2715 //
2716 firstentry = firstevno;
2717 //
2718 if ( checkfirst < check ){
2719 lastentry = i-1;
2720 } else {
2721 lastentry = i;
2722 };
2723 //
2724 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2725 //
2726 glrun->SetEV_FROM(firstentry);
2727 glrun->SetEV_TO(lastentry);
2728 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2729 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2730 lastentry--;
2731 };
2732 glrun->SetNEVENTS(lastentry-firstentry+1);
2733 //
2734 glrun->Set_GL_RUNH0();
2735 glrun->Set_GL_RUNT0();
2736 //
2737 glrun->SetLAST_TIMESYNC(0);
2738 glrun->SetOBT_TIMESYNC(0);
2739 //
2740 T->GetEntry(firstentry);
2741 ph = eh->GetPscuHeader();
2742 firstObt = ph->GetOrbitalTime();
2743 firstTime = this->GetAbsTime(firstObt);
2744 firstPkt = ph->GetCounter();
2745 //
2746 T->GetEntry(lastentry);
2747 ph = eh->GetPscuHeader();
2748 lastObt = ph->GetOrbitalTime();
2749 lastTime = this->GetAbsTime(lastObt);
2750 lastPkt = ph->GetCounter();
2751 //
2752 glrun->SetRUNHEADER_PKT(firstPkt);
2753 glrun->SetRUNTRAILER_PKT(lastPkt);
2754 //
2755 glrun->SetRUNHEADER_OBT(firstObt);
2756 glrun->SetRUNTRAILER_OBT(lastObt);
2757 //
2758 if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2759 rh->GetEntry(nrunh1-1);
2760 phh = ehh->GetPscuHeader();
2761 nrunh1++;
2762 glrun->Set_GL_RUNH(runh,phh);
2763 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2764 if ( IsDebug() ) printf(" We have the runheader \n");
2765 };
2766 if ( lastev == i && checkfirst == check ){
2767 glrun->Set_GL_RUNT(runt,pht);
2768 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2769 if ( IsDebug() ) printf(" We have the runtrailer \n");
2770 };
2771 //
2772 if ( lastentry == (firstentry-2) ){ // no events in the run
2773 emptyruns = true;
2774 if ( IsDebug() ) printf(" No events in the run \n");
2775 lastTime = firstTime;
2776 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2777 lastObt = glrun->RUNHEADER_OBT;
2778 lastPkt = glrun->RUNHEADER_PKT;
2779 } else {
2780 lastObt = firstObt;
2781 lastPkt = firstPkt;
2782 };
2783 glrun->SetRUNTRAILER_PKT(lastPkt);
2784 glrun->SetRUNTRAILER_OBT(lastObt);
2785 lastentry++;
2786 };
2787 //
2788 this->SetCommonGLRUN(firstTime,lastTime);
2789 //
2790 if ( !IsRunAlreadyInserted() ){
2791 glrun->SetID(this->AssignRunID());
2792 glrun->SetID_RUN_FRAG(0);
2793 glrun->Fill_GL_RUN(conn);
2794 };
2795 //
2796 if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2797 //
2798 firstentry = i;
2799 //
2800 lastentry = i;
2801 //
2802 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2803 //
2804 glrun->SetEV_FROM(firstentry);
2805 glrun->SetEV_TO(lastentry);
2806 glrun->SetNEVENTS(lastentry-firstentry+1);
2807 //
2808 glrun->Set_GL_RUNH0();
2809 //
2810 glrun->SetLAST_TIMESYNC(0);
2811 glrun->SetOBT_TIMESYNC(0);
2812 //
2813 T->GetEntry(firstentry);
2814 ph = eh->GetPscuHeader();
2815 firstObt = ph->GetOrbitalTime();
2816 firstTime = this->GetAbsTime(firstObt);
2817 firstPkt = ph->GetCounter();
2818 //
2819 glrun->SetRUNHEADER_PKT(firstPkt);
2820 //
2821 glrun->SetRUNHEADER_OBT(firstObt);
2822 //
2823 glrun->Set_GL_RUNT(runt,pht);
2824 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2825 if ( IsDebug() ) printf(" We have the runtrailer \n");
2826 //
2827 this->SetCommonGLRUN(firstTime,lastTime);
2828 //
2829 if ( !IsRunAlreadyInserted() ){
2830 glrun->SetID(this->AssignRunID());
2831 glrun->SetID_RUN_FRAG(0);
2832 glrun->Fill_GL_RUN(conn);
2833 };
2834 };
2835 //
2836 firstevno = lastentry + 1;
2837 //
2838 checkfirst = check;
2839 //
2840 };
2841 //
2842 if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2843 //
2844 };
2845 };
2846 };
2847 //
2848 return;
2849 };
2850
2851
2852 /**
2853 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2854 */
2855 Int_t PamelaDBOperations::insertCALO_CALIB(){
2856 //
2857 TSQLResult *result = 0;
2858 TSQLRow *row = 0;
2859 //
2860 stringstream oss;
2861 oss.str("");
2862 //
2863 CalibCalPedEvent *calibCalPed = 0;
2864 TTree *tr = 0;
2865 EventHeader *eh = 0;
2866 PscuHeader *ph = 0;
2867 //
2868 UInt_t nevents = 0;
2869 UInt_t fromtime = 0;
2870 UInt_t totime = 0;
2871 UInt_t obt = 0;
2872 UInt_t pkt = 0;
2873 //
2874 tr = (TTree*)file->Get("CalibCalPed");
2875 if ( !tr || tr->IsZombie() ) throw -21;
2876 //
2877 tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2878 tr->SetBranchAddress("Header", &eh);
2879 nevents = tr->GetEntries();
2880 //
2881 if ( !nevents ) return(1);
2882 //
2883 for (UInt_t i=0; i < nevents; i++){
2884 tr->GetEntry(i);
2885 for (UInt_t section = 0; section < 4; section++){
2886 //
2887 if ( calibCalPed->cstwerr[section] ){
2888 valid = 1;
2889 if ( calibCalPed->cperror[section] ) valid = 0;
2890 ph = eh->GetPscuHeader();
2891 obt = ph->GetOrbitalTime();
2892 pkt = ph->GetCounter();
2893 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2894 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2895 //
2896 if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2897 //
2898 // check if the calibration has already been inserted
2899 //
2900 oss.str("");
2901 oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2902 << " SECTION = "<< section << " AND "
2903 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2904 << " OBT = "<< obt << " AND "
2905 << " PKT = "<< pkt << ";";
2906 //
2907 if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2908 result = conn->Query(oss.str().c_str());
2909 //
2910 if ( !result ) throw -4;
2911 //
2912 row = result->Next();
2913 //
2914 if ( row ){
2915 //
2916 if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2917 //
2918 } else {
2919 //
2920 // we have to insert a new calibration, check where to place it
2921 //
2922 oss.str("");
2923 oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2924 << " SECTION = "<< section << " AND "
2925 << " FROM_TIME < "<< fromtime << " AND "
2926 << " TO_TIME > "<< fromtime << ";";
2927 //
2928 if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2929 result = conn->Query(oss.str().c_str());
2930 //
2931 if ( !result ) throw -4;
2932 //
2933 row = result->Next();
2934 //
2935 if ( !row ){
2936 //
2937 // no calibrations in the db contain our calibration
2938 //
2939 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2940 if ( fromtime < 1150871000 ){ //1150866904
2941 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2942 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2943 };
2944 //
2945 oss.str("");
2946 oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2947 << " SECTION = "<< section << " AND "
2948 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2949 //
2950 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2951 result = conn->Query(oss.str().c_str());
2952 //
2953 if ( !result ) throw -4;
2954 //
2955 row = result->Next();
2956 if ( !row ){
2957 totime = numeric_limits<UInt_t>::max();
2958 } else {
2959 totime = (UInt_t)atoll(row->GetField(0));
2960 };
2961 //
2962 } else {
2963 //
2964 // determine upper and lower limits and make space for the new calibration
2965 //
2966 totime = (UInt_t)atoll(row->GetField(1));
2967 //
2968 oss.str("");
2969 oss << " UPDATE GL_CALO_CALIB SET "
2970 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2971 << " ID = "<< row->GetField(0) << ";";
2972 //
2973 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2974 result = conn->Query(oss.str().c_str());
2975 //
2976 if ( !result ) throw -4;
2977 //
2978 };
2979 //
2980 oss.str("");
2981 oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2982 << " VALUES (NULL,' "
2983 << idroot << "','"
2984 << i << "','"
2985 << fromtime << "','"
2986 << totime << "','"
2987 << section << "','"
2988 << obt << "','"
2989 << pkt << "','"
2990 << this->GetBOOTnumber() << "','"
2991 << valid << "');";
2992 //
2993 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2994 //
2995 result = conn->Query(oss.str().c_str());
2996 //
2997 if ( !result ) throw -4;
2998 //
2999 };
3000 //
3001 } else {
3002 //
3003 if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3004 //
3005 };
3006 //
3007 };
3008 };
3009 };
3010 //
3011 return(0);
3012 };
3013
3014 /**
3015 * Fill the GL_TRK_CALIB table
3016 */
3017 void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
3018 //
3019 TSQLResult *result = 0;
3020 TSQLRow *row = 0;
3021 //
3022 stringstream oss;
3023 oss.str("");
3024 //
3025 UInt_t totime = 0;
3026 //
3027 if ( !pk1 && !pk2 ){
3028 if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
3029 };
3030 //
3031 // check if the calibration has already been inserted
3032 //
3033 oss.str("");
3034 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
3035 << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
3036 oss << " AND ( ( ";
3037 if ( pk1 ){
3038 oss << " OBT1 = "<< obt1 << " AND "
3039 << " PKT1 = "<< pkt1
3040 << " ) OR ( ";
3041 } else {
3042 oss << " PKT1 = "<< pkt2-1
3043 << " ) OR ( ";
3044 };
3045 if ( pk2 ){
3046 oss << " OBT2 = "<< obt2 << " AND "
3047 << " PKT2 = "<< pkt2;
3048 } else {
3049 oss << " PKT2 = "<< pkt1+1;
3050 };
3051 oss << " ) );";
3052 //
3053 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3054 result = conn->Query(oss.str().c_str());
3055 //
3056 if ( !result ) throw -4;
3057 //
3058 row = result->Next();
3059 //
3060 if ( row ){
3061 //
3062 if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
3063 //
3064 } else {
3065 //
3066 // we have to insert a new calibration, check where to place it
3067 //
3068 oss.str("");
3069 oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
3070 << " FROM_TIME < "<< fromtime << " AND "
3071 << " TO_TIME > "<< fromtime << ";";
3072 //
3073 if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
3074 result = conn->Query(oss.str().c_str());
3075 //
3076 if ( !result ) throw -4;
3077 //
3078 row = result->Next();
3079 //
3080 if ( !row ){
3081 //
3082 // no calibrations in the db contain our calibration
3083 //
3084 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
3085 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
3086 //
3087 oss.str("");
3088 oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
3089 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3090 //
3091 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3092 result = conn->Query(oss.str().c_str());
3093 //
3094 if ( !result ) throw -4;
3095 //
3096 row = result->Next();
3097 if ( !row ){
3098 totime = numeric_limits<UInt_t>::max();
3099 } else {
3100 totime = (UInt_t)atoll(row->GetField(0));
3101 };
3102 //
3103 } else {
3104 //
3105 // determine upper and lower limits and make space for the new calibration
3106 //
3107 totime = (UInt_t)atoll(row->GetField(1));
3108 //
3109 oss.str("");
3110 oss << " UPDATE GL_TRK_CALIB SET "
3111 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3112 << " ID = "<< row->GetField(0) << ";";
3113 //
3114 if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
3115 result = conn->Query(oss.str().c_str());
3116 //
3117 if ( !result ) throw -4;
3118 //
3119 };
3120 //
3121 oss.str("");
3122 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) "
3123 << " VALUES (NULL,' "
3124 << idroot << "',";
3125 //
3126 if ( !pk1 ){
3127 oss << "NULL,";
3128 } else {
3129 oss << "'"
3130 << t1 << "',";
3131 };
3132 //
3133 if ( !pk2 ){
3134 oss << "NULL,'";
3135 } else {
3136 oss << "'"
3137 << t2 << "','";
3138 };
3139 //
3140 oss << fromtime << "','"
3141 << totime << "','"
3142 << obt1 << "','"
3143 << pkt1 << "','"
3144 << obt2 << "','"
3145 << pkt2 << "','"
3146 << this->GetBOOTnumber() << "','"
3147 << valid << "');";
3148 //
3149 if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
3150 //
3151 result = conn->Query(oss.str().c_str());
3152 //
3153 if ( !result ) throw -4;
3154 //
3155 };
3156 //
3157 };
3158
3159 /**
3160 * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
3161 */
3162 Int_t PamelaDBOperations::insertTRK_CALIB(){
3163 //
3164 CalibTrk1Event *caltrk1 = 0;
3165 CalibTrk2Event *caltrk2 = 0;
3166 TTree *tr1 = 0;
3167 TTree *tr2 = 0;
3168 EventHeader *eh1 = 0;
3169 PscuHeader *ph1 = 0;
3170 EventHeader *eh2 = 0;
3171 PscuHeader *ph2 = 0;
3172 //
3173 PacketType *pctp=0;
3174 EventCounter *codt2=0;
3175 //
3176 Int_t nevents1 = 0;
3177 Int_t nevents2 = 0;
3178 //
3179 fromtime = 0;
3180 //
3181 obt1 = 0;
3182 pkt1 = 0;
3183 obt2 = 0;
3184 pkt2 = 0;
3185 //
3186 tr1 = (TTree*)file->Get("CalibTrk1");
3187 if ( !tr1 || tr1->IsZombie() ) throw -22;
3188 tr2 = (TTree*)file->Get("CalibTrk2");
3189 if ( !tr2 || tr2->IsZombie() ) throw -23;
3190 //
3191 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
3192 tr1->SetBranchAddress("Header", &eh1);
3193 nevents1 = tr1->GetEntries();
3194 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
3195 tr2->SetBranchAddress("Header", &eh2);
3196 nevents2 = tr2->GetEntries();
3197 //
3198 if ( !nevents1 && !nevents2 ) return(1);
3199 //
3200 t2 = -1;
3201 Int_t pret2 = 0;
3202 Int_t t2t1cal = 0;
3203 //
3204 for (t1=0; t1 < nevents1; t1++){
3205 //
3206 pret2 = t2;
3207 tr1->GetEntry(t1);
3208 //
3209 ph1 = eh1->GetPscuHeader();
3210 obt1 = ph1->GetOrbitalTime();
3211 pkt1 = ph1->GetCounter();
3212 fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
3213 //
3214 // valid = 1;
3215 // //
3216 // if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3217 //
3218 //
3219 if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3220 //
3221 if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3222 //
3223 valid = ValidateTrkCalib( caltrk1, eh1 );
3224 if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3225 //
3226 // Do we have the second calibration packet?
3227 //
3228 while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
3229 //
3230 t2++;
3231 //
3232 if ( t2 < nevents2 ){
3233 tr2->GetEntry(t2);
3234 codt2 = eh2->GetCounter();
3235 t2t1cal = codt2->Get(pctp->CalibTrk1);
3236 //
3237 ph2 = eh2->GetPscuHeader();
3238 obt2 = ph2->GetOrbitalTime();
3239 pkt2 = ph2->GetCounter();
3240 //
3241 // if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3242 //
3243 } else {
3244 //
3245 // running out of vector without finding the corresponding calibration, sig
3246 //
3247 pret2 = t2;
3248 obt2 = 0;
3249 pkt2 = pkt1+2;
3250 t2t1cal = t1+1;
3251 };
3252 if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
3253 //
3254 // running out of vector without finding the corresponding calibration, sig
3255 //
3256 pret2 = t2;
3257 obt2 = 0;
3258 pkt2 = pkt1+2;
3259 t2t1cal = t1+1;
3260 };
3261 //
3262 };
3263 //
3264 if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
3265 //
3266 // The calibration is good
3267 //
3268 if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3269 //
3270 if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
3271 //
3272 UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3273 if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3274 valid = valid & valid2;
3275 //
3276 // Handle good calib
3277 //
3278 this->HandleTRK_CALIB(true,true);
3279 //
3280 // Check for missing calibtrk1
3281 //
3282 if ( t2 != pret2+1 ){
3283 //
3284 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);
3285 //
3286 while ( t2 > pret2+1 ){
3287 //
3288 // handle missing calib1
3289 //
3290 pret2++;
3291 //
3292 obt1 = 0;
3293 pkt1 = 0;
3294 //
3295 tr2->GetEntry(pret2);
3296 ph2 = eh2->GetPscuHeader();
3297 obt2 = ph2->GetOrbitalTime();
3298 pkt2 = ph2->GetCounter();
3299 //
3300 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3301 //
3302 valid = 0;
3303 this->HandleTRK_CALIB(false,true);
3304 //
3305 };
3306 //
3307 };
3308 //
3309 } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
3310 //
3311 // Check for missing calibtrk2
3312 //
3313 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3314 t2 = pret2;
3315 //
3316 // handle missing calib2
3317 //
3318 obt2 = 0;
3319 pkt2 = 0;
3320 valid = 0;
3321 this->HandleTRK_CALIB(true,false);
3322 //
3323 };
3324 //
3325 } else {
3326 //
3327 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3328 //
3329 };
3330 //
3331 };
3332 //
3333 // we have one more calib pkt2 !
3334 //
3335 t2++;
3336 while ( t2 < nevents2 ){
3337 //
3338 // handle missing calib1
3339 //
3340 obt1 = 0;
3341 pkt1 = 0;
3342 //
3343 tr2->GetEntry(t2);
3344 ph2 = eh2->GetPscuHeader();
3345 obt2 = ph2->GetOrbitalTime();
3346 pkt2 = ph2->GetCounter();
3347 //
3348 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3349 valid = 0;
3350 if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3351 //
3352 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3353 //
3354 this->HandleTRK_CALIB(false,true);
3355 //
3356 };
3357 //
3358 t2++;
3359 //
3360 };
3361 //
3362 return(0);
3363 };
3364
3365
3366 /**
3367 * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3368 */
3369 Int_t PamelaDBOperations::insertS4_CALIB(){
3370 //
3371 TSQLResult *result = 0;
3372 TSQLRow *row = 0;
3373 //
3374 stringstream oss;
3375 oss.str("");
3376 //
3377 TTree *tr = 0;
3378 EventHeader *eh = 0;
3379 PscuHeader *ph = 0;
3380 //
3381 UInt_t nevents = 0;
3382 UInt_t fromtime = 0;
3383 UInt_t totime = 0;
3384 UInt_t obt = 0;
3385 UInt_t pkt = 0;
3386 //
3387 tr = (TTree*)file->Get("CalibS4");
3388 if ( !tr || tr->IsZombie() ) throw -24;
3389 //
3390 tr->SetBranchAddress("Header", &eh);
3391 //
3392 nevents = tr->GetEntries();
3393 //
3394 if ( !nevents ) return(1);
3395 //
3396 for (UInt_t i = 0; i < nevents; i++){
3397 //
3398 tr->GetEntry(i);
3399 //
3400 ph = eh->GetPscuHeader();
3401 obt = ph->GetOrbitalTime();
3402 pkt = ph->GetCounter();
3403 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3404 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3405 //
3406 if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3407 //
3408 // check if the calibration has already been inserted
3409 //
3410 oss.str("");
3411 oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3412 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3413 << " OBT = "<< obt << " AND "
3414 << " PKT = "<< pkt << ";";
3415 //
3416 if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3417 result = conn->Query(oss.str().c_str());
3418 //
3419 if ( !result ) throw -4;
3420 //
3421 row = result->Next();
3422 //
3423 if ( row ){
3424 //
3425 if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3426 //
3427 } else {
3428 //
3429 // we have to insert a new calibration, check where to place it
3430 //
3431 oss.str("");
3432 oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3433 << " FROM_TIME < "<< fromtime << " AND "
3434 << " TO_TIME > "<< fromtime << ";";
3435 //
3436 if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3437 result = conn->Query(oss.str().c_str());
3438 //
3439 if ( !result ) throw -4;
3440 //
3441 row = result->Next();
3442 //
3443 if ( !row ){
3444 //
3445 // no calibrations in the db contain our calibration
3446 //
3447 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3448 if ( fromtime < 1150871000 ){
3449 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3450 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3451 };
3452 //
3453 oss.str("");
3454 oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3455 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3456 //
3457 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3458 result = conn->Query(oss.str().c_str());
3459 //
3460 if ( !result ) throw -4;
3461 //
3462 row = result->Next();
3463 if ( !row ){
3464 totime = numeric_limits<UInt_t>::max();
3465 } else {
3466 totime = (UInt_t)atoll(row->GetField(0));
3467 };
3468 //
3469 } else {
3470 //
3471 // determine upper and lower limits and make space for the new calibration
3472 //
3473 totime = (UInt_t)atoll(row->GetField(1));
3474 //
3475 oss.str("");
3476 oss << " UPDATE GL_S4_CALIB SET "
3477 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3478 << " ID = "<< row->GetField(0) << ";";
3479 //
3480 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3481 result = conn->Query(oss.str().c_str());
3482 //
3483 if ( !result ) throw -4;
3484 //
3485 };
3486 //
3487 oss.str("");
3488 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3489 << " VALUES (NULL,' "
3490 << idroot << "','"
3491 << i << "','"
3492 << fromtime << "','"
3493 << totime << "','"
3494 << obt << "','"
3495 << pkt << "','"
3496 << this->GetBOOTnumber() << "');";
3497 //
3498 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3499 //
3500 result = conn->Query(oss.str().c_str());
3501 //
3502 if ( !result ) throw -4;
3503 //
3504 };
3505 //
3506 } else {
3507 //
3508 if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3509 //
3510 };
3511 //
3512 };
3513 //
3514 return(0);
3515 };
3516
3517 /**
3518 * Scan the fragment table and move old fragments to the GL_RUN table
3519 */
3520 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3521 return(this->CleanGL_RUN_FRAGMENTS(""));
3522 };
3523
3524 /**
3525 * Scan the fragment table and move old fragments to the GL_RUN table
3526 */
3527 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3528 //
3529 TSQLResult *result = 0;
3530 TSQLRow *row = 0;
3531 TSQLResult *result2 = 0;
3532 TSQLRow *row2 = 0;
3533 //
3534 UInt_t moved = 0;
3535 //
3536 stringstream oss;
3537 oss.str("");
3538 //
3539 if ( !strcmp(fcleanfile.Data(),"") ){
3540 //
3541 // check if there are entries older than "olderthan" seconds from now
3542 //
3543 oss.str("");
3544 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3545 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3546 //
3547 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3548 result = conn->Query(oss.str().c_str());
3549 //
3550 } else {
3551 oss.str("");
3552 oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3553 if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3554 result = conn->Query(oss.str().c_str());
3555 //
3556 if ( result ){
3557 //
3558 row = result->Next();
3559 //
3560 if ( row ){
3561 oss.str("");
3562 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3563 << " ID_ROOT_L0=" << row->GetField(0) << ";";
3564 //
3565 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3566 result = conn->Query(oss.str().c_str());
3567 //
3568 };
3569 } else {
3570 return(2);
3571 };
3572 };
3573 //
3574 if ( result ){
3575 //
3576 row = result->Next();
3577 //
3578 while ( row ){
3579 //
3580 oss.str("");
3581 oss << " ID= "<< row->GetField(0);
3582 //
3583 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3584 //
3585 oss.str("");
3586 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3587 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3588 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3589 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3590 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3591 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3592 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3593 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3594 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3595 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3596 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3597 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3598 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3599 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3600 //
3601 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3602 result2 = conn->Query(oss.str().c_str());
3603 //
3604 if ( !result2 ) throw -4;
3605 //
3606 row2 = result2->Next();
3607 //
3608 if ( !row2 ){
3609 //
3610 if ( IsDebug() ) printf(" The run is new \n");
3611 if ( IsDebug() ) printf(" -> fill the DB \n");
3612 //
3613 // glrun->SetID(this->AssignRunID()); we use the old run number!
3614 glrun->SetID_RUN_FRAG(glrun->GetID());
3615 glrun->Fill_GL_RUN(conn);
3616 //
3617 // oss.str("");
3618 // oss << " SELECT ID FROM GL_RUN WHERE "
3619 // << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3620 // << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3621 // << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3622 // << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3623 // << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3624 // //
3625 // if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3626 // result2 = conn->Query(oss.str().c_str());
3627 // //
3628 // if ( !result2 ) throw -4;
3629 // //
3630 // row2 = result2->Next();
3631 // //
3632 // if ( !row2 ) throw -25;
3633 // //
3634 // oss.str("");
3635 // oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3636 // if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3637 // result2 = conn->Query(oss.str().c_str());
3638 // //
3639 // if ( !result2 ) throw -4;
3640 //
3641 moved++;
3642 //
3643 } else {
3644 if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3645 };
3646 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3647 //
3648 //
3649 glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3650 // oss.str("");
3651 // oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3652 // if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3653 // result2 = conn->Query(oss.str().c_str());
3654 // //
3655 // if ( !result2 ) throw -4;
3656 // //
3657 row = result->Next();
3658 };
3659 };
3660 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3661 return(0);
3662 };
3663
3664 /**
3665 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3666 */
3667 Int_t PamelaDBOperations::ValidateRuns(){
3668 return(this->ValidateRuns(""));
3669 };
3670
3671 /**
3672 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3673 */
3674 Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3675 //
3676 TSQLResult *result = 0;
3677 TSQLRow *row = 0;
3678 //
3679 UInt_t calibtime = 50;
3680 //
3681 stringstream oss;
3682 oss.str("");
3683 //
3684 // =======================================================
3685 // validate runs by checking missing calibrations
3686 // =======================================================
3687 UInt_t t_stop = 0;
3688 UInt_t t_start = 0;
3689 if ( !strcmp(valfile.Data(),"") ) {
3690 // --------------------------------------------------------------
3691 // 1) get the OBT of the last run inserted after clean-time limit
3692 // --------------------------------------------------------------
3693 oss.str("");
3694 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3695 << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3696 if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3697 result = conn->Query(oss.str().c_str());
3698 if ( !result ) throw -4;
3699 if ( !result->GetRowCount() ) {
3700 printf(" No runs to validate \n");
3701 return(1);
3702 }else{
3703 row = result->Next();
3704 t_start = (UInt_t)atoll(row->GetField(4));
3705 };
3706 // --------------------------------------------------------------
3707 // 2) get the OBT of the last validated run
3708 // --------------------------------------------------------------
3709 oss.str("");
3710 oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3711 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3712 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3713 result = conn->Query(oss.str().c_str());
3714 if ( !result ) throw -4;
3715 if ( result->GetRowCount() ){
3716 row = result->Next();
3717 t_stop = (UInt_t)atoll(row->GetField(4));
3718 };
3719 if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3720 // --------------------------------------------------------------
3721 // now retrieves runs to be validated
3722 // --------------------------------------------------------------
3723 oss.str("");
3724 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3725 oss << " AND RUNHEADER_TIME >="<< t_stop;
3726 oss << " ORDER BY RUNHEADER_TIME DESC;";
3727 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3728 result = conn->Query(oss.str().c_str());
3729 } else {
3730 //
3731 stringstream myquery;
3732 UInt_t myid = 0;
3733 myquery.str("");
3734 myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3735 //
3736 result = conn->Query(myquery.str().c_str());
3737 //
3738 row = result->Next();
3739 if( !row ){
3740 if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3741 if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3742 return(2);
3743 };
3744 if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3745 return(0);
3746 };
3747 myid=(UInt_t)atoll(row->GetField(0));
3748 //
3749 myquery.str("");
3750 myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3751 //
3752 result = conn->Query(myquery.str().c_str());
3753 //
3754 row = result->Next();
3755 if( !row->GetField(0) || !row->GetField(1)){
3756 //
3757 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3758 //
3759 return(0);
3760 //
3761 } else {
3762 //
3763 UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3764 UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3765 UInt_t caltime = 0;
3766 //
3767 myquery.str("");
3768 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3769 myquery << " order by FROM_TIME asc limit 1;";
3770 //
3771 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3772 //
3773 //
3774 result = conn->Query(myquery.str().c_str());
3775 //
3776 row = result->Next();
3777 if( !row ){
3778 caltime = runhtime;
3779 } else {
3780 caltime = (UInt_t)atoll(row->GetField(0));
3781 };
3782 //
3783 myquery.str("");
3784 myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3785 myquery << caltime << " order by RUNHEADER_TIME DESC";
3786 //
3787 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
3788 //
3789 result = conn->Query(myquery.str().c_str());
3790 //
3791 };
3792 };
3793 //
3794 if ( !result ) throw -4;
3795 if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3796 //
3797 Int_t nrow = 0;
3798 GL_RUN* this_run = new GL_RUN();
3799 GL_RUN* next_run = new GL_RUN();
3800 Int_t nseq_max = 1000;
3801 // UInt_t* sequence = new UInt_t[100];
3802 vector<UInt_t> sequence(nseq_max);
3803 Int_t nseq = 0;
3804 Bool_t CHECK = false;
3805 Bool_t this_ONLINE = false;
3806 Bool_t next_ONLINE = false;
3807 UInt_t t1=0,t2=0;
3808 // ---------------------------------------------------------------------------------
3809 // - loop over runs, back in time,
3810 // - select sequences of runs close in time (less than calibtime s apart),
3811 // which could be preceeded by a calibration
3812 // - check if there might be a missing calibration
3813 // ---------------------------------------------------------------------------------
3814 while(1){
3815
3816 row = result->Next();
3817 if( row == NULL ) break;
3818
3819 //------------
3820 //get run info
3821 //------------
3822 this_run->Set_GL_RUN(row);
3823
3824 Bool_t this_BAD = false;
3825 if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3826 else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3827 else{
3828 // printf("Missing or corrupted header!! \n");
3829 this_ONLINE = false;
3830 this_BAD = true;
3831 };
3832
3833 //-----------------------------------
3834 //compare with previous(next in time)
3835 //-----------------------------------
3836 CHECK = false;
3837 UInt_t interval=0;
3838
3839 if( nrow != 0){
3840
3841
3842 t1 = this_run->GetRUNTRAILER_TIME();
3843 t2 = next_run->GetRUNHEADER_TIME();
3844 interval = (t2-t1);
3845
3846 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3847
3848 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3849
3850 if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration
3851
3852 if( !CHECK && this_run->VALIDATION ){
3853 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3854 nseq=0;
3855 }
3856
3857 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3858
3859 CHECK = true;
3860
3861 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3862
3863 assignVALIDATION(next_run->ID,true);
3864 nseq=0;
3865 }
3866 }
3867
3868 //----------------------------
3869 //check run sequence for calib
3870 //----------------------------
3871 if( CHECK ){
3872 // check if calibration exists
3873 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3874 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3875 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3876 nseq=0;
3877 };
3878 //--------------
3879 //store run info
3880 //--------------
3881 *next_run = *this_run;
3882 next_ONLINE = this_ONLINE;
3883 if( !this_BAD ){
3884 if(nseq < nseq_max){
3885 sequence[nseq] = this_run->ID;
3886 nseq++;
3887 }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3888 };
3889
3890 if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3891 nrow++;
3892
3893 };
3894 delete this_run;
3895 delete next_run;
3896 //
3897 return(0);
3898 };
3899 /**
3900 * Check if there might be a missing tracker calibration in a given time interval
3901 * @param t1 From absolute time
3902 * @param t2 To absolute time
3903 * @return true if there might be a missing calibration
3904 */
3905 Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3906
3907 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3908
3909 // get the closest VALIDATED calibration before the run start (t2)
3910 if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3911
3912 if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3913
3914 //==============================================================
3915 // Check is done first on the basis of time between calibration,
3916 // which should be equal to the time between ascending-nodes.
3917 //==============================================================
3918 if ( t2 - trkcalib->FROM_TIME > 5700) {
3919 if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3920 //==============================================================
3921 // there might be a missing calibration, due to:
3922 // - MM full
3923 // - corrupted packets
3924 // - loss of data
3925 // There is an exception in case a download was done during ascending node
3926 //==============================================================
3927 Bool_t DOWNLOAD = false;
3928 // check if the calib was skipped becouse of download .... DA FARE!!
3929 if(DOWNLOAD)return(false);
3930
3931 return(true); //>>> missing
3932
3933 };
3934
3935 //==============================================================
3936 // If the last calibration is close to the run less than this time,
3937 // it is enough to say that there are no missing calibrations
3938 //==============================================================
3939 // the long time interval bewteen runs might be due to download
3940 if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3941 return(false);
3942
3943 };
3944 /**
3945 * Assign VALIDATION value to a GL_RUN entry
3946 * @param idrun Run ID
3947 * @param validation true/false
3948 */
3949 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3950 TSQLResult *result = 0;
3951 stringstream oss;
3952 oss.str("");
3953 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3954 //
3955 // if ( IsDebug() )
3956 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3957 if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3958 result = conn->Query(oss.str().c_str());
3959 if ( !result ) throw -4;
3960 return(0);
3961 }
3962
3963
3964
3965 // Insert TLEs from file tlefilename in the table GL_TLE in the db
3966 // opened by conn, sorting them by date from older to newer, if each
3967 // TLE has not been alread inserted.
3968 Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3969 {
3970 fstream tlefile(tlefilename, ios::in);
3971
3972 if ( !tlefile ) throw -7;
3973
3974 vector<cTle*> ctles;
3975 vector<cTle*>::iterator iter;
3976 int present = 0;
3977
3978 // Get three lines from tlefile, create a cTle object and put it
3979 // into ctles
3980 while(1) {
3981 cTle *tlef;
3982 string str1, str2, str3;
3983
3984 getline(tlefile, str1);
3985 if(tlefile.eof()) break;
3986
3987 getline(tlefile, str2);
3988 if(tlefile.eof()) break;
3989
3990 getline(tlefile, str3);
3991 if(tlefile.eof()) break;
3992
3993 // We now have three good lines for a cTle.
3994 tlef = new cTle(str1, str2, str3);
3995 ctles.push_back(tlef);
3996 }
3997
3998 tlefile.close();
3999
4000 // Sort by date
4001 sort(ctles.begin(), ctles.end(), compTLE);
4002
4003 // Now we insert each TLE into the db
4004 for(iter = ctles.begin(); iter != ctles.end(); iter++) {
4005 cTle *tle = *iter;
4006
4007 // Do nothing if it's already present in the db. Just increase
4008 // the counter present.
4009 if (! isTlePresent(tle))
4010 {
4011 int status = insertTle(tle);
4012
4013 // Insert query failed. Return 1.
4014 if(status == EXIT_FAILURE) {
4015
4016 if( IsDebug() ) {
4017 cerr << "Error: inserting TLE:" << endl
4018 << tle->getName() << endl
4019 << tle->getLine1() << endl
4020 << tle->getLine2() << endl;
4021 }
4022
4023 throw -4;
4024 return 1;
4025 }
4026
4027 }
4028 else
4029 present++;
4030
4031 }
4032
4033 int inserted = ctles.size() - present; // Number of inserted TLE.
4034 if ( IsDebug() )
4035 cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
4036 << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
4037
4038 ctles.clear();
4039
4040
4041 // Return 2 if no new TLE has been inserted. 0 otherwise.
4042 if(! inserted ) return 2;
4043 return 0;
4044 }
4045
4046
4047 // Insert tle in the table GL_TLE using the connection conn.
4048 Int_t PamelaDBOperations::insertTle(cTle *tle)
4049 {
4050 stringstream oss;
4051 TSQLResult *result = 0;
4052
4053 oss.str("");
4054 oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
4055 << " VALUES ( '"
4056 << tle->getName() << "', '"
4057 << tle->getLine1() << "', '"
4058 << tle->getLine2() << "', '"
4059 << getTleDatetime(tle) << "')";
4060
4061 // cout << oss.str().c_str() << endl;
4062 result = conn->Query(oss.str().c_str());
4063 if (result == NULL)
4064 return EXIT_FAILURE;
4065
4066 return EXIT_SUCCESS;
4067 }
4068
4069
4070 // Return whether tle is already in the db connected by conn.
4071 bool PamelaDBOperations::isTlePresent(cTle *tle)
4072 {
4073 stringstream oss;
4074 TSQLResult *result = 0;
4075
4076 oss.str("");
4077 oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
4078 << getTleDatetime(tle) << "'";
4079
4080 result = conn->Query(oss.str().c_str());
4081 if (result == NULL) throw -4;
4082
4083 if (result->GetRowCount())
4084 return true;
4085 else
4086 return false;
4087 }
4088
4089
4090 // Return whether the first TLE is dated early than the second
4091 bool compTLE (cTle *tle1, cTle *tle2)
4092 {
4093 return getTleJulian(tle1) < getTleJulian(tle2);
4094 }
4095
4096
4097 // Return the date of the tle using the format (year-2000)*1e3 +
4098 // julian day. e.g. 6365 is the 31th Dec 2006.
4099 // It does *not* return a cJulian date.
4100 float getTleJulian(cTle *tle) {
4101 return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
4102 }
4103
4104
4105 // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
4106 // format.
4107 string getTleDatetime(cTle *tle)
4108 {
4109 int year, mon, day, hh, mm, ss;
4110 double dom; // day of month (is double!)
4111 stringstream date; // date in datetime format
4112
4113 // create a cJulian from the date in tle
4114 cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
4115
4116 // get year, month, day of month
4117 jdate.getComponent(&year, &mon, &dom);
4118
4119 // build a datetime YYYY-MM-DD hh:mm:ss
4120 date.str("");
4121 day = (int) floor(dom);
4122 hh = (int) floor( (dom - day) * 24);
4123 mm = (int) floor( ((dom - day) * 24 - hh) * 60);
4124 ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
4125 // ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
4126
4127 date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
4128
4129 return date.str();
4130 }
4131
4132 /**
4133 * Remove a file from the DB, delete on cascade all entries related to that file
4134 * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4135 * calibration
4136 **/
4137 Int_t PamelaDBOperations::removeFile(TString remfile){
4138 //
4139 // Determine ID_ROOT_L0 and ID_RAW
4140 //
4141 TSQLResult *pResult;
4142 TSQLRow *Row;
4143 stringstream myquery;
4144 //
4145 myquery.str("");
4146 myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4147 //
4148 pResult = conn->Query(myquery.str().c_str());
4149 //
4150 Row = pResult->Next();
4151 if( !Row ){
4152 if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4153 if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4154 return(1);
4155 };
4156 if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4157 return(0);
4158 };
4159 //
4160 this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4161 this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4162 //
4163 this->ValidationOFF();
4164 //
4165 this->RemoveCALIBS();
4166 //
4167 this->RemoveRUNS();
4168 //
4169 this->RemoveFILES();
4170 //
4171 this->SetID_ROOT(0);
4172 this->SetID_RAW(0);
4173 //
4174 return(0);
4175 };
4176
4177 /**
4178 *
4179 * Set validation bit to zero for runs following the removing file till
4180 * 1) a run with TRK_CALIB_USED=140
4181 * 2) a run with VALIDATION = 0
4182 * 3) the next calibration
4183 *
4184 **/
4185 void PamelaDBOperations::ValidationOFF(){
4186 TSQLResult *pResult;
4187 TSQLRow *Row;
4188 stringstream myquery;
4189 Int_t unv = 0;
4190 //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4191 myquery.str("");
4192 myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4193 //
4194 pResult = conn->Query(myquery.str().c_str());
4195 //
4196 Row = pResult->Next();
4197 if( !Row->GetField(0) ){
4198 //
4199 if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4200 //
4201 } else {
4202 //
4203 UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4204 UInt_t caltime = 0;
4205 //
4206 myquery.str("");
4207 myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4208 myquery << " order by FROM_TIME asc limit 1;";
4209 //
4210 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4211 //
4212 //
4213 delete pResult;
4214 pResult = conn->Query(myquery.str().c_str());
4215 //
4216 Row = pResult->Next();
4217 if( !Row ){
4218 caltime = runhtime;
4219 } else {
4220 caltime = (UInt_t)atoll(Row->GetField(0));
4221 };
4222 //
4223 myquery.str("");
4224 myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4225 myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4226 //
4227 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4228 //
4229 pResult = conn->Query(myquery.str().c_str());
4230 //
4231 Row = pResult->Next();
4232 if( !Row ){
4233 //
4234 if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4235 //
4236 } else {
4237 myquery.str("");
4238 myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4239 myquery << " RUNHEADER_TIME>=" <<runhtime;
4240 myquery << " order by RUNHEADER_TIME asc;";
4241 //
4242 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4243 //
4244 pResult = conn->Query(myquery.str().c_str());
4245 //
4246 Row = pResult->Next();
4247 while ( Row ){
4248 //
4249 unv++;
4250 this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4251 Row = pResult->Next();
4252 //
4253 };
4254 };
4255 };
4256 if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4257 };
4258
4259 /**
4260 *
4261 * Rearrange GL_RUN table and remove runs
4262 *
4263 **/
4264 void PamelaDBOperations::RemoveRUNS(){
4265 TSQLResult *pResult;
4266 TSQLRow *Row;
4267 stringstream myquery;
4268 UInt_t drun = 0;
4269 GL_RUN *delrun = new GL_RUN();
4270 //
4271 myquery.str("");
4272 myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4273 //
4274 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4275 //
4276 pResult = conn->Query(myquery.str().c_str());
4277 //
4278 Row = pResult->Next();
4279 //
4280 //
4281 if ( !Row ){
4282 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4283 } else {
4284 if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");
4285 while ( Row ){
4286 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4287 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4288 drun++;
4289 Row = pResult->Next();
4290 };
4291 };
4292 //
4293 //
4294 myquery.str("");
4295 myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4296 //
4297 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4298 //
4299 pResult = conn->Query(myquery.str().c_str());
4300 //
4301 Row = pResult->Next();
4302 //
4303 if ( !Row ){
4304 if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4305 } else {
4306 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");
4307 while ( Row ){
4308 if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4309 delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4310 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4311 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4312 if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4313 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4314 delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4315 };
4316 drun++;
4317 Row = pResult->Next();
4318 };
4319 };
4320 //
4321 if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);
4322 //
4323 //
4324 //
4325 drun = 0;
4326 //
4327 myquery.str("");
4328 myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4329 //
4330 pResult = conn->Query(myquery.str().c_str());
4331 //
4332 Row = pResult->Next();
4333 //
4334 if ( !Row ){
4335 if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4336 } else {
4337 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");
4338 while ( Row ){
4339 if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4340 myquery.str("");
4341 myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";
4342 conn->Query(myquery.str().c_str());
4343 drun++;
4344 Row = pResult->Next();
4345 };
4346 };
4347 //
4348 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);
4349 //
4350 //
4351 //
4352 drun = 0;
4353 //
4354 myquery.str("");
4355 myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4356 //
4357 pResult = conn->Query(myquery.str().c_str());
4358 //
4359 Row = pResult->Next();
4360 //
4361 if ( !Row ){
4362 if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4363 } else {
4364 if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");
4365 while ( Row ){
4366 if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4367 myquery.str("");
4368 myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4369 conn->Query(myquery.str().c_str());
4370 drun++;
4371 Row = pResult->Next();
4372 };
4373 };
4374 //
4375 if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);
4376 //
4377 //
4378 //
4379 delete delrun;
4380 //
4381 };
4382
4383
4384 /**
4385 *
4386 * Rearrange calibration tables
4387 *
4388 **/
4389 void PamelaDBOperations::RemoveFILES(){
4390 stringstream myquery;
4391 //
4392 myquery.str("");
4393 myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4394 //
4395 if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str());
4396 //
4397 conn->Query(myquery.str().c_str());
4398 //
4399 };
4400
4401 /**
4402 *
4403 * Rearrange calibration tables
4404 *
4405 **/
4406 void PamelaDBOperations::RemoveCALIBS(){
4407 TSQLResult *pResult;
4408 TSQLRow *Row;
4409 stringstream myquery;
4410 //
4411 //
4412 // Calorimeter
4413 //
4414 for (Int_t section = 0; section < 4; section++){
4415 myquery.str("");
4416 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4417 myquery << " SECTION=" << section << ";";
4418 //
4419 pResult = conn->Query(myquery.str().c_str());
4420 //
4421 Row = pResult->Next();
4422 if( !Row->GetField(0) || !Row->GetField(1) ){
4423 //
4424 if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4425 //
4426 } else {
4427 //
4428 myquery.str("");
4429 myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4430 myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4431 myquery << " SECTION=" << section << ";";
4432 //
4433 pResult = conn->Query(myquery.str().c_str());
4434 //
4435 if( !pResult ){
4436 //
4437 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4438 //
4439 throw -4;
4440 //
4441 };
4442 //
4443 };
4444 };
4445 myquery.str("");
4446 myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4447 //
4448 pResult = conn->Query(myquery.str().c_str());
4449 //
4450 if( !pResult ){
4451 //
4452 if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4453 //
4454 throw -4;
4455 //
4456 };
4457 //
4458 // Tracker
4459 //
4460 myquery.str("");
4461 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4462 //
4463 pResult = conn->Query(myquery.str().c_str());
4464 //
4465 Row = pResult->Next();
4466 if( !Row->GetField(0) || !Row->GetField(1) ){
4467 //
4468 if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4469 //
4470 } else {
4471 //
4472 myquery.str("");
4473 myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4474 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4475 //
4476 pResult = conn->Query(myquery.str().c_str());
4477 //
4478 if( !pResult ){
4479 //
4480 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4481 //
4482 throw -4;
4483 //
4484 };
4485 //
4486 myquery.str("");
4487 myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4488 //
4489 pResult = conn->Query(myquery.str().c_str());
4490 //
4491 if( !pResult ){
4492 //
4493 if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4494 //
4495 throw -4;
4496 //
4497 };
4498 };
4499 //
4500 //
4501 // S4
4502 //
4503 myquery.str("");
4504 myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4505 //
4506 pResult = conn->Query(myquery.str().c_str());
4507 //
4508 Row = pResult->Next();
4509 if( !Row->GetField(0) || !Row->GetField(1) ){
4510 //
4511 if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4512 //
4513 } else {
4514 //
4515 myquery.str("");
4516 myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4517 myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4518 //
4519 pResult = conn->Query(myquery.str().c_str());
4520 //
4521 if( !pResult ){
4522 //
4523 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4524 //
4525 throw -4;
4526 //
4527 };
4528 //
4529 myquery.str("");
4530 myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4531 //
4532 pResult = conn->Query(myquery.str().c_str());
4533 //
4534 if( !pResult ){
4535 //
4536 if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4537 //
4538 throw -4;
4539 //
4540 };
4541 //
4542 };
4543 };
4544
4545 /**
4546 *
4547 * Rearrange calibration tables
4548 *
4549 **/
4550 UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4551
4552 Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4553 UInt_t timeaftercalib=120000; //2000;
4554 // ----------
4555 // Check CRCs
4556 // ----------
4557 for(Int_t ipkt=0; ipkt<6; ipkt++){
4558 if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4559 for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4560 }
4561 // -----------------------
4562 // Check missing packets:
4563 // -----------------------
4564 // Readout order:
4565 // ------------------
4566 // DSP packet board
4567 // ------------------
4568 // 12 0 1
4569 // 10 1 1
4570 // 8 2 1
4571 // 4 3 1
4572 // 6 4 1
4573 // 2 5 1
4574 // ------------------
4575 // 11 0 2
4576 // 9 1 2
4577 // 7 2 2
4578 // 3 3 2
4579 // 5 4 2
4580 // 1 5 2
4581 // ------------------
4582 // -------------------------------------------------
4583 // Check if it is first or second calibration packet
4584 // -------------------------------------------------
4585 UInt_t build=0;
4586 TString classname = caltrk->GetName();
4587 UInt_t base=0;
4588 UInt_t mask=0;
4589 if(classname.Contains("CalibTrk1Event")){
4590 base=12;
4591 mask=0x03F000;
4592 }
4593 if(classname.Contains("CalibTrk2Event")){
4594 base=18;
4595 mask=0xFC0000;
4596 }
4597 // -------------------------------------------------
4598 // Count number of packets and set build variable
4599 // -------------------------------------------------
4600 Int_t npkts=0;
4601 for(Int_t ipkt=0; ipkt<6; ipkt++){
4602 if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4603 npkts++;
4604 build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4605 }
4606 }
4607 // if( npkts==6 )return 1; // :-)
4608
4609 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4610
4611 // -----------------------------------------------
4612 // If missing packets: check the acq configuration
4613 // (some DSPs might be excluded from acquisition)
4614 // -----------------------------------------------
4615
4616 // -----------------------------------------------
4617 // retrieve the first run header after calib
4618 // -----------------------------------------------
4619 PacketType *pctp;
4620 EventCounter *cod;
4621 cod = eh->GetCounter();
4622 Int_t irun = cod->Get(pctp->RunHeader);
4623 TTree *rh=(TTree*)file->Get("RunHeader");
4624 if ( !rh || rh->IsZombie() ) throw -17;
4625 if( rh->GetEntries() == irun ){
4626 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<<endl;
4627 return 0; // :-(
4628 }
4629
4630 RunHeaderEvent *run = 0;
4631 EventHeader *hrun = 0;
4632 rh->SetBranchAddress("RunHeader", &run);
4633 rh->SetBranchAddress("Header", &hrun);
4634 rh->GetEntry(irun);
4635 // cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4636
4637 if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4638 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4639 return 0; // :-(
4640 }
4641
4642 if( !run->RM_ACQ_AFTER_CALIB ){
4643 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<<endl;
4644 return 0; // :-(
4645 }
4646
4647 UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4648 if( dtime > timeaftercalib ){
4649 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4650 return 0; // :-(
4651 }
4652
4653
4654
4655 if( (run->ACQ_BUILD_INFO & mask) != build ){
4656 if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4657 return 0; // :-(
4658 }
4659 return 1; // :-)
4660
4661 }
4662
4663 /**
4664 *
4665 * Check the DB (only for overlapping runs at the moment)
4666 *
4667 **/
4668 UInt_t PamelaDBOperations::Check(){
4669 //
4670 UInt_t test = 0;
4671 //
4672 UInt_t thisrht = 0;
4673 UInt_t thisrtt = 0;
4674 UInt_t thisid = 0;
4675 UInt_t prevrht = 0;
4676 UInt_t prevrtt = 0;
4677 UInt_t previd = 0;
4678 //
4679 UInt_t prevl0id = 0;
4680 UInt_t thisl0id = 0;
4681 //
4682 stringstream oss;
4683 TSQLResult *result = 0;
4684 TSQLRow *row = 0;
4685 TSQLResult *result2 = 0;
4686 TSQLRow *row2 = 0;
4687 TSQLResult *result3 = 0;
4688 TSQLRow *row3 = 0;
4689 oss.str("");
4690 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
4691 // oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4692 result = conn->Query(oss.str().c_str());
4693 //
4694 if ( !result ) throw -4;;
4695 //
4696 row = result->Next();
4697 //
4698 while ( row ){
4699 thisid = (UInt_t)atoll(row->GetField(0));
4700 thisl0id = (UInt_t)atoll(row->GetField(1));
4701 thisrht = (UInt_t)atoll(row->GetField(2));
4702 thisrtt = (UInt_t)atoll(row->GetField(3));
4703 //
4704 if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
4705 //
4706 //
4707 //
4708 oss.str("");
4709 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4710 << thisid << " AND RUNHEADER_TIME="
4711 << thisrht << " AND RUNTRAILER_TIME!="
4712 << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4713 result3 = conn->Query(oss.str().c_str());
4714 if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4715 if ( result3 ){
4716 row3 = result3->Next();
4717 //
4718 while ( row3 ){
4719 //
4720 // 2 runs with same runheader
4721 //
4722 printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4723 row3 = result3->Next();
4724 };
4725 };
4726 //
4727 oss.str("");
4728 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4729 << thisid << " AND RUNHEADER_TIME!="
4730 << thisrht << " AND RUNTRAILER_TIME="
4731 << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4732 result3 = conn->Query(oss.str().c_str());
4733 if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4734 if ( result3 ){
4735 row3 = result3->Next();
4736 //
4737 while ( row3 ){
4738 //
4739 // 2 runs with same runtrailer
4740 //
4741 printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4742 row3 = result3->Next();
4743 };
4744 };
4745 //
4746 oss.str("");
4747 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4748 << thisid << " AND RUNHEADER_TIME>"
4749 << thisrht << " AND RUNTRAILER_TIME<"
4750 << thisrtt << " order by RUNHEADER_TIME asc;";
4751 result3 = conn->Query(oss.str().c_str());
4752 if ( result3 ){
4753 row3 = result3->Next();
4754 //
4755 while ( row3 ){
4756 //
4757 // run contained in the checked one
4758 //
4759 printf(" CHECK n.6 RUN %u CONTAINS RUN %u \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4760 row3 = result3->Next();
4761 };
4762 };
4763 //
4764 oss.str("");
4765 oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4766 << thisid << " AND RUNHEADER_TIME="
4767 << thisrht << " AND RUNTRAILER_TIME="
4768 << thisrtt << " AND ID_RUN_FRAG!="
4769 << thisid << " order by RUNHEADER_TIME asc;";
4770 result3 = conn->Query(oss.str().c_str());
4771 if ( result3 ){
4772 row3 = result3->Next();
4773 //
4774 while ( row3 ){
4775 //
4776 // duplicated run
4777 //
4778 printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4779 row3 = result3->Next();
4780 };
4781 };
4782 };
4783 //
4784 // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4785 // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4786 if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4787 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4788 printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4789 TString prevf = "";
4790 TString thisf = "";
4791 oss.str("");
4792 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4793 result2 = conn->Query(oss.str().c_str());
4794 if ( !result2 ) throw -4;;
4795 row2 = result2->Next();
4796 prevf = (TString)row2->GetField(0);
4797 oss.str("");
4798 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4799 result2 = conn->Query(oss.str().c_str());
4800 if ( !result2 ) throw -4;;
4801 row2 = result2->Next();
4802 thisf = (TString)row2->GetField(0);
4803 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4804 test = 1;
4805 };
4806 //
4807 if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4808 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4809 printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4810 TString prevf = "";
4811 TString thisf = "";
4812 oss.str("");
4813 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4814 result2 = conn->Query(oss.str().c_str());
4815 if ( !result2 ) throw -4;
4816 row2 = result2->Next();
4817 prevf = (TString)row2->GetField(0);
4818 oss.str("");
4819 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4820 result2 = conn->Query(oss.str().c_str());
4821 if ( !result2 ) throw -4;;
4822 row2 = result2->Next();
4823 thisf = (TString)row2->GetField(0);
4824 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4825 test = 1;
4826 };
4827 //
4828 if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4829 if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4830 printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4831 TString prevf = "";
4832 TString thisf = "";
4833 oss.str("");
4834 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4835 result2 = conn->Query(oss.str().c_str());
4836 if ( !result2 ) throw -4;;
4837 row2 = result2->Next();
4838 prevf = (TString)row2->GetField(0);
4839 oss.str("");
4840 oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4841 result2 = conn->Query(oss.str().c_str());
4842 if ( !result2 ) throw -4;;
4843 row2 = result2->Next();
4844 thisf = (TString)row2->GetField(0);
4845 if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4846 test = 1;
4847 };
4848
4849 //
4850 prevrht = thisrht;
4851 prevrtt = thisrtt;
4852 previd = thisid;
4853 prevl0id = thisl0id;
4854 row = result->Next();
4855 };
4856 //
4857 return(test);
4858 //
4859 };

  ViewVC Help
Powered by ViewVC 1.1.23