#include "AsapPilotImpl.hh" #include "Pilot.h" #include "Element.hh" #include "UTCTime.hh" #include "CdbTree.h" #include "RootTreeImpl.h" #include "RootTree.hh" #include "DbTreeCorba.hh" #include "AsapException.hh" #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 "AsapConfSection.hh" #include "ScopedLock.hh" #include "LogStream.hh" #include "TFile.h" #include #include #include #include using namespace std; using namespace asap; namespace asap { namespace pilot { namespace facade { namespace corba { using namespace asap::jdbtree::facade::corba; using namespace asap::servers::corba; AsapPilotFactoryImpl* AsapPilotFactoryImpl::pilotFactory = NULL; asap::Log AsapPilotFactoryImpl::log("AsapPilotFactoryImpl"); asap::Log AsapPilotImpl::log("AsapPilotImpl"); AsapPilotFactoryImpl::AsapPilotFactoryImpl() { } AsapPilotFactoryImpl::~AsapPilotFactoryImpl() { log(DEBUG).printf("AsapPilotFactory deleted\n"); } AsapPilot_ptr AsapPilotFactoryImpl::createPilot(const char* fname) { // Synchronize this method ScopedLock lock; try { log(DEBUG).printf("Creating pilot ... \n"); Pilot* p = new Pilot(fname); AsapPilotImpl *pilotImpl = new AsapPilotImpl(p); PortableServer::ServantBase_var servant = pilotImpl; AsapPilot_var pilot = pilotImpl->_this(); log(DEBUG).printf("End of Pilot Creation...send back a Corba reference\n"); 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() { log(DEBUG).printf("AsapPilot deleted\n"); } void AsapPilotImpl::destroy() { try { log(DEBUG).printf("Destroying Pilot Corba Proxy on Server : \n"); // 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()); int irow = config["configelementvar"].getRow(id); if (irow == -1) throw AsapServerException(Form("Invalid element id %s", id)); config["configelementvar"].setInt("activate", irow, (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"; section += string(id,0,3); // hack int irow = config[section].getRow(id); if (irow == -1) throw AsapServerException(Form("Invalid sensor id %s", id)); config[section].setInt("activate", irow, (activate ? 1 : 0)); config[section].setInt("absolute", irow, (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"]; int irow = sensor_offset.getRow(id); if (irow == -1) { irow = sensor_offset.getInt("nrow"); sensor_offset.setString("name", irow, id); sensor_offset.setRowName(irow, id); sensor_offset.setInt("nrow", irow+1); } sensor_offset.setFloat("x", irow, x); sensor_offset.setFloat("y", irow, y); sensor_offset.setFloat("mag", irow, mag); sensor_offset.setFloat("tz", irow, tz); sensor_offset.setFloat("sx", irow, err_x); sensor_offset.setFloat("sy", irow, err_y); sensor_offset.setFloat("smag", irow, err_mag); sensor_offset.setFloat("stz", irow, err_tz); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::setConditions(const Conditions& cond) { // Synchronize this method ScopedLock lock; try { log(DEBUG).printf("setConditions_old %lld %lld %lld %s\n", (long long)cond.since, (long long)cond.till, (long long)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 { log(DEBUG).printf("AsapPilotImpl::writeOutputData\n"); Handle out = m_p->getOutputHandler(); TTree* outtree = out -> getCurrentTree (); TFile* fout = TFile::Open(rootfilename, "RECREATE"); if (!fout) { log(ERR).printf("writeOutputData: cannot open output file %s\n", rootfilename); return; } outtree->CloneTree()->Write(); fout->Close (); } catch (AsapException e) { throw AsapServerException(e.what()); } } void AsapPilotImpl::clearOutputData () { // Synchronize this method ScopedLock lock; try { log(DEBUG).printf("AsapPilotImpl::clearOutputData\n"); m_p->setOutputHandler(new CdbAsap()); } catch (AsapException e) { throw AsapServerException(e.what()); } } CORBA::Boolean AsapPilotImpl::reco(const FitParameters& thefit) { // Synchronize this method ScopedLock lock; try { log(INFO).printf("reco: running ASAP reconstruction on IOV %lld, tag %s, since %s, till %s\n", 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); return 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.eg = asapCorr.getEG(); corbaCorr.tw = asapCorr.getTW(); corbaCorr.sp = asapCorr.getSP(); corbaCorr.sn = asapCorr.getSN(); corbaCorr.bp = asapCorr.getBP(); corbaCorr.bn = asapCorr.getBN(); corbaCorr.bz = asapCorr.getBZ(); corbaCorr.ep = asapCorr.getEP(); corbaCorr.en = asapCorr.getEN(); corbaCorr.tr = asapCorr.getTR(); corbaCorr.pg = asapCorr.getPG(); 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()); } } } } } }