Hello, I’m entirely new to root and to c++, but I understand that root works primarily on c++.
I have therefore written a script in c++ that works, compiles and runs, and am now trying to run it in root.
I dont know whether this is possible or not, or whether I would need to change various library types that I use but the code is below and any help with getting it running in root would be great.
The code basically takes input from a file written for a different display language, scans it for keywords and then takes the appropriate values from the file and at the moment simply prints them to screen. Eventually I would like to be able to make a histogram for each of the data collections and then produce a root tree, (possibly with tuples of the data attached) containing all of the histograms.
Many Thanks for any help.
Geoff Herbert.
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sstream>
//using namespace std;
//int main()
void histogram2()
{
const string ws = " \t"; //going to be used to test input
string filename = "wbbj.top"; //will need to obtain this from elsewhere eventually
const char* filenamechar = filename.c_str();
ifstream inwards; //initialise the opening if the file
inwards.open(filenamechar);
if( !inwards.good() ) //test to see if the file can be opened
{
cerr << "Cannot open the file: \"" << filename+"\"!" << endl;
abort(); //if it can't then print an error and abort
}
//initialise the variables
string line("");
string title_a("");
string title_b("");
string title_c("");
string title_d("");
string left_title("");
string top_title("");
string bottom_title("");
string lower_x_limit("");
string upper_x_limit("");
string scale("");
string limits("");
float number1;
float number2;
float number3;
int new_plot_switch = 0;
int loc_0;
int loc_1;
int loc_2;
int loc_3;
int loc_4;
int loc_5;
int loc_6;
int loc_7;
int loc_8;
int loc_9;
int loc_10;
int loc_11;
int loc_12;
int loc_13;
int title_counter = 0;
int begin_hist = 0;
int end_hist = 0;
while( getline(inwards, line)) { //whilst there are still lines from the file to read
// line=""; //initialise line to be empty
// inwards >> line; //take the line from the file
// cout << "line inputted: " << line << endl; //print the inputted line to the screen
std::string::size_type loc_0 = line.find_first_not_of(ws); // find the first non whitespace element
if(std::string::npos == loc_0) //if the line is empty then just continue
{
continue;
}
else //If the line has text, then erase all whitespace up to that text
{
line.erase(0, loc_0);
}
// Find the location of all the important strings if they're there
loc_1 = line.find( "SCALE", 0 );
loc_2 = line.find( "TITLE", 0 );
loc_3 = line.find( "SET LIMITS", 0);
loc_4 = line.find( "title", 0);
loc_5 = line.find( "BEGIN HIST", 0);
loc_6 = line.find( "END HIST", 0);
loc_7 = line.find( "NEW PLOT", 0);
//test to see if anything interesting has been found
if (std::string::npos != loc_1) //If the line contains the y-axis scale
{
scale = line.substr(12); //extract just the scale used
continue; //Once this line has been identified then we can move on with the next line
}
else if (std::string::npos != loc_2) //If the line contains one of the TITLES
{
loc_8 = line.find( "TOP", 0); //find the location of all the relevant strings
loc_9 = line.find( "BOTTOM", 0);
loc_10 = line.find( "LEFT", 0);
loc_11 = line.find( "SET TITLE", 0);
if (std::string::npos != loc_8) //If the line contains the TITLE TOP
{
top_title = line.substr(10);
continue;
}
else if (std::string::npos != loc_9) //If the line contains the TITLE BOTTOM
{
bottom_title = line.substr(13);
continue;
}
else if (std::string::npos != loc_10) //If the line contains the TITLE LEFT
{
left_title = line.substr(11);
continue;
}
else if (std::string::npos != loc_11) //If the title doesnt contain anything interesting
{
continue;
}
else
{
title_counter++;
if (title_counter == 1)
{
title_a = line.substr(14);
continue;
}
else if (title_counter == 2)
{
title_b = line.substr(14);
continue;
}
else
{
title_c = line.substr(14);
title_counter = 0;
continue;
}
}
}
else if (std::string::npos != loc_3)//If the line contains the limits of the x-axis
{
limits = line.substr(16);
loc_12 = limits.find_last_of(ws);
loc_13 = limits.find_first_of(ws);
line.erase(0, loc_0);
lower_x_limit = limits;
lower_x_limit.erase(loc_13, lower_x_limit.length());
upper_x_limit = limits;
upper_x_limit.erase(0, loc_12+1);
continue;
}
else if (std::string::npos != loc_4) //If the line contains the title
{
title_d = line.substr(23);
continue;
}
else if (std::string::npos != loc_5) //If the line contains the BEGIN HIST marker
{
begin_hist++;
cout << "x\ty\tdy" << endl;
while(1)
{
getline(inwards, line);
std::istringstream streamer(line);
loc_6 = line.find( "END HIST", 0);
if (std::string::npos != loc_6) //If the line contains the END HIST marker
{
end_hist++;
break;
}
streamer >> number1;
streamer >> number2;
streamer >> number3;
cout << number1 << "\t" << number2 << "\t" << number3 << endl;
}
}
else if (std::string::npos != loc_7) //If the line contains the NEW PLOT marker
{
new_plot_switch = 1;
cout << "Histogram Title A: " << title_a << endl;
cout << "Histogram Title B: " << title_b << endl;
cout << "Histogram Title C: " << title_c << endl;
cout << "Histogram Title D: " << title_d << endl;
cout << "Histogram Top Title: " << top_title << endl;
cout << "Histogram Left Title: " << left_title << endl;
cout << "Histogram Bottom Title: " << bottom_title << endl;
cout << "Histogram Scale: " << scale << endl;
cout << "Lower x-axis limit: " << lower_x_limit << endl;
cout << "Upper x-axis limit " << upper_x_limit << endl;
cout << "\n" << endl;
continue;
}
else //Its a line that I dont care about
{
continue;
}
}
return 0;
}
NB, the code will compile and run correctly if you un-comment out the two lines:
//using namespace std;
//int main()
and instead comment out
void histogram2()
Currently the error that I recieve is:
root [2] .x histogram2.C
Error: ws already declared as different type. ~string() called
histogram2.C:13:
*** Interpreter error recovered ***
As I said above, I am entirely new to root and so any pointers on what might not be possible in root would be greatly appreciated.
Thank you.