00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00058 #include <math.h>
00059 #include <string>
00060
00061 #include "Calibrator/MuonDriftCalScheme.h"
00062 #include "Calibrator.h"
00063 #include "MessageService/MsgService.h"
00064 #include "Plex/PlexHandle.h"
00065 #include "DatabaseInterface/DbiValidityRec.h"
00066 #include "DatabaseInterface/DbiSqlContext.h"
00067 #include "Validity/VldRange.h"
00068 #include "Conventions/Munits.h"
00069
00070 using std::string;
00071
00072 ClassImp(MuonDriftCalScheme)
00073 CVSID("$Id: MuonDriftCalScheme.cxx,v 1.13 2009/05/22 10:59:42 tinti Exp $");
00074
00075
00076
00077 MuonDriftCalScheme::MuonDriftCalScheme()
00078 {
00079 MSG("Calib",Msg::kVerbose)
00080 << "MuonDriftCalScheme::MuonDriftCalScheme"
00081 << endl;
00082
00083 Registry r;
00084
00085
00086 r.Set("Task",0);
00087 r.Set("ReferenceDate",20051201);
00088 r.Set("ReferenceTime",000010);
00089
00090 InitializeConfig(r);
00091 }
00092
00093
00094 void MuonDriftCalScheme::ConfigModified()
00095 {
00096 int refdate;
00097 int reftime;
00098
00099 bool ok = true;
00100 ok = ok && GetConfig().Get("ReferenceDate",refdate);
00101 ok = ok && GetConfig().Get("ReferenceTime",reftime);
00102 ok = ok && GetConfig().Get("Task",fTask);
00103 if(!ok) MSG("Calib",Msg::kWarning) << "MuonDriftCalibrator "
00104 << " Problem when configuring. " <<endl;
00105
00106
00107 fReferenceTime = VldTimeStamp(refdate,reftime,0);
00108
00109
00110 Reset(GetContext(),true);
00111 }
00112
00113 void MuonDriftCalScheme::DoReset( const VldContext& vc )
00114 {
00115
00122
00123 fCurError = 0;
00124 fCurDrift.Set(1.0 ,0.5);
00125 string refRecoVersion = "";
00126 string recoVersion = "";
00127
00129
00130 VldContext refContext(vc.GetDetector(),
00131 vc.GetSimFlag(),
00132 fReferenceTime);
00133
00135
00136 fRefTable.NewQuery(refContext,fTask);
00137
00138
00139 FloatErr reference(1,0);
00140
00141
00142 if(fRefTable.GetNumRows() == 0) {
00143 IncrementErrors(kDriftCalibrator,kMissingTable);
00144 MAXMSG("Calib",Msg::kError,10)
00145 << "No rows in CALDRIFT reference table task=" << fTask
00146 << " cx=" << refContext.AsString() << std::endl;
00147 MAXMSG("Calib",Msg::kError,10)
00148 << "Your database is bad or MuonDriftCalScheme is misconfigured!" << endl;
00149
00150 fCurError=1;
00151 refRecoVersion = "Null";
00152 assert(fRefTable.GetNumRows() && "No Table row for reference date");
00153 } else {
00154 const CalDrift* refRow = fRefTable.GetRow(0);
00155 reference = refRow->GetMedianAsR1_18_2(vc.GetDetector());
00156 reference.SetError(0);
00157 refRecoVersion = refRow->GetRecoVersion();
00158 }
00159
00160
00161 FloatErr current(1,0);
00162
00163 fCurTable.NewQuery(vc,fTask);
00164
00165 if(fCurTable.GetNumRows() == 0) {
00166 IncrementErrors(kDriftCalibrator,kMissingTable);
00167
00168 MAXMSG("Calib",Msg::kError,10)
00169 << "No rows in CALDRIFT table task=" << fTask
00170 << " cx=" << vc.AsString()
00171
00172 << ". Trying extrapolation." << std::endl;
00173
00174
00175 VldTimeStamp prevTime;
00176 const CalDrift* prevRow =0;
00177 VldTimeStamp nextTime;
00178 const CalDrift* nextRow =0;
00179
00180
00181 const char* sqlprev = Form("(TIMEEND<='%s') "
00182 "and (TASK=%d) "
00183 "and (DETECTORMASK & %d) "
00184 "and (SIMMASK & %d) "
00185 "order by TIMEEND desc limit 1",
00186 vc.GetTimeStamp().AsString("s"),
00187 fTask,
00188 vc.GetDetector(),
00189 vc.GetSimFlag() );
00190
00191 DbiSqlContext prevContext(sqlprev);
00192 DbiResultPtr<CalDrift> prevTable("CALDRIFT",prevContext);
00193 if(prevTable.GetNumRows()>0) {
00194 prevRow = prevTable.GetRow(0);
00195 VldRange r = prevTable.GetValidityRec(prevRow)->GetVldRange();
00196 prevTime = r.GetTimeStart() + 0.5*(r.GetTimeEnd() - r.GetTimeStart());
00197 }
00198
00199
00200 const char* sqlnext = Form("(TIMESTART>='%s') "
00201 "and (TASK=%d) "
00202 "and (DETECTORMASK & %d) "
00203 "and (SIMMASK & %d) "
00204 "order by TIMESTART asc limit 1",
00205 vc.GetTimeStamp().AsString("s"),
00206 fTask,
00207 vc.GetDetector(),
00208 vc.GetSimFlag() );
00209
00210 DbiSqlContext nextContext(sqlnext);
00211 DbiResultPtr<CalDrift> nextTable("CALDRIFT",nextContext);
00212 if(nextTable.GetNumRows()>0) {
00213 nextRow = nextTable.GetRow(0);
00214 VldRange r = nextTable.GetValidityRec(nextRow)->GetVldRange();
00215 nextTime = r.GetTimeStart() + 0.5*(r.GetTimeEnd() - r.GetTimeStart());
00216 }
00217
00218 if((nextRow)&&(prevRow)) {
00219
00220
00221
00222 double dt = (nextTime-prevTime);
00223 Float_t y1a=prevRow->GetMedianAsR1_18_2(vc.GetDetector());
00224 Float_t y1b=prevRow->GetMedianErrAsR1_18_2(vc.GetDetector());
00225
00226 FloatErr y1(y1a, y1b);
00227 Float_t y2a=nextRow->GetMedianAsR1_18_2(vc.GetDetector());
00228 Float_t y2b=nextRow->GetMedianErrAsR1_18_2(vc.GetDetector());
00229
00230 FloatErr y2(y2a,y2b);
00231 FloatErr y = (y2-y1)*(vc.GetTimeStamp()-prevTime)/dt + y1;
00232 if(fCurError==0)
00233 fCurDrift = y/reference;
00234
00235 } else if (nextRow) {
00236
00237
00238
00239 Float_t ya=nextRow->GetMedianAsR1_18_2(vc.GetDetector());
00240 Float_t yb= nextRow->GetMedianErrAsR1_18_2(vc.GetDetector());
00241 FloatErr y(ya,yb);
00242
00243
00244 y *= FloatErr(1.0, 0.02 * fabs(nextTime-vc.GetTimeStamp())/
00245 (30*Munits::day));
00246 if(fCurError==0)
00247 fCurDrift = y/reference;
00248
00249 } else if (prevRow) {
00250
00251
00252 MSG("Calib",Msg::kDebug) << "Extrapolating prev. ";
00253 Float_t ya=prevRow->GetMedianAsR1_18_2(vc.GetDetector());
00254 Float_t yb=prevRow->GetMedianErrAsR1_18_2(vc.GetDetector());
00255
00256 FloatErr y(ya,yb);
00257
00258 y *= FloatErr(1.0, 0.02 * fabs(prevTime-vc.GetTimeStamp())/
00259 (30*Munits::day));
00260 if(fCurError==0)
00261 fCurDrift = y/reference;
00262
00263 } else {
00264
00265 fCurError = 1;
00266 MAXMSG("Calib",Msg::kError,10)
00267 << "No rows in CALDRIFT task=" << fTask
00268 << "for ANY time! Badness." << std::endl;
00269 }
00270 } else {
00271
00272 MSG("Calib",Msg::kDebug) << "Have data. ";
00273 const CalDrift* nowRow = fCurTable.GetRow(0);
00274 Float_t ya=nowRow->GetMedianAsR1_18_2(vc.GetDetector());
00275 Float_t yb=nowRow->GetMedianErrAsR1_18_2(vc.GetDetector());
00276 current.Set(ya,yb);
00277
00278 if(fCurError == 0)
00279 fCurDrift = current/reference;
00280 }
00281 MSG("Calib",Msg::kDebug) << "Drift constant = " << fCurDrift.AsString()
00282 << " for time " << vc.AsString() << std::endl;
00283
00284
00285 }
00286
00287
00288
00289 void MuonDriftCalScheme::PrintConfig( std::ostream& os ) const
00290 {
00291 os << " Muon Drift Scheme: " << endl;
00292 os << " ReferenceTime: " << fReferenceTime.AsString() << endl;
00293 os << " Task: " << fTask << endl;
00294 }
00295
00296
00297
00298 FloatErr MuonDriftCalScheme::GetDriftCorrected(FloatErr rawcharge,
00299 const PlexStripEndId& ) const
00300 {
00309
00310 if(fCurError) {
00311 IncrementErrors(kDriftCalibrator,kMissingRow);
00312 }
00313 return rawcharge/fCurDrift;
00314 }
00315
00316
00317 FloatErr MuonDriftCalScheme::DecalDrift(FloatErr undrifted,
00318 const PlexStripEndId& ) const
00319 {
00328
00329 if(fCurError) {
00330 IncrementErrors(kDriftCalibrator,kMissingRow);
00331 }
00332 return undrifted*fCurDrift;
00333 }
00334