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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations) (download)
Mon Sep 11 16:38:18 2006 UTC (18 years, 5 months ago) by pam-fi
Branch: MAIN
Changes since 1.7: +60 -80 lines
implemented run validation

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 <TDatime.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 <PamelaDBOperations.h>
35 //
36 using namespace std;
37 using namespace pamela;
38
39 /**
40 * Constructor.
41 * @param host hostname for the SQL connection.
42 * @param user username for the SQL connection.
43 * @param password password for the SQL connection.
44 * @param filerawname The path and name to the raw file.
45 * @param filerootname The path and name of the raw file.
46 * @param boot file BOOT number.
47 * @param obt0 file obt0.
48 * @param tsync file timesync.
49 * @param debug debug flag.
50 */
51 PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, Long64_t olderthan){
52 //
53 //
54 SetConnection(host,user,password);
55 //
56 SetDebugFlag(debug);
57 //
58 glrun = new GL_RUN();
59 //
60 if ( !boot ) SetNOBOOT(false);
61 SetBOOTnumber(boot);
62 SetTsync(tsync);
63 SetObt0(obt0);
64 //
65 //
66 INSERT_RAW =!filerawname.IsNull();
67 if(INSERT_RAW)SetRawName(filerawname);
68 //
69 INSERT_ROOT = !filerootname.IsNull();
70 if(INSERT_ROOT){
71 file = TFile::Open(this->GetRootName().Data());
72 if ( !file ) INSERT_ROOT = false;
73 };
74 // this->OpenFile();
75 //
76 this->SetID_RAW(0);
77 this->SetID_ROOT(0);
78
79 VALIDATE = false;
80 clean_time = new TDatime();
81 if(olderthan >= 0){
82 VALIDATE = true;
83 UInt_t timelim = 0;
84 // timelim = now - olderthan
85 timelim = (UInt_t)clean_time->Convert() - olderthan;
86 clean_time->Set(timelim,false);
87 };
88
89 //
90 };
91
92 /**
93 * Destructor
94 */
95 void PamelaDBOperations::Close(){
96 if( conn && conn->IsConnected() ) conn->Close();
97 delete clean_time;
98 delete glrun;
99 delete this;
100 };
101
102 //
103 // SETTERS
104 //
105
106 /**
107 * Open the DB connection
108 * @param host hostname for the SQL connection.
109 * @param user username for the SQL connection.
110 * @param password password for the SQL connection.
111 */
112 void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
113 conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
114 };
115
116 /**
117 * Store the ID of the ROOT file.
118 * @param idr ID of the ROOT file
119 */
120 void PamelaDBOperations::SetID_ROOT(UInt_t idr){
121 idroot=idr;
122 };
123
124 /**
125 * Store the ID of the RAW file.
126 * @param idr ID of the RAW file
127 */
128 void PamelaDBOperations::SetID_RAW(UInt_t idr){
129 id=idr;
130 };
131
132 /**
133 * Set the debug flag
134 *
135 */
136 void PamelaDBOperations::SetDebugFlag(Bool_t dbg){
137 debug = dbg;
138 };
139
140 /**
141 * Store the BOOT number of the RAW file.
142 * @param boot BOOT number of the RAW file
143 */
144 void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
145 BOOTNO=boot;
146 };
147
148 /**
149 * Store the time sync of the RAW file.
150 * @param boot time sync
151 */
152 void PamelaDBOperations::SetTsync(UInt_t ts){
153 tsync=ts;
154 };
155
156 /**
157 * Store the time sync of the RAW file.
158 * @param boot time sync
159 */
160 void PamelaDBOperations::SetObt0(UInt_t ts){
161 obt0=ts;
162 };
163
164 /**
165 * Store the RAW filename.
166 * @param str the RAW filename.
167 */
168 void PamelaDBOperations::SetRawName(TString str){
169 filerawname=str;
170 };
171
172 /**
173 * Store the ROOT filename.
174 * @param str the ROOT filename.
175 */
176 void PamelaDBOperations::SetRootName(TString str){
177 filerootname=str;
178 };
179
180 /**
181 * Store the NOBOOT flag.
182 * @param noboot true/false.
183 */
184 void PamelaDBOperations::SetNOBOOT(Bool_t noboot){
185 NOBOOT = noboot;
186 };
187
188 /**
189 * Store the olderthan variable
190 * @param olderthan
191 */
192 // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
193 // olderthan = oldthan;
194 // };
195
196 /**
197 * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
198 */
199 Bool_t PamelaDBOperations::SetID_RAW(){
200 stringstream oss;
201 TSQLResult *result = 0;
202 TSQLRow *row = 0;
203 oss.str("");
204 oss << "SELECT ID FROM GL_RAW WHERE "
205 << " PATH = '" << this->GetRawPath().Data() << "' AND "
206 << " NAME = '" << this->GetRawFile().Data() << "' ";
207 result = conn->Query(oss.str().c_str());
208 if ( result == NULL ) throw -4;
209 row = result->Next();
210 if ( !row ) return(false);
211 delete result;
212 id = (UInt_t)atoll(row->GetField(0));
213 return(true);
214 }
215
216 /**
217 *
218 * Set the variables which have to be stored in the GL_RUN table and that do not depend on the RUN
219 *
220 */
221 void PamelaDBOperations::SetCommonGLRUN(UInt_t absth, UInt_t abstt){
222 glrun->SetBOOTNUMBER(BOOTNO);
223 glrun->SetRUNHEADER_TIME(absth);
224 glrun->SetRUNTRAILER_TIME(abstt);
225 glrun->SetID_ROOT_L2(0);
226 glrun->SetID_ROOT_L0(idroot);
227 glrun->SetVALIDATION(0);
228 };
229
230 /**
231 * Patch, look for upper limits to avoid processing retransmitted data
232 */
233 Int_t PamelaDBOperations::SetUpperLimits(){
234 UInt_t nevent = 0;
235 UInt_t pktlast = 0;
236 UInt_t obtlast = 0;
237 UInt_t t_pktlast = 0;
238 UInt_t t_obtlast = 0;
239 UInt_t upperpkt2 = 0;
240 ULong64_t upperobt2 = 0;
241 UInt_t zomp = 0;
242 UInt_t jump = 50000; // was 5000
243 EventCounter *code=0;
244 //
245 // pcksList packetsNames;
246 // pcksList::iterator Iter;
247 // getPacketsNames(packetsNames);
248 //
249 pktfirst = 0;
250 obtfirst = 0;
251 //
252 TTree *T = 0;
253 T = (TTree*)file->Get("Physics");
254 if ( !T || T->IsZombie() ) throw -16;
255 EventHeader *eh = 0;
256 PscuHeader *ph = 0;
257 T->SetBranchAddress("Header", &eh);
258 nevent = T->GetEntries();
259 //
260 T->GetEntry(0);
261 ph = eh->GetPscuHeader();
262 pktfirst = ph->GetCounter();
263 obtfirst = ph->GetOrbitalTime();
264 //
265 // code = eh->GetCounter();
266 // UInt_t en = 0;
267 // for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
268 // en = code->Get(GetPacketType(*Iter));
269 // if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);
270 //};
271 //
272 T->GetEntry(nevent-1);
273 ph = eh->GetPscuHeader();
274 pktlast = ph->GetCounter();
275 obtlast = ph->GetOrbitalTime();
276 //
277 upperpkt = PKT(pktlast);
278 upperobt = OBT(obtlast);
279 upperentry = nevent-1;
280 //
281 if ( IsDebug() ) printf(" First entries are: OBT %llu pkt_num %i \n",obtfirst,pktfirst);
282 //
283 if ( IsDebug() ) printf(" Last entries are: OBT %llu pkt_num %i entry %i\n",upperobt,upperpkt,upperentry);
284 //
285 if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);
286 //
287 if ( !nevent ) return(2);
288 //
289 if ( nevent < 2 ) return(4);
290 //
291 if ( PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst) ){
292 // go back
293 zomp = nevent - 2;
294 //
295 while ( jump > 0 ){
296 //
297 t_pktlast = PKT(pktlast);
298 t_obtlast = OBT(obtlast);
299 //
300 for (UInt_t i = zomp; i>1; i-=jump){
301 //
302 if ( i >= 0 ) T->GetEntry(i);
303 ph = eh->GetPscuHeader();
304 upperpkt = PKT(ph->GetCounter());
305 upperobt = OBT(ph->GetOrbitalTime());
306 upperentry = i;
307 //
308 if ( (i-1) >= 0 ) T->GetEntry(i-1);
309 ph = eh->GetPscuHeader();
310 upperpkt2 = PKT(ph->GetCounter());
311 upperobt2 = OBT(ph->GetOrbitalTime());
312 //
313 if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ) throw -13;
314 //
315 if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
316 zomp = i + jump + 1;
317 if ( zomp > nevent-2 ) zomp = nevent - 2;
318 if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %i pktlast %i upperobt %llu obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
319 break;
320 };
321 //
322 t_pktlast = upperpkt;
323 t_obtlast = upperobt;
324 };
325 //
326 if ( jump == 1 ) jump = 0;
327 if ( jump == 10 ) jump = 1;
328 if ( jump == 100 ) jump = 10;
329 if ( jump == 1000 ) jump = 100;
330 if ( jump == 5000 ) jump = 1000;
331 if ( jump == 50000 ) jump = 5000;
332 //
333 };
334 //
335 };
336 //
337 // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
338 //
339 PacketType *pctp=0;
340 TTree *rh=(TTree*)file->Get("RunHeader");
341 if ( !rh || rh->IsZombie() ) throw -17;
342 TTree *rt=(TTree*)file->Get("RunTrailer");
343 if ( !rt || rt->IsZombie() ) throw -18;
344 //
345 rh->SetBranchAddress("RunHeader", &runh);
346 rh->SetBranchAddress("Header", &ehh);
347 //
348 rt->SetBranchAddress("RunTrailer", &runt);
349 rt->SetBranchAddress("Header", &eht);
350 //
351 rhev = rh->GetEntries();
352 rtev = rt->GetEntries();
353 UInt_t sobtt = 0;
354 UInt_t sobth = 0;
355 UInt_t spktt = 0;
356 UInt_t spkth = 0;
357 UInt_t pktt = 0;
358 ULong64_t obtt = 0;
359 UInt_t pkth = 0;
360 ULong64_t obth = 0;
361 //
362 T->GetEntry(upperentry);
363 code = eh->GetCounter();
364 Int_t lasttrail = code->Get(pctp->RunTrailer);
365 Int_t lasthead = code->Get(pctp->RunHeader);
366 if ( lasttrail < rtev ){
367 rt->GetEntry(lasttrail);
368 pht = eht->GetPscuHeader();
369 pktt = PKT(pht->GetCounter());
370 obtt = OBT(pht->GetOrbitalTime());
371 };
372 //
373 if ( lasthead < rhev ){
374 rh->GetEntry(lasthead);
375 phh = ehh->GetPscuHeader();
376 pkth = PKT(phh->GetCounter());
377 obth = OBT(phh->GetOrbitalTime());
378 };
379 //
380 if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
381 if ( pkth > upperpkt && obth > upperobt ){
382 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
383 upperpkt = pkth;
384 upperobt = obth;
385 rhev = lasthead+1;
386 } else {
387 rhev = lasthead;
388 };
389 if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
390 //
391 if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
392 if ( pktt > upperpkt && obtt > upperobt ){
393 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
394 upperpkt = pktt;
395 upperobt = obtt;
396 rtev = lasttrail+1;
397 } else {
398 rtev = lasttrail;
399 };
400 if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
401 // goto kikko;
402 //
403 //
404 // Check if runtrailer/runheader are within lower limits
405 //
406 //
407 pkth = 0;
408 obth = 0;
409 spkth = 0;
410 sobth = 0;
411 for (Int_t k=0; k<rhev; k++){
412 if ( k > 0 ){
413 spkth = pkth;
414 sobth = obth;
415 };
416 rh->GetEntry(k);
417 phh = ehh->GetPscuHeader();
418 pkth = PKT(phh->GetCounter());
419 obth = OBT(phh->GetOrbitalTime());
420 //
421 // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
422 //
423 if ( pkth < spkth && obth < sobth ){
424 if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev);
425 //
426 rhev = k-1;
427 rh->GetEntry(rhev);
428 pkth = spkth;
429 obth = sobth;
430 //
431 UInt_t evbefh = 0;
432 code = ehh->GetCounter();
433 evbefh = code->Get(pctp->Physics);
434 if ( evbefh >= 0 ){
435 T->GetEntry(evbefh);
436 ph = eh->GetPscuHeader();
437 t_pktlast = PKT(ph->GetCounter());
438 t_obtlast = OBT(ph->GetOrbitalTime());
439 if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
440 upperpkt = pkth;
441 upperobt = obth;
442 upperentry = evbefh-1;
443 } else {
444 while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
445 evbefh++;
446 T->GetEntry(evbefh);
447 ph = eh->GetPscuHeader();
448 t_pktlast = PKT(ph->GetCounter());
449 t_obtlast = OBT(ph->GetOrbitalTime());
450 };
451 T->GetEntry(evbefh-1);
452 ph = eh->GetPscuHeader();
453 upperpkt = PKT(ph->GetCounter());
454 upperobt = OBT(ph->GetOrbitalTime());
455 upperentry = evbefh-1;
456 };
457 };
458 if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
459 goto kikko0;
460 };
461 };
462 kikko0:
463 //
464 //
465 //
466 pktt = 0;
467 obtt = 0;
468 spktt = 0;
469 sobtt = 0;
470 for (Int_t k=0; k<rtev; k++){
471 if ( k > 0 ){
472 spktt = pktt;
473 sobtt = obtt;
474 };
475 rt->GetEntry(k);
476 pht = eht->GetPscuHeader();
477 pktt = PKT(pht->GetCounter());
478 obtt = OBT(pht->GetOrbitalTime());
479 //
480 // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
481 //
482 if ( pktt < spktt && obtt < sobtt ){
483 if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
484 //
485 rtev = k-1;
486 rt->GetEntry(rtev);
487 pktt = spktt;
488 obtt = sobtt;
489 if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
490 //
491 UInt_t evbeft = 0;
492 code = eht->GetCounter();
493 evbeft = code->Get(pctp->Physics);
494 if ( evbeft >= 0 ){
495 T->GetEntry(evbeft);
496 ph = eh->GetPscuHeader();
497 t_pktlast = PKT(ph->GetCounter());
498 t_obtlast = OBT(ph->GetOrbitalTime());
499 if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
500 upperpkt = pktt;
501 upperobt = obtt;
502 upperentry = evbeft-1;
503 } else {
504 while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
505 evbeft++;
506 T->GetEntry(evbeft);
507 ph = eh->GetPscuHeader();
508 t_pktlast = PKT(ph->GetCounter());
509 t_obtlast = OBT(ph->GetOrbitalTime());
510 };
511 T->GetEntry(evbeft-1);
512 ph = eh->GetPscuHeader();
513 upperpkt = PKT(ph->GetCounter());
514 upperobt = OBT(ph->GetOrbitalTime());
515 upperentry = evbeft-1;
516 };
517 };
518 if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
519 goto kikko;
520 // break;
521 //
522 };
523 //
524 };
525 //
526 kikko:
527 //
528 T->GetEntry(upperentry);
529 code = eh->GetCounter();
530 lasttrail = code->Get(pctp->RunTrailer);
531 lasthead = code->Get(pctp->RunHeader);
532 if ( lasttrail < rtev ){
533 rt->GetEntry(lasttrail);
534 pht = eht->GetPscuHeader();
535 pktt = PKT(pht->GetCounter());
536 obtt = OBT(pht->GetOrbitalTime());
537 };
538 //
539 if ( lasthead < rhev ){
540 rh->GetEntry(lasthead);
541 phh = ehh->GetPscuHeader();
542 pkth = PKT(phh->GetCounter());
543 obth = OBT(phh->GetOrbitalTime());
544 };
545 //
546 if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
547 if ( pkth > upperpkt && obth > upperobt ){
548 if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
549 upperpkt = pkth;
550 upperobt = obth;
551 rhev = lasthead+1;
552 } else {
553 rhev = lasthead;
554 };
555 if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
556 //
557 if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
558 if ( pktt > upperpkt && obtt > upperobt ){
559 if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
560 upperpkt = pktt;
561 upperobt = obtt;
562 rtev = lasttrail+1;
563 } else {
564 rtev = lasttrail;
565 };
566 if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
567 //
568 if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);
569 //
570 return(0);
571 }
572
573 //
574 // GETTERS
575 //
576
577 /**
578 *
579 * Returns the DB absolute time needed to associate calibrations to data
580 *
581 */
582 UInt_t PamelaDBOperations::GetAbsTime(UInt_t obt){
583 //
584 return(((UInt_t)(OBT(obt)/1000)+toffset));
585 //
586 };
587
588 /**
589 *
590 * List of packet types (just to make easily the loops)
591 *
592 */
593 const PacketType* PamelaDBOperations::GetPacketType(const char* type){
594 if ( !strcmp(type,"Pscu") ) return(PacketType::Pscu);
595 if ( !strcmp(type,"PhysEndRun") ) return(PacketType::PhysEndRun);
596 if ( !strcmp(type,"CalibCalPulse1") ) return(PacketType::CalibCalPulse1);
597 if ( !strcmp(type,"CalibCalPulse2") ) return(PacketType::CalibCalPulse2);
598 if ( !strcmp(type,"Physics") ) return(PacketType::Physics);
599 if ( !strcmp(type,"CalibTrkBoth") ) return(PacketType::CalibTrkBoth);
600 if ( !strcmp(type,"CalibTrk1") ) return(PacketType::CalibTrk1);
601 if ( !strcmp(type,"CalibTrk2") ) return(PacketType::CalibTrk2);
602 if ( !strcmp(type,"CalibTof") ) return(PacketType::CalibTof);
603 if ( !strcmp(type,"CalibS4") ) return(PacketType::CalibS4);
604 if ( !strcmp(type,"CalibCalPed") ) return(PacketType::CalibCalPed);
605 if ( !strcmp(type,"Calib1_Ac1") ) return(PacketType::Calib1_Ac1);
606 if ( !strcmp(type,"Calib2_Ac1") ) return(PacketType::Calib2_Ac1);
607 if ( !strcmp(type,"Calib1_Ac2") ) return(PacketType::Calib1_Ac2);
608 if ( !strcmp(type,"Calib2_Ac2") ) return(PacketType::Calib2_Ac2);
609 if ( !strcmp(type,"CalibCal") ) return(PacketType::CalibCal);
610 if ( !strcmp(type,"RunHeader") ) return(PacketType::RunHeader);
611 if ( !strcmp(type,"RunTrailer") ) return(PacketType::RunTrailer);
612 if ( !strcmp(type,"CalibHeader") ) return(PacketType::CalibHeader);
613 if ( !strcmp(type,"CalibTrailer") ) return(PacketType::CalibTrailer);
614 if ( !strcmp(type,"InitHeader") ) return(PacketType::InitHeader);
615 if ( !strcmp(type,"InitTrailer") ) return(PacketType::InitTrailer);
616 if ( !strcmp(type,"EventTrk") ) return(PacketType::EventTrk);
617 if ( !strcmp(type,"Log") ) return(PacketType::Log);
618 if ( !strcmp(type,"VarDump") ) return(PacketType::VarDump);
619 if ( !strcmp(type,"ArrDump") ) return(PacketType::ArrDump);
620 if ( !strcmp(type,"TabDump") ) return(PacketType::TabDump);
621 if ( !strcmp(type,"Tmtc") ) return(PacketType::Tmtc);
622 if ( !strcmp(type,"Mcmd") ) return(PacketType::Mcmd);
623 if ( !strcmp(type,"ForcedFECmd") ) return(PacketType::ForcedFECmd);
624 if ( !strcmp(type,"Ac1Init") ) return(PacketType::Ac1Init);
625 if ( !strcmp(type,"CalInit") ) return(PacketType::CalInit);
626 if ( !strcmp(type,"TrkInit") ) return(PacketType::TrkInit);
627 if ( !strcmp(type,"TofInit") ) return(PacketType::TofInit);
628 if ( !strcmp(type,"TrgInit") ) return(PacketType::TrgInit);
629 if ( !strcmp(type,"NdInit") ) return(PacketType::NdInit);
630 if ( !strcmp(type,"S4Init") ) return(PacketType::S4Init);
631 if ( !strcmp(type,"Ac2Init") ) return(PacketType::Ac2Init);
632 if ( !strcmp(type,"CalAlarm") ) return(PacketType::CalAlarm);
633 if ( !strcmp(type,"Ac1Alarm") ) return(PacketType::Ac1Alarm);
634 if ( !strcmp(type,"TrkAlarm") ) return(PacketType::TrkAlarm);
635 if ( !strcmp(type,"TrgAlarm") ) return(PacketType::TrgAlarm);
636 if ( !strcmp(type,"TofAlarm") ) return(PacketType::TofAlarm);
637 if ( !strcmp(type,"S4Alarm") ) return(PacketType::S4Alarm);
638 if ( !strcmp(type,"Ac2Alarm") ) return(PacketType::Ac2Alarm);
639 if ( !strcmp(type,"TsbT") ) return(PacketType::TsbT);
640 if ( !strcmp(type,"TsbB") ) return(PacketType::TsbB);
641 return(PacketType::Invalid);
642 };
643
644 //
645 // PRIVATE FUNCTIONS
646 //
647
648 // /**
649 // * Open the ROOT filename for reading
650 // */
651 // void PamelaDBOperations::OpenFile(){
652 // file = TFile::Open(this->GetRootName().Data());
653 // //
654 // if ( !file ) throw -12;
655 // };
656
657
658 /**
659 * Check if LEVEL0 file and DB connection have really be opened
660 */
661 void PamelaDBOperations::CheckConnection(){
662 //
663 // check connection
664 //
665 if( !conn ) throw -1;
666 bool connect = conn->IsConnected();
667 if( !connect ) throw -1;
668 };
669
670 /**
671 * Return the correct packet number if we went back to zero
672 */
673 UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){
674 //
675 // if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (pktfirst - (UInt_t)(16777212/2)) is %u \n",pkt_num,pktfirst,(pktfirst - (UInt_t)(16777212/2)));
676 //
677 if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215));
678 //
679 if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
680 if ( (pkt_num-16777215) < 0 ){
681 return((16777215-pkt_num));
682 } else {
683 return((pkt_num-16777215));
684 };
685 };
686 //
687 return(pkt_num);
688 //
689 };
690
691 /**
692 * Return the correct On Board Time if we went back to zero
693 */
694 ULong64_t PamelaDBOperations::OBT(UInt_t obt){
695 //
696 if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));
697 //
698 if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
699 if ( (obt-numeric_limits<UInt_t>::max()) < 0 ){
700 return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));
701 } else {
702 return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));
703 };
704 };
705 //
706 return((ULong64_t)obt);
707 };
708
709 /**
710 *
711 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
712 *
713 */
714 void PamelaDBOperations::FillClass(){
715 this->FillClass(false,false,0,0);
716 };
717
718 /**
719 *
720 * Fill the glrun class with infos about the run when we have both runtrailer and runheader
721 *
722 */
723 void PamelaDBOperations::FillClass(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
724 //
725 TTree *T = 0;
726 T = (TTree*)file->Get("Physics");
727 if ( !T || T->IsZombie() ) throw -16;
728 //
729 EventHeader *eh = 0;
730 PscuHeader *ph = 0;
731 T->SetBranchAddress("Header", &eh);
732 PacketType *pctp=0;
733 EventCounter *codt=0;
734 EventCounter *codh=0;
735 UInt_t firstObt = 0;
736 UInt_t lastObt = 0;
737 UInt_t firstPkt = 0;
738 UInt_t lastPkt = 0;
739 UInt_t rhtime = 0;
740 UInt_t rttime = 0;
741 if ( !mishead ){
742 codh = ehh->GetCounter();
743 firstev = codh->Get(pctp->Physics);
744 rhtime = this->GetAbsTime(phh->GetOrbitalTime());
745 glrun->Set_GL_RUNH(runh,phh);
746 firstObt = glrun->GetRUNHEADER_OBT();
747 firstPkt = glrun->GetRUNHEADER_PKT();
748 };
749 if ( !mistrail ){
750 codt = eht->GetCounter();
751 lastev = codt->Get(pctp->Physics)-1;
752 rttime = this->GetAbsTime(pht->GetOrbitalTime());
753 glrun->Set_GL_RUNT(runt,pht);
754 lastObt = glrun->GetRUNTRAILER_OBT();
755 lastPkt = glrun->GetRUNTRAILER_PKT();
756 };
757 //
758 if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
759 //
760 if ( mishead ) {
761 glrun->Set_GL_RUNH0();
762 //
763 if ( lastev+1 == firstev ){
764 firstObt = lastObt;
765 firstPkt = lastPkt;
766 rhtime = rttime;
767 } else {
768 T->GetEntry(firstev);
769 ph = eh->GetPscuHeader();
770 firstObt = ph->GetOrbitalTime();
771 rhtime = this->GetAbsTime(firstObt);
772 firstPkt = ph->GetCounter();
773 };
774 //
775 glrun->SetRUNHEADER_PKT(firstPkt);
776 glrun->SetRUNHEADER_OBT(firstObt);
777 //
778 };
779 if ( mistrail ){
780 glrun->Set_GL_RUNT0();
781 //
782 if ( lastev+1 == firstev ){
783 lastObt = firstObt;
784 lastPkt = firstPkt;
785 rttime = rhtime;
786 } else {
787 T->GetEntry(lastev);
788 ph = eh->GetPscuHeader();
789 lastObt = ph->GetOrbitalTime();
790 rttime = this->GetAbsTime(lastObt);
791 lastPkt = ph->GetCounter();
792 };
793 //
794 glrun->SetRUNTRAILER_OBT(lastObt);
795 glrun->SetRUNTRAILER_PKT(lastPkt);
796 //
797 };
798 glrun->SetEV_FROM(firstev);
799 glrun->SetEV_TO(lastev);
800 glrun->SetNEVENTS(lastev-firstev+1);
801 //
802 this->SetCommonGLRUN(rhtime,rttime);
803 //
804 };
805
806 //
807 // PUBLIC FUNCTIONS
808 //
809
810 /**
811 * Insert a new row into GL_RAW table.
812 */
813 Int_t PamelaDBOperations::insertPamelaRawFile(){
814 //
815 stringstream oss;
816 //
817 Bool_t idr = this->SetID_RAW();
818 if ( idr ) return(1);
819 //
820 oss.str("");
821 oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
822 << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
823 if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
824 //
825 idr = this->SetID_RAW();
826 if ( !idr ) throw -11;
827 //
828 return(0);
829 }
830
831
832 /**
833 * Look for one timesync information in the file and
834 * 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
835 */
836 Int_t PamelaDBOperations::insertPamelaGL_TIMESYNC(){
837 //
838 TSQLResult *result = 0;
839 TSQLRow *row = 0;
840 UInt_t t0 = 0;
841 //
842 stringstream oss;
843 //
844 if ( this->GetID_RAW() == 0 ) throw -11;
845 //
846 oss.str("");
847 oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "
848 << id << " AND TO_ID_RAW >= "
849 << id << ";";
850 if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
851 result = conn->Query(oss.str().c_str());
852 if ( !result ) throw -10;
853 row = result->Next();
854 if ( !row ) throw -10;
855 //
856 t0 = (UInt_t)TDatime(row->GetField(0)).Convert();
857 /*
858 * Verify that the TIMESYNC have been not already processed
859 */
860 oss.str("");
861 oss << " SELECT COUNT(GL_TIMESYNC.ID),GL_TIMESYNC.OBT0,GL_TIMESYNC.TIMESYNC FROM GL_TIMESYNC "
862 << " LEFT JOIN GL_RAW "
863 << " ON GL_RAW.ID = GL_TIMESYNC.ID_RAW "
864 << " WHERE GL_TIMESYNC.ID_RAW = " << this->GetID_RAW()
865 << " GROUP BY GL_TIMESYNC.OBT0;";
866 if ( IsDebug() ) printf(" check for old timesync: query is \n %s \n",oss.str().c_str());
867 result = conn->Query(oss.str().c_str());
868 if (result == NULL) throw -10;
869 row = result->Next();
870 if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
871 toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
872 return(1);
873 };
874 //
875 TTree *T = 0;
876 Int_t signal = 0;
877 //
878 UInt_t nevent = 0;
879 UInt_t recEntries = 0;
880 //
881 UInt_t OBT = 0;
882 UInt_t TYPE = 0;
883 //
884 Double_t minimum = 0.;
885 Double_t maximum = 0.;
886 Double_t minimum2 = 0.;
887 Double_t maximum2 = 0.;
888 //
889 UInt_t TSYNC = 0;
890 //
891 pamela::McmdEvent *mc = 0;
892 pamela::McmdRecord *mcrc = 0;
893 TArrayC *mcmddata = 0;
894 //
895 minimum = numeric_limits<Double_t>::max();
896 maximum = numeric_limits<Double_t>::min();
897 minimum2 = numeric_limits<Double_t>::max();
898 maximum2 = numeric_limits<Double_t>::min();
899 //
900 T = (TTree*)file->Get("Mcmd");
901 if ( !T || T->IsZombie() ) throw -19;
902 T->SetBranchAddress("Mcmd",&mc);
903 //
904 nevent = T->GetEntries();
905 //
906 // loop over events
907 //
908 Bool_t existsts = false;
909 //
910 for (UInt_t i=0; i<nevent;i++){
911 //
912 T->GetEntry(i);
913 //
914 recEntries = mc->Records->GetEntries();
915 //
916 for (UInt_t j = 0; j < recEntries; j++){
917 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
918 mcmddata = mcrc->McmdData;
919 //
920 if (mcrc->ID1 == 0xE0){ // mcmd timesync
921 //
922 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
923 //
924 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);
925 //
926 TYPE = 55;//224;
927 //
928 if ( TSYNC && OBT ){
929 existsts = true;
930 goto eout;
931 };
932 //
933 };
934 };
935 };
936 if ( !existsts ) { // try with runheader and runtrailer
937 //
938 if ( IsDebug() ) printf(" No ts mcmd \n");
939 signal = 2;
940 //
941 TTree *rh=(TTree*)file->Get("RunHeader");
942 if ( !rh || rh->IsZombie() ) throw -17;
943 TTree *rt=(TTree*)file->Get("RunTrailer");
944 if ( !rt || rt->IsZombie() ) throw -18;
945 //
946 rh->SetBranchAddress("RunHeader", &runh);
947 //
948 rt->SetBranchAddress("RunTrailer", &runt);
949 //
950 if ( rhev > 0 ){
951 rh->GetEntry(0);
952 //
953 TSYNC = runh->LAST_TIME_SYNC_INFO;
954 OBT = runh->OBT_TIME_SYNC * 1000;
955 //
956 TYPE = 20;
957 //
958 if ( TSYNC && OBT ){
959 existsts = true;
960 goto eout;
961 };
962 //
963 };
964 if ( rtev > 0 ){
965 //
966 if ( IsDebug() ) printf(" No runheader \n");
967 signal = 6;
968 //
969 rt->GetEntry(0);
970 //
971 TSYNC = runt->LAST_TYME_SYNC_INFO;
972 OBT = runt->OBT_TYME_SYNC * 1000;
973 //
974 TYPE = 21;
975 //
976 if ( TSYNC && OBT ){
977 existsts = true;
978 goto eout;
979 };
980 //
981 } else {
982 if ( IsDebug() ) printf(" No runheader \n");
983 };
984 };
985 //
986 if ( !existsts ){ // try with inclination mcmd
987 //
988 if ( IsDebug() ) printf(" No runtrailer \n");
989 signal = 14;
990 //
991 Double_t timesync = 0.;
992 for (UInt_t i=0; i<nevent;i++){
993 //
994 T->GetEntry(i);
995 //
996 recEntries = mc->Records->GetEntries();
997 // //
998 for (UInt_t j = 0; j < recEntries; j++){
999 mcrc = (pamela::McmdRecord*)mc->Records->At(j);
1000 mcmddata = mcrc->McmdData;
1001 //
1002 if (mcrc->ID1 == 0xE2){ // mcmd inclination
1003 timesync = 0.;
1004 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);
1005 //
1006 if ( timesync > maximum2){
1007 maximum2 = timesync;
1008 OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
1009 };
1010 };
1011 //
1012 };
1013 };
1014 if ( maximum2 > numeric_limits<Double_t>::min() ){
1015 TSYNC = (UInt_t)(maximum2 + 0.5);
1016 TYPE = 666;
1017 if ( TSYNC && OBT ){
1018 existsts = true;
1019 goto eout;
1020 };
1021 };
1022 };
1023 //
1024 if ( !existsts && obt0 ){ // insert timesync by hand
1025 //
1026 if ( IsDebug() ) printf(" No incl mcmd \n");
1027 signal = 30;
1028 //
1029 OBT = obt0;
1030 TSYNC = tsync;
1031 TYPE = 999;
1032 existsts = true;
1033 goto eout;
1034 };
1035 //
1036 eout:
1037 //
1038 if ( !existsts ) throw -3;
1039 //
1040 oss.str("");
1041 oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1042 << this->GetID_RAW() << "','"//224'"
1043 << dec << (UInt_t)TYPE << "','"
1044 << dec << (UInt_t)OBT << "','"
1045 << dec << (UInt_t)TSYNC << "');";
1046 conn->Query(oss.str().c_str());
1047 if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1048 //
1049 toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1050 //
1051 delete result;
1052 return(signal);
1053 }
1054
1055 /**
1056 * Insert all the new rows into GL_ROOT.
1057 * The raw file indicates in the parameters should be already been stored in the database.
1058 */
1059 Int_t PamelaDBOperations::insertPamelaRootFile(){
1060 stringstream oss;
1061 TSQLResult *result = 0;
1062 TSQLRow *row = 0;
1063 UInt_t idtimesync = 0;
1064 //
1065 oss.str("");
1066 oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1067 << " LEFT JOIN GL_ROOT "
1068 << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1069 << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1070 << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1071 result = conn->Query(oss.str().c_str());
1072 //
1073 if ( !result ) throw -12;
1074 //
1075 row = result->Next();
1076 //
1077 if ( !row ) throw -10;
1078 if ( row != NULL && (UInt_t)atoll(row->GetField(0))>0 ){
1079 idroot = (UInt_t)atoll(row->GetField(2));
1080 return(1);
1081 };
1082 //
1083 // determine which timesync has to be used
1084 //
1085 oss.str("");
1086 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;";
1087 result = conn->Query(oss.str().c_str());
1088 //
1089 if ( !result ) throw -3;
1090 //
1091 row = result->Next();
1092 //
1093 if ( !row ) throw -3;
1094 idtimesync = (UInt_t)atoll(row->GetField(0));
1095 //
1096 oss.str("");
1097 oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1098 << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1099 //
1100 if (conn->Query(oss.str().c_str()) == 0) throw -4;
1101 //
1102 delete result;
1103 //
1104 oss.str("");
1105 oss << "SELECT ID FROM GL_ROOT WHERE ID_RAW=" << this->GetID_RAW() << ";";
1106 //
1107 result = conn->Query(oss.str().c_str());
1108 if ( !result ) throw -12;
1109 row = result->Next();
1110 this->SetID_ROOT((UInt_t)atoll(row->GetField(0)));
1111 //
1112 delete result;
1113 //
1114 return(0);
1115 }
1116
1117 /**
1118 * Assign the BOOT_NUMBER to the raw file.
1119 */
1120 Int_t PamelaDBOperations::assignBOOT_NUMBER(){
1121 stringstream oss;
1122 TSQLResult *result = 0;
1123 TSQLRow *row = 0;
1124 oss.str("");
1125 oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1126 << " PATH = '" << this->GetRawPath().Data() << "' AND "
1127 << " NAME = '" << this->GetRawFile().Data() << "' ";
1128 result = conn->Query(oss.str().c_str());
1129 //
1130 if ( !result ) throw -4;;
1131 row = result->Next();
1132 if ( !row ) return(16);
1133 if ( row->GetField(1) ){
1134 this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1135 return(1);
1136 };
1137 if ( !row->GetField(0) ) throw -26;
1138 //
1139 UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1140 //
1141 //
1142 //
1143 TTree *trDumpEv = 0;
1144 trDumpEv = (TTree*)file->Get("VarDump");
1145 if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1146 //
1147 VarDumpEvent *vde = 0;
1148 VarDumpRecord *vdr = 0;
1149 //
1150 trDumpEv->SetBranchAddress("VarDump", &vde);
1151 if ( trDumpEv->GetEntries() > 0 ){
1152 Bool_t found = false;
1153 for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1154 trDumpEv->GetEntry(i);
1155 vde->Records->GetEntries();
1156 if ( vde->Records->GetEntries()>0 ){
1157 found = true;
1158 goto fill;
1159 };
1160 };
1161 fill:
1162 if ( found ){
1163 //
1164 vdr = (VarDumpRecord*)vde->Records->At(6);
1165 //
1166 this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1167 //
1168 } else {
1169 if ( !this->GetBOOTnumber() ) return(4);
1170 };
1171 } else {
1172 if ( !this->GetBOOTnumber() ) return(2);
1173 };
1174 //
1175 oss.str("");
1176 oss << " UPDATE GL_RAW "
1177 << " SET GL_RAW.BOOT_NUMBER = '" << dec << this->GetBOOTnumber() << "'"
1178 << " WHERE GL_RAW.ID = '" << idRaw << "'";
1179 conn->Query(oss.str().c_str());
1180 //
1181 delete result;
1182 return(0);
1183 };
1184
1185 /**
1186 * Scan runtrailer packet, fill the GL_RUN table and
1187 * check for missing and truncated runs
1188 */
1189 Int_t PamelaDBOperations::insertPamelaRUN(){
1190 Int_t signal = 0;
1191 //
1192 stringstream oss;
1193 oss.str("");
1194 //
1195 signal = this->SetUpperLimits();
1196 //
1197 // loop on runheader and runtrailer events
1198 //
1199 TTree *rh=(TTree*)file->Get("RunHeader");
1200 if ( !rh || rh->IsZombie() ) throw -17;
1201 TTree *rt=(TTree*)file->Get("RunTrailer");
1202 if ( !rt || rt->IsZombie() ) throw -18;
1203 //
1204 PacketType *pctp=0;
1205 EventCounter *cod=0;
1206 //
1207 rh->SetBranchAddress("RunHeader", &runh);
1208 rh->SetBranchAddress("Header", &ehh);
1209 //
1210 rt->SetBranchAddress("RunTrailer", &runt);
1211 rt->SetBranchAddress("Header", &eht);
1212 //
1213 UInt_t obtt = 0;
1214 UInt_t obth = 0;
1215 UInt_t pktt = 0;
1216 UInt_t pkth = 0;
1217 Int_t pth = -1;
1218 Int_t ptht = -1;
1219 Int_t evbeft = 0;
1220 Int_t evbefh = 0;
1221 //
1222 // no runtrailers in the file!
1223 //
1224 if ( !rtev ){
1225 if ( !upperentry ){
1226 if ( IsDebug() ) printf(" No physics events nor runs in the file \n");
1227 throw -8;
1228 } else {
1229 this->HandleRunFragments(true,true,0,upperentry);
1230 };
1231 } else {
1232 //
1233 for (Int_t ptt=0; ptt<rtev; ptt++){
1234 //
1235 rt->GetEntry(ptt);
1236 pht = eht->GetPscuHeader();
1237 pktt = pht->GetCounter();
1238 obtt = pht->GetOrbitalTime();
1239 //
1240 cod = eht->GetCounter();
1241 ptht = cod->Get(pctp->RunHeader) - 1;
1242 evbeft = cod->Get(pctp->Physics);
1243 //
1244 if ( !ptt && !(ptht+1) ){
1245 //
1246 if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);
1247 //
1248 this->HandleRunFragments(true,false,0,(evbeft-1));
1249 //
1250 //
1251 } else if ( pth == ptht ){
1252 //
1253 if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1254 //
1255 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1256 rt->GetEntry(ptt-1);
1257 cod = eht->GetCounter();
1258 evbefh = cod->Get(pctp->Physics);
1259 rt->GetEntry(ptt);
1260 pht = eht->GetPscuHeader();
1261 //
1262 if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %i \n",ptht,pth,ptt);
1263 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1264 //
1265 this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1266 //
1267 } else {
1268 //
1269 if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);
1270 //
1271 rh->GetEntry(ptht);
1272 phh = ehh->GetPscuHeader();
1273 pkth = phh->GetCounter();
1274 obth = phh->GetOrbitalTime();
1275 cod = ehh->GetCounter();
1276 evbefh = cod->Get(pctp->Physics);
1277 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1278 //
1279 // handle this run
1280 //
1281 this->HandleRun();
1282 //
1283 //
1284 //
1285 if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1286 //
1287 if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");
1288 //
1289 this->HandleRunFragments(true,true,0,(evbefh-1));
1290 //
1291 };
1292 //
1293 //
1294 if ( (ptht - pth) > 1 ){
1295 //
1296 if ( IsDebug() ) printf(" Missing runtrailers! \n");
1297 if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);
1298 // is not the consecutive header
1299 while ( pth != ptht ){
1300 //
1301 // treat the header(s) in the middle and then go to the next header, repeat until you reach the correct header.
1302 //
1303 pth++;
1304 //
1305 rh->GetEntry(pth+1);
1306 phh = ehh->GetPscuHeader();
1307 pktt = phh->GetCounter();
1308 obtt = phh->GetOrbitalTime();
1309 cod = ehh->GetCounter();
1310 evbeft = cod->Get(pctp->Physics);
1311 rh->GetEntry(pth);
1312 phh = ehh->GetPscuHeader();
1313 cod = ehh->GetCounter();
1314 pkth = phh->GetCounter();
1315 obth = phh->GetOrbitalTime();
1316 evbefh = cod->Get(pctp->Physics);
1317 //
1318 if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %i \n",ptht,pth,ptt);
1319 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);
1320 //
1321 this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1322 //
1323 };
1324 //
1325 } else if ( !(ptht - pth) ){
1326 //
1327 if ( IsDebug() ) printf(" Missing runheader! \n");
1328 if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);
1329 if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1330 //
1331 } else {
1332 //
1333 // go on with next header
1334 //
1335 pth = ptht;
1336 };
1337 //
1338 };
1339 //
1340 if ( ptt+1 == rtev){
1341 ptht++;
1342 if ( ptht < rhev ){
1343 rh->GetEntry(ptht);
1344 phh = ehh->GetPscuHeader();
1345 pkth = phh->GetCounter();
1346 obth = phh->GetOrbitalTime();
1347 cod = ehh->GetCounter();
1348 evbefh = cod->Get(pctp->Physics);
1349 if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);
1350 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);
1351 if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);
1352 //
1353 this->HandleRunFragments(false,true,evbefh,upperentry);
1354 } else {
1355 //
1356 // check if we have a fragment with no header
1357 //
1358 if ( (UInt_t)evbeft < upperentry-1 ){
1359 if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1360 //
1361 if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1362 rt->GetEntry(ptt-1);
1363 cod = eht->GetCounter();
1364 evbefh = cod->Get(pctp->Physics);
1365 rt->GetEntry(ptt);
1366 pht = eht->GetPscuHeader();
1367 this->HandleRunFragments(true,true,evbefh,upperentry);
1368 };
1369 };
1370 };
1371 //
1372 };
1373 };
1374 //
1375 return(signal);
1376 };
1377
1378 /**
1379 *
1380 * Check if the run has already been inserted
1381 *
1382 */
1383 Bool_t PamelaDBOperations::IsRunAlreadyInserted(){
1384 //
1385 TSQLResult *result = 0;
1386 TSQLRow *row = 0;
1387 //
1388 stringstream oss;
1389 oss.str("");
1390 //
1391 // the where clause is of the type: boot_number = _our_boot && (
1392 // ( runhead_time >= (_our_runhead_time-10) && runtrail_time <= (_our_runtrail_time+10) &&
1393 // ( runhead_obt >= _our_runheadobt || runhead_pkt >= _our_runheadpkt ) &&
1394 // ( runtrail_obt >= _our_runtrailobt || runtrail_pkt >= _our_runtrailpkt ) )
1395 // ||
1396 // ( runhead_time <= _our_runhead_time && runtrail_time >= _our_runtrail_time) &&
1397 // ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1398 // ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1399 // )
1400 //
1401 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
1402 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1403 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1404 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1405 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1406 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1407 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1408 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1409 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1410 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1411 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1412 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1413 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1414 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1415 //
1416 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1417 result = conn->Query(oss.str().c_str());
1418 //
1419 if ( !result ) throw -4;
1420 //
1421 row = result->Next();
1422 //
1423 if ( !row ){
1424 if ( IsDebug() ) printf(" The run is new \n");
1425 if ( IsDebug() ) printf(" -> fill the DB \n");
1426 return(false); // the file has not been inserted in the DB, go on.
1427 };
1428 //
1429 Bool_t signal = true;
1430 //
1431 while ( row != NULL ){
1432 if ( IsDebug() ) printf(" A run exists with runheader and runtrailer time and packets compatible with this one \n");
1433 //
1434 // the run has already been inserted
1435 //
1436 // return(true); //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1437 //
1438 // PATCH!
1439 // 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
1440 // while the old run doesn't have it 3) we have more events than the old run
1441 //
1442 if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1443 //
1444 if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1445 oss.str("");
1446 oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1447 if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1448 conn->Query(oss.str().c_str());
1449 if ( signal ) signal = false;
1450 goto gonext;
1451 //
1452 } else if ( glrun->GetNEVENTS() < (UInt_t)atoll(row->GetField(1)) ){
1453 if ( IsDebug() ) printf(" The new run has less events than the old one \n");
1454 if ( IsDebug() ) printf(" The run is already inserted \n");
1455 goto gonext;
1456 };
1457 //
1458 if ( glrun->GetTRK_CALIB() && !(UInt_t)atoll(row->GetField(2)) ){
1459 //
1460 if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1461 //
1462 oss.str("");
1463 oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1464 if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1465 conn->Query(oss.str().c_str());
1466 //
1467 if ( signal ) signal = false;
1468 goto gonext;
1469 } else if ( !glrun->GetTRK_CALIB() && (UInt_t)atoll(row->GetField(2)) ){
1470 if ( IsDebug() ) printf(" The new run has the same number of events but miss the runheader the old has the runheader \n");
1471 if ( IsDebug() ) printf(" The run is already inserted \n");
1472 goto gonext;
1473 };
1474 //
1475 if ( glrun->GetPKT_COUNTER() && !(UInt_t)atoll(row->GetField(3)) ){
1476 //
1477 if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1478 //
1479 oss.str("");
1480 oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1481 if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());
1482 conn->Query(oss.str().c_str());
1483 if ( signal ) signal = false;
1484 //
1485 };
1486 //
1487 gonext:
1488 // END PATCH!
1489 //
1490 row = result->Next();
1491 //
1492 };
1493 //
1494 delete result;
1495 //
1496 if ( signal && IsDebug() ) printf(" The run has already been inserted \n");
1497 return(signal);
1498 };
1499
1500 /**
1501 * Handle runs which seems to be good ones.
1502 **/
1503 void PamelaDBOperations::HandleRun(){
1504 ULong64_t chkpkt = 0;
1505 ULong64_t pktt = (ULong64_t)PKT(pht->GetCounter());
1506 ULong64_t pkth = (ULong64_t)PKT(phh->GetCounter());
1507 //
1508 chkpkt = pkth + (ULong64_t)runt->PKT_COUNTER + 1ULL + 1ULL;
1509 //
1510 if ( labs(chkpkt-pktt)<2 ){
1511 //
1512 if ( IsDebug() ) printf(" check %llu pktt %llu \n",chkpkt,pktt);
1513 //
1514 // it must be a good run, fill the db
1515 //
1516 this->FillClass();
1517 //
1518 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
1519 } else {
1520 //
1521 if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
1522 if ( IsDebug() ) printf(" try to recover run(s) without runheader and runtrailer between runheader and runtrailer\n");
1523 //
1524 this->HandleSuspiciousRun();
1525 //
1526 };
1527 //
1528 //
1529 return;
1530 };
1531
1532
1533 /**
1534 * Handle run fragments at the beginning or at the end of the file
1535 **/
1536 void PamelaDBOperations::HandleRunFragments(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
1537 //
1538 UInt_t rhfirstev = firstev;
1539 UInt_t rtlastev = lastev;
1540 Bool_t found = false;
1541 //
1542 TSQLResult *result = 0;
1543 TSQLRow *row = 0;
1544 //
1545 stringstream oss;
1546 oss.str("");
1547 //
1548 // is the piece of run good (no other packets inside)?
1549 //
1550 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
1551 //
1552 // if not, handle other pieces and continue with the first one
1553 //
1554 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
1555 //
1556 };
1557 //
1558 // we have now the good first piece of a run, fill the glrun object
1559 //
1560 if ( rhfirstev != firstev && !mishead ) mishead = true;
1561 if ( rtlastev != lastev && !mistrail ) mistrail = true;
1562 //
1563 this->FillClass(mishead,mistrail,firstev,lastev);
1564 //
1565 if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1566 //
1567 // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1568 //
1569 if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
1570 // missing it no way we can found a piece in the frag table
1571 //
1572 oss.str("");
1573 oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1574 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1575 << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()
1576 << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1577 //
1578 if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
1579 result = conn->Query(oss.str().c_str());
1580 //
1581 if ( !result ) throw -4;
1582 //
1583 row = result->Next();
1584 //
1585 if ( !row ){
1586 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1587 found = false;
1588 } else {
1589 //
1590 found = false; // default value
1591 //
1592 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1593 //
1594 // if we have both runheader and runtrailer we can check with pkt_counter:
1595 //
1596 if ( !mistrail && (UInt_t)atoll(row->GetField(1)) != 0 ){
1597 ULong64_t chkpkt = 0;
1598 ULong64_t pktt = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1599 ULong64_t pkth = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1600 //
1601 chkpkt = pkth + (ULong64_t)glrun->GetPKT_COUNTER() + 1ULL + 1ULL;
1602 //
1603 if ( labs(chkpkt-pktt)<2 ){
1604 //
1605 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1606 //
1607 found = true;
1608 //
1609 } else {
1610 //
1611 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1612 //
1613 found = false;
1614 //
1615 };
1616 };
1617 if ( !found ){
1618 //
1619 // 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
1620 //
1621 ULong64_t chkpkt1 = 0;
1622 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1623 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1624 chkpkt1 = labs(orunh1-dbrunt1);
1625 //
1626 ULong64_t chkpkt2 = 0;
1627 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNHEADER_OBT());
1628 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1629 chkpkt2 = labs(orunh2-dbrunt2);
1630 //
1631 ULong64_t chkpkt3 = 0;
1632 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNHEADER_TIME());
1633 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
1634 chkpkt3 = labs(orunh3-dbrunt3);
1635 //
1636 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
1637 // if ( chkpkt1 < 100 && chkpkt2 < 30000 && chkpkt3 < 30 ){
1638 //
1639 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
1640 //
1641 found = true;
1642 //
1643 } else {
1644 //
1645 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
1646 //
1647 found = false;
1648 //
1649 };
1650 };
1651 };
1652 //
1653 if ( found ){
1654 //
1655 // 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
1656 //
1657 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1658 //
1659 GL_RUN *glrun1 = new GL_RUN();
1660 //
1661 UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1662 //
1663 oss.str("");
1664 oss << " ID="<<row->GetField(0)<<";";
1665 //
1666 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runheader infos
1667 //
1668 // merge infos
1669 //
1670 UInt_t apkt = PKT(glrun1->GetRUNTRAILER_PKT());
1671 ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
1672 UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
1673 ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
1674 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1675 TTree *T= 0;
1676 T = (TTree*)file->Get("Physics");
1677 if ( !T || T->IsZombie() ) throw -16;
1678 EventHeader *eh = 0;
1679 PscuHeader *ph = 0;
1680 T->SetBranchAddress("Header", &eh);
1681 while ( apkt > bpkt && aobt > bobt && firstev < lastev ){
1682 T->GetEntry(firstev);
1683 ph = eh->GetPscuHeader();
1684 bpkt = PKT(ph->GetCounter());
1685 bobt = OBT(ph->GetOrbitalTime());
1686 firstev++;
1687 };
1688 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1689 //
1690 glrun1->SetID(0);
1691 glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1692 glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1693 glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
1694 glrun1->SetRUNTRAILER_OBT(glrun->GetRUNTRAILER_OBT());
1695 glrun1->SetRUNTRAILER_PKT(glrun->GetRUNTRAILER_PKT());
1696 //
1697 glrun->SetEV_FROM(firstev);
1698 glrun->SetNEVENTS(lastev-firstev+1);
1699 glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1700 glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1701 glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
1702 glrun->SetCOMPILATIONTIMESTAMP(glrun1->GetCOMPILATIONTIMESTAMP());
1703 glrun->SetFAV_WRK_SCHEDULE(glrun1->GetFAV_WRK_SCHEDULE());
1704 glrun->SetEFF_WRK_SCHEDULE(glrun1->GetEFF_WRK_SCHEDULE());
1705 glrun->SetPRH_VAR_TRG_MODE_A(glrun1->GetPRH_VAR_TRG_MODE_A());
1706 glrun->SetPRH_VAR_TRG_MODE_B(glrun1->GetPRH_VAR_TRG_MODE_B());
1707 glrun->SetACQ_BUILD_INFO(glrun1->GetACQ_BUILD_INFO());
1708 glrun->SetACQ_VAR_INFO(glrun1->GetACQ_VAR_INFO());
1709 glrun->SetRM_ACQ_AFTER_CALIB(glrun1->GetRM_ACQ_AFTER_CALIB());
1710 glrun->SetRM_ACQ_SETTING_MODE(glrun1->GetRM_ACQ_SETTING_MODE());
1711 glrun->SetTRK_CALIB_USED(glrun1->GetTRK_CALIB_USED());
1712 glrun->SetCAL_DSP_MASK(glrun1->GetCAL_DSP_MASK());
1713 glrun->SetLAST_TIMESYNC(glrun1->GetLAST_TIMESYNC());
1714 glrun->SetOBT_TIMESYNC(glrun1->GetOBT_TIMESYNC());
1715 //
1716 if ( !IsRunAlreadyInserted() ){
1717 //
1718 glrun->Fill_GL_RUN(conn);
1719 //
1720 // get id number
1721 //
1722 oss.str("");
1723 oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1724 << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1725 << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1726 //
1727 if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1728 //
1729 result = conn->Query(oss.str().c_str());
1730 if ( !result ) throw -4;
1731 row = result->Next();
1732 //
1733 UInt_t idrun0 = 0;
1734 if ( !row ){
1735 throw -10;
1736 } else {
1737 idrun0 = (UInt_t)atoll(row->GetField(0));
1738 };
1739 //
1740 glrun1->SetID_RUN_FRAG(idrun0);
1741 //
1742 glrun1->Fill_GL_RUN(conn);
1743 //
1744 oss.str("");
1745 oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1746 //
1747 if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1748 //
1749 result = conn->Query(oss.str().c_str());
1750 if ( !result ) throw -4;
1751 row = result->Next();
1752 //
1753 UInt_t idrun1 = 0;
1754 if ( !row ){
1755 throw -10;
1756 } else {
1757 idrun1 = (UInt_t)atoll(row->GetField(0));
1758 };
1759 //
1760 oss.str("");
1761 oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
1762 //
1763 result = conn->Query(oss.str().c_str());
1764 if ( !result ) throw -4;
1765 //
1766 };
1767 //
1768 delete glrun1;
1769 //
1770 // delete old entry in fragment table
1771 //
1772 oss.str("");
1773 //
1774 oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";
1775 //
1776 if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());
1777 result = conn->Query(oss.str().c_str());
1778 if ( !result ) throw -4;
1779 //
1780 return;
1781 //
1782 };
1783 //
1784 };
1785 //
1786 if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
1787 // missing it no way we can found a piece in the frag table
1788 //
1789 oss.str("");
1790 oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1791 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1792 << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()
1793 << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1794 //
1795 if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
1796 result = conn->Query(oss.str().c_str());
1797 //
1798 if ( !result ) throw -4;
1799 //
1800 row = result->Next();
1801 //
1802 if ( !row ){
1803 if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1804 found = false;
1805 } else {
1806 //
1807 found = false; // default value
1808 //
1809 if ( IsDebug() ) printf(" Found a possible candidate, checking if it is the good one... \n");
1810 //
1811 // if we have both runheader and runtrailer we can check with pkt_counter:
1812 //
1813 if ( !mishead && (UInt_t)atoll(row->GetField(1)) != 0 ){
1814 ULong64_t chkpkt = 0;
1815 ULong64_t pktt = (ULong64_t)PKT((UInt_t)atoll(row->GetField(4)));
1816 ULong64_t pkth = (ULong64_t)PKT(glrun->GetRUNHEADER_PKT());
1817 //
1818 chkpkt = pkth + (ULong64_t)((UInt_t)atoll(row->GetField(1))) + 1ULL + 1ULL;
1819 //
1820 if ( labs(chkpkt-pktt)<2 ){
1821 //
1822 if ( IsDebug() ) printf(" FOUND!!! check %llu pktt %llu \n",chkpkt,pktt);
1823 //
1824 found = true;
1825 //
1826 } else {
1827 //
1828 if ( IsDebug() ) printf(" The check with pkt counter failed: check %llu pktt %llu \n",chkpkt,pktt);
1829 //
1830 found = false;
1831 //
1832 };
1833 };
1834 if ( !found ){
1835 //
1836 // 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
1837 //
1838 ULong64_t chkpkt1 = 0;
1839 ULong64_t orunh1 = (ULong64_t)PKT(glrun->GetRUNTRAILER_PKT());
1840 ULong64_t dbrunt1 = (ULong64_t)PKT((UInt_t)atoll(row->GetField(5)));
1841 chkpkt1 = labs(orunh1-dbrunt1);
1842 //
1843 ULong64_t chkpkt2 = 0;
1844 ULong64_t orunh2 = (ULong64_t)OBT(glrun->GetRUNTRAILER_OBT());
1845 ULong64_t dbrunt2 = (ULong64_t)OBT((UInt_t)atoll(row->GetField(3)));
1846 chkpkt2 = labs(orunh2-dbrunt2);
1847 //
1848 ULong64_t chkpkt3 = 0;
1849 ULong64_t orunh3 = (ULong64_t)(glrun->GetRUNTRAILER_TIME());
1850 ULong64_t dbrunt3 = (ULong64_t)((UInt_t)atoll(row->GetField(2)));
1851 chkpkt3 = labs(orunh3-dbrunt3);
1852 //
1853 if ( (chkpkt1 < 200 || chkpkt2 < 20000) && chkpkt3 < 20 ){
1854 //
1855 if ( IsDebug() ) printf(" FOUND!!! check1 %llu<200 cechk2 %llu<20000 check3 %llu<20 \n",chkpkt1,chkpkt2,chkpkt3);
1856 //
1857 found = true;
1858 //
1859 } else {
1860 //
1861 if ( IsDebug() ) printf(" Check failed: check1 %llu<200? cechk2 %llu<20000? check3 %llu<20? \n",chkpkt1,chkpkt2,chkpkt3);
1862 //
1863 found = false;
1864 //
1865 };
1866 };
1867 };
1868 //
1869 if ( found ){
1870 //
1871 // 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
1872 //
1873 if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1874 //
1875 GL_RUN *glrun1 = new GL_RUN();
1876 //
1877 UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1878 //
1879 oss.str("");
1880 oss << " ID="<<row->GetField(0)<<";";
1881 //
1882 glrun1->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn); // here we have runtrailer infos
1883 //
1884 // merge infos
1885 //
1886 UInt_t apkt = PKT(glrun->GetRUNTRAILER_PKT());
1887 ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
1888 UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
1889 ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
1890 if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
1891 TTree *T= 0;
1892 T = (TTree*)file->Get("Physics");
1893 if ( !T || T->IsZombie() ) throw -16;
1894 EventHeader *eh = 0;
1895 PscuHeader *ph = 0;
1896 T->SetBranchAddress("Header", &eh);
1897 while ( apkt > bpkt && aobt > bobt && lastev > 0 ){
1898 T->GetEntry(lastev);
1899 ph = eh->GetPscuHeader();
1900 apkt = PKT(ph->GetCounter());
1901 aobt = OBT(ph->GetOrbitalTime());
1902 lastev--;
1903 };
1904 if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
1905 //
1906 glrun->SetEV_TO(lastev);
1907 glrun->SetNEVENTS(lastev-firstev+1);
1908 glrun->SetPKT_COUNTER(glrun1->GetPKT_COUNTER());
1909 glrun->SetPKT_READY_COUNTER(glrun1->GetPKT_READY_COUNTER());
1910 glrun->SetRUNTRAILER_TIME(glrun1->GetRUNTRAILER_TIME());
1911 glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
1912 glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
1913 //
1914 glrun1->SetID(0);
1915 glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
1916 glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
1917 glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
1918 glrun1->SetCOMPILATIONTIMESTAMP(glrun->GetCOMPILATIONTIMESTAMP());
1919 glrun1->SetFAV_WRK_SCHEDULE(glrun->GetFAV_WRK_SCHEDULE());
1920 glrun1->SetEFF_WRK_SCHEDULE(glrun->GetEFF_WRK_SCHEDULE());
1921 glrun1->SetPRH_VAR_TRG_MODE_A(glrun->GetPRH_VAR_TRG_MODE_A());
1922 glrun1->SetPRH_VAR_TRG_MODE_B(glrun->GetPRH_VAR_TRG_MODE_B());
1923 glrun1->SetACQ_BUILD_INFO(glrun->GetACQ_BUILD_INFO());
1924 glrun1->SetACQ_VAR_INFO(glrun->GetACQ_VAR_INFO());
1925 glrun1->SetRM_ACQ_AFTER_CALIB(glrun->GetRM_ACQ_AFTER_CALIB());
1926 glrun1->SetRM_ACQ_SETTING_MODE(glrun->GetRM_ACQ_SETTING_MODE());
1927 glrun1->SetTRK_CALIB_USED(glrun->GetTRK_CALIB_USED());
1928 glrun1->SetCAL_DSP_MASK(glrun->GetCAL_DSP_MASK());
1929 glrun1->SetLAST_TIMESYNC(glrun->GetLAST_TIMESYNC());
1930 glrun1->SetOBT_TIMESYNC(glrun->GetOBT_TIMESYNC());
1931 //
1932 if ( !IsRunAlreadyInserted() ){
1933 //
1934 glrun->Fill_GL_RUN(conn);
1935 //
1936 // get id number
1937 //
1938 oss.str("");
1939 oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1940 << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1941 << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1942 //
1943 if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1944 //
1945 result = conn->Query(oss.str().c_str());
1946 if ( !result ) throw -4;
1947 row = result->Next();
1948 //
1949 UInt_t idrun0 = 0;
1950 if ( !row ){
1951 throw -10;
1952 } else {
1953 idrun0 = (UInt_t)atoll(row->GetField(0));
1954 };
1955 //
1956 glrun1->SetID_RUN_FRAG(idrun0);
1957 //
1958 glrun1->Fill_GL_RUN(conn);
1959 //
1960 oss.str("");
1961 oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1962 //
1963 if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1964 //
1965 result = conn->Query(oss.str().c_str());
1966 if ( !result ) throw -4;
1967 row = result->Next();
1968 //
1969 UInt_t idrun1 = 0;
1970 if ( !row ){
1971 throw -10;
1972 } else {
1973 idrun1 = (UInt_t)atoll(row->GetField(0));
1974 };
1975 //
1976 oss.str("");
1977 oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
1978 //
1979 result = conn->Query(oss.str().c_str());
1980 if ( !result ) throw -4;
1981 //
1982 };
1983 //
1984 delete glrun1;
1985 //
1986 // delete old entry in fragment table
1987 //
1988 oss.str("");
1989 //
1990 oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";
1991 //
1992 if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());
1993 result = conn->Query(oss.str().c_str());
1994 if ( !result ) throw -4;
1995 //
1996 //
1997 return;
1998 //
1999 };
2000 //
2001 };
2002 //
2003 if ( !found ){
2004 //
2005 if ( IsDebug() ) printf(" not found, check if we have already processed the file \n ");
2006 //
2007 // not found, has this run already inserted in the GL_RUN or in the GL_RUN_FRAGMENTS table?
2008 //
2009 oss.str("");
2010 oss << " SELECT ID FROM GL_RUN WHERE "
2011 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2012 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2013 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2014 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2015 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2016 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2017 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2018 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2019 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2020 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2021 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2022 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2023 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2024 //
2025 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2026 result = conn->Query(oss.str().c_str());
2027 //
2028 if ( !result ) throw -4;
2029 //
2030 row = result->Next();
2031 //
2032 if ( !row ){
2033 //
2034 oss.str("");
2035 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
2036 << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
2037 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
2038 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
2039 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
2040 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2041 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
2042 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
2043 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2044 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
2045 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
2046 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2047 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2048 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2049 //
2050 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2051 result = conn->Query(oss.str().c_str());
2052 //
2053 if ( !result ) throw -4;
2054 //
2055 row = result->Next();
2056 //
2057 if ( !row ){
2058 //
2059 // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2060 //
2061 if ( IsDebug() ) printf(" The run is new \n");
2062 if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2063 //
2064 glrun->Fill_GL_RUN_FRAGMENTS(conn);
2065 //
2066 } else {
2067 if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2068 };
2069 } else {
2070 if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2071 };
2072 };
2073 //
2074 return;
2075 };
2076
2077
2078 /**
2079 * Handle run without header or trailer
2080 **/
2081 void PamelaDBOperations::HandleMissingHoT(Bool_t mishead, Bool_t mistrail, UInt_t firstev, UInt_t lastev){
2082 //
2083 //
2084 // is the piece of run good (no other packets inside)?
2085 //
2086 if ( !this->IsRunConsistent(mishead,mistrail,firstev,lastev)){
2087 //
2088 // if not, handle other pieces and continue with the first one
2089 //
2090 if ( IsDebug() ) printf("The run is not consistent, it contains non-physics packets! The run has been handled \n");
2091 //
2092 } else {
2093 //
2094 this->FillClass(mishead,mistrail,firstev,lastev);
2095 //
2096 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2097 //
2098 };
2099 //
2100 return;
2101 };
2102
2103 /**
2104 *
2105 * check if we have non-physics packets inside the run
2106 *
2107 */
2108 Bool_t PamelaDBOperations::IsRunConsistent(Bool_t mishead, Bool_t mistrail, UInt_t &firstev, UInt_t &lastev){
2109 //
2110 EventCounter *code=0;
2111 //
2112 UInt_t nevent = 0;
2113 UInt_t checkfirst = 0;
2114 UInt_t checklast = 0;
2115 UInt_t firstentry = 0;
2116 UInt_t lastentry = 0;
2117 UInt_t firstTime = 0;
2118 UInt_t lastTime = 0;
2119 UInt_t firstPkt = 0;
2120 UInt_t lastPkt = 0;
2121 UInt_t firstObt = 0;
2122 UInt_t lastObt = 0;
2123 //
2124 pcksList packetsNames;
2125 pcksList::iterator Iter;
2126 getPacketsNames(packetsNames);
2127 //
2128 TTree *T= 0;
2129 T =(TTree*)file->Get("Physics");
2130 if ( !T || T->IsZombie() ) throw -16;
2131 EventHeader *eh = 0;
2132 PscuHeader *ph = 0;
2133 T->SetBranchAddress("Header", &eh);
2134 nevent = T->GetEntries();
2135 //
2136 //
2137 if ( firstev == lastev+1 ) { // no events inside the run!
2138 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2139 // return true is correct
2140 return(true);
2141 //
2142 } else {
2143 //
2144 T->GetEntry(firstev);
2145 code = eh->GetCounter();
2146 checkfirst = 0;
2147 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2148 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2149 };
2150 if ( IsDebug() ) printf(" Check first is %i firstev is %i\n",checkfirst,firstev);
2151 //
2152 T->GetEntry(lastev);
2153 code = eh->GetCounter();
2154 checklast = 0;
2155 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2156 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2157 };
2158 if ( IsDebug() ) printf(" Check last is %i lastev is %i\n",checklast,lastev);
2159 //
2160 if ( checkfirst == checklast ){
2161 //
2162 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2163 //
2164 return(true);
2165 //
2166 } else {
2167 //
2168 if ( IsDebug() ) printf(" There are no-phyics packets inside the run!\n");
2169 //
2170 // HERE WE MUST HANDLE THAT RUNS AND GO BACK
2171 //
2172 if ( IsDebug() ) printf(" Never seen this case, try to handle it anyway, it was throw -95\n");
2173 //
2174 Bool_t emptyruns = false;
2175 UInt_t check = 0;
2176 UInt_t lastevtemp = lastev;
2177 UInt_t firstevno = firstev;
2178 //
2179 for (UInt_t i=firstev; i<=lastev; i++){
2180 //
2181 T->GetEntry(i);
2182 code = eh->GetCounter();
2183 //
2184 check = 0;
2185 //
2186 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2187 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2188 };
2189 //
2190 if ( checkfirst < check || i == lastev ){
2191 //
2192 firstentry = firstevno;
2193 //
2194 if ( checkfirst < check ){
2195 lastentry = i-1;
2196 } else {
2197 lastentry = i;
2198 };
2199 //
2200 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2201 //
2202 glrun->SetEV_FROM(firstentry);
2203 glrun->SetEV_TO(lastentry);
2204 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2205 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2206 lastentry--;
2207 };
2208 glrun->SetNEVENTS(lastentry-firstentry+1);
2209 //
2210 glrun->Set_GL_RUNH0();
2211 glrun->Set_GL_RUNT0();
2212 //
2213 glrun->SetLAST_TIMESYNC(0);
2214 glrun->SetOBT_TIMESYNC(0);
2215 //
2216 T->GetEntry(firstentry);
2217 ph = eh->GetPscuHeader();
2218 firstObt = ph->GetOrbitalTime();
2219 firstTime = this->GetAbsTime(firstObt);
2220 firstPkt = ph->GetCounter();
2221 //
2222 T->GetEntry(lastentry);
2223 ph = eh->GetPscuHeader();
2224 lastObt = ph->GetOrbitalTime();
2225 lastTime = this->GetAbsTime(lastObt);
2226 lastPkt = ph->GetCounter();
2227 //
2228 glrun->SetRUNHEADER_PKT(firstPkt);
2229 glrun->SetRUNTRAILER_PKT(lastPkt);
2230 //
2231 glrun->SetRUNHEADER_OBT(firstObt);
2232 glrun->SetRUNTRAILER_OBT(lastObt);
2233 //
2234 if ( firstev == firstentry && !emptyruns && !mishead ){
2235 glrun->Set_GL_RUNH(runh,phh);
2236 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2237 if ( IsDebug() ) printf(" We have the runheader \n");
2238 };
2239 if ( lastev == i && !mistrail ){
2240 glrun->Set_GL_RUNT(runt,pht);
2241 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2242 if ( IsDebug() ) printf(" We have the runtrailer \n");
2243 };
2244 //
2245 if ( lastentry == (firstentry-2) ){ // no events in the run
2246 emptyruns = true;
2247 if ( IsDebug() ) printf(" No events in the run \n");
2248 lastTime = firstTime;
2249 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2250 lastObt = glrun->RUNHEADER_OBT;
2251 lastPkt = glrun->RUNHEADER_PKT;
2252 } else {
2253 lastObt = firstObt;
2254 lastPkt = firstPkt;
2255 };
2256 glrun->SetRUNTRAILER_PKT(lastPkt);
2257 glrun->SetRUNTRAILER_OBT(lastObt);
2258 lastentry++;
2259 };
2260 //
2261 this->SetCommonGLRUN(firstTime,lastTime);
2262 //
2263 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2264 //
2265 firstevno = lastentry + 1;
2266 //
2267 checkfirst = check;
2268 //
2269 };
2270 //
2271 if ( check == checklast && i != lastev ){
2272 lastevtemp = i - 1;
2273 i = lastev - 1;
2274 };
2275 //
2276 };
2277 //
2278 lastev = lastevtemp;
2279 //
2280 return(false);
2281 //
2282 };
2283 };
2284 //
2285 return(false); // should never arrive here
2286 };
2287
2288 /**
2289 *
2290 * 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
2291 * 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
2292 * looking for non-physics packets inside.
2293 *
2294 */
2295 void PamelaDBOperations::HandleSuspiciousRun(){
2296 //
2297 PacketType *pctp=0;
2298 EventCounter *codt=0;
2299 EventCounter *codh=0;
2300 EventCounter *code=0;
2301 UInt_t firstev = 0;
2302 UInt_t lastev = 0;
2303 UInt_t nevent = 0;
2304 UInt_t checkfirst = 0;
2305 UInt_t checklast = 0;
2306 UInt_t firstentry = 0;
2307 UInt_t lastentry = 0;
2308 UInt_t firstTime = 0;
2309 UInt_t lastTime = 0;
2310 UInt_t firstPkt = 0;
2311 UInt_t lastPkt = 0;
2312 UInt_t firstObt = 0;
2313 UInt_t lastObt = 0;
2314 //
2315 pcksList packetsNames;
2316 pcksList::iterator Iter;
2317 getPacketsNames(packetsNames);
2318 //
2319 TTree *rh=0;
2320 rh = (TTree*)file->Get("RunHeader");
2321 if ( !rh || rh->IsZombie() ) throw -17;
2322 TTree *T=0;
2323 T =(TTree*)file->Get("Physics");
2324 if ( !T || T->IsZombie() ) throw -16;
2325 EventHeader *eh = 0;
2326 PscuHeader *ph = 0;
2327 T->SetBranchAddress("Header", &eh);
2328 nevent = T->GetEntries();
2329 //
2330 codt = eht->GetCounter();
2331 codh = ehh->GetCounter();
2332 firstev = codh->Get(pctp->Physics);
2333 lastev = codt->Get(pctp->Physics)-1;
2334 //
2335 if ( firstev == lastev+1 ) { // no events inside the run!
2336 if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2337 //
2338 this->FillClass();
2339 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2340 //
2341 } else {
2342 //
2343 UInt_t nrunh = 0;
2344 UInt_t nrunh1 = 0;
2345 T->GetEntry(firstev);
2346 code = eh->GetCounter();
2347 checkfirst = 0;
2348 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2349 if ( strcmp(*Iter,"Physics") ) checkfirst += code->Get(GetPacketType(*Iter));
2350 if ( !strcmp(*Iter,"RunHeader") ) nrunh1++;
2351 };
2352 if ( IsDebug() ) printf(" Check first is %i \n",checkfirst);
2353 //
2354 T->GetEntry(lastev);
2355 code = eh->GetCounter();
2356 checklast = 0;
2357 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2358 if ( strcmp(*Iter,"Physics") ) checklast += code->Get(GetPacketType(*Iter));
2359 };
2360 if ( IsDebug() ) printf(" Check last is %i \n",checklast);
2361 //
2362 if ( checkfirst == checklast ){
2363 //
2364 if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2365 //
2366 this->FillClass();
2367 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2368 //
2369 } else {
2370 //
2371 if ( IsDebug() ) printf(" There are no-physics packets inside the run, try to separate runs \n");
2372 //
2373 Bool_t emptyruns = false;
2374 UInt_t check = 0;
2375 UInt_t firstevno = firstev;
2376 //
2377 for (UInt_t i=firstev; i<=lastev; i++){
2378 //
2379 T->GetEntry(i);
2380 code = eh->GetCounter();
2381 //
2382 check = 0;
2383 //
2384 for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
2385 if ( strcmp(*Iter,"Physics") ) check += code->Get(GetPacketType(*Iter));
2386 if ( !strcmp(*Iter,"RunHeader") ) nrunh++;
2387 };
2388 //
2389 if ( checkfirst < check || i == lastev ){
2390 //
2391 firstentry = firstevno;
2392 //
2393 if ( checkfirst < check ){
2394 lastentry = i-1;
2395 } else {
2396 lastentry = i;
2397 };
2398 //
2399 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2400 //
2401 glrun->SetEV_FROM(firstentry);
2402 glrun->SetEV_TO(lastentry);
2403 if ( lastentry == (firstentry-1) ){ // no physics packets inside physics run with no runheader no runtrailer
2404 if ( IsDebug() ) printf(" no physics packets inside physics run with no runheader no runtrailer\n");
2405 lastentry--;
2406 };
2407 glrun->SetNEVENTS(lastentry-firstentry+1);
2408 //
2409 glrun->Set_GL_RUNH0();
2410 glrun->Set_GL_RUNT0();
2411 //
2412 glrun->SetLAST_TIMESYNC(0);
2413 glrun->SetOBT_TIMESYNC(0);
2414 //
2415 T->GetEntry(firstentry);
2416 ph = eh->GetPscuHeader();
2417 firstObt = ph->GetOrbitalTime();
2418 firstTime = this->GetAbsTime(firstObt);
2419 firstPkt = ph->GetCounter();
2420 //
2421 T->GetEntry(lastentry);
2422 ph = eh->GetPscuHeader();
2423 lastObt = ph->GetOrbitalTime();
2424 lastTime = this->GetAbsTime(lastObt);
2425 lastPkt = ph->GetCounter();
2426 //
2427 glrun->SetRUNHEADER_PKT(firstPkt);
2428 glrun->SetRUNTRAILER_PKT(lastPkt);
2429 //
2430 glrun->SetRUNHEADER_OBT(firstObt);
2431 glrun->SetRUNTRAILER_OBT(lastObt);
2432 //
2433 if ( (firstev == firstentry && !emptyruns) || nrunh == (nrunh1 + 1) ){
2434 rh->GetEntry(nrunh1-1);
2435 phh = ehh->GetPscuHeader();
2436 nrunh1++;
2437 glrun->Set_GL_RUNH(runh,phh);
2438 firstTime = this->GetAbsTime(phh->GetOrbitalTime());
2439 if ( IsDebug() ) printf(" We have the runheader \n");
2440 };
2441 if ( lastev == i && checkfirst == check ){
2442 glrun->Set_GL_RUNT(runt,pht);
2443 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2444 if ( IsDebug() ) printf(" We have the runtrailer \n");
2445 };
2446 //
2447 if ( lastentry == (firstentry-2) ){ // no events in the run
2448 emptyruns = true;
2449 if ( IsDebug() ) printf(" No events in the run \n");
2450 lastTime = firstTime;
2451 if ( (UInt_t)firstTime == this->GetAbsTime(phh->GetOrbitalTime()) ){
2452 lastObt = glrun->RUNHEADER_OBT;
2453 lastPkt = glrun->RUNHEADER_PKT;
2454 } else {
2455 lastObt = firstObt;
2456 lastPkt = firstPkt;
2457 };
2458 glrun->SetRUNTRAILER_PKT(lastPkt);
2459 glrun->SetRUNTRAILER_OBT(lastObt);
2460 lastentry++;
2461 };
2462 //
2463 this->SetCommonGLRUN(firstTime,lastTime);
2464 //
2465 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2466 //
2467 if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2468 //
2469 firstentry = i;
2470 //
2471 lastentry = i;
2472 //
2473 if ( IsDebug() ) printf(" Run between %i and %i entries\n",firstentry,lastentry);
2474 //
2475 glrun->SetEV_FROM(firstentry);
2476 glrun->SetEV_TO(lastentry);
2477 glrun->SetNEVENTS(lastentry-firstentry+1);
2478 //
2479 glrun->Set_GL_RUNH0();
2480 //
2481 glrun->SetLAST_TIMESYNC(0);
2482 glrun->SetOBT_TIMESYNC(0);
2483 //
2484 T->GetEntry(firstentry);
2485 ph = eh->GetPscuHeader();
2486 firstObt = ph->GetOrbitalTime();
2487 firstTime = this->GetAbsTime(firstObt);
2488 firstPkt = ph->GetCounter();
2489 //
2490 glrun->SetRUNHEADER_PKT(firstPkt);
2491 //
2492 glrun->SetRUNHEADER_OBT(firstObt);
2493 //
2494 glrun->Set_GL_RUNT(runt,pht);
2495 lastTime = this->GetAbsTime(pht->GetOrbitalTime());
2496 if ( IsDebug() ) printf(" We have the runtrailer \n");
2497 //
2498 this->SetCommonGLRUN(firstTime,lastTime);
2499 //
2500 if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);
2501 };
2502 //
2503 firstevno = lastentry + 1;
2504 //
2505 checkfirst = check;
2506 //
2507 };
2508 //
2509 if ( check == checklast && i != lastev ) i = lastev - 1; // >>>>>>>>>>>>>>>>>>>>>>
2510 //
2511 };
2512 };
2513 };
2514 //
2515 return;
2516 };
2517
2518
2519 /**
2520 * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
2521 */
2522 Int_t PamelaDBOperations::insertCALO_CALIB(){
2523 //
2524 TSQLResult *result = 0;
2525 TSQLRow *row = 0;
2526 //
2527 stringstream oss;
2528 oss.str("");
2529 //
2530 CalibCalPedEvent *calibCalPed = 0;
2531 TTree *tr = 0;
2532 EventHeader *eh = 0;
2533 PscuHeader *ph = 0;
2534 //
2535 UInt_t nevents = 0;
2536 UInt_t fromtime = 0;
2537 UInt_t totime = 0;
2538 UInt_t obt = 0;
2539 UInt_t pkt = 0;
2540 //
2541 tr = (TTree*)file->Get("CalibCalPed");
2542 if ( !tr || tr->IsZombie() ) throw -21;
2543 //
2544 tr->SetBranchAddress("CalibCalPed", &calibCalPed);
2545 tr->SetBranchAddress("Header", &eh);
2546 nevents = tr->GetEntries();
2547 //
2548 if ( !nevents ) return(1);
2549 //
2550 for (UInt_t i=0; i < nevents; i++){
2551 tr->GetEntry(i);
2552 for (UInt_t section = 0; section < 4; section++){
2553 //
2554 if ( calibCalPed->cstwerr[section] ){
2555 valid = 1;
2556 if ( calibCalPed->cperror[section] ) valid = 0;
2557 ph = eh->GetPscuHeader();
2558 obt = ph->GetOrbitalTime();
2559 pkt = ph->GetCounter();
2560 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
2561 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2562 //
2563 if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2564 //
2565 // check if the calibration has already been inserted
2566 //
2567 oss.str("");
2568 oss << " SELECT ID FROM GL_CALO_CALIB WHERE "
2569 << " SECTION = "<< section << " AND "
2570 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
2571 << " OBT = "<< obt << " AND "
2572 << " PKT = "<< pkt << ";";
2573 //
2574 if ( IsDebug() ) printf(" Check if the calo calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2575 result = conn->Query(oss.str().c_str());
2576 //
2577 if ( !result ) throw -4;
2578 //
2579 row = result->Next();
2580 //
2581 if ( row ){
2582 //
2583 if ( IsDebug() ) printf(" Calo calibration already inserted in the DB\n");
2584 //
2585 } else {
2586 //
2587 // we have to insert a new calibration, check where to place it
2588 //
2589 oss.str("");
2590 oss << " SELECT ID,TO_TIME FROM GL_CALO_CALIB WHERE "
2591 << " SECTION = "<< section << " AND "
2592 << " FROM_TIME < "<< fromtime << " AND "
2593 << " TO_TIME > "<< fromtime << ";";
2594 //
2595 if ( IsDebug() ) printf(" Check where to place the calo calibration: query is \n %s \n",oss.str().c_str());
2596 result = conn->Query(oss.str().c_str());
2597 //
2598 if ( !result ) throw -4;
2599 //
2600 row = result->Next();
2601 //
2602 if ( !row ){
2603 //
2604 // no calibrations in the db contain our calibration
2605 //
2606 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2607 if ( fromtime < 1150863400 ){
2608 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2609 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
2610 };
2611 //
2612 oss.str("");
2613 oss << " SELECT FROM_TIME FROM GL_CALO_CALIB WHERE "
2614 << " SECTION = "<< section << " AND "
2615 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2616 //
2617 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2618 result = conn->Query(oss.str().c_str());
2619 //
2620 if ( !result ) throw -4;
2621 //
2622 row = result->Next();
2623 if ( !row ){
2624 totime = numeric_limits<UInt_t>::max();
2625 } else {
2626 totime = (UInt_t)atoll(row->GetField(0));
2627 };
2628 //
2629 } else {
2630 //
2631 // determine upper and lower limits and make space for the new calibration
2632 //
2633 totime = (UInt_t)atoll(row->GetField(1));
2634 //
2635 oss.str("");
2636 oss << " UPDATE GL_CALO_CALIB SET "
2637 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2638 << " ID = "<< row->GetField(0) << ";";
2639 //
2640 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
2641 result = conn->Query(oss.str().c_str());
2642 //
2643 if ( !result ) throw -4;
2644 //
2645 };
2646 //
2647 oss.str("");
2648 oss << " INSERT INTO GL_CALO_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
2649 << " VALUES (NULL,' "
2650 << idroot << "','"
2651 << i << "','"
2652 << fromtime << "','"
2653 << totime << "','"
2654 << section << "','"
2655 << obt << "','"
2656 << pkt << "','"
2657 << this->GetBOOTnumber() << "','"
2658 << valid << "');";
2659 //
2660 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
2661 //
2662 result = conn->Query(oss.str().c_str());
2663 //
2664 if ( !result ) throw -4;
2665 //
2666 };
2667 //
2668 } else {
2669 //
2670 if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2671 //
2672 };
2673 //
2674 };
2675 };
2676 };
2677 //
2678 return(0);
2679 };
2680
2681 /**
2682 * Fill the GL_TRK_CALIB table
2683 */
2684 void PamelaDBOperations::HandleTRK_CALIB(Bool_t pk1, Bool_t pk2){
2685 //
2686 TSQLResult *result = 0;
2687 TSQLRow *row = 0;
2688 //
2689 stringstream oss;
2690 oss.str("");
2691 //
2692 UInt_t totime = 0;
2693 //
2694 if ( !pk1 && !pk2 ){
2695 if ( IsDebug() ) printf(" Cannot handle trk calibration with both packet missing!\n");
2696 };
2697 //
2698 // check if the calibration has already been inserted
2699 //
2700 oss.str("");
2701 oss << " SELECT ID FROM GL_TRK_CALIB WHERE "
2702 << " BOOT_NUMBER = "<< this->GetBOOTnumber(); //
2703 oss << " AND ( ( ";
2704 if ( pk1 ){
2705 oss << " OBT1 = "<< obt1 << " AND "
2706 << " PKT1 = "<< pkt1
2707 << " ) OR ( ";
2708 } else {
2709 oss << " PKT1 = "<< pkt2-1
2710 << " ) OR ( ";
2711 };
2712 if ( pk2 ){
2713 oss << " OBT2 = "<< obt2 << " AND "
2714 << " PKT2 = "<< pkt2;
2715 } else {
2716 oss << " PKT2 = "<< pkt1+1;
2717 };
2718 oss << " ) );";
2719 //
2720 if ( IsDebug() ) printf(" Check if the trk calibration has already been inserted: query is \n %s \n",oss.str().c_str());
2721 result = conn->Query(oss.str().c_str());
2722 //
2723 if ( !result ) throw -4;
2724 //
2725 row = result->Next();
2726 //
2727 if ( row ){
2728 //
2729 if ( IsDebug() ) printf(" Trk calibration already inserted in the DB\n");
2730 //
2731 } else {
2732 //
2733 // we have to insert a new calibration, check where to place it
2734 //
2735 oss.str("");
2736 oss << " SELECT ID,TO_TIME FROM GL_TRK_CALIB WHERE "
2737 << " FROM_TIME < "<< fromtime << " AND "
2738 << " TO_TIME > "<< fromtime << ";";
2739 //
2740 if ( IsDebug() ) printf(" Check where to place the trk calibration: query is \n %s \n",oss.str().c_str());
2741 result = conn->Query(oss.str().c_str());
2742 //
2743 if ( !result ) throw -4;
2744 //
2745 row = result->Next();
2746 //
2747 if ( !row ){
2748 //
2749 // no calibrations in the db contain our calibration
2750 //
2751 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2752 if ( fromtime < 1150863400 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
2753 //
2754 oss.str("");
2755 oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
2756 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
2757 //
2758 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
2759 result = conn->Query(oss.str().c_str());
2760 //
2761 if ( !result ) throw -4;
2762 //
2763 row = result->Next();
2764 if ( !row ){
2765 totime = numeric_limits<UInt_t>::max();
2766 } else {
2767 totime = (UInt_t)atoll(row->GetField(0));
2768 };
2769 //
2770 } else {
2771 //
2772 // determine upper and lower limits and make space for the new calibration
2773 //
2774 totime = (UInt_t)atoll(row->GetField(1));
2775 //
2776 oss.str("");
2777 oss << " UPDATE GL_TRK_CALIB SET "
2778 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
2779 << " ID = "<< row->GetField(0) << ";";
2780 //
2781 if ( IsDebug() ) printf(" Make space for the new trk calibration: query is \n %s \n",oss.str().c_str());
2782 result = conn->Query(oss.str().c_str());
2783 //
2784 if ( !result ) throw -4;
2785 //
2786 };
2787 //
2788 oss.str("");
2789 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) "
2790 << " VALUES (NULL,' "
2791 << idroot << "',";
2792 //
2793 if ( !pk1 ){
2794 oss << "NULL,";
2795 } else {
2796 oss << "'"
2797 << t1 << "',";
2798 };
2799 //
2800 if ( !pk2 ){
2801 oss << "NULL,'";
2802 } else {
2803 oss << "'"
2804 << t2 << "','";
2805 };
2806 //
2807 oss << fromtime << "','"
2808 << totime << "','"
2809 << obt1 << "','"
2810 << pkt1 << "','"
2811 << obt2 << "','"
2812 << pkt2 << "','"
2813 << this->GetBOOTnumber() << "','"
2814 << valid << "');";
2815 //
2816 if ( IsDebug() ) printf(" Insert the new trk calibration: query is \n %s \n",oss.str().c_str());
2817 //
2818 result = conn->Query(oss.str().c_str());
2819 //
2820 if ( !result ) throw -4;
2821 //
2822 };
2823 //
2824 };
2825
2826 /**
2827 * Scan tracker calibrations packets, fill the GL_TRK_CALIB table
2828 */
2829 Int_t PamelaDBOperations::insertTRK_CALIB(){
2830 //
2831 CalibTrk1Event *caltrk1 = 0;
2832 CalibTrk2Event *caltrk2 = 0;
2833 TTree *tr1 = 0;
2834 TTree *tr2 = 0;
2835 EventHeader *eh1 = 0;
2836 PscuHeader *ph1 = 0;
2837 EventHeader *eh2 = 0;
2838 PscuHeader *ph2 = 0;
2839 //
2840 PacketType *pctp=0;
2841 EventCounter *codt2=0;
2842 //
2843 Int_t nevents1 = 0;
2844 Int_t nevents2 = 0;
2845 //
2846 fromtime = 0;
2847 //
2848 obt1 = 0;
2849 pkt1 = 0;
2850 obt2 = 0;
2851 pkt2 = 0;
2852 //
2853 tr1 = (TTree*)file->Get("CalibTrk1");
2854 if ( !tr1 || tr1->IsZombie() ) throw -22;
2855 tr2 = (TTree*)file->Get("CalibTrk2");
2856 if ( !tr2 || tr2->IsZombie() ) throw -23;
2857 //
2858 tr1->SetBranchAddress("CalibTrk1", &caltrk1);
2859 tr1->SetBranchAddress("Header", &eh1);
2860 nevents1 = tr1->GetEntries();
2861 tr2->SetBranchAddress("CalibTrk2", &caltrk2);
2862 tr2->SetBranchAddress("Header", &eh2);
2863 nevents2 = tr2->GetEntries();
2864 //
2865 if ( !nevents1 && !nevents2 ) return(1);
2866 //
2867 t2 = -1;
2868 Int_t pret2 = 0;
2869 Int_t t2t1cal = 0;
2870 //
2871 for (t1=0; t1 < nevents1; t1++){
2872 //
2873 pret2 = t2;
2874 tr1->GetEntry(t1);
2875 //
2876 ph1 = eh1->GetPscuHeader();
2877 obt1 = ph1->GetOrbitalTime();
2878 pkt1 = ph1->GetCounter();
2879 fromtime = this->GetAbsTime(ph1->GetOrbitalTime());
2880 //
2881 valid = 1;
2882 //
2883 if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
2884 //
2885 //
2886 if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
2887 //
2888 if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
2889 //
2890 // Do we have the second calibration packet?
2891 //
2892 while ( t2t1cal < t1+1 ){ // get the calibration packet2 that follows the packet1
2893 //
2894 t2++;
2895 //
2896 if ( t2 < nevents2 ){
2897 tr2->GetEntry(t2);
2898 codt2 = eh2->GetCounter();
2899 t2t1cal = codt2->Get(pctp->CalibTrk1);
2900 //
2901 ph2 = eh2->GetPscuHeader();
2902 obt2 = ph2->GetOrbitalTime();
2903 pkt2 = ph2->GetCounter();
2904 //
2905 if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
2906 //
2907 } else {
2908 //
2909 // running out of vector without finding the corresponding calibration, sig
2910 //
2911 pret2 = t2;
2912 obt2 = 0;
2913 pkt2 = pkt1+2;
2914 t2t1cal = t1+1;
2915 };
2916 if ( this->PKT(pkt2) < this->PKT(pktfirst) && this->OBT(obt2) < this->OBT(obtfirst) ){
2917 //
2918 // running out of vector without finding the corresponding calibration, sig
2919 //
2920 pret2 = t2;
2921 obt2 = 0;
2922 pkt2 = pkt1+2;
2923 t2t1cal = t1+1;
2924 };
2925 //
2926 };
2927 //
2928 if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);
2929 //
2930 // The calibration is good
2931 //
2932 if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
2933 //
2934 if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
2935 //
2936 // Handle good calib
2937 //
2938 this->HandleTRK_CALIB(true,true);
2939 //
2940 // Check for missing calibtrk1
2941 //
2942 if ( t2 != pret2+1 ){
2943 //
2944 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i pret2 is %i \n",obt2,pkt2,t2,pret2);
2945 //
2946 while ( t2 > pret2+1 ){
2947 //
2948 // handle missing calib1
2949 //
2950 pret2++;
2951 //
2952 obt1 = 0;
2953 pkt1 = 0;
2954 //
2955 tr2->GetEntry(pret2);
2956 ph2 = eh2->GetPscuHeader();
2957 obt2 = ph2->GetOrbitalTime();
2958 pkt2 = ph2->GetCounter();
2959 //
2960 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
2961 //
2962 valid = 0;
2963 this->HandleTRK_CALIB(false,true);
2964 //
2965 };
2966 //
2967 };
2968 //
2969 } else if ( this->PKT(pkt2) > this->PKT(pkt1)+1 ){
2970 //
2971 // Check for missing calibtrk2
2972 //
2973 if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
2974 t2 = pret2;
2975 //
2976 // handle missing calib2
2977 //
2978 obt2 = 0;
2979 pkt2 = 0;
2980 valid = 0;
2981 this->HandleTRK_CALIB(true,false);
2982 //
2983 };
2984 //
2985 } else {
2986 //
2987 if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
2988 //
2989 };
2990 //
2991 };
2992 //
2993 // we have one more calib pkt2 !
2994 //
2995 t2++;
2996 while ( t2 < nevents2 ){
2997 //
2998 // handle missing calib1
2999 //
3000 obt1 = 0;
3001 pkt1 = 0;
3002 //
3003 tr2->GetEntry(t2);
3004 ph2 = eh2->GetPscuHeader();
3005 obt2 = ph2->GetOrbitalTime();
3006 pkt2 = ph2->GetCounter();
3007 //
3008 fromtime = this->GetAbsTime(ph2->GetOrbitalTime());
3009 valid = 0;
3010 if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3011 //
3012 if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);
3013 //
3014 this->HandleTRK_CALIB(false,true);
3015 //
3016 };
3017 //
3018 t2++;
3019 //
3020 };
3021 //
3022 return(0);
3023 };
3024
3025
3026 /**
3027 * Scan S4 calibrations packets, fill the GL_S4_CALIB table
3028 */
3029 Int_t PamelaDBOperations::insertS4_CALIB(){
3030 //
3031 TSQLResult *result = 0;
3032 TSQLRow *row = 0;
3033 //
3034 stringstream oss;
3035 oss.str("");
3036 //
3037 TTree *tr = 0;
3038 EventHeader *eh = 0;
3039 PscuHeader *ph = 0;
3040 //
3041 UInt_t nevents = 0;
3042 UInt_t fromtime = 0;
3043 UInt_t totime = 0;
3044 UInt_t obt = 0;
3045 UInt_t pkt = 0;
3046 //
3047 tr = (TTree*)file->Get("CalibS4");
3048 if ( !tr || tr->IsZombie() ) throw -24;
3049 //
3050 tr->SetBranchAddress("Header", &eh);
3051 //
3052 nevents = tr->GetEntries();
3053 //
3054 if ( !nevents ) return(1);
3055 //
3056 for (UInt_t i = 0; i < nevents; i++){
3057 //
3058 tr->GetEntry(i);
3059 //
3060 ph = eh->GetPscuHeader();
3061 obt = ph->GetOrbitalTime();
3062 pkt = ph->GetCounter();
3063 fromtime = this->GetAbsTime(ph->GetOrbitalTime());
3064 if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3065 //
3066 if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3067 //
3068 // check if the calibration has already been inserted
3069 //
3070 oss.str("");
3071 oss << " SELECT ID FROM GL_S4_CALIB WHERE "
3072 << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3073 << " OBT = "<< obt << " AND "
3074 << " PKT = "<< pkt << ";";
3075 //
3076 if ( IsDebug() ) printf(" Check if the S4 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3077 result = conn->Query(oss.str().c_str());
3078 //
3079 if ( !result ) throw -4;
3080 //
3081 row = result->Next();
3082 //
3083 if ( row ){
3084 //
3085 if ( IsDebug() ) printf(" S4 calibration already inserted in the DB\n");
3086 //
3087 } else {
3088 //
3089 // we have to insert a new calibration, check where to place it
3090 //
3091 oss.str("");
3092 oss << " SELECT ID,TO_TIME FROM GL_S4_CALIB WHERE "
3093 << " FROM_TIME < "<< fromtime << " AND "
3094 << " TO_TIME > "<< fromtime << ";";
3095 //
3096 if ( IsDebug() ) printf(" Check where to place the S4 calibration: query is \n %s \n",oss.str().c_str());
3097 result = conn->Query(oss.str().c_str());
3098 //
3099 if ( !result ) throw -4;
3100 //
3101 row = result->Next();
3102 //
3103 if ( !row ){
3104 //
3105 // no calibrations in the db contain our calibration
3106 //
3107 if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3108 if ( fromtime < 1150863400 ){
3109 if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3110 fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode
3111 };
3112 //
3113 oss.str("");
3114 oss << " SELECT FROM_TIME FROM GL_S4_CALIB WHERE "
3115 << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3116 //
3117 if ( IsDebug() ) printf(" Check the upper limit for calibration: query is \n %s \n",oss.str().c_str());
3118 result = conn->Query(oss.str().c_str());
3119 //
3120 if ( !result ) throw -4;
3121 //
3122 row = result->Next();
3123 if ( !row ){
3124 totime = numeric_limits<UInt_t>::max();
3125 } else {
3126 totime = (UInt_t)atoll(row->GetField(0));
3127 };
3128 //
3129 } else {
3130 //
3131 // determine upper and lower limits and make space for the new calibration
3132 //
3133 totime = (UInt_t)atoll(row->GetField(1));
3134 //
3135 oss.str("");
3136 oss << " UPDATE GL_S4_CALIB SET "
3137 << " TO_TIME = "<< fromtime << " WHERE " // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3138 << " ID = "<< row->GetField(0) << ";";
3139 //
3140 if ( IsDebug() ) printf(" Make space for the new calibration: query is \n %s \n",oss.str().c_str());
3141 result = conn->Query(oss.str().c_str());
3142 //
3143 if ( !result ) throw -4;
3144 //
3145 };
3146 //
3147 oss.str("");
3148 oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3149 << " VALUES (NULL,' "
3150 << idroot << "','"
3151 << i << "','"
3152 << fromtime << "','"
3153 << totime << "','"
3154 << obt << "','"
3155 << pkt << "','"
3156 << this->GetBOOTnumber() << "');";
3157 //
3158 if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3159 //
3160 result = conn->Query(oss.str().c_str());
3161 //
3162 if ( !result ) throw -4;
3163 //
3164 };
3165 //
3166 } else {
3167 //
3168 if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);
3169 //
3170 };
3171 //
3172 };
3173 //
3174 return(0);
3175 };
3176
3177 /**
3178 * Scan the fragment table and move old fragments to the GL_RUN table
3179 */
3180 Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3181 //
3182 TSQLResult *result = 0;
3183 TSQLRow *row = 0;
3184 TSQLResult *result2 = 0;
3185 TSQLRow *row2 = 0;
3186 //
3187 UInt_t moved = 0;
3188 // UInt_t timelim = 0;
3189 // TDatime *time = new TDatime();
3190 //
3191 stringstream oss;
3192 oss.str("");
3193 //
3194 //
3195 // check if there are entries older than "olderthan" seconds from now
3196 //
3197 oss.str("");
3198 oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3199 << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3200 //
3201 if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3202 result = conn->Query(oss.str().c_str());
3203 //
3204 if ( result ){
3205 //
3206 row = result->Next();
3207 //
3208 if ( row ){
3209 //
3210 oss.str("");
3211 oss << " ID= "<< row->GetField(0);
3212 //
3213 glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);
3214 //
3215 oss.str("");
3216 oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3217 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3218 << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3219 << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3220 << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3221 << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3222 << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3223 << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3224 << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3225 << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3226 << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3227 << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3228 << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3229 << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3230 //
3231 if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3232 result2 = conn->Query(oss.str().c_str());
3233 //
3234 if ( !result2 ) throw -4;
3235 //
3236 row2 = result2->Next();
3237 //
3238 if ( !row2 ){
3239 //
3240 if ( IsDebug() ) printf(" The run is new \n");
3241 if ( IsDebug() ) printf(" -> fill the DB \n");
3242 //
3243 glrun->SetID(0);
3244 glrun->Fill_GL_RUN(conn);
3245 //
3246 oss.str("");
3247 oss << " SELECT ID FROM GL_RUN WHERE "
3248 << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3249 << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3250 << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3251 << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3252 << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3253 //
3254 if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3255 result2 = conn->Query(oss.str().c_str());
3256 //
3257 if ( !result2 ) throw -4;
3258 //
3259 row2 = result2->Next();
3260 //
3261 if ( !row2 ) throw -25;
3262 //
3263 oss.str("");
3264 oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3265 if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3266 result2 = conn->Query(oss.str().c_str());
3267 //
3268 if ( !result2 ) throw -4;
3269 //
3270 moved++;
3271 //
3272 } else {
3273 if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3274 };
3275 if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));
3276 //
3277 //
3278 oss.str("");
3279 oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3280 if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3281 result2 = conn->Query(oss.str().c_str());
3282 //
3283 if ( !result2 ) throw -4;
3284 //
3285 };
3286 };
3287 if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3288 return(0);
3289 };
3290
3291 /**
3292 * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3293 */
3294 Int_t PamelaDBOperations::ValidateRuns(){
3295 //
3296 TSQLResult *result = 0;
3297 TSQLRow *row = 0;
3298 //
3299 stringstream oss;
3300 oss.str("");
3301 //
3302 // =======================================================
3303 // validate runs by checking missing calibrations
3304 // =======================================================
3305 UInt_t t_stop = 0;
3306 UInt_t t_start = 0;
3307 // --------------------------------------------------------------
3308 // 1) get the OBT of the last run inserted after clean-time limit
3309 // --------------------------------------------------------------
3310 oss.str("");
3311 oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3312 << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3313 if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3314 result = conn->Query(oss.str().c_str());
3315 if ( !result ) throw -4;
3316 if ( !result->GetRowCount() ) {
3317 printf(" No runs to validate \n");
3318 return(1);
3319 }else{
3320 row = result->Next();
3321 t_start = (UInt_t)atoll(row->GetField(4));
3322 };
3323 // --------------------------------------------------------------
3324 // 2) get the OBT of the last validated run
3325 // --------------------------------------------------------------
3326 oss.str("");
3327 oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3328 <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3329 if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3330 result = conn->Query(oss.str().c_str());
3331 if ( !result ) throw -4;
3332 if ( result->GetRowCount() ){
3333 row = result->Next();
3334 t_stop = (UInt_t)atoll(row->GetField(4));
3335 };
3336 if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3337 // --------------------------------------------------------------
3338 // now retrieves runs to be validated
3339 // --------------------------------------------------------------
3340 oss.str("");
3341 oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start;
3342 oss << " AND RUNHEADER_TIME >="<< t_stop;
3343 oss << " ORDER BY RUNHEADER_TIME DESC;";
3344 // if ( IsDebug() )
3345 if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3346 result = conn->Query(oss.str().c_str());
3347 if ( !result ) throw -4;
3348 if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3349 // printf("------------------------------------------------------------------------------- \n");
3350
3351 Int_t nrow = 0;
3352 GL_RUN* this_run = new GL_RUN();
3353 GL_RUN* next_run = new GL_RUN();
3354 Int_t nseq_max = 1000;
3355 // UInt_t* sequence = new UInt_t[100];
3356 vector<UInt_t> sequence(nseq_max);
3357 Int_t nseq = 0;
3358 Bool_t CHECK = false;
3359 Bool_t this_ONLINE = false;
3360 Bool_t next_ONLINE = false;
3361 UInt_t t1=0,t2=0;
3362 // ---------------------------------------------------------------------------------
3363 // - loop over runs, back in time,
3364 // - select sequences of runs close in time (less than 60 s apart),
3365 // which could be preceeded by a calibration
3366 // - check if there might be a missing calibration
3367 // ---------------------------------------------------------------------------------
3368 while(1){
3369
3370 row = result->Next();
3371 if( row == NULL ) break;
3372
3373 //------------
3374 //get run info
3375 //------------
3376 this_run->Set_GL_RUN(row);
3377
3378 Bool_t this_BAD = false;
3379 if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3380 else if (this_run->GetTRK_CALIB_USED() == 104) this_ONLINE = false;
3381 else{
3382 // printf("Missing or corrupted header!! \n");
3383 this_ONLINE = false;
3384 this_BAD = true;
3385 };
3386
3387 //-----------------------------------
3388 //compare with previous(next in time)
3389 //-----------------------------------
3390 CHECK = false;
3391 UInt_t interval=0;
3392
3393 if( nrow != 0){
3394
3395
3396 t1 = this_run->GetRUNTRAILER_TIME();
3397 t2 = next_run->GetRUNHEADER_TIME();
3398 interval = (t2-t1);
3399
3400 if(this_ONLINE && next_ONLINE){ // this: ON-LINE + next: ON-LINE
3401
3402 if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments
3403
3404 if( interval >= 60 )CHECK = true; //more than 60 s => there might be a calibration
3405
3406 if( !CHECK && this_run->VALIDATION ){
3407 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3408 nseq=0;
3409 }
3410
3411 }else if( !this_ONLINE && next_ONLINE) { // this: DEFAULT + next:ON-LINE
3412
3413 CHECK = true;
3414
3415 }else if( !next_ONLINE ){ // this:ANY + next:DEFAULT
3416
3417 assignVALIDATION(next_run->ID,true);
3418 nseq=0;
3419 }
3420 }
3421
3422 //----------------------------
3423 //check run sequence for calib
3424 //----------------------------
3425 if( CHECK ){
3426 // check if calibration exists
3427 if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3428 Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3429 for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3430 nseq=0;
3431 };
3432 //--------------
3433 //store run info
3434 //--------------
3435 *next_run = *this_run;
3436 next_ONLINE = this_ONLINE;
3437 if( !this_BAD ){
3438 if(nseq < nseq_max){
3439 sequence[nseq] = this_run->ID;
3440 nseq++;
3441 }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3442 };
3443
3444 if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3445 nrow++;
3446
3447 };
3448 delete this_run;
3449 delete next_run;
3450 //
3451 return(0);
3452 };
3453 /**
3454 * Check if there might be a missing tracker calibration in a given time interval
3455 * @param t1 From absolute time
3456 * @param t2 To absolute time
3457 * @return true if there might be a missing calibration
3458 */
3459 Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3460
3461 GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3462
3463 // get the closest VALIDATED calibration before the run start (t2)
3464 if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true); //>>> missing
3465
3466 if ( trkcalib->TO_TIME < t2 ) return(true); //>>> missing
3467
3468 //==============================================================
3469 // Check is done first on the basis of time between calibration,
3470 // which should be equal to the time between ascending-nodes.
3471 //==============================================================
3472 if ( t2 - trkcalib->FROM_TIME > 5700) {
3473 if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3474 //==============================================================
3475 // there might be a missing calibration, due to:
3476 // - MM full
3477 // - corrupted packets
3478 // - loss of data
3479 // There is an exception in case a download was done during ascending node
3480 //==============================================================
3481 Bool_t DOWNLOAD = false;
3482 // check if the calib was skipped becouse of download .... DA FARE!!
3483 if(DOWNLOAD)return(false);
3484
3485 return(true); //>>> missing
3486
3487 };
3488
3489 //==============================================================
3490 // If the last calibration is close to the run less than this time,
3491 // it is enough to say that there are no missing calibrations
3492 //==============================================================
3493 // the long time interval bewteen runs might be due to download
3494 if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3495 return(false);
3496
3497 };
3498 /**
3499 * Assign VALIDATION value to a GL_RUN entry
3500 * @param idrun Run ID
3501 * @param validation true/false
3502 */
3503 Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3504 TSQLResult *result = 0;
3505 stringstream oss;
3506 oss.str("");
3507 oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3508 //
3509 // if ( IsDebug() )
3510 // printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3511 if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3512 result = conn->Query(oss.str().c_str());
3513 if ( !result ) throw -4;
3514 return(0);
3515 }
3516
3517
3518

  ViewVC Help
Powered by ViewVC 1.1.23