#include "AsapPilotImpl.hh" #include "Pilot.h" #include "Element.h" #include "UTCTime.hh" #include "CdbTree.h" #include "RootTreeImpl.h" #include "RootTree.hh" #include "DbTreeCorba.hh" #include "AsapException.hh" #include "Rasnik.h" #include "SurveySensor.hh" #include "Handle.hh" #include "AsapIOV.hh" #include "AsapResultHeader.hh" #include "AsapCorrection.hh" #include "AsapSensorSummary.hh" #include "AsapSensorMeasurement.hh" #include "AsapSensorResidual.hh" #include "AsapLog.hh" #include "AsapConfSection.hh" #include "ScopedLock.hh" #include "TFile.h" #include #include #include using namespace std; namespace asap { namespace pilot { namespace facade { namespace corba { using namespace asap::jdbtree::facade::corba; using namespace asap::servers::corba; AsapPilotFactoryImpl* AsapPilotFactoryImpl::pilotFactory = NULL; AsapPilotFactoryImpl::AsapPilotFactoryImpl() { } AsapPilotFactoryImpl::~AsapPilotFactoryImpl() { AsapLog::printf(AsapLog::DEBUG, "AsapPilotFactory deleted"); } AsapPilot_ptr AsapPilotFactoryImpl::createPilot(const char* fname) { // Synchronize this method ScopedLock lock; try { AsapLog::printf(AsapLog::DEBUG, "Creating pilot ... "); Pilot* p = new Pilot(fname); AsapPilotImpl *pilotImpl = new AsapPilotImpl(p); PortableServer::ServantBase_var servant = pilotImpl; AsapPilot_var pilot = pilotImpl->_this(); AsapLog::printf(AsapLog::DEBUG, "End of Pilot Creation...send back a Corba reference"); return pilot._retn(); } catch (AsapException e) { throw AsapServerException(e.what()); } } AsapPilotFactoryImpl* AsapPilotFactoryImpl::getInstance() { if(!pilotFactory) { pilotFactory = new AsapPilotFactoryImpl; } return pilotFactory; } AsapPilotImpl::AsapPilotImpl(Pilot* pilot) : m_p(pilot) { } AsapPilotImpl::~AsapPilotImpl() { AsapLog::printf(AsapLog::DEBUG, "AsapPilot deleted"); } void AsapPilotImpl::destroy() { try { AsapLog::printf(AsapLog::DEBUG, "Destroying Pilot Corba Proxy on Server : "); // get the poa PortableServer::POA_var poa = this->_default_POA(); PortableServer::ObjectId_var oid = poa->servant_to_id(this); poa->deactivate_object(oid.in()); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setElementConfiguration(const char* id, CORBA::Boolean activate) { // Synchronize this method ScopedLock lock; try { AsapConfiguration& config = *(this->m_p->getConfiguration()); string key = "activate__"; config["configelementvar"].setInt(key+id, (activate ? 1 : 0)); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setOpticallineConfiguration(const char* id, CORBA::Boolean activate, CORBA::Boolean absolute) { // Synchronize this method ScopedLock lock; try { AsapConfiguration& config = *(this->m_p->getConfiguration()); string section = "configoptlinevar"; string key1 = "activate__"; string key2 = "absolute__"; config[section+string(id,0,3)].setInt(key1+id, (activate ? 1 : 0)); config[section+string(id,0,3)].setInt(key2+id, (absolute ? 1 : 0)); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setAmdb(const char* amdbfile) { // Synchronize this method ScopedLock lock; try { AsapConfiguration& config = *(this->m_p->getConfiguration()); config.setAmdb("amdb", amdbfile); config.setAmdb("amdb_nom", amdbfile); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setSensorOffset(const char* id, CORBA::Double x, CORBA::Double y, CORBA::Double mag, CORBA::Double tz, CORBA::Double err_x, CORBA::Double err_y, CORBA::Double err_mag, CORBA::Double err_tz) { // Synchronize this method ScopedLock lock; try { AsapConfiguration& config = *(this->m_p->getConfiguration()); AsapConfSection& sensor_offset = config["sensor_offset"]; sensor_offset.setFloat(sensor_offset.itemName("x", id), x); sensor_offset.setFloat(sensor_offset.itemName("y", id), y); sensor_offset.setFloat(sensor_offset.itemName("mag", id), mag); sensor_offset.setFloat(sensor_offset.itemName("tz", id), tz); sensor_offset.setFloat(sensor_offset.itemName("sx", id), err_x); sensor_offset.setFloat(sensor_offset.itemName("sy", id), err_y); sensor_offset.setFloat(sensor_offset.itemName("smag", id), err_mag); sensor_offset.setFloat(sensor_offset.itemName("stz", id), err_tz); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setConditions(const Conditions& cond) { // Synchronize this method ScopedLock lock; try { AsapLog::printf(AsapLog::DEBUG, "AsapPilotImpl::setConditions_old %ld %ld %ld %s", cond.since, cond.till, cond.iov_id, (const char*)cond.tag); // create a new Conditions object to send to Pilot Handle< ::Conditions > asap_cond = new ::Conditions(); asap_cond -> setSince(UTCTime(cond.since)); asap_cond -> setTill(UTCTime(cond.till)); asap_cond -> setIovId(cond.iov_id); asap_cond -> setTag ((const char*)cond.tag); PortableServer::POA_var poa = this->_default_POA(); TTree* ictree = NULL; PortableServer::ServantBase_var servantIcTree; if(!CORBA::is_nil(cond.icaras_tree)) { servantIcTree = poa->reference_to_servant(cond.icaras_tree); RootTreeImpl *rootIcTree = dynamic_cast(servantIcTree.in()); ictree = rootIcTree->getTree(); } asap_cond -> setIcarasData(new CdbTree(ictree)); TTree* temptree = NULL; PortableServer::ServantBase_var servantTempTree; if(!CORBA::is_nil(cond.temp_tree)) { servantTempTree = poa->reference_to_servant(cond.temp_tree); RootTreeImpl *rootTempTree = dynamic_cast(servantTempTree.in()); temptree = rootTempTree->getTree(); } asap_cond->setTemperatureData(new CdbTree(temptree)); // send the conditions to Pilot this->m_p->setConditions(asap_cond); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::getOutputData(IOVList_out outIOVs) { // Synchronize this method ScopedLock lock; try { Handle outputs = m_p->getOutputHandler(); //printOutput(outputs); unsigned nIov = outputs->getEntries(); outIOVs = new IOVList(); outIOVs->length(nIov); for(unsigned i = 0; i < nIov; i++) { convertIOV(*outputs->getIOV(i), outIOVs[i]); } } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::writeOutputData (const char* rootfilename) { // Synchronize this method ScopedLock lock; try { AsapLog::printf(AsapLog::DEBUG, "AsapPilotImpl::writeOutputData"); Handle out = m_p->getOutputHandler(); TTree* outtree = out -> getCurrentTree (); TFile* fout = TFile::Open(rootfilename, "RECREATE"); if (!fout) { AsapLog::printf(AsapLog::ERROR, "AsapPilotImpl::writeOutputData: cannot open output file %s", rootfilename); return; } outtree->CloneTree()->Write(); fout->Close (); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::clearOutputData () { // Synchronize this method ScopedLock lock; try { AsapLog::printf(AsapLog::DEBUG, "AsapPilotImpl::clearOutputData"); m_p->setOutputHandler(new CdbAsap()); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::reco(const FitParameters& thefit) { // Synchronize this method ScopedLock lock; try { AsapLog::printf(AsapLog::NOTICE, "AsapPilotImpl::reco: running ASAP reconstruction on IOV %lld, tag %s, since %s, till %s", this->m_p->getConditions()->getIovId(), this->m_p->getConditions()->getTag().Data(), UTCTime(this->m_p->getConditions()->getSince()).getString().c_str(), UTCTime(this->m_p->getConditions()->getTill()).getString().c_str()); // Set the output data container Handle local_out = new CdbAsap (); this->m_p->setOutputHandler (local_out); this->m_p->reco(thefit.iterations); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::printOutput (Handle out) { try { int ncorr = out -> getEntries (); for (int icorr = 0; icorr < ncorr; icorr ++) { cout << "***" << endl << "*** IOV number " << icorr << endl << "***" << endl; const AsapIOV* iov = out->getIOV(icorr); const AsapResultHeader *h = iov -> getResultHeader(0); int ncorr = h -> getNCorrections (); cout << "since = " << UTCTime(iov -> getSinceT ()).getString () << endl << "till = " << UTCTime(iov -> getTillT ()).getString () << endl << "Chi2 = " << h -> getChi2Norm () << endl << "NCorrections = " << ncorr << endl; for (int i=0; i getCorrection (i); cout << "---------------------------------------" << endl << "Chamber = " << c -> getAsapID () << endl << "s = " << c -> getS () << endl << "z = " << c -> getZ () << endl << "t = " << c -> getT () << endl << "thetaS = " << c -> getThetaS () << endl << "thetaZ = " << c -> getThetaZ () << endl << "thetaT = " << c -> getThetaT () << endl; } } } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertIOV(const AsapIOV& asapIOV, AlignIOV& corbaIOV) { try { corbaIOV.iov_id = asapIOV.getIovId(); corbaIOV.since = asapIOV.getSinceT(); corbaIOV.till = asapIOV.getTillT(); assert(asapIOV.getNResultHeaders() == 1); const AsapResultHeader& asapResult = *asapIOV.getResultHeader(0); corbaIOV.results.length(1); convertResult(asapResult, corbaIOV.results[0]); int nCorr = asapResult.getNCorrections(); corbaIOV.corrections.length(nCorr); for(int i = 0; i < nCorr; i++) { convertCorrection(*asapResult.getCorrection(i), corbaIOV.corrections[i]); } int nSensorSummary = asapResult.getNSensorSummary(); corbaIOV.sensorsummary.length(nSensorSummary); for(int i = 0; i < nSensorSummary; i++) { convertSensorSummary(*asapResult.getSensorSummary(i), corbaIOV.sensorsummary[i]); } int nSensorResidual = asapResult.getNSensorResiduals(); corbaIOV.sensorresidual.length(nSensorResidual); for(int i = 0; i < nSensorResidual; i++) { convertSensorResidual(*asapResult.getSensorResidual(i), corbaIOV.sensorresidual[i]); } } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertResult(const AsapResultHeader& asapResult, AlignResult& corbaResult) { try { corbaResult.chi2 = asapResult.getChi2(); corbaResult.chi2norm = asapResult.getChi2Norm(); corbaResult.nDOF = asapResult.getNDOF(); corbaResult.icarasSince = asapResult.getSinceT(); corbaResult.icarasTill = asapResult.getTillT(); corbaResult.cycleStart = asapResult.getCycleStart(); corbaResult.cycleEnd = asapResult.getCycleEnd(); corbaResult.nIteration = asapResult.getNIteration(); corbaResult.nMaxIteration = asapResult.getNMaxIteration(); corbaResult.exitReason = asapResult.getExitReason(); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertCorrection(const AsapCorrection& asapCorr, AlignCorrection& corbaCorr) { try { corbaCorr.asap_id = asapCorr.getAsapID(); corbaCorr.asap_type = asapCorr.getAsapType(); corbaCorr.amdb_typ = asapCorr.getAmdbTyp(); corbaCorr.amdb_jff = asapCorr.getAmdbJff(); corbaCorr.amdb_jzz = asapCorr.getAmdbJzz(); corbaCorr.amdb_job = asapCorr.getAmdbJob(); corbaCorr.s = asapCorr.getS(); corbaCorr.z = asapCorr.getZ(); corbaCorr.t = asapCorr.getT(); corbaCorr.thetas = asapCorr.getThetaS(); corbaCorr.thetaz = asapCorr.getThetaZ(); corbaCorr.thetat = asapCorr.getThetaT(); corbaCorr.torw = asapCorr.getTORW(); corbaCorr.sagro = asapCorr.getSAGRO(); corbaCorr.saghv = asapCorr.getSAGHV(); corbaCorr.dwro = asapCorr.getDWRO(); corbaCorr.dwhv = asapCorr.getDWHV(); corbaCorr.saglbv = asapCorr.getSAGLBV(); corbaCorr.saglbh = asapCorr.getSAGLBH(); corbaCorr.dwac = asapCorr.getDWAC(); corbaCorr.temp = asapCorr.getTEMP(); corbaCorr.xatlas = asapCorr.getXAtlas(); corbaCorr.yatlas = asapCorr.getYAtlas(); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertSensorSummary(const AsapSensorSummary& asapSensorSummary, AlignSensorSummary& corbaSensorSummary) { try { corbaSensorSummary.sensorType = asapSensorSummary.getSensorType(); corbaSensorSummary.nSensorTotal = asapSensorSummary.getNSensorsTotal(); corbaSensorSummary.nSensorFailure = asapSensorSummary.getNSensorsFailure(); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertSensorResidual(const AsapSensorResidual& asapSensorResidual, AlignSensorResidual& corbaSensorResidual) { try { corbaSensorResidual.channelName = asapSensorResidual.getChannelName(); corbaSensorResidual.type = asapSensorResidual.getType(); corbaSensorResidual.nmeas = asapSensorResidual.getNMeas(); corbaSensorResidual.distCL = asapSensorResidual.getDistCL(); corbaSensorResidual.distLM = asapSensorResidual.getDistLM(); int nMeas = asapSensorResidual.getNMeas(); corbaSensorResidual.meas.length(nMeas); for(int i = 0; i < nMeas; i++) { convertSensorMeasurement(asapSensorResidual.at(i), corbaSensorResidual.meas[i]); } } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::convertSensorMeasurement(const AsapSensorMeasurement& asapSensorMeasurement, AlignSensorMeasurement& corbaSensorMeasurement) { try { corbaSensorMeasurement.id = asapSensorMeasurement.id(); corbaSensorMeasurement.meas = asapSensorMeasurement.meas(); corbaSensorMeasurement.model = asapSensorMeasurement.model(); corbaSensorMeasurement.error = asapSensorMeasurement.error(); corbaSensorMeasurement.measraw = asapSensorMeasurement.measraw(); corbaSensorMeasurement.measrawerror = asapSensorMeasurement.measrawerror(); corbaSensorMeasurement.measrawnpoints = asapSensorMeasurement.measrawnpoints(); } catch (AsapException e) { throw AsapServerException(e.what()); } } } } } }