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

PEGainCalScheme.cxx

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

Generated on Sat Nov 7 01:27:03 2009 for loon by  doxygen 1.3.9.1