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.13 2009/05/22 10:59:42 tinti 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.13  2009/05/22 10:59:42  tinti
00011 // Get rid of duplicate code. Now all reco version factors are stored in a single function in CalDrift. To get the right constants it's enough to use CalDrift::GetMeadianAsR1_18_2(..) - the same for the associate error.
00012 //
00013 // Revision 1.12  2009/05/21 18:24:43  tinti
00014 // reco factor for Dogwood. Calibration drift constant is FrontMedian() in the Near detector if it is Dogwood. Change the interpolator to apply reco scale factor consistently
00015 //
00016 // Revision 1.11  2007/04/01 14:00:20  evans
00017 // Adding the Cedar reco version factor for the far detector.
00018 //
00019 // Revision 1.10  2007/03/21 12:07:55  evans
00020 // Ensuring the version shear factors are detector-specific.
00021 //
00022 // Revision 1.9  2007/03/16 17:59:53  evans
00023 // Putting in a version shear correction factor for all reconstruction
00024 // versions. Cedar is not yet corrected for (waiting for a batch job to run).
00025 //
00026 // All versions are corrected to be consistent with R1_18_2.
00027 //
00028 // Revision 1.8  2006/04/19 18:50:38  rhatcher
00029 // Change DetectorType:: to Detector:: everywhere.
00030 //
00031 // Revision 1.7  2006/04/09 16:48:39  evansj
00032 // Removing another cout debugging statement.
00033 //
00034 // Revision 1.6  2006/04/09 16:46:12  evansj
00035 // Removing a cout debugging statement.
00036 //
00037 // Revision 1.5  2006/04/09 16:38:10  evansj
00038 // Updating the DoReset() method to include the version shear factor.
00039 // Now all drift correction factors are corrected to be consistent with R1_18 reconstruction.
00040 //
00041 // Revision 1.4  2006/04/07 22:01:49  tagg
00042 // Add <math.h> to make gcc 4.0 happy.
00043 //
00044 // Revision 1.3  2006/04/07 16:58:33  evansj
00045 // Adding a RecoVersion flag to CalDrift.
00046 // Minor change to a warning message in MuonDriftCalScheme.
00047 // Making CalDrift copy constructor public.
00048 //
00049 // Revision 1.2  2006/02/27 20:43:29  tagg
00050 // Dangit! Erased my last CVS commit message.
00051 // Change the default reference date to Dec 01, 2005.
00052 //
00053 // Revision 1.1  2006/02/27 20:40:50  tagg
00054 // *** empty log message ***
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    //task default is still 0, meaning pre Dogwood database
00085    //from Dogwood1 on the task number needs to be set to 1
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   // Interpret date.
00107   fReferenceTime = VldTimeStamp(refdate,reftime,0);
00108 
00109   // Ensure the DB has been fixed up.
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); // Default: No drift, 50% error.
00125   string refRecoVersion = "";
00126   string recoVersion = "";
00127 
00129   // Build context for reference point.
00130   VldContext refContext(vc.GetDetector(),
00131                         vc.GetSimFlag(),
00132                         fReferenceTime);
00133   
00135   // Get the reference point data.
00136   fRefTable.NewQuery(refContext,fTask);
00137 
00138   // The reference value:
00139   FloatErr reference(1,0);
00140 
00141   // Maybe we don't have any data there....
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     // Flag it.
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); // We know at least one row exists.
00155     reference = refRow->GetMedianAsR1_18_2(vc.GetDetector());
00156     reference.SetError(0); // The error on the reference point is not relevant.
00157     refRecoVersion = refRow->GetRecoVersion();
00158  }
00159   
00160   // The current value:
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 //       << "  cx=" << refContext.AsString() 
00172       << ". Trying extrapolation." << std::endl;
00173     // Ok, now we have work to do.
00174 
00175     VldTimeStamp prevTime;
00176     const CalDrift* prevRow =0;
00177     VldTimeStamp nextTime;
00178     const CalDrift* nextRow =0;
00179 
00180     // Attempt to find the previous valid data.
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     //MSG("Calib",Msg::kDebug) << "PrevRequest: " << sqlprev << endl;        
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     // Attempt to find the next valid data.
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     //MSG("Calib",Msg::kDebug) << "NextRequest: " << sqlnext << endl;        
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       // Interpolate between these two straddling points.
00221       //MSG("Calib",Msg::kDebug) << "Interpolating.      ";
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       // Extrapolate from the next point.
00238       //MSG("Calib",Msg::kDebug) << "Extrapolating next. ";
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       // Degrade the error by 2% per month it's out of date.
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       // Extrapolate from the previous point.
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       // Degrade the error by 2% per day it's out of date.
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       // No data at all!
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     // We have current data, so all the above is unneccessary.
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& /*seid*/) 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& /*seid*/) const
00319 {
00328 
00329   if(fCurError) {
00330     IncrementErrors(kDriftCalibrator,kMissingRow);
00331   }
00332   return undrifted*fCurDrift;
00333 }
00334 

Generated on Sat Nov 7 01:26:31 2009 for loon by  doxygen 1.3.9.1