Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

BDSpillAccessor.cxx

Go to the documentation of this file.
00001 #include "BDSpillAccessor.h"
00002 
00003 #include <Validity/VldContext.h>
00004 
00005 #include <MessageService/MsgService.h>
00006 CVSID("$Id: BDSpillAccessor.cxx,v 1.24 2009/01/06 22:01:46 loiacono Exp $");
00007 
00008 #include <DatabaseInterface/DbiResultPtr.tpl>
00009 template class  DbiResultPtr<BeamMonSpill>;
00010 
00011 #include <cmath>
00012 using namespace std;
00013 
00014 BDSpillAccessor* BDSpillAccessor::fInstance = 0;
00015 
00016 BDSpillAccessor& BDSpillAccessor::Get()
00017 {
00018     if (!fInstance) fInstance = new BDSpillAccessor();
00019     return *fInstance;
00020 }
00021 
00022 
00023 BDSpillAccessor::BDSpillAccessor()
00024     : fPrevPtr(0)
00025     , fCenterPtr(new DbiResultPtr<BeamMonSpill>)
00026     , fNextPtr(0)
00027     , fNeedNext(true)
00028     , fNeedPrev(true)
00029 {
00030 }
00031 BDSpillAccessor::~BDSpillAccessor()
00032 {
00033     if (fPrevPtr)   delete fPrevPtr;
00034     if (fCenterPtr) delete fCenterPtr;
00035     if (fNextPtr)   delete fNextPtr;
00036     fPrevPtr = fCenterPtr = fNextPtr = 0;
00037 }
00038 
00039 const BeamMonSpill* BDSpillAccessor::LoadSpill(VldTimeStamp vts, int which)
00040 {
00041     const BeamMonSpill *before=0, *after=0;
00042     this->SeekClosest(vts,before,after);
00043 
00044     // Apply corrections and calibrations
00045     //cout << "before: "  << before->fTortgt << endl;
00046     if (before) this->CalibrateSpill(before);
00047     //cout << "after: "  << before->fTortgt << endl;
00048     if (after) this->CalibrateSpill(after);
00049     
00050     if (which < 0) return before;
00051     if (which > 0) return after;
00052 
00053     if (!before) return after;
00054     if (!after) return before;
00055 
00056     VldTimeStamp vts_before = before->SpillTime();
00057     VldTimeStamp vts_after = after->SpillTime();
00058 
00059     MSG("BDU",Msg::kDebug)
00060        << vts_before << " " << vts << " " << vts_after << endl;
00061 
00062     if (vts - vts_before < vts_after - vts)
00063         return before;
00064     return after;
00065 
00066 }
00067 
00068 
00069 void BDSpillAccessor::SeekClosest(const VldTimeStamp &vts,
00070                                   const BeamMonSpill* &before,
00071                                   const BeamMonSpill* &after)
00072 {
00073     Detector::Detector_t det = Detector::kNear;
00074     SimFlag::SimFlag_t simflag = SimFlag::kData;
00075     VldContext vc(det,simflag,vts);
00076 
00077     fCenterPtr->NewQuery(vc,0,true);
00078 
00079     if (! fResKey.IsEqualTo(fCenterPtr->GetKey()))
00080     {
00081        fNeedPrev = fNeedNext = true;
00082     }
00083     
00084     const DbiValidityRec* vr = fCenterPtr->GetValidityRec();
00085 
00086     VldTimeStamp beg = vr->GetVldRange().GetTimeStart();
00087     VldTimeStamp end = vr->GetVldRange().GetTimeEnd();
00088 
00089     before=after=0;
00090     this->TableSearch(*fCenterPtr,vts,before,after);
00091 
00092     if (before && after) return;
00093 
00094     MSG("BDU",Msg::kDebug)
00095         << vts << " not in center table, before @ " << (void*)before
00096         << " after @ " << (void*)after 
00097         << " seqno = " << vr->GetSeqNo() << endl;
00098 
00099     if (!before) {
00100        Detector::Detector_t det = Detector::kNear;
00101        SimFlag::SimFlag_t simflag = SimFlag::kData;
00102        VldContext vc(det,simflag,beg);
00103        if(!fPrevPtr || !fNeedPrev)
00104        {
00105           fPrevPtr = new DbiResultPtr<BeamMonSpill>();
00106        }
00107        fPrevPtr->NewQuery(vc,0,true);
00108               
00109        fNeedPrev = false;
00110        
00111        const BeamMonSpill* dummy=0;
00112        this->TableSearch(*fPrevPtr,vts,before,dummy);
00113        if(dummy)
00114        {
00115           MSG("BDU",Msg::kError)
00116              << "Found next time in previous table: "
00117              << dummy->SpillTime() << endl;
00118        }
00119        
00120     }
00121     if (!after){
00122        Detector::Detector_t det = Detector::kNear;
00123        SimFlag::SimFlag_t simflag = SimFlag::kData;
00124        VldContext vc(det,simflag,end);
00125        if(!fNextPtr || !fNeedNext)
00126        {
00127           fNextPtr = new DbiResultPtr<BeamMonSpill>();
00128        }
00129        fNextPtr->NewQuery(vc,0,true);
00130               
00131        fNeedNext = false;
00132        
00133        const BeamMonSpill* dummy=0;
00134        this->TableSearch(*fNextPtr,vts,dummy,after);
00135         if(dummy)
00136         {
00137            MSG("BDU",Msg::kError)
00138               << "Found previous time in next table: "
00139               << dummy->SpillTime() << endl;
00140         }
00141     }
00142     
00143 }
00144 
00145 void BDSpillAccessor::TableSearch(DbiResultPtr<BeamMonSpill> &table,
00146                                   const VldTimeStamp& vts, 
00147                                   const BeamMonSpill* &before,
00148                                   const BeamMonSpill* &after)
00149 {
00150     before = after = 0;
00151 
00152     int nrows = table.GetNumRows();
00153     if (nrows <= 0) return;
00154 
00155     double minneg = 1e10;
00156     double minpos = 1e10;
00157     for (int row = 0; row < nrows; ++row) {
00158         const BeamMonSpill* spill = table.GetRow(row);
00159         double dt = spill->SpillTime() - vts;
00160         if (dt <= 0) {
00161             if (-dt < minneg) {
00162                 minneg = -dt;
00163                 before = spill;
00164             }
00165         }
00166         else {
00167             if (dt < minpos) {
00168                 minpos = dt;
00169                 after = spill;
00170             }
00171         }
00172     }
00173 }
00174 
00175 void BDSpillAccessor::CalibrateSpill(const BeamMonSpill* spill)
00176 {
00177     // Apply corrections to the data pulled out of the batabase
00178     //
00179     // Apply corrections to tortgt
00180     // From Mary:
00181     // (Corrected for the new values from Sasha after he realised
00182     // that he had distributed the wrong numbers - 2005-11-16)
00183     //
00184     // Recommend only using TORTGT. There were several iterations of changes
00185     // applied to TOR101 in early June. It was recalibrated again on July 22
00186     // (-4.0%). TORTGT was changed only once - on July 26th.
00187     //
00188     // TORTGT calibrations (same calibrations apply to TRTGTD (20080313)):
00189     // --------------------
00190     // Before July 26th 2:17:41 GMT Doug Jensens calibrations indicate a
00191     // correction of -4.0% to the ACNET values of TORTGT.
00192     //
00193     // The period July 26th 2:17:41 GMT to August 1st 22:43:23 GMT is when the
00194     // Beam data process was applying the wrong ACNET hardware calibration. The
00195     // BDP correction is -12.6%. There is a further correction of -0.0 % based on
00196     // Doug Jensens studies = -12.6% total
00197     //
00198     // After August 1st 22:43:23 the only correction needed is Doug Jensen's
00199     // -0.0%.
00200     //
00201     BeamMonSpill* spill_cor = const_cast<BeamMonSpill*>(spill);
00202 
00203     // Avoid double calibration
00204     if (spill_cor->fStatus.bits.calibrated) return;
00205     spill_cor->fStatus.bits.calibrated = 1;
00206 
00207     int timesec = spill_cor->SpillTime().GetSec();
00208     double tortgt_cor = 1.0;
00209     double trtgtd_cor = 1.0;
00210     //  Before July 26th, 2005  2:17:41 UTC
00211     if (timesec < 1122344261) 
00212     {
00213       tortgt_cor = 0.96;
00214       trtgtd_cor = 0.96;
00215     }
00216     // After previous date but before Aug 1st 2005 22:43:23
00217     else if (timesec < 1122936203)
00218     { 
00219       tortgt_cor = 0.874;
00220       trtgtd_cor = 0.874;
00221     }
00222     // after previous date
00223     else 
00224     {
00225       tortgt_cor = 1.0;
00226       trtgtd_cor = 1.0;
00227     }
00228     //
00229     spill_cor->fTortgt *= tortgt_cor;
00230     spill_cor->fTrtgtd *= trtgtd_cor;
00231     //
00232     // Horn Current Calibrations
00233     // -------------------------
00234     // From Jim Hylen (2005-10-28):
00235     //   A -1.6% change applied to total horn current would be fine -
00236     //   the individual offsets etc. are pretty minor.
00237     //   So far there is no known time dependence.
00238     //
00239     // There was a miscommunication about the direction of the change,
00240     // the total horn current should be larger by 1.8%.  
00241     //
00242     double horcur_cor = 0.982;
00243     spill_cor->fHornCur /= horcur_cor;
00244 }    

Generated on Mon Feb 15 11:06:24 2010 for loon by  doxygen 1.3.9.1