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

MuonDriftCalScheme.cxx

Go to the documentation of this file.
00001 
00002 // $Id: MuonDriftCalScheme.cxx,v 1.11 2007/04/01 14:00:20 evans Exp $
00003 //
00004 // Calibrator for detector-wide drift by muon measurements.
00005 //
00006 // Justin Evans, Nathaniel Tagg
00007 // j.evans2@physics.ox.ac.uk, tagg@minos.phy.tufts.edu
00008 //
00009 // $Log: MuonDriftCalScheme.cxx,v $
00010 // Revision 1.11  2007/04/01 14:00:20  evans
00011 // Adding the Cedar reco version factor for the far detector.
00012 //
00013 // Revision 1.10  2007/03/21 12:07:55  evans
00014 // Ensuring the version shear factors are detector-specific.
00015 //
00016 // Revision 1.9  2007/03/16 17:59:53  evans
00017 // Putting in a version shear correction factor for all reconstruction
00018 // versions. Cedar is not yet corrected for (waiting for a batch job to run).
00019 //
00020 // All versions are corrected to be consistent with R1_18_2.
00021 //
00022 // Revision 1.8  2006/04/19 18:50:38  rhatcher
00023 // Change DetectorType:: to Detector:: everywhere.
00024 //
00025 // Revision 1.7  2006/04/09 16:48:39  evansj
00026 // Removing another cout debugging statement.
00027 //
00028 // Revision 1.6  2006/04/09 16:46:12  evansj
00029 // Removing a cout debugging statement.
00030 //
00031 // Revision 1.5  2006/04/09 16:38:10  evansj
00032 // Updating the DoReset() method to include the version shear factor.
00033 // Now all drift correction factors are corrected to be consistent with R1_18 reconstruction.
00034 //
00035 // Revision 1.4  2006/04/07 22:01:49  tagg
00036 // Add <math.h> to make gcc 4.0 happy.
00037 //
00038 // Revision 1.3  2006/04/07 16:58:33  evansj
00039 // Adding a RecoVersion flag to CalDrift.
00040 // Minor change to a warning message in MuonDriftCalScheme.
00041 // Making CalDrift copy constructor public.
00042 //
00043 // Revision 1.2  2006/02/27 20:43:29  tagg
00044 // Dangit! Erased my last CVS commit message.
00045 // Change the default reference date to Dec 01, 2005.
00046 //
00047 // Revision 1.1  2006/02/27 20:40:50  tagg
00048 // *** empty log message ***
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   // Interpret date.
00099   fReferenceTime = VldTimeStamp(refdate,reftime,0);
00100 
00101   // Ensure the DB has been fixed up.
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); // Default: No drift, 50% error.
00117   string refRecoVersion = "";
00118   string recoVersion = "";
00119 
00120   //Make all drift numbers consistent with R1_18_2.
00121   Double_t r1_18Factor = 1.0; //R1_18_2 = r1_18Factor*R1_18;
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; //R1_18_2 = r1_18Factor*R1_18;
00127     r1_18_2Factor = 1.0;
00128     r1_18_4Factor = 1.0;
00129     cedarFactor = 1.0007; //R1_18_2 = cedarFactor*cedar;
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; //R1_18_2 = cedarFactor*cedar;
00136   }
00137 
00139   // Build context for reference point.
00140   VldContext refContext(vc.GetDetector(),
00141                         vc.GetSimFlag(),
00142                         fReferenceTime);
00143 
00144 
00146   // Get the reference point data.
00147   fRefTable.NewQuery(refContext,fTask);
00148 
00149 
00150   // The reference value:
00151   FloatErr reference(1,0);
00152 
00153   // Maybe we don't have any data there....
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     // Flag it.
00162     fCurError=1;
00163     refRecoVersion = "Null";
00164   } else {
00165     const CalDrift* refRow = fRefTable.GetRow(0); // We know at least one row exists.
00166     reference = refRow->GetMedian();
00167     //    if ("R1_18" == refRow->GetRecoVersion()){reference *= corrFact;}
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); // The error on the reference point is not relevant.
00173     refRecoVersion = refRow->GetRecoVersion();
00174   }
00175   
00176 
00177   // The current value:
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 //       << "  cx=" << refContext.AsString() 
00189       << ". Trying extrapolation." << std::endl;
00190     // Ok, now we have work to do.
00191 
00192     VldTimeStamp prevTime;
00193     const CalDrift* prevRow =0;
00194     VldTimeStamp nextTime;
00195     const CalDrift* nextRow =0;
00196 
00197     // Attempt to find the previous valid data.
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     //MSG("Calib",Msg::kDebug) << "PrevRequest: " << sqlprev << endl;        
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     // Attempt to find the next valid data.
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     //MSG("Calib",Msg::kDebug) << "NextRequest: " << sqlnext << endl;        
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       // Interpolate between these two straddling points.
00238       //MSG("Calib",Msg::kDebug) << "Interpolating.      ";
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       // Extrapolate from the next point.
00259       //MSG("Calib",Msg::kDebug) << "Extrapolating next. ";
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       // Degrade the error by 2% per month it's out of date.
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       // Extrapolate from the previous point.
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       // Degrade the error by 2% per day it's out of date.
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       // No data at all!
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     // We have current data, so all the above is unneccessary.
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& /*seid*/) 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& /*seid*/) const
00343 {
00352 
00353   if(fCurError) {
00354     IncrementErrors(kDriftCalibrator,kMissingRow);
00355   }
00356   return undrifted*fCurDrift;
00357 }
00358 

Generated on Fri Mar 28 15:35:15 2008 for loon by  doxygen 1.3.9.1