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