StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
readTextFile_old.C
1 #include <fstream>
2 
3 const char* textfile="links/P00hk.nofield.refitOS.undoPTCME.slice/dip5.typec.slice.txt";
4 const char* outfile="links/P00hk.nofield.refitOS.undoPTCME.slice/dip5.typec.slice.new.root";
5 
6 int debug=0;
7 void readTextFile(const char* textFile=textfile,
8  const char* outFile=outfile)
9 {
10  cout << "reading textFile=" << textFile << endl;
11  cout << "writing " << outFile << endl;
12 
13  TFile outRoot(outFile,"RECREATE");
14  if(!outRoot.IsOpen()) return;
15 
16  ifstream iss(textFile);
17  if(!iss) return;
18 
19  int count(0),limit(1000);
20  TString buffer; char name[100],title[100],buf[500];
21  bool isarray=false,new=false;
22  double array[300];
23  int nBin(0),min(0),max(0);
24  TH1D* h=0;
25  while(!iss.eof()){
26 
27  iss.getline(buf,500);
28  buffer=buf;
29 
30  if(debug)cout << "the line is**" << buf << "**" << endl;
31 
32  if(buffer.Contains("name:")){
33  parse(buffer,"name:");
34  strcpy(name,buffer.Data());
35  new=true;
36  if(h){
37  int stat = h->Write();
38  if(!stat) {
39  cout << "could not write to " << outFile << endl;
40  }
41  delete h;
42  }
43  continue;
44  }
45  if(buffer.Contains("title:")){
46  parse(buffer,"title:");
47  strcpy(title,buffer.Data());
48  continue;
49  }
50  if(buffer.Contains("bins:")){
51  parse(buffer,"bins:");
52  nBin=atoi(buffer.Data());
53  continue;
54  }
55  if(buffer.Contains("isarray")){
56  isarray=true;
57  iss.getline(buf,500);
58  buffer=buf;
59  if(debug)cout << "**" << buffer << "**" << endl;
60  int k=0;
61  count=0;
62  while(1){
63  TString temp=buffer;
64  if(!temp.Length()) break;
65  temp.Replace(temp.First(' '),temp.Length(),"");
66  array[count++] = atof(temp.Data());
67  buffer.Replace(0,buffer.First(' ')+1,"");
68  }
69  if(debug){
70  for(int i=0; i<nBin+1; i++){ cout << array[i]<< " "; }
71  cout << endl;
72  }
73 
74  continue;
75  }
76  if(buffer.Contains("notarray")){
77  isarray=false;
78  iss.getline(buf,500); buffer = buf;
79  parse(buffer,"min:");
80  min=atof(buffer.Data());
81  iss.getline(buf,500); buffer = buf;
82  parse(buffer,"max:");
83  max=atof(buffer.Data());
84  continue;
85  }
86  if(buffer.Contains("bin=")){
87 
88  if(new){
89  new=false;
90  if(debug)
91  cout << "name=" <<name << " title=" << title
92  << " nbin=" << nBin << endl;
93  if(isarray){
94  h=new TH1D(name,title,nBin,array);
95  }
96  else{
97  h=new TH1D(name,title,nBin,min,max);
98  }
99  }
100  buffer.ReplaceAll("bin=","");
101  TString temp = split(buffer);
102  int bin=atoi(temp.Data());
103 
104  buffer.ReplaceAll("value: ","");
105  temp=split(buffer);
106  double value=atof(temp.Data());
107 
108  buffer.ReplaceAll("error: ","");
109  double error=atof(buffer.Data());
110 
111  if(debug)
112  cout << "bin=" << bin << " value=" << value
113  << " error=" << error<<endl;
114 
115  if(!h){cout << "h not created?" << endl; return;}
116  h->SetBinContent(bin,value);
117  h->SetBinError(bin,error);
118  }
119 
120  if(debug && ++count>limit) break;
121 
122  }
123  outRoot->Close();
124 
125 }
126 
127 void parse(TString& a,char* b){
128  a.ReplaceAll(b,""); a.ReplaceAll(" ","");
129 }
130 
131 TString split(TString& a){
132  TString temp=a.Data();
133  temp.Replace(temp.First(' '),temp.Length(),"");
134  a.Replace(0,a.First(' ')+1,"");
135  return temp;
136 }