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