source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/dataManager.cc @ 481

Last change on this file since 481 was 481, checked in by garnier, 10 years ago

Suppression de methodes deprecated et de quelques warning de compilation

File size: 11.3 KB
RevLine 
[431]1#include <stdio.h>
2#include <fstream>
3
[27]4#include "dataManager.h"
[243]5#include "mathematicalConstants.h"
6#include "PhysicalConstants.h"
[431]7
8#include "GWt_pspaApplication.h"
9#include "GWt_console.h"
[455]10#include "GWt_ligneFaisceau.h" // FIXME to be removed !
[431]11
[347]12#include <boost/filesystem.hpp>
[27]13
[424]14#include "UAP/UAPUtilities.hpp"
15#include "AML/AMLReader.hpp"
16
[431]17#define BAVARD 0
18
[342]19dataManager::dataManager(PspaApplication* pspa) :
[359]20pspa_ (pspa)
[342]21{}
[305]22
[359]23dataManager::~dataManager()
[50]24{
[431]25  unsigned k;
26  for (k=0; k < jobList_.size();k++) {
27    if ( jobList_[k] != NULL ) delete jobList_[k];
28  }
[50]29}
[38]30
[431]31void dataManager::consoleMessage(string message)
32{
33  GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console"));
34  if (console) console->addConsoleMessage(message + "\n");
35  pspa_->processEvents();
[313]36}
[299]37
[118]38
[302]39void dataManager::initializeExecution()
[102]40{
[371]41  string workingDir = pspa_->getWorkingDir();
42  if (workingDir == "") {
43    return;
44  }
45  removeFile(workingDir + "parmdesz");
46  removeFile(workingDir + "parmin");
47  removeFile(workingDir + "parin.input0");
48  removeFile(workingDir + "transport.input");
49  removeFile(workingDir + "transport.output");
50  removeFile(workingDir + "generator.in");
51  removeFile(workingDir + "faisceau.ini");
52  removeFile(workingDir + "generator.output");
53  diagnosticBeam_.clear();
[386]54  indexElementToIndexDiag_.clear();
[455]55  for (unsigned int a=0; a< jobList_.size(); a++) {
56      jobList_[a]->clearSectionToExecute();
57  }
[105]58}
59
[305]60void dataManager::removeFile(string nameOfFile)
[105]61{
[431]62  ifstream fileExists;
63  fileExists.open(nameOfFile.c_str(), ios::in);
64  if (fileExists) {
65    fileExists.close();
66    remove(nameOfFile.c_str());
67  }
[112]68}
69
[313]70bool dataManager::executeAll()
[112]71{
[436]72#if BAVARD > 0
[431]73  cout << "***********************************" << endl;
74  cout << " dataManager::executeAll() " << endl << endl;
[436]75#endif
[431]76
[371]77  bool success = true;
78  string workingDir = pspa_->getWorkingDir();
[431]79 
[455]80
81  // Main loop !
[424]82  for(unsigned k = 0; k < jobList_.size(); k++) {
[455]83    sector* sector = jobList_[k];
84   
85    // for the moment, we put everything in one line without loops
86    for (unsigned int l=0; l <sector->getSectionsToExecute().size(); l++) {
87      sectionToExecute* section = sector->getSectionsToExecute()[l];
88      abstractSoftware* softw = section->getSoftware();
89     
90#if BAVARD > 0
[424]91    cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl;
[436]92#endif
[455]93     
94      //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " à " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl;
95     
[424]96    if (softw == NULL) {
[431]97      success = false;
98      consoleMessage("dataManager::executeAll : unknown software");
99      break;
100    }
[479]101      // END OF FIXME
102      success = softw->createInputFile(sector->getParticleBeam(),workingDir);
[431]103    if ( success ) {
104      success = softw->execute(workingDir);
[371]105      if ( success ) {
[431]106        success = softw->buildBeamAfterElements(workingDir);
[371]107      }
[431]108    }
109   
[480]110    if ( success  && (diagnosticBeam_.size() > 0)) {
[472]111      sector->setParticleBeam(&diagnosticBeam_.at(indexElementToIndexDiag_.back()));
[431]112      cout << " execute termine avec succes " << endl;
113    } else {
[472]114      sector->setParticleBeam(NULL);
[431]115      cout << " execute termine en ECHEC " << endl;
116    }
117    if ( !success ) break;
118   
[455]119#if BAVARD > 0
120      cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
121      cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
122      std::vector< abstractElement* > elements = section->getElements();
123      for (unsigned j = 0; j < elements.size(); j++) {
124        cout << "[" << j << "] " << elements[j]->getNomdElement().getExpandedName() << endl;
125      }
[436]126#endif
[455]127    } //l
[424]128  } //k
[472]129  // if ( currentBeam_deprecated_ ) {
[442]130  // string aml_file = workingDir + "/" + "faisceau_final" + ".aml";
[472]131  //   currentBeam_deprecated_->writeToAMLFile(aml_file);
[442]132  //   // TESTS
[472]133  //   currentBeam_deprecated_->readFromAMLFile(aml_file);
[442]134  // }
[455]135  cout << " dataManager::executeAll() " << endl;
[436]136
[371]137  return success;
[102]138}
[50]139
[347]140void dataManager::saveConfiguration(string folder, string nameOfFile)
[112]141{
[431]142  ofstream outfile;
[436]143  //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
[431]144  cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl;
145  cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl;
146  cout << " et qu'on puisse restaurer normalement" << endl;
147
148  string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; 
149  // make dir if not exist
150  boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
151 
152  outfile.open(name.c_str(), ios::out);
153  if (!outfile) {
154    cerr<<" ERROR opening output file for persistency "<<name<<endl;
155  }
156 
157  outfile << globParam_.FileOutputFlow();
[436]158
[455]159  // FIXME Francois: Comment sauver la configuration des section ?
160  for (unsigned int a=0; a< jobList_.size(); a++) {
161    outfile << jobList_[a]->getSectorParameters().FileOutputFlow();
162  }
163  // END
164 
[431]165  abstractElement* elPtr;
[455]166  for (unsigned k = 0; k < jobList_.size(); k++) {
167    sector* sector = jobList_[k];
168    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
169      sectionToExecute* section = sector->getSectionsToExecute()[l];
170      for (unsigned m = 0; m < section->getElements().size(); m++) {
171        elPtr = section->getElements()[m];
172        outfile << elPtr->FileOutputFlow();
173      }
[50]174    }
[455]175  }
[431]176  outfile.close();
[112]177}
178
[424]179// ecriture sur fichier AML
180void dataManager::writeToAMLFile(string fileName)
181{
182  UAPNode* uap_root = NULL;
183  uap_root = new UAPNode("UAP_root");
184  UAPNode* rep = uap_root->addChild("AML_representation");
[431]185 
[424]186  // root node in the hierarchy
187  UAPNode* lab = rep->addChild("laboratory");
188  lab->addAttribute("name","PSPA lab");
[431]189 
[436]190  // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077
[424]191  globParam_.InputRep(lab);
[431]192 
[424]193  // accelerator or section of accelerator
194  //UAPNode* acc = lab->addChild("machine");
195  //acc->addAttribute("name",fileName);
196
[436]197  //sectorParam_.InputRep(lab);
[431]198
[424]199  // sequence of elements
200  UAPNode* sect = lab->addChild("sector");
201  sect->addAttribute("name","a_sect");
202
203  abstractElement* elPtr;
[455]204  for (unsigned k = 0; k < jobList_.size(); k++) {
205    sector* sector = jobList_[k];
206    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
207      sectionToExecute* section = sector->getSectionsToExecute()[l];
208      for (unsigned m = 0; m < section->getElements().size(); m++) {
209        elPtr = section->getElements()[m];
210        elPtr->InputRep(sect);
211      }
212    }
[424]213  }
214  cout << "!Create the AML file ---------------------------" << endl;
215  AMLReader reader;
216  string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml";
217  reader.AMLRepToAMLFile (uap_root, aml_file);
218}
219
[431]220bool dataManager::restoreElements(string inputFileName)
[112]221{
[431]222  cout << "***********************************" << endl;
223  cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl;
224
225  ifstream infile;
226  string name = inputFileName;
227  infile.open(name.c_str(), ios::in);
228  if (!infile) {
229    cerr << " error opening input stream " << name << endl;
230    return false;
231  }
232  else cout << " successful opening input stream : " << name << endl;
233 
[436]234  //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
[431]235 
[474]236  // Clear the BeamLine
237  jobList_.clear();
238
239  // FIXME : For test purpose !!!
240  sector* mySect = addNewSector();
241 
[431]242  nomdElements::typedElement elementType;
243  string elementLabel;
[479]244  abstractElement* nouveau = NULL;
[431]245  while (infile >> elementLabel) {
[436]246    if (elementLabel == string("globals") ) {
247      globParam_.raz();
248      globParam_.FileInput(infile);
249    } else if (elementLabel == string("sectors") ) {
[455]250      // FIXME Francois !!!!!!!
251//      sectorParam_.raz();
252//      sectorParam_.FileInput(infile);
253      // END
[112]254    } else {
[431]255      elementType = nomdElements::getTypeFromLabel(elementLabel);
[479]256      nouveau = mySect->addElementAfter(elementType,nouveau);
[431]257      if ( nouveau == NULL ) {
258        cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl;
259        return false;
260      } else {
261        nouveau->FileInput(infile);
262      }
[112]263    }
[431]264  }// while
265 
[436]266#if BAVARD > 1
[474]267/*  unsigned k;
[431]268  for(k = 0; k < getBeamLineSize(); k++) {
[469]269    abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k);
[431]270    cout << "recupere " << ptr->getLabel() << endl;
271  }
[474]272*/
[431]273#endif
274
275  infile.close();
276  return true;
[305]277}
[50]278
[305]279particleBeam* dataManager::getDiagnosticBeam(unsigned index)
[179]280{
[386]281  if (index >= indexElementToIndexDiag_.size() ) {
282    return NULL;
283  } else {
284    int indDiag = indexElementToIndexDiag_.at(index);
285    return &diagnosticBeam_.at(indDiag);
286  }
[179]287}
[149]288
[455]289particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
[386]290{
[455]291  // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label
292  unsigned int number = 0;
293  abstractElement* elPtr;
294  for (unsigned k = 0; k < jobList_.size(); k++) {
295    sector* sector = jobList_[k];
296    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
297      sectionToExecute* section = sector->getSectionsToExecute()[l];
298      for (unsigned m = 0; m < section->getElements().size(); m++) {
299        elPtr = section->getElements()[m];
300        if (elPtr->getLabel() == elementLabel) {
301          return getDiagnosticBeam(number);
302        }
303        number ++;
304      }
305    }
306  }
307  return NULL;
[386]308}
[236]309
[359]310
[342]311// on ne tient pas compte des elements "snapshot" presents dans la beamLine
[381]312void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
[342]313{
314  double longueur = 0.0;
315  double valeur = 0.0;
316  xcor.clear();
317  ycor.clear();
[381]318  titre.clear();
319  titre = " rms enveloppe ";
320  legendx.clear();
321  legendx = " z (m)";
[342]322  if ( type == "x" ) {
323    unsigned i = 0;
[455]324    cout << " dataManager::donneesRmsEnveloppe " << endl;
[368]325    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
[455]326    for (unsigned k = 0; k < jobList_.size(); k++) {
327      sector* sector = jobList_[k];
328      for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
329        sectionToExecute* section = sector->getSectionsToExecute()[l];
330        for (unsigned m = 0; m < section->getElements().size(); m++) {
331          abstractElement* elPtr = section->getElements()[m];
332          //     if(elPtr->getNomdElement().getElementType() == snapshot) continue;
333          //      if(elPtr->getNomdElement().getElementType() == fit) continue;
334          particleBeam* beamToDraw = getDiagnosticBeam(i);
335          if ( !beamToDraw->momentRepresentationOk() ) {
336            beamToDraw->buildMomentRepresentation();
337          }
338         
339          longueur += elPtr->getLenghtOfElement();
340          valeur = beamToDraw->getXmaxRms();
341          cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveloppe : " << valeur << endl;
342          xcor.push_back(0.01*longueur);  // on passe en metres
343          ycor.push_back(valeur);
344          i++;
345        }
[316]346      }
[342]347    }
[381]348    legendy.clear();
349    legendy = " x (cm) ";
[342]350  }
[316]351}
[342]352
353
[401]354
[449]355sector* dataManager::addNewSector() {
356  std::stringstream str;
357  str << getSectors().size()+1;
358  std::string result;
359  str >> result;
360 
[455]361  sector* sect = new sector(this, std::string("sector ") + result);
362  jobList_.push_back(sect);
[449]363  return sect;
364}
365
Note: See TracBrowser for help on using the repository browser.