#include #include #include "dataManager.h" #include "mathematicalConstants.h" #include "PhysicalConstants.h" #include "GWt_pspaApplication.h" #include "GWt_console.h" #include "GWt_ligneFaisceau.h" // FIXME to be removed ! #include #include "UAP/UAPUtilities.hpp" #include "AML/AMLReader.hpp" #define BAVARD 0 dataManager::dataManager(PspaApplication* pspa) : pspa_ (pspa) {} dataManager::~dataManager() { unsigned k; for (k=0; k < jobList_.size();k++) { if ( jobList_[k] != NULL ) delete jobList_[k]; } } void dataManager::consoleMessage(string message) { GWt_console* console = static_cast (wApp->findWidget ("console")); if (console) console->addConsoleMessage(message + "\n"); pspa_->processEvents(); } void dataManager::initializeExecution() { string workingDir = pspa_->getWorkingDir(); if (workingDir == "") { return; } removeFile(workingDir + "parmdesz"); removeFile(workingDir + "parmin"); removeFile(workingDir + "parin.input0"); removeFile(workingDir + "transport.input"); removeFile(workingDir + "transport.output"); removeFile(workingDir + "generator.in"); removeFile(workingDir + "faisceau.ini"); removeFile(workingDir + "generator.output"); diagnosticBeam_.clear(); indexElementToIndexDiag_.clear(); for (unsigned int a=0; a< jobList_.size(); a++) { jobList_[a]->clearSectionToExecute(); } } void dataManager::removeFile(string nameOfFile) { ifstream fileExists; fileExists.open(nameOfFile.c_str(), ios::in); if (fileExists) { fileExists.close(); remove(nameOfFile.c_str()); } } bool dataManager::executeAll() { #if BAVARD > 0 cout << "***********************************" << endl; cout << " dataManager::executeAll() " << endl << endl; #endif bool success = true; string workingDir = pspa_->getWorkingDir(); // Main loop ! cout << " dataManager::executeAll #jobList_= " << jobList_.size() << endl; for(unsigned k = 0; k < jobList_.size(); k++) { sector* sector = jobList_[k]; cout << " dataManager::executeAll sector name= " << sector->getName() << endl; cout << " dataManager::executeAll #sectionsToExecute= " << sector->getSectionsToExecute().size() << endl; // for the moment, we put everything in one line without loops for(unsigned int l = 0; l < sector->getSectionsToExecute().size(); l++) { sectionToExecute* section = sector->getSectionsToExecute()[l]; abstractSoftware* softw = section->getSoftware(); cout << " execution du SOFWARE " << softw->getName() << endl; cout << " adresse section " << section << endl; cout << " adresse faisceau " << sector->getParticleBeam() << endl; if (softw == NULL) { success = false; consoleMessage("dataManager::executeAll : unknown software"); break; } success = softw->createInputFile(sector->getParticleBeam(),workingDir); if ( success ) { success = softw->execute(workingDir); if ( success ) { success = softw->buildBeamAfterElements(workingDir); } } cout << " dataManager::executeAll success = " << success << " y-a-ty du faisceau : " << diagnosticBeam_.size() << endl; if ( success && (diagnosticBeam_.size() > 0)) { sector->setParticleBeam(&diagnosticBeam_.at(indexElementToIndexDiag_.back())); cout << " dataManager::executeAll termine avec succes " << endl; } else { sector->setParticleBeam(NULL); cout << " dataManager::executeAll termine en ECHEC " << endl; } if ( !success ) break; #if BAVARD > 0 cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; std::vector< abstractElement* > elements = section->getElements(); for (unsigned j = 0; j < elements.size(); j++) { cout << "[" << j << "] " << elements[j]->getNomdElement().getExpandedName() << endl; } #endif } //l } //k // if ( currentBeam_deprecated_ ) { // string aml_file = workingDir + "/" + "faisceau_final" + ".aml"; // currentBeam_deprecated_->writeToAMLFile(aml_file); // // TESTS // currentBeam_deprecated_->readFromAMLFile(aml_file); // } return success; } void dataManager::saveConfiguration(string folder, string nameOfFile) { ofstream outfile; //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save"; cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl; cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl; cout << " et qu'on puisse restaurer normalement" << endl; string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; // make dir if not exist boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/"); outfile.open(name.c_str(), ios::out); if (!outfile) { cerr<<" ERROR opening output file for persistency "<getSectorParameters().FileOutputFlow(); } // END abstractElement* elPtr; for (unsigned k = 0; k < jobList_.size(); k++) { sector* sector = jobList_[k]; for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { sectionToExecute* section = sector->getSectionsToExecute()[l]; for (unsigned m = 0; m < section->getElements().size(); m++) { elPtr = section->getElements()[m]; outfile << elPtr->FileOutputFlow(); } } } outfile.close(); } // ecriture sur fichier AML void dataManager::writeToAMLFile(string fileName) { UAPNode* uap_root = NULL; uap_root = new UAPNode("UAP_root"); UAPNode* rep = uap_root->addChild("AML_representation"); // root node in the hierarchy UAPNode* lab = rep->addChild("laboratory"); lab->addAttribute("name","PSPA lab"); // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077 globParam_.InputRep(lab); // accelerator or section of accelerator //UAPNode* acc = lab->addChild("machine"); //acc->addAttribute("name",fileName); //sectorParam_.InputRep(lab); // sequence of elements UAPNode* sect = lab->addChild("sector"); sect->addAttribute("name","a_sect"); abstractElement* elPtr; for (unsigned k = 0; k < jobList_.size(); k++) { sector* sector = jobList_[k]; for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { sectionToExecute* section = sector->getSectionsToExecute()[l]; for (unsigned m = 0; m < section->getElements().size(); m++) { elPtr = section->getElements()[m]; elPtr->InputRep(sect); } } } cout << "!Create the AML file ---------------------------" << endl; AMLReader reader; string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml"; reader.AMLRepToAMLFile (uap_root, aml_file); } bool dataManager::restoreElements(string inputFileName) { cout << "***********************************" << endl; cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl; ifstream infile; string name = inputFileName; infile.open(name.c_str(), ios::in); if (!infile) { cerr << " error opening input stream " << name << endl; return false; } else cout << " successful opening input stream : " << name << endl; //NOTE:: je ne sais pas si les "secteurs" seront conservés, aussi pour ne pas trop changer les fichiers je lis les données après "globals" dans la boucle // Clear the BeamLine jobList_.clear(); // FIXME : For test purpose !!! sector* mySect = addNewSector(); nomdElements::typedElement elementType; string elementLabel; abstractElement* nouveau = NULL; while (infile >> elementLabel) { if (elementLabel == string("globals") ) { globParam_.raz(); globParam_.FileInput(infile); } else if (elementLabel == string("sectors") ) { // FIXME Francois !!!!!!! // sectorParam_.raz(); // sectorParam_.FileInput(infile); // END } else { elementType = nomdElements::getTypeFromLabel(elementLabel); nouveau = mySect->addElementAfter(elementType,nouveau); if ( nouveau == NULL ) { cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl; return false; } else { nouveau->FileInput(infile); } } }// while #if BAVARD > 1 /* unsigned k; for(k = 0; k < getBeamLineSize(); k++) { abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k); cout << "recupere " << ptr->getLabel() << endl; } */ #endif infile.close(); return true; } particleBeam* dataManager::getDiagnosticBeam(unsigned index) { if (index >= indexElementToIndexDiag_.size() ) { return NULL; } else { int indDiag = indexElementToIndexDiag_.at(index); return &diagnosticBeam_.at(indDiag); } } particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel) { // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label unsigned int number = 0; abstractElement* elPtr; for (unsigned k = 0; k < jobList_.size(); k++) { sector* sector = jobList_[k]; for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { sectionToExecute* section = sector->getSectionsToExecute()[l]; for (unsigned m = 0; m < section->getElements().size(); m++) { elPtr = section->getElements()[m]; if (elPtr->getLabel() == elementLabel) { return getDiagnosticBeam(number); } number ++; } } } return NULL; } // on ne tient pas compte des elements "snapshot" presents dans la beamLine void dataManager::donneesRmsEnveloppe(string type,vector& xcor,vector& ycor, string& titre, string& legendx, string& legendy) { double longueur = 0.0; double valeur = 0.0; xcor.clear(); ycor.clear(); titre.clear(); titre = " rms enveloppe "; legendx.clear(); legendx = " z (m)"; if ( type == "x" ) { unsigned i = 0; cout << " dataManager::donneesRmsEnveloppe " << endl; // for (unsigned k = 0; k < getBeamLineSize(); k++) { for (unsigned k = 0; k < jobList_.size(); k++) { sector* sector = jobList_[k]; for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { sectionToExecute* section = sector->getSectionsToExecute()[l]; for (unsigned m = 0; m < section->getElements().size(); m++) { abstractElement* elPtr = section->getElements()[m]; // if(elPtr->getNomdElement().getElementType() == snapshot) continue; // if(elPtr->getNomdElement().getElementType() == fit) continue; particleBeam* beamToDraw = getDiagnosticBeam(i); if ( !beamToDraw->momentRepresentationOk() ) { beamToDraw->buildMomentRepresentation(); } longueur += elPtr->getLenghtOfElement(); valeur = beamToDraw->getXmaxRms(); cout << " dataManager::donneesRmsEnveloppe index = " << k << " longueur = " << longueur << " enveloppe : " << valeur << endl; xcor.push_back(0.01*longueur); // on passe en metres ycor.push_back(valeur); i++; } } } legendy.clear(); legendy = " x (cm) "; } } sector* dataManager::addNewSector() { std::stringstream str; str << getSectors().size()+1; std::string result; str >> result; sector* sect = new sector(this, std::string("sector ") + result); jobList_.push_back(sect); return sect; }