1 #include "readTextFile.h"
8 const char* textfile=
"REAL/finish_central_cut1.hist.txt";
9 const char* outfile=
"REAL/finish_central_cut1_new.hist.root";
12 int main(
int argc,
char** argv)
15 char* argvZ[] = {
"-b"};
17 TApplication r00t(
"r00t", &argcZ, argvZ);
20 const char* options =
"i:o:";
22 char textFile[300],outFile[300];
23 while ((chr = getopt(argc, argv, options)) >= 0){
25 case 'i': strcpy(textFile,optarg); i=1;
break;
26 case 'o': strcpy(outFile,optarg); o=1;
break;
30 cout <<
"-i infile -o outfile" << endl;
34 cout <<
"reading textFile=" << textFile << endl;
35 cout <<
"writing to " << outFile << endl;
37 TFile outRoot(outFile,
"RECREATE");
38 if(!outRoot.IsOpen()) {
39 cout <<
"Cannot open " << outFile << endl;
return -1;
41 ifstream iss(textFile);
43 cout <<
"Cannot open " << textFile << endl;
46 int count(0),limit(1000);
47 TString buffer;
char name[100],title[100];
49 char* buf=
new char[bufSz];
50 bool isarray=
false,isnew=
false;
52 int nBin[3]={0},dim(0),iBin=0;
53 float min[3]={0},max[3]={0};
57 iss.getline(buf,bufSz-1);
61 if(strstr(buf,
"name:")){
63 parse(buffer,
"name:");
64 strcpy(name,buffer.Data());
68 for(
int i=0; i<3;i++){
69 nBin[i]=0; min[i]=0; max[i]=0;
71 int stat = h->Write();
73 cout <<
"could not write to " << outFile << endl;
79 if(strstr(buf,
"title:")){
81 parse(buffer,
"title:");
82 strcpy(title,buffer.Data());
85 if(strstr(buf,
"dim:")){
91 if(strstr(buf,
"ary:")){
97 if(strstr(buf,
"bins:")){
99 parse(buffer,
"bins:");
100 nBin[iBin++]=atoi(buffer.Data());
104 if(strstr(buf,
"min:")){
106 parse(buffer,
"min:");
107 min[iBin-1]=atof(buffer.Data());
110 if(strstr(buf,
"max:")){
112 parse(buffer,
"max:");
113 max[iBin-1]=atof(buffer.Data());
116 if(strstr(buf,
"bin")){
120 cout <<
"name=" <<name <<
" title=" << title
121 <<
" dim=" << dim << endl;
124 case 1: h=
new TH1D(name,title,nBin[0],min[0],max[0]);
break;
125 case 2: h=
new TH2D(name,title,
126 nBin[0],min[0],max[0],
127 nBin[1],min[1],max[1]);
break;
128 case 3: h=
new TH3D(name,title,
129 nBin[0],min[0],max[0],
130 nBin[1],min[1],max[1],
131 nBin[2],min[2],max[2]);
break;
132 default: cout <<
"Wrong dim=" << dim << endl; exit(1);
134 for(
int i=0; i<dim; i++){
135 cout <<
"nbin=" << nBin[i] <<
",min="<<min[i] <<
",max=" << max[i] << endl;
139 cout <<
"Cannot handle array bins" << endl; exit(1);
145 buffer.ReplaceAll(
"bin=",
"");
146 strcpy(buf,buffer.Data());
148 char* a = strtok(buf,
" ");
151 double value=atof(a);
153 double error=atof(a);
157 cout <<
"bin=" << bin <<
" value=" << value
158 <<
" error=" << error<<endl;
161 if(!h){cout <<
"h not created?" << endl;
return -1;}
162 h->SetBinContent(bin,value);
163 h->SetBinError(bin,error);
173 cout <<
"done" << endl;
179 void parse(TString& a,
char* b){
180 a.ReplaceAll(b,
""); a.ReplaceAll(
" ",
"");
185 TString split(TString& a){
186 TString temp=a.Data();
187 temp.Replace(temp.First(
' '),temp.Length(),
"");
188 a.Replace(0,a.First(
' ')+1,
"");
192 void removeLead(TString& a){
193 while(a.First(
' ')==0){
198 void removeTrail(TString& a){
199 a.Remove(a.First(
' '));