| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

RunDataSvc.cc

Go to the documentation of this file.
00001 #include "RunDataSvc.h"
00002 
00003 #include "GaudiKernel/GaudiException.h"
00004 #include "GaudiKernel/MsgStream.h"
00005 #include "Event/RunHeader.h"
00006 #include "Event/RunData.h"
00007 #include "RootIOSvc/RootIOAddress.h"
00008 #include "RootIOSvc/IRootIOSvc.h"
00009 #include "GaudiKernel/IConversionSvc.h"
00010 #include "PerRunData/PerRunHeader.h"
00011 #include "RootIOSvc/RootIOTypedCnv.h"
00012 #include "Conventions/RunType.h"
00013 
00014 RunDataSvc::RunDataSvc(const std::string& name,
00015                              ISvcLocator* svc) :
00016   Service(name,
00017           svc),
00018   m_fileIsInitialized(false)
00019 {
00020   declareProperty("ReadFromFile",m_readFromFile=false,
00021                   "Load the run data from the current input file?");
00022   declareProperty("ReadFromDatabase",m_readFromDatabase=true,
00023                   "Check the database for run data?");
00024   declareProperty("RunDataLocation",
00025                   m_runDataLocation = DayaBay::RunHeaderLocation::Default,
00026                   "Path for run data if reading from file");
00027   declareProperty("SimRunNumber",m_simRunNumber = 0,
00028                   "Pass NuWa command-line run number to this service");
00029   declareProperty("SimRunType",m_simRunType = "Unknown",
00030                   "Set the simulation run type by name");
00031   declareProperty("SimStartTime",m_simStartTime = 0,
00032                   "Pass NuWa command-line start time to this service");
00033   declareProperty("SimCalibSources",m_simCalibSources,
00034                   "Active calibration sources in the simulation");
00035   declareProperty("SimCalibZPosition",m_simCalibZPosition,
00036                   "Calibration source Z position for movable AD sources");
00037   declareProperty("SimLedFrequency",m_simLedFrequency,
00038                   "Calibration LED pulsing frequency");
00039 }
00040 
00041 StatusCode RunDataSvc::queryInterface(const InterfaceID& id,
00042                                       void** interface) {
00043   if (IRunDataSvc::interfaceID().versionMatch(id)) {
00044     // If good enough match return this object.
00045     *interface = (IRunDataSvc*)this;
00046     addRef();
00047   } else {
00048     // Interface is not directly available: try out a base class.
00049     return Service::queryInterface(id,
00050                                    interface);
00051   }
00052 
00053   return StatusCode::SUCCESS;
00054 }
00055 
00056 StatusCode RunDataSvc::initialize() {
00057   StatusCode status = Service::initialize();
00058   if (status.isFailure()) {
00059     throw GaudiException("Could not initialize super class",
00060                          name(),
00061                          status);
00062   }
00063   return StatusCode::SUCCESS;
00064 }
00065 
00066 StatusCode RunDataSvc::finalize() {
00067   // Clear run data list
00068   DayaBay::RunHeader::RunDataContainer::iterator it, done = m_runDataList.end();
00069   for (it = m_runDataList.begin(); it != done; ++it) {
00070     // boost::pool somehow owns the run data objects, so don't delete
00071     //delete *it;
00072   }
00073   m_runDataList.clear();
00074   return Service::finalize();
00075 }
00076 
00077 const DayaBay::RunData* RunDataSvc::runData(const ServiceMode& svcMode) {
00078   // Return the Run Data based on the context
00079   // If task == 1, make new simulated Run Data if needed.
00080 
00081   MsgStream log(msgSvc(), "RunDataSvc");
00082   StatusCode sc = StatusCode::SUCCESS;
00083   // Catch new simulation, and make new run data if needed
00084   if(svcMode.task() == 1) return this->simRunData();
00085 
00086   // Check input sources if needed
00087   if( m_readFromDatabase ){
00088     sc = this->readDataFromDatabase(svcMode);
00089     if(!sc.isSuccess()) return 0;
00090   }
00091   if( m_readFromFile ){
00092     sc = this->readDataFromFile();
00093     if(!sc.isSuccess()) return 0;
00094   }
00095 
00096   DayaBay::RunData* runData = 0;
00097   int nRuns = m_runDataList.size();
00098   TimeStamp currentTime = svcMode.context().GetTimeStamp();
00099   DayaBay::Detector currentDet(svcMode.context().GetSite(),
00100                                svcMode.context().GetDetId());
00101   // Find correct run from current list of runs, based on the context
00102   for(int runIdx=0; runIdx<nRuns; runIdx++){
00103     DayaBay::RunData* currentRun = m_runDataList[runIdx];
00104     log << MSG::DEBUG << "Checking run data for run number " 
00105         << currentRun->runNumber() << endreq;
00106     if(currentRun->hasDetector(currentDet)
00107        && currentRun->startTime() <= currentTime 
00108        && (currentRun->endTime() >= currentTime
00109            || currentRun->endTime().IsNull())){
00110       // Run fits this context
00111       runData = currentRun;
00112     }
00113   }
00114   return runData;
00115 }
00116 
00117 StatusCode RunDataSvc::setRunData(const DayaBay::RunData& runData){
00118   // Add or update the Run Data for this run
00119   int nRuns = m_runDataList.size();
00120   bool updateRun = false;
00121   for(int runIdx=0; runIdx<nRuns; runIdx++){
00122     if( m_runDataList[runIdx]->runNumber() == runData.runNumber() ){
00123       // Found run in current list; update
00124       *(m_runDataList[runIdx]) = runData;
00125       updateRun = true;
00126       break;
00127     }
00128   }
00129   // Catch new run
00130   if( !updateRun ) m_runDataList.push_back(new DayaBay::RunData(runData));
00131   return StatusCode::SUCCESS;
00132 }
00133 
00134 const std::vector<DayaBay::RunData*>& RunDataSvc::cachedRunData(){
00135   // Return the current internal list of run data.
00136   // Use this for Database / File storage of run data
00137   if(m_readFromFile && !m_fileIsInitialized){
00138     StatusCode sc = this->readDataFromFile();
00139     sc.ignore();
00140   }
00141   return m_runDataList;
00142 }
00143 
00144 StatusCode RunDataSvc::readDataFromDatabase(const ServiceMode& /*svcMode*/){
00145   // Read the run data from the DBI Database
00146   // FIXME: ADD code here
00147   MsgStream log(msgSvc(), "RunDataSvc");
00148   log << MSG::WARNING << "readDataFromDatabase: Loading data from DBI is not yet implemented." << endreq;
00149   return StatusCode::SUCCESS;
00150 }
00151 
00152 StatusCode RunDataSvc::readDataFromFile(){
00153   // Read run data from current input file
00154 
00155   // If file already read, don't read again
00156   if( m_fileIsInitialized ) return StatusCode::SUCCESS;
00157 
00158   MsgStream log(msgSvc(), "RunDataSvc");
00159   IService* isvc = 0; 
00160   StatusCode sc = this->service("RootIOCnvSvc",isvc,true);
00161   if(sc.isFailure()){
00162     log << MSG::ERROR << "Failed to get RootIOCnvSvc as IService" << endreq;
00163     return sc;
00164   }
00165   IRootIOSvc* m_rioSvc = 0;
00166   sc = isvc->queryInterface(IRootIOSvc::interfaceID(), (void**)&m_rioSvc);
00167   if (sc.isFailure()) {
00168     log << MSG::ERROR << "Conversion service RootIOCnvSvc"
00169         << " does not implement IRootIOCnvSvc" << endreq;
00170     return sc;
00171   }
00172   IConversionSvc* convSvc = 0;
00173   sc = isvc->queryInterface(IConversionSvc::interfaceID(), (void**)&convSvc);
00174   if (sc.isFailure()) {
00175     log << MSG::ERROR << "Conversion service RootIOCnvSvc"
00176         << " does not implement IConversionSvc" << endreq;
00177     return sc;
00178   }
00179   IRootIOSvc::InputStreamMap& ism = m_rioSvc->inputStreams();
00180   IRootIOSvc::InputStreamMap::iterator it, done = ism.end();
00181   for (it = ism.begin(); it != done; ++it) {
00182     log << MSG::DEBUG << "checking input stream: " << it->first << endreq;
00183     if( it->first == m_runDataLocation ){
00184       // Get Persistent RunHeader from file
00185       RootInputStream* ris = it->second;
00186       ris->setEntry(0);
00187       log << MSG::DEBUG << "Return status of read: " << ris->read() << "\t" 
00188           << ris->obj() << endreq;
00189       PerRunHeader* perRunHeader = (PerRunHeader*)(ris->obj());
00190       if(!perRunHeader){
00191         log << MSG::ERROR << "Failed to get persistent RunHeader" << endreq;
00192         return StatusCode::FAILURE;
00193       }
00194       log << MSG::DEBUG << "Got persistent run data for "
00195           << perRunHeader->runDataList.size() << " runs" << endreq;
00196       // Convert Persistent RunHeader to Transient
00197       DayaBay::RunHeader runHeader;
00198       IConverter* converter = convSvc->converter(runHeader.classID());
00199       if (!converter) {
00200         log << MSG::ERROR << "Failed to get RunHeader converter" << endreq;
00201         return StatusCode::FAILURE;
00202       }
00203       RootIOTypedCnv<PerRunHeader, DayaBay::RunHeader>* runHdrCnv = 0;
00204       runHdrCnv = dynamic_cast< RootIOTypedCnv<PerRunHeader, DayaBay::RunHeader>* >(converter);
00205       if (!runHdrCnv) {
00206         log << MSG::ERROR << "Failed to cast RunHeader converter" << endreq;
00207         return StatusCode::FAILURE;
00208       }
00209       sc = runHdrCnv->PerToTran(*perRunHeader, runHeader);
00210       if (sc.isFailure()) {
00211         log << MSG::ERROR << "Failed to translate persistent RunHeader" 
00212             << endreq;
00213         return sc;
00214       }
00215       const DayaBay::RunHeader::RunDataContainer& runDataList 
00216         = runHeader.runDataList();
00217       int nRuns = runDataList.size();
00218       log << MSG::DEBUG << "Got run data for " << nRuns << " runs" << endreq;
00219       for(int runIdx=0; runIdx < nRuns; runIdx++){
00220         // Add each run data to the current list
00221         m_runDataList.push_back(new DayaBay::RunData(*(runDataList[runIdx])));
00222       }
00223     }
00224   }
00225   m_fileIsInitialized = true;
00226   return StatusCode::SUCCESS;
00227 }
00228 
00229 const DayaBay::RunData* RunDataSvc::simRunData() {
00230   // Return the specified simulation Run Data.
00231   // If it doesn't exist, create it.
00232   MsgStream log(msgSvc(), "RunDataSvc");
00233   int nRuns = m_runDataList.size();
00234   // Find correct run from current list of runs, based on the sim run number
00235   for(int runIdx=0; runIdx<nRuns; runIdx++){
00236     DayaBay::RunData* currentRun = m_runDataList[runIdx];
00237     log << MSG::DEBUG << "Checking run data for run number " 
00238         << currentRun->runNumber() << endreq;
00239     if(currentRun->runNumber() == m_simRunNumber){
00240       // Found the simulation run, return it
00241       return currentRun;
00242     }
00243   }
00244 
00245   // Make a new run data, using the properties of RunDataSvc.  These
00246   // are automatically set by the nuwa.py command line options --run
00247   // and --time.
00248   DayaBay::RunData* runData = new DayaBay::RunData();
00249   runData->setRunNumber(m_simRunNumber);
00250   runData->setRunType( DayaBay::RunType::FromString(m_simRunType.c_str()) );
00251   runData->setStartTime(TimeStamp(m_simStartTime));
00252   // Set the simulated sources
00253   DayaBay::RunData::CalibSources calibSources;
00254   int nSources = m_simCalibSources.size();
00255   for(int srcIdx = 0; srcIdx<nSources; srcIdx++){
00256     std::string srcName = m_simCalibSources[srcIdx];
00257     DayaBay::CalibSourceId sourceId( srcName );
00258     DayaBay::CalibSource source;
00259     source.setId( sourceId );
00260     // Set AD Z position for movable sources
00261     std::map<std::string, double>::iterator zIter = 
00262       m_simCalibZPosition.find( srcName );
00263     if( zIter != m_simCalibZPosition.end() ) 
00264       source.setAdZPosition( zIter->second );
00265     // Set Frequency for LED sources
00266     std::map<std::string, double>::iterator freqIter = 
00267       m_simLedFrequency.find( srcName );
00268     if( freqIter != m_simLedFrequency.end() ) 
00269       source.setLedFrequency( freqIter->second );
00270     // Add to source list
00271     calibSources.push_back( source );
00272   }
00273   runData->setCalibSources( calibSources );
00274   m_runDataList.push_back( runData );
00275   return runData;
00276 }
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:22:43 2011 for RunDataSvc by doxygen 1.4.7