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