00001 #include "readTextFile.h"
00002
00003
00004
00005
00006
00007
00008 const char* textfile="REAL/finish_central_cut1.hist.txt";
00009 const char* outfile="REAL/finish_central_cut1_new.hist.root";
00010
00011 int debug=0;
00012 int main(int argc,char** argv)
00013 {
00014
00015 char* argvZ[] = {"-b"};
00016 Int_t argcZ = 1;
00017 TApplication r00t("r00t", &argcZ, argvZ);
00018
00019 extern char* optarg;
00020 const char* options = "i:o:";
00021 Int_t chr, i=0,o=0;
00022 char textFile[300],outFile[300];
00023 while ((chr = getopt(argc, argv, options)) >= 0){
00024 switch(chr){
00025 case 'i': strcpy(textFile,optarg); i=1; break;
00026 case 'o': strcpy(outFile,optarg); o=1; break;
00027 }
00028 }
00029 if(!(i*o)){
00030 cout << "-i infile -o outfile" << endl;
00031 exit(-1);
00032 }
00033
00034 cout << "reading textFile=" << textFile << endl;
00035 cout << "writing to " << outFile << endl;
00036
00037 TFile outRoot(outFile,"RECREATE");
00038 if(!outRoot.IsOpen()) {
00039 cout << "Cannot open " << outFile << endl; return -1;
00040 }
00041 ifstream iss(textFile);
00042 if(!iss) {
00043 cout << "Cannot open " << textFile << endl;
00044 return -1;
00045 }
00046 int count(0),limit(1000);
00047 TString buffer; char name[100],title[100];
00048 const int bufSz=100;
00049 char* buf=new char[bufSz];
00050 bool isarray=false,isnew=false;
00051
00052 int nBin[3]={0},dim(0),iBin=0;
00053 float min[3]={0},max[3]={0};
00054 TH1* h=0;
00055 while(!iss.eof()){
00056
00057 iss.getline(buf,bufSz-1);
00058
00059
00060
00061 if(strstr(buf,"name:")){
00062 buffer=buf;
00063 parse(buffer,"name:");
00064 strcpy(name,buffer.Data());
00065 isnew=true;
00066 if(h){
00067 iBin=0;
00068 for(int i=0; i<3;i++){
00069 nBin[i]=0; min[i]=0; max[i]=0;
00070 }
00071 int stat = h->Write();
00072 if(!stat) {
00073 cout << "could not write to " << outFile << endl;
00074 }
00075 delete h;
00076 }
00077 continue;
00078 }
00079 if(strstr(buf,"title:")){
00080 buffer=buf;
00081 parse(buffer,"title:");
00082 strcpy(title,buffer.Data());
00083 continue;
00084 }
00085 if(strstr(buf,"dim:")){
00086 buffer=buf;
00087 parse(buffer,"dim:");
00088 dim=atoi(buffer);
00089 continue;
00090 }
00091 if(strstr(buf,"ary:")){
00092 buffer=buf;
00093 parse(buffer,"ary:");
00094 isarray=atoi(buffer);
00095 continue;
00096 }
00097 if(strstr(buf,"bins:")){
00098 buffer=buf;
00099 parse(buffer,"bins:");
00100 nBin[iBin++]=atoi(buffer.Data());
00101 continue;
00102 }
00103
00104 if(strstr(buf,"min:")){
00105 buffer=buf;
00106 parse(buffer,"min:");
00107 min[iBin-1]=atof(buffer.Data());
00108 continue;
00109 }
00110 if(strstr(buf,"max:")){
00111 buffer=buf;
00112 parse(buffer,"max:");
00113 max[iBin-1]=atof(buffer.Data());
00114 continue;
00115 }
00116 if(strstr(buf,"bin")){
00117 if(isnew){
00118 isnew=false;
00119 if(debug)
00120 cout << "name=" <<name << " title=" << title
00121 << " dim=" << dim << endl;
00122 if(!isarray){
00123 switch(dim){
00124 case 1: h=new TH1D(name,title,nBin[0],min[0],max[0]); break;
00125 case 2: h=new TH2D(name,title,
00126 nBin[0],min[0],max[0],
00127 nBin[1],min[1],max[1]); break;
00128 case 3: h=new TH3D(name,title,
00129 nBin[0],min[0],max[0],
00130 nBin[1],min[1],max[1],
00131 nBin[2],min[2],max[2]); break;
00132 default: cout << "Wrong dim=" << dim << endl; exit(1);
00133 }
00134 for(int i=0; i<dim; i++){
00135 cout << "nbin=" << nBin[i] << ",min="<<min[i] << ",max=" << max[i] << endl;
00136 }
00137 }
00138 else{
00139 cout << "Cannot handle array bins" << endl; exit(1);
00140 }
00141 }
00142
00143
00144 buffer=buf;
00145 buffer.ReplaceAll("bin=","");
00146 strcpy(buf,buffer.Data());
00147
00148 char* a = strtok(buf," ");
00149 int bin=atoi(a);
00150 a=strtok(NULL," ");
00151 double value=atof(a);
00152 a=strtok(NULL," ");
00153 double error=atof(a);
00154
00155
00156 if(debug)
00157 cout << "bin=" << bin << " value=" << value
00158 << " error=" << error<<endl;
00159
00160
00161 if(!h){cout << "h not created?" << endl; return -1;}
00162 h->SetBinContent(bin,value);
00163 h->SetBinError(bin,error);
00164
00165 }
00166
00167
00168
00169 }
00170
00171 h->Write();
00172
00173 cout << "done" << endl;
00174 outRoot.Close();
00175
00176 return 0;
00177 }
00178
00179 void parse(TString& a,char* b){
00180 a.ReplaceAll(b,""); a.ReplaceAll(" ","");
00181 }
00182
00183
00184
00185 TString split(TString& a){
00186 TString temp=a.Data();
00187 temp.Replace(temp.First(' '),temp.Length(),"");
00188 a.Replace(0,a.First(' ')+1,"");
00189 return temp;
00190 }
00191
00192 void removeLead(TString& a){
00193 while(a.First(' ')==0){
00194 a.Remove(0,1);
00195 }
00196 }
00197
00198 void removeTrail(TString& a){
00199 a.Remove(a.First(' '));
00200 }