TDbiValidityRec.cxx

Go to the documentation of this file.
00001 // $Id: TDbiValidityRec.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $
00002 
00003 #include "DbiDetector.hxx"
00004 #include "DbiSimFlag.hxx"
00005 #include "TDbiBinaryFile.hxx"
00006 #include "TDbiInRowStream.hxx"
00007 #include "TDbiOutRowStream.hxx"
00008 #include "TDbiValidityRec.hxx"
00009 #include <TSK_DBI_Log.hxx>
00010 #include <MsgFormat.h>
00011 using std::endl;
00012 #include "TVldRange.hxx"
00013 #include "TVldTimeStamp.hxx"
00014 
00015 #include "TString.h"
00016 
00017 #include <sstream>
00018 #include <iostream>
00019 
00020 ClassImp(TDbiValidityRec)
00021 
00022 
00023 //   Definition of static data members
00024 //   *********************************
00025 
00026 
00027 //  Instantiate associated Result Pointer class.
00028 //  *******************************************
00029 
00030 #include "TDbiResultSetHandle.tpl"
00031 template class  TDbiResultSetHandle<TDbiValidityRec>;
00032 
00033 //  Global functions
00034 //  *****************
00035 
00036 //.....................................................................
00037 
00038 ostream& operator<<(ostream& os, const TDbiValidityRec& vRec) {
00039 //
00040 //
00041 //  Purpose:  Output validity record to ostream.
00042 //
00043 //  Arguments:
00044 //    os           in    output stream
00045 //    vRec         in    Validity record to be output
00046 //
00047 //  Return:        updated ostream
00048 //
00049 //  Contact:   N. West
00050 //
00051 //  Specification:-
00052 //  =============
00053 //
00054 //  o Output validity record to ostream.
00055 
00056 //  Program Notes:-
00057 //  =============
00058 
00059 //  None.
00060 
00061   const TVldRange&           range(vRec.GetVldRange());
00062 
00063   ostringstream os2;  //Use local to avoid manipulators from making permanent change to os.
00064   os2 << "SeqNo: " << vRec.GetSeqNo()
00065       << "  AggNo: " << vRec.GetAggregateNo()
00066       << "  DbNo: " << vRec.GetDbNo();
00067   if ( vRec.IsGap() ) os2 << " (gap)";
00068   os2  << "  TVldRange: |"
00069        << setprecision(3) << std::ios::hex << setfill ('0') << "0x" <<  setw(3) << range.GetDetectorMask() << "|"
00070        << "0x" << setw(3) << range.GetSimMask() << "|"
00071        << " " << range.GetTimeStart().AsString("s") << " .. "
00072        << range.GetTimeEnd().AsString("s")
00073        << " Epoch: " << vRec.GetEpoch()
00074        << " Task: " << vRec.GetTask()
00075        << " from: " << range.GetDataSource();
00076   os << os2.str();
00077   return os;
00078 
00079 }
00080 
00081 // Definition of member functions (alphabetical order)
00082 // ***************************************************
00083 
00084 
00085 //.....................................................................
00086 
00087 void TDbiValidityRec::AndTimeWindow(const TVldTimeStamp& startOther,
00088                                    const TVldTimeStamp& endOther) {
00089 //
00090 //
00091 //  Purpose:  And in supplied start and end times.
00092 //
00093 //  Arguments:
00094 //    startOther   in    Start time to be ANDed in.
00095 //    endOther     in    End time to be ANDed in.
00096 //
00097 //  Return:   None.
00098 //
00099 //  Contact:   N. West
00100 //
00101 //  Specification:-
00102 //  =============
00103 //
00104 //  o Trim time windown on current record to be the overlap (and)
00105 //    with the supplied time window.
00106 
00107 //  Program Notes:-
00108 //  =============
00109 
00110 //  None.
00111 
00112   TVldTimeStamp start = fVldRange.GetTimeStart();
00113   TVldTimeStamp end   = fVldRange.GetTimeEnd();
00114 
00115   SetTimeWindow( start > startOther ? start : startOther,
00116                  end   < endOther   ? end   : endOther );
00117 }
00118 
00119 //.....................................................................
00120 
00121 TDbiValidityRec::TDbiValidityRec(Int_t dbNo,Bool_t isGap) :
00122 
00123 fAggregateNo(-2),
00124 fDbNo(dbNo),
00125 fIsGap(isGap),
00126 fTask(0),
00127 fSeqNo(0),
00128 fTableProxy(0),
00129 fEpoch(0),
00130 fReality(0)
00131 {
00132 //
00133 //
00134 //  Purpose:  Default constructor
00135 //
00136 //  Arguments:.
00137 //    dbNo     in    Cascade no. of database holding record.
00138 //    isGap    in    = kTRUE if a gap.
00139 //
00140 //  Return:    n/a
00141 //
00142 //  Contact:   N. West
00143 //
00144 //  Specification:-
00145 //  =============
00146 //
00147 //  o Create ValidityRec.
00148 
00149 
00150 //  Program Notes:-
00151 //  =============
00152 
00153 //  None.
00154 
00155 
00156   SK_DBI_Trace( "Creating TDbiValidityRec" << "  ");
00157 }
00158 //.....................................................................
00159 
00160 TDbiValidityRec::TDbiValidityRec(const TDbiValidityRec& from) : TDbiTableRow(from)
00161 {
00162 //
00163 //
00164 //  Purpose:  Copy constructor
00165 //
00166 //  Arguments:
00167 //    from    in    Object to be copied from.
00168 //
00169 //  Return:    n/a
00170 //
00171 //  Contact:   N. West
00172 //
00173 //  Specification:-
00174 //  =============
00175 //
00176 //  o Create ValidityRec.
00177 
00178 
00179 //  Program Notes:-
00180 //  =============
00181 
00182 //  Explicit just for leak checking.
00183 
00184 
00185   SK_DBI_Trace( "Creating TDbiValidityRec" << "  ");
00186 
00187   *this = from;
00188 
00189 }
00190 //.....................................................................
00191 
00192 TDbiValidityRec::TDbiValidityRec(const TVldRange& range,
00193                                TDbi::Task task,
00194                                Int_t aggNo,
00195                                UInt_t seqNo,
00196                                Int_t dbNo,
00197                                Bool_t isGap,
00198                                TVldTimeStamp time,
00199                                UInt_t epoch) :
00200 fAggregateNo(aggNo),
00201 fCreationDate(time),
00202 fDbNo(dbNo),
00203 fIsGap(isGap),
00204 fTask(task),
00205 fSeqNo(seqNo),
00206 fTableProxy(0),
00207 fVldRange(range),
00208 fEpoch(epoch),
00209 fReality(0)
00210 {
00211 //
00212 //
00213 //  Purpose:  Constructor from components
00214 //
00215 //  Arguments:
00216 //    range    in   component range
00217 //    task     in   component task
00218 //    aggNo    in   component aggregate number
00219 //    seqNo    in   component sequence number
00220 //    dbNo     in    Cascade no. of database holding record.
00221 //    isGap    in    = kTRUE if a gap.
00222 //    epoch    in   component calibration epoch
00223 //
00224 //
00225 //
00226 //  Return:    n/a
00227 //
00228 //  Contact:   N. West
00229 //
00230 //  Specification:-
00231 //  =============
00232 //
00233 //  o Create ValidityRec.
00234 
00235 
00236 //  Program Notes:-
00237 //  =============
00238 
00239 //  None.
00240 
00241 
00242   SK_DBI_Trace( "Creating TDbiValidityRec" << "  ");
00243 }
00244 
00245 //.....................................................................
00246 
00247 TDbiValidityRec::~TDbiValidityRec() {
00248 //
00249 //
00250 //  Purpose: Destructor
00251 //
00252 //  Arguments:
00253 //    None.
00254 //
00255 //  Return:    n/a
00256 //
00257 //  Contact:   N. West
00258 //
00259 //  Specification:-
00260 //  =============
00261 //
00262 //  o  Destroy ValidityRec.
00263 
00264 
00265 //  Program Notes:-
00266 //  =============
00267 
00268 //  None.
00269 
00270 
00271   SK_DBI_Trace( "Destroying TDbiValidityRec" << "  ");
00272 
00273 }
00274 
00275 //.....................................................................
00276 
00277 void TDbiValidityRec::Fill(TDbiInRowStream& rs,
00278                           const TDbiValidityRec* /* vrec */) {
00279 //
00280 //
00281 //  Purpose:  Fill oject from Result Set
00282 //
00283 //  Arguments:
00284 //    rs           in    Result Set used to fill object
00285 //    vrec         in    =0.  If filling other table rows it points
00286 //                       to the associated validity record.
00287 //
00288 //  Return:
00289 //
00290 //  Contact:   N. West
00291 //
00292 //  Specification:-
00293 //  =============
00294 //
00295 //  o Fill object from current row of Result Set.
00296 
00297 //  Program Notes:-
00298 //  =============
00299 
00300 //  None.
00301 
00302     TVldTimeStamp start, end;
00303     Int_t detMask, simMask;
00304 
00305 //  Establish source TDbiTableProxy and cascade no.
00306 
00307     fTableProxy = rs.GetTableProxy();
00308     fDbNo       = rs.GetDbNo();
00309 
00310 //  Read data from row.
00311 
00312     rs >> fSeqNo >> start >> end;
00313 
00314     if ( rs.HasEpoch() ) rs >> fEpoch >> fReality;
00315     else fEpoch = fReality = 0;
00316 
00317     rs >> detMask >> simMask >> fTask
00318        >> fAggregateNo    >> fCreationDate  >> fInsertDate;
00319 
00320     fIsGap = kFALSE;
00321 
00322     TVldRange vr(detMask, simMask, start, end, "From Database");
00323     fVldRange = vr;
00324 
00325     SK_DBI_Verbose(  "TDbiValidityRec for row " << rs.CurRowNum()
00326          << ": " << fVldRange.AsString()
00327          << " seq num: " << fSeqNo
00328          << " agg no: "  << fAggregateNo
00329          << " task: " << "  ");
00330 }
00331 
00332 //.....................................................................
00333 
00334 std::string TDbiValidityRec::GetL2CacheName() const {
00335 //
00336 //
00337 //  Purpose:  Return the associated Level 2 Cache Name.
00338 //
00339 //  Specification:-
00340 //  =============
00341 //
00342 //  o See GetL2CacheName(UInt_t seqLo, UInt_t seqHi, TVldTimeStamp ts);
00343 
00344   return TDbiValidityRec::GetL2CacheName(this->GetSeqNo(),
00345                                         this->GetSeqNo(),
00346                                         this->GetCreationDate());
00347 
00348 }
00349 //.....................................................................
00350 
00351 std::string TDbiValidityRec::GetL2CacheName(UInt_t seqLo,
00352                                            UInt_t seqHi,
00353                                            TVldTimeStamp ts) {
00354 //
00355 //
00356 //  Purpose:  Return the associated Level 2 Cache Name.
00357 //
00358 //  Specification:-
00359 //  =============
00360 //
00361 //  o For unaggregated queries name format: <Seqno>_<CreationDate>
00362 //
00363 //  o For aggregated queries name format:   <LowSeqno>_<HighSeqNo>_<MaxCreationDate>
00364 
00365 //  Program Notes:-
00366 //  =============
00367 
00368 //  The static routine centralises the definition of cache names both for
00369 //  TDbiValidityRec and TDbiValidityRecBuilder.
00370 
00371   ostringstream oss;
00372   oss << seqLo << "_";
00373   if ( seqLo != seqHi )  oss << seqHi << "_";
00374   oss << ts.AsString("s");
00375   std::string str(oss.str());
00376   // Convert white space to underscore.
00377   int i = str.size();
00378   while ( i-- ) if ( str[i] == ' ' ) str[i] = '_';
00379   return str;
00380 
00381 }
00382 
00383 //.....................................................................
00384 
00385 Bool_t TDbiValidityRec::HasExpired(const TDbiValidityRec& other) const {
00386 //
00387 //
00388 //  Purpose: See if validity record has expired i.e. is compatible with
00389 //           this one except for date.
00390 //
00391 //  Arguments:
00392 //    other        in    Other TDbiValidityRec to be compared with
00393 //
00394 //  Return:
00395 //
00396 //  Contact:   N. West
00397 //
00398 //  Specification:-
00399 //  =============
00400 //
00401 //  o See if validity record has expired i.e. is compatible with
00402 //    this one except for date.
00403 
00404 //  Program Notes:-
00405 //  =============
00406 
00407 //  This method is used by TDbiCache to select candidates for
00408 //  purging.
00409 
00410   const TVldRange& otherVR =other.GetVldRange();
00411 
00412   return
00413      (       other.GetTask() == fTask
00414         && ( otherVR.GetDetectorMask() & fVldRange.GetDetectorMask() )
00415         && ( otherVR.GetSimMask()      & fVldRange.GetSimMask() )
00416         && (    otherVR.GetTimeStart() >= fVldRange.GetTimeEnd()
00417              || otherVR.GetTimeEnd()   <= fVldRange.GetTimeStart() )
00418      );
00419 
00420 }
00421 //.....................................................................
00422 
00423 
00424 Bool_t TDbiValidityRec::HasExpired(const TVldContext& vc,
00425                                   const TDbi::Task& task) const {
00426 //
00427 //
00428 //  Purpose: See if validity record has expired i.e. is compatible with
00429 //           this context query except for date.
00430 //
00431 //  Arguments:
00432 //    vc           in    Context of query
00433 //    task         in    Task of query
00434 //
00435 //
00436 //  Contact:   N. West
00437 
00438 //  Program Notes:-
00439 //  =============
00440 
00441 //  This method is used by TDbiResultSet to identify when ready for
00442 //  purging.
00443 
00444   TVldTimeStamp    ts = vc.GetTimeStamp();
00445   const TVldRange& vr = this->GetVldRange();
00446 
00447   return
00448      (       task             == this->GetTask()
00449         && ( vc.GetDetector() &  vr.GetDetectorMask() )
00450         && ( vc.GetSimFlag()  &  vr.GetSimMask() )
00451         && (    ts            >= vr.GetTimeEnd()
00452              || ts            <  vr.GetTimeStart() )
00453      );
00454 
00455 }
00456 
00457 //.....................................................................
00458 
00459 
00460 Bool_t TDbiValidityRec::IsCompatible(const TVldContext& vc,
00461                                     const TDbi::Task& task) const {
00462 //
00463 //
00464 //  Purpose: See if result is compatible with query.
00465 //
00466 //  Arguments:
00467 //    vc           in    Context of new query
00468 //    task         in    Task of new query
00469 //
00470 //  Return:
00471 //
00472 //  Contact:   N. West
00473 //
00474 //  Specification:-
00475 //  =============
00476 //
00477 //  o See if result is compatible with query.
00478 
00479 //  Program Notes:-
00480 //  =============
00481 
00482 //  None.
00483 
00484    SK_DBI_Debug(  " TDbiValidityRec::IsCompatible : tasks:"
00485      << task << "," << fTask
00486      << " is compat: " << fVldRange.IsCompatible(vc) << "  "
00487      << "   range " << fVldRange.AsString() << "  "
00488      << "   context " << vc.AsString() << "  ");
00489 
00490   return task == fTask  && fVldRange.IsCompatible(vc);
00491 
00492 }
00493 
00494 //.....................................................................
00495 
00496 
00497 Bool_t TDbiValidityRec::IsHigherPriority(const TDbiValidityRec& that,
00498                                         Bool_t useCreationDate) const {
00499 //
00500 //  Purpose:  Compare priorities (used in context queries to resolve ambiguities between VLD entries).
00501 //
00502 //  Arguments:
00503 //    that             in    The TDbiValidityRec to be compared with
00504 //    useCreationDate  in    If false (the default value) use the T2K resolution scheme
00505 //                           which is resolved, in order, by EPOCH, STARTTIME, INSERTDATE
00506 //                           If true use the MINOS resolution scheme which is resolved by CREATIONDATE.
00507 //
00508 //  Return:   True if this is of higher priority than that.
00509 
00510   if ( useCreationDate) return fCreationDate > that.fCreationDate;
00511 
00512   if ( fEpoch != that.fEpoch ) return fEpoch > that.fEpoch;
00513   if ( fVldRange.GetTimeStart() != that.fVldRange.GetTimeStart() )
00514     return fVldRange.GetTimeStart() > that.fVldRange.GetTimeStart();
00515   return fInsertDate > that.fInsertDate;
00516 
00517 }
00518 
00519 //.....................................................................
00520 
00521 
00522 void TDbiValidityRec::SetTimeWindow(const TVldTimeStamp& start,
00523                                    const TVldTimeStamp& end) {
00524 //
00525 //
00526 //  Purpose:  Set supplied start and end times.
00527 //
00528 //  Arguments:
00529 //    start        in    Start time to be set,
00530 //    end          in    End time to be set.
00531 //
00532 //  Return:   None.
00533 //
00534 //  Contact:   N. West
00535 //
00536 //  Specification:-
00537 //  =============
00538 //
00539 //  o Set time windown on current record.
00540 
00541 //  Program Notes:-
00542 //  =============
00543 
00544 //  The method here is rather clumsy, but I don't know of anything
00545 //  better!
00546 
00547   TVldRange  range(fVldRange.GetDetectorMask(),
00548                   fVldRange.GetSimMask(),
00549                   start,
00550                   end,
00551                   fVldRange.GetDataSource() );
00552   fVldRange = range;
00553 
00554 }
00555 
00556 //.....................................................................
00557 
00558 void TDbiValidityRec::Store(TDbiOutRowStream& ors,
00559                            const TDbiValidityRec* /* vrec */) const {
00560 //
00561 //
00562 //  Purpose:  Stream object to output row stream
00563 //
00564 //  Arguments:
00565 //    ors          in    Output row stream.
00566 //    vrec         in    =0.  If filling other table rows it points
00567 //                       to the associated validity record.
00568 //
00569 //  Return:
00570 //
00571 //  Contact:   N. West
00572 //
00573 //  Specification:-
00574 //  =============
00575 //
00576 //  o  Stream object to output row stream.
00577 
00578 //  Program Notes:-
00579 //  =============
00580 
00581 //  None.
00582 
00583 //  Write data to row.
00584 
00585     ors << fSeqNo
00586         << fVldRange.GetTimeStart()
00587         << fVldRange.GetTimeEnd();
00588     if ( ors.HasEpoch() ) ors << fEpoch << fReality;
00589     ors << fVldRange.GetDetectorMask()
00590         << fVldRange.GetSimMask()
00591         << fTask
00592         << fAggregateNo
00593         << fCreationDate
00594         << fInsertDate;
00595 }
00596 //.....................................................................
00597 
00598 void TDbiValidityRec::Streamer(TDbiBinaryFile& file) {
00599 //
00600 //
00601 //  Purpose:  I/O to binary file
00602 //
00603 //  Program Notes:-
00604 //  =============
00605 
00606 //  Don't store the pointer fTableProxy (don't want to get into storing
00607 //  pointers!); the TDbiCache will fix it up on input
00608 
00609   if ( file.IsReading() ) {
00610     file >> fAggregateNo
00611          >> fCreationDate
00612          >> fEpoch
00613          >> fReality
00614          >> fDbNo
00615          >> fInsertDate
00616          >> fIsGap
00617          >> fTask
00618          >> fSeqNo
00619          >> fVldRange;
00620     fTableProxy = 0;
00621   }
00622   else if ( file.IsWriting() ) {
00623     file << fAggregateNo
00624          << fCreationDate
00625          << fEpoch
00626          << fReality
00627          << fDbNo
00628          << fInsertDate
00629          << fIsGap
00630          << fTask
00631          << fSeqNo
00632          << fVldRange;
00633   }
00634 }
00635 
00636 //.....................................................................
00637 
00638 void TDbiValidityRec::Trim(const TVldTimeStamp& queryTime,
00639                           const TDbiValidityRec& other) {
00640 //
00641 //
00642 //  Purpose:  Trim this validity record so that represents
00643 //            best validity record for query.
00644 //
00645 //  Arguments:
00646 //    queryTime    in    Time of query
00647 //    other        in    TDbiValidity record satisfying query
00648 //
00649 //  Return:    None.
00650 //
00651 //  Contact:   N. West
00652 //
00653 //  Specification:-
00654 //  =============
00655 //
00656 //  o Update this validity record so that it remains the best
00657 //    validity record taking into account the supplied record.
00658 
00659 
00660 //  Program Notes:-
00661 //  =============
00662 
00663 //  This is the function that deal with validity management.
00664 //  It takes into account that several validity records may
00665 //  overlap and that the best one is the one with:-
00666 //    For MINOS:  the latest creation date
00667 //    For T2K:    the highest EPOCH,TIMESTART,INSERTDATE
00668 //  that brackets the query date.  Other higher priority entries
00669 //  may trim start or end times.
00670 
00671 //  Assumptions:-
00672 //  ===========
00673 //
00674 //  That entries are submitted in strict descending priority i.e.:-
00675 //
00676 //  1)  Entries for a higher priority database precede those from a
00677 //      lower priority one.
00678 //
00679 //  2)  Within a database entries are in descending CREATIONDATE or
00680 //      EPOCH,TIMESTART,INSERTDATE order.
00681 //
00682 //  This means that, until an entry that overlaps the supplied query time
00683 //  is reach, the entries just restrict the possible time window.  The first
00684 //  entry that overlaps the supplied time is accepted with a trimmed window
00685 //  and all subsequent entries ignored.
00686 
00687 //  Ignore other records that are either gaps or have wrong
00688 //  aggregate number.
00689 
00690   if ( fAggregateNo != other.fAggregateNo || other.IsGap() ) return;
00691 
00692 //  If this record is not a gap then the other record can be ignore
00693 //  as it is of lower priority.
00694 
00695   if ( ! IsGap() ) return;
00696 
00697   TVldTimeStamp start      = fVldRange.GetTimeStart();
00698   TVldTimeStamp end        = fVldRange.GetTimeEnd();
00699   TVldTimeStamp startOther = other.GetVldRange().GetTimeStart();
00700   TVldTimeStamp endOther   = other.GetVldRange().GetTimeEnd();
00701 
00702 // If entry brackets query date, then use it but with a validity that
00703 // is trimmed by the current record.
00704 
00705   if ( startOther <= queryTime && endOther > queryTime ) {
00706     if ( start < startOther ) start = startOther;
00707     if ( end   > endOther   ) end   = endOther;
00708     *this = other;
00709     SetTimeWindow(start,end);
00710   }
00711 
00712 // It doesn't bracket, so use it to trim the window
00713 
00714   else {
00715 
00716     if ( endOther <= queryTime ) {
00717       if ( start < endOther   ) SetTimeWindow(endOther,end);
00718     }
00719     else if ( startOther > queryTime ) {
00720       if ( end   > startOther ) SetTimeWindow(start, startOther);
00721     }
00722   }
00723 
00724 }
00725 
00726 /*    Template for New Member Function
00727 
00728 //.....................................................................
00729 
00730 TDbiValidityRec:: {
00731 //
00732 //
00733 //  Purpose:
00734 //
00735 //  Arguments:
00736 //    xxxxxxxxx    in    yyyyyy
00737 //
00738 //  Return:
00739 //
00740 //  Contact:   N. West
00741 //
00742 //  Specification:-
00743 //  =============
00744 //
00745 //  o
00746 
00747 //  Program Notes:-
00748 //  =============
00749 
00750 //  None.
00751 
00752 
00753 }
00754 
00755 */
00756 
00757 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1