Manu S Manu S - 7 days ago 6
C++ Question

In data file there are 100 points with 10 columns, the missing data point will have value 999.99. How to read data without 999.99 and polt using c++?

This is the code read the data file. I have to skip data points with values 99999.99, 99999.99 and 999.99. I am using CERN ROOT platform to plot the points. It is simple program, only difficult i am facing is that if statement used in the program if(mag_B<9999.99) with add operator it skips the entire line if There is data in other columns. The program runs fine there is no problem in the code. Only thing i dont know is to how to ignore the missing values in a data file. While plotting it even plots these missing values too. Any help will be appreciated. Thanks

// To compliel the code g++ filename -o objectname `root-config --glibs --cflags`

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "TFile.h"
#include "TNtuple.h"
using namespace std;
int main()
{
int yy,doy,hh,mn;
float mag_B,Bx_GSE,By_GSE,Bz_GSE,By_GSM,Bz_GSM,speed,n,day;
int AL,AU,SymD,SymH;
char fn[20];
ifstream readfile("omni_min_2000.lst");
TFile *fid = new TFile("omni_min_2000.root","RECREATE");
int count =0;
for(int i=1; i<367; i++)
{
sprintf(fn,"tn%d",i);
TNtuple* tn = new TNtuple(fn,"omni_min_2000","doy:day:mag_B:Bx_GSE:By_GSM:Bz_GSM:speed:n:SymH"); // ntuple pointer

while(true)
{
readfile>>yy;
if(readfile.eof())break;
readfile>>doy>>hh>>mn>>mag_B>>Bx_GSE>>By_GSE>>Bz_GSE>>By_GSM>>Bz_GSM>>speed>>n>>AL>>AU>>SymD>>SymH; // reading column in a file
day = doy + (hh + mn/60.0)/24.0; // day of the year
count = count +1;
//i=doy;
if(doy==i)
{
if (mag_B<9999.0 && Bx_GSE<9999.0 && By_GSM<9999.0 && Bz_GSM<9999.0 && speed<99999.0 && n<999.0)
{
tn->Fill(doy,day,mag_B,Bx_GSE,By_GSM,Bz_GSM,speed,n,SymH);
}
}
else { break;}
}
}
cout<<count<<endl;
fid->Write();
fid->Close();
readfile.close();
return 0;
}

Answer

Use || instead of &&, so you only skip the row if all the columns are invalid.

if (mag_B<9999.0 || Bx_GSE<9999.0 || By_GSM<9999.0 | Bz_GSM<9999.0 || speed<99999.0 || n<999.0)
Comments