musrsim/trimsp/TrimSPGUI4/readAscii.C

89 lines
2.0 KiB
C

// File: readAscii.C
// Author: Thomas Prokscha
// Date: 24/04/2005
// Purpose: ROOT macro to read ASCII data from file FileName
// Assume sections
// Comment:
// and
// Data:
// label1 label2 ...
// value1 value2 ...
//
// gROOT->Reset();
void readAscii(char *FileName, Int_t Col1, Int_t Col2)
{
TObjString *ostr;
TString str,xlab,ylab;
TString line, label, treeLabel;
TString rootFileName;
TObjArray *token;
TObjString *strtoken;
Int_t nPars = 0;
Int_t ntokens = 0;
Int_t i = 0;
Int_t nline=0;
Ssiz_t pos;
Double_t x[100],y[100];
FILE *fp = fopen(FileName,"r");
if ( fp == NULL ){
printf("File %s does not exist!\n", FileName);
return;
}
while (line.Gets(fp)){
if ( nline==0 ){
// First line, get data labels
nline++;
token = line.Tokenize(" ");
ntokens = token->GetEntries();
nPars=ntokens;
ostr = dynamic_cast<TObjString*>(token->At(Col1));
xlab = ostr->GetString();
ostr = dynamic_cast<TObjString*>(token->At(Col2));
ylab = ostr->GetString();
} else {
token = line.Tokenize(" ");
ntokens = token->GetEntries();
// if ( ntokens != nPars) {
// // We have a problems
// printf("The number of columns found is: %d not equal to %d\n",ntokens,nPars);
// }
strtoken = (TObjString*) token->At(Col1);
label = strtoken->GetName();
x[i] = label.Atof();
strtoken = (TObjString*) token->At(Col2);
label = strtoken->GetName();
y[i] = label.Atof();
printf("(x,y)[%d]= (%f,%f)\n",i,x[i],y[i]);
nline++;
i++;
}
}
TGraph *gr = new TGraph(i-1,x,y);
TCanvas *c = new TCanvas("c",str);
gr->GetXaxis()->SetTitle(xlab);
gr->GetYaxis()->SetTitle(ylab);
gr->Draw("AC*");
c->Show();
delete token;
// I am not sure what this does, but it waits until canvas is closed
c->WaitPrimitive(" ");
cout << endl << "Canvas Closed" << endl ;
// Then quit root cleanly
gApplication->Terminate();
}