softwareBeta.cc 35.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

#include "softwareBeta.h"
#include "dataManager.h"

#include "mathematicalConstants.h"
#include "PhysicalConstants.h"
#include "mixedTools.h"
#include "INIReader.h"

#include <iomanip>      // std::setw

softwareBeta::softwareBeta() : abstractSoftware()
{
  INIReader reader("../../pspa_config.ini");
  debug = reader.GetInteger("Debugging", "debug_mode", -1);
  
  nameOfSoftware_ = new nomDeLogiciel("beta");
}

softwareBeta::softwareBeta(computingBlock *cmpb,dataManager *dt) : abstractSoftware(cmpb,dt)
{
  INIReader reader("../../pspa_config.ini");
  debug = reader.GetInteger("Debugging", "debug_mode", -1);

  nameOfSoftware_ = new nomDeLogiciel("beta"); 
}

bool softwareBeta::createInputFile(particleBeam *beamBefore)
{
  if(debug > 0) cout << "softwareBeta::createInputFile()\n";

  // beam
  unsigned firstIndex = 0;
  abstractElement *elPtr= getComputingBlock()->getFirstElement();
  string eType= elPtr->getGenericName();

  if( !bunch_.empty() ) bunch_.clear();
  if (eType == "beam") {

    // beam is the 1st element
    firstIndex = 1;
    beamData( elPtr->parametersToSoftware() );
    
  } else {

    if (beamBefore == NULL) {

      dataManager_->consoleMessage("ERROR softwareBeta::createInputFile: no input beam => input file not created\n");
      return false;

    } else {

      dataManager_->consoleMessage(" softwareBeta::createInputFile: extract from input beam the quantities to be supplied to the beta commands\n");

      if (beamBefore->particleRepresentationOk()) {
	beamBefore->buildMomentRepresentation();
      } 

      if (!beamBefore->momentRepresentationOk()) {
	dataManager_->consoleMessage("ERROR softwareBeta::createInputFile: that's unclear: the beam is represented neither by macroparticles neither by moments\n");
	return false;
      }

      beamData( beamBefore );
    }
  }

  ofstream outfile;
  // inserts a space ' ' at the beginning of the stream
  // unicode value for space
  char space = '\u0020';
  
  // 1- Creates the structure file => xxx-str.txt
  string sfile= userDir_ + simulationId_ + "-str.txt";
  outfile.open(sfile.c_str(),ios::out);

  // machine description (1)
  outfile.put(space) << "*** VERSION ***" << endl;
  outfile.put(space) << "BETA-LNS v8.04" << endl;                  
  outfile.put(space) << "*** AUTHOR ***" << endl;
  outfile.put(space) << "PSPA" << endl;
  outfile.put(space) << "*** TITRE ***" << endl;   
  outfile.put(space) << simulationId_ << endl;
  
  // machine description (2) : elements in the structure
  outfile << beamLine( firstIndex ) << endl;

  const vector< pair< string,vector<smap> > >& commands= getComputingBlock()->actions();
  string action= commands.at( 0 ).first;
  const vector<smap>& amap= commands.at( 0 ).second;

  // machine description (3)
  if (action == "twiss") {
    outfile << twissStatement( amap );
  }
  
  if (action == "match") {
    outfile << matchStatement( amap );
  }
  
  outfile.put(space) << "*** ENDFILE ***" << endl;
  outfile.close();

  // 2- creates the input beta file commands => xxx-input.txt
  string input= userDir_ + getFileName("input",false);
  outfile.open(input.c_str(),ios::out);

  outfile << "FI\n"; // file commands: FI
  outfile << "READ\n";
  outfile << simulationId_+"-str.txt" << endl;
  outfile << "%%\n";
 
  outfile << "X\n"; // the execute command: X
  outfile << batchCommands();

  if (action == "match") {
    outfile << "AJ\n"; // the matching: AJ
    outfile << "FIT\n";
    outfile << "%%\n";
    outfile << batchCommands();
  }

  outfile << "GR\n"; // optical function plots: GR
  outfile << "BETA\n";
  outfile << graphicStep();
  outfile << "%%\n";
  outfile.close();
  
  dataManager_->consoleMessage("inputFile done for beta\n");
  return true;
}

void softwareBeta::beamData(const vector<psvs>& v)
{
  if(debug > 0) cout << "softwareBeta::beamData( 1 )\n";

  string str= v.at(1).first;
  if (str.find("particleDistribution") == std::string::npos) {
    fromParameters( v );
  } else {
    fromDistribution( v );
  }
}

void softwareBeta::fromParameters(const vector<psvs>& v)
{
  if(debug > 0) cout << "softwareBeta::fromParameters()\n";

  string WEN= "1000"; // 1GeV
  string key= v.at(1).second.at(1);
  if (key.find("none") == std::string::npos) {
    WEN= key; // MeV
    bunch_[ "energy" ]= WEN;
  } else {
    bunch_[ "energy" ]= "none";
  }

  key= v.at(1).second.at(2);
  if (key.find("none") == std::string::npos) {
    bunch_[ "pc" ]= key; // MeV
  } else {
    bunch_[ "pc" ]= WEN;
  }

  bunch_[ "emit_x" ] = v.at(1).second.at(3); // m.rad
  bunch_[ "emit_y" ] = v.at(1).second.at(4); // m.rad
  bunch_[ "emit_z" ] = v.at(1).second.at(5); // m.rad
  bunch_[ "sig_s" ] = v.at(1).second.at(6); // m
  bunch_[ "sig_e" ] = v.at(1).second.at(7);
  bunch_[ "beta_x" ] = "1.0000000E+00";
  bunch_[ "beta_y" ] = "1.0000000E+00";
  bunch_[ "alfa_x" ] = "0.0000000E+00";
  bunch_[ "alfa_y" ] = "0.0000000E+00";
  bunch_[ "eta_x" ]  = "0.0000000E+00";
  bunch_[ "etap_x" ] = "0.0000000E+00";
}

void softwareBeta::fromDistribution(const vector<psvs>& v)
{
  if(debug > 0) cout << "softwareBeta::fromDistribution()\n";

  bunch_[ "energy" ] = "none";
  bunch_[ "pc" ] = v.at(1).second.at(1); // MeV
  bunch_[ "emit_x" ] = v.at(1).second.at(2); // m.rad
  bunch_[ "emit_y" ] = v.at(1).second.at(3); // m.rad
  bunch_[ "emit_z" ] = "none";
  bunch_[ "sig_s" ] = "none";
  bunch_[ "sig_e" ] = "none";
  bunch_[ "beta_x" ] = v.at(1).second.at(4);
  bunch_[ "beta_y" ] = v.at(1).second.at(5);
  bunch_[ "alfa_x" ] = v.at(1).second.at(6);
  bunch_[ "alfa_y" ] = v.at(1).second.at(7);
  bunch_[ "eta_x" ]  = "0.0000000E+00";
  bunch_[ "etap_x" ] = "0.0000000E+00";
}

void softwareBeta::beamData(particleBeam *beam)
{
  if(debug > 0) cout << "softwareBeta::beamData( 2 )\n";

  bunch_[ "energy" ]= "none";
  double pc = 1000*(beam->getP0Transport()); // en MeV
  bunch_[ "pc" ] = mixedTools::doubleToString( pc );
  
  bunch_[ "emit_z" ] = "none";
  bunch_[ "sig_s" ]  = "none";
  bunch_[ "sig_e" ]  = "none";

  const beam2Moments& mts = beam->getTransportMoments();
  const vector< vector<double> >& rij= mts.getMoments();

  double epsx= beam->emit_x();
  bunch_[ "emit_x" ] = mixedTools::doubleToString( epsx );
  double betax= rij.at(0).at(0)*rij.at(0).at(0)/epsx;
  bunch_[ "beta_x" ] = mixedTools::doubleToString( betax );
  double r10= rij.at(1).at(0);
  double alfax= -r10/sqrt( 1.0-r10*r10 );
  bunch_[ "alfa_x" ] = mixedTools::doubleToString( alfax );
  
  if (debug > 0) {
    cout << "get xemit = " << epsx << endl;
    cout << "get xbeta = " << betax << endl;
    cout << "get xalfa = " << alfax << endl;
  }

  double epsy=  beam->emit_y();
  bunch_[ "emit_y" ] = mixedTools::doubleToString( epsy );
  double betay= rij.at(2).at(2)*rij.at(2).at(2)/epsy;
  bunch_[ "beta_y" ] = mixedTools::doubleToString( betay );
  double r32= rij.at(3).at(2);
  double alfay= -r32/sqrt( 1.0-r32*r32 );
  bunch_[ "alfa_y" ] = mixedTools::doubleToString( alfay );
  
  if (debug > 0) {
    cout << "get yemit = " << epsy << endl;
    cout << "get ybeta = " << betay << endl;
    cout << "get yalfa = " << alfay << endl;
  }
  
  bunch_[ "eta_x" ]  = "0.0000000E+00";
  bunch_[ "etap_x" ] = "0.0000000E+00";
}

string softwareBeta::beamLine(unsigned firstIndex)
{
  if(debug > 0) cout << "softwareBeta::beamLine()\n";

  list<string> lElts;   // List of the defined elements
  vector<string> mystr; // Elements in the machine
  map<string,int> edge; // List of the dipoles
  int num = 0; // number of dipoles counter
  
  ostringstream os1;
  unsigned nElts= getComputingBlock()->getNumberOfElements();
  for (unsigned k = firstIndex; k < nElts; k++)
    {
      abstractElement *elPtr= getComputingBlock()->getElement(k);
      string label= elPtr->getLabel();
      mixedTools::toupper( label );
      
      if (find(lElts.begin(),lElts.end(),label) == lElts.end()) {
	
	vector<psvs> v= elPtr->parametersToSoftware();
	os1 << elementsData(v,num);
	lElts.push_back( label );
	if(elPtr->getGenericName() == "bend") edge[label] = num;
	
      }

      if (elPtr->getGenericName() == "bend") {

	// add the dipole edges CO%E & CO%S where % = num
	string str= "CO" + mixedTools::intToString( edge[label] );
	mystr.push_back( str + "E" );
	mystr.push_back( label );
	mystr.push_back( str + "S" );
	
      } else {

	mystr.push_back( label );

      }

    }

  ostringstream os;
  os.put('\u0020') << "*** LIST OF ELEMENTS ***" << endl;
  // number of defined elements
  os.put('\t') << 2*num + lElts.size() << endl;
  // list of the elements
  os << os1.str();

  os.put('\u0020') << "*** STRUCTURE ***" << endl;
  // number of elements in the structure
  os.put('\t') << mystr.size() << endl;
  // arrangement of the elts in the machine (6 by line)
  for (unsigned k = 0; k < mystr.size(); k++)
    {
      if (k%6 == 0) os.put('\u0020') << mystr[ k ] << '\t';
      else os << mystr[ k ] << '\t';
      if ((k+1)%6 == 0) os << '\n';
    }

  os << '\n';
  return os.str();
}

string softwareBeta::elementsData(const vector<psvs>& v,int &num)
{
  if(debug > 0) cout << "softwareBeta::inputFormat()\n";

  string keyword= v.at(0).second.at(0);
  string label= v.at(0).second.at(1);
  mixedTools::toupper( label );
  
  ostringstream os;
  os.precision(7);
  int dl = 20;
  
  if (keyword == "marker") {

    os.put('\u0020') << label << "\t\t" << "OB\n";
     
  } else if (keyword == "drift") {

    double ln= atof(v.at(1).second.at(0).c_str());
    os.put('\u0020') << label << "\t\t" << "SD " << scientific << ln << endl;
    
   } else if(keyword == "qpole") {
    
    double ln= atof(v.at(1).second.at(0).c_str());
    double k1= atof(v.at(1).second.at(1).c_str());
     os.put('\u0020') << label << "\t\t" << "QP " << scientific << ln << " " << k1 << " " << 0.0 << endl;
    
  } else if(keyword == "spole") {

    double ln= atof(v.at(1).second.at(0).c_str());
    double k2= atof(v.at(1).second.at(1).c_str());
    // the sextupole strength S = 0.5*k2 m^-3
    os.put('\u0020') << label << "\t\t" << "SX " << scientific << ln << " " << 0.5*k2 << endl;
    
  } else if (keyword == "bend") {

    double alfa= atof(v.at(1).second.at(1).c_str()); // rad
    double rho = atof(v.at(1).second.at(2).c_str()); // m

    string str;
    str= v.at(1).second.at(3); // e1
    double e1= 0.0;
    if (str.find("none") == std::string::npos) {
      e1= atof(str.c_str()); // rad
    }
    
    str= v.at(1).second.at(4); // e1
    double e2= 0.0;
    if (str.find("none") == std::string::npos) {
      e2= atof(str.c_str()); // rad
    }

    str= v.at(1).second.at(5); // f_int1
    double f_int1= 0.0;
    if (str.find("none") == std::string::npos) {
      f_int1= atof(str.c_str());
    }

    str= v.at(1).second.at(6); // f_int1
    double f_int2= 0.0;
    if (str.find("none") == std::string::npos) {
      f_int2= atof(str.c_str());
    }

    // the fringe field extension Lff = 6.*2.*hgap*fint
    double Lf_int1= 0.0;
    str= v.at(1).second.at(7); // h_gap1
    if (str.find("none") == std::string::npos) {
      if (str.find("HGAP") == std::string::npos) {
	double h_gap1= atof(str.c_str());
	Lf_int1= 12.*f_int1*h_gap1;
      } else {
	Lf_int1= f_int1;
      }
    }
    
    double Lf_int2= 0.0;
    str= v.at(1).second.at(8); // h_gap2
    if (str.find("none") == std::string::npos) {
      if (str.find("HGAP") == std::string::npos) {
	double h_gap2= atof(str.c_str());
	Lf_int2= 12.*f_int2*h_gap2;
      } else {
	Lf_int2= f_int2;
      }
    }
         
    num++;
    string s1= "CO" + mixedTools::intToString( num ) + "E";
     os.put('\u0020') << s1 << "\t\t" << "CO " << scientific << e1 << " " << rho << " " << Lf_int1 << " " << 0.0 << " " << 0.0 << endl;
     os.put('\u0020')  << label << "\t\t" << "DI " << scientific << alfa << " " << rho << " " << 0.0 << " " << 0.0 << endl;
    string s2= "CO" + mixedTools::intToString( num ) + "S";
     os.put('\u0020') << s2 << "\t\t" << "CO " << scientific << e2 << " " << rho << " " << Lf_int2 << " " << 0.0 << " " << 0.0 << endl;
																
  } else if(keyword == "solenoid") {

    double ln= atof(v.at(1).second.at(0).c_str()); // m
    double ks= atof(v.at(1).second.at(1).c_str()); // rad/m
    // the solenoid strength SO = 0.5*ks 
     os.put('\u0020') << label << "\t\t" << "SO " << scientific << ln << " " << 0.5*ks << " " << 0.0 << endl;
    
  } else if(keyword == "rf_cavity") {
    
    double volt= atof(v.at(1).second.at(1).c_str()); // MV
    volt *= 1.e+06; // V
    int harmon = atoi(v.at(1).second.at(4).c_str()); // number h
    // il manque l'energie de référence E (eV)
     os.put('\u0020' ) << label << "\t\t" << "CA " << scientific << volt << " " << harmon << " " << 0.0 << endl;
    
  } else if(keyword == "kicker") {

    double xkick= atof(v.at(1).second.at(1).c_str()); // rad
    double ykick= atof(v.at(1).second.at(2).c_str()); // rad
    os.put('\u0020' ) << label << "\t\t" << "KI " << scientific << xkick << " " << ykick << endl;

  } else {

    dataManager_->messageEcran("ERROR","in softwareBeta::elementsData the type " + keyword + " is not defined");
    
  }

  return os.str();
}

string softwareBeta::twissStatement(const std::vector<smap>& v)
{
  // type of structure (BEAM:linear, BETA:periodical)
  string str = v[0].find("twiss_option")->second.at(0);

  if (str.find("option 1") != string::npos) {

    bunch_[ "option" ] = "BEAM";
    
  }
  
  if (str.find("option 2") != string::npos) {

    bunch_[ "option" ] = "BEAM";
    initialValues( v[1] );
    
   }

  if (str.find("option 3") != string::npos) {
    
    bunch_[ "option" ] = "BETA";
    
  }

  ostringstream os;
  os << executeCommand();
  return os.str();
}

void softwareBeta::initialValues(const smap& v)
{
  smap::const_iterator kt;
  kt= v.find("beta_x");
  if (kt != v.end()) bunch_[ "beta_x" ]= kt->second.at(0);
  kt= v.find("alpha_x");
  if (kt != v.end()) bunch_[ "alfa_x" ]= kt->second.at(0);
  kt= v.find("dx");
  if (kt != v.end()) bunch_[ "eta_x" ] = kt->second.at(0);
  kt= v.find("beta_y");
  if (kt != v.end()) bunch_[ "beta_y" ]= kt->second.at(0);
  kt= v.find("alpha_y");
  if (kt != v.end()) bunch_[ "alfa_y" ]= kt->second.at(0);
  kt= v.find("dpx");
  if (kt != v.end()) bunch_[ "etap_x" ]= kt->second.at(0);
}

string softwareBeta::executeCommand()
{
  if(debug > 0) cout << "softwareBeta::executeCommand()\n";

  ostringstream os;
  //number of periods
  //int np = 1; 
  //os << "*** PERIOD ***\n";
  //os << setfill (' ') << setw(5) << np << endl << endl;

  // type of structure (BEAM:linear, BETA:periodical)
  os.put('\u0020') << "*** OPTION ***\n";
  os.put('\u0020') << bunch_.find("option")->second << endl;

  // beam matrix (twiss parameters)
  os.put('\u0020') << "*** BEAM-MATRIX ***\n";
  os << beam_matrix();

  os.precision(7);
  // initial dispersions
  os.put('\u0020') << "*** DISPERSION ***\n";
  string key;
  key= bunch_.find("eta_x")->second;
  double eta_x = atof( key.c_str() );
  key= bunch_.find("etap_x")->second;
  double etap_x = atof( key.c_str() );
  os.put('\u0020') << scientific << eta_x << " " << etap_x << endl;
  os.put('\u0020') << scientific << 0.0  << " " << 0.0 << endl;
  
  // particle (E:electron, P:proton, O:other)
  // if O, the next line contains the mass and the charge of the particle
  os.put('\u0020') << "*** PARTICLE TYPE ***\n";
  os.put('\u0020') << "E\n";

  // particle kinetic energy and mass in MeV
  os.put('\u0020') << "*** ENERGIE CINETIQUE (MeV) ***\n";
  key= bunch_.find("pc")->second;
  os.put('\u0020') << scientific << atof( key.c_str() ) << endl;
  os.put('\u0020') << scientific << 0.511003435 << endl;
  
  // beam emittances in π.m.rad 
  os.put('\u0020') << "*** EMITTANCE ***\n";
  key= bunch_.find("emit_x")->second;
  double emit_x= atof( key.c_str() );
  key= bunch_.find("emit_y")->second;
  double emit_y= atof( key.c_str() );
  double emit_z= 0.0000000E+00;
  key= bunch_.find("emit_z")->second;
  if(key.find("none") == string::npos) emit_z= atof( key.c_str() );
  os.put('\u0020') << scientific << emit_x << " " << emit_y << " " << emit_z << endl;
  
  return os.str();
}

string softwareBeta::beam_matrix()
{
  if(debug > 0) cout << "softwareBeta::beam_matrix()\n";

  double sij[21]= {0.0000000E+00};
  string key;
  key= bunch_.find("beta_x")->second;
  sij[0]= atof( key.c_str() );
  key= bunch_.find("alfa_x")->second;
  sij[1]= -1.0*atof( key.c_str() );
  sij[2]= (1.+sij[1]*sij[1])/sij[0];
  key= bunch_.find("beta_y")->second;
  sij[5]= atof( key.c_str() );
  key= bunch_.find("alfa_y")->second;
  sij[8]= -1.0*atof( key.c_str() );
  sij[9]= (1.+sij[8]*sij[8])/sij[5];

  ostringstream os;
  os.precision(5);
  os << scientific;
  os.put('\u0020') << sij[0]  << endl;
  os.put('\u0020') << sij[1]  << " " << sij[2]  << endl;
  os.put('\u0020') << sij[3]  << " " << sij[4]  << " " << sij[5]  << endl;
  os.put('\u0020') << sij[6]  << " " << sij[7]  << " " << sij[8]  << " " << sij[9]  << endl;
  os.put('\u0020') << sij[10] << " " << sij[11] << " " << sij[12] << " " << sij[13] << " " << sij[14] << endl;
  os.put('\u0020') << sij[15] << " " << sij[16] << " " << sij[17] << " " << sij[18] << " " << sij[19] << " " << sij[20] << endl;
  
  return os.str();
}

string softwareBeta::matchStatement(const std::vector<smap>& v)
{
  // type of structure (BEAM:linear, BETA:periodical)
  bunch_[ "option" ] = "BEAM";
  if( !v[ 1 ].empty() ) initialValues( v[1] );

  ostringstream os;
  os << executeCommand();

  // matching parameters 
  os.put('\u0020') << "*** PARAMETERS OF FIT ***\n";

  // the number of variables
  os.put('\u0020') << "*** VARIABLES ***\n";
  os.put('\t') << v[ 4 ].size() << endl;
  // Each variable is defined by:
  smap::const_iterator it;
  for(it = v[ 4 ].begin(); it != v[ 4 ].end(); ++it) 
    {
      // 1- the element name
      string label= it->first;
      mixedTools::toupper( label );
 
      // 2- parameter of the element which vary (= it->second.at(0))
      string att= "2";

      // 3- the [coupling] field

      // 4- the step gives the starting step of the variable
      string step= it->second.at(1);
      if (step.find("none") != string::npos ) step= "0.00000E+00";
      
      //os << label << setfill (' ') << setw(10) << att << "   0 " + step + "  0\n";
      os.put('\u0020') << label << "\t\t" << att << "   0 " << scientific << atof(step.c_str()) << "  0\n";

    }

  // the number of constraints
  os.put('\u0020') << "*** CONSTRAINTS ***\n";
  os.put('\t') << v[ 2 ].size() << endl;
  for (int k = 0; k < v[ 2 ].size(); ++k)
    {
      it= v[ 2 ].find( mixedTools::intToString(k) );
      if (it == v[ 2 ].end()) continue;

      // constraint is defined by its location 
      string where= it->second.at(0); 

      // its type
      string type= nameOpticalFunction( it->second.at(1) );
      if (type.empty()) continue;

      // the desired value
      string value= it->second.at(2); 
      if (value.find("none") != string::npos) continue;
      
      // the weight associated 
      string weight= it->second.at(3);
      if (weight.find("none") != string::npos) {
	string s1= "in softwareBeta::matchStatement => for constraint a weight associated is mandatory\n";
	string s2= "Weight is set to the default value 1.0\n";
	dataManager_->messageEcran("WARNING",s1+s2);
	weight = "1.00000E+00";
      }
      
      //os << setfill (' ') << setw(5) << type << setfill (' ') << setw(15) << where << setfill (' ') << setw(15) << value << " " << weight << "  0\n";
      os.put('\u0020') << type << "\t\t" << where << "\t\t" << scientific << atof(value.c_str()) << " " << atof(weight.c_str()) << "  0\n";
  }

  return os.str();
}

string softwareBeta::nameOpticalFunction(string type)
{
  if(debug > 0) cout << "softwareBeta::nameOpticalFunction()\n";
  // see The matching: constraint list page 125)
  
  string key;
  if (type.find("beta_x")  != std::string::npos ) key= "0  1  1";
  if (type.find("alpha_x") != std::string::npos ) key= "0  2  1";
  if (type.find("mu_x") != std::string::npos ) key= "2  1  START";
  if( type.find("beta_y") != std::string::npos ) key= "0  3  3";
  if( type.find("alpha_y") != std::string::npos ) key= "0  4  3";
  if( type.find("mu_y") != std::string::npos ) key= "2  3  START";
  if( type.find("dx") != std::string::npos ) key= "6  1  0";
  if( type.find("dpx") != std::string::npos ) key= "6  2  0";

  return key;
}

string softwareBeta::batchCommands()
{
  ostringstream os;
  os << "#begin batch file\n";
  os << "#Panel  :PARAMETERS CALCULATION\n";

  os << "#Tabset :EXEC\n";
  os << "#Title  :CALCULATION ORDER\n";
  os << "#Button :1st Order 2nd Order  Scaling\n";
  os << 1 << " " << 1 << " " << 1 << '\n';

  os << "#Title  :MOMEMTUM VALUES\n"; 
  os << "#Label  :dp/p" << '\n'; 
  os << scientific << setprecision(5) << 0.00000E+00 << '\n';
  os << "#Label  :Scaling factor\n";
  os << 0.10000E+01 << '\n';
  os << "#Label  :Momentum spread\n";
  os << 0.00000E+00 << '\n'; 

  os << "#Title  :SCALING FLAG\n";
  os << "#Button :Bend Scaling New Scaling Anal. Dipoles Cavity Scaling\n";
  os << 0 << " " << 0 << " " << 0 << " " << 0 <<"\n";

  string key;
  key= bunch_.find("option")->second;
  if (key.find("BETA") != string::npos) {
    os << "#Title  :PARAMETER OF CLOSED ORBIT SEARCH\n";
    os << "#Label  :Closed orbit precision\n";
    os << 0.10000E-05 << '\n';
    os << "#Label  :Nb steps, nb turns/step\n";
    os << 10 << " " << 2 << '\n';
  }

  os << "#Title  :SEXTUPOLAR ANALYZE\n";
  os << "#Button :Analytic Harmonic\n";  
  os << 1 << " " << 1 << '\n';  
  os << "#Label  :Nb of harmonic\n";
  os << 200 << '\n';  

  os << "#Tabset :SPCH\n";
  os << "#Title  :SPACE CHARGE EFFECTS\n";
  os << "#Title  :WARNING : BETA NEEDS A FIRST RUN\n";
  os << "#Title  :WITHOUT SPACE CHARGE EFFECTS\n";
  os << "#Button :No 2D\n";
  os << 1 << " " << 1 << '\n';

  os << "#Tabset :BEAM CURRENT\n";
  os << "#Title  :PARTICLE TYPE\n";
  os << "#Button :electron proton muon ion\n";
  os << 1 << " " << 1 << " " << 1 << " " << 1 << '\n';  
  os << "#Title  :INTENSITY COMPUTATION\n";
  os << "#Button :Nb particles  Intensity (A) Nb charges(C)\n";
  os << 0 << " " << 0 << " " << 0 << '\n';  
  os << "#Label  :Bunch length   (m)\n";
  os << 0.00000E+00 << '\n';  
  os << "#Label  :Nb of particles in the bunch\n";
  os << 0.00000E+00 << '\n';  
  os << "#Label  :Peak intensity (A)\n";
  os << 0.00000E+00 << '\n';  
  os << "#Label  :Nb of charges in the bunch (C)\n";
  os << 0.00000E+00 << '\n';  
  os << "#Label  :A,Q\n";
  os << 0.10000E+01 << " " << 0.10000E+01 << '\n';  

  os << "#Label  :Kinetic energy (MeV)\n";
  key= bunch_.find("pc")->second;
  os << atof( key.c_str() ) << '\n';

  os << "#Tabset :BEAM\n";
  os << "#Title  :BEAM OPTION\n";
  os << "#Button :BEAM BETA\n";
  key = bunch_.find("option")->second;
  if (key.find("BETA") == string::npos) {
    os << 1 << " " << 1 << '\n'; // BEAM:linear
  } else {
    os << 2 << " " << 2 << '\n'; // BETA:periodical
  }

  os << "#Title  :BEAM MATRIX\n";
  os << "#Button :TWISS MODE SIGMA MODE\n";
  os << 1 << " " << 1 << '\n';
  os << beam_matrix();

  os << "#Title  :Entry type\n";
  os << "#Button :RMS EMITTANCES MAX EMITTANCES RMS BEAM SIZES MAX BEAM SIZES\n";
  os << 1 << " " << 1 << " " << 1 << " " << 1 << '\n';  
  os << "#Label  :Nb of rms at boundaries\n"; 
  os << 0.10000E+01 << '\n';  
 
  os << "#Title  :EMITTANCES\n"; 
  os << "#Label  :RMS  Ex/Pi (m.rad)\n"; 
  key= bunch_.find("emit_x")->second;
  double xemit= atof( key.c_str() );
  //os << scientific << setprecision(7) << xemit << '\n';
  os << xemit << '\n'; 
  os << "#Label  :RMS  Ez/Pi (m.rad)\n"; 
  key= bunch_.find("emit_y")->second;
  double yemit= atof( key.c_str() );
  //os << scientific << setprecision(7) << yemit << '\n';
  os << yemit << '\n'; 
  os << "#Label  :RMS  Es/Pi (m.rad)\n"; 
  os << 0.15000E-03 << '\n';   

  os << "#Title  :BEAM SIZES\n"; 
  os << "#Label  :Horizontal   (m)\n";
  key= bunch_.find("beta_x")->second;
  double xrms= xemit*atof( key.c_str() );
  os << sqrt( xrms ) << '\n';   
  os << "#Label  :Vertical     (m)\n";
  key= bunch_.find("beta_y")->second;
  double yrms= yemit*atof( key.c_str() );
  os << sqrt( yrms ) << '\n'; 
  os << "#Label  :Longitudinal (m)\n"; 
  os << 0.00000E+00 << '\n';   

  os << "#Title  :DISPERSION\n"; 
  os << "#Label  :ETAX ETAXP\n"; 
  key= bunch_.find("eta_x")->second;
  double eta_x= atof( key.c_str() );
  key= bunch_.find("etap_x")->second;
  double etap_x= atof( key.c_str() );
  os << eta_x << " " << etap_x << '\n';   
  os << "#Label  :ETAZ ETAZP\n"; 
  os << 0.00000E+00 << " " << 0.00000E+00 << '\n'; 
  
  os << "#Title  :CHROMATIC FUNCTION\n"; 
  os << "#Label  :Wx PHIx DDx DDxp\n"; 
  os << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << '\n';   
  os << "#Label  :Wy PHIy DDy DDyp\n"; 
  os << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << '\n'; 

  os << "#Tabset :Dipolar correction\n"; 
  os << "#Title  :DIPOLAR CORRECTION\n"; 
  os << "#Button :H Correction V Correction\n";  
  os << 0 << " " << 0  << '\n';
  os << "#Title  :DIPOLAR CORRECTION METHOD\n"; 
  os << "#Button :Least Square SVD\n";    
  os << 1 << " " << 1 << '\n';  
  os << "#Label  :Corrector precision\n"; 
  os << 0.10000E-04 << '\n';  
  os << "#Label  :Nb of steps\n"; 
  os << 20 << '\n';  
  os << "#Label  :Eigen Vector Number (0< <1)\n"; 
  os << 0.10000E+01 << '\n';  
  os << "#Label  :Eigen Vector Threshold   (0< <1)\n"; 
  os << 0.10000E-04 << '\n';  
  os << "#Title  :SEXTUPOLES CONTROL [Nb step > 1]\n"; 
  os << "#Button :R.A.Z Sextupoles at 1st step\n";  
  os << 1 << '\n';

  os << "#Tabset :Skew correction\n";
  os << "#Title  :SKEW CORRECTION\n";
  os << "#Button :Skew Correction\n";  
  os << 0  << '\n'; 
  os << "#Button :Fix Coupling\n";     
  os << 0 << '\n';  
  os << "#Label  :Coupling : Value, Precision\n";
  os << 0.0 << " " <<  0.10000E-04 << '\n';  
  os << "#Title  :CROSS MATRIX CORRECTION METHOD\n";
  os << "#Button :Least Square SVD\n";
  os << 1 << " " << 1 << '\n';  
  os << "#Label  :Cross Matrix Correcting Weight\n";
  os << 1.0 << '\n';  
  os << "#Button :Total Cross Matrix Local Cross Matrix\n";
  os << 1 << " " << 1 << '\n';  
  os << "#Button :Normalized Total Cross Matrix\n";
  os << 0 << '\n';  
  os << "#Button :etax  etaxp etaz  etazp\n";
  os << 0 << " " << 0 << " " << 0 << " " << 0 << '\n';    
  os << "#Label  :BPM Coupling            Weight\n";  
  os << 0.0 << '\n';    
  os << "#Label  :Constant Coupling       Weight\n";  
  os << 0.0 << '\n';
  os << "#Title  :DISPERSION CORRECTION\n";
  os << "#Label  :Dispersion Correcting   Weight\n";
  os << 0.0 << '\n';

  os << "#End Panel" << endl;
  os << "#end batch file" << endl;
  return os.str();
}

string softwareBeta::graphicStep()
{
  ostringstream os;
  os << "#begin batch file\n";
  os << "#Panel  :GRAPHIC SCALES\n";
  
  os << "#Label  :Nb step (x200)\n";
  os << 1 << '\n';
  os << "#Title  :GRAPHIC AXIS\n";
  os << "#Button :Auto Axis\n";
  os << 1 << '\n';  
  os << "#Label  :X min,max\n";
  os << scientific << setprecision(5);
  os << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << '\n';
  os << "#Label  :Y min,max\n";
  os << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << '\n';
  os << "#Button :Draw Axis\n";      
  os << 1 << '\n';  
  os << "#Button :Log scale X   Log scale Y\n";    
  os << 0 << " "<< 0 << '\n';  
  os << "#Button :Clear Graphic\n";  
  os << 0 << '\n';  
  os << "#Title  :Horizontal\n";
  os << "#Button :Black  Red  Green  Blue  Pink  Cyan  Yellow  Grey\n";
  os << "2 2 2 2 2 2 2 2" << '\n'; 
  os << "#Title  :Vertical\n";
  os << "#Button :Black  Red  Green  Blue  Pink  Cyan  Yellow  Grey\n";
  os << "4 4 4 4 4 4 4 4"  << '\n';
  os << "#Title  :FILE OUTPUT OPTIONS\n";
  os << "#Button :Print out the values\n";  
  os << 1 << '\n';  

  os << simulationId_+"-layout.txt" << '\n';
  os << simulationId_+"-twiss.txt"  << '\n';

  os << "#End Panel\n";
  os << "%%\n";
  os << "#end batch file\n";
  return os.str();
}

bool softwareBeta::execute()
{
  if(debug > 0) cout << "softwareBeta::execute()\n";

  ostringstream sortie;
  bool status= true;

  string wrkDir = dataManager_->getSoftwareDir();
  string mjob= wrkDir.append("betabatch");
  if (!mixedTools::exists( mjob.c_str())) {
    
    sortie << "Error in softwareBeta::execute: " << mjob << " does not exist\n";
    status= false;

  } else {

    sortie << "run betabatch " << getFileName("input",false) << endl;
    mjob += string(" < ") + getFileName("input",false);
    string resultOfRun;
    bool success= launchJob(mjob,resultOfRun);

    if ( !success ) {
      status= false;
    } else {
      sortie << "betabatch has finished normally\n";
      dataManager_->consoleOutput(resultOfRun);

      // creates the output file => xxx-output.txt;
      string nameOut= userDir_ + getFileName("output",false);
      ofstream outfile;
      outfile.open(nameOut.c_str(),ios::out);
      if ( !outfile ) {
     	sortie << "error opening betabatch output " << nameOut << endl;
	status= false;
      } else {
	// copy output in xxx-output.txt
	outfile << resultOfRun << endl;
	outfile.close();
      }
    }
  }
  
  dataManager_->consoleMessage(sortie.str());
  return status;
}

bool softwareBeta::buildBeamAfterElements()
{
  if(debug > 0) cout << "softwareBeta::buildBeamAfterElements()\n";

  const vector< pair< string,vector<smap> > >& commands= getComputingBlock()->actions();
  string action= commands.at( 0 ).first;
  if( action == "match" ) FinalValues();

  // read contents of xxx-twiss.txt
  string name= userDir_ + getFileName("twiss",false);
  ifstream infile;
  infile.open(name.c_str(),ios::in);
  if (!infile) {
    dataManager_->consoleMessage("softwareBeta::ERROR when opening "+name+"\n");
    return false;
  }

  const smap& vmap= getTableContents( infile );
  infile.close();

  int n1= getComputingBlock()->getNumberOfElements();
  smap::const_iterator it= vmap.find("betx");
  int n2= it->second.size();
  int minz= ( ( n1 > n2 ) ?  n2:n1 );

  double ws[5];
  for (unsigned k = 0; k < minz; k++)
    {
      abstractElement *ptr= getComputingBlock()->getElement( k );
      //cout << "$ NAME(" << k << ")= " << ptr->getLabel() << endl;
      it= vmap.find("betx");
      ws[0]= atof( it->second.at( k ).c_str() );
      it= vmap.find("bety");
      ws[1]= atof( it->second.at( k ).c_str() );
      it= vmap.find("alfx");
      ws[2]= atof( it->second.at( k ).c_str() );
      it= vmap.find("alfy");
      ws[3]= atof( it->second.at( k ).c_str() );
      it= vmap.find("dx");
      ws[4]= atof( it->second.at( k ).c_str() );
      setBeamParameters( ws );
    }

  return true;
}

void softwareBeta::FinalValues()
{
  if(debug > 0) cout << "softwareBeta::FinalValues()\n";
  
  // read the contents of fort.7
  string filename= userDir_ + "fort.7";
  ifstream infile;
  infile.open(filename.c_str(),ios::in);
  if (!infile) {
    cout << "ERROR softwareBeta::opening " << filename << endl;
    return;
  }
  
  string buf;
  getline(infile,buf); // read the TITLE
  string eLabel, iType, xValue;
  do { 
    getline(infile,buf);
    stringstream ss;
    ss << buf;
    ss >> eLabel >> iType >> xValue;

    // among all the elements look for those of label= eLabel
    unsigned nElts= getComputingBlock()->getNumberOfElements();
    for (unsigned k = 0; k < nElts; k++)
      {
	abstractElement *ptr= getComputingBlock()->getElement(k);
	string label= ptr->getLabel();
	mixedTools::toupper( label );
	if (label != eLabel ) continue;

	// fit only on the elements qpole, spole and solenoid 
	// ptr->getGenericName() == "qpole", "spole" or "solenoid"
	string *param= ptr->getParametersString();
	if (iType == "1") param[2]= xValue; // length
	if (iType == "2") param[3]= xValue; // strength
	ptr->setParametersString( param );
      }
  } while ( !infile.eof() );
}


smap softwareBeta::getTableContents(ifstream& infile)
{
  if(debug > 0) cout << "softwareBeta::getTableContents()\n";

  // structure of twiss parameters file: s betx alfx mux dx dpx x bety alfy muy dy dpy y
  vector<string> vaxis;
  vaxis.push_back("betx");
  vaxis.push_back("alfx");
  vaxis.push_back("mux");
  vaxis.push_back("dx");
  vaxis.push_back("dpx");
  vaxis.push_back("x");
  vaxis.push_back("bety");
  vaxis.push_back("alfy");
  vaxis.push_back("muy");
  vaxis.push_back("dy");
  vaxis.push_back("dpy");
  vaxis.push_back("y");

  smap vmap; 
  vector<string>::iterator where;
  string key, buf;
  vmap.insert(pair<string,vector<string> >("s",vector<string>()) );
  while (infile >> buf) { // s
    key= "s";
    where= find(vmap[ key ].begin(),vmap[ key ].end(),buf);
    if (where != vmap[ key ].end()) { 
      // buf already existed get last values
      for (int k = 0; k < vaxis.size(); ++k)
	{
	  key= vaxis[k];
	  infile >> buf;
	  vmap[ key ].back()= buf;
	}
    } else {
      vmap[ key ].push_back( buf );
      for (int k = 0; k < vaxis.size(); ++k)
	{
	  key= vaxis[k];
	  infile >> buf;
	  vmap[ key ].push_back( buf );
	}
    }
  }

  return vmap;
}

void softwareBeta::setBeamParameters(double ws[5])
{
  if(debug > 0) cout << "softwareBeta::setBeamParameters()\n";

  // emittances
  string key;
  key= bunch_.find("emit_x")->second;
  double EX= atof( key.c_str() );
  key= bunch_.find("emit_y")->second;
  double EY= atof( key.c_str() );
  key= bunch_.find("pc")->second;
  double PC= atof( key.c_str() );  

  double rij[21]= {0.0};
  double aux;
  rij[0]= sqrt( EX*ws[0] );
  aux= 1.+ws[2]*ws[2];
  rij[1]= -ws[2]/sqrt(aux);
  rij[2]= sqrt( EX*aux/ws[0] );
  rij[5]= sqrt( EY*ws[1] );
  aux= 1.+ws[3]*ws[3];
  rij[8]= -ws[3]/sqrt(aux); 
  rij[9]= sqrt( EY*aux/ws[1] );
  
  beam2Moments mts;
  mts.readFromTransportOutput( rij );
  particleBeam *newDiag= dataManager_->updateCurrentDiagnostic(true);
  newDiag->set2Moments( mts );
  newDiag->setP0Transport( PC );
  newDiag->setOpticalParameters( ws );
}

json softwareBeta::readOpticalParameters()
{
  if(debug > 0) cout << "softwareBeta::readOpticalParameters()\n";

  json j;
  j["software"]= "beta";

  const vector< pair< string,vector<smap> > >& commands= getComputingBlock()->actions();
  j["action"]= commands.at(0).first;

  // read contents of xxx-twiss.txt
  string name= userDir_ + getFileName("twiss",false);
  ifstream infile;
  infile.open(name.c_str(),ios::in);
  if ( !infile ) {
    cout << "ERROR softwareBeta:: in opening " << name << endl;
    return j;
  }

  const smap& vmap= getTableContents( infile );
  infile.close();

  smap::const_iterator it= vmap.find("s");
  for (int k = 0; k < it->second.size(); k++)
    {
      double xcoor= atof( it->second.at(k).c_str() );
      j["zcoor"].push_back(xcoor);
    }
  
  it= vmap.find("betx");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["xbeta"].push_back(ycoor);
    } 

  it= vmap.find("alfx");
  for(int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["xalfa"].push_back(ycoor);
    }

  it= vmap.find("mux");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["xmu"].push_back(ycoor);
    }

  it= vmap.find("dx");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["xeta"].push_back(ycoor);
    }
  
  it= vmap.find("dpx");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["xetap"].push_back(ycoor);
    }
  
  it= vmap.find("bety");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["ybeta"].push_back(ycoor);
    }

  it= vmap.find("alfy");
  for (int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["yalfa"].push_back(ycoor);
    }
  
  it= vmap.find("muy");
  for(int k = 0; k < it->second.size(); k++)
    {
      double ycoor= atof( it->second.at(k).c_str() );
      j["ymu"].push_back(ycoor);
    }

  // creates the output file => xxx-plot.txt
  string fname= userDir_ + getFileName("plot",false);
  ofstream outfile;
  outfile.open(fname.c_str(),ios::out);
  outfile << std::setw(4) << j << std::endl;
  outfile.close();
  
  return j;
}