#include ; #include ; #include ; #include ; //#include ; using namespace std; // function for opening the txt file which contains the names of the data files ifstream* openFileofFile() { // p2FileStream is a pointer which points to objects of structure type ifstream // ifstream FileStream = ifstream* p2FileStream => p2FileStream=&FileStream ifstream* p2FileStream=0; for(;;) { char file2Name[200]; cout << "$$$ Enter the filename (.txt) of the measurement files. $$$ " << endl; cin >> file2Name; p2FileStream = new ifstream(file2Name); // opens the txt file specified by the user if (p2FileStream->good()) { break; } // if the file opens, the function will return the p2FileStream which points to the correct filename // else you have to enter the correct filename cerr << "couldn't open " << file2Name <get(symbol); } while (symbol!='\n'); } string whiteline (string stringName) { string::size_type start=0; // start is a variable that in this level corresponds to the first position of the string bool commas = true; // in this loop all the commas that there are in the current line (DataLine) // will be substituted by whitespaces and the new string(line) will be saved again in the DataLine while (commas) { // find the position of the first existing comma starting by the position start // (in each first loop start = 0) // and save it to index string::size_type index = stringName.find(',',start); if (index!=string::npos) // if a comma occurs then replace it with a whitespace { stringName.replace(index,1,1,' '); start = index; } // and change start position to current position else { commas = false; } // else return false to exit the loop } return stringName; } // -------------------------------------------------------------------------------------------------------------------------------------- int main() { gROOT->Reset(); char answer; for (;;) { cout << "$$$ Do you want to read a main file? : (y/n) $$$" << endl; cin >> answer; if ((answer=='n') || (answer=='N')) { return 0; } break; } // data variables Double_t HS1,HS2,HS3,COMET,Xentaur1,Xentaur2,WetMirror,Spare; Double_t TIME, TIME0; Double_t VarValue; string VarName; // creating a root file to save the tree and other root procs TFile *fr = new TFile ("datasensor.root","RECREATE"); // creating the tree for the data that come from the csv file TTree *datasensortree = new TTree("T","data from humidity sensors"); // creating the branches respectively to the number of variables // 19 variables // 19 branches datasensortree->Branch("HS1",&HS1,"HS1/D"); datasensortree->Branch("HS2",&HS2,"HS2/D"); datasensortree->Branch("HS3",&HS3,"HS3/D"); datasensortree->Branch("COMET",&COMET,"COMET/D"); datasensortree->Branch("Xentaur1",&Xentaur1,"Xentaur1/D"); datasensortree->Branch("Xentaur2",&Xentaur2,"Xentaur2/D"); datasensortree->Branch("WetMirror",&WetMirror,"WetMirror/D"); datasensortree->Branch("Spare",&Spare,"Spare/D"); datasensortree->Branch("TIME",&TIME,"TIME/D"); ifstream* p2FileStream=openFileofFile(); int numberofiles=0; bool firstVarName=false; // set false for every new file bool firstTimeValue=false; int NumberOfValues=0,Lcounter; while ( (!(p2FileStream->eof()))) { char fileName[200]; (*p2FileStream) >> fileName; // read the filename of a csv file from the txt file ifstream* pFileStream = new ifstream(fileName); if ((pFileStream->fail()) || (fileName[0]=='\n')) // try to read the file with the corresponding filename { cout << "*** Error in the filename at line " << numberofiles+1 << " !!!" << endl; return 0; } numberofiles++; // filename counter // DataLine is a string variable and it is used in order to save the current line in each loop string DataLine; // the first line of the csv file is disregarded because it contains useless information newLine(pFileStream); while (!(pFileStream->eof())) { // we use an internal loop which corresponds to the number of variables in order to have the ability // to fill the different branches of the tree simultaneously Lcounter=NumberOfValues; for (int i=0; i<(8-Lcounter); i++) { getline((*pFileStream),DataLine); // DataLine = current line if (pFileStream->fail()) { break; } // break if the line can't be read //DataLine=whiteline(DataLine); string::size_type start=0; // start is a variable that in this level corresponds to the first position of the string bool commas = true; // in this loop all the commas that there are in the current line (DataLine) // will be substituted by whitespaces and the new string(line) will be saved again in the DataLine while (commas) { // find the position of the first existing comma starting by the position start // (in each first loop start = 0) // and save it to index string::size_type index = DataLine.find(',',start); if (index!=string::npos) // if a comma occurs then replace it with a whitespace { DataLine.replace(index,1,1,' '); start = index; } // and change start position to current position else { commas = false; } // else return false to exit the loop } // necessary types of data that should be read from the csv file // create a variable istringstream, which is a type of an input stream, // so the string can be used like a regular file stream istringstream inpstring (DataLine); inpstring >> VarName; // the first word from the line should be the name of each variable int slen = VarName.length(); VarName=VarName.substr(0,slen); // remove the "" from the VarName if (VarName=="exit") { return 0; } // from every file we will not take into account the first and the last incompleted events // discard the first incompleted event if (firstVarName==false) { while (VarName!="DB444.DewPoint[0]") // "Ref_HSH_0" is the first variable of every event { getline((*pFileStream),DataLine); DataLine=whiteline(DataLine); istringstream inpstring (DataLine); // read VarName until you read the first "Ref_HSH_0" inpstring >> VarName; // the first word from the line should be the name of each variable int slen = VarName.length(); VarName=VarName.substr(0,slen); } firstVarName=true; } // this subfunction functions only once for each file // ignore some whitespaces in order to read the correct variable's value // the TIME variable has a different structure in the data file // so we have to ignore more whitespaces for (int n=0; n<4; n++) { cout << "yes" << '\n'; inpstring.ignore(100,' '); } cout << "yes" << '\n'; inpstring >> VarValue; // read the value // specify which value you are erading and save it in the right system variable if (VarName == "DB444.DewPoint[0]") { HS1 = VarValue; NumberOfValues++; for (int t=0; t<2; t++) { inpstring.ignore(100,' '); } inpstring >> VarValue; // read the value if (firstTimeValue==false) { TIME0=VarValue; firstTimeValue=true; cout << TIME0 << endl; } TIME = (VarValue-TIME0)*24*60*60/(1000000*20); NumberOfValues++; // time in sec but per 20 sec inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[1]") { HS2 = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[2]") { HS3 = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[3]") { COMET = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[4]") { Xentaur1 = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[5]") { Xentaur2 = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[6]") { WetMirror = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line else if (VarName == "DB444.DewPoint[7]") { Spare = VarValue; NumberOfValues++; inpstring.ignore(100,'\n'); } // ignore until the end of the line } // the internal loop ends and the program has read the values // of each variable which have been produced simultaneously by the PLCs if (NumberOfValues==8) // control of the last incomplete event { datasensortree->Fill(); NumberOfValues=0; } // filling the branches } // end while loop } // end do-while loop cout << TIME << endl; datasensortree->Print(); cout << "the program read " << numberofiles << " measurement files and it found " << nentries << " events !!!" << endl; // datasensortree->Scan("TIME"); system ("PAUSE"); return 0; }