c++ - Creating variables from different line types imported from text file -


i'm trying import data text file , assign variables can analyse functions. data in following format:

run 141544 event 5 njets 0 m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1 dptinv: 0.000370146 m2: pt,eta,phi,m= 222.408 -0.198471 0.942319 0.1 dptinv: 0.00038302  run 141544 event 7 njets 1 m1: pt,eta,phi,m= 281.327 -0.489914 1.12498 0.1 dptinv: 0.000406393 m2: pt,eta,phi,m= 238.38 0.128715 -2.07527 0.1 dptinv: 0.000399279 

... there around 15000 entries, each 4 lines. on each line, values delimited spaces, , between each entry, there blank line. because each line of entry in different format, wrote loop separate cases. problem i'm having appears wrong code assigns variables. when use loop output lines of type, runs perfectly. once try break each line variables , assign , print variables, program prints same line multiple times , crashes. here's code:

#include <iostream> #include <fstream> #include <sstream> #include <cmath> #include <numeric> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <iterator> using namespace std; using std::cout; using std::endl;  struct rowtype1 // structure of lines containing run data {     string runnumber;     string eventnumber; };  struct rowtype2 // structure of lines containing data muon1 , muon2 {     string ptvalue1;     string etavalue1;     string phivalue1;     string massvalue1; };  vector<rowtype1> row1values; vector<rowtype2> row2values;  int main() {     string line;     ifstream indata;     indata.open("/users/edward/downloads/muons.txt");      if (indata.is_open())     {         while ( indata.good() )         {             while (getline(indata,line))             {                 if (line[0] == 'n') // recognizes , skips blank lines                 {                     continue;                 }                 else if (line[1] == 'u') // recognizes lines containing run information                 {                     istringstream ss(line);                     istream_iterator<string> begin(ss), end;                     vector<string> words(begin, end);                     rowtype1 s { words[1], words[3]};                     row1values.push_back(s);                     for(auto && s : row1values)                         cout << "run " << s.runnumber << " " << "event " << s.eventnumber << "\n";                 }                 else if (line[1] == '1') // recognizes lines containing muon1 information                 {                     istringstream ss(line);                     istream_iterator<string> begin(ss), end;                     vector<string> words(begin, end);                     rowtype2 s { words[2], words[3], words[4], words[5] };                     row2values.push_back(s);                     for(auto && s : row2values)                         cout << "m1 " << s.ptvalue1 << " " << s.etavalue1 << " " << s.phivalue1 << " " << s.massvalue1 << "\n";                 }                 else if (line[1] == '2') // recognizes lines containing muon2 information                 {                   istringstream ss(line);                     istream_iterator<string> begin(ss), end;                     vector<string> words(begin, end);                     rowtype2 s { words[2], words[3], words[4], words[5] };                     row2values.push_back(s);                     for(auto && s : row2values)                         cout << "m2 " << s.ptvalue1 << " " << s.etavalue1 << " " << s.phivalue1 << " " << s.massvalue1 << "\n";                 }             }         }         indata.close();     }     return 0; }; 

to test if variables being assigned correctly, had code output values. instead of looping through rows , outputting variables, output looks this:

run 141544 event 5 run 141544 event 5 m1 231.277 0.496237 -2.22082 0.1 m2 231.277 0.496237 -2.22082 0.1 m2 222.408 -0.198471 0.942319 0.1 run 141544 event 5 run 141544 event 7 run 141544 event 7 m1 231.277 0.496237 -2.22082 0.1 m1 222.408 -0.198471 0.942319 0.1 m1 281.327 -0.489914 1.12498 0.1 m2 231.277 0.496237 -2.22082 0.1 m2 222.408 -0.198471 0.942319 0.1 m2 281.327 -0.489914 1.12498 0.1 m2 238.38 0.128715 -2.07527 0.1 run 141544 event 5 run 141544 event 7 run 141572 event 2 

there many issues code won't go detail address.

mainly, think issue related fact you didn't parse file correctly, , variables misaligned in assignments.

while attempting fix , making more modular, rewrote following (i did not perform checks - that's can yourself. all data assumed correct.):

#include <iostream> #include <fstream> #include <sstream> #include <string> #include <cstdlib> #include <vector>  typedef std::vector<int> ivec; typedef std::vector<double> dvec; typedef std::vector<std::string> svec;   void get_runs_events(std::string const &varstr, ivec &runs, ivec &events) {   std::istringstream iss(varstr);   std::string t1, t2;    int run = 0, event = 0;    if (iss >> t1 >> run >> t2 >> event) {     runs.push_back(run);     events.push_back(event);   } }  void get_njets(std::string const &varstr, ivec &njets) {   std::istringstream iss(varstr);   std::string t1;    int njet;    if (iss >> t1 >> njet) {     njets.push_back(njet);   } }  void set_m_params(std::string const &varstr, dvec &pt, dvec &eta, dvec &phi, dvec &m, dvec &dptinv) {   std::string dpt = "dptinv:";   std::string pre_str = varstr.substr(varstr.find('=') + 1);   std::string str = pre_str.substr(0, pre_str.find(dpt));    std::string dpt_value = pre_str.substr(pre_str.find(dpt) + dpt.length());    double m_pt, m_eta, m_phi, m_m, m_dptinv;    std::istringstream iss(str);    if (iss >> m_pt >> m_eta >> m_phi >> m_m) {     pt.push_back(m_pt);     eta.push_back(m_eta);     phi.push_back(m_phi);     m.push_back(m_m);   }    iss.str(dpt_value);   iss.clear();    if (iss >> m_dptinv) {     dptinv.push_back(m_dptinv);   } }  int main() {   std::ifstream ifile("text", std::ifstream::in);   std::string temp;    ivec runs, events, njets;   dvec m1_pt, m1_eta, m1_phi, m1_m, m1_dptinv;   dvec m2_pt, m2_eta, m2_phi, m2_m, m2_dptinv;    svec raw;    if (ifile.is_open()) {      while(std::getline(ifile, temp)) {       raw.push_back(temp);     }      int = 0;      //now iterate on raw data , accordingly, fill containers     //why % 5? because although said lines repeat every 4 lines,     //in actuality, repeat every 5 lines blank line counts one.     //there many ways go this, implementation reads entire file     //line line , skips 5th line, or in case of % 5 case,     //would % 5 == 4. since that's assumed invalid, ignored entirely,     //hence code, shown below.     (svec::const_iterator = raw.begin(); != raw.end(); ++it, ++i) {       if (i % 5 == 0) {         get_runs_events(*it, runs, events);       }       else if (i % 5 == 1) {         get_njets(*it, njets);       }       else if (i % 5 == 2) {         set_m_params(*it, m1_pt, m1_eta, m1_phi, m1_m, m1_dptinv);       }       else if (i % 5 == 3) {         set_m_params(*it, m2_pt, m2_eta, m2_phi, m2_m, m2_dptinv);       }     }      //now output information see correct     (i = 0; < runs.size(); ++i) {       std::cout << runs[i] << " " << events[i] << " " << njets[i] << "\n";       std::cout << m1_pt[i] << " " << m1_eta[i] << " " << m1_phi[i] << " " << m1_m[i] << " " << m1_dptinv[i] << "\n";       std::cout << m2_pt[i] << " " << m2_eta[i] << " " << m2_phi[i] << " " << m2_m[i] << " " << m2_dptinv[i] << "\n\n";     }   }   else {     exit(1);   }    ifile.close();    return 0; } 

using data (modified original):

run 141544 event 5 njets 0 m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1 dptinv: 0.000370146 m2: pt,eta,phi,m= 222.408 -0.198471 0.942319 0.1 dptinv: 0.00038302  run 141545 event 7 njets 1 m1: pt,eta,phi,m= 281.327 -0.489914 1.12498 0.1 dptinv: 0.000406393 m2: pt,eta,phi,m= 238.38 0.128715 -2.07527 0.1 dptinv: 0.00039927  run 141546 event 5 njets 0 m1: pt,eta,phi,m= 231.277 0.496237 -2.22082 0.1 dptinv: 0.000370146 m2: pt,eta,phi,m= 222.408 -0.198471 0.942319 0.1 dptinv: 0.00038302  run 141547 event 7 njets 1 m1: pt,eta,phi,m= 281.327 -0.489914 1.12498 0.1 dptinv: 0.000406393 m2: pt,eta,phi,m= 238.38 0.128715 -2.07527 0.1 dptinv: 0.00039927 

you correct results of following ordering:

  • 1st row: {run} {event} {njet}
  • 2nd row: {m1 pt} {m1 eta} {m1 phi} {m1 m} {m1 dptinv}
  • 3rd row: {m2 pt} {m2 eta} {m2 phi} {m2 m} {m2 dptinv}

and following output:

141544 5 0 231.277 0.496237 -2.22082 0.1 0.000370146 222.408 -0.198471 0.942319 0.1 0.00038302  141545 7 1 281.327 -0.489914 1.12498 0.1 0.000406393 238.38 0.128715 -2.07527 0.1 0.00039927  141546 5 0 231.277 0.496237 -2.22082 0.1 0.000370146 222.408 -0.198471 0.942319 0.1 0.00038302  141547 7 1 281.327 -0.489914 1.12498 0.1 0.000406393 238.38 0.128715 -2.07527 0.1 0.00039927 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -