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
00052 #include <math.h>
00053 #include <string>
00054
00055 #include "Calibrator/MuonDriftCalScheme.h"
00056 #include "Calibrator.h"
00057 #include "MessageService/MsgService.h"
00058 #include "Plex/PlexHandle.h"
00059 #include "DatabaseInterface/DbiValidityRec.h"
00060 #include "DatabaseInterface/DbiSqlContext.h"
00061 #include "Validity/VldRange.h"
00062 #include "Conventions/Munits.h"
00063
00064 using std::string;
00065
00066 ClassImp(MuonDriftCalScheme)
00067 CVSID("$Id: MuonDriftCalScheme.cxx,v 1.11 2007/04/01 14:00:20 evans Exp $");
00068
00069
00070
00071 MuonDriftCalScheme::MuonDriftCalScheme()
00072 {
00073 MSG("Calib",Msg::kVerbose)
00074 << "MuonDriftCalScheme::MuonDriftCalScheme"
00075 << endl;
00076
00077 Registry r;
00078 r.Set("Task",0);
00079 r.Set("ReferenceDate",20051201);
00080 r.Set("ReferenceTime",000010);
00081
00082 InitializeConfig(r);
00083 }
00084
00085
00086 void MuonDriftCalScheme::ConfigModified()
00087 {
00088 int refdate;
00089 int reftime;
00090
00091 bool ok = true;
00092 ok = ok && GetConfig().Get("ReferenceDate",refdate);
00093 ok = ok && GetConfig().Get("ReferenceTime",reftime);
00094 ok = ok && GetConfig().Get("Task",fTask);
00095 if(!ok) MSG("Calib",Msg::kWarning) << "MuonDriftCalibrator "
00096 << " Problem when configuring. " <<endl;
00097
00098
00099 fReferenceTime = VldTimeStamp(refdate,reftime,0);
00100
00101
00102 Reset(GetContext(),true);
00103 }
00104
00105
00106 void MuonDriftCalScheme::DoReset( const VldContext& vc )
00107 {
00114
00115 fCurError = 0;
00116 fCurDrift.Set(1.0 ,0.5);
00117 string refRecoVersion = "";
00118 string recoVersion = "";
00119
00120
00121 Double_t r1_18Factor = 1.0;
00122 Double_t r1_18_2Factor = 1.0;
00123 Double_t r1_18_4Factor = 1.0;
00124 Double_t cedarFactor = 1.0;
00125 if (Detector::kNear == vc.GetDetector()){
00126 r1_18Factor = 1.00129;
00127 r1_18_2Factor = 1.0;
00128 r1_18_4Factor = 1.0;
00129 cedarFactor = 1.0007;
00130 }
00131 if (Detector::kFar == vc.GetDetector()){
00132 r1_18Factor = 1.0;
00133 r1_18_2Factor = 1.0;
00134 r1_18_4Factor = 1.0;
00135 cedarFactor = 0.96688;
00136 }
00137
00139
00140 VldContext refContext(vc.GetDetector(),
00141 vc.GetSimFlag(),
00142 fReferenceTime);
00143
00144
00146
00147 fRefTable.NewQuery(refContext,fTask);
00148
00149
00150
00151 FloatErr reference(1,0);
00152
00153
00154 if(fRefTable.GetNumRows() == 0) {
00155 IncrementErrors(kDriftCalibrator,kMissingTable);
00156 MAXMSG("Calib",Msg::kError,10)
00157 << "No rows in CALDRIFT reference table task=" << fTask
00158 << " cx=" << refContext.AsString() << std::endl;
00159 MAXMSG("Calib",Msg::kError,10)
00160 << "Your database is bad or MuonDriftCalScheme is misconfigured!" << endl;
00161
00162 fCurError=1;
00163 refRecoVersion = "Null";
00164 } else {
00165 const CalDrift* refRow = fRefTable.GetRow(0);
00166 reference = refRow->GetMedian();
00167
00168 if ("R1_18" == refRow->GetRecoVersion()){reference *= r1_18Factor;}
00169 if ("R1_18_2" == refRow->GetRecoVersion()){reference *= r1_18_2Factor;}
00170 if ("R1_18_4" == refRow->GetRecoVersion()){reference *= r1_18_4Factor;}
00171 if ("Cedar" == refRow->GetRecoVersion()){reference *= cedarFactor;}
00172 reference.SetError(0);
00173 refRecoVersion = refRow->GetRecoVersion();
00174 }
00175
00176
00177
00178 FloatErr current(1,0);
00179
00180 fCurTable.NewQuery(vc,fTask);
00181
00182 if(fCurTable.GetNumRows() == 0) {
00183 IncrementErrors(kDriftCalibrator,kMissingTable);
00184
00185 MAXMSG("Calib",Msg::kError,10)
00186 << "No rows in CALDRIFT table task=" << fTask
00187 << " cx=" << vc.AsString()
00188
00189 << ". Trying extrapolation." << std::endl;
00190
00191
00192 VldTimeStamp prevTime;
00193 const CalDrift* prevRow =0;
00194 VldTimeStamp nextTime;
00195 const CalDrift* nextRow =0;
00196
00197
00198 const char* sqlprev = Form("(TIMEEND<='%s') "
00199 "and (TASK=%d) "
00200 "and (DETECTORMASK & %d) "
00201 "and (SIMMASK & %d) "
00202 "order by TIMEEND desc limit 1",
00203 vc.GetTimeStamp().AsString("s"),
00204 fTask,
00205 vc.GetDetector(),
00206 vc.GetSimFlag() );
00207
00208 DbiSqlContext prevContext(sqlprev);
00209 DbiResultPtr<CalDrift> prevTable("CALDRIFT",prevContext);
00210 if(prevTable.GetNumRows()>0) {
00211 prevRow = prevTable.GetRow(0);
00212 VldRange r = prevTable.GetValidityRec(prevRow)->GetVldRange();
00213 prevTime = r.GetTimeStart() + 0.5*(r.GetTimeEnd() - r.GetTimeStart());
00214 }
00215
00216
00217 const char* sqlnext = Form("(TIMESTART>='%s') "
00218 "and (TASK=%d) "
00219 "and (DETECTORMASK & %d) "
00220 "and (SIMMASK & %d) "
00221 "order by TIMESTART asc limit 1",
00222 vc.GetTimeStamp().AsString("s"),
00223 fTask,
00224 vc.GetDetector(),
00225 vc.GetSimFlag() );
00226
00227 DbiSqlContext nextContext(sqlnext);
00228 DbiResultPtr<CalDrift> nextTable("CALDRIFT",nextContext);
00229 if(nextTable.GetNumRows()>0) {
00230 nextRow = nextTable.GetRow(0);
00231 VldRange r = nextTable.GetValidityRec(nextRow)->GetVldRange();
00232 nextTime = r.GetTimeStart() + 0.5*(r.GetTimeEnd() - r.GetTimeStart());
00233 }
00234
00235 if((nextRow)&&(prevRow)) {
00236
00237
00238
00239 double dt = (nextTime-prevTime);
00240 Float_t prevFact = 1.0;
00241 Float_t nextFact = 1.0;
00242 if ("R1_18" == nextRow->GetRecoVersion()){nextFact = r1_18Factor;}
00243 if ("R1_18_2" == nextRow->GetRecoVersion()){nextFact = r1_18_2Factor;}
00244 if ("R1_18_4" == nextRow->GetRecoVersion()){nextFact = r1_18_4Factor;}
00245 if ("Cedar" == nextRow->GetRecoVersion()){nextFact = cedarFactor;}
00246 if ("R1_18" == prevRow->GetRecoVersion()){prevFact = r1_18Factor;}
00247 if ("R1_18_2" == prevRow->GetRecoVersion()){prevFact = r1_18_2Factor;}
00248 if ("R1_18_4" == prevRow->GetRecoVersion()){prevFact = r1_18_4Factor;}
00249 if ("Cedar" == prevRow->GetRecoVersion()){prevFact = cedarFactor;}
00250 FloatErr y1(prevRow->GetMedian(), prevRow->GetMedianErr());
00251 FloatErr y2(nextRow->GetMedian(), nextRow->GetMedianErr());
00252 FloatErr y = (y2*nextFact-y1*nextFact)*(vc.GetTimeStamp()-prevTime)/dt + y1;
00253 if(fCurError==0)
00254 fCurDrift = y/reference;
00255
00256 } else if (nextRow) {
00257
00258
00259
00260 FloatErr y(nextRow->GetMedian(), nextRow->GetMedianErr());
00261 if ("R1_18" == nextRow->GetRecoVersion()){y *= r1_18Factor;}
00262 if ("R1_18_2" == nextRow->GetRecoVersion()){y *= r1_18_2Factor;}
00263 if ("R1_18_4" == nextRow->GetRecoVersion()){y *= r1_18_4Factor;}
00264 if ("Cedar" == nextRow->GetRecoVersion()){y *= cedarFactor;}
00265
00266 y *= FloatErr(1.0, 0.02 * fabs(nextTime-vc.GetTimeStamp())/(30*Munits::day));
00267 if(fCurError==0)
00268 fCurDrift = y/reference;
00269
00270 } else if (prevRow) {
00271
00272
00273 MSG("Calib",Msg::kDebug) << "Extrapolating prev. ";
00274 FloatErr y(prevRow->GetMedian(), prevRow->GetMedianErr());
00275 if ("R1_18" == prevRow->GetRecoVersion()){y *= r1_18Factor;}
00276 if ("R1_18_2" == prevRow->GetRecoVersion()){y *= r1_18_2Factor;}
00277 if ("R1_18_4" == prevRow->GetRecoVersion()){y *= r1_18_4Factor;}
00278 if ("Cedar" == prevRow->GetRecoVersion()){y *= cedarFactor;}
00279
00280 y *= FloatErr(1.0, 0.02 * fabs(prevTime-vc.GetTimeStamp())/(30*Munits::day));
00281 if(fCurError==0)
00282 fCurDrift = y/reference;
00283
00284 } else {
00285
00286 fCurError = 1;
00287 MAXMSG("Calib",Msg::kError,10)
00288 << "No rows in CALDRIFT task=" << fTask
00289 << "for ANY time! Badness." << std::endl;
00290 }
00291
00292
00293 } else {
00294
00295 MSG("Calib",Msg::kDebug) << "Have data. ";
00296 const CalDrift* nowRow = fCurTable.GetRow(0);
00297 current.Set(nowRow->GetMedian(), nowRow->GetMedianErr());
00298 if ("R1_18" == nowRow->GetRecoVersion()){current *= r1_18Factor;}
00299 if ("R1_18_2" == nowRow->GetRecoVersion()){current *= r1_18_2Factor;}
00300 if ("R1_18_4" == nowRow->GetRecoVersion()){current *= r1_18_4Factor;}
00301 if ("Cedar" == nowRow->GetRecoVersion()){current *= cedarFactor;}
00302 if(fCurError == 0)
00303 fCurDrift = current/reference;
00304 }
00305 MSG("Calib",Msg::kDebug) << "Drift constant = " << fCurDrift.AsString()
00306 << " for time " << vc.AsString() << std::endl;
00307
00308
00309 }
00310
00311
00312
00313 void MuonDriftCalScheme::PrintConfig( std::ostream& os ) const
00314 {
00315 os << " Muon Drift Scheme: " << endl;
00316 os << " ReferenceTime: " << fReferenceTime.AsString() << endl;
00317 os << " Task: " << fTask << endl;
00318 }
00319
00320
00321
00322 FloatErr MuonDriftCalScheme::GetDriftCorrected(FloatErr rawcharge,
00323 const PlexStripEndId& ) const
00324 {
00333
00334 if(fCurError) {
00335 IncrementErrors(kDriftCalibrator,kMissingRow);
00336 }
00337 return rawcharge/fCurDrift;
00338 }
00339
00340
00341 FloatErr MuonDriftCalScheme::DecalDrift(FloatErr undrifted,
00342 const PlexStripEndId& ) const
00343 {
00352
00353 if(fCurError) {
00354 IncrementErrors(kDriftCalibrator,kMissingRow);
00355 }
00356 return undrifted*fCurDrift;
00357 }
00358