00001 #include <fstream>
00002
00003 const char* textfile="links/P00hk.nofield.refitOS.undoPTCME.slice/dip5.typec.slice.txt";
00004 const char* outfile="links/P00hk.nofield.refitOS.undoPTCME.slice/dip5.typec.slice.new.root";
00005
00006 int debug=0;
00007 void readTextFile(const char* textFile=textfile,
00008 const char* outFile=outfile)
00009 {
00010 cout << "reading textFile=" << textFile << endl;
00011 cout << "writing " << outFile << endl;
00012
00013 TFile outRoot(outFile,"RECREATE");
00014 if(!outRoot.IsOpen()) return;
00015
00016 ifstream iss(textFile);
00017 if(!iss) return;
00018
00019 int count(0),limit(1000);
00020 TString buffer; char name[100],title[100],buf[500];
00021 bool isarray=false,new=false;
00022 double array[300];
00023 int nBin(0),min(0),max(0);
00024 TH1D* h=0;
00025 while(!iss.eof()){
00026
00027 iss.getline(buf,500);
00028 buffer=buf;
00029
00030 if(debug)cout << "the line is**" << buf << "**" << endl;
00031
00032 if(buffer.Contains("name:")){
00033 parse(buffer,"name:");
00034 strcpy(name,buffer.Data());
00035 new=true;
00036 if(h){
00037 int stat = h->Write();
00038 if(!stat) {
00039 cout << "could not write to " << outFile << endl;
00040 }
00041 delete h;
00042 }
00043 continue;
00044 }
00045 if(buffer.Contains("title:")){
00046 parse(buffer,"title:");
00047 strcpy(title,buffer.Data());
00048 continue;
00049 }
00050 if(buffer.Contains("bins:")){
00051 parse(buffer,"bins:");
00052 nBin=atoi(buffer.Data());
00053 continue;
00054 }
00055 if(buffer.Contains("isarray")){
00056 isarray=true;
00057 iss.getline(buf,500);
00058 buffer=buf;
00059 if(debug)cout << "**" << buffer << "**" << endl;
00060 int k=0;
00061 count=0;
00062 while(1){
00063 TString temp=buffer;
00064 if(!temp.Length()) break;
00065 temp.Replace(temp.First(' '),temp.Length(),"");
00066 array[count++] = atof(temp.Data());
00067 buffer.Replace(0,buffer.First(' ')+1,"");
00068 }
00069 if(debug){
00070 for(int i=0; i<nBin+1; i++){ cout << array[i]<< " "; }
00071 cout << endl;
00072 }
00073
00074 continue;
00075 }
00076 if(buffer.Contains("notarray")){
00077 isarray=false;
00078 iss.getline(buf,500); buffer = buf;
00079 parse(buffer,"min:");
00080 min=atof(buffer.Data());
00081 iss.getline(buf,500); buffer = buf;
00082 parse(buffer,"max:");
00083 max=atof(buffer.Data());
00084 continue;
00085 }
00086 if(buffer.Contains("bin=")){
00087
00088 if(new){
00089 new=false;
00090 if(debug)
00091 cout << "name=" <<name << " title=" << title
00092 << " nbin=" << nBin << endl;
00093 if(isarray){
00094 h=new TH1D(name,title,nBin,array);
00095 }
00096 else{
00097 h=new TH1D(name,title,nBin,min,max);
00098 }
00099 }
00100 buffer.ReplaceAll("bin=","");
00101 TString temp = split(buffer);
00102 int bin=atoi(temp.Data());
00103
00104 buffer.ReplaceAll("value: ","");
00105 temp=split(buffer);
00106 double value=atof(temp.Data());
00107
00108 buffer.ReplaceAll("error: ","");
00109 double error=atof(buffer.Data());
00110
00111 if(debug)
00112 cout << "bin=" << bin << " value=" << value
00113 << " error=" << error<<endl;
00114
00115 if(!h){cout << "h not created?" << endl; return;}
00116 h->SetBinContent(bin,value);
00117 h->SetBinError(bin,error);
00118 }
00119
00120 if(debug && ++count>limit) break;
00121
00122 }
00123 outRoot->Close();
00124
00125 }
00126
00127 void parse(TString& a,char* b){
00128 a.ReplaceAll(b,""); a.ReplaceAll(" ","");
00129 }
00130
00131 TString split(TString& a){
00132 TString temp=a.Data();
00133 temp.Replace(temp.First(' '),temp.Length(),"");
00134 a.Replace(0,a.First(' ')+1,"");
00135 return temp;
00136 }