SetupNavigator Class Reference

#include <SetupNavigator.h>

List of all members.

Public Member Functions

void Close ()
void SetCommandLineArgs (const ARGUMENTS &args)
 Closes the database if this is a calibration run.
int GetRunNumber () const
std::string GetBank (const IDs::channel &) const
void CacheCalibDB ()
 Caches the information stored in the calib DB.
double GetPedestal (const IDs::channel &channel) const
 Gets the pedestal from the SQLite database.
double GetNoise (const IDs::channel &channel) const
 Gets the error on the pedestal from the SQLite database.
double GetCoarseTimeOffset (IDs::source src) const
std::pair< double, double > GetEnergyCalibrationConstants (const IDs::channel &) const
 Gets the energy calibration constants.
double GetAdcToEnergyGain (const IDs::channel &ch) const
double GetAdcToEnergyConstant (const IDs::channel &ch) const
void SetPedestalAndNoise (const IDs::channel &channel, double pedestal, double noise)
void SetCoarseTimeOffset (const IDs::source &src, double dt)

Static Public Member Functions

static SetupNavigatorInstance ()

Private Types

typedef std::pair< double, double > EnergyCalibRow_t
 The map that stores energy calibration constants as <Gain,Pedestal> pairs.

Private Member Functions

 SetupNavigator ()
 ~SetupNavigator ()
 Closes the database if open or saves.
bool ReadPedestalAndNoiseValues ()
 Reads the pedestal and pedestal error values.
bool ReadCoarseTimeOffsetValues ()
 Reads the gross time offset values.
std::vector< std::string > GetCoarseTimeOffsetColumns ()
 Read in gross time offset columns determining what TAP generators we're ready for.
bool ReadEnergyCalibrationConstants ()
 Read in energy calibration constants.
void OutputCalibCSV ()
bool IsCalibRun () const

Private Attributes

ARGUMENTS fCommandLineArgs
const std::string fSQLiteFilename
 The SQLite filename, the SQLite server object and the current table name.
TSQLiteServer * fServer
const std::string fPedestalNoiseTableName
const std::string fCoarseTimeOffsetTableName
const std::string fEnergyCalibrationConstantsTableName

Static Private Attributes

static SetupNavigatorfThis = NULL
static std::map< IDs::channel,
double > 
fPedestalValues
 The map that stores the pedestal values that we get from the SQLite database.
static std::map< IDs::channel,
double > 
fNoiseValues
 The map that stores the pedestal error values (i.e. noise) that we get from the SQLite database.
static std::map< IDs::source,
double > 
fCoarseTimeOffset
 The map that stores the gross time offset values from SQLite database.
static std::map< IDs::channel,
EnergyCalibRow_t
fEnergyCalibrationConstants

Detailed Description

Definition at line 21 of file SetupNavigator.h.


Member Typedef Documentation

typedef std::pair<double,double> SetupNavigator::EnergyCalibRow_t [private]

The map that stores energy calibration constants as <Gain,Pedestal> pairs.

Definition at line 100 of file SetupNavigator.h.


Constructor & Destructor Documentation

SetupNavigator::SetupNavigator (  )  [private]

Definition at line 37 of file SetupNavigator.cpp.

References fServer.

Referenced by Instance().

00037                                :
00038   fCommandLineArgs(),
00039   fSQLiteFilename("sqlite://calibration.db"), fServer(new TSQLiteServer(fSQLiteFilename.c_str())),
00040   fPedestalNoiseTableName("PedestalAndNoise"), fCoarseTimeOffsetTableName("CoarseTimeOffset"),
00041   fEnergyCalibrationConstantsTableName("Energy") {
00042   if (fServer->IsZombie()) {
00043     std::cout << "SetupNavigator: ERROR: Couldn't connect to SQLite database." << std::endl;
00044     throw Except::NoCalibDB();
00045   }
00046 }

SetupNavigator::~SetupNavigator (  )  [private]

Closes the database if open or saves.

Definition at line 49 of file SetupNavigator.cpp.

00049                                 {
00050 }


Member Function Documentation

void SetupNavigator::CacheCalibDB (  ) 

Caches the information stored in the calib DB.

Definition at line 73 of file SetupNavigator.cpp.

References fCoarseTimeOffsetTableName, fEnergyCalibrationConstantsTableName, fPedestalNoiseTableName, fServer, GetRunNumber(), IsCalibRun(), ReadCoarseTimeOffsetValues(), ReadEnergyCalibrationConstants(), and ReadPedestalAndNoiseValues().

Referenced by main().

00073                                   {
00074   // Cache all the variables we have in the database
00075   // First the pedestals and noises
00076   if (!fServer->HasTable(fPedestalNoiseTableName.c_str())) {
00077     std::cout << "SetupNavigator: ERROR: Table " << fPedestalNoiseTableName << " does not exist." << std::endl;
00078     if (!IsCalibRun()) throw Except::MissingTable(fPedestalNoiseTableName.c_str());
00079   } else {
00080     if (!ReadPedestalAndNoiseValues()) {
00081       std::cout << "SetupNavigator: ERROR: Table " << fPedestalNoiseTableName 
00082                 << " contains no calib data for this run ("<<GetRunNumber()<<")" << std::endl;
00083     if (!IsCalibRun()) throw Except::UncalibratedRun();
00084     }
00085   }
00086   if (!fServer->HasTable(fCoarseTimeOffsetTableName.c_str())) {
00087     std::cout << "SetupNavigator: ERROR: Table " << fCoarseTimeOffsetTableName << " does not exist." << std::endl;
00088     if(!IsCalibRun()) throw Except::MissingTable(fPedestalNoiseTableName.c_str());
00089   } else {
00090     if (!ReadCoarseTimeOffsetValues()) {
00091       std::cout << "SetupNavigator: ERROR: Table " << fCoarseTimeOffsetTableName 
00092                 << " contains no calib data for this run ("<<GetRunNumber()<<")" << std::endl;
00093     if (!IsCalibRun()) throw Except::UncalibratedRun();
00094     }
00095   }
00096   if (!fServer->HasTable(fEnergyCalibrationConstantsTableName.c_str())) {
00097     std::cout << "SetupNavigator: Error: Table " << fEnergyCalibrationConstantsTableName << " does not exist." << std::endl;
00098     if (!IsCalibRun()) throw Except::MissingTable(fEnergyCalibrationConstantsTableName.c_str());
00099   } else {
00100     if (!ReadEnergyCalibrationConstants()) {
00101       std::cout << "SetupNavigator: ERROR: Table " << fEnergyCalibrationConstantsTableName
00102                 << " contains no calib data for this run ("<<GetRunNumber()<<")" << std::endl;
00103       if (!IsCalibRun()) throw Except::UncalibratedRun();
00104     }
00105   }
00106 }

void SetupNavigator::Close (  ) 

Definition at line 53 of file SetupNavigator.cpp.

References fServer, fThis, IsCalibRun(), and OutputCalibCSV().

Referenced by Finish().

00053                            {
00054   if(IsCalibRun()) 
00055     OutputCalibCSV();
00056   fServer->Close();
00057   if (fThis)
00058     delete fThis;
00059   fThis = NULL;
00060 }

double SetupNavigator::GetAdcToEnergyConstant ( const IDs::channel ch  )  const [inline]
double SetupNavigator::GetAdcToEnergyGain ( const IDs::channel ch  )  const [inline]
std::string SetupNavigator::GetBank ( const IDs::channel src  )  const

Definition at line 63 of file SetupNavigator.cpp.

References modules::parser::iequals(), Instance(), and IDs::channel::str().

Referenced by TVAnalysedPulseGenerator::SetChannel().

00063                                                            {
00064   std::map<std::string, std::string>* bankDetNameMap=&TSetupData::Instance()->fBankToDetectorMap;
00065   for(std::map<std::string, std::string>::iterator it = bankDetNameMap->begin();
00066       it != bankDetNameMap->end(); ++it){
00067     if(modules::parser::iequals(it->second , src.str())) return it->first;
00068   }
00069   std::cout << "Invalid detector name: " << src.str() << std::endl;
00070   throw Except::InvalidDetector();
00071 }

double SetupNavigator::GetCoarseTimeOffset ( IDs::source  src  )  const

Definition at line 262 of file SetupNavigator.cpp.

References fCoarseTimeOffset, IDs::source::Generator(), IDs::source::matches(), and IDs::source::str().

00262                                                                   {
00263 // Massive hack to remove config strings that specify other options.  Assumes
00264 // the timing option is placed first for the generator
00265  std::string conf=source.Generator().Config();
00266  unsigned curly_br=conf.find('}');
00267 if(curly_br!=std::string::npos){ source.Generator().Config(conf.substr(0,curly_br+1));}
00268  
00269  return source.matches(IDs::channel("muSc")) ? 0. : alcap::at<Except::InvalidDetector>(fCoarseTimeOffset,source,source.str().c_str());
00270  }

std::vector< std::string > SetupNavigator::GetCoarseTimeOffsetColumns (  )  [private]

Read in gross time offset columns determining what TAP generators we're ready for.

Definition at line 164 of file SetupNavigator.cpp.

References fCoarseTimeOffsetTableName, and fServer.

Referenced by ReadCoarseTimeOffsetValues().

00164                                                                 {
00165   std::vector<std::string> cols;
00166   std::stringstream cmd;
00167   cmd << "PRAGMA table_info(" << fCoarseTimeOffsetTableName << ")";
00168   TSQLResult* res = fServer->Query(cmd.str().c_str());
00169   TSQLRow* row;
00170   while ((row = res->Next())) {
00171     if (row->GetField(1) != std::string("run") && row->GetField(1) != std::string("channel"))
00172       cols.push_back(std::string(row->GetField(1)));
00173     delete row;
00174   }
00175   delete res;
00176   return cols;
00177 }

SetupNavigator::EnergyCalibRow_t SetupNavigator::GetEnergyCalibrationConstants ( const IDs::channel ch  )  const

Gets the energy calibration constants.

Definition at line 259 of file SetupNavigator.cpp.

References fEnergyCalibrationConstants, and IDs::channel::str().

Referenced by GeSpectrum::GeSpectrum().

00259                                                                                                        {
00260   return alcap::at<Except::InvalidDetector>(fEnergyCalibrationConstants, ch, ch.str().c_str());
00261 }

double SetupNavigator::GetNoise ( const IDs::channel channel  )  const

Gets the error on the pedestal from the SQLite database.

Definition at line 252 of file SetupNavigator.cpp.

References fNoiseValues, and IDs::channel::str().

Referenced by functions::InterpolatePulse(), and PulseCandidateFinder::SetChannel().

00252                                                                {
00253   return alcap::at<Except::InvalidDetector>(fNoiseValues,channel,channel.str().c_str());
00254 }

double SetupNavigator::GetPedestal ( const IDs::channel channel  )  const

Gets the pedestal from the SQLite database.

Definition at line 255 of file SetupNavigator.cpp.

References fPedestalValues, and IDs::channel::str().

Referenced by PulseCandidateFinder::SetChannel().

00255                                                                   { 
00256   return alcap::at<Except::InvalidDetector>(fPedestalValues,channel,channel.str().c_str());
00257 }

int SetupNavigator::GetRunNumber (  )  const [inline]
SetupNavigator * SetupNavigator::Instance (  )  [inline, static]
bool SetupNavigator::IsCalibRun (  )  const [inline, private]

Definition at line 75 of file SetupNavigator.h.

Referenced by CacheCalibDB(), Close(), SetCoarseTimeOffset(), and SetPedestalAndNoise().

00075 {return fCommandLineArgs.calib;}

void SetupNavigator::OutputCalibCSV (  )  [private]

Definition at line 200 of file SetupNavigator.cpp.

References fCoarseTimeOffset, fCoarseTimeOffsetTableName, fNoiseValues, fPedestalNoiseTableName, fPedestalValues, and GetRunNumber().

Referenced by Close().

00200                                     {
00201   char r[6];
00202   sprintf(r, "%05d", GetRunNumber());
00203 
00204   std::ofstream fPN((std::string("calib.run") + r + "." + fPedestalNoiseTableName + ".csv").c_str());
00205   fPN << "run,channel,pedestal,noise" << std::endl;
00206   for (std::map<IDs::channel, double>::const_iterator i = fPedestalValues.begin(); i != fPedestalValues.end(); ++i)
00207     fPN << GetRunNumber() << "," << i->first.str() << ","
00208         << i->second << "," << fNoiseValues.at(i->first) << std::endl;
00209 
00210   std::ofstream fTO((std::string("calib.run") + r + "." + fCoarseTimeOffsetTableName + ".csv").c_str());
00211   std::set<IDs::generator> gens;
00212   std::set<IDs::channel> chns;
00213   for (std::map<IDs::source, double>::const_iterator i = fCoarseTimeOffset.begin(); i != fCoarseTimeOffset.end(); ++i) {
00214     gens.insert(i->first.Generator());
00215     chns.insert(i->first.Channel());
00216   }
00217   fTO << "run,channel";
00218   for (std::set<IDs::generator>::const_iterator i = gens.begin(); i != gens.end(); ++i)
00219     fTO << "," << i->str();
00220   fTO << std::endl;
00221   for (std::set<IDs::channel>::const_iterator i = chns.begin(); i != chns.end(); ++i) {
00222     fTO << GetRunNumber() << "," << i->str();
00223     for (std::set<IDs::generator>::const_iterator j = gens.begin(); j != gens.end(); ++j) {
00224       fTO << ",";
00225       if (fCoarseTimeOffset.count(IDs::source(*i, *j)))
00226         fTO << fCoarseTimeOffset.at(IDs::source(*i, *j));
00227     }
00228     fTO << std::endl;
00229   }
00230 }

bool SetupNavigator::ReadCoarseTimeOffsetValues (  )  [private]

Reads the gross time offset values.

Definition at line 138 of file SetupNavigator.cpp.

References DEBUG_VALUE, fCoarseTimeOffset, fCoarseTimeOffsetTableName, IDs::field_separator, fServer, GetCoarseTimeOffsetColumns(), and GetRunNumber().

Referenced by CacheCalibDB().

00138                                                 {
00139   // The values that we will read in
00140   const std::vector<std::string> table = GetCoarseTimeOffsetColumns();
00141 for(unsigned i=0;i<table.size();++i) DEBUG_VALUE(i, table[i]);
00142 
00143   std::stringstream query;
00144   query << "SELECT channel";
00145   for (unsigned int i = 0; i < table.size(); ++i)
00146     query << ",\"" << table[i] << '"';
00147   query << " FROM " << fCoarseTimeOffsetTableName << " WHERE run==" << GetRunNumber();
00148   TSQLResult* res = fServer->Query(query.str().c_str());
00149 
00150   TSQLRow* row;
00151   while ((row = res->Next())) {
00152     for (unsigned int i = 0; i < table.size(); ++i) {
00153       std::stringstream srcstr;
00154       srcstr << row->GetField(0) << IDs::field_separator << table[i];
00155       if(row->GetField(1+i))
00156              fCoarseTimeOffset[IDs::source(srcstr.str())] = atof(row->GetField(1 + i));
00157     }
00158     delete row;
00159   }
00160   delete res;
00161   return !fCoarseTimeOffset.empty();
00162 }

bool SetupNavigator::ReadEnergyCalibrationConstants (  )  [private]

Read in energy calibration constants.

Definition at line 180 of file SetupNavigator.cpp.

References fEnergyCalibrationConstants, fEnergyCalibrationConstantsTableName, fServer, and GetRunNumber().

Referenced by CacheCalibDB().

00180                                                     {
00181   std::stringstream cmd;
00182   // // Ben:  In the future we will want to include generator parameters in the
00183   // // table to be able to calibrate multiple generators simultaneously:
00184   // cmd << "SELECT channel,gen_params,gain,offset FROM " << fEnergyCalibrationConstantsTableName 
00185   cmd << "SELECT channel,gain,offset FROM " << fEnergyCalibrationConstantsTableName
00186       << " WHERE run==" << GetRunNumber();
00187   TSQLResult* res = fServer->Query(cmd.str().c_str());
00188   TSQLRow* row;
00189   while ((row = res->Next())) {
00190     fEnergyCalibrationConstants[IDs::channel(row->GetField(0))] =
00191       EnergyCalibRow_t(atof(row->GetField(1)), atof(row->GetField(2)));
00192     delete row;
00193   }
00194   delete res;
00195   //  return !fEnergyCalibrationConstants.empty();
00196   return true; // Right now there's no reason this should be filled for anything
00197 }

bool SetupNavigator::ReadPedestalAndNoiseValues (  )  [private]

Reads the pedestal and pedestal error values.

Definition at line 108 of file SetupNavigator.cpp.

References definitions::DefaultValue, fNoiseValues, fPedestalNoiseTableName, fPedestalValues, fServer, and GetRunNumber().

Referenced by CacheCalibDB().

00108                                                 {
00109   // The values that we will read in
00110   double pedestal=definitions::DefaultValue;
00111   double noise=definitions::DefaultValue;
00112   std::string channelname;
00113 
00114   std::stringstream query;
00115   int run_number = GetRunNumber(); // get this run number (Note that if we don't have a catalogue of pedestals and noises for each run then we will want to change this)
00116 
00117   query << "SELECT * FROM " << fPedestalNoiseTableName << " WHERE run=" << run_number << ";"; // get all the pedestals and noises
00118   TSQLiteResult* result = (TSQLiteResult*) fServer->Query(query.str().c_str());  // get the result of this query
00119   query.str(""); // clear the stringstream after use
00120   
00121   TSQLiteRow* row = (TSQLiteRow*) result->Next(); // get the first row
00122   while (row != NULL) {
00123     channelname = row->GetField(1);
00124     pedestal = atof(row->GetField(2));
00125     noise = atof(row->GetField(3));
00126     
00127     fPedestalValues[channelname] = pedestal;
00128     fNoiseValues[channelname] = noise;
00129     delete row;
00130     row = (TSQLiteRow*) result->Next(); // get the next row
00131   }
00132   delete result; // user has to delete the result
00133 
00134   return !fPedestalValues.empty();
00135 }

void SetupNavigator::SetCoarseTimeOffset ( const IDs::source src,
double  dt 
)

Definition at line 242 of file SetupNavigator.cpp.

References IDs::source::Channel(), fCoarseTimeOffset, IDs::source::Generator(), IsCalibRun(), and StripTimeShiftConfigFromString().

Referenced by PlotTAP_TDiff::AfterLastEntry(), and CalcCoarseTimeOffset::AfterLastEntry().

00242                                                                         {
00243   if(!IsCalibRun()) {
00244     std::cout << "SetupNavigator: Warning: Request to edit coarse time offsets "
00245                  "when not flagged as calibration. Not setting." << std::endl;
00246     return;
00247   }
00248   // The excess code here is to strip {no_time_shift=*} from the generator config string
00249   fCoarseTimeOffset[IDs::source(src.Channel(), IDs::generator(StripTimeShiftConfigFromString(src.Generator().str())))] = dt;
00250 }

void SetupNavigator::SetCommandLineArgs ( const ARGUMENTS args  )  [inline]

Closes the database if this is a calibration run.

Definition at line 33 of file SetupNavigator.h.

Referenced by main().

00033 {fCommandLineArgs = args;}

void SetupNavigator::SetPedestalAndNoise ( const IDs::channel channel,
double  pedestal,
double  noise 
)

Definition at line 232 of file SetupNavigator.cpp.

References fNoiseValues, fPedestalValues, and IsCalibRun().

Referenced by PlotTPI_PedestalAndNoise::AfterLastEntry().

00232                                                                                       {
00233   if(!IsCalibRun()) {
00234     std::cout << "SetupNavigator: Warning: Request to edit pedestals and "
00235                  "noises when not flagged as calibration. Not setting." << std::endl;
00236     return;
00237   }
00238   fPedestalValues[chn] = ped;
00239   fNoiseValues[chn] = nse;
00240 }


Member Data Documentation

std::map< IDs::source, double > SetupNavigator::fCoarseTimeOffset [static, private]

The map that stores the gross time offset values from SQLite database.

Definition at line 97 of file SetupNavigator.h.

Referenced by GetCoarseTimeOffset(), OutputCalibCSV(), ReadCoarseTimeOffsetValues(), and SetCoarseTimeOffset().

const std::string SetupNavigator::fCoarseTimeOffsetTableName [private]

Definition at line 79 of file SetupNavigator.h.

Definition at line 87 of file SetupNavigator.h.

Referenced by CacheCalibDB(), and ReadEnergyCalibrationConstants().

std::map< IDs::channel, double > SetupNavigator::fNoiseValues [static, private]

The map that stores the pedestal error values (i.e. noise) that we get from the SQLite database.

Definition at line 94 of file SetupNavigator.h.

Referenced by GetNoise(), OutputCalibCSV(), ReadPedestalAndNoiseValues(), and SetPedestalAndNoise().

const std::string SetupNavigator::fPedestalNoiseTableName [private]

Definition at line 85 of file SetupNavigator.h.

Referenced by CacheCalibDB(), OutputCalibCSV(), and ReadPedestalAndNoiseValues().

std::map< IDs::channel, double > SetupNavigator::fPedestalValues [static, private]

The map that stores the pedestal values that we get from the SQLite database.

Definition at line 91 of file SetupNavigator.h.

Referenced by GetPedestal(), OutputCalibCSV(), ReadPedestalAndNoiseValues(), and SetPedestalAndNoise().

TSQLiteServer* SetupNavigator::fServer [private]
const std::string SetupNavigator::fSQLiteFilename [private]

The SQLite filename, the SQLite server object and the current table name.

Definition at line 83 of file SetupNavigator.h.

SetupNavigator * SetupNavigator::fThis = NULL [static, private]

Definition at line 78 of file SetupNavigator.h.

Referenced by Close(), and Instance().


The documentation for this class was generated from the following files:

Generated on 15 Jun 2016 for AlcapDAQ by  doxygen 1.6.1