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

PEGainAggCalScheme.cxx

Go to the documentation of this file.
00001 
00002 // $Id: PEGainAggCalScheme.cxx,v 1.2 2007/04/24 18:32:18 tagg Exp $
00003 //
00004 // PE gain Calibrator class.
00005 // The new version uses Minsuk's aggregated tables
00006 //
00007 // tagg@minos.phy.tufts.edu
00009 #include "PEGainAggCalScheme.h"
00010 #include "MessageService/MsgService.h"
00011 #include "Plex/PlexHandle.h"
00012 #include "Plex/PlexStripEndId.h"
00013 #include "DatabaseInterface/DbiResultKey.h"
00014 
00015 ClassImp(PEGainAggCalScheme)
00016 CVSID("$Id: PEGainAggCalScheme.cxx,v 1.2 2007/04/24 18:32:18 tagg Exp $");
00017 
00018 //......................................................................
00019 PEGainAggCalScheme::PEGainAggCalScheme() :
00020   fDefaultRowQIE(0),
00021   fDefaultRowVA(0)
00022 {
00026   Registry r;
00027   r.Set("GuessOtherSpots",1);
00028   r.Set("GuessOtherPixels",1);
00029   r.Set("DefaultGainVA",60.);
00030   r.Set("DefaultGainQIE",100.);
00031   r.Set("DefaultWidthVA",30.);
00032   r.Set("DefaultWidthQIE",50.);
00033   r.Set("TuneGainFactor",1.0);
00034   r.Set("UseCache",1);
00035   r.Set("UseLinearized",1);
00036   InitializeConfig(r);
00037   
00038   fLastKey= new DbiResultKey(fResPtr.GetKey());
00039 }
00040 
00041 PEGainAggCalScheme::~PEGainAggCalScheme()
00042 {
00043   ResetCache(); 
00044   if(fDefaultRowQIE) delete fDefaultRowQIE; fDefaultRowQIE = 0;
00045   if(fDefaultRowVA ) delete fDefaultRowVA;  fDefaultRowVA= 0;
00046 }
00047 
00048 
00049 void PEGainAggCalScheme::ResetCache()
00050 {
00051   // Deletes and wipes any cached info.
00052   fSpotCache.clear();
00053   fStripCache.clear();
00054   for(UInt_t i=0;i<fCacheStorage.size();i++) {
00055     delete fCacheStorage[i];
00056   }
00057   fCacheStorage.clear();
00058 
00059 }
00060 
00061 //......................................................................
00062 void PEGainAggCalScheme::DoReset( const VldContext& context )
00063 {
00064   Int_t task = 0;
00065   if(fUseLinearized && context.GetDetector()==Detector::kNear) task = 1;
00066 
00067   fResPtr.NewQuery(context,task);  
00068   if(fResPtr.GetNumRows()==0) {
00069     MAXMSG("Calib",Msg::kWarning,10) 
00070       << "PEGainAggCalScheme: No rows in database table with validity context "
00071       << context.AsString() << "  No calibration will be applied." << endl;
00072     IncrementErrors(kPeCalibrator,kMissingTable);
00073   }  
00074 
00075 
00076   if(fUseCache) {
00077     const DbiResultKey* newKey = fResPtr.GetKey();
00078     
00079     if(fLastKey!=0) 
00080       if(!(fLastKey->IsEqualTo(newKey) ) ) ResetCache(); 
00081     
00082     if(fLastKey) delete fLastKey;
00083     fLastKey = new DbiResultKey(newKey);
00084   } else {
00085     // Not using cache.
00086     ResetCache(); // Do it every time.
00087   }
00088   
00089 
00090   
00091 }
00092 
00093 //......................................................................
00094 void PEGainAggCalScheme::ConfigModified()
00095 {
00096   bool ok = true;
00097   ok = ok && GetConfig().Get("GuessOtherSpots",fGuessOtherSpots);
00098   ok = ok && GetConfig().Get("GuessOtherPixels",fGuessOtherPixels);
00099   ok = ok && GetConfig().Get("DefaultGainVA",fDefaultGainVA);
00100   ok = ok && GetConfig().Get("DefaultGainQIE",fDefaultGainQIE);
00101   ok = ok && GetConfig().Get("DefaultWidthVA",fDefaultWidthVA);
00102   ok = ok && GetConfig().Get("DefaultWidthQIE",fDefaultWidthQIE);
00103   ok = ok && GetConfig().Get("TuneGainFactor",fTuneGainFactor);
00104   ok = ok && GetConfig().Get("UseCache",fUseCache);
00105   ok = ok && GetConfig().Get("UseLinearized",fUseLinearized);
00106   
00107   if(!ok) MSG("Calib",Msg::kWarning) << "PEGainAggCalScheme couldn't configure properly!" << endl;
00108 
00109   if(fDefaultRowQIE) delete fDefaultRowQIE; fDefaultRowQIE = 0;
00110   if(fDefaultRowVA ) delete fDefaultRowVA;  fDefaultRowVA= 0;
00111 
00112   // 50% error on gains:
00113   fDefaultRowQIE = new CalADCtoPEs(0,0,fDefaultGainQIE,fDefaultGainQIE*0.5, fDefaultWidthQIE, fDefaultWidthQIE*0.3);
00114   fDefaultRowVA  = new CalADCtoPEs(0,0,fDefaultGainVA ,fDefaultGainVA*0.5,  fDefaultWidthVA,  fDefaultWidthVA*0.3 );
00115 
00116   if(fTuneGainFactor!=1.0) 
00117     MSG("Calib",Msg::kWarning) << "-- WARNING -- TuneGainFactor is set to " << fTuneGainFactor
00118                                << "  This is for calibration study only and should not be used for analysis!"
00119                                << std::endl;
00120   
00121 }
00122 
00123 //......................................................................
00124 void PEGainAggCalScheme::PrintConfig( std::ostream& os ) const
00125 {
00126   if(fUseLinearized)
00127     os << "  Will use linearity-corrected tables for the ND" << endl;
00128   
00129   if(fGuessOtherSpots) 
00130     os << "  Will attempt to guess unknown gains from neighboring spots." << endl;
00131   if(fGuessOtherPixels) 
00132     os << "  Will attempt to guess unknown gains from neighboring pixels." << endl;
00133 
00134   os << "  Default Gain (VA)   = " << fDefaultGainVA <<  " ADCs " << endl;
00135   os << "  Default Gain (QIE)  = " << fDefaultGainQIE <<  " ADCs " << endl;
00136   os << "  Default Width (VA)  = " << fDefaultWidthVA <<  " ADCs " << endl;
00137   os << "  Default Width (QIE) = " << fDefaultWidthQIE <<  " ADCs " << endl;
00138 }
00139 
00140 
00141 
00142 
00143 //......................................................................
00144 FloatErr PEGainAggCalScheme::GetPhotoElectrons(FloatErr rawcharge, 
00145                                             const PlexStripEndId& seid) const
00146 {
00155 
00156   const CalADCtoPEs* row = GetRow(seid);
00157 
00158   return rawcharge / FloatErr(row->GetGain() * fTuneGainFactor,
00159                               row->GetGainErr() );
00160 }
00161 
00162 //......................................................................
00163 void    PEGainAggCalScheme::DecalGainAndWidth(FloatErr& gain, FloatErr& width,    
00164                                            const PlexStripEndId& seid) const
00165 {
00175 
00176   
00177   const CalADCtoPEs* row = GetRow(seid);
00178   
00179   gain.Set(row->GetGain() * fTuneGainFactor,
00180            row->GetGainErr());
00181   width = row->GetSPEWidth() * fTuneGainFactor;
00182   // Protect against bad values:
00183   if(width<=0) width = gain*(float)0.5; 
00184 }
00185 
00186 
00187 //......................................................................
00188 void    PEGainAggCalScheme::DecalGainAndWidth(FloatErr& gain, FloatErr& width,    
00189                                            const PlexPixelSpotId& psid) const
00190 {
00200 
00201   const CalADCtoPEs* row = GetRow(psid);
00202   
00203   gain.Set(row->GetGain() * fTuneGainFactor,
00204            row->GetGainErr());
00205   width = row->GetSPEWidth() * fTuneGainFactor;
00206   // Protect against bad values:
00207   if(width<=0) width = gain*(float)0.5; 
00208 }
00209 
00210 
00211 Bool_t PEGainAggCalScheme::RowIsGood(const CalADCtoPEs* row) const
00212 {
00213   if(!row) return false;
00214   if(row->GetGain() <= 0.0) return false;
00215   return true;
00216 }
00217 
00218 
00219 
00220 
00221 const CalADCtoPEs* PEGainAggCalScheme::GuessGainAndWidth( const PlexPixelSpotId& psid) const
00222 {                                               
00229 
00230   CalADCtoPEs* output = fDefaultRowVA;
00231   
00232   if(!psid.IsValid()) {
00233     MAXMSG("Calib",Msg::kWarning,20) << "Attempted to find gain on invalid pixel. Return VA defaults." << endl;
00234     return output;
00235     if(GetContext().GetDetector()==Detector::kNear)  return fDefaultRowQIE;
00236     else                                             return fDefaultRowVA;
00237   }
00238 
00239   if(fGuessOtherSpots) {
00240     // Find the average gain from other spots on this pixel.
00241 
00242     double gaintot=0;
00243     double widthtot=0;
00244     double pixtot=0;
00245 
00246     PlexHandle plex(GetContext());
00247     for(int spot=0;spot<65;spot++) {
00248       PlexPixelSpotId trialspot = psid;
00249       trialspot.SetSpot(spot);
00250       PlexStripEndId seid = plex.GetStripEndId(psid);
00251       if(seid.IsValid()) {
00252         const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00253         if(row) {
00254           pixtot+=1;
00255           gaintot+=row->GetGain();
00256           widthtot+=row->GetSPEWidth();
00257         }
00258       }
00259     }
00260     if(pixtot>0) {
00261       double gain = gaintot/pixtot;
00262       double width= widthtot/pixtot;
00263       output = new CalADCtoPEs(0,0,0, gain, gain*0.1, width); // Assume 10% error on gain.
00264       fCacheStorage.push_back(output);
00265       return output;
00266     }
00267 
00268   }
00269 
00270   if(fGuessOtherPixels) {
00271     // Find the average gain of the next and previous pixel numbers.
00272 
00273     double gaintot=0;
00274     double widthtot=0;
00275     double pixtot=0;
00276     PlexHandle plex(GetContext());
00277 
00278     for(int pixel=psid.GetPixel()-1; pixel<psid.GetPixel()+1; pixel+=2) {
00279       PlexPixelSpotId trialspot = psid;
00280       trialspot.SetPixel(pixel);
00281       PlexStripEndId seid = plex.GetStripEndId(psid);
00282       if(seid.IsValid()) {
00283         const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00284         if(row) {
00285           pixtot+=1;
00286           gaintot+=row->GetGain();
00287           widthtot+=row->GetSPEWidth();
00288         }
00289       }
00290     }
00291     if(pixtot>0) {
00292       double gain = gaintot/pixtot;
00293       double width= widthtot/pixtot;
00294       output = new CalADCtoPEs(0,0,0, gain, gain*0.2, width); // Assume 20% error on gain.
00295       fCacheStorage.push_back(output);
00296       return output;
00297     }
00298     
00299   }
00300   
00301 
00302   // Ok, that didn't work. Try returning the defaults.
00303   if(psid.GetElecType()==ElecType::kQIE) {
00304     return fDefaultRowQIE;
00305   }
00306   if(psid.GetElecType()==ElecType::kVA) {
00307     return fDefaultRowVA;
00308   }
00309 
00310   IncrementErrors(kPeCalibrator,kGeneralErr);
00311   MSG("Calib",Msg::kError) << "Unknown electronics type on pixel " << psid << endl;
00312   
00313   if(GetContext().GetDetector()==Detector::kNear)  return fDefaultRowQIE;
00314   return fDefaultRowVA;
00315 }
00316 
00317 
00318 const CalADCtoPEs* PEGainAggCalScheme::GetRow(const PlexStripEndId& seid) const
00319 {
00320   //
00321   // Look for the info in the cache, look for the gain in the DB,
00322   // or attempt to reconstruct the gain from the DB if it's not there.
00323   //
00324   
00325   if(!seid.IsValid()) {
00326     MAXMSG("Calib",Msg::kWarning,10) 
00327       << "PEGainAggCalScheme: Requested stripend " << seid.AsString() << " is invalid. \n";    
00328     IncrementErrors(kPeCalibrator,kBadInput,seid);    
00329     if(GetContext().GetDetector()==Detector::kNear) return fDefaultRowQIE;
00330     else                                                return fDefaultRowVA;
00331   } 
00332 
00333   if(fUseCache) {
00334     StripCache_t::iterator it = fStripCache.find(seid);
00335     if(it != fStripCache.end()) return it->second;
00336     
00337     // Look up the data. Cache it if it's good and return.
00338     PlexHandle plex(GetContext());
00339     PlexPixelSpotId psid = plex.GetPixelSpotId(seid);
00340   
00341     const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00342     if(RowIsGood(row)) {
00343       fStripCache[seid] = row;
00344       fSpotCache[psid] = row;
00345       return row;
00346     };
00347     
00348     // The DB data isn't good.
00349     if(fResPtr.GetNumRows()>0) {
00350       MAXMSG("Calib",Msg::kWarning,10) 
00351         << "PEGainAggCalScheme: No database row for StripEnd " << seid.AsString() << "\n";    
00352       IncrementErrors(kPeCalibrator,kMissingRow,seid);
00353     }
00354     
00355     
00356     // Fallback: make a guess.
00357     const CalADCtoPEs* newrow = GuessGainAndWidth(psid);
00358     fStripCache[seid] = newrow;
00359     fSpotCache[psid] = newrow;
00360     
00361     return newrow;
00362 
00363   } else {
00364     // NOT using the cache
00365 
00366     // Look up the data. Cache it if it's good and return.
00367     PlexHandle plex(GetContext());
00368     PlexPixelSpotId psid = plex.GetPixelSpotId(seid);
00369 
00370     const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00371     if(RowIsGood(row)) {
00372       return row;
00373     }    
00374     // The DB data isn't good.
00375     if(fResPtr.GetNumRows()>0) {
00376       MAXMSG("Calib",Msg::kWarning,10) 
00377         << "PEGainAggCalScheme: No database row for StripEnd " << seid.AsString() << "\n";    
00378     IncrementErrors(kPeCalibrator,kMissingRow,seid);
00379     }
00380     
00381     // Fallback: make a guess.
00382     const CalADCtoPEs* guess = GuessGainAndWidth(psid);
00383     return guess;
00384 
00385   }
00386 
00387   return NULL;
00388 }
00389 
00390 const CalADCtoPEs* PEGainAggCalScheme::GetRow(const PlexPixelSpotId& psid) const
00391 {
00392    //
00393   // Look for the info in the cache, look for the gain in the DB,
00394   // or attempt to reconstruct the gain from the DB if it's not there.
00395   //
00396   if(! psid.IsValid()) {
00397     //MAXMSG("Calib",Msg::kWarning,10) 
00398     //  << "PEGainAggCalScheme: Requested pixelspot " << psid.AsString() << " is invalid. \n";    
00399     IncrementErrors(kPeCalibrator,kBadInput,psid);    
00400     if(GetContext().GetDetector()==Detector::kNear) return fDefaultRowQIE;
00401     else                                            return fDefaultRowVA;
00402   }
00403 
00404   if(fUseCache) {
00405 
00406     SpotCache_t::iterator it = fSpotCache.find(psid);
00407     if(it != fSpotCache.end()) return it->second;
00408     
00409     // Look up the data. Cache it if it's good and return.
00410     PlexHandle plex(GetContext());
00411     PlexStripEndId seid = plex.GetStripEndId(psid);
00412     
00413     const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00414     if(RowIsGood(row)) {
00415       fSpotCache[psid] = row;
00416       fStripCache[seid] = row;
00417       return row;
00418     };
00419     
00420     // The DB data isn't good.
00421     if(fResPtr.GetNumRows()>0) {
00422       MAXMSG("Calib",Msg::kWarning,10) 
00423         << "PEGainAggCalScheme: No database row for StripEnd " << seid.AsString() << "\n";    
00424       IncrementErrors(kPeCalibrator,kMissingRow,seid);
00425     }
00426     
00427     const CalADCtoPEs* newrow = GuessGainAndWidth(psid);
00428     fStripCache[seid] = newrow;
00429     fSpotCache[psid] = newrow;
00430     
00431     return newrow;
00432   } else {
00433     // Look up the data. Cache it if it's good and return.
00434     PlexHandle plex(GetContext());
00435     PlexStripEndId seid = plex.GetStripEndId(psid);
00436     
00437     const CalADCtoPEs* row = fResPtr.GetRowByIndex(seid.BuildPlnStripEndKey());
00438     if(RowIsGood(row))  return row;
00439 
00440     // The DB data isn't good.
00441     if(fResPtr.GetNumRows()>0) {
00442       MAXMSG("Calib",Msg::kWarning,10) 
00443         << "PEGainAggCalScheme: No database row for StripEnd " << seid.AsString() << "\n";    
00444       IncrementErrors(kPeCalibrator,kMissingRow,seid);
00445     }
00446     
00447     const CalADCtoPEs* guess = GuessGainAndWidth(psid);    
00448     return guess;
00449   }
00450 
00451   return NULL;
00452 }

Generated on Mon Feb 15 11:07:18 2010 for loon by  doxygen 1.3.9.1