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
00045
00046 if (before) this->CalibrateSpill(before);
00047
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
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 BeamMonSpill* spill_cor = const_cast<BeamMonSpill*>(spill);
00202
00203
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
00211 if (timesec < 1122344261)
00212 {
00213 tortgt_cor = 0.96;
00214 trtgtd_cor = 0.96;
00215 }
00216
00217 else if (timesec < 1122936203)
00218 {
00219 tortgt_cor = 0.874;
00220 trtgtd_cor = 0.874;
00221 }
00222
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
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 double horcur_cor = 0.982;
00243 spill_cor->fHornCur /= horcur_cor;
00244 }