--- YodaProfiler/src/PamelaDBOperations.cpp 2006/10/24 14:24:23 1.15 +++ YodaProfiler/src/PamelaDBOperations.cpp 2007/04/30 10:14:42 1.37 @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include // #include @@ -56,8 +56,10 @@ * @param debug debug flag. * @param tlefilename ascii file with TLE 3 line elements. */ -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){ +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, UInt_t dwinput, Bool_t staticp){ // + STATIC=false; + if ( staticp ) STATIC=true; // SetConnection(host,user,password); // @@ -79,8 +81,10 @@ INSERT_ROOT = !filerootname.IsNull(); if( INSERT_ROOT ){ this->SetRootName(filerootname); - this->SetOrbitNo(); + this->SetOrbitNo(dwinput); file = TFile::Open(this->GetRootName().Data()); + } else { + this->SetRootName(""); }; // this->SetID_RAW(0); @@ -110,10 +114,11 @@ // void PamelaDBOperations::CheckValidate(Long64_t olderthan){ clean_time = new TDatime(); + // if(olderthan >= 0){ VALIDATE = true; UInt_t timelim = 0; - timelim = (UInt_t)clean_time->Convert() - olderthan; + timelim = (UInt_t)clean_time->Convert(true) - olderthan; clean_time->Set(timelim,false); }; }; @@ -154,6 +159,22 @@ }; /** + * Set the autoboot flag + * + */ +void PamelaDBOperations::SetAutoBoot(Bool_t dbg){ + AUTOBOOT = dbg; +}; + +/** + * Set the nofrag flag + * + */ +void PamelaDBOperations::SetNoFrag(Bool_t nf){ + NOFRAG = nf; +}; + +/** * Store the BOOT number of the RAW file. * @param boot BOOT number of the RAW file */ @@ -196,8 +217,15 @@ /** * Store the downlink orbit number from filename. */ -void PamelaDBOperations::SetOrbitNo(){ +void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){ dworbit = 0; + // + if ( dwinput ){ + dworbit = dwinput; + if ( IsDebug() ) printf(" Downlink orbit given by hand: %i \n",dworbit); + return; + }; + // TString name = this->GetRootFile(); Int_t nlength = name.Length(); if ( nlength < 5 ) return; @@ -235,6 +263,22 @@ tlefilename = str; }; +TString PamelaDBOperations::GetRawPath(){ + if ( STATIC ){ + return((TString)gSystem->DirName(filerawname.Data())+'/'); + } else { + return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/'); + }; +}; + +TString PamelaDBOperations::GetRootPath(){ + if ( STATIC ){ + return((TString)gSystem->DirName(filerootname.Data())+'/'); + } else { + return((TString)gSystem->ExpandPathName("$PAM_L0")+'/'); + }; +}; + /** * Store the olderthan variable * @param olderthan @@ -251,9 +295,15 @@ TSQLResult *result = 0; TSQLRow *row = 0; oss.str(""); - oss << "SELECT ID FROM GL_RAW WHERE " - << " PATH = '" << this->GetRawPath().Data() << "' AND " - << " NAME = '" << this->GetRawFile().Data() << "' "; + if ( STATIC ){ + oss << "SELECT ID FROM GL_RAW WHERE " + << " PATH = '" << this->GetRawPath().Data() << "' AND " + << " NAME = '" << this->GetRawFile().Data() << "' "; + } else { + oss << "SELECT ID FROM GL_RAW WHERE " + << " PATH = '$PAM_RAW' AND " + << " NAME = '" << this->GetRawFile().Data() << "' "; + } result = conn->Query(oss.str().c_str()); if ( result == NULL ) throw -4; row = result->Next(); @@ -284,23 +334,26 @@ UInt_t nevent = 0; UInt_t pktlast = 0; UInt_t obtlast = 0; - UInt_t t_pktlast = 0; - UInt_t t_obtlast = 0; - UInt_t upperpkt2 = 0; - ULong64_t upperobt2 = 0; + Long64_t t_pktlast = 0LL; + // UInt_t t_obtlast = 0; + Long64_t t_obtlast = 0LL; + Long64_t upperpkt2 = 0LL; + Long64_t upperobt2 = 0LL; UInt_t zomp = 0; UInt_t jump = 50000; // was 5000 EventCounter *code=0; // - UInt_t deltapkt = 5000; - ULong64_t deltaobt = 50000; + Long64_t deltapkt = 5000LL; + Long64_t deltaobt = 50000LL; // - // pcksList packetsNames; - // pcksList::iterator Iter; - // getPacketsNames(packetsNames); + pcksList packetsNames; + pcksList::iterator Iter; + getPacketsNames(packetsNames); // pktfirst = 0; obtfirst = 0; + ppktfirst = 0; + pobtfirst = 0; // TTree *T = 0; T = (TTree*)file->Get("Physics"); @@ -314,13 +367,29 @@ ph = eh->GetPscuHeader(); pktfirst = ph->GetCounter(); obtfirst = ph->GetOrbitalTime(); + ppktfirst = pktfirst; + pobtfirst = obtfirst; // - // code = eh->GetCounter(); - // UInt_t en = 0; - // for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ - // en = code->Get(GetPacketType(*Iter)); - // if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en); - //}; + code = eh->GetCounter(); + UInt_t en = 0; + for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ + en = code->Get(GetPacketType(*Iter)); + if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){ + if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en); + // + TTree *TC = 0; + TC = (TTree*)file->Get("CalibCalPed"); + if ( !TC || TC->IsZombie() ) throw -16; + EventHeader *ehc = 0; + PscuHeader *phc = 0; + TC->SetBranchAddress("Header", &ehc); + TC->GetEntry(0); + phc = ehc->GetPscuHeader(); + pktfirst = phc->GetCounter(); + obtfirst = phc->GetOrbitalTime(); + // + }; + }; // T->GetEntry(nevent-1); ph = eh->GetPscuHeader(); @@ -331,18 +400,22 @@ upperobt = OBT(obtlast); upperentry = nevent-1; // - if ( IsDebug() ) printf(" First entries are: OBT %llu pkt_num %i \n",obtfirst,pktfirst); + if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u \n",obtfirst,pktfirst); // - if ( IsDebug() ) printf(" Last entries are: OBT %llu pkt_num %i entry %i\n",upperobt,upperpkt,upperentry); + if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry); // - if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1); + if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(32); // - if ( !nevent ) return(2); + if ( !nevent ) return(64); // - if ( nevent < 2 ) return(4); + if ( nevent < 2 ) return(128); + if ( nevent < jump ) jump = 1; + // if ( nevent < jump ) jump = int(nevent/10); + // if ( !jump ) jump = 1; // - if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst)) deltapkt ){ // + if ( IsDebug() ) printf(" starting jump %i \n",jump); // go back zomp = nevent - 2; // @@ -364,12 +437,17 @@ upperpkt2 = PKT(ph->GetCounter()); upperobt2 = OBT(ph->GetOrbitalTime()); // - if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ) throw -13; + if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){ + if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2); + if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast); + if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i); + throw -13; + }; // if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){ zomp = i + jump + 1; if ( zomp > nevent-2 ) zomp = nevent - 2; - 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); + if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i); break; }; // @@ -404,222 +482,226 @@ // rhev = rh->GetEntries(); rtev = rt->GetEntries(); - UInt_t sobtt = 0; - UInt_t sobth = 0; - UInt_t spktt = 0; - UInt_t spkth = 0; - UInt_t pktt = 0; - ULong64_t obtt = 0; - UInt_t pkth = 0; - ULong64_t obth = 0; - // - T->GetEntry(upperentry); - code = eh->GetCounter(); - Int_t lasttrail = code->Get(pctp->RunTrailer); - Int_t lasthead = code->Get(pctp->RunHeader); - if ( lasttrail < rtev ){ - rt->GetEntry(lasttrail); - pht = eht->GetPscuHeader(); - pktt = PKT(pht->GetCounter()); - obtt = OBT(pht->GetOrbitalTime()); - }; - // - if ( lasthead < rhev ){ - rh->GetEntry(lasthead); - phh = ehh->GetPscuHeader(); - pkth = PKT(phh->GetCounter()); - obth = OBT(phh->GetOrbitalTime()); - }; - // - if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); - if ( pkth > upperpkt && obth > upperobt ){ - if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt); - upperpkt = pkth; - upperobt = obth; - rhev = lasthead+1; - } else { - rhev = lasthead; - }; - if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); - // - if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); - if ( pktt > upperpkt && obtt > upperobt ){ - if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt); - upperpkt = pktt; - upperobt = obtt; - rtev = lasttrail+1; - } else { - rtev = lasttrail; - }; - if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); - // goto kikko; - // - // - // Check if runtrailer/runheader are within lower limits - // + Long64_t sobtt = 0LL; + Long64_t sobth = 0LL; + Long64_t spktt = 0LL; + Long64_t spkth = 0LL; + Long64_t pktt = 0LL; + Long64_t obtt = 0LL; + Long64_t pkth = 0LL; + Long64_t obth = 0LL; // - pkth = 0; - obth = 0; - spkth = 0; - sobth = 0; - for (Int_t k=0; k 0 ){ - spkth = pkth; - sobth = obth; - }; - rh->GetEntry(k); - phh = ehh->GetPscuHeader(); - pkth = PKT(phh->GetCounter()); - obth = OBT(phh->GetOrbitalTime()); - // -// if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth); - // - if ( pkth < spkth && obth < sobth ){ - if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev); - // - rhev = k-1; - rh->GetEntry(rhev); - pkth = spkth; - obth = sobth; - // - UInt_t evbefh = 0; - code = ehh->GetCounter(); - evbefh = code->Get(pctp->Physics); - if ( evbefh >= 0 ){ - T->GetEntry(evbefh); - ph = eh->GetPscuHeader(); - t_pktlast = PKT(ph->GetCounter()); - t_obtlast = OBT(ph->GetOrbitalTime()); - if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump - upperpkt = pkth; - upperobt = obth; - upperentry = evbefh-1; - } else { - while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){ - evbefh++; - T->GetEntry(evbefh); - ph = eh->GetPscuHeader(); - t_pktlast = PKT(ph->GetCounter()); - t_obtlast = OBT(ph->GetOrbitalTime()); - }; - T->GetEntry(evbefh-1); + if ( rhev || rtev ){ + + T->GetEntry(upperentry); + code = eh->GetCounter(); + Int_t lasttrail = code->Get(pctp->RunTrailer); + Int_t lasthead = code->Get(pctp->RunHeader); + if ( lasttrail < rtev ){ + rt->GetEntry(lasttrail); + pht = eht->GetPscuHeader(); + pktt = PKT(pht->GetCounter()); + obtt = OBT(pht->GetOrbitalTime()); + }; + // + if ( lasthead < rhev ){ + rh->GetEntry(lasthead); + phh = ehh->GetPscuHeader(); + pkth = PKT(phh->GetCounter()); + obth = OBT(phh->GetOrbitalTime()); + }; + // + if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); + if ( pkth > upperpkt && obth > upperobt ){ + if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt); + upperpkt = pkth; + upperobt = obth; + rhev = lasthead+1; + } else { + rhev = lasthead; + }; + if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); + // + if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + if ( pktt > upperpkt && obtt > upperobt ){ + if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt); + upperpkt = pktt; + upperobt = obtt; + rtev = lasttrail+1; + } else { + rtev = lasttrail; + }; + if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + // goto kikko; + // + // + // Check if runtrailer/runheader are within lower limits + // + // + pkth = 0LL; + obth = 0LL; + spkth = 0LL; + sobth = 0LL; + for (Int_t k=0; k 0 ){ + spkth = pkth; + sobth = obth; + }; + rh->GetEntry(k); + phh = ehh->GetPscuHeader(); + pkth = PKT(phh->GetCounter()); + obth = OBT(phh->GetOrbitalTime()); + // + // if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth); + // + if ( pkth < spkth && obth < sobth ){ + if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i \n",rhev); + // + rhev = k-1; + rh->GetEntry(rhev); + pkth = spkth; + obth = sobth; + // + UInt_t evbefh = 0; + code = ehh->GetCounter(); + evbefh = code->Get(pctp->Physics); + if ( evbefh >= 0 ){ + T->GetEntry(evbefh); ph = eh->GetPscuHeader(); - upperpkt = PKT(ph->GetCounter()); - upperobt = OBT(ph->GetOrbitalTime()); - upperentry = evbefh-1; - }; + t_pktlast = PKT(ph->GetCounter()); + t_obtlast = OBT(ph->GetOrbitalTime()); + if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump + upperpkt = pkth; + upperobt = obth; + upperentry = evbefh-1; + } else { + while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){ + evbefh++; + T->GetEntry(evbefh); + ph = eh->GetPscuHeader(); + t_pktlast = PKT(ph->GetCounter()); + t_obtlast = OBT(ph->GetOrbitalTime()); + }; + T->GetEntry(evbefh-1); + ph = eh->GetPscuHeader(); + upperpkt = PKT(ph->GetCounter()); + upperobt = OBT(ph->GetOrbitalTime()); + upperentry = evbefh-1; + }; + }; + if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); + goto kikko0; }; - if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); - goto kikko0; - }; - }; - kikko0: - // - // - // - pktt = 0; - obtt = 0; - spktt = 0; - sobtt = 0; - for (Int_t k=0; k 0 ){ - spktt = pktt; - sobtt = obtt; }; - rt->GetEntry(k); - pht = eht->GetPscuHeader(); - pktt = PKT(pht->GetCounter()); - obtt = OBT(pht->GetOrbitalTime()); + kikko0: // -// if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt); // - if ( pktt < spktt && obtt < sobtt ){ - if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev); + // + pktt = 0LL; + obtt = 0LL; + spktt = 0LL; + sobtt = 0LL; + for (Int_t k=0; k 0 ){ + spktt = pktt; + sobtt = obtt; + }; + rt->GetEntry(k); + pht = eht->GetPscuHeader(); + pktt = PKT(pht->GetCounter()); + obtt = OBT(pht->GetOrbitalTime()); // - rtev = k-1; - rt->GetEntry(rtev); - pktt = spktt; - obtt = sobtt; - if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); + // if ( IsDebug() ) printf(" k %i rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt); // - UInt_t evbeft = 0; - code = eht->GetCounter(); - evbeft = code->Get(pctp->Physics); - if ( evbeft >= 0 ){ - T->GetEntry(evbeft); - ph = eh->GetPscuHeader(); - t_pktlast = PKT(ph->GetCounter()); - t_obtlast = OBT(ph->GetOrbitalTime()); - if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump - upperpkt = pktt; - upperobt = obtt; - upperentry = evbeft-1; - } else { - while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){ - evbeft++; - T->GetEntry(evbeft); + if ( pktt < spktt && obtt < sobtt ){ + if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev); + // + rtev = k-1; + rt->GetEntry(rtev); + pktt = spktt; + obtt = sobtt; + if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + // + UInt_t evbeft = 0; + code = eht->GetCounter(); + evbeft = code->Get(pctp->Physics); + if ( evbeft >= 0 ){ + T->GetEntry(evbeft); + ph = eh->GetPscuHeader(); + t_pktlast = PKT(ph->GetCounter()); + t_obtlast = OBT(ph->GetOrbitalTime()); + if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump + upperpkt = pktt; + upperobt = obtt; + upperentry = evbeft-1; + } else { + while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){ + evbeft++; + T->GetEntry(evbeft); + ph = eh->GetPscuHeader(); + t_pktlast = PKT(ph->GetCounter()); + t_obtlast = OBT(ph->GetOrbitalTime()); + }; + T->GetEntry(evbeft-1); ph = eh->GetPscuHeader(); - t_pktlast = PKT(ph->GetCounter()); - t_obtlast = OBT(ph->GetOrbitalTime()); + upperpkt = PKT(ph->GetCounter()); + upperobt = OBT(ph->GetOrbitalTime()); + upperentry = evbeft-1; }; - T->GetEntry(evbeft-1); - ph = eh->GetPscuHeader(); - upperpkt = PKT(ph->GetCounter()); - upperobt = OBT(ph->GetOrbitalTime()); - upperentry = evbeft-1; }; + if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + goto kikko; + // break; + // }; - if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); - goto kikko; - // break; - // + // }; - // - }; - // - kikko: - // - T->GetEntry(upperentry); - code = eh->GetCounter(); - lasttrail = code->Get(pctp->RunTrailer); - lasthead = code->Get(pctp->RunHeader); - if ( lasttrail < rtev ){ - rt->GetEntry(lasttrail); - pht = eht->GetPscuHeader(); - pktt = PKT(pht->GetCounter()); - obtt = OBT(pht->GetOrbitalTime()); - }; - // - if ( lasthead < rhev ){ - rh->GetEntry(lasthead); - phh = ehh->GetPscuHeader(); - pkth = PKT(phh->GetCounter()); - obth = OBT(phh->GetOrbitalTime()); - }; - // - if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); - if ( pkth > upperpkt && obth > upperobt ){ - if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt); - upperpkt = pkth; - upperobt = obth; - rhev = lasthead+1; - } else { - rhev = lasthead; - }; - if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt); - // - if ( IsDebug() ) printf(" rtev beforev %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); - if ( pktt > upperpkt && obtt > upperobt ){ - if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt); - upperpkt = pktt; - upperobt = obtt; - rtev = lasttrail+1; - } else { - rtev = lasttrail; + // + kikko: + // + T->GetEntry(upperentry); + code = eh->GetCounter(); + lasttrail = code->Get(pctp->RunTrailer); + lasthead = code->Get(pctp->RunHeader); + if ( lasttrail < rtev ){ + rt->GetEntry(lasttrail); + pht = eht->GetPscuHeader(); + pktt = PKT(pht->GetCounter()); + obtt = OBT(pht->GetOrbitalTime()); + }; + // + if ( lasthead < rhev ){ + rh->GetEntry(lasthead); + phh = ehh->GetPscuHeader(); + pkth = PKT(phh->GetCounter()); + obth = OBT(phh->GetOrbitalTime()); + }; + // + if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); + if ( pkth > upperpkt && obth > upperobt ){ + if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt); + upperpkt = pkth; + upperobt = obth; + rhev = lasthead+1; + } else { + rhev = lasthead; + }; + if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt); + // + if ( IsDebug() ) printf(" rtev beforev %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + if ( pktt > upperpkt && obtt > upperobt ){ + if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt); + upperpkt = pktt; + upperobt = obtt; + rtev = lasttrail+1; + } else { + rtev = lasttrail; + }; + if ( IsDebug() ) printf(" rtev after %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt); + // }; - if ( IsDebug() ) printf(" rtev after %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt); // - if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry); + if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry); // return(0); } @@ -753,46 +835,68 @@ if( !conn ) throw -1; bool connect = conn->IsConnected(); if( !connect ) throw -1; - if ( !dworbit ) throw -27; + // + if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort()); + // + if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27; + // + // set DB timezone to UTC + // + stringstream oss; + // + oss.str(""); + oss << "SET time_zone='+0:00';"; + TSQLResult *result = 0; + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -10; + oss.str(""); + oss << "SET wait_timeout=173000;"; + conn->Query(oss.str().c_str()); + // }; /** * Return the correct packet number if we went back to zero */ -UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){ +Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){ // - // 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))); + if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (UInt_t)(16777214/2)) is %u \n",pkt_num,ppktfirst,(UInt_t)(16777214/2)); // - if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215)); + if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2) ){ + if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL); + return((Long64_t)pkt_num+16777215LL); + }; // - if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ - if ( (pkt_num-16777215) < 0 ){ - return((16777215-pkt_num)); - } else { - return((pkt_num-16777215)); - }; + if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){ + if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL); + return((Long64_t)pkt_num-16777215LL); }; // - return(pkt_num); + if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num); + return((Long64_t)pkt_num); // }; /** * Return the correct On Board Time if we went back to zero */ -ULong64_t PamelaDBOperations::OBT(UInt_t obt){ +Long64_t PamelaDBOperations::OBT(UInt_t obt){ // - if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((ULong64_t)(obt+numeric_limits::max())); + if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u (numeric_limits::max()/2) is %u \n",obt,pobtfirst,(UInt_t)(numeric_limits::max()/2)); // - if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ - if ( (obt-numeric_limits::max()) < 0 ){ - return((ULong64_t)(numeric_limits::max()-obt)); - } else { - return((ULong64_t)(obt-numeric_limits::max())); - }; + if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits::max()/2) ){ + if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits::max())); + return((Long64_t)(obt+numeric_limits::max())); + }; + // + if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits::max()/2) ){ + if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2)); + if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits::max()); + return((Long64_t)obt-(Long64_t)numeric_limits::max()); }; // - return((ULong64_t)obt); + if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt); + return((Long64_t)obt); }; /** @@ -907,8 +1011,12 @@ if ( idr ) return(1); // oss.str(""); - oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" - << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')"; + if ( STATIC ){ + oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('" + << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')"; + } else { + oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')"; + }; if ( conn->Query(oss.str().c_str()) == 0 ) throw -4; // idr = this->SetID_RAW(); @@ -927,13 +1035,16 @@ TSQLResult *result = 0; TSQLRow *row = 0; UInt_t t0 = 0; + Int_t signal = 0; + // + signal = this->SetUpperLimits(); // stringstream oss; // if ( this->GetID_RAW() == 0 ) throw -11; // oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" << this->GetRawFile().Data() << "';"; if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); @@ -942,7 +1053,7 @@ // if ( !row ){ oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " << dworbit << " order by FROM_ORBIT desc limit 1;"; if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); result = conn->Query(oss.str().c_str()); @@ -951,7 +1062,10 @@ if ( !row ) throw -10; }; // - t0 = (UInt_t)TDatime(row->GetField(0)).Convert(); + TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0); + t0 = (UInt_t)tu.GetSec(); + if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5)); + // /* * Verify that the TIMESYNC have been not already processed */ @@ -966,12 +1080,16 @@ if (result == NULL) throw -10; row = result->Next(); if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){ + if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0); toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0; + // + tsync = (UInt_t)atoll(row->GetField(2)); + obt0 = (UInt_t)atoll(row->GetField(1)); + // return(1); }; // TTree *T = 0; - Int_t signal = 0; // UInt_t nevent = 0; UInt_t recEntries = 0; @@ -1160,8 +1278,12 @@ conn->Query(oss.str().c_str()); if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str()); // + if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0); toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0; // + tsync = TSYNC; + obt0 = OBT; + // delete result; return(signal); } @@ -1177,11 +1299,19 @@ UInt_t idtimesync = 0; // oss.str(""); - oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " - << " LEFT JOIN GL_ROOT " - << " ON GL_RAW.ID = GL_ROOT.ID_RAW " - << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " - << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + if ( STATIC ){ + oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " + << " LEFT JOIN GL_ROOT " + << " ON GL_RAW.ID = GL_ROOT.ID_RAW " + << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND " + << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + } else { + oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW " + << " LEFT JOIN GL_ROOT " + << " ON GL_RAW.ID = GL_ROOT.ID_RAW " + << " WHERE GL_RAW.PATH = '$PAM_RAW' AND " + << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID "; + }; result = conn->Query(oss.str().c_str()); // if ( !result ) throw -12; @@ -1208,8 +1338,13 @@ idtimesync = (UInt_t)atoll(row->GetField(0)); // oss.str(""); - oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" - << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')"; + if ( STATIC ){ + oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" + << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')"; + } else { + oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('" + << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')"; + }; // if (conn->Query(oss.str().c_str()) == 0) throw -4; // @@ -1236,9 +1371,15 @@ TSQLResult *result = 0; TSQLRow *row = 0; oss.str(""); - oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE " - << " PATH = '" << this->GetRawPath().Data() << "' AND " - << " NAME = '" << this->GetRawFile().Data() << "' "; + if ( STATIC ){ + oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE " + << " PATH = '" << this->GetRawPath().Data() << "' AND " + << " NAME = '" << this->GetRawFile().Data() << "' "; + } else { + oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE " + << " PATH = '$PAM_RAW' AND " + << " NAME = '" << this->GetRawFile().Data() << "' "; + }; result = conn->Query(oss.str().c_str()); // if ( !result ) throw -4;; @@ -1261,13 +1402,14 @@ VarDumpEvent *vde = 0; VarDumpRecord *vdr = 0; // + Bool_t found = false; trDumpEv->SetBranchAddress("VarDump", &vde); if ( trDumpEv->GetEntries() > 0 ){ - Bool_t found = false; + found = false; for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){ trDumpEv->GetEntry(i); - vde->Records->GetEntries(); - if ( vde->Records->GetEntries()>0 ){ + // vde->Records->GetEntries(); + if ( vde->Records->GetEntries()>5 ){ found = true; goto fill; }; @@ -1280,10 +1422,60 @@ this->SetBOOTnumber((Int_t)vdr->VAR_VALUE); // } else { - if ( !this->GetBOOTnumber() ) return(4); + if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4); + }; + } else { + if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2); + }; + // + UInt_t bn = 0; + Bool_t afound = false; + if ( !found && this->AutoBoot()){ + afound = true; + // + // Search for other files with similar timesync + // + if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0); + UInt_t upperts = tsync-(obt0/1000)+5; + UInt_t lowerts = tsync-(obt0/1000)-5; + oss.str(""); + oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<" + << upperts + << " AND TIMESYNC-(OBT0/1000)>" + << lowerts + << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;"; + result = conn->Query(oss.str().c_str()); + if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str()); + // + if ( !result ) throw -4;; + found = true; + if ( result->GetRowCount()<3 ){ + if ( IsDebug() ) printf(" AGH! no results!\n"); + found = false; + } else { + row = result->Next(); + bn = (UInt_t)atoll(row->GetField(0)); + for ( Int_t r=1; rGetRowCount() ;r++){ + if ( !row ) throw -4; + if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0)); + if ( bn != (UInt_t)atoll(row->GetField(0)) ){ + if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0))); + found = false; + }; + row = result->Next(); + }; }; + }; + // + Int_t sgn = 0; + // + if ( !found && !BOOTNO ){ + throw -29; } else { - if ( !this->GetBOOTnumber() ) return(2); + if ( afound ){ + this->SetBOOTnumber(bn); + sgn = 8; + }; }; // oss.str(""); @@ -1293,7 +1485,7 @@ conn->Query(oss.str().c_str()); // delete result; - return(0); + return(sgn); }; /** @@ -1306,7 +1498,7 @@ stringstream oss; oss.str(""); // - signal = this->SetUpperLimits(); + // signal = this->SetUpperLimits(); // // loop on runheader and runtrailer events // @@ -1357,14 +1549,14 @@ // if ( !ptt && !(ptht+1) ){ // - if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" Piece of run at the beginning of the file %u %u %u \n",ptht,pth,ptt); // this->HandleRunFragments(true,false,0,(evbeft-1)); // // } else if ( pth == ptht ){ // - if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt); + if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt); // if ( (ptt-1) < 0 ) throw -15; // should never arrive here! rt->GetEntry(ptt-1); @@ -1373,14 +1565,14 @@ rt->GetEntry(ptt); pht = eht->GetPscuHeader(); // - if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %i %i %i \n",ptht,pth,ptt); - if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt); + if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer %u %u %u \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt); // this->HandleMissingHoT(true,false,evbefh,evbeft-1); // } else { // - if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt); + if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %u %u %u\n",ptht,pth,ptt); // rh->GetEntry(ptht); phh = ehh->GetPscuHeader(); @@ -1388,7 +1580,7 @@ obth = phh->GetOrbitalTime(); cod = ehh->GetCounter(); evbefh = cod->Get(pctp->Physics); - if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt); + if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt); // // handle this run // @@ -1400,15 +1592,23 @@ // if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n"); // - this->HandleRunFragments(true,true,0,(evbefh-1)); - // + if ( evbefh == 1 ){ + // + signal = 8; + if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n"); + // + } else { + // + this->HandleRunFragments(true,true,0,(evbefh-1)); + // + }; }; // // if ( (ptht - pth) > 1 ){ // if ( IsDebug() ) printf(" Missing runtrailers! \n"); - if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt); // is not the consecutive header while ( pth != ptht ){ // @@ -1429,8 +1629,8 @@ obth = phh->GetOrbitalTime(); evbefh = cod->Get(pctp->Physics); // - if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %i %i %i \n",ptht,pth,ptt); - if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt); + if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader %u %u %u \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt); // this->HandleMissingHoT(false,true,evbefh,evbeft-1); // @@ -1439,7 +1639,7 @@ } else if ( !(ptht - pth) ){ // if ( IsDebug() ) printf(" Missing runheader! \n"); - if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt); if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n"); // } else { @@ -1460,9 +1660,9 @@ obth = phh->GetOrbitalTime(); cod = ehh->GetCounter(); evbefh = cod->Get(pctp->Physics); - if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt); - if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt); - if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev); + if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt); + if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt); + if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev); // this->HandleRunFragments(false,true,evbefh,upperentry); } else { @@ -1611,6 +1811,7 @@ delete result; // if ( signal && IsDebug() ) printf(" The run has already been inserted \n"); + if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n"); return(signal); }; @@ -1659,6 +1860,7 @@ UInt_t rhfirstev = firstev; UInt_t rtlastev = lastev; Bool_t found = false; + Bool_t foundinrun = false; // TSQLResult *result = 0; TSQLRow *row = 0; @@ -1685,6 +1887,48 @@ // if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n"); // + // First of all insert the run in the fragment table... + // + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND (" + << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND " + << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND (" + << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR " + << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND (" + << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR " + << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR " + << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND (" + << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR " + << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND (" + << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR " + << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));"; + // + if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + row = result->Next(); + // + if ( !row ){ + // + // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!) + // + if ( IsDebug() ) printf(" The run is new \n"); + if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n"); + // + glrun->SetID(this->AssignRunID()); + glrun->SetID_RUN_FRAG(0); + glrun->Fill_GL_RUN_FRAGMENTS(conn); + // + } else { + if ( IsDebug() ) printf(" The run is already present in the fragment table \n"); + return; + }; + // + // // can we find the other piece of the run in the GL_RUN_FRAGMENTS table? // if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is @@ -1693,7 +1937,8 @@ oss.str(""); oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE " << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " - << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() + << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " ID != " << glrun->ID << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! // if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str()); @@ -1703,6 +1948,26 @@ // row = result->Next(); // + if ( !row && NoFrag() ){ + // + oss.str(""); + oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " + << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " + << " ID != " << glrun->ID + << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!! + // + if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + foundinrun = true; + // + row = result->Next(); + // + }; + // if ( !row ){ if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); found = false; @@ -1777,9 +2042,14 @@ // if ( IsDebug() ) printf(" now you can handle the piece of the run \n "); // + if ( foundinrun ){ + glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS"); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); + }; + // GL_RUN *glrun1 = new GL_RUN(); // - UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); + // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); // oss.str(""); oss << " ID="<GetField(0)<<";"; @@ -1792,7 +2062,7 @@ ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT()); UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT()); ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT()); - if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); TTree *T= 0; T = (TTree*)file->Get("Physics"); if ( !T || T->IsZombie() ) throw -16; @@ -1806,7 +2076,7 @@ bobt = OBT(ph->GetOrbitalTime()); firstev++; }; - if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); + if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev); // glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER()); glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER()); @@ -1816,6 +2086,7 @@ // glrun->SetEV_FROM(firstev); glrun->SetNEVENTS(lastev-firstev+1); + // glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME()); glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT()); glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT()); @@ -1835,70 +2106,23 @@ // if ( !IsRunAlreadyInserted() ){ // - glrun->SetID(this->AssignRunID()); + // glrun->SetID(this->AssignRunID()); glrun->SetID_RUN_FRAG(glrun1->GetID()); glrun->Fill_GL_RUN(conn); // - // get id number - // -// oss.str(""); -// oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<GetBOOTnumber()<<" AND " -// << " RUNHEADER_OBT="<GetRUNHEADER_OBT()<<" AND " -// << " RUNTRAILER_OBT="<GetRUNTRAILER_OBT()<<";"; -// // -// if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str()); -// // -// result = conn->Query(oss.str().c_str()); -// if ( !result ) throw -4; -// row = result->Next(); -// // -// UInt_t idrun0 = 0; -// if ( !row ){ -// throw -10; -// } else { -// idrun0 = (UInt_t)atoll(row->GetField(0)); -// }; + // set id number // - // glrun1->SetID_RUN_FRAG(idrun0); glrun1->SetID_RUN_FRAG(glrun->GetID()); glrun1->Fill_GL_RUN(conn); // -// oss.str(""); -// oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<Query(oss.str().c_str()); -// if ( !result ) throw -4; -// row = result->Next(); -// // -// UInt_t idrun1 = 0; -// if ( !row ){ -// throw -10; -// } else { -// idrun1 = (UInt_t)atoll(row->GetField(0)); -// }; -// // -// oss.str(""); -// oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<Query(oss.str().c_str()); -// if ( !result ) throw -4; - // }; + // delete old entry in fragment table // - delete glrun1; + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); // - // delete old entry in fragment table + delete glrun1; // - glrun->DeleteRun(conn,idfrag,"GL_RUN_FRAGMENTS"); -// oss.str(""); -// // -// oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";"; -// // -// if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str()); -// result = conn->Query(oss.str().c_str()); -// if ( !result ) throw -4; // return; // @@ -1907,12 +2131,13 @@ }; // if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is - // missing it no way we can found a piece in the frag table + // missing it no way we can found a piece in the frag table // oss.str(""); oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE " << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " - << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() + << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND " + << " ID != " << glrun->ID << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;"; // if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str()); @@ -1922,6 +2147,25 @@ // row = result->Next(); // + if ( !row && NoFrag() ){ + // + oss.str(""); + oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE " + << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND " + << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND " + << " ID != " << glrun->ID + << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;"; + // + if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4; + // + foundinrun = true; + row = result->Next(); + // + }; + // if ( !row ){ if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n"); found = false; @@ -1995,9 +2239,14 @@ // if ( IsDebug() ) printf(" now you can handle the piece of the run \n "); // + if ( foundinrun ){ + glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS"); + glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN"); + }; + // GL_RUN *glrun1 = new GL_RUN(); // - UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); + // UInt_t idfrag = (UInt_t)atoll(row->GetField(0)); // oss.str(""); oss << " ID="<GetField(0)<<";"; @@ -2010,7 +2259,7 @@ ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT()); UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT()); ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT()); - if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); + if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); TTree *T= 0; T = (TTree*)file->Get("Physics"); if ( !T || T->IsZombie() ) throw -16; @@ -2024,7 +2273,7 @@ aobt = OBT(ph->GetOrbitalTime()); lastev--; }; - if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); + if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev); // glrun->SetEV_TO(lastev); glrun->SetNEVENTS(lastev-firstev+1); @@ -2053,73 +2302,24 @@ // if ( !IsRunAlreadyInserted() ){ // - glrun->SetID(this->AssignRunID()); + // glrun->SetID(this->AssignRunID()); // glrun->SetID_RUN_FRAG(glrun1->GetID()); glrun->Fill_GL_RUN(conn); // - // get id number + // set id number // -// oss.str(""); -// oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<GetBOOTnumber()<<" AND " -// << " RUNHEADER_OBT="<GetRUNHEADER_OBT()<<" AND " -// << " RUNTRAILER_OBT="<GetRUNTRAILER_OBT()<<";"; -// // -// if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str()); -// // -// result = conn->Query(oss.str().c_str()); -// if ( !result ) throw -4; -// row = result->Next(); -// // -// UInt_t idrun0 = 0; -// if ( !row ){ -// throw -10; -// } else { -// idrun0 = (UInt_t)atoll(row->GetField(0)); -// }; -// // -// glrun1->SetID_RUN_FRAG(idrun0); -// glrun1->SetID_RUN_FRAG(glrun->GetID()); glrun1->Fill_GL_RUN(conn); // -// oss.str(""); -// oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<Query(oss.str().c_str()); -// if ( !result ) throw -4; -// row = result->Next(); -// // -// UInt_t idrun1 = 0; -// if ( !row ){ -// throw -10; -// } else { -// idrun1 = (UInt_t)atoll(row->GetField(0)); -// }; -// // -// oss.str(""); -// oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<Query(oss.str().c_str()); -// if ( !result ) throw -4; - // }; // - delete glrun1; - // - // delete old entry in fragment table + // delete old entries in fragment table // - glrun->DeleteRun(conn,idfrag,"GL_RUN_FRAGMENTS"); -// oss.str(""); -// // -// oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";"; -// // -// if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str()); -// result = conn->Query(oss.str().c_str()); -// if ( !result ) throw -4; + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); // + delete glrun1; // return; // @@ -2156,47 +2356,14 @@ // row = result->Next(); // - if ( !row ){ - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE " - << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND (" - << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND " - << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND (" - << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR " - << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND (" - << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR " - << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR " - << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND " - << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND (" - << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR " - << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND (" - << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR " - << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));"; - // - if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - // - if ( !result ) throw -4; - // - row = result->Next(); - // - if ( !row ){ - // - // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!) - // - if ( IsDebug() ) printf(" The run is new \n"); - if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n"); - // - glrun->SetID(this->AssignRunID()); - glrun->SetID_RUN_FRAG(0); - glrun->Fill_GL_RUN_FRAGMENTS(conn); - // - } else { - if ( IsDebug() ) printf(" The run is already present in the fragment table \n"); - }; - } else { + if ( row ){ if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n"); + } else { + if ( NoFrag() ){ + glrun->SetID_RUN_FRAG(glrun->GetID()); + glrun->Fill_GL_RUN(conn); + glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS"); + }; }; }; // @@ -2713,7 +2880,7 @@ fromtime = this->GetAbsTime(ph->GetOrbitalTime()); if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){ // - if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt); + if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt); // // check if the calibration has already been inserted // @@ -2757,8 +2924,8 @@ // no calibrations in the db contain our calibration // if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section); - if ( fromtime < 1150863400 ){ - if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime); + if ( fromtime < 1150871000 ){ //1150866904 + if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime); fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode }; // @@ -2820,7 +2987,7 @@ // } else { // - if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt); + if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt); // }; // @@ -2902,7 +3069,7 @@ // no calibrations in the db contain our calibration // if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n"); - 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 + if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode // oss.str(""); oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE " @@ -3031,14 +3198,17 @@ pkt1 = ph1->GetCounter(); fromtime = this->GetAbsTime(ph1->GetOrbitalTime()); // - valid = 1; - // - if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1 +// valid = 1; +// // +// if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1 // // if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){ // - if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1); + if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1); + // + valid = ValidateTrkCalib( caltrk1, eh1 ); + if ( IsDebug() ) cout << " pkt1 validation --> "<GetOrbitalTime(); pkt2 = ph2->GetCounter(); // - if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2 +// if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2 // } else { // @@ -3078,13 +3248,17 @@ // }; // - if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2); + if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2); // // The calibration is good // if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){ // - if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2); + if ( IsDebug() ) printf(" The trk calibration2 at obt %u pkt %u t2 is %u is good \n",obt2,pkt2,t2); + // + UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 ); + if ( IsDebug() ) cout << " pkt2 validation --> "< pret2+1 ){ // @@ -3123,7 +3297,7 @@ // // Check for missing calibtrk2 // - if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2); + if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); t2 = pret2; // // handle missing calib2 @@ -3137,7 +3311,7 @@ // } else { // - if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1); + if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1); // }; // @@ -3162,7 +3336,7 @@ valid = 0; if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){ // - if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2); + if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2); // this->HandleTRK_CALIB(false,true); // @@ -3216,7 +3390,7 @@ fromtime = this->GetAbsTime(ph->GetOrbitalTime()); if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){ // - if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt); + if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt); // // check if the calibration has already been inserted // @@ -3258,8 +3432,8 @@ // no calibrations in the db contain our calibration // if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n"); - if ( fromtime < 1150863400 ){ - if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime); + if ( fromtime < 1150871000 ){ + if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime); fromtime = 0;// the first flight calibration was taken at about 1156429100 s, this line allow to analyze first runs in raw mode }; // @@ -3318,7 +3492,7 @@ // } else { // - if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt); + if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt); // }; // @@ -3331,6 +3505,13 @@ * Scan the fragment table and move old fragments to the GL_RUN table */ Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){ + return(this->CleanGL_RUN_FRAGMENTS("")); +}; + +/** + * Scan the fragment table and move old fragments to the GL_RUN table + */ +Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){ // TSQLResult *result = 0; TSQLRow *row = 0; @@ -3338,21 +3519,44 @@ TSQLRow *row2 = 0; // UInt_t moved = 0; -// UInt_t timelim = 0; -// TDatime *time = new TDatime(); // stringstream oss; oss.str(""); // - // - // check if there are entries older than "olderthan" seconds from now - // - oss.str(""); - oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" - << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';"; - // - if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str()); - result = conn->Query(oss.str().c_str()); + if ( !strcmp(fcleanfile.Data(),"") ){ + // + // check if there are entries older than "olderthan" seconds from now + // + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" + << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';"; + // + if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + } else { + oss.str(""); + oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';"; + if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + if ( result ){ + // + row = result->Next(); + // + if ( row ){ + oss.str(""); + oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE" + << " ID_ROOT_L0=" << row->GetField(0) << ";"; + // + if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + // + }; + } else { + return(2); + }; + }; // if ( result ){ // @@ -3448,10 +3652,19 @@ * Check if runs are good, i.e. if the tracker calibration is correctly associated.. */ Int_t PamelaDBOperations::ValidateRuns(){ + return(this->ValidateRuns("")); +}; + +/** + * Check if runs are good, i.e. if the tracker calibration is correctly associated.. + */ +Int_t PamelaDBOperations::ValidateRuns(TString valfile){ // TSQLResult *result = 0; TSQLRow *row = 0; // + UInt_t calibtime = 50; + // stringstream oss; oss.str(""); // @@ -3460,50 +3673,114 @@ // ======================================================= UInt_t t_stop = 0; UInt_t t_start = 0; - // -------------------------------------------------------------- - // 1) get the OBT of the last run inserted after clean-time limit - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString() - << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; - if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - if ( !result->GetRowCount() ) { - printf(" No runs to validate \n"); - return(1); - }else{ - row = result->Next(); - t_start = (UInt_t)atoll(row->GetField(4)); - }; - // -------------------------------------------------------------- - // 2) get the OBT of the last validated run - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start - <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; - if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); - if ( !result ) throw -4; - if ( result->GetRowCount() ){ - row = result->Next(); - t_stop = (UInt_t)atoll(row->GetField(4)); + if ( !strcmp(valfile.Data(),"") ) { + // -------------------------------------------------------------- + // 1) get the OBT of the last run inserted after clean-time limit + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE INSERT_TIME <= '" << clean_time->AsSQLString() + << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; + if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4; + if ( !result->GetRowCount() ) { + printf(" No runs to validate \n"); + return(1); + }else{ + row = result->Next(); + t_start = (UInt_t)atoll(row->GetField(4)); + }; + // -------------------------------------------------------------- + // 2) get the OBT of the last validated run + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start + <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; + if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + if ( !result ) throw -4; + if ( result->GetRowCount() ){ + row = result->Next(); + t_stop = (UInt_t)atoll(row->GetField(4)); + }; + if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start); + // -------------------------------------------------------------- + // now retrieves runs to be validated + // -------------------------------------------------------------- + oss.str(""); + oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start; + oss << " AND RUNHEADER_TIME >="<< t_stop; + oss << " ORDER BY RUNHEADER_TIME DESC;"; + if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str()); + result = conn->Query(oss.str().c_str()); + } else { + // + stringstream myquery; + UInt_t myid = 0; + myquery.str(""); + myquery << " SELECT ID FROM GL_ROOT where NAME='"<Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row ){ + if ( strcmp(valfile.Data(),GetRootName().Data()) ){ + if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n"); + return(2); + }; + if ( IsDebug() ) printf(" No file to be validated (force mode)! \n"); + return(0); + }; + myid=(UInt_t)atoll(row->GetField(0)); + // + myquery.str(""); + myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";"; + // + result = conn->Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row->GetField(0) || !row->GetField(1)){ + // + if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n"); + // + return(0); + // + } else { + // + UInt_t runhtime = (UInt_t)atoll(row->GetField(0)); + UInt_t runttime = (UInt_t)atoll(row->GetField(1)); + UInt_t caltime = 0; + // + myquery.str(""); + myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <Query(myquery.str().c_str()); + // + row = result->Next(); + if( !row ){ + caltime = runhtime; + } else { + caltime = (UInt_t)atoll(row->GetField(0)); + }; + // + myquery.str(""); + myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ; + myquery << caltime << " order by RUNHEADER_TIME DESC"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + result = conn->Query(myquery.str().c_str()); + // + }; }; - if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start); - // -------------------------------------------------------------- - // now retrieves runs to be validated - // -------------------------------------------------------------- - oss.str(""); - oss << " SELECT * FROM GL_RUN WHERE RUNHEADER_TIME <=" << t_start; - oss << " AND RUNHEADER_TIME >="<< t_stop; - oss << " ORDER BY RUNHEADER_TIME DESC;"; -// if ( IsDebug() ) - if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str()); - result = conn->Query(oss.str().c_str()); + // if ( !result ) throw -4; - if ( !result->GetRowCount() ) printf(" No runs to validate \n"); -// printf("------------------------------------------------------------------------------- \n"); - + if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n"); + // Int_t nrow = 0; GL_RUN* this_run = new GL_RUN(); GL_RUN* next_run = new GL_RUN(); @@ -3517,7 +3794,7 @@ UInt_t t1=0,t2=0; // --------------------------------------------------------------------------------- // - loop over runs, back in time, - // - select sequences of runs close in time (less than 60 s apart), + // - select sequences of runs close in time (less than calibtime s apart), // which could be preceeded by a calibration // - check if there might be a missing calibration // --------------------------------------------------------------------------------- @@ -3557,7 +3834,7 @@ if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0; //=> run fragments - if( interval >= 60 )CHECK = true; //more than 60 s => there might be a calibration + if( interval >= calibtime )CHECK = true; //more than calibtime s => there might be a calibration if( !CHECK && this_run->VALIDATION ){ for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true); @@ -3597,7 +3874,7 @@ }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max); }; - if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID); + if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID); nrow++; }; @@ -3626,7 +3903,7 @@ // which should be equal to the time between ascending-nodes. //============================================================== if ( t2 - trkcalib->FROM_TIME > 5700) { - if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); + if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME); //============================================================== // there might be a missing calibration, due to: // - MM full @@ -3647,7 +3924,7 @@ // it is enough to say that there are no missing calibrations //============================================================== // the long time interval bewteen runs might be due to download - if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); + if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME); return(false); }; @@ -3755,7 +4032,7 @@ // Insert tle in the table GL_TLE using the connection conn. -int PamelaDBOperations::insertTle(cTle *tle) +Int_t PamelaDBOperations::insertTle(cTle *tle) { stringstream oss; TSQLResult *result = 0; @@ -3838,3 +4115,650 @@ return date.str(); } + +/** + * Remove a file from the DB, delete on cascade all entries related to that file + * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following + * calibration + **/ +Int_t PamelaDBOperations::removeFile(TString remfile){ + // + // Determine ID_ROOT_L0 and ID_RAW + // + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + myquery.str(""); + myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + if ( strcmp(remfile.Data(),GetRootName().Data()) ){ + if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n"); + return(1); + }; + if ( IsDebug() ) printf(" No file to be removed (force mode)! \n"); + return(0); + }; + // + this->SetID_ROOT((UInt_t)atoll(Row->GetField(0))); + this->SetID_RAW((UInt_t)atoll(Row->GetField(1))); + // + this->ValidationOFF(); + // + this->RemoveCALIBS(); + // + this->RemoveRUNS(); + // + this->RemoveFILES(); + // + this->SetID_ROOT(0); + this->SetID_RAW(0); + // + return(0); +}; + +/** + * + * Set validation bit to zero for runs following the removing file till + * 1) a run with TRK_CALIB_USED=140 + * 2) a run with VALIDATION = 0 + * 3) the next calibration + * + **/ +void PamelaDBOperations::ValidationOFF(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + Int_t unv = 0; + //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1; + myquery.str(""); + myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) ){ + // + if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + UInt_t runhtime = (UInt_t)atoll(Row->GetField(0)); + UInt_t caltime = 0; + // + myquery.str(""); + myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + caltime = runhtime; + } else { + caltime = (UInt_t)atoll(Row->GetField(0)); + }; + // + myquery.str(""); + myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ; + myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row ){ + // + if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n"); + // + } else { + myquery.str(""); + myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND "; + myquery << " RUNHEADER_TIME>=" <Query(myquery.str().c_str()); + // + Row = pResult->Next(); + while ( Row ){ + // + unv++; + this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false); + Row = pResult->Next(); + // + }; + }; + }; + if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv); +}; + +/** + * + * Rearrange GL_RUN table and remove runs + * + **/ +void PamelaDBOperations::RemoveRUNS(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + UInt_t drun = 0; + GL_RUN *delrun = new GL_RUN(); + // + myquery.str(""); + myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n"); + while ( Row ){ + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN"); + if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0))); + drun++; + Row = pResult->Next(); + }; + }; + // + // + myquery.str(""); + myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1))); + delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS"); + if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1))); + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN"); + if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){ + if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0))); + delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN"); + }; + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun); + // + // + // + drun = 0; + // + myquery.str(""); + myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0))); + myquery.str(""); + myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";"; + conn->Query(myquery.str().c_str()); + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun); + // + // + // + drun = 0; + // + myquery.str(""); + myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + // + if ( !Row ){ + if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n"); + } else { + if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n"); + while ( Row ){ + if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0))); + myquery.str(""); + myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";"; + conn->Query(myquery.str().c_str()); + drun++; + Row = pResult->Next(); + }; + }; + // + if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun); + // + // + // + delete delrun; + // +}; + + +/** + * + * Rearrange calibration tables + * + **/ +void PamelaDBOperations::RemoveFILES(){ + stringstream myquery; + // + myquery.str(""); + myquery << " DELETE FROM GL_RAW WHERE ID=" <GetID_RAW() <<";"; + // + if ( IsDebug() ) printf(" query is \n %s \n",myquery.str().c_str()); + // + conn->Query(myquery.str().c_str()); + // +}; + +/** + * + * Rearrange calibration tables + * + **/ +void PamelaDBOperations::RemoveCALIBS(){ + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + // + // Calorimeter + // + for (Int_t section = 0; section < 4; section++){ + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND "; + myquery << " SECTION=" << section << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND "; + myquery << " SECTION=" << section << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n"); + // + throw -4; + // + }; + // + }; + }; + myquery.str(""); + myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n"); + // + throw -4; + // + }; + // + // Tracker + // + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n"); + // + throw -4; + // + }; + // + myquery.str(""); + myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n"); + // + throw -4; + // + }; + }; + // + // + // S4 + // + myquery.str(""); + myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( !Row->GetField(0) || !Row->GetField(1) ){ + // + if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n"); + // + } else { + // + myquery.str(""); + myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1); + myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";"; + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n"); + // + throw -4; + // + }; + // + myquery.str(""); + myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT(); + // + pResult = conn->Query(myquery.str().c_str()); + // + if( !pResult ){ + // + if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n"); + // + throw -4; + // + }; + // + }; +}; + +/** + * + * Rearrange calibration tables + * + **/ +UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){ + + Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0}; + UInt_t timeaftercalib=120000; //2000; +// ---------- +// Check CRCs +// ---------- + for(Int_t ipkt=0; ipkt<6; ipkt++){ + if( caltrk->crc_hcal[ipkt] )return 0; // :-( + for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-( + } +// ----------------------- +// Check missing packets: +// ----------------------- +// Readout order: +// ------------------ +// DSP packet board +// ------------------ +// 12 0 1 +// 10 1 1 +// 8 2 1 +// 4 3 1 +// 6 4 1 +// 2 5 1 +// ------------------ +// 11 0 2 +// 9 1 2 +// 7 2 2 +// 3 3 2 +// 5 4 2 +// 1 5 2 +// ------------------ +// ------------------------------------------------- +// Check if it is first or second calibration packet +// ------------------------------------------------- + UInt_t build=0; + TString classname = caltrk->GetName(); + UInt_t base=0; + UInt_t mask=0; + if(classname.Contains("CalibTrk1Event")){ + base=12; + mask=0x03F000; + } + if(classname.Contains("CalibTrk2Event")){ + base=18; + mask=0xFC0000; + } +// ------------------------------------------------- +// Count number of packets and set build variable +// ------------------------------------------------- + Int_t npkts=0; + for(Int_t ipkt=0; ipkt<6; ipkt++){ + if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){ + npkts++; + build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) ); + } + } +// if( npkts==6 )return 1; // :-) + +// cout << classname << " "<GetPscuHeader()->GetOrbitalTime()<GetCounter(); + Int_t irun = cod->Get(pctp->RunHeader); + TTree *rh=(TTree*)file->Get("RunHeader"); + if ( !rh || rh->IsZombie() ) throw -17; + if( rh->GetEntries() == irun ){ + if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1) -- cannot validate :-( "<SetBranchAddress("RunHeader", &run); + rh->SetBranchAddress("Header", &hrun); + rh->GetEntry(irun); +// cout << classname << " "<GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){ + if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<RM_ACQ_AFTER_CALIB ){ + if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0 -- cannot validate :-( "<GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime()); + if( dtime > timeaftercalib ){ + if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<ACQ_BUILD_INFO & mask) != build ){ + if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<ACQ_BUILD_INFO&mask) << " != "<< build << dec<10170 and ID<10190 order by RUNHEADER_TIME asc;"; + result = conn->Query(oss.str().c_str()); + // + if ( !result ) throw -4;; + // + row = result->Next(); + // + while ( row ){ + thisid = (UInt_t)atoll(row->GetField(0)); + thisl0id = (UInt_t)atoll(row->GetField(1)); + thisrht = (UInt_t)atoll(row->GetField(2)); + thisrtt = (UInt_t)atoll(row->GetField(3)); + // + // if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){ + // if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){ + if ( (thisrht < prevrtt) && (thisrht != prevrht) ){ + if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt); + printf(" CHECK n.1 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid); + TString prevf = ""; + TString thisf = ""; + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + prevf = (TString)row2->GetField(0); + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + thisf = (TString)row2->GetField(0); + if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); + test = 1; + }; + // + if ( (thisrtt < prevrht) && (thisrht != prevrht) ){ + if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt); + printf(" CHECK n.2 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid); + TString prevf = ""; + TString thisf = ""; + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + prevf = (TString)row2->GetField(0); + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + thisf = (TString)row2->GetField(0); + if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); + test = 1; + }; + // + if ( (thisrht > thisrtt) && (thisrht != prevrht) ){ + if ( IsDebug() ) printf(" IDprev %u ID %u prevrht %u prevrtt %u thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt); + printf(" CHECK n.3 TIME SCREW of %u s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid); + TString prevf = ""; + TString thisf = ""; + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + prevf = (TString)row2->GetField(0); + oss.str(""); + oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";"; + result2 = conn->Query(oss.str().c_str()); + if ( !result2 ) throw -4;; + row2 = result2->Next(); + thisf = (TString)row2->GetField(0); + if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data()); + test = 1; + }; + + // + prevrht = thisrht; + prevrtt = thisrtt; + previd = thisid; + prevl0id = thisl0id; + row = result->Next(); + }; + // + return(test); + // +};