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

Contents of /YodaProfiler/src/PamelaDBOperations.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Fri Sep 8 12:44:27 2006 UTC (18 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.3: +149 -12 lines
Adeed cross check on the sequence of RH and RT

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

  ViewVC Help
Powered by ViewVC 1.1.23