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